Difference between revisions of "Fdisk (简体中文)"

From ArchWiki
Jump to: navigation, search
(Move Gdisk info to Gdisk (简体中文).)
(Sync to latest English page.)
Line 10: Line 10:
 
{{Related|Partitioning (简体中文)}}
 
{{Related|Partitioning (简体中文)}}
 
{{Related articles end}}
 
{{Related articles end}}
{{TranslationStatus (简体中文)|Fdisk|2015-08-18|391664}}
+
{{TranslationStatus (简体中文)|Fdisk|2018-08-31|537498}}
  
来自 util-linux fdisk (用于 MBR 硬盘)等价。。
+
[https://git.kernel.org/cgit/utils/util-linux/util-linux.git/ util-linux fdisk] 是基于命令行界面的分区表创建和编辑工具。一个硬盘需要分为一个或多个分区,这个信息在分区表里面记录。
  
来自 util-linux (基于 util-linux 内建的 libfdisk) 的 fdisk 工具部分支持 GPT, 但仍在测试阶段 (自从2013年10月7日). 相关的 cfdisk 和 sfdisk 仍不支持 GPT, 并在 GPT 硬盘上使用的话有可能损坏 GPT 头和分区表。
+
本文介绍 {{man|8|fdisk}} {{man|8|sfdisk}} 工具的使用。
  
== 使用 MBR - 传统方式 ==
+
{{Tip|{{man|8|cfdisk}} 工具提供了基本的功能和文本界面。}}
  
{{警告|如果打算以 BIOS 模式双启动 Windows (这是32位版 Windows 和64位版 Windows XP 的唯一选择),'''不要''' 使用 GPT,因为 Windows '''不支持'''在使用 BIOS 的机器上从 GPT 分区的磁盘上启动。你需要像下面所说的那样,使用 MBR 分区,并且从 BIOS 模式启动。对于现代在 UEFI 模式下的64位 Windows 版本不存在这个限制。}}
+
== 安装 ==
  
使用 MBR 的话,可以使用''fdisk'' 工具编辑分区表。新版本的 ''fdisk'' 已经废弃使用柱面作为默认显示单位的方式,同时默认也放弃了对MS-DOS的兼容性。最新版本的 ''fdisk'' 会自动将所有分区对齐到2048扇区,或1024 KiB,以兼容所有已知的 SSD 制造商的 EBS 大小。即使用默认设置即可获得合适的分区对齐。
+
要使用 ''fdisk'' 及相关工具,请使用 {{pkg|util-linux}} 软件包,这个软件包已经位于 {{grp|base}} 软件包组。
  
注意,在以前 ''fdisk '' 使用柱面作为默认显示单位,并且保留了 MS-DOS 兼容性,这使其默认无法进行 SSD 对齐。因此人们需要从网上查找各种资料以保证分区对齐。使用最新版本的 ''fdisk'' 简单得多,正如本文档所述那样。
+
== 显示分区 ==
  
=== Fdisk 用法 ===
+
To list partition tables and partitions on a device, you can run the following, where device is a name like {{ic|/dev/sda}}:
  
* 以 root 身份启动 ''fdisk'' (''disk-device'' 形如 {{ic|/dev/sda}}):
+
# fdisk -l /dev/sda
# fdisk ''disk-device''
 
* 如果是全新的磁盘或你想重新分区,使用 {{ic|o}} 命令建立一个新的空 DOS 分区表。
 
* 使用{{ic|n}} 命令创建一个新的分区(主分区/第一分区)。
 
* 使用 {{ic|+''x''G}} 的格式指定分区大小为 ''x'' GB。例如,如果你需要创建一个 15 GiB 的分区,你需要输入 {{ic|+15G}}。
 
* 使用{{ic|t}} 命令将分区的 ID 从默认值修改为 Linux({{ic|type 83}})。这是一个可选步骤。如果用户想创建其他类型的分区,如swap,NTFS,LVM 等也可以。注意,完整的可用分区类型列表可以通过 {{ic|l}} 命令获取。
 
* 其他分区的处理方式类似。
 
* 使用 {{ic|w}} 命令将分区表写入磁盘并退出。
 
* 将新分区格式化为[[File systems (简体中文)|文件系统]]。
 
  
===== 用 fdisk 建立 MBR 分区 =====
+
{{Note|If the device is not specified, ''fdisk'' will list all partitions in {{ic|/proc/partitions}}.}}
  
{{注意|安装媒介中亦有一个叫 {{ic|cfdisk}} 的工具,它的 UI 与 {{ic|cgdisk}} 相似;但目前仍不能正确地自动对齐好第一个分区。所以我们在这里改用经典的 fdisk 工具。}}
+
== 备份和恢复分区 ==
  
启动 ''fdisk'' :
+
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 ===
 +
 
 +
{{Move|Disk cloning#Using dd|''dd'' is not part of {{Pkg|util-linux}} and does not have anything to do with ''fdisk''.}}
 +
 
 +
The MBR is stored in the the first 512 bytes of the disk. It consists of 4 parts:
 +
 
 +
# The first 440 bytes contain the bootstrap code (boot loader).
 +
# The next 6 bytes contain the disk signature.
 +
# The next 64 bytes contain the partition table (4 entries of 16 bytes each, one entry for each primary partition).
 +
# The last 2 bytes contain a boot signature.
 +
 
 +
To save the MBR as {{ic|mbr_file.img}}:
 +
 
 +
# dd if=/dev/sd''X'' 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/sd''X'' 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 [http://www.pixelbeat.org/docs/disk/ 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/sd''X'' bs=440 count=1
 +
 
 +
To restore only the partition table, one must use:
 +
 
 +
# dd if=''/path/to/mbr_file.img'' of=/dev/sd''X'' 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/sd''X'' 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 {{ic|-d}}/{{ic|--dump}} option. Run the following command for device {{ic|/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:
 +
 
 +
{{hc|sda.dump|2=
 +
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 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'' 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}}:
  
 
  # fdisk /dev/sda
 
  # fdisk /dev/sda
  
创建分区表:
+
This opens the ''fdisk'' dialogue where you can type in commands.
 +
 
 +
=== Create new table ===
  
* {{ic|Command (m for help):}} 输入 {{ic|o}} 并按下 {{ic|Enter}}
+
{{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.
  
# {{ic|Command (m for help):}} 输入 {{ic|n}} 并按下 {{ic|Enter}}
+
=== Create partitions ===
# Partition type: {{ic|Select (default p):}} 按下 {{ic|Enter}}
 
# {{ic|Partition number (1-4, default 1):}} 按下 {{ic|Enter}}
 
# {{ic|First sector (2048-209715199, default 2048):}} 按下 {{ic|Enter}}
 
# {{ic|Last sector, +sectors or +size{K,M,G} (2048-209715199....., default 209715199):}} 输入 {{ic|+15G}} 并按下 {{ic|Enter}}
 
  
然后建立第二个分区:
+
{{Expansion|Place each step into a subsection.}}
  
# {{ic|Command (m for help):}} 输入 {{ic|n}} 并按下 {{ic|Enter}}
+
Create a new partition with the {{ic|n}} command. You enter a partition type, partition number, starting sector, and an ending sector.
# Partition type: {{ic|Select (default p):}} 按下 {{ic|Enter}}
 
# {{ic|Partition number (1-4, default 2):}} 按下 {{ic|Enter}}
 
# {{ic|First sector (31459328-209715199, default 31459328):}} 按下 {{ic|Enter}}
 
# {{ic|Last sector, +sectors or +size{K,M,G} (31459328-209715199....., default 209715199):}} 按下 {{ic|Enter}}
 
  
 +
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.
  
现在预览下新的分区表:
+
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 {{ic|+}} symbol to specify a position relative to the start sector measured in sectors, kibibytes ({{ic|K}}), mebibytes ({{ic|M}}), gibibytes ({{ic|G}}), tebibytes ({{ic|T}}), or pebibytes ({{ic|P}}); for instance, setting {{ic|+2G}} as the last sector will specify a point 2GiB after the start 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.
  
* {{ic|Command (m for help):}} 输入 {{ic|p}} 并按下 {{ic|Enter}}
+
{{Expansion|Suggest using [https://www.freedesktop.org/wiki/Specifications/DiscoverablePartitionsSpec/ Discoverable Partitions].}}
  
{{bc|
+
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}}.
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
 
Units &#61; sectors of 1 * 512 &#61; 512 bytes
 
Sector size (logical/physical): 512 bytes / 512 bytes
 
I/O size (minimum/optimal): 512 bytes / 512 bytes
 
Disk identifier: 0x5698d902
 
  
  Device Boot    Start        End    Blocks  Id  System
+
{{Tip|
/dev/sda1          2048    31459327  15728640  83  Linux
+
* 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.
/dev/sda2      31459328  209715199  89127936  83  Linux
+
* On a MBR partitioned disk leave at least 16.5 KiB free space at the end of the disk to simplify [[gdisk#Convert between MBR and GPT|converting between MBR and GPT]] if the need ever arises.
 +
* [[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.
 +
* 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.
 
}}
 
}}
  
然后向磁盘写入这些改动:
+
Repeat this procedure until you have the partitions you desire.
 +
 
 +
=== Write changes to disk ===
 +
 
 +
Write the table to disk and exit via the {{ic|w}} command.
  
* {{ic|Command (m for help):}} 输入 {{ic|w}} 并按下 {{ic|Enter}}
+
== Tips and tricks ==
 +
=== Sort partitions ===
  
如果一切顺利无错误的话,fdisk 程序将显示如下信息:
+
This applies for when a new partition is created in the space between two partitions or a partition is deleted. {{ic|/dev/sda}} is used in this example.
  
{{bc|
+
# sfdisk -r /dev/sda
The partition table has been altered!
 
  
Calling ioctl() to re-read partition table.
+
After sorting the partitions if you are not using [[Persistent block device naming]], it might be required to adjust the {{ic|/etc/fstab}} and/or the {{ic|/etc/crypttab}} configuration files.
Syncing disks.
 
}}
 
  
若因 ''fdisk'' 遇到错误导致以上操作无法完成,可以用 {{ic|q}} 命令来退出。
+
{{Note|The kernel must read the new partition table for the partitions (e.g. {{ic|/dev/sda1}}) to be usable. Reboot the system or tell the kernel to [https://serverfault.com/questions/36038/reread-partition-table-without-rebooting reread the partition table].}}
  
 
== See also ==
 
== See also ==
 
*[http://www.novell.com/coolsolutions/feature/19350.html Manually Partitioning Your Hard Drive with fdisk]
 
*[http://www.novell.com/coolsolutions/feature/19350.html Manually Partitioning Your Hard Drive with fdisk]

Revision as of 08:39, 31 August 2018

翻译状态: 本文是英文页面 Fdisk翻译,最后翻译时间:2018-08-31,点击这里可以查看翻译后英文页面的改动。

util-linux fdisk 是基于命令行界面的分区表创建和编辑工具。一个硬盘需要分为一个或多个分区,这个信息在分区表里面记录。

本文介绍 fdisk(8)sfdisk(8) 工具的使用。

Tip: cfdisk(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
Note: If the device is not specified, fdisk will list all partitions in /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

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

创建分区表和分区

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.

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.

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

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