Relocating a Hyper-V VM Folder

In my Hyper-V setup, I had spaces in the VM names (what we see in the server manager), which by default, also added spaces in folder names where the VHD and the other VM files were stored. When I started using PowerShell to work with the VMs, I had to use quotes around VM names because of the spaces. After some time, I decided to rename the VMs to have the same name as the network names of the VM (the names that can be pinged), which had no spaces. This renaming appeared to be a simple task because there was a right-click > Rename option.

I renamed the VMs easily and got rid of the spaces in the names. However, the folder paths and the VHD names remained unchanged and continued to use the old names with spaces. While my original problem had been resolved, the discrepancy in the VM name and the file/folder names was a new annoyance to me even though I could have ignored it quite easily. If I rename a folder/file (portion of path), I essentially get a new path for the files/folders.

Now, there may be situations where I would actually want to move the VM files from one location to another. Suppose that the disk on which the VM files exist is getting filled up and I need to expand the disk for one of the VMs. In this case, I would like to relocate the files to another drive. In another situation, I might want to distribute my existing VMs to different physical disks to minimize the I/O contention as multiple VMs run concurrently. In both these cases, I need to move the files for an existing VM from one location to another.

There are probably other alternatives to the approach that I adopted. The steps described below just summarize what worked for me and may or may not work in other situations. The following steps accomplished this change for me:

  1. Shut down the VM
  2. Back up the VM folder.
  3. If there are any snapshots for the VM, delete the snapshots. This step may or may not be necessary, but I suspect that there must be references to the AVHD files which are likely to be broken by path changes.
    1. Wait for merge to complete (A merge is indicated by a “Cancel Merge in progress …” button in the Actions pane for the VM. A merge combines the VHD and AVHD files into one VHD file. After the merge is complete, the cancel button disappears and the AVHD files also disappear from the disk.
  4. Create the new VM folder, and move the VHD file to the new folder.
    1. Rename the VHD file now, if renaming the VHD is desired for any reason.
  5. In the Hyper-V manager, open settings for the VM.
    1. Under Hardware > IDE Controller x > Hard Drive, change the path of the VHD file by browsing to the new location/name of the VHD file.
    2. Under Management > Snapshot File Location, select the new path to the base VM folder. Carefully look at the path before changing it so as to keep it at the same level.
  6. Stop Hyper-V service in Windows Services.
  7. Move the remaining contents of the VM folder to the new location.
  8. Hyper-V internally tracks the location of the VM folder/files using symlinks stored in C:\ProgramData\Microsoft\Windows\Hyper-V. The link to the VM under consideration needs to be updated.
  9. Using Windows Explorer or Command line, do the following
    1. Find the symlink with its name matching the VM ID. It is a long ID with xml extension. It can be matched easily by looking at the file with identical name (as the symlink) in the VM folder.
    2. Move this link out of this folder as a backup.
    3. Remove the file with the same name from the cache folder – C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines Cache
  10. Start cmd (Run as Administrator), and run the following commands after inserting the correct VM ID and pathin the commands
    1. cd C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines
    2. mklink <VM_ID>.xml "<PATH_TO_VM_FOLDER>\Virtual Machines\<VM_ID>.xml"
      1. The output of this command looks like:
        symbolic link created for <VM_ID>.xml <<===>> <PATH_TO_VM_FOLDER>\Virtual Machines\<VM_ID>.xml
    3. Fix permissions on the symlink and the file using the following commands:
      1. icacls <VM_ID>.xml /grant "NT VIRTUAL MACHINE\<VM_ID>":F
      2. icacls <VM_ID>.xml /grant "NT VIRTUAL MACHINE\<VM_ID>":F /L
  11. Start the Hyper-V service
  12. If VM renaming is desired, rename the VM using right-click > Rename In Hyper-V Manager
  13. Start the VM
  14. Verify that everything is working normally.
Advertisements

9 thoughts on “Relocating a Hyper-V VM Folder

  1. Hi, Thanks for the info. Worth mentioning that it is possible to move the VM to a new folder by right clicking on it and choosing move. You can opt to just move data and select only the elements (VHD / config files / snapshot / smart cache) you want and then assign them a new folder.

    Better for people looking at a basic change like this who wish to stay away from editing xml docs etc.

    1. I am not sure what your environment is, but there is no Move option on 2008 R2 SP1 server. People without a Move option can still use the approach detailed in this post.

      1. I’m betting Andy’s environment is Windows 8 Client Hyper-V, which does offer the ability to move a VM in one fell swoop. I just found it myself.

    1. Oops.
      Yep, I’m using Windows Server 2008 and 2012 Hyper-V which both offer this.

      Thought it was a bit odd if i’d found this and you guys hadn’t.

      Thanks again for the post

      Andy

      1. Definitely odd! I don’t have anything newer than 2008 R2 SP1 Server in my server environment (particularly, no 2012), and I clearly don’t see this option. Now, I haven’t updated my Hyper-V host in a while and am not sure if any update adds this functionality.

  2. If moving to VHDs to another physical disk on the host and not using default folder structures, you may need to fix permission on your VHDs as in step 10.3.2, permssions (R,W) are sufficient for this explicit add.
    e.g. F:\VMDisks\Server\ icacls *.vhd /grant “NT VIRTUAL MACHINE\{VM_ID}”:(R,W)

    Also re the discussion above: live migration is a feature added to Server 2012 Hyper V. Nothing so nice as this procedure exists in Server 2008 Hyper V. Be prepared for some downtime.

    I was running out of space on partition with the VM disks, and was trying to regain space by collapsing snapshots… It takes double the space of the existing AVHD and VHD to complete the process. Luckily, it was possible to interrupt the compaction by stopping the Hyper-V Manager Service and move the VHD and AVHD files to a freer drive to let the process complete before restarting the VM. It took a few moments to see the disk compaction process restart (after restarting the Manager service, but before starting the VM).

    Thanks for the post.

  3. In Hyper-V Server 2012R2 it’s not necessary anymore to fix the permissions on the created symlinks (when moving the VM manually instead through the management GUI). Just create the symbolic link after moving the VM – and that’s it.

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