How to resize virtual machine disk?

Is there anyway to resize a virtual machine's disk? Say increasing the disk size from 32GB to 64GB. I am running KVM/Qemu on Ubuntu server 11.10 64bit. Thanks.


Solution 1:

On Debian based distro you should use virt-resize instead. This handle pretty much everything under the hood now. Let's assume your image is called Win7 (why not?). First thing make sure your VM is shut down:

Install the tool:

# apt-get install libguestfs-tools

Get the location of your VM disk:

# virsh dumpxml Win7 | xpath -e /domain/devices/disk/source
Found 2 nodes in stdin:
-- NODE --
<source file="/var/lib/libvirt/images/Win7.img" />
-- NODE --
<source file="/var/lib/libvirt/images/Win7.iso" />

You may need to adapt /var/lib/libvirt/images/Win7.img in the following:

# virt-filesystems --long --parts --blkdevs -h -a /var/lib/libvirt/images/Win7.img
Name       Type       MBR  Size  Parent
/dev/sda1  partition  07   100M  /dev/sda
/dev/sda2  partition  07   32G   /dev/sda
/dev/sda   device     -    32G   -

Create your 64G disk:

# truncate -s 64G /var/lib/libvirt/images/outdisk

You'll need to expand /dev/sda2 (not the boot partition):

# virt-resize --expand /dev/sda2 /var/lib/libvirt/images/Win7.img /var/lib/libvirt/images/outdisk
Examining /var/lib/libvirt/images/Win7.img ...
 100% [progress bar] --:--
**********

Summary of changes:

/dev/sda1: This partition will be left alone.

/dev/sda2: This partition will be resized from 32G to 64G.  The 
    filesystem ntfs on /dev/sda2 will be expanded using the 
    'ntfsresize' method.

**********
Setting up initial partition table on outdisk ...
Copying /dev/sda1 ...
Copying /dev/sda2 ...
 100% [progress bar] 00:00
 100% [progress bar] 00:00
Expanding /dev/sda2 using the 'ntfsresize' method ...

Resize operation completed with no errors.  Before deleting the old 
disk, carefully check that the resized disk boots and works correctly.

Make a backup just in case (or use mv if you do not want the backup):

# cp /var/lib/libvirt/images/Win7.img /var/lib/libvirt/images/Win7.img.old
# mv /var/lib/libvirt/images/outdisk /var/lib/libvirt/images/Win7.img

Now boot !

For more info: man virt-resize

Solution 2:

I recommend before doing any of this you take a complete copy of the disk image as it is, then when it all breaks you can copy it back to start over.

There's 3 things you need to do:

1) Make the disk image bigger. In your host:

qemu-img resize foo.qcow2 +32G

Now your guest can see a bigger disk, but still has old partitions and filesystems.

2) Make the partition inside the disk image bigger. You need to boot off a LiveCD in your guest for this, since you won't be able to mess with a mounted partition. This is quite involved and probably the most dangerous part. It's quite a lot to copy here, so I'll just link instead for now. You want to do something like this:

http://www.howtoforge.com/linux_resizing_ext3_partitions_p2

OR 2b) creating a new partition would be simpler (and safer) if you just want more storage space. Use fdisk or cfdisk, or whatever you feel comfortable with - you should see a whole bunch of unallocated space on your guest disk now.

3) Finally, if you resized your existing partition, make the filesystem inside the new bigger partition bigger (this is actually in the guide linked above anyway). Inside your guest:

resize2fs /dev/sda1