Automatically mount virtual box shared folder when machine reboots

This is done by adding the mount to fstab
ref: https://askubuntu.com/questions/252853/how-to-mount-a-virtualbox-shared-folder-at-startup

Note, add `nofail` to mount options so as to not stop machine from booting if device disappears
This commit is contained in:
sophia 2020-05-01 10:37:30 -04:00
parent 821b120873
commit 298027b70e
2 changed files with 9 additions and 2 deletions

View File

@ -11,8 +11,9 @@ module VagrantPlugins
@@logger.debug("Mounting #{name} (#{options[:hostpath]} to #{guestpath})")
builtin_mount_type = "-cit vboxsf"
addon_mount_type = "-t vboxsf"
mount_type = "vboxsf"
builtin_mount_type = "-cit #{mount_type}"
addon_mount_type = "-t #{mount_type}"
mount_options = options.fetch(:mount_options, [])
detected_ids = detect_owner_group_ids(machine, guest_path, mount_options, options)
@ -27,6 +28,10 @@ module VagrantPlugins
# Create the guest path if it doesn't exist
machine.communicate.sudo("mkdir -p #{guest_path}")
# Add mount to fstab so that if the machine reboots, will remount
fstab_entry = "#{name} #{guest_path} #{mount_type} #{mount_options},nofail 0 0"
machine.communicate.sudo("grep -x '#{fstab_entry}' /etc/fstab || echo '#{fstab_entry}' >> /etc/fstab")
stderr = ""
result = machine.communicate.sudo(mount_command, error_check: false) do |type, data|
stderr << data if type == :stderr

View File

@ -43,6 +43,7 @@ describe "VagrantPlugins::GuestLinux::Cap::MountVirtualBoxSharedFolder" do
expect(comm).to receive(:execute).with("id -u #{mount_owner}", anything).and_yield(:stdout, mount_uid)
expect(comm).to receive(:execute).with("getent group #{mount_group}", anything).and_yield(:stdout, "vagrant:x:#{mount_gid}:")
expect(comm).to receive(:sudo).with("mount -t vboxsf -o uid=#{mount_uid},gid=#{mount_gid} #{mount_name} #{mount_guest_path}", anything)
expect(comm).to receive(:sudo).with(/\/etc\/fstab/)
cap.mount_virtualbox_shared_folder(machine, mount_name, mount_guest_path, folder_options)
end
@ -50,6 +51,7 @@ describe "VagrantPlugins::GuestLinux::Cap::MountVirtualBoxSharedFolder" do
expect(comm).to receive(:execute).with("id -u #{mount_owner}", anything).and_yield(:stdout, mount_uid)
expect(comm).to receive(:execute).with("getent group #{mount_group}", anything).and_yield(:stdout, "vagrant:x:#{mount_gid}:")
expect(comm).to receive(:sudo).with("mount -t vboxsf -o uid=#{mount_uid},gid=#{mount_gid} #{mount_name} #{mount_guest_path}", anything)
expect(comm).to receive(:sudo).with(/\/etc\/fstab/)
expect(comm).to receive(:sudo).with("chown #{mount_uid}:#{mount_gid} #{mount_guest_path}")
cap.mount_virtualbox_shared_folder(machine, mount_name, mount_guest_path, folder_options)
end