openwrtbuilder 18 KB

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