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

11 thoughts on “Howto resize a libvirt (kvm/qemu) disk image

  1. 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. 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. Great post.Thanks alot..That article helps me out.Before i had some problems with it.
    Greetings from Germany

  4. 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.

  5. There are much much simpler ways exist. Modern coreutils has an utility named truncate – it can “truncate” afile to _larger_ size. So, provided you had image.raw of size N GB, and you want it to be N+M Gb, you use truncate –size $((N+M))G image.raw. _Much_ safer and simpler… ;)

  6. Hey why aren’t you mentioning the partition needs to be resized to fit the new “drive” ?

  7. OK I guess you are but you need to read it a couple of times IMHO to understand that fact..

  8. truncate -s +15G image.raw

    is safest (note the +, which guarantees you will only enlarge the image).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>