Resize KVM qcow2 image live

KVM VMs backed by qcow2 image can be live resized with the virsh blockresize command.

Example, we had a VM with only 10 GB root disk:

[root@VM ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vda    252:0    0   10G  0 disk
├─vda1 252:1    0    1M  0 part
├─vda2 252:2    0  200M  0 part /efi
│                               /boot/efi
├─vda3 252:3    0    1G  0 part /boot
└─vda4 252:4    0  8.8G  0 part /

From the hypervisor, we then resized the qcow2 image:

[root@hypervisor ~]## virsh blockresize VMname /path/to/image.qcow2 128G
Block device '/path/to/image.qcow2' is resized

then back in the VM, we could immediately see that the disk is now the new size:

[root@VM ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vda    252:0    0  128G  0 disk
├─vda1 252:1    0    1M  0 part
├─vda2 252:2    0  200M  0 part /efi
│                               /boot/efi
├─vda3 252:3    0    1G  0 part /boot
└─vda4 252:4    0  8.8G  0 part /

and then file system could be resized by growing partition and file system:

[root@VM ~]# growpart /dev/vda 4
CHANGED: partition=4 start=2510848 old: size=18460639 end=20971486 new: size=265924575 end=268435422

[root@VM ~]# xfs_growfs /dev/vda4
meta-data=/dev/vda4              isize=512    agcount=4, agsize=576895 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=1 inobtcount=1 nrext64=0
data     =                       bsize=4096   blocks=2307579, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=16384, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2307579 to 33240571

[root@VM ~]# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda4       127G  8.6G  119G   7% /`

We were probably lucky in that this was the last partition. Guess it wouldn’t have been that easy to resize the earlier partitions…