Monday, June 14, 2010

Howto extend a KVM virtual disk, using lvm, with larger disk space

Prerequisites:
An installed KVM Ubuntu 10.04 guest with regular disk using LVM (lvm makes the resize operation a bit more tricky).
In this example the original disk was 20 GB and I want to extend it with 20GB more.

  1. Stop guest

  2. Extend imagefile med zeroes to desired size
    - create addon space with the size you want to extend disk with
    sudo qemu-img create -f raw addon.raw 20G
    - make backup of the original disk
    mv ubuntu-dev.img ubuntu-dev.img.save
    - concatenate the extra space on top of the old image
    cat ubuntu-dev.img.save addon.raw >> ubuntu-dev.img

  3. Start guest with gparted live iso
    Partitions might look like:
    Number  Start   End     Size    Type      File system  Flags
    1 1049kB 256MB 255MB primary ext2 boot
    2 257MB 20.5GB 20.3GB extended
    5 257MB 20.5GB 20.3GB logical lvm

    - extend physical partition (2) with gparted (easiest) to fill upp the entire (new) disk

  4. Reboot into guest
    - Remove the logical partition (lvm)
    - Create a new logic partition, with exact same start but new ending
    rm 5; mkpart logical ext2 0 4000; set 5 lvm on

  5. Resize the lvm physical volume (use pvdisplay to find out the device /dev/vda5)
    - pvresize /dev/vda5

  6. Resize the lvm locial volume
    - lvresize -L+4309 /dev/www/root
    repeat the above until you fill up the entire free space, use pvdisplay and lvdisplay to see the free size

  7. fsck the filesystem, the filesystem name is visible when you do lvdisplay
    - fsck -n /dev/www/root

  8. Resize the filesystem to fill up the space
    - resize2fs /dev/www/root

1 comment:

tomas said...

A lot safer is to create a new partition in the new space and just extend the LVM with a new volume.

>qemu-img create -f raw addon.raw 40G
>cat machine.img addon.raw > machine-extended.img

Boot with gparted livecd:
extend physical partition (2) with gparted (easiest) to fill upp the entire (new) disk

Boot into real guest again:

>parted
>select /dev/vda
>print
>mkpart logical ext2 64.2GB 107GB (values so you fill max space)
>set 6 lvm on
>print
>quit

Model: Virtio Block Device (virtblk)
Disk /dev/vda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 1049kB 256MB 255MB primary ext2 boot
2 257MB 107GB 107GB extended
5 257MB 64.4GB 64.2GB logical lvm
6 64.4GB 107GB 42.9GB logical lvm

>pvcreate /dev/vda6
>pvdisplay
>vgextend mail /dev/vda6
>vgdisplay
>lvextend -L+20000 /dev/machine/root (repeat with decreasing values until you filled all space)
>lvdisplay
>resize2fs /dev/machine/root
>fsck /dev/machine/root
>grub-install (just to be sure)
>reboot (and keep your fingers crossed)