跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

mount.go

139 行 · 5.2 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package hostagent
5
6import (
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
21type mount struct {
22 location string
23 mountPoint string
24 // expectRemove makes the next removal of a host path by the guest a no-op on the host.
25 // It returns false when unsupported.
26 expectRemove func(hostPath string) bool
27 close func() error
28}
29
30// mountPathAndSftpServer returns location in the native path form, and the
31// sftp-server from sshExe's toolchain. Every driver gets the native form
32// because sshocker rewrites a Cygwin path to backslashes, which a Cygwin
33// ssh.exe then eats, leaving the server a path it cannot resolve. Both server
34// flavors accept the native form. The server is empty for the builtin driver,
35// which sshocker serves in-process, and when the toolchain ships none.
36func 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
50func (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
66func (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: allow_other requires "user_allow_other" in /etc/fuse.conf
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 // Create a copy of sshConfig to avoid
88 // modifying HostAgent's sshConfig in case of Windows
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 // cygwin/msys2 doesn't support full feature set over mux socket, this has at least 2 side effects:
104 // 1. unnecessary pollutes output with error on errors encountered (ssh will try to tolerate them with fallbacks);
105 // 2. these errors still imply additional coms over mux socket, which resulted sftp-server to fail more often statistically during test runs.
106 // It is reasonable to disable this on Windows if required feature is not fully operational.
107 rsf.SSHConfig.Persist = false
108
109 // HostAgent's `sshConfig` already has some ControlMaster related args in `AdditionalArgs`,
110 // so it is necessary to remove them to avoid overrides above `Persist=false`.
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: nonempty is not supported for libfuse3: https://github.com/canonical/multipass/issues/1381
119 // Added to the options, not in their place: they include cache=no.
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}