2016-12-01 02:47:08 +00:00
|
|
|
require_relative "../../../synced_folders/unix_mount_helpers"
|
2016-06-25 01:19:58 +00:00
|
|
|
|
2013-04-04 06:01:43 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestLinux
|
|
|
|
module Cap
|
|
|
|
class MountVirtualBoxSharedFolder
|
2016-12-01 02:47:08 +00:00
|
|
|
extend SyncedFolder::UnixMountHelpers
|
2016-06-25 01:19:58 +00:00
|
|
|
|
2013-04-04 06:01:43 +00:00
|
|
|
def self.mount_virtualbox_shared_folder(machine, name, guestpath, options)
|
2016-06-25 01:19:58 +00:00
|
|
|
guest_path = Shellwords.escape(guestpath)
|
2013-04-04 06:01:43 +00:00
|
|
|
|
2016-08-09 21:25:16 +00:00
|
|
|
@@logger.debug("Mounting #{name} (#{options[:hostpath]} to #{guestpath})")
|
2013-09-17 03:51:09 +00:00
|
|
|
|
2018-05-07 20:00:41 +00:00
|
|
|
builtin_mount_type = "-cit vboxsf"
|
|
|
|
addon_mount_type = "-t vboxsf"
|
|
|
|
|
2016-08-09 21:25:16 +00:00
|
|
|
mount_options = options.fetch(:mount_options, [])
|
2016-12-01 02:47:08 +00:00
|
|
|
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}"
|
2016-08-09 21:25:16 +00:00
|
|
|
mount_options = mount_options.join(',')
|
2018-05-07 20:00:41 +00:00
|
|
|
mount_command = "mount #{addon_mount_type} -o #{mount_options} #{name} #{guest_path}"
|
2013-04-04 06:01:43 +00:00
|
|
|
|
|
|
|
# Create the guest path if it doesn't exist
|
2016-06-25 01:19:58 +00:00
|
|
|
machine.communicate.sudo("mkdir -p #{guest_path}")
|
2013-04-04 06:01:43 +00:00
|
|
|
|
2016-06-25 01:19:58 +00:00
|
|
|
stderr = ""
|
2018-05-07 20:00:41 +00:00
|
|
|
result = machine.communicate.sudo(mount_command, error_check: false) do |type, data|
|
|
|
|
stderr << data if type == :stderr
|
|
|
|
end
|
|
|
|
|
|
|
|
if result != 0
|
|
|
|
if stderr.include?("-cit")
|
|
|
|
@@logger.info("Detected builtin vboxsf module, modifying mount command")
|
|
|
|
mount_command.sub!(addon_mount_type, builtin_mount_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Attempt to mount the folder. We retry here a few times because
|
|
|
|
# it can fail early on.
|
|
|
|
stderr = ""
|
|
|
|
retryable(on: Vagrant::Errors::VirtualBoxMountFailed, tries: 3, sleep: 5) do
|
|
|
|
machine.communicate.sudo(mount_command,
|
|
|
|
error_class: Vagrant::Errors::VirtualBoxMountFailed,
|
|
|
|
error_key: :virtualbox_mount_failed,
|
|
|
|
command: mount_command,
|
|
|
|
output: stderr,
|
|
|
|
) { |type, data| stderr = data if type == :stderr }
|
|
|
|
end
|
2013-04-04 06:01:43 +00:00
|
|
|
end
|
|
|
|
|
2014-02-04 15:01:14 +00:00
|
|
|
# Chown the directory to the proper user. We skip this if the
|
|
|
|
# mount options contained a readonly flag, because it won't work.
|
2014-02-08 00:04:36 +00:00
|
|
|
if !options[:mount_options] || !options[:mount_options].include?("ro")
|
2016-08-09 21:25:16 +00:00
|
|
|
chown_command = "chown #{mount_uid}:#{mount_gid} #{guest_path}"
|
|
|
|
machine.communicate.sudo(chown_command)
|
2014-02-04 15:01:14 +00:00
|
|
|
end
|
2013-11-25 19:31:15 +00:00
|
|
|
|
2016-12-01 02:47:08 +00:00
|
|
|
emit_upstart_notification(machine, guest_path)
|
2013-04-04 06:01:43 +00:00
|
|
|
end
|
2014-04-16 21:59:23 +00:00
|
|
|
|
2016-12-01 02:47:08 +00:00
|
|
|
|
2014-04-16 21:59:23 +00:00
|
|
|
def self.unmount_virtualbox_shared_folder(machine, guestpath, options)
|
2016-06-25 01:19:58 +00:00
|
|
|
guest_path = Shellwords.escape(guestpath)
|
|
|
|
|
|
|
|
result = machine.communicate.sudo("umount #{guest_path}", error_check: false)
|
2014-04-16 21:59:23 +00:00
|
|
|
if result == 0
|
2016-06-25 01:19:58 +00:00
|
|
|
machine.communicate.sudo("rmdir #{guest_path}", error_check: false)
|
2014-04-16 21:59:23 +00:00
|
|
|
end
|
|
|
|
end
|
2013-04-04 06:01:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|