5333 private links
You can compact using GUI tools, but PowerShell provides the complete method. Remember two things:
- You cannot compact a VHDX connected to a running virtual machine.
- The computer system that runs the Optimize-VHD cmdlet must have the complete Hyper-V role installed. The PowerShell module alone does not contain the necessary system services. If you must run the compact operation on a system that does not have Hyper-V, you can use diskpart instead.
The process centers around the Optimize-VHD cmdlet. In general, you’ll want to mount the VHDX into the management operating system to get the best results. For example:
Mount-VHD .dyndisk.vhdx -ReadOnly
Optimize-VHD .dyndisk.vhdx -Mode Full
Dismount-VHD .dyndisk.vhdx
Be aware that even for very small files, that can take some time to complete. The amount of time depends mostly on your available CPU cycle and the speed of your disk subsystem.
Lots of people confuse “shrink” with “compact” for virtual hard disks. The “compact” operation squeezes the empty bits out of a dynamically expanding hard disk to reduce the amount of space that it consumes on physical disk. Compacting does not alter the maximum size of the disk, nor does it report anything different to the guest operating system. Furthermore, “compact” does not work on fixed VHDX at all. The “shrink” operation, the topic of this article, reduces the physical consumption of a VHDX and reduces the disk size reported to the guest OS. //
What to remember: A compact operation reclaims the space from empty blocks. A shrink operation reclaims the space from unallocated blocks. We will make the distinction more obvious when we look at the shrink operation. //
You Cannot Shrink a VHDX that Contains a Dynamic Logical Disk
If the logical disk inside the VHDX that you’re working with is Dynamic, then you can’t shrink it. Do not confuse the Dynamic logical disk with dynamically expanding VHDX, as they are dramatically different things. //
The moral of the story: don’t use Dynamic logical disks. Hyper-V isn’t the only thing that doesn’t know what to do with them. Unfortunately, some products out there require Dynamic disks. Even at Microsoft, at least one team hasn’t yet gotten the memo to stop using Dynamic disks. So, the secondary moral: if you must use Dynamic logical disks, provision small because you can always grow them.
This article is presented to you as a tool for performing virtual-to-physical conversions because it is universally applicable to any target hardware environment. However, because it is target-neutral, the steps presented here could very easily be adapted to other situations. For instance:
- Virtual-to-virtual conversions for which there is no applicable tool
- Physical-to-virtual conversions
- VHDX-to-physical conversions //
Virtual-to-Physical Conversions are Not Ideal
My first recommendation is: find another way. Even though V2P is possible, it has a fairly high chance of being unsuccessful even with your best effort. A preferred approach would be migration. Install the desired operating system on the new hardware and migrate settings and applications. Most software vendors have a documented method for performing this. You are almost guaranteed to prefer the process and results over a V2P.
If you’re using Hyper-V Server and you’re converting a VM running a recent version of Windows, you could install the OS on the physical hardware and then mount the VHDXs directly inside the new operating system and transfer files via copy, xcopy, robocopy, Windows Explorer, etc.. //
The very first thing you must do is take a complete backup of the machine to be converted. You are going to absolutely rework this machine from bottom to top, things are almost definitely going to go wrong, and your sole saving grace will be a good backup. //
Your virtual machine should be off before proceeding. You’ve got a clean virtual machine, but what you need is a clean image. The easiest way to proceed from here will be to use some form of imaging software. There is any number of solutions available, but these directions will use Clonezilla Live. If you have some other product, the process should be similar.
There are two basic methods to perform a clone. One is over the network, the other is is by hard drive transfer. I’ll show you the basic setup operations for each method, and then in the next section, I’ll go through running Clonezilla with appropriate branches to fit your method.
Using checkpoints to revert virtual machines to a previous stateOne of the great benefits to virtualization is the ability to easily save the state of a virtual machine. In Hyper-V this is done through the use of virtual machine checkpoints. You may want to create a virtual machine checkpoint before making software configuration changes, applying a software update, or installing new software. If a system change were to cause an issue, the virtual machine can be reverted to the state at which it was when then checkpoint was taken. //
Deleting checkpoints
Deleting checkpoints can help create space on your Hyper-V host.
Behind the scenes, checkpoints are stored as .avhdx files in the same location as the .vhdx files for the virtual machine. When you delete a checkpoint, Hyper-V merges the .avhdx and .vhdx files for you. Once completed, the checkpoint's .avhdx file will be deleted from the file system.
You should not delete the .avhdx files directly.
Optimize-VHD
Module:
Hyper-V
Optimizes the allocation of space used by virtual hard disk files, except for fixed virtual hard disks.
Resize-VHD
Module:
Hyper-V
Resizes a virtual hard disk.
One of the many benefits of virtualization is portability, meaning that you can easily move selected virtual machines (VMs) from one location to another without experiencing downtime or service disruption. Portability within a Hyper-V environment is ensured through the Hyper-V Export and Import functionality, which allows you to easily copy and move a Hyper-V VM to a target location and then restore the VM to how it was at the time of export.