kevinli 2019-12-04
我们 Linux 系统中有两种常见的分区表 MBR 分区表(主引导记录分区表)和 GPT 分区表(GUID 分 区表)
parted 命令也有点小问题,就是命令自身分区的时候只能格式化成 ext2 文件系统,不支持 ext3 文件系统,那就更不用说 ext4 文件系统了,不过这没有太多的影响,因为我们可以先分区再用 mkfs 进行格式化。
GNU Parted 2.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) m check NUMBER 文件系统检查 cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER 复制文件系统到另一个分区 help [COMMAND] 帮助 mklabel,mktable LABEL-TYPE 创建分区表 mkfs NUMBER FS-TYPE 建立文件系统在一个新分区上 mkpart PART-TYPE [FS-TYPE] START END 创建新分区 mkpartfs PART-TYPE FS-TYPE START END 创建新分区,且建立文件系统 move NUMBER START END 更该分区id name NUMBER NAME 将分区id作为分区名 print [devices|free|list,all|NUMBER] 打印分区信息 quit 退出 rescue START END 修复丢失的分区 resize NUMBER START END 修改分区大小 rm NUMBER 删除分区 select DEVICE 选择需要编辑的设备 set NUMBER FLAG STATE 设置分区表及 toggle [NUMBER [FLAG]] 切换分区表的状态 unit UNIT 默认单位
#修改成 GPT 分区表,我们转换分区表的目的是为了支持大于 2TB 的分区,如果分区并没有大于 2TB,那么这步是可以不执行的。 (parted) mklabel gpt Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? yes #此时再查看分区信息 (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags
(parted) mkpart Partition name? []? sdb1 分区名,任意取 File system type? [ext2]? Start? 1MB 起始位置 End? 5000MB 结束位置 (parted) print 查看信息 Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 1049kB 5000MB 4999MB sdb1
分区分完了,我们还需要格式化。不过我们已经知道如果使用 parted 交互命令格式化的话,只能格式化成 ext2 文件系统。我们这里是要演示下 parted 命令的格式化方法,所以就格式化成 ext2 吧。
(parted) mkfs WARNING: you are attempting to use parted to operate on (mkfs) a file system. parted's file system manipulation code is not as robust as what you'll find in dedicated, file-system-specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release. Warning: The existing file system will be destroyed and all data on the partition will be lost. Do you want to continue? Yes/No? yes #提示信息,告诉你,你将会删除改分区上的所有信息,yes就行。 Partition number? 1 File system type? [ext2]? (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags #此时就有文件系统了。 1 1049kB 5000MB 4999MB ext2 sdb1
parted 命令分区支持调整分区大小。parted 调整已经挂载使用的分区时,是不会影响分区中的数据的,也就是说数据不会丢失。但是一定要先卸载分区,再调整分区大小,否则数据是会出现问题的。还有要调整大小的分区必须已经建立了文件系统(格式化),否则会报错。
(parted) resize WARNING: you are attempting to use parted to operate on (resize) a file system. parted's file system manipulation code is not as robust as what you'll find in dedicated, file-system-specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release. Partition number? 1 Start? [1049kB]? End? [5000MB]? 6000MB (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 1049kB 6000MB 5999MB ext2 sdb1
[ ~]# mkfs -T ext4 /dev/sdb1 #再进入parted命令行查看 [ ~]# parted /dev/sdb GNU Parted 2.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 1049kB 6000MB 5999MB ext4 sdb1