openwrtbuilder 24 KB

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