扩展虚拟机的系统分区

xiaoxiao2021-02-28  117

使用一些Cloud Image的时候,经常遇到的一个问题是该image的磁盘太小,且系统盘太小,需要扩展。那么怎么做呢?下面的步骤给出答案。 虚拟机环境:Ubuntu 16.04 若虚拟机环境是CentOS 7.3,也同样需要以下四个步骤,且在步骤二之后需重启。

1. 扩展整个磁盘

命令是:

sudo qemu-img resize <your image file> +50G

经过这一步之后,重启虚拟机。 然后,在虚拟机中运行 df -h 会发现仍然没有任何分区被扩展,但是运行

fdisk -l

就会发现实际上整个磁盘已经被扩展了50GB.

2. 运行fdisk来扩展系统分区

在这一步中,无须担心的一点是,即使删除了系统分区 /dev/vda1 再重新建立,数据也基本上不会丢失。以下是一段示例:

$ sudo fdisk /dev/sda Command (m for help): p Device Boot Start End Blocks Id System /dev/sda1 * 2048 9437183 4717568 83 Linux Command (m for help): d Selected partition 1 Command (m for help): p Device Boot Start End Blocks Id System Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4, default 1): 1 First sector (2048-10485759, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): Using default value 10485759 Command (m for help): p Device Boot Start End Blocks Id System /dev/sda1 2048 10485759 5241856 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.

对于虚拟机来说,将上面的/dev/sda换成/dev/vda即可。

3. 重新加载partition table而无须重启

运行命令:

sudo partprobe /dev/vda

然后需要重启。

4. 运行resize2fs或xfs_growfs以使得fdisk的扩展生效

若非xfs文件系统,命令是:

sudo resize2fs /dev/vda1

若是xfs文件系统,应该运行命令:

sudo xfs_growfs /dev/vda1

至此,系统分区扩展完毕。

参考文献:

https://askubuntu.com/questions/24027/how-can-i-resize-an-ext-root-partition-at-runtimehttps://serverfault.com/questions/36038/reread-partition-table-without-rebooting

(完)

转载请注明原文地址: https://www.6miu.com/read-61328.html

最新回复(0)