Difference between revisions of "Fdisk"

From ArchWiki
Jump to: navigation, search
(add gdisk to related articles)
(Create partitions: flag with Template:Expansion, each partition creation step should have their own subsection)
(4 intermediate revisions by the same user not shown)
Line 101: 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 ''fdisk'' to perform both the creation of a partition table and the creation of the actual partitions.
+
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 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 {{ic|/dev/sda}}:
 
Start ''fdisk'' against your drive as root. In this example we are using {{ic|/dev/sda}}:
Line 117: Line 113:
 
{{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.}}
 
{{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 {{ic|o}} at the prompt. Skip this step if the table you require has already been created.
+
To create a new partition table and clear all current partition data type {{ic|o}} at the prompt for a MBR partition table or {{ic|g}} for a GUID Partition Table (GPT). Skip this step if the table you require has already been created.
  
 
=== Create partitions ===
 
=== Create partitions ===
  
{{Expansion|Suggest using [https://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/ Discoverable Partitions].}}
+
{{Expansion|Place each step into a subsection.}}
  
 
Create a new partition with the {{ic|n}} command. You enter a partition type, 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.
  
 
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.  
 +
 +
{{Accuracy|Only last sector can use {{ic|''±size''}} in ''fdisk''.}}
  
 
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.
 +
 +
{{Expansion|Suggest using [https://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/ Discoverable Partitions].}}
  
 
Select the partition's type id. The default, {{ic|Linux filesystem}}, should be fine for most use. Press {{ic|l}} to show the codes list. You can make the partition bootable by typing {{ic|a}}.
 
Select the partition's type id. The default, {{ic|Linux filesystem}}, should be fine for most use. Press {{ic|l}} to show the codes list. You can make the partition bootable by typing {{ic|a}}.
Line 133: Line 133:
 
{{Tip|
 
{{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.
 
* 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 type {{ic|EFI System}}.
 
* [[GRUB]] requires a [[BIOS boot partition]] with type {{ic|BIOS boot}} 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|Linux swap}} 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.

Revision as of 05:07, 12 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.

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 partition table and clear all current partition data type o at the prompt for a MBR partition table or g for a GUID Partition Table (GPT). 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: Place each step into a subsection. (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.

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: Only last sector can use ±size in fdisk. (Discuss in Talk:Fdisk#)

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.

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

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

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 type EFI System.
  • 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