openwrtbuilder 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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 hash dnf &>/dev/null; then
  73. ID="fedora"
  74. RPM_MGR="dnf"
  75. elif hash yum &>/dev/null; then
  76. ID="centos"
  77. RPM_MGR="yum"
  78. elif hash apt &>/dev/null; then
  79. ID="ubuntu"
  80. elif hash 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 -y "$@"; } ;;
  91. debian|ubuntu) pkg_install(){ sudo apt-get install -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. "python2"
  167. "python3"
  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. if [[ ! -f $img_gz ]]; then
  380. echo "$img_gz does not exist"
  381. echo "Check your build output"
  382. return 1
  383. fi
  384. execute gunzip -qfk "$img_gz"
  385. echo "Unmounting target device $dev partitions"
  386. partitions=("$dev"?*)
  387. execute sudo umount "${partitions[@]}"
  388. if execute sudo dd if="$img" of="$dev" bs=2M conv=fsync; then
  389. sync
  390. echo "Image flashed sucessfully!"
  391. else
  392. echo "dd failed!"
  393. return 1
  394. fi
  395. }
  396. ssh_upgrade() {
  397. debug "${FUNCNAME[0]}"
  398. local img_gz="$1"
  399. local ssh_path="$2"
  400. local img_fname="${img_gz##*/}"
  401. if ! [[ -f $img_gz ]]; then
  402. echo "$img_gz is missing, check build output"
  403. return 1
  404. fi
  405. echo "Copying '$img_gz' to $ssh_path/tmp/$img_fname"
  406. debug "scp $img_gz $ssh_path:/tmp/$img_fname"
  407. if ! scp "$img_gz" "$ssh_path:/tmp/$img_fname"; then
  408. echo "Could not copy $img_gz to $ssh_path:/tmp/$img_fname"
  409. return 1
  410. fi
  411. echo "Executing remote sysupgrade"
  412. debug "ssh $ssh_path sysupgrade -F /tmp/$img_fname"
  413. # shellcheck disable=SC2029
  414. # execute remotely
  415. # this will probably be a weird exit code from closed connection
  416. ssh "$ssh_path" "sysupgrade -F /tmp/$img_fname"
  417. }
  418. from_source() {
  419. debug "${FUNCNAME[0]}" "$*"
  420. local seed_url="$1"
  421. local src_url="https://github.com/openwrt/openwrt.git"
  422. local seed_file="$GITWORKTREEDIR/.config"
  423. local pkg config commit seed_file wt_commit description
  424. local -a make_opts config_opts
  425. echo "Building from source is under development"
  426. # Update source code
  427. if [[ ! -d "$GITSRCDIR" ]]; then
  428. execute mkdir -p "$GITSRCDIR"
  429. execute git clone "$src_url" "$GITSRCDIR"
  430. fi
  431. git -C "$GITSRCDIR" pull
  432. # Generate commitish for git worktree
  433. case "$RELEASE" in
  434. snapshot)
  435. wt_commit="origin/main"
  436. ;;
  437. [0-9][0-9].[0-9][0-9].*)
  438. local branch="openwrt-${RELEASE%.*}"
  439. local tag="v$RELEASE"
  440. if ask_ok "Use $branch branch HEAD (y, recommended) or $tag tag (N)?"; then
  441. wt_commit="origin/$branch"
  442. else
  443. wt_commit="$tag"
  444. fi
  445. ;;
  446. *)
  447. debug "Passing '$RELEASE' commit-ish to git worktree"
  448. wt_commit="$RELEASE"
  449. ;;
  450. esac
  451. # TODO There's a bug in the make clean functions that seem to invoke a full make
  452. # if [[ -d "$GITWORKTREEDIR" ]]; then
  453. # execute git -C "$GITWORKTREEDIR" checkout "$wt_commit"
  454. # execute git -C "$GITWORKTREEDIR" pull
  455. # else
  456. # execute git -C "$GITSRCDIR" worktree add --force --detach "$GITWORKTREEDIR" "$wt_commit"
  457. # fi
  458. # To workaround bug, don't use make *clean, blow it away and start fresh
  459. [[ -d "$GITWORKTREEDIR" ]] && execute rm -rf "$GITWORKTREEDIR"
  460. execute git -C "$GITSRCDIR" worktree add --force --detach "$GITWORKTREEDIR" "$wt_commit"
  461. # Print commit information
  462. commit=$(git -C "$GITWORKTREEDIR" rev-parse HEAD)
  463. description=$(git -C "$GITWORKTREEDIR" describe)
  464. echo "Current commit hash: $commit"
  465. echo "Git worktree description: $description"
  466. if ((DEBUG)); then
  467. if ((YES)); then
  468. git --no-pager -C "$GITWORKTREEDIR" log -1
  469. else
  470. git -C "$GITWORKTREEDIR" log -1
  471. fi
  472. fi
  473. # Enter worktree
  474. execute pushd "$GITWORKTREEDIR" || return 1
  475. # Update package feed
  476. ./scripts/feeds update -i -f &&
  477. ./scripts/feeds update -a -f &&
  478. ./scripts/feeds install -a -f
  479. # Grab the release seed config
  480. if ! execute "$DL_TOOL" "-o" "$seed_file" "$seed_url"; then
  481. echo "Could not obtain $seed_file from $seed_url"
  482. return 1
  483. fi
  484. # Set compilation output dir
  485. config_opts+=("CONFIG_BINARY_FOLDER=\"$BINDIR\"")
  486. # Add custom packages
  487. for pkg in $PACKAGES; do
  488. if [[ $pkg == -* ]]; then
  489. config_opts+=("CONFIG_PACKAGE_${pkg#-}=n") # remove package
  490. else
  491. config_opts+=("CONFIG_PACKAGE_$pkg=y") # add package
  492. fi
  493. done
  494. # Add config options from profile
  495. for config in ${P_ARR[config]}; do
  496. config_opts+=("$config")
  497. done
  498. # Only compile selected fs
  499. execute sed -i '/CONFIG_TARGET_ROOTFS_/d' "$seed_file"
  500. config_opts+=("CONFIG_TARGET_PER_DEVICE_ROOTFS=n")
  501. if [[ $FILESYSTEM == "squashfs" ]]; then
  502. config_opts+=("CONFIG_TARGET_ROOTFS_EXT4FS=n")
  503. config_opts+=("CONFIG_TARGET_ROOTFS_SQUASHFS=y")
  504. elif [[ $FILESYSTEM == "ext4" ]]; then
  505. config_opts+=("CONFIG_TARGET_ROOTFS_SQUASHFS=n")
  506. config_opts+=("CONFIG_TARGET_ROOTFS_EXT4FS=y")
  507. fi
  508. # Only compile selected target image
  509. execute sed -i '/CONFIG_TARGET_DEVICE_/d' "$seed_file"
  510. config_opts+=("CONFIG_TARGET_MULTI_PROFILE=n")
  511. config_opts+=("CONFIG_TARGET_PROFILE=DEVICE_$DEVICE")
  512. config_opts+=("CONFIG_TARGET_${TARGET//\//_}_DEVICE_$DEVICE=y")
  513. config_opts+=("CONFIG_SDK=n")
  514. config_opts+=("CONFIG_SDK_LLVM_BPF=n")
  515. config_opts+=("CONFIG_IB=n")
  516. config_opts+=("CONFIG_MAKE_TOOLCHAIN=n")
  517. # Write options to config seed file
  518. for config in "${config_opts[@]}"; do
  519. debug "Writing $config to $seed_file"
  520. echo "$config" >> "$seed_file"
  521. done
  522. # Cleaning modes
  523. # make clean # compiled output
  524. # make targetclean # compiled output, toolchain
  525. # make dirclean # compiled output, toolchain, build tools
  526. # make distclean # compiled output, toolchain, build tools, .config, feeds, .ccache
  527. # Make prep
  528. ((DEBUG)) && make_opts+=("V=s")
  529. #execute make "${make_opts[@]}" "-j1" dirclean # TODO 'dirclean' has a bug that triggers menuconfig
  530. execute make "${make_opts[@]}" "-j1" defconfig
  531. execute make "${make_opts[@]}" "-j1" download
  532. # Make image
  533. if ! execute ionice -c 3 chrt --idle 0 nice -n19 make "${make_opts[@]}" "-j$(($(nproc)+1))" world; then
  534. echo "Error: make failed"
  535. return 1
  536. fi
  537. popd || return 1
  538. # Symlink output images to root of BINDIR (match Image Builder)
  539. shopt -s nullglob
  540. for image in "$BINDIR/targets/${TARGET}/"*.{img,img.gz,ubi}; do
  541. ln -fs "$image" "$BINDIR/${image##*/}"
  542. done
  543. shopt -u nullglob
  544. return 0
  545. }
  546. # Generic helpers
  547. debug() { ((DEBUG)) && echo "Debug: $*"; }
  548. ask_ok() {
  549. ((YES)) && return
  550. local r
  551. read -r -p "$* [y/N]: " r
  552. r=${r,,}
  553. [[ "$r" =~ ^(yes|y)$ ]]
  554. }
  555. extract() {
  556. debug "${FUNCNAME[0]}" "$*"
  557. local archive="$1"
  558. local out_dir="$2"
  559. if ! execute tar -axf "$archive" -C "$out_dir" --strip-components 1; then
  560. echo "Extraction failed"
  561. return 1
  562. fi
  563. }
  564. verify() {
  565. debug "${FUNCNAME[0]}" "$*"
  566. local file_to_check="$1"
  567. local sumfile="$2"
  568. local checksum
  569. hash sha256sum &>/dev/null || return 1
  570. [[ -f $sumfile && -f $file_to_check ]] || return 1
  571. checksum=$(grep "${file_to_check##*/}" "$sumfile" | cut -f1 -d' ')
  572. echo -n "$checksum $file_to_check" | sha256sum --check --status
  573. }
  574. load() {
  575. debug "${FUNCNAME[0]}" "$*"
  576. local source_file="$1"
  577. # shellcheck disable=SC1090
  578. [[ -f $source_file ]] && source "$source_file"
  579. }
  580. execute() {
  581. if debug "$*"; then
  582. "$@"
  583. else
  584. "$@" &>/dev/null
  585. fi
  586. }
  587. main() {
  588. debug "${FUNCNAME[0]}"
  589. init
  590. load "$SCRIPTDIR/profiles"
  591. parse_input "$@"
  592. # Fallback to SCRIPTDIR if BUILDROOT has not been set
  593. declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
  594. declare -g FILESDIR="${FILESDIR:=$BUILDROOT/src/files}"
  595. # This could be dangerous
  596. if [[ $BUILDROOT == "/" ]]; then
  597. echo "Invalid --buildroot"
  598. exit 1
  599. fi
  600. for dir in "$BUILDROOT/src" "$BUILDROOT/bin"; do
  601. [[ -d "$dir" ]] || execute mkdir -p "$dir"
  602. done
  603. # Allow --reset without a profile
  604. if ((RESET)) && [[ ${#PROFILES} -lt 1 ]]; then
  605. for d in "$BUILDROOT/src" "$BUILDROOT/bin"; do
  606. ask_ok "Remove $d?" && execute rm -rf "$d"
  607. done
  608. exit $?
  609. fi
  610. install_dependencies
  611. for profile in "${PROFILES[@]}"; do
  612. debug "Running profile: $profile"
  613. if [[ ! ${!profile@a} = A ]]; then
  614. echo "Profile '$profile' does not exist"
  615. return 1
  616. fi
  617. # Store profile in P_ARR nameref
  618. declare -gn P_ARR="$profile"
  619. declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
  620. declare -g TARGET="${P_ARR[target]}"
  621. declare -g DEVICE="${P_ARR[device]}"
  622. declare -g PACKAGES="${P_ARR[packages]:-}"
  623. declare -g RELEASE="${USER_RELEASE:=${P_ARR[release]:=$RELEASE}}"
  624. # normalize RELEASE
  625. case "$RELEASE" in
  626. snapshot|latest|main|master) RELEASE="snapshot" ;;
  627. v[0-9][0-9].[0-9][0-9].*) RELEASE="${RELEASE#v}" ;;
  628. [0-9][0-9].[0-9][0-9].*) ;;
  629. *)
  630. if ! ((FROM_SOURCE)); then
  631. echo "Error: Invalid release version format"
  632. echo "Use semantic version, tag, or 'snapshot'"
  633. exit 1
  634. fi
  635. ;;
  636. esac
  637. declare -g GITSRCDIR="$BUILDROOT/src/openwrt"
  638. declare -g GITWORKTREEDIR="$BUILDROOT/src/$profile/$RELEASE-src"
  639. declare -g BUILDDIR="$BUILDROOT/src/$profile/$RELEASE"
  640. declare -g BINDIR="$BUILDROOT/bin/$profile/$RELEASE"
  641. if [[ "$RELEASE" == "snapshot" ]]; then
  642. local url_prefix="https://downloads.openwrt.org/snapshots/targets/$TARGET"
  643. local url_filename="openwrt-imagebuilder-${TARGET//\//-}.Linux-x86_64.tar.zst"
  644. local img_fname="openwrt-${TARGET//\//-}-$DEVICE-$FILESYSTEM"
  645. else
  646. local url_prefix="https://downloads.openwrt.org/releases/$RELEASE/targets/$TARGET"
  647. local url_filename="openwrt-imagebuilder-$RELEASE-${TARGET//\//-}.Linux-x86_64.tar.xz"
  648. local img_fname="openwrt-$RELEASE-${TARGET//\//-}-$DEVICE-$FILESYSTEM"
  649. fi
  650. local ib_url="$url_prefix/$url_filename"
  651. local ib_file="$BUILDDIR/$url_filename"
  652. local ib_sha256_url="$url_prefix/sha256sums"
  653. local ib_sha256_file="$BUILDDIR/sha256sums"
  654. local seed_url="$url_prefix/config.buildinfo"
  655. if ((FROM_SOURCE)); then
  656. declare -g SYSUPGRADEIMGGZ="$BINDIR/targets/$img_fname-sysupgrade.img.gz"
  657. else
  658. declare -g SYSUPGRADEIMGGZ="$BUILDDIR/$img_fname-sysupgrade.img.gz"
  659. fi
  660. if ((RESET)); then
  661. if ((FROM_SOURCE)); then
  662. [[ -d $GITWORKTREEDIR ]] && ask_ok "Remove $GITWORKTREEDIR?"
  663. execute git worktree remove --force "$GITWORKTREEDIR"
  664. execute rm -rf "$GITWORKTREEDIR"
  665. elif [[ -d $BUILDDIR ]] && ask_ok "Remove $BUILDDIR?"; then
  666. execute rm -rf "$BUILDDIR"
  667. fi
  668. fi
  669. if ((DEBUG)); then
  670. echo "Profile settings:"
  671. for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
  672. echo "Environment variables:"
  673. declare -p
  674. fi
  675. if ((FROM_SOURCE)); then
  676. from_source "$seed_url" || return $?
  677. else
  678. [[ -d $BUILDDIR ]] || mkdir -p "$BUILDDIR"
  679. get_imagebuilder "$ib_url" "$ib_file" "$ib_sha256_url" "$ib_sha256_file" &&
  680. verify "$ib_file" "$ib_sha256_file" &&
  681. extract "$ib_file" "$BUILDDIR" || return $?
  682. add_repos
  683. make_images
  684. # Verify output image for stock builds (in testing)
  685. if [[ ! -v P_ARR[packages] || -z ${P_ARR[packages]} ]]; then
  686. shopt -s nullglob
  687. local -a outfiles=("$BINDIR"/*.img.gz "$BINDIR"/*.img)
  688. shopt -u nullglob
  689. for outfile in "${outfiles[@]}"; do
  690. verify "$outfile" "$ib_sha256_file" || return 1
  691. done
  692. fi
  693. #copyFiles
  694. fi
  695. [[ -v SSH_BACKUP_PATH ]] && ssh_backup
  696. [[ -v SSH_UPGRADE_PATH ]] && ssh_upgrade "$SYSUPGRADEIMGGZ" "$SSH_UPGRADE_PATH"
  697. [[ -v FLASH_DEV ]] && flash_images "$SYSUPGRADEIMGGZ" "$FLASH_DEV"
  698. done
  699. }
  700. main "$@"
  701. exit $?