| 1 | |
| 2 | |
| 3 | |
| 4 | package hostagent |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "runtime" |
| 13 | |
| 14 | "github.com/lima-vm/sshocker/pkg/reversesshfs" |
| 15 | "github.com/sirupsen/logrus" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 18 | "github.com/lima-vm/lima/v2/pkg/sshutil" |
| 19 | ) |
| 20 | |
| 21 | type mount struct { |
| 22 | location string |
| 23 | mountPoint string |
| 24 | |
| 25 | |
| 26 | expectRemove func(hostPath string) bool |
| 27 | close func() error |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | func mountPathAndSftpServer(ctx context.Context, sshExe sshutil.SSHExe, driver, location string) (resolvedLocation, sftpServerBinary string) { |
| 37 | resolvedLocation = filepath.ToSlash(location) |
| 38 | if driver == limatype.SFTPDriverBuiltin { |
| 39 | return resolvedLocation, "" |
| 40 | } |
| 41 | sftpServerBinary = sshutil.SftpServerForSSH(ctx, sshExe) |
| 42 | if sftpServerBinary == "" { |
| 43 | logrus.Infof("reverse-sshfs: no sftp-server in the toolchain of %#q; falling back to a generic search, which may pick one that cannot resolve %#q", sshExe.Exe, resolvedLocation) |
| 44 | } else { |
| 45 | logrus.Debugf("reverse-sshfs: host path %#q resolved to %#q for sftp-server %#q", location, resolvedLocation, sftpServerBinary) |
| 46 | } |
| 47 | return resolvedLocation, sftpServerBinary |
| 48 | } |
| 49 | |
| 50 | func (a *HostAgent) setupMounts(ctx context.Context) ([]*mount, error) { |
| 51 | var ( |
| 52 | res []*mount |
| 53 | errs []error |
| 54 | ) |
| 55 | for _, f := range a.instConfig.Mounts { |
| 56 | m, err := a.setupMount(ctx, f) |
| 57 | if err != nil { |
| 58 | errs = append(errs, err) |
| 59 | continue |
| 60 | } |
| 61 | res = append(res, m) |
| 62 | } |
| 63 | return res, errors.Join(errs...) |
| 64 | } |
| 65 | |
| 66 | func (a *HostAgent) setupMount(ctx context.Context, m limatype.Mount) (*mount, error) { |
| 67 | if err := os.MkdirAll(m.Location, 0o755); err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | NOTE: |
| 71 | sshfsOptions := "allow_other" |
| 72 | if !*m.SSHFS.Cache { |
| 73 | sshfsOptions += ",cache=no" |
| 74 | } |
| 75 | if *m.SSHFS.FollowSymlinks { |
| 76 | sshfsOptions += ",follow_symlinks" |
| 77 | } |
| 78 | logrus.Infof("Mounting %#q on %#q", m.Location, *m.MountPoint) |
| 79 | |
| 80 | resolvedLocation := m.Location |
| 81 | var sftpServerBinary string |
| 82 | if runtime.GOOS == "windows" { |
| 83 | resolvedLocation, sftpServerBinary = mountPathAndSftpServer(ctx, a.sshExe, *m.SSHFS.SFTPDriver, m.Location) |
| 84 | } |
| 85 | |
| 86 | sshAddress, sshPort := a.sshAddressPort() |
| 87 | |
| 88 | |
| 89 | sshConfig := *a.sshConfig |
| 90 | rsf := &reversesshfs.ReverseSSHFS{ |
| 91 | Driver: *m.SSHFS.SFTPDriver, |
| 92 | OpensshSftpServerBinary: sftpServerBinary, |
| 93 | SSHConfig: &sshConfig, |
| 94 | LocalPath: resolvedLocation, |
| 95 | Host: sshAddress, |
| 96 | Port: sshPort, |
| 97 | RemotePath: *m.MountPoint, |
| 98 | Readonly: !(*m.Writable), |
| 99 | ReadonlyNames: m.SSHFS.ReadonlyNames, |
| 100 | SSHFSAdditionalArgs: []string{"-o", sshfsOptions}, |
| 101 | } |
| 102 | if runtime.GOOS == "windows" { |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | rsf.SSHConfig.Persist = false |
| 108 | |
| 109 | |
| 110 | |
| 111 | rsf.SSHConfig.AdditionalArgs = sshutil.DisableControlMasterOptsFromSSHArgs(rsf.SSHConfig.AdditionalArgs) |
| 112 | } |
| 113 | if err := rsf.Prepare(); err != nil { |
| 114 | return nil, fmt.Errorf("failed to prepare reverse sshfs for %#q on %#q: %w", resolvedLocation, *m.MountPoint, err) |
| 115 | } |
| 116 | if err := rsf.Start(); err != nil { |
| 117 | logrus.WithError(err).Warnf("failed to mount reverse sshfs for %#q on %#q, retrying with `-o nonempty`", resolvedLocation, *m.MountPoint) |
| 118 | NOTE: |
| 119 | |
| 120 | rsf.SSHFSAdditionalArgs = append(rsf.SSHFSAdditionalArgs, "-o", "nonempty") |
| 121 | if err := rsf.Start(); err != nil { |
| 122 | return nil, fmt.Errorf("failed to mount reverse sshfs for %#q on %#q: %w", resolvedLocation, *m.MountPoint, err) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | res := &mount{ |
| 127 | location: m.Location, |
| 128 | mountPoint: *m.MountPoint, |
| 129 | expectRemove: rsf.ExpectRemove, |
| 130 | close: func() error { |
| 131 | logrus.Infof("Unmounting %#q", resolvedLocation) |
| 132 | if err := rsf.Close(); err != nil { |
| 133 | return fmt.Errorf("failed to unmount reverse sshfs for %#q on %#q: %w", resolvedLocation, *m.MountPoint, err) |
| 134 | } |
| 135 | return nil |
| 136 | }, |
| 137 | } |
| 138 | return res, nil |
| 139 | } |