Subject: New fdisk program
To: None <current-users@netbsd.org, port-i386@netbsd.org>
From: David Laight <david@l8s.co.uk>
List: port-i386
Date: 04/30/2003 18:01:02
I have a rototilled fdisk program that supports:
- editing of the extended partition list
- user friendly default start/size for new partitions
- partition start/size input in sectors, cylinders or MB
- bootsel menu names configured with paritition bounds
- partition update loop asks used which partition to change
  (instead of asking about each partition in turn).
- detection (and erroring) of overlapped partitions.
- automatic installation of correct mbr_xxx code (after prompting user)

If someone really, really wants to review the code changes they are
welcome - but the diff -u output is larger than the source file.

Below is a diff of the man page, passed through 'col -b' to be friendly
to all mail readers (by deleting all the underlines).

Any comments?

	David

--- /usr/share/man/cat8/fdisk.0 2003-03-04 08:50:48.000000000 +0000
+++ fdisk.cat8	2003-04-30 15:53:55.000000000 +0100
@@ -4,86 +4,45 @@
      fdisk - DOS partition maintenance program

 SYNOPSIS
-     fdisk [-aiufBS] [-0 | -1 | -2 | -3] [-b cylinders/heads/sectors]
-	    [-s id/start/size] [-c bootcode] [device]
+     fdisk [-afiuvBS] [-0 | -1 | -2 | -3] [-E number]
+	    [-b cylinders/heads/sectors] [-s id/start/size[/bootmenu]]
+	    [-c bootcode] [-r|w file] [device]
+     fdisk -l

 DESCRIPTION
-     In order for the BIOS to boot the kernel, certain conventions must be ad-
-     hered to.	 Sector 0 of the disk must contain boot code, a partition
-     table, and a magic number.  Newer BIOS programs want the entire first
-     track reserved.  BIOS partitions can be used to break the disk up into
-     several pieces.  The BIOS brings in sector 0, verifies the magic number,
-     and begins executing the code at the first byte.	This code in turn
-     searches the DOS partition table for an `active' partition.  If one is
-     found, the boot block from that partition is loaded and replaces the
-     original boot block.  Under DOS, you could have one or more partitions
-     with one active.	The DOS fdisk program can be used to divide space on
-     the disk into partitions and set one active.
-
-     The NetBSD program fdisk serves a similar purpose to the DOS program.
-     When called with no arguments, it prints the sector 0 partition table.
-     An example follows:
-
-	      NetBSD disklabel disk geometry:
-	      cylinders: 769 heads: 15 sectors/track: 33 (495 sectors/cylinder)
-
-	      BIOS geometry:
-	      cylinders: 769 heads: 15 sectors/track: 33 (495 sectors/cylinder)
-
-	      Partition table:
-	      0: sysid 169 (NetBSD)
-		  start 495, size 380160 (185 MB), flag 0
-		      beg: cylinder    1, head	 0, sector  1
-		      end: cylinder  768, head	14, sector 33
-	      1: sysid 164 (reserved)
-		  start 378180, size 2475 (1 MB), flag 0
-		      beg: cylinder  764, head	 0, sector  1
-		      end: cylinder  768, head	14, sector 33
-	      2: <UNUSED>
-	      3: sysid 99 (GNU HURD or Mach or Sys V/386 (such as ISC UNIX))
-		  start 380656, size 224234 (109 MB), flag 80
-		      beg: cylinder  769, head	 0, sector  2
-		      end: cylinder  197, head	14, sector 33
-
-     This example disk is divided into three partitions that happen to fill
-     the disk.	 The second partition overlaps the end of the first (used for
-     debugging purposes).
-
-     The various fields in each partition are:
-
-     sysid	is used to label the partition.	 NetBSD reserves the magic num-
-		ber 169 decimal (A9 in hex).  The number 0 is used to mark a
-		partition as unused.  See the -l flag.
-
-     start, size
-		start address and size of a partition in sectors.
-
-     flag 80	specifies that this is the active partition.
-
-     cylinder, head, sector
-		the beginning or ending address of a partition.
-
-     Note: these numbers are calculated using BIOS's understanding of the disk
-     geometry and saved in the bootblock.
+     The fdisk program is used to divide space on a disk into partitions and
+     set one active.  There can be at most 4 partitions defined in sector 0,
+     one of which can be an extended partition which can be split into any
+     number of sub-partitions.
+
+     NetBSD does not use the partitioning done by fdisk, instead it uses a
+     NetBSD disklabel saved in sector 1 of the NetBSD partition.
+
+     The standard mbr boot code will only boot the 'active' partition.	 Howev-
+     er NetBSD contains two alternate boot programs which allow the user to
+     interactively select which of the partitions to boot.  The 'mbr_ext' code
+     will boot NetBSD from an extended partition but will not work on old sys-
+     tems that do not support LBA reads.

      At the start the fdisk program will determine whether the disk sector 0
      is valid as a boot sector.  (This is determined by checking the magic
-     number.)	If not, fdisk will initialize the boot code as well as the par-
+     number.)	If not, fdisk will initialise the boot code as well as the par-
      tition table.  During this, all four partitions will be marked empty.

      The flags -a, -i or -u are used to indicate that the partition data is to
      be updated.  The fdisk program will enter an interactive conversational
      mode.  This mode is designed not to change any data unless you explicitly
      tell it to; fdisk selects defaults for its questions to guarantee that
-     behavior.
+     behaviour.

-     Getting the cylinder, head, and sector fields correct is tricky.	So by
-     default, they will be calculated for you; you can specify them if you
-     choose.
+     fdisk will calculate the correct cylinder, head, and sector values for
+     any partition you edit.  If you specify -v you will be asked whether you
+     want to specify them yourself.

      Finally, when all the data for the first sector has been accumulated,
-     fdisk will ask if you really want to rewrite sector 0.  Only if you reply
-     affirmatively to this question will fdisk write anything to the disk.
+     fdisk will ask if you really want to write the new partition table.  Only
+     if you reply affirmatively to this question will fdisk write anything to
+     the disk.

      Available options:

@@ -102,16 +61,24 @@
	      Specify the BIOS parameters for cylinders, heads and sectors.  It
	      is used only in conjunction with the -u flag.

-     -B      On an i386 or x86_64 system, interactively install or update the
-	      boot selector code and settings.	(The boot selector permits the
-	      user to interactively select the boot partition, and thus which
-	      operating system is run, at system boot time.  See mbr(8) for
-	      more information.)
+     -B      On an i386 or amd64 system, interactively update the boot selec-
+	      tor settings.  (The boot selector permits the user to interac-
+	      tively select the boot partition, and thus which operating system
+	      is run, at system boot time.  See mbr(8) for more information.)

      -c bootcode
	      Specify the filename that fdisk should read the bootcode from.
-	      The default is to read from /usr/mdec/mbr if run on an i386 ma-
-	      chine, and leave the bootcode empty for other machines.
+	      If the name of a directory is specified, then fdisk will look for
+	      files with the default names in that directory.  The default is
+	      to read from /usr/mdec/mbr, /usr/mdec/mbr_bootsel or
+	      /usr/mdec/mbr_ext depending on whether bootmenu was specified for
+	      any partitions on an i386 machine, and leave the bootcode empty
+	      for other machines.
+
+     -E number
+	      Update extended partition number.	 If the specified extended par-
+	      tition doesn't exist an additional extended partition will be
+	      created.

      -f      Run fdisk in a non-interactive mode.  In this mode, you can only
	      change the disk parameters by using the -b flag.	This is provid-
@@ -120,53 +87,180 @@
	      it impossible to specify the starting and ending cylinder, head
	      and sector fields.  They will be automatically computed using the
	      BIOS geometry.
+	      If -u and Fl s are specified then the details of the specified
+	      partition will be changed.  Any other partitions which overlap
+	      the requested part of the disk will be silently deleted.

-     -i      Explicitly request initialization of the master boot code (simi-
+     -i      Explicitly request initialisation of the master boot code (simi-
	      lar to what fdisk /mbr does under MSDOS), even if the magic num-
	      ber in the first sector is ok.  The partition table is left alone
	      by this (but see above).

      -l      Lists known sysid values and exit.

-     -s id/start/size
-	      Specify the partition id, start and size.	 This flag requires the
-	      use of a partition selection flag (-0, -1, -2, or -3)
+     -r file
+	      Read the boot record from file file instead of the specified
+	      disk.  The geometry information used is still that of the disk
+	      volume.  Any changes are written back to the file.
+
+     -s id/start/size[/bootmenu]
+	      Specify the partition id, start, size and optionally bootmenu.
+	      This flag requires the use of a partition selection flag (-0, -1,
+	      -2, -3, or -E number)

      -S      When used with no other flags print a series of /bin/sh commands
-	      for setting variables to the partition information.  This is used
-	      by the installation scripts.
+	      for setting variables to the partition information.  This could
+	      be used by installation scripts.
+
+     -u      Display the partitions and interactively ask which one you want
+	      to edit.	fdisk will step through each field showing the old val-
+	      ue and asking for a new one.  The start and size can be specified
+	      in blocks (nn), cylinders (nnc) or megabytes (nnm), values in
+	      megabytes will be rounded to the nearest cylinder boundary.  The
+	      size may be specified as $ in which case the partition will ex-
+	      tend to the end of the available free space.
+	      fdisk will not allow you to create partitions which overlap.
+
+	      If bootmenu is specified for any partition fdisk will determine
+	      whether the installed boot code supports the bootselect code, if
+	      it doesn't you will be asked whether you want to install the re-
+	      quired boot code.
+
+     -v      Be more verbose, specifying -v more than once may increase the
+	      amount of output.
+	      Specifying -v with -u allows the user to explicitly specify some
+	      values for with the default ies usually correct.
+
+     -w file
+	      Write the modified partition table to file file instead of the
+	      disk.
+
+     When called with no arguments, it prints the partition table.  An example
+     follows:
+
+	  Disk: /dev/rwd0d
+	  NetBSD disklabel disk geometry:
+	  cylinders: 16383, heads: 16, sectors/track: 63 (1008 sectors/cylinder)
+	  total sectors: 40032696
+
+	  BIOS disk geometry:
+	  cylinders: 1023, heads: 255, sectors/track: 63 (16065 sectors/cylinder)
+	  total sectors: 40032696
+
+	  Partition table:
+	  0: NetBSD (sysid 169)
+	      bootmenu: net 1.5.
+	      start 4209030, size 8289540 (4048 MB, Cyls 262-778), Active
+	  1: Primary DOS with 32 bit FAT (sysid 11)
+	      bootmenu: win98
+	      start 63, size 4208967 (2055 MB, Cyls 0-262)
+	  2: NetBSD (sysid 169)
+	      bootmenu: current
+	      start 32515560, size 7517136 (3670 MB, Cyls 2024-2491/234/40)
+	  3: Ext. partition - LBA (sysid 15)
+	      start 12498570, size 20016990 (9774 MB, Cyls 778-2024)
+	  Extended partition table:
+	  E0: NetBSD (sysid 169)
+	      bootmenu: test
+	      start 12498633, size 12305727 (6009 MB, Cyls 778-1544)
+	  E1: Primary DOS with 32 bit FAT (sysid 11)
+	      start 24804423, size 4096512 (2000 MB, Cyls 1544-1799)
+	  E2: Primary DOS with 32 bit FAT (sysid 11)
+	      start 28900998, size 3614562 (1765 MB, Cyls 1799-2024)
+	  Bootselector enabled, infinite timeout.
+
+     This example disk is divided into four partitions the last of which is an
+     extended partition.  The sub-partitions of the extended partition are al-
+     so shown.	 In this case there is no free space in either the disk or in
+     the extended partition.
+
+     The various fields in each partition entry are:
+	  ptn_number: id_name (sysid id_number)
+	      bootmenu: bootmenu
+	      start start, size size (MB MB, Cyls first-next)[, Active]
+
+     ptn_number
+		is the number of the partition.
+
+     id_name	is the name of the filesystem type or operating system that us-
+		es this partition.
+
+     id_number
+		is the number that identifies the partition type.  169 decimal
+		is used for NetBSD partitions.	The number 0 is used to mark a
+		partition as unused.  Use fdisk -l to list the known partition
+		types.
+
+     bootmenu	is the menu prompt output by the interactive boot code for this
+		partition.  This line is omitted if the prompt is not defined.

-     -u      Display each partition in turn and interactively ask if you want
-	      to edit it.  If you reply affirmatively, it will step through
-	      each field showing the old value and asking for a new one.  When
-	      you are done with a partition, fdisk will display the information
-	      again and ask if it is correct.  fdisk will then proceed to the
-	      next entry.
+     start, size
+		are the start address and size of the partition in sectors.
+
+     MB	is the size of the partition in megabytes.
+
+     first, next
+		are the bounds of this partition displayed as cylin-
+		der/head/sector.  If the partition starts (or ends) on a cylin-
+		der boundary the head and sector values are omitted.  If -v is
+		not specified the start of extended partitions and the first
+		partition on the disk are rounded down to include the mandatory
+		red tape in the preceeding track.
+
+     Active	is output if this is the active partition.
+
+     If the -v flag is given two additional lines are output for each parti-
+     tion:
+	  beg: cylinder cylinder, head head, sector sector
+	  end: cylinder cylinder, head head, sector sector
+
+     cylinder, head, sector
+		are the beginning or ending address of a partition.
+		Note: these numbers are read from the bootblock, so are the
+		values calculated by a previous run of fdisk.

 NOTES
+     Traditionally the partition boundaries should be on cylinder boundaries
+     using the BIOS geometry, all of the first track should be reserved for
+     use by the BIOS.	Although the BIOS geometry is typically different from
+     the geometry reported by the drive, neither will match the actual physi-
+     cal geometry for modern disks (the actual geometry will vary across the
+     disk).  Keeping the partition boundaries on cylinder boundaries makes
+     partitioning a driver easier as only relatively small numbers need be en-
+     tered.
+
      The automatic calculation of the starting cylinder and other parameters
      uses a set of figures that represent what the BIOS thinks is the geometry
-     of the drive.  These figures are by default taken from the incore diskla-
-     bel, but fdisk gives you an opportunity to change them.  This allows the
-     user to create a bootblock that can work with drives that use geometry
-     translation under the BIOS.
+     of the drive.  The default values should be correct for the system on
+     which fdisk is run, however if you move the disk to a different system
+     the BIOS of that system might use a different geometry translation.
+
+     If you run the equivalent of fdisk on a different operating system then
+     the bootmenu strings associated with extended partitions may be lost.

      Editing an existing partition is risky, and may cause you to lose all the
      data in that partition.

      You should run this program interactively once or twice to see how it
      works.  This is completely safe as long as you answer the last question
-     in the negative.
+     in the negative.	You can also specify -w file to write the output to a
+     file and later specify -r file to read back the updated information.
+     This can be done without having write access to the disk volume.

 FILES
      /usr/mdec/mbr	     Default location of i386 bootcode
      /usr/mdec/mbr_bootsel  Default location of i386 bootselect code
+     /usr/mdec/mbr_ext	     Default location of i386 bootselect for extended
+			     partitions

 SEE ALSO
      disklabel(8), mbr(8), mbrlabel(8)

 BUGS
+     The word 'partition' is used to mean both a mbr partition and a NetBSD
+     partition, sometimes in the same sentence.
+
      There are subtleties that the program detects that are not explained in
      this manual page.

-NetBSD 1.6			December 19, 2000			      3
+NetBSD 1.6			 April 30, 2003				      4

-- 
David Laight: david@l8s.co.uk