openwrtbuilder 23 KB

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