Resize a CentOS Virtual Machine Image

Following our series of articles about virtual machine images, we now look at a common problem faced by system engineers – what happens when you run out of disk space on your virtual machine? This guide addresses this issue and explains how to expand a CentOS Virtual Machine image, resizing any partitions contained within.

We assume that the VM image to resize has been created using the centoskvm.sh shell script, as explained on my previous post “Automated CentOS Virtual Machine Image Creation“. This is because the resizing script (resizevm.sh) assumes details about storage volumes that rely on the centoskvm.sh having be used. In any case all the steps are illustrated here so you can adapt the commands to your needs.

The resizevm.sh shell script is part of the CentOS-KVM-Image-Tools project that you can clone on your machine using the following command:

git clone git://github.com/fubralimited/CentOS-KVM-Image-Tools.git

The resizevm.sh script accepts the VM name and the new size as command line parameters and resizes the virtual machine in place. For example:

sh resizevm.sh centos_vm 20G

Where centos_vm is the name of the virtual machine we want to resize and 20G is the new size.
You can also resize relative to the current size of the disk image by using the + symbol:

sh resizevm.sh centos_vm +10G

What’s on the inside?

The following section illustrates the main operations performed by the resizevm.sh script to better understand the process and provide some tips.
We assume that centos_vm is the name of the virtual image we want resize.

1. Shut the virtual machine down

The following operations and commands should not be used on live virtual machines, you must shut the virtual machine down before workig on it.

virsh shutdown centos_vm

2. Change directory

Now change to the images directory where we store the VM images.

cd /var/lib/libvirt/images

3. Clone VM image

To speedup the resizing process we start by creating a copy of the existing VM image file.
The resizing script contains an additional step to extract the VM image filename, since we already know the name of the image we can skip this step.

cp -f centos_vm.qcow2 centos_vm_temp.qcow2

4. Resize the cloned image

Now that we have cloned the image we can use the following qemu-img command to grow the image.

Please note that only images in raw format can be resized regardless of version. The Operating Systems based on Red Hat Enterprise Linux 6.1 and later adds the ability to grow (but not shrink) images in qcow2 format.

qemu-img resize centos_vm_temp.qcow2 20G

Alternatively you can also resize relative to the current size of the disk image by using the + symbol prefixing the storage parameter:

qemu-img resize centos_vm_temp.qcow2 +10G

5. Resize the partitions

Using the virt-resize command we can resize a virtual machine image, making it larger or smaller overall, and resizing or deleting any partitions contained within.

Virt-resize cannot resize VM images in-place, so this is why we cloned and expanded the VM image in the previous steps.

This would first expand the partition (and PV), and then expand the root device to fill the extra space in the PV.

The “–expand” option, expands the Physical Volume (PV) /dev/vda2 to fill any extra space.
The /dev/vda2 is the name of the PV disk device as originally defined using the centoskvm.sh creation script.

The “–LV-expand” option expands the root device (/dev/vg_main/lv_root) to fill the extra space in the PV .

6. Make a backup of the VM for any eventuality

At this point it may seem odd to make a backup half way through the process. In fact, this is because we have not actually edited the original image, we simply need to free up the name so that the new image can inherit the name of the original image.

mv -f centos_vm.qcow2 centos_vm.backup.qcow2

7. Sparsify image

Using the virt-sparsify tool we can make a virtual machine image sparse a.k.a. thin-provisioned. This means that free space within the disk image can be converted back to free space on the host.