Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/hack/inject-cmdline-to-template.sh
1637 views
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
16
set -eu -o pipefail
17
18
template="$1"
19
appending_options="$2"
20
# 1. detect arch from template if not provided
21
arch="${3:-$(yq '.arch // ""' "${template}")}"
22
arch="${arch:-$(uname -m)}"
23
24
# normalize arch. amd64 -> x86_64, arm64 -> aarch64
25
case "${arch}" in
26
amd64 | x86_64) arch=x86_64 ;;
27
aarch64 | arm64) arch=aarch64 ;;
28
armv7l | armhf) arch=armv7l ;;
29
ppc64el | ppc64le) arch=ppc64le ;;
30
s390x) arch=s390x ;;
31
riscv64) arch=riscv64 ;;
32
*)
33
echo "Unsupported arch: ${arch}" >&2
34
exit 1
35
;;
36
esac
37
38
# 2. extract location by parsing template using arch
39
readonly yq_filter="
40
.images[]|select(.arch == \"${arch}\")|.location
41
"
42
parsed=$(yq eval "${yq_filter}" "${template}")
43
44
# 3. get the image location
45
function 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
}
50
while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
51
readonly locations=("${arr[@]}")
52
for ((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
60
done
61
62
# 4. check the image location is supported
63
if [[ -z ${location} ]]; then
64
echo "Failed to get the image location for ${template}" >&2
65
exit 1
66
elif [[ ${location} == https://cloud-images.ubuntu.com/minimal/* ]]; then
67
readonly default_cmdline="root=/dev/vda1 ro console=tty1 console=ttyAMA0"
68
elif [[ ${location} == https://cloud-images.ubuntu.com/* ]]; then
69
readonly default_cmdline="root=LABEL=cloudimg-rootfs ro console=tty1 console=ttyAMA0"
70
else
71
echo "Unsupported image location: ${location}" >&2
72
exit 1
73
fi
74
75
# 5. build the kernel and initrd location, digest, and cmdline
76
location_dirname=$(dirname "${location}")/unpacked
77
sha256sums=$(curl -sSLf "${location_dirname}/SHA256SUMS")
78
location_basename=$(basename "${location}")
79
80
# cmdline
81
cmdline="${default_cmdline} ${appending_options}"
82
83
# kernel
84
kernel_basename="${location_basename/.img/-vmlinuz-generic}"
85
kernel_digest=$(awk "/${kernel_basename}/{print \"sha256:\"\$1}" <<<"${sha256sums}")
86
kernel_location="${location_dirname}/${kernel_basename}"
87
88
# initrd
89
initrd_basename="${location_basename/.img/-initrd-generic}"
90
initrd_digest=$(awk "/${initrd_basename}/{print \"sha256:\"\$1}" <<<"${sha256sums}")
91
initrd_location="${location_dirname}/${initrd_basename}"
92
93
# 6. inject the kernel and initrd location, digest, and cmdline to the template
94
function inject_to() {
95
# shellcheck disable=SC2034
96
local template=$1 arch=$2 index=$3 key=$4 location=$5 digest=$6 cmdline=${7:-} fields=() IFS=,
97
# shellcheck disable=SC2310
98
check_location "${location}" || return 0
99
for field_name in location digest cmdline; do
100
[[ -z ${!field_name} ]] || fields+=("\"${field_name}\": \"${!field_name}\"")
101
done
102
limactl edit --log-level error --set "setpath([(.images[] | select(.arch == \"${arch}\") | path)].[${index}] + \"${key}\"; { ${fields[*]}})" "${template}"
103
}
104
inject_to "${template}" "${arch}" "${index}" "kernel" "${kernel_location}" "${kernel_digest}" "${cmdline}"
105
inject_to "${template}" "${arch}" "${index}" "initrd" "${initrd_location}" "${initrd_digest}"
106
107
# 7. output kernel_location, kernel_digest, cmdline, initrd_location, initrd_digest
108
readonly outputs=(kernel_location kernel_digest cmdline initrd_location initrd_digest)
109
for output in "${outputs[@]}"; do
110
echo "${output}=${!output}"
111
done
112
113