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

From ArchWiki
Jump to: navigation, search
(Update Installation.)
(Update translation.)
Line 10: Line 10:
 
{{Related|Partitioning}}
 
{{Related|Partitioning}}
 
{{Related articles end}}
 
{{Related articles end}}
{{TranslationStatus (简体中文)|Parted|2015-03-01|363208}}
+
{{TranslationStatus (简体中文)|Parted|2018-12-05|558274}}
  
 
[https://www.gnu.org/software/parted/parted.html GNU Parted] 是创建和处理分区表的程序。GParted 是 GUI 前端。
 
[https://www.gnu.org/software/parted/parted.html GNU Parted] 是创建和处理分区表的程序。GParted 是 GUI 前端。
  
== 安装到 Arch ==
+
== 安装 ==
  
 
从[[官方源]][[pacman|安装]] 软件包 {{Pkg|parted}}, 要使用图像界面,安装 {{Pkg|gparted}}。
 
从[[官方源]][[pacman|安装]] 软件包 {{Pkg|parted}}, 要使用图像界面,安装 {{Pkg|gparted}}。
  
== 小技巧 ==
+
== 使用 ==
 +
 
 +
Parted 有两种模式:命令行和交互,请用下面命令启动:
 +
 
 +
# parted device
 +
 
 +
{{ic|''device''}} 是要编辑的硬盘设备(例如 {{ic|/dev/sda}})。如果忽略了 {{ic|''device''}} 参数,''parted'' 将尝试猜测要使用的设备。
  
=== 把 GParted-live 添加至您的 GRUB 菜单 ===
+
=== 命令行模式 ===
  
将 GParted-live 添加至您的 GRUB 菜单的步骤,请参阅 [[Gparted-Live]] wiki 文章。好处是您可以直接从 GRUB 启动进 GParted-live CD 的 live 环境,不需要另外准备一张 CD!
+
在命令行模式下,可以同时执行一个或多个命令:
  
=== 双启动 Windows XP ===
+
# parted /dev/sda mklabel gpt mkpart P1 ext3 1MiB 8MiB
  
如果您打算将一个同属于启动分区的 Windows XP 分区移动到另一块硬盘,只要将以下的注册表删除,之后就可以用 GParted 轻易地操作,Windows 不会出现任何问题:
+
{{注意|{{ic|--help}} 等参数只有在命令行中才能指定。}}
  
HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices
+
=== 交互模式 ===
  
相关资料参见[http://gparted-forum.surf4.info/viewtopic.php?pid=8347#p8347 这里]。
+
交互模式简化了分区过程,可以自动对设备执行分区操作。打开需要新建分区表的设备:
  
=== 修复混乱的分区顺序 ===
+
# parted /dev/sd''x''
  
如果您的硬盘上有逻辑分区,刪除其中一个可能会导致分区顺序混乱。例如以下的范例:
+
命令提示符会从 ({{ic|#}}) 变成 {{ic|(parted)}}: 要查看可用的命令:
/dev/sda1 (主分区)
 
/dev/sda2 (主分区)
 
/dev/sda3 (主分区)
 
/dev/sda4 ('''扩展'''分区)
 
/dev/sda5 (逻辑分区)
 
/dev/sda6 (逻辑分区)
 
/dev/sda7 (逻辑分区)
 
  
1-3 是主分区。5-6 是隶属于扩展分区 (4) 的逻辑分区。假设您砍掉 {{ic|/dev/sda5}},并将 {{ic|/dev/sda2}} 复制一份到释出的空间上,那么现在您的硬盘看起来像下面这样:
+
  (parted) help
/dev/sda1 (主分区)
 
/dev/sda2 (主分区)
 
/dev/sda3 (主分区)
 
/dev/sda4 ('''扩展'''分区)
 
/dev/sda7 (逻辑分区)
 
/dev/sda5 (逻辑分区)
 
  /dev/sda6 (逻辑分区)
 
  
注意到在删除、复制/粘贴分区之后,分区顺序混乱了。这可能会导致各种问题:无法顺利挂载分区、出现 GRUB Error 17 "no bootable system"等等。解決这个小问题的方法很简单:
+
完成操作后,用下面命令退出:
# 用您的 Arch Live CD、GParted Live CD (或任何其他 live Linux CD) 启动系统
 
# 运行 fdisk, 选择硬盘,进入高级模式,修复分区顺序,并将变更写入硬盘
 
  
例如使用 {{ic|/dev/sda}}:
+
  (parted) quit
  # fdisk /dev/sda
 
# 进入 fdisk 之后,选择 {{ic|x}} 选项 (额外功能 (仅限进阶用户)) 并按 enter
 
# 接着选择 {{ic|f}} 选项 (修复分区顺序) 并按 enter
 
# 接着选择 {{ic|w}} 选项 (写入分区表之后退出) 并按 enter
 
  
{{注意|您必须以 root 身分执行 '''partprobe''',或是重启系统,这样内核才能读取新的分区表!}}
+
退出后命令提示符变成 {{ic|#}}.
  
=== 从菜单启动 GParted ===
+
如果命令没带参数,Parted 会进行询问:
  
如果您从菜单 (例如 xfce 的应用程序菜单) 载入 GParted 时发生任何问题,安装 {{Pkg|polkit}} 软件包,并设定为和当前会话同时启动。
+
(parted) mklabel
 +
New disk label type? gpt
  
=== 启用 GParted 在线调整大小和设备对应器磁盘阵列(dmraid)功能 ===
+
== 数值设定 ==
  
你需要以以下参数重新编译软件包:
+
很多分区系统有复杂的限制,Parted 可能会对输入的数值进行稍微的修改。例如设定了 10.4Mb,实际会使用 10.352Mb。如果修正后的数值差异太大,Parted 会进行提示确认。用扇区数值("s" 后缀)可以进行精确的数值设置。
  
./configure --enable-online-resize --enable-libparted-dmraid
+
parted 2.4 开始,当使用 “MiB”, “GiB”, “TiB” 等 IEC 单位时,parted 会使用精确数值,不进行修正。而使用 “4GB” 这样的设置时,可能会落在前后 500MB 的未知。所以在创建分区时,应该指定比特(“B”)、扇区(“s”)或 IEC 二进制单位 “MiB”,不要使用 “MB”, “GB”。
  
 
== Partitioning ==
 
== Partitioning ==
Line 80: Line 67:
 
如果设备没有分区,或者要改变分区表类型,重建分区结构,需要新建分区表。  
 
如果设备没有分区,或者要改变分区表类型,重建分区结构,需要新建分区表。  
  
打开需要新建分区表的设备:
+
用下面命令打开分区:
  
 
  # parted /dev/sd''x''
 
  # parted /dev/sd''x''
Line 179: Line 166:
 
  (parted) mkpart primary linux-swap 20G 24G
 
  (parted) mkpart primary linux-swap 20G 24G
 
  (parted) mkpart primary ext4 24G 100%
 
  (parted) mkpart primary ext4 24G 100%
 +
 +
=== Resizing partitions ===
 +
 +
{{Warning|Partitions that are being resized must be unmounted and not in use. If it cannot be done (e.g. the partition that mounts to {{ic|/}}), use a live media/rescue system.}}
 +
{{Note|
 +
* You can only move the end of the partition with {{ic|parted}}.
 +
* As of parted v4.2 ''resizepart'' may need the use of [[#Interactive mode]].[https://bugs.launchpad.net/ubuntu/+source/parted/+bug/1270203]
 +
* These instructions apply to partitions that have ext2, ext3 or ext4 filesystems.
 +
}}
 +
 +
If you are growing a partition, you have to first resize the partition and then resize the filesystem on it, while for shrinking the filesystem must be resized before the partition to avoid data loss.
 +
 +
==== Growing partitions ====
 +
 +
To grow a partition (in parted interactive mode):
 +
 +
(parted) resizepart ''number'' ''end''
 +
 +
Where {{ic|''number''}} is the number of the partition you are growing, and {{ic|''end''}} is the new end of the partition (which needs to be larger than the old end).
 +
 +
Then, to grow the filesystem on the partition:
 +
 +
# resize2fs /dev/''sdaX'' ''size''
 +
 +
Where {{ic|''sdaX''}} stands for the partition you are growing, and {{ic|''size''}} is the new size of the partition.
 +
 +
==== Shrinking partitions ====
 +
 +
To shrink the filesystem on the partition:
 +
 +
# resize2fs /dev/''sdaX'' ''size''
 +
 +
Where {{ic|''sdaX''}} stands for the partition you are shrinking, and {{ic|''size''}} is the new size of the partition.
 +
 +
Then shrink the partition (in parted interactive mode):
 +
 +
(parted) resizepart ''number'' ''end''
 +
 +
Where {{ic|''number''}} is the number of the partition you are shrinking, and {{ic|''end''}} is the new end of the partition (which needs to be smaller than the old end).
 +
 +
When done, use the ''resizepart'' command from {{Pkg|util-linux}} to tell the kernel about the new size:
 +
 +
# resizepart ''device'' ''number'' ''size''
 +
 +
Where {{ic|''device''}} is the device that holds the partition, {{ic|''number''}} is the number of the partition and {{ic|''size''}} is the new size of the partition.
 +
 +
== Warnings ==
 +
 +
Parted will always warn you before doing something that is potentially dangerous, unless the command is one of those that is inherently dangerous (viz., rm, mklabel and mkpart).
 +
 +
=== Alignment ===
 +
 +
When creating a partition, ''parted'' might warn about improper partition alignment but does not hint about proper alignment. For example:
 +
 +
(parted) mkpart primary fat16 0 32M
 +
Warning: The resulting partition is not properly aligned for best performance.
 +
Ignore/Cancel?                                                   
 +
 +
The warning means the partition start is not aligned. Enter "Ignore" to go ahead anyway, print the partition table in sectors to see where it starts, and remove/recreate the partition with the start sector rounded up to increasing powers of 2 until the warning stops. As one example, on a flash drive with 512B sectors, Parted wanted partitions to start on sectors that were a multiple of 2048, which is 1 MiB alignment.
 +
 +
If you want ''parted'' to attempt to calculate the correct alignment for you, specify the start position as 0% instead of some concrete value. To make one large ext4 partition, your command would look like this:
 +
 +
(parted) mkpart primary ext4 0% 100%
 +
 +
== 小技巧 ==
 +
 +
=== 双启动 Windows XP ===
 +
 +
如果您打算将一个同属于启动分区的 Windows XP 分区移动到另一块硬盘,只要将以下的注册表删除,之后就可以用 GParted 轻易地操作,Windows 不会出现任何问题:
 +
 +
HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices
 +
 +
相关资料参见[http://gparted-forum.surf4.info/viewtopic.php?pid=8347#p8347 这里]。
 +
 +
=== Check alignment ===
 +
 +
On an already partitioned disk, you can use ''parted'' to verify the alignment of a partition on a device. For instance, to verify alignment of partition 1 on {{ic|/dev/sda}}:
 +
 +
# parted /dev/sda
 +
(parted) align-check optimal 1
 +
1 aligned

Revision as of 07:57, 5 December 2018

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

GNU Parted 是创建和处理分区表的程序。GParted 是 GUI 前端。

安装

官方源安装 软件包 parted, 要使用图像界面,安装 gparted

使用

Parted 有两种模式:命令行和交互,请用下面命令启动:

# parted device

device 是要编辑的硬盘设备(例如 /dev/sda)。如果忽略了 device 参数,parted 将尝试猜测要使用的设备。

命令行模式

在命令行模式下,可以同时执行一个或多个命令:

# parted /dev/sda mklabel gpt mkpart P1 ext3 1MiB 8MiB 
注意: --help 等参数只有在命令行中才能指定。

交互模式

交互模式简化了分区过程,可以自动对设备执行分区操作。打开需要新建分区表的设备:

# parted /dev/sdx

命令提示符会从 (#) 变成 (parted): 要查看可用的命令:

(parted) help

完成操作后,用下面命令退出:

(parted) quit

退出后命令提示符变成 #.

如果命令没带参数,Parted 会进行询问:

(parted) mklabel
New disk label type? gpt

数值设定

很多分区系统有复杂的限制,Parted 可能会对输入的数值进行稍微的修改。例如设定了 10.4Mb,实际会使用 10.352Mb。如果修正后的数值差异太大,Parted 会进行提示确认。用扇区数值("s" 后缀)可以进行精确的数值设置。

parted 2.4 开始,当使用 “MiB”, “GiB”, “TiB” 等 IEC 单位时,parted 会使用精确数值,不进行修正。而使用 “4GB” 这样的设置时,可能会落在前后 500MB 的未知。所以在创建分区时,应该指定比特(“B”)、扇区(“s”)或 IEC 二进制单位 “MiB”,不要使用 “MB”, “GB”。

Partitioning

创建新分区表

如果设备没有分区,或者要改变分区表类型,重建分区结构,需要新建分区表。

用下面命令打开分区:

# parted /dev/sdx

为 BIOS 系统创建 MBR/msdos 分区表:

(parted) mklabel msdos

为 UEFI 系统创建 GPT 分区表:

(parted) mklabel gpt

分区方案

您可以决定磁盘应该分为多少个区,每个分区又挂载在系统的哪个目录。将分区如何映射至目录(一般称此为挂载点),取决于您的分区方案。需要满足:

  • 至少需要创建一个 / (root) 目录,有些分区类型和 启动加载器组合有额外的分区要求:
  • BIOS/GPT + GRUB: 需要按照 BIOS 启动分区设置 的方式创建一个 1M 或 2M 的 EF02 类型分区.
  • UEFI 的主板,需要一个 EFI 系统分区[broken link: invalid section].
  • 如果您需要加密磁盘,则必须加以调整分区方案。系统安装后,也可以再配置加密文件夹,容器或 home 目录。

系统需要需要 /boot/home 等目录, Arch 文件系统架构 有各目录的详细介绍。如果没有创建单独的/boot/home 分区,这些目录直接放到了根分区下面。后面会介绍如何创建 交换分区

In the examples below it is assumed that a new and contiguous partitioning scheme is applied to a single device. Some optional partitions will also be created for the /boot and /home directories: see also Arch filesystem hierarchy for an explanation of the purpose of the various directories; if separate partitions for directories like /boot or /home are not created, these will simply be contained in the / partition. Also the creation of an optional partiton for swap space will be illustrated.

用下面命令打开 parted 交互模式:

# parted /dev/sdx

用下面命令创建分区:

(parted) mkpart part-type fs-type start end
  • part-type 是分区类型,可以选择 primary, extendedlogical,仅用于 MBR 分区表.
  • fs-type 是文件系统类型,支持的类型列表可以通过 help mkpart 查看。 mkpart 并不会实际创建文件系统, fs-type 参数仅是让 parted 设置一个 1-byte 编码,让启动管理器可以提前知道分区中有什么格式的数据。参阅 Wikipedia:Disk partitioning#PC partition types.
Tip: Most Linux native file systems map to the same partition code (0x83), so it is perfectly safe to e.g. use ext2 for an ext4-formatted partition.
  • start 是分区的起始位置,可以带单位, 例如 1M 指 1MiB.
  • end 是设备的结束位置(不是start 值的差),同样可以带单位,也可以用百分比,例如 100% 表示到设备的末尾。
  • 为了不留空隙,分区的开始和结束应该首尾相连。
Warning: It is important that the partitions do not overlap each other: if you do not want to leave unused space in the device, make sure that each partition starts where the previous one ends.

如果看到下面警告:

Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel?

表示分区没 对齐,请按照 分区对齐 进行修正。

下面命令设置 /boot 为启动目录:

(parted) set partition boot on
  • partition 是分区的编号,从 print 命令获取。

UEFI/GPT 示例

首先需要一个 EFI 系统分区[broken link: invalid section].如果是和 Windows 双系统启动,此分区已经存在,不要重新创建。

用下面命令创建分区 (建议大小是 512MiB)。

(parted) mkpart ESP fat32 1M 513M
(parted) set 1 boot on

剩下的空间可以按需要创建,root 占用全部 100% 剩余空间:

(parted) mkpart primary ext4 513M 100%

/ (20GiB),剩下的给 /home

(parted) mkpart primary ext4 513M 20.5G
(parted) mkpart primary ext4 20.5G 100%

创建 / (20GiB), swap (4Gib), 剩下给 /home

(parted) mkpart primary ext4 513M 20.5G
(parted) mkpart primary linux-swap 20.5G 24.5G
(parted) mkpart primary ext4 24.5G 100%

BIOS/MBR 示例

单根目录分区:

(parted) mkpart primary ext4 1M 100%
(parted) set 1 boot on

20Gib / 分区,剩下的给 /home

(parted) mkpart primary ext4 1M 20G
(parted) set 1 boot on
(parted) mkpart primary ext4 20G 100%

/boot (100MiB), / (20Gib), swap (4GiB) 剩下的给 /home:

(parted) mkpart primary ext4 1M 100M
(parted) set 1 boot on
(parted) mkpart primary ext4 100M 20G
(parted) mkpart primary linux-swap 20G 24G
(parted) mkpart primary ext4 24G 100%

Resizing partitions

Warning: Partitions that are being resized must be unmounted and not in use. If it cannot be done (e.g. the partition that mounts to /), use a live media/rescue system.
Note:
  • You can only move the end of the partition with parted.
  • As of parted v4.2 resizepart may need the use of #Interactive mode.[1]
  • These instructions apply to partitions that have ext2, ext3 or ext4 filesystems.

If you are growing a partition, you have to first resize the partition and then resize the filesystem on it, while for shrinking the filesystem must be resized before the partition to avoid data loss.

Growing partitions

To grow a partition (in parted interactive mode):

(parted) resizepart number end

Where number is the number of the partition you are growing, and end is the new end of the partition (which needs to be larger than the old end).

Then, to grow the filesystem on the partition:

# resize2fs /dev/sdaX size

Where sdaX stands for the partition you are growing, and size is the new size of the partition.

Shrinking partitions

To shrink the filesystem on the partition:

# resize2fs /dev/sdaX size

Where sdaX stands for the partition you are shrinking, and size is the new size of the partition.

Then shrink the partition (in parted interactive mode):

(parted) resizepart number end

Where number is the number of the partition you are shrinking, and end is the new end of the partition (which needs to be smaller than the old end).

When done, use the resizepart command from util-linux to tell the kernel about the new size:

# resizepart device number size

Where device is the device that holds the partition, number is the number of the partition and size is the new size of the partition.

Warnings

Parted will always warn you before doing something that is potentially dangerous, unless the command is one of those that is inherently dangerous (viz., rm, mklabel and mkpart).

Alignment

When creating a partition, parted might warn about improper partition alignment but does not hint about proper alignment. For example:

(parted) mkpart primary fat16 0 32M
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel?                                                     

The warning means the partition start is not aligned. Enter "Ignore" to go ahead anyway, print the partition table in sectors to see where it starts, and remove/recreate the partition with the start sector rounded up to increasing powers of 2 until the warning stops. As one example, on a flash drive with 512B sectors, Parted wanted partitions to start on sectors that were a multiple of 2048, which is 1 MiB alignment.

If you want parted to attempt to calculate the correct alignment for you, specify the start position as 0% instead of some concrete value. To make one large ext4 partition, your command would look like this:

(parted) mkpart primary ext4 0% 100%

小技巧

双启动 Windows XP

如果您打算将一个同属于启动分区的 Windows XP 分区移动到另一块硬盘,只要将以下的注册表删除,之后就可以用 GParted 轻易地操作,Windows 不会出现任何问题:

HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices

相关资料参见这里

Check alignment

On an already partitioned disk, you can use parted to verify the alignment of a partition on a device. For instance, to verify alignment of partition 1 on /dev/sda:

# parted /dev/sda
(parted) align-check optimal 1
1 aligned