| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | set -eu -o pipefail |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | shopt -s inherit_errexit || error_exit "inherit_errexit not supported. Please use bash 4.4 or later." |
| 14 | |
| 15 | function freebsd_print_help() { |
| 16 | cat <<HELP |
| 17 | $(basename "${BASH_SOURCE[0]}"): Update the FreeBSD image location in the specified templates |
| 18 | |
| 19 | Usage: |
| 20 | $(basename "${BASH_SOURCE[0]}") [--version <major>.<minor>] <template.yaml>... |
| 21 | |
| 22 | Description: |
| 23 | This script updates the FreeBSD image location in the specified templates. |
| 24 | Image location basename format: |
| 25 | |
| 26 | FreeBSD-<version>-RELEASE-<arch>[-BASIC-CLOUDINIT]-<fs>.<format>.xz |
| 27 | |
| 28 | Published FreeBSD image information is fetched from the following URL: |
| 29 | |
| 30 | ${freebsd_archive_url} |
| 31 | |
| 32 | The downloaded files will be cached in the Lima cache directory. |
| 33 | |
| 34 | Examples: |
| 35 | Update the FreeBSD image location in templates/**.yaml: |
| 36 | $ $(basename "${BASH_SOURCE[0]}") templates/**.yaml |
| 37 | |
| 38 | Update the FreeBSD image location to version 14.3 in ~/.lima/freebsd/lima.yaml: |
| 39 | $ $(basename "${BASH_SOURCE[0]}") --version 14.3 ~/.lima/freebsd/lima.yaml |
| 40 | $ limactl factory-reset freebsd |
| 41 | |
| 42 | Flags: |
| 43 | --version <major>.<minor> Use the specified <major>.<minor> version. |
| 44 | -h, --help Print this help message |
| 45 | HELP |
| 46 | } |
| 47 | |
| 48 | |
| 49 | readonly freebsd_archive_url='http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/VM-IMAGES/' |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | function freebsd_url_spec_from_location() { |
| 61 | local location=$1 jq_filter url_spec |
| 62 | jq_filter='capture(" |
| 63 | ^http://ftp-archive\\.freebsd\\.org/pub/FreeBSD-Archive/old-releases/VM-IMAGES/ |
| 64 | (?<version>\\d+\\.\\d+)-RELEASE/(?<dir_arch>[^/]+)/Latest/ |
| 65 | FreeBSD-\\d+\\.\\d+-RELEASE-(?<filename_arch>arm64-aarch64|riscv-riscv64|amd64) |
| 66 | (?<cloudinit>-BASIC-CLOUDINIT)?-(?<fs>zfs|ufs)\\.(?<format>raw|qcow2)\\.xz$ |
| 67 | ";"x") | .cloudinit = (.cloudinit != null) |
| 68 | ' |
| 69 | url_spec=$(jq -e -r "${jq_filter}" <<<"\"${location}\"") |
| 70 | echo "${url_spec}" |
| 71 | } |
| 72 | |
| 73 | readonly freebsd_jq_filter_directory='"http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/old-releases/VM-IMAGES/\(.version)-RELEASE/\(.dir_arch)/Latest/"' |
| 74 | readonly freebsd_jq_filter_filename=' |
| 75 | "FreeBSD-\(.version)-RELEASE-\(.filename_arch)\(if .cloudinit then "-BASIC-CLOUDINIT" else "" end)-\(.fs).\(.format).xz" |
| 76 | ' |
| 77 | |
| 78 | |
| 79 | function freebsd_location_from_url_spec() { |
| 80 | local url_spec=$1 |
| 81 | jq -e -r "${freebsd_jq_filter_directory} + ${freebsd_jq_filter_filename}" <<<"${url_spec}" || |
| 82 | error_exit "Failed to get the location for ${url_spec}" |
| 83 | } |
| 84 | |
| 85 | function freebsd_directory_from_url_spec() { |
| 86 | local url_spec=$1 |
| 87 | jq -e -r "${freebsd_jq_filter_directory}" <<<"${url_spec}" || |
| 88 | error_exit "Failed to get the directory for ${url_spec}" |
| 89 | } |
| 90 | |
| 91 | function freebsd_filename_from_url_spec() { |
| 92 | local url_spec=$1 |
| 93 | jq -e -r "${freebsd_jq_filter_filename}" <<<"${url_spec}" || |
| 94 | error_exit "Failed to get the filename for ${url_spec}" |
| 95 | } |
| 96 | |
| 97 | function freebsd_latest_image_entry_for_url_spec() { |
| 98 | local url_spec=$1 releases_page latest_version newer_url_spec location filename checksum_url downloaded_checksum digest arch |
| 99 | releases_page=$(download_to_cache "${freebsd_archive_url}") |
| 100 | |
| 101 | |
| 102 | latest_version=$(jq -e -Rrs --argjson spec "${url_spec}" ' |
| 103 | [ |
| 104 | split("\n").[] | |
| 105 | select(test("href=\"\\d+\\.\\d+-RELEASE/\"")) | |
| 106 | capture("href=\"(?<ver>\\d+\\.\\d+)-RELEASE/\"") | |
| 107 | .ver | |
| 108 | select((split(".")[0] | tonumber) == ($spec.version | split(".")[0] | tonumber)) |
| 109 | ] | |
| 110 | sort_by(split(".") | map(tonumber)) | |
| 111 | last |
| 112 | ' <"${releases_page}") || |
| 113 | error_exit "No RELEASE found for FreeBSD $(jq -r '.version | split(".")[0]' <<<"${url_spec}").x in ${freebsd_archive_url}" |
| 114 | [[ -n ${latest_version} ]] || return |
| 115 | newer_url_spec=$(jq -e -r ". + {version: \"${latest_version}\"}" <<<"${url_spec}") |
| 116 | filename=$(freebsd_filename_from_url_spec "${newer_url_spec}") |
| 117 | checksum_url="$(freebsd_directory_from_url_spec "${newer_url_spec}")CHECKSUM.SHA256" |
| 118 | downloaded_checksum=$(download_to_cache "${checksum_url}") |
| 119 | digest="sha256:$(awk "/SHA256 \\(${filename}\\) =/{print \$4}" "${downloaded_checksum}")" |
| 120 | [[ -n ${digest} ]] || error_exit "Failed to get the digest for ${filename}" |
| 121 | location=$(freebsd_location_from_url_spec "${newer_url_spec}") |
| 122 | location=$(validate_url "${location}") |
| 123 | arch=$(jq -e -r '.dir_arch' <<<"${newer_url_spec}") |
| 124 | arch=$(limayaml_arch "${arch}") |
| 125 | json_vars location arch digest |
| 126 | } |
| 127 | |
| 128 | function freebsd_cache_key_for_image_kernel() { |
| 129 | local location=$1 url_spec |
| 130 | url_spec=$(freebsd_url_spec_from_location "${location}") |
| 131 | jq -r '["freebsd", (.version | split(".")[0]), .dir_arch, (if .cloudinit then "cloudinit" else "" end), .fs, .format] | join(":")' <<<"${url_spec}" |
| 132 | } |
| 133 | |
| 134 | function freebsd_image_entry_for_image_kernel() { |
| 135 | local location=$1 kernel_is_not_supported=$2 overriding=${3:-'{}'} url_spec image_entry='' |
| 136 | [[ ${kernel_is_not_supported} == "null" ]] || echo "Updating kernel information is not supported on FreeBSD" >&2 |
| 137 | url_spec=$(freebsd_url_spec_from_location "${location}" | jq -r ". + ${overriding}") |
| 138 | image_entry=$(freebsd_latest_image_entry_for_url_spec "${url_spec}") |
| 139 | # shellcheck disable=SC2031 |
| 140 | if [[ -z ${image_entry} ]]; then |
| 141 | error_exit "Failed to get the ${url_spec} image location for ${location}" |
| 142 | elif jq -e ".location == \"${location}\"" <<<"${image_entry}" >/dev/null; then |
| 143 | echo "Image location is up-to-date: ${location}" >&2 |
| 144 | else |
| 145 | echo "${image_entry}" |
| 146 | fi |
| 147 | } |
| 148 | |
| 149 | # check if the script is executed or sourced |
| 150 | # shellcheck disable=SC1091 |
| 151 | if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then |
| 152 | scriptdir=$(dirname "${BASH_SOURCE[0]}") |
| 153 | # shellcheck source=./cache-common-inc.sh |
| 154 | . "${scriptdir}/cache-common-inc.sh" |
| 155 | |
| 156 | # shellcheck source=/dev/null # avoid shellcheck hangs on source looping |
| 157 | . "${scriptdir}/update-template.sh" |
| 158 | else |
| 159 | # this script is sourced |
| 160 | if [[ -v SUPPORTED_DISTRIBUTIONS ]]; then |
| 161 | SUPPORTED_DISTRIBUTIONS+=("freebsd") |
| 162 | else |
| 163 | declare -a SUPPORTED_DISTRIBUTIONS=("freebsd") |
| 164 | fi |
| 165 | return 0 |
| 166 | fi |
| 167 | |
| 168 | declare -a templates=() |
| 169 | declare overriding='{}' |
| 170 | while [[ $# -gt 0 ]]; do |
| 171 | case "$1" in |
| 172 | -h | --help) |
| 173 | freebsd_print_help |
| 174 | exit 0 |
| 175 | ;; |
| 176 | -d | --debug) set -x ;; |
| 177 | --version) |
| 178 | if [[ -n ${2:-} && $2 != -* ]]; then |
| 179 | version="$2" |
| 180 | shift |
| 181 | else |
| 182 | error_exit "--version requires a value" |
| 183 | fi |
| 184 | ;& |
| 185 | --version=*) |
| 186 | version=${version:-${1#*=}} |
| 187 | overriding=$( |
| 188 | [[ ${version} =~ ^[0-9]+\.[0-9]+$ ]] || |
| 189 | error_exit "The version must be in the format <major>.<minor>" |
| 190 | json_vars version <<<"${overriding}" |
| 191 | ) |
| 192 | ;; |
| 193 | *.yaml) templates+=("$1") ;; |
| 194 | *) |
| 195 | error_exit "Unknown argument: $1" |
| 196 | ;; |
| 197 | esac |
| 198 | shift |
| 199 | [[ -z ${overriding} ]] && overriding="{}" |
| 200 | done |
| 201 | |
| 202 | if [[ ${#templates[@]} -eq 0 ]]; then |
| 203 | freebsd_print_help |
| 204 | exit 0 |
| 205 | fi |
| 206 | |
| 207 | declare -A image_entry_cache=() |
| 208 | |
| 209 | for template in "${templates[@]}"; do |
| 210 | echo "Processing ${template}" |
| 211 | # 1. extract location by parsing template using arch |
| 212 | yq_filter=" |
| 213 | .images[] | [.location, .kernel.location, .kernel.cmdline] | @tsv |
| 214 | " |
| 215 | parsed=$(yq eval "${yq_filter}" "${template}") |
| 216 | |
| 217 | # 3. get the image location |
| 218 | arr=() |
| 219 | while IFS= read -r line; do arr+=("${line}"); done <<<"${parsed}" |
| 220 | locations=("${arr[@]}") |
| 221 | for ((index = 0; index < ${#locations[@]}; index++)); do |
| 222 | [[ ${locations[index]} != "null" ]] || continue |
| 223 | set -e |
| 224 | IFS=$'\t' read -r location kernel_location kernel_cmdline <<<"${locations[index]}" |
| 225 | set +e # Disable 'set -e' to avoid exiting on error for the next assignment. |
| 226 | cache_key=$( |
| 227 | set -e # Enable 'set -e' for the next command. |
| 228 | freebsd_cache_key_for_image_kernel "${location}" "${kernel_location}" |
| 229 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 230 | # shellcheck disable=2181 |
| 231 | [[ $? -eq 0 ]] || continue |
| 232 | image_entry=$( |
| 233 | set -e # Enable 'set -e' for the next command. |
| 234 | if [[ -v image_entry_cache[${cache_key}] ]]; then |
| 235 | echo "${image_entry_cache[${cache_key}]}" |
| 236 | else |
| 237 | freebsd_image_entry_for_image_kernel "${location}" "${kernel_location}" "${overriding}" |
| 238 | fi |
| 239 | ) # Check exit status separately to prevent disabling 'set -e' by using the function call in the condition. |
| 240 | # shellcheck disable=2181 |
| 241 | [[ $? -eq 0 ]] || continue |
| 242 | set -e |
| 243 | image_entry_cache[${cache_key}]="${image_entry}" |
| 244 | if [[ -n ${image_entry} ]]; then |
| 245 | [[ ${kernel_cmdline} != "null" ]] && |
| 246 | jq -e 'has("kernel")' <<<"${image_entry}" >/dev/null && |
| 247 | image_entry=$(jq ".kernel.cmdline = \"${kernel_cmdline}\"" <<<"${image_entry}") |
| 248 | echo "${image_entry}" | jq |
| 249 | limactl edit --log-level error --set " |
| 250 | .images[${index}] *= ${image_entry}| |
| 251 | (.images[${index}] | ..) style = \"double\" |
| 252 | " "${template}" |
| 253 | fi |
| 254 | done |
| 255 | done |