2014-04-09 17:46:56 +00:00
|
|
|
require "shellwords"
|
2016-12-01 02:47:08 +00:00
|
|
|
require_relative "../../../synced_folders/unix_mount_helpers"
|
2014-04-09 17:46:56 +00:00
|
|
|
|
2014-02-26 23:54:53 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestLinux
|
|
|
|
module Cap
|
|
|
|
class MountSMBSharedFolder
|
2016-12-01 02:47:08 +00:00
|
|
|
|
|
|
|
extend SyncedFolder::UnixMountHelpers
|
|
|
|
|
2014-02-26 23:54:53 +00:00
|
|
|
def self.mount_smb_shared_folder(machine, name, guestpath, options)
|
|
|
|
expanded_guest_path = machine.guest.capability(
|
|
|
|
:shell_expand_guest_path, guestpath)
|
|
|
|
|
|
|
|
mount_device = "//#{options[:smb_host]}/#{name}"
|
|
|
|
|
2016-12-01 02:47:08 +00:00
|
|
|
mount_options = options.fetch(:mount_options, [])
|
2017-03-24 20:32:49 +00:00
|
|
|
detected_ids = detect_owner_group_ids(machine, guestpath, mount_options, options)
|
2016-12-01 02:47:08 +00:00
|
|
|
mount_uid = detected_ids[:uid]
|
|
|
|
mount_gid = detected_ids[:gid]
|
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] ||= []
|
2017-12-16 00:31:44 +00:00
|
|
|
if machine.env.host.capability?(:smb_mount_options)
|
|
|
|
options[:mount_options] += machine.env.host.capability(:smb_mount_options)
|
|
|
|
else
|
|
|
|
options[:mount_options] << "sec=ntlm"
|
|
|
|
end
|
2015-11-23 19:11:24 +00:00
|
|
|
options[:mount_options] << "credentials=/etc/smb_creds_#{name}"
|
2014-02-26 23:54:53 +00:00
|
|
|
|
|
|
|
mount_options = "-o uid=#{mount_uid},gid=#{mount_gid}"
|
2016-12-01 02:47:08 +00:00
|
|
|
mount_options += ",#{Array(options[:mount_options]).join(",")}" if options[:mount_options]
|
|
|
|
mount_command = "mount -t cifs #{mount_options} #{mount_device} #{expanded_guest_path}"
|
2014-02-26 23:54:53 +00:00
|
|
|
|
|
|
|
# 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.
|
2017-12-14 20:31:43 +00:00
|
|
|
begin
|
|
|
|
retryable(on: Vagrant::Errors::LinuxMountFailed, tries: 10, sleep: 2) do
|
|
|
|
no_such_device = false
|
|
|
|
stderr = ""
|
|
|
|
status = machine.communicate.sudo(mount_command, error_check: false) do |type, data|
|
|
|
|
if type == :stderr
|
|
|
|
no_such_device = true if data =~ /No such device/i
|
|
|
|
stderr += data.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if status != 0 || no_such_device
|
|
|
|
clean_command = mount_command.gsub(smb_password, "PASSWORDHIDDEN")
|
|
|
|
raise Vagrant::Errors::LinuxMountFailed,
|
|
|
|
command: clean_command,
|
|
|
|
output: stderr
|
2014-02-26 23:54:53 +00:00
|
|
|
end
|
|
|
|
end
|
2017-12-14 20:31:43 +00:00
|
|
|
ensure
|
|
|
|
# Always remove credentials file after mounting attempts
|
|
|
|
# have been completed
|
|
|
|
machine.communicate.sudo("rm /etc/smb_creds_#{name}")
|
2014-02-26 23:54:53 +00:00
|
|
|
end
|
|
|
|
|
2016-12-01 02:47:08 +00:00
|
|
|
emit_upstart_notification(machine, expanded_guest_path)
|
2014-02-26 23:54:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|