From 872fc559f54ac9e343a2c4d9df668d0eaabf57f6 Mon Sep 17 00:00:00 2001 From: Gildas Cherruel Date: Tue, 1 Dec 2015 13:44:34 +0900 Subject: [PATCH] Export of Hyper-V/VMCX machines --- powershell/hyperv/hyperv.go | 92 +++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/powershell/hyperv/hyperv.go b/powershell/hyperv/hyperv.go index 7a981cb1e..e34907521 100644 --- a/powershell/hyperv/hyperv.go +++ b/powershell/hyperv/hyperv.go @@ -288,6 +288,98 @@ func ExportVirtualMachine(vmName string, path string) error { var script = ` param([string]$vmName, [string]$path) Export-VM -Name $vmName -Path $path + +if (Test-Path -Path ([IO.Path]::Combine($path, $vmName, 'Virtual Machines', '*.VMCX'))) +{ + $vm = Get-VM -Name $vmName + $vm_adapter = Get-VMNetworkAdapter -VM $vm | Select -First 1 + + $config = [xml]@" + + + + $($vm.Generation - 1) + $($vm.Name) + + + + $($vm.ProcessorCount) + + + + $($vm.DynamicMemoryEnabled) + $($vm.MemoryMaximum / 1MB) + $($vm.MemoryMinimum / 1MB) + $($vm.MemoryStartup / 1MB) + + + + $($vm_adapter.SwitchName) + + Optical + + False + $($vm.Notes) + + +"@ + + if ($vm.Generation -eq 1) + { + $vm_controllers = Get-VMIdeController -VM $vm + $controller_type = $config.SelectSingleNode('/configuration/vm-controllers') + # IDE controllers are not stored in a special XML container + } + else + { + $vm_controllers = Get-VMScsiController -VM $vm + $controller_type = $config.CreateElement('scsi') + $controller_type.SetAttribute('ChannelInstanceGuid', 'x') + # SCSI controllers are stored in the scsi XML container + if ((Get-VMFirmware -VM $vm).SecureBoot -eq [Microsoft.HyperV.PowerShell.OnOffState]::On) + { + $config.configuration.secure_boot_enabled.'#text' = 'True' + } + else + { + $config.configuration.secure_boot_enabled.'#text' = 'False' + } + } + + $vm_controllers | ForEach { + $controller = $config.CreateElement('controller' + $_.ControllerNumber) + $_.Drives | ForEach { + $drive = $config.CreateElement('drive' + ($_.DiskNumber + 0)) + $drive_path = $config.CreateElement('pathname') + $drive_path.SetAttribute('type', 'string') + $drive_path.AppendChild($config.CreateTextNode($_.Path)) + $drive_type = $config.CreateElement('type') + $drive_type.SetAttribute('type', 'string') + if ($_ -is [Microsoft.HyperV.PowerShell.HardDiskDrive]) + { + $drive_type.AppendChild($config.CreateTextNode('VHD')) + } + elseif ($_ -is [Microsoft.HyperV.PowerShell.DvdDrive]) + { + $drive_type.AppendChild($config.CreateTextNode('ISO')) + } + else + { + $drive_type.AppendChild($config.CreateTextNode('NONE')) + } + $drive.AppendChild($drive_path) + $drive.AppendChild($drive_type) + $controller.AppendChild($drive) + } + $controller_type.AppendChild($controller) + } + if ($controller_type.Name -ne 'vm-controllers') + { + $config.SelectSingleNode('/configuration/vm-controllers').AppendChild($controller_type) + } + + $config.Save([IO.Path]::Combine($path, $vm.Name, 'Virtual Machines', 'box.xml')) +} ` var ps powershell.PowerShellCmd