From 5405aaac82ab38924cc80f4d19bf86babae5d523 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 14 Apr 2020 14:01:39 -0700 Subject: [PATCH] Remove disk file after detaching from hyper-v guest --- plugins/providers/hyperv/cap/cleanup_disks.rb | 5 +---- plugins/providers/hyperv/driver.rb | 5 +++-- plugins/providers/hyperv/scripts/remove_disk_drive.ps1 | 6 +++++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/plugins/providers/hyperv/cap/cleanup_disks.rb b/plugins/providers/hyperv/cap/cleanup_disks.rb index 23ebb775e..3a9efd7ac 100644 --- a/plugins/providers/hyperv/cap/cleanup_disks.rb +++ b/plugins/providers/hyperv/cap/cleanup_disks.rb @@ -43,10 +43,7 @@ module VagrantPlugins disk_actual = all_disks.select { |a| a["Path"] == d["path"] }.first - machine.provider.driver.remove_disk(disk_actual["ControllerType"], disk_actual["ControllerNumber"], disk_actual["ControllerLocation"]) - - # TODO: delete disk - # maybe we can just "dismount" instead of removing the disk all together + machine.provider.driver.remove_disk(disk_actual["ControllerType"], disk_actual["ControllerNumber"], disk_actual["ControllerLocation"], disk_actual["Path"]) end end end diff --git a/plugins/providers/hyperv/driver.rb b/plugins/providers/hyperv/driver.rb index 6808a7e67..fb9044517 100644 --- a/plugins/providers/hyperv/driver.rb +++ b/plugins/providers/hyperv/driver.rb @@ -56,9 +56,10 @@ module VagrantPlugins # @param [String] controller_number # @param [String] controller_location # @param [Hash] opts - def remove_disk(controller_type, controller_number, controller_location, **opts) + def remove_disk(controller_type, controller_number, controller_location, disk_file_path, **opts) execute(:remove_disk_drive, VmId: @vm_id, ControllerType: controller_type, - ControllerNumber: controller_number, ControllerLocation: controller_location) + ControllerNumber: controller_number, ControllerLocation: controller_location, + DiskFilePath: disk_file_path) end # @param [String] path diff --git a/plugins/providers/hyperv/scripts/remove_disk_drive.ps1 b/plugins/providers/hyperv/scripts/remove_disk_drive.ps1 index 0d24d7ed5..caff08e68 100644 --- a/plugins/providers/hyperv/scripts/remove_disk_drive.ps1 +++ b/plugins/providers/hyperv/scripts/remove_disk_drive.ps1 @@ -8,13 +8,17 @@ param( [Parameter(Mandatory=$true)] [string]$ControllerNumber, [Parameter(Mandatory=$true)] - [string]$ControllerLocation + [string]$ControllerLocation, + [Parameter(Mandatory=$true)] + [string]$DiskFilePath ) try { $VM = Hyper-V\Get-VM -Id $VmId Hyper-v\Remove-VMHardDiskDrive -VMName $VM.Name -ControllerType $ControllerType -ControllerNumber $ControllerNumber -ControllerLocation $ControllerLocation + + Remove-Item -Path $DiskFilePath } catch { Write-ErrorMessage "Failed to remove disk ${DiskFilePath} to VM ${VM}: ${PSItem}" exit 1