From cde39e26ba0c9cdfe8ef308de5b9794a0cafbaa0 Mon Sep 17 00:00:00 2001 From: sophia Date: Thu, 30 Jul 2020 16:11:21 -0500 Subject: [PATCH] Make mount options a synced_folder capability --- .../action/builtin/mixin_synced_folders.rb | 6 ++++- lib/vagrant/machine.rb | 25 ++++++++++++++++++- .../cap/mount_virtualbox_shared_folder.rb | 2 +- .../linux/cap/persist_mount_shared_folder.rb | 4 +-- plugins/kernel_v2/config/vm.rb | 8 +++--- .../providers/virtualbox/cap/mount_options.rb | 23 +++++++++++++++++ plugins/providers/virtualbox/plugin.rb | 5 ++++ plugins/synced_folders/unix_mount_helpers.rb | 12 --------- 8 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 plugins/providers/virtualbox/cap/mount_options.rb diff --git a/lib/vagrant/action/builtin/mixin_synced_folders.rb b/lib/vagrant/action/builtin/mixin_synced_folders.rb index 1209f5d69..ad7c4d528 100644 --- a/lib/vagrant/action/builtin/mixin_synced_folders.rb +++ b/lib/vagrant/action/builtin/mixin_synced_folders.rb @@ -36,7 +36,11 @@ module Vagrant # Find the proper implementation ordered.each do |_, key, impl| - return key if impl.new.usable?(machine) + begin + return key if impl.new.usable?(machine) + rescue + # Don't do anything. If an error is raised, the impl is not usable + end end return nil diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index f8e6bd6cb..7f57c8309 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -11,7 +11,7 @@ module Vagrant # API for querying the state and making state changes to the machine, which # is backed by any sort of provider (VirtualBox, VMware, etc.). class Machine - include Vagrant::Action::Builtin::MixinSyncedFolders + extend Vagrant::Action::Builtin::MixinSyncedFolders # The box that is backing this machine. # @@ -619,6 +619,29 @@ module Vagrant end end + # This returns the set of shared folders that should be done for + # this machine. It returns the folders in a hash keyed by the + # implementation class for the synced folders. + # + # @return [Hash>] + def synced_folders + self.class.synced_folders(self) + end + + # Returns the SyncedFolder object associated with this machine. + # + # @return [List] + def synced_folder_types + return @synced_folder_types if defined?(@synced_folder_types) + plugins = Vagrant.plugin("2").manager.synced_folders + @synced_folder_types = synced_folders.map { |type, folders| + impl = plugins[type][0].new() + impl._initialize(self, type) + [type, impl] + }.to_h + @synced_folder_types + end + protected # Returns the path to the file that stores the UID. diff --git a/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb b/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb index a2a206745..fcc4b0343 100644 --- a/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb +++ b/plugins/guests/linux/cap/mount_virtualbox_shared_folder.rb @@ -16,7 +16,7 @@ module VagrantPlugins builtin_mount_type = "-cit #{VB_MOUNT_TYPE}" addon_mount_type = "-t #{VB_MOUNT_TYPE}" - mount_options, mount_uid, mount_gid = mount_options(machine, name, guest_path, options) + mount_options, mount_uid, mount_gid = machine.synced_folder_types[:virtualbox].capability(:mount_options, name, guestpath, options) mount_command = "mount #{addon_mount_type} -o #{mount_options} #{name} #{guest_path}" # Create the guest path if it doesn't exist diff --git a/plugins/guests/linux/cap/persist_mount_shared_folder.rb b/plugins/guests/linux/cap/persist_mount_shared_folder.rb index 076cd18b0..9e7dda16d 100644 --- a/plugins/guests/linux/cap/persist_mount_shared_folder.rb +++ b/plugins/guests/linux/cap/persist_mount_shared_folder.rb @@ -22,7 +22,7 @@ module VagrantPlugins end export_folders = fstab_folders.map do |name, data| guest_path = Shellwords.escape(data[:guestpath]) - mount_options, mount_uid, mount_gid = mount_options(machine, name, guest_path, data) + mount_options, mount_uid, mount_gid = machine.synced_folder_types[:virtualbox].capability(:mount_options, name, guest_path, data) mount_options = "#{mount_options},nofail" { name: name, @@ -45,4 +45,4 @@ module VagrantPlugins end end end -end \ No newline at end of file +end diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index 4bf64f355..451fa77c6 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -727,11 +727,9 @@ module VagrantPlugins if @allow_fstab_modification == UNSET_VALUE plugins = Vagrant.plugin("2").manager.synced_folders - machine.synced_folders.each do |type, _| - instance = plugins[type.to_sym][0].new - instance._initialize(machine, type) - if instance.capability?(:default_fstab_modification) - if instance.capability(:default_fstab_modification) == false + machine.synced_folder_types.each do |_, inst| + if inst.capability?(:default_fstab_modification) + if inst.capability(:default_fstab_modification) == false @allow_fstab_modification = false break end diff --git a/plugins/providers/virtualbox/cap/mount_options.rb b/plugins/providers/virtualbox/cap/mount_options.rb new file mode 100644 index 000000000..1639ba8e9 --- /dev/null +++ b/plugins/providers/virtualbox/cap/mount_options.rb @@ -0,0 +1,23 @@ +require_relative "../../../synced_folders/unix_mount_helpers" + +module VagrantPlugins + module ProviderVirtualBox + module Cap + module MountOptions + extend VagrantPlugins::SyncedFolder::UnixMountHelpers + + def self.mount_options(machine, name, guest_path, options) + mount_options = options.fetch(:mount_options, []) + detected_ids = detect_owner_group_ids(machine, guest_path, mount_options, options) + mount_uid = detected_ids[:uid] + mount_gid = detected_ids[:gid] + + mount_options << "uid=#{mount_uid}" + mount_options << "gid=#{mount_gid}" + mount_options = mount_options.join(',') + return mount_options, mount_uid, mount_gid + end + end + end + end +end diff --git a/plugins/providers/virtualbox/plugin.rb b/plugins/providers/virtualbox/plugin.rb index 8a44e613e..e193040ca 100644 --- a/plugins/providers/virtualbox/plugin.rb +++ b/plugins/providers/virtualbox/plugin.rb @@ -68,6 +68,11 @@ module VagrantPlugins require_relative "cap" Cap end + + synced_folder_capability(:virtualbox, "mount_options") do + require_relative "cap/mount_options" + Cap::MountOptions + end end autoload :Action, File.expand_path("../action", __FILE__) diff --git a/plugins/synced_folders/unix_mount_helpers.rb b/plugins/synced_folders/unix_mount_helpers.rb index f49462bb0..d96077091 100644 --- a/plugins/synced_folders/unix_mount_helpers.rb +++ b/plugins/synced_folders/unix_mount_helpers.rb @@ -99,18 +99,6 @@ module VagrantPlugins fi EOH end - - def mount_options(machine, name, guest_path, options) - mount_options = options.fetch(:mount_options, []) - detected_ids = detect_owner_group_ids(machine, guest_path, mount_options, options) - mount_uid = detected_ids[:uid] - mount_gid = detected_ids[:gid] - - mount_options << "uid=#{mount_uid}" - mount_options << "gid=#{mount_gid}" - mount_options = mount_options.join(',') - return mount_options, mount_uid, mount_gid - end end end end