openwrtbuilder 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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.0-rc2"}"
  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-rc2 --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. "perl-base"
  210. "perl-Data-Dumper"
  211. "perl-File-Compare"
  212. "perl-File-Copy"
  213. "perl-FindBin"
  214. "perl-Thread-Queue"
  215. "clang" # for qosify
  216. )
  217. ;;
  218. debian|ubuntu)
  219. pkg_list+=(
  220. "build-essential"
  221. "clang"
  222. "flex"
  223. "g++"
  224. "gawk"
  225. "gcc-multilib"
  226. "gettext"
  227. "git"
  228. "libncurses5-dev"
  229. "libssl-dev"
  230. "python3-distutils"
  231. "rsync"
  232. "unzip"
  233. "zlib1g-dev"
  234. "file"
  235. "wget"
  236. )
  237. ;;
  238. arch)
  239. pkg_list+=(
  240. "base-devel"
  241. "autoconf"
  242. "automake"
  243. "bash"
  244. "binutils"
  245. "bison"
  246. "bzip2"
  247. "clang"
  248. "fakeroot"
  249. "file"
  250. "findutils"
  251. "flex"
  252. "gawk"
  253. "gcc"
  254. "gettext"
  255. "git"
  256. "grep"
  257. "groff"
  258. "gzip"
  259. "libelf"
  260. "libtool"
  261. "libxslt"
  262. "m4"
  263. "make"
  264. "ncurses"
  265. "openssl"
  266. "patch"
  267. "pkgconf"
  268. "python"
  269. "rsync"
  270. "sed"
  271. "texinfo"
  272. "time"
  273. "unzip"
  274. "util-linux"
  275. "wget"
  276. "which"
  277. "zlib"
  278. )
  279. ;;
  280. *)
  281. debug "Skipping dependency install, your OS is unsupported"
  282. return 1
  283. ;;
  284. esac
  285. else
  286. # For Imagebuilder
  287. case "$ID" in
  288. fedora|centos)
  289. pkg_list+=(
  290. "@c-development"
  291. "@development-tools"
  292. "@development-libs"
  293. "perl-FindBin"
  294. "zlib-static"
  295. "elfutils-libelf-devel"
  296. "gawk"
  297. "unzip"
  298. "file"
  299. "wget"
  300. "python3"
  301. "python2"
  302. "axel"
  303. "perl-IPC-Cmd"
  304. )
  305. ;;
  306. debian|ubuntu)
  307. pkg_list+=(
  308. "build-essential"
  309. "libncurses5-dev"
  310. "libncursesw5-dev"
  311. "zlib1g-dev"
  312. "gawk"
  313. "git"
  314. "gettext"
  315. "libssl-dev"
  316. "xsltproc"
  317. "wget"
  318. "unzip"
  319. "python"
  320. "axel"
  321. )
  322. ;;
  323. *)
  324. debug "Skipping dependency install, your OS is unsupported"
  325. return 1
  326. ;;
  327. esac
  328. fi
  329. # Skip dependency installation if lock file is present
  330. [[ -f $lock_file ]] && return
  331. pkg_install "${pkg_list[@]}" && echo "${pkg_list[@]}" > "$lock_file"
  332. }
  333. getImageBuilder() {
  334. debug "${FUNCNAME[0]}"
  335. declare url="$1"
  336. if [[ -f "$IB_ARCHIVE" ]]; then
  337. if askOk "$IB_ARCHIVE exists. Re-download?"; then
  338. execute rm -f "$IB_ARCHIVE"
  339. else
  340. return 0
  341. fi
  342. fi
  343. echo "Downloading Image Builder archive using $DL_TOOL"
  344. execute "$DL_TOOL" "-o" "$IB_ARCHIVE" "$url"
  345. }
  346. getImageBuilderChecksum() {
  347. debug "${FUNCNAME[0]}"
  348. if [[ -f $IB_SHA256_FILE ]]; then
  349. if askOk "$IB_SHA256_FILE exists. Re-download?"; then
  350. execute rm -f "$IB_SHA256_FILE"
  351. else
  352. return 0
  353. fi
  354. fi
  355. execute "$DL_TOOL -o $IB_SHA256_FILE $IB_SHA256_URL"
  356. }
  357. addRepos() {
  358. debug "${FUNCNAME[0]}"
  359. if [[ -v P_ARR[repo] ]]; then
  360. if ! grep -q "${P_ARR[repo]}" "$BUILDDIR/repositories.conf"; then
  361. echo "${P_ARR[repo]}" >> "$BUILDDIR/repositories.conf"
  362. fi
  363. sed -i '/option check_signature/d' "$BUILDDIR/repositories.conf"
  364. fi
  365. }
  366. sshBackup() {
  367. debug "${FUNCNAME[0]}"
  368. declare date hostname backup_fname
  369. [[ -d "$FILESDIR" ]] || mkdir -p "$FILESDIR"
  370. printf -v date '%(%Y-%m-%d-%H-%M-%S)T'
  371. hostname=$(ssh -qt "$SSH_BACKUP_PATH" echo -n \$HOSTNAME)
  372. backup_fname="backup-$hostname-$date.tar.gz"
  373. # Make backup archive on remote
  374. if ! execute "ssh -t $SSH_BACKUP_PATH sysupgrade -b /tmp/$backup_fname"; then
  375. echo "SSH backup failed"
  376. exit 1
  377. fi
  378. # Move backup archive locally
  379. if ! execute "rsync -avz --remove-source-files $SSH_BACKUP_PATH:/tmp/$backup_fname $BUILDDIR/"; then
  380. echo "Could not copy SSH backup"
  381. exit 1
  382. fi
  383. # Extract backup archive
  384. if ! execute "tar -C $FILESDIR -xzf $BUILDDIR/$backup_fname"; then
  385. echo "Could not extract SSH backup"
  386. exit 1
  387. fi
  388. execute "rm $BUILDDIR/$backup_fname"
  389. }
  390. makeImages() {
  391. debug "${FUNCNAME[0]}"
  392. # Reuse the existing output
  393. if [[ -d "$BINDIR" ]]; then
  394. if askOk "$BINDIR exists. Rebuild?"; then
  395. execute rm -rf "$BINDIR"
  396. else
  397. return 0
  398. fi
  399. fi
  400. make image \
  401. BIN_DIR="$BINDIR" \
  402. PROFILE="$DEVICE" \
  403. PACKAGES="$PACKAGES" \
  404. FILES="$FILESDIR" \
  405. --directory="$BUILDDIR" \
  406. --jobs="$(nproc)" \
  407. > "$BUILDDIR/make.log"
  408. }
  409. verifyImages() {
  410. debug "${FUNCNAME[0]}"
  411. declare outfile
  412. for outfile in "$BINDIR"/*.img.gz; do
  413. verify "$outfile" "$IB_OUT_SHA256_FILE" || return 1
  414. done
  415. }
  416. flashImage() {
  417. debug "${FUNCNAME[0]}"
  418. declare img_gz="$1"
  419. declare dev="$2"
  420. declare img="${img_gz%.gz}"
  421. declare partitions
  422. if [[ ! -e "$dev" ]]; then
  423. echo "The device specified by --flash could not be found"
  424. return 1
  425. fi
  426. if [[ ! -f $img_gz ]]; then
  427. echo "$img_gz does not exist"
  428. echo "Check your build output"
  429. return 1
  430. fi
  431. execute gunzip -qfk "$img_gz"
  432. echo "Unmounting target device $dev partitions"
  433. partitions=( "$dev"?* )
  434. execute sudo umount "${partitions[@]}"
  435. if execute sudo dd if="$img" of="$dev" bs=2M conv=fsync; then
  436. sync
  437. echo "Image flashed sucessfully!"
  438. else
  439. echo "dd failed!"
  440. exit 1
  441. fi
  442. }
  443. sshUpgrade() {
  444. debug "${FUNCNAME[0]}"
  445. declare img_gz="$1"
  446. declare ssh_path="$2"
  447. declare img_fname="${img_gz##*/}"
  448. if ! [[ -f $img_gz ]]; then
  449. echo "$img_gz is missing, check build output"
  450. return 1
  451. fi
  452. echo "Copying '$img_gz' to $ssh_path/tmp/$img_fname"
  453. debug "scp $img_gz $ssh_path:/tmp/$img_fname"
  454. if ! scp "$img_gz" "$ssh_path:/tmp/$img_fname"; then
  455. echo "Could not copy $img_gz to $ssh_path:/tmp/$img_fname"
  456. return 1
  457. fi
  458. echo "Executing remote sysupgrade"
  459. debug "ssh $ssh_path sysupgrade -F /tmp/$img_fname"
  460. # shellcheck disable=SC2029
  461. # execute remotely
  462. # this will probably be a weird exit code from closed connection
  463. ssh "$ssh_path" "sysupgrade -F /tmp/$img_fname"
  464. }
  465. fromSource() {
  466. debug "${FUNCNAME[0]}"
  467. declare src_url="https://github.com/openwrt/openwrt.git"
  468. declare seed_file="$GITWORKTREEDIR/.config"
  469. declare pkg kopt opt commit seed_file wt_cmd wt_commit description
  470. declare -a make_opts config_opts
  471. echo "Building from source is under development"
  472. # Update source code
  473. if [[ ! -d "$GITSRCDIR" ]]; then
  474. mkdir -p "$GITSRCDIR"
  475. git clone "$src_url" "$GITSRCDIR"
  476. fi
  477. git -C "$GITSRCDIR" pull
  478. wt_cmd=(git -C "$GITSRCDIR"
  479. worktree add
  480. --force
  481. --detach
  482. "$GITWORKTREEDIR")
  483. # Generate commitish for git worktree
  484. case "$RELEASE" in
  485. snapshot)
  486. wt_commit="origin/main"
  487. ;;
  488. [0-9][0-9].[0-9][0-9].*)
  489. local branch="openwrt-${RELEASE%.*}"
  490. local tag="v$RELEASE"
  491. if askOk "Use HEAD of $branch branch (y, recommended) or $tag tag (n)?"; then
  492. wt_commit="origin/$branch"
  493. else
  494. wt_commit="$tag"
  495. fi
  496. ;;
  497. *)
  498. debug "Passing '$RELEASE' commit-ish to git worktree"
  499. wt_commit="$RELEASE"
  500. ;;
  501. esac
  502. [[ -d "$GITWORKTREEDIR" ]] && rm -rf "$GITWORKTREEDIR"
  503. execute "${wt_cmd[@]}" "$wt_commit"
  504. # Print commit information
  505. commit=$(git -C "$GITWORKTREEDIR" rev-parse HEAD)
  506. description=$(git -C "$GITWORKTREEDIR" describe)
  507. echo "Current commit hash: $commit"
  508. echo "Git worktree description: $description"
  509. if (( DEBUG )); then
  510. if (( YES )); then
  511. git --no-pager -C "$GITWORKTREEDIR" log -1
  512. else
  513. git -C "$GITWORKTREEDIR" log -1
  514. fi
  515. fi
  516. # Enter worktree
  517. pushd "$GITWORKTREEDIR" || return 1
  518. # Update package feed
  519. ./scripts/feeds update -i -f &&
  520. ./scripts/feeds update -a -f &&
  521. ./scripts/feeds install -a -f
  522. # Grab the release seed config
  523. if ! curl -so "$seed_file" "$SEED_URL"; then
  524. echo "Could not obtain $seed_file from $SEED_URL"
  525. return 1
  526. fi
  527. # Set compilation output dir
  528. config_opts+=("CONFIG_BINARY_FOLDER=\"$BINDIR\"")
  529. # Add custom packages
  530. for pkg in $PACKAGES; do
  531. if [[ $pkg == -* ]]; then
  532. config_opts+=("CONFIG_PACKAGE_${pkg#-}=n") # remove package
  533. else
  534. config_opts+=("CONFIG_PACKAGE_$pkg=y") # add package
  535. fi
  536. done
  537. # Add kopts from profile
  538. for kopt in ${P_ARR[kopts]}; do
  539. config_opts+=("$kopt")
  540. done
  541. # Only compile selected fs
  542. sed -i '/CONFIG_TARGET_ROOTFS_/d' "$seed_file"
  543. config_opts+=("CONFIG_TARGET_PER_DEVICE_ROOTFS=n")
  544. if [[ $FILESYSTEM == "squashfs" ]]; then
  545. config_opts+=("CONFIG_TARGET_ROOTFS_EXT4FS=n")
  546. config_opts+=("CONFIG_TARGET_ROOTFS_SQUASHFS=y")
  547. elif [[ $FILESYSTEM == "ext4" ]]; then
  548. config_opts+=("CONFIG_TARGET_ROOTFS_SQUASHFS=n")
  549. config_opts+=("CONFIG_TARGET_ROOTFS_EXT4FS=y")
  550. fi
  551. # Only compile selected target image
  552. sed -i '/CONFIG_TARGET_DEVICE_/d' "$seed_file"
  553. config_opts+=("CONFIG_TARGET_MULTI_PROFILE=n")
  554. config_opts+=("CONFIG_TARGET_PROFILE=DEVICE_$DEVICE")
  555. config_opts+=("CONFIG_TARGET_${TARGET//\//_}_DEVICE_$DEVICE=y")
  556. config_opts+=("CONFIG_SDK=n")
  557. config_opts+=("CONFIG_SDK_LLVM_BPF=n")
  558. config_opts+=("CONFIG_IB=n")
  559. config_opts+=("CONFIG_MAKE_TOOLCHAIN=n")
  560. # Write options to config seed file
  561. for opt in "${config_opts[@]}"; do
  562. debug "Writing $opt to $seed_file"
  563. echo "$opt" >> "$seed_file"
  564. done
  565. # Cleaning modes
  566. # make clean # compiled output
  567. # make targetclean # compiled output, toolchain
  568. # make dirclean # compiled output, toolchain, build tools
  569. # make distclean # compiled output, toolchain, build tools, .config, feeds, .ccache
  570. # Make image
  571. (( DEBUG )) && make_opts+=("V=s")
  572. execute make "${make_opts[@]}" defconfig
  573. execute make "${make_opts[@]}" dirclean
  574. execute make "${make_opts[@]}" download
  575. #execute make -f Makefile.aperl inst_perl MAP_TARGET=perl
  576. #execute make -f Makefile.aperl map_clean
  577. execute make "${make_opts[@]}" "-j$(nproc)" world
  578. popd || return 1
  579. # Provide symlinks to images in root of BINDIR (to match Image Builder)
  580. shopt -s nullglob
  581. for image in "$BINDIR/targets/${TARGET}/"*.{img,img.gz,ubi}; do
  582. ln -fs "$image" "$BINDIR/${image##*/}"
  583. done
  584. shopt -u nullglob
  585. return 0
  586. }
  587. # Generic helpers
  588. debug() { (( DEBUG )) && echo "Debug: $*"; }
  589. askOk() {
  590. (( YES )) && return
  591. local r
  592. read -r -p "$* [y/N]: " r
  593. r=${r,,}
  594. [[ "$r" =~ ^(yes|y)$ ]]
  595. }
  596. extract() {
  597. debug "${FUNCNAME[0]}"
  598. declare archive="$1"
  599. declare out_dir="$2"
  600. if ! execute tar -xf "$archive" -C "$out_dir" --strip-components 1; then
  601. echo "Extraction failed"
  602. return 1
  603. fi
  604. }
  605. verify() {
  606. debug "${FUNCNAME[0]}"
  607. declare file_to_check="$1"
  608. declare sumfile="$2"
  609. declare checksum
  610. hash sha256sum &>/dev/null || return 1
  611. [[ -f $sumfile && -f $file_to_check ]] || return 1
  612. checksum=$(grep "${file_to_check##*/}" "$sumfile" | cut -f1 -d' ')
  613. echo -n "$checksum $file_to_check" | sha256sum --check --status
  614. }
  615. load() {
  616. debug "${FUNCNAME[0]}"
  617. declare source_file="$1"
  618. # shellcheck disable=SC1090
  619. [[ -f $source_file ]] && source "$source_file"
  620. }
  621. execute() {
  622. declare cmd="$*"
  623. debug "$cmd" || cmd+=" &>/dev/null"
  624. eval "${cmd[*]}"
  625. }
  626. main() {
  627. debug "${FUNCNAME[0]}"
  628. init
  629. load "$SCRIPTDIR/profiles"
  630. readInput "$@"
  631. # Fallback to SCRIPTDIR if BUILDROOT has not been set
  632. declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
  633. declare -g FILESDIR="${FILESDIR:=$BUILDROOT/src/files}"
  634. # This could be dangerous
  635. if [[ $BUILDROOT == "/" ]]; then
  636. echo "Invalid --buildroot"
  637. exit 1
  638. fi
  639. for dir in "$BUILDROOT/src" "$BUILDROOT/bin"; do
  640. [[ -d "$dir" ]] || mkdir -p "$dir"
  641. done
  642. # Allow --reset without a profile
  643. if (( RESET )) && [[ ${#PROFILES} -lt 1 ]]; then
  644. for d in "$BUILDROOT/src" "$BUILDROOT/bin"; do
  645. askOk "Remove $d?" && execute rm -rf "$d"
  646. done
  647. exit $?
  648. fi
  649. installDependencies
  650. for profile in "${PROFILES[@]}"; do
  651. debug "Starting profile: $profile"
  652. if [[ ! ${!profile@a} = A ]]; then
  653. echo "Profile '$profile' does not exist"
  654. return 1
  655. fi
  656. # Store profile in P_ARR nameref
  657. declare -gn P_ARR="$profile"
  658. # Load profile
  659. declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
  660. declare -g TARGET="${P_ARR[target]}"
  661. declare -g DEVICE="${P_ARR[device]}"
  662. declare -g PACKAGES="${P_ARR[packages]:-}"
  663. # Release precedence: user input>profile>env>hardcode
  664. declare -g RELEASE="${USER_RELEASE:=${P_ARR[release]:=$RELEASE}}"
  665. # normalize release input
  666. case "$RELEASE" in
  667. snapshot|latest|main|master) # normalize aliases
  668. RELEASE="snapshot"
  669. ;;
  670. v[0-9][0-9].[0-9][0-9].*) # tag to semantic
  671. RELEASE="${RELEASE#v}"
  672. ;;
  673. [0-9][0-9].[0-9][0-9].*)
  674. ;;
  675. *)
  676. if ! (( FROM_SOURCE )); then
  677. echo "Error: Invalid release version format"
  678. echo "Use semantic version, tag, or 'snapshot'"
  679. exit 1
  680. fi
  681. ;;
  682. esac
  683. declare -g GITSRCDIR="$BUILDROOT/src/openwrt"
  684. declare -g GITWORKTREEDIR="$BUILDROOT/src/$profile/$RELEASE-src"
  685. declare -g BUILDDIR="$BUILDROOT/src/$profile/$RELEASE"
  686. declare -g BINDIR="$BUILDROOT/bin/$profile/$RELEASE"
  687. if (( RESET )); then
  688. if (( FROM_SOURCE )); then
  689. [[ -d $GITWORKTREEDIR ]] && askOk "Remove $GITWORKTREEDIR?"
  690. execute git worktree remove --force "$GITWORKTREEDIR"
  691. execute rm -rf "$GITWORKTREEDIR"
  692. elif [[ -d $BUILDDIR ]] && askOk "Remove $BUILDDIR?"; then
  693. execute rm -rf "$BUILDDIR"
  694. fi
  695. fi
  696. if [[ "$RELEASE" == "snapshot" ]]; then
  697. declare url_prefix="https://downloads.openwrt.org/snapshots/targets/$TARGET"
  698. declare url_filename="openwrt-imagebuilder-${TARGET//\//-}.Linux-x86_64.tar.xz"
  699. declare img_fname="openwrt-${TARGET//\//-}-$DEVICE-$FILESYSTEM"
  700. else
  701. declare url_prefix="https://downloads.openwrt.org/releases/$RELEASE/targets/$TARGET"
  702. declare url_filename="openwrt-imagebuilder-$RELEASE-${TARGET//\//-}.Linux-x86_64.tar.xz"
  703. declare img_fname="openwrt-$RELEASE-${TARGET//\//-}-$DEVICE-$FILESYSTEM"
  704. fi
  705. declare ib_url="$url_prefix/$url_filename"
  706. if (( FROM_SOURCE )); then
  707. declare -g SYSUPGRADEIMGGZ="$BINDIR/targets/$img_fname-sysupgrade.img.gz"
  708. declare -g SEED_URL="$url_prefix/config.buildinfo"
  709. else
  710. declare -g SYSUPGRADEIMGGZ="$BUILDDIR/$img_fname-sysupgrade.img.gz"
  711. declare -g IB_ARCHIVE="$BUILDDIR/$url_filename"
  712. declare -g IB_SHA256_URL="$url_prefix/sha256sums"
  713. declare -g IB_SHA256_FILE="$IB_ARCHIVE.sha256sums"
  714. declare -g IB_OUT_SHA256_FILE="$BINDIR/sha256sums"
  715. fi
  716. if (( DEBUG )); then
  717. echo "Profile settings:"
  718. for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
  719. echo "Build settings:"
  720. cat <<- EOF
  721. PROFILE, P_ARR (should match)=$profile, ${!P_ARR}
  722. BUILDROOT=$BUILDROOT
  723. BUILDDIR=$BUILDDIR
  724. GITSRCDIR=$GITSRCDIR
  725. GITWORKTREEDIR=$GITWORKTREEDIR
  726. BINDIR=$BINDIR
  727. TARGET=$TARGET
  728. DEVICE=$DEVICE
  729. RELEASE=$RELEASE
  730. FILESYSTEM=$FILESYSTEM
  731. SYSUPGRADEIMGGZ=$SYSUPGRADEIMGGZ
  732. ib_url=$ib_url
  733. EOF
  734. fi
  735. if (( FROM_SOURCE )); then
  736. fromSource || return $?
  737. else
  738. [[ -d $BUILDDIR ]] || mkdir -p "$BUILDDIR"
  739. getImageBuilder "$ib_url" &&
  740. getImageBuilderChecksum &&
  741. verify "$IB_ARCHIVE" "$IB_SHA256_FILE" &&
  742. extract "$IB_ARCHIVE" "$BUILDDIR" || return $?
  743. addRepos
  744. makeImages &&
  745. verifyImages
  746. #copyFiles
  747. fi
  748. [[ -v SSH_BACKUP_PATH ]] &&
  749. sshBackup
  750. [[ -v SSH_UPGRADE_PATH ]] &&
  751. sshUpgrade "$SYSUPGRADEIMGGZ" "$SSH_UPGRADE_PATH"
  752. [[ -v FLASH_DEV ]] &&
  753. flashImage "$SYSUPGRADEIMGGZ" "$FLASH_DEV"
  754. done
  755. }
  756. main "$@"
  757. exit
  758. # VM setup (for testing)
  759. # sudo sgdisk -N 0 /dev/vda &&
  760. # sudo mkfs.ext4 /dev/vda1
  761. # mkdir ~/mnt
  762. # sudo mount /dev/vda1 ~/mnt
  763. # sudo chown liveuser:liveuser -R ~/mnt