From 2e879aa06f9b69a850bd9d7476540d63dd07c554 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Thu, 28 May 2020 11:37:31 -0400 Subject: [PATCH] Scan storage controllers for DVD cleanup --- .../providers/virtualbox/cap/cleanup_disks.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/plugins/providers/virtualbox/cap/cleanup_disks.rb b/plugins/providers/virtualbox/cap/cleanup_disks.rb index 4c215118e..8467d6f46 100644 --- a/plugins/providers/virtualbox/cap/cleanup_disks.rb +++ b/plugins/providers/virtualbox/cap/cleanup_disks.rb @@ -26,8 +26,8 @@ module VagrantPlugins # @param [VagrantPlugins::Kernel_V2::VagrantConfigDisk] defined_disks # @param [Hash] disk_meta - A hash of all the previously defined disks from the last configure_disk action def self.handle_cleanup_disk(machine, defined_disks, disk_meta) - storage_controller = machine.provider.driver.storage_controllers.detect { |c| c[:type] == "IntelAhci" } - primary_disk = storage_controller[:attachments].detect { |a| a[:port] == "0" && a[:device] == "0" }[:uuid] + sata_controller = machine.provider.driver.storage_controllers.detect { |c| c[:type] == "IntelAhci" } + primary_disk = sata_controller[:attachments].detect { |a| a[:port] == "0" && a[:device] == "0" }[:uuid] unless disk_meta.nil? disk_meta.each do |d| @@ -43,6 +43,7 @@ module VagrantPlugins if disk_info.empty? LOGGER.warn("Disk '#{d["name"]}' not attached to guest, but still exists.") else + # TODO: write test for sata controller with another name machine.provider.driver.remove_disk(disk_info[:port], disk_info[:device]) end @@ -56,8 +57,7 @@ module VagrantPlugins # @param [VagrantPlugins::Kernel_V2::VagrantConfigDisk] defined_dvds # @param [Hash] dvd_meta - A hash of all the previously defined dvds from the last configure_disk action def self.handle_cleanup_dvd(machine, defined_dvds, dvd_meta) - controller = "IDE Controller" - vm_info = machine.provider.driver.show_vm_info + ide_controller = machine.provider.driver.storage_controllers.detect { |c| c[:type] == "PIIX4" } unless dvd_meta.nil? dvd_meta.each do |d| @@ -66,13 +66,10 @@ module VagrantPlugins next else LOGGER.warn("Found dvd not in Vagrantfile config: '#{d["name"]}'. Removing dvd from guest #{machine.name}") - (0..1).each do |port| - (0..1).each do |device| - if d["uuid"] == vm_info["#{controller}-ImageUUID-#{port}-#{device}"] - machine.ui.warn("DVD '#{d["name"]}' no longer exists in Vagrant config. Removing medium from guest...", prefix: true) - machine.provider.driver.remove_disk(port.to_s, device.to_s, controller) - end - end + attachments = ide_controller[:attachments].select { |a| a[:uuid] == d["uuid"] } + attachments.each do |attachment| + machine.ui.warn("DVD '#{d["name"]}' no longer exists in Vagrant config. Removing medium from guest...", prefix: true) + machine.provider.driver.remove_disk(attachment[:port].to_s, attachment[:device].to_s, ide_controller[:name]) end end end