KVM Guest Disk Resizing

I ve been using KVM for virtualization. Sometimes we need to resize our VM’s disk. We can either expand or shrink. Below I am going to describe how we can do it.

Expand

The KVM guests I been using are stored in RAW format. There is also qcow2 format.
First of all we need to see which partition we need to add the space to.
We are going to use the tool virt-filesystems
If it this is not installed then you need to install libguestfs-tools
On CentOS systems execute

# yum install libguestfs-tools

Go to the directory where KVM images are stored. The default place is /var/lib/libvirt/images/
If we want to resize ubuntu12.img then execute

# virt-filesystems --long -h -a ubuntu12.img
Name       Type        VFS   Label  Size  Parent
/dev/sda1  filesystem  ext4  -      15G   - 

As you can see we are going to resize /dev/sda1 partition of our VM.

Terminate the VM

# virsh destroy ubuntu12

Keep a backup of your image file

# cp ubuntu12.img ubuntu12.img.bck

Create an file as big as the additional space you would like to add (in this example 3 GB)

# qemu-img create -f raw additional.raw 3G

Append the raw additional space to the end of your raw image:

# cat additional.raw >> ubuntu12.img
# truncate -r ubuntu12.img ubuntu12.img.new
# virt-resize --expand /dev/sda1 ubuntu12.img ubuntu12.img.new
Examining ubuntu.img ...
**********
Summary of changes:
/dev/sda1: This partition will be resized from 14.6G to 17.6G.  The
filesystem ext4 on /dev/sda1 will be expanded using the 'resize2fs'
method.
/dev/sda2: This partition will be left alone.
**********
Setting up initial partition table on ubuntu12.img.new ...
Copying /dev/sda1 ...
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Copying /dev/sda2 ...
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Expanding /dev/sda1 using the 'resize2fs' method ... 

Move the new image ubuntu12.img.new to the actual image

# rm -rf ubuntu12.img
# mv ubuntu12.img.new ubuntu12.img

Last step is to start the VM and check if the resize was successful

# virsh start ubuntu12 --console

In the method described above no LVM is used and neither qcow2 format is used. If you need to convert raw to qcow2 or the opposite use the tool qemu-convert
It seems to be working on Ubuntu 12 guests and not on CentOS 5.x.

Links
http://tuxdna.wordpress.com/2011/11/29/how-to-resize-a-vm-and-its-partitions/
http://michael.orlitzky.com/articles/resizing_a_kvm_or_qemu_disk_image.php
http://blog.pebblecode.com/post/51229954039/expanding-and-resizing-qemu-virtual-disks
http://n40lab.wordpress.com/2013/05/29/resizing-a-qemu-kvm-linux-image-using-virt-resize-in-centos-6-4/

Shrink

Our ubuntu12 image was very big so we had to shrink its main partition from 26GB to 10GB

Terminate the VM

# virsh destroy ubuntu12
# virt-df ubuntu12.img
Filesystem                           1K-blocks       Used  Available  Use%
ubuntu12.img:/dev/sda1                10321208    1186952    8609972   12% 

Use guestfish tool

# guestfish -a ubuntu12.img
Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.
Type: 'help' for help on commands
'man' to read the manual
'quit' to quit the shell
><fs> run
><fs> list-filesystems
/dev/sda1: ext4
/dev/sda2: unknown
/dev/sda5: swap
><fs> e2fsck-f /dev/sda1
><fs> resize2fs-size /dev/sda1 10G
><fs> exit 
# truncate -s 14G ubuntu12.img.new
# virt-resize --resize /dev/sda1=-16G ubuntu12.img ubuntu12.img.new
Examining ubuntu12.img ...
**********
Summary of changes:
/dev/sda1: This partition will be resized from 26.1G to 10.1G.  The
filesystem ext4 on /dev/sda1 will be expanded using the 'resize2fs'
method.
/dev/sda2: This partition will be left alone.
There is a surplus of 799.0K.  The surplus space is not large enough
for an extra partition to be created and so it will just be ignored.
**********
Setting up initial partition table on ubuntu12.img.new ...
Copying /dev/sda1 ...
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Copying /dev/sda2 ...
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Expanding /dev/sda1 using the 'resize2fs' method ...
Resize operation completed with no errors.  Before deleting the old
disk, carefully check that the resized disk boots and works correctly.

ubuntu12.img.new now is shrinked and ready to be used

Links
http://ghost.thekindof.me/resizing-kvm-vm-storage-image-lvm-virt-resize-shrink/
http://rwmj.wordpress.com/2010/09/27/virt-resize-shrink-now-works/
http://www.blog.turmair.de/2010/11/how-to-shrink-raw-qemu-kvm-images/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s