How to resize a qcow2 harddrive images

I usually make the hard drives on my virtual machines as small as possible to save disk space on the host. I do this because it is reasonably easy to resize them at a later date (especially if you using LVM). Anyhow here is a quick guide to resizing a qcow2 disk image.

1 – Shutdown the guest.. It’s important to shutdown the vm properly rather than pausing it or forcing it to a stop because you need the file system to be a perfect condition before resizing it.

virsh shutdown hostname

2 – Increase the qcow2 disk. It a simple command to increase the size of the disk. Here I am adding 5GB to an Ubuntu Server disk.

qemu-img resize ubuntu-server.qcow2 +5GB

3 – Resize the partition Now that the virtual disk has been resized you need to resize the guest’s partitions within the image. I recommend using a small live Linux distrobution such as GParted Live. First download an iso image of GParted and attach it to the virtual machine by editing the vm settings.

virsh edit hostname

Add a boot order at the top. By default there will be one node that should read:

<boot dev='hd'/>

Simply change this to:

<boot dev='cdrom'/>

Add a cdrom after the hard disk. Make sure to change the /path/to/image.iso to the ISO image you want to boot from. If you don’t set this correctly the VM will not boot.

<disk type='file' device='cdrom'/> 
<source file='/mnt/iso/gparted-live-0.16.1-1-i486.iso'/>
<target dev='hdc' bus='ide'/>

Finally, start the VM and VNC into it. I tend to use Virtual Machine Manager to connect to a VM. It works brilliantly on most Linux machines. You should then be able to use GParted to increase the size of the partition.



4 – Start the VM backup Note: Unless you really need it – I generally edit the VM and remove the cdrom and boot order by reversing the step described above.

virsh start hostname

How to mount a qcow2 disk image on Ubuntu

This is a quick guide to mounting a qcow2 disk images on your host server. This is useful to reset passwords, edit files, or recover something without the virtual machine running.

Step 1 – Enable NBD on the Host
You will need to enable the nbd (network block device) kernel module on the host. This should be available on all Ubuntu servers after Intrepid.

sudo modprobe nbd max_part=8

Step 2- Connect the QCOW2 as network block device
Use qemu-nbd to connect your qcow2 file as a network block device. Note: You will need to specify the full path of qcow2 file even if you are the directory of the qcow files.

sudo qemu-nbd --connect=/dev/nbd0 /mnt/kvm/wordpress-sites.qcow2

Step 3 – Find The Virtual Machine Partitions
You can find a list of the partitions on the vm using the following command

sudo fdisk /dev/nbd0 -l

Step 4 – Mount the partition from the virtual machine.
For example, I want to mount partition 1 and therefore run:

 sudo mount /dev/nbd0p1 /mnt/somepoint/

Step 5 – Browse Your VM
You can then just browse the files in mnt/somepoint/

Afterwards unmount:

 sudo umount /mnt/somepoint/

Then disconnect the disk:

sudo qemu-nbd --disconnect /dev/nbd0