fdisk

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. Alternatively, gptfdisk may be used; see gdisk for more information.

Installation

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

List partitions

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

# 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/ option. Run the following command for device :

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

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

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 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 :

# fdisk /dev/sda

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

Create new table

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

Create partitions

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

Partition type

When using MBR, fdisk will ask for the MBR partition type. Specify it, type to create a primary partition or 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 would be . 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 (), gibibytes (), tebibytes (), or pebibytes ();

The position of the last sector can be specified in:

  • absolute terms from the start of the disk. E.g. as a first sector specifies a position 40 MiB from the start of the disk.
  • relative terms by preceding the size with or . E.g. 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 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.

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 to change the type of a partition. The default, Linux filesystem, should be fine for most use.

Make a partition bootable

You can make the MBR partition bootable by typing .

Write changes to disk

Write the table to disk and exit via the command.

Moving partitions

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 is the number of sectors to move the partition (the + indicates moving it forward), is the device that holds the partition, and 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. 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 and/or the configuration files.

gollark: Yet you can get Discord?
gollark: Your laptop can't access YouTube? What?
gollark: Obviously you should listen to osmarks internet radioâ„¢ instead.
gollark: The worst they can practically do is be mildly irritating toward you. Although social anxiety things are probably not hugely rational so pointing that out may not change your ability to do so. Oh well.
gollark: I see.

See also

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.