openwrtbuilder 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. #!/usr/bin/env bash
  2. # Builds and deploys OpenWRT images
  3. # Copyright 2022-24 Bryan C. Roessler
  4. # Apache 2.0 License
  5. # See README.md and ./profiles
  6. # Set default release
  7. : "${RELEASE:="23.05.5"}"
  8. print_help() {
  9. debug "${FUNCNAME[0]}"
  10. cat <<-'EOF'
  11. Build and deploy OpenWRT images
  12. USAGE:
  13. openwrtbuilder [OPTION [VALUE]]... -p PROFILE [-p PROFILE]...
  14. OPTIONS
  15. --profile,-p PROFILE
  16. --release,-r,--version,-v RELEASE ("snapshot", "22.03.5")
  17. --buildroot,-b PATH
  18. Default: location of openwrtbuilder script
  19. --source
  20. Build image from source, not from Image Builder
  21. --ssh-upgrade HOST
  22. Examples: root@192.168.1.1, root@router.lan
  23. --ssh-backup SSH_PATH
  24. Enabled by default for --ssh-upgrade
  25. --flash,-f DEVICE
  26. Example: /dev/sdX
  27. --reset
  28. Cleanup all source and output files
  29. --yes,-y
  30. Assume yes for all questions (automatic mode)
  31. --debug,-d
  32. --help,-h
  33. EXAMPLES
  34. ./openwrtbuilder -p r4s -r snapshot
  35. ./openwrtbuilder -p ax6000 -r 23.05.0-rc3 --source --debug
  36. ./openwrtbuilder -p rpi4 -r 22.03.3 --flash /dev/sdX
  37. ./openwrtbuilder -p linksys -r snapshot --ssh-upgrade root@192.168.1.1
  38. EOF
  39. }
  40. init() {
  41. debug "${FUNCNAME[0]}"
  42. declare -g ID RPM_MGR SCRIPTDIR DL_TOOL
  43. ((DEBUG)) || echo "To enable debugging output, use --debug or -d"
  44. # Save the script directory
  45. # https://stackoverflow.com/a/4774063
  46. SCRIPTDIR="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit $? ; pwd -P)"
  47. if [[ -e "/etc/os-release" ]]; then
  48. source "/etc/os-release"
  49. else
  50. echo "/etc/os-release not found"
  51. echo "Your OS is unsupported"
  52. print_help
  53. exit 1
  54. fi
  55. debug "Detected host platform: $ID"
  56. # normalize distro ID
  57. case "$ID" in
  58. debian|arch) ;;
  59. centos|fedora)
  60. if command -v dnf &>/dev/null; then
  61. RPM_MGR="dnf"
  62. elif command -v yum &>/dev/null; then
  63. RPM_MGR="yum"
  64. fi
  65. ;;
  66. rhel) ID="centos" ;;
  67. linuxmint|neon|*ubuntu*) ID="ubuntu" ;;
  68. *suse*) ID="suse" ;;
  69. raspbian) ID="debian" ;;
  70. *)
  71. echo "Autodetecting distro, this may be unreliable"
  72. if command -v dnf &>/dev/null; then
  73. ID="fedora"
  74. RPM_MGR="dnf"
  75. elif command -v yum &>/dev/null; then
  76. ID="centos"
  77. RPM_MGR="yum"
  78. elif command -v apt &>/dev/null; then
  79. ID="ubuntu"
  80. elif command -v pacman &>/dev/null; then
  81. ID="arch"
  82. else
  83. return 1
  84. fi
  85. ;;
  86. esac
  87. debug "Using host platform: $ID"
  88. # Set distro-specific functions
  89. case "$ID" in
  90. fedora|centos) pkg_install(){ sudo "$RPM_MGR" install --skip-unavailable -y "$@"; } ;;
  91. debian|ubuntu) pkg_install(){ sudo apt-get install --ignore-missing -y -q0 "$@"; } ;;
  92. suse) pkg_install(){ sudo zypper --non-interactive -q install --force --no-confirm "$@"; } ;;
  93. arch) pkg_install(){ sudo pacman -S --noconfirm --needed "$@"; } ;;
  94. esac
  95. if command -v axel &>/dev/null; then
  96. DL_TOOL="axel"
  97. elif command -v curl &>/dev/null; then
  98. DL_TOOL="curl"
  99. else
  100. echo "Downloading the Image Builder requires axel or curl"
  101. return 1
  102. fi
  103. }
  104. parse_input() {
  105. debug "${FUNCNAME[0]}" "$*"
  106. declare -ga PROFILES
  107. local long_opts='release:,version:,profile:,buildroot:,source,'
  108. long_opts+='ssh-upgrade:,ssh-backup:,flash:,reset,yes,debug,help'
  109. if _input=$(getopt -o +r:v:p:b:sf:ydh -l $long_opts -- "$@"); then
  110. eval set -- "$_input"
  111. while true; do
  112. case "$1" in
  113. --release|-r|--version|-v) shift; declare -g USER_RELEASE="$1" ;;
  114. --profile|-p) shift; PROFILES+=("$1") ;;
  115. --buildroot|-b) shift; BUILDROOT="$1" ;;
  116. --source|-s) FROM_SOURCE=1 ;;
  117. --ssh-upgrade) shift; SSH_UPGRADE_PATH="$1" ;;
  118. --ssh-backup) shift; SSH_BACKUP_PATH="$1" ;;
  119. --flash|-f) shift; FLASH_DEV="$1" ;;
  120. --reset) RESET=1 ;;
  121. --yes|-y) YES=1 ;;
  122. --debug|-d) echo "Debugging on"; DEBUG=1 ;;
  123. --help|-h) print_help; exit 0 ;;
  124. --)
  125. shift
  126. break
  127. ;;
  128. esac
  129. shift
  130. done
  131. else
  132. echo "Incorrect options provided"
  133. print_help; exit 1
  134. fi
  135. }
  136. install_dependencies() {
  137. debug "${FUNCNAME[0]}"
  138. local -a pkg_list
  139. local lock_file
  140. if ((FROM_SOURCE)); then
  141. lock_file="$BUILDROOT/.dependencies_sc"
  142. else
  143. lock_file="$BUILDROOT/.dependencies_ib"
  144. fi
  145. if [[ ! -f $lock_file ]]; then
  146. if ((FROM_SOURCE)); then
  147. # For building from source code
  148. # https://openwrt.org/docs/guide-developer/toolchain/install-buildsystem
  149. case "$ID" in
  150. fedora|centos)
  151. pkg_list+=(
  152. "bash-completion"
  153. "bzip2"
  154. "gcc"
  155. "gcc-c++"
  156. "git"
  157. "make"
  158. "ncurses-devel"
  159. "patch"
  160. "rsync"
  161. "tar"
  162. "unzip"
  163. "wget"
  164. "which"
  165. "diffutils"
  166. "python3"
  167. "python3-devel"
  168. "python3-setuptools"
  169. "python3-pyelftools"
  170. "perl-base"
  171. "perl-Data-Dumper"
  172. "perl-File-Compare"
  173. "perl-File-Copy"
  174. "perl-FindBin"
  175. "perl-IPC-Cmd"
  176. "perl-Thread-Queue"
  177. "perl-Time-Piece"
  178. "perl-JSON-PP"
  179. "swig"
  180. "clang" # for qosify
  181. "llvm15-libs"
  182. "patch")
  183. ;;
  184. debian|ubuntu)
  185. pkg_list+=(
  186. "build-essential"
  187. "clang"
  188. "flex"
  189. "g++"
  190. "gawk"
  191. "gcc-multilib"
  192. "gettext"
  193. "git"
  194. "libncurses5-dev"
  195. "libssl-dev"
  196. "python3-distutils"
  197. "rsync"
  198. "unzip"
  199. "zlib1g-dev"
  200. "file"
  201. "wget"
  202. "patch")
  203. ;;
  204. arch)
  205. pkg_list+=(
  206. "base-devel"
  207. "autoconf"
  208. "automake"
  209. "bash"
  210. "binutils"
  211. "bison"
  212. "bzip2"
  213. "clang"
  214. "fakeroot"
  215. "file"
  216. "findutils"
  217. "flex"
  218. "gawk"
  219. "gcc"
  220. "gettext"
  221. "git"
  222. "grep"
  223. "groff"
  224. "gzip"
  225. "libelf"
  226. "libtool"
  227. "libxslt"
  228. "m4"
  229. "make"
  230. "ncurses"
  231. "openssl"
  232. "patch"
  233. "pkgconf"
  234. "python"
  235. "rsync"
  236. "sed"
  237. "texinfo"
  238. "time"
  239. "unzip"
  240. "util-linux"
  241. "wget"
  242. "which"
  243. "zlib"
  244. "patch")
  245. ;;
  246. *)
  247. debug "Skipping dependency install, your OS is unsupported"
  248. return 1
  249. ;;
  250. esac
  251. else
  252. # For Imagebuilder
  253. case "$ID" in
  254. fedora|centos)
  255. pkg_list+=(
  256. "@c-development"
  257. "@development-tools"
  258. "@development-libs"
  259. "perl-FindBin"
  260. "zlib-static"
  261. "elfutils-libelf-devel"
  262. "gawk"
  263. "unzip"
  264. "file"
  265. "wget"
  266. "python3"
  267. "python2"
  268. "axel"
  269. "perl-IPC-Cmd")
  270. ;;
  271. debian|ubuntu)
  272. pkg_list+=(
  273. "build-essential"
  274. "libncurses5-dev"
  275. "libncursesw5-dev"
  276. "zlib1g-dev"
  277. "gawk"
  278. "git"
  279. "gettext"
  280. "libssl-dev"
  281. "xsltproc"
  282. "wget"
  283. "unzip"
  284. "python"
  285. "axel")
  286. ;;
  287. *)
  288. debug "Skipping dependency install, your OS is unsupported"
  289. return 1
  290. ;;
  291. esac
  292. fi
  293. fi
  294. pkg_install "${pkg_list[@]}" && echo "${pkg_list[@]}" > "$lock_file"
  295. }
  296. get_imagebuilder() {
  297. debug "${FUNCNAME[0]}" "$*"
  298. local -a url_file_pairs=("$@")
  299. for ((i=0; i<${#url_file_pairs[@]}; i+=2)); do
  300. local url="${url_file_pairs[i]}"
  301. local file="${url_file_pairs[i+1]}"
  302. # Check if file exists and ask user to remove and redownload
  303. if [[ -f $file ]] && ! ask_ok "Use existing $file?"; then
  304. execute rm -f "$file"
  305. fi
  306. # Download the file if it doesn't exist
  307. if [[ ! -f "$file" ]]; then
  308. echo "Downloading $url to $file using $DL_TOOL"
  309. execute "$DL_TOOL" "-o" "$file" "$url"
  310. fi
  311. done
  312. }
  313. add_repos() {
  314. debug "${FUNCNAME[0]}"
  315. if [[ -v P_ARR[repo] ]]; then
  316. if ! grep -q "${P_ARR[repo]}" "$BUILDDIR/repositories.conf"; then
  317. echo "${P_ARR[repo]}" >> "$BUILDDIR/repositories.conf"
  318. fi
  319. sed -i '/option check_signature/d' "$BUILDDIR/repositories.conf"
  320. fi
  321. }
  322. ssh_backup() {
  323. debug "${FUNCNAME[0]}"
  324. local date hostname backup_fname
  325. printf -v date '%(%Y-%m-%d-%H-%M-%S)T'
  326. hostname=$(ssh -qt "$SSH_BACKUP_PATH" echo -n \$HOSTNAME)
  327. backup_fname="backup-$hostname-$date.tar.gz"
  328. [[ -d "$FILESDIR" ]] || execute mkdir -p "$FILESDIR"
  329. # Make backup archive on remote
  330. if ! execute "ssh -t $SSH_BACKUP_PATH sysupgrade -b /tmp/$backup_fname"; then
  331. echo "SSH backup failed"
  332. exit 1
  333. fi
  334. # Move backup archive locally
  335. if ! execute "rsync -avz --remove-source-files $SSH_BACKUP_PATH:/tmp/$backup_fname $BUILDDIR/"; then
  336. echo "Could not copy SSH backup"
  337. exit 1
  338. fi
  339. # Extract backup archive
  340. if ! execute "tar -C $FILESDIR -xzf $BUILDDIR/$backup_fname"; then
  341. echo "Could not extract SSH backup"
  342. exit 1
  343. fi
  344. execute "rm $BUILDDIR/$backup_fname"
  345. }
  346. make_images() {
  347. debug "${FUNCNAME[0]}"
  348. # Reuse the existing output
  349. # if [[ -d "$BINDIR" ]]; then
  350. # if ask_ok "$BINDIR exists. Rebuild?"; then
  351. # execute rm -rf "$BINDIR"
  352. # else
  353. # return 0
  354. # fi
  355. # fi
  356. debug make image BIN_DIR="$BINDIR" \
  357. PROFILE="$DEVICE" PACKAGES="$PACKAGES" \
  358. FILES="$FILESDIR" --directory="$BUILDDIR" \
  359. --jobs="$(($(nproc) - 1))"
  360. make image \
  361. BIN_DIR="$BINDIR" \
  362. PROFILE="$DEVICE" \
  363. PACKAGES="$PACKAGES" \
  364. FILES="$FILESDIR" \
  365. --directory="$BUILDDIR" \
  366. --jobs="$(($(nproc) - 1))" \
  367. > "$BUILDDIR/make.log"
  368. }
  369. flash_images() {
  370. debug "${FUNCNAME[0]}"
  371. local img_gz="$1"
  372. local dev="$2"
  373. local img="${img_gz%.gz}"
  374. local partitions
  375. if [[ ! -e "$dev" ]]; then
  376. echo "The device specified by --flash could not be found"
  377. return 1
  378. fi
  379. [[ -f $img_gz ]] || { echo "$img_gz does not exist"; return 1; }
  380. execute gunzip -qfk "$img_gz"
  381. echo "Unmounting target device $dev partitions"
  382. partitions=("$dev"?*)
  383. execute sudo umount "${partitions[@]}"
  384. if execute sudo dd if="$img" of="$dev" bs=2M conv=fsync; then
  385. sync
  386. echo "Image flashed successfully!"
  387. else
  388. echo "dd failed!"
  389. return 1
  390. fi
  391. }
  392. ssh_upgrade() {
  393. debug "${FUNCNAME[0]}"
  394. local img_gz="$1"
  395. local ssh_path="$2"
  396. local img_fname="${img_gz##*/}"
  397. [[ -f $img_gz ]] || { echo "$img_gz is missing, check build output"; return 1; }
  398. echo "Copying '$img_gz' to $ssh_path/tmp/$img_fname"
  399. if ! execute scp "$img_gz" "$ssh_path:/tmp/$img_fname"; then
  400. echo "Could not copy $img_gz to $ssh_path:/tmp/$img_fname"
  401. return 1
  402. fi
  403. echo "Executing remote sysupgrade"
  404. # This may result in weird exit code from closing the ssh connection
  405. # shellcheck disable=SC2029
  406. ssh "$ssh_path" "sysupgrade -F /tmp/$img_fname"
  407. return 0
  408. }
  409. from_source() {
  410. debug "${FUNCNAME[0]}" "$*"
  411. local seed_url="$1"
  412. local src_url="https://github.com/openwrt/openwrt.git"
  413. local seed_file="$GITWORKTREEDIR/.config"
  414. local pkg config commit seed_file wt_commit description
  415. local -a make_opts config_opts
  416. echo "Building from source is under development"
  417. # Update source code
  418. if [[ ! -d "$GITSRCDIR" ]]; then
  419. execute mkdir -p "$GITSRCDIR"
  420. execute git clone "$src_url" "$GITSRCDIR"
  421. fi
  422. git -C "$GITSRCDIR" pull
  423. # Generate commitish for git worktree
  424. case "$RELEASE" in
  425. snapshot) wt_commit="origin/main" ;;
  426. [0-9][0-9].[0-9][0-9].*)
  427. local branch="openwrt-${RELEASE%.*}"
  428. local tag="v$RELEASE"
  429. if ask_ok "Use $branch branch HEAD (y, recommended) or $tag tag (N)?"; then
  430. wt_commit="origin/$branch"
  431. else
  432. wt_commit="$tag"
  433. fi
  434. ;;
  435. *)
  436. debug "Passing '$RELEASE' commit-ish to git worktree"
  437. wt_commit="$RELEASE"
  438. ;;
  439. esac
  440. # TODO There's a bug in the make clean functions that seem to invoke a full make
  441. if [[ -d "$GITWORKTREEDIR" ]]; then
  442. execute git -C "$GITWORKTREEDIR" checkout "$wt_commit"
  443. execute git -C "$GITWORKTREEDIR" pull
  444. else
  445. execute git -C "$GITSRCDIR" worktree add --force --detach "$GITWORKTREEDIR" "$wt_commit"
  446. fi
  447. # To workaround bug, don't use make *clean, blow it away and start fresh
  448. # [[ -d "$GITWORKTREEDIR" ]] && execute rm -rf "$GITWORKTREEDIR"
  449. # execute git -C "$GITSRCDIR" worktree add --force --detach "$GITWORKTREEDIR" "$wt_commit"
  450. # Print commit information
  451. commit=$(git -C "$GITWORKTREEDIR" rev-parse HEAD)
  452. description=$(git -C "$GITWORKTREEDIR" describe)
  453. echo "Current commit hash: $commit"
  454. echo "Git worktree description: $description"
  455. ((DEBUG)) && git --no-pager -C "$GITWORKTREEDIR" log -1
  456. # Enter worktree
  457. execute pushd "$GITWORKTREEDIR" || return 1
  458. # Update package feed
  459. ./scripts/feeds update -i -f &&
  460. ./scripts/feeds update -a -f &&
  461. ./scripts/feeds install -a -f
  462. # Grab the release seed config
  463. if ! execute "$DL_TOOL" "-o" "$seed_file" "$seed_url"; then
  464. echo "Could not obtain $seed_file from $seed_url"
  465. return 1
  466. fi
  467. # Set compilation output dir
  468. config_opts+=("CONFIG_BINARY_FOLDER=\"$BINDIR\"")
  469. # Add custom packages
  470. for pkg in $PACKAGES; do
  471. if [[ $pkg == -* ]]; then
  472. config_opts+=("CONFIG_PACKAGE_${pkg#-}=n") # remove package
  473. else
  474. config_opts+=("CONFIG_PACKAGE_$pkg=y") # add package
  475. fi
  476. done
  477. # Add config options from profile
  478. for config in ${P_ARR[config]}; do
  479. config_opts+=("$config")
  480. done
  481. # Only compile selected fs
  482. execute sed -i '/CONFIG_TARGET_ROOTFS_/d' "$seed_file"
  483. config_opts+=("CONFIG_TARGET_PER_DEVICE_ROOTFS=n")
  484. if [[ $FILESYSTEM == "squashfs" ]]; then
  485. config_opts+=("CONFIG_TARGET_ROOTFS_EXT4FS=n")
  486. config_opts+=("CONFIG_TARGET_ROOTFS_SQUASHFS=y")
  487. elif [[ $FILESYSTEM == "ext4" ]]; then
  488. config_opts+=("CONFIG_TARGET_ROOTFS_SQUASHFS=n")
  489. config_opts+=("CONFIG_TARGET_ROOTFS_EXT4FS=y")
  490. fi
  491. # Only compile selected target image
  492. execute sed -i '/CONFIG_TARGET_DEVICE_/d' "$seed_file"
  493. config_opts+=("CONFIG_TARGET_MULTI_PROFILE=n")
  494. config_opts+=("CONFIG_TARGET_PROFILE=DEVICE_$DEVICE")
  495. config_opts+=("CONFIG_TARGET_${TARGET//\//_}_DEVICE_$DEVICE=y")
  496. config_opts+=("CONFIG_SDK=n")
  497. config_opts+=("CONFIG_SDK_LLVM_BPF=n")
  498. config_opts+=("CONFIG_IB=n")
  499. config_opts+=("CONFIG_MAKE_TOOLCHAIN=n")
  500. # Write options to config seed file
  501. for config in "${config_opts[@]}"; do
  502. debug "Writing $config to $seed_file"
  503. echo "$config" >> "$seed_file"
  504. done
  505. # Cleaning modes
  506. # make clean # compiled output
  507. # make targetclean # compiled output, toolchain
  508. # make dirclean # compiled output, toolchain, build tools
  509. # make distclean # compiled output, toolchain, build tools, .config, feeds, .ccache
  510. # Make prep
  511. ((DEBUG)) && make_opts+=("V=s")
  512. execute make "${make_opts[@]}" "-j1" distclean # TODO 'dirclean' has a bug that triggers menuconfig
  513. execute make "${make_opts[@]}" "-j1" defconfig
  514. execute make "${make_opts[@]}" "-j1" download
  515. ((DEBUG)) && make_opts+=("-j1") || make_opts+=("-j$(($(nproc)+1))")
  516. # Make image
  517. if ! execute ionice -c 3 chrt --idle 0 nice -n19 make "${make_opts[@]}" world; then
  518. echo "Error: make failed"
  519. return 1
  520. fi
  521. popd || return 1
  522. # Symlink output images to root of BINDIR (match Image Builder)
  523. shopt -s nullglob
  524. for image in "$BINDIR/targets/${TARGET}/"*.{img,img.gz,ubi}; do
  525. execute ln -fs "$image" "$BINDIR/${image##*/}"
  526. done
  527. shopt -u nullglob
  528. return 0
  529. }
  530. # Generic helpers
  531. debug() { ((DEBUG)) && echo "Debug: $*"; }
  532. ask_ok() {
  533. ((YES)) && return
  534. local r
  535. read -r -p "$* [y/N]: " r
  536. r=${r,,}
  537. [[ "$r" =~ ^(yes|y)$ ]]
  538. }
  539. extract() {
  540. debug "${FUNCNAME[0]}" "$*"
  541. local archive="$1"
  542. local out_dir="$2"
  543. if ! execute tar -axf "$archive" -C "$out_dir" --strip-components 1; then
  544. echo "Extraction failed"
  545. return 1
  546. fi
  547. }
  548. verify() {
  549. debug "${FUNCNAME[0]}" "$*"
  550. local file_to_check="$1"
  551. local sumfile="$2"
  552. local checksum
  553. command -v sha256sum &>/dev/null || return 1
  554. [[ -f $sumfile && -f $file_to_check ]] || return 1
  555. checksum=$(grep "${file_to_check##*/}" "$sumfile" | cut -f1 -d' ')
  556. echo -n "$checksum $file_to_check" | sha256sum --check --status
  557. }
  558. load() {
  559. debug "${FUNCNAME[0]}" "$*"
  560. local source_file="$1"
  561. # shellcheck disable=SC1090
  562. [[ -f $source_file ]] && source "$source_file"
  563. }
  564. execute() {
  565. if debug "$*"; then
  566. "$@"
  567. else
  568. "$@" &>/dev/null
  569. fi
  570. }
  571. main() {
  572. debug "${FUNCNAME[0]}"
  573. init
  574. load "$SCRIPTDIR/profiles"
  575. parse_input "$@"
  576. # Fallback to SCRIPTDIR if BUILDROOT has not been set
  577. declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
  578. declare -g FILESDIR="${FILESDIR:=$BUILDROOT/src/files}"
  579. # This could be dangerous
  580. if [[ $BUILDROOT == "/" ]]; then
  581. echo "Invalid --buildroot"
  582. exit 1
  583. fi
  584. for dir in "$BUILDROOT/src" "$BUILDROOT/bin"; do
  585. [[ -d "$dir" ]] || execute mkdir -p "$dir"
  586. done
  587. # Allow --reset without a profile
  588. if ((RESET)) && [[ ${#PROFILES} -lt 1 ]]; then
  589. for d in "$BUILDROOT/src" "$BUILDROOT/bin"; do
  590. ask_ok "Remove $d?" && execute rm -rf "$d"
  591. done
  592. exit $?
  593. fi
  594. install_dependencies
  595. for profile in "${PROFILES[@]}"; do
  596. debug "Running profile: $profile"
  597. if [[ ! ${!profile@a} = A ]]; then
  598. echo "Profile '$profile' does not exist"
  599. return 1
  600. fi
  601. # Store profile in P_ARR nameref
  602. declare -gn P_ARR="$profile"
  603. declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
  604. declare -g TARGET="${P_ARR[target]}"
  605. declare -g DEVICE="${P_ARR[device]}"
  606. declare -g PACKAGES="${P_ARR[packages]:-}"
  607. declare -g RELEASE="${USER_RELEASE:=${P_ARR[release]:=$RELEASE}}"
  608. # normalize RELEASE
  609. case "$RELEASE" in
  610. snapshot|latest|main|master) RELEASE="snapshot" ;;
  611. v[0-9][0-9].[0-9][0-9].*) RELEASE="${RELEASE#v}" ;;
  612. [0-9][0-9].[0-9][0-9].*) ;;
  613. *)
  614. if ! ((FROM_SOURCE)); then
  615. echo "Error: Invalid release version format"
  616. echo "Use semantic version, tag, or 'snapshot'"
  617. exit 1
  618. fi
  619. ;;
  620. esac
  621. declare -g GITSRCDIR="$BUILDROOT/src/openwrt"
  622. declare -g GITWORKTREEDIR="$BUILDROOT/src/$profile/$RELEASE-src"
  623. declare -g BUILDDIR="$BUILDROOT/src/$profile/$RELEASE"
  624. declare -g BINDIR="$BUILDROOT/bin/$profile/$RELEASE"
  625. if [[ "$RELEASE" == "snapshot" ]]; then
  626. local url_prefix="https://downloads.openwrt.org/snapshots/targets/$TARGET"
  627. local url_filename="openwrt-imagebuilder-${TARGET//\//-}.Linux-x86_64.tar.zst"
  628. local img_fname="openwrt-${TARGET//\//-}-$DEVICE-$FILESYSTEM"
  629. else
  630. local url_prefix="https://downloads.openwrt.org/releases/$RELEASE/targets/$TARGET"
  631. local url_filename="openwrt-imagebuilder-$RELEASE-${TARGET//\//-}.Linux-x86_64.tar.xz"
  632. local img_fname="openwrt-$RELEASE-${TARGET//\//-}-$DEVICE-$FILESYSTEM"
  633. fi
  634. local ib_url="$url_prefix/$url_filename"
  635. local ib_file="$BUILDDIR/$url_filename"
  636. local ib_sha256_url="$url_prefix/sha256sums"
  637. local ib_sha256_file="$BUILDDIR/sha256sums"
  638. local seed_url="$url_prefix/config.buildinfo"
  639. if ((FROM_SOURCE)); then
  640. declare -g SYSUPGRADEIMGGZ="$BINDIR/targets/$img_fname-sysupgrade.img.gz"
  641. else
  642. declare -g SYSUPGRADEIMGGZ="$BUILDDIR/$img_fname-sysupgrade.img.gz"
  643. fi
  644. if ((RESET)); then
  645. if ((FROM_SOURCE)); then
  646. [[ -d $GITWORKTREEDIR ]] && ask_ok "Remove $GITWORKTREEDIR?"
  647. execute git worktree remove --force "$GITWORKTREEDIR"
  648. execute rm -rf "$GITWORKTREEDIR"
  649. elif [[ -d $BUILDDIR ]] && ask_ok "Remove $BUILDDIR?"; then
  650. execute rm -rf "$BUILDDIR"
  651. fi
  652. fi
  653. if ((DEBUG)); then
  654. echo "Profile settings:"
  655. for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
  656. echo "Environment variables:"
  657. declare -p
  658. fi
  659. if ((FROM_SOURCE)); then
  660. from_source "$seed_url" || return $?
  661. else
  662. [[ -d $BUILDDIR ]] || mkdir -p "$BUILDDIR"
  663. get_imagebuilder "$ib_url" "$ib_file" "$ib_sha256_url" "$ib_sha256_file" &&
  664. verify "$ib_file" "$ib_sha256_file" &&
  665. extract "$ib_file" "$BUILDDIR" || return $?
  666. add_repos
  667. make_images
  668. # Verify output image for stock builds (in testing)
  669. if [[ ! -v P_ARR[packages] || -z ${P_ARR[packages]} ]]; then
  670. shopt -s nullglob
  671. local -a outfiles=("$BINDIR"/*.img.gz "$BINDIR"/*.img)
  672. shopt -u nullglob
  673. for outfile in "${outfiles[@]}"; do
  674. verify "$outfile" "$ib_sha256_file" || return 1
  675. done
  676. fi
  677. #copyFiles
  678. fi
  679. [[ -v SSH_BACKUP_PATH ]] && ssh_backup
  680. [[ -v SSH_UPGRADE_PATH ]] && ssh_upgrade "$SYSUPGRADEIMGGZ" "$SSH_UPGRADE_PATH"
  681. [[ -v FLASH_DEV ]] && flash_images "$SYSUPGRADEIMGGZ" "$FLASH_DEV"
  682. done
  683. }
  684. main "$@"
  685. exit