分区管理工具-parted命令实战案例
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
parted的操作都是实时生效的,生产环境中请谨慎使用。因为你做的任何操作都是立即执行当你执行了一些命令想要撤销是不可行的。推荐使用fdisk和gdisk创建分区。
一.分区表概述
博主推荐阅读: https://www.cnblogs.com/yinzhengjie/p/12347792.html
二.parted案例展示
1>.查看parted命令的帮助信息
[root@yinzhengjie.com ~]# man parted
2>.创建MBR分区表
[root@yinzhengjie.com ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 2T 0 disk ├─sda1 8:1 0 2M 0 part ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 2T 0 part ├─centos-root 253:0 0 500G 0 lvm / └─centos-yinzhengjie 253:1 0 1.5T 0 lvm /yinzhengjie sdb 8:16 0 600G 0 disk sdc 8:32 0 400G 0 disk sdd 8:48 0 800G 0 disk sde 8:64 0 1000G 0 disk sr0 11:0 1 1024M 0 rom [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# fdisk -l /dev/sdb Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# parted /dev/sdb mklabel msdos Information: You may need to update /etc/fstab. [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# fdisk -l /dev/sdb Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos #类型为dos,其实就对应着MBR分区表 Disk identifier: 0x0006a574 Device Boot Start End Blocks Id System [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# hexdump -C /dev/sdb -n 512 00000000 fa b8 00 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |................| 00000010 fb be 00 7c bf 00 06 b9 00 02 f3 a4 ea 21 06 00 |...|.........!..| 00000020 00 be be 07 38 04 75 0b 83 c6 10 81 fe fe 07 75 |....8.u........u| 00000030 f3 eb 16 b4 02 b0 01 bb 00 7c b2 80 8a 74 01 8b |.........|...t..| 00000040 4c 02 cd 13 ea 00 7c 00 00 eb fe 00 00 00 00 00 |L.....|.........| 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 000001b0 00 00 00 00 00 00 00 00 74 a5 06 00 00 00 00 00 |........t.......| 000001c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.| 00000200 [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# hexdump -C /dev/sdb -n 512 -v 00000000 fa b8 00 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |................| 00000010 fb be 00 7c bf 00 06 b9 00 02 f3 a4 ea 21 06 00 |...|.........!..| 00000020 00 be be 07 38 04 75 0b 83 c6 10 81 fe fe 07 75 |....8.u........u| 00000030 f3 eb 16 b4 02 b0 01 bb 00 7c b2 80 8a 74 01 8b |.........|...t..| 00000040 4c 02 cd 13 ea 00 7c 00 00 eb fe 00 00 00 00 00 |L.....|.........| 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001b0 00 00 00 00 00 00 00 00 74 a5 06 00 00 00 00 00 |........t.......| 000001c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.| 00000200 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
3>.创建GPT分区表
[root@yinzhengjie.com ~]# fdisk -l /dev/sdb Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos #类型为dos,其实就对应着MBR分区表 Disk identifier: 0x0006a574 Device Boot Start End Blocks Id System [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# parted /dev/sdb 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工具再进行分区时会提示咱们是否继续,输入yes就可以确认重写为gpt分区。 Information: You may need to update /etc/fstab. [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# fdisk -l /dev/sdb WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. Disk /dev/sdb: 644.2 GB, 644245094400 bytes, 1258291200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: gpt #很显然,已经成功更改为GPT类型的分区啦~ Disk identifier: 566063CE-4FF3-451F-B57A-C42D79F93C20 # Start End Size Type Name [root@yinzhengjie.com ~]#
4>.查看分区信息
[root@yinzhengjie.com ~]# parted /dev/sdb print #查看/dev/sdb设备的分区信息 Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 644GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags #很明显,目前该磁盘没有任何的分区信息生成,仅仅声明了当前设备是GPT分区。 [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# parted -l Model: VMware, VMware Virtual S (scsi) Disk /dev/sda: 2199GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: pmbr_boot Number Start End Size File system Name Flags 1 1049kB 3146kB 2097kB bios_grub 2 3146kB 1077MB 1074MB xfs 3 1077MB 2199GB 2198GB lvm Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 644GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags Error: /dev/sdc: unrecognised disk label Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc: 429GB Sector size (logical/physical): 512B/512B Partition Table: unknown Disk Flags: Error: /dev/sdd: unrecognised disk label Model: VMware, VMware Virtual S (scsi) Disk /dev/sdd: 859GB Sector size (logical/physical): 512B/512B Partition Table: unknown Disk Flags: Error: /dev/sde: unrecognised disk label Model: VMware, VMware Virtual S (scsi) Disk /dev/sde: 1074GB Sector size (logical/physical): 512B/512B Partition Table: unknown Disk Flags: Model: Linux device-mapper (linear) (dm) Disk /dev/mapper/centos-yinzhengjie: 1661GB Sector size (logical/physical): 512B/512B Partition Table: loop Disk Flags: Number Start End Size File system Flags 1 0.00B 1661GB 1661GB xfs Model: Linux device-mapper (linear) (dm) Disk /dev/mapper/centos-root: 537GB Sector size (logical/physical): 512B/512B Partition Table: loop Disk Flags: Number Start End Size File system Flags 1 0.00B 537GB 537GB xfs [root@yinzhengjie.com ~]#
5>.创建主分区
[root@yinzhengjie.com ~]# parted /dev/sdb print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 644GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 2T 0 disk ├─sda1 8:1 0 2M 0 part ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 2T 0 part ├─centos-root 253:0 0 500G 0 lvm / └─centos-yinzhengjie 253:1 0 1.5T 0 lvm /yinzhengjie sdb 8:16 0 600G 0 disk sdc 8:32 0 400G 0 disk sdd 8:48 0 800G 0 disk sde 8:64 0 1000G 0 disk sr0 11:0 1 1024M 0 rom [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# parted /dev/sdb mkpart primary 1 120000 #创建主分区,指定起始位置为1,结束位置为120000,即分配了大概120G的分区。 Information: You may need to update /etc/fstab. [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 2T 0 disk ├─sda1 8:1 0 2M 0 part ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 2T 0 part ├─centos-root 253:0 0 500G 0 lvm / └─centos-yinzhengjie 253:1 0 1.5T 0 lvm /yinzhengjie sdb 8:16 0 600G 0 disk └─sdb1 8:17 0 111.8G 0 part sdc 8:32 0 400G 0 disk sdd 8:48 0 800G 0 disk sde 8:64 0 1000G 0 disk sr0 11:0 1 1024M 0 rom [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# parted /dev/sdb print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 644GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 120GB 120GB primary [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# parted /dev/sdb mkpart primary 120001 240000 #创建第二个主分区依旧需要咱们自己指定分区的起始位置,这就得需要您自己提前规划好哟~ Information: You may need to update /etc/fstab. [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# parted /dev/sdb print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 644GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 120GB 120GB primary 2 120GB 240GB 120GB primary [root@yinzhengjie.com ~]#
6>.删除分区
[root@yinzhengjie.com ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 2T 0 disk ├─sda1 8:1 0 2M 0 part ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 2T 0 part ├─centos-root 253:0 0 500G 0 lvm / └─centos-yinzhengjie 253:1 0 1.5T 0 lvm /yinzhengjie sdb 8:16 0 600G 0 disk ├─sdb1 8:17 0 111.8G 0 part ├─sdb2 8:18 0 111.8G 0 part └─sdb3 8:19 0 512B 0 part sdc 8:32 0 400G 0 disk sdd 8:48 0 800G 0 disk sde 8:64 0 1000G 0 disk sr0 11:0 1 1024M 0 rom [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# parted /dev/sdb print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 644GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 120GB 120GB primary 2 120GB 240GB 120GB primary 3 240GB 240GB 512B primary #该分区我手动分配错误啦,其大小仅512B(即仅一个扇区),因此我们需要手动删除它。 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# parted /dev/sdb rm 3 #指定要删除的分区编号 Information: You may need to update /etc/fstab. [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# parted /dev/sdb print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 644GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 120GB 120GB primary 2 120GB 240GB 120GB primary [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 2T 0 disk ├─sda1 8:1 0 2M 0 part ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 2T 0 part ├─centos-root 253:0 0 500G 0 lvm / └─centos-yinzhengjie 253:1 0 1.5T 0 lvm /yinzhengjie sdb 8:16 0 600G 0 disk ├─sdb1 8:17 0 111.8G 0 part └─sdb2 8:18 0 111.8G 0 part sdc 8:32 0 400G 0 disk sdd 8:48 0 800G 0 disk sde 8:64 0 1000G 0 disk sr0 11:0 1 1024M 0 rom [root@yinzhengjie.com ~]#
7>.清空整个分区表(请谨慎操作!)
[root@yinzhengjie.com ~]# hexdump -C /dev/sdb -n 512 -v 00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001c0 01 00 ee fe ff ff 01 00 00 00 ff ff ff 4a 00 00 |.............J..| 000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.| 00000200 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# lsblk /dev/sdb #为情况分区表之前该设备有2个分区表信息 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 600G 0 disk ├─sdb1 8:17 0 111.8G 0 part └─sdb2 8:18 0 111.8G 0 part [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# dd if=/dev/zero of=/dev/sdb bs=1 count=512 #清空磁盘的第一个扇区。 512+0 records in 512+0 records out 512 bytes (512 B) copied, 0.00229388 s, 223 kB/s [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]# lsblk /dev/sdb #清空分区表后就看不到对应的分区信息啦 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 600G 0 disk [root@yinzhengjie.com ~]#
[root@yinzhengjie.com ~]# hexdump -C /dev/sdb -n 512 -v 00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000000f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 00000200 [root@yinzhengjie.com ~]# [root@yinzhengjie.com ~]#
三.fdisk分区管理工具
博主推荐阅读: https://www.cnblogs.com/yinzhengjie/p/12359248.html