From 694104b005dcb4c4764e31c3769bed47e09217f4 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Thu, 14 Jun 2018 16:40:37 -0700 Subject: [PATCH 1/2] Attempt to provide helper error message on failed VM import When importing a Hyper-V VM fails, attempt to manually build a system definition and inspect the result to determine cause of the failure. --- .../scripts/utils/VagrantVM/VagrantVM.psm1 | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 b/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 index b555a0797..4ca3ef3fd 100644 --- a/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 +++ b/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 @@ -83,6 +83,13 @@ function New-VagrantVMVMCX { VirtualMachinePath = $DataPath; } $VMConfig = (Hyper-V\Compare-VM -Copy -GenerateNewID @NewVMConfig) + + # If the config is empty it means the import failed. Attempt to provide + # context for failure + if($VMConfig -eq $null) { + Error-VagrantVMImport -VMConfigFile $VMConfigFile + } + $VM = $VMConfig.VM $Gen = $VM.Generation @@ -338,6 +345,68 @@ VirtualMachine. The cloned Hyper-V VM. #> } +function Error-VagrantVMImport { + param ( + [parameter(Mandatory=$true)] + [string] $VMConfigFile + ) + + $ManagementService = Get-WmiObject -Namespace 'root\virtualization\v2' -Class 'Msvm_VirtualSystemManagementService' + $Result = $ManagementService.ImportSystemDefinition($VMConfigFile, $null, $true) + if($Result.ReturnValue -eq 0) { + throw "Unknown error encountered while importing VM" + } elseif($Result.ReturnValue -eq 4096) { + $job = Get-WmiObject -Namespace 'root\virtualization\v2' -Query 'select * from Msvm_ConcreteJob' | Where {$_.__PATH -eq $Result.Job} + while($job.JobState -eq 3 -or $job.JobState -eq 4) { + start-sleep 1 + $job = Get-WmiObject -Namespace 'root\virtualization\v2' -Query 'select * from Msvm_ConcreteJob' | Where {$_.__PATH -eq $Result.Job} + } + $ErrorMsg = $job.ErrorDescription + "`n`n" + $ErrorMsg = $ErrorMsg + "Error Code: " + $job.ErrorCode + "`n" + $cause = "Unknown" + switch($job.ErrorCode) { + 32768 { $cause = "Failed" } + 32769 { $cause = "Access Denied" } + 32770 { $cause = "Not Supported" } + 32771 { $cause = "Status is unknown" } + 32772 { $cause = "Timeout" } + 32773 { $cause = "Invalid parameter" } + 32774 { $cause = "System is in use" } + 32775 { $cause = "Invalid state for this operation" } + 32776 { $cause = "Incorrect data type" } + 32777 { $cause = "System is not available" } + 32778 { $cause = "Out of memory" } + 32779 { $cause = "File in Use" } + 32784 { $cause = "VM version is unsupported" } + } + $ErrorMsg = $ErrorMsg + "Cause: ${cause}" + throw $ErrorMsg + } else { + throw "Failed to run VM import job. Error value: ${Result.ReturnValue}" + } +<# +.SYNOPSIS + +Determines cause of error for VM import. + +.DESCRIPTION + +Runs a local import of the VM configuration and attempts to determine +the underlying cause of the import failure. + +.PARAMETER VMConfigFile +Path to the Hyper-V VM configuration file. + +.INPUTS + +None. + +.OUTPUTS + +None. +#> +} + # Vagrant VM configuration functions function Set-VagrantVMMemory { @@ -368,9 +437,10 @@ function Set-VagrantVMMemory { } if($DynamicMemory) { - if($MemoryMaximumBytes < $MemoryMinimumBytes) { + if($MemoryMaximumBytes -lt $MemoryMinimumBytes) { throw "Maximum memory value is less than required minimum memory value." - } else if ($MemoryMaximumBytes < $MemoryStartupBytes) { + } + if ($MemoryMaximumBytes -lt $MemoryStartupBytes) { throw "Maximum memory value is less than configured startup memory value." } From 8fbf27772a422b69f25c3655861d77839cc60a31 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 15 Jun 2018 07:48:14 -0700 Subject: [PATCH 2/2] Always ensure full path to configuration file on import --- .../hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 b/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 index 4ca3ef3fd..a380a38f3 100644 --- a/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 +++ b/plugins/providers/hyperv/scripts/utils/VagrantVM/VagrantVM.psm1 @@ -352,7 +352,13 @@ function Error-VagrantVMImport { ) $ManagementService = Get-WmiObject -Namespace 'root\virtualization\v2' -Class 'Msvm_VirtualSystemManagementService' - $Result = $ManagementService.ImportSystemDefinition($VMConfigFile, $null, $true) + + # Relative path names will fail when attempting to import a system + # definition so always ensure we are using the full path to the + # configuration file. + $FullPathFile = (Resolve-Path $VMConfigFile).Path + + $Result = $ManagementService.ImportSystemDefinition($FullPathFile, $null, $true) if($Result.ReturnValue -eq 0) { throw "Unknown error encountered while importing VM" } elseif($Result.ReturnValue -eq 4096) {