Index: stable/4/sbin/disklabel/disklabel.8 =================================================================== --- stable/4/sbin/disklabel/disklabel.8 (revision 68022) +++ stable/4/sbin/disklabel/disklabel.8 (revision 68023) @@ -1,685 +1,577 @@ .\" Copyright (c) 1987, 1988, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" Symmetric Computer Systems. .\" .\" 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 acknowledgment: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. .\" .\" @(#)disklabel.8 8.2 (Berkeley) 4/19/94 .\" $FreeBSD$ .\" .Dd July 30, 1999 .Dt DISKLABEL 8 .Os FreeBSD .Sh NAME .Nm disklabel .Nd read and write disk pack label .Sh SYNOPSIS .Nm disklabel .Op Fl r .Ar disk .Nm disklabel .Fl w .Op Fl r -.Ar disk Ar disktype/auto +.Ar disk Ar disktype .Oo Ar packid Oc .Nm disklabel .Fl e .Op Fl r .Ar disk .Nm disklabel .Fl R .Op Fl r .Ar disk Ar protofile .Nm disklabel .Op Fl NW .Ar disk .sp .Nm disklabel .Fl B .Oo .Fl b Ar boot1 .Fl s Ar boot2 .Oc .Ar disk -.Oo Ar disktype/auto Oc +.Oo Ar disktype Oc .Nm disklabel .Fl w .Fl B .Oo .Fl b Ar boot1 .Fl s Ar boot2 .Oc -.Ar disk Ar disktype/auto +.Ar disk Ar disktype .Oo Ar packid Oc .Nm disklabel .Fl R .Fl B .Oo .Fl b Ar boot1 .Fl s Ar boot2 .Oc .Ar disk Ar protofile -.Oo Ar disktype/auto Oc +.Oo Ar disktype Oc .Sh DESCRIPTION .Nm Disklabel installs, examines or modifes the label on a disk drive or pack. When writing the label, it can be used to change the drive identification, the disk partitions on the drive, or to replace a damaged label. There are several forms of the command that read (display), install or edit the label on a disk. In addition, .Nm can install bootstrap code. .Ss Raw or in-core label .Pp The disk label is resident close to or at the beginning of each disk partition. For faster access, the kernel maintains a copy in core at all times. By default, most .Nm access the in-core copy of the label. To access the raw (on-disk) copy, use the .Fl r option. This option allows a label to be installed on a disk without kernel support for a label, such as when labels are first installed on a system; it must be used when first installing a label on a disk. The specific effect of .Fl r is described under each command. .Pp .Ss Disk device name .Pp All .Nm disklabel forms require a disk device name, which should always be the raw -device name representing the disk or slice. For example -.Pa da0 -represents the entire disk irregardless of any DOS partitioning, -and -.Pa da0s1 -represents a slice. Some devices, most notably -.Ar ccd , -require that the -.if t ``whole-disk'' (or ``c'') -.if n "whole-disk" (or "c") -partition be specified. For example -.Pa ccd0c . -You do not have to include the -.Pa /dev/ -path prefix when specifying the device. +.if t ``complete'' (or ``c'') +.if n "complete" (or "c") +partition, for example +.Pa /dev/da0c . .Nm -will automatically prepend it. +understands the abbreviation +.Pa da0 , +which it converts internally to +.Pa /dev/da0c . .Ss Reading the disk label .Pp To examine or save the label on a disk drive, use .Nm without options: .Pp .Nm disklabel .Op Fl r .Ar disk .Pp .Ar disk represents the raw disk in question, and may be in the form .Pa da0 or .Pa /dev/da0c . It will display all of the parameters associated with the drive and its partition layout. Unless the .Fl r flag is given, the kernel's in-core copy of the label is displayed; if the disk has no label, or the partition types on the disk are incorrect, the kernel may have constructed or modified the label. If the .Fl r flag is given, .Nm -reads the label from the raw disk and displays it. Both versions are usually -identical except in the case where a label has not yet been initialized or -is corrupt. +reads the label from the raw disk and displays it. .Ss Writing a standard label .Pp To write a standard label, use the form .Pp .Nm disklabel .Fl w .Op Fl r -.Ar disk Ar disktype/auto +.Ar disk Ar disktype .Oo Ar packid Oc .Pp -.Nm disklabel -.Fl w -.Op Fl r -.Ar disk -auto -.Pp The required arguments to .Nm are the drive to be labeled and the drive type as described in the .Pa disktab(5) file. The drive parameters and partitions are taken from that file. If different disks of the same physical type are to have different partitions, it will be necessary to have separate disktab entries describing each, or to edit the label after installation as described below. The optional argument is a pack identification string, up to 16 characters long. The pack id must be quoted if it contains blanks. If the .Fl r flag is given, the disk sectors containing the label and bootstrap will be written directly. A side-effect of this is that any existing bootstrap code will be overwritten and the disk rendered unbootable. See the boot options below for a method of writing the label and the bootstrap at the same time. If .Fl r is not specified, the existing label will be updated via the in-core copy and any bootstrap code will be unaffected. If the disk does not already have a label, the .Fl r flag must be used. In either case, the kernel's in-core label is replaced. .Pp For a virgin disk that is not known to .Xr disktab 5 , .Ar disktype can be specified as .Dq auto . In this case, the driver is requested to produce a virgin label for the disk. This might or might not be successful, depending on whether the driver for the disk is able to get the required data without reading anything from the disk at all. It will likely succeed for all SCSI disks, most IDE disks, and vnode devices. Writing a label to the disk is the only supported operation, and the .Ar disk itself must be provided as the canonical name, i.e. not as a full path name. -.Pp -PC-based systems have special requirements in order for the BIOS to properly -recognize a FreeBSD disklabel. Older systems may require what is known as -a ``dangerously dedicated'' -disklabel, which creates a fake DOS partition to work around problems older -BIOSes have with modern disk geometries. On newer systems you generally want -to create a normal DOS slice using -.Ar fdisk -and then create a FreeBSD disklabel within that slice. This is described -later on in this page. -.Pp -Installing a new disklabel does not in of itself allow your system to boot -a kernel using that label. You must also install boot blocks, which is -described later on in this manual page. .Ss Editing an existing disk label .Pp To edit an existing disk label, use the form .Pp .Nm disklabel .Fl e .Op Fl r .Ar disk .Pp This command reads the label from the in-core kernel copy, or directly from the disk if the .Fl r flag is also specified. The label is written to a file in ASCII and then supplied to an editor for changes. If no editor is specified in an .Ev EDITOR environment variable, .Xr vi 1 is used. When the editor terminates, the label file is used to rewrite the disk label. Existing bootstrap code is unchanged regardless of whether .Fl r was specified. .Ss Restoring a disk label from a file .Pp To restore a disk label from a file, use the form .Pp .Nm disklabel .Fl R .Op Fl r .Ar disk Ar protofile .Pp .Nm is capable of restoring a disk label that was previously saved in a file ASCII. The prototype file used to create the label should be in the same format as that produced when reading or editing a label. Comments are delimited by .Ar \&# and newline. As when writing a new label, any existing bootstrap code will be clobbered if .Fl r is specified and will be unaffected otherwise. See the boot options below for a method of restoring the label and writing the bootstrap at the same time. .Ss Enabling and disabling writing to the disk label area .Pp By default, it is not possible to write to the disk label area at the beginning of a disk. The disk driver silently ignores any attempt to do so. If you need to write to this area (for example, to obliterate the label), use the form .Pp .Nm disklabel .Op Fl W .Ar disk .Pp To disallow writing to the label area after previously allowing it, use the command .Pp .Nm disklabel .Op Fl N .Ar disk .Ss Installing bootstraps .Pp The final three forms of .Nm -are used to install bootstrap code. If you are creating a -``dangerously-dedicated'' partition for compatibility with older PC systems, -you generally want to specify the raw disk name such as -.Pa da0. -If you are creating a label within an existing DOS slice, you should specify -the slice name such as -.Pa da0s1 . -Making a partition bootable can be tricky. If you are using a normal DOS -slice you typically install (or leave) a standard MBR on the base disk and -then install the FreeBSD bootblocks in the slice. +are used to install bootstrap code: .Pp .Nm disklabel .Fl B .Oo .Fl b Ar boot1 .Fl s Ar boot2 .Oc .Ar disk .Oo Ar disktype Oc .Pp This form installs the bootstrap only. It does not change the disk label. -You should never use this command on a base disk unless you intend to create -a ``dangerously-dedicated'' disk, such as -.Ar da0 . -This command is typically run on a slice such as -.Ar da0s1 . .Pp .Nm disklabel .Fl w .Fl B .Oo .Fl b Ar boot1 .Fl s Ar boot2 .Oc .Ar disk Ar disktype .Oo Ar packid Oc .Pp .if t This form corresponds to the ``write label'' command described above. .if n This form corresponds to the "write label" command described above. In addition to writing a new volume label, it also installs the bootstrap. -If run on a base disk this command will create a ``dangerously-dedicated'' -label. This command is normally run on a slice rather then a base disk. .Pp .Nm disklabel .Fl R .Fl B .Oo .Fl b Ar boot1 .Fl s Ar boot2 .Oc .Ar disk Ar protofile .Oo Ar disktype Oc .Pp .if t This form corresponds to the ``restore label'' command described above. .if n This form corresponds to the "restore label" command described above. In addition to restoring the volume label, it also installs the bootstrap. -If run on a base disk this command will create a ``dangerously-dedicated'' -label. This command is normally run on a slice rather then a base disk. .Pp The bootstrap commands always access the disk directly, so it is not necessary to specify the .Fl r flag. .Pp The bootstrap code is comprised of two boot programs. Specify the name of the boot programs to be installed in one of these ways: .Bl -enum .It Specify the names explicitly with the .Fl b and .Fl s flags. .Fl b indicates the primary boot program and .Fl s the secondary boot program. The boot programs are located in .Pa /boot . .It If the .Fl b and .Fl s flags are not specified, but .Ar disktype was specified, the names of the programs are taken from the .if t ``b0'' and ``b1'' .if n "b0" and "b1" parameters of the .Xr disktab 5 entry for the disk if the disktab entry exists and includes those parameters. .It Otherwise, the default boot image names are used: .Pa /boot/boot1 and .Pa /boot/boot2 for the standard stage1 and stage2 boot images (details may vary on architectures like the Alpha, where only a single-stage boot is used). .El -.Ss Initializing/Formatting a bootable disk from scratch -.Pp -To initialize a disk from scratch the following sequence is recommended. -Please note that this will wipe everything that was previously on the disk, -including any non-FreeBSD slices. -.Bl -enum -.It -Use -.Ar fdisk -to initialize the DOS partition table, creating a real whole-disk slice to -hold the FreeBSD disklabel, and installing a master boot record. -.It -Use -.Ar disklabel -to initialize a virgin FreeBSD disklabel and install FreeBSD boot blocks . -.It -Use -.Ar disklabel -to edit your newly created label, addiing appropriate partitions. -.It -Finally newfs the filesystem partitions you created in the label. A typical -disklabel partiioning scheme would be to have an ``a'' partition -of approximately 128MB to hold the root filesystem, a ``b'' partition for -swap, a ``d'' partition for /var (usually 128MB), an ``e'' partition -for /var/tmp (usually 128MB), an ``f'' partition for /usr (usually around 2G), -and finally a ``g'' partition for /home (usally all remaining space). -Your mileage may vary. -.El -.Pp -.Nm fdisk -BI da0 -.Pp -.Nm disklabel -.Fl w -.Fl r -.Fl B -.Ar da0s1 -auto -.Pp -.Pp -.Nm disklabel -.Fl e -.Ar da0s1 -.Pp .Sh FILES .Bl -tag -width Pa -compact .It Pa /etc/disktab .It Pa /boot/ .It Pa /boot/boot .El .Sh SAVED FILE FORMAT .Nm uses an ASCII version of the label when examining, editing or restoring a disk label. The format is: .Bd -literal -offset 4n # /dev/da1c: type: SCSI disk: da0s1 label: flags: bytes/sector: 512 sectors/track: 51 tracks/cylinder: 19 sectors/cylinder: 969 cylinders: 1211 sectors/unit: 1173930 rpm: 3600 interleave: 1 trackskew: 0 cylinderskew: 0 headswitch: 0 # milliseconds track-to-track seek: 0 # milliseconds drivedata: 0 8 partitions: # size offset fstype [fsize bsize bps/cpg] a: 81920 0 4.2BSD 1024 8192 16 # (Cyl. 0 - 84*) b: 160000 81920 swap # (Cyl. 84* - 218*) c: 1173930 0 unused 0 0 # (Cyl. 0 - 1211*) h: 962010 211920 vinum # (Cyl. 218*- 1211*) .Ed .Pp Lines starting with a # mark are comments. Most of the other specifications are no longer used. The ones which must still be set correctly are: .Pp .Bl -hang -width 20n .It Nm label is an optional label, set by the .Ar packid option when writing a label. .It Nm flags Flags may be .Ar removable , .Ar ecc or .Ar badsect . .Ar removable is set for removable media drives, but no current FreeBSD driver evaluates this flag. .Ar ecc is no longer supported; .Ar badsect specifies that the drive can perform bad sector remapping. .It Nm sectors/unit describes the total size of the disk. This value must be correct. .It Nm the partition table This is the UNIX partition table, not the Microsoft partition table described in .Xr fdisk 8 . .El .Pp The partition table can have up to 8 entries. It contains the following information: .Bl -hang -width 10n .It identifier The partition identifier is a single letter in the range .Nm a to .Nm h . By convention, partition .Nm c is reserved to describe the entire disk. .It size is the size of the partition in sectors. .It offset is the offset of the start of the partition from the beginning of the drive. .It fstype describes the purpose of the partition. The example shows most normal usages. For UFS file systems, use type 4.2BSD. See .Pa /usr/include/sys/disklabel.h for a complete list. .It fsize For file systems only, the fragment size. .It bsize For file systems only, the block size. .It bps/cpg For UFS file systems, the number of cylinders in a cylinder group. For LFS file systems, the segment shift value. .El The remainder of the line is a comment and shows the cylinder allocations based on the obsolete (but possibly correct) geometry information about the drive. The asterisk (*) indicates that the partition does not begin or end exactly on a cylinder boundary. .Sh EXAMPLES .Dl disklabel da0 .Pp Display the in-core label for -.Pa da0s1 +.Pa da0 as obtained via -.Pa /dev/da0s1 . -When reading a label, FreeBSD will allow you to specify the base disk name -even if the label resides on a slice. However, to be proper you should -specify the base disk name only if you are using a ``dangerously-dedicated'' -label. Normally you specify the slice. +.Pa /dev/da0c . .Pp -.Dl disklabel da0s1 > savedlabel +.Dl disklabel da0 > savedlabel .Pp Save the in-core label for -.Pa da0s1 +.Pa da0 into the file .Pa savedlabel . This file can be used with the .Fl R flag to restore the label at a later date. .Pp -.Dl disklabel -w -r /dev/da0s1 da2212 foo +.Dl disklabel -w -r /dev/da0c da2212 foo .Pp Create a label for -.Pa da0s1 +.Pa da0 based on information for .if t ``da2212'' found in .if n "da2212" found in .Pa /etc/disktab . Any existing bootstrap code will be clobbered. .Pp -.Dl disklabel -e -r da0s1 +.Dl disklabel -e -r da0 .Pp Read the on-disk label for -.Pa da0s1 , +.Pa da0 , edit it and reinstall in-core as well as on-disk. Existing bootstrap code is unaffected. .Pp -.Dl disklabel -r -w da0s1 auto +.Dl disklabel -r -w da0 auto .Pp Try to auto-detect the required information from -.Pa da0s1 , +.Pa da0 , and write a new label to the disk. Use another disklabel -e command to edit the partitioning and file system information. .Pp -.Dl disklabel -R da0s1 savedlabel +.Dl disklabel -R da0 savedlabel .Pp Restore the on-disk and in-core label for -.Pa da0s1 +.Pa da0 from information in .Pa savedlabel . Existing bootstrap code is unaffected. .Pp -.Dl disklabel -B da0s1 +.Dl disklabel -B da0 .Pp Install a new bootstrap on -.Pa da0s1 . +.Pa da0 . The boot code comes from .Pa /boot/boot1 and possibly .Pa /boot/boot2 . On-disk and in-core labels are unchanged. .Pp -.Dl disklabel -w -B /dev/da0s1 -b newboot1 -s newboot da2212 +.Dl disklabel -w -B /dev/da0c -b newboot1 -s newboot da2212 .Pp Install a new label and bootstrap. .if t The label is derived from disktab information for ``da2212'' and .if n The label is derived from disktab information for "da2212" and installed both in-core and on-disk. The bootstrap code comes from the files .Pa /boot/newboot1 and .Pa /boot/newboot2 . -.Pp -.Dl dd if=/dev/zero of=/dev/da0 bs=512 count=32 -.Dl fdisk -BI da0 -.Dl dd if=/dev/zero of=/dev/da0s1 bs=512 count=32 -.Dl disklabel -w -r -B da0s1 auto -.Dl disklabel -e da0s1 -.Pp -Completely wipe any prior information on the disk, creating a new bootable -disk with a DOS partition table containing one ``whole-disk'' slice. Then -initialize the slice, then edit it to your needs. The -.Pa dd -commands are optional, but may be necessary for some BIOSes to properly -recognize the disk. .Sh SEE ALSO .Xr disklabel 5 , .Xr disktab 5 , .Xr boot0cfg 8 , .Xr fdisk 8 .Sh DIAGNOSTICS The kernel device drivers will not allow the size of a disk partition to be decreased or the offset of a partition to be changed while it is open. Some device drivers create a label containing only a single large partition if a disk is unlabeled; thus, the label must be written to the .if t ``a'' .if n "a" partition of the disk while it is open. This sometimes requires the desired label to be set in two steps, the first one creating at least one other partition, and the second setting the label on the new partition while shrinking the .if t ``a'' .if n "a" partition. .Pp On some machines the bootstrap code may not fit entirely in the area allocated for it by some filesystems. As a result, it may not be possible to have filesystems on some partitions .if t of a ``bootable'' disk. .if n of a "bootable" disk. When installing bootstrap code, .Nm checks for these cases. If the installed boot code would overlap a partition of type FS_UNUSED it is marked as type FS_BOOT. The .Xr newfs 8 utility will disallow creation of filesystems on FS_BOOT partitions. Conversely, if a partition has a type other than FS_UNUSED or FS_BOOT, .Nm will not install bootstrap code that overlaps it. .Sh BUGS When a disk name is given without a full pathname, .if t the constructed device name uses the ``c'' partition. .if n the constructed device name uses the "c" partition. .Pp For the i386 architecture, the primary bootstrap sector contains an embedded .Em fdisk table. .Nm Disklabel takes care to not clobber it when installing a bootstrap only .Pq Fl B , or when editing an existing label .Pq Fl e , but it unconditionally writes the primary bootstrap program onto the disk for .Fl w or .Fl R , thus replacing the .Em fdisk table by the dummy one in the bootstrap program. This is only of concern if the disk is fully dedicated, so that the BSD disklabel starts at absolute block 0 on the disk. .Pp .Nm does not perform adequate error checking. No warning is given if partitions overlap, nor if space remains unused. Index: stable/4/sbin/disklabel/disklabel.c =================================================================== --- stable/4/sbin/disklabel/disklabel.c (revision 68022) +++ stable/4/sbin/disklabel/disklabel.c (revision 68023) @@ -1,1467 +1,1460 @@ /* * Copyright (c) 1987, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Symmetric Computer Systems. * * 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 by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1987, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94"; /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */ #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #define DKTYPENAMES #include #include #include #include #include #include #include #include #include #include #include #include "pathnames.h" /* * Disklabel: read and write disklabels. * The label is usually placed on one of the first sectors of the disk. * Many machines also place a bootstrap in the same area, * in which case the label is embedded in the bootstrap. * The bootstrap source must leave space at the proper offset * for the label on such machines. */ #ifndef BBSIZE #define BBSIZE 8192 /* size of boot area, with label */ #endif #ifdef tahoe #define NUMBOOT 0 #else #if defined(__alpha__) || defined(hp300) || defined(hp800) #define NUMBOOT 1 #else #define NUMBOOT 2 #endif #endif void makelabel __P((char *, char *, struct disklabel *)); int writelabel __P((int, char *, struct disklabel *)); void l_perror __P((char *)); struct disklabel * readlabel __P((int)); struct disklabel * makebootarea __P((char *, struct disklabel *, int)); void display __P((FILE *, struct disklabel *)); int edit __P((struct disklabel *, int)); int editit __P((void)); char * skip __P((char *)); char * word __P((char *)); int getasciilabel __P((FILE *, struct disklabel *)); int checklabel __P((struct disklabel *)); void setbootflag __P((struct disklabel *)); void Warning (char *, ...); void usage __P((void)); extern u_short dkcksum __P((struct disklabel *)); struct disklabel * getvirginlabel __P((void)); #define DEFEDITOR _PATH_VI #define streq(a,b) (strcmp(a,b) == 0) char *dkname; char *specname; char tmpfil[] = PATH_TMPFILE; char namebuf[BBSIZE], *np = namebuf; struct disklabel lab; char bootarea[BBSIZE]; #if NUMBOOT > 0 int installboot; /* non-zero if we should install a boot program */ char *bootbuf; /* pointer to buffer with remainder of boot prog */ int bootsize; /* size of remaining boot program */ char *xxboot; /* primary boot */ char *bootxx; /* secondary boot */ char boot0[MAXPATHLEN]; char boot1[MAXPATHLEN]; #endif enum { UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT } op = UNSPEC; int rflag; #ifdef DEBUG int debug; #define OPTIONS "BNRWb:ders:w" #else #define OPTIONS "BNRWb:ers:w" #endif int main(argc, argv) int argc; char *argv[]; { register struct disklabel *lp; FILE *t; int ch, f = 0, flag, error = 0; char *name = 0; while ((ch = getopt(argc, argv, OPTIONS)) != -1) switch (ch) { #if NUMBOOT > 0 case 'B': ++installboot; break; case 'b': xxboot = optarg; break; #if NUMBOOT > 1 case 's': bootxx = optarg; break; #endif #endif case 'N': if (op != UNSPEC) usage(); op = NOWRITE; break; case 'R': if (op != UNSPEC) usage(); op = RESTORE; break; case 'W': if (op != UNSPEC) usage(); op = WRITEABLE; break; case 'e': if (op != UNSPEC) usage(); op = EDIT; break; case 'r': ++rflag; break; case 'w': if (op != UNSPEC) usage(); op = WRITE; break; #ifdef DEBUG case 'd': debug++; break; #endif case '?': default: usage(); } argc -= optind; argv += optind; #if NUMBOOT > 0 if (installboot) { rflag++; if (op == UNSPEC) op = WRITEBOOT; } else { if (op == UNSPEC) op = READ; xxboot = bootxx = 0; } #else if (op == UNSPEC) op = READ; #endif if (argc < 1) usage(); dkname = argv[0]; if (dkname[0] != '/') { (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART); specname = np; np += strlen(specname) + 1; } else specname = dkname; f = open(specname, op == READ ? O_RDONLY : O_RDWR); if (f < 0 && errno == ENOENT && dkname[0] != '/') { (void)sprintf(specname, "%s%s", _PATH_DEV, dkname); np = namebuf + strlen(specname) + 1; f = open(specname, op == READ ? O_RDONLY : O_RDWR); } if (f < 0) err(4, "%s", specname); switch(op) { case UNSPEC: break; case EDIT: if (argc != 1) usage(); lp = readlabel(f); error = edit(lp, f); break; case NOWRITE: flag = 0; if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) err(4, "ioctl DIOCWLABEL"); break; case READ: if (argc != 1) usage(); lp = readlabel(f); display(stdout, lp); error = checklabel(lp); break; case RESTORE: #if NUMBOOT > 0 if (installboot && argc == 3) { makelabel(argv[2], 0, &lab); argc--; /* * We only called makelabel() for its side effect * of setting the bootstrap file names. Discard * all changes to `lab' so that all values in the * final label come from the ASCII label. */ bzero((char *)&lab, sizeof(lab)); } #endif if (argc != 2) usage(); if (!(t = fopen(argv[1], "r"))) err(4, "%s", argv[1]); if (!getasciilabel(t, &lab)) exit(1); lp = makebootarea(bootarea, &lab, f); *lp = lab; error = writelabel(f, bootarea, lp); break; case WRITE: if (argc == 3) { name = argv[2]; argc--; } if (argc != 2) usage(); makelabel(argv[1], name, &lab); lp = makebootarea(bootarea, &lab, f); *lp = lab; if (checklabel(lp) == 0) error = writelabel(f, bootarea, lp); break; case WRITEABLE: flag = 1; if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0) err(4, "ioctl DIOCWLABEL"); break; #if NUMBOOT > 0 case WRITEBOOT: { struct disklabel tlab; lp = readlabel(f); tlab = *lp; if (argc == 2) makelabel(argv[1], 0, &lab); lp = makebootarea(bootarea, &lab, f); *lp = tlab; if (checklabel(lp) == 0) error = writelabel(f, bootarea, lp); break; } #endif } exit(error); } /* * Construct a prototype disklabel from /etc/disktab. As a side * effect, set the names of the primary and secondary boot files * if specified. */ void makelabel(type, name, lp) char *type, *name; register struct disklabel *lp; { register struct disklabel *dp; if (strcmp(type, "auto") == 0) dp = getvirginlabel(); else dp = getdiskbyname(type); if (dp == NULL) errx(1, "%s: unknown disk type", type); *lp = *dp; #if NUMBOOT > 0 /* * Set bootstrap name(s). * 1. If set from command line, use those, * 2. otherwise, check if disktab specifies them (b0 or b1), * 3. otherwise, makebootarea() will choose ones based on the name * of the disk special file. E.g. /dev/ra0 -> raboot, bootra */ if (!xxboot && lp->d_boot0) { if (*lp->d_boot0 != '/') (void)sprintf(boot0, "%s/%s", _PATH_BOOTDIR, lp->d_boot0); else (void)strcpy(boot0, lp->d_boot0); xxboot = boot0; } #if NUMBOOT > 1 if (!bootxx && lp->d_boot1) { if (*lp->d_boot1 != '/') (void)sprintf(boot1, "%s/%s", _PATH_BOOTDIR, lp->d_boot1); else (void)strcpy(boot1, lp->d_boot1); bootxx = boot1; } #endif #endif /* d_packname is union d_boot[01], so zero */ bzero(lp->d_packname, sizeof(lp->d_packname)); if (name) (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname)); } int writelabel(f, boot, lp) int f; char *boot; register struct disklabel *lp; { int flag; #ifdef __alpha__ u_long *p, sum; int i; #endif #ifdef vax register int i; #endif setbootflag(lp); lp->d_magic = DISKMAGIC; lp->d_magic2 = DISKMAGIC; lp->d_checksum = 0; lp->d_checksum = dkcksum(lp); if (rflag) { /* * First set the kernel disk label, * then write a label to the raw disk. * If the SDINFO ioctl fails because it is unimplemented, * keep going; otherwise, the kernel consistency checks * may prevent us from changing the current (in-core) * label. */ if (ioctl(f, DIOCSDINFO, lp) < 0 && errno != ENODEV && errno != ENOTTY) { l_perror("ioctl DIOCSDINFO"); return (1); } (void)lseek(f, (off_t)0, SEEK_SET); #ifdef __alpha__ /* * Generate the bootblock checksum for the SRM console. */ for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++) sum += p[i]; p[63] = sum; #endif /* * write enable label sector before write (if necessary), * disable after writing. */ flag = 1; if (ioctl(f, DIOCWLABEL, &flag) < 0) warn("ioctl DIOCWLABEL"); if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) { warn("write"); return (1); } #if NUMBOOT > 0 /* * Output the remainder of the disklabel */ if (bootbuf && write(f, bootbuf, bootsize) != bootsize) { warn("write"); return(1); } #endif flag = 0; (void) ioctl(f, DIOCWLABEL, &flag); } else if (ioctl(f, DIOCWDINFO, lp) < 0) { l_perror("ioctl DIOCWDINFO"); return (1); } #ifdef vax if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) { daddr_t alt; alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors; for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) { (void)lseek(f, (off_t)((alt + i) * lp->d_secsize), SEEK_SET); if (write(f, boot, lp->d_secsize) < lp->d_secsize) warn("alternate label %d write", i/2); } } #endif return (0); } void l_perror(s) char *s; { switch (errno) { case ESRCH: warnx("%s: no disk label on disk;", s); fprintf(stderr, "use \"disklabel -r\" to install initial label\n"); break; case EINVAL: warnx("%s: label magic number or checksum is wrong!", s); fprintf(stderr, "(disklabel or kernel is out of date?)\n"); break; case EBUSY: warnx("%s: open partition would move or shrink", s); break; case EXDEV: warnx("%s: '%c' partition must start at beginning of disk", s, 'a' + RAW_PART); break; default: warn((char *)NULL); break; } } /* * Fetch disklabel for disk. * Use ioctl to get label unless -r flag is given. */ struct disklabel * readlabel(f) int f; { register struct disklabel *lp; if (rflag) { if (read(f, bootarea, BBSIZE) < BBSIZE) err(4, "%s", specname); for (lp = (struct disklabel *)bootarea; lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp)); lp = (struct disklabel *)((char *)lp + 16)) if (lp->d_magic == DISKMAGIC && lp->d_magic2 == DISKMAGIC) break; if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) || lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC || dkcksum(lp) != 0) errx(1, "bad pack magic number (label is damaged, or pack is unlabeled)"); } else { lp = &lab; if (ioctl(f, DIOCGDINFO, lp) < 0) err(4, "ioctl DIOCGDINFO"); } return (lp); } /* * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' * Returns a pointer to the disklabel portion of the bootarea. */ struct disklabel * makebootarea(boot, dp, f) char *boot; register struct disklabel *dp; int f; { struct disklabel *lp; register char *p; int b; #if NUMBOOT > 0 char *dkbasename; struct stat sb; #endif #ifdef __alpha__ u_long *bootinfo; int n; #endif #ifdef __i386__ char *tmpbuf; int i, found; #endif /* XXX */ if (dp->d_secsize == 0) { dp->d_secsize = DEV_BSIZE; dp->d_bbsize = BBSIZE; } lp = (struct disklabel *) (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET); bzero((char *)lp, sizeof *lp); #if NUMBOOT > 0 /* * If we are not installing a boot program but we are installing a * label on disk then we must read the current bootarea so we don't * clobber the existing boot. */ if (!installboot) { if (rflag) { if (read(f, boot, BBSIZE) < BBSIZE) err(4, "%s", specname); bzero((char *)lp, sizeof *lp); } return (lp); } /* * We are installing a boot program. Determine the name(s) and * read them into the appropriate places in the boot area. */ if (!xxboot || !bootxx) { dkbasename = np; if ((p = rindex(dkname, '/')) == NULL) p = dkname; else p++; while (*p && !isdigit(*p)) *np++ = *p++; *np++ = '\0'; if (!xxboot) { (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR); xxboot = boot0; } #if NUMBOOT > 1 if (!bootxx) { (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR); bootxx = boot1; } #endif } #ifdef DEBUG if (debug) fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n", xxboot, bootxx ? bootxx : "NONE"); #endif /* * Strange rules: * 1. One-piece bootstrap (hp300/hp800) * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest * is remembered and written later following the bootarea. * 2. Two-piece bootstraps (vax/i386?/mips?) * up to d_secsize bytes of ``xxboot'' go in first d_secsize * bytes of bootarea, remaining d_bbsize-d_secsize filled * from ``bootxx''. */ b = open(xxboot, O_RDONLY); if (b < 0) err(4, "%s", xxboot); #if NUMBOOT > 1 #ifdef __i386__ /* * XXX Botch alert. * The i386 has the so-called fdisk table embedded into the * primary bootstrap. We take care to not clobber it, but * only if it does already contain some data. (Otherwise, * the xxboot provides a template.) */ if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0) err(4, "%s", xxboot); memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize); #endif /* i386 */ if (read(b, boot, (int)dp->d_secsize) < 0) err(4, "%s", xxboot); (void)close(b); #ifdef __i386__ for (i = DOSPARTOFF, found = 0; !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition); i++) found = tmpbuf[i] != 0; if (found) memcpy((void *)&boot[DOSPARTOFF], (void *)&tmpbuf[DOSPARTOFF], NDOSPART * sizeof(struct dos_partition)); free(tmpbuf); #endif /* i386 */ b = open(bootxx, O_RDONLY); if (b < 0) err(4, "%s", bootxx); if (fstat(b, &sb) != 0) err(4, "%s", bootxx); if (dp->d_secsize + sb.st_size > dp->d_bbsize) errx(4, "%s too large", bootxx); if (read(b, &boot[dp->d_secsize], (int)(dp->d_bbsize-dp->d_secsize)) < 0) err(4, "%s", bootxx); #else /* !(NUMBOOT > 1) */ #ifdef __alpha__ /* * On the alpha, the primary bootstrap starts at the * second sector of the boot area. The first sector * contains the label and must be edited to contain the * size and location of the primary bootstrap. */ n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize); if (n < 0) err(4, "%s", xxboot); bootinfo = (u_long *)(boot + 480); bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize; bootinfo[1] = 1; /* start at sector 1 */ bootinfo[2] = 0; /* flags (must be zero) */ #else /* !__alpha__ */ if (read(b, boot, (int)dp->d_bbsize) < 0) err(4, "%s", xxboot); #endif /* __alpha__ */ if (fstat(b, &sb) != 0) err(4, "%s", xxboot); bootsize = (int)sb.st_size - dp->d_bbsize; if (bootsize > 0) { /* XXX assume d_secsize is a power of two */ bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1); bootbuf = (char *)malloc((size_t)bootsize); if (bootbuf == 0) err(4, "%s", xxboot); if (read(b, bootbuf, bootsize) < 0) { free(bootbuf); err(4, "%s", xxboot); } } #endif /* NUMBOOT > 1 */ (void)close(b); #endif /* NUMBOOT > 0 */ /* * Make sure no part of the bootstrap is written in the area * reserved for the label. */ for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++) if (*p) errx(2, "bootstrap doesn't leave room for disk label"); return (lp); } void display(f, lp) FILE *f; register struct disklabel *lp; { register int i, j; register struct partition *pp; fprintf(f, "# %s:\n", specname); if ((unsigned) lp->d_type < DKMAXTYPES) fprintf(f, "type: %s\n", dktypenames[lp->d_type]); else fprintf(f, "type: %u\n", lp->d_type); fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename), lp->d_typename); fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname), lp->d_packname); fprintf(f, "flags:"); if (lp->d_flags & D_REMOVABLE) fprintf(f, " removeable"); if (lp->d_flags & D_ECC) fprintf(f, " ecc"); if (lp->d_flags & D_BADSECT) fprintf(f, " badsect"); fprintf(f, "\n"); fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize); fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors); fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks); fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl); fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders); fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit); fprintf(f, "rpm: %u\n", lp->d_rpm); fprintf(f, "interleave: %u\n", lp->d_interleave); fprintf(f, "trackskew: %u\n", lp->d_trackskew); fprintf(f, "cylinderskew: %u\n", lp->d_cylskew); fprintf(f, "headswitch: %lu\t\t# milliseconds\n", (u_long)lp->d_headswitch); fprintf(f, "track-to-track seek: %ld\t# milliseconds\n", (u_long)lp->d_trkseek); fprintf(f, "drivedata: "); for (i = NDDATA - 1; i >= 0; i--) if (lp->d_drivedata[i]) break; if (i < 0) i = 0; for (j = 0; j <= i; j++) fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]); fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions); fprintf(f, "# size offset fstype [fsize bsize bps/cpg]\n"); pp = lp->d_partitions; for (i = 0; i < lp->d_npartitions; i++, pp++) { if (pp->p_size) { fprintf(f, " %c: %8lu %8lu ", 'a' + i, (u_long)pp->p_size, (u_long)pp->p_offset); if ((unsigned) pp->p_fstype < FSMAXTYPES) fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); else fprintf(f, "%8d", pp->p_fstype); switch (pp->p_fstype) { case FS_UNUSED: /* XXX */ fprintf(f, " %5lu %5lu %5.5s ", (u_long)pp->p_fsize, (u_long)(pp->p_fsize * pp->p_frag), ""); break; case FS_BSDFFS: fprintf(f, " %5lu %5lu %5u ", (u_long)pp->p_fsize, (u_long)(pp->p_fsize * pp->p_frag), pp->p_cpg); break; case FS_BSDLFS: fprintf(f, " %5lu %5lu %5d", (u_long)pp->p_fsize, (u_long)(pp->p_fsize * pp->p_frag), pp->p_cpg); break; default: fprintf(f, "%20.20s", ""); break; } fprintf(f, "\t# (Cyl. %4lu", (u_long)(pp->p_offset / lp->d_secpercyl)); if (pp->p_offset % lp->d_secpercyl) putc('*', f); else putc(' ', f); fprintf(f, "- %lu", (u_long)((pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl - 1)); if (pp->p_size % lp->d_secpercyl) putc('*', f); fprintf(f, ")\n"); } } fflush(f); } int edit(lp, f) struct disklabel *lp; int f; { register int c, fd; struct disklabel label; FILE *fp; if ((fd = mkstemp(tmpfil)) == -1 || (fp = fdopen(fd, "w")) == NULL) { warnx("can't create %s", tmpfil); return (1); } display(fp, lp); fclose(fp); for (;;) { if (!editit()) break; fp = fopen(tmpfil, "r"); if (fp == NULL) { warnx("can't reopen %s for reading", tmpfil); break; } bzero((char *)&label, sizeof(label)); if (getasciilabel(fp, &label)) { *lp = label; if (writelabel(f, bootarea, lp) == 0) { fclose(fp); (void) unlink(tmpfil); return (0); } } fclose(fp); printf("re-edit the label? [y]: "); fflush(stdout); c = getchar(); if (c != EOF && c != (int)'\n') while (getchar() != (int)'\n') ; if (c == (int)'n') break; } (void) unlink(tmpfil); return (1); } int editit() { register int pid, xpid; int stat, omask; omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); while ((pid = fork()) < 0) { if (errno == EPROCLIM) { warnx("you have too many processes"); return(0); } if (errno != EAGAIN) { warn("fork"); return(0); } sleep(1); } if (pid == 0) { register char *ed; sigsetmask(omask); setgid(getgid()); setuid(getuid()); if ((ed = getenv("EDITOR")) == (char *)0) ed = DEFEDITOR; execlp(ed, ed, tmpfil, 0); err(1, "%s", ed); } while ((xpid = wait(&stat)) >= 0) if (xpid == pid) break; sigsetmask(omask); return(!stat); } char * skip(cp) register char *cp; { while (*cp != '\0' && isspace(*cp)) cp++; if (*cp == '\0' || *cp == '#') return ((char *)NULL); return (cp); } char * word(cp) register char *cp; { register char c; while (*cp != '\0' && !isspace(*cp) && *cp != '#') cp++; if ((c = *cp) != '\0') { *cp++ = '\0'; if (c != '#') return (skip(cp)); } return ((char *)NULL); } /* * Read an ascii label in from fd f, * in the same format as that put out by display(), * and fill in lp. */ int getasciilabel(f, lp) FILE *f; register struct disklabel *lp; { register char **cpp, *cp; register struct partition *pp; char *tp, *s, line[BUFSIZ]; int v, lineno = 0, errors = 0; lp->d_bbsize = BBSIZE; /* XXX */ lp->d_sbsize = SBSIZE; /* XXX */ while (fgets(line, sizeof(line) - 1, f)) { lineno++; if ((cp = index(line,'\n')) != 0) *cp = '\0'; cp = skip(line); if (cp == NULL) continue; tp = index(cp, ':'); if (tp == NULL) { fprintf(stderr, "line %d: syntax error\n", lineno); errors++; continue; } *tp++ = '\0', tp = skip(tp); if (streq(cp, "type")) { if (tp == NULL) tp = "unknown"; cpp = dktypenames; for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) if ((s = *cpp) && streq(s, tp)) { lp->d_type = cpp - dktypenames; goto next; } v = atoi(tp); if ((unsigned)v >= DKMAXTYPES) fprintf(stderr, "line %d:%s %d\n", lineno, "Warning, unknown disk type", v); lp->d_type = v; continue; } if (streq(cp, "flags")) { for (v = 0; (cp = tp) && *cp != '\0';) { tp = word(cp); if (streq(cp, "removeable")) v |= D_REMOVABLE; else if (streq(cp, "ecc")) v |= D_ECC; else if (streq(cp, "badsect")) v |= D_BADSECT; else { fprintf(stderr, "line %d: %s: bad flag\n", lineno, cp); errors++; } } lp->d_flags = v; continue; } if (streq(cp, "drivedata")) { register int i; for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) { lp->d_drivedata[i++] = atoi(cp); tp = word(cp); } continue; } if (sscanf(cp, "%d partitions", &v) == 1) { if (v == 0 || (unsigned)v > MAXPARTITIONS) { fprintf(stderr, "line %d: bad # of partitions\n", lineno); lp->d_npartitions = MAXPARTITIONS; errors++; } else lp->d_npartitions = v; continue; } if (tp == NULL) tp = ""; if (streq(cp, "disk")) { strncpy(lp->d_typename, tp, sizeof (lp->d_typename)); continue; } if (streq(cp, "label")) { strncpy(lp->d_packname, tp, sizeof (lp->d_packname)); continue; } if (streq(cp, "bytes/sector")) { v = atoi(tp); if (v <= 0 || (v % DEV_BSIZE) != 0) { fprintf(stderr, "line %d: %s: bad sector size\n", lineno, tp); errors++; } else lp->d_secsize = v; continue; } if (streq(cp, "sectors/track")) { v = atoi(tp); if (v <= 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_nsectors = v; continue; } if (streq(cp, "sectors/cylinder")) { v = atoi(tp); if (v <= 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_secpercyl = v; continue; } if (streq(cp, "tracks/cylinder")) { v = atoi(tp); if (v <= 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_ntracks = v; continue; } if (streq(cp, "cylinders")) { v = atoi(tp); if (v <= 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_ncylinders = v; continue; } if (streq(cp, "sectors/unit")) { v = atoi(tp); if (v <= 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_secperunit = v; continue; } if (streq(cp, "rpm")) { v = atoi(tp); if (v <= 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_rpm = v; continue; } if (streq(cp, "interleave")) { v = atoi(tp); if (v <= 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_interleave = v; continue; } if (streq(cp, "trackskew")) { v = atoi(tp); if (v < 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_trackskew = v; continue; } if (streq(cp, "cylinderskew")) { v = atoi(tp); if (v < 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_cylskew = v; continue; } if (streq(cp, "headswitch")) { v = atoi(tp); if (v < 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_headswitch = v; continue; } if (streq(cp, "track-to-track seek")) { v = atoi(tp); if (v < 0) { fprintf(stderr, "line %d: %s: bad %s\n", lineno, tp, cp); errors++; } else lp->d_trkseek = v; continue; } if ('a' <= *cp && *cp <= 'z' && cp[1] == '\0') { unsigned part = *cp - 'a'; if (part > lp->d_npartitions) { fprintf(stderr, "line %d: bad partition name\n", lineno); errors++; continue; } pp = &lp->d_partitions[part]; #define NXTNUM(n) { \ if (tp == NULL) { \ fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ errors++; \ break; \ } else { \ cp = tp, tp = word(cp); \ if (tp == NULL) \ tp = cp; \ (n) = atoi(cp); \ } \ } NXTNUM(v); if (v < 0) { fprintf(stderr, "line %d: %s: bad partition size\n", lineno, cp); errors++; } else pp->p_size = v; NXTNUM(v); if (v < 0) { fprintf(stderr, "line %d: %s: bad partition offset\n", lineno, cp); errors++; } else pp->p_offset = v; cp = tp, tp = word(cp); cpp = fstypenames; for (; cpp < &fstypenames[FSMAXTYPES]; cpp++) if ((s = *cpp) && streq(s, cp)) { pp->p_fstype = cpp - fstypenames; goto gottype; } if (isdigit(*cp)) v = atoi(cp); else v = FSMAXTYPES; if ((unsigned)v >= FSMAXTYPES) { fprintf(stderr, "line %d: %s %s\n", lineno, "Warning, unknown filesystem type", cp); v = FS_UNUSED; } pp->p_fstype = v; gottype: switch (pp->p_fstype) { case FS_UNUSED: /* XXX */ NXTNUM(pp->p_fsize); if (pp->p_fsize == 0) break; NXTNUM(v); pp->p_frag = v / pp->p_fsize; break; case FS_BSDFFS: NXTNUM(pp->p_fsize); if (pp->p_fsize == 0) break; NXTNUM(v); pp->p_frag = v / pp->p_fsize; NXTNUM(pp->p_cpg); break; case FS_BSDLFS: NXTNUM(pp->p_fsize); if (pp->p_fsize == 0) break; NXTNUM(v); pp->p_frag = v / pp->p_fsize; NXTNUM(pp->p_cpg); break; default: break; } continue; } fprintf(stderr, "line %d: %s: Unknown disklabel field\n", lineno, cp); errors++; next: ; } errors += checklabel(lp); return (errors == 0); } /* * Check disklabel for errors and fill in * derived fields according to supplied values. */ int checklabel(lp) register struct disklabel *lp; { register struct partition *pp; int i, errors = 0; char part; if (lp->d_secsize == 0) { fprintf(stderr, "sector size 0\n"); return (1); } if (lp->d_nsectors == 0) { fprintf(stderr, "sectors/track 0\n"); return (1); } if (lp->d_ntracks == 0) { fprintf(stderr, "tracks/cylinder 0\n"); return (1); } if (lp->d_ncylinders == 0) { fprintf(stderr, "cylinders/unit 0\n"); errors++; } if (lp->d_rpm == 0) Warning("revolutions/minute 0"); if (lp->d_secpercyl == 0) lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; if (lp->d_secperunit == 0) lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders; if (lp->d_bbsize == 0) { fprintf(stderr, "boot block size 0\n"); errors++; } else if (lp->d_bbsize % lp->d_secsize) Warning("boot block size %% sector-size != 0"); if (lp->d_sbsize == 0) { fprintf(stderr, "super block size 0\n"); errors++; } else if (lp->d_sbsize % lp->d_secsize) Warning("super block size %% sector-size != 0"); if (lp->d_npartitions > MAXPARTITIONS) Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", (u_long)lp->d_npartitions, MAXPARTITIONS); for (i = 0; i < lp->d_npartitions; i++) { part = 'a' + i; pp = &lp->d_partitions[i]; if (pp->p_size == 0 && pp->p_offset != 0) Warning("partition %c: size 0, but offset %lu", part, (u_long)pp->p_offset); #ifdef notdef if (pp->p_size % lp->d_secpercyl) Warning("partition %c: size %% cylinder-size != 0", part); if (pp->p_offset % lp->d_secpercyl) Warning("partition %c: offset %% cylinder-size != 0", part); #endif if (pp->p_offset > lp->d_secperunit) { fprintf(stderr, "partition %c: offset past end of unit\n", part); errors++; } if (pp->p_offset + pp->p_size > lp->d_secperunit) { fprintf(stderr, "partition %c: partition extends past end of unit\n", part); errors++; } } for (; i < MAXPARTITIONS; i++) { part = 'a' + i; pp = &lp->d_partitions[i]; if (pp->p_size || pp->p_offset) Warning("unused partition %c: size %d offset %lu", 'a' + i, pp->p_size, (u_long)pp->p_offset); } return (errors); } /* * When operating on a "virgin" disk, try getting an initial label * from the associated device driver. This might work for all device * drivers that are able to fetch some initial device parameters * without even having access to a (BSD) disklabel, like SCSI disks, * most IDE drives, or vn devices. * * The device name must be given in its "canonical" form. */ struct disklabel * getvirginlabel(void) { static struct disklabel lab; char namebuf[BBSIZE]; int f; if (dkname[0] == '/') { warnx("\"auto\" requires the usage of a canonical disk name"); return (NULL); } (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname); if ((f = open(namebuf, O_RDONLY)) == -1) { warn("cannot open %s", namebuf); return (NULL); } - - /* - * Try to use the new get-virgin-label ioctl. If it fails, - * fallback to the old get-disdk-info ioctl. - */ - if (ioctl(f, DIOCGDVIRGIN, &lab) < 0) { - if (ioctl(f, DIOCGDINFO, &lab) < 0) { - warn("ioctl DIOCGDINFO"); - close(f); - return (NULL); - } + if (ioctl(f, DIOCGDINFO, &lab) < 0) { + warn("ioctl DIOCGDINFO"); + close(f); + return (NULL); } close(f); lab.d_boot0 = NULL; lab.d_boot1 = NULL; return (&lab); } /* * If we are installing a boot program that doesn't fit in d_bbsize * we need to mark those partitions that the boot overflows into. * This allows newfs to prevent creation of a filesystem where it might * clobber bootstrap code. */ void setbootflag(lp) register struct disklabel *lp; { register struct partition *pp; int i, errors = 0; char part; u_long boffset; if (bootbuf == 0) return; boffset = bootsize / lp->d_secsize; for (i = 0; i < lp->d_npartitions; i++) { part = 'a' + i; pp = &lp->d_partitions[i]; if (pp->p_size == 0) continue; if (boffset <= pp->p_offset) { if (pp->p_fstype == FS_BOOT) pp->p_fstype = FS_UNUSED; } else if (pp->p_fstype != FS_BOOT) { if (pp->p_fstype != FS_UNUSED) { fprintf(stderr, "boot overlaps used partition %c\n", part); errors++; } else { pp->p_fstype = FS_BOOT; Warning("boot overlaps partition %c, %s", part, "marked as FS_BOOT"); } } } if (errors) errx(4, "cannot install boot program"); } /*VARARGS1*/ void Warning(char *fmt, ...) { va_list ap; fprintf(stderr, "Warning, "); va_start(ap, fmt); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); va_end(ap); } void usage() { #if NUMBOOT > 0 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "usage: disklabel [-r] disk", "\t\t(to read label)", " disklabel -w [-r] disk type [ packid ]", "\t\t(to write label with existing boot program)", " disklabel -e [-r] disk", "\t\t(to edit label)", " disklabel -R [-r] disk protofile", "\t\t(to restore label with existing boot program)", #if NUMBOOT > 1 " disklabel -B [ -b boot1 [ -s boot2 ] ] disk [ type ]", "\t\t(to install boot program with existing label)", " disklabel -w -B [ -b boot1 [ -s boot2 ] ] disk type [ packid ]", "\t\t(to write label and boot program)", " disklabel -R -B [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]", "\t\t(to restore label and boot program)", #else " disklabel -B [ -b bootprog ] disk [ type ]", "\t\t(to install boot program with existing on-disk label)", " disklabel -w -B [ -b bootprog ] disk type [ packid ]", "\t\t(to write label and install boot program)", " disklabel -R -B [ -b bootprog ] disk protofile [ type ]", "\t\t(to restore label and install boot program)", #endif " disklabel [-NW] disk", "\t\t(to write disable/enable label)"); #else fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "usage: disklabel [-r] disk", "(to read label)", " disklabel -w [-r] disk type [ packid ]", "\t\t(to write label)", " disklabel -e [-r] disk", "\t\t(to edit label)", " disklabel -R [-r] disk protofile", "\t\t(to restore label)", " disklabel [-NW] disk", "\t\t(to write disable/enable label)"); #endif exit(1); }