Class: VagrantPlugins::GuestLinux::Cap::MountSSHFS

Inherits:
Object
  • Object
show all
Extended by:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb

Constant Summary collapse

@@logger =
Log4r::Logger.new("vagrant::synced_folders::sshfs_mount")

Class Method Summary collapse

Class Method Details

.list_mounts_commandObject



20
21
22
# File 'lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb', line 20

def self.list_mounts_command
  "cat /proc/mounts"
end

.sshfs_forward_is_folder_mounted(machine, opts) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb', line 24

def self.sshfs_forward_is_folder_mounted(machine, opts)
  mounted = false
  guest_path = opts[:guestpath]

  # If the path doesn't exist at all in the machine then we
  # can safely say it is not mounted
  exists = machine.communicate.test("test -e #{guest_path}", sudo: true)
  return false unless exists

  # 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, guest_path)

  # consult /proc/mounts to see if it is mounted or not
  machine.communicate.execute(self.list_mounts_command) do |type, data|
    if type == :stdout
      data.each_line do |line|
        if line.split()[1] == absolute_guest_path
          mounted = true
          break
        end
      end
    end
  end
  return mounted
end

.sshfs_forward_mount_folder(machine, opts) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb', line 52

def self.sshfs_forward_mount_folder(machine, opts)
  # opts contains something like:
  #   { :type=>:sshfs,
  #     :guestpath=>"/sharedfolder",
  #     :hostpath=>"/guests/sharedfolder",
  #     :disabled=>false
  #     :ssh_host=>"192.168.1.1"
  #     :ssh_port=>"22"
  #     :ssh_username=>"username"
  #     :ssh_password=>"password"
  #   }

  # expand the guest path so we can handle things like "~/vagrant"
  expanded_guest_path = machine.guest.capability(
    :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

  # Mount path information: if arbitrary host mounting then
  # take as is. If not, then expand the hostpath to the real
  # path.
  if opts[:ssh_host]
      hostpath = opts[:hostpath].dup
  else
      hostpath = File.expand_path(opts[:hostpath], machine.env.root_path)
      hostpath  = Vagrant::Util::Platform.fs_real_path(hostpath).to_s
  end
   

  # Add in some sshfs/fuse options that are common to both mount methods
  opts[:sshfs_opts] = ' -o allow_other ' # allow non-root users to access
  opts[:sshfs_opts]+= ' -o noauto_cache '# disable caching based on mtime

  # Add in some ssh options that are common to both mount methods
  opts[:ssh_opts] = ' -o StrictHostKeyChecking=no '# prevent yes/no question
  opts[:ssh_opts]+= ' -o ServerAliveInterval=30 '  # send keepalives

  # Do a normal mount only if the user provided host information
  if opts.has_key?(:ssh_host) and opts[:ssh_host]
    self.sshfs_normal_mount(machine, opts, hostpath, expanded_guest_path)
  else
    self.sshfs_slave_mount(machine, opts, hostpath, expanded_guest_path)
  end
end

.sshfs_forward_unmount_folder(machine, opts) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vagrant-sshfs/cap/guest/linux/sshfs_forward_mount.rb', line 101

def self.sshfs_forward_unmount_folder(machine, opts)
  # opts contains something like:
  #   { :type=>:sshfs,
  #     :guestpath=>"/sharedfolder",
  #     :hostpath=>"/guests/sharedfolder",
  #     :disabled=>false
  #     :ssh_host=>"192.168.1.1"
  #     :ssh_port=>"22"
  #     :ssh_username=>"username"
  #     :ssh_password=>"password"
  #   }

  # expand the guest path so we can handle things like "~/vagrant"
  expanded_guest_path = machine.guest.capability(
    :shell_expand_guest_path, opts[:guestpath])

  # Log some information
  machine.ui.info(I18n.t("vagrant.sshfs.actions.unmounting_folder",
                         guestpath: expanded_guest_path))

  # Build up the command and connect
  error_class = VagrantPlugins::SyncedFolderSSHFS::Errors::SSHFSUnmountFailed
  cmd = "umount #{expanded_guest_path}"
  machine.communicate.sudo(
    cmd, error_class: error_class, error_key: :unmount_failed)
end