Skip to content

Commit 72dba6d

Browse files
committed
Added support for cygwin guest
1 parent 0f5c864 commit 72dba6d

File tree

4 files changed

+112
-6
lines changed

4 files changed

+112
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module VagrantPlugins
2+
module GuestCygwin
3+
module Cap
4+
class SSHFSClient
5+
def self.sshfs_installed(machine)
6+
machine.communicate.test("type sshfs")
7+
end
8+
end
9+
end
10+
end
11+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
require_relative "../linux/sshfs_forward_mount"
2+
3+
module VagrantPlugins
4+
module GuestCygwin
5+
module Cap
6+
class MountSSHFS < VagrantPlugins::GuestLinux::Cap::MountSSHFS
7+
def self.sshfs_command
8+
# cygwin does not have sudo command
9+
"env sshfs"
10+
end
11+
12+
def self.get_umount_command(expanded_guest_path)
13+
# sshfs-win mount is not seen as mount by cygwin,
14+
# so we cannot unmount it using umount,
15+
# we need to kill sshfs process ( which causes umount )
16+
17+
cmd = 'sh -c "'
18+
# iterate over cmdlines of all cygwin processes
19+
cmd += 'for cmdline in /proc/*/cmdline ; do'
20+
# if command starts with sshfs
21+
cmd += ' if strings -n 1 \\"\\${cmdline}\\" | head -n 1 | grep -q \'^sshfs\\$\''
22+
# and contains #{expanded_guest_path}
23+
cmd += " && strings -n 1 \\\"\\${cmdline}\\\" | grep -q '^#{expanded_guest_path}\\$' ;"
24+
cmd += ' then'
25+
# get pid from proc path
26+
cmd += ' pid=\\"\\$( basename \\"\\$( dirname \\"\\${cmdline}\\" )\\" )\\" ;'
27+
cmd += ' printf \'Syncing cached writes ...\\\\n\' ;'
28+
# synchronize cashed writes to filesystems (just in case)
29+
cmd += ' sync ;'
30+
cmd += ' printf \'Killing sshfs process: %s ...\\\\n\' \\"\\${pid}\\" ;'
31+
# kill sshfs process
32+
cmd += ' kill \\"\\${pid}\\" ;'
33+
# break the loop
34+
cmd += ' break ;'
35+
cmd += ' fi'
36+
cmd += ' done'
37+
cmd += '"'
38+
39+
return cmd
40+
end
41+
42+
def self.create_mount_point(machine, guest_path)
43+
# for sshfs-win/cygwin to work, directory must NOT exist in place
44+
# of future mount (unlike for sshfs/linux)
45+
end
46+
47+
def self.sshfs_forward_is_folder_mounted(machine, opts)
48+
guest_path = opts[:guestpath]
49+
# If path exists in cygwin it is considered mounted
50+
# ( see comments for create_mount_point higher )
51+
return machine.communicate.test("test -e #{guest_path}", sudo: true)
52+
end
53+
end
54+
end
55+
end
56+
end

lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ def self.list_mounts_command
2121
"cat /proc/mounts"
2222
end
2323

24+
def self.sshfs_command
25+
"sudo -E sshfs"
26+
end
27+
28+
def self.get_umount_command(expanded_guest_path)
29+
return "umount #{expanded_guest_path}"
30+
end
31+
32+
def self.create_mount_point(machine, expanded_guest_path)
33+
machine.communicate.tap do |comm|
34+
comm.sudo("mkdir -p #{expanded_guest_path}")
35+
comm.sudo("chmod 777 #{expanded_guest_path}")
36+
end
37+
end
38+
2439
def self.sshfs_forward_is_folder_mounted(machine, opts)
2540
mounted = false
2641
guest_path = opts[:guestpath]
@@ -66,10 +81,7 @@ def self.sshfs_forward_mount_folder(machine, opts)
6681
:shell_expand_guest_path, opts[:guestpath])
6782

6883
# Create the mountpoint inside the guest
69-
machine.communicate.tap do |comm|
70-
comm.sudo("mkdir -p #{expanded_guest_path}")
71-
comm.sudo("chmod 777 #{expanded_guest_path}")
72-
end
84+
self.create_mount_point(machine, expanded_guest_path)
7385

7486
# Mount path information: if arbitrary host mounting then
7587
# take as is. If not, then expand the hostpath to the real
@@ -120,7 +132,7 @@ def self.sshfs_forward_unmount_folder(machine, opts)
120132

121133
# Build up the command and connect
122134
error_class = VagrantPlugins::SyncedFolderSSHFS::Errors::SSHFSUnmountFailed
123-
cmd = "umount #{expanded_guest_path}"
135+
cmd = self.get_umount_command(expanded_guest_path)
124136
machine.communicate.sudo(
125137
cmd, error_class: error_class, error_key: :unmount_failed)
126138
end
@@ -210,7 +222,8 @@ def self.sshfs_slave_mount(machine, opts, hostpath, expanded_guest_path)
210222

211223
# The remote sshfs command that will run (in slave mode)
212224
sshfs_opts+= ' -o slave '
213-
sshfs_cmd = "sudo -E sshfs :#{hostpath} #{expanded_guest_path}"
225+
sshfs_cmd = self.sshfs_command
226+
sshfs_cmd += " :#{hostpath} #{expanded_guest_path}"
214227
sshfs_cmd+= sshfs_opts + ' ' + sshfs_opts_append + ' '
215228

216229
# The ssh command to connect to guest and then launch sshfs

lib/vagrant-sshfs/plugin.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,32 @@ class Plugin < Vagrant.plugin("2")
186186
require_relative "cap/guest/freebsd/sshfs_client"
187187
VagrantPlugins::GuestFreeBSD::Cap::SSHFSClient
188188
end
189+
190+
guest_capability("cygwin", "sshfs_forward_mount_folder") do
191+
require_relative "cap/guest/cygwin/sshfs_forward_mount"
192+
VagrantPlugins::GuestCygwin::Cap::MountSSHFS
193+
end
194+
195+
guest_capability("cygwin", "sshfs_forward_unmount_folder") do
196+
require_relative "cap/guest/cygwin/sshfs_forward_mount"
197+
VagrantPlugins::GuestCygwin::Cap::MountSSHFS
198+
end
199+
200+
guest_capability("cygwin", "sshfs_forward_is_folder_mounted") do
201+
require_relative "cap/guest/cygwin/sshfs_forward_mount"
202+
VagrantPlugins::GuestCygwin::Cap::MountSSHFS
203+
end
204+
205+
guest_capability("cygwin", "sshfs_get_absolute_path") do
206+
require_relative "cap/guest/linux/sshfs_get_absolute_path"
207+
VagrantPlugins::GuestLinux::Cap::SSHFSGetAbsolutePath
208+
end
209+
210+
guest_capability("cygwin", "sshfs_installed") do
211+
require_relative "cap/guest/cygwin/sshfs_client"
212+
VagrantPlugins::GuestCygwin::Cap::SSHFSClient
213+
end
214+
189215
end
190216
end
191217
end

0 commit comments

Comments
 (0)