跳到主要内容

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

浏览文件

inject-cmdline-to-template.sh

112 行 · 3.8 KB历史下载
1#!/usr/bin/env bash
2
3# SPDX-FileCopyrightText: Copyright The Lima Authors
4# SPDX-License-Identifier: Apache-2.0
5
6#
7# This script does
8# 1. detect arch from template if not provided
9# 2. extract location by parsing template using arch
10# 3. get the image location
11# 4. check the image location is supported
12# 5. build the kernel and initrd location, digest, and cmdline
13# 6. inject the kernel and initrd location, digest, and cmdline to the template
14# 7. output kernel_location, kernel_digest, cmdline, initrd_location, initrd_digest
15
16set -eu -o pipefail
17
18template="$1"
19appending_options="$2"
20# 1. detect arch from template if not provided
21arch="${3:-$(yq '.arch // ""' "${template}")}"
22arch="${arch:-$(uname -m)}"
23
24# normalize arch. amd64 -> x86_64, arm64 -> aarch64
25case "${arch}" in
26amd64 | x86_64) arch=x86_64 ;;
27aarch64 | arm64) arch=aarch64 ;;
28armv7l | armhf) arch=armv7l ;;
29ppc64el | ppc64le) arch=ppc64le ;;
30s390x) arch=s390x ;;
31riscv64) arch=riscv64 ;;
32*)
33 echo "Unsupported arch: ${arch}" >&2
34 exit 1
35 ;;
36esac
37
38# 2. extract location by parsing template using arch
39readonly yq_filter="
40 .images[]|select(.arch == \"${arch}\")|.location
41"
42parsed=$(yq eval "${yq_filter}" "${template}")
43
44# 3. get the image location
45function check_location() {
46 local location=$1 http_code
47 http_code=$(curl -sIL -w "%{http_code}" "${location}" -o /dev/null)
48 [[ ${http_code} -eq 200 ]]
49}
50while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
51readonly locations=("${arr[@]}")
52for ((i = 0; i < ${#locations[@]}; i++)); do
53 [[ ${locations[i]} != "null" ]] || continue
54 # shellcheck disable=SC2310
55 if check_location "${locations[i]}"; then
56 location=${locations[i]}
57 index=${i}
58 break
59 fi
60done
61
62# 4. check the image location is supported
63case "${location:-}" in
64https://cloud-images.ubuntu.com/minimal/* | https://cloud-images.ubuntu.com/daily/server/minimal/*)
65 readonly default_cmdline="root=/dev/vda1 ro console=tty1 console=ttyAMA0"
66 ;;
67https://cloud-images.ubuntu.com/*)
68 readonly default_cmdline="root=LABEL=cloudimg-rootfs ro console=tty1 console=ttyAMA0"
69 ;;
70*)
71 echo "Unsupported image location: ${location}" >&2
72 exit 1
73 ;;
74esac
75
76# 5. build the kernel and initrd location, digest, and cmdline
77location_dirname=$(dirname "${location}")/unpacked
78sha256sums=$(curl -sSLf "${location_dirname}/SHA256SUMS")
79location_basename=$(basename "${location}")
80
81# cmdline
82cmdline="${default_cmdline} ${appending_options}"
83
84# kernel
85kernel_basename="${location_basename/.img/-vmlinuz-generic}"
86kernel_digest=$(awk "/${kernel_basename}/{print \"sha256:\"\$1}" <<<"${sha256sums}")
87kernel_location="${location_dirname}/${kernel_basename}"
88
89# initrd
90initrd_basename="${location_basename/.img/-initrd-generic}"
91initrd_digest=$(awk "/${initrd_basename}/{print \"sha256:\"\$1}" <<<"${sha256sums}")
92initrd_location="${location_dirname}/${initrd_basename}"
93
94# 6. inject the kernel and initrd location, digest, and cmdline to the template
95function inject_to() {
96 # shellcheck disable=SC2034
97 local template=$1 arch=$2 index=$3 key=$4 location=$5 digest=$6 cmdline=${7:-} fields=() IFS=,
98 # shellcheck disable=SC2310
99 check_location "${location}" || return 0
100 for field_name in location digest cmdline; do
101 [[ -z ${!field_name} ]] || fields+=("\"${field_name}\": \"${!field_name}\"")
102 done
103 limactl edit --log-level error --set "setpath([(.images[] | select(.arch == \"${arch}\") | path)].[${index}] + \"${key}\"; { ${fields[*]}})" "${template}"
104}
105inject_to "${template}" "${arch}" "${index}" "kernel" "${kernel_location}" "${kernel_digest}" "${cmdline}"
106inject_to "${template}" "${arch}" "${index}" "initrd" "${initrd_location}" "${initrd_digest}"
107
108# 7. output kernel_location, kernel_digest, cmdline, initrd_location, initrd_digest
109readonly outputs=(kernel_location kernel_digest cmdline initrd_location initrd_digest)
110for output in "${outputs[@]}"; do
111 echo "${output}=${!output}"
112done