Skip to content

Instantly share code, notes, and snippets.

@albal
Created February 19, 2025 20:26
Show Gist options
  • Select an option

  • Save albal/dce11fd6b7846d5094e28ced48625109 to your computer and use it in GitHub Desktop.

Select an option

Save albal/dce11fd6b7846d5094e28ced48625109 to your computer and use it in GitHub Desktop.
Download binaries needed for airgapped K3S install
#!/usr/bin/env bash
#
# Usage:
# ./download-k3s-airgap-images.sh [K3S_RELEASE_TAG] [ARCH]
#
# Examples:
# # Download for the latest stable release, auto-detect arch
# ./download-k3s-airgap-images.sh
#
# # Download for a specific release and architecture
# ./download-k3s-airgap-images.sh v1.26.2+k3s1 arm64
#
# Notes:
# - This script uses the GitHub API to detect the latest stable release.
# - It downloads the .tar.zst file and the associated sha256sum.
# - It then verifies the sha256sum.
set -euo pipefail
# Optional: specify a release tag (e.g. "v1.26.2+k3s1").
# If none is provided, 'latest' is used, which triggers detection of the latest stable release.
K3S_RELEASE="${1:-latest}"
# Optional: specify an architecture. If none is provided, we'll detect it from uname -m.
# Common valid K3s arches: amd64, arm64, armhf
ARCH="${2:-$(uname -m)}"
# Convert uname architecture to K3s naming if needed
case "$ARCH" in
x86_64)
ARCH="amd64"
;;
aarch64)
ARCH="arm64"
;;
armv7l)
ARCH="arm"
;;
esac
# -- Choose the binary name based on architecture --
# - amd64 -> k3s
# - arm64 -> k3s-arm64
# - armhf -> k3s-armhf
case "$ARCH" in
amd64)
BINARY_NAME="k3s"
;;
arm64)
BINARY_NAME="k3s-arm64"
;;
arm)
BINARY_NAME="k3s-armhf"
;;
*)
echo "Unsupported or unknown architecture: $ARCH"
exit 1
;;
esac
# Function to get the latest stable (non-pre-release) K3s version from GitHub
get_latest_stable_release() {
# Pull all releases, filter out pre-releases and drafts, sort by version, take the latest.
curl -s https://api.github.com/repos/k3s-io/k3s/releases \
| jq -r '.[] | select(.prerelease == false and .draft == false) | .tag_name' \
| sort -rV \
| head -n1
}
# If 'latest' was specified, we pull the latest stable release from the API
if [ "$K3S_RELEASE" == "latest" ]; then
echo "No release specified, finding the latest stable K3s release..."
K3S_RELEASE="$(get_latest_stable_release)"
if [ -z "$K3S_RELEASE" ]; then
echo "Error: Could not determine the latest stable release from GitHub."
exit 1
fi
echo "Latest stable K3s release is: $K3S_RELEASE"
fi
# Construct the file name based on the chosen release and architecture
IMAGE_FILENAME="k3s-airgap-images-${ARCH}.tar.gz"
K3S_URL="https://github.com/k3s-io/k3s/releases/download/${K3S_RELEASE}/${BINARY_NAME}"
DOWNLOAD_URL="https://github.com/k3s-io/k3s/releases/download/${K3S_RELEASE}/${IMAGE_FILENAME}"
SHA256_URL="https://github.com/k3s-io/k3s/releases/download/${K3S_RELEASE}/sha256sum-${ARCH}.txt"
echo "-----------------------------------------------------"
echo "K3S Release: $K3S_RELEASE"
echo "Architecture: $ARCH"
echo "Image file: $IMAGE_FILENAME"
echo "Download URL: $DOWNLOAD_URL"
echo "Download K3S: $K3S_URL"
echo "Checksum URL: $SHA256_URL"
echo "-----------------------------------------------------"
# Download the airgap image tar.zst
echo "Downloading airgap image..."
curl -fSL -O "${DOWNLOAD_URL}"
# Download the k3s binary
echo "Downloading k3s binary..."
curl -fSL -O "${K3S_URL}"
# Download the sha256sum file
echo "Downloading airgap image sha256sum..."
curl -fSL -O "${SHA256_URL}"
# Extract used checksums
grep gz sha256sum-${ARCH}.txt > sha256sum-gz.txt
grep "${BINARY_NAME}$" sha256sum-${ARCH}.txt > sha256sum-k3s.txt
# Validate the downloaded images file
echo "Validating images checksum..."
sha256sum -c "sha256sum-gz.txt"
# Validate the downloaded k3s file
echo "Validating k3s checksum..."
sha256sum -c "sha256sum-k3s.txt"
echo "-----------------------------------------------------"
echo "Download and validation complete!"
echo "You now have ${IMAGE_FILENAME} (and its verified checksum)."
echo "Use this file for your air-gapped K3s installation."
echo "-----------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment