Fdisk (简体中文)
util-linux fdisk 是基于命令行界面的分区表创建和编辑工具。一个硬盘需要分为一个或多个分区,这个信息在分区表里面记录。
本文介绍 fdisk(8) 和 sfdisk(8) 工具的使用。
安装
要使用 fdisk 及相关工具,请使用 util-linux 软件包,这个软件包已经位于 base 软件包组。
显示分区
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
/proc/partitions
.备份和恢复分区
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
See Dd#Backup and restore MBR partition table[断开的链接:无效的部分].
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
创建分区表和分区
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
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
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.
The first sector must be specified in absolute terms using sector numbers. The last sector can be specified using the absolute position in sectors or using the +
symbol to specify a position relative to the start sector measured in sectors, kibibytes (K
), mebibytes (M
), gibibytes (G
), tebibytes (T
), or pebibytes (P
); for instance, setting +2G
as the last sector will specify a point 2GiB after the start 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
.
- 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.
- On a MBR partitioned disk leave at least 16.5 KiB free space at the end of the disk to simplify converting between MBR and GPT if the need ever arises.
- 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.
/dev/sda1
) to be usable. Reboot the system or tell the kernel to reread the partition table.