Hugh's Blog

通过 fdisk 对磁盘分区扩容

 

最近 Hyper-V 里面虚拟机的硬盘容量不够用了,便在管理器里直接修改虚拟硬盘的大小,但是进去虚拟机查看并没有生效,网上查了下,还需要手动修改分区大小,这里用的是 fdisk 命令,从 50G 扩展到 200G。

记得操作前先设置一下检查点,以便恢复快照。

查看当前的挂载点:

root@us1804:~$ df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  924M     0  924M   0% /dev
tmpfs          tmpfs     191M   11M  181M   6% /run
/dev/sda2      ext4       49G   25G   22G  53% /
tmpfs          tmpfs     953M     0  953M   0% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     953M     0  953M   0% /sys/fs/cgroup
/dev/loop0     squashfs   91M   91M     0 100% /snap/core/6350
tmpfs          tmpfs     191M     0  191M   0% /run/user/1000

查看分区信息:

root@us1804:~$ fdisk -l
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 6EB98689-A74E-4AE2-B170-7E527EC91CCD

Device     Start       End   Sectors Size Type
/dev/sda1   2048      4095      2048   1M BIOS boot
/dev/sda2   4096 104855551 104851456  50G Linux filesystem

修改虚拟硬盘分区大小:

root@us1804:~$ fdisk /dev/sda

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m #查看命令说明

Help:

  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
   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): d #先删除分区
Partition number (1,2, default 2): 2 #sda2,分区号是 2

Partition 2 has been deleted.

Command (m for help): n #新建分区,选择主分区就行,p
Partition number (2-128, default 2): 2 #sda2,分区号还是 2
First sector (4096-419430366, default 4096):  #磁盘开始位置,默认回车就行
Last sector, +sectors or +size{K,M,G,T,P} (4096-419430366, default 419430366):  #分配的磁盘大小,全部分配,默认回车就行

Created a new partition 2 of type 'Linux filesystem' and of size 200 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: Y #如果旧的分区有签名信息,移除

The signature will be removed by a write command.

Command (m for help): p #查看新分区信息,可以看到,200G 已经完全分配
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 40301CF6-FD50-414E-8CB2-4BC129149EA7

Device     Start       End   Sectors  Size Type
/dev/sda1   2048      4095      2048    1M BIOS boot
/dev/sda2   4096 419430366 419426271  200G Linux filesystem

Filesystem/RAID signature on partition 2 will be wiped.

Command (m for help): w #保存新分区
The partition table has been altered.
Syncing disks.

接下来重启虚拟机 reboot,重启之后更新文件系统大小:

root@us1804:~$ resize2fs /dev/sda2
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 7, new_desc_blocks = 25
The filesystem on /dev/sda2 is now 52428283 (4k) blocks long.

查看新分区信息:

root@us1804:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            924M     0  924M   0% /dev
tmpfs           191M   11M  181M   6% /run
/dev/sda2       197G   25G  164G  14% /
tmpfs           953M     0  953M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           953M     0  953M   0% /sys/fs/cgroup
/dev/loop0       91M   91M     0 100% /snap/core/6350
tmpfs           191M     0  191M   0% /run/user/1000

可以看到,扩容已经完成。