#fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).
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/vda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000cb9dd
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 6293503 2097152 82 Linux swap / Solaris
/dev/vda3 6293504 125829119 59767808 83 Linux <= 这个是需要扩展的分区3
Command (m for help): d <= 删除分区
Partition number (1-3, default 3): 3 <= 选择分区3,即删除
Partition 3 is deleted
Command (m for help): p <= 再次打印当前磁盘分区
Disk /dev/vda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000cb9dd
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 6293503 2097152 82 Linux swap / Solaris <= 可以看到分区3已经消失
Command (m for help): n <= 添加新分析,也就是再次添加分区3
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p <= 设置新增加的分区是主分区
Partition number (3,4, default 3): 3 <= 分区3
First sector (6293504-251658239, default 6293504): 6293504 <= 关键点:重建的分区3起始扇区必需和原先删除的分区3完全一致
Last sector, +sectors or +size{K,M,G} (6293504-251658239, default 251658239): <= 关键点:重建的分区3的结束扇区值扩大了,完整占据磁盘剩余空间
Using default value 251658239
Partition 3 of type Linux and of size 117 GiB is set
Command (m for help): p <= 再次打印当前磁盘分区
Disk /dev/vda: 128.8 GB, 128849018880 bytes, 251658240 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: 0x000cb9dd
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 6293503 2097152 82 Linux swap / Solaris
/dev/vda3 6293504 251658239 122682368 83 Linux <= 确认重建的分区3正确
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.
#partprobe
Error: Partition(s) 3 on /dev/vda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes.
#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda3 56G 2.4G 51G 5% /
...
执行以下命令扩展文件系统(默认扩展成分区大小,也可以指定文件系统大小)
resize2fs /dev/vda3
显示输出
resize2fs 1.43.5 (04-Aug-2017)
Filesystem at /dev/vda3 is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 8
The filesystem on /dev/vda3 is now 30670592 (4k) blocks long.
再次检查挂载的/分区,可以看到空间已经扩展到116G
#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda3 116G 2.4G 108G 3% /
...