Linux常用命令-parted

命令

parted

描述

a partition manipulation program
磁盘分区工具

用法

1
parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Options:
-l, --list 显示系统中所有块设备的分区信息
-m, --machine 显示原始格式,配合-l使用
-s, --script 不提示任何信息
-a, --align=[none|cyl|min|opt] 新分区的对齐方式

COMMANDs:
align-check TYPE N 检查分区N的对齐方式,TYPE可选项为min或opt
help [COMMAND] 帮助
mklabel,mktable LABEL-TYPE 创建新的分区表,可选项为gpt,msdos,loop等
mkpart PART-TYPE [FS-TYPE] START END 创建分区,FS-TYPE可选项为ext4,xfs等
name NUMBER NAME 重命名指定编号的分区名字
print [devices|free|list,all|NUMBER] 显示分区信息
quit 退出
rescue START END 恢复START到END之间的分区
resizepart NUMBER END 调整指定分区的大小
rm NUMBER 删除指定分区
select DEVICE 指定需要操作的设备
disk_set FLAG STATE 修改选中设备的FLAG标记,FLAG可选项为boot,root,swap,lvm等,STATE可选项为on或off
disk_toggle [FLAG] 切换选中设备的FLAG标记
set NUMBER FLAG STATE 修改指定分区的FLAG标记
toggle [NUMBER [FLAG]] 切换指定分区的FLAG标记
unit UNIT 设置默认显示的磁盘大小单位,可选项为s(sectors),B(bytes),kB,MB,MiB,GB,TB,%

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 交互式分区操作
$ parted /dev/sdd
GNU Parted 3.1
Using /dev/sdd
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
# 创建gpt分区表
(parted) mklabel
New disk label type?
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type? gpt
Warning: The existing disk label on /dev/sdd will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? y
# 创建分区1,大小10G
(parted) mkpart
Partition name? []? part1
File system type? [ext2]? xfs
Start? 1
End? 10G
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdd: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10.0GB 10000MB part1
# 创建分区2,大小5G,注意后续分区的起始大小是上一个分区的大小,如这里是10G
(parted) mkpart part2 xfs 10G 15G
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdd: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10.0GB 10000MB part1
2 10.0GB 15.0GB 5000MB part2
# 创建分区3,大小3G
(parted) mkpart part3 15g 18g
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdd: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10.0GB 10000MB part1
2 10.0GB 15.0GB 5000MB part2
3 15.0GB 18.0GB 3000MB part3
# 删除分区3
(parted) rm 3
(parted) quit
Information: You may need to update /etc/fstab.
# 查看uuid并格式化分区和配置挂载
$ blkid /dev/sdd1
/dev/sdd1: PARTLABEL="part1" PARTUUID="10cd8a04-8e1a-4f38-88fe-fa076e29ec1e"
$ mkfs.xfs /dev/sdd1
$ mount /dev/sdd1 /data/
$ df -hT /data
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdd1 xfs 9.4G 33M 9.3G 1% /data


# 非交互式分区操作
$ parted -s /dev/sdd mklabel gpt
$ parted -s /dev/sdd print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdd: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
# 创建分区1,大小10G
$ parted -s /dev/sdd mkpart part1 xfs 1 10g
Warning: The resulting partition is not properly aligned for best performance.
# 创建分区2,大小5G
$ parted -s /dev/sdd mkpart part2 xfs 10g 15g
$ parted -s /dev/sdd print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdd: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10.0GB 10000MB xfs part1
2 10.0GB 15.0GB 5000MB xfs part2
# 格式化和挂载同上-略
...

# 其他创建分区命令参考
# 创建一个分区,占用完整的磁盘空间,即磁盘只分一个区
$ parted -s /dev/sdd mkpart data xfs 1 100%