2014-04-09 17:46:56 +00:00
|
|
|
require "shellwords"
|
|
|
|
|
2014-02-26 23:54:53 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestLinux
|
|
|
|
module Cap
|
|
|
|
class MountSMBSharedFolder
|
|
|
|
def self.mount_smb_shared_folder(machine, name, guestpath, options)
|
|
|
|
expanded_guest_path = machine.guest.capability(
|
|
|
|
:shell_expand_guest_path, guestpath)
|
|
|
|
|
|
|
|
mount_commands = []
|
|
|
|
mount_device = "//#{options[:smb_host]}/#{name}"
|
|
|
|
|
|
|
|
if options[:owner].is_a? Integer
|
|
|
|
mount_uid = options[:owner]
|
|
|
|
else
|
|
|
|
mount_uid = "`id -u #{options[:owner]}`"
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:group].is_a? Integer
|
|
|
|
mount_gid = options[:group]
|
|
|
|
mount_gid_old = options[:group]
|
|
|
|
else
|
|
|
|
mount_gid = "`getent group #{options[:group]} | cut -d: -f3`"
|
|
|
|
mount_gid_old = "`id -g #{options[:group]}`"
|
|
|
|
end
|
2015-12-24 20:44:58 +00:00
|
|
|
|
2015-03-23 00:47:55 +00:00
|
|
|
# If a domain is provided in the username, separate it
|
|
|
|
username, domain = (options[:smb_username] || '').split('@', 2)
|
2015-11-23 19:11:24 +00:00
|
|
|
smb_password = options[:smb_password]
|
2015-03-23 00:47:55 +00:00
|
|
|
|
2014-02-26 23:54:53 +00:00
|
|
|
options[:mount_options] ||= []
|
|
|
|
options[:mount_options] << "sec=ntlm"
|
2015-11-23 19:11:24 +00:00
|
|
|
options[:mount_options] << "credentials=/etc/smb_creds_#{name}"
|
2014-02-26 23:54:53 +00:00
|
|
|
|
|
|
|
# First mount command uses getent to get the group
|
|
|
|
mount_options = "-o uid=#{mount_uid},gid=#{mount_gid}"
|
|
|
|
mount_options += ",#{options[:mount_options].join(",")}" if options[:mount_options]
|
|
|
|
mount_commands << "mount -t cifs #{mount_options} #{mount_device} #{expanded_guest_path}"
|
|
|
|
|
|
|
|
# Second mount command uses the old style `id -g`
|
|
|
|
mount_options = "-o uid=#{mount_uid},gid=#{mount_gid_old}"
|
|
|
|
mount_options += ",#{options[:mount_options].join(",")}" if options[:mount_options]
|
|
|
|
mount_commands << "mount -t cifs #{mount_options} #{mount_device} #{expanded_guest_path}"
|
|
|
|
|
|
|
|
# Create the guest path if it doesn't exist
|
|
|
|
machine.communicate.sudo("mkdir -p #{expanded_guest_path}")
|
|
|
|
|
2015-11-23 19:11:24 +00:00
|
|
|
# Write the credentials file
|
|
|
|
machine.communicate.sudo(<<-SCRIPT)
|
2016-02-29 12:19:45 +00:00
|
|
|
cat <<"EOF" >/etc/smb_creds_#{name}
|
2015-11-23 19:11:24 +00:00
|
|
|
username=#{username}
|
|
|
|
password=#{smb_password}
|
|
|
|
#{domain ? "domain=#{domain}" : ""}
|
|
|
|
EOF
|
|
|
|
chmod 0600 /etc/smb_creds_#{name}
|
|
|
|
SCRIPT
|
|
|
|
|
2014-02-26 23:54:53 +00:00
|
|
|
# Attempt to mount the folder. We retry here a few times because
|
|
|
|
# it can fail early on.
|
|
|
|
attempts = 0
|
|
|
|
while true
|
|
|
|
success = true
|
|
|
|
|
2014-10-14 17:25:13 +00:00
|
|
|
stderr = ""
|
2014-02-26 23:54:53 +00:00
|
|
|
mount_commands.each do |command|
|
|
|
|
no_such_device = false
|
2014-10-14 17:25:13 +00:00
|
|
|
stderr = ""
|
2014-02-26 23:54:53 +00:00
|
|
|
status = machine.communicate.sudo(command, error_check: false) do |type, data|
|
2014-10-14 17:25:13 +00:00
|
|
|
if type == :stderr
|
|
|
|
no_such_device = true if data =~ /No such device/i
|
|
|
|
stderr += data.to_s
|
|
|
|
end
|
2014-02-26 23:54:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
success = status == 0 && !no_such_device
|
|
|
|
break if success
|
|
|
|
end
|
|
|
|
|
|
|
|
break if success
|
|
|
|
|
|
|
|
attempts += 1
|
|
|
|
if attempts > 10
|
2014-04-09 17:42:39 +00:00
|
|
|
command = mount_commands.join("\n")
|
2014-04-09 17:46:56 +00:00
|
|
|
command.gsub!(smb_password, "PASSWORDHIDDEN")
|
2014-04-09 17:42:39 +00:00
|
|
|
|
2014-02-26 23:54:53 +00:00
|
|
|
raise Vagrant::Errors::LinuxMountFailed,
|
2014-10-14 17:25:13 +00:00
|
|
|
command: command,
|
|
|
|
output: stderr
|
2014-02-26 23:54:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
sleep 2
|
|
|
|
end
|
|
|
|
|
|
|
|
# Emit an upstart event if we can
|
2015-11-19 00:18:29 +00:00
|
|
|
machine.communicate.sudo <<-SCRIPT
|
|
|
|
if command -v /sbin/init &>/dev/null && /sbin/init --version | grep upstart &>/dev/null; then
|
|
|
|
/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}'
|
|
|
|
fi
|
|
|
|
SCRIPT
|
2014-02-26 23:54:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|