openwrtbuilder 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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_stock -r 22.03.3 --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
  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. case "$RELEASE" in
  484. snapshot)
  485. execute "${wt_cmd[@]}" origin/main
  486. ;;
  487. [0-9][0-9].[0-9][0-9].*)
  488. local branch="openwrt-${RELEASE%.*}"
  489. local tag="v$RELEASE"
  490. if askOk "Use HEAD of $branch branch (y, recommended) or $tag tag (n)?"; then
  491. execute "${wt_cmd[@]}" "origin/$branch"
  492. else
  493. execute "${wt_cmd[@]}" "$tag"
  494. fi
  495. ;;
  496. *)
  497. debug "Passing '$RELEASE' commit-ish to git worktree"
  498. execute "${wt_cmd[@]}" "$RELEASE"
  499. ;;
  500. esac
  501. # Print commit information
  502. commit=$(git -C "$GITWORKTREEDIR" rev-parse HEAD)
  503. echo "Current commit hash: $commit"
  504. (( DEBUG )) && git -C "$GITWORKTREEDIR" log -1
  505. (( DEBUG )) && git -C "$GITWORKTREEDIR" describe
  506. # Enter worktree
  507. pushd "$GITWORKTREEDIR" || return 1
  508. # Update package feed
  509. ./scripts/feeds update -i -f &&
  510. ./scripts/feeds update -a -f &&
  511. ./scripts/feeds install -a -f
  512. # Grab the release seed config
  513. if ! curl -so "$seed_file" "$SEED_URL"; then
  514. echo "Could not obtain $seed_file from $SEED_URL"
  515. return 1
  516. fi
  517. # Set compilation output dir
  518. config_opts+=("CONFIG_BINARY_FOLDER=\"$BINDIR\"")
  519. # Add custom packages
  520. for pkg in $PACKAGES; do
  521. if [[ $pkg == -* ]]; then
  522. config_opts+=("CONFIG_PACKAGE_${pkg#-}=n") # remove package
  523. else
  524. config_opts+=("CONFIG_PACKAGE_$pkg=y") # add package
  525. fi
  526. done
  527. # Add kopts from profile
  528. for kopt in ${P_ARR[kopts]}; do
  529. config_opts+=("$kopt")
  530. done
  531. # Only compile selected fs
  532. sed -i '/CONFIG_TARGET_ROOTFS_/d' "$seed_file"
  533. config_opts+=("CONFIG_TARGET_PER_DEVICE_ROOTFS=n")
  534. if [[ $FILESYSTEM == "squashfs" ]]; then
  535. config_opts+=("CONFIG_TARGET_ROOTFS_EXT4FS=n")
  536. config_opts+=("CONFIG_TARGET_ROOTFS_SQUASHFS=y")
  537. elif [[ $FILESYSTEM == "ext4" ]]; then
  538. config_opts+=("CONFIG_TARGET_ROOTFS_SQUASHFS=n")
  539. config_opts+=("CONFIG_TARGET_ROOTFS_EXT4FS=y")
  540. fi
  541. # Only compile selected target image
  542. sed -i '/CONFIG_TARGET_DEVICE_/d' "$seed_file"
  543. config_opts+=("CONFIG_TARGET_MULTI_PROFILE=n")
  544. config_opts+=("CONFIG_TARGET_PROFILE=DEVICE_$DEVICE")
  545. config_opts+=("CONFIG_TARGET_${TARGET//\//_}_DEVICE_$DEVICE=y")
  546. config_opts+=("CONFIG_SDK=n")
  547. config_opts+=("CONFIG_SDK_LLVM_BPF=n")
  548. config_opts+=("CONFIG_IB=n")
  549. config_opts+=("CONFIG_MAKE_TOOLCHAIN=n")
  550. # Write options to config seed file
  551. for opt in "${config_opts[@]}"; do
  552. debug "Writing $opt to $seed_file"
  553. echo "$opt" >> "$seed_file"
  554. done
  555. # Cleaning modes
  556. # make clean # compiled output
  557. # make targetclean # compiled output, toolchain
  558. # make dirclean # compiled output, toolchain, build tools
  559. # make distclean # compiled output, toolchain, build tools, .config, feeds, .ccache
  560. # Make image
  561. (( DEBUG )) && make_opts+=("V=s")
  562. execute make "${make_opts[@]}" defconfig
  563. execute make "${make_opts[@]}" targetclean
  564. execute make "${make_opts[@]}" download
  565. execute make -f Makefile.aperl inst_perl MAP_TARGET=perl
  566. execute make -f Makefile.aperl map_clean
  567. execute make "${make_opts[@]}" "-j$(nproc)" world
  568. popd || return 1
  569. # Provide symlinks to images in root of BINDIR (to match Image Builder)
  570. shopt -s nullglob
  571. for image in "$BINDIR/targets/${TARGET}/"*.{img,img.gz,ubi}; do
  572. ln -fs "$image" "$BINDIR/${image##*/}"
  573. done
  574. shopt -u nullglob
  575. return 0
  576. }
  577. # Generic helpers
  578. debug() { (( DEBUG )) && echo "Debug: $*"; }
  579. askOk() {
  580. (( YES )) && return
  581. local r
  582. read -r -p "$* [y/N]: " r
  583. r=${r,,}
  584. [[ "$r" =~ ^(yes|y)$ ]]
  585. }
  586. extract() {
  587. debug "${FUNCNAME[0]}"
  588. declare archive="$1"
  589. declare out_dir="$2"
  590. if ! execute tar -xf "$archive" -C "$out_dir" --strip-components 1; then
  591. echo "Extraction failed"
  592. return 1
  593. fi
  594. }
  595. verify() {
  596. debug "${FUNCNAME[0]}"
  597. declare file_to_check="$1"
  598. declare sumfile="$2"
  599. declare checksum
  600. hash sha256sum &>/dev/null || return 1
  601. [[ -f $sumfile && -f $file_to_check ]] || return 1
  602. checksum=$(grep "${file_to_check##*/}" "$sumfile" | cut -f1 -d' ')
  603. echo -n "$checksum $file_to_check" | sha256sum --check --status
  604. }
  605. load() {
  606. debug "${FUNCNAME[0]}"
  607. declare source_file="$1"
  608. # shellcheck disable=SC1090
  609. [[ -f $source_file ]] && source "$source_file"
  610. }
  611. execute() {
  612. declare cmd="$*"
  613. debug "$cmd" || cmd+=" &>/dev/null"
  614. eval "${cmd[*]}"
  615. }
  616. main() {
  617. debug "${FUNCNAME[0]}"
  618. init
  619. load "$SCRIPTDIR/profiles"
  620. readInput "$@"
  621. # Fallback to SCRIPTDIR if BUILDROOT has not been set
  622. declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
  623. declare -g FILESDIR="${FILESDIR:=$BUILDROOT/src/files}"
  624. # This could be dangerous
  625. if [[ $BUILDROOT == "/" ]]; then
  626. echo "Invalid --buildroot"
  627. exit 1
  628. fi
  629. for dir in "$BUILDROOT/src" "$BUILDROOT/bin"; do
  630. [[ -d "$dir" ]] || mkdir -p "$dir"
  631. done
  632. # Allow --reset without a profile
  633. if (( RESET )) && [[ ${#PROFILES} -lt 1 ]]; then
  634. for d in "$BUILDROOT/src" "$BUILDROOT/bin"; do
  635. askOk "Remove $d?" && execute rm -rf "$d"
  636. done
  637. exit $?
  638. fi
  639. installDependencies
  640. for profile in "${PROFILES[@]}"; do
  641. debug "Starting profile: $profile"
  642. if [[ ! ${!profile@a} = A ]]; then
  643. echo "Profile '$profile' does not exist"
  644. return 1
  645. fi
  646. # Store profile in P_ARR nameref
  647. declare -gn P_ARR="$profile"
  648. # Load profile
  649. declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
  650. declare -g TARGET="${P_ARR[target]}"
  651. declare -g DEVICE="${P_ARR[device]}"
  652. declare -g PACKAGES="${P_ARR[packages]:-}"
  653. # Release precedence: user input>profile>env>hardcode
  654. declare -g RELEASE="${USER_RELEASE:=${P_ARR[release]:=$RELEASE}}"
  655. # normalize release input
  656. case "$RELEASE" in
  657. snapshot|latest|main|master) # normalize aliases
  658. RELEASE="snapshot"
  659. ;;
  660. v[0-9][0-9].[0-9][0-9].*) # tag to semantic
  661. RELEASE="${RELEASE#v}"
  662. ;;
  663. [0-9][0-9].[0-9][0-9].*)
  664. ;;
  665. *)
  666. if ! (( FROM_SOURCE )); then
  667. echo "Error: Invalid release version format"
  668. echo "Use semantic version, tag, or 'snapshot'"
  669. exit 1
  670. fi
  671. ;;
  672. esac
  673. declare -g GITSRCDIR="$BUILDROOT/src/openwrt"
  674. declare -g GITWORKTREEDIR="$BUILDROOT/src/$profile/$RELEASE-src"
  675. declare -g BUILDDIR="$BUILDROOT/src/$profile/$RELEASE"
  676. declare -g BINDIR="$BUILDROOT/bin/$profile/$RELEASE"
  677. if (( RESET )); then
  678. if (( FROM_SOURCE )); then
  679. [[ -d $GITWORKTREEDIR ]] && askOk "Remove $GITWORKTREEDIR?"
  680. execute git worktree remove --force "$GITWORKTREEDIR"
  681. execute rm -rf "$GITWORKTREEDIR"
  682. elif [[ -d $BUILDDIR ]] && askOk "Remove $BUILDDIR?"; then
  683. execute rm -rf "$BUILDDIR"
  684. fi
  685. fi
  686. if [[ "$RELEASE" == "snapshot" ]]; then
  687. declare url_prefix="https://downloads.openwrt.org/snapshots/targets/$TARGET"
  688. declare url_filename="openwrt-imagebuilder-${TARGET//\//-}.Linux-x86_64.tar.xz"
  689. declare img_fname="openwrt-${TARGET//\//-}-$DEVICE-$FILESYSTEM"
  690. else
  691. declare url_prefix="https://downloads.openwrt.org/releases/$RELEASE/targets/$TARGET"
  692. declare url_filename="openwrt-imagebuilder-$RELEASE-${TARGET//\//-}.Linux-x86_64.tar.xz"
  693. declare img_fname="openwrt-$RELEASE-${TARGET//\//-}-$DEVICE-$FILESYSTEM"
  694. fi
  695. declare ib_url="$url_prefix/$url_filename"
  696. if (( FROM_SOURCE )); then
  697. declare -g SYSUPGRADEIMGGZ="$BINDIR/targets/$img_fname-sysupgrade.img.gz"
  698. declare -g SEED_URL="$url_prefix/config.buildinfo"
  699. else
  700. declare -g SYSUPGRADEIMGGZ="$BUILDDIR/$img_fname-sysupgrade.img.gz"
  701. declare -g IB_ARCHIVE="$BUILDDIR/$url_filename"
  702. declare -g IB_SHA256_URL="$url_prefix/sha256sums"
  703. declare -g IB_SHA256_FILE="$IB_ARCHIVE.sha256sums"
  704. declare -g IB_OUT_SHA256_FILE="$BINDIR/sha256sums"
  705. fi
  706. if (( DEBUG )); then
  707. echo "Profile settings:"
  708. for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
  709. echo "Build settings:"
  710. cat <<- EOF
  711. PROFILE, P_ARR (should match)=$profile, ${!P_ARR}
  712. BUILDROOT=$BUILDROOT
  713. BUILDDIR=$BUILDDIR
  714. GITSRCDIR=$GITSRCDIR
  715. GITWORKTREEDIR=$GITWORKTREEDIR
  716. BINDIR=$BINDIR
  717. TARGET=$TARGET
  718. DEVICE=$DEVICE
  719. RELEASE=$RELEASE
  720. FILESYSTEM=$FILESYSTEM
  721. SYSUPGRADEIMGGZ=$SYSUPGRADEIMGGZ
  722. ib_url=$ib_url
  723. EOF
  724. fi
  725. if (( FROM_SOURCE )); then
  726. fromSource || return $?
  727. else
  728. [[ -d $BUILDDIR ]] || mkdir -p "$BUILDDIR"
  729. getImageBuilder "$ib_url" &&
  730. getImageBuilderChecksum &&
  731. verify "$IB_ARCHIVE" "$IB_SHA256_FILE" &&
  732. extract "$IB_ARCHIVE" "$BUILDDIR" || return $?
  733. addRepos
  734. makeImages &&
  735. verifyImages
  736. #copyFiles
  737. fi
  738. [[ -v SSH_BACKUP_PATH ]] &&
  739. sshBackup
  740. [[ -v SSH_UPGRADE_PATH ]] &&
  741. sshUpgrade "$SYSUPGRADEIMGGZ" "$SSH_UPGRADE_PATH"
  742. [[ -v FLASH_DEV ]] &&
  743. flashImage "$SYSUPGRADEIMGGZ" "$FLASH_DEV"
  744. done
  745. }
  746. main "$@"
  747. exit
  748. # VM setup (for testing)
  749. # sudo sgdisk -N 0 /dev/vda &&
  750. # sudo mkfs.ext4 /dev/vda1
  751. # mkdir ~/mnt
  752. # sudo mount /dev/vda1 ~/mnt
  753. # sudo chown liveuser:liveuser -R ~/mnt