跳到主要内容

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

浏览文件

mounts_linux.go

94 行 · 2.9 KB历史下载
1//go:build linux
2
3package containerenv
4
5import (
6 "context"
7 "errors"
8 "os"
9 "path/filepath"
10 "strconv"
11 "strings"
12)
13
14// Read procfs from the host, not through a guest command or a guest-supplied
15// path. Correlate the global PID with the exact engine container scope first.
16func (r *Runtime) inspectMounts(container *engineContainer) (MountPropagation, error) {
17 if !container.State.Running || container.State.Pid < 2 {
18 return MountPropagation{}, nil
19 }
20 process := filepath.Join("/proc", strconv.Itoa(container.State.Pid))
21 cgroups, err := os.ReadFile(filepath.Join(process, "cgroup"))
22 if err != nil {
23 return MountPropagation{}, err
24 }
25 prefix := "docker-"
26 if r.config.Engine == "podman" {
27 prefix = "libpod-"
28 }
29 scope := "/" + prefix + container.ID + ".scope"
30 owned := false
31 for _, line := range strings.Split(string(cgroups), "\n") {
32 parts := strings.SplitN(line, ":", 3)
33 if len(parts) == 3 && (strings.HasSuffix(parts[2], scope) || strings.Contains(parts[2], scope+"/")) {
34 owned = true
35 }
36 }
37 if !owned {
38 return MountPropagation{}, errors.New("无法匹配容器全局进程身份,未读取其他进程的挂载")
39 }
40 for _, namespace := range []string{"mnt", "user"} {
41 host, err := os.Readlink(filepath.Join("/proc/self/ns", namespace))
42 if err != nil {
43 return MountPropagation{}, err
44 }
45 guest, err := os.Readlink(filepath.Join(process, "ns", namespace))
46 if err != nil {
47 return MountPropagation{}, err
48 }
49 if host == guest {
50 return MountPropagation{}, errors.New("容器必须使用独立用户与挂载命名空间")
51 }
52 }
53 mounts, err := os.ReadFile(filepath.Join(process, "mountinfo"))
54 if err != nil {
55 return MountPropagation{}, err
56 }
57 hostMounts, err := os.ReadFile("/proc/self/mountinfo")
58 if err != nil {
59 return MountPropagation{}, err
60 }
61 result, err := rootMountPropagation(string(mounts), string(hostMounts))
62 if err != nil {
63 return MountPropagation{}, err
64 }
65 result.PrivateNamespace = true
66 return result, nil
67}
68
69// Called only after this Start booted a stopped container, before any project
70// share or Cage application is started. Detach inherited propagation first,
71// then establish a new group entirely inside the private container namespace.
72func (r *Runtime) initializeMounts(ctx context.Context, containerID string) error {
73 container, err := r.inspectContainer(ctx)
74 if err != nil {
75 return err
76 }
77 if container == nil || container.ID != containerID || !container.State.Running {
78 return errors.New("容器启动身份已变化")
79 }
80 if _, err := r.inspectMounts(container); err != nil {
81 return err
82 }
83 if _, err := r.output(ctx, "exec", "--user", "0", containerID, "/bin/sh", "-c", "set -eu; mount --make-rprivate /; mount --make-rshared /"); err != nil {
84 return err
85 }
86 state, err := r.inspectMounts(container)
87 if err != nil {
88 return err
89 }
90 if !state.Ready() {
91 return errors.New("容器项目挂载传播未能初始化")
92 }
93 return nil
94}