Resizing a KVM virtual disk


Please note that this blog has been moved.

Now it has its own domain: mynixworld.info🙂

If you want to read the latest version of this article (recommended) please click here and I open the page for you.

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.

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=<bs> count=<count> if=/dev/<part> of=<img path>

where:

  • <bs> 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
  • <part> is the name of your Linux partition which plays the role of Win2K3 disk (eg: sda5 or sdb5 in my example)
  • <img path> is the full path where you want to save (with dd) the file image for your Win2K3 disk (in fact the Linux /dev/<part> partition’s content)
  • <count> 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 <bs> bytes only <count> times, so <count> will be X divided by <bs> (where <bs> 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.

About Eugen Mihailescu

Always looking to learn more about *nix world, about the fundamental concepts of arithmetic, algebra and geometry. I am also passionate about programming, database and systems administration.
This entry was posted in kvm, linux and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s