Is it possible to resize a QEMU disk image?

66,674

Solution 1

From here:

!!! Back up your disk image before trying the below !!!

If you are using a sparse raw image, then do

dd if=/dev/zero of=hdd.img seek=N obs=1MB count=0

where hdd.img is the raw format image that you want to resize and N is the new size that you want the image to be, in megabytes. To change the units of N, change obs to something else such as 1GB for units in gigabytes (1000x1000x1000).

If you want to resize a raw image but you do not want it to become sparse (you actually want those zeros in the file) then do "dd if=/dev/zero of=image seek=S count=N-S obs=1" instead, where N is the new size and S is the old size (in bytes).

If you want to resize a qcow2 image, this is not yet supported.

this email shows some experimenting with resizing qcow images with a hex editor.

Resizing or growing images in other formats (VMware, Bochs, cow, or cloop) is not supported to the best of my knowledge.

On a Windows host it is possible to resize a raw format disk image using the 'copy' command. You can use qemu-img to convert your existing image to raw format if need be. We will use a temporary raw format disk image that will be appended on to the end of your existing raw format disk image. The size of this temporary image is the size the existing image will be expanded by:

qemu-img create -f raw temp.img 300M

You should then issue the below command - orig.img is your existing raw format image that you want to make larger, temp.img is the temporary image file created earlier, and new.img is the resized resultant image:

copy /b orig.img+temp.img new.img

You will then need to repartition and resize the existing partition(s) and filesystem(s) on the new image. One method of doing this is to boot gparted in QEMU with the gparted livecd iso and the new disk image.


Other links you might want to check out if the above doesn't work:

http://qemu-forum.ipi.fi/viewtopic.php?p=12362
http://kev.coolcavemen.com/2007/04/how-to-grow-any-qemu-system-image/
http://bryan-murdock.blogspot.com/2007/12/resize-qemukvm-windows-disk-image.html
http://www.larsen-b.com/Article/329.html
http://www.brabbel.net/wp/archives/174

Solution 2

A qcow2 image can be resized to grow with a new/current version of qemu. For example, i have a arch.qcow2 thats 2G and I want it to become 50G, in that case i type:

qemu-img resize arch.qcow2 50G

then qemu tells me:

image resized

thats it, I just did this today. redhat actually has some nice docs on qemu:

Solution 3

Short answer for 2017: To add e.g. 30 GB to an existing raw image I just used this command:

qemu-img resize nameofimg.img +30G

This adds 30 GB to your existing image file (no need to create a new file). Then in your guest VM you can extend your existing partitions, in Windows 10 e.g. with "Disk Management", easy.

More info and options:

man qemu-img

See also: qemu wiki > qemu-img

Solution 4

Yes you can. And no it will not change the partitions or table. The partition table may need to be updated to have the full disk size, and there will be empty unused space at the end if you grow it, and you will chop a partition and lose it or the last part of its data if you shrink it.

If you are in the qemu monitor (or use QMP probably), and the interface supports it (such as virtio-scsi-pci with rbd which I tested here), then without rebooting the VM, you can do this:

(qemu) info block -v disk1
disk1 (#block165): rbd:rbd/manjaro (raw)
    Cache mode:       writeback

Images:
image: rbd:rbd/manjaro
file format: raw
virtual size: 4.0G (4294967296 bytes)
[...]

(qemu) block_resize disk1 5120

And poof, the image is resized to the size you specified in MiB, and the VM will show the new size.

I tested the following on qemu 2.7.0:

  • RBD from Ceph - works, fast, grow or shrink
  • qcow2 disk file - works, slower, grow only
  • raw disk file - works, fast, grow or shrink
  • raw LVM disk - in monitor looks normal but has no effect
Share:
66,674

Related videos on Youtube

Richie Marquez
Author by

Richie Marquez

Updated on September 17, 2022

Comments

  • Richie Marquez
    Richie Marquez over 1 year

    If possible, what happens to the partitions within it? Are they automatically resized as well (doubtful), or is there just a new block of unused space following them?

  • Yamaneko
    Yamaneko about 11 years
    However, the space increased will be unnallocated. You will need to resize your virtual disk using some partition tool.
  • HDave
    HDave about 10 years
    qemu-img version 1.0 tells me qemu-img: This image format does not support resize
  • cloaked1
    cloaked1 over 4 years
    Appreciate the table at the bottom. That was super helpful.
  • trysis
    trysis over 3 years
    If you want to resize a qcow2 image, this is not yet supported. - Is this still true, 11 years later?
  • Aaron Franke
    Aaron Franke over 2 years
    How do I edit the partitions to fill the space in this new resized disk?
  • Aaron Franke
    Aaron Franke over 2 years
    How do I open the disk image in a partition editor on my host OS? When I try to grow the partition in my guest it doesn't work. sudo resize2fs /dev/vda1 gives The filesystem is already 6130935 (4k) blocks long. Nothing to do!
  • Kamil Maciorowski
    Kamil Maciorowski about 2 years
    Welcome to Super User. Link-only answers are low quality, they will be useless if the links break. In general we encourage to edit and add the relevant fragments as citations, so the answer is standalone.
  • clem
    clem about 2 years
    Ok, thanks for this precisions