[root@xuexi ~]# fdisk /dev/sdb # sdb后没加数字 Command (m for help): m # 输入m查看可用命令帮助 Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition # 删除分区,如果删除扩展分区同时会删除里面的逻辑分区 l list known partition types # 列出分区类型 m print this menu # 显示帮助信息 n add a new partition # 创建新分区 o create a new empty DOS partition table p print the partition table # 输出分区信息 q quit without saving changes # 不保存退出 s create a new empty Sun disklabel t change a partition's system id # 修改分区类型 u change display/entry units v verify the partition table w write table to disk and exit # 保存分区信息并退出 x extra functionality (experts only)
Command (m for help): n # 添加分区 Command action e extended # 添加扩展分区 p primary partition (1-4) # 添加主分区 p # 输入p来创建第一个主分区 Partition number (1-4): 1 # 输入分区号,从1开始 First cylinder (1-1305, default 1): # 输入柱面号,不输人默认是1 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +2G # 给第一个主分区/dev/sdb1分2G,也可以使用柱面号来指定大小
Command (m for help): p # 第一个分区结束,p查看下已分区信息
Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x2d8d64eb Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ 83 Linux
Command (m for help): n # 再建一个分区 Command action e extended p primary partition (1-4) e # 创建扩展分区 Partition number (1-4): 2 # 扩展分区号为2 First cylinder (263-1305, default 263): Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-1305, default 1305): # 剩余空间全部给扩展分区 Using default value 1305 Command (m for help): p Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x2d8d64eb Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ 83 Linux /dev/sdb2 263 1305 8377897+ 5 Extended
Command (m for help): n # 新建逻辑分区 Command action l logical (5 or over) # 这里不再是扩展分区标识e,只有l。 # 如果已有3个主分区,这里连l都没有 p primary partition (1-4) l # 新建逻辑分区 First cylinder (263-1305, default 263): # 这里也不能选逻辑分区号了 Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-1305, default 1305): +3G Command (m for help): p Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x2d8d64eb Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ 83 Linux /dev/sdb2 263 1305 8377897+ 5 Extended /dev/sdb5 263 655 3156741 83 Linux 分区结束,保存。如果不保存,则按q。 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
[root@xuexi ~]# partprobe # 执行partprobe,下面一堆信息,不理它 Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Error: Invalid partition table - recursive partition on /dev/sr0.
[root@xuexi ~]# gdisk /dev/sdb GPT fdisk (gdisk) version 0.8.10 Partition table scan: MBR: not present BSD: not present APM: not present GPT: not present Creating new GPT entries.
Command (? for help): ? b back up GPT data to a file c change a partition's name d delete a partition # 删除分区 i show detailed information on a partition # 列出分区详细信息 l list known partition types # 列出所以已知的分区类型 n add a new partition # 添加新分区 o create a new empty GUID partition table (GPT) # 创建一个新的空的guid分区表 p print the partition table # 输出分区表信息 q quit without saving changes # 退出gdisk工具 r recovery and transformation options (experts only) s sort partitions t change a partition's type code # 修改分区类型 v verify disk w write table to disk and exit # 将分区信息写入到磁盘 x extra functionality (experts only) ? print this menu
Command (? for help): n Partition number (1-128, default 1): First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +10G Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): Changed type of partition to 'Linux filesystem' Command (? for help): p Disk /dev/sdb: 41943040 sectors, 20.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): F8AE925F-515F-4807-92ED-4109D0827191 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 41943006 Partitions will be aligned on 2048-sector boundaries Total free space is 20971453 sectors (10.0 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 20973567 10.0 GiB 8300 Linux filesystem Command (? for help): i Using 1 Partition GUID code: 0FC63DAF-8483-4772-8E79-3D69D8477DE4 (Linux filesystem) Partition unique GUID: B2452103-4F32-4B60-AEF7-4BA42B7BF089 First sector: 2048 (at 1024.0 KiB) Last sector: 20973567 (at 10.0 GiB) Partition size: 20971520 sectors (10.0 GiB) Attribute flags: 0000000000000000 Partition name: 'Linux filesystem'
保存分区表到磁盘。
1 2 3 4 5 6 7 8
Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): Y OK; writing new GUID partition table (GPT) to /dev/sdb. The operation has completed successfully.
Command (? for help): x Expert command (? for help): ? a set attributes c change partition GUID d display the sector alignment value e relocate backup data structures to the end of the disk g change disk GUID h recompute CHS values in protective/hybrid MBR i show detailed information on a partition l set the sector alignment value m return to main menu n create a new protective MBR o print protective MBR data p print the partition table q quit without saving changes r recovery and transformation options (experts only) s resize partition table # 修改分区表大小,注意不是分区大小 t transpose two partition table entries u Replicate partition table on new device # 将分区表导出 v verify disk w write table to disk and exit z zap (destroy) GPT data structures and exit # 损毁gpt上的数据 ? print this menu
[root@xuexi ~]# parted /dev/sdc GNU Parted 2.1 Using /dev/sdc Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) help align-check TYPE N check partition N for TYPE(min|opt) alignment check NUMBER (centos 7上已删除该功能) do a simple check on the file system cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER (centos 7上已删除该功能) copy file system to another partition help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkfs NUMBER FS-TYPE (centos 7上已删除该功能) make a FS-TYPE file system on partition NUMBER mkpart PART-TYPE [FS-TYPE] START END make a partition mkpartfs PART-TYPE FS-TYPE START END (centos 7上已删除该功能) make a partition with a file system move NUMBER START END (centos 7上已删除该功能) move partition NUMBER name NUMBER NAME name partition NUMBER as NAME print [devices|free|list,all|NUMBER] display the partition table,available devices,free space, all found partitions,or a particular partition quit exit program rescue START END rescue a lost partition near START and END resize NUMBER START END (修改分区大小(centos 7上已删除该功能)) resize partition NUMBER and its file system rm NUMBER (删除分区) delete partition NUMBER select DEVICE (重选磁盘进入parted状态) choose the device to edit set NUMBER FLAG STATE (设置分区状态,如将其off或on) change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] (修改文件系统类型,如swap、lvm) toggle the state of FLAG on partition NUMBER unit UNIT (修改默认单位,kB/MB/GB等) set the default unit to UNIT version display the version number and copyright information of GNU Parted
parted分区的前提是磁盘已经有分区表(partition table)或磁盘标签(disk label),否则将显示unrecognised disk label,这是和fdisk/gdisk不同的地方,所以需要先使用mklabel创建标签或分区表,最常见的标签(分区表)为msdos和gpt,其中msdos分区就是MBR格式的分区表,也就是会有主分区、扩展分区和逻辑分区的概念和限制。
[root@xuexi ~]# parted /dev/sdc GNU Parted 2.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel # 创建磁盘分区标签(分区表类型) New disk label type? msdos # 选择msdos即MBR类型 # 上面的两步也可以直接一步进行:(parted) mklabel msdos (parted) mkpart # 开始进行分区 Partition type? primary/extended? p # 创建主分区 File system type? [ext2]? ext4 # 创建ext4文件系统 # (这里虽指明了文件系统,但没有意义,仍需手动格式化并选择文件系统类型) Start? 1 # 分区开始位置,默认是M为单位,表示从1M开始,也可直接指定1G这种方式 End? 1024 # 分区结束位置,1024-1=1023M (parted) p # print,查看分区信息 Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1024MB 1023MB primary # 可以一步完成一个命令中的多个动作 (parted) mkpart p ext4 1026M 4096M # 可一步完成,也可一步完成到任何位置,然后继续交互下一步 # 可能会提示分区未对齐"Warning: The resulting partition is not properly aligned for best performance.",忽略它 (parted) mkpart e 4098 -1 # 创建扩展分区,注意创建扩展分区时不指定文件系统类型;-1表示剩余的全部分配给该分区 (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1024MB 1023MB primary 2 1026MB 4096MB 3070MB primary 3 4098MB 21.5GB 17.4GB extended lba (parted) mkpart l ext4 4099 8194 # 创建逻辑分区,指定ext4 (parted) mkpart l ext4 8195 -1 # 继续创建逻辑分区 (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1024MB 1023MB primary 2 1026MB 4096MB 3070MB primary 3 4098MB 21.5GB 17.4GB extended lba 5 4099MB 8194MB 4095MB logical 6 8195MB 21.5GB 13.3GB logical (parted) rm 5 # 删除5号分区 (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1024MB 1023MB primary 2 1026MB 4096MB 3070MB primary 3 4098MB 21.5GB 17.4GB extended lba 5 8195MB 21.5GB 13.3GB logical (parted) quit # 退出parted工具 Information: You may need to update /etc/fstab. # 提示要更新/etc/fstab中的配置,说明该工具可在线分区
mkfs和mkpartfs等命令不完善,下面的警告信息已经给出了提示。
1 2 3 4 5 6 7 8 9 10 11
(parted) mkfs 1 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? parted: invalid token: 1 Yes/No? n
$ parted -s /dev/sdb mkpart 3 14G 16G Warning: You requested a partition from 14.0GB to 16.0GB. The closest location we can manage is 15.0GB to 16.0GB. Is this still acceptable to you? Information: You may need to update /etc/fstab.
$ mke2fs -t ext4 -I 256 /dev/sdb2 -b 4096 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 655360 inodes, 2621440 blocks 131072 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2684354560 80 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.