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 :
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 | |
|---|---|
| output | |
|---|---|
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 | |
|---|---|
- -s : --size=SIZE : use this size
- +5G: well..... stop reading if you don't get this
| resize | |
|---|---|
These are very quick command since they produce empty files. Test the result so far ? :
| test | |
|---|---|
| output | |
|---|---|
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 | |
|---|---|
Examining vm_name.img
Summary of changes:
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 :