2016-06-25 01:19:58 +00:00
|
|
|
require "shellwords"
|
|
|
|
|
|
|
|
require "vagrant/util/retryable"
|
|
|
|
|
2013-04-04 06:01:43 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestLinux
|
|
|
|
module Cap
|
|
|
|
class MountVirtualBoxSharedFolder
|
2016-08-09 21:25:16 +00:00
|
|
|
@@logger = Log4r::Logger.new("vagrant::guest::linux::mount_virtualbox_shared_folder")
|
|
|
|
|
2016-06-25 01:19:58 +00:00
|
|
|
extend Vagrant::Util::Retryable
|
|
|
|
|
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
|
|
|
|
2016-08-09 21:25:16 +00:00
|
|
|
if options[:owner].to_i.to_s == options[:owner].to_s
|
2013-10-18 02:40:21 +00:00
|
|
|
mount_uid = options[:owner]
|
2016-08-09 21:25:16 +00:00
|
|
|
@@logger.debug("Owner user ID (provided): #{mount_uid}")
|
2013-10-18 02:40:21 +00:00
|
|
|
else
|
2016-08-09 21:25:16 +00:00
|
|
|
output = {stdout: '', stderr: ''}
|
|
|
|
uid_command = "id -u #{options[:owner]}"
|
|
|
|
machine.communicate.execute(uid_command,
|
|
|
|
error_class: Vagrant::Errors::VirtualBoxMountFailed,
|
|
|
|
error_key: :virtualbox_mount_failed,
|
|
|
|
command: uid_command,
|
|
|
|
output: output[:stderr]
|
|
|
|
) { |type, data| output[type] << data if output[type] }
|
|
|
|
mount_uid = output[:stdout].chomp
|
|
|
|
@@logger.debug("Owner user ID (lookup): #{options[:owner]} -> #{mount_uid}")
|
2013-10-18 02:40:21 +00:00
|
|
|
end
|
|
|
|
|
2016-08-09 21:25:16 +00:00
|
|
|
if options[:group].to_i.to_s == options[:group].to_s
|
2013-10-18 02:40:21 +00:00
|
|
|
mount_gid = options[:group]
|
2016-08-09 21:25:16 +00:00
|
|
|
@@logger.debug("Owner group ID (provided): #{mount_gid}")
|
2013-10-18 02:40:21 +00:00
|
|
|
else
|
2016-08-09 21:25:16 +00:00
|
|
|
begin
|
|
|
|
output = {stdout: '', stderr: ''}
|
|
|
|
gid_command = "getent group #{options[:group]}"
|
|
|
|
machine.communicate.execute(gid_command,
|
|
|
|
error_class: Vagrant::Errors::VirtualBoxMountFailed,
|
|
|
|
error_key: :virtualbox_mount_failed,
|
|
|
|
command: gid_command,
|
|
|
|
output: output[:stderr]
|
|
|
|
) { |type, data| output[type] << data if output[type] }
|
|
|
|
mount_gid = output[:stdout].split(':').at(2)
|
|
|
|
@@logger.debug("Owner group ID (lookup): #{options[:group]} -> #{mount_gid}")
|
|
|
|
rescue Vagrant::Errors::VirtualBoxMountFailed
|
|
|
|
if options[:owner] == options[:group]
|
|
|
|
@@logger.debug("Failed to locate group `#{options[:group]}`. Group name matches owner. Fetching effective group ID.")
|
|
|
|
output = {stdout: ''}
|
|
|
|
result = machine.communicate.execute("id -g #{options[:owner]}",
|
|
|
|
error_check: false
|
|
|
|
) { |type, data| output[type] << data if output[type] }
|
|
|
|
mount_gid = output[:stdout].chomp if result == 0
|
|
|
|
@@logger.debug("Owner group ID (effective): #{mount_gid}")
|
|
|
|
end
|
|
|
|
raise unless mount_gid
|
|
|
|
end
|
2013-10-18 02:40:21 +00:00
|
|
|
end
|
|
|
|
|
2016-08-09 21:25:16 +00:00
|
|
|
mount_options = options.fetch(:mount_options, [])
|
|
|
|
mount_options += ["uid=#{mount_uid}", "gid=#{mount_gid}"]
|
|
|
|
mount_options = mount_options.join(',')
|
|
|
|
mount_command = "mount -t vboxsf -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
|
|
|
|
|
|
|
# Attempt to mount the folder. We retry here a few times because
|
|
|
|
# it can fail early on.
|
2016-06-25 01:19:58 +00:00
|
|
|
stderr = ""
|
|
|
|
retryable(on: Vagrant::Errors::VirtualBoxMountFailed, tries: 3, sleep: 5) do
|
2016-08-09 21:25:16 +00:00
|
|
|
machine.communicate.sudo(mount_command,
|
2016-06-25 01:19:58 +00:00
|
|
|
error_class: Vagrant::Errors::VirtualBoxMountFailed,
|
|
|
|
error_key: :virtualbox_mount_failed,
|
2016-08-09 21:25:16 +00:00
|
|
|
command: mount_command,
|
2016-06-25 01:19:58 +00:00
|
|
|
output: stderr,
|
|
|
|
) { |type, data| stderr = data if type == :stderr }
|
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
|
|
|
|
|
|
|
# Emit an upstart event if we can
|
2016-06-25 01:19:58 +00:00
|
|
|
machine.communicate.sudo <<-EOH.gsub(/^ {12}/, "")
|
2016-11-29 07:10:25 +00:00
|
|
|
if command -v /sbin/init && /sbin/init 2>/dev/null --version | grep upstart; then
|
2016-06-25 01:19:58 +00:00
|
|
|
/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guest_path}
|
|
|
|
fi
|
|
|
|
EOH
|
2013-04-04 06:01:43 +00:00
|
|
|
end
|
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
|