1.\" $FreeBSD: src/sbin/i386/fdisk/fdisk.8,v 1.17.2.11 2002/04/25 16:25:12 trhodes Exp $ 2.\" 3.Dd February 14, 2020 4.Dt FDISK 8 5.Os 6.Sh NAME 7.Nm fdisk 8.Nd PC slice table (MBR) maintenance program 9.Sh SYNOPSIS 10.Nm 11.Op Fl BCEIaistux 12.Op Fl b Ar bootcode 13.Op Fl p Ar diskimage 14.Op Fl 1234 15.Op Ar disk 16.Nm 17.Fl f Ar configfile 18.Op Fl itv 19.Op Ar disk 20.Sh PROLOGUE 21In order for the BIOS to boot the kernel, 22certain conventions must be adhered to. 23Sector 0 of the disk must contain an MBR, which contain boot code, 24a slice table, 25and a magic number. 26BIOS slices can be used to break the disk up into several pieces. 27.Dx 28slices are called partitions under DOS 29.Dq ( partition 30has another meaning under 31.Dx , 32see 33.Xr disklabel 8 ) . 34.Dx 35supports 30 MBR slices, s1-s4 will be DOS primary partitions, 36s5-s30 will be DOS partitions in extended DOS partitions. 37The BIOS brings in sector 0 and verifies the magic number. 38The sector 390 boot code then searches the slice table to determine which 40slice is marked 41.Em active . 42This boot code then brings in the bootstrap from the 43.Em active 44slice and, if marked bootable, runs it. 45Under DOS, 46you can have one or more slices with one 47.Em active . 48The DOS 49.Nm 50program can be used to divide space on the disk into slices and set one 51.Em active . 52.Sh DESCRIPTION 53The 54.Dx 55program, 56.Nm , 57serves a similar purpose to the DOS program. 58The first form is used to 59display slice information or to interactively edit the slice table. 60The second is used to write a slice table using a 61.Ar configfile 62and is designed to be used by other scripts/programs. 63.Pp 64Options are: 65.Bl -tag -width indent 66.It Fl a 67Change the active slice only. 68Ignored if 69.Fl f 70is given. 71.It Fl b Ar bootcode 72Get the boot code from the file 73.Ar bootcode . 74Default is 75.Pa /boot/mbr . 76.It Fl p Ar diskimage 77The disk image is specified as a normal file instead of as a device, 78which is useful when building emulated disks for vmware, bochs, etc.\& 79.It Fl B 80Reinitialize the boot code contained in sector 0 of the disk. 81Ignored if 82.Fl f 83is given. 84.It Fl C 85Set CHS fields to wrapped values. 86Normally the CHS fields for a slice are set to all 1's if they 87would otherwise wrap. 88This typically causes BIOSes to properly detect 89that the disk should be put in Large mode. 90This option may be needed on very old PCs. 91.It Fl E 92Use TRIM to erase the device/partition before creating the file system. 93The underlying device must have the TRIM sysctl enabled. 94Only devices that support TRIM will have such a sysctl option 95.Va ( kern.cam.da.X.trim_enabled ) . 96For use with the 97.Fl I 98or 99.Fl u 100option. 101.It Fl f Ar configfile 102Set slice values using the file 103.Ar configfile . 104The 105.Ar configfile 106always modifies existing slices, unless 107.Fl i 108is also given, in which case all existing slices are deleted (marked 109as 110.Dq unused ) 111before the 112.Ar configfile 113is read. 114The 115.Ar configfile 116can be 117.Sq - , 118in which case 119.Ar stdin 120is read. 121See 122.Sx CONFIGURATION FILE , 123below, for file syntax. 124.Pp 125.Em WARNING : 126when 127.Fl f 128is used, you are not asked if you really want to write the slices 129table (as you are in the interactive mode). 130Use with caution! 131.It Fl i 132Initialize sector 0 of the disk. 133This implies 134.Fl u , 135unless 136.Fl f 137is given. 138.It Fl I 139Initialize the contents of sector 0 140for one 141.Dx 142slice covering the entire disk. 143.It Fl s 144Print a summary of all slices on the disk and exit. 145All other options will be ignored. 146.It Fl t 147Test mode; do not write slice values. 148Generally used with the 149.Fl f 150option to see what would be written to the slice table. 151Implies 152.Fl v . 153.It Fl u 154Is used for updating (editing) sector 0 of the disk. 155Ignored if 156.Fl f 157is given. 158.It Fl v 159Be verbose. 160When 161.Fl f 162is used, 163.Nm 164prints out the slice table that is written to the disk. 165.It Fl x 166Expand the last valid slice in an existing fdisk table to fit 167the device. Used to expand traditional DOS labels when the underlying 168storage has been enlarged, or the image has been copied to a larger 169device. 170.Pp 171This feature does not work on GPT partitions, use 'gpt expand device' 172instead. 173.It Fl 1234 174Operate on a single fdisk entry only. 175Ignored if 176.Fl f 177is given. 178.El 179.Pp 180The final disk name can be provided as a 181.Sq bare 182disk name only, e.g.\& 183.Pa da0 , 184or as a fully qualified device node under 185.Pa /dev . 186If omitted, the disks 187.Pa ad0 , 188.Pa da0 189and 190.Pa vkd0 191are searched in that order, until one is found to respond. 192.Pp 193When called without options, 194.Nm 195prints the sector 0 slice table. 196An example follows: 197.Bd -literal 198 ******* Working on device /dev/ad0 ******* 199 parameters extracted from in-core disklabel are: 200 cylinders=769 heads=15 sectors/track=33 (495 blks/cyl) 201 202 parameters to be used for BIOS calculations are: 203 cylinders=769 heads=15 sectors/track=33 (495 blks/cyl) 204 205 Warning: BIOS sector numbering starts with sector 1 206 Information from DOS bootblock is: 207 The data for partition 1 is: 208 sysid 108,(DragonFly BSD) 209 start 495, size 380160 (185 Meg), flag 0 210 beg: cyl 1/ sector 1/ head 0; 211 end: cyl 768/ sector 33/ head 14 212 The data for partition 2 is: 213 sysid 164,(unknown) 214 start 378180, size 2475 (1 Meg), flag 0 215 beg: cyl 764/ sector 1/ head 0; 216 end: cyl 768/ sector 33/ head 14 217 The data for partition 3 is: 218 <UNUSED> 219 The data for partition 4 is: 220 sysid 99,(ISC UNIX, other System V/386, GNU HURD or Mach) 221 start 380656, size 224234 (109 Meg), flag 80 222 beg: cyl 769/ sector 2/ head 0; 223 end: cyl 197/ sector 33/ head 14 224.Ed 225.Pp 226The disk is divided into three slices that happen to fill the disk. 227The second slice overlaps the end of the first. 228(Used for debugging purposes.) 229.Bl -tag -width "cyl, sector and head" 230.It Em "sysid" 231is used to label the slice. 232.Dx 233reserves the magic number 108 decimal (6C in hexadecimal). 234.It Em start No and Em size 235fields provide the start address 236and size of a slice in sectors. 237.It Em "flag 80" 238specifies that this is the active slice. 239.It Em cyl , sector No and Em head 240fields are used to specify the beginning and end addresses of the slice. 241.It Em Note : 242these numbers are calculated using BIOS's understanding of the disk geometry 243and saved in the bootblock. 244.El 245.Pp 246The 247.Fl i 248and 249.Fl u 250flags are used to indicate that the slice data is to be updated. 251Unless the 252.Fl f 253option is also given, 254.Nm 255will enter a conversational mode. 256In this mode, no changes will be written to disk unless you explicitly tell 257.Nm 258to. 259.Pp 260.Nm Fdisk 261will display each slice and ask whether you want to edit it. 262If you say yes, 263.Nm 264will step through each field, show you the old value, 265and ask you for a new one. 266When you are done with the slice, 267.Nm 268will display it and ask you whether it is correct. 269.Nm Fdisk 270will then proceed to the next entry. 271.Pp 272Getting the 273.Em cyl , sector , 274and 275.Em head 276fields correct is tricky, so by default, 277they will be calculated for you; 278you can specify them if you choose to though. 279.Pp 280After all the slices are processed, 281you are given the option to change the 282.Em active 283slice. 284Finally, when all the new data for sector 0 has been accumulated, 285you are asked to confirm whether you really want to rewrite it. 286.Pp 287The difference between the 288.Fl u 289and 290.Fl i 291flags is that 292the 293.Fl u 294flag just edits (updates) the fields as they appear on the disk, 295while the 296.Fl i 297flag is used to 298.Dq initialize 299sector 0; 300it will set up the last BIOS slice to use the whole disk for 301.Dx 302and make it active. 303.Sh NOTES 304The automatic calculation of starting cylinder etc.\& uses 305a set of figures that represent what the BIOS thinks the 306geometry of the drive is. 307These figures are taken from the in-core disklabel by default, 308but 309.Nm 310initially gives you an opportunity to change them. 311This allows you to create a bootblock that can work with drives 312that use geometry translation under the BIOS. 313.Pp 314If you hand craft your disk layout, 315please make sure that the 316.Dx 317slice starts on a cylinder boundary. 318A number of decisions made later may assume this. 319(This might not be necessary later.) 320.Pp 321Editing an existing slice will most likely result in the loss of 322all data in that slice. 323.Pp 324You should run 325.Nm 326interactively once or twice to see how it works. 327This is completely safe as long as you answer the last question 328in the negative. 329There are subtleties that 330.Nm 331detects that are not fully explained in this manual page. 332.Sh CONFIGURATION FILE 333When the 334.Fl f 335option is given, a disk's slice table can be written using values 336from a 337.Ar configfile . 338The syntax of this file is very simple; 339each line is either a comment or a specification, as follows: 340.Bl -tag -width Ds 341.It Xo 342.Ic # 343.Ar comment ... 344.Xc 345Lines beginning with a 346.Sq # 347are comments and are ignored. 348.It Xo 349.Ic g 350.Ar spec1 351.Ar spec2 352.Ar spec3 353.Xc 354Set the BIOS geometry used in slice calculations. 355There must be 356three values specified, with a letter preceding each number: 357.Bl -tag -width Ds 358.Sm off 359.It Cm c Ar num 360.Sm on 361Set the number of cylinders to 362.Ar num . 363.Sm off 364.It Cm h Ar num 365.Sm on 366Set the number of heads to 367.Ar num . 368.Sm off 369.It Cm s Ar num 370.Sm on 371Set the number of sectors/track to 372.Ar num . 373.El 374.Pp 375These specs can occur in any order, as the leading letter determines 376which value is which; however, all three must be specified. 377.Pp 378This line must occur before any lines that specify slice 379information. 380.Pp 381It is an error if the following is not true: 382.Bd -literal -offset indent 3831 \(<= number of cylinders 3841 \(<= number of heads \(<= 256 3851 \(<= number of sectors/track < 64 386.Ed 387.Pp 388The number of cylinders should be less than or equal to 1024, but this 389is not enforced, although a warning will be printed. 390Note that a bootable 391.Dx 392slice (the 393.Dq Pa / 394file system) must lie completely within the 395first 1024 cylinders, if 396.Dq packet 397mode isn't used, see 398.Xr boot0cfg 8 ; 399if this is not true, booting may fail. 400Non-bootable slices do not have this restriction. 401.Pp 402Example (all of these are equivalent), for a disk with 1019 cylinders, 40339 heads, and 63 sectors: 404.Bd -literal -offset indent 405g c1019 h39 s63 406g h39 c1019 s63 407g s63 h39 c1019 408.Ed 409.It Xo 410.Ic p 411.Ar slice 412.Ar type 413.Ar start 414.Ar length 415.Xc 416Set the slice given by 417.Ar slice 418(1-4) to type 419.Ar type , 420starting at sector 421.Ar start 422for 423.Ar length 424sectors. 425.Pp 426Only those slices explicitly mentioned by these lines are modified; 427any slice not referenced by a 428.Cm p 429line will not be modified. 430However, if an invalid slice table is present, or the 431.Fl i 432option is specified, all existing slice entries will be cleared 433(marked as unused), and these 434.Cm p 435lines will have to be used to 436explicitly set slice information. 437If multiple slices need to be 438set, multiple 439.Cm p 440lines must be specified; one for each slice. 441.Pp 442These slice lines must occur after any geometry specification lines, 443if one is present. 444.Pp 445The 446.Ar type 447is 108 for 448.Dx 449slices. 450Specifying a slice type of zero is 451the same as clearing the slice and marking it as unused; however, 452dummy values (such as 453.Sq 0 ) 454must still be specified for 455.Ar start 456and 457.Ar length . 458.Pp 459Note: the start offset will be rounded upwards to a head boundary if 460necessary, and the end offset will be rounded downwards to a cylinder 461boundary if necessary. 462.Pp 463Example: to clear slice 4 and mark it as unused: 464.Bd -literal -offset indent 465p 4 0 0 0 466.Ed 467.Pp 468Example: to set slice 1 to a 469.Dx 470slice, starting at sector 1 471for 2503871 sectors (note: these numbers will be rounded upwards and 472downwards to correspond to head and cylinder boundaries): 473.Bd -literal -offset indent 474p 1 108 1 2503871 475.Ed 476.It Xo 477.Ic a 478.Ar slice 479.Xc 480Make 481.Ar slice 482the active slice. 483Can occur anywhere in the config file, but only 484one must be present. 485If no 486.Cm a 487line is present, all slices of the disk are made inactive. 488.Pp 489Example: to make slice 1 the active slice: 490.Bd -literal -offset indent 491a 1 492.Ed 493.El 494.Sh FILES 495.Bl -tag -width /boot/mbr -compact 496.It Pa /boot/mbr 497The default boot code 498.El 499.Sh COMPATIBILITY 500Due to the use of 32 bit to store the number of sectors in the MBR, 501.Nm 502can at most use 2^32 - 1 sectors. 503For the prevalent sector size of 512B this means a maximum of 2TB. 504Larger disks should be partitioned using 505.Xr gpt 8 . 506.Pp 507The MBR that 508.Nm 509manipulates, is used by many different OSs, but the exact requirements seems to differ, 510it can be a hassle to get multiple OSs to recognize the same MBR. 511Some OSs will require that slices are located at cylinder boundaries. 512.Sh SEE ALSO 513.Xr boot0cfg 8 , 514.Xr disklabel 8 , 515.Xr gpt 8 , 516.Xr newfs 8 517.Sh HISTORY 518A version of 519.Nm 520first appeared in the Mach Operating System. 521It was subsequently ported to 522.Bx 386 . 523.Sh AUTHORS 524.An -nosplit 525.Nm 526for Mach Operating System was written by 527.An Robert Baron Aq Mt rvb@cs.cmu.edu . 528It was ported to 529.Bx 386 530by 531.An Julian Elischer Aq Mt julian@tfs.com . 532.Sh BUGS 533Only slices s1-s4 534.Pq primary DOS partitions 535can be changed by 536.Nm , 537s5-s30 538.Pq slices in extended DOS partitions 539can't be changed by 540.Nm . 541.Pp 542The default boot code will not necessarily handle all slice types 543correctly, in particular those introduced since MS-DOS 6.x. 544.Pp 545The entire program should be made more user-friendly. 546.Pp 547Most users new to 548.Dx 549do not understand the difference between 550.Ar slice 551and 552.Ar partition 553causing difficulty to adjust. 554.Pp 555You cannot use this command to completely dedicate a disk to 556.Dx . 557The 558.Xr disklabel 8 559command must be used for this. 560