openwrtbuilder 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. # Set default release
  9. : "${RELEASE:="22.03.3"}"
  10. printHelp() {
  11. debug "${FUNCNAME[0]}"
  12. cat <<-'EOF'
  13. Build and deploy OpenWRT images
  14. USAGE:
  15. openwrtbuilder [OPTION [VALUE]] -p PROFILE [-p PROFILE2]...
  16. OPTIONS
  17. --profile, -p PROFILE
  18. --info, -i (print profile info)
  19. --list-profiles, -l
  20. --release, --version, -r, -v RELEASE ("snapshot", "22.03.3")
  21. --buildroot, -b PATH
  22. --ssh-upgrade HOST
  23. Example: root@192.168.1.1
  24. --ssh-backup SSH_PATH
  25. (Enabled by default for --ssh-upgrade)
  26. --flash, -f DEVICE
  27. Example: /dev/sdX
  28. --reset
  29. Cleanup all source and output files
  30. --debug, -d
  31. --help, -h
  32. EOF
  33. }
  34. init() {
  35. debug "${FUNCNAME[0]}"
  36. declare -g ID RPM_MGR SCRIPTDIR DL_TOOL
  37. debug || echo "To enable debugging output, use --debug or -d"
  38. # Save the script directory
  39. SCRIPTDIR="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit $? ; pwd -P)"
  40. if [[ -e "/etc/os-release" ]]; then
  41. source "/etc/os-release"
  42. else
  43. err "/etc/os-release not found"
  44. err "Your OS is unsupported"
  45. printHelp
  46. exit 1
  47. fi
  48. debug "Detected host platform: $ID"
  49. # normalize distro ID
  50. case "$ID" in
  51. debian|arch)
  52. ;;
  53. centos|fedora)
  54. if hash dnf &>/dev/null; then
  55. RPM_MGR="dnf"
  56. elif hash yum &>/dev/null; then
  57. RPM_MGR="yum"
  58. fi
  59. ;;
  60. rhel)
  61. ID="centos"
  62. ;;
  63. linuxmint|neon|*ubuntu*)
  64. ID="ubuntu"
  65. ;;
  66. *suse*)
  67. ID="suse"
  68. ;;
  69. raspbian)
  70. ID="debian"
  71. ;;
  72. *)
  73. echo "Autodetecting distro, this may be unreliable"
  74. if hash dnf &>/dev/null; then
  75. ID="fedora"
  76. RPM_MGR="dnf"
  77. elif hash yum &>/dev/null; then
  78. ID="centos"
  79. RPM_MGR="yum"
  80. elif hash apt &>/dev/null; then
  81. ID="ubuntu"
  82. elif hash pacman &>/dev/null; then
  83. ID="arch"
  84. else
  85. return 1
  86. fi
  87. ;;
  88. esac
  89. debug "Using host platform: $ID"
  90. # Set distro-specific functions
  91. case "$ID" in
  92. fedora|centos)
  93. pkg_install(){ sudo "$RPM_MGR" install -y "$@"; }
  94. ;;
  95. debian|ubuntu)
  96. pkg_install(){ sudo apt-get install -y -q0 "$@"; }
  97. ;;
  98. suse)
  99. pkg_install(){ sudo zypper --non-interactive -q install --force --no-confirm "$@"; }
  100. ;;
  101. arch)
  102. pkg_install(){ sudo pacman -S --noconfirm --needed "$@"; }
  103. ;;
  104. esac
  105. if hash axel &>/dev/null; then
  106. DL_TOOL="axel"
  107. elif hash curl &>/dev/null; then
  108. DL_TOOL="curl"
  109. else
  110. echo "Downloading the Image Builder requires axel or curl!"
  111. return 1
  112. fi
  113. }
  114. readInput() {
  115. debug "${FUNCNAME[0]}"
  116. unset RESET
  117. declare -ga PROFILES
  118. declare -g PROFILE_INFO
  119. declare long_opts
  120. long_opts='release:,version:,profile:,info:,list-profiles,buildroot:,'
  121. long_opts+='source,ssh-upgrade:,ssh-backup:,flash:,reset,debug,help'
  122. if _input=$(getopt -o +r:v:p:i:lb:sf:dh -l $long_opts -- "$@"); then
  123. eval set -- "$_input"
  124. while true; do
  125. case "$1" in
  126. --release|-r|--version|-v)
  127. shift && declare -g USER_RELEASE="$1"
  128. ;;
  129. --profile|-p)
  130. shift && PROFILES+=("$1")
  131. ;;
  132. --info|-i)
  133. PROFILE_INFO=1
  134. ;;
  135. --list-profiles|-l)
  136. listProfiles && exit $?
  137. ;;
  138. --buildroot|-b)
  139. shift && BUILDROOT="$1"
  140. ;;
  141. --source|-s)
  142. FROM_SOURCE=1
  143. ;;
  144. --ssh-upgrade)
  145. shift && SSH_UPGRADE_PATH="$1"
  146. ;;
  147. --ssh-backup)
  148. shift && SSH_BACKUP_PATH="$1"
  149. ;;
  150. --flash|-f)
  151. shift && FLASH_DEV="$1"
  152. ;;
  153. --reset)
  154. RESET=1
  155. ;;
  156. --debug|-d)
  157. echo "Debugging on"
  158. DEBUG=1
  159. ;;
  160. --help|-h)
  161. printHelp && exit 0
  162. ;;
  163. --)
  164. shift
  165. break
  166. ;;
  167. esac
  168. shift
  169. done
  170. else
  171. echo "Incorrect options provided"
  172. printHelp && exit 1
  173. fi
  174. }
  175. listProfiles() {
  176. debug "${FUNCNAME[0]}"
  177. grep "declare -Ag" "$PFILE" | cut -d" " -f3
  178. }
  179. installDependencies() {
  180. debug "${FUNCNAME[0]}"
  181. declare -a pkg_list
  182. # TODO please contribute your platform here
  183. if (( FROM_SOURCE )); then
  184. # For building from source with make
  185. # https://openwrt.org/docs/guide-developer/toolchain/install-buildsystem
  186. case "$ID" in
  187. fedora|centos)
  188. pkg_list+=(
  189. "bash-completion"
  190. "bzip2"
  191. "gcc"
  192. "gcc-c++"
  193. "git"
  194. "make"
  195. "ncurses-devel"
  196. "patch"
  197. "rsync"
  198. "tar"
  199. "unzip"
  200. "wget"
  201. "which"
  202. "diffutils"
  203. "python2"
  204. "python3"
  205. "perl-base"
  206. "perl-Data-Dumper"
  207. "perl-File-Compare"
  208. "perl-File-Copy"
  209. "perl-FindBin"
  210. "perl-Thread-Queue"
  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. "fakeroot"
  243. "file"
  244. "findutils"
  245. "flex"
  246. "gawk"
  247. "gcc"
  248. "gettext"
  249. "git"
  250. "grep"
  251. "groff"
  252. "gzip"
  253. "libelf"
  254. "libtool"
  255. "libxslt"
  256. "m4"
  257. "make"
  258. "ncurses"
  259. "openssl"
  260. "patch"
  261. "pkgconf"
  262. "python"
  263. "rsync"
  264. "sed"
  265. "texinfo"
  266. "time"
  267. "unzip"
  268. "util-linux"
  269. "wget"
  270. "which"
  271. "zlib"
  272. )
  273. ;;
  274. esac
  275. else
  276. # For Imagebuilder
  277. case "$ID" in
  278. fedora|centos)
  279. pkg_list+=(
  280. "@c-development"
  281. "@development-tools"
  282. "@development-libs"
  283. "perl-FindBin"
  284. "zlib-static"
  285. "elfutils-libelf-devel"
  286. "gawk"
  287. "unzip"
  288. "file"
  289. "wget"
  290. "python3"
  291. "python2"
  292. "axel"
  293. )
  294. ;;
  295. debian|ubuntu)
  296. pkg_list+=(
  297. "build-essential"
  298. "libncurses5-dev"
  299. "libncursesw5-dev"
  300. "zlib1g-dev"
  301. "gawk"
  302. "git"
  303. "gettext"
  304. "libssl-dev"
  305. "xsltproc"
  306. "wget"
  307. "unzip"
  308. "python"
  309. "axel"
  310. )
  311. ;;
  312. esac
  313. fi
  314. pkg_install "${pkg_list[@]}"
  315. }
  316. getImageBuilder() {
  317. debug "${FUNCNAME[0]}"
  318. if [[ -f "$IB_ARCHIVE" ]]; then
  319. if askOK "$IB_ARCHIVE exists. Re-download?"; then
  320. rm -f "$IB_ARCHIVE"
  321. else
  322. return 0
  323. fi
  324. fi
  325. echo "Downloading Image Builder archive using $DL_TOOL"
  326. debug "$DL_TOOL -o $IB_ARCHIVE $IB_URL"
  327. "$DL_TOOL" -o "$IB_ARCHIVE" "$IB_URL"
  328. }
  329. getImageBuilderChecksum() {
  330. debug "${FUNCNAME[0]}"
  331. if [[ -f $IB_SHA256_FILE ]]; then
  332. if askOk "$IB_SHA256_FILE exists. Re-download?"; then
  333. rm -f "$IB_SHA256_FILE"
  334. else
  335. return 0
  336. fi
  337. fi
  338. "$DL_TOOL" -o "$IB_SHA256_FILE" "$IB_SHA256_URL"
  339. }
  340. extractImageBuilder() {
  341. debug "${FUNCNAME[0]}"
  342. echo "Extracting Image Builder archive"
  343. [[ ! -d "$BUILDDIR" ]] && mkdir -p "$BUILDDIR"
  344. debug "tar -xf $IB_ARCHIVE -C $BUILDDIR --strip-components 1"
  345. if ! tar -xf "$IB_ARCHIVE" -C "$BUILDDIR" --strip-components 1; then
  346. echo "Extraction failed"
  347. return 1
  348. fi
  349. }
  350. addRepos() {
  351. debug "${FUNCNAME[0]}"
  352. if [[ -v P_ARR[repo] ]]; then
  353. if ! grep -q "${P_ARR[repo]}" "$BUILDDIR/repositories.conf"; then
  354. echo "${P_ARR[repo]}" >> "$BUILDDIR/repositories.conf"
  355. fi
  356. sed -i '/option check_signature/d' "$BUILDDIR/repositories.conf"
  357. fi
  358. }
  359. sshBackup() {
  360. debug "${FUNCNAME[0]}"
  361. local _date _hostname _backup_fname
  362. [[ -d "$FILESDIR" ]] || mkdir -p "$FILESDIR"
  363. printf -v _date '%(%Y-%m-%d-%H-%M-%S)T'
  364. _hostname=$(ssh -qt "$SSH_BACKUP_PATH" echo -n \$HOSTNAME)
  365. _backup_fname="backup-$_hostname-$_date.tar.gz"
  366. # Make backup archive on remote
  367. debug "ssh -t $SSH_BACKUP_PATH sysupgrade -b /tmp/$_backup_fname"
  368. if ! ssh -t "$SSH_BACKUP_PATH" "sysupgrade -b /tmp/$_backup_fname"; then
  369. echo "SSH backup failed"
  370. exit 1
  371. fi
  372. # Move backup archive locally
  373. debug "rsync -avz --remove-source-files $SSH_BACKUP_PATH:/tmp/$_backup_fname $BUILDDIR/"
  374. if ! rsync -avz --remove-source-files \
  375. "$SSH_BACKUP_PATH":"/tmp/$_backup_fname" "$BUILDDIR/"; then
  376. echo "Could not copy SSH backup"
  377. exit 1
  378. fi
  379. # Extract backup archive
  380. debug "tar -C $FILESDIR -xzf $BUILDDIR/$_backup_fname"
  381. if ! tar -C "$FILESDIR" -xzf "$BUILDDIR/$_backup_fname"; then
  382. echo "Could not extract SSH backup"
  383. exit 1
  384. fi
  385. rm "$BUILDDIR/$_backup_fname"
  386. }
  387. makeImages() {
  388. debug "${FUNCNAME[0]}"
  389. # Reuse the existing output
  390. if [[ -d "$THIS_BINDIR" ]]; then
  391. if askOk "$THIS_BINDIR exists. Rebuild?"; then
  392. rm -rf "$THIS_BINDIR"
  393. else
  394. return 0
  395. fi
  396. fi
  397. [[ -d "$BUILDDIR" ]] || mkdir -p "$BUILDDIR"
  398. make image \
  399. BIN_DIR="$THIS_BINDIR" \
  400. PROFILE="${P_ARR[profile]}" \
  401. PACKAGES="${P_ARR[packages]:+"${P_ARR[packages]}"}" \
  402. FILES="${FILESDIR}" \
  403. --directory="$BUILDDIR" \
  404. --jobs="$(nproc)" \
  405. > "$BUILDDIR/make.log"
  406. }
  407. verifyImages() {
  408. debug "${FUNCNAME[0]}"
  409. declare outfile
  410. for outfile in "$THIS_BINDIR"/*.img.gz; do
  411. verify "$outfile" "$IB_OUT_SHA256_FILE" || return 1
  412. done
  413. }
  414. flashImage() {
  415. debug "${FUNCNAME[0]}"
  416. declare img img_gz partitions
  417. if [[ ! -e "$FLASH_DEV" ]]; then
  418. echo "The device specified by --flash could not be found"
  419. exit 1
  420. fi
  421. # TODO Roughly choose the correct image
  422. if [[ -f "$FACTORYIMGGZ" ]]; then
  423. img_gz="$FACTORYIMGGZ"
  424. img="$FACTORYIMG"
  425. elif [[ -f "$SYSUPGRADEIMGGZ" ]]; then
  426. img_gz="$SYSUPGRADEIMGGZ"
  427. img="$SYSUPGRADEIMG"
  428. else
  429. return 1
  430. fi
  431. debug "$img_gz $img"
  432. debug "gunzip -qfk $img_gz"
  433. gunzip -qfk "$img_gz"
  434. echo "Unmounting target device $FLASH_DEV partitions"
  435. partitions=( "$FLASH_DEV"?* )
  436. debug "umount ${partitions[*]}"
  437. sudo umount "${partitions[@]}"
  438. debug "sudo dd if=\"$img\" of=\"$FLASH_DEV\" bs=2M conv=fsync"
  439. if sudo dd if="$img" of="$FLASH_DEV" bs=2M conv=fsync; then
  440. sync
  441. echo "Image flashed sucessfully!"
  442. else
  443. echo "dd failed!"
  444. exit 1
  445. fi
  446. }
  447. sshUpgrade() {
  448. debug "${FUNCNAME[0]}"
  449. echo "Copying '$SYSUPGRADEIMGGZ' to $SSH_UPGRADE_PATH/tmp/"
  450. debug "scp \"$SYSUPGRADEIMGGZ\" \"$SSH_UPGRADE_PATH\":\"/tmp/$SYSUPGRADEIMGGZFNAME\""
  451. if ! scp "$SYSUPGRADEIMGGZ" "$SSH_UPGRADE_PATH":"/tmp/$SYSUPGRADEIMGGZFNAME"; then
  452. echo "Could not access the --ssh-upgrade PATH"
  453. exit 1
  454. fi
  455. echo "Executing remote sysupgrade"
  456. debug "ssh \"$SSH_UPGRADE_PATH\" \"sysupgrade -F /tmp/$SYSUPGRADEIMGGZFNAME\""
  457. # shellcheck disable=SC2029
  458. ssh "$SSH_UPGRADE_PATH" "sysupgrade -F /tmp/$SYSUPGRADEIMGGZFNAME"
  459. }
  460. fromSource() {
  461. debug "${FUNCNAME[0]}"
  462. declare src_url="https://github.com/openwrt/openwrt.git"
  463. declare pkg kopt
  464. echo "Building from source is under development"
  465. if [[ ! -d "$GITSRCDIR" ]]; then
  466. mkdir -p "$GITSRCDIR"
  467. git clone "$src_url" "$GITSRCDIR"
  468. fi
  469. pushd "$GITSRCDIR" &>/dev/null || return 1
  470. if [[ ${P_ARR[release]} == "snapshot" ]]; then
  471. git checkout master
  472. else
  473. git checkout "v$RELEASE"
  474. fi
  475. # Update package feed
  476. ./scripts/feeds update -a &&
  477. ./scripts/feeds install -a
  478. # Grab the release seed config
  479. if [[ -f "$SEED_FILE" ]]; then
  480. if askOk "$SEED_FILE exists. Re-download?"; then
  481. rm -f "$SEED_FILE"
  482. fi
  483. fi
  484. if [[ -f "$SEED_FILE" ]]; then
  485. echo "Reusing existing $SEED_FILE"
  486. else
  487. if ! curl -so "$SEED_FILE" "$SEED_URL"; then
  488. echo "Could not obtain seed config"
  489. fi
  490. fi
  491. # Override default .config seed with profile packages
  492. for pkg in ${P_ARR[packages]:+${P_ARR[packages]}}; do
  493. if [[ $pkg == -* ]]; then
  494. echo "CONFIG_PACKAGE_${pkg#-}=n" >> "$SEED_FILE" # remove package
  495. else
  496. echo "CONFIG_PACKAGE_$pkg=y" >> "$SEED_FILE" # add package
  497. fi
  498. done
  499. # Add custom kernel config options
  500. for kopt in ${P_ARR[kopts]:+${P_ARR[kopts]}}; do
  501. echo "$kopt" >> "$SEED_FILE"
  502. done
  503. make defconfig # normalize .config and remove dupes
  504. # Only compile our target
  505. # TODO causes a "configuration is out of sync" error w/o defconfig
  506. echo "CONFIG_TARGET_MULTI_PROFILE=n" >> "$SEED_FILE"
  507. sed -i '/CONFIG_TARGET_DEVICE_/d' "$SEED_FILE"
  508. echo "CONFIG_TARGET_DEVICE_${P_ARR[target]//\//_}_DEVICE_${P_ARR[profile]}=y" >> "$SEED_FILE"
  509. # output to bindir instead of builddir
  510. echo "CONFIG_BINARY_FOLDER=\"$THIS_BINDIR\"" >> "$SEED_FILE"
  511. make targetclean
  512. make download &&
  513. make -j"$(nproc)" world
  514. popd &>/dev/null || return 1
  515. exit # TODO exit here for fromSource() testing
  516. }
  517. debug() { (( DEBUG )) && echo "Debug: $*"; }
  518. askOk() {
  519. local _response
  520. read -r -p "$* [y/N]" _response
  521. _response=${_response,,}
  522. [[ "$_response" =~ ^(yes|y)$ ]]
  523. }
  524. resetAll() {
  525. debug "${FUNCNAME[0]}"
  526. askOk "Remove $SRCDIR and $BINDIR?" || exit $?
  527. debug "rm -rf $SRCDIR $BINDIR"
  528. rm -rf "$SRCDIR" "$BINDIR"
  529. }
  530. resetProfile() {
  531. debug "${FUNCNAME[0]}"
  532. askOk "Remove $BUILDDIR and $THIS_BINDIR?" || exit $?
  533. debug "rm -rf $BUILDDIR $THIS_BINDIR"
  534. rm -rf "$BUILDDIR" "$THIS_BINDIR"
  535. }
  536. loadProfiles() {
  537. debug "${FUNCNAME[0]}"
  538. declare -g PFILE
  539. # https://stackoverflow.com/a/4774063
  540. PFILE="$SCRIPTDIR/profiles"
  541. # shellcheck source=./profiles
  542. ! source "$PFILE" && echo "profiles file missing!" && return 1
  543. }
  544. verify() {
  545. debug "${FUNCNAME[0]}"
  546. declare file_to_check="$1"
  547. declare sumfile="$2"
  548. declare checksum
  549. hash sha256sum &>/dev/null || return 1
  550. [[ -f $sumfile && -f $file_to_check ]] || return 1
  551. checksum=$(grep "${file_to_check##*/}" "$sumfile" | cut -f1 -d' ')
  552. echo -n "$checksum $file_to_check" | sha256sum --check --status
  553. }
  554. main() {
  555. debug "${FUNCNAME[0]}"
  556. init
  557. loadProfiles
  558. readInput "$@"
  559. # Fallback to SCRIPTDIR if BUILDROOT has not been set
  560. declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
  561. [[ $BUILDROOT == "/" ]] && echo "Invalid --buildroot" && exit 1
  562. declare -g FILESDIR="${FILESDIR:=$BUILDROOT/src/files}"
  563. declare -g SRCDIR="$BUILDROOT/src" # input/build
  564. declare -g BINDIR="$BUILDROOT/bin" # output
  565. declare -g GITSRCDIR="$SRCDIR/openwrt"
  566. for dir in "$SRCDIR" "$BINDIR"; do
  567. [[ -d "$dir" ]] || mkdir -p "$dir"
  568. done
  569. # Allow --reset without a profile
  570. if [[ ${#PROFILES} -lt 1 ]]; then
  571. if (( RESET )); then
  572. resetAll
  573. exit
  574. else
  575. echo "No profile supplied" && return 1
  576. fi
  577. fi
  578. installDependencies
  579. for profile in "${PROFILES[@]}"; do
  580. debug "Starting profile: $profile"
  581. if [[ ! ${!profile@a} = A ]]; then
  582. echo "Profile '$profile' does not exist"
  583. return 1
  584. fi
  585. # Store profile settings in P_ARR
  586. declare -gn P_ARR="$profile"
  587. # release precedence: user input>profile>env>hardcode
  588. declare -g RELEASE="${USER_RELEASE:=${P_ARR[release]:=$RELEASE}}"
  589. declare -g BUILDDIR="$SRCDIR/${P_ARR[profile]}-$RELEASE"
  590. declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
  591. declare -g THIS_BINDIR="$BINDIR/${P_ARR[profile]}-$RELEASE"
  592. if [[ "$RELEASE" == "snapshot" ]]; then
  593. declare url_prefix="https://downloads.openwrt.org/snapshots/targets/${P_ARR[target]}"
  594. declare url_filename="openwrt-imagebuilder-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
  595. declare img_prefix="$BUILDDIR/openwrt-${P_ARR[target]//\//-}-${P_ARR[profile]}-$FILESYSTEM"
  596. else
  597. declare url_prefix="https://downloads.openwrt.org/releases/$RELEASE/targets/${P_ARR[target]}"
  598. declare url_filename="openwrt-imagebuilder-$RELEASE-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
  599. declare img_prefix="$BUILDDIR/openwrt-$RELEASE-${P_ARR[target]//\//-}-${P_ARR[profile]}-$FILESYSTEM"
  600. fi
  601. declare -g IB_URL="$url_prefix/$url_filename"
  602. declare -g IB_ARCHIVE="$SRCDIR/$url_filename"
  603. declare -g IB_SHA256_URL="$url_prefix/sha256sums"
  604. declare -g IB_SHA256_FILE="$BUILDDIR/sha256sums"
  605. declare -g IB_OUT_SHA256_FILE="$THIS_BINDIR/sha256sums"
  606. declare -g SEED_URL="$url_prefix/config.buildinfo"
  607. declare -g SEED_FILE="$GITSRCDIR/.config"
  608. declare -g FACTORYIMG="$img_prefix-factory.img"
  609. declare -g FACTORYIMGGZ="$img_prefix-factory.img.gz"
  610. declare -g FACTORYIMGGZFNAME="${FACTORYIMGGZ##*/}"
  611. declare -g SYSUPGRADEIMG="$img_prefix-sysupgrade.img"
  612. declare -g SYSUPGRADEIMGGZ="$img_prefix-sysupgrade.img.gz"
  613. declare -g SYSUPGRADEIMGGZFNAME="${SYSUPGRADEIMGGZ##*/}"
  614. if (( DEBUG )) || (( PROFILE_INFO )); then
  615. echo "Profile settings:"
  616. for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
  617. echo "Build settings:"
  618. cat <<- EOF
  619. ALIAS=$profile
  620. BUILDROOT=$BUILDROOT
  621. BUILDDIR=$BUILDDIR
  622. SRCDIR=$SRCDIR
  623. BINDIR=$BINDIR
  624. GITSRCDIR=$GITSRCDIR
  625. THIS_BINDIR=$THIS_BINDIR
  626. TARGET=${P_ARR[target]}
  627. PROFILE=${P_ARR[profile]}
  628. RELEASE=$RELEASE
  629. FILESYSTEM=$FILESYSTEM
  630. IB_URL=$IB_URL
  631. IB_ARCHIVE=$IB_ARCHIVE
  632. SEED_URL=$SEED_URL
  633. SEED_FILE=$SEED_FILE
  634. IB_SHA256_URL=$IB_SHA256_URL
  635. IB_SHA256_FILE=$IB_SHA256_FILE
  636. IB_OUT_SHA256_FILE=$IB_OUT_SHA256_FILE
  637. FACTORYIMGGZ: $FACTORYIMGGZ
  638. FACTORYIMGGZFNAME: $FACTORYIMGGZFNAME
  639. SYSUPGRADEIMGGZ: $SYSUPGRADEIMGGZ
  640. SYSUPGRADEIMGGZFNAME: $SYSUPGRADEIMGGZFNAME
  641. EOF
  642. fi
  643. (( RESET )) && resetProfile
  644. (( FROM_SOURCE )) && fromSource
  645. # Acquire and verify Image Builder
  646. getImageBuilder &&
  647. getImageBuilderChecksum &&
  648. verify "$IB_ARCHIVE" "$IB_SHA256_FILE" ||
  649. return $?
  650. extractImageBuilder || return $?
  651. addRepos
  652. #copyFiles
  653. [[ -v SSH_BACKUP_PATH ]] && sshBackup
  654. if makeImages && verifyImages; then
  655. [[ -v SSH_UPGRADE_PATH ]] && sshUpgrade
  656. [[ -v FLASH_DEV ]] && flashImage
  657. fi
  658. done
  659. }
  660. main "$@"
  661. exit