Saturday, July 2, 2011

Expanding a KVM guest disk image using virt-resize

Decide on how you wish to expand the guest. Run virt-df -h and virt-list-partitions -lh on the guest disk, as shown in the following output:

# virt-df -h /dev/vg_kvm/kvm7
Filesystem                                Size       Used  Available  Use%
/dev/vg_kvm/kvm7:/dev/sda1              484.2M      48.8M     410.4M   11%
/dev/vg_kvm/kvm7:/dev/sda3               2.5G       1.3G      25.4G    5%

# virt-list-partitions -lh  /dev/vg_kvm/kvm7
/dev/sda1 ext4 500.0M
/dev/sda2 swap 1.0G
/dev/sda3 ext4 2.5G

This example we will increase the size of /dev/sda3 to 30G

1. Shutdown guest

2. Rename the original disk for the backup purpose, for lvm use lvrename command, for file use 'mv' command

# lvrename /dev/vg_kvm/kvm7 /dev/vg_kvm/kvm7.backup
  Renamed "kvm7" to "kvm7.backup" in volume group "vg_kvm"

3. Create new disk
# lvcreate -L 30G -n kvm7 vg_kvm
Logical volume "kvm7" created

4. Now resize the /dev/sda3, refer to virt-resize man for detail.

# virt-resize \
       /dev/vg_kvm/kvm7.backup /dev/vg_kvm/kvm7 \
       --expand /dev/sda3
Summary of changes:
/dev/sda1: partition will be left alone
/dev/sda2: partition will be left alone
/dev/sda3: partition will be resized from 2.5G to 28.5G
Copying /dev/sda1 ... done
Copying /dev/sda2 ... done
Copying /dev/sda3 ... done

Parition /dev/sda3 contains a filesystem or LVM PV, then this content is not automatically resized.  You can resize it afterwards either using guestfish(1) (offline) or using commands inside the guest (online resizing).

Here we resize the filesystem using guestfish

[root@kvm ~]# guestfish -a /dev/vg_kvm/kvm7

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for help with commands
      'quit' to quit the shell

> run
> resize2fs /dev/sda3
libguestfs: error: resize2fs: resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/vda3' first.
> e2fsck-f /dev/sda3
> resize2fs /dev/sda3
> exit

[root@kvm ~]# virt-df -h sl6-5
Filesystem                                Size       Used  Available  Use%
sl6-5:/dev/sda1                         484.2M      48.8M     410.4M   11%
sl6-5:/dev/sda3                          28.1G       1.3G      25.4G    5%


Try to boot the virtual machine. If it works you can delete the backup disk.

# lvchange -an vg_kvm/kvm7.backup

# lvremove vg_kvm/kvm7.backup
  Logical volume "kvm7.backup" successfully removed

Clone KVM with virt-clone

virt-clone is a command line tool for cloning existing virtual machine images using the "libvirt" hypervisor management library. It will copy the disk images of any existing virtual machine, and define a new guest with an identical virtual hardware configuration. Elements which require uniqueness will be updated to avoid a clash between old and new guests

virt-clone of python-virtinst package is a great tool for creating cloned KVM virtual machine quickly. 

Example:  LVM based kvm

# virt-clone --original sl6-3  \
             --name sl6-4 \
              --file /dev/mapper/vg_kvm-kvm6 --prompt


In the above example /dev/mapper/vg_kvm-kvm6 LVM should be created before executing the virt-clone command.

Creating LVM
# lvcreate -L 4G -n kvm6 vg_kvm

Example file based storage
virt-clone \
              --original ubuntu11 \
              --name ubuntu-2 \
              --file /var/lib/libvirt/images/ubuntu-2.img

--original sl6-3 = name of the existing KVM (make sure the original KVM is shutdown before creating clone out of it)

--name se6-4 = Name of the New KVM (cloned)
--file = In LVM case path to the existing storage, in file based storage the file will be automatically created by virt-clone

Troubleshooting Network issue
Upon booting the new cloned KVM VM I encounter with problem of not showing any network interface. After digging more it appears that udev on virtual machine changed the eth0 interface with eth1 due to change in MAC of the interface. Quick search on Google suggested the following solution to get back the 'eth0' interface on cloned vm.

1. Open /etc/sysconfig/network-scripts/ifcfg-eth0 and delete the following line and save the file

HWADDR=52:54:00:8f:62:01

2. Remove the following filen and reboot the virtual machine

/etc/udev/rules.d/*persistent-net*'

After reboot eth0 network interface will be available.