Thursday, June 25, 2009

Converting Hyper-V VHDs

I recently was assigned the task of rebuilding our Team Foundation Server with TFS 2010 for many reasons. One of those is because the old one has a VHD that is consuming far more resources than it should be (it's a 100 gigabyte vhd and only 8 gigabytes are in use). I seemed to recall somewhere that Hyper-V could "compact" a virtual hard drive, but I couldn't remember where. After doing a bit of searching around Hyper-V, I found what I needed.

Here's a few facts about this before getting started.
First, shrinking a hard drive only applies to Dynamically sizing disks. Since these do no shrink on their own (there's a lot of reasons why) but only grow, they might need to be compacted later to free up host space. It only reduces the .vhd file size by shrinking the 'shell' (if you will) to take up the drive space that is not being used by the guest OS.

In my situation, I was dealing with a drive that was not dynamically sizing but was static. If the vhd is static, the Compact button will not show up when you go to edit the drive.
In my case, I did not have to compact the drive. As I said, a drive cannot be compacted unless it is dynamic. Since mine was static, I converted it to dynamic to regain the compacting functionality but because of the way the conversion process works, it automatically 'compacts' the .vhd. My original static .vhd was 100 gigabytes. The output was 15.5 gigabytes.

Though I did not have to compact my .vhd because the conversion process did it for me, I'm going to put the instructions on how to compact the .vhd anyways.

For starters, the virtual machine that the hard drive is attached to must be turned off. Once the server is offline, from within the Hyper-V Manager (it's an mmc snap-in) go to the virtual machine's properties (right click the machine and select properties). Select the drive you want to shrink on the left panel that lists the various devices attached to the virtual machine. After selecting the drive, on the right panel, select Edit. This will bring up a window that tells you what editing a drive does and gives you the option to not show that screen again. Click Next. From here you should have three options (unless the .vhd is static). Select Compact and click Next. Finally, click Finish and Hyper-V Manager will shrink the .vhd as much as it can.

1 comment:

  1. Hi Aaron, here's the email I sent to Shay, the VSTS 2010 Lab Mgmt Pgm Leader:

    Hi Shay, thank you for getting back with me. Here's my situation:
    1. I began with the "TFS 2010 Beta 1 Install Guide 05152009.CHM" as a guide. That documents states -
    • " Create and host one or more virtual machines (VM) by using Windows Server 2008 and Hyper-V™. The computer on which you host a VM must be running Windows Server 2008 Release 2 or a later version. (Library Machine right?)

    • Install and configure Microsoft System Center Virtual Machine Manager (VMM) 2008. The computer on which you install VMM must be running Windows Server 2008 Release 2 or a later version." (VMM machine right?)
    So, I hope I'm using the right OS. I'm using one and the same machine for the both the library and VMM. The build I installed is:
    Windows Sever 2008 R2 Enterprise. Evaluation copy. Build 7100
    (BTW, it says I only have 18 days left to activate, but we have tried and not been able to obtain the information or instructions for doing this)
    Later, I found the "Lab Management Setup Guide" word document, which states for the VMM machine "Windows Server 2008, 64-bit" (R2? SP2? ?), and for the Library Machine "Windows Server 2008" (64-bit?, R2?, SP2? ?)
    2. I had already created the VM using Hyper-V and installed all needed SW. I can see and manage it from the VMM management console, but I only see it on the Virtual Machines view (Host Groups>All Hosts>[machine]) and not in anywhere in the Library View. What I see on the Library view is this:
    Library Servers
    - [machine name fully qualified]
    -- EnvironmentTemplates
    -- GoldenVMs
    -- MSSCVMMLibrary
    --- VHDs (2 aredisplayed in the middle panel: Blank Disk - Small and Blank Disk - Large)
    -- VMs and Templates (this icon is not a folder but a library and it has no contents)

    So, here's what happens when I follow your procedure:
    2.1 - While in the Library View I click on "New Template" under Library Actions and the wizard comes up
    2.2 - I select "From an existing VM that is deployed on a host" and click "Browse"
    2.3 - The "Select Template Source" dialog comes up and is empty. So, what do I do? Here's where I'm stopped.

    I apologize for the length of the email, but as I'm installing on a secure network (national defense security), I cannot copy any information/screen shots back on to our email network.

    I really appreciate your help. if I need to strart over, that's OK; but, hopefully not.

    Best regards, Bob

    ReplyDelete