Implement a '-f' flag to teach bsdlabel to work on files instead of

disk partitions.
This commit is contained in:
Luigi Rizzo 2004-03-30 23:15:03 +00:00
parent a6b03f428a
commit 7747c959fa
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=127650
2 changed files with 52 additions and 13 deletions

View File

@ -44,38 +44,44 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl A .Op Fl A
.Ar disk .Op Fl f
.Ar disk/file
.Nm .Nm
.Fl w .Fl w
.Op Fl \&An .Op Fl \&An
.Op Fl B Op Fl b Ar boot .Op Fl B Op Fl b Ar boot
.Op Fl m Ar machine .Op Fl m Ar machine
.Ar disk .Op Fl f
.Ar disk/file
.Op Ar type .Op Ar type
.Nm .Nm
.Fl e .Fl e
.Op Fl \&An .Op Fl \&An
.Op Fl B Op Fl b Ar boot .Op Fl B Op Fl b Ar boot
.Op Fl m Ar machine .Op Fl m Ar machine
.Ar disk .Op Fl f
.Ar disk/file
.Nm .Nm
.Fl R .Fl R
.Op Fl \&An .Op Fl \&An
.Op Fl B Op Fl b Ar boot .Op Fl B Op Fl b Ar boot
.Op Fl m Ar machine .Op Fl m Ar machine
.Ar disk protofile .Op Fl f
.Ar disk/file protofile
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Nm .Nm
utility utility
installs, examines or modifies the installs, examines or modifies the
.Bx .Bx
label on a disk partition. label on a disk partition, or on a file containing a partition image.
In addition, In addition,
.Nm .Nm
can install bootstrap code. can install bootstrap code.
.Ss Disk Device Name .Ss Disk Device Name
When specifying the device, When specifying the device (i.e. when the
.Fl f
option is not used),
the the
.Pa /dev/ .Pa /dev/
path prefix may be omitted; path prefix may be omitted;
@ -91,8 +97,14 @@ label.
If the option is not given, suitable values are set for these fields. If the option is not given, suitable values are set for these fields.
.Pp .Pp
The The
.Fl f
option tells
.Nm
that the program will operate on a file instead of a disk partition.
.Pp
The
.Fl n .Fl n
stops the option stops the
.Nm .Nm
program right before the disk would have been modified, and displays program right before the disk would have been modified, and displays
the result instead of writing it. the result instead of writing it.

View File

@ -139,6 +139,7 @@ enum {
static int disable_write; /* set to disable writing to disk label */ static int disable_write; /* set to disable writing to disk label */
static int is_file; /* work on a file (abs. pathname), "-f" opt. */
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -147,7 +148,7 @@ main(int argc, char *argv[])
int ch, error = 0; int ch, error = 0;
char const *name = 0; char const *name = 0;
while ((ch = getopt(argc, argv, "ABb:em:nRrs:w")) != -1) while ((ch = getopt(argc, argv, "ABb:efm:nRrs:w")) != -1)
switch (ch) { switch (ch) {
case 'A': case 'A':
allfields = 1; allfields = 1;
@ -158,6 +159,9 @@ main(int argc, char *argv[])
case 'b': case 'b':
xxboot = optarg; xxboot = optarg;
break; break;
case 'f':
is_file=1;
break;
case 'm': case 'm':
if (!strcmp(optarg, "i386") || if (!strcmp(optarg, "i386") ||
!strcmp(optarg, "amd64") || !strcmp(optarg, "amd64") ||
@ -213,7 +217,9 @@ main(int argc, char *argv[])
errx(1, "a -m <architecture> option must be specified"); errx(1, "a -m <architecture> option must be specified");
/* Figure out the names of the thing we're working on */ /* Figure out the names of the thing we're working on */
if (argv[0][0] != '/') { if (is_file) {
dkname = specname = argv[0];
} else if (argv[0][0] != '/') {
dkname = argv[0]; dkname = argv[0];
asprintf(&specname, "%s%s", _PATH_DEV, argv[0]); asprintf(&specname, "%s%s", _PATH_DEV, argv[0]);
} else { } else {
@ -393,6 +399,10 @@ writelabel(void)
fd = open(specname, O_RDWR); fd = open(specname, O_RDWR);
if (fd < 0) { if (fd < 0) {
if (is_file) {
warn("cannot open file %s for writing label", specname);
return(1);
}
grq = gctl_get_handle(); grq = gctl_get_handle();
gctl_ro_param(grq, "verb", -1, "write label"); gctl_ro_param(grq, "verb", -1, "write label");
gctl_ro_param(grq, "class", -1, "BSD"); gctl_ro_param(grq, "class", -1, "BSD");
@ -431,6 +441,21 @@ writelabel(void)
return (0); return (0);
} }
static void
get_file_parms(int f)
{
int i;
struct stat sb;
if (fstat(f, &sb) != 0)
err(4, "fstat failed");
i = sb.st_mode & S_IFMT;
if (i != S_IFREG && i != S_IFLNK)
errx(4, "%s is not a valid file or link", specname);
secsize = DEV_BSIZE;
mediasize = sb.st_size;
}
/* /*
* Fetch disklabel for disk. * Fetch disklabel for disk.
* Use ioctl to get label unless -r flag is given. * Use ioctl to get label unless -r flag is given.
@ -446,8 +471,9 @@ readlabel(int flag)
f = open(specname, O_RDONLY); f = open(specname, O_RDONLY);
if (f < 0) if (f < 0)
err(1, specname); err(1, specname);
/* New world order */ if (is_file)
if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || get_file_parms(f);
else if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
(ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
err(4, "cannot get disk geometry"); err(4, "cannot get disk geometry");
} }
@ -1329,8 +1355,9 @@ getvirginlabel(void)
return (NULL); return (NULL);
} }
/* New world order */ if (is_file)
if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) || get_file_parms(f);
else if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
(ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) { (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
close (f); close (f);
return (NULL); return (NULL);