openwrtbuilder 24 KB

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