Add option to allow/disable fstab modification

Defaults to allow modification of fstab
This commit is contained in:
sophia 2020-05-27 15:05:33 -04:00
parent 1dc761a6c4
commit fe7968315b
6 changed files with 55 additions and 0 deletions

View File

@ -8,6 +8,13 @@ module VagrantPlugins
class PersistMountSharedFolder
extend SyncedFolder::UnixMountHelpers
# Inserts fstab entry for a set of synced folders. Will fully replace
# the currently managed group of Vagrant managed entries. Note, passing
# empty list of folders will just remove entries
#
# @param [Machine] machine The machine to run the action on
# @param [Map<String, Map>] A map of folders to add to fstab
# @param [String] mount type, ex. vboxfs, cifs, etc
def self.persist_mount_shared_folder(machine, fstab_folders, mount_type)
if fstab_folders.empty?
self.remove_vagrant_managed_fstab(machine)

View File

@ -22,6 +22,7 @@ module VagrantPlugins
DEFAULT_VM_NAME = :default
attr_accessor :allowed_synced_folder_types
attr_accessor :allow_fstab_modification
attr_accessor :base_mac
attr_accessor :base_address
attr_accessor :boot_timeout
@ -56,6 +57,7 @@ module VagrantPlugins
@logger = Log4r::Logger.new("vagrant::config::vm")
@allowed_synced_folder_types = UNSET_VALUE
@allow_fstab_modification = UNSET_VALUE
@base_mac = UNSET_VALUE
@base_address = UNSET_VALUE
@boot_timeout = UNSET_VALUE
@ -452,6 +454,7 @@ module VagrantPlugins
def finalize!
# Defaults
@allow_fstab_modification = true if @allow_fstab_modification == UNSET_VALUE
@allowed_synced_folder_types = nil if @allowed_synced_folder_types == UNSET_VALUE
@base_mac = nil if @base_mac == UNSET_VALUE
@base_address = nil if @base_address == UNSET_VALUE
@ -928,6 +931,12 @@ module VagrantPlugins
end
end
if ![TrueClass, FalseClass].include?(@allow_fstab_modification.class)
errors["vm"] << I18n.t("vagrant.config.vm.config_type",
option: "allow_fstab_modification", given: @allow_fstab_modification.class, required: "Boolean"
)
end
errors
end

View File

@ -61,6 +61,10 @@ module VagrantPlugins
end
end
if machine.guest.capability?(:persist_mount_shared_folder)
# If Vagrant has been configured to not allow fstab modification, then
# execute the guest capability with an empty list in order to ensure
# there is no Vagrant managed fstab entries.
fstab_folders = [] if !machine.config.vm.allow_fstab_modification
machine.guest.capability(:persist_mount_shared_folder, fstab_folders, "vboxsf")
end
end

View File

@ -1947,6 +1947,7 @@ en:
box_download_options_not_converted: |-
Something went wrong converting VM config `box_download_options`. Value for provided key '%{missing_key}' is invalid type. Should be String or Bool
clone_and_box: "Only one of clone or box can be specified."
config_type: "Found '%{option}' specified as type '%{given}', should be '%{required}'"
hostname_invalid_characters: |-
The hostname set for the VM '%{name}' should only contain letters, numbers,
hyphens or dots. It cannot start with a hyphen or dot.

View File

@ -6,10 +6,24 @@ require Vagrant.source_root.join("plugins/providers/virtualbox/synced_folder")
describe VagrantPlugins::ProviderVirtualBox::SyncedFolder do
include_context "unit"
let(:vm_config) do
double("vm_config").tap do |vm_config|
allow(vm_config).to receive(:allow_fstab_modification).and_return(true)
end
end
let(:machine_config) do
double("machine_config").tap do |top_config|
allow(top_config).to receive(:vm).and_return(vm_config)
end
end
let(:machine) do
double("machine").tap do |m|
allow(m).to receive(:provider_config).and_return(VagrantPlugins::ProviderVirtualBox::Config.new)
allow(m).to receive(:provider_name).and_return(:virtualbox)
allow(m).to receive(:config).and_return(machine_config)
end
end
@ -73,6 +87,21 @@ describe VagrantPlugins::ProviderVirtualBox::SyncedFolder do
test_folders = folders.merge(no_guestpath_folder)
subject.enable(machine, test_folders, nil)
end
context "fstab modification disabled" do
before do
allow(vm_config).to receive(:allow_fstab_modification).and_return(false)
end
it "should not persist folders" do
expect(guest).to receive(:capability).with(:mount_virtualbox_shared_folder, "folder", any_args)
expect(guest).not_to receive(:capability).with(:mount_virtualbox_shared_folder, "no_guestpath_folder", any_args)
expect(guest).to receive(:capability?).with(:persist_mount_shared_folder).and_return(true)
expect(guest).to receive(:capability).with(:persist_mount_shared_folder, [], "vboxsf")
test_folders = folders.merge(no_guestpath_folder)
subject.enable(machine, test_folders, nil)
end
end
end
describe "#prepare" do

View File

@ -16,6 +16,11 @@ machine that Vagrant manages.
## Available Settings
- `config.vm.allow_fstab_modification` (boolean) - If true, will add fstab
entries for synced folders. If false, no modifications to fstab will be made
by Vagrant. Note, this may mean that folders will not be automatically mounted
on machine reboot. Defaults to true.
- `config.vm.base_mac` (string) - The MAC address to be assigned to the default
NAT interface on the guest. _Support for this option is provider dependent._