Monday, October 03, 2016

Converting from VMware Workstation to Hyper-V

Why Hyper-V?

The first question is: Why Hyper-V? Depending on your age, the answer could be as simple as: For the same reason that Internet Explorer killed Netscape Navigator. It's good enough, and the price is lower.
A Windows 10 Professional ($140) license is about the same cost as VMware Workstation ($250). Yet, it includes the ability to join a domain, and use BitLocker. AND you get Hyper-V, too. Windows 10 will probably be around for years, with included updates. Plan on re-buying Workstation every year. Are there things that VMware Workstation does better? Probably. But Hyper-V is good enough for many things.
For many of my classes, the student work is distributed as a VMDK file. Yet, I want to be able to run under Hyper-V. There are some forensic ways to do this using dd and boot CD's, but it isn't really an elegant way to go. Fortunately, @da_667 helped me out!
I'm going to paraphrase @da_667. Any errors are mine!

Installing Microsoft Virtual Machine Converter

Download the Microsoft Virtual Machine Converter from: https://www.microsoft.com/en-us/download/details.aspx?id=42497. You want version 3.0 or later. Grab the MSI and install.
Now you need an Administrative PowerShell windows. Hit the start button. Type PowerShell. Right click on "Windows PowerShell" and select "Run as Administrator". Hit Yes if prompted.
Allow the execution of downloaded scripts by typing:
Set-ExecutionPolicy Bypass
Answer Yes.
Load the newly installed PowerShell module by typing:
Import-Module 'C:\Program Files\Microsoft Virtual Machine Converter\MvmcCmdlet.psd1'
Now you're finally ready to do the conversion. 

Converting the VMDK

You're going to run a command that looks like this to do the conversion:
PS C:\WINDOWS\system32> ConvertTo-MvmcVirtualHardDisk -SourceLiteralPath 'C:\Users\markj\vms\import\Sec-511-Linux\Virtual Disk-cl1.vmdk' -VhdType DynamicHardDisk -vhdformat vhdx -Destination 'C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\'
As far as I can tell, it'll work with two arbitrary files. You don't have to store your disk images anywhere special on disk to get the command to work.
Maybe you'll get lucky and be done at this point. It was never that easy for me.

The Inevitable Error

Here's the infuriating error you'll get:

PS C:\WINDOWS\system32> ConvertTo-MvmcVirtualHardDisk -SourceLiteralPath 'C:\Users\markj\vms\import\Sec-511-Linux\Virtua
l Disk-cl1.vmdk'
ConvertTo-MvmcVirtualHardDisk : The entry 2 is not a supported disk database entry for the descriptor.
At line:1 char:1
+ ConvertTo-MvmcVirtualHardDisk -SourceLiteralPath 'C:\Users\markj\vms\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (Microsoft.Accel...nversionService:DriveConversionService) [ConvertTo-MvmcVirtualHardDisk], VmdkDescriptorParseException
    + FullyQualifiedErrorId : DiskConversion,Microsoft.Accelerators.Mvmc.Cmdlet.Commands.ConvertToMvmcVirtualHardDiskCommand

ConvertTo-MvmcVirtualHardDisk : One or more errors occurred.
At line:1 char:1
+ ConvertTo-MvmcVirtualHardDisk -SourceLiteralPath 'C:\Users\markj\vms\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (Microsoft.Accel...nversionService:DriveConversionService) [ConvertTo-MvmcVirtualHardDisk], AggregateException
    + FullyQualifiedErrorId : DiskConversion,Microsoft.Accelerators.Mvmc.Cmdlet.Commands.ConvertToMvmcVirtualHardDiskCommand

PS C:\WINDOWS\system32> 

Or, if you prefer graphics:
Like, I have no idea what any of that means.

The Fix

This sounds scary, but isn't. :)
Open up the VMDK file in your editor of choice. I used Notepad++ but Notepad or any normal ASCII text editor will work fine. Heck, if you've got Bash on Ubuntu on Windows installed, you could use vi!
Find the line in the vmdk file that looks like:
ddb.toolsInstallType = "2"
and just comment it out with a #, so it looks like this:
#ddb.toolsInstallType = "2"
That should be it!
Re-Run your conversion and it should work fine!
Like this:
PS C:\WINDOWS\system32> ConvertTo-MvmcVirtualHardDisk -SourceLiteralPath 'C:\Users\markj\vms\import\Sec-511-Linux\Virtual Disk-cl1.vmdk' -VhdType DynamicHardDisk -VhdFormat vhdx -DestinationLiteralPath C:\Users\markj\vms\sec-511-linux\


Now you should have a VMDK file all ready to go.

Creating the new Hyper-V VM

I'm going to skimp on this part because there are plenty of tutorials out there on how to do this. Basically, use the "New Virtual Machine" function to create the new VM. When you get to the "Connect Virtual Hard Disk" option, select "Use an existing virtual hard disk" and point it to the image you just created.
And you're done!