Skip to content

Commit 04117c8

Browse files
committed
Added support for cygwin guest
1 parent 0f5c864 commit 04117c8

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
8+
def self.is_cygwin(machine, opts)
9+
return true
10+
end
11+
12+
def self.sshfs_forward_get_umount_cmd(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 casched 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+
end
43+
end
44+
end
45+
end

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

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

24+
def self.is_cygwin(machine, opts)
25+
return false
26+
end
27+
2428
def self.sshfs_forward_is_folder_mounted(machine, opts)
2529
mounted = false
2630
guest_path = opts[:guestpath]
@@ -29,6 +33,11 @@ def self.sshfs_forward_is_folder_mounted(machine, opts)
2933
# can safely say it is not mounted
3034
exists = machine.communicate.test("test -e #{guest_path}", sudo: true)
3135
return false unless exists
36+
# If path exists in cygwin it is considered mounted
37+
# ( see comments for creating mountpoint lower )
38+
if self.is_cygwin(machine, opts)
39+
return true
40+
end
3241

3342
# find the absolute path so that we can properly check if it is mounted
3443
# https://github.com/dustymabe/vagrant-sshfs/issues/44
@@ -66,9 +75,13 @@ def self.sshfs_forward_mount_folder(machine, opts)
6675
:shell_expand_guest_path, opts[:guestpath])
6776

6877
# 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}")
78+
# for sshfs-win/cygwin to work, directory must NOT exist in place
79+
# of future mount (unlike for sshfs/linux)
80+
unless self.is_cygwin(machine, opts)
81+
machine.communicate.tap do |comm|
82+
comm.sudo("mkdir -p #{expanded_guest_path}")
83+
comm.sudo("chmod 777 #{expanded_guest_path}")
84+
end
7285
end
7386

7487
# Mount path information: if arbitrary host mounting then
@@ -98,6 +111,10 @@ def self.sshfs_forward_mount_folder(machine, opts)
98111
end
99112
end
100113

114+
def self.sshfs_forward_get_umount_cmd(expanded_guest_path)
115+
return "umount #{expanded_guest_path}"
116+
end
117+
101118
def self.sshfs_forward_unmount_folder(machine, opts)
102119
# opts contains something like:
103120
# { :type=>:sshfs,
@@ -120,7 +137,7 @@ def self.sshfs_forward_unmount_folder(machine, opts)
120137

121138
# Build up the command and connect
122139
error_class = VagrantPlugins::SyncedFolderSSHFS::Errors::SSHFSUnmountFailed
123-
cmd = "umount #{expanded_guest_path}"
140+
cmd = self.sshfs_forward_get_umount_cmd(expanded_guest_path)
124141
machine.communicate.sudo(
125142
cmd, error_class: error_class, error_key: :unmount_failed)
126143
end
@@ -210,7 +227,12 @@ def self.sshfs_slave_mount(machine, opts, hostpath, expanded_guest_path)
210227

211228
# The remote sshfs command that will run (in slave mode)
212229
sshfs_opts+= ' -o slave '
213-
sshfs_cmd = "sudo -E sshfs :#{hostpath} #{expanded_guest_path}"
230+
if self.is_cygwin(machine, opts)
231+
# cygwin does not support -E option
232+
sshfs_cmd = "sudo sshfs :#{hostpath} #{expanded_guest_path}"
233+
else
234+
sshfs_cmd = "sudo -E sshfs :#{hostpath} #{expanded_guest_path}"
235+
end
214236
sshfs_cmd+= sshfs_opts + ' ' + sshfs_opts_append + ' '
215237

216238
# 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)