From 72dba6d7dcc9ff22bdde6a8d678de4f1c0eb9f1f Mon Sep 17 00:00:00 2001 From: Zdenek Zambersky Date: Thu, 31 Mar 2022 17:18:59 +0200 Subject: [PATCH] Added support for cygwin guest --- .../cap/guest/cygwin/sshfs_client.rb | 11 ++++ .../cap/guest/cygwin/sshfs_forward_mount.rb | 56 +++++++++++++++++++ .../cap/guest/linux/sshfs_forward_mount.rb | 25 +++++++-- lib/vagrant-sshfs/plugin.rb | 26 +++++++++ 4 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 lib/vagrant-sshfs/cap/guest/cygwin/sshfs_client.rb create mode 100644 lib/vagrant-sshfs/cap/guest/cygwin/sshfs_forward_mount.rb diff --git a/lib/vagrant-sshfs/cap/guest/cygwin/sshfs_client.rb b/lib/vagrant-sshfs/cap/guest/cygwin/sshfs_client.rb new file mode 100644 index 0000000..302198e --- /dev/null +++ b/lib/vagrant-sshfs/cap/guest/cygwin/sshfs_client.rb @@ -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 diff --git a/lib/vagrant-sshfs/cap/guest/cygwin/sshfs_forward_mount.rb b/lib/vagrant-sshfs/cap/guest/cygwin/sshfs_forward_mount.rb new file mode 100644 index 0000000..4a60d0b --- /dev/null +++ b/lib/vagrant-sshfs/cap/guest/cygwin/sshfs_forward_mount.rb @@ -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 diff --git a/lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb b/lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb index 3dbc804..cd629de 100644 --- a/lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb +++ b/lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb @@ -21,6 +21,21 @@ module VagrantPlugins "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] @@ -66,10 +81,7 @@ module VagrantPlugins :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 @@ -120,7 +132,7 @@ module VagrantPlugins # 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 @@ -210,7 +222,8 @@ module VagrantPlugins # 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 diff --git a/lib/vagrant-sshfs/plugin.rb b/lib/vagrant-sshfs/plugin.rb index 0b651e9..74edc96 100644 --- a/lib/vagrant-sshfs/plugin.rb +++ b/lib/vagrant-sshfs/plugin.rb @@ -186,6 +186,32 @@ module VagrantPlugins 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 -- 2.41.0