跳到主要内容

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

浏览文件

boot.sh

259 行 · 8.5 KB历史下载
1#!/bin/sh
2
3# SPDX-FileCopyrightText: Copyright The Lima Authors
4# SPDX-License-Identifier: Apache-2.0
5
6set -eu
7
8INFO() {
9 echo "LIMA $(date -Iseconds)| $*"
10}
11
12WARNING() {
13 echo "LIMA $(date -Iseconds)| WARNING: $*"
14}
15
16UNAME="$(uname -s)"
17
18RUN="/run"
19if [ "${UNAME}" != "Linux" ]; then
20 RUN="/var/run"
21fi
22rm -f "${RUN}/lima-boot-done"
23
24# A boot script that reboots the guest creates this file first, so that the rest
25# of this script knows the guest is on its way down.
26LIMA_REBOOT_REQUIRED="${RUN}/lima-reboot-required"
27export LIMA_REBOOT_REQUIRED
28rm -f "${LIMA_REBOOT_REQUIRED}"
29
30# shellcheck disable=SC2163
31while read -r line; do [ -n "$line" ] && export "$line"; done <"${LIMA_CIDATA_MNT}"/lima.env
32# shellcheck disable=SC2163
33while read -r line; do [ -n "$line" ] && export "$line"; done <"${LIMA_CIDATA_MNT}"/param.env
34
35# shellcheck disable=SC2163
36while read -r line; do
37 # pam_env implementation:
38 # - '#' is treated the same as newline; terminates value
39 # - skip leading tabs and spaces
40 # - skip leading "export " prefix (only single space)
41 # - skip leading quote ('\'' or '"') on the value side
42 # - skip trailing quote only if leading quote has been skipped;
43 # quotes don't need to match; trailing quote may be omitted
44 line="$(echo "$line" | sed -E "s/^[ \\t]*(export )?//; s/#.*//; s/(^[^=]+=)[\"'](.*[^\"'])?[\"']?$/\1\2/")"
45 [ -n "$line" ] && export "$line"
46done <"${LIMA_CIDATA_MNT}"/etc_environment
47
48PATH="${LIMA_CIDATA_MNT}"/util:"${PATH}"
49export PATH
50
51CODE=0
52
53# Don't make any changes to /etc or /var/lib until boot.Linux/04-persistent-data-volume.sh
54# has run because it might move the directories to /mnt/data on first boot. In that
55# case changes made on restart would be lost.
56
57run_boot_scripts() {
58 boot="$1"
59 if [ -e "${boot}" ]; then
60 for f in "${boot}"/*.sh; do
61 INFO "Executing $f"
62 if ! "$f"; then
63 WARNING "Failed to execute $f"
64 CODE=1
65 fi
66 done
67 fi
68}
69
70# The boot.essential.${UNAME} scripts are executed in plain mode too.
71run_boot_scripts "${LIMA_CIDATA_MNT}/boot.essential.${UNAME}"
72
73if [ "$LIMA_CIDATA_PLAIN" = "1" ]; then
74 INFO "Plain mode. Skipping to run non-essential boot scripts. Provisioning scripts will be still executed. Guest agent will not be running."
75else
76 run_boot_scripts "${LIMA_CIDATA_MNT}/boot.${UNAME}"
77fi
78
79# indirect variable lookup, like ${!var} in bash
80deref() {
81 eval echo \$"$1"
82}
83
84if [ -d "${LIMA_CIDATA_MNT}"/provision.data ]; then
85 for f in "${LIMA_CIDATA_MNT}"/provision.data/*; do
86 filename=$(basename "$f")
87 overwrite=$(deref "LIMA_CIDATA_DATAFILE_${filename}_OVERWRITE")
88 owner=$(deref "LIMA_CIDATA_DATAFILE_${filename}_OWNER")
89 path=$(deref "LIMA_CIDATA_DATAFILE_${filename}_PATH")
90 permissions=$(deref "LIMA_CIDATA_DATAFILE_${filename}_PERMISSIONS")
91 user="${owner%%:*}"
92 if [ -e "$path" ] && [ "$overwrite" = "false" ]; then
93 INFO "Not overwriting $path"
94 else
95 INFO "Copying $f to $path"
96 if ! sudo -iu "${user}" mkdir -p "$(dirname "$path")"; then
97 WARNING "Failed to create directory for ${path} (as user ${user})"
98 WARNING "Falling back to creating directory as root to maintain compatibility"
99 mkdir -p "$(dirname "$path")"
100 fi
101 cp "$f" "$path"
102 chown "$owner" "$path"
103 chmod "$permissions" "$path"
104 fi
105 done
106fi
107
108if [ -d "${LIMA_CIDATA_MNT}"/provision.yq ]; then
109 yq="${LIMA_CIDATA_MNT}/lima-guestagent yq"
110 for f in "${LIMA_CIDATA_MNT}"/provision.yq/*; do
111 filename=$(basename "${f}")
112 format=$(deref "LIMA_CIDATA_YQ_PROVISION_${filename}_FORMAT")
113 owner=$(deref "LIMA_CIDATA_YQ_PROVISION_${filename}_OWNER")
114 path=$(deref "LIMA_CIDATA_YQ_PROVISION_${filename}_PATH")
115 permissions=$(deref "LIMA_CIDATA_YQ_PROVISION_${filename}_PERMISSIONS")
116 user="${owner%%:*}"
117 # Creating intermediate directories may fail if the user does not have permission.
118 # TODO: Create intermediate directories with the specified group ownership.
119 if ! sudo -iu "${user}" mkdir -p "$(dirname "${path}")"; then
120 WARNING "Failed to create directory for ${path} (as user ${user})"
121 CODE=1
122 continue
123 fi
124 # Since CIDATA is mounted with dmode=700,fmode=700,
125 # `lima-guestagent yq` cannot be executed by non-root users,
126 # and provision.yq/* files cannot be read by non-root users.
127 if [ -f "${path}" ]; then
128 INFO "Updating ${path}"
129 # If the user does not have write permission, it should fail.
130 # This avoids changes being made by the wrong user.
131 if ! sudo -iu "${user}" test -w "${path}"; then
132 WARNING "File ${path} is not writable by user ${user}"
133 CODE=1
134 continue
135 fi
136 # Relies on the fact that yq does not change the owner of the existing file.
137 if ! ${yq} --inplace --from-file "${f}" --input-format "${format}" --output-format "${format}" "${path}"; then
138 WARNING "Failed to update ${path} (as user ${user})"
139 CODE=1
140 continue
141 fi
142 else
143 if [ "${format}" = "auto" ]; then
144 # yq can't determine the output format from non-existing files
145 case "${path}" in
146 *.csv) format=csv ;;
147 *.ini) format=ini ;;
148 *.json) format=json ;;
149 *.properties) format=properties ;;
150 *.toml) format=toml ;;
151 *.tsv) format=tsv ;;
152 *.xml) format=xml ;;
153 *.yaml | *.yml) format=yaml ;;
154 *)
155 format=yaml
156 WARNING "Cannot determine file type for ${path}, using yaml format"
157 ;;
158 esac
159 fi
160 INFO "Creating ${path}"
161 if ! ${yq} --null-input --from-file "${f}" --output-format "${format}" | sudo -iu "${user}" tee "${path}"; then
162 WARNING "Failed to create ${path} (as user ${user})"
163 CODE=1
164 continue
165 fi
166 fi
167 if ! sudo -iu "${user}" chown "${owner}" "${path}"; then
168 WARNING "Failed to set owner for ${path} (as user ${user})"
169 CODE=1
170 fi
171 if ! sudo -iu "${user}" chmod "${permissions}" "${path}"; then
172 WARNING "Failed to set permissions for ${path} (as user ${user})"
173 CODE=1
174 fi
175 done
176fi
177
178if [ -d "${LIMA_CIDATA_MNT}"/provision.system ]; then
179 for f in "${LIMA_CIDATA_MNT}"/provision.system/*; do
180 INFO "Executing $f"
181 if ! "$f"; then
182 WARNING "Failed to execute $f"
183 CODE=1
184 fi
185 done
186fi
187
188USER_SCRIPT="${LIMA_CIDATA_HOME}/.lima-user-script"
189if [ -d "${LIMA_CIDATA_MNT}"/provision.user ]; then
190 if [ "$UNAME" = "Linux" ] && [ ! -f /sbin/openrc-run ]; then
191 until [ -e "/run/user/${LIMA_CIDATA_UID}/systemd/private" ]; do sleep 3; done
192 fi
193 params=$(grep -o '^PARAM_[^=]*' "${LIMA_CIDATA_MNT}"/param.env | paste -sd , -)
194 for f in "${LIMA_CIDATA_MNT}"/provision.user/*; do
195 INFO "Executing $f (as user ${LIMA_CIDATA_USER})"
196 cp "$f" "${USER_SCRIPT}"
197 chown "${LIMA_CIDATA_USER}" "${USER_SCRIPT}"
198 chmod 755 "${USER_SCRIPT}"
199 XDG_RUNTIME_DIR_ENV=
200 if [ "${UNAME}" = "Linux" ]; then
201 XDG_RUNTIME_DIR_ENV="XDG_RUNTIME_DIR=/run/user/${LIMA_CIDATA_UID}"
202 fi
203 # shellcheck disable=SC2086
204 if ! sudo -iu "${LIMA_CIDATA_USER}" "--preserve-env=${params}" $XDG_RUNTIME_DIR_ENV "${USER_SCRIPT}"; then
205 WARNING "Failed to execute $f (as user ${LIMA_CIDATA_USER})"
206 CODE=1
207 fi
208 rm "${USER_SCRIPT}"
209 done
210fi
211
212if [ "${UNAME}" != "Darwin" ] && [ "${LIMA_CIDATA_PASSWORDLESS_SUDO}" != "1" ]; then
213 if [ ! -f "${LIMA_CIDATA_HOME}/.lima-password-set" ]; then
214 PASSWORD=$(head -c 8 /dev/urandom | od -An -tx1 | tr -d ' \n')
215 if command -v chpasswd >/dev/null 2>&1; then
216 if ! echo "${LIMA_CIDATA_USER}:${PASSWORD}" | chpasswd; then
217 WARNING "failed to set password via chpasswd"
218 PASSWORD=""
219 CODE=1
220 fi
221 elif command -v pw >/dev/null 2>&1; then
222 if ! echo "${PASSWORD}" | pw usermod "${LIMA_CIDATA_USER}" -h 0; then
223 WARNING "failed to set password via pw"
224 PASSWORD=""
225 CODE=1
226 fi
227 else
228 WARNING "no supported tool to set password found"
229 PASSWORD=""
230 CODE=1
231 fi
232 if [ -n "${PASSWORD}" ]; then
233 (umask 077 && : >"${LIMA_CIDATA_HOME}/password")
234 chown "${LIMA_CIDATA_USER}:${LIMA_CIDATA_USER}" "${LIMA_CIDATA_HOME}/password" || {
235 WARNING "failed to chown ${LIMA_CIDATA_HOME}/password"
236 CODE=1
237 }
238 echo "${PASSWORD}" >"${LIMA_CIDATA_HOME}/password"
239 touch "${LIMA_CIDATA_HOME}/.lima-password-set"
240 chown "${LIMA_CIDATA_USER}:${LIMA_CIDATA_USER}" "${LIMA_CIDATA_HOME}/.lima-password-set" || {
241 WARNING "failed to chown ${LIMA_CIDATA_HOME}/.lima-password-set"
242 CODE=1
243 }
244 fi
245 fi
246fi
247# Signal that provisioning is done. The instance ID changes on every boot,
248# so any value from a previous boot cycle will be different.
249# A pending reboot suppresses the signal, because the reboot is asynchronous
250# and the host agent would otherwise call the guest ready while sshd is
251# already going down.
252if [ -e "${LIMA_REBOOT_REQUIRED}" ]; then
253 INFO "Not signaling boot completion because a boot script requested a reboot"
254else
255 echo "${LIMA_CIDATA_IID}" >"${RUN}/lima-boot-done"
256fi
257
258INFO "Exiting with code $CODE"
259exit "$CODE"