3 Extending the image file

First, we need to extend the actual image file. This cannot be done easily with a QCOW2 file because QCOW2 is based on copy-on-write. As a result, a QCOW2 file only store sectors that were written back to the virtual disk.

As a result, to extend an image file, it is necessary to convert a QCOW2 file to a raw file first. A raw file is, byte-by-byte, a direct dump of a physical disk. The following command converts a QCOW2 file to a raw file:

qemu-img convert -f qcow2 -O raw hd1.qcow2 hd1.raw  
  

This command assumes that hd1.qcow2 is the original hard disk image file encoded in QCOW2, and hd1.raw is the new raw disk image file converted from hd1.qcow2.

Note that qemu-img may be in a subfolder bin of the main QEMU folder, especially in version 0.9.0 of QEMU. In that case, you need to use bin\qemu-img in place of qemu-img.

Next, you need to create another raw file that represents the extension to the disk image. You can do this using the following command:

qemu-img create -f raw extension.raw 512MB  
  

This command creates a file that is 512MB. Change this number depending how much you want to extend the original partition.

The last step of this operation creates a new file based on the two raw files created in the previous steps. This is the command to use in DOS/Windows:

copy /b hd1.raw+extension.raw hd1-big.raw  
  

In Linux, use the following command instead:

cat hd1.raw extension.raw > hd1-big.raw  
  

This command creates a new image file that is extended from the old file.

Now, change the batch file to use hd1-big.raw instead of hd1.qcow2.