My *nix world

Resize KVM virtual disk

It's happening that I have a Win2K3 kvm VM hosted on a Linux host. On Linux I have installed two disks and on each of these disks I've created many partitions. Two of these partitions serve like "physical disks" to my Win2K3 VM (eg: /dev/sda5 and /dev/sdb5).

Despite the fact that for host (Linux) they are seen like partitions created on the physical disks, for the Windows (the VM) they are seen like and act like physical disks. When I installed Win2K3 on these "disks" Windows created MBR + its system partition on /dev/sda5 and one/more other partition(s) on the secondary "disk" (/dev/sdb5).

Initially I thought that I will need something like 20GB for system disk and other 50GB for the other disk. So my /dev/sda5 has about 20GB and /dev/sdb5 has 50GB respectively.

Recently, after using more than 3 years this Win2K3 VM, I understood that I do not need so much space allocated for Windows because it seems that I will never use it. So I decided myself to shrink both disks to a more ressonable size (eg: /dev/sda5=10GB and /dev/sdb5=20GB).

If they were simply just ext{2,3,4}/ntfs partitions then everything should worked like a charm but in fact even if for Linux they appears like partitions, they don't expose (to Linux) a ntfs partition's structure but a Windows disk structure (which is unknown by Linux). So the problem is little bit more complicated then it seems at the first thought.

Resize KVM virtual disk

My approach:

  1. inside the VM boot a Parted Magic CD (create a virtual CD using a .iso image for Parted Magic)
  2. after Parted Magic booted open the gparted utility and try to resize/shrink every partition you like; note the new physical dimension for each of these (preferably in bytes, if possible)
  3. poweroff the VM started at step 2
  4. we will try to copy the new shrinked disks from the Linux partitions (those that plays the role of physical disks for Windows, I mean /dev/sda5 and /dev/sdb5 in my example); for this run dd command like in the example bellow:
dd bs= count= if=/dev/ of=Resize KVM virtual disk

where:

  • is the cache size of your disk (eg: if 16M write 16M or 16384K or just 16777216); you can find out what is your disk cache size with "hdparm -i /dev/sdx|grep -i BuffSize", where sdx is the corresponding device for your hdd
  • is the name of your Linux partition which plays the role of Win2K3 disk (eg: sda5 or sdb5 in my example)
  • Resize KVM virtual disk is the full path where you want to save (with dd) the file image for your Win2K3 disk (in fact the Linux /dev/ partition's content)
  • is a number we have to calculate:
    • let's suppose that after you have shrinked the Windows partitions (see step 2 above) you have wrote down the number of bytes of the new partition (which inside the VM is seen like /dev/vdaX, where X is the number of partition); let's suppose that the new number of bytes your partition have is X
    • we have to copy (with dd) about bytes only times, so will be X divided by (where should be transformed to bytes, if it's not already); if you get a decimal number make sure you round it up (better to have a bigger disk than smaller)

An example for this dd command could be:

dd bs=16M count=769 if=/dev/sda5 of=/tmp/sda5.img

The above command will create a image file of 16M*769=12304M=12G

I know, the initial Linux partition had 20G but we cut with the scissor about 8G unused/unpartitioned space.

Q: It's is that possible?

A: Why not?

Note: after getting the image file of the new shrunk Linux partition you can create a VM virtual disk based on it and try to test it. If it boots/works and Windows don't complain then everything should works.

Make sure you backup your importing data! You have been warned...

If you have to work with Windows dynamic-disk then the steps involved would be preceded by these described here.

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

 
The following two tabs change content below.
Resize KVM virtual disk

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers: