From b387f0e15d9c0bca7319b7ec7896b82b6da16249 Mon Sep 17 00:00:00 2001 From: Matija K Date: Sun, 24 May 2015 12:39:28 +0200 Subject: [PATCH 1/3] Capability to mount smb shares in darwin guests --- lib/vagrant/errors.rb | 4 +++ .../darwin/cap/choose_addressable_ip_addr.rb | 20 +++++++++++++ .../darwin/cap/mount_smb_shared_folder.rb | 29 +++++++++++++++++++ plugins/guests/darwin/plugin.rb | 10 +++++++ 4 files changed, 63 insertions(+) create mode 100644 plugins/guests/darwin/cap/choose_addressable_ip_addr.rb create mode 100644 plugins/guests/darwin/cap/mount_smb_shared_folder.rb diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index de002ffed..3072d50ff 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -316,6 +316,10 @@ module Vagrant error_key(:corrupt_machine_index) end + class DarwinMountFailed < VagrantError + error_key(:darwin_mount_failed) + end + class DarwinNFSMountFailed < VagrantError error_key(:darwin_nfs_mount_failed) end diff --git a/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb b/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb new file mode 100644 index 000000000..f5b578ebd --- /dev/null +++ b/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb @@ -0,0 +1,20 @@ +module VagrantPlugins + module GuestDarwin + module Cap + module ChooseAddressableIPAddr + def self.choose_addressable_ip_addr(machine, possible) + machine.communicate.tap do |comm| + possible.each do |ip| + command = "ping -c1 -t1 #{ip}" + if comm.test(command) + return ip + end + end + end + + nil + end + end + end + end +end diff --git a/plugins/guests/darwin/cap/mount_smb_shared_folder.rb b/plugins/guests/darwin/cap/mount_smb_shared_folder.rb new file mode 100644 index 000000000..2d47f97ce --- /dev/null +++ b/plugins/guests/darwin/cap/mount_smb_shared_folder.rb @@ -0,0 +1,29 @@ +require "vagrant/util/retryable" +require "shellwords" + +module VagrantPlugins + module GuestDarwin + module Cap + class MountSMBSharedFolder + extend Vagrant::Util::Retryable + def self.mount_smb_shared_folder(machine, name, guestpath, options) + expanded_guest_path = machine.guest.capability(:shell_expand_guest_path, guestpath) + machine.communicate.execute("mkdir -p #{expanded_guest_path}") + + smb_password = Shellwords.shellescape(options[:smb_password]) + mount_options = options[:mount_options]; + + mount_command = "mount -t smbfs " + + (mount_options ? "-o '#{mount_options.join(",")}' " : "") + + "'//#{options[:smb_username]}:#{smb_password}@#{options[:smb_host]}/#{name}' " + + "#{expanded_guest_path}" + retryable(on: Vagrant::Errors::DarwinMountFailed, tries: 10, sleep: 5) do + machine.communicate.sudo( + mount_command, + error_class: Vagrant::Errors::DarwinMountFailed) + end + end + end + end + end +end diff --git a/plugins/guests/darwin/plugin.rb b/plugins/guests/darwin/plugin.rb index 305154fde..8e6dc279b 100644 --- a/plugins/guests/darwin/plugin.rb +++ b/plugins/guests/darwin/plugin.rb @@ -16,6 +16,11 @@ module VagrantPlugins Cap::ChangeHostName end + guest_capability("darwin", "choose_addressable_ip_addr") do + require_relative "cap/choose_addressable_ip_addr" + Cap::ChooseAddressableIPAddr + end + guest_capability("darwin", "configure_networks") do require_relative "cap/configure_networks" Cap::ConfigureNetworks @@ -36,6 +41,11 @@ module VagrantPlugins Cap::MountNFSFolder end + guest_capability("darwin", "mount_smb_shared_folder") do + require_relative "cap/mount_smb_shared_folder" + Cap::MountSMBSharedFolder + end + guest_capability("darwin", "mount_vmware_shared_folder") do require_relative "cap/mount_vmware_shared_folder" Cap::MountVmwareSharedFolder From 09eec472f62ae31583666f6199ef98674185ea93 Mon Sep 17 00:00:00 2001 From: Matija K Date: Sun, 24 May 2015 15:11:34 +0200 Subject: [PATCH 2/3] Mounting as regular user instead of root --- plugins/guests/darwin/cap/mount_smb_shared_folder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/guests/darwin/cap/mount_smb_shared_folder.rb b/plugins/guests/darwin/cap/mount_smb_shared_folder.rb index 2d47f97ce..acd2b33a9 100644 --- a/plugins/guests/darwin/cap/mount_smb_shared_folder.rb +++ b/plugins/guests/darwin/cap/mount_smb_shared_folder.rb @@ -18,7 +18,7 @@ module VagrantPlugins "'//#{options[:smb_username]}:#{smb_password}@#{options[:smb_host]}/#{name}' " + "#{expanded_guest_path}" retryable(on: Vagrant::Errors::DarwinMountFailed, tries: 10, sleep: 5) do - machine.communicate.sudo( + machine.communicate.execute( mount_command, error_class: Vagrant::Errors::DarwinMountFailed) end From 1c04934d89e3e67399274409633e7be7a9571159 Mon Sep 17 00:00:00 2001 From: alh84001 Date: Mon, 25 May 2015 15:35:06 +0200 Subject: [PATCH 3/3] Mounting to paths where user has no write permissions (e.g. /) --- .../guests/darwin/cap/mount_smb_shared_folder.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/plugins/guests/darwin/cap/mount_smb_shared_folder.rb b/plugins/guests/darwin/cap/mount_smb_shared_folder.rb index acd2b33a9..5f1cdaec4 100644 --- a/plugins/guests/darwin/cap/mount_smb_shared_folder.rb +++ b/plugins/guests/darwin/cap/mount_smb_shared_folder.rb @@ -8,16 +8,24 @@ module VagrantPlugins extend Vagrant::Util::Retryable def self.mount_smb_shared_folder(machine, name, guestpath, options) expanded_guest_path = machine.guest.capability(:shell_expand_guest_path, guestpath) - machine.communicate.execute("mkdir -p #{expanded_guest_path}") + + mount_point_owner = options[:owner]; + if mount_point_owner + machine.communicate.sudo("mkdir -p #{expanded_guest_path}") + machine.communicate.sudo("chown #{mount_point_owner} #{expanded_guest_path}") + else + # fallback to assumption that user has permission + # to create the specified mountpoint + machine.communicate.execute("mkdir -p #{expanded_guest_path}") + end smb_password = Shellwords.shellescape(options[:smb_password]) mount_options = options[:mount_options]; - mount_command = "mount -t smbfs " + (mount_options ? "-o '#{mount_options.join(",")}' " : "") + "'//#{options[:smb_username]}:#{smb_password}@#{options[:smb_host]}/#{name}' " + "#{expanded_guest_path}" - retryable(on: Vagrant::Errors::DarwinMountFailed, tries: 10, sleep: 5) do + retryable(on: Vagrant::Errors::DarwinMountFailed, tries: 10, sleep: 2) do machine.communicate.execute( mount_command, error_class: Vagrant::Errors::DarwinMountFailed)