How to increase the HDD size of a KVM VSP

How to increase the HDD size of a KVM VSP

If you have a VPS (Virtual Private Server) installed on a main server via KVM you might need at some point to increase the HDD size of the VPS. To do this there are 2 steps:

  • Increase the KVM VSP image size
  • Increasing the usable space for the Linux VPS

For this tutorial we are using Ubuntu, so if you are on a different distribution, the commands might be a little different.

 

Increase the VPS image via the KVM

Increasing the VPSimage size is quite easy. Just because we are playing with everything that is available on the server we will first make a backup. Making backup to a KVM VPS is very easy, we shut down the server and copy the image file somewhere else.

List all the KVM images available on the system

First let’s see what images we have:

virsh list

You might want to find out more information about the images including where they are on the disk:

virsh dumpxml ubuntu1604

and look throught the XML file until you see the info about the image location:

If you are unsure this is the image you have to resize you can also check its size:

virsh vol-info /var/kvm/images/ubuntu1604.img

Making a backup of the KVM image

First you have to gracefully stop the server:

virsh shutdown ubuntu160

4 (where ubuntu1604 is the image name that we want to backup)

Let’s copy the image somewhere else:


cp -p /var/kvm/images/ubuntu1604.img /home/backup

It might also be a good ideea to backup the configuration file as well. For example:

cp -p /etc/libvirt/qemu/ubuntu1604.xml /home/backup

Expanding the disk size of the KVM image

To expand the disk size of a KVM image you have to use a qemu command (for this example we are increasing the disk size with 1 GB

qemu-img resize  /var/kvm/images/ubuntu1604.img +1G

You can check if the image size was increased

virsh vol-info /var/kvm/images/ubuntu1604.img


You can now start the virtual machine and see if everything is ok:

virsh start ubuntu1604

 

Increasing the usable space for the Linux VPS

In this step we let the VPS know that there is more space and make it available for use.

Let’s see how the space is distributed:

fdisk -l

– will show the partitions, for example:

As you can see a partition is used as swap, so we need to shut down swaping first:

swapoff -a

 

Also, please note carefully where the main partition starts – 2048 (your value might be different and it’s essential to re-create the new partitions exactly from this point or all the data will be lost!)

Let’s delete delete the partitions (this will not delete the data) – your disk and partitions name might be different:

d

to initiate the delete command then delete all the partitions (repeat d and the partition name):

Recreate the first partition:

Press n then p for primary, partition number 1, first sector 2048, last sector +100G (1 GB more than we had at the beginning).

 

Let’s recreate the swap partition:

n, p for primary, 2 as partition number, enter, enter

to create the swap partition on the entire space that is left.

Let’s mark partition 2 as being swap:

t, select partition 2, then write 82 (82 means Linux swap / Solaris) and press enter:

We now have to make partition1 bootable:

a, then select partition 1


All this done we have to write the new partitions to the disk. Take a big breath, check again that you have copied the KVM image then press:

w

At this point you might get an error that looks scarry!

 

Re-reading the partition table failed.: 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).

The solution is simple;run partprobe.

partbrobe

 

Now we need to resize our filesystem on /dev/vda1:

resize2fs /dev/vda1

 

Let’s make the swap work again:

mkswap /dev/vda2

 

Copy the UUID value, then edit the fstab file

vim /etc/fstab/

and replace the UUID swap value

Start swap:

swapon -a