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