Skip to content

Added support for cygwin guest #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/vagrant-sshfs/cap/guest/cygwin/sshfs_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module VagrantPlugins
module GuestCygwin
module Cap
class SSHFSClient
def self.sshfs_installed(machine)
machine.communicate.test("type sshfs")
end
end
end
end
end
56 changes: 56 additions & 0 deletions lib/vagrant-sshfs/cap/guest/cygwin/sshfs_forward_mount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require_relative "../linux/sshfs_forward_mount"

module VagrantPlugins
module GuestCygwin
module Cap
class MountSSHFS < VagrantPlugins::GuestLinux::Cap::MountSSHFS
def self.sshfs_command
# cygwin does not have sudo command
"env sshfs"
end

def self.get_umount_command(expanded_guest_path)
# sshfs-win mount is not seen as mount by cygwin,
# so we cannot unmount it using umount,
# we need to kill sshfs process ( which causes umount )

cmd = 'sh -c "'
# iterate over cmdlines of all cygwin processes
cmd += 'for cmdline in /proc/*/cmdline ; do'
# if command starts with sshfs
cmd += ' if strings -n 1 \\"\\${cmdline}\\" | head -n 1 | grep -q \'^sshfs\\$\''
# and contains #{expanded_guest_path}
cmd += " && strings -n 1 \\\"\\${cmdline}\\\" | grep -q '^#{expanded_guest_path}\\$' ;"
cmd += ' then'
# get pid from proc path
cmd += ' pid=\\"\\$( basename \\"\\$( dirname \\"\\${cmdline}\\" )\\" )\\" ;'
cmd += ' printf \'Syncing cached writes ...\\\\n\' ;'
# synchronize cashed writes to filesystems (just in case)
cmd += ' sync ;'
cmd += ' printf \'Killing sshfs process: %s ...\\\\n\' \\"\\${pid}\\" ;'
# kill sshfs process
cmd += ' kill \\"\\${pid}\\" ;'
# break the loop
cmd += ' break ;'
cmd += ' fi'
cmd += ' done'
cmd += '"'

return cmd
end

def self.create_mount_point(machine, guest_path)
# for sshfs-win/cygwin to work, directory must NOT exist in place
# of future mount (unlike for sshfs/linux)
end

def self.sshfs_forward_is_folder_mounted(machine, opts)
guest_path = opts[:guestpath]
# If path exists in cygwin it is considered mounted
# ( see comments for create_mount_point higher )
return machine.communicate.test("test -e #{guest_path}", sudo: true)
end
end
end
end
end
25 changes: 19 additions & 6 deletions lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ def self.list_mounts_command
"cat /proc/mounts"
end

def self.sshfs_command
"sudo -E sshfs"
end

def self.get_umount_command(expanded_guest_path)
return "umount #{expanded_guest_path}"
end

def self.create_mount_point(machine, expanded_guest_path)
machine.communicate.tap do |comm|
comm.sudo("mkdir -p #{expanded_guest_path}")
comm.sudo("chmod 777 #{expanded_guest_path}")
end
end

def self.sshfs_forward_is_folder_mounted(machine, opts)
mounted = false
guest_path = opts[:guestpath]
Expand Down Expand Up @@ -66,10 +81,7 @@ def self.sshfs_forward_mount_folder(machine, opts)
:shell_expand_guest_path, opts[:guestpath])

# Create the mountpoint inside the guest
machine.communicate.tap do |comm|
comm.sudo("mkdir -p #{expanded_guest_path}")
comm.sudo("chmod 777 #{expanded_guest_path}")
end
self.create_mount_point(machine, expanded_guest_path)

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

# Build up the command and connect
error_class = VagrantPlugins::SyncedFolderSSHFS::Errors::SSHFSUnmountFailed
cmd = "umount #{expanded_guest_path}"
cmd = self.get_umount_command(expanded_guest_path)
machine.communicate.sudo(
cmd, error_class: error_class, error_key: :unmount_failed)
end
Expand Down Expand Up @@ -210,7 +222,8 @@ def self.sshfs_slave_mount(machine, opts, hostpath, expanded_guest_path)

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

# The ssh command to connect to guest and then launch sshfs
Expand Down
26 changes: 26 additions & 0 deletions lib/vagrant-sshfs/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,32 @@ class Plugin < Vagrant.plugin("2")
require_relative "cap/guest/freebsd/sshfs_client"
VagrantPlugins::GuestFreeBSD::Cap::SSHFSClient
end

guest_capability("cygwin", "sshfs_forward_mount_folder") do
require_relative "cap/guest/cygwin/sshfs_forward_mount"
VagrantPlugins::GuestCygwin::Cap::MountSSHFS
end

guest_capability("cygwin", "sshfs_forward_unmount_folder") do
require_relative "cap/guest/cygwin/sshfs_forward_mount"
VagrantPlugins::GuestCygwin::Cap::MountSSHFS
end

guest_capability("cygwin", "sshfs_forward_is_folder_mounted") do
require_relative "cap/guest/cygwin/sshfs_forward_mount"
VagrantPlugins::GuestCygwin::Cap::MountSSHFS
end

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

guest_capability("cygwin", "sshfs_installed") do
require_relative "cap/guest/cygwin/sshfs_client"
VagrantPlugins::GuestCygwin::Cap::SSHFSClient
end

end
end
end