Wednesday, 17 October 2012

Easy way to resize raw or qcow2 image files in Linux

What:

KVM or Xen on Linux

Problem:

Easy way to expand your raw, qcow2 virtual hard drive images.

Solution:

Shut down your virtual guest gracefully and make a backup of your disk.img file

Option 1 - raw & qcow2
===============================

To expand your drive by 10GB use a below command, it will add unallocated space to the end of your drive.
qemu-img resize disk.img +10G
  • disk.img can be in raw or qcow2 format
Check your new image size:
ls -lsh
If your virtual operating system is Windows 2008 (Vista) or above, go to disk manager and expand your drive, otherwise use live cd with gparted.

Option 2 - raw
===============================

Add to the end of the disk empty space
dd of=disk.img bs=1 seek=50G count=0
of - is your original image file
seek - new file size (original + added space)

Use values like 512M, 50G or 1T for number of bytes.
dd sees G as 1024*1024*1024, but GB as 1000*1000*1000

Check your new image size:
ls -lsh
s - will show you a real space occupied by file on a hard drive

If your virtual operating system is Windows 2008 (Vista) or above, go to disk manager and expand your drive, otherwise use live cd with gparted.

Option 3 - raw with preallocated space
===============================

To preallocate space while expanding drive use "cat" (it is more time consuming process, but benefit is that file is not getting fragmented with time)

Create new image file first:
qemu-img create -f raw expand.img 10G
or
 dd if=/dev/zero of=expand.img bs=1 seek=10G count=0
Add expand.img to your existing image:
cat expand.img >> disk.img
Check new size:
ls -lsh

No comments:

Post a Comment