2013-04-04 06:01:43 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestLinux
|
|
|
|
module Cap
|
|
|
|
class MountVirtualBoxSharedFolder
|
|
|
|
def self.mount_virtualbox_shared_folder(machine, name, guestpath, options)
|
|
|
|
expanded_guest_path = machine.guest.capability(
|
|
|
|
:shell_expand_guest_path, guestpath)
|
|
|
|
|
2013-09-17 03:51:09 +00:00
|
|
|
mount_commands = []
|
|
|
|
|
|
|
|
# First mount command uses getent to get the group
|
2013-06-06 11:11:49 +00:00
|
|
|
mount_options = "-o uid=`id -u #{options[:owner]}`,gid=`getent group #{options[:group]} | cut -d: -f3`"
|
2013-09-17 03:51:09 +00:00
|
|
|
mount_options += ",#{options[:mount_options].join(",")}" if options[:mount_options]
|
|
|
|
mount_commands << "mount -t vboxsf #{mount_options} #{name} #{expanded_guest_path}"
|
2013-09-01 19:25:21 +00:00
|
|
|
|
2013-09-17 03:51:09 +00:00
|
|
|
# Second mount command uses the old style `id -g`
|
|
|
|
mount_options = "-o uid=`id -u #{options[:owner]}`,gid=`id -g #{options[:group]}`"
|
|
|
|
mount_options += ",#{options[:mount_options].join(",")}" if options[:mount_options]
|
|
|
|
mount_commands << "mount -t vboxsf #{mount_options} #{name} #{expanded_guest_path}"
|
2013-04-04 06:01:43 +00:00
|
|
|
|
|
|
|
# Create the guest path if it doesn't exist
|
|
|
|
machine.communicate.sudo("mkdir -p #{expanded_guest_path}")
|
|
|
|
|
|
|
|
# Attempt to mount the folder. We retry here a few times because
|
|
|
|
# it can fail early on.
|
|
|
|
attempts = 0
|
|
|
|
while true
|
|
|
|
success = true
|
2013-09-17 03:51:09 +00:00
|
|
|
|
|
|
|
mount_commands.each do |command|
|
2013-09-21 00:50:29 +00:00
|
|
|
no_such_device = false
|
|
|
|
status = machine.communicate.sudo(command, error_check: false) do |type, data|
|
|
|
|
no_such_device = true if type == :stderr && data =~ /No such device/i
|
2013-09-17 03:51:09 +00:00
|
|
|
end
|
|
|
|
|
2013-09-21 00:50:29 +00:00
|
|
|
success = status == 0 && !no_such_device
|
2013-09-17 03:51:09 +00:00
|
|
|
break if success
|
2013-04-04 06:01:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
break if success
|
|
|
|
|
|
|
|
attempts += 1
|
2013-09-21 00:52:36 +00:00
|
|
|
if attempts > 10
|
|
|
|
raise Vagrant::Errors::LinuxMountFailed,
|
|
|
|
command: mount_commands.join("\n")
|
|
|
|
end
|
|
|
|
|
2013-04-04 06:01:43 +00:00
|
|
|
sleep 2
|
|
|
|
end
|
|
|
|
|
|
|
|
# Chown the directory to the proper user
|
2013-09-17 03:51:09 +00:00
|
|
|
chown_commands = []
|
|
|
|
chown_commands << "chown `id -u #{options[:owner]}`:`getent group #{options[:group]} " +
|
|
|
|
"| cut -d: -f3` #{expanded_guest_path}"
|
2013-09-22 02:41:06 +00:00
|
|
|
chown_commands << "chown `id -u #{options[:owner]}`:`id -g #{options[:group]}` " +
|
2013-09-17 03:51:09 +00:00
|
|
|
"#{expanded_guest_path}"
|
|
|
|
|
|
|
|
exit_status = machine.communicate.sudo(chown_commands[0], error_check: false)
|
|
|
|
return if exit_status == 0
|
|
|
|
machine.communicate.sudo(chown_commands[1])
|
2013-04-04 06:01:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|