From ce19f97efcf130f92584e26e103c6af738f16e0b Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Sun, 14 Feb 2010 11:37:31 -0800 Subject: [PATCH] disklabel[32,64] utilities - Change the default disklabel program * 'disklabel' is now the 64-bit disklabel program. * The 32 bit disklabel program can be accessed via 'disklabel32' * The 64 bit disklabel program can also be accessed via 'disklabel64' --- sbin/Makefile | 2 +- sbin/disklabel/Makefile | 9 - sbin/disklabel/disklabel.5 | 111 --- sbin/disklabel/disklabel.8 | 1042 ----------------------- sbin/disklabel/disklabel.c | 1822 ---------------------------------------- sbin/disklabel/pathnames.h | 41 - sbin/disklabel32/Makefile | 9 + sbin/disklabel32/disklabel.c | 1822 ++++++++++++++++++++++++++++++++++++++++ sbin/disklabel32/disklabel32.5 | 111 +++ sbin/disklabel32/disklabel32.8 | 1042 +++++++++++++++++++++++ sbin/disklabel32/pathnames.h | 41 + sbin/disklabel64/Makefile | 4 + 12 files changed, 3030 insertions(+), 3026 deletions(-) delete mode 100644 sbin/disklabel/Makefile delete mode 100644 sbin/disklabel/disklabel.5 delete mode 100644 sbin/disklabel/disklabel.8 delete mode 100644 sbin/disklabel/disklabel.c delete mode 100644 sbin/disklabel/pathnames.h create mode 100644 sbin/disklabel32/Makefile create mode 100644 sbin/disklabel32/disklabel.c create mode 100644 sbin/disklabel32/disklabel32.5 create mode 100644 sbin/disklabel32/disklabel32.8 create mode 100644 sbin/disklabel32/pathnames.h diff --git a/sbin/Makefile b/sbin/Makefile index b380006..4da27c9 100644 --- a/sbin/Makefile +++ b/sbin/Makefile @@ -15,7 +15,7 @@ SUBDIR= adjkerntz \ devd \ devfsctl \ dhclient \ - disklabel \ + disklabel32 \ disklabel64 \ diskinfo \ dmesg \ diff --git a/sbin/disklabel/Makefile b/sbin/disklabel/Makefile deleted file mode 100644 index f428f2a..0000000 --- a/sbin/disklabel/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# @(#)Makefile 8.2 (Berkeley) 3/17/94 -# $FreeBSD: src/sbin/disklabel/Makefile,v 1.3.14.3 2001/04/25 10:58:15 ru Exp $ -# $DragonFly: src/sbin/disklabel/Makefile,v 1.5 2006/10/17 02:18:51 pavalos Exp $ - -PROG= disklabel -SRCS= disklabel.c -MAN= disklabel.5 disklabel.8 - -.include diff --git a/sbin/disklabel/disklabel.5 b/sbin/disklabel/disklabel.5 deleted file mode 100644 index aa33211..0000000 --- a/sbin/disklabel/disklabel.5 +++ /dev/null @@ -1,111 +0,0 @@ -.\" Copyright (c) 1987, 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.5.5 8.1 (Berkeley) 6/5/93 -.\" $FreeBSD: src/sbin/disklabel/disklabel.5,v 1.7.2.4 2002/08/21 18:58:16 trhodes Exp $ -.\" $DragonFly: src/sbin/disklabel/disklabel.5,v 1.6 2008/08/21 21:08:08 thomas Exp $ -.\" -.Dd August 21, 2008 -.Dt DISKLABEL 5 -.Os -.Sh NAME -.Nm disklabel -.Nd 32 bit disk pack label -.Sh SYNOPSIS -.In sys/disklabel.h -.Sh DESCRIPTION -Each disk or disk pack on a system may contain a disk label -which provides detailed information -about the geometry of the disk and the partitions into which the disk -is divided. -It should be initialized when the disk is formatted, -and may be changed later with the -.Xr disklabel 8 -program. -This information is used by the system disk driver and by the bootstrap -program to determine how to program the drive -and where to find the file systems on the disk partitions. -Additional information is used by the file system in order -to use the disk most efficiently and to locate important file system information. -The description of each partition contains an identifier for the partition -type (standard file system, swap area, etc.). -The file system updates the in-core copy of the label if it contains -incomplete information about the file system. -.Pp -The label is located in sector number -.Dv LABELSECTOR -of the drive, usually sector 0 where it may be found -without any information about the disk geometry. -It is at an offset -.Dv LABELOFFSET -from the beginning of the sector, to allow room for the initial bootstrap. -The disk sector containing the label is normally made read-only -so that it is not accidentally overwritten by pack-to-pack copies -or swap operations; -the -.Dv DIOCWLABEL -.Xr ioctl 2 , -which is done as needed by the -.Xr disklabel 8 -program. -.Pp -A copy of the in-core label for a disk can be obtained with the -.Dv DIOCGDINFO -.Xr ioctl 2 ; -this works with a file descriptor for a block or character (``raw'') device -for any partition of the disk. -The in-core copy of the label is set by the -.Dv DIOCSDINFO -.Xr ioctl 2 . -The offset of a partition cannot generally be changed while it is open, -nor can it be made smaller while it is open. -One exception is that any change is allowed if no label was found -on the disk, and the driver was able to construct only a skeletal label -without partition information. -Finally, the -.Dv DIOCWDINFO -.Xr ioctl 2 -operation sets the in-core label and then updates the on-disk label; -there must be an existing label on the disk for this operation to succeed. -Thus, the initial label for a disk or disk pack must be installed -by writing to the raw disk. -All of these operations are normally done using -.Xr disklabel 8 . -.Pp -The format of the disk label is specified in -.In sys/disklabel32.h . -.Sh SEE ALSO -.Xr disklabel64 5 , -.Xr disktab 5 , -.Xr disklabel 8 diff --git a/sbin/disklabel/disklabel.8 b/sbin/disklabel/disklabel.8 deleted file mode 100644 index 36a20ed..0000000 --- a/sbin/disklabel/disklabel.8 +++ /dev/null @@ -1,1042 +0,0 @@ -.\" 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: src/sbin/disklabel/disklabel.8,v 1.15.2.22 2003/04/17 17:56:34 trhodes Exp $ -.\" $DragonFly: src/sbin/disklabel/disklabel.8,v 1.30 2008/09/16 20:45:36 thomas Exp $ -.\" -.Dd September 28, 2009 -.Dt DISKLABEL 8 -.Os -.Sh NAME -.Nm disklabel -.Nd read and write 32 bit disk pack label -.Sh SYNOPSIS -.Nm -.Op Fl r -.Ar disk -.Nm -.Fl w -.Op Fl r -.Op Fl n -.Ar disk Ar disktype Ns / Ns Cm auto -.Oo Ar packid Oc -.Nm -.Fl e -.Op Fl r -.Op Fl n -.Ar disk -.Nm -.Fl R -.Op Fl r -.Op Fl n -.Ar disk Ar protofile -.Nm -.Op Fl NW -.Ar disk -.Pp -.Nm -.Fl B -.Oo -.Fl b Ar boot1 -.Fl s Ar boot2 -.Oc -.Ar disk -.Oo Ar disktype Ns / Ns Cm auto Oc -.Nm -.Fl w -.Fl B -.Op Fl n -.Oo -.Fl b Ar boot1 -.Fl s Ar boot2 -.Oc -.Ar disk Ar disktype Ns / Ns Cm auto -.Oo Ar packid Oc -.Nm -.Fl R -.Fl B -.Op Fl n -.Oo -.Fl b Ar boot1 -.Fl s Ar boot2 -.Oc -.Ar disk Ar protofile -.Oo Ar disktype Ns / Ns Cm auto Oc -.Nm -.Fl f Ar slice_start_lba -.Oo Ar options Oc -.Sh DESCRIPTION -The -.Nm -utility -installs, examines or modifies a 32 bit 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 -The disk label resides close to or at the beginning of each disk slice. -For faster access, the kernel maintains a copy in core at all times. -By -default, most forms of the -.Nm -command 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. -.Ss Disk device name -All -.Nm -forms require a disk device name, which should always be the raw -device name representing the disk or slice. -.Dx -uses the following scheme for slice numbering: -If the disk doesn't use GPT (typically laid out by -.Xr gpt 8 ) , -but e.g.\& MBR (typically laid out by -.Xr fdisk 8 ) , -then slice 0, e.g.\& -.Pa da0s0 , -represents the entire disk regardless of any DOS partitioning. -Slice 0 is called the compatibility slice, -and slice 1 and onward, e.g.\& -.Pa da0s1 , -represents a -.Bx -slice. -If the disk does use GPT, then all slices are -.Bx -slices, slice 0 isn't special, it is just the first slice on the disk. -You do not have to include the -.Pa /dev/ -path prefix when specifying the device. -The -.Nm -utility will automatically prepend it. -.Ss Reading the disk label -To examine the label on a disk drive, use -.Nm -without options: -.Pp -.Nm -.Op Fl r -.Ar disk -.Pp -.Ar disk -represents the raw disk in question, and may be in the form -.Pa da0s1 -or -.Pa /dev/da0s1 . -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. -.Ss Writing a standard label -To write a standard label, use the form -.Pp -.Nm -.Fl w -.Op Fl r -.Op Fl n -.Ar disk Ar disktype Ns / Ns Cm auto -.Oo Ar packid Oc -.Pp -The required arguments to -.Nm -are the drive to be labeled and the drive type as described in the -.Xr 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. -.Pp -If the -.Fl n -flag is given, no data will be written to the device, and instead the -disklabel that would have been written will be printed to stdout. -.Pp -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 -.Cm 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 -For most harddisks, a label based on percentages for most partitions (and -one partition with a size of -.Ql * ) -will produce a reasonable configuration. -.Pp -PC-based systems have special requirements in order for the BIOS to properly -recognize a -.Dx -disklabel. -Older systems may require what is known as a -.Dq 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 partition using -.Ar fdisk -and then create a -.Dx -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 -To edit an existing disk label, use the form -.Pp -.Nm -.Fl e -.Op Fl r -.Op Fl n -.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. -If -.Fl n -is specified, no data will be written to the device, and instead the -disklabel that would have been written will be printed to stdout. -This is -useful to see how a partitioning scheme will work out for a specific disk. -.Ss Restoring a disk label from a file -To restore a disk label from a file, use the form -.Pp -.Nm -.Fl R -.Op Fl r -.Op Fl n -.Ar disk Ar protofile -.Pp -.Nm -is capable of restoring a disk label that was previously saved in a file -in ASCII format. -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 -.Ql # -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. -If -.Fl n -is used, no data will be written to the device, and instead the -disklabel that would have been written will be printed to stdout. -This is -useful to see how a partitioning scheme will work out for a specific disk. -.Ss Enabling and disabling writing to the disk label area -By default, it is not possible to write to the disk label area at the beginning -of a disk. -The disk driver arranges for -.Xr write 2 -and similar system calls -to return -.Er EROFS -on any attempt to do so. -If you need -to write to this area (for example, to obliterate the label), use the form -.Pp -.Nm -.Fl W -.Ar disk -.Pp -To disallow writing to the label area after previously allowing it, -use the command -.Pp -.Nm -.Fl N -.Ar disk -.Ss Installing bootstraps -The final three forms of -.Nm -are used to install bootstrap code, which allows boot from a -.Xr UFS 5 -file system. -If you are creating a -.Dq dangerously-dedicated -slice for compatibility with older PC systems, -you generally want to specify the compatibility slice, such as -.Pa da0s0 . -If you are creating a label within an existing DOS slice, -you should specify -the slice name such as -.Pa da0s1 . -Making a slice 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 -.Dx -bootblocks in the slice. -.Pp -.Nm -.Fl B -.Oo -.Fl b Ar boot1 -.Fl s Ar boot2 -.Oc -.Ar disk -.Oo Ar disktype Ns / Ns Cm auto Oc -.Pp -This form installs the bootstrap only. -It does not change the disk label. -You should never use this command on the compatibility slice unless you -intend to create a -.Dq dangerously-dedicated -disk, such as -.Ar da0s0 . -This command is typically run on a -.Bx -slice such as -.Ar da0s1 . -.Pp -.Nm -.Fl w -.Fl B -.Op Fl n -.Oo -.Fl b Ar boot1 -.Fl s Ar boot2 -.Oc -.Ar disk Ar disktype Ns / Ns Cm auto -.Oo Ar packid Oc -.Pp -This form corresponds to the -.Dq write label -command described above. -In addition to writing a new volume label, it also installs the bootstrap. -If run on the compatibility slice this command will create a -.Dq dangerously-dedicated -label. -This command is normally run on a -.Bx -slice rather than the compatibility slice. -If -.Fl n -is used, no data will be written to the device, and instead the -disklabel that would have been written will be printed to stdout. -.Pp -.Nm -.Fl R -.Fl B -.Op Fl n -.Oo -.Fl b Ar boot1 -.Fl s Ar boot2 -.Oc -.Ar disk Ar protofile -.Oo Ar disktype Ns / Ns Cm auto Oc -.Pp -This form corresponds to the -.Dq restore label -command described above. -In addition to restoring the volume label, it also installs the bootstrap. -If run on the compatibility slice this command will create a -.Dq dangerously-dedicated -label. -This command is normally run on a -.Bx -slice rather than the compatibility -slice. -.Pp -The bootstrap commands always access the disk directly, -so it is not necessary to specify the -.Fl r -flag. -If -.Fl n -is used, no data will be written to the device, and instead the -disklabel that would have been written will be printed to stdout. -.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 normally 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 -.Dq b0 -and -.Dq 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. -.El -.Ss Initializing/Formatting a bootable disk from scratch -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 -.No non- Ns Dx -slices. -.Bl -enum -.It -Use -.Xr gpt 8 -or -.Xr fdisk 8 -to initialize the hard disk, and create a GPT or MBR slice table, -referred to as the -.Dq "partition table" -in -.Tn DOS . -.It -Use -.Nm -or -.Xr disklabel64 8 -to define partitions on -.Dx -slices created in the previous step. -.It -Finally use -.Xr newfs_hammer 8 -or -.Xr newfs 8 -to create file systems on new partitions. -.El -.Pp -A typical partitioning scheme would be to have an -.Ql a -partition -of approximately 512MB to hold the root file system, a -.Ql b -partition for -swap (usually 4GB), a -.Ql d -partition for -.Pa /var -(usually 2GB), an -.Ql e -partition for -.Pa /var/tmp -(usually 2GB), an -.Ql f -partition for -.Pa /usr -(usually around 4GB), -and finally a -.Ql g -partition for -.Pa /home -(usually all remaining space). -If you are tight on space all sizes can be halved. -Your mileage may vary. -.Pp -.Dl "fdisk -BI da0" -.Dl "disklabel -w -B da0s1 auto" -.Dl "disklabel -e da0s1" -.Ss Manual offset -.Dx -no longer snoop-adjusts the on-disk label when reading or writing -raw labels. -.Nm -is now responsible for adjusting the label when operating in raw mode. -Traditional (32 bit -.Bx ) -disklabels store offsets as absolute block numbers -rather than slice-relative block numbers. -If -.Nm -is unable to issue the -.Dv DIOCGPART -ioctl to get slice information it will -refuse to read or write the label in raw mode. -The -.Fl f -option may be used to force the operation by supplying a manual offset. -.Sh FILES -.Bl -tag -width ".Pa /etc/disktab" -compact -.It Pa /boot/boot1 -Default stage1 boot image. -.It Pa /boot/boot2 -Default stage2 boot image. -.It Pa /etc/disktab -Disk description file. -.El -.Sh SAVED FILE FORMAT -The -.Nm -utility uses an -.Tn ASCII -version of the label when examining, editing, or restoring a disk label. -The format is: -.Bd -literal -offset 4n -# /dev/ad4s4: -type: unknown -disk: amnesiac -label: fictitious -flags: -bytes/sector: 512 -sectors/track: 63 -tracks/cylinder: 24 -sectors/cylinder: 1512 -cylinders: 161098 -sectors/unit: 243581184 -rpm: 3600 -interleave: 1 -trackskew: 0 -cylinderskew: 0 -headswitch: 0 # milliseconds -track-to-track seek: 0 # milliseconds -drivedata: 0 - -16 partitions: -# size offset fstype - a: 1048560 16 4.2BSD # 511.992MB - b: 8388608 1048576 swap # 4096.000MB - c: 243581184 0 unused # 118936.125MB - d: 4194304 9437184 4.2BSD # 2048.000MB - e: 4194304 13631488 4.2BSD # 2048.000MB - f: 8388608 17825792 4.2BSD # 4096.000MB - h: 196395264 26214400 HAMMER # 95896.125MB - i: 10485760 222609664 ccd # 5120.000MB - j: 10485760 233095424 vinum # 5120.000MB -.Ed -.Pp -Lines starting with a -.Ql # -mark are comments. -Most of the other specifications are no longer used. -The ones which must still be set correctly are: -.Bl -inset -.It Ar label -is an optional label, set by the -.Ar packid -option when writing a label. -.It Ar flags -may be -.Cm removable , ecc -or -.Cm badsect . -.Cm removable -is set for removable media drives, but no current -.Dx -driver evaluates this -flag. -.Cm ecc -is no longer supported; -.Cm badsect -specifies that the drive can perform bad sector remapping. -.It Ar sectors/unit -describes the total size of the disk. -This value must be correct. -.It Ar "the partition table" -is the -.Ux -partition table, not the -.Tn DOS -partition table described in -.Xr fdisk 8 . -.El -.Pp -The partition table can have up to 16 entries. -It contains the following information: -.Bl -tag -width indent -.It Ar # -The partition identifier is a single letter in the range -.Ql a -to -.Ql p . -By convention, partition -.Ql c -is reserved to describe the entire disk. -.It Ar size -The size of the partition in sectors, -.Cm K -(kilobytes - 1024), -.Cm M -(megabytes - 1024*1024), -.Cm G -(gigabytes - 1024*1024*1024), -.Cm % -(percentage of free space -.Em after -removing any fixed-size partitions other than partition -.Ql c ) , -or -.Cm * -(all remaining free space -.Em after -fixed-size and percentage partitions). -For partition -.Ql c , -a size of -.Cm * -indicates the entire disk. -Lowercase versions of -.Cm K , M , -and -.Cm G -are allowed. -Size and type should be specified without any spaces between them. -.Pp -Example: 2097152, 1G, 1024M and 1048576K are all the same size -(assuming 512-byte sectors). -.It Ar offset -The offset of the start of the partition from the beginning of the -drive in sectors, or -.Cm * -to have -.Nm -calculate the correct offset to use (the end of the previous partition plus -one, ignoring partition -.Ql c . -For partition -.Ql c , -.Cm * -will be interpreted as an offset of 0. -.It Ar fstype -Describes the purpose of the partition. -The example shows all currently used partition types. -For -.Xr UFS 5 -file systems, use type -.Cm 4.2BSD . -For -.Xr HAMMER 5 -file systems, use type -.Cm HAMMER . -For -.Xr ccd 4 -partitions, use type -.Cm ccd . -For Vinum drives, use type -.Cm vinum . -Other common types are -.Cm swap -and -.Cm unused . -By convention, partition -.Ql c -represents the entire slice and should be of type -.Cm unused , -though -.Nm -does not enforce this convention. -The -.Nm -utility -also knows about a number of other partition types, -none of which are in current use. -(See -.Dv fstypenames -in -.In sys/dtype.h -for more details). -.El -.Pp -The remainder of the line is a comment and shows the size of -the partition in MB. -.Sh EXAMPLES -.Dl "disklabel da0s1" -.Pp -Display the in-core label for the first slice of the -.Pa da0 -disk, as obtained via -.Pa /dev/da0s1 . -(If the disk is -.Dq dangerously-dedicated , -the compatibility slice name should be specified, such as -.Pa da0s0 . ) -.Pp -.Dl "disklabel da0s1 > savedlabel" -.Pp -Save the in-core label for -.Pa da0s1 -into the file -.Pa savedlabel . -This file can be used with the -.Fl R -option to restore the label at a later date. -.Pp -.Dl "disklabel -w -r /dev/da0s1 da2212 foo" -.Pp -Create a label for -.Pa da0s1 -based on information for -.Dq da2212 -found in -.Pa /etc/disktab . -Any existing bootstrap code will be clobbered -and the disk rendered unbootable. -.Pp -.Dl "disklabel -e -r da0s1" -.Pp -Read the on-disk label for -.Pa da0s1 , -edit it, and reinstall in-core as well as on-disk. -Existing bootstrap code is unaffected. -.Pp -.Dl "disklabel -e -r -n da0s1" -.Pp -Read the on-disk label for -.Pa da0s1 , -edit it, and display what the new label would be (in sectors). -It does -.Em not -install the new label either in-core or on-disk. -.Pp -.Dl "disklabel -r -w da0s1 auto" -.Pp -Try to auto-detect the required information from -.Pa da0s1 , -and write a new label to the disk. -Use another -.Nm Fl e -command to edit the -partitioning and file system information. -.Pp -.Dl "disklabel -R da0s1 savedlabel" -.Pp -Restore the on-disk and in-core label for -.Pa da0s1 -from information in -.Pa savedlabel . -Existing bootstrap code is unaffected. -.Pp -.Dl "disklabel -R -n da0s1 label_layout" -.Pp -Display what the label would be for -.Pa da0s1 -using the partition layout in -.Pa label_layout . -This is useful for determining how much space would be allotted for various -partitions with a labelling scheme using -.Cm % Ns -based -or -.Cm * -partition sizes. -.Pp -.Dl "disklabel -B da0s1" -.Pp -Install a new bootstrap on -.Pa da0s1 . -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 newboot2 da2212" -.Pp -Install a new label and bootstrap. -The label is derived from disktab information for -.Dq da2212 -and installed both in-core and on-disk. -The bootstrap code comes from the files -.Pa newboot1 -and -.Pa 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 -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 -.Dq 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. -.Pp -.Dl "disklabel -W da0s1" -.Dl "dd if=/dev/zero of=/dev/da0s1 bs=512 count=32" -.Dl "disklabel64 -r -w da0s1 auto" -.Dl "disklabel64 -N da0s1" -.Pp -Completely wipe any prior information on the slice, -changing label format to 64 bit. -The wiping is needed as -.Nm disklabel64 -and -.Nm , -as a safety measure, -won't do any operations if label with other format is already installed. -.Pp -This is an example disklabel that uses some of the new partition size types -such as -.Cm % , M , G , -and -.Cm * , -which could be used as a source file for -.Pp -.Dl "disklabel -R ad0s1 new_label_file" -.Bd -literal -offset 4n -# /dev/ad0s1: -type: ESDI -disk: ad0s1 -label: -flags: -bytes/sector: 512 -sectors/track: 63 -tracks/cylinder: 16 -sectors/cylinder: 1008 -cylinders: 40633 -sectors/unit: 40959009 -rpm: 3600 -interleave: 1 -trackskew: 0 -cylinderskew: 0 -headswitch: 0 # milliseconds -track-to-track seek: 0 # milliseconds -drivedata: 0 - -16 partitions: -# size offset fstype - a: 400M 0 4.2BSD - b: 1G * swap - c: * * unused - e: 204800 * 4.2BSD - f: 5g * 4.2BSD - g: * * 4.2BSD -.Ed -.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 -.Ql 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 -.Ql a -partition. -.Pp -On some machines the bootstrap code may not fit entirely in the area -allocated for it by some file systems. -As a result, it may not be possible to have file systems on some partitions -of a -.Dq bootable -disk. -When installing bootstrap code, -.Nm -checks for these cases. -If the installed boot code would overlap a partition of type -.Dv FS_UNUSED -it is marked as type -.Dv FS_BOOT . -The -.Xr newfs 8 -utility will disallow creation of file systems on -.Dv FS_BOOT -partitions. -Conversely, if a partition has a type other than -.Dv FS_UNUSED -or -.Dv FS_BOOT , -.Nm -will not install bootstrap code that overlaps it. -.Sh COMPATIBILITY -Due to -.Xr disklabel 5 -storing sector numbers in 32 bit format -.Nm -is restricted to 2TB, using the prevalent sector size of 512B. -.Xr disklabel64 5 -labels should be used to partition larger disks. -.Pp -The various -.Bx Ns s -use slightly different versions of -.Bx -disklabels -and are not generally compatible. -The -.Dx -kernel can often use labels from other -.Bx Ns s -for read-only operation. -.Sh SEE ALSO -.Xr dd 1 , -.Xr ccd 4 , -.Xr disklabel 5 , -.Xr disktab 5 , -.Xr boot0cfg 8 , -.Xr diskinfo 8 , -.Xr disklabel64 8 , -.Xr fdisk 8 , -.Xr gpt 8 , -.Xr newfs 8 , -.Xr newfs_hammer 8 , -.Xr vinum 8 -.Sh BUGS -For the i386 architecture, the primary bootstrap sector contains -an embedded -.Em fdisk -table. -The -.Nm -utility 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 -.Bx -disklabel -starts at absolute block 0 on the disk. -.Pp -The -.Nm -utility -does not perform all possible error checking. -Warning -.Em is -given if partitions -overlap; if an absolute offset does not match the expected offset; if the -.Ql c -partition does not start at 0 or does not cover the entire slice; if a -partition runs past the end of the device; and a number of other errors; but -no warning is given if space remains unused. diff --git a/sbin/disklabel/disklabel.c b/sbin/disklabel/disklabel.c deleted file mode 100644 index deb78c5..0000000 --- a/sbin/disklabel/disklabel.c +++ /dev/null @@ -1,1822 +0,0 @@ -/* - * 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. - * - * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved. - * @(#)disklabel.c 1.2 (Symmetric) 11/28/85 - * @(#)disklabel.c 8.2 (Berkeley) 1/7/94 - * $FreeBSD: src/sbin/disklabel/disklabel.c,v 1.28.2.15 2003/01/24 16:18:16 des Exp $ - * $DragonFly: src/sbin/disklabel/disklabel.c,v 1.28 2008/08/21 21:22:36 thomas Exp $ - */ - -#include -#include -#include -#include -#define DKTYPENAMES -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#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 - -/* FIX! These are too low, but are traditional */ -#define DEFAULT_NEWFS_BLOCK 8192U -#define DEFAULT_NEWFS_FRAG 1024U -#define DEFAULT_NEWFS_CPG 16U - -#define BIG_NEWFS_BLOCK 16384U -#define BIG_NEWFS_FRAG 2048U -#define BIG_NEWFS_CPG 64U - -#define NUMBOOT 2 - -void makelabel(const char *, const char *, struct disklabel32 *); -int writelabel(int, const char *, struct disklabel32 *); -void l_perror(const char *); -struct disklabel32 *readlabel(int); -struct disklabel32 *makebootarea(char *, struct disklabel32 *, int); -void display(FILE *, const struct disklabel32 *); -int edit(struct disklabel32 *, int); -int editit(void); -char *skip(char *); -char *word(char *); -int getasciilabel(FILE *, struct disklabel32 *); -int getasciipartspec(char *, struct disklabel32 *, int, int); -int checklabel(struct disklabel32 *); -void setbootflag(struct disklabel32 *); -void Warning(const char *, ...) __printflike(1, 2); -void usage(void); -int checkoldboot(int, const char *); -const char *fixlabel(int, struct disklabel32 *, int); -struct disklabel32 *getvirginlabel(void); -struct disklabel32 *getdisklabelfromdisktab(const char *name); - -#define DEFEDITOR _PATH_VI -#define streq(a,b) (strcmp(a,b) == 0) - -char *dkname; -char *specname; -char tmpfil[] = PATH_TMPFILE; - -char namebuf[BBSIZE]; -struct disklabel32 lab; -char bootarea[BBSIZE]; - -#define MAX_PART ('z') -#define MAX_NUM_PARTS (1 + MAX_PART - 'a') -char part_size_type[MAX_NUM_PARTS]; -char part_offset_type[MAX_NUM_PARTS]; -int part_set[MAX_NUM_PARTS]; - -#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; -int disable_write; /* set to disable writing to disk label */ -int forceflag; -u_int32_t slice_start_lba; - -#ifdef DEBUG -int debug; -#define OPTIONS "BNRWb:def:nrs:w" -#else -#define OPTIONS "BNRWb:ef:nrs:w" -#endif - -int -main(int argc, char *argv[]) -{ - struct disklabel32 *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; - - case 'f': - forceflag = 1; - slice_start_lba = strtoul(optarg, NULL, 0); - break; -#if NUMBOOT > 1 - case 's': - bootxx = optarg; - break; -#endif -#endif - case 'N': - if (op != UNSPEC) - usage(); - op = NOWRITE; - break; - case 'n': - disable_write = 1; - 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 = getdevpath(argv[0], 0); - specname = dkname; - 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); - if (checkoldboot(f, NULL)) - warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel"); - 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 disklabel32 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(const char *type, const char *name, struct disklabel32 *lp) -{ - struct disklabel32 *dp; - - if (strcmp(type, "auto") == 0) - dp = getvirginlabel(); - else - dp = getdisklabelfromdisktab(type); - if (dp == NULL) - errx(1, "%s: unknown disk type", type); - *lp = *dp; - - /* - * NOTE: boot control files may no longer be specified in disktab. - */ - if (name) - strncpy(lp->d_packname, name, sizeof(lp->d_packname)); -} - -int -writelabel(int f, const char *boot, struct disklabel32 *lp) -{ - const char *msg; - int flag; - int r; - - if (disable_write) { - Warning("write to disk label suppressed - label was as follows:"); - display(stdout, lp); - return (0); - } else { - /* make sure we are not overwriting our boot code */ - if (checkoldboot(f, boot)) - errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!"); - setbootflag(lp); - lp->d_magic = DISKMAGIC32; - lp->d_magic2 = DISKMAGIC32; - lp->d_checksum = 0; - lp->d_checksum = dkcksum32(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, DIOCSDINFO32, lp) < 0 && - errno != ENODEV && errno != ENOTTY) { - l_perror("ioctl DIOCSDINFO32"); - return (1); - } - lseek(f, (off_t)0, SEEK_SET); - - /* - * write enable label sector before write - * (if necessary), disable after writing. - */ - flag = 1; - if (ioctl(f, DIOCWLABEL, &flag) < 0) - warn("ioctl DIOCWLABEL"); - msg = fixlabel(f, lp, 1); - if (msg) { - warn(msg); - return (1); - } - r = write(f, boot, lp->d_bbsize); - fixlabel(f, lp, 0); - if (r != ((ssize_t)lp->d_bbsize)) { - warn("write"); - return (1); - } -#if NUMBOOT > 0 - /* - * Output the remainder of the disklabel - */ - if (bootbuf) { - fixlabel(f, lp, 1); - r = write(f, bootbuf, bootsize); - fixlabel(f, lp, 0); - if (r != bootsize) { - warn("write"); - return(1); - } - } -#endif - flag = 0; - ioctl(f, DIOCWLABEL, &flag); - } else if (ioctl(f, DIOCWDINFO32, lp) < 0) { - l_perror("ioctl DIOCWDINFO32"); - return (1); - } - } - return (0); -} - -void -l_perror(const char *s) -{ - switch (errno) { - - case ESRCH: - warnx("%s: no disk label on disk;", s); - fprintf(stderr, "add \"-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; - - case ENOATTR: - warnx("%s: the disk already has a label of a different type,\n" - "probably a 64 bit disklabel. It must be cleaned out " - "first.\n", s); - break; - - default: - warn(NULL); - break; - } -} - -/* - * Fetch disklabel for disk. - * Use ioctl to get label unless -r flag is given. - */ -struct disklabel32 * -readlabel(int f) -{ - const char *msg; - struct disklabel32 *lp; - int r; - - if (rflag) { - r = read(f, bootarea, BBSIZE); - if (r < BBSIZE) - err(4, "%s", specname); - for (lp = (struct disklabel32 *)bootarea; - lp <= (struct disklabel32 *)(bootarea + BBSIZE - sizeof(*lp)); - lp = (struct disklabel32 *)((char *)lp + 16)) { - if (lp->d_magic == DISKMAGIC32 && - lp->d_magic2 == DISKMAGIC32) - break; - } - if (lp > (struct disklabel32 *)(bootarea+BBSIZE-sizeof(*lp)) || - lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32 || - dkcksum32(lp) != 0) { - errx(1, "bad pack magic number (label is damaged, " - "or pack is unlabeled)"); - } - if ((msg = fixlabel(f, lp, 0)) != NULL) - errx(1, msg); - } else { - lp = &lab; - if (ioctl(f, DIOCGDINFO32, lp) < 0) { - l_perror("ioctl DIOCGDINFO32"); - exit(4); - } - } - return (lp); -} - -/* - * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' - * Returns a pointer to the disklabel portion of the bootarea. - */ -struct disklabel32 * -makebootarea(char *boot, struct disklabel32 *dp, int f) -{ - struct disklabel32 *lp; - char *p; - int b; -#if NUMBOOT > 0 - struct stat sb; -#endif -#ifdef __i386__ - char *tmpbuf; - unsigned int i, found; -#endif - - /* XXX */ - if (dp->d_secsize == 0) { - dp->d_secsize = DEV_BSIZE; - dp->d_bbsize = BBSIZE; - } - lp = (struct disklabel32 *) - (boot + (LABELSECTOR32 * dp->d_secsize) + LABELOFFSET32); - 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) { - if (!xxboot) { - sprintf(boot0, "%s/boot1", _PATH_BOOTDIR); - xxboot = boot0; - } -#if NUMBOOT > 1 - if (!bootxx) { - 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); - 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) */ - if (read(b, boot, (int)dp->d_bbsize) < 0) - err(4, "%s", xxboot); - 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 */ - 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 disklabel32); p++) - if (*p) - errx(2, "bootstrap doesn't leave room for disk label"); - return (lp); -} - -void -display(FILE *f, const struct disklabel32 *lp) -{ - int i, j; - const struct partition32 *pp; - - fprintf(f, "# %s:\n", specname); - if (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:"); - 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 = NDDATA32 - 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\n"); - pp = lp->d_partitions; - for (i = 0; i < lp->d_npartitions; i++, pp++) { - if (pp->p_size) { - u_long onemeg = 1024 * 1024 / lp->d_secsize; - fprintf(f, " %c: ", 'a' + i); - - fprintf(f, "%10lu ", (u_long)pp->p_size); - fprintf(f, "%10lu ", (u_long)pp->p_offset); - if (pp->p_fstype < FSMAXTYPES) - fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); - else - fprintf(f, "%8d", pp->p_fstype); - - fprintf(f, "\t# %11.3fMB", (double)pp->p_size / onemeg); - fprintf(f, "\n"); - } - } - fflush(f); -} - -int -edit(struct disklabel32 *lp, int f) -{ - int c, fd; - struct disklabel32 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); - 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; - } - unlink(tmpfil); - return (1); -} - -int -editit(void) -{ - int pid, xpid; - int status, omask; - const char *ed; - - 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) { - sigsetmask(omask); - setgid(getgid()); - setuid(getuid()); - if ((ed = getenv("EDITOR")) == NULL) - ed = DEFEDITOR; - execlp(ed, ed, tmpfil, NULL); - err(1, "%s", ed); - } - while ((xpid = wait(&status)) >= 0) - if (xpid == pid) - break; - sigsetmask(omask); - return(!status); -} - -char * -skip(char *cp) -{ - - while (*cp != '\0' && isspace(*cp)) - cp++; - if (*cp == '\0' || *cp == '#') - return (NULL); - return (cp); -} - -char * -word(char *cp) -{ - char c; - - while (*cp != '\0' && !isspace(*cp) && *cp != '#') - cp++; - if ((c = *cp) != '\0') { - *cp++ = '\0'; - if (c != '#') - return (skip(cp)); - } - return (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(FILE *f, struct disklabel32 *lp) -{ - char *cp; - const char **cpp; - u_int part; - char *tp, line[BUFSIZ]; - u_long v; - int lineno = 0, errors = 0; - int i; - char empty[] = ""; - char unknown[] = "unknown"; - - bzero(&part_set, sizeof(part_set)); - bzero(&part_size_type, sizeof(part_size_type)); - bzero(&part_offset_type, sizeof(part_offset_type)); - lp->d_bbsize = BBSIZE; /* XXX */ - lp->d_sbsize = SBSIZE; /* XXX */ - while (fgets(line, sizeof(line) - 1, f)) { - lineno++; - if ((cp = strchr(line,'\n')) != 0) - *cp = '\0'; - cp = skip(line); - if (cp == NULL) - continue; - tp = strchr(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 (*cpp && strcasecmp(*cpp, tp) == 0) { - lp->d_type = cpp - dktypenames; - break; - } - } - if (cpp < &dktypenames[DKMAXTYPES]) - continue; - v = strtoul(tp, NULL, 10); - if (v >= DKMAXTYPES) { - fprintf(stderr, "line %d:%s %lu\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 |= 0; /* obsolete */ - else if (streq(cp, "ecc")) - v |= 0; /* obsolete */ - else if (streq(cp, "badsect")) - v |= 0; /* obsolete */ - else { - fprintf(stderr, - "line %d: %s: bad flag\n", - lineno, cp); - errors++; - } - } - lp->d_flags = v; - continue; - } - if (streq(cp, "drivedata")) { - for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA32;) { - lp->d_drivedata[i++] = strtoul(cp, NULL, 10); - tp = word(cp); - } - continue; - } - if (sscanf(cp, "%lu partitions", &v) == 1) { - if (v == 0 || v > MAXPARTITIONS32) { - fprintf(stderr, - "line %d: bad # of partitions\n", lineno); - lp->d_npartitions = MAXPARTITIONS32; - errors++; - } else - lp->d_npartitions = v; - continue; - } - if (tp == NULL) - tp = empty; - 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 = strtoul(tp, NULL, 10); - 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 = strtoul(tp, NULL, 10); -#if (ULONG_MAX != 0xffffffffUL) - if (v == 0 || v > 0xffffffff) { -#else - if (v == 0) { -#endif - fprintf(stderr, "line %d: %s: bad %s\n", - lineno, tp, cp); - errors++; - } else - lp->d_nsectors = v; - continue; - } - if (streq(cp, "sectors/cylinder")) { - v = strtoul(tp, NULL, 10); - 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 = strtoul(tp, NULL, 10); - 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 = strtoul(tp, NULL, 10); - 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 = strtoul(tp, NULL, 10); - 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 = strtoul(tp, NULL, 10); - if (v == 0 || v > USHRT_MAX) { - fprintf(stderr, "line %d: %s: bad %s\n", - lineno, tp, cp); - errors++; - } else - lp->d_rpm = v; - continue; - } - if (streq(cp, "interleave")) { - v = strtoul(tp, NULL, 10); - if (v == 0 || v > USHRT_MAX) { - fprintf(stderr, "line %d: %s: bad %s\n", - lineno, tp, cp); - errors++; - } else - lp->d_interleave = v; - continue; - } - if (streq(cp, "trackskew")) { - v = strtoul(tp, NULL, 10); - if (v > USHRT_MAX) { - fprintf(stderr, "line %d: %s: bad %s\n", - lineno, tp, cp); - errors++; - } else - lp->d_trackskew = v; - continue; - } - if (streq(cp, "cylinderskew")) { - v = strtoul(tp, NULL, 10); - if (v > USHRT_MAX) { - fprintf(stderr, "line %d: %s: bad %s\n", - lineno, tp, cp); - errors++; - } else - lp->d_cylskew = v; - continue; - } - if (streq(cp, "headswitch")) { - v = strtoul(tp, NULL, 10); - lp->d_headswitch = v; - continue; - } - if (streq(cp, "track-to-track seek")) { - v = strtoul(tp, NULL, 10); - lp->d_trkseek = v; - continue; - } - /* the ':' was removed above */ - if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { - fprintf(stderr, - "line %d: %s: Unknown disklabel field\n", lineno, - cp); - errors++; - continue; - } - - /* Process a partition specification line. */ - part = *cp - 'a'; - if (part >= lp->d_npartitions) { - fprintf(stderr, - "line %d: partition name out of range a-%c: %s\n", - lineno, 'a' + lp->d_npartitions - 1, cp); - errors++; - continue; - } - part_set[part] = 1; - - if (getasciipartspec(tp, lp, part, lineno) != 0) { - errors++; - break; - } - } - errors += checklabel(lp); - return (errors == 0); -} - -#define NXTNUM(n) do { \ - if (tp == NULL) { \ - fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ - return (1); \ - } else { \ - cp = tp, tp = word(cp); \ - (n) = strtoul(cp, NULL, 10); \ - } \ -} while (0) - -/* retain 1 character following number */ -#define NXTWORD(w,n) do { \ - if (tp == NULL) { \ - fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ - return (1); \ - } else { \ - char *tmp; \ - cp = tp, tp = word(cp); \ - (n) = strtoul(cp, &tmp, 10); \ - if (tmp) (w) = *tmp; \ - } \ -} while (0) - -/* - * Read a partition line into partition `part' in the specified disklabel. - * Return 0 on success, 1 on failure. - */ -int -getasciipartspec(char *tp, struct disklabel32 *lp, int part, int lineno) -{ - struct partition32 *pp; - char *cp; - const char **cpp; - u_long v; - - pp = &lp->d_partitions[part]; - cp = NULL; - - /* - * size - */ - v = 0; - NXTWORD(part_size_type[part],v); - if (v == 0 && part_size_type[part] != '*') { - fprintf(stderr, - "line %d: %s: bad partition size\n", lineno, cp); - return (1); - } - pp->p_size = v; - - /* - * offset - */ - v = 0; - NXTWORD(part_offset_type[part],v); - if (v == 0 && part_offset_type[part] != '*' && - part_offset_type[part] != '\0') { - fprintf(stderr, - "line %d: %s: bad partition offset\n", lineno, cp); - return (1); - } - pp->p_offset = v; - - /* - * fstype - */ - if (tp == NULL) { - fprintf(stderr, - "line %d: no filesystem type was specified\n", lineno); - return(1); - } - cp = tp; - tp = word(cp); - for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) { - if (*cpp && strcasecmp(*cpp, cp) == 0) - break; - } - if (*cpp != NULL) { - pp->p_fstype = cpp - fstypenames; - } else { - if (isdigit(*cp)) - v = strtoul(cp, NULL, 10); - else - v = FSMAXTYPES; - if (v >= FSMAXTYPES) { - fprintf(stderr, - "line %d: Warning, unknown filesystem type %s\n", - lineno, cp); - v = FS_UNUSED; - } - pp->p_fstype = v; - } - - pp->p_fsize = 0; - pp->p_frag = 0; - pp->p_cpg = 0; - - cp = tp; - if (tp) { - fprintf(stderr, "line %d: Warning, fragment, block, " - "and bps/cpg fields are no\n" - "longer supported and must be specified " - "via newfs options instead.\n", - lineno); - } - return(0); -} - -/* - * Check disklabel for errors and fill in - * derived fields according to supplied values. - */ -int -checklabel(struct disklabel32 *lp) -{ - struct partition32 *pp; - int i, errors = 0; - char part; - u_long base_offset, needed, total_size, total_percent, current_offset; - long free_space; - int seen_default_offset; - int hog_part; - int j; - struct partition32 *pp2; - - 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 > MAXPARTITIONS32) - Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", - (u_long)lp->d_npartitions, MAXPARTITIONS32); - - /* first allocate space to the partitions, then offsets */ - total_size = 0; /* in sectors */ - total_percent = 0; /* in percent */ - hog_part = -1; - /* find all fixed partitions */ - for (i = 0; i < lp->d_npartitions; i++) { - pp = &lp->d_partitions[i]; - if (part_set[i]) { - - if (part_size_type[i] == '*') { - if (i == RAW_PART) { - pp->p_size = lp->d_secperunit; - } else { - if (part_offset_type[i] != '*') { - if (total_size < pp->p_offset) - total_size = pp->p_offset; - } - if (hog_part != -1) - Warning("Too many '*' partitions (%c and %c)", - hog_part + 'a',i + 'a'); - else - hog_part = i; - } - } else { - off_t size; - - size = pp->p_size; - switch (part_size_type[i]) { - case '%': - total_percent += size; - break; - case 'k': - case 'K': - size *= 1024ULL; - break; - case 'm': - case 'M': - size *= 1024ULL * 1024ULL; - break; - case 'g': - case 'G': - size *= 1024ULL * 1024ULL * 1024ULL; - break; - case '\0': - break; - default: - Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); - break; - } - /* don't count %'s yet */ - if (part_size_type[i] != '%') { - /* - * for all not in sectors, convert to - * sectors - */ - if (part_size_type[i] != '\0') { - if (size % lp->d_secsize != 0) - Warning("partition %c not an integer number of sectors", - i + 'a'); - size /= lp->d_secsize; - pp->p_size = size; - } - /* else already in sectors */ - if (i != RAW_PART) - total_size += size; - } - } - } - } - - /* Find out the total free space, excluding the boot block area. */ - base_offset = BBSIZE / lp->d_secsize; - free_space = 0; - for (i = 0; i < lp->d_npartitions; i++) { - pp = &lp->d_partitions[i]; - if (!part_set[i] || i == RAW_PART || - part_size_type[i] == '%' || part_size_type[i] == '*') - continue; - if (pp->p_offset > base_offset) - free_space += pp->p_offset - base_offset; - if (pp->p_offset + pp->p_size > base_offset) - base_offset = pp->p_offset + pp->p_size; - } - if (base_offset < lp->d_secperunit) - free_space += lp->d_secperunit - base_offset; - - /* handle % partitions - note %'s don't need to add up to 100! */ - if (total_percent != 0) { - if (total_percent > 100) { - fprintf(stderr,"total percentage %lu is greater than 100\n", - total_percent); - errors++; - } - - if (free_space > 0) { - for (i = 0; i < lp->d_npartitions; i++) { - pp = &lp->d_partitions[i]; - if (part_set[i] && part_size_type[i] == '%') { - /* careful of overflows! and integer roundoff */ - pp->p_size = ((double)pp->p_size/100) * free_space; - total_size += pp->p_size; - - /* FIX we can lose a sector or so due to roundoff per - partition. A more complex algorithm could avoid that */ - } - } - } else { - fprintf(stderr, - "%ld sectors available to give to '*' and '%%' partitions\n", - free_space); - errors++; - /* fix? set all % partitions to size 0? */ - } - } - /* give anything remaining to the hog partition */ - if (hog_part != -1) { - /* - * Find the range of offsets usable by '*' partitions around - * the hog partition and how much space they need. - */ - needed = 0; - base_offset = BBSIZE / lp->d_secsize; - for (i = hog_part - 1; i >= 0; i--) { - pp = &lp->d_partitions[i]; - if (!part_set[i] || i == RAW_PART) - continue; - if (part_offset_type[i] == '*') { - needed += pp->p_size; - continue; - } - base_offset = pp->p_offset + pp->p_size; - break; - } - current_offset = lp->d_secperunit; - for (i = lp->d_npartitions - 1; i > hog_part; i--) { - pp = &lp->d_partitions[i]; - if (!part_set[i] || i == RAW_PART) - continue; - if (part_offset_type[i] == '*') { - needed += pp->p_size; - continue; - } - current_offset = pp->p_offset; - } - - if (current_offset - base_offset <= needed) { - fprintf(stderr, "Cannot find space for partition %c\n", - hog_part + 'a'); - fprintf(stderr, - "Need more than %lu sectors between %lu and %lu\n", - needed, base_offset, current_offset); - errors++; - lp->d_partitions[hog_part].p_size = 0; - } else { - lp->d_partitions[hog_part].p_size = current_offset - - base_offset - needed; - total_size += lp->d_partitions[hog_part].p_size; - } - } - - /* Now set the offsets for each partition */ - current_offset = BBSIZE / lp->d_secsize; /* in sectors */ - seen_default_offset = 0; - for (i = 0; i < lp->d_npartitions; i++) { - part = 'a' + i; - pp = &lp->d_partitions[i]; - if (part_set[i]) { - if (part_offset_type[i] == '*') { - if (i == RAW_PART) { - pp->p_offset = 0; - } else { - pp->p_offset = current_offset; - seen_default_offset = 1; - } - } else { - /* allow them to be out of order for old-style tables */ - if (pp->p_offset < current_offset && - seen_default_offset && i != RAW_PART && - pp->p_fstype != FS_VINUM) { - fprintf(stderr, -"Offset %ld for partition %c overlaps previous partition which ends at %lu\n", - (long)pp->p_offset,i+'a',current_offset); - fprintf(stderr, -"Labels with any *'s for offset must be in ascending order by sector\n"); - errors++; - } else if (pp->p_offset != current_offset && - i != RAW_PART && seen_default_offset) { - /* - * this may give unneeded warnings if - * partitions are out-of-order - */ - Warning( -"Offset %ld for partition %c doesn't match expected value %ld", - (long)pp->p_offset, i + 'a', current_offset); - } - } - if (i != RAW_PART) - current_offset = pp->p_offset + pp->p_size; - } - } - - 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++; - } - if (i == RAW_PART) - { - if (pp->p_fstype != FS_UNUSED) - Warning("partition %c is not marked as unused!",part); - if (pp->p_offset != 0) - Warning("partition %c doesn't start at 0!",part); - if (pp->p_size != lp->d_secperunit) - Warning("partition %c doesn't cover the whole unit!",part); - - if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || - (pp->p_size != lp->d_secperunit)) { - Warning("An incorrect partition %c may cause problems for " - "standard system utilities",part); - } - } - - /* check for overlaps */ - /* this will check for all possible overlaps once and only once */ - for (j = 0; j < i; j++) { - pp2 = &lp->d_partitions[j]; - if (j != RAW_PART && i != RAW_PART && - pp->p_fstype != FS_VINUM && - pp2->p_fstype != FS_VINUM && - part_set[i] && part_set[j]) { - if (pp2->p_offset < pp->p_offset + pp->p_size && - (pp2->p_offset + pp2->p_size > pp->p_offset || - pp2->p_offset >= pp->p_offset)) { - fprintf(stderr,"partitions %c and %c overlap!\n", - j + 'a', i + 'a'); - errors++; - } - } - } - } - for (; i < 8 || i < lp->d_npartitions; 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. - */ -static struct disklabel32 dlab; - -struct disklabel32 * -getvirginlabel(void) -{ - struct partinfo info; - struct disklabel32 *dl = &dlab; - int f; - - if ((f = open(dkname, O_RDONLY)) == -1) { - warn("cannot open %s", dkname); - return (NULL); - } - - /* - * Check to see if the media is too big for a 32 bit disklabel. - */ - if (ioctl(f, DIOCGPART, &info) == 0) { - if (info.media_size >= 0x100000000ULL * 512) { - warnx("The media is too large for a 32 bit disklabel," - " please use disklabel64."); - return (NULL); - } - } - - /* - * Generate a virgin disklabel via ioctl - */ - if (ioctl(f, DIOCGDVIRGIN32, dl) < 0) { - l_perror("ioctl DIOCGDVIRGIN32"); - close(f); - return(NULL); - } - close(f); - return (dl); -} - -struct disklabel32 * -getdisklabelfromdisktab(const char *name) -{ - struct disktab *dt; - struct disklabel32 *dl = &dlab; - int i; - - if ((dt = getdisktabbyname(name)) == NULL) - return(NULL); - dl->d_magic = DISKMAGIC32; - dl->d_type = dt->d_typeid; - dl->d_subtype = 0; - dl->d_secsize = dt->d_media_blksize; - dl->d_nsectors = dt->d_secpertrack; - dl->d_ntracks = dt->d_nheads; - dl->d_ncylinders = dt->d_ncylinders; - dl->d_secpercyl = dt->d_secpercyl; - dl->d_secperunit = dt->d_media_blocks; - dl->d_rpm = dt->d_rpm; - dl->d_interleave = dt->d_interleave; - dl->d_trackskew = dt->d_trackskew; - dl->d_cylskew = dt->d_cylskew; - dl->d_headswitch = dt->d_headswitch; - dl->d_trkseek = dt->d_trkseek; - dl->d_magic2 = DISKMAGIC32; - dl->d_npartitions = dt->d_npartitions; - dl->d_bbsize = dt->d_bbsize; - dl->d_sbsize = dt->d_sbsize; - for (i = 0; i < dt->d_npartitions; ++i) { - struct partition32 *dlp = &dl->d_partitions[i]; - struct dt_partition *dtp = &dt->d_partitions[i]; - - dlp->p_size = dtp->p_size; - dlp->p_offset = dtp->p_offset; - dlp->p_fsize = dtp->p_fsize; - dlp->p_fstype = dtp->p_fstype; - dlp->p_frag = dtp->p_fsize; - } - return(dl); -} - -/* - * 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(struct disklabel32 *lp) -{ - struct partition32 *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(const char *fmt, ...) -{ - va_list ap; - - fprintf(stderr, "Warning, "); - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); - va_end(ap); -} - -/* - * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks - * and earlier DFly bb's are packed against the old disklabel and a new - * disklabel would blow them up. This is a hack that should be removed - * in 2006 sometime (if ever). - */ - -int -checkoldboot(int f, const char *bootbuffer) -{ - char buf[BBSIZE]; - - if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0) - return(0); - lseek(f, (off_t)0, SEEK_SET); - if (read(f, buf, sizeof(buf)) != sizeof(buf)) - return(0); - if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */ - return(0); - if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */ - return(1); - return(0); -} - -/* - * Traditional 32 bit disklabels actually use absolute sector numbers on - * disk, NOT slice relative sector numbres. The OS hides this from us - * when we use DIOC ioctls to access the label, but newer versions of - * Dragonfly no longer adjusts the disklabel when snooping reads or writes - * so we have to figure it out ourselves. - */ -const char * -fixlabel(int f, struct disklabel32 *lp, int writeadj) -{ - const char *msg = NULL; - struct partinfo info; - struct partition32 *pp; - u_int64_t start; - u_int64_t end; - u_int64_t offset; - int part; - int rev; - size_t rev_len = sizeof(rev); - - if (sysctlbyname("kern.osrevision", &rev, &rev_len, NULL, 0) < 0) { - errx(1, "Cannot use raw mode on non-DragonFly systems\n"); - } - if (rev < 200701) { - warnx("Warning running new disklabel on old DragonFly systems,\n" - "assuming the disk layer will fixup the label.\n"); - sleep(3); - return(NULL); - } - - pp = &lp->d_partitions[RAW_PART]; - - if (forceflag) { - info.media_offset = slice_start_lba * lp->d_secsize; - info.media_blocks = pp->p_size; - info.media_blksize = lp->d_secsize; - } else if (ioctl(f, DIOCGPART, &info) < 0) { - msg = "Unable to extract the slice starting LBA, " - "you must use the -f option\n" - "to specify it manually, or perhaps try without " - "using -r and let the kernel deal with it\n"; - return(msg); - } - - if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32) - return ("fixlabel: invalid magic"); - if (dkcksum32(lp) != 0) - return ("fixlabel: invalid checksum"); - - /* - * What a mess. For ages old backwards compatibility the disklabel - * on-disk stores absolute offsets instead of slice-relative offsets. - * So fix it up when reading, writing, or snooping. - * - * The in-core label is always slice-relative. - */ - if (writeadj) { - /* - * incore -> disk - */ - start = 0; - offset = info.media_offset / info.media_blksize; - } else { - /* - * disk -> incore - */ - start = info.media_offset / info.media_blksize; - offset = -info.media_offset / info.media_blksize; - } - if (pp->p_offset != start) - return ("fixlabel: raw partition offset != slice offset"); - if (pp->p_size != info.media_blocks) { - if (pp->p_size > info.media_blocks) - return ("fixlabel: raw partition size > slice size"); - } - end = start + info.media_blocks; - if (start > end) - return ("fixlabel: slice wraps"); - if (lp->d_secpercyl <= 0) - return ("fixlabel: d_secpercyl <= 0"); - pp -= RAW_PART; - for (part = 0; part < lp->d_npartitions; part++, pp++) { - if (pp->p_offset != 0 || pp->p_size != 0) { - if (pp->p_offset < start - || pp->p_offset + pp->p_size > end - || pp->p_offset + pp->p_size < pp->p_offset) { - /* XXX else silently discard junk. */ - bzero(pp, sizeof *pp); - } else { - pp->p_offset += offset; - } - } - } - lp->d_checksum = 0; - lp->d_checksum = dkcksum32(lp); - return (NULL); -} - -void -usage(void) -{ -#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] [-n] disk type [packid]", - "\t\t(to write label with existing boot program)", - " disklabel -e [-r] [-n] disk", - "\t\t(to edit label)", - " disklabel -R [-r] [-n] disk protofile", - "\t\t(to restore label with existing boot program)", -#if NUMBOOT > 1 - " disklabel -B [-n] [-b boot1 -s boot2] disk [type]", - "\t\t(to install boot program with existing label)", - " disklabel -w -B [-n] [-b boot1 -s boot2] disk type [packid]", - "\t\t(to write label and boot program)", - " disklabel -R -B [-n] [-b boot1 -s boot2] disk protofile [type]", - "\t\t(to restore label and boot program)", -#else - " disklabel -B [-n] [-b bootprog] disk [type]", - "\t\t(to install boot program with existing on-disk label)", - " disklabel -w -B [-n] [-b bootprog] disk type [packid]", - "\t\t(to write label and install boot program)", - " disklabel -R -B [-n] [-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] [-n] disk type [packid]", - "\t\t(to write label)", - " disklabel -e [-r] [-n] disk", - "\t\t(to edit label)", - " disklabel -R [-r] [-n] disk protofile", - "\t\t(to restore label)", - " disklabel [-NW] disk", - "\t\t(to write disable/enable label)"); -#endif - fprintf(stderr, "%s\n%s\n", - " disklabel [-f slice_start_lba] [options]", - "\t\t(to force using manual offset)"); - exit(1); -} diff --git a/sbin/disklabel/pathnames.h b/sbin/disklabel/pathnames.h deleted file mode 100644 index d66074b..0000000 --- a/sbin/disklabel/pathnames.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 1989, 1993 - * The Regents of the University of California. 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 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. - * - * @(#)pathnames.h 8.1 (Berkeley) 6/5/93 - * $FreeBSD: src/sbin/disklabel/pathnames.h,v 1.4 2000/01/11 07:28:46 bde Exp $ - * $DragonFly: src/sbin/disklabel/pathnames.h,v 1.2 2003/06/17 04:27:32 dillon Exp $ - */ - -#include - -#define _PATH_BOOTDIR "/boot" -#define PATH_TMPFILE "/tmp/EdDk.XXXXXXXXXX" diff --git a/sbin/disklabel32/Makefile b/sbin/disklabel32/Makefile new file mode 100644 index 0000000..5435f60 --- /dev/null +++ b/sbin/disklabel32/Makefile @@ -0,0 +1,9 @@ +# @(#)Makefile 8.2 (Berkeley) 3/17/94 +# $FreeBSD: src/sbin/disklabel/Makefile,v 1.3.14.3 2001/04/25 10:58:15 ru Exp $ +# $DragonFly: src/sbin/disklabel/Makefile,v 1.5 2006/10/17 02:18:51 pavalos Exp $ + +PROG= disklabel32 +SRCS= disklabel.c +MAN= disklabel32.5 disklabel32.8 + +.include diff --git a/sbin/disklabel32/disklabel.c b/sbin/disklabel32/disklabel.c new file mode 100644 index 0000000..5c51819 --- /dev/null +++ b/sbin/disklabel32/disklabel.c @@ -0,0 +1,1822 @@ +/* + * 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. + * + * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved. + * @(#)disklabel.c 1.2 (Symmetric) 11/28/85 + * @(#)disklabel.c 8.2 (Berkeley) 1/7/94 + * $FreeBSD: src/sbin/disklabel/disklabel.c,v 1.28.2.15 2003/01/24 16:18:16 des Exp $ + * $DragonFly: src/sbin/disklabel/disklabel.c,v 1.28 2008/08/21 21:22:36 thomas Exp $ + */ + +#include +#include +#include +#include +#define DKTYPENAMES +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#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 + +/* FIX! These are too low, but are traditional */ +#define DEFAULT_NEWFS_BLOCK 8192U +#define DEFAULT_NEWFS_FRAG 1024U +#define DEFAULT_NEWFS_CPG 16U + +#define BIG_NEWFS_BLOCK 16384U +#define BIG_NEWFS_FRAG 2048U +#define BIG_NEWFS_CPG 64U + +#define NUMBOOT 2 + +void makelabel(const char *, const char *, struct disklabel32 *); +int writelabel(int, const char *, struct disklabel32 *); +void l_perror(const char *); +struct disklabel32 *readlabel(int); +struct disklabel32 *makebootarea(char *, struct disklabel32 *, int); +void display(FILE *, const struct disklabel32 *); +int edit(struct disklabel32 *, int); +int editit(void); +char *skip(char *); +char *word(char *); +int getasciilabel(FILE *, struct disklabel32 *); +int getasciipartspec(char *, struct disklabel32 *, int, int); +int checklabel(struct disklabel32 *); +void setbootflag(struct disklabel32 *); +void Warning(const char *, ...) __printflike(1, 2); +void usage(void); +int checkoldboot(int, const char *); +const char *fixlabel(int, struct disklabel32 *, int); +struct disklabel32 *getvirginlabel(void); +struct disklabel32 *getdisklabelfromdisktab(const char *name); + +#define DEFEDITOR _PATH_VI +#define streq(a,b) (strcmp(a,b) == 0) + +char *dkname; +char *specname; +char tmpfil[] = PATH_TMPFILE; + +char namebuf[BBSIZE]; +struct disklabel32 lab; +char bootarea[BBSIZE]; + +#define MAX_PART ('z') +#define MAX_NUM_PARTS (1 + MAX_PART - 'a') +char part_size_type[MAX_NUM_PARTS]; +char part_offset_type[MAX_NUM_PARTS]; +int part_set[MAX_NUM_PARTS]; + +#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; +int disable_write; /* set to disable writing to disk label */ +int forceflag; +u_int32_t slice_start_lba; + +#ifdef DEBUG +int debug; +#define OPTIONS "BNRWb:def:nrs:w" +#else +#define OPTIONS "BNRWb:ef:nrs:w" +#endif + +int +main(int argc, char *argv[]) +{ + struct disklabel32 *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; + + case 'f': + forceflag = 1; + slice_start_lba = strtoul(optarg, NULL, 0); + break; +#if NUMBOOT > 1 + case 's': + bootxx = optarg; + break; +#endif +#endif + case 'N': + if (op != UNSPEC) + usage(); + op = NOWRITE; + break; + case 'n': + disable_write = 1; + 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 = getdevpath(argv[0], 0); + specname = dkname; + 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); + if (checkoldboot(f, NULL)) + warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel"); + 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 disklabel32 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(const char *type, const char *name, struct disklabel32 *lp) +{ + struct disklabel32 *dp; + + if (strcmp(type, "auto") == 0) + dp = getvirginlabel(); + else + dp = getdisklabelfromdisktab(type); + if (dp == NULL) + errx(1, "%s: unknown disk type", type); + *lp = *dp; + + /* + * NOTE: boot control files may no longer be specified in disktab. + */ + if (name) + strncpy(lp->d_packname, name, sizeof(lp->d_packname)); +} + +int +writelabel(int f, const char *boot, struct disklabel32 *lp) +{ + const char *msg; + int flag; + int r; + + if (disable_write) { + Warning("write to disk label suppressed - label was as follows:"); + display(stdout, lp); + return (0); + } else { + /* make sure we are not overwriting our boot code */ + if (checkoldboot(f, boot)) + errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!"); + setbootflag(lp); + lp->d_magic = DISKMAGIC32; + lp->d_magic2 = DISKMAGIC32; + lp->d_checksum = 0; + lp->d_checksum = dkcksum32(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, DIOCSDINFO32, lp) < 0 && + errno != ENODEV && errno != ENOTTY) { + l_perror("ioctl DIOCSDINFO32"); + return (1); + } + lseek(f, (off_t)0, SEEK_SET); + + /* + * write enable label sector before write + * (if necessary), disable after writing. + */ + flag = 1; + if (ioctl(f, DIOCWLABEL, &flag) < 0) + warn("ioctl DIOCWLABEL"); + msg = fixlabel(f, lp, 1); + if (msg) { + warn(msg); + return (1); + } + r = write(f, boot, lp->d_bbsize); + fixlabel(f, lp, 0); + if (r != ((ssize_t)lp->d_bbsize)) { + warn("write"); + return (1); + } +#if NUMBOOT > 0 + /* + * Output the remainder of the disklabel + */ + if (bootbuf) { + fixlabel(f, lp, 1); + r = write(f, bootbuf, bootsize); + fixlabel(f, lp, 0); + if (r != bootsize) { + warn("write"); + return(1); + } + } +#endif + flag = 0; + ioctl(f, DIOCWLABEL, &flag); + } else if (ioctl(f, DIOCWDINFO32, lp) < 0) { + l_perror("ioctl DIOCWDINFO32"); + return (1); + } + } + return (0); +} + +void +l_perror(const char *s) +{ + switch (errno) { + + case ESRCH: + warnx("%s: no disk label on disk;", s); + fprintf(stderr, "add \"-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; + + case ENOATTR: + warnx("%s: the disk already has a label of a different type,\n" + "probably a 64 bit disklabel. It must be cleaned out " + "first.\n", s); + break; + + default: + warn(NULL); + break; + } +} + +/* + * Fetch disklabel for disk. + * Use ioctl to get label unless -r flag is given. + */ +struct disklabel32 * +readlabel(int f) +{ + const char *msg; + struct disklabel32 *lp; + int r; + + if (rflag) { + r = read(f, bootarea, BBSIZE); + if (r < BBSIZE) + err(4, "%s", specname); + for (lp = (struct disklabel32 *)bootarea; + lp <= (struct disklabel32 *)(bootarea + BBSIZE - sizeof(*lp)); + lp = (struct disklabel32 *)((char *)lp + 16)) { + if (lp->d_magic == DISKMAGIC32 && + lp->d_magic2 == DISKMAGIC32) + break; + } + if (lp > (struct disklabel32 *)(bootarea+BBSIZE-sizeof(*lp)) || + lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32 || + dkcksum32(lp) != 0) { + errx(1, "bad pack magic number (label is damaged, " + "or pack is unlabeled)"); + } + if ((msg = fixlabel(f, lp, 0)) != NULL) + errx(1, msg); + } else { + lp = &lab; + if (ioctl(f, DIOCGDINFO32, lp) < 0) { + l_perror("ioctl DIOCGDINFO32"); + exit(4); + } + } + return (lp); +} + +/* + * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot'' + * Returns a pointer to the disklabel portion of the bootarea. + */ +struct disklabel32 * +makebootarea(char *boot, struct disklabel32 *dp, int f) +{ + struct disklabel32 *lp; + char *p; + int b; +#if NUMBOOT > 0 + struct stat sb; +#endif +#ifdef __i386__ + char *tmpbuf; + unsigned int i, found; +#endif + + /* XXX */ + if (dp->d_secsize == 0) { + dp->d_secsize = DEV_BSIZE; + dp->d_bbsize = BBSIZE; + } + lp = (struct disklabel32 *) + (boot + (LABELSECTOR32 * dp->d_secsize) + LABELOFFSET32); + 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) { + if (!xxboot) { + sprintf(boot0, "%s/boot1", _PATH_BOOTDIR); + xxboot = boot0; + } +#if NUMBOOT > 1 + if (!bootxx) { + 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); + 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) */ + if (read(b, boot, (int)dp->d_bbsize) < 0) + err(4, "%s", xxboot); + 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 */ + 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 disklabel32); p++) + if (*p) + errx(2, "bootstrap doesn't leave room for disk label"); + return (lp); +} + +void +display(FILE *f, const struct disklabel32 *lp) +{ + int i, j; + const struct partition32 *pp; + + fprintf(f, "# %s:\n", specname); + if (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:"); + 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 = NDDATA32 - 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\n"); + pp = lp->d_partitions; + for (i = 0; i < lp->d_npartitions; i++, pp++) { + if (pp->p_size) { + u_long onemeg = 1024 * 1024 / lp->d_secsize; + fprintf(f, " %c: ", 'a' + i); + + fprintf(f, "%10lu ", (u_long)pp->p_size); + fprintf(f, "%10lu ", (u_long)pp->p_offset); + if (pp->p_fstype < FSMAXTYPES) + fprintf(f, "%8.8s", fstypenames[pp->p_fstype]); + else + fprintf(f, "%8d", pp->p_fstype); + + fprintf(f, "\t# %11.3fMB", (double)pp->p_size / onemeg); + fprintf(f, "\n"); + } + } + fflush(f); +} + +int +edit(struct disklabel32 *lp, int f) +{ + int c, fd; + struct disklabel32 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); + 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; + } + unlink(tmpfil); + return (1); +} + +int +editit(void) +{ + int pid, xpid; + int status, omask; + const char *ed; + + 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) { + sigsetmask(omask); + setgid(getgid()); + setuid(getuid()); + if ((ed = getenv("EDITOR")) == NULL) + ed = DEFEDITOR; + execlp(ed, ed, tmpfil, NULL); + err(1, "%s", ed); + } + while ((xpid = wait(&status)) >= 0) + if (xpid == pid) + break; + sigsetmask(omask); + return(!status); +} + +char * +skip(char *cp) +{ + + while (*cp != '\0' && isspace(*cp)) + cp++; + if (*cp == '\0' || *cp == '#') + return (NULL); + return (cp); +} + +char * +word(char *cp) +{ + char c; + + while (*cp != '\0' && !isspace(*cp) && *cp != '#') + cp++; + if ((c = *cp) != '\0') { + *cp++ = '\0'; + if (c != '#') + return (skip(cp)); + } + return (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(FILE *f, struct disklabel32 *lp) +{ + char *cp; + const char **cpp; + u_int part; + char *tp, line[BUFSIZ]; + u_long v; + int lineno = 0, errors = 0; + int i; + char empty[] = ""; + char unknown[] = "unknown"; + + bzero(&part_set, sizeof(part_set)); + bzero(&part_size_type, sizeof(part_size_type)); + bzero(&part_offset_type, sizeof(part_offset_type)); + lp->d_bbsize = BBSIZE; /* XXX */ + lp->d_sbsize = SBSIZE; /* XXX */ + while (fgets(line, sizeof(line) - 1, f)) { + lineno++; + if ((cp = strchr(line,'\n')) != 0) + *cp = '\0'; + cp = skip(line); + if (cp == NULL) + continue; + tp = strchr(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 (*cpp && strcasecmp(*cpp, tp) == 0) { + lp->d_type = cpp - dktypenames; + break; + } + } + if (cpp < &dktypenames[DKMAXTYPES]) + continue; + v = strtoul(tp, NULL, 10); + if (v >= DKMAXTYPES) { + fprintf(stderr, "line %d:%s %lu\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 |= 0; /* obsolete */ + else if (streq(cp, "ecc")) + v |= 0; /* obsolete */ + else if (streq(cp, "badsect")) + v |= 0; /* obsolete */ + else { + fprintf(stderr, + "line %d: %s: bad flag\n", + lineno, cp); + errors++; + } + } + lp->d_flags = v; + continue; + } + if (streq(cp, "drivedata")) { + for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA32;) { + lp->d_drivedata[i++] = strtoul(cp, NULL, 10); + tp = word(cp); + } + continue; + } + if (sscanf(cp, "%lu partitions", &v) == 1) { + if (v == 0 || v > MAXPARTITIONS32) { + fprintf(stderr, + "line %d: bad # of partitions\n", lineno); + lp->d_npartitions = MAXPARTITIONS32; + errors++; + } else + lp->d_npartitions = v; + continue; + } + if (tp == NULL) + tp = empty; + 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 = strtoul(tp, NULL, 10); + 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 = strtoul(tp, NULL, 10); +#if (ULONG_MAX != 0xffffffffUL) + if (v == 0 || v > 0xffffffff) { +#else + if (v == 0) { +#endif + fprintf(stderr, "line %d: %s: bad %s\n", + lineno, tp, cp); + errors++; + } else + lp->d_nsectors = v; + continue; + } + if (streq(cp, "sectors/cylinder")) { + v = strtoul(tp, NULL, 10); + 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 = strtoul(tp, NULL, 10); + 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 = strtoul(tp, NULL, 10); + 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 = strtoul(tp, NULL, 10); + 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 = strtoul(tp, NULL, 10); + if (v == 0 || v > USHRT_MAX) { + fprintf(stderr, "line %d: %s: bad %s\n", + lineno, tp, cp); + errors++; + } else + lp->d_rpm = v; + continue; + } + if (streq(cp, "interleave")) { + v = strtoul(tp, NULL, 10); + if (v == 0 || v > USHRT_MAX) { + fprintf(stderr, "line %d: %s: bad %s\n", + lineno, tp, cp); + errors++; + } else + lp->d_interleave = v; + continue; + } + if (streq(cp, "trackskew")) { + v = strtoul(tp, NULL, 10); + if (v > USHRT_MAX) { + fprintf(stderr, "line %d: %s: bad %s\n", + lineno, tp, cp); + errors++; + } else + lp->d_trackskew = v; + continue; + } + if (streq(cp, "cylinderskew")) { + v = strtoul(tp, NULL, 10); + if (v > USHRT_MAX) { + fprintf(stderr, "line %d: %s: bad %s\n", + lineno, tp, cp); + errors++; + } else + lp->d_cylskew = v; + continue; + } + if (streq(cp, "headswitch")) { + v = strtoul(tp, NULL, 10); + lp->d_headswitch = v; + continue; + } + if (streq(cp, "track-to-track seek")) { + v = strtoul(tp, NULL, 10); + lp->d_trkseek = v; + continue; + } + /* the ':' was removed above */ + if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') { + fprintf(stderr, + "line %d: %s: Unknown disklabel field\n", lineno, + cp); + errors++; + continue; + } + + /* Process a partition specification line. */ + part = *cp - 'a'; + if (part >= lp->d_npartitions) { + fprintf(stderr, + "line %d: partition name out of range a-%c: %s\n", + lineno, 'a' + lp->d_npartitions - 1, cp); + errors++; + continue; + } + part_set[part] = 1; + + if (getasciipartspec(tp, lp, part, lineno) != 0) { + errors++; + break; + } + } + errors += checklabel(lp); + return (errors == 0); +} + +#define NXTNUM(n) do { \ + if (tp == NULL) { \ + fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ + return (1); \ + } else { \ + cp = tp, tp = word(cp); \ + (n) = strtoul(cp, NULL, 10); \ + } \ +} while (0) + +/* retain 1 character following number */ +#define NXTWORD(w,n) do { \ + if (tp == NULL) { \ + fprintf(stderr, "line %d: too few numeric fields\n", lineno); \ + return (1); \ + } else { \ + char *tmp; \ + cp = tp, tp = word(cp); \ + (n) = strtoul(cp, &tmp, 10); \ + if (tmp) (w) = *tmp; \ + } \ +} while (0) + +/* + * Read a partition line into partition `part' in the specified disklabel. + * Return 0 on success, 1 on failure. + */ +int +getasciipartspec(char *tp, struct disklabel32 *lp, int part, int lineno) +{ + struct partition32 *pp; + char *cp; + const char **cpp; + u_long v; + + pp = &lp->d_partitions[part]; + cp = NULL; + + /* + * size + */ + v = 0; + NXTWORD(part_size_type[part],v); + if (v == 0 && part_size_type[part] != '*') { + fprintf(stderr, + "line %d: %s: bad partition size\n", lineno, cp); + return (1); + } + pp->p_size = v; + + /* + * offset + */ + v = 0; + NXTWORD(part_offset_type[part],v); + if (v == 0 && part_offset_type[part] != '*' && + part_offset_type[part] != '\0') { + fprintf(stderr, + "line %d: %s: bad partition offset\n", lineno, cp); + return (1); + } + pp->p_offset = v; + + /* + * fstype + */ + if (tp == NULL) { + fprintf(stderr, + "line %d: no filesystem type was specified\n", lineno); + return(1); + } + cp = tp; + tp = word(cp); + for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) { + if (*cpp && strcasecmp(*cpp, cp) == 0) + break; + } + if (*cpp != NULL) { + pp->p_fstype = cpp - fstypenames; + } else { + if (isdigit(*cp)) + v = strtoul(cp, NULL, 10); + else + v = FSMAXTYPES; + if (v >= FSMAXTYPES) { + fprintf(stderr, + "line %d: Warning, unknown filesystem type %s\n", + lineno, cp); + v = FS_UNUSED; + } + pp->p_fstype = v; + } + + pp->p_fsize = 0; + pp->p_frag = 0; + pp->p_cpg = 0; + + cp = tp; + if (tp) { + fprintf(stderr, "line %d: Warning, fragment, block, " + "and bps/cpg fields are no\n" + "longer supported and must be specified " + "via newfs options instead.\n", + lineno); + } + return(0); +} + +/* + * Check disklabel for errors and fill in + * derived fields according to supplied values. + */ +int +checklabel(struct disklabel32 *lp) +{ + struct partition32 *pp; + int i, errors = 0; + char part; + u_long base_offset, needed, total_size, total_percent, current_offset; + long free_space; + int seen_default_offset; + int hog_part; + int j; + struct partition32 *pp2; + + 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 > MAXPARTITIONS32) + Warning("number of partitions (%lu) > MAXPARTITIONS (%d)", + (u_long)lp->d_npartitions, MAXPARTITIONS32); + + /* first allocate space to the partitions, then offsets */ + total_size = 0; /* in sectors */ + total_percent = 0; /* in percent */ + hog_part = -1; + /* find all fixed partitions */ + for (i = 0; i < lp->d_npartitions; i++) { + pp = &lp->d_partitions[i]; + if (part_set[i]) { + + if (part_size_type[i] == '*') { + if (i == RAW_PART) { + pp->p_size = lp->d_secperunit; + } else { + if (part_offset_type[i] != '*') { + if (total_size < pp->p_offset) + total_size = pp->p_offset; + } + if (hog_part != -1) + Warning("Too many '*' partitions (%c and %c)", + hog_part + 'a',i + 'a'); + else + hog_part = i; + } + } else { + off_t size; + + size = pp->p_size; + switch (part_size_type[i]) { + case '%': + total_percent += size; + break; + case 'k': + case 'K': + size *= 1024ULL; + break; + case 'm': + case 'M': + size *= 1024ULL * 1024ULL; + break; + case 'g': + case 'G': + size *= 1024ULL * 1024ULL * 1024ULL; + break; + case '\0': + break; + default: + Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]); + break; + } + /* don't count %'s yet */ + if (part_size_type[i] != '%') { + /* + * for all not in sectors, convert to + * sectors + */ + if (part_size_type[i] != '\0') { + if (size % lp->d_secsize != 0) + Warning("partition %c not an integer number of sectors", + i + 'a'); + size /= lp->d_secsize; + pp->p_size = size; + } + /* else already in sectors */ + if (i != RAW_PART) + total_size += size; + } + } + } + } + + /* Find out the total free space, excluding the boot block area. */ + base_offset = BBSIZE / lp->d_secsize; + free_space = 0; + for (i = 0; i < lp->d_npartitions; i++) { + pp = &lp->d_partitions[i]; + if (!part_set[i] || i == RAW_PART || + part_size_type[i] == '%' || part_size_type[i] == '*') + continue; + if (pp->p_offset > base_offset) + free_space += pp->p_offset - base_offset; + if (pp->p_offset + pp->p_size > base_offset) + base_offset = pp->p_offset + pp->p_size; + } + if (base_offset < lp->d_secperunit) + free_space += lp->d_secperunit - base_offset; + + /* handle % partitions - note %'s don't need to add up to 100! */ + if (total_percent != 0) { + if (total_percent > 100) { + fprintf(stderr,"total percentage %lu is greater than 100\n", + total_percent); + errors++; + } + + if (free_space > 0) { + for (i = 0; i < lp->d_npartitions; i++) { + pp = &lp->d_partitions[i]; + if (part_set[i] && part_size_type[i] == '%') { + /* careful of overflows! and integer roundoff */ + pp->p_size = ((double)pp->p_size/100) * free_space; + total_size += pp->p_size; + + /* FIX we can lose a sector or so due to roundoff per + partition. A more complex algorithm could avoid that */ + } + } + } else { + fprintf(stderr, + "%ld sectors available to give to '*' and '%%' partitions\n", + free_space); + errors++; + /* fix? set all % partitions to size 0? */ + } + } + /* give anything remaining to the hog partition */ + if (hog_part != -1) { + /* + * Find the range of offsets usable by '*' partitions around + * the hog partition and how much space they need. + */ + needed = 0; + base_offset = BBSIZE / lp->d_secsize; + for (i = hog_part - 1; i >= 0; i--) { + pp = &lp->d_partitions[i]; + if (!part_set[i] || i == RAW_PART) + continue; + if (part_offset_type[i] == '*') { + needed += pp->p_size; + continue; + } + base_offset = pp->p_offset + pp->p_size; + break; + } + current_offset = lp->d_secperunit; + for (i = lp->d_npartitions - 1; i > hog_part; i--) { + pp = &lp->d_partitions[i]; + if (!part_set[i] || i == RAW_PART) + continue; + if (part_offset_type[i] == '*') { + needed += pp->p_size; + continue; + } + current_offset = pp->p_offset; + } + + if (current_offset - base_offset <= needed) { + fprintf(stderr, "Cannot find space for partition %c\n", + hog_part + 'a'); + fprintf(stderr, + "Need more than %lu sectors between %lu and %lu\n", + needed, base_offset, current_offset); + errors++; + lp->d_partitions[hog_part].p_size = 0; + } else { + lp->d_partitions[hog_part].p_size = current_offset - + base_offset - needed; + total_size += lp->d_partitions[hog_part].p_size; + } + } + + /* Now set the offsets for each partition */ + current_offset = BBSIZE / lp->d_secsize; /* in sectors */ + seen_default_offset = 0; + for (i = 0; i < lp->d_npartitions; i++) { + part = 'a' + i; + pp = &lp->d_partitions[i]; + if (part_set[i]) { + if (part_offset_type[i] == '*') { + if (i == RAW_PART) { + pp->p_offset = 0; + } else { + pp->p_offset = current_offset; + seen_default_offset = 1; + } + } else { + /* allow them to be out of order for old-style tables */ + if (pp->p_offset < current_offset && + seen_default_offset && i != RAW_PART && + pp->p_fstype != FS_VINUM) { + fprintf(stderr, +"Offset %ld for partition %c overlaps previous partition which ends at %lu\n", + (long)pp->p_offset,i+'a',current_offset); + fprintf(stderr, +"Labels with any *'s for offset must be in ascending order by sector\n"); + errors++; + } else if (pp->p_offset != current_offset && + i != RAW_PART && seen_default_offset) { + /* + * this may give unneeded warnings if + * partitions are out-of-order + */ + Warning( +"Offset %ld for partition %c doesn't match expected value %ld", + (long)pp->p_offset, i + 'a', current_offset); + } + } + if (i != RAW_PART) + current_offset = pp->p_offset + pp->p_size; + } + } + + 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++; + } + if (i == RAW_PART) + { + if (pp->p_fstype != FS_UNUSED) + Warning("partition %c is not marked as unused!",part); + if (pp->p_offset != 0) + Warning("partition %c doesn't start at 0!",part); + if (pp->p_size != lp->d_secperunit) + Warning("partition %c doesn't cover the whole unit!",part); + + if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) || + (pp->p_size != lp->d_secperunit)) { + Warning("An incorrect partition %c may cause problems for " + "standard system utilities",part); + } + } + + /* check for overlaps */ + /* this will check for all possible overlaps once and only once */ + for (j = 0; j < i; j++) { + pp2 = &lp->d_partitions[j]; + if (j != RAW_PART && i != RAW_PART && + pp->p_fstype != FS_VINUM && + pp2->p_fstype != FS_VINUM && + part_set[i] && part_set[j]) { + if (pp2->p_offset < pp->p_offset + pp->p_size && + (pp2->p_offset + pp2->p_size > pp->p_offset || + pp2->p_offset >= pp->p_offset)) { + fprintf(stderr,"partitions %c and %c overlap!\n", + j + 'a', i + 'a'); + errors++; + } + } + } + } + for (; i < 8 || i < lp->d_npartitions; 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. + */ +static struct disklabel32 dlab; + +struct disklabel32 * +getvirginlabel(void) +{ + struct partinfo info; + struct disklabel32 *dl = &dlab; + int f; + + if ((f = open(dkname, O_RDONLY)) == -1) { + warn("cannot open %s", dkname); + return (NULL); + } + + /* + * Check to see if the media is too big for a 32 bit disklabel. + */ + if (ioctl(f, DIOCGPART, &info) == 0) { + if (info.media_size >= 0x100000000ULL * 512) { + warnx("The media is too large for a 32 bit disklabel," + " please use disklabel64."); + return (NULL); + } + } + + /* + * Generate a virgin disklabel via ioctl + */ + if (ioctl(f, DIOCGDVIRGIN32, dl) < 0) { + l_perror("ioctl DIOCGDVIRGIN32"); + close(f); + return(NULL); + } + close(f); + return (dl); +} + +struct disklabel32 * +getdisklabelfromdisktab(const char *name) +{ + struct disktab *dt; + struct disklabel32 *dl = &dlab; + int i; + + if ((dt = getdisktabbyname(name)) == NULL) + return(NULL); + dl->d_magic = DISKMAGIC32; + dl->d_type = dt->d_typeid; + dl->d_subtype = 0; + dl->d_secsize = dt->d_media_blksize; + dl->d_nsectors = dt->d_secpertrack; + dl->d_ntracks = dt->d_nheads; + dl->d_ncylinders = dt->d_ncylinders; + dl->d_secpercyl = dt->d_secpercyl; + dl->d_secperunit = dt->d_media_blocks; + dl->d_rpm = dt->d_rpm; + dl->d_interleave = dt->d_interleave; + dl->d_trackskew = dt->d_trackskew; + dl->d_cylskew = dt->d_cylskew; + dl->d_headswitch = dt->d_headswitch; + dl->d_trkseek = dt->d_trkseek; + dl->d_magic2 = DISKMAGIC32; + dl->d_npartitions = dt->d_npartitions; + dl->d_bbsize = dt->d_bbsize; + dl->d_sbsize = dt->d_sbsize; + for (i = 0; i < dt->d_npartitions; ++i) { + struct partition32 *dlp = &dl->d_partitions[i]; + struct dt_partition *dtp = &dt->d_partitions[i]; + + dlp->p_size = dtp->p_size; + dlp->p_offset = dtp->p_offset; + dlp->p_fsize = dtp->p_fsize; + dlp->p_fstype = dtp->p_fstype; + dlp->p_frag = dtp->p_fsize; + } + return(dl); +} + +/* + * 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(struct disklabel32 *lp) +{ + struct partition32 *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(const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "Warning, "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + va_end(ap); +} + +/* + * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks + * and earlier DFly bb's are packed against the old disklabel and a new + * disklabel would blow them up. This is a hack that should be removed + * in 2006 sometime (if ever). + */ + +int +checkoldboot(int f, const char *bootbuffer) +{ + char buf[BBSIZE]; + + if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0) + return(0); + lseek(f, (off_t)0, SEEK_SET); + if (read(f, buf, sizeof(buf)) != sizeof(buf)) + return(0); + if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */ + return(0); + if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */ + return(1); + return(0); +} + +/* + * Traditional 32 bit disklabels actually use absolute sector numbers on + * disk, NOT slice relative sector numbres. The OS hides this from us + * when we use DIOC ioctls to access the label, but newer versions of + * Dragonfly no longer adjusts the disklabel when snooping reads or writes + * so we have to figure it out ourselves. + */ +const char * +fixlabel(int f, struct disklabel32 *lp, int writeadj) +{ + const char *msg = NULL; + struct partinfo info; + struct partition32 *pp; + u_int64_t start; + u_int64_t end; + u_int64_t offset; + int part; + int rev; + size_t rev_len = sizeof(rev); + + if (sysctlbyname("kern.osrevision", &rev, &rev_len, NULL, 0) < 0) { + errx(1, "Cannot use raw mode on non-DragonFly systems\n"); + } + if (rev < 200701) { + warnx("Warning running new disklabel on old DragonFly systems,\n" + "assuming the disk layer will fixup the label.\n"); + sleep(3); + return(NULL); + } + + pp = &lp->d_partitions[RAW_PART]; + + if (forceflag) { + info.media_offset = slice_start_lba * lp->d_secsize; + info.media_blocks = pp->p_size; + info.media_blksize = lp->d_secsize; + } else if (ioctl(f, DIOCGPART, &info) < 0) { + msg = "Unable to extract the slice starting LBA, " + "you must use the -f option\n" + "to specify it manually, or perhaps try without " + "using -r and let the kernel deal with it\n"; + return(msg); + } + + if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32) + return ("fixlabel: invalid magic"); + if (dkcksum32(lp) != 0) + return ("fixlabel: invalid checksum"); + + /* + * What a mess. For ages old backwards compatibility the disklabel + * on-disk stores absolute offsets instead of slice-relative offsets. + * So fix it up when reading, writing, or snooping. + * + * The in-core label is always slice-relative. + */ + if (writeadj) { + /* + * incore -> disk + */ + start = 0; + offset = info.media_offset / info.media_blksize; + } else { + /* + * disk -> incore + */ + start = info.media_offset / info.media_blksize; + offset = -info.media_offset / info.media_blksize; + } + if (pp->p_offset != start) + return ("fixlabel: raw partition offset != slice offset"); + if (pp->p_size != info.media_blocks) { + if (pp->p_size > info.media_blocks) + return ("fixlabel: raw partition size > slice size"); + } + end = start + info.media_blocks; + if (start > end) + return ("fixlabel: slice wraps"); + if (lp->d_secpercyl <= 0) + return ("fixlabel: d_secpercyl <= 0"); + pp -= RAW_PART; + for (part = 0; part < lp->d_npartitions; part++, pp++) { + if (pp->p_offset != 0 || pp->p_size != 0) { + if (pp->p_offset < start + || pp->p_offset + pp->p_size > end + || pp->p_offset + pp->p_size < pp->p_offset) { + /* XXX else silently discard junk. */ + bzero(pp, sizeof *pp); + } else { + pp->p_offset += offset; + } + } + } + lp->d_checksum = 0; + lp->d_checksum = dkcksum32(lp); + return (NULL); +} + +void +usage(void) +{ +#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] [-n] disk type [packid]", + "\t\t(to write label with existing boot program)", + " disklabel -e [-r] [-n] disk", + "\t\t(to edit label)", + " disklabel -R [-r] [-n] disk protofile", + "\t\t(to restore label with existing boot program)", +#if NUMBOOT > 1 + " disklabel -B [-n] [-b boot1 -s boot2] disk [type]", + "\t\t(to install boot program with existing label)", + " disklabel -w -B [-n] [-b boot1 -s boot2] disk type [packid]", + "\t\t(to write label and boot program)", + " disklabel -R -B [-n] [-b boot1 -s boot2] disk protofile [type]", + "\t\t(to restore label and boot program)", +#else + " disklabel -B [-n] [-b bootprog] disk [type]", + "\t\t(to install boot program with existing on-disk label)", + " disklabel -w -B [-n] [-b bootprog] disk type [packid]", + "\t\t(to write label and install boot program)", + " disklabel -R -B [-n] [-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] [-n] disk type [packid]", + "\t\t(to write label)", + " disklabel -e [-r] [-n] disk", + "\t\t(to edit label)", + " disklabel -R [-r] [-n] disk protofile", + "\t\t(to restore label)", + " disklabel [-NW] disk", + "\t\t(to write disable/enable label)"); +#endif + fprintf(stderr, "%s\n%s\n", + " disklabel [-f slice_start_lba] [options]", + "\t\t(to force using manual offset)"); + exit(1); +} diff --git a/sbin/disklabel32/disklabel32.5 b/sbin/disklabel32/disklabel32.5 new file mode 100644 index 0000000..aa33211 --- /dev/null +++ b/sbin/disklabel32/disklabel32.5 @@ -0,0 +1,111 @@ +.\" Copyright (c) 1987, 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.5.5 8.1 (Berkeley) 6/5/93 +.\" $FreeBSD: src/sbin/disklabel/disklabel.5,v 1.7.2.4 2002/08/21 18:58:16 trhodes Exp $ +.\" $DragonFly: src/sbin/disklabel/disklabel.5,v 1.6 2008/08/21 21:08:08 thomas Exp $ +.\" +.Dd August 21, 2008 +.Dt DISKLABEL 5 +.Os +.Sh NAME +.Nm disklabel +.Nd 32 bit disk pack label +.Sh SYNOPSIS +.In sys/disklabel.h +.Sh DESCRIPTION +Each disk or disk pack on a system may contain a disk label +which provides detailed information +about the geometry of the disk and the partitions into which the disk +is divided. +It should be initialized when the disk is formatted, +and may be changed later with the +.Xr disklabel 8 +program. +This information is used by the system disk driver and by the bootstrap +program to determine how to program the drive +and where to find the file systems on the disk partitions. +Additional information is used by the file system in order +to use the disk most efficiently and to locate important file system information. +The description of each partition contains an identifier for the partition +type (standard file system, swap area, etc.). +The file system updates the in-core copy of the label if it contains +incomplete information about the file system. +.Pp +The label is located in sector number +.Dv LABELSECTOR +of the drive, usually sector 0 where it may be found +without any information about the disk geometry. +It is at an offset +.Dv LABELOFFSET +from the beginning of the sector, to allow room for the initial bootstrap. +The disk sector containing the label is normally made read-only +so that it is not accidentally overwritten by pack-to-pack copies +or swap operations; +the +.Dv DIOCWLABEL +.Xr ioctl 2 , +which is done as needed by the +.Xr disklabel 8 +program. +.Pp +A copy of the in-core label for a disk can be obtained with the +.Dv DIOCGDINFO +.Xr ioctl 2 ; +this works with a file descriptor for a block or character (``raw'') device +for any partition of the disk. +The in-core copy of the label is set by the +.Dv DIOCSDINFO +.Xr ioctl 2 . +The offset of a partition cannot generally be changed while it is open, +nor can it be made smaller while it is open. +One exception is that any change is allowed if no label was found +on the disk, and the driver was able to construct only a skeletal label +without partition information. +Finally, the +.Dv DIOCWDINFO +.Xr ioctl 2 +operation sets the in-core label and then updates the on-disk label; +there must be an existing label on the disk for this operation to succeed. +Thus, the initial label for a disk or disk pack must be installed +by writing to the raw disk. +All of these operations are normally done using +.Xr disklabel 8 . +.Pp +The format of the disk label is specified in +.In sys/disklabel32.h . +.Sh SEE ALSO +.Xr disklabel64 5 , +.Xr disktab 5 , +.Xr disklabel 8 diff --git a/sbin/disklabel32/disklabel32.8 b/sbin/disklabel32/disklabel32.8 new file mode 100644 index 0000000..36a20ed --- /dev/null +++ b/sbin/disklabel32/disklabel32.8 @@ -0,0 +1,1042 @@ +.\" 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: src/sbin/disklabel/disklabel.8,v 1.15.2.22 2003/04/17 17:56:34 trhodes Exp $ +.\" $DragonFly: src/sbin/disklabel/disklabel.8,v 1.30 2008/09/16 20:45:36 thomas Exp $ +.\" +.Dd September 28, 2009 +.Dt DISKLABEL 8 +.Os +.Sh NAME +.Nm disklabel +.Nd read and write 32 bit disk pack label +.Sh SYNOPSIS +.Nm +.Op Fl r +.Ar disk +.Nm +.Fl w +.Op Fl r +.Op Fl n +.Ar disk Ar disktype Ns / Ns Cm auto +.Oo Ar packid Oc +.Nm +.Fl e +.Op Fl r +.Op Fl n +.Ar disk +.Nm +.Fl R +.Op Fl r +.Op Fl n +.Ar disk Ar protofile +.Nm +.Op Fl NW +.Ar disk +.Pp +.Nm +.Fl B +.Oo +.Fl b Ar boot1 +.Fl s Ar boot2 +.Oc +.Ar disk +.Oo Ar disktype Ns / Ns Cm auto Oc +.Nm +.Fl w +.Fl B +.Op Fl n +.Oo +.Fl b Ar boot1 +.Fl s Ar boot2 +.Oc +.Ar disk Ar disktype Ns / Ns Cm auto +.Oo Ar packid Oc +.Nm +.Fl R +.Fl B +.Op Fl n +.Oo +.Fl b Ar boot1 +.Fl s Ar boot2 +.Oc +.Ar disk Ar protofile +.Oo Ar disktype Ns / Ns Cm auto Oc +.Nm +.Fl f Ar slice_start_lba +.Oo Ar options Oc +.Sh DESCRIPTION +The +.Nm +utility +installs, examines or modifies a 32 bit 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 +The disk label resides close to or at the beginning of each disk slice. +For faster access, the kernel maintains a copy in core at all times. +By +default, most forms of the +.Nm +command 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. +.Ss Disk device name +All +.Nm +forms require a disk device name, which should always be the raw +device name representing the disk or slice. +.Dx +uses the following scheme for slice numbering: +If the disk doesn't use GPT (typically laid out by +.Xr gpt 8 ) , +but e.g.\& MBR (typically laid out by +.Xr fdisk 8 ) , +then slice 0, e.g.\& +.Pa da0s0 , +represents the entire disk regardless of any DOS partitioning. +Slice 0 is called the compatibility slice, +and slice 1 and onward, e.g.\& +.Pa da0s1 , +represents a +.Bx +slice. +If the disk does use GPT, then all slices are +.Bx +slices, slice 0 isn't special, it is just the first slice on the disk. +You do not have to include the +.Pa /dev/ +path prefix when specifying the device. +The +.Nm +utility will automatically prepend it. +.Ss Reading the disk label +To examine the label on a disk drive, use +.Nm +without options: +.Pp +.Nm +.Op Fl r +.Ar disk +.Pp +.Ar disk +represents the raw disk in question, and may be in the form +.Pa da0s1 +or +.Pa /dev/da0s1 . +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. +.Ss Writing a standard label +To write a standard label, use the form +.Pp +.Nm +.Fl w +.Op Fl r +.Op Fl n +.Ar disk Ar disktype Ns / Ns Cm auto +.Oo Ar packid Oc +.Pp +The required arguments to +.Nm +are the drive to be labeled and the drive type as described in the +.Xr 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. +.Pp +If the +.Fl n +flag is given, no data will be written to the device, and instead the +disklabel that would have been written will be printed to stdout. +.Pp +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 +.Cm 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 +For most harddisks, a label based on percentages for most partitions (and +one partition with a size of +.Ql * ) +will produce a reasonable configuration. +.Pp +PC-based systems have special requirements in order for the BIOS to properly +recognize a +.Dx +disklabel. +Older systems may require what is known as a +.Dq 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 partition using +.Ar fdisk +and then create a +.Dx +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 +To edit an existing disk label, use the form +.Pp +.Nm +.Fl e +.Op Fl r +.Op Fl n +.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. +If +.Fl n +is specified, no data will be written to the device, and instead the +disklabel that would have been written will be printed to stdout. +This is +useful to see how a partitioning scheme will work out for a specific disk. +.Ss Restoring a disk label from a file +To restore a disk label from a file, use the form +.Pp +.Nm +.Fl R +.Op Fl r +.Op Fl n +.Ar disk Ar protofile +.Pp +.Nm +is capable of restoring a disk label that was previously saved in a file +in ASCII format. +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 +.Ql # +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. +If +.Fl n +is used, no data will be written to the device, and instead the +disklabel that would have been written will be printed to stdout. +This is +useful to see how a partitioning scheme will work out for a specific disk. +.Ss Enabling and disabling writing to the disk label area +By default, it is not possible to write to the disk label area at the beginning +of a disk. +The disk driver arranges for +.Xr write 2 +and similar system calls +to return +.Er EROFS +on any attempt to do so. +If you need +to write to this area (for example, to obliterate the label), use the form +.Pp +.Nm +.Fl W +.Ar disk +.Pp +To disallow writing to the label area after previously allowing it, +use the command +.Pp +.Nm +.Fl N +.Ar disk +.Ss Installing bootstraps +The final three forms of +.Nm +are used to install bootstrap code, which allows boot from a +.Xr UFS 5 +file system. +If you are creating a +.Dq dangerously-dedicated +slice for compatibility with older PC systems, +you generally want to specify the compatibility slice, such as +.Pa da0s0 . +If you are creating a label within an existing DOS slice, +you should specify +the slice name such as +.Pa da0s1 . +Making a slice 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 +.Dx +bootblocks in the slice. +.Pp +.Nm +.Fl B +.Oo +.Fl b Ar boot1 +.Fl s Ar boot2 +.Oc +.Ar disk +.Oo Ar disktype Ns / Ns Cm auto Oc +.Pp +This form installs the bootstrap only. +It does not change the disk label. +You should never use this command on the compatibility slice unless you +intend to create a +.Dq dangerously-dedicated +disk, such as +.Ar da0s0 . +This command is typically run on a +.Bx +slice such as +.Ar da0s1 . +.Pp +.Nm +.Fl w +.Fl B +.Op Fl n +.Oo +.Fl b Ar boot1 +.Fl s Ar boot2 +.Oc +.Ar disk Ar disktype Ns / Ns Cm auto +.Oo Ar packid Oc +.Pp +This form corresponds to the +.Dq write label +command described above. +In addition to writing a new volume label, it also installs the bootstrap. +If run on the compatibility slice this command will create a +.Dq dangerously-dedicated +label. +This command is normally run on a +.Bx +slice rather than the compatibility slice. +If +.Fl n +is used, no data will be written to the device, and instead the +disklabel that would have been written will be printed to stdout. +.Pp +.Nm +.Fl R +.Fl B +.Op Fl n +.Oo +.Fl b Ar boot1 +.Fl s Ar boot2 +.Oc +.Ar disk Ar protofile +.Oo Ar disktype Ns / Ns Cm auto Oc +.Pp +This form corresponds to the +.Dq restore label +command described above. +In addition to restoring the volume label, it also installs the bootstrap. +If run on the compatibility slice this command will create a +.Dq dangerously-dedicated +label. +This command is normally run on a +.Bx +slice rather than the compatibility +slice. +.Pp +The bootstrap commands always access the disk directly, +so it is not necessary to specify the +.Fl r +flag. +If +.Fl n +is used, no data will be written to the device, and instead the +disklabel that would have been written will be printed to stdout. +.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 normally 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 +.Dq b0 +and +.Dq 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. +.El +.Ss Initializing/Formatting a bootable disk from scratch +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 +.No non- Ns Dx +slices. +.Bl -enum +.It +Use +.Xr gpt 8 +or +.Xr fdisk 8 +to initialize the hard disk, and create a GPT or MBR slice table, +referred to as the +.Dq "partition table" +in +.Tn DOS . +.It +Use +.Nm +or +.Xr disklabel64 8 +to define partitions on +.Dx +slices created in the previous step. +.It +Finally use +.Xr newfs_hammer 8 +or +.Xr newfs 8 +to create file systems on new partitions. +.El +.Pp +A typical partitioning scheme would be to have an +.Ql a +partition +of approximately 512MB to hold the root file system, a +.Ql b +partition for +swap (usually 4GB), a +.Ql d +partition for +.Pa /var +(usually 2GB), an +.Ql e +partition for +.Pa /var/tmp +(usually 2GB), an +.Ql f +partition for +.Pa /usr +(usually around 4GB), +and finally a +.Ql g +partition for +.Pa /home +(usually all remaining space). +If you are tight on space all sizes can be halved. +Your mileage may vary. +.Pp +.Dl "fdisk -BI da0" +.Dl "disklabel -w -B da0s1 auto" +.Dl "disklabel -e da0s1" +.Ss Manual offset +.Dx +no longer snoop-adjusts the on-disk label when reading or writing +raw labels. +.Nm +is now responsible for adjusting the label when operating in raw mode. +Traditional (32 bit +.Bx ) +disklabels store offsets as absolute block numbers +rather than slice-relative block numbers. +If +.Nm +is unable to issue the +.Dv DIOCGPART +ioctl to get slice information it will +refuse to read or write the label in raw mode. +The +.Fl f +option may be used to force the operation by supplying a manual offset. +.Sh FILES +.Bl -tag -width ".Pa /etc/disktab" -compact +.It Pa /boot/boot1 +Default stage1 boot image. +.It Pa /boot/boot2 +Default stage2 boot image. +.It Pa /etc/disktab +Disk description file. +.El +.Sh SAVED FILE FORMAT +The +.Nm +utility uses an +.Tn ASCII +version of the label when examining, editing, or restoring a disk label. +The format is: +.Bd -literal -offset 4n +# /dev/ad4s4: +type: unknown +disk: amnesiac +label: fictitious +flags: +bytes/sector: 512 +sectors/track: 63 +tracks/cylinder: 24 +sectors/cylinder: 1512 +cylinders: 161098 +sectors/unit: 243581184 +rpm: 3600 +interleave: 1 +trackskew: 0 +cylinderskew: 0 +headswitch: 0 # milliseconds +track-to-track seek: 0 # milliseconds +drivedata: 0 + +16 partitions: +# size offset fstype + a: 1048560 16 4.2BSD # 511.992MB + b: 8388608 1048576 swap # 4096.000MB + c: 243581184 0 unused # 118936.125MB + d: 4194304 9437184 4.2BSD # 2048.000MB + e: 4194304 13631488 4.2BSD # 2048.000MB + f: 8388608 17825792 4.2BSD # 4096.000MB + h: 196395264 26214400 HAMMER # 95896.125MB + i: 10485760 222609664 ccd # 5120.000MB + j: 10485760 233095424 vinum # 5120.000MB +.Ed +.Pp +Lines starting with a +.Ql # +mark are comments. +Most of the other specifications are no longer used. +The ones which must still be set correctly are: +.Bl -inset +.It Ar label +is an optional label, set by the +.Ar packid +option when writing a label. +.It Ar flags +may be +.Cm removable , ecc +or +.Cm badsect . +.Cm removable +is set for removable media drives, but no current +.Dx +driver evaluates this +flag. +.Cm ecc +is no longer supported; +.Cm badsect +specifies that the drive can perform bad sector remapping. +.It Ar sectors/unit +describes the total size of the disk. +This value must be correct. +.It Ar "the partition table" +is the +.Ux +partition table, not the +.Tn DOS +partition table described in +.Xr fdisk 8 . +.El +.Pp +The partition table can have up to 16 entries. +It contains the following information: +.Bl -tag -width indent +.It Ar # +The partition identifier is a single letter in the range +.Ql a +to +.Ql p . +By convention, partition +.Ql c +is reserved to describe the entire disk. +.It Ar size +The size of the partition in sectors, +.Cm K +(kilobytes - 1024), +.Cm M +(megabytes - 1024*1024), +.Cm G +(gigabytes - 1024*1024*1024), +.Cm % +(percentage of free space +.Em after +removing any fixed-size partitions other than partition +.Ql c ) , +or +.Cm * +(all remaining free space +.Em after +fixed-size and percentage partitions). +For partition +.Ql c , +a size of +.Cm * +indicates the entire disk. +Lowercase versions of +.Cm K , M , +and +.Cm G +are allowed. +Size and type should be specified without any spaces between them. +.Pp +Example: 2097152, 1G, 1024M and 1048576K are all the same size +(assuming 512-byte sectors). +.It Ar offset +The offset of the start of the partition from the beginning of the +drive in sectors, or +.Cm * +to have +.Nm +calculate the correct offset to use (the end of the previous partition plus +one, ignoring partition +.Ql c . +For partition +.Ql c , +.Cm * +will be interpreted as an offset of 0. +.It Ar fstype +Describes the purpose of the partition. +The example shows all currently used partition types. +For +.Xr UFS 5 +file systems, use type +.Cm 4.2BSD . +For +.Xr HAMMER 5 +file systems, use type +.Cm HAMMER . +For +.Xr ccd 4 +partitions, use type +.Cm ccd . +For Vinum drives, use type +.Cm vinum . +Other common types are +.Cm swap +and +.Cm unused . +By convention, partition +.Ql c +represents the entire slice and should be of type +.Cm unused , +though +.Nm +does not enforce this convention. +The +.Nm +utility +also knows about a number of other partition types, +none of which are in current use. +(See +.Dv fstypenames +in +.In sys/dtype.h +for more details). +.El +.Pp +The remainder of the line is a comment and shows the size of +the partition in MB. +.Sh EXAMPLES +.Dl "disklabel da0s1" +.Pp +Display the in-core label for the first slice of the +.Pa da0 +disk, as obtained via +.Pa /dev/da0s1 . +(If the disk is +.Dq dangerously-dedicated , +the compatibility slice name should be specified, such as +.Pa da0s0 . ) +.Pp +.Dl "disklabel da0s1 > savedlabel" +.Pp +Save the in-core label for +.Pa da0s1 +into the file +.Pa savedlabel . +This file can be used with the +.Fl R +option to restore the label at a later date. +.Pp +.Dl "disklabel -w -r /dev/da0s1 da2212 foo" +.Pp +Create a label for +.Pa da0s1 +based on information for +.Dq da2212 +found in +.Pa /etc/disktab . +Any existing bootstrap code will be clobbered +and the disk rendered unbootable. +.Pp +.Dl "disklabel -e -r da0s1" +.Pp +Read the on-disk label for +.Pa da0s1 , +edit it, and reinstall in-core as well as on-disk. +Existing bootstrap code is unaffected. +.Pp +.Dl "disklabel -e -r -n da0s1" +.Pp +Read the on-disk label for +.Pa da0s1 , +edit it, and display what the new label would be (in sectors). +It does +.Em not +install the new label either in-core or on-disk. +.Pp +.Dl "disklabel -r -w da0s1 auto" +.Pp +Try to auto-detect the required information from +.Pa da0s1 , +and write a new label to the disk. +Use another +.Nm Fl e +command to edit the +partitioning and file system information. +.Pp +.Dl "disklabel -R da0s1 savedlabel" +.Pp +Restore the on-disk and in-core label for +.Pa da0s1 +from information in +.Pa savedlabel . +Existing bootstrap code is unaffected. +.Pp +.Dl "disklabel -R -n da0s1 label_layout" +.Pp +Display what the label would be for +.Pa da0s1 +using the partition layout in +.Pa label_layout . +This is useful for determining how much space would be allotted for various +partitions with a labelling scheme using +.Cm % Ns -based +or +.Cm * +partition sizes. +.Pp +.Dl "disklabel -B da0s1" +.Pp +Install a new bootstrap on +.Pa da0s1 . +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 newboot2 da2212" +.Pp +Install a new label and bootstrap. +The label is derived from disktab information for +.Dq da2212 +and installed both in-core and on-disk. +The bootstrap code comes from the files +.Pa newboot1 +and +.Pa 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 -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 +.Dq 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. +.Pp +.Dl "disklabel -W da0s1" +.Dl "dd if=/dev/zero of=/dev/da0s1 bs=512 count=32" +.Dl "disklabel64 -r -w da0s1 auto" +.Dl "disklabel64 -N da0s1" +.Pp +Completely wipe any prior information on the slice, +changing label format to 64 bit. +The wiping is needed as +.Nm disklabel64 +and +.Nm , +as a safety measure, +won't do any operations if label with other format is already installed. +.Pp +This is an example disklabel that uses some of the new partition size types +such as +.Cm % , M , G , +and +.Cm * , +which could be used as a source file for +.Pp +.Dl "disklabel -R ad0s1 new_label_file" +.Bd -literal -offset 4n +# /dev/ad0s1: +type: ESDI +disk: ad0s1 +label: +flags: +bytes/sector: 512 +sectors/track: 63 +tracks/cylinder: 16 +sectors/cylinder: 1008 +cylinders: 40633 +sectors/unit: 40959009 +rpm: 3600 +interleave: 1 +trackskew: 0 +cylinderskew: 0 +headswitch: 0 # milliseconds +track-to-track seek: 0 # milliseconds +drivedata: 0 + +16 partitions: +# size offset fstype + a: 400M 0 4.2BSD + b: 1G * swap + c: * * unused + e: 204800 * 4.2BSD + f: 5g * 4.2BSD + g: * * 4.2BSD +.Ed +.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 +.Ql 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 +.Ql a +partition. +.Pp +On some machines the bootstrap code may not fit entirely in the area +allocated for it by some file systems. +As a result, it may not be possible to have file systems on some partitions +of a +.Dq bootable +disk. +When installing bootstrap code, +.Nm +checks for these cases. +If the installed boot code would overlap a partition of type +.Dv FS_UNUSED +it is marked as type +.Dv FS_BOOT . +The +.Xr newfs 8 +utility will disallow creation of file systems on +.Dv FS_BOOT +partitions. +Conversely, if a partition has a type other than +.Dv FS_UNUSED +or +.Dv FS_BOOT , +.Nm +will not install bootstrap code that overlaps it. +.Sh COMPATIBILITY +Due to +.Xr disklabel 5 +storing sector numbers in 32 bit format +.Nm +is restricted to 2TB, using the prevalent sector size of 512B. +.Xr disklabel64 5 +labels should be used to partition larger disks. +.Pp +The various +.Bx Ns s +use slightly different versions of +.Bx +disklabels +and are not generally compatible. +The +.Dx +kernel can often use labels from other +.Bx Ns s +for read-only operation. +.Sh SEE ALSO +.Xr dd 1 , +.Xr ccd 4 , +.Xr disklabel 5 , +.Xr disktab 5 , +.Xr boot0cfg 8 , +.Xr diskinfo 8 , +.Xr disklabel64 8 , +.Xr fdisk 8 , +.Xr gpt 8 , +.Xr newfs 8 , +.Xr newfs_hammer 8 , +.Xr vinum 8 +.Sh BUGS +For the i386 architecture, the primary bootstrap sector contains +an embedded +.Em fdisk +table. +The +.Nm +utility 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 +.Bx +disklabel +starts at absolute block 0 on the disk. +.Pp +The +.Nm +utility +does not perform all possible error checking. +Warning +.Em is +given if partitions +overlap; if an absolute offset does not match the expected offset; if the +.Ql c +partition does not start at 0 or does not cover the entire slice; if a +partition runs past the end of the device; and a number of other errors; but +no warning is given if space remains unused. diff --git a/sbin/disklabel32/pathnames.h b/sbin/disklabel32/pathnames.h new file mode 100644 index 0000000..d66074b --- /dev/null +++ b/sbin/disklabel32/pathnames.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. 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 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. + * + * @(#)pathnames.h 8.1 (Berkeley) 6/5/93 + * $FreeBSD: src/sbin/disklabel/pathnames.h,v 1.4 2000/01/11 07:28:46 bde Exp $ + * $DragonFly: src/sbin/disklabel/pathnames.h,v 1.2 2003/06/17 04:27:32 dillon Exp $ + */ + +#include + +#define _PATH_BOOTDIR "/boot" +#define PATH_TMPFILE "/tmp/EdDk.XXXXXXXXXX" diff --git a/sbin/disklabel64/Makefile b/sbin/disklabel64/Makefile index 8d93b09..10f9486 100644 --- a/sbin/disklabel64/Makefile +++ b/sbin/disklabel64/Makefile @@ -6,4 +6,8 @@ PROG= disklabel64 SRCS= disklabel64.c crc32.c MAN= disklabel64.5 disklabel64.8 +LINKS= ${BINDIR}/disklabel64 ${BINDIR}/disklabel +MLINKS+= disklabel64.8 disklabel.8 +MLINKS+= disklabel64.5 disklabel.5 + .include -- 1.7.7.2