| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | set -eu -o pipefail |
| 17 | |
| 18 | template="$1" |
| 19 | appending_options="$2" |
| 20 | |
| 21 | arch="${3:-$(yq '.arch // ""' "${template}")}" |
| 22 | arch="${arch:-$(uname -m)}" |
| 23 | |
| 24 | |
| 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 | |
| 39 | readonly yq_filter=" |
| 40 | .images[]|select(.arch == \"${arch}\")|.location |
| 41 | " |
| 42 | parsed=$(yq eval "${yq_filter}" "${template}") |
| 43 | |
| 44 | |
| 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 | |
| 55 | if check_location "${locations[i]}"; then |
| 56 | location=${locations[i]} |
| 57 | index=${i} |
| 58 | break |
| 59 | fi |
| 60 | done |
| 61 | |
| 62 | |
| 63 | case "${location:-}" in |
| 64 | https://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 | ;; |
| 67 | https://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 | ;; |
| 74 | esac |
| 75 | |
| 76 | |
| 77 | location_dirname=$(dirname "${location}")/unpacked |
| 78 | sha256sums=$(curl -sSLf "${location_dirname}/SHA256SUMS") |
| 79 | location_basename=$(basename "${location}") |
| 80 | |
| 81 | |
| 82 | cmdline="${default_cmdline} ${appending_options}" |
| 83 | |
| 84 | |
| 85 | kernel_basename="${location_basename/.img/-vmlinuz-generic}" |
| 86 | kernel_digest=$(awk "/${kernel_basename}/{print \"sha256:\"\$1}" <<<"${sha256sums}") |
| 87 | kernel_location="${location_dirname}/${kernel_basename}" |
| 88 | |
| 89 | |
| 90 | initrd_basename="${location_basename/.img/-initrd-generic}" |
| 91 | initrd_digest=$(awk "/${initrd_basename}/{print \"sha256:\"\$1}" <<<"${sha256sums}") |
| 92 | initrd_location="${location_dirname}/${initrd_basename}" |
| 93 | |
| 94 | |
| 95 | function inject_to() { |
| 96 | |
| 97 | local template=$1 arch=$2 index=$3 key=$4 location=$5 digest=$6 cmdline=${7:-} fields=() IFS=, |
| 98 | |
| 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 | } |
| 105 | inject_to "${template}" "${arch}" "${index}" "kernel" "${kernel_location}" "${kernel_digest}" "${cmdline}" |
| 106 | inject_to "${template}" "${arch}" "${index}" "initrd" "${initrd_location}" "${initrd_digest}" |
| 107 | |
| 108 | |
| 109 | readonly outputs=(kernel_location kernel_digest cmdline initrd_location initrd_digest) |
| 110 | for output in "${outputs[@]}"; do |
| 111 | echo "${output}=${!output}" |
| 112 | done |