Fdisk: Difference between revisions

From ArchWiki
m (→‎First and last sector: Add "on a BIOS-based system" to 'Tip' section to clarify BIOS boot partition not required when using UEFI/GPT.)
 
(25 intermediate revisions by 6 users not shown)
Line 5: Line 5:
[[de:fdisk]]
[[de:fdisk]]
[[es:Fdisk]]
[[es:Fdisk]]
[[fa:Fdisk]]
[[ja:Fdisk]]
[[ja:Fdisk]]
[[ru:Fdisk]]
[[ru:Fdisk]]
[[tr:Fdisk]]
[[zh-hans:Fdisk]]
[[zh-hans:Fdisk]]
{{Related articles start}}
{{Related articles start}}
Line 31: Line 31:
== List partitions ==
== List partitions ==


To list partition tables and partitions on a device, you can run the following, where device is a name like {{ic|/dev/sda}}:
To list partition tables and partitions on a [[block device]], you can run the following, where device is a name like {{ic|/dev/sda}}, {{ic|/dev/nvme0n1}}, {{ic|/dev/mmcblk0}}, etc.:


  # fdisk -l /dev/sda
  # fdisk -l /dev/sda
Line 54: Line 54:
first-lba: 34
first-lba: 34
last-lba: 1048576
last-lba: 1048576
sector-size: 512


/dev/sda1 : start=2048, size=1048576, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=BBF1CD36-9262-463E-A4FB-81E32C12BDE7
/dev/sda1 : start=2048, size=1048576, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=BBF1CD36-9262-463E-A4FB-81E32C12BDE7
Line 68: Line 69:
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.


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'' performs partition alignment automatically on a 2048 512-byte sector (1 MiB) block size base which should be compatible with all [[Advanced Format]] HDDs and the vast majority of [[SSD]]s if not all. This means that the default settings will give you proper alignment.
''fdisk'' performs partition alignment automatically on a 2048 512-byte sector (1 MiB) block size base which should be compatible with all [[Advanced Format]] HDDs and the vast majority of [[SSD]]s if not all. This means that the default settings will give you proper alignment.


To use ''fdisk'', run the program with the name of the [[block device]] you want to change/edit. This example uses {{ic|/dev/sda}}:
To use ''fdisk'', run the program with the name of the [[block device]] you want to change/edit. This example uses {{ic|/dev/sda}}:
Line 80: Line 81:
{{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 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.
{{Tip|
* Check that your NVMe drives and Advanced Format hard disk drives are using the [[Advanced Format|optimal logical sector size]] before partitioning.
* Consider performing [[SSD memory cell clearing]] before partitioning an SSD.
}}
 
To create a new partition table and clear all current partition data type {{ic|g}} at the prompt for a GUID Partition Table (GPT) or {{ic|o}} for an MBR partition table. Skip this step if the table you require has already been created.


=== Create partitions ===
=== Create partitions ===


Create a new partition with the {{ic|n}} command. You must enter a MBR partition type, partition number, starting sector, and an ending sector.
Create a new partition with the {{ic|n}} command. You must enter an MBR partition type, partition number, starting sector, and an ending sector.


{{Note|See [[Partitioning#Partition scheme]] for considerations concerning the size and location of partitions.}}
{{Note|See [[Partitioning#Partition scheme]] for considerations concerning the size and location of partitions.}}
Line 92: Line 98:
When using MBR, ''fdisk'' will ask for the MBR partition type. Specify it, 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 using MBR, ''fdisk'' will ask for the MBR partition type. Specify it, type {{ic|p}} to create a primary partition or {{ic|e}} to create an extended one. There may be up to four primary partitions.


''fdisk'' does not ask for the partition type ID and uses 'Linux filesystem' by default; you can change it later.
''fdisk'' does not ask for the partition type ID and uses 'Linux filesystem' by default; you can [[#Change partition type|change it later]].


==== Partition number ====
==== Partition number ====


A partition number is the number assigned to a partition, e.g. a partition with number {{ic|1}} on a disk {{ic|/dev/sda}} would be {{ic|/dev/sda1}}. Partition numbers may not always match the order of partitions on disk, in which case they can be [[#Sort partitions|sorted]].
A partition number is the number assigned to a partition, e.g. a partition with number {{ic|1}} on a disk {{ic|/dev/sda}} would be {{ic|/dev/sda1}}, {{ic|/dev/nvme0n1p1}} on {{ic|/dev/nvme0n1}} and {{ic|/dev/mmcblk0p1}} on {{ic|/dev/mmcblk0}}. See [[Device file#Partition]] for details on the naming scheme. Partition numbers may not always match the order of partitions on disk, in which case they can be [[#Sort partitions|sorted]].


It is advised to choose the default number suggested by ''fdisk''.
It is advised to choose the default number suggested by ''fdisk''.
Line 111: Line 117:
Pressing the {{ic|Enter}} key with no input specifies the default value, which is the start of the largest available block for the first sector and the end of the same block for the last sector.
Pressing the {{ic|Enter}} key with no input specifies the default value, which is the start of the largest available block for the first sector and the end of the same block for the last sector.


{{Tip|
{{Note|
* When partitioning it is always a good idea to follow the default value for a partition's first sector. Additionally, specify partition sizes with the {{ic|+''size{M,G,T,P}''}} notation and do not use sizes smaller than 1 MiB. Such partitions will always be aligned according to the device properties.
* When partitioning it is always a good idea to follow the default value for a partition's first sector. Additionally, make sure to specify partition sizes with the {{ic|+''size{M,G,T,P}''}} notation and do not use sizes smaller than 1 MiB. Such partitions will always be aligned according to the device properties.
* On a disk with a MBR partition table leave at least 33 512-byte sectors (16.5 KiB) of free unpartitioned space at the end of the disk to allow [[gdisk#Convert between MBR and GPT|converting between MBR and GPT]].
* [[EFI system partition]] requires the partition type {{ic|EFI System}}.
* [[EFI system partition]] requires type {{ic|EFI System}}.
* [[GRUB]] requires a [[BIOS boot partition]] with partition type {{ic|BIOS boot}} when installing GRUB to a GPT partitioned disk on a BIOS-based system.
* [[GRUB]] requires a [[BIOS boot partition]] with type {{ic|BIOS boot}} when installing GRUB to a GPT partitioned disk, on a BIOS-based system.
* 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.
}}
}}
{{Tip|On a disk with an MBR partition table leave at least 33 512-byte sectors (16.5 KiB) of free unpartitioned space at the end of the disk to allow [[gdisk#Convert between MBR and GPT|converting between MBR and GPT]].}}


Repeat this procedure until you have the partitions you desire.
Repeat this procedure until you have the partitions you desire.
Line 130: Line 135:
{{Tip|
{{Tip|
* Press {{ic|L}} to show fdisk's internal code list.
* Press {{ic|L}} to show fdisk's internal code list.
* When using GPT, it is advised to follow the [https://uapi-group.org/specifications/specs/discoverable_partitions_specification/ Discoverable Partitions Specification] since {{man|8|systemd-gpt-auto-generator}} will automount them.
* When using GPT, it is advised to follow the [https://uapi-group.org/specifications/specs/discoverable_partitions_specification/ Discoverable Partitions Specification] since [[systemd#GPT partition automounting|systemd-gpt-auto-generator]] can automount them. I.e. use {{ic|Linux root (x86-64)}} for the root partititon, {{ic|Linux swap}} for the swap partition, {{ic|Linux home}} for the home partition and so on.
}}
}}


=== Make a partition bootable ===
=== Make an MBR partition bootable ===


You can make the MBR partition bootable by typing {{ic|a}}.
You can make an MBR partition [[Wikipedia:Boot flag|bootable (aka active)]] by typing {{ic|a}}.


=== Write changes to disk ===
=== Review changes to the partition table ===


Write the table to disk and exit via the {{ic|w}} command.
* Print changes via the {{ic|p}} command.
* Abort changes via the {{ic|q}} command.
* Write changes to disk and exit via the {{ic|w}} command.


== Moving partitions ==
== Moving partitions ==


{{Warning|Partitions can only be moved while offline. Because moving a partition requires the whole partition to be rewritten on disk, it is a slow and potentially hazardous operation. Backups are strongly recommended! According to the ''sfdisk'' man page, "this operation is risky and not atomic."}}
{{Warning|Partitions can only be moved while offline. Because moving a partition requires the whole partition to be rewritten on disk, it is a slow and potentially hazardous operation. Backups are strongly recommended! According {{man|8|sfdisk|OPTIONS}}, "this operation is risky and not atomic."}}


In order to move a partition, you need to have free space available where the partition will be moved. If necessary, you can make room by shrinking your partitions and the filesystems on them. See [[Parted#Shrinking partitions]]. To relocate a partition:
In order to move a partition, you need to have free space available where the partition will be moved. If necessary, you can make room by shrinking your partitions and the filesystems on them. See [[Parted#Shrinking partitions]]. To relocate a partition:

Latest revision as of 07:09, 16 March 2024

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.

Note: fdisk supports GPT since util-linux 2.23. [1] Alternatively, gptfdisk may be used; see gdisk for more information.
Tip: For basic partitioning functionality with a curses-based user interface, cfdisk(8) can be used.

Installation

fdisk and its associated utilities are provided by the util-linux package, which is a dependency of the base meta package.

List partitions

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

# 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.

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
sector-size: 512

/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.

fdisk performs partition alignment automatically on a 2048 512-byte sector (1 MiB) block size base which should be compatible with all Advanced Format HDDs and the vast majority of SSDs if not all. This means that the default settings will give you proper alignment.

To use fdisk, run the program with the name of the block device you want to change/edit. This example uses /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.
Tip:

To create a new partition table and clear all current partition data type g at the prompt for a GUID Partition Table (GPT) or o for an MBR partition table. Skip this step if the table you require has already been created.

Create partitions

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

Note: See Partitioning#Partition scheme for considerations concerning the size and location of partitions.

Partition type

When using MBR, fdisk will ask for the MBR partition type. Specify it, type p to create a primary partition or e to create an extended one. There may be up to four primary partitions.

fdisk does not ask for the partition type ID and uses 'Linux filesystem' by default; you can change it later.

Partition number

A partition number is the number assigned to a partition, e.g. a partition with number 1 on a disk /dev/sda would be /dev/sda1, /dev/nvme0n1p1 on /dev/nvme0n1 and /dev/mmcblk0p1 on /dev/mmcblk0. See Device file#Partition for details on the naming scheme. Partition numbers may not always match the order of partitions on disk, in which case they can be sorted.

It is advised to choose the default number suggested by fdisk.

First and last sector

The first sector must be specified in absolute terms using sector numbers. The last sector can be specified using the absolute position in sector numbers or as positions measured in kibibytes (K), mebibytes (M), gibibytes (G), tebibytes (T), or pebibytes (P);

The position of the last sector can be specified in:

  • absolute terms from the start of the disk. E.g. 40M as a first sector specifies a position 40 MiB from the start of the disk.
  • relative terms by preceding the size with +size or -size. E.g. +2G to specify a point 2 GiB after the start sector, or -200M to specify a point 200 MiB 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 first sector and the end of the same block for the last sector.

Note:
  • When partitioning it is always a good idea to follow the default value for a partition's first sector. Additionally, make sure to specify partition sizes with the +size{M,G,T,P} notation and do not use sizes smaller than 1 MiB. Such partitions will always be aligned according to the device properties.
  • EFI system partition requires the partition type EFI System.
  • GRUB requires a BIOS boot partition with partition type BIOS boot when installing GRUB to a GPT partitioned disk on a BIOS-based system.
Tip: On a disk with an MBR partition table leave at least 33 512-byte sectors (16.5 KiB) of free unpartitioned space at the end of the disk to allow converting between MBR and GPT.

Repeat this procedure until you have the partitions you desire.

Change partition type

Each partition is associated with a type. MBR uses partition IDs; GPT uses Partition type GUIDs.

Press t to change the type of a partition. The default, Linux filesystem, should be fine for most use.

Tip:

Make an MBR partition bootable

You can make an MBR partition bootable (aka active) by typing a.

Review changes to the partition table

  • Print changes via the p command.
  • Abort changes via the q command.
  • Write changes to disk and exit via the w command.

Moving partitions

Warning: Partitions can only be moved while offline. Because moving a partition requires the whole partition to be rewritten on disk, it is a slow and potentially hazardous operation. Backups are strongly recommended! According sfdisk(8) § OPTIONS, "this operation is risky and not atomic."

In order to move a partition, you need to have free space available where the partition will be moved. If necessary, you can make room by shrinking your partitions and the filesystems on them. See Parted#Shrinking partitions. To relocate a partition:

# echo '+sectors,' | sfdisk --move-data device -N number

Where sectors is the number of sectors to move the partition (the + indicates moving it forward), device is the device that holds the partition, and number is the partition number. Note that if you add a new partition in the middle or at the beginning of your disk, you will likely want to renumber the partitions. See #Sort partitions or the "extra functionality" mode of fdisk.

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 (i.e. partprobe /dev/sda).

See also