Agramont.net

VP of Mural Consulting...SaaS Strategy & Execution. Accelerated.
Welcome to Agramont.net Sign in | Join | Help
in Search

Conrad Agramont's Blog

Copy/Increase a VHD using VHDMount

So after my first post on the subject of "expanding a VHD" by actually copying the contents of a VHD image using WAIK (Copy a Microsoft Virtual Machine VHD/Increase VHD size using Windows Automated Installation Kit), I got some good feedback on my laziness of not going down the VHDMount method. So I took a bit of time tonight to work through that scenario.

A key new feature in Service Pack 1 for Microsoft Virtual Server 2005 R2 is the availability of a new tool named, "Offline VHD Mounting". It's a command-line utility that allows you to mount a Virtual Hard Drive (VHD) as a local drive on a host server. The great advantage to doing it this way versus using an imaging technology (like I did using ImageX.exe as part of the WAIK), is that you don't need the overhead of taking a snapshot, deploying, rebooting, etc. You simply need to mount the original image, mount the new image, and then do a copy.

Before we get started, please make sure that you have downloaded and installed the following

So here are the steps I took to "expand a VHD" by coping the contents from the original to a new VHD that was created at a much larger maximum size.

  1. Ensure the Virtual Machine (VM) that holds the original VHD is currently stopped.
  2. Mount the original image
    1. Open a Command Prompt and change directories

      cd "C:\Program Files\Microsoft Virtual Server\Vhdmount\"

    2. Mount the original image (the path I have below reflects my deployment.

      vhdmount.exe /m F:\Images\Configs\DepA-Web02\DepA-Web02.vhd V

  3. Create a new VHD - Using the Virtual Server Administration Website, create a new Virtual Hard Drive (VHD) that has the new storage capacity that you feel you'll need.
    1. Click Start > All Programs > Microsoft Virtual Server > Virtual Server Administration Website
    2. From the Virtual Disks menu, select "Dynamically Expanding Virtual Hard Disk" or "Fixed Sized Virtual Hard Disk"
    3. Provide the new hard drive name and location. You may also notice that the default size for a VHD is no longer 16 GB, but is now 127 GB.
    4. Click the Create button
  4. Mount the new VHD (same steps as step #2, but you need to point to the new VHD location)
  5. Once the new VHD is mounted, I wasn't able to actually see it using File Explorer. As any new drive, virtual or physical, it has yet to be partitioned and formatted. So the next step is to create a partition and format the new VHD. This (as is everything else) is done on the host machine. There are two ways that we can perform this task: The GUI way using the "Disk Management" MMC Snap-in or the command-line way using the "DiskPart" utility. Below are the steps using DiskPart
    1. Open a Command Prompt and execute:

      diskpart

    2. Find the disk number for the new image. It should have a "Size" and "Free" value that are the same number AND should be the size of the VHD that you created.

      list disk

    3. Now we'll create the partition on the drive (the disk number for my new VHD was 4, but you should use the number that you found from the above command) and assign it a drive letter

      select disk 4

      clean

      create partition primary

      assign letter=w

      exit

    4. The next step is to format the new drive

      format w: /FS:NTFS /Q

    5. Finally, we'll go back into diskpart and activate the partition

      diskpart

      select disk 4

      select partition 1

      active

      exit

  6. At this point, we now have a VHD drive ready to be used. So now we'll simply start copying all of the contents from the original image to the new image. (This will take a while…)
    1. Using the default xcopy command within Windows, we'll copy all of the contents

      xcopy v:\ w:\ /E/H/K/O

  7. Once the copying is complete, we should now unmount the drives
    1. Open a Command Prompt and change directories

      cd "C:\Program Files\Microsoft Virtual Server\Vhdmount\"

    2. Mount the original image

      vhdmount.exe /u v

      vhdmount.exe /u w

  8. We're now ready to point the target Virtual Machine from the original VHD to the new VHD.
    1. Click Start > All Programs > Microsoft Virtual Server > Virtual Server Administration Website
    2. From the "Virtual Machines" menu on the left side, hover over the "Configure" section and then click on the target Virtual Machine from the pop out window.
    3. Click on the "Hard Disks" configuration section link
    4. We don't "really" need to remove and add a new hard drive. What we'll do instead is point the old path to the new path in the "Fully qualified path to file" section for the appropriate disk. Then click OK.
  9. We're all done! Now you can start up you VM and it should now be using your new VHD, but with the previous (and I'm sure lots of time invested in created) content. But don't delete you VHD just yet. I'd give it a few days or hours of testing before you delete the original VHD to save space.

I'm sure most of this could be scripted out and perhaps that's another late night project for me. But for now, this should at least get you going down a path that worked for me.

Update:

I thought some of you might find the before and after file sizes interesting:

  • Original VHD: 15 GB
  • WIM of original VHD: 5.58 GB
  • VHD using WAIK: 12.5 GB
  • VHD using VHDMount: 13.3 GB

Comments

 

Clive Watson's Weblog said:

Dugie and others have posted about this in the past, now Conrad has added some great detail, especially

January 4, 2007 4:36 AM
 

Dugie's Pensieve said:

All round good guy and virtualisation blogger Clive Watson, has just answered one of my burning questions...

January 5, 2007 1:27 AM
 

David L's Blog said:

So I've been building the mother of all Virtual Machines for showcasing Dynamics development using all

February 11, 2007 9:02 PM
 

Regis Mauger's blog said:

Pour la préparation des TechDays j'ai eu à étendre un fichier VHD, si vous êtes confronté à cela voici

February 19, 2007 2:25 AM
 

Technical Weblog of Eric Charran said:

I recently created a Vista Virtual Machine but ran out of space when I tried to install the service pack.
March 16, 2007 7:52 AM
 

.: Daniel Melanchthon :. said:

Eine meiner Hauptaufgaben in meinem Job bei MIcrosoft ist es, anständige Präsentationen für die Veranstaltungen,

March 20, 2007 4:20 PM
 

Scott Louvau said:

Thanks for the post! Actually you can even do this without VHDMount if you don't want to install the Beta, as long as you already have a Virtual Machine which will boot to Windows. You can just mount the empty drive and the source drive within another VM and do the copy there. * Make a copy of the VHD you wish to expand. (Source VHD) * Using the Administration site, make a new empty VHD (127GB limit) * Shut down the VM which you will use to do the copy. In the Administration site, mount the Source VHD and Empty VHD as other hard drives. * Boot to Windows and follow the steps in this post (diskpart, format, xcopy). -Scott
March 21, 2007 11:15 AM
 

Laramie Hartmann said:

I followed this post to a T and when I start the VM with the new VHD the VM will simply not boot. I remounted the drives with VHDMount and they are ostensibly identical. Any suggestions?
April 27, 2007 8:18 AM
 

Laramie Hartmann said:

Aha! I made the simple mistake of not marking the new mounted VHD as an active partition before unmounting it. Everything works great now.. Thanks Conrad.
April 27, 2007 8:27 AM
 

Emanuele Roserba said:

Mounting the new image with the /f switch it's possible to speed up the entire process (no need to commit the changes to the disk while unmounting it) and save a lot of disk space during the operation itself. Instead it's safer to mount the original image without the /f and unmount it without committing changes in order to avoid data modifications on it due to antivirus software etc. So the process will look like: Vhdmount.exe /m x:\original.vhd Vhdmount.exe /m /f x:\new.vhd Create volume on new, format it, activate, copy data from original Vhdmount.exe /u all.
May 22, 2007 3:24 AM
 

Annerose said:

These comments have been invaluable to me as is this whole site. I thank you for your comment.
June 7, 2007 1:32 AM
 

PT said:

Why not just use VHDResizer: http://vmtoolkit.com/files/folders/converters/entry87.aspx
June 11, 2007 11:52 PM
 

Kelly Murphy said:

When I tried this I ran into permission issues with XCOPY with some GAC'ed assemblies on the source drive, as well as encountering issues when XCOPY attempted to copy files from the hidden System Volume Information directory. I suppose one could (and should) exclude that directory, but before I thought of that I discovered that Robocopy is now a part of Vista (which I happen to be running) and I replaced the XCOPY step with: ROBOCOPY v:\ w:\ /E /ZB /COPYALL That did the trick for me.
June 14, 2007 9:45 PM
 

Carl said:

This does not work on my x64 box with 12gb of memory. I had try to xcopy a 16Gb vhd disk with this aproach and after 12 hours the system hang. Lost virtual server applicatio after reboot and need to re-install vserver. There is an issue with vhdmount with large disk.
June 15, 2007 6:20 AM
 

Nick said:

I have problem with XCOPY. So I mounted both vhd and using Acronis true image disk clone to copy the disks. That works for me.
July 10, 2007 12:22 PM
 

GuruLeeNyc said:

Awesome solution---thank!

August 28, 2007 1:11 PM
 

zhuoyang said:

I can not connet to Virtual Server Administration Website ereyone can tell me why

September 28, 2007 8:17 PM
 

NormK said:

I was able to use the free vhdresizer (thanks P above!) to add an additional 16 GB to my vhd using XP.  Then I simply used vhdmount (with Ben Armstrong's suggestion of /p instead of /m for XP ... see blogs.msdn.com/.../740763.aspx

Then use diskpart with the "extend" volume option on the mounted disk and avoid the copying.  It worked like a charm.

DISKPART

List Disk

Select Disk N

List Volume

Select Volume N

Extend size=8000 Extends by 8GB

Then unmount with vhdmount /u

Note: After installing the latest Virtual Server from Microsoft, you MUST install the SP1 since the R2 does not come with VHDMount.  SP1 will add it for you.

December 21, 2007 5:40 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About agramont

Conrad Agramont is a Partner Technology Specialist (PTS) focused on the Microsoft Server product lines in the Small and Mid-Market Solutions and Partners (SMSP) area for the Mid-Atlantic district. Conrad was previously the Senior Architect for a Microsoft Gold Partner where he was responsible for product planning, software architecture, and technical evangelism focusing on Service Providers around the world. Agramont was previously a Program Manager at Microsoft driving hosting scenarios and architecting components for the Microsoft Provisioning System, Service Provisioning component in Microsoft Solutions for Hosted Messaging & Collaboration, Hosted Exchange 2003, and Windows based Hosting 3.0. Conrad has over 8 years of experience in the Microsoft automation and hosting space, speaking at public events, and publishing articles in magazines. Conrad Agramont is also an active blogger focusing on many Microsoft Hosting related topics. His blog can be found at http://agramont.net/

This Blog

Syndication

Community Tools