openwrtbuilder 25 KB

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