How To Copy a Hyper-V Virtual Machine to a New Location
Here is a step-by-step guide for copying a stateless virtual machine hosted on a server to a RAM disk and run it from there.
March 8, 2024
In a recent article, I discussed improving Hyper-V virtual machine performance by hosting it on a RAM disk. While this can boost speed, there is a major downside: RAM disks are volatile, meaning they instantly purge all their contents when the machine powers off or reboots.
Since RAM disks are a volatile storage type, you should use them exclusively for stateless workloads. You should also keep a copy of the VM on a normal storage volume. That way, you don’t have to recreate the VM from scratch each time. Alternatively, you could automate VM setup with a script that handles the unattended OS installation.
For this article, let’s assume we have a stateless VM hosted on a server and mount a RAM disk during each boot. Our goal is to copy the VM to the RAM disk and run it from there.
While we can easily script this process, I will walk you through it step by step.
Exporting the Virtual Machine
The first step is to export the virtual machine. The export action creates a copy of the VM that we can use to generate additional instances as needed.
To export a VM, open an elevated PowerShell session and enter the Export-VM cmdlet. Specify the VM name and the target folder (the location for storing the exported copy) as parameters. For example, to export a VM named VM to the D:\VMs folder, use this command:
Export-VM -Name VM -Path D:\VMs
Figure 1. I am exporting the virtual machine.
Important notes:
You can export a virtual machine to any path. Nevertheless, note the exact export path. You will need this information later.
The export process only creates a reproducible copy of the VM. It does not affect the original VM, which stays in its original location (see Figure 2).
Figure 2. The original virtual machine remains unaffected.
Locating the exported VM identifier
After the export, the target folder contains a complete copy of the virtual machine. You must identify the VM's identifier assigned by Hyper-V to create clones. Follow these steps:
Open File Explorer and navigate to the folder specified during export (e.g., D:\VMs).
Open the subfolder named after the VM (e.g., D:\VMs\VM\Virtual Machines).
Locate the file with a .VMCX extension and note its name. The file name represents the VM's identifier (e.g., BD45FCC2-F93A-492D-A9F5-0CB58B73686D.vmcx).
Next steps
Where you go from here depends on your goal. Choose one of the following approaches:
Replace the Original VM: Delete the original VM and use the exported copy to spawn a new VM each time the RAM disk is mounted.
Run Concurrently: Keep the original VM intact and run the RAM disk VM as a separate instance.
Replacing the VM
If you want to replace the VM with a copy hosted on a RAM disk, follow these steps:
1. Backup the Original VM: Always back up the VM before proceeding to ensure you can recover it if needed.
2. Import the VM to the RAM Disk: Use the Import-VM cmdlet to load the exported VM into the desired location. For example, if the exported VM resides in D:\VMs and is named VM, the command might look like this:
Import-VM -Path 'D:\VMs\VM\Virtual Machines\BD45FCC2-F93A-492D-A9F5-0CB58B73686D.vmcx' -copy -VHDDestinationPath 'C:\VMs\VM' -VirtualMachinePath 'C:\VMs\VM'
In this example, the VM is imported to C:\VMs. However, in practice, you would specify a path on your RAM disk. In Figure 3, note that the command refers to the .VMCX file mentioned earlier.
Figure 3. The import command must reference the exported VM’s .VMCX file.
Figure 4. This is what the import process looks like.
3. Review the Import Process: The import process creates a fully functional Hyper-V virtual machine. The new VM will reside at the location specified by the import command (Figure 5).
4. Preserve the Exported Process: The original exported VM remains intact, as shown in Figure 6. This allows you to reuse the exported copy to spawn additional VMs as needed.
Figure 5. The import command has created a new virtual machine.
Figure 6. The exported copy still exists.
Creating a Duplicate VM
In some cases, instead of replacing the original VM, you may want to create a duplicate hosted alongside it. While simultaneously running both copies may not always be ideal depending on their configurations, duplicating the VM can be valuable for testing or redundancy.
The command for creating a secondary VM copy resembles the one I shared earlier:
Import-VM –Path 'D:\VMs\VM\Virtual Machines\BD45FCC2-F93A-492D-A9F5-0CB58B73686D.vmcx' -Copy -GenerateNewID
Note this command uses the -Copy and the -GenerateNewID parameters. The -GenerateNewID parameter assigns a unique identifier to the new copy, allowing it to coexist with the original. You can see the command in action in Figure 7.
Figure 7. You can use the Import-VM cmdlet to create a secondary VM copy.
When the command finishes, Hyper-V will host two identical VMs capable of running side by side (Figure 8).
Figure 8. There are now two VM copies.
The two copies, while identical, are stored in separate paths (Figure 9).
Figure 9. The two VM copies exist separately, with files stored in differing locations.
About the Author
You May Also Like