KVM - The Linux Kernel-Based Virtual Machine
News, Blogs and Resources on the Linux (KVM) Kernel-Based Virtual Machine

How to resize your kvm virtual disk

In this post I'll walk through the procedure for resizing your virtual disk in place without having to create a copy of it or performing any kind of cloning. If you've ever been in a situation where you have used up almost all of your guest virtual disk space and you need to increase the size of your virtual disk, you'll find this procedure useful. I needed to do this myself and realized that I could find very little documentation of this anywhere online. In looking through kvm documentation I discovered a neat little feature that was added about a year ago in April 2010. The only caveat is that this procedure will only work with raw and qcow2 disk formats.

In this example, I'll be resizing a virtual disk for a windows xp guest but this will work with any guest OS. In order to carryout this procedure, the only thing required is another Linux virtual machine running a recent version of gparted. Really though, you can use another type of virtual machine with disk partitioning tools such as partition magic on windows but I chose gparted on linux as it is the most widely available free tool that will do the job. Below outlines the 3 steps.

 

1.  Resize your virtual disk

Before beginning this procedure I would recommend backing up any important data on your virtual machine. There really is little chance that you'll lose any data but you should do it if you can't afford to lose anything.

The first step in the procedure involves resizing your virtual disk. Currently this feature is only available by command line but is very simple. All you're really doing here is modifying your virtual disk header information to resize your virtual disk so this command runs instantly. As an example, if you wanted to add 5GB to your windows virtual disk named windows.qcow2 , you'd type a command with the following syntax. So make sure you power down your VM first then run the following command on your virtual disk. Remember you virtual disk must be either raw or qcow2 format for this to work.

# qemu-img resize windows.qcow2 +5GB
# Image resized.

You'll get an return almost instantly saying "Image resized" and you're done. From within your disk management on our windows XP guest, you will now see unallocated space if you boot into it as shown below.

At this point , because you will be using gparted on linux, you need to shut down your guest virtual machine and add the virtual disk to your linux virtual machine in preparation for step 2; resizing your filesystem.

 

2.  Resize your filesystem

This step requires the use of some kind of partition manipulation software to increase the size of the filesystem on your modified virtual disk to fill up your new space. You could use something like partition magic on windows or some other similar tool. The only tool I've tried this with is gparted on linux and have not had any problems.

Boot your linux virtual machine running gparted with your virtual disk attached to the linux virtual machine. From within your linux guest machine, start gparted.

Select your virtual disk from the drop down menu, right click on the filesystem and select "Resize/Move".

The easiest way to resize is to click and drag the box surrounding the extent of your filesystem so that it fills the whole virtual disk as shown in the snapshot below.

When you're done doing this you must click on apply changes or this will not take effect. This takes about a minute to complete for small changes.

When complete you'll see a message like that shown below confirmating the completion of it.

All that remains to do now is boot into your virtual machine with the expanded virtual disk/filesystem.

 

3.  Reboot your virtual machine

This last step is probably the simplest as it will require you to do almost nothing for a windows guest virtual machine. This step will vary depending on your guest OS but practically all will have filesystem checking on boot. For a windows XP guest machine, all it requires you to do is boot your virtual machine. On boot , windows will detect any inconsistencies in your file system and automatically bring up the check disk utility to fix these inconsistencies.

Once this is completed, windows will reboot and the procedure is complete. You now have additional disk space.

 

Conclusion

This procedure provides a nice simple way to expand your guest virtual disk without having to add another virtual disk or disk cloning. It is quick and easy to remember. I think the resize functionality would be a superb feature to have in management tools such as virt-manager. Next time you need a little extra space on your guest, give this procedure a try and see how it works. As usual feel free to post any comments or questions you have.

 

See Also

Comments

Try to use

Try to use http://libguestfs.org virt-resize

Without reboot

Nice article.

One additional info:
A long time ago, Dell created a tool called extpart that you can use to extend windows partitions (and filesystems, including system partition as well) online without reboot.
Here you can download it:
http://support.dell.com/support/downloads/download.aspx?c=us&cs=19&l=en&...

It is working fine and stable, I've already used it several times.

Balazs

Re: virt-resize

librarian,

virt-resize is indeed an option but it cannot resize a disk in place which is what I wanted to do. It is an option if you don't mind going through the procedure of cloning your virtual disk. 

qemu-img resize however avoids the cloning step. 

Online resizing

What we really need is online resizing.
Having to shutdown the guest isn't really "enterprise ready".

We use KVM machines en LVM (with Proxmox), it would really be cool if KVM, at least with Virtio drivers, can pass the new size to the Guest OS.

Resize guest filesystem

I just extend the filesystem as follows;

dd if=/dev/zero BS=1M count=5120 >>

If its a windows machine reboot when convenient then at a command prompt;

diskpart
list volume
select volume
extend

Re: Online resizing

virtio drives *can* now pass the new size up to a live guest. This is a relatively new change that we got into qemu and RHEL 6.2.

The problem is that no guests can do anything useful with that information. Partly this is down to the fact that resizing MBR and (especially) GPT partitions is seriously hard work, particularly if you want to do it without any risk of losing data.

KVM virtual disk increase

This was very helpful.

In conjunction with http://itsignals.cascadia.com.au/?p=28 and looking at the properties for the Win 7 virtual launcher, I was able to figure out that my file was in /var/lib/libvirt/images and was the .raw file, not the .qcow2 file in the clone directory. I used the qemu-img resize command to carve out a 50G space. Instead of adding it to the existing Windows C: drive, I created an E: drive within Win 7 (D: is the CD). Better for my uses anyway.

I am a Linux novice and this conversion process to a new Linux based machine has been painful to say the least!

It was really helpful

Thank you it was really helpful. Saved at least a few hours.

Great help. Thanks

Hi guys,

Thanks very much. Very helpful.

Manda

Super Helpful

Thanks very much for the original post - and to "Wylie" for the Windows commandline stuff - worked like a champ for a Windows 8 VM I'd run out of disk space.

Thanks again.
Cheers,
BB

Post new comment

The content of this field is kept private and will not be shown publicly.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.