1.\" $NetBSD: fdisk.8,v 1.75 2012/01/04 10:57:40 wiz Exp $ 2.\" 3.Dd December 1, 2011 4.Dt FDISK 8 5.Os 6.Sh NAME 7.Nm fdisk 8.Nd MS-DOS partition maintenance program 9.Sh SYNOPSIS 10.Nm 11.Op Fl aBFfIiSuv 12.Bk -words 13.Oo 14.Fl 0 | 1 | 2 | 3 | E Ar number 15.Op Fl s Ar id/start/size Ns Bq Ar /bootmenu 16.Oc 17.Ek 18.Bk -words 19.Op Fl r Ar file | Fl w Ar file 20.Ek 21.Bk -words 22.Op Fl A Ar ptn_alignment Ns Bq Ar /ptn_0_offset 23.Ek 24.Bk -words 25.Op Fl b Ar cylinders/heads/sectors 26.Ek 27.Bk -words 28.Op Fl c Ar bootcode 29.Ek 30.Bk -words 31.Op Fl T Ar disktype 32.Ek 33.Bk -words 34.Op Fl t Ar disktab 35.Ek 36.Bk -words 37.Op Ar device 38.Ek 39.Nm 40.Fl l 41.Sh DESCRIPTION 42The 43.Nm 44program is used to display or update the 45.Em "master boot record" 46or 47.Em MBR 48in the first sector (sector 0) 49of a disk that uses the MBR style of partitioning. 50The following 51.Nx 52ports use this style of disk partitioning: 53amd64, arc, bebox, cobalt, hpcarm, hpcmips, hpcsh, i386, macppc, 54mvmeppc, netwinder, ofppc, and prep. 55.Pp 56The MBR contains bootable code, a partition table, 57an indication of which partition is 58.Sq active , 59and (optionally, depending on the boot code) a menu 60for selecting a partition to be booted. 61There can be at most 4 partitions defined in sector 0, 62one of which can be an extended 63partition which can be split into any number of sub-partitions (then called 64logical partitions). 65.Pp 66The boot code in the MBR is usually invoked by the BIOS or firmware, 67and the MBR passes control to the next stage boot code 68stored in the first sector of the partition to be booted 69(the 70.Em "partition boot record" 71or 72.Em PBR ) . 73.Pp 74After booting, 75.Nx 76does not use the partitioning done by 77.Nm , 78instead it uses a 79.Nx 80disklabel saved in sector 1 of the 81.Nx 82partition. 83See 84.Xr mbrlabel 8 85for a way of using information from the MBR 86to construct a 87.Nx 88disklabel. 89.Pp 90The standard MBR boot code will only boot the 91.Sq active 92partition. 93However, 94.Nx 95contains additional boot programs which allow the user to 96interactively select which of the partitions to boot. 97The 98.Sq mbr_ext 99code will also boot 100.Nx 101from an extended partition but will not work on old systems that do not 102support LBA reads, the 103.Sq mbr_com0 104and 105.Sq mbr_com0_9600 106will read and write from a serial port. 107At the start the 108.Nm 109program will determine whether the disk sector 0 is valid as a boot sector. 110(This is determined by checking the magic number.) 111If not, 112.Nm 113will initialise the boot code as well as the partition table. 114During this, all four partitions will be marked empty. 115.Pp 116The flags 117.Fl a , 118.Fl i 119or 120.Fl u 121are used to indicate that the partition data is to be updated. 122The 123.Nm 124program will enter an interactive conversational mode. 125This mode is designed not to change any data unless you explicitly tell it to; 126.Nm 127selects defaults for its questions to guarantee that behaviour. 128.Pp 129If partition data is going to be updated and the disk carries GUID Partition 130Tables, 131.Nm 132will remove both primary and backup GPT headers from the disk. 133See 134.Xr gpt 8 135for information on how to manipulate GUID Partition Tables. 136.Pp 137.Nm 138will calculate the correct 139.Em cylinder , 140.Em head , 141and 142.Em sector 143values for any partition you edit. 144If you specify 145.Fl v 146you will be asked whether you want to specify them yourself. 147.Pp 148Finally, when all the data for the first sector has been accumulated, 149.Nm 150will ask if you really want to write the new partition table. 151Only if you reply affirmatively to this question will 152.Nm 153write anything to the disk. 154.Pp 155Available options: 156.Pp 157.Bl -tag -width Ds 158.It Fl 0 159Specify partition slot 0 to be printed or updated. 160.It Fl 1 161Specify partition slot 1 to be printed or updated. 162.It Fl 2 163Specify partition slot 2 to be printed or updated. 164.It Fl 3 165Specify partition slot 3 to be printed or updated. 166.It Fl A Ar ptn_alignment Ns Bq Ar /ptn_0_offset 167Specify the alignment for all partitions and optionally the offset for the 168first partition of the disk and of logical partitions. 169If 170.Ar ptn_alignment 171is specified and 172.Ar ptn_0_offset 173is not specified, then the offset is set to the alignment. 174If 175.Fl A 176isn't specified, then the alignment of the first partition is inspected. 177If it ends on a 2048 sector boundary, then the alignment is set to 2048, 178if the start is a power of 2 less than, or equal to 2048 then the offset 179is set to the start sector. 180If the first partition isn't defined then the alignment and offset for disks 181larger than 128GB is set to 2048 (1MB). 182In all other cases the alignment default to a cylinder 183and the offset to a track (both using the BIOS geometry). 184The 1MB alignment is the same as that used by recent windows versions. 185.It Fl a 186Change the active partition. 187In interactive mode this question will be asked after the partitions 188have been processed. 189.It Fl B 190On an i386 or amd64 system, interactively update the boot selector settings. 191(The boot selector permits the user to interactively select the boot 192partition, and thus which operating system is run, at system boot time; see 193.Xr mbr 8 194for more information.) 195.It Fl b Ar cylinders/heads/sectors 196Specify the BIOS geometry parameters for 197.Ar cylinders , 198.Ar heads , 199and 200.Ar sectors . 201It is used only in conjunction with the 202.Fl u 203flag. 204If not specified the BIOS geometry will be obtained using sysctl (i386 and 205amd64) or by solving the simultaenous equations from the existing partition 206information. 207If that fails then either the geometry from the disklabel or 63 sectors and 20816 heads is used. 209.It Fl c Ar bootcode 210Specify the filename that 211.Nm 212should read the bootcode from. 213If the name of a directory is specified, then 214.Nm 215will look for files with the default names in that directory. 216The default is to read from 217.Pa /usr/mdec/mbr , 218.Pa /usr/mdec/mbr_bootsel 219or 220.Pa /usr/mdec/mbr_ext 221depending on whether 222.Ar bootmenu 223was specified for any partitions 224on an i386 machine, and leave the bootcode empty for other 225machines. 226.It Fl E Ar number 227Specify logical partition 228.Ar number 229to be printed or updated. 230If the specified logical partition doesn't exist on updating partition data 231an additional logical partition will be created. 232.It Fl F 233Indicate that 234.Ar device 235is a regular file. 236Unless the geometry of 237.Ar device 238is told to 239.Nm 240by 241.Fl T Ar disktype , 242.Nm 243will count the 512-byte sectors in 244.Ar device 245and produce a fake geometry. 246If 247.Ar device 248is a regular file, 249.Fl F 250will be used implicitly. 251.It Fl f 252Run 253.Nm 254in a non-interactive mode. 255In this mode, you can only change the disk parameters by using the 256.Fl b 257flag. 258This is provided only so scripts or other programs may use 259.Nm 260as part of an automatic installation process. 261.Pp 262Using the 263.Fl f 264flag with 265.Fl u 266makes it impossible to specify the starting and ending 267.Ar cylinder , 268.Ar head , 269and 270.Ar sector 271fields 272.Pq only Ar start No and Ar size No can be specified by Fl s No option . 273They will be automatically computed using the BIOS geometry. 274.It Fl I 275Ignore errors from overlapping partitions. 276Some devices (cameras CHDK) require overlapping partitions to support 277bigger than 4GB cards. 278The 279.Fl I 280flag ignores overlapping error checks and does not fix them, allowing these 281incorrect configurations to be used. 282.It Fl i 283Explicitly request initialisation of the master boot code 284(similar to what 285.Ic fdisk /mbr 286does under 287.Tn MS-DOS ) , 288even if the magic number in the first sector is ok. 289The partition table is left alone by this (but see above). 290.It Fl l 291Lists known 292.Em sysid 293values and exit. 294.It Fl r Ar file 295Read the boot record from file 296.Ar file 297instead of the specified disk. 298The geometry information used is still that of the disk volume. 299Any changes are written back to the file. 300.It Fl S 301When used with no other flags print a series of 302.Pa /bin/sh 303commands for setting variables to the partition information. 304This could be used by installation scripts. 305.It Fl s Ar id/start/size Ns Bq Ar /bootmenu 306Specify the partition 307.Ar id , 308.Ar start , 309.Ar size , 310and optionally 311.Ar bootmenu . 312This flag requires the use of a partition selection flag 313.Pq Fl 0 , 1 , 2 , 3 , No or Fl E Ar number . 314.It Fl T Ar disktype 315Use the disklabel 316.Ar disktype 317instead of the disklabel on 318.Ar device . 319.It Fl t Ar disktab 320Read 321.Ar disktype 322from the named 323.Xr disktab 5 324file instead of from 325.Pa /etc/disktab . 326.It Fl u 327Update partition data, including 328.Em id , start , No and Em size . 329Unless 330.Fl f 331option 332.Pq non-interactive mode 333is specified, 334.Nm 335will display the partitions and interactively ask which one you want to edit. 336.Nm 337will step through each field showing the old value and asking for a new one. 338The 339.Em start 340and 341.Em size 342can be specified in blocks (NN), 343cylinders (NNc or NNcyl), 344megabytes (NNm or NNMB), 345or gigabytes (NNg or NNGB), values in megabytes and gigabytes 346will be rounded to the nearest cylinder boundary. 347The 348.Em size 349may be specified as 350.Em $ 351in which case the partition will extend to the end of the available free space. 352.Pp 353In a non-interactive mode 354.Pq specified by Fl f No option , 355partition data should be specified by 356.Fl s 357option. 358A partition selection option 359.Pq Fl 0 , 1 , 2 , 3 , No or Fl E Ar number 360should also be specified to select a partition slot to be updated. 361.Pp 362.Nm 363will not allow you to create partitions which overlap. 364If 365.Fl u 366and 367.Fl s 368are specified in a non-interactive mode 369then the details of the specified partition will be changed. 370Any other partitions which overlap the requested part of the disk will be 371silently deleted. 372.Pp 373If 374.Em bootmenu 375is specified for any partition 376.Nm 377will determine whether the installed boot code supports the bootselect code, 378if it doesn't you will be asked whether you want to install the required 379boot code. 380To remove a 381.Em bootmenu 382label, simply press 383.Aq space 384followed by 385.Aq return . 386.It Fl v 387Be more verbose, specifying 388.Fl v 389more than once may increase the amount of output. 390.Pp 391Using 392.Fl v 393with 394.Fl u 395allows the user to change more parameters than normally permitted. 396.It Fl w Ar file 397Write the modified partition table to file 398.Ar file 399instead of the disk. 400.El 401.Pp 402When called with no arguments, it prints the partition table. 403An example follows: 404.Bd -literal 405 Disk: /dev/rwd0d 406 NetBSD disklabel disk geometry: 407 cylinders: 16383, heads: 16, sectors/track: 63 (1008 sectors/cylinder) 408 total sectors: 40032696 409 410 BIOS disk geometry: 411 cylinders: 1023, heads: 255, sectors/track: 63 (16065 sectors/cylinder) 412 total sectors: 40032696 413 414 Partition table: 415 0: NetBSD (sysid 169) 416 bootmenu: net 1.5. 417 start 4209030, size 8289540 (4048 MB, Cyls 262-778), Active 418 1: Primary DOS with 32 bit FAT (sysid 11) 419 bootmenu: win98 420 start 63, size 4208967 (2055 MB, Cyls 0-262) 421 2: NetBSD (sysid 169) 422 bootmenu: current 423 start 32515560, size 7517136 (3670 MB, Cyls 2024-2491/234/40) 424 3: Ext. partition - LBA (sysid 15) 425 start 12498570, size 20016990 (9774 MB, Cyls 778-2024) 426 Extended partition table: 427 E0: NetBSD (sysid 169) 428 bootmenu: test 429 start 12498633, size 12305727 (6009 MB, Cyls 778-1544) 430 E1: Primary DOS with 32 bit FAT (sysid 11) 431 start 24804423, size 4096512 (2000 MB, Cyls 1544-1799) 432 E2: Primary DOS with 32 bit FAT (sysid 11) 433 start 28900998, size 3614562 (1765 MB, Cyls 1799-2024) 434 Bootselector enabled, infinite timeout. 435 First active partition: 0 436.Ed 437.Pp 438This example disk is divided into four partitions, the last of which is 439an extended partition. 440The logical partitions of the extended partition are also shown. 441In this case there is no free space in either the disk or in the extended 442partition. 443.Pp 444The various fields in each partition entry are: 445.br 446.in +4 447.Em ptn_number : id_name 448(sysid 449.Em id_number ) 450.br 451.in +4 452bootmenu: 453.Em bootmenu 454.br 455start 456.Em start , 457size 458.Em size ( MB 459MB, Cyls 460.Em first Ns No - Ns Em next ) 461.Op , Active 462.in -4 463.in -4 464.Bl -tag -width "bootmenu" 465.It Em ptn_number 466is the number of the partition. 467.It Em id_name 468is the name of the filesystem type or operating system that uses this partition. 469.It Em id_number 470is the number that identifies the partition type. 471169 decimal is used for 472.Nx 473partitions, 47415 decimal to create an extended partition 475and 0 to mark a partition as unused. 476Use 477.Nm 478.Fl l 479to list the known partition types. 480.It Em bootmenu 481is the menu prompt output by the interactive boot code for this partition. 482This line is omitted if the prompt is not defined. 483.It Em start , Em size 484are the start address and size of the partition in sectors. 485.It Em MB 486is the size of the partition in megabytes. 487.It Em first , Em next 488are the bounds of this partition displayed as cylinder/head/sector. 489If the partition starts (or ends) on a cylinder boundary the head and 490sector values are omitted. 491If 492.Fl v 493is not specified the start of logical partitions and the first partition 494on the disk are rounded down to include the mandatory red tape in the 495preceding track. 496.It Active 497is output if this is the active partition. 498.El 499.Pp 500If the 501.Fl v 502flag is specified, the beginning and end of each partition are also 503displayed as follows: 504.in +4 505beg: cylinder 506.Em cylinder , 507head 508.Em head , 509sector 510.Em sector 511.br 512end: cylinder 513.Em cylinder , 514head 515.Em head , 516sector 517.Em sector 518.in -4 519.Bl -tag -width "bootmenu" 520.It Em "cylinder" , Em "head" , Em "sector" 521are the beginning or ending address of a partition. 522.Pp 523.Em "Note:" 524these numbers are read from the bootblock, so are the values calculated 525by a previous run of 526.Nm . 527.El 528.Pp 529.Nm 530attempts to check whether each partition is bootable, 531by checking the magic number and some other characteristics 532of the first sector of each partition (the PBR). 533If the partition does not appear to be bootable, 534.Nm 535will print a line containing 536.Dq "PBR is not bootable" 537followed by an error message. 538If the partition is bootable, and if the 539.Fl v 540flag is specified, 541.Nm 542will print 543.Dq "PBR appears to be bootable" . 544If the 545.Fl v 546flag is specified more than once, 547.Nm 548will print the heading 549.Dq "Information from PBR:" 550followed by one or more lines of information gleaned from the PBR; 551this additional information may be incorrect or misleading, 552because different operating systems use different PBR formats. 553Note that, even if no errors are reported, an attempt to boot 554from the partition might fail. 555.Nx 556partitions may be made bootable using 557.Xr installboot 8 . 558.Sh NOTES 559This program is only available (and useful) on systems with PC-platform-style 560MBR partitioning. 561.Pp 562Traditionally the partition boundaries should be on cylinder boundaries 563using the BIOS geometry, with the exception of the first partition, 564which traditionally begins in the second track of the first cylinder 565(cylinder 0, head 1, sector 1). 566Although the BIOS geometry is typically different from the geometry 567reported by the drive, neither will match the actual physical geometry 568for modern disks (the actual geometry will vary across the disk). 569Keeping the partition boundaries on cylinder boundaries makes partitioning 570a driver easier as only relatively small numbers need be entered. 571.Pp 572The automatic calculation of the starting cylinder and 573other parameters uses 574a set of figures that represent what the BIOS thinks is the 575geometry of the drive. 576The default values should be correct for the system on which 577.Nm 578is run; however, if you move the disk to a different system, the 579BIOS of that system might use a different geometry translation. 580.Pp 581If you run the equivalent of 582.Nm 583on a different operating system then the 584.Ar bootmenu 585strings associated with extended partitions may be lost. 586.Pp 587Editing an existing partition is risky, and may cause you to 588lose all the data in that partition. 589.Pp 590You should run this program interactively once or twice to see how it works. 591This is completely safe as long as you answer the last question in the negative. 592You can also specify 593.Fl w Ar file 594to write the output to a file and later specify 595.Fl r Ar file 596to read back the updated information. 597This can be done without having write access to the disk volume. 598.Sh FILES 599.Bl -tag -width /usr/mdec/mbrxxxxxxxx -compact 600.It Pa /usr/mdec/mbr 601Default location of i386 bootcode 602.It Pa /usr/mdec/mbr_bootsel 603Default location of i386 bootselect code 604.It Pa /usr/mdec/mbr_ext 605Default location of i386 bootselect for extended partitions (i.e., NetBSD on 606logical partitions) 607.El 608.Sh EXAMPLES 609Update MBR partition data of 610.Pa /dev/rwd0d 611in interactive mode: 612.Pp 613.Dl Ic fdisk -u /dev/rwd0d 614.Pp 615Change active MBR partition of 616.Pa /dev/rwd0d 617in interactive mode: 618.Pp 619.Dl Ic fdisk -a /dev/rwd0d 620.Pp 621Install MBR bootcode 622.Pa /usr/mdec/mbr_bootsel 623into 624.Pa /dev/rwd0d : 625.Pp 626.Dl Ic fdisk -c /usr/mdec/mbr_bootsel /dev/rwd0d 627.Pp 628Set MBR partition data for slot 0 of 629.Pa /dev/rwd0d 630specifying values without prompt: 631.Pp 632.Dl Ic fdisk -f -u -0 -s 169/63/2097089 /dev/rwd0d 633.Pp 634Make partition slot 0 of 635.Pa /dev/rwd0d 636active without prompt: 637.Pp 638.Dl Ic fdisk -f -a -0 /dev/rwd0d 639.Pp 640Initialize and create MBR partition data using bootcode 641.Pa destdir/usr/mdec/mbr 642without prompt against 1GB disk image file 643.Pa diskimg : 644.Pp 645.Dl Ic fdisk -f -i -b 130/255/63 -c destdir/usr/mdec/mbr -F diskimg 646.Pp 647Create MBR partition data for slot 0 which has an active 648.Nx 649partition using whole disk without prompt against 1GB disk image file 650.Pa diskimg : 651.Pp 652.Dl Ic fdisk -f -a -u -0 -s 169/63/2097089 -F diskimg 653.Sh SEE ALSO 654.Xr disktab 5 , 655.Xr boot 8 , 656.Xr disklabel 8 , 657.Xr gpt 8 , 658.Xr installboot 8 , 659.Xr mbr 8 , 660.Xr mbrlabel 8 661.Sh BUGS 662The word 663.Sq partition 664is used to mean both an MBR partition and a 665.Nx 666partition, sometimes in the same sentence. 667.Pp 668There are subtleties that the program detects that are not explained in 669this manual page. 670