How-to grow any Qemu system image

Qemu images can’t be growed. In this exemple I will show you a little hack to grow a 6GiB qcow image to a 10GiB image. Beware: these operations can take a lot of time to perform and require lots of free space.

First, convert your qcow image to a plain raw file:

qemu-img convert system.qcow -O raw system.raw

Then, create a dummy file (filled with zeros) of the size of extra space you want to add to your image. In this case, 4GiB (=10GiB – 6GiB):

dd if=/dev/zero of=zeros.raw bs=1024k count=4096

Fearlessly, add your extra space to your raw system image:

cat system.raw zeros.raw > big10G.raw

After that you can boot qemu to verify that added free space is available:

qemu -hda big10G.raw

Here is an real case exemple of what you can see in a qemu image on which Windows XP was installed:
Windows XP in Qemu - Primary Disk Extra Free Space

Now, to grow your primary partition, I suggest you to download a Live CD like gparted Live CD or System Rescue CD, and boot on the .iso file with qemu:

qemu -hda big10G.raw -cdrom gparted-livecd-0.3.4-5.iso -boot d

This will allow you to grow and manipulate all your partitions safely thanks to parted and other open source system tools.

Finally you can convert back your raw image to a qcow one to not waste space:

qemu-img convert big10G.raw -O qcow growed-system.qcow

That’s all !

By the way, I think it’s possible to perform the second and third step of this how-to in a single operation using dd only.

Update: I missed it, but this issue is also described in the FAQ from the unofficial #qemu wiki (look at “How do I resize a disk image?” question).

10 Responses to “How-to grow any Qemu system image”


  • Thanks for putting this up. I’ve tried this a few times but after I cat my raw files together it no longer boots:

    # qemu winXP_pro-test.img
    

    - works fine

    # qemu-img convert winXP_pro-test.img -O raw system.raw
    # qemu system.raw
    

    - came up to a safe mode prompt the first time, boots normally now.

    # qemu-img info system.raw
    image: system.raw
    file format: raw
    virtual size: 2.0G (2097152000 bytes)
    disk size: 1.0G
    # qemu-img info zeros.raw
    image: zeros.raw
    file format: raw
    virtual size: 6.0G (6442450944 bytes)
    disk size: 6.0G
    # cat system.raw zeros.raw > winXP_8G.raw
    # qemu-img info winXP_8G.raw
    image: winXP_8G.raw
    file format: raw
    virtual size: 8.0G (8539602944 bytes)
    disk size: 8.0G
    # qemu winXP_8G.raw
    Bochs says:  A disk read error has occurred.
    

    If I run an image image of partedmagic-1.6 it shows the partition correctly and lets me resize it – but resizing a non bootable image doesn’t mean a whole lot. My guess is the problem has something to do with the virtual size: 2.0G disk size: 1.0G of my raw file. That’s what qemu-img gives me converting from cow format. XP shows the drive as being 2GB when I run the original image.

  • I’ve never encounter your problem during my experiments, and I have no idea how to fix your problem. Sorry…

    I didn’t know the qemu-img info command. I don’t know if this can help you, but the only raw image file I still have return me this:

    [kevin@localhost qemu]$ qemu-img info qemu-winXP.raw
    image: qemu-winXP.raw
    file format: raw
    virtual size: 10G (10737418240 bytes)
    disk size: 9.0G
    

    As you can see, like you, I have the same 1G offset…

  • If you have the image in a raw file and the host filesystem is supporting files with holes a simpler, quicker and less space-wasting way to increase your virtual disk size is:

    dd if=/dev/zero of=system.raw bs=1 count=1 seek=10G
    

    In this way dd leaves a hole of 10G from the beginning of the file (leaving everything untouched) and then writes a 0.

  • For details on “A disk read error has occurred.” please check this post: http://qemu-forum.ipi.fi/viewtopic.php?p=12362

    The complete procedure would now be

    1- Convert to a raw image

    qemu-img convert system.qcow -O raw system.raw
    

    2- Increase the size of the raw image

    dd if=/dev/zero of=system.raw seek=N obs=1GB count=0
    

    where N is the number of GigaBytes

    3- Fix the NTFS BPB
    Edit the raw file: hexedit system.raw
    Goto to offset 7E00: 7E00
    Change 80 to FF at offset 7E1A and save: FF --> Ctrl-x --> y

    4- Boot Windows and check that you see a bigger disk in the admin tools

    qemu hda system.raw
    

    5- Use (fantastic) gparted to resize your NTFS partition

    qemu -hda system.raw -cdrom gparted-livecd-0.3.4-5.iso -boot d
    

    6- Boot windows to check and perform a chkdsk

    qemu hda system.raw
    

    7- If you need to save space, convert back to qcow

    qemu-img convert system.raw -O qcow growed-system.qcow
    
  • Anonymous
    cat system.raw zeros.raw > big10G.raw
    

    shouldn’t that be:

    cat system.raw zeros.raw >> big10G.raw
    

    Maybe this is why people are having trouble with it?

  • The reason that cat system.raw zeros.raw > big10G.raw works is because you’re cat’ing the contents of both files before redirecting them to big10G.raw

    In THEORY you should be able to do cat zeros.raw >> system.raw

    but don’t quote me on that or blame me if it screws up your image :-)

  • I tried

    cat zeros.raw >> system.raw
    

    and it works!

    At least for me, it’s an old linux ext2 image.

    I also tried a gparted twist without the liveCD:

    losetup /dev/loop1 system.raw
    gparted /dev/loop1
    

    it can see it, but cannot resize it :(

    e2label: Bad magic number in superblock ...
    
  • hello!
    Great tutorial, but how i do it in windows?

  • Install a qemu guest with linux and do it :P

    Serious (or not) do not know!

    Great tutorial btw, thanks

  • tartanandsnarly

    Hi,
    Just to say that I accomplished this on windows by replacing the dd and cat steps – with the windows type command…..

    I created the raw image:

    CMD> qemu-img convert disk.qcow -O raw rawdisk1.raw
    

    then created a copy of that:

    CMD> copy rawdisk1.raw rawdisk2.raw
    

    then used that to create a 3rd disk:

    CMD> type rawdisk1.raw rawdisk2.raw > rawdisk3.raw
    

    rawdisk3 could then boot normally both using qemu (in which case it was in the same condition as rawdisk1) and using the gparted iso – in which case the extra space could be seen.

    I manipulated the partitions using gparted and grew the disk.

    …as they say in Scotland – nae bother!

Leave a Reply

Additional comments powered by BackType