1999-09-10 20:41:10 +04:00
|
|
|
/* $NetBSD: bootblks.c,v 1.4 1999/09/10 16:41:10 drochner Exp $ */
|
1997-06-14 02:14:58 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1996
|
|
|
|
* Matthias Drochner. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed for the NetBSD Project
|
|
|
|
* by Matthias Drochner.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
1997-07-21 22:04:35 +04:00
|
|
|
#include <errno.h>
|
1997-06-14 02:14:58 +04:00
|
|
|
#include <err.h>
|
|
|
|
|
|
|
|
#include "installboot.h"
|
|
|
|
|
|
|
|
static char bootblkpath[MAXPATHLEN];
|
1997-07-21 22:04:35 +04:00
|
|
|
static char backuppath[MAXPATHLEN];
|
|
|
|
static char backupext[] = ".bak";
|
|
|
|
static int havebackup;
|
1997-06-14 02:14:58 +04:00
|
|
|
|
|
|
|
ino_t
|
1997-07-21 22:04:35 +04:00
|
|
|
createfileondev(diskdev, bootblkname, bp, size)
|
1997-06-14 02:14:58 +04:00
|
|
|
char *diskdev, *bootblkname;
|
|
|
|
char *bp;
|
1998-07-29 00:10:53 +04:00
|
|
|
unsigned int size;
|
1997-06-14 02:14:58 +04:00
|
|
|
{
|
|
|
|
char *mntpoint;
|
|
|
|
int fd = -1;
|
|
|
|
struct stat statbuf;
|
|
|
|
int allok = 0;
|
|
|
|
|
|
|
|
if ((mntpoint = getmountpoint(diskdev)) == NULL)
|
|
|
|
return ((ino_t) - 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* create file in fs root for bootloader data
|
1997-07-21 22:04:35 +04:00
|
|
|
* try to rename existing file before
|
1997-06-14 02:14:58 +04:00
|
|
|
*/
|
1997-07-21 22:04:35 +04:00
|
|
|
havebackup = 0;
|
1998-07-29 00:10:53 +04:00
|
|
|
(void) sprintf(bootblkpath, "%s/%s", mntpoint, bootblkname);
|
|
|
|
(void) sprintf(backuppath, "%s%s", bootblkpath, backupext);
|
|
|
|
if (rename(bootblkpath, backuppath) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
1997-07-21 22:04:35 +04:00
|
|
|
warn("rename old %s", bootblkpath);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
1998-07-29 00:10:53 +04:00
|
|
|
if (verbose)
|
|
|
|
(void) fprintf(stderr, "renamed %s -> %s\n",
|
|
|
|
bootblkpath, backuppath);
|
1997-07-21 22:04:35 +04:00
|
|
|
havebackup = 1;
|
|
|
|
}
|
1999-09-10 20:41:10 +04:00
|
|
|
fd = open(bootblkpath, O_RDWR | O_CREAT | O_EXCL, 0400);
|
1997-06-14 02:14:58 +04:00
|
|
|
if (fd < 0) {
|
|
|
|
warn("open %s", bootblkpath);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* do the write, flush, get inode number
|
|
|
|
*/
|
|
|
|
if (write(fd, bp, size) < 0) {
|
|
|
|
warn("write %s", bootblkpath);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (fsync(fd) != 0) {
|
|
|
|
warn("fsync: %s", bootblkpath);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &statbuf) != 0) {
|
|
|
|
warn("fstat: %s", bootblkpath);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
allok = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (fd != -1) {
|
1998-07-29 00:10:53 +04:00
|
|
|
(void) close(fd);
|
1997-06-14 02:14:58 +04:00
|
|
|
if (!allok)
|
1998-07-29 00:10:53 +04:00
|
|
|
(void) unlink(bootblkpath);
|
1997-06-14 02:14:58 +04:00
|
|
|
}
|
1998-07-29 00:10:53 +04:00
|
|
|
if (!allok && havebackup)
|
|
|
|
(void) rename(backuppath, bootblkpath);
|
1997-06-14 02:14:58 +04:00
|
|
|
cleanupmount(mntpoint);
|
|
|
|
return (allok ? statbuf.st_ino : (ino_t) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-07-21 22:04:35 +04:00
|
|
|
cleanupfileondev(diskdev, bootblkname, recover)
|
1997-06-14 02:14:58 +04:00
|
|
|
char *diskdev, *bootblkname;
|
1998-07-29 00:10:53 +04:00
|
|
|
int recover;
|
1997-06-14 02:14:58 +04:00
|
|
|
{
|
|
|
|
char *mntpoint;
|
|
|
|
|
1997-07-21 22:04:35 +04:00
|
|
|
/* save some work if nothing to do */
|
1998-07-29 00:10:53 +04:00
|
|
|
if (!(recover || havebackup))
|
1997-07-21 22:04:35 +04:00
|
|
|
return;
|
|
|
|
|
1997-06-14 02:14:58 +04:00
|
|
|
if ((mntpoint = getmountpoint(diskdev)) == NULL)
|
|
|
|
return;
|
|
|
|
|
1998-07-29 00:10:53 +04:00
|
|
|
(void) sprintf(bootblkpath, "%s/%s", mntpoint, bootblkname);
|
|
|
|
(void) sprintf(backuppath, "%s%s", bootblkpath, backupext);
|
1997-07-21 22:04:35 +04:00
|
|
|
|
1998-07-29 00:10:53 +04:00
|
|
|
if (recover) {
|
|
|
|
(void) unlink(bootblkpath);
|
|
|
|
if (havebackup) {
|
|
|
|
(void) fprintf(stderr, "renaming %s -> %s\n",
|
|
|
|
backuppath, bootblkpath);
|
|
|
|
(void) rename(backuppath, bootblkpath);
|
1997-07-21 22:04:35 +04:00
|
|
|
}
|
1998-07-29 00:10:53 +04:00
|
|
|
} else if (havebackup) {
|
|
|
|
if (verbose)
|
|
|
|
(void) fprintf(stderr, "deleting %s\n", backuppath);
|
|
|
|
(void) unlink(backuppath);
|
1997-07-21 22:04:35 +04:00
|
|
|
}
|
1997-06-14 02:14:58 +04:00
|
|
|
|
|
|
|
cleanupmount(mntpoint);
|
|
|
|
}
|