使用一些Cloud Image的时候,经常遇到的一个问题是该image的磁盘太小,且系统盘太小,需要扩展。那么怎么做呢?下面的步骤给出答案。 虚拟机环境:Ubuntu 16.04 若虚拟机环境是CentOS 7.3,也同样需要以下四个步骤,且在步骤二之后需重启。
命令是:
sudo qemu-img resize <your image file> +50G经过这一步之后,重启虚拟机。 然后,在虚拟机中运行 df -h 会发现仍然没有任何分区被扩展,但是运行
fdisk -l就会发现实际上整个磁盘已经被扩展了50GB.
在这一步中,无须担心的一点是,即使删除了系统分区 /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即可。
运行命令:
sudo partprobe /dev/vda然后需要重启。
若非xfs文件系统,命令是:
sudo resize2fs /dev/vda1若是xfs文件系统,应该运行命令:
sudo xfs_growfs /dev/vda1至此,系统分区扩展完毕。
(完)