download_kernel_images.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. # TODO(https://github.com/curl/curl/issues/15729): restore --parallel here if and when it properly
  25. # supports ETags.
  26. # TODO(https://github.com/curl/curl/issues/15730): restore --create-dirs when it works in the
  27. # presence of `--etag-save`.`
  28. #
  29. # Note: `--etag-{compare,save}` are not idempotent until curl 8.9.0 which included
  30. # https://github.com/curl/curl/commit/85efbb92b8e6679705e122cee45ce76c56414a3e. At the time of
  31. # writing our CI uses Ubuntu 22.04 which has curl 7.81.0 and the latest available is Ubuntu 24.04
  32. # which has curl 8.5.0. Since neither has a new enough curl, we don't bother to update, but we
  33. # should do so when Ubuntu 24.10 or later is available.
  34. mkdir -p "$OUTPUT_DIR"
  35. KEEP=()
  36. for FILE in "${FILES[@]}"; do
  37. name=$(basename "$FILE")
  38. etag_name="$name.etag"
  39. KEEP+=("$name" "$etag_name")
  40. etag="$OUTPUT_DIR/$etag_name"
  41. curl -sfSL --output-dir "$OUTPUT_DIR" --remote-name-all --etag-compare "$etag" --etag-save "$etag" "$FILE"
  42. done
  43. # Remove any files that were previously downloaded that are no longer needed.
  44. FIND_ARGS=()
  45. for FILE in "${KEEP[@]}"; do
  46. FIND_ARGS+=("!" "-name" "$FILE")
  47. done
  48. find "$OUTPUT_DIR" -type f "${FIND_ARGS[@]}" -exec rm {} +