Difference between revisions of "Fdisk"

From ArchWiki
Jump to: navigation, search
m (Nl6720 moved page Gdisk to Fdisk without leaving a redirect: move page back to it's proper place)
(add gdisk to related articles)
(One intermediate revision by the same user not shown)
Line 8: Line 8:
 
{{Related articles start}}
 
{{Related articles start}}
 
{{Related|File systems}}
 
{{Related|File systems}}
 +
{{Related|gdisk}}
 
{{Related|GNU Parted}}
 
{{Related|GNU Parted}}
 
{{Related|Partitioning}}
 
{{Related|Partitioning}}
Line 14: Line 15:
 
[https://git.kernel.org/cgit/utils/util-linux/util-linux.git/ util-linux fdisk] is a dialogue-driven command-line utility that creates and manipulates partition tables and partitions on a hard disk. Hard disks are divided into partitions and this division is described in the partition table.
 
[https://git.kernel.org/cgit/utils/util-linux/util-linux.git/ util-linux fdisk] is a dialogue-driven command-line utility that creates and manipulates partition tables and partitions on a hard disk. Hard disks are divided into partitions and this division is described in the partition table.
  
[http://www.rodsbooks.com/gdisk/ gdisk] ([[GPT]] fdisk), works "on Globally Unique Identifier (GUID) Partition Table ([[GPT]]) disks, rather than on the more common (through at least early 2013) Master Boot Record ([[MBR]]) partition tables."
+
This article covers {{man|8|fdisk}} and its related {{man|8|sfdisk}} utility.
  
This article covers {{man|8|fdisk}} and its related {{man|8|sfdisk}} utility, as well as the analogous {{man|8|gdisk}} and {{man|8|sgdisk}} utilities.
+
{{Tip|For basic partitioning functionality with a text user interface, {{man|8|cfdisk}} can be used.}}
 
 
{{Tip|
 
* For basic partitioning functionality with a text user interface, {{man|8|cfdisk}} and {{man|8|cgdisk}} can be used.
 
* GPT fdisk website has detailed walkthroughs for [http://www.rodsbooks.com/gdisk/walkthrough.html gdisk], [http://www.rodsbooks.com/gdisk/cgdisk-walkthrough.html cgdisk] and [http://www.rodsbooks.com/gdisk/sgdisk-walkthrough.html sgdisk].
 
}}
 
  
 
== Installation ==
 
== Installation ==
  
 
To use ''fdisk'' and its associated utilities, the {{pkg|util-linux}} package, which is part of the {{grp|base}} group is required.
 
To use ''fdisk'' and its associated utilities, the {{pkg|util-linux}} package, which is part of the {{grp|base}} group is required.
 
To use ''gdisk'' and its associated utilities, [[install]] the {{pkg|gptfdisk}} package.
 
  
 
== List partitions ==
 
== List partitions ==
Line 36: Line 30:
  
 
{{Note|If the device is not specified, ''fdisk'' will list all partitions in {{ic|/proc/partitions}}.}}
 
{{Note|If the device is not specified, ''fdisk'' will list all partitions in {{ic|/proc/partitions}}.}}
 
Or for the ''gdisk'':
 
 
# gdisk -l /dev/sda
 
  
 
== Backup and restore partition table ==
 
== Backup and restore partition table ==
Line 104: Line 94:
  
 
  # sfdisk /dev/sda < sda.dump
 
  # sfdisk /dev/sda < sda.dump
 
=== Using sgdisk ===
 
 
Using ''sgdisk'' you can create a binary backup consisting of the protective MBR, the main GPT header, the backup GPT header, and one copy of the partition table. The example below will save the partition table of {{ic|/dev/sda}} to a file {{ic|sgdisk-sda.bin}}:
 
 
# sgdisk -b=sgdisk-sda.bin /dev/sda
 
 
You can later restore the backup by running:
 
 
# sgdisk -l=sgdisk-sda.bin /dev/sda
 
 
If you want to clone your current device's partition layout ({{ic|/dev/sda}} in this case) to another drive ({{ic|/dev/sdc}}) run:
 
 
# sgdisk -R=/dev/sdc /dev/sda
 
 
If both drives will be in the same computer, you need to randomize the GUID's:
 
 
# sgdisk -G /dev/sdc
 
  
 
== Create a partition table and partitions ==
 
== Create a partition table and partitions ==
Line 129: Line 101:
 
Before beginning, you may wish to [[#Backup and restore partition table|backup]] your current partition table and scheme.
 
Before beginning, you may wish to [[#Backup and restore partition table|backup]] your current partition table and scheme.
  
The following shows how to use both ''gdisk'' and ''fdisk'' to perform both the creation of a partition table and the creation of the actual partitions. Differences are noted when necessary.
+
The following shows how to use ''fdisk'' to perform both the creation of a partition table and the creation of the actual partitions.
  
 
=== Start the partition manipulator ===
 
=== Start the partition manipulator ===
 
Start either ''fdisk'' or ''gdisk'' as instructed in the following sections. Then continue with [[#Create new table]].
 
 
==== fdisk ====
 
  
 
Using [[MBR]], the utility for editing the partition table is called ''fdisk''. Recent versions of ''fdisk'' have abandoned the deprecated system of using cylinders as the default display unit, as well as MS-DOS compatibility by default. ''fdisk'' automatically aligns all partitions to 2048 sectors, or 1 MiB, which should work for all EBS sizes that are known to be used by SSD manufacturers. This means that the default settings will give you proper alignment.
 
Using [[MBR]], the utility for editing the partition table is called ''fdisk''. Recent versions of ''fdisk'' have abandoned the deprecated system of using cylinders as the default display unit, as well as MS-DOS compatibility by default. ''fdisk'' automatically aligns all partitions to 2048 sectors, or 1 MiB, which should work for all EBS sizes that are known to be used by SSD manufacturers. This means that the default settings will give you proper alignment.
Line 144: Line 112:
  
 
This opens the ''fdisk'' dialogue where you can type in commands.
 
This opens the ''fdisk'' dialogue where you can type in commands.
 
==== gdisk ====
 
 
Using [[GPT]], the utility for editing the partition table is called ''gdisk''. Alternatively, you may use the curses-based version called ''cgdisk''; however, the following instructions do not apply to it. See {{man|8|cgdisk}} for its usage.
 
 
''gdisk'' performs partition alignment automatically on a 2048 sector (1 MiB) block size base which should be compatible with the vast majority of SSDs if not all. [[GNU Parted]] also supports GPT, but is [http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=601813 less user-friendly] for aligning partitions.
 
 
To use ''gdisk'', run the program with the name of the device you want to change/edit. This example uses {{ic|/dev/sda}}:
 
 
# gdisk /dev/sda
 
  
 
=== Create new table ===
 
=== Create new table ===
Line 165: Line 123:
 
{{Expansion|Suggest using [https://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/ Discoverable Partitions].}}
 
{{Expansion|Suggest using [https://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/ Discoverable Partitions].}}
  
Create a new partition with the {{ic|n}} command. You enter a partition type (''fdisk'' only), partition number, starting sector, and an ending sector.
+
Create a new partition with the {{ic|n}} command. You enter a partition type, partition number, starting sector, and an ending sector.
  
For ''fdisk'', when prompted, specify the partition type, type {{ic|p}} to create a primary partition or {{ic|e}} to create an extended one. There may be up to four primary partitions.  
+
When prompted, specify the partition type, type {{ic|p}} to create a primary partition or {{ic|e}} to create an extended one. There may be up to four primary partitions.  
  
 
Both start and end sectors can be specified in absolute terms as sector numbers or as positions measured in kibibytes ({{ic|K}}), mebibytes ({{ic|M}}), gibibytes ({{ic|G}}), tebibytes ({{ic|T}}), or pebibytes ({{ic|P}}); for instance, {{ic|40M}} specifies a position 40MiB from the start of the disk. You can specify locations relative to the start or end of the specified default range by preceding the number by a {{ic|+}} or {{ic|-}} symbol, as in {{ic|+2G}} to specify a point 2GiB after the default start sector, or {{ic|-200M}} to specify a point 200MiB before the last available sector. Pressing the {{ic|Enter}} key with no input specifies the default value, which is the start of the largest available block for the start sector and the end of the same block for the end sector.
 
Both start and end sectors can be specified in absolute terms as sector numbers or as positions measured in kibibytes ({{ic|K}}), mebibytes ({{ic|M}}), gibibytes ({{ic|G}}), tebibytes ({{ic|T}}), or pebibytes ({{ic|P}}); for instance, {{ic|40M}} specifies a position 40MiB from the start of the disk. You can specify locations relative to the start or end of the specified default range by preceding the number by a {{ic|+}} or {{ic|-}} symbol, as in {{ic|+2G}} to specify a point 2GiB after the default start sector, or {{ic|-200M}} to specify a point 200MiB before the last available sector. Pressing the {{ic|Enter}} key with no input specifies the default value, which is the start of the largest available block for the start sector and the end of the same block for the end sector.
Line 176: Line 134:
 
* When partitioning it is always a good idea to follow the default values for first and last partition sectors. Additionally, specify partition sizes with the ''+<size>{M,G,...}'' notation. Such partitions are always aligned according to the device properties.
 
* When partitioning it is always a good idea to follow the default values for first and last partition sectors. Additionally, specify partition sizes with the ''+<size>{M,G,...}'' notation. Such partitions are always aligned according to the device properties.
 
* [[EFI system partition]] requires code {{ic|ef00}} with ''gdisk'' and {{ic|EFI System}} with ''fdisk''.
 
* [[EFI system partition]] requires code {{ic|ef00}} with ''gdisk'' and {{ic|EFI System}} with ''fdisk''.
* [[GRUB]] requires a [[BIOS boot partition]] with code {{ic|ef02}} with ''gdisk'' and {{ic|BIOS boot}} with ''fdisk'' when installing GRUB to a disk.
+
* [[GRUB]] requires a [[BIOS boot partition]] with type {{ic|BIOS boot}} when installing GRUB to a disk.
* It is recommended to use {{ic|8200}} with ''gdisk'' and {{ic|Linux swap}} with ''fdisk'' for any [[swap]] partitions, since systemd will automount it.
+
* It is recommended to use {{ic|Linux swap}} for any [[swap]] partitions, since systemd will automount it.
 
See the respective articles for considerations concerning the size and location of these partitions.
 
See the respective articles for considerations concerning the size and location of these partitions.
 
}}
 
}}
Line 188: Line 146:
  
 
== Tips and tricks ==
 
== Tips and tricks ==
 
=== Convert between MBR and GPT ===
 
 
{{Style|Remove opinions "one of the best". Consolidate long note.}}
 
 
''gdisk'', ''sgdisk'' and ''cgdisk'' have the ability to convert MBR and [[Wikipedia:BSD disklabel|BSD disklabels]] to GPT without data loss. Upon conversion, all the MBR primary partitions and the logical partitions become GPT partitions with the correct partition type GUIDs and Unique partition GUIDs created for each partition.  See Rod Smith's [http://www.rodsbooks.com/gdisk/mbr2gpt.html Converting to or from GPT] for more info.
 
 
After conversion, the bootloaders will need to be reinstalled to configure them to boot from GPT.
 
 
{{Note|
 
* GPT stores a secondary table at the end of disk. This data structure consumes 33 512-byte sectors by default. MBR does not have a similar data structure at its end, which means that the last partition on an MBR disk sometimes extends to the very end of the disk and prevents complete conversion. If this happens to you, you must abandon the conversion and resize the final partition.
 
* If your boot loader is GRUB, it needs a [[BIOS boot partition]].
 
* There are known corruption issues with the backup GPT table on laptops that are Intel chipset based, and run in RAID mode. The solution is to use AHCI instead of RAID, if possible.
 
}}
 
 
To convert an MBR partition table to GPT, use ''sgdisk''.
 
 
# sgdisk -g /dev/sda
 
 
To convert GPT to MBR use the {{ic|m}} option. Note that it is not possible to convert more than four partitions from GPT to MBR.
 
 
# sgdisk -m /dev/sda
 
  
 
=== Sort partitions ===
 
=== Sort partitions ===
  
 
This applies for when a new partition is created in the space between two partitions or a partition is deleted. {{ic|/dev/sda}} is used in this example.
 
This applies for when a new partition is created in the space between two partitions or a partition is deleted. {{ic|/dev/sda}} is used in this example.
 
MBR:
 
  
 
  # sfdisk -r /dev/sda
 
  # sfdisk -r /dev/sda
 
GPT:
 
 
# sgdisk -s /dev/sda
 
  
 
After sorting the partitions if you are not using [[Persistent block device naming]], it might be required to adjust the {{ic|/etc/fstab}} and/or the {{ic|/etc/crypttab}} configuration files.
 
After sorting the partitions if you are not using [[Persistent block device naming]], it might be required to adjust the {{ic|/etc/fstab}} and/or the {{ic|/etc/crypttab}} configuration files.
  
 
{{Note|The kernel must read the new partition table for the partitions (e.g. {{ic|/dev/sda1}}) to be usable. Reboot the system or tell the kernel to [https://serverfault.com/questions/36038/reread-partition-table-without-rebooting reread the partition table].}}
 
{{Note|The kernel must read the new partition table for the partitions (e.g. {{ic|/dev/sda1}}) to be usable. Reboot the system or tell the kernel to [https://serverfault.com/questions/36038/reread-partition-table-without-rebooting reread the partition table].}}
 
=== Recover GPT header ===
 
 
In case main GPT header or backup GPT header gets damaged, you can recover one from the other with ''gdisk''. {{ic|/dev/sda}} is used in this example.
 
 
# gdisk /dev/sda
 
 
choose {{ic|r}} for recovery and transformation options (experts only). From there choose either
 
 
* {{ic|b}}: use backup GPT header (rebuilding main)
 
* {{ic|d}}: use main GPT header (rebuilding backup)
 
 
When done write the table to disk and exit via the {{ic|w}} command.
 
  
 
== See also ==
 
== See also ==
  
* [http://www.rodsbooks.com/gdisk/index.html GPT fdisk Tutorial] - offical webpage of gdisk with detailed walkthroughs.
 
 
* [http://www.novell.com/coolsolutions/feature/19350.html Manually Partitioning Your Hard Drive with fdisk]
 
* [http://www.novell.com/coolsolutions/feature/19350.html Manually Partitioning Your Hard Drive with fdisk]

Revision as of 09:12, 11 August 2018

util-linux fdisk is a dialogue-driven command-line utility that creates and manipulates partition tables and partitions on a hard disk. Hard disks are divided into partitions and this division is described in the partition table.

This article covers fdisk(8) and its related sfdisk(8) utility.

Tip: For basic partitioning functionality with a text user interface, cfdisk(8) can be used.

Installation

To use fdisk and its associated utilities, the util-linux package, which is part of the base group is required.

List partitions

To list partition tables and partitions on a device, you can run the following, where device is a name like /dev/sda:

# fdisk -l /dev/sda
Note: If the device is not specified, fdisk will list all partitions in /proc/partitions.

Backup and restore partition table

Before making changes to a hard disk, you may want to backup the partition table and partition scheme of the drive. You can also use a backup to copy the same partition layout to numerous drives.

Using dd

Tango-go-next.pngThis article or section is a candidate for moving to Disk cloning#Using dd.Tango-go-next.png

Notes: dd is not part of util-linux and does not have anything to do with fdisk. (Discuss in Talk:Fdisk#)

The MBR is stored in the the first 512 bytes of the disk. It consists of 4 parts:

  1. The first 440 bytes contain the bootstrap code (boot loader).
  2. The next 6 bytes contain the disk signature.
  3. The next 64 bytes contain the partition table (4 entries of 16 bytes each, one entry for each primary partition).
  4. The last 2 bytes contain a boot signature.

To save the MBR as mbr_file.img:

# dd if=/dev/sdX of=/path/to/mbr_file.img bs=512 count=1

You can also extract the MBR from a full dd disk image:

# dd if=/path/to/disk.img of=/path/to/mbr_file.img bs=512 count=1

To restore (be careful, this destroys the existing partition table and with it access to all data on the disk):

# dd if=/path/to/mbr_file.img of=/dev/sdX bs=512 count=1
Warning: Restoring the MBR with a mismatching partition table will make your data unreadable and nearly impossible to recover. If you simply need to reinstall the bootloader see their respective pages as they also employ the DOS compatibility region: GRUB or Syslinux.

If you only want to restore the boot loader, but not the primary partition table entries, just restore the first 440 bytes of the MBR:

# dd if=/path/to/mbr_file.img of=/dev/sdX bs=440 count=1

To restore only the partition table, one must use:

# dd if=/path/to/mbr_file.img of=/dev/sdX bs=1 skip=446 count=64

To erase the MBR bootstrap code (may be useful if you have to do a full reinstall of another operating system) only the first 440 bytes need to be zeroed:

# dd if=/dev/zero of=/dev/sdX bs=440 count=1

Using sfdisk

For both GPT and MBR you can use sfdisk to save the partition layout of your device to a file with the -d/--dump option. Run the following command for device /dev/sda:

# sfdisk -d /dev/sda > sda.dump

The file should look something like this for a single ext4 partition that is 1 GiB in size:

sda.dump
label: gpt
label-id: AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE
device: /dev/sda
unit: sectors
first-lba: 34
last-lba: 1048576

/dev/sda1 : start=2048, size=1048576, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=BBF1CD36-9262-463E-A4FB-81E32C12BDE7

To later restore this layout you can run:

# sfdisk /dev/sda < sda.dump

Create a partition table and partitions

The first step to partitioning a disk is making a partition table. After that, the actual partitions are created according to the desired partition scheme. See the partition table article to help decide whether to use MBR or GPT.

Before beginning, you may wish to backup your current partition table and scheme.

The following shows how to use fdisk to perform both the creation of a partition table and the creation of the actual partitions.

Start the partition manipulator

Using MBR, the utility for editing the partition table is called fdisk. Recent versions of fdisk have abandoned the deprecated system of using cylinders as the default display unit, as well as MS-DOS compatibility by default. fdisk automatically aligns all partitions to 2048 sectors, or 1 MiB, which should work for all EBS sizes that are known to be used by SSD manufacturers. This means that the default settings will give you proper alignment.

Start fdisk against your drive as root. In this example we are using /dev/sda:

# fdisk /dev/sda

This opens the fdisk dialogue where you can type in commands.

Create new table

Warning: If you create a new partition table on a disk with data on it, it will erase all the data on the disk. Make sure this is what you want to do.

To create a new MBR partition table and clear all current partition data, type o at the prompt. Skip this step if the table you require has already been created.

Create partitions

Tango-view-fullscreen.pngThis article or section needs expansion.Tango-view-fullscreen.png

Reason: Suggest using Discoverable Partitions. (Discuss in Talk:Fdisk#)

Create a new partition with the n command. You enter a partition type, partition number, starting sector, and an ending sector.

When prompted, specify the partition type, type p to create a primary partition or e to create an extended one. There may be up to four primary partitions.

Both start and end sectors can be specified in absolute terms as sector numbers or as positions measured in kibibytes (K), mebibytes (M), gibibytes (G), tebibytes (T), or pebibytes (P); for instance, 40M specifies a position 40MiB from the start of the disk. You can specify locations relative to the start or end of the specified default range by preceding the number by a + or - symbol, as in +2G to specify a point 2GiB after the default start sector, or -200M to specify a point 200MiB before the last available sector. Pressing the Enter key with no input specifies the default value, which is the start of the largest available block for the start sector and the end of the same block for the end sector.

Select the partition's type id. The default, Linux filesystem, should be fine for most use. Press l to show the codes list. You can make the partition bootable by typing a.

Tip:
  • When partitioning it is always a good idea to follow the default values for first and last partition sectors. Additionally, specify partition sizes with the +<size>{M,G,...} notation. Such partitions are always aligned according to the device properties.
  • EFI system partition requires code ef00 with gdisk and EFI System with fdisk.
  • GRUB requires a BIOS boot partition with type BIOS boot when installing GRUB to a disk.
  • It is recommended to use Linux swap for any swap partitions, since systemd will automount it.

See the respective articles for considerations concerning the size and location of these partitions.

Repeat this procedure until you have the partitions you desire.

Write changes to disk

Write the table to disk and exit via the w command.

Tips and tricks

Sort partitions

This applies for when a new partition is created in the space between two partitions or a partition is deleted. /dev/sda is used in this example.

# sfdisk -r /dev/sda

After sorting the partitions if you are not using Persistent block device naming, it might be required to adjust the /etc/fstab and/or the /etc/crypttab configuration files.

Note: The kernel must read the new partition table for the partitions (e.g. /dev/sda1) to be usable. Reboot the system or tell the kernel to reread the partition table.

See also