Reclaiming Lost Hard Drive Space on Hyper-V Host Servers (Part 2)

by [Published on 24 Feb. 2011 / Last Updated on 24 Feb. 2011]

This article concludes the series on reclaiming lost hard disk space, by examining three different methods for managing your virtual hard drive files.

If you would like to read the first part in this article series please go to Reclaiming Lost Hard Drive Space on Hyper-V Host Servers (Part 1).

Introduction

In the first part of this article series, I explained that when you create a virtual hard drive in Hyper-V, a dynamically expanding virtual hard drive will be created by default. Dynamically expanding virtual hard drives initially start out much smaller than the amount of space that you have allocated to them, but will automatically expand as data is written to them. Unless an organization carefully manages storage resources, this dynamic expansion can make it easy to accidentally over commit storage resources. The nature of dynamically expanding hard drives also makes it easy to waste a lot of storage space since the virtual hard drive files do not automatically shrink when data is removed from them. In this article, I want to explore some techniques for resolving these issues.

Manually Shrinking a VHD File

The first option for reclaiming lost hard disk space is to manually shrink the virtual hard disk file. Although there is nothing difficult about shrinking a virtual hard disk file using the method that I am about to show you, it can be time consuming, and does require you to take to corresponding virtual server offline.

The Hyper-V Manager gives you everything that you need for manually shrinking a volume. Having said that however, if you attempt to shrink a dynamically expanding virtual hard drive file without doing any preparation work first, then you may not see much benefit from the operation.

The reason behind this has to do with the way that the shrinking process works. Essentially, Hyper-V checks for free space at the end of the virtual hard disk file, and then truncates the empty space. Actually, there is a little bit more to it than that, but the technical details are not important for what we need to accomplish.

The problem with truncating the empty space at the end of the virtual hard drive file is that you don’t know where the empty space within the file is actually located. The virtual hard drive file expanded to its current size as the result of data being written to the file. As such, the end of the virtual hard drive is guaranteed to have once contained data, and that data may still be there. Empty space within a virtual hard drive file is usually scattered throughout the file. As such, we need to move all of the empty space to the end of the file before we can compact the virtual hard drive.

In many cases, you can accomplish this by performing a defragmentation. Believe it or not, this can be accomplished in a very straightforward manner, through the guest operating system that is using the virtual hard drive file.

The exact method that you will use to perform the defragmentation varies depending on the guest operating system that is using the virtual hard drive file. In the case of Windows Server 2008 R2, you would boot the operating system, go into Windows Explorer, right click on the drive that represents the virtual hard drive file, and then choose the Properties command from the resulting shortcut menu.

At this point, you would go to the properties sheet’s Tools tab. As you can see in Figure A, this tab contains a Defragment Now button.


Figure A: You should defragment a virtual hard disk prior to compacting it.

On a physical server, Windows moves around blocks and attempts to arrange them in a contiguous manner. The same thing happens on a virtualized system, except that the blocks are being moved within a virtual hard drive file rather than on a physical drive. This has the effect of moving the data to the beginning of the file, and shifting empty space to the end of the file. As with a physical hard drive though, there may be some data that cannot be moved.

After you defragment the virtual hard drive, shut down the guest operating system. Now, open the Hyper-V Manager, right click on the virtual machine, and choose the Settings command from the resulting shortcut menu. When you do, Windows will display a Settings dialog box for the virtual machine.

Locate the listing for the virtual hard disk that you want to compact, as shown in Figure B, and then click the Edit button.


Figure B: Locate the listing for the virtual hard disk that you want to compact and then click the Edit button.

At this point, Windows will take you to the screen shown in Figure C. As you can see in the figure, you have the option of compacting the virtual hard drive. With the Compact option selected, click Next. When you do, you will see a summary screen that confirms that you are about to compact the drive. Assuming that everything on the summary screen looks correct, click Next to compact the virtual hard disk file.


Figure C: The Hyper-V Manager provides the option of compacting the virtual hard drive file.

Using a Third Party Utility

The second option is to use a third party utility to compact a virtual hard disk file. I’m sure you may be wondering why you would want to use a third party utility when Hyper-V can do it for free. Well, third party utilities may be able to automate the process, or show you which dynamically expanding virtual hard drive files can benefit from being compacted.

One such utility is V-Locity 2.0 from Diskeeper. V-Locity tells you how much space can be saved by compacting your dynamically expanding virtual hard disk files, and also provides you with a single click mechanism for shrinking the files. You can see what V-Locity looks like in Figure D.


Figure D: V-Locity 2.0 can shrink dynamically expanding virtual hard disk files on Hyper-V servers.

Converting to a Fixed Size

The last option that is available to you is to convert your dynamically expanding virtual hard disk files into files of a fixed size. Keep in mind that performing this type of conversion will not reclaim any lost hard disk space. What it will do however, is prevent you from accidentally over committing your disk space resources. Additionally, using fixed size virtual hard drive files can help to improve I/O performance.

The actual conversion process is really simple, but the virtual machine does have to be offline before you can convert the file. If you look back at Figure C, you will notice that there is an option to convert a dynamic virtual hard disk to a fixed virtual hard disk. Select this option and click Next. When you do, you will be prompted to select a name and location for the fixed size disk. Just make sure that the location that you choose has plenty of free disk space to allow the operation to complete. When you are done, click Next and you will see a summary screen telling you that the virtual hard disk file is about to be converted. Click Finish to begin the process. Keep in mind that depending on the size of the file and the underlying hardware capabilities, the conversion process can take many hours to complete. 

Conclusion

As you can see, there are at least three good options for taking control of your virtual hard drive files. Even if you are not having disk space issues at the moment, it may still be worthwhile to take a look at these techniques and see if they can help you.

If you would like to read the first part in this article series please go to Reclaiming Lost Hard Drive Space on Hyper-V Host Servers (Part 1).

Featured Links