DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag
Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition
Misc m print this menu u change display/entry units x extra functionality (experts only)
Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file
Save & Exit w write table to disk and exit q quit without saving changes
Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table
Command (m for help):
(5)按 p 查看当前磁盘下的分区:
1 2 3 4 5 6 7 8 9 10 11 12
Command (m for help): p Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 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 Disklabel type: dos Disk identifier: 0x7bb4c495
Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 125829119 125827072 60G 83 Linux # 本例该磁盘仅仅一个分区,Boot 下面有个可启动标记 *,/boot 没有独立分区
(6)按 d 删除 / 分区:
1 2 3 4 5 6
Command (m for help): d Selected partition 1 Partition 1 has been deleted.
Command (m for help): # 本例中只有一个分区,所以直接删除了,如果是有多个分区,会提示输入数字选择
(7)按 n 创建新分区:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p #选择 p primary Partition number (1-4, default 1): #直接回车默认 1 即 sda1 First sector (2048-209715199, default 2048): #直接回车默认值 (sysin) Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): #直接回车默认值,使用全部剩余空间
Created a new partition 1 of type 'Linux' and of size 100 GiB. Partition #1 contains a xfs signature.
Do you want to remove the signature? [Y]es/[N]o: N #按 N 保留 xfs 签名,移除的话分区的 UUID 会变更。
The signature will be removed by a write command.
Command (m for help):
(8)按 p 再次查看状态:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Command (m for help): p Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 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 Disklabel type: dos Disk identifier: 0x6a72cc03
Device Boot Start End Sectors Size Id Type /dev/sda1 2048 209715199 209713152 100G 83 Linux
Filesystem/RAID signature on partition 1 will be wiped.
Command (m for help):
(9)重要步骤:按 a 设置可引导:
本例 /boot 没有独立分区,需要需要设置 boot flag 即将分区设置为可引导:
/boot 独立分区的不需要此步骤。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Command (m for help): a Selected partition 1 The bootable flag on partition 1 is enabled now. # 按 p 再次确认,Boot 下面有了 * 符号 Command (m for help): p Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 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 Disklabel type: dos Disk identifier: 0x6a72cc03
Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 209715199 209713152 100G 83 Linux
Filesystem/RAID signature on partition 1 will be wiped.
Command (m for help):
(10)按 w 保存:
1 2 3
Command (m for help): w The partition table has been altered. Syncing disks.
Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
Command (m for help): p #p 查看当前分区
Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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 Disk identifier: 0x000af364
Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 335544319 166722560 8e Linux LVM
Command (m for help): n #n 新建分区 Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p #p 主分区 Partition number (3,4, default 3): #默认按顺序,直接回车 First sector (335544320-545259519, default 335544320): #默认直接回车 Using default value 335544320 Last sector, +sectors or +size{K,M,G} (335544320-545259519, default 545259519): #默认直接回车使用全部剩余空间 Using default value 545259519 Partition 3 of type Linux and of size 100 GiB is set
Command (m for help): p #p 再次查看分区
Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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 Disk identifier: 0x000af364
Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 335544319 166722560 8e Linux LVM /dev/sda3 335544320 545259519 104857600 83 Linux
Command (m for help): w #w 保存 The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
(2)刷新分区
1 2 3 4 5 6 7 8 9 10 11 12 13
# root @ C7-SYSIN in ~ [13:31:00] $ partprobe /dev/sda # root @ C7-SYSIN in ~ [13:31:54] $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 260G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 159G 0 part │ ├─centos-root 253:0 0 155G 0 lvm / │ └─centos-swap 253:1 0 4G 0 lvm [SWAP] └─sda3 8:3 0 100G 0 part sr0 11:0 1 1024M 0 rom
$ vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size <159.00 GiB PE Size 4.00 MiB Total PE 40703 Alloc PE / Size 40703 / <159.00 GiB Free PE / Size 0 / 0 VG UUID Aul9M5-OJu8-3RB5-DU9Y-yi5m-ngyd-QyIKLw
VG 名称为 centos
(5)扩展 VG
使用 /dev/sda3 PV 扩展到 centos VG 中。
1 2
$ vgextend centos /dev/sda3 Volume group "centos" successfully extended
$ lvdisplay --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID ntq8LZ-qivt-B6ij-AZWi-97a9-H2Q5-YxznfS LV Write Access read/write LV Creation host, time localhost, 2021-08-22 14:12:50 +0800 LV Status available # open 1 LV Size <155.00 GiB Current LE 39679 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
--- Logical volume --- LV Path /dev/centos/swap LV Name swap VG Name centos LV UUID jvNgUR-KUsk-1hD4-h383-w3pc-OhBT-ZFMpyi LV Write Access read/write LV Creation host, time localhost, 2021-08-22 14:12:51 +0800 LV Status available # open 2 LV Size 4.00 GiB Current LE 1024 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1
需要扩展的根分区 LV 为 /dev/centos/root
(7)将 VG 中的空闲空间扩展到根分区 LV
1 2 3
$ lvextend -l +100%FREE /dev/centos/root Size of logical volume centos/root changed from <155.00 GiB (39679 extents) to 254.99 GiB (65278 extents). Logical volume centos/root successfully resized.
Welcome to fdisk (util-linux 2.32.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
Command (m for help): p #查看当然分区 Disk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 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 Disklabel type: dos Disk identifier: 0x316023cd
Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 8388607 8386560 4G 82 Linux swap / Solaris
Command (m for help): d #删除分区,只有一个分区不会提示选择数字 Selected partition 1 Partition 1 has been deleted.
Command (m for help): n #新建分区 Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p #主分区,输入 P 或者直接回车 Partition number (1-4, default 1): #直接回车按顺序 1 First sector (2048-33554431, default 2048): #直接回车 Last sector, +sectors or +size{K,M,G,T,P} (2048-33554431, default 33554431): #直接回车使用全部可用空间
Created a new partition 1 of type 'Linux' and of size 16 GiB. Partition #1 contains a swap signature.
Do you want to remove the signature? [Y]es/[N]o: N # 选择 N 不要移除 swap 签名,磁盘 UUID 不变
The signature will be removed by a write command.
Command (m for help): t #更改分区类型 Selected partition 1 Hex code (type L to list all codes): L #输入 L,查看可用代码
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT 10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/ 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto 1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep 1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT Hex code (type L to list all codes): 82 #输入 82,更改为 SWAP Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p #再次查看分区,已经是 swap Disk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 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 Disklabel type: dos Disk identifier: 0x316023cd
Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 33554431 33552384 16G 82 Linux swap / Solaris
Filesystem/RAID signature on partition 1 will be wiped.
Command (m for help): w #保存退出 The partition table has been altered. Calling ioctl() to re-read partition table. Re-reading the partition table failed.: Device or resource busy
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
Command (m for help): p #查看当前分区
Disk /dev/sda: 184.7 GB, 184683593728 bytes, 360710144 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 Disk identifier: 0x000af364
Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 335544319 166722560 8e Linux LVM
Command (m for help): n #新建分区 Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): #直接回车,默认主分区 Using default response p Partition number (3,4, default 3): #直接回车默认按顺序 3 First sector (335544320-360710143, default 335544320): #直接回车 Using default value 335544320 Last sector, +sectors or +size{K,M,G} (335544320-360710143, default 360710143): #直接回车,使用全部剩余空间 Using default value 360710143 Partition 3 of type Linux and of size 12 GiB is set #12G 没错
Command (m for help): p #再次查看分区
Disk /dev/sda: 184.7 GB, 184683593728 bytes, 360710144 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 Disk identifier: 0x000af364
Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 335544319 166722560 8e Linux LVM /dev/sda3 335544320 360710143 12582912 83 Linux
Command (m for help): w #保存退出 The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. You have new mail.
(3)刷新分区
1 2 3 4 5 6 7 8 9 10 11 12 13
# root @ C7-SYSIN in ~ [13:31:00] $ partprobe /dev/sda # root @ C7-SYSIN in ~ [13:31:54] $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 172G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 159G 0 part │ ├─centos-root 253:0 0 155G 0 lvm / │ └─centos-swap 253:1 0 4G 0 lvm └─sda3 8:3 0 12G 0 part sr0 11:0 1 1024M 0 rom
$ vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size <159.00 GiB PE Size 4.00 MiB Total PE 40703 Alloc PE / Size 40703 / <159.00 GiB Free PE / Size 0 / 0 VG UUID Aul9M5-OJu8-3RB5-DU9Y-yi5m-ngyd-QyIKLw
VG 名称为 centos
(6)扩展 VG
使用 /dev/sda3 PV 扩展到 centos VG 中。
1 2
$ vgextend centos /dev/sda3 Volume group "centos" successfully extended
$ lvdisplay --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID ntq8LZ-qivt-B6ij-AZWi-97a9-H2Q5-YxznfS LV Write Access read/write LV Creation host, time localhost, 2021-08-22 14:12:50 +0800 LV Status available # open 1 LV Size <155.00 GiB Current LE 39679 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
--- Logical volume --- LV Path /dev/centos/swap LV Name swap VG Name centos LV UUID jvNgUR-KUsk-1hD4-h383-w3pc-OhBT-ZFMpyi LV Write Access read/write LV Creation host, time localhost, 2021-08-22 14:12:51 +0800 LV Status available # open 2 LV Size 4.00 GiB Current LE 1024 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1
需要扩展的根分区 LV 为 /dev/centos/swap
(8)将 VG 中的空闲空间扩展到根分区 LV
1 2 3
$ lvextend -l +100%FREE /dev/centos/swap Size of logical volume centos/swap changed from 4.00 GiB (1024 extents) to <16.00 GiB (4095 extents). Logical volume centos/swap successfully resized.
# sa @ U20 in ~ [16:23:43] C:1 $ sudo fdisk /dev/sdb #将 /dev/sdb 创建分区 [sudo] password for sa:
Welcome to fdisk (util-linux 2.34). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0x9a7ae73a.
Command (m for help): n #n 新建分区 Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): #直接回车,主分区
Using default response p. Partition number (1-4, default 1): #直接回车,按顺序编号 1 First sector (2048-209715199, default 2048): #直接回车 Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199): #直接回车,使用全部可用空间
Created a new partition 1 of type 'Linux' and of size 100 GiB.
Command (m for help): p #查看分区 Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors Disk model: VMware Virtual S Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x9a7ae73a
Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 209715199 209713152 100G 83 Linux
Command (m for help): w #保存退出 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
# sa @ U20 in ~ [16:23:43] C:1 $ sudo fdisk /dev/sdc #将 /dev/sdc 创建分区 [sudo] password for sa:
Welcome to fdisk (util-linux 2.34). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.
Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0x9a7ae73a.
Command (m for help): n #n 新建分区 Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): #直接回车,主分区
Using default response p. Partition number (1-4, default 1): #直接回车,按顺序编号 1 First sector (2048-209715199, default 2048): #直接回车 Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199): #直接回车,使用全部可用空间
Created a new partition 1 of type 'Linux' and of size 100 GiB.
Command (m for help): p #查看分区 Disk /dev/sdc: 100 GiB, 107374182400 bytes, 209715200 sectors Disk model: VMware Virtual S Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x9a7ae73a
Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 209715199 209713152 100G 83 Linux
Command (m for help): w #保存退出 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.