Tag Archives: hard drive image

Growing a KVM raw hard drive image file

I’m posting this as a reminder to myself the next time I need to make a raw KVM hard drive image bigger.

<disclaimer>
This worked for me – it may not for you. Make sure you have a backup of the image file before starting. I believe this will only work for raw type images… using it on qcow2, vmdk or any other type will probably cause a corrupted file. You have been warned…
</disclaimer>

You’ll need to know how big you want the final image to be in bytes. This command will grow the file to the size you want without actually allocating the full size.

dd if=/dev/zero of=file-to-grow.img bs=1 seek=(number of bytes - 1) count=1
Update, this command is more concise and does the same thing:
dd of=file-to-grow.img bs=1 seek=(number of bytes) count=0

You can also use shorthand values like 20G or 512M or 2T for number of bytes. Mind your abbreviations – dd sees G as 1024*1024*1024, but sees GB as 1000*1000*1000.

The file will then be allocated as the VM writes to it. You can check the allocated size of a file with ls -lsh. (the first column is the allocated size)
Note that this command does not expand the partition(s) or file system(s) on the hard drive image – you’ll need to use another tool like Gparted (available on the Ubuntu live CD) for this task.
You may need to use sudo in front of the command, depending on your setup.