跳到主要内容

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

浏览文件

macos_darwin.go

182 行 · 5.7 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package macos
5
6import (
7 "context"
8 "errors"
9 "fmt"
10 "os"
11 "path/filepath"
12 "time"
13
14 "github.com/sirupsen/logrus"
15
16 "github.com/lima-vm/lima/v2/pkg/apfs"
17 "github.com/lima-vm/lima/v2/pkg/imgutil/nativeimgutil/asifutil"
18 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
19 "github.com/lima-vm/lima/v2/pkg/osutil"
20)
21
22// attachImageWithRetry retries `diskutil image attach` because the
23// command occasionally returns "Resource temporarily unavailable".
24func attachImageWithRetry(ctx context.Context, disk string, retry int) (*asifutil.AttachedDisk, error) {
25 var (
26 attached *asifutil.AttachedDisk
27 err error
28 )
29 for range retry {
30 attached, err = asifutil.DiskutilImageAttachNoMount(ctx, disk)
31 if !errors.Is(err, asifutil.ErrResourceTemporarilyUnavailable) {
32 break
33 }
34 time.Sleep(3 * time.Second)
35 }
36 return attached, err
37}
38
39// Patch prepares a macOS guest disk for first boot. It writes the
40// LaunchDaemon plist, init script, and setup markers via a noowners
41// mount, then fixes file ownership by patching APFS inode records
42// directly on the raw disk image. No sudo required.
43func Patch(ctx context.Context, disk string) error {
44 if err := patchWriteGuestFiles(ctx, disk); err != nil {
45 return err
46 }
47 return patchFixOwnership(ctx, disk)
48}
49
50// patchWriteGuestFiles attaches the disk image, mounts the Data
51// volume with noowners, writes guest files, then detaches.
52func patchWriteGuestFiles(ctx context.Context, disk string) error {
53 attached, err := attachImageWithRetry(ctx, disk, 3)
54 if err != nil {
55 return fmt.Errorf("failed to attach disk: %w", err)
56 }
57 if attached == nil || attached.Data == "" {
58 return errors.New("failed to find data slice in attached disk")
59 }
60 dataDevPath := "/dev/" + attached.Data
61 defer func() {
62 // Detaching the data slice is enough to detach the whole ASIF.
63 if err := asifutil.DetachASIF(dataDevPath); err != nil {
64 logrus.WithError(err).Warnf("failed to detach %#q (%#q)", dataDevPath, disk)
65 }
66 }()
67
68 instDir := filepath.Dir(disk)
69 mnt := filepath.Join(instDir, filenames.MntDir)
70 if err := os.MkdirAll(mnt, 0o755); err != nil {
71 return fmt.Errorf("failed to create mount point %#q: %w", mnt, err)
72 }
73 defer func() {
74 if err := os.Remove(mnt); err != nil {
75 logrus.WithError(err).Warnf("failed to remove mount point %#q", mnt)
76 }
77 }()
78 return writeGuestFiles(ctx, dataDevPath, mnt)
79}
80
81// patchFixOwnership attaches the disk image and patches APFS inode
82// records on the raw container device to set root:wheel ownership.
83func patchFixOwnership(ctx context.Context, disk string) error {
84 attached, err := attachImageWithRetry(ctx, disk, 3)
85 if err != nil {
86 return fmt.Errorf("failed to attach disk for ownership fix: %w", err)
87 }
88 if attached == nil || attached.Data == "" {
89 return errors.New("failed to find data slice in attached disk")
90 }
91 dataDevPath := "/dev/" + attached.Data
92 defer func() {
93 if err := asifutil.DetachASIF(dataDevPath); err != nil {
94 logrus.WithError(err).Warnf("failed to detach %#q (%#q)", dataDevPath, disk)
95 }
96 }()
97
98 if attached.Container == "" {
99 return errors.New("diskutil did not report an APFS container device")
100 }
101
102 // Patch APFS inode records via the raw container device.
103 // The noowners mount stores files with uid=99 (nobody);
104 // LaunchDaemon plists must be owned by root:wheel for launchd
105 // to load them, so we patch them to UID 0 / GID 0 directly.
106 containerDev := "/dev/r" + attached.Container
107 if err = apfs.Chown(containerDev, apfs.VolRoleData, 0, 0,
108 "private/var/db/.AppleSetupDone",
109 "Library/User Template/.skipbuddy",
110 "usr/local/sbin",
111 "usr/local/sbin/lima-macos-init.sh",
112 "Library/LaunchDaemons/io.lima-vm.lima-macos-init.plist",
113 ); err != nil {
114 return fmt.Errorf("failed to fix file ownership on disk: %w", err)
115 }
116
117 return nil
118}
119
120// writeGuestFiles mounts the data volume with noowners and writes the
121// LaunchDaemon plist, init script, and setup markers.
122func writeGuestFiles(ctx context.Context, dataSliceDevice, mnt string) error {
123 // Mount with "noowners" so non-root users can write to the volume.
124 if err := osutil.Mount(ctx, "apfs", dataSliceDevice, mnt, []string{"noowners"}); err != nil {
125 return err
126 }
127 defer func() {
128 if err := osutil.Umount(ctx, mnt); err != nil {
129 logrus.WithError(err).Warnf("failed to unmount %#q", mnt)
130 }
131 }()
132
133 filesToTouch := []string{
134 filepath.Join(mnt, "private/var/db/.AppleSetupDone"),
135 filepath.Join(mnt, "Library/User Template/.skipbuddy"),
136 }
137 for _, file := range filesToTouch {
138 if err := osutil.Touch(file); err != nil {
139 return fmt.Errorf("failed to touch %#q: %w", file, err)
140 }
141 }
142
143 const initSh = `#!/bin/sh
144set -eux
145date
146if [ ! -e /Volumes/cidata ]; then
147 echo "/Volumes/cidata is not mounted" >&2
148 exit 1
149fi
150exec /Volumes/cidata/lima-guestagent fake-cloud-init
151`
152 if err := os.MkdirAll(filepath.Join(mnt, "usr/local/sbin"), 0o755); err != nil {
153 return err
154 }
155 if err := os.WriteFile(filepath.Join(mnt, "usr/local/sbin/lima-macos-init.sh"), []byte(initSh), 0o755); err != nil {
156 return err
157 }
158
159 const plist = `<?xml version="1.0" encoding="UTF-8"?>
160<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
161<plist version="1.0">
162<dict>
163 <key>Label</key>
164 <string>io.lima-vm.lima-macos-init</string>
165 <key>ProgramArguments</key>
166 <array>
167 <string>/usr/local/sbin/lima-macos-init.sh</string>
168 </array>
169 <key>RunAtLoad</key>
170 <true/>
171 <key>StandardOutPath</key>
172 <string>/dev/tty.virtio</string>
173 <key>StandardErrorPath</key>
174 <string>/dev/tty.virtio</string>
175</dict>
176</plist>
177`
178 if err := os.WriteFile(filepath.Join(mnt, "Library/LaunchDaemons/io.lima-vm.lima-macos-init.plist"), []byte(plist), 0o755); err != nil {
179 return err
180 }
181 return nil
182}