Skip to content

Commit 6935a63

Browse files
feat(templates): add amazonlinux-2023 distro
This commit adds a simple template for the Amazon Linux 2023 operating system's 2025-12-08 version. The OS is scheduled to release every two weeks, therefore, a new script called hack/update-template-amazonlinux.sh is added next to the other template updater scripts. Find the latest images here: cdn.amazonlinux.com/al2023/os-images/latest/ This patch: - Adds amazonlinux-2023 template with 2025-12-08 release by default (fallback). - Resolves mount issues on AL2023 with host OS. - Introduces a script on VM init where it updates the system to latest release. - Adds update-template-amazonlinux.sh script to help maintainers automatically update the verison. Signed-off-by: Gyokhan Kochmarla <[email protected]>
1 parent b18eed7 commit 6935a63

File tree

6 files changed

+244
-0
lines changed

6 files changed

+244
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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 amazonlinux_print_help() {
16+
cat <<HELP
17+
$(basename "${BASH_SOURCE[0]}"): Update the Amazon Linux 2023 image location in the specified templates
18+
19+
Usage:
20+
$(basename "${BASH_SOURCE[0]}") <template.yaml>...
21+
22+
Description:
23+
This script updates the Amazon Linux 2023 image location in the specified templates.
24+
Image location format:
25+
https://cdn.amazonlinux.com/al2023/os-images/<version>/<folder>/<filename>
26+
27+
Latest version information is fetched from:
28+
https://cdn.amazonlinux.com/al2023/os-images/latest/
29+
30+
Examples:
31+
Update the Amazon Linux 2023 image location in templates/**.yaml:
32+
$ $(basename "${BASH_SOURCE[0]}") templates/**.yaml
33+
34+
Flags:
35+
-h, --help Print this help message
36+
HELP
37+
}
38+
39+
function amazonlinux_cache_key_for_image_kernel() {
40+
local location=$1
41+
case "${location}" in
42+
https://cdn.amazonlinux.com/al2023/os-images/*) ;;
43+
*) return 1 ;;
44+
esac
45+
# Cache key based on architecture and variant derived from location
46+
# We use a simple heuristic: file path components.
47+
# The URL structure is .../os-images/<version>/<folder>/...
48+
# We want a key that represents "amazonlinux:al2023:<folder>"
49+
local folder
50+
if [[ "${location}" == *"/kvm-arm64/"* ]]; then
51+
folder="kvm-arm64"
52+
elif [[ "${location}" == *"/kvm/"* ]]; then
53+
folder="kvm"
54+
else
55+
return 1
56+
fi
57+
echo "amazonlinux:al2023:${folder}"
58+
}
59+
60+
function amazonlinux_image_entry_for_image_kernel() {
61+
local location=$1
62+
case "${location}" in
63+
https://cdn.amazonlinux.com/al2023/os-images/*) ;;
64+
*) return 1 ;;
65+
esac
66+
67+
local latest_url
68+
# Get the redirect URL to find the latest version
69+
latest_url=$(curl -Ls -o /dev/null -w '%{url_effective}' https://cdn.amazonlinux.com/al2023/os-images/latest/)
70+
local version
71+
version=$(basename "${latest_url}")
72+
73+
local arch folder
74+
if [[ "${location}" == *"x86_64"* ]]; then
75+
# shellcheck disable=SC2034
76+
arch="x86_64"
77+
folder="kvm"
78+
elif [[ "${location}" == *"arm64"* ]] || [[ "${location}" == *"aarch64"* ]]; then
79+
# shellcheck disable=SC2034
80+
arch="aarch64"
81+
folder="kvm-arm64"
82+
else
83+
error_exit "Unknown arch for amazonlinux location: ${location}"
84+
fi
85+
86+
local base_url="https://cdn.amazonlinux.com/al2023/os-images/${version}/${folder}"
87+
local checksums
88+
checksums=$(download_to_cache "${base_url}/SHA256SUMS")
89+
90+
local filename digest line
91+
# Find the qcow2 file in SHA256SUMS
92+
line=$(grep "\.qcow2$" "${checksums}" | head -n 1)
93+
[[ -n ${line} ]] || error_exit "No qcow2 image found in ${base_url}/SHA256SUMS"
94+
95+
# shellcheck disable=SC2034
96+
digest=$(echo "${line}" | awk '{print "sha256:"$1}')
97+
filename=$(echo "${line}" | awk '{print $2}')
98+
99+
# shellcheck disable=SC2034
100+
local location="${base_url}/${filename}"
101+
102+
json_vars location arch digest
103+
}
104+
105+
# check if the script is executed or sourced
106+
# shellcheck disable=SC1091
107+
if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
108+
scriptdir=$(dirname "${BASH_SOURCE[0]}")
109+
# shellcheck source=./cache-common-inc.sh
110+
. "${scriptdir}/cache-common-inc.sh"
111+
112+
# shellcheck source=/dev/null # avoid shellcheck hangs on source looping
113+
. "${scriptdir}/update-template.sh"
114+
else
115+
# this script is sourced
116+
if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then
117+
SUPPORTED_DISTRIBUTIONS+=("amazonlinux")
118+
else
119+
declare -a SUPPORTED_DISTRIBUTIONS=("amazonlinux")
120+
fi
121+
return 0
122+
fi
123+
124+
declare -a templates=()
125+
while [[ $# -gt 0 ]]; do
126+
case "$1" in
127+
-h | --help)
128+
amazonlinux_print_help
129+
exit 0
130+
;;
131+
-d | --debug) set -x ;;
132+
*.yaml) templates+=("$1") ;;
133+
*)
134+
error_exit "Unknown argument: $1"
135+
;;
136+
esac
137+
shift
138+
done
139+
140+
if [[ ${#templates[@]} -eq 0 ]]; then
141+
amazonlinux_print_help
142+
exit 0
143+
fi
144+
145+
declare -A image_entry_cache=()
146+
147+
for template in "${templates[@]}"; do
148+
echo "Processing ${template}"
149+
# 1. extract location by parsing template
150+
yq_filter="
151+
.images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv
152+
"
153+
parsed=$(yq eval "${yq_filter}" "${template}")
154+
155+
# 2. get the image location
156+
arr=()
157+
while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}"
158+
locations=("${arr[@]}")
159+
for ((index = 0; index < ${#locations[@]}; index++)); do
160+
[[ ${locations[index]} != "null" ]] || continue
161+
set -e
162+
IFS=$'\t' read -r location _kernel_location _kernel_cmdline <<<"${locations[index]}"
163+
set +e # Disable 'set -e' to avoid exiting on error for the next assignment.
164+
cache_key=$(
165+
set -e # Enable 'set -e' for the next command.
166+
amazonlinux_cache_key_for_image_kernel "${location}"
167+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
168+
# shellcheck disable=2181
169+
[[ $? -eq 0 ]] || continue
170+
image_entry=$(
171+
set -e # Enable 'set -e' for the next command.
172+
if [[ -v image_entry_cache[${cache_key}] ]]; then
173+
echo "${image_entry_cache[${cache_key}]}"
174+
else
175+
amazonlinux_image_entry_for_image_kernel "${location}"
176+
fi
177+
) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition.
178+
# shellcheck disable=2181
179+
[[ $? -eq 0 ]] || continue
180+
set -e
181+
image_entry_cache[${cache_key}]="${image_entry}"
182+
if [[ -n ${image_entry} ]]; then
183+
echo "${image_entry}" | jq
184+
limactl edit --log-level error --set "
185+
.images[${index}] = ${image_entry}|
186+
(.images[${index}] | ..) style = \"double\"
187+
" "${template}"
188+
fi
189+
done
190+
done

hack/update-template.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then
170170
. "${scriptdir}/update-template-fedora.sh"
171171
# shellcheck source=./update-template-opensuse.sh
172172
. "${scriptdir}/update-template-opensuse.sh"
173+
# shellcheck source=./update-template-amazonlinux.sh
174+
. "${scriptdir}/update-template-amazonlinux.sh"
173175
else
174176
# this script is sourced
175177
return 0
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# SPDX-FileCopyrightText: Copyright The Lima Authors
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
set -eux -o pipefail
7+
8+
# Workaround for Amazon Linux 2023
9+
if [ -f /etc/os-release ] && grep -q "Amazon Linux" /etc/os-release; then
10+
# 1. Create missing mount.virtiofs helper
11+
if [ ! -e /sbin/mount.virtiofs ]; then
12+
cat >/sbin/mount.virtiofs <<'EOF'
13+
#!/bin/sh
14+
exec mount -i -t virtiofs "$@"
15+
EOF
16+
chmod +x /sbin/mount.virtiofs
17+
fi
18+
19+
# Cloud-init fails to mount 'mount0' because it doesn't recognize it as a device,
20+
# so we extract the intended path from user-data and mount it manually.
21+
USER_DATA="/var/lib/cloud/instance/user-data.txt"
22+
if [ -f "${USER_DATA}" ]; then
23+
MOUNT_POINT=$(grep "mount0" "${USER_DATA}" | awk -F, '{print $2}' | tr -d '[:space:]')
24+
if [ -n "${MOUNT_POINT}" ] && ! mountpoint -q "${MOUNT_POINT}"; then
25+
mkdir -p "${MOUNT_POINT}"
26+
mount -t virtiofs mount0 "${MOUNT_POINT}" || true
27+
fi
28+
fi
29+
fi
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
images:
2+
- location: "https://cdn.amazonlinux.com/al2023/os-images/2023.9.20251208.0/kvm/al2023-kvm-2023.9.20251208.0-kernel-6.1-x86_64.xfs.gpt.qcow2"
3+
arch: "x86_64"
4+
digest: "sha256:c91f244483d9a460869738f7fcfb5e37eb402aec8582d10456cf0c108e1a4d4c"
5+
- location: "https://cdn.amazonlinux.com/al2023/os-images/2023.9.20251208.0/kvm-arm64/al2023-kvm-2023.9.20251208.0-kernel-6.1-arm64.xfs.gpt.qcow2"
6+
arch: "aarch64"
7+
digest: "sha256:7aef1f189076a0416cd16062bbf7e0928d81061e68411cab8904447e1bd5eafd"

templates/amazonlinux-2023.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
minimumLimaVersion: 2.0.0
2+
3+
base:
4+
- template:_images/amazonlinux-2023
5+
- template:_default/mounts
6+
7+
provision:
8+
- mode: system
9+
script: |
10+
#!/bin/bash
11+
set -eux -o pipefail
12+
# Update to the latest release on the first boot
13+
if [ ! -f /etc/lima-al2023-updated ]; then
14+
dnf upgrade -y --releasever=latest
15+
fi

templates/amazonlinux.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
amazonlinux-2023.yaml

0 commit comments

Comments
 (0)