跳到主要内容

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

浏览文件

update-template-macos.sh

201 行 · 7.2 KB历史下载
1#!/usr/bin/env bash
2
3# SPDX-FileCopyrightText: Copyright The Lima Authors
4# SPDX-License-Identifier: Apache-2.0
5
6set -eu -o pipefail
7
8# Functions in this script assume error handling with 'set -e'.
9# To ensure 'set -e' works correctly:
10# - Use 'set +e' before assignments and '$(set -e; <function>)' to capture output without exiting on errors.
11# - Avoid calling functions directly in conditions to prevent disabling 'set -e'.
12# - Use 'shopt -s inherit_errexit' (Bash 4.4+) to avoid repeated 'set -e' in all '$(...)'.
13shopt -s inherit_errexit || error_exit "inherit_errexit not supported. Please use bash 4.4 or later."
14
15function macos_print_help() {
16 cat <<HELP
17$(basename "${BASH_SOURCE[0]}"): Update the macOS image location in the specified templates
18
19Usage:
20 $(basename "${BASH_SOURCE[0]}") <template.yaml>...
21
22Description:
23 This script updates the macOS image location in the specified templates.
24 Image location format:
25
26 https://updates.cdn-apple.com/.../UniversalMac_<version>_<build>_Restore.ipsw
27
28 Published macOS image information (URL and SHA256 digest) is fetched from
29 the ipsw.me API:
30
31 https://api.ipsw.me/v4/device/VirtualMac2,1
32
33 The downloaded JSON will be cached in the Lima cache directory.
34
35Examples:
36 Update the macOS image location in templates/_images/macos-*.yaml:
37 $ $(basename "${BASH_SOURCE[0]}") templates/_images/macos-*.yaml
38
39Flags:
40 -h, --help Print this help message
41HELP
42}
43
44# URL of the ipsw.me device API endpoint for Apple Virtual Machine 1 (VirtualMac2,1).
45# This returns all available macOS IPSW firmwares with URL and SHA256 digest.
46readonly macos_ipsw_me_device_url="https://api.ipsw.me/v4/device/VirtualMac2,1"
47
48# macos_url_spec_from_location prints the URL spec for the given location.
49# If the location is not a macOS IPSW URL from Apple's CDN, it returns 1.
50# The number of the path components varies across the releases, so any depth is accepted.
51# e.g.
52# ```console
53# macos_url_spec_from_location https://updates.cdn-apple.com/2025SummerFCS/fullrestores/082-08674/51294E4D-A273-44BE-A280-A69FC347FB87/UniversalMac_15.6_24G84_Restore.ipsw
54# {"version":"15.6","major_version":"15","build":"24G84"}
55# macos_url_spec_from_location https://updates.cdn-apple.com/2026FallFCS/afcfc88e-bbe6-44bf-a5da-07c56eebc06c/UniversalMac_27.0_26A428_Restore.ipsw
56# {"version":"27.0","major_version":"27","build":"26A428"}
57# ```
58function macos_url_spec_from_location() {
59 local location=$1 jq_filter url_spec
60 jq_filter='capture("
61 ^https://updates\\.cdn-apple\\.com/(?:[^/]+/)+
62 UniversalMac_(?<version>(?<major_version>\\d+)(?:\\.\\d+)+)_(?<build>[^_]+)_Restore\\.ipsw$
63 ";"x")
64 '
65 url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"")
66 echo "${url_spec}"
67}
68
69# macos_latest_image_entry_for_url_spec prints the latest image entry for the given URL spec.
70# e.g.
71# ```console
72# macos_latest_image_entry_for_url_spec '{"major_version":"15"}'
73# {"location":"https://updates.cdn-apple.com/.../UniversalMac_15.6.1_24G90_Restore.ipsw","arch":"aarch64","digest":"sha256:..."}
74# ```
75function macos_latest_image_entry_for_url_spec() {
76 local url_spec=$1 major_version ipsw_me_file latest_entry location digest arch="aarch64"
77 major_version=$(jq -r '.major_version' <<<"${url_spec}")
78 ipsw_me_file=$(download_to_cache "${macos_ipsw_me_device_url}")
79 latest_entry=$(jq -e -r --arg major "${major_version}" '
80 .firmwares |
81 [.[] | select(.version | test("^" + $major + "\\."))] |
82 sort_by(.releasedate) |
83 last
84 ' "${ipsw_me_file}")
85 [[ -n ${latest_entry} && ${latest_entry} != "null" ]] ||
86 error_exit "Failed to get the latest macOS ${major_version} image from ${macos_ipsw_me_device_url}"
87 location=$(jq -r '.url' <<<"${latest_entry}")
88 digest=$(jq -r '.sha256sum // "" | if . != "" then "sha256:" + . else "" end' <<<"${latest_entry}")
89 json_vars location arch digest
90}
91
92function macos_cache_key_for_image_kernel() {
93 local location=$1 url_spec
94 url_spec=$(macos_url_spec_from_location "${location}")
95 jq -r '["macos", .major_version] | join(":")' <<<"${url_spec}"
96}
97
98function macos_image_entry_for_image_kernel() {
99 local location=$1 kernel_is_not_supported=$2 url_spec image_entry=''
100 [[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on macOS" >&2
101 url_spec=$(macos_url_spec_from_location "${location}")
102 image_entry=$(macos_latest_image_entry_for_url_spec "${url_spec}")
103 # shellcheck disable=SC2031
104 if [[ -z ${image_entry} ]]; then
105 error_exit "Failed to get the image location for ${location}"
106 elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
107 echo "Image location is up-to-date: ${location}" >&2
108 else
109 echo "${image_entry}"
110 fi
111}
112
113# check if the script is executed or sourced
114# shellcheck disable=SC1091
115if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
116 scriptdir=$(dirname "${BASH_SOURCE[0]}")
117 # shellcheck source=./cache-common-inc.sh
118 . "${scriptdir}/cache-common-inc.sh"
119
120 # shellcheck source=/dev/null # avoid shellcheck hangs on source looping
121 . "${scriptdir}/update-template.sh"
122else
123 # this script is sourced
124 if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
125 SUPPORTED_DISTRIBUTIONS+=("macos")
126 else
127 declare -a SUPPORTED_DISTRIBUTIONS=("macos")
128 fi
129 return 0
130fi
131
132declare -a templates=()
133while [[ $# -gt 0 ]]; do
134 case "$1" in
135 -h | --help)
136 macos_print_help
137 exit 0
138 ;;
139 -d | --debug) set -x ;;
140 *.yaml) templates+=("$1") ;;
141 *)
142 error_exit "Unknown argument: $1"
143 ;;
144 esac
145 shift
146done
147
148if [[ ${#templates[@]} -eq 0 ]]; then
149 macos_print_help
150 exit 0
151fi
152
153declare -A image_entry_cache=()
154
155for template in "${templates[@]}"; do
156 echo "Processing ${template}"
157 # 1. extract location by parsing template using arch
158 yq_filter="
159 .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
160 "
161 parsed=$(yq eval "${yq_filter}" "${template}")
162
163 # 3. get the image location
164 arr=()
165 while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
166 locations=("${arr[@]}")
167 for ((index = 0; index < ${#locations[@]}; index++)); do
168 [[ ${locations[index]} != "null" ]] || continue
169 set -e
170 IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}"
171 set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
172 cache_key=$(
173 set -e # Enable 'set -e' for the next command.
174 macos_cache_key_for_image_kernel "${location}" "${kernel_location}"
175 ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
176 # shellcheck disable=2181
177 [[ $? -eq 0 ]] || continue
178 image_entry=$(
179 set -e # Enable 'set -e' for the next command.
180 if [[ -v image_entry_cache[${cache_key}] ]]; then
181 echo "${image_entry_cache[${cache_key}]}"
182 else
183 macos_image_entry_for_image_kernel "${location}" "${kernel_location}"
184 fi
185 ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
186 # shellcheck disable=2181
187 [[ $? -eq 0 ]] || continue
188 set -e
189 image_entry_cache[${cache_key}]="${image_entry}"
190 if [[ -n ${image_entry} ]]; then
191 [[ ${kernel_cmdline} != "null" ]] &&
192 jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null &&
193 image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
194 echo "${image_entry}" | jq
195 limactl edit --log-level error --set "
196 .images[${index}] *= ${image_entry}|
197 (.images[${index}] | ..) style = \"double\"
198 " "${template}"
199 fi
200 done
201done