download_kernel_images.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Check for required arguments.
  4. if [ "$#" -lt 3 ]; then
  5. echo "Usage: $0 <output directory> <architecture> <version1> [<version2> ...]"
  6. exit 1
  7. fi
  8. OUTPUT_DIR=$1
  9. ARCHITECTURE=$2
  10. shift 2
  11. VERSIONS=("$@")
  12. URLS=$(lynx -dump -listonly -nonumbers https://mirrors.wikimedia.org/debian/pool/main/l/linux/)
  13. readonly URLS
  14. # Find the latest revision of each kernel version.
  15. FILES=()
  16. for VERSION in "${VERSIONS[@]}"; do
  17. REGEX="linux-image-${VERSION//./\\.}\\.[0-9]+(-[0-9]+)?(\+bpo)?-cloud-${ARCHITECTURE}-unsigned_.*\\.deb"
  18. match=$(printf '%s\n' "$URLS" | grep -E "$REGEX" | sort -V | tail -n1) || {
  19. printf '%s\nVERSION=%s\nREGEX=%s\n' "$URLS" "$VERSION" "$REGEX" >&2
  20. exit 1
  21. }
  22. FILES+=("$match")
  23. done
  24. # Note: `--etag-{compare,save}` are not idempotent until curl 8.9.0 which included
  25. # https://github.com/curl/curl/commit/85efbb92b8e6679705e122cee45ce76c56414a3e. At the time of
  26. # writing our CI uses Ubuntu 22.04 which has curl 7.81.0 and the latest available is Ubuntu 24.04
  27. # which has curl 8.5.0. Since neither has a new enough curl, we don't bother to update, but we
  28. # should do so when Ubuntu 24.10 or later is available.
  29. mkdir -p "$OUTPUT_DIR"
  30. KEEP=()
  31. for FILE in "${FILES[@]}"; do
  32. name=$(basename "$FILE")
  33. etag_name="$name.etag"
  34. KEEP+=("$name" "$etag_name")
  35. etag="$OUTPUT_DIR/$etag_name"
  36. curl -sfSL --output-dir "$OUTPUT_DIR" --remote-name-all --etag-compare "$etag" --etag-save "$etag" "$FILE"
  37. done
  38. # Remove any files that were previously downloaded that are no longer needed.
  39. FIND_ARGS=()
  40. for FILE in "${KEEP[@]}"; do
  41. FIND_ARGS+=("!" "-name" "$FILE")
  42. done
  43. find "$OUTPUT_DIR" -type f "${FIND_ARGS[@]}" -exec rm {} +