Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

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 simply 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
share|improve this answer
    
Thanks for the quick answer. It looks like a viable solution. I'll be out of town for a few days, but once I am back to office next week, I'll give it a try and report back if it works. –  Tong Wang Feb 24 '12 at 13:58
1  
One more question: if I want to shrink the disk, do I perform the steps in a reversed order? Like: 1. shrink the file system; 2. shrink the partition; 3. shrink the disk image. Right? –  Tong Wang Feb 24 '12 at 14:40
    
Yes, resize2fs can shrink as well as grow, but it'll obviously need enough free space in the filesystem. When shrinking the partition I'd err on leaving a bit more than necessary so you don't accidentally chop the end off the filesystem :) –  Caesium Feb 24 '12 at 15:03

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% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ --:--
**********

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% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 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:

# 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 simply boot !

For more info: man virt-resize

share|improve this answer

I think Caesium's answer is fine, I'd just like to write down some other commands to achieve the same thing.

Assume you have a file disk.img with a disk image, i.e. it has a partition table and one or more partitions, and say you want to make the last partition larger. What you have to do is to

1) make the whole file bigger, say 4GiB. A quick way to do it is to use dd

dd if=/dev/zero of=disk.img bs=1c seek=4G count=0

2) make the partition bigger using fdisk (I wish I could do this in parted or some nicer tool... Anyone?)

fdisk disk.img

Type p to print out the partiton table and look for the start sector of your partition, for instance partition 2 starts at sector 106496.

What you will do is to delete that partition from the partition table and create a new partition that starts at the exact same sector but ends at a later one. Then the partition will contain a valid file system.

Type d and give the partition number to delete. (Gulp!)

Type n and give the desired partition number, and then the start sector. You MUST use the same start sector as was used before. Finally give the end sector or just let fdisk pick the highest sector you can use.

Type w to write the changes back to the disk image file, and exit fdisk.

3) Now you need to resize the filesystem. For this you need the offset to (= the position of the) file system inside the disk image. You can compute this from the sector number if you know the sector size (which is usually 512), or you can use parted

parted disk.img u b p

(parted can take commands as command line arguments, so this means 'unit bytes' and 'print', so it prints the partition table and uses bytes as size uint.)

This prints out the partition starts and ends. Say that your partition starts at 54525952, then you make a loopback block devices with losetup.

losetup -f --show -o 54525952 disk.img 

losetup tells you which dev it picked, for instance /dev/loop0. Now you can use resize2fs:

resize2fs /dev/loop0

and finally remove the loop device

losetup -d /dev/loop0
share|improve this answer

Best solution found is here:

http://www.linux-kvm.com/content/how-resize-your-kvm-virtual-disk

I think what @Caesium said is given from the above official link

I hope this will help.

3 Steps:

1.qemu-img resize windows.qcow2 +5GB

2.GParted live cd to resize

3.reboot and use os tools to resize
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.