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.
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.
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.
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.
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.