Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-VHD Getting mounted storage instance failed for VHDX due to ResourceBusy #188

Open
craigjbutler opened this issue Feb 12, 2023 · 5 comments

Comments

@craigjbutler
Copy link

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

Terraform v1.3.3

Affected Resource(s)

  • hyperv_vhd (Resource)

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Terraform Configuration Files

resource "hyperv_vhd" "u22_lts" {
  path = "${var.vhd_folder}\\${var.vm_name}.vhdx"
  source = var.vm_template
}
..
resource "hyperv_machine_instance" "linux" {
  name                   = var.vm_name
  memory_maximum_bytes   = var.vm_memory * 1024 * 1024
  memory_minimum_bytes   = var.vm_memory * 1024 * 1024
...
  # Create a hard disk drive
  hard_disk_drives {
    controller_number   = 0
    controller_location = 0
    path                = hyperv_vhd.u22_lts.path
  }

Debug Output

I added some debug code to the provider to track down the cause of the issue. The output is as follows:

2023-02-12T02:19:36.820+1100 [WARN] unexpected data: myserver/local/hyperv:stdout="The result of the Powershell 
call was: run command operation returned code=1
stderr:

stdOut:
Get-VHD : Getting the mounted storage instance for the path C:\Users\test\VirtualHardDisks\hv-test3.vhdx failed.
The operation cannot be performed while the object is in use.
At C:\Users\test\AppData\Local\Temp\shell-terraform-2Lb9B4V3ZIQeAOzsFcZ8gaGvPEE.ps1:7 char:15
+     $vhdObject = Get-VHD -path $path | %{ @{
    + CategoryInfo          : ResourceBusy: (:) [Get-VHD], VirtualizationException
    + FullyQualifiedErrorId : ObjectInUse,Microsoft.Vhd.PowerShell.Cmdlets.GetVHD, 
2023-02-12T02:19:36.820+1100 [TRACE] provider.stdio: waiting for stdio data

Result data: { 0 0 0  0 0 0 false 0 0 0 0  Unknown Unknown}

Panic Output

NA

Expected Behavior

Windows VHDx should have been created as per resource definition.

Actual Behavior

Terraform apply failed with error message above

Steps to Reproduce

Please list the steps required to reproduce the issue:

  1. Place VHDx template on network share (in this example a NFS server)
  2. terraform plan
  3. terraform apply

Important Factoids

  • Nothing that I can think of, HyperV is running on Windows 11 Pro
  • As a workaround I found that if I modify the provider code (api/hyperv-winrm/vhd.go) and catch the error and repeat the call (after a short delay) it completes successfully
func (c *ClientConfig) GetVhd(ctx context.Context, path string) (result api.Vhd, err error) {
	for {
		err = c.WinRmClient.RunScriptWithResult(ctx, getVhdTemplate, getVhdArgs{
			Path: path,
		}, &result)
        # Workaround Get-VHD ResourceBusy error
        if err != nil && strings.Contains(err.Error(), getVHDErrorMessage) {
          time.Sleep(10 * time.Second)
          continue
        }
       break
  ..

Example with workaround:

module.hvU22Instance.hyperv_vhd.u22_lts: Still creating... [10s elapsed]
..
module.hvU22Instance.hyperv_vhd.u22_lts: Creation complete after 1m14s [id=C:\Users\test\VirtualHardDisks\hv-test3.vhdx]
module.hvU22Instance.hyperv_machine_instance.linux: Creating...
..
module.hvU22Instance.hyperv_machine_instance.linux: Creation complete after 1m9s [id=hv-test3]
Apply complete! Resources: 9 added, 0 changed, 0 destroyed.

References

  • None
@dataenligne
Copy link

+1 - I'm also having the exact same issue.. would be great to have this fixed.

@fsdrw08
Copy link
Contributor

fsdrw08 commented May 11, 2023

any plan to fix this issue? I have the same issue here

@KimRechnagel
Copy link

I'm experiencing the same error when deploying multiple VHDs without the -parallelism=1 option.
Terraform v1.7.5
source = "taliesins/hyperv"
version = "1.2.1"

@kosh000
Copy link

kosh000 commented Mar 25, 2024

edited the code in api/hyperv-winrm/vhd.go.

func (c *ClientConfig) GetVhd(ctx context.Context, path string) (result api.Vhd, err error) {
	time.Sleep(200 * time.Second)
        // it takes my SSD 200 to do the clone.
        // so I have slept this ***** for 200 sec.
	err = c.WinRmClient.RunScriptWithResult(ctx, getVhdTemplate, getVhdArgs{
		Path: path,
	}, &result)
	return result, err
}

running the apply with terraform -apply -parallelism=1
built the binaries and it worked for me. Just wanted to share this.

I am not an expert at this but if an exception can be added for error to delay for like 10 sec as said by @craigjbutler. It should fix the box.

@skynetlab1
Copy link

This is not an error because of terraform and not need to change anything, the error is because -$vhdObject = Get-VHD -path $path expercts var of type object and not string. Here is an example of variable
variable "vhdx" {
type = object({
path = string
}).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants