sdk.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. # Downloads an OpenWrt SDK and verifies it against the target sha256sums
  3. # before extracting it. Sourced by package*.sh.
  4. #
  5. # Usage:
  6. # SDK_DIR="$(download_verified_sdk "$SDK_DIR_URL" "$WORK_DIR")" || exit 1
  7. download_verified_sdk() {
  8. local dir_url="$1"
  9. local work_dir="$2"
  10. local name sums line sdk_path
  11. sums="$work_dir/sha256sums"
  12. if ! curl -fsSL "${dir_url}sha256sums" -o "$sums"; then
  13. echo "ERROR: no sha256sums under ${dir_url}" >&2
  14. return 1
  15. fi
  16. name="$(curl -fsSL "$dir_url" 2>/dev/null | grep -o 'openwrt-sdk-[^"]*\.tar\.zst' | head -1 || true)"
  17. if [ -z "$name" ]; then
  18. echo "ERROR: SDK not found under ${dir_url}" >&2
  19. return 1
  20. fi
  21. line="$(awk -v n="$name" '
  22. {
  23. candidate = $NF
  24. sub(/^\*/, "", candidate)
  25. if (candidate == n) {
  26. print
  27. found = 1
  28. }
  29. }
  30. END { exit found ? 0 : 1 }
  31. ' "$sums")" || {
  32. echo "ERROR: ${name} is not listed in sha256sums" >&2
  33. return 1
  34. }
  35. printf '%s\n' "$line" > "$work_dir/sdk.sha256"
  36. echo " SDK: ${dir_url}${name}" >&2
  37. sdk_path="$work_dir/$name"
  38. if ! curl -fsSL "${dir_url}${name}" -o "$sdk_path"; then
  39. echo "ERROR: could not download ${name}" >&2
  40. return 1
  41. fi
  42. if ! (cd "$work_dir" && sha256sum -c sdk.sha256) >&2; then
  43. echo "ERROR: SDK checksum mismatch for ${name}" >&2
  44. return 1
  45. fi
  46. if ! tar --zstd -xf "$sdk_path" -C "$work_dir"; then
  47. echo "ERROR: could not extract ${name}" >&2
  48. return 1
  49. fi
  50. printf '%s\n' "$work_dir/${name%.tar.zst}"
  51. }
  52. # ipk_data_tar <ipk>: writes the member data.tar.gz of an ipk to stdout.
  53. #
  54. # The ipk container is not always an `ar` archive (#2016): ipkg-build in the
  55. # 24.10 SDK packs debian-binary, data.tar.gz and control.tar.gz as a gzip tar
  56. # (members come as ./data.tar.gz), so `ar p` on it dies with "file format not
  57. # recognized" and the release job failed before uploading anything. Both shapes
  58. # are valid packages, so detect instead of assuming.
  59. ipk_data_tar() {
  60. local ipk="$1" member listing
  61. if ar t "$ipk" >/dev/null 2>&1; then
  62. ar p "$ipk" data.tar.gz
  63. return
  64. fi
  65. listing="$(tar -tzf "$ipk" 2>/dev/null)" || {
  66. echo "ERROR: $ipk is neither an ar ipk nor a gzip tar one" >&2
  67. return 1
  68. }
  69. for member in ./data.tar.gz data.tar.gz; do
  70. if printf '%s\n' "$listing" | grep -qx -- "$member"; then
  71. tar -xzOf "$ipk" "$member"
  72. return
  73. fi
  74. done
  75. echo "ERROR: no data.tar.gz inside $ipk" >&2
  76. return 1
  77. }
  78. # verify_ipk_modes <ipk> <path>=<octal-mode>... checks that the packaged files
  79. # carry the expected permission bits inside data.tar.gz (#1701: ipkg-build
  80. # does not fix permissions, and a non-executable /etc/init.d script breaks
  81. # enable/start on the router). Uses ar + tar, both present on CI hosts.
  82. verify_ipk_modes() {
  83. local ipk="$1"
  84. shift
  85. local spec path want got line data_file
  86. if [ ! -f "$ipk" ]; then
  87. echo "ERROR: verify_ipk_modes: ipk not found: $ipk" >&2
  88. exit 1
  89. fi
  90. echo " Verifying packaged file modes in $(basename "$ipk")..."
  91. # A temp file, not a variable: data.tar.gz is gzip and command substitution
  92. # would drop its NUL bytes.
  93. data_file="$(mktemp)"
  94. if ! ipk_data_tar "$ipk" > "$data_file"; then
  95. rm -f "$data_file"
  96. echo "ERROR: verify_ipk_modes: could not read $ipk" >&2
  97. exit 1
  98. fi
  99. for spec in "$@"; do
  100. path="${spec%%=*}"
  101. want="${spec##*=}"
  102. got="$(tar -tvzf "$data_file" 2>/dev/null \
  103. | awk -v p="$path" '$NF ~ ("(^|/)" p "$") {print $1; exit}')"
  104. if [ -z "$got" ]; then
  105. echo "ERROR: $path not found inside the ipk" >&2
  106. exit 1
  107. fi
  108. case "$want" in
  109. 755) line="rwxr-xr-x" ;;
  110. 644) line="rw-r--r--" ;;
  111. *) echo "ERROR: verify_ipk_modes: unsupported mode $want" >&2; exit 1 ;;
  112. esac
  113. if [ "$got" != "-$line" ]; then
  114. echo "ERROR: $path has mode $got, want -$line ($want)" >&2
  115. exit 1
  116. fi
  117. echo " $path: $got OK"
  118. done
  119. rm -f "$data_file"
  120. }