Howto resize a libvirt (kvm/qemu) disk image

I’m using kvm for a while at work. Everything works quite fine, but today I needed to grow a disk image. I found some informations, but none are really clear so here the result :

First create a empty image file .. with this command (don’t use dd,  qemu-img is really quicker than dd):

qemu-img create -f raw temp.img 10G

Next simply your image file + the temp one, in a biggest one ..

cat foo.img temp.img > bar.img

You will get a new image file which is 10G bigger than the original one .. Now you can boot your OS, and discover (via cfdisk for example), that your system has a additionnal 10G unused space .. So next step:

  • Just create a new partition, and mount it in the normal way
  • Boot your kvm OS from a ISO file containing Gparted

I tried the second approach, and used a ubuntu install to boot (using virt-manager, this is really easy to do). And resized the partition to my need .. simply reboot and “tada” :)

Enjoy disk ?

Related posts :

admin April 28th, 2009


4 Responses to “Howto resize a libvirt (kvm/qemu) disk image”

  1. Marius Gedminason 28 Apr 2009 at 7:51 pm

    Can you do it faster by replacing the second step with

    cat temp.img >> foo.img

    ?

    Can you do it even faster by enlarging the first file in-place with

    dd if=/dev/null of=foo.img bs=1b seek=20G count=0

    (in this case the 20G is the new total size, not the increment!)?

  2. adminon 28 Apr 2009 at 9:18 pm

    For Marius:

    cat temp.img >> foo.img

    Yes, you can do this, but this would delete the backup too ..

    For the second way, I don’t think this will work because enlarge it with /dev/zero (not null, i think).. and this not a valid partition..

    Bye

  3. Dalam Hension 09 Jun 2009 at 11:28 pm

    Great post.Thanks alot..That article helps me out.Before i had some problems with it.
    Greetings from Germany

  4. ephemienton 11 Feb 2010 at 6:28 am

    Much faster way, using dd:

    dd if=/dev/zero of=foo.img bs=1 count=1 seek=$(($(stat -c%s foo.img)+10*1024**3-1))

    Obviously your method (and this) only work when dealing with raw images.

Comments RSS

Leave a Reply