download_kernel_images.sh 1.8 KB

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