Skip to content

Libvirt

Resize a VM

The problem :

I created a virtual machine completely automatic, with a size of 10GB, memory a 8GB and 2CPU's. Now when creating such an image by hand you would stumble upon the partitioning in swap and disk space and it would become apparent that this would end up with a swap partition of 8GB only leaving 2GB for the system.

Automated... It didn't.

But it did become clear when the logs were getting full so we needed to resize this disk to a more manageable size. We can use virt-resize for this. But there are more commands needed and useful. First locate the image involved, by default they are in :

directory
1
2
3
4
cd /var/lib/libvirt
ls

vm_name.img

Chances are you will recognize the disk in question by it's name. In my case it was rather easy being the only one there : vm_name.img.

From the man page :

Virt-resize cannot resize disk images in-place. Virt-resize should not be used on live virtual machines - for consistent results, shut the virtual machine down before resizing it.

So ... shutdown the virtual machine. Virt-filesystems

This tool allows you to discover and list filesystems and layout on vm images, so run it to see what gives :

  • --long : Display extra columns of data ("long format").
  • -h : In --long mode, display sizes in human-readable format.
  • --all : Display everything.
  • -a : add file (to the commandline, you can do multiple)
list filesystems
virt-filesystems --long -h --all -a vm_name.img
output
1
2
3
4
5
6
Name       Type        VFS   Label  MBR  Size  Parent
/dev/sda1  filesystem  swap  -      -    1.0G  -
/dev/sda2  filesystem  ext4  -      -    9.0G  -
/dev/sda1  partition   -     -      82   1.0G  /dev/sda
/dev/sda2  partition   -     -      83   9.0G  /dev/sda
/dev/sda   device      -     -      -    10G   -

So this displays our resizable disk rather well, this is an example disk so it's 1 and 9 GB instead of 2 and 8, but it's got the same difficulties.

truncate

This not a libvirt command but a normal linux command, it can be used to shrink or extend files to the specified size. So first we use it to prepare a disk as a working copy,

  • -r : --reference=FILE : use this file's size
resize
truncate -r vm_name.img vm_name_new_img
  • -s : --size=SIZE : use this size
  • +5G: well..... stop reading if you don't get this
resize
truncate -s +5G vm_name_new_img

These are very quick command since they produce empty files. Test the result so far ? :

test
ls -l
output
-rw------- 1 root root 10737418240 Apr  5 16:51 vm_name.img
-rw-r--r-- 1 root root 16106127360 Apr  5 16:57 vm_name_new_img

virt-resize

Now for the real action, resize the correct filesystems within the disk image :

  • --expand : Expand the named partition so it uses up all extra space
  • vm_name.img : the donor image
  • vm_name_new.img : the new image
virt-resize
virt-resize --expand /dev/sda2 vm_name.img vm_name_new.img

Examining vm_name.img

Summary of changes:

examine image
/dev/sda1: This partition will be left alone.
/dev/sda2: This partition will be resized from 9.0G to 14.0G.  The
    filesystem ext4 on /dev/sda2 will be expanded using the 'resize2fs'
    method.
**********
Setting up initial partition table on vm_name_new_img ...
Copying /dev/sda1 ...
 100% [#########################################################################################################] 00:00
Copying /dev/sda2 ...
 100% [#########################################################################################################] 00:00
Expanding /dev/sda2 using the 'resize2fs' method ...
Fatal error: exception Guestfs.Error("resize2fs: e2fsck 1.41.12 (17-May-2010)
/dev/sda2 has unsupported feature(s): 64bit
e2fsck: Get a newer version of e2fsck!")

Now this last one is kind of logical since we used a centos 6 hypervisor and centos 7 VM's. We are checking a newer EXT4 filesystem with an older version of e2fsck. I leave this part in for reference. So i retried this with a better matching VM's :