From 5be7989f2d6228977ebf030c6b181aafe0b0848c Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 10 Dec 2019 16:00:27 -0800 Subject: [PATCH] Add basic driver methods for disk operations --- .../virtualbox/cap/configure_disks.rb | 20 ++++---- plugins/providers/virtualbox/driver/base.rb | 49 +++++++++++++++++++ 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/plugins/providers/virtualbox/cap/configure_disks.rb b/plugins/providers/virtualbox/cap/configure_disks.rb index 2074d1a55..102a51159 100644 --- a/plugins/providers/virtualbox/cap/configure_disks.rb +++ b/plugins/providers/virtualbox/cap/configure_disks.rb @@ -7,20 +7,20 @@ module VagrantPlugins # @param [Vagrant::Machine] machine # @param [VagrantPlugins::Kernel_V2::VagrantConfigDisk] defined_disks def self.configure_disks(machine, defined_disks) - return nil if defined_disks.empty? - disks = current_vm_disks(machine) + return if defined_disks.empty? + + current_disks = machine.provider.driver.list_hdds + # Compare current disks to config, and if there's a delta, adjust accordingly + # + # Compare by name if possible + defined_disks.each do |disk| + if disk.type == :disk + end + end end protected - # Maybe move these into the virtualbox driver?? - # Versioning might be an issue :shrug: - - def self.current_vm_disks(machine) - disks = {} - info = machine.provider.driver.show_vm_info(machine.id) - end - def self.vmdk_to_vdi(driver) end diff --git a/plugins/providers/virtualbox/driver/base.rb b/plugins/providers/virtualbox/driver/base.rb index 23f29cd6d..cbd4baf93 100644 --- a/plugins/providers/virtualbox/driver/base.rb +++ b/plugins/providers/virtualbox/driver/base.rb @@ -383,6 +383,55 @@ module VagrantPlugins info end + # @return [Array] hdds An array of hashes of harddrive info for a guest + def list_hdds + hdds = [] + tmp_drive = {} + execute('list', 'hdds', retryable: true).split("\n").each do |line| + if line == "" # separator between disks + hdds << tmp_drive + tmp_drive = {} + next + end + parts = line.partition(":") + key = parts.first.strip + value = parts.last.strip + tmp_drive[key] = value + end + hdds << tmp_drive unless tmp_drive.empty? + + hdds + end + + # @param [String] disk_file + # @param [Integer] disk_size_in_mb + def resize_disk(disk_file, disk_size_in_mb) + # todo: better error handling for this execute + # todo: MEDIUM changes if virtualbox is older than 5. Need a proper check/switch + # Maybe move this into version_4, then version_5 + execute("modify#{MEDIUM}", disk_file, '--resize', disk_size_in_mb.to_s) + end + + # @param [String] uui - virtual machines uuid + # @param [Hash] disk - disk to attach + def attach_disk(uuid, disk) + controller = disk[:controller] + port = disk[:port] + device = disk[:device] + file = disk[:file] + + # todo: hard set to type hdd, need to look if all types are compatible with these flags + execute('storageattach', uuid, '--storagectl', controller, '--port', port, '--device', device, '--type', 'hdd', '--medium', file) + end + + # Removes a disk from the given virtual machine + # + # @param [String] disk_file + def remove_disk(disk_file) + # todo: better error handling for this execute + execute("closemedium", disk_file, '--delete') + end + # Execute the given subcommand for VBoxManage and return the output. def execute(*command, &block) # Get the options hash if it exists