openwrtbuilder 24 KB

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