Resizing a Virtual (or Guest) Machine on Linux-based Fedora Distribution

Fedora operating system includes several tools for managing KVM, QEMU, and Xen based virtual machines (VMs), also called guest machines/systems. In this article, I will discuss one such tool called “virt-resize” that can be used to resize (expand or shrink) an already existing virtual machine (VM). The need for resizing a VM can arise due to several reasons, but I am not going to discuss them. Also there are several methods to resize a VM, however I found “virt-resize” command really easy to use. Specifically, I am going to discuss how to increase disk size of an existing VM using “virt-resize”. My system has up-to-date Fedora 14 as host machine and RHEL6 (kvm-based) as a guest machine. RHEL6 guest machine has disk image of 6GB that I want to increase to 8GB. Although the instructions provided in this article are tested on the previously stated setup, they should work with other guest operating systems (OS) too as far as the host machine uses the tools described here. Before going further, please make sure, you have “virt-resize” installed on your host machine. “virt-resize” is part of libguestfs-tools package that can be installed on Fedora using yum command (just for note, yum is packager manager on Fedora distribution) as follows (Note: you need to be root user to run all the commands described in this article or should have sudo permissions):

$yum install libguestfs-tools

Now locate the disk image of your VM on your system. Most likely, the VM’s disk image should be in the directory /var/lib/libvirt/images on Fedora-based host machine. I use “virt-manager” GUI (this can be installed by using “sudo yum install virt-manager” if not already installed) for managing my virtual machines. The name of my RHEL6 VM as viewed using virt-manager is “rhel6”, and the name of the its disk image is “rhel6.img” (Really there is no correlation between the VM’s name and its disk image’s name. Both can be different). So the disk image file “rhel6.img” exists in /var/lib/libvirt/images. Please make sure you choose the right image file. Please make sure that your VM is turned off, as virt-resize can only do offline resizing at the moment, and now run the following command on the host machine:

$sudo virt-filesystems –long -h –all -a rhel6.img
Name Type VFS Label Size Parent
/dev/sda1 filesystem ext4 – 500M –
/dev/vg_rhel6/lv_root filesystem ext4 – 3.5G –
/dev/vg_rhel6/lv_swap filesystem swap – 2.0G –
/dev/vg_rhel6/lv_root lv – – 3.5G /dev/vg_rhel6
/dev/vg_rhel6/lv_swap lv – – 2.0G /dev/vg_rhel6
/dev/vg_rhel6 vg – – 5.5G –
/dev/sda2 pv – – 5.5G –
/dev/sda1 partition – – 500M /dev/sda
/dev/sda2 partition – – 5.5G /dev/sda
/dev/sda device – – 6.0G –

The command “virt-filesystems” provides file system information of VMs. The important things to note in the output are that /dev/sda2 is the main partition, and /dev/sda1 is the boot partition. /dev/sda2 is further configured as 2 logical volumes that are “/dev/vg_rhel6/lv_root” and “/dev/vg_rhel6/lv_swap”. /dev/vg_rhel6/lv_root is the root partition and is of size 3.5 GB, whereas /dev/vg_rhel6/lv_swap is the swap partition. The boot partition (/dev/sda1) and the root partition (/dev/vg_rhel6/lv_root) are of ext4 type file systems. Here, our main goal is to increase the size of the root partition. Just to note, that my rhel6 VM was installed using virt-manager, and I selected default options (like partitions types and their size) during the installation. Another point to note is that “virt-resize” does not do in-place resizing, and one needs to create a new disk image of 8GB as follows:

$cd /var/lib/libvirt/images
$truncate -s 8G rhel6-new.img

“truncate” command creates a new file of size 8GB. This command is part of coreutils package on Fedora system, and must exist on your system. Next command is the virt-resize command that will help us achieve our main goal as follows:

$virt-resize –expand /dev/sda2 rhel6.img rhel6-new.img –LV-expand /dev/vg_rhel6/lv_root
Summary of changes:
/dev/sda1: partition will be left alone
/dev/sda2: partition will be resized from 5.5G to 7.5G
/dev/sda2: content will be expanded using the ‘pvresize’ method
/dev/vg_rhel6/lv_root: LV will be expanded to maximum size
/dev/vg_rhel6/lv_root: content will be expanded using the ‘resize2fs’ method
Copying /dev/sda1 …
[############################################################################]
Copying /dev/sda2 …
[############################################################################]
Expanding /dev/sda2 using the ‘pvresize’ method
Expanding /dev/vg_rhel6/lv_root using the ‘resize2fs’ method

The virt-resize command specifically tells that the /dev/sda2 should be expanded to the extra new space, and then the logical volume /dev/vg_rhel6/lv_root should be expanded. Since we do not want to change the size of /dev/sda1 and /dev/vg_rhel6/lv_swap, they remain same and are just copied to the new disk image as shown in the output. The new image “rhel6-new.img” now is a clone of the rhel6.img with size extended to 8GB. The output also shows some of the underlying commands (pvresize and resize2fs) used by virt-resize. In essence, the virt-resize command just automates the process of resizing the virtual machines, and makes it easier for users. Now if you run the following command again:

$virt-filesystems –long -h –all -a rhel6-new.img
Name Type VFS Label Size Parent
/dev/sda1 filesystem ext4 – 500M –
/dev/vg_rhel6/lv_root filesystem ext4 – 5.5G –
/dev/vg_rhel6/lv_swap filesystem swap – 2.0G –
/dev/vg_rhel6/lv_root lv – – 5.5G /dev/vg_rhel6
/dev/vg_rhel6/lv_swap lv – – 2.0G /dev/vg_rhel6
/dev/vg_rhel6 vg – – 7.5G –
/dev/sda2 pv – – 7.5G –
/dev/sda1 partition – – 500M /dev/sda
/dev/sda2 partition – – 7.5G /dev/sda
/dev/sda device – – 8.0G –

You can see the expanded size of /dev/sda, /dev/sda2, /dev/vg_rhel6, and /dev/vg_rhel6/lv_root by 2GB, as we aimed to increased the size of VM to 8GB from 6GB. This new disk image “rhel6-new.img” can be managed and started by importing using “virt-manager”, or can be given a new guest name and started using “virsh” command. Please see their man pages for details. One thing to note is that the above process creates a new image file “rhel6-new.img” (please make sure that this new image is also in the directory /var/lib/libvirt/images), and the old image file “rhel6.img” still exists in the same location /var/lib/libvirt/images. For caution, please boot the new VM (rhel6-new.img) and make sure everything is fine with the new image before deleting the old image.

Useful links:
http://libguestfs.org/virt-resize.1.html
http://libguestfs.org/virt-filesystems.1.html
http://virt-manager.et.redhat.com
http://www.linux-kvm.org
http://wiki.qemu.org

This entry was posted in Technology and tagged . Bookmark the permalink.

One Response to Resizing a Virtual (or Guest) Machine on Linux-based Fedora Distribution

  1. NICOLAS says:

    HI, CONGRATULATIONS BY THE POST….
    I HAVE A QUESTION ABOUT “VIRT-RESIZE”…. THIS FEATURE IS SUPPORTED ONLY RHEL 5.6 AND HIGHER?
    I HAVE RHEL 5.5 AND I DON’T GET INSTALL THE PACKAGE VIA:

    yum install libguestfs-tools

    DOES RHEL 5.5 (X86_64) SUPPORT IT?
    THANKS AND SORRY BY MY “little english” 🙂

    NICOLAS

Leave a Reply

Your email address will not be published.