Skip to content

Commit

Permalink
Merge pull request #56 from dustymabe/dusty-fix-sym-link
Browse files Browse the repository at this point in the history
properly check mounts that are sym links
  • Loading branch information
dustymabe authored Nov 12, 2016
2 parents b0c5bd8 + 9032543 commit 3ddc6a2
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 22 deletions.
9 changes: 5 additions & 4 deletions lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ class MountSSHFS

def self.sshfs_forward_is_folder_mounted(machine, opts)
mounted = false
# expand the guest path so we can handle things like "~/vagrant"
expanded_guest_path = machine.guest.capability(
:shell_expand_guest_path, opts[:guestpath])
# find the absolute path so that we can properly check if it is mounted
# https://github.com/dustymabe/vagrant-sshfs/issues/44
absolute_guest_path = machine.guest.capability(
:sshfs_get_absolute_path, opts[:guestpath])
machine.communicate.execute("cat /proc/mounts") do |type, data|
if type == :stdout
data.each_line do |line|
if line.split()[1] == expanded_guest_path
if line.split()[1] == absolute_guest_path
mounted = true
break
end
Expand Down
25 changes: 25 additions & 0 deletions lib/vagrant-sshfs/cap/guest/linux/sshfs_get_absolute_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module VagrantPlugins
module GuestLinux
module Cap
class SSHFSGetAbsolutePath
def self.sshfs_get_absolute_path(machine, path)
abs_path = ""
machine.communicate.execute("readlink -f #{path}") do |type, data|
if type == :stdout
abs_path = data
end
end

if ! abs_path
# If no real absolute path was detected then error out
error_class = VagrantPlugins::SyncedFolderSSHFS::Errors::SSHFSGetAbsolutePathFailed
raise error_class, path: path
end

# Chomp the string so that any trailing newlines are killed
return abs_path.chomp
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/vagrant-sshfs/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class SSHFSNotInstalledInGuest < SSHFSError
class SSHFSExeNotAvailable < SSHFSError
error_key(:exe_not_in_host)
end

class SSHFSGetAbsolutePathFailed < SSHFSError
error_key(:get_absolute_path_failed)
end
end
end
end
5 changes: 5 additions & 0 deletions lib/vagrant-sshfs/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class Plugin < Vagrant.plugin("2")
VagrantPlugins::GuestLinux::Cap::MountSSHFS
end

guest_capability("linux", "sshfs_get_absolute_path") do
require_relative "cap/guest/linux/sshfs_get_absolute_path"
VagrantPlugins::GuestLinux::Cap::SSHFSGetAbsolutePath
end

guest_capability("redhat", "sshfs_installed") do
require_relative "cap/guest/redhat/sshfs_client"
VagrantPlugins::GuestRedHat::Cap::SSHFSClient
Expand Down
2 changes: 2 additions & 0 deletions locales/synced_folder_sshfs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ en:
try again.
sshfs_not_in_guest: |-
The necessary SSHFS software is not installed in the guest.
get_absolute_path_failed: |-
Could not get the absolute path of the folder within the guest '%{path}'
install_failed_arch: |-
The install of the sshfs client software failed. On Arch this is most likely
because the package lists are not up to date [1] and partial upgrades are not
Expand Down
23 changes: 15 additions & 8 deletions test/misc/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@ export THIRD_PARTY_HOST='192.168.121.73'
export THIRD_PARTY_HOST_USER='vagrant'
export THIRD_PARTY_HOST_PASS='vagrant'

# Next vagrant up - will do 3 mounts (normal, slave, reverse).
# Next vagrant up - will do 4 mounts
# - slave
# - slave with sym link
# - normal
# - reverse
vagrant up

# Next run the script to test the mounts:
$ bash dotests.sh
Testing normal mount!
a57e39fa692f294e860349a9451be67c
Testing slave mount!
e2c4ceac71dc414cb3ed864cff04a917
Testing slave forward mount!
d635332fe7aa4d4fb48e5cb9357bdedf
Testing slave forward mount with a symlink!
d635332fe7aa4d4fb48e5cb9357bdedf
Testing normal forward mount!
6ccc3034df924bd289dd16205bf3d629
Testing reverse mount!
508619e7e68e446c84d1fcdf7e0dc577
508619e7e68e446c84d1fcdf7e0dc577

# We are printing out the machine-id under each mount to prove each
# mount is from a different machine.
# We are printing out the machine-id under each mount. The first two
should be the same, because they are from the same machine. The last
two should be different.
12 changes: 8 additions & 4 deletions test/misc/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ Vagrant.configure(2) do |config|

config.ssh.insert_key = 'true'

# Test a forward slave mount:
# mounting /etc/ from the vagrant host into the guest
config.vm.synced_folder "/etc/", "/tmp/forward_slave_mount_etc/", type: "sshfs"

# Test a forward mount to a location that is a symbolic link
# https://github.com/dustymabe/vagrant-sshfs/issues/44
config.vm.synced_folder "/etc/", "/sbin/forward_slave_mount_sym_link_test/", type: "sshfs"

# Test a forward normal mount:
# mounting a folder from a 3rd party host into guest
config.vm.synced_folder "/etc/", "/tmp/forward_normal_mount_etc/", type: "sshfs",
ssh_host: ENV['THIRD_PARTY_HOST'],
ssh_username: ENV['THIRD_PARTY_HOST_USER'],
ssh_password: ENV['THIRD_PARTY_HOST_PASS']

# Test a forward slave mount:
# mounting /etc/ from the vagrant host into the guest
config.vm.synced_folder "/etc/", "/tmp/forward_slave_mount_etc/", type: "sshfs"

# Test a reverse mount:
# mounting /etc/ from vagrant guest into vagrant host
config.vm.synced_folder "/tmp/reverse_mount_etc/", "/etc", type: "sshfs", reverse: true
Expand Down
16 changes: 10 additions & 6 deletions test/misc/dotests.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#!/bin/bash
set -eu

# Test the three mounts we have done
# Test the four mounts we have done

echo "Testing normal mount!"
vagrant ssh -- cat /tmp/forward_normal_mount_etc/machine-id

echo "Testing slave mount!"
echo -en "Testing slave forward mount!\n\t"
vagrant ssh -- cat /tmp/forward_slave_mount_etc/machine-id

echo "Testing reverse mount!"
# https://github.com/dustymabe/vagrant-sshfs/issues/44
echo -en "Testing slave forward mount with a symlink!\n\t"
vagrant ssh -- cat /usr/sbin/forward_slave_mount_sym_link_test/machine-id

echo -en "Testing normal forward mount!\n\t"
vagrant ssh -- cat /tmp/forward_normal_mount_etc/machine-id

echo -en "Testing reverse mount!\n\t"
cat /tmp/reverse_mount_etc/machine-id

0 comments on commit 3ddc6a2

Please sign in to comment.