Make get_port_and_device disk method a driver method instead

This commit is contained in:
Brian Cain 2020-01-23 09:43:32 -08:00
parent 252a7f7a4e
commit b9a72ce8ff
No known key found for this signature in database
GPG key ID: 9FC4639B2E4510A0
4 changed files with 25 additions and 42 deletions

View file

@ -21,24 +21,6 @@ module VagrantPlugins
protected
# TODO: This method is duplicated in configure_disks
#
# @param [Hash] vm_info - A guests information from vboxmanage
# @param [String] disk_uuid - the UUID for the disk we are searching for
# @return [Hash] disk_info - Contains a device and port number
def self.get_port_and_device(vm_info, disk_uuid)
disk = {}
disk_info_key = vm_info.key(disk_uuid)
return disk if !disk_info_key
disk_info = disk_info_key.split("-")
disk[:port] = disk_info[2]
disk[:device] = disk_info[3]
return disk
end
# @param [Vagrant::Machine] machine
# @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
@ -52,7 +34,7 @@ module VagrantPlugins
next
else
LOGGER.warn("Found disk not in Vagrantfile config: '#{d["name"]}'. Removing disk from guest #{machine.name}")
disk_info = get_port_and_device(vm_info, d["uuid"])
disk_info = machine.provider.driver.get_port_and_device(d["uuid"])
machine.ui.warn("Disk '#{d["name"]}' no longer exists in Vagrant config. Removing and closing medium from guest...", prefix: true)

View file

@ -90,8 +90,7 @@ module VagrantPlugins
else
# TODO: What if it needs to be resized?
vm_info = machine.provider.driver.show_vm_info
disk_info = get_port_and_device(vm_info, current_disk)
disk_info = machine.provider.driver.get_port_and_device(current_disk["UUID"])
if disk_info.empty?
LOGGER.warn("Disk '#{disk.name}' is not connected to guest '#{machine.name}', Vagrant will attempt to connect disk to guest")
dsk_info = get_next_port(machine)
@ -185,33 +184,13 @@ module VagrantPlugins
dsk_info
end
# Looks up a defined_disk for a guests information from virtualbox. If
# no matching UUID is found, it returns an empty hash.
#
# @param [Hash] vm_info - Guest info from show_vm_info
# @param [Hash] defined_disk - A specific disk with info from list_hdd
# @return [Hash] disk - A hash with `port` and `device` keys found from a matching UUID in vm_info
def self.get_port_and_device(vm_info, defined_disk)
disk = {}
disk_info_key = vm_info.key(defined_disk["UUID"])
return disk if !disk_info_key
disk_info = disk_info_key.split("-")
disk[:port] = disk_info[2]
disk[:device] = disk_info[3]
return disk
end
def self.resize_disk(machine, disk_config, defined_disk)
machine.ui.detail("Disk '#{disk_config.name}' needs to be resized. Resizing disk...", prefix: true)
if defined_disk["Storage format"] == "VMDK"
LOGGER.warn("Disk type VMDK cannot be resized in VirtualBox. Vagrant will convert disk to VDI format to resize first, and then convert resized disk back to VMDK format")
# grab disk to be resized port and device number
vm_info = machine.provider.driver.show_vm_info
disk_info = get_port_and_device(vm_info, defined_disk)
disk_info = machine.provider.driver.get_port_and_device(defined_disk["UUID"])
# clone disk to vdi formatted disk
vdi_disk_file = vmdk_to_vdi(machine.provider.driver, defined_disk["Location"])

View file

@ -116,6 +116,7 @@ module VagrantPlugins
:execute_command,
:export,
:forward_ports,
:get_port_and_device,
:halt,
:import,
:list_snapshots,

View file

@ -383,6 +383,27 @@ module VagrantPlugins
nil
end
# Returns port and device for an attached disk given a disk uuid. Returns
# empty hash if disk is not attachd to guest
#
# @param [Hash] vm_info - A guests information from vboxmanage
# @param [String] disk_uuid - the UUID for the disk we are searching for
# @return [Hash] disk_info - Contains a device and port number
def get_port_and_device(disk_uuid)
vm_info = show_vm_info
disk = {}
disk_info_key = vm_info.key(disk_uuid)
return disk if !disk_info_key
disk_info = disk_info_key.split("-")
disk[:port] = disk_info[2]
disk[:device] = disk_info[3]
return disk
end
def halt
execute("controlvm", @uuid, "poweroff", retryable: true)
end