summaryrefslogtreecommitdiffstats
path: root/sbin/newfs/newfs.c
diff options
context:
space:
mode:
authorluigi <luigi@FreeBSD.org>2008-12-03 18:36:59 +0000
committerluigi <luigi@FreeBSD.org>2008-12-03 18:36:59 +0000
commit25cd12c1b2ca0757ebe329ef77becf7d56711cd2 (patch)
treec4b9b5c278df89601572732efb3483d2b101edfd /sbin/newfs/newfs.c
parent18b0226daa640e115ac2c6c817f884b4729095df (diff)
downloadFreeBSD-src-25cd12c1b2ca0757ebe329ef77becf7d56711cd2.zip
FreeBSD-src-25cd12c1b2ca0757ebe329ef77becf7d56711cd2.tar.gz
Enable operation of newfs on plain files, which is useful when you
want to prepare disk images for emulators (though 'makefs' in port can do something similar). This relies on: + minor changes to pass the consistency checks even when working on a file; + an additional option, '-p partition' , to specify the disk partition to initialize; + some changes on the I/O routines to deal with partition offsets. The latter was a bit tricky to implement, see the details in newfs.h: in newfs, I/O is done through libufs which assumes that the file descriptor refers to the whole partition. Introducing support for the offset in libufs would require a non-backward compatible change in the library, to be dealt with a version bump or with symbol versioning. I felt both approaches to be overkill for this specific application, especially because there might be other changes to libufs that might become necessary in the near future. So I used the following trick: - read access is always done by calling bread() directly, so we just add the offset in the (few) places that call bread(); - write access is done through bwrite() and sbwrite(), which in turn calls bwrite(). To avoid rewriting sbwrite(), we supply our own version of bwrite() here, which takes precedence over the version in libufs. MFC after: 4 weeks
Diffstat (limited to 'sbin/newfs/newfs.c')
-rw-r--r--sbin/newfs/newfs.c87
1 files changed, 76 insertions, 11 deletions
diff --git a/sbin/newfs/newfs.c b/sbin/newfs/newfs.c
index d5fb66d..269707b 100644
--- a/sbin/newfs/newfs.c
+++ b/sbin/newfs/newfs.c
@@ -139,6 +139,9 @@ u_char *volumelabel = NULL; /* volume label for filesystem */
struct uufsd disk; /* libufs disk structure */
static char device[MAXPATHLEN];
+static u_char bootarea[BBSIZE];
+static int is_file; /* work on a file, not a device */
+static char *dkname;
static char *disktype;
static int unlabeled;
@@ -147,6 +150,18 @@ static struct disklabel *getdisklabel(char *s);
static void rewritelabel(char *s, struct disklabel *lp);
static void usage(void);
+ufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */
+
+/*
+ * need to replace the library's bwrite so that sbwrite uses this one
+ */
+ssize_t
+bwrite(struct uufsd *disk, ufs2_daddr_t blockno, const void *data, size_t size)
+{
+ return pwrite(disk->d_fd, data, size,
+ (off_t)((part_ofs + blockno) * disk->d_bsize));
+}
+
int
main(int argc, char *argv[])
{
@@ -158,7 +173,9 @@ main(int argc, char *argv[])
intmax_t reserved;
int ch, i;
off_t mediasize;
+ char part_name; /* partition name, default to full disk */
+ part_name = 'c';
reserved = 0;
while ((ch = getopt(argc, argv,
"EJL:NO:RS:T:UXa:b:c:d:e:f:g:h:i:lm:no:r:s:")) != -1)
@@ -276,6 +293,11 @@ main(int argc, char *argv[])
*cp != '\0' || reserved < 0)
errx(1, "%s: bad reserved size", optarg);
break;
+ case 'p':
+ is_file = 1;
+ part_name = optarg[0];
+ break;
+
case 's':
errno = 0;
fssize = strtoimax(optarg, &cp, 0);
@@ -294,6 +316,8 @@ main(int argc, char *argv[])
usage();
special = argv[0];
+ if (!special[0])
+ err(1, "empty file/special name");
cp = strrchr(special, '/');
if (cp == 0) {
/*
@@ -303,7 +327,16 @@ main(int argc, char *argv[])
special = device;
}
- if (ufs_disk_fillout_blank(&disk, special) == -1 ||
+ if (is_file) {
+ /* bypass ufs_disk_fillout_blank */
+ bzero( &disk, sizeof(disk));
+ disk.d_bsize = 1;
+ disk.d_name = special;
+ disk.d_fd = open(special, O_RDONLY);
+ if (disk.d_fd < 0 ||
+ (!Nflag && ufs_disk_write(&disk) == -1))
+ errx(1, "%s: ", special);
+ } else if (ufs_disk_fillout_blank(&disk, special) == -1 ||
(!Nflag && ufs_disk_write(&disk) == -1)) {
if (disk.d_error != NULL)
errx(1, "%s: %s", special, disk.d_error);
@@ -312,22 +345,30 @@ main(int argc, char *argv[])
}
if (fstat(disk.d_fd, &st) < 0)
err(1, "%s", special);
- if ((st.st_mode & S_IFMT) != S_IFCHR)
- errx(1, "%s: not a character-special device", special);
+ if ((st.st_mode & S_IFMT) != S_IFCHR) {
+ warn("%s: not a character-special device", special);
+ is_file = 1; /* assume it is a file */
+ dkname = special;
+ if (sectorsize == 0)
+ sectorsize = 512;
+ mediasize = st.st_size;
+ /* set fssize from the partition */
+ } else {
+ part_name = special[strlen(special) - 1];
+ if ((part_name < 'a' || part_name > 'h') && !isdigit(part_name))
+ errx(1, "%s: can't figure out file system partition",
+ special);
- if (sectorsize == 0)
+ if (sectorsize == 0)
if (ioctl(disk.d_fd, DIOCGSECTORSIZE, &sectorsize) == -1)
- sectorsize = 0; /* back out on error for safety */
- if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1)
+ sectorsize = 0; /* back out on error for safety */
+ if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1)
getfssize(&fssize, special, mediasize / sectorsize, reserved);
+ }
pp = NULL;
lp = getdisklabel(special);
if (lp != NULL) {
- cp = strchr(special, '\0');
- cp--;
- if ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))
- errx(1, "%s: can't figure out file system partition",
- special);
+ cp = &part_name;
if (isdigit(*cp))
pp = &lp->d_partitions[RAW_PART];
else
@@ -346,6 +387,8 @@ main(int argc, char *argv[])
fsize = pp->p_fsize;
if (bsize == 0)
bsize = pp->p_frag * pp->p_fsize;
+ if (is_file)
+ part_ofs = pp->p_offset;
}
if (sectorsize <= 0)
errx(1, "%s: no default sector size", special);
@@ -414,6 +457,19 @@ getdisklabel(char *s)
static struct disklabel lab;
struct disklabel *lp;
+ if (is_file) {
+ if (read(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
+ err(4, "cannot read bootarea");
+ if (bsd_disklabel_le_dec(
+ bootarea + (0 /* labeloffset */ +
+ 1 /* labelsoffset */ * sectorsize),
+ &lab, MAXPARTITIONS))
+ errx(1, "no valid label found");
+
+ lp = &lab;
+ return &lab;
+ }
+
if (ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab) != -1)
return (&lab);
unlabeled++;
@@ -432,6 +488,14 @@ rewritelabel(char *s, struct disklabel *lp)
return;
lp->d_checksum = 0;
lp->d_checksum = dkcksum(lp);
+ if (is_file) {
+ bsd_disklabel_le_enc(bootarea + 0 /* labeloffset */ +
+ 1 /* labelsoffset */ * sectorsize, lp);
+ lseek(disk.d_fd, 0, SEEK_SET);
+ if (write(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
+ errx(1, "cannot write label");
+ return;
+ }
if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) == -1)
warn("ioctl (WDINFO): %s: can't rewrite disk label", s);
}
@@ -467,6 +531,7 @@ usage()
fprintf(stderr, "\t-n do not create .snap directory\n");
fprintf(stderr, "\t-m minimum free space %%\n");
fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
+ fprintf(stderr, "\t-p partition name (a..h)\n");
fprintf(stderr, "\t-r reserved sectors at the end of device\n");
fprintf(stderr, "\t-s file system size (sectors)\n");
exit(1);
OpenPOWER on IntegriCloud