Fdisk

From ArchWiki
Revision as of 01:48, 22 August 2016 by Taurolyon (talk | contribs) (→‎Convert between MBR and GPT: Linked supporting package)
Jump to navigation Jump to search

zh-CN:Fdisk

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

GPT fdisk, as implemented in the gdisk program and its associated utilities, 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." [1]

This article covers fdisk(8) its related sfdisk(8) and cfdisk(8) utilities, as well as the analogous gdisk(8), sgdisk and cgdisk utilities.

Installation

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

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.

Or for the gdisk:

# gdisk -l /dev/sda

Backup and restore

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. See also Master Boot Record#Backup and restoration.

Using sfdisk

For both GPT and MBR you can use sfdisk to save the partition layout of your device to a file with the --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 1GB 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

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:

# sgdisk -b=sgdisk-sda.bak

You can later restore the backup by running:

# sgdisk -l=sgdisk-sda.bak

If you want to clone your current device's partition layout (/dev/sda in this case) to another drive (/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

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

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. The latest fdisk automatically aligns all partitions to 2048 sectors, or 1024 KiB, 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.

Note that in the olden days, fdisk used cylinders as the default display unit, and retained an MS-DOS compatibility quirk that messed with SSD alignment. Therefore one will find many guides around the internet from around 2008-2009 making a big deal out of getting everything correct. With the latest fdisk, things are much simpler, as reflected in this guide.

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.

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 cgdisk for its usage.

gdisk can perform partition alignment automatically on a 2048 sector (or 1024KiB) block size base which should be compatible with the vast majority of SSDs if not all. GNU Parted also supports GPT, but is 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 /dev/sda:

# gdisk /dev/sda

Create new table

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.

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.

Create partitions

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

For fdisk, 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 (fdisk) or L (gdisk) to show the codes list.

Tip:
  • EFI System Partitions require code EF00 with gdisk and EFI System with fdisk.
  • GRUB requires a BIOS boot partition with code EF02 with gdisk and BIOS boot with fdisk when installing GRUB to a disk.
  • It is recommended to use 8200 with gdisk and Linux swap with fdisk for any swap partitions, since systemd will automount it.

See the above 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.

Convert between MBR and GPT

To convert an MBR partition table to GPT, you need the tool sgdisk (part of gptfdisk).

# sgdisk -g /dev/sda

To convert GPT to MBR use the m option. Note that it is not possible to convert more than four partitions from GPT to MBR.

# sgdisk -m /dev/sda

If the device will be bootable you will need to set the bootable flag with fdisk.

See also GUID Partition Table#Convert from MBR to GPT.