openwrtbuilder 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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
  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. }
  106. readInput() {
  107. debug "${FUNCNAME[0]}"
  108. unset RESET
  109. declare -ga PROFILES
  110. declare -g PROFILE_INFO
  111. declare long_opts
  112. long_opts='release:,version:,profile:,info:,list-profiles,buildroot:,'
  113. long_opts+='source,ssh-upgrade:,ssh-backup:,flash:,reset,debug,help'
  114. if _input=$(getopt -o +r:v:p:i:lb:sf:dh -l $long_opts -- "$@"); then
  115. eval set -- "$_input"
  116. while true; do
  117. case "$1" in
  118. --release|-r|--version|-v)
  119. shift && declare -g USER_RELEASE="$1"
  120. ;;
  121. --profile|-p)
  122. shift && PROFILES+=("$1")
  123. ;;
  124. --info|-i)
  125. PROFILE_INFO=1
  126. ;;
  127. --list-profiles|-l)
  128. listProfiles && exit $?
  129. ;;
  130. --buildroot|-b)
  131. shift && BUILDROOT="$1"
  132. ;;
  133. --source|-s)
  134. FROM_SOURCE=1
  135. ;;
  136. --ssh-upgrade)
  137. shift && SSH_UPGRADE_PATH="$1"
  138. ;;
  139. --ssh-backup)
  140. shift && SSH_BACKUP_PATH="$1"
  141. ;;
  142. --flash|-f)
  143. shift && FLASH_DEV="$1"
  144. ;;
  145. --reset)
  146. RESET=1
  147. ;;
  148. --debug|-d)
  149. echo "Debugging on"
  150. DEBUG=1
  151. ;;
  152. --help|-h)
  153. printHelp && exit 0
  154. ;;
  155. --)
  156. shift
  157. break
  158. ;;
  159. esac
  160. shift
  161. done
  162. else
  163. echo "Incorrect options provided"
  164. printHelp && exit 1
  165. fi
  166. }
  167. listProfiles() {
  168. debug "${FUNCNAME[0]}"
  169. grep "declare -Ag" "$PFILE" | cut -d" " -f3
  170. }
  171. installDependencies() {
  172. debug "${FUNCNAME[0]}"
  173. declare -a pkg_list
  174. # TODO please contribute your platform here
  175. if (( FROM_SOURCE )); then
  176. # For building from source with make
  177. # https://openwrt.org/docs/guide-developer/toolchain/install-buildsystem
  178. case "$ID" in
  179. fedora|centos)
  180. pkg_list+=(
  181. "bash-completion"
  182. "bzip2"
  183. "gcc"
  184. "gcc-c++"
  185. "git"
  186. "make"
  187. "ncurses-devel"
  188. "patch"
  189. "rsync"
  190. "tar"
  191. "unzip"
  192. "wget"
  193. "which"
  194. "diffutils"
  195. "python2"
  196. "python3"
  197. "perl-base"
  198. "perl-Data-Dumper"
  199. "perl-File-Compare"
  200. "perl-File-Copy"
  201. "perl-FindBin"
  202. "perl-Thread-Queue"
  203. )
  204. ;;
  205. debian|ubuntu)
  206. pkg_list+=(
  207. "build-essential"
  208. "clang"
  209. "flex"
  210. "g++"
  211. "gawk"
  212. "gcc-multilib"
  213. "gettext"
  214. "git"
  215. "libncurses5-dev"
  216. "libssl-dev"
  217. "python3-distutils"
  218. "rsync"
  219. "unzip"
  220. "zlib1g-dev"
  221. "file"
  222. "wget"
  223. )
  224. ;;
  225. arch)
  226. pkg_list+=(
  227. "base-devel"
  228. "autoconf"
  229. "automake"
  230. "bash"
  231. "binutils"
  232. "bison"
  233. "bzip2"
  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. declare dl_tool
  311. if [[ -f "$IB_ARCHIVE" ]]; then
  312. echo "Image Builder $IB_ARCHIVE exists"
  313. if askOk "Redownload Image Builder archive?"; then
  314. rm -f "$IB_ARCHIVE"
  315. else
  316. return 0
  317. fi
  318. fi
  319. if hash axel &>/dev/null; then
  320. dl_tool="axel"
  321. elif hash curl &>/dev/null; then
  322. dl_tool="curl"
  323. else
  324. echo "Downloading the Image Builder requires axel or curl!"
  325. return 1
  326. fi
  327. echo "Downloading Image Builder archive using $dl_tool"
  328. debug "$dl_tool -o $IB_ARCHIVE $IB_URL"
  329. if ! "$dl_tool" -o "$IB_ARCHIVE" "$IB_URL"; then
  330. echo "Could not download Image Builder archive"
  331. return 1
  332. fi
  333. if [[ ! -f "$IB_ARCHIVE" ]]; then
  334. echo "Archive missing"
  335. return 1
  336. fi
  337. # if hash sha256sum &>/dev/null; then
  338. # echo "Verifying checksums"
  339. # debug "$dl_tool -s "${P_ARR[sha256_url]}" | grep $filename | cut -f1 -d' '"
  340. # sha256sum=$($dl_tool -s "${P_ARR[sha256_url]}" |grep "$filename" |cut -f1 -d' ')
  341. # debug "Downloaded sha256sum: $sha256sum"
  342. # fi
  343. echo "Extracting Image Builder archive"
  344. [[ ! -d "$BUILDDIR" ]] && mkdir -p "$BUILDDIR"
  345. debug "tar -xf $IB_ARCHIVE -C $BUILDDIR --strip-components 1"
  346. if ! tar -xf "$IB_ARCHIVE" -C "$BUILDDIR" --strip-components 1; then
  347. echo "Extraction failed"
  348. return 1
  349. fi
  350. }
  351. addRepos() {
  352. debug "${FUNCNAME[0]}"
  353. if [[ -v P_ARR[repo] ]]; then
  354. if ! grep -q "${P_ARR[repo]}" "$BUILDDIR/repositories.conf"; then
  355. echo "${P_ARR[repo]}" >> "$BUILDDIR/repositories.conf"
  356. fi
  357. sed -i '/option check_signature/d' "$BUILDDIR/repositories.conf"
  358. fi
  359. }
  360. sshBackup() {
  361. debug "${FUNCNAME[0]}"
  362. local _date _hostname _backup_fname
  363. [[ -d "$FILESDIR" ]] || mkdir -p "$FILESDIR"
  364. printf -v _date '%(%Y-%m-%d-%H-%M-%S)T'
  365. _hostname=$(ssh -qt "$SSH_BACKUP_PATH" echo -n \$HOSTNAME)
  366. _backup_fname="backup-$_hostname-$_date.tar.gz"
  367. # Make backup archive on remote
  368. debug "ssh -t $SSH_BACKUP_PATH sysupgrade -b /tmp/$_backup_fname"
  369. if ! 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. debug "rsync -avz --remove-source-files $SSH_BACKUP_PATH:/tmp/$_backup_fname $BUILDDIR/"
  375. if ! rsync -avz --remove-source-files \
  376. "$SSH_BACKUP_PATH":"/tmp/$_backup_fname" "$BUILDDIR/"; then
  377. echo "Could not copy SSH backup"
  378. exit 1
  379. fi
  380. # Extract backup archive
  381. debug "tar -C $FILESDIR -xzf $BUILDDIR/$_backup_fname"
  382. if ! tar -C "$FILESDIR" -xzf "$BUILDDIR/$_backup_fname"; then
  383. echo "Could not extract SSH backup"
  384. exit 1
  385. fi
  386. rm "$BUILDDIR/$_backup_fname"
  387. }
  388. makeImage() {
  389. debug "${FUNCNAME[0]}"
  390. # Reuse the existing output
  391. if [[ -d "$BUILDDIR" ]]; then
  392. if askOk "$BUILDDIR exists. Rebuild?"; then
  393. rm -rf "$BUILDDIR"
  394. else
  395. return 0
  396. fi
  397. fi
  398. [[ ! -d "$BUILDDIR" ]] && mkdir -p "$BUILDDIR"
  399. if ! make image \
  400. BIN_DIR="$BINDIR" \
  401. PROFILE="${P_ARR[profile]}" \
  402. PACKAGES="${P_ARR[packages]}" \
  403. FILES="${FILESDIR}" \
  404. --directory="$BUILDDIR" \
  405. --jobs="$(nproc)" \
  406. > make.log; then
  407. echo "Make image failed!"
  408. exit 1
  409. fi
  410. }
  411. flashImage() {
  412. debug "${FUNCNAME[0]}"
  413. declare img img_gz partitions
  414. if [[ ! -e "$FLASH_DEV" ]]; then
  415. echo "The device specified by --flash could not be found"
  416. exit 1
  417. fi
  418. # TODO Roughly chooses the correct image
  419. if [[ -f "$FACTORYIMGGZ" ]]; then
  420. img_gz="$FACTORYIMGGZ"
  421. img="$FACTORYIMG"
  422. elif [[ -f "$SYSUPGRADEIMGGZ" ]]; then
  423. img_gz="$SYSUPGRADEIMGGZ"
  424. img="$SYSUPGRADEIMG"
  425. else
  426. return 1
  427. fi
  428. debug "$img_gz $img"
  429. debug "gunzip -qfk $img_gz"
  430. gunzip -qfk "$img_gz"
  431. echo "Unmounting target device $FLASH_DEV partitions"
  432. partitions=( "$FLASH_DEV"?* )
  433. debug "umount ${partitions[*]}"
  434. sudo umount "${partitions[@]}"
  435. debug "sudo dd if=\"$img\" of=\"$FLASH_DEV\" bs=2M conv=fsync"
  436. if sudo dd if="$img" of="$FLASH_DEV" bs=2M conv=fsync; then
  437. sync
  438. echo "Image flashed sucessfully!"
  439. else
  440. echo "dd failed!"
  441. exit 1
  442. fi
  443. }
  444. sshUpgrade() {
  445. debug "${FUNCNAME[0]}"
  446. echo "Copying '$SYSUPGRADEIMGGZ' to $SSH_UPGRADE_PATH/tmp/"
  447. debug "scp \"$SYSUPGRADEIMGGZ\" \"$SSH_UPGRADE_PATH\":\"/tmp/$SYSUPGRADEIMGGZFNAME\""
  448. if ! scp "$SYSUPGRADEIMGGZ" "$SSH_UPGRADE_PATH":"/tmp/$SYSUPGRADEIMGGZFNAME"; then
  449. echo "Could not access the --ssh-upgrade PATH"
  450. exit 1
  451. fi
  452. echo "Executing remote sysupgrade"
  453. debug "ssh \"$SSH_UPGRADE_PATH\" \"sysupgrade -F /tmp/$SYSUPGRADEIMGGZFNAME\""
  454. # shellcheck disable=SC2029
  455. ssh "$SSH_UPGRADE_PATH" "sysupgrade -F /tmp/$SYSUPGRADEIMGGZFNAME"
  456. }
  457. fromSource() {
  458. debug "${FUNCNAME[0]}"
  459. declare src_url="https://github.com/openwrt/openwrt.git"
  460. declare src_dir="$SRCDIR/openwrt"
  461. declare -a pkg_list
  462. echo "Building from source is under development"
  463. if [[ ! -d "$src_dir" ]]; then
  464. mkdir -p "$src_dir"
  465. git clone "$src_url" "$src_dir"
  466. fi
  467. pushd "$src_dir" || return 1
  468. if [[ ${P_ARR[release]} == "snapshot" ]]; then
  469. git checkout master
  470. else
  471. git checkout "v$RELEASE"
  472. fi
  473. ./scripts/feeds update -a
  474. ./scripts/feeds install -a
  475. # Grab the release config.seed
  476. k_options=$(curl -s "$SEED_URL")
  477. debug "$k_options"
  478. make distclean
  479. make download
  480. make -j"$(nproc)" world
  481. popd || return 1
  482. exit # TODO exit here for fromSource() testing
  483. }
  484. debug() { (( DEBUG )) && echo "Debug: $*"; }
  485. askOk() {
  486. local _response
  487. read -r -p "$* [y/N]" _response
  488. _response=${_response,,}
  489. [[ "$_response" =~ ^(yes|y)$ ]]
  490. }
  491. resetAll() {
  492. debug "${FUNCNAME[0]}"
  493. askOk "Remove $SRCDIR and $BINDIR?" || exit $?
  494. debug "rm -rf $SRCDIR $BINDIR"
  495. rm -rf "$SRCDIR" "$BINDIR"
  496. }
  497. resetProfile() {
  498. debug "${FUNCNAME[0]}"
  499. askOk "Remove $BUILDDIR?" || exit $?
  500. debug "rm -rf $BUILDDIR"
  501. rm -rf "$BUILDDIR"
  502. }
  503. loadProfiles() {
  504. debug "${FUNCNAME[0]}"
  505. declare -g PFILE
  506. # https://stackoverflow.com/a/4774063
  507. PFILE="$SCRIPTDIR/profiles"
  508. # shellcheck source=./profiles
  509. ! source "$PFILE" && echo "profiles file missing!" && return 1
  510. }
  511. main() {
  512. debug "${FUNCNAME[0]}"
  513. init
  514. loadProfiles
  515. readInput "$@"
  516. # Fallback to SCRIPTDIR if BUILDROOT has not been set
  517. declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
  518. [[ $BUILDROOT == "/" ]] && echo "Invalid --buildroot" && exit 1
  519. declare -g FILESDIR="${FILESDIR:=$BUILDROOT/src/files}"
  520. declare -g SRCDIR="$BUILDROOT/src" # input/build
  521. declare -g BINDIR="$BUILDROOT/bin" # output
  522. for dir in "$SRCDIR" "$BINDIR"; do
  523. [[ -d "$dir" ]] || mkdir -p "$dir"
  524. done
  525. # Allow --reset without a profile
  526. if [[ ${#PROFILES} -lt 1 ]]; then
  527. if (( RESET )); then
  528. resetAll
  529. exit
  530. else
  531. echo "No profile supplied" && return 1
  532. fi
  533. fi
  534. installDependencies
  535. for profile in "${PROFILES[@]}"; do
  536. debug "Starting profile: $profile"
  537. if [[ ! ${!profile@a} = A ]]; then
  538. echo "Profile '$profile' does not exist"
  539. return 1
  540. fi
  541. # Store profile settings in P_ARR
  542. declare -gn P_ARR="$profile"
  543. # release precedence: user input>profile>env>hardcode
  544. declare -g RELEASE="${USER_RELEASE:=${P_ARR[release]:=$RELEASE}}"
  545. declare -g BUILDDIR="$SRCDIR/${P_ARR[profile]}-$RELEASE"
  546. declare -g IB_ARCHIVE="$SRCDIR/${P_ARR[profile]}-$RELEASE.tar.xz"
  547. declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
  548. if [[ "$RELEASE" == "snapshot" ]]; then
  549. declare url_prefix="https://downloads.openwrt.org/snapshots/targets/${P_ARR[target]}"
  550. declare url_filename="openwrt-imagebuilder-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
  551. declare img_prefix="$BUILDDIR/openwrt-${P_ARR[target]//\//-}-${P_ARR[profile]}-$FILESYSTEM"
  552. else
  553. declare url_prefix="https://downloads.openwrt.org/releases/$RELEASE/targets/${P_ARR[target]}"
  554. declare url_filename="openwrt-imagebuilder-$RELEASE-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
  555. declare img_prefix="$BUILDDIR/openwrt-$RELEASE-${P_ARR[target]//\//-}-${P_ARR[profile]}-$FILESYSTEM"
  556. fi
  557. declare -g IB_URL="$url_prefix/$url_filename"
  558. declare -g SHA256_URL="$url_prefix/sha256sums"
  559. declare -g SEED_URL="$url_prefix/config.buildinfo"
  560. declare -g FACTORYIMG="$img_prefix-factory.img"
  561. declare -g FACTORYIMGGZ="$img_prefix-factory.img.gz"
  562. declare -g SYSUPGRADEIMG="$img_prefix-sysupgrade.img"
  563. declare -g SYSUPGRADEIMGGZ="$img_prefix-sysupgrade.img.gz"
  564. declare -g SYSUPGRADEIMGGZFNAME="${SYSUPGRADEIMGGZ##*/}"
  565. if (( DEBUG )) || (( PROFILE_INFO )); then
  566. echo "Profile settings:"
  567. for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
  568. echo "Build settings:"
  569. cat <<- EOF
  570. ALIAS: $profile
  571. BUILDDIR: $BUILDDIR
  572. TARGET: ${P_ARR[target]}
  573. PROFILE: ${P_ARR[profile]}
  574. RELEASE: $RELEASE
  575. FILESYSTEM: $FILESYSTEM
  576. IB_URL: $IB_URL
  577. IB_ARCHIVE: $IB_ARCHIVE
  578. EOF
  579. fi
  580. (( RESET )) && resetProfile
  581. (( FROM_SOURCE )) && fromSource
  582. getImageBuilder || return $?
  583. addRepos
  584. #copyFiles
  585. [[ -v SSH_BACKUP_PATH ]] && sshBackup
  586. if makeImage; then
  587. [[ -v SSH_UPGRADE_PATH ]] && sshUpgrade
  588. [[ -v FLASH_DEV ]] && flashImage
  589. fi
  590. done
  591. }
  592. main "$@"
  593. exit