Parted
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 系统分区.
- 如果您需要加密磁盘,则必须加以调整分区方案。系统安装后,也可以再配置加密文件夹,容器或 home 目录。
系统需要需要 /boot
、/home
等目录, Arch 文件系统架构 有各目录的详细介绍。如果没有创建单独的/boot
或 /home
分区,这些目录直接放到了根分区下面。后面会介绍如何创建 交换分区。
在下面的示例中,假定将新的连续分区方案应用于单个设备。 还将为 /boot
和 /home
目录创建一些可选分区:另请参阅 Arch filesystem hierarchy,那里将解释各个目录的用途。 如果没有为 /boot
或 /home
等目录创建单独的分区,则这些分区将仅包含在 /
分区中。 此外,还将说明为 swap space 创建可选分区的过程。
用下面命令打开 parted 交互模式:
# parted /dev/sdx
用下面命令创建分区:
(parted) mkpart part-type fs-type start end
part-type
是分区类型,可以选择primary
,extended
或logical
,仅用于 MBR 分区表.fs-type
是文件系统类型,支持的类型列表可以通过help mkpart
查看。 mkpart 并不会实际创建文件系统,fs-type
参数仅是让 parted 设置一个 1-byte 编码,让启动管理器可以提前知道分区中有什么格式的数据。参阅 Wikipedia:Disk partitioning#PC partition types.
start
是分区的起始位置,可以带单位, 例如1M
指 1MiB.end
是设备的结束位置(不是 与start
值的差),同样可以带单位,也可以用百分比,例如100%
表示到设备的末尾。- 为了不留空隙,分区的开始和结束应该首尾相连。
如果看到下面警告:
Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel?
表示分区没对齐,请参照分区对齐进行修正。
下面命令设置 /boot
为启动目录:
(parted) set partition boot on
partition
是分区的编号,从print
命令获取。
UEFI/GPT 示例[编辑 | 编辑源代码]
首先需要一个 EFI 系统分区.如果是和 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%
调整分区[编辑 | 编辑源代码]
/
,使用安装介质/备用系统。 )如果要扩展分区,则必须先调整分区的大小,然后再调整其上的文件系统的大小; 如果要缩小分区,必须先调整文件系统的大小,再调整分区,以避免数据丢失。
扩展分区[编辑 | 编辑源代码]
要扩展分区(在 parted 交互模式下):
(parted) resizepart number end
其中 number
是您正在扩展的分区的编号,而 end
是该分区的新末端(需要大于旧的末端)。
然后,扩展此分区上的文件系统:
# resize2fs /dev/sdaX size
sdaX
代表您正在扩展的分区,而 size
是分区的新大小。
缩小分区[编辑 | 编辑源代码]
缩小分区上的文件系统:
# resize2fs /dev/sdaX size
其中 sdaX
代表您要缩小的分区,而 size
是该分区的新大小。
然后缩小分区(在 parted 交互模式下):
(parted) resizepart number end
其中 number
是您要缩小的分区的编号,而 end
是该分区的新末端(需要小于旧末端)。
完成后,使用 util-linux包 中的 resizepart 命令告诉内核新的分区大小:
# resizepart device number size
其中 device
是保存分区的设备,number
是分区的编号, size
是分区的新大小。
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