Resize qcow2 with blockresize on running VM

So I sent an email to libvirt-users@redhat.com (there are some amazing wizards there!) and got an almost immediate response from Peter:

You can resize a QCOW2 (or any VM disk image) with virsh blockresize VM /path/to/image.qcow2 SIZE'.

This requires the VM to be running.

virsh blockresize ISPA /var/lib/libvirt/images/ispa.qcow2 300G

Result:
jeff@ispa-mx ~ $ sudo fdisk -l
[sudo] password for jeff:  
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors

 

Next, Install gdisk and cloud utils package for growpart, a Linux command line tool used to extend a partition in a partition table to fill available space.

apt install cloud-utils gdisk

Problem is that often/usually, in our case, the swap partition sits at the end of the disk, so it needs to be removed and added back after growing the primary data partition sda1.

root@ispa-mx:~# lsblk 
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   300G  0 disk 
├─sda1   8:1    0 212.2G  0 part /
└─sda2   8:2    0   7.8G  0 part [SWAP]

 Turn swap OFF.

swapoff -a

EDIT /etc/fstab and comment it OUT as a reboot may be required and it would hang the system because after recreating the partition the UUID will have changed!

Overview to end: Run fdisk, delete the swap, then recreate the swap partition at the end of the disk where it belongs. Write fdisk changes and run partprobe to get the kernel reinitialised with the change. 

Then:

growpart /dev/sda 1 #Note the space

resize2fs /dev/sda1

Then fix the swap partition:

mkswap /dev/sda2 

Setting up swapspace version 1, size = 7.8 GiB (8389423104 bytes)
no label, UUID=5a00a73e-a01c-4900-8a63-ae76c42fa61f

Turn swap back on:

swapon -a

Note the new UUID. Edit /etc/fstab and update the swap UUID.

Fin.

Category