Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/hack/update-template-oraclelinux.sh
2609 views
1
#!/usr/bin/env bash
2
3
# SPDX-FileCopyrightText: Copyright The Lima Authors
4
# SPDX-License-Identifier: Apache-2.0
5
6
set -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 '$(...)'.
13
shopt -s inherit_errexit || error_exit "inherit_errexit not supported. Please use bash 4.4 or later."
14
15
function oraclelinux_print_help() {
16
cat <<HELP
17
$(basename "${BASH_SOURCE[0]}"): Update the Oracle Linux image location in the specified templates
18
19
Usage:
20
$(basename "${BASH_SOURCE[0]}") [--version-major <major version>] <template.yaml>...
21
22
Description:
23
This script updates the Oracle Linux image location in the specified templates.
24
Image location basename format:
25
26
OL<major version>U<minor version>_<arch>-kvm[-cloud]-b<build number>.qcow2
27
28
Published Oracle Linux image information is fetched from the following URLs:
29
30
OL8:
31
x86_64: https://yum.oracle.com/templates/OracleLinux/ol8-template.json
32
aarch64: https://yum.oracle.com/templates/OracleLinux/ol8_aarch64-cloud-template.json
33
34
OL9:
35
x86_64: https://yum.oracle.com/templates/OracleLinux/ol9-template.json
36
aarch64: https://yum.oracle.com/templates/OracleLinux/ol9_aarch64-cloud-template.json
37
38
OL10:
39
x86_64: https://yum.oracle.com/templates/OracleLinux/ol10-template.json
40
aarch64: https://yum.oracle.com/templates/OracleLinux/ol10_aarch64-cloud-template.json
41
42
The downloaded files will be cached in the Lima cache directory.
43
44
Examples:
45
Update the Oracle Linux image location in templates/**.yaml:
46
$ $(basename "${BASH_SOURCE[0]}") templates/**.yaml
47
48
Update the Oracle Linux image location to major version 9 in ~/.lima/oraclelinux/lima.yaml:
49
$ $(basename "${BASH_SOURCE[0]}") --version-major 9 ~/.lima/oraclelinux/lima.yaml
50
$ limactl factory-reset oraclelinux
51
52
Flags:
53
--version-major <major version> Use the specified Oracle Linux <major version>.
54
The major version must be 7+ for x86_64 or 8+ for aarch64.
55
-h, --help Print this help message
56
HELP
57
}
58
59
# print the URL spec for the given location
60
function oraclelinux_url_spec_from_location() {
61
local location=$1 jq_filter url_spec
62
jq_filter='capture("
63
^https://yum\\.oracle\\.com/templates/OracleLinux/OL(?<path_major_version>\\d+)/u(?<path_minor_version>\\d+)/(?<path_arch>[^/]+)/
64
OL(?<major_version>\\d+)U(?<minor_version>\\d+)_(?<arch>[^-]+)-(?<type>[^-]+)(?<cloud>-cloud)?-b(?<build_number>\\d+)\\.(?<file_extension>.*)$
65
";"x")
66
'
67
url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"")
68
echo "${url_spec}"
69
}
70
71
readonly oraclelinux_jq_filter_json_url='
72
"https://yum.oracle.com/templates/OracleLinux/" +
73
"ol\(.path_major_version)\(if .path_arch != "x86_64" then "_" + .path_arch else "" end)\(.cloud // "")-template.json"
74
'
75
76
function oraclelinux_json_url_from_url_spec() {
77
local -r url_spec=$1
78
jq -e -r "${oraclelinux_jq_filter_json_url}" <<<"${url_spec}" ||
79
error_exit "Failed to get the JSON url for ${url_spec}"
80
}
81
82
function oraclelinux_latest_image_entry_for_url_spec() {
83
local url_spec=$1 arch json_url downloaded_json latest_version_info
84
# shellcheck disable=SC2034
85
arch=$(jq -r '.arch' <<<"${url_spec}")
86
json_url=$(oraclelinux_json_url_from_url_spec "${url_spec}")
87
downloaded_json=$(download_to_cache "${json_url}")
88
latest_version_info="$(jq -e -r --argjson spec "${url_spec}" '{
89
location: ("https://yum.oracle.com" + .base_url + "/" + .[$spec.type].image),
90
sha256: ("sha256:" + .[$spec.type].sha256)
91
}' <"${downloaded_json}")"
92
[[ -n ${latest_version_info} ]] || return
93
local location digest
94
# prefer the v<major>.<minor> in the path
95
location=$(jq -e -r '.location' <<<"${latest_version_info}")
96
location=$(validate_url_without_redirect "${location}")
97
# shellcheck disable=SC2034
98
digest=$(jq -e -r '.sha256' <<<"${latest_version_info}")
99
json_vars location arch digest
100
}
101
102
function oraclelinux_cache_key_for_image_kernel() {
103
local location=$1 overriding=${3:-"{}"} url_spec
104
url_spec=$(oraclelinux_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
105
jq -r '["oraclelinux", .path_major_version, .type, .cloud // empty, .arch, .file_extension] | join(":")' <<<"${url_spec}"
106
}
107
108
function oraclelinux_image_entry_for_image_kernel() {
109
local location=$1 kernel_is_not_supported=$2 overriding=${3:-"{}"} url_spec image_entry=''
110
[[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on Oracle Linux" >&2
111
url_spec=$(oraclelinux_url_spec_from_location "${location}" | jq -r ". + ${overriding}")
112
image_entry=$(oraclelinux_latest_image_entry_for_url_spec "${url_spec}")
113
# shellcheck disable=SC2031
114
if [[ -z ${image_entry} ]]; then
115
error_exit "Failed to get the ${url_spec} image location for ${location}"
116
elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then
117
echo "Image location is up-to-date: ${location}" >&2
118
else
119
echo "${image_entry}"
120
fi
121
}
122
123
# check if the script is executed or sourced
124
# shellcheck disable=SC1091
125
if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
126
scriptdir=$(dirname "${BASH_SOURCE[0]}")
127
# shellcheck source=./cache-common-inc.sh
128
. "${scriptdir}/cache-common-inc.sh"
129
130
# shellcheck source=/dev/null # avoid shellcheck hangs on source looping
131
. "${scriptdir}/update-template.sh"
132
else
133
# this script is sourced
134
if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
135
SUPPORTED_DISTRIBUTIONS+=("oraclelinux")
136
else
137
declare -a SUPPORTED_DISTRIBUTIONS=("oraclelinux")
138
fi
139
return 0
140
fi
141
142
declare -a templates=()
143
declare overriding='{}'
144
while [[ $# -gt 0 ]]; do
145
case "$1" in
146
-h | --help)
147
oraclelinux_print_help
148
exit 0
149
;;
150
-d | --debug) set -x ;;
151
--version-major)
152
if [[ -n $2 && $2 != -* ]]; then
153
overriding=$(
154
path_major_version="${2}"
155
[[ ${path_major_version} =~ ^[0-9]+$ ]] || error_exit "Oracle Linux major version must be a number"
156
[[ ${path_major_version} -eq 7 ]] && echo 'Oracle Linux major version 7 exists only for x86_64. It may fail for aarch64.' >&2
157
[[ ${path_major_version} -gt 7 ]] || error_exit "Oracle Linux major version must be 7+ for x86_64 or 8+ for aarch64"
158
json_vars path_major_version <<<"${overriding}"
159
)
160
shift
161
else
162
error_exit "--version-major requires a value"
163
fi
164
;;
165
--version-major=*)
166
overriding=$(
167
path_major_version="${1#*=}"
168
[[ ${path_major_version} =~ ^[0-9]+$ ]] || error_exit "Oracle Linux major version must be a number"
169
[[ ${path_major_version} -eq 7 ]] && echo 'Oracle Linux major version 7 exists only for x86_64. It may fail for aarch64.' >&2
170
[[ ${path_major_version} -gt 7 ]] || error_exit "Oracle Linux major version must be 7+ for x86_64 or 8+ for aarch64"
171
json_vars path_major_version <<<"${overriding}"
172
)
173
;;
174
*.yaml) templates+=("$1") ;;
175
*)
176
error_exit "Unknown argument: $1"
177
;;
178
esac
179
shift
180
[[ -z ${overriding} ]] && overriding="{}"
181
done
182
183
if [[ ${#templates[@]} -eq 0 ]]; then
184
oraclelinux_print_help
185
exit 0
186
fi
187
188
declare -A image_entry_cache=()
189
190
for template in "${templates[@]}"; do
191
echo "Processing ${template}"
192
# 1. extract location by parsing template using arch
193
yq_filter="
194
.images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
195
"
196
parsed=$(yq eval "${yq_filter}" "${template}")
197
198
# 3. get the image location
199
arr=()
200
while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
201
locations=("${arr[@]}")
202
for ((index = 0; index < ${#locations[@]}; index++)); do
203
[[ ${locations[index]} != "null" ]] || continue
204
set -e
205
IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}"
206
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
207
cache_key=$(
208
set -e # Enable 'set -e' for the next command.
209
oraclelinux_cache_key_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
210
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
211
# shellcheck disable=2181
212
[[ $? -eq 0 ]] || continue
213
image_entry=$(
214
set -e # Enable 'set -e' for the next command.
215
if [[ -v image_entry_cache[${cache_key}] ]]; then
216
echo "${image_entry_cache[${cache_key}]}"
217
else
218
oraclelinux_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}"
219
fi
220
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
221
# shellcheck disable=2181
222
[[ $? -eq 0 ]] || continue
223
set -e
224
image_entry_cache[${cache_key}]="${image_entry}"
225
if [[ -n ${image_entry} ]]; then
226
[[ ${kernel_cmdline} != "null" ]] &&
227
jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null &&
228
image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}")
229
echo "${image_entry}" | jq
230
limactl edit --log-level error --set "
231
.images[${index}] = ${image_entry}|
232
(.images[${index}] | ..) style = \"double\"
233
" "${template}"
234
fi
235
done
236
done
237
238