openwrtBuild 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #!/usr/bin/env bash
  2. #
  3. # This script/function will build and flash/upgrade OpenWRT based on user-defined custom profiles
  4. # For Fedora/Debian/Ubuntu only
  5. #
  6. # MIT License
  7. # Copyright (c) 2020 Bryan Roessler
  8. # Permission is hereby granted, free of charge, to any person obtaining a copy
  9. # of this software and associated documentation files (the "Software"), to deal
  10. # in the Software without restriction, including without limitation the rights
  11. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the Software is
  13. # furnished to do so, subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included in all
  16. # copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25. printHelpAndExit() {
  26. debug "${FUNCNAME[0]}"
  27. cat <<-'EOF'
  28. USAGE:
  29. openwrtBuild [[OPTION] [VALUE]]...
  30. If PROFILE is set and TARGET is not, buildOpenwrt can use a custom profile specified in DEFAULTS
  31. OPTIONS
  32. --profile, -p PROFILE
  33. --version, -v OPENWRT_VERSION
  34. --builddir, -b PATH
  35. --ssh-upgrade SSH_PATH
  36. Example: root@192.168.1.1
  37. --ssh-backup SSH_PATH
  38. Enabled by default for --ssh-upgrade
  39. --flash, -f DEVICE
  40. Example: /dev/sdX
  41. --debug, -d
  42. --help, -h
  43. EOF
  44. # Exit using passed exit code
  45. [[ -z $1 ]] && exit 0 || exit "$1"
  46. }
  47. setDefaults() {
  48. debug "${FUNCNAME[0]}"
  49. export _target _factory_suffix _sysupgrade_suffix _profile _builddir _debug _filesroot
  50. declare -ag _packages
  51. [[ -z $_debug ]] && _debug="false" # Set to true to enable debugging by default
  52. [[ -z $_builddir ]] && _builddir="$PWD"
  53. [[ -z $_filesroot ]] && _filesroot="$_builddir/files/"
  54. # Additional packages for all profiles
  55. _packages+=("luci" "nano" "htop" "tcpdump" "diffutils" "tar" "iperf")
  56. # If no profile is specified, use the TP-Link Archer C7 v2 dumb AP
  57. [[ -z $_profile ]] && _profile="tplink_archer-c7-v2"
  58. # Custom profiles
  59. # TP-Link Archer C7v2 WAP (dumb AP) w/ legacy drivers for better performance
  60. if [[ "$_profile" == "tplink_archer-c7-v2" ]]; then
  61. [[ -z $_version ]] && _version="21.02.0-rc1"
  62. _target="ath79/generic"
  63. _factory_suffix="squashfs-factory.bin"
  64. _sysupgrade_suffix="squashfs-sysupgrade.bin"
  65. _packages+=("-dnsmasq" \
  66. "-odhcpd" \
  67. "-iptables" \
  68. "-ath10k-firmware-qca988x-ct" \
  69. "-kmod-ath10k-ct" \
  70. "ath10k-firmware-qca988x" \
  71. "kmod-ath10k")
  72. # Raspberry Pi 4B router with USB->Ethernet dongle
  73. elif [[ "$_profile" == "rpi-4" ]]; then
  74. [[ -z $_version ]] && _version="21.02.0-rc1"
  75. _target="bcm27xx/bcm2711"
  76. _factory_suffix="ext4-factory.img"
  77. _sysupgrade_suffix="ext4-sysupgrade.img"
  78. _packages+=("kmod-usb-net-asix-ax88179" \
  79. "kmod-usb-net-rtl8152" \
  80. "luci-app-upnp" \
  81. "luci-app-wireguard" \
  82. "luci-app-vpn-policy-routing" \
  83. "-dnsmasq" \
  84. "dnsmasq-full" \
  85. "luci-app-ddns" \
  86. "luci-app-sqm")
  87. # NanoPi R2S router
  88. elif [[ "$_profile" == "r2s" ]]; then
  89. [[ -z $_version ]] && _version="21.02.0-rc1"
  90. _profile="friendlyarm_nanopi-r2s"
  91. _target="rockchip/armv8"
  92. _factory_suffix="ext4-factory.img"
  93. _sysupgrade_suffix="ext4-sysupgrade.img"
  94. _packages+=("luci-app-upnp" \
  95. "luci-app-wireguard" \
  96. "luci-app-vpn-policy-routing" \
  97. "-dnsmasq" \
  98. "dnsmasq-full" \
  99. "luci-app-ddns" \
  100. "luci-app-sqm" \
  101. "luci-app-statistics" \
  102. "collectd-mod-sensors" \
  103. "collectd-mod-thermal" \
  104. "lm-sensors")
  105. fi
  106. }
  107. parseInput() {
  108. debug "${FUNCNAME[0]}"
  109. if _input=$(getopt -o +v:p:b:f:dh -l version:,profile:,builddir:,ssh-upgrade:,ssh-backup:,flash:,debug,help -- "$@"); then
  110. eval set -- "$_input"
  111. while true; do
  112. case "$1" in
  113. --version|-v)
  114. shift && _version="$1"
  115. ;;
  116. --profile|-p)
  117. shift && _profile="$1"
  118. ;;
  119. --builddir|-b)
  120. shift && _builddir="$1"
  121. ;;
  122. --ssh-upgrade)
  123. shift && _ssh_upgrade_path="$1"
  124. ;;
  125. --ssh-backup)
  126. shift && _ssh_backup_path="$1"
  127. ;;
  128. --flash|-f)
  129. shift && _flash_dev="$1"
  130. ;;
  131. --debug|-d)
  132. echo "Debugging on"
  133. _debug="true"
  134. ;;
  135. --help|-h)
  136. printHelpAndExit 0
  137. ;;
  138. --)
  139. shift
  140. break
  141. ;;
  142. esac
  143. shift
  144. done
  145. else
  146. echo "Incorrect options provided"
  147. printHelpAndExit 1
  148. fi
  149. }
  150. debug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
  151. setVars() {
  152. debug "${FUNCNAME[0]}"
  153. getOS () {
  154. debug "${FUNCNAME[0]}"
  155. if [[ -f /etc/os-release ]]; then
  156. # shellcheck disable=SC1091
  157. source /etc/os-release
  158. export ID="$ID"
  159. echo "Detected platform: $ID"
  160. else
  161. echo "Cannot detect OS!"
  162. exit 1
  163. fi
  164. }
  165. getOS
  166. export _source_archive="$_builddir/sources/$_profile-$_version.tar.xz"
  167. export _source_dir="${_source_archive%.tar.xz}"
  168. export _out_bin_dir="$_builddir/bin/$_profile-$_version/"
  169. if [[ "$_version" == "snapshot" ]]; then
  170. local _out_prefix="$_out_bin_dir/openwrt-${_target//\//-}-$_profile"
  171. else
  172. local _out_prefix="$_out_bin_dir/openwrt-$_version-${_target//\//-}-$_profile"
  173. fi
  174. export _factory_bin="$_out_prefix-$_factory_suffix"
  175. export _factory_bin_fname="${_factory_bin##*/}"
  176. export _factory_bin_gz="$_factory_bin.gz"
  177. export _factory_bin_gz_fname="${_factory_bin_gz##*/}"
  178. export _sysupgrade_bin="$_out_prefix-$_sysupgrade_suffix"
  179. export _sysupgrade_bin_fname="${_sysupgrade_bin##*/}"
  180. export _sysupgrade_bin_gz="$_sysupgrade_bin.gz"
  181. export _sysupgrade_bin_gz_fname="${_sysupgrade_bin_gz##*/}"
  182. }
  183. installPrerequisites() {
  184. debug "${FUNCNAME[0]}"
  185. local -a _pkg_list
  186. local _pkg_cmd
  187. if [[ "$ID" == "fedora" ]]; then
  188. _pkg_list=("@c-development" "@development-tools" "@development-libs" "perl-FindBin" "zlib-static" "elfutils-libelf-devel" "gawk" "unzip" "file" "wget" "python3" "python2" "axel")
  189. _pkg_cmd="dnf"
  190. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  191. _pkg_list=("build-essential" "libncurses5-dev" "libncursesw5-dev" "zlib1g-dev" "gawk" "git" "gettext" "libssl-dev" "xsltproc" "wget" "unzip" "python" "axel")
  192. _pkg_cmd="apt-get"
  193. fi
  194. echo "Installing dependencies"
  195. debug "sudo $_pkg_cmd -y install ${_pkg_list[*]}"
  196. if ! sudo "$_pkg_cmd" -y install "${_pkg_list[@]}" > /dev/null 2>&1; then
  197. echo "Warning: Problem installing prerequisites"
  198. return 1
  199. fi
  200. }
  201. acquireImageBuilder() {
  202. debug "${FUNCNAME[0]}"
  203. local _url _filename
  204. if [[ "$_version" == "snapshot" ]]; then
  205. # Remove existing ImageBuilders
  206. [[ -f "$_source_archive" ]] && rm "$_source_archive"
  207. _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
  208. _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
  209. else
  210. # Reuse existing ImageBuilders
  211. [[ -f "$_source_archive" ]] && return 0
  212. _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
  213. _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
  214. fi
  215. # Make sources directory if it does not exist
  216. [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
  217. echo "Downloading image archive"
  218. debug "axel -o $_source_archive $_url"
  219. if ! axel -o "$_source_archive" "$_url" > /dev/null 2>&1; then
  220. echo "Could not download Image Builder"
  221. exit 1
  222. fi
  223. }
  224. extractImageBuilder() {
  225. debug "${FUNCNAME[0]}"
  226. [[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir"
  227. if [[ ! -f "$_source_archive" ]]; then
  228. echo "Archive missing"
  229. exit 1
  230. fi
  231. echo "Extracting image archive"
  232. debug "tar -xf $_source_archive -C $_source_dir --strip-components 1"
  233. if ! tar -xf "$_source_archive" -C "$_source_dir" --strip-components 1; then
  234. echo "Extraction failed"
  235. exit 1
  236. fi
  237. }
  238. makeImage() {
  239. debug "${FUNCNAME[0]}"
  240. # move to extracted source directory
  241. if ! pushd "$_source_dir" > /dev/null 2>&1; then
  242. exit 1
  243. fi
  244. # Make bin dir
  245. [[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir"
  246. # build image
  247. echo "Running make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot"
  248. debug "make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot > make.log"
  249. if ! make image BIN_DIR="$_out_bin_dir" PROFILE="$_profile" PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then
  250. echo "Make image failed!"
  251. exit 1
  252. fi
  253. if ! popd > /dev/null 2>&1; then
  254. exit 1
  255. fi
  256. }
  257. extractImage() {
  258. debug "${FUNCNAME[0]}" "$@"
  259. local _gz
  260. [[ $# -lt 1 ]] && echo "extractImage() requires at least one argument" && exit 1
  261. for _gz in "$@"; do
  262. [[ ! -f "$_gz" ]] && return 1
  263. debug "gunzip -qfk $_gz"
  264. if ! gunzip -qfk "$_gz"; then
  265. echo "$_gz extraction failed!"
  266. fi
  267. done
  268. }
  269. flashImage() {
  270. debug "${FUNCNAME[0]}"
  271. if [[ -z $_factory_bin && -f "$_factory_bin_gz" ]]; then
  272. extractImage "$_factory_bin_gz"
  273. fi
  274. if [[ ! -e "$_flash_dev" ]]; then
  275. echo "The device specified by --flash could not be found"
  276. exit 1
  277. fi
  278. echo "Unmounting target device $_flash_dev partitions"
  279. debug "umount $_flash_dev?*"
  280. sudo umount "$_flash_dev?*"
  281. debug "sudo dd if=\"$_factory_bin\" of=\"$_flash_dev\" bs=2M conv=fsync"
  282. if sudo dd if="$_factory_bin" of="$_flash_dev" bs=2M conv=fsync; then
  283. sync
  284. echo "Image flashed sucessfully!"
  285. else
  286. echo "dd failed!"
  287. exit 1
  288. fi
  289. }
  290. sshBackup() {
  291. debug "${FUNCNAME[0]}"
  292. local _source="$1"
  293. local _random="$RANDOM"
  294. if ! ssh -t "$_source" "sysupgrade -b /tmp/backup-${_random}.tar.gz"; then
  295. echo "SSH backup failed"
  296. exit 1
  297. fi
  298. if ! scp "$_source":/tmp/backup-"${_random}".tar.gz "$_builddir"; then
  299. echo "Could not copy SSH backup"
  300. exit 1
  301. fi
  302. if ! ssh -t "$_source" "rm -f /tmp/backup-${_random}.tar.gz"; then
  303. echo "Could not remove /tmp/backup-${_random}.tar.gz from $_source"
  304. fi
  305. [[ -d "$_filesroot" ]] && rm -rf "$_filesroot"
  306. mkdir -p "$_filesroot"
  307. if ! tar xzf "$_builddir/backup-${_random}.tar.gz" etc/ -C "$_filesroot"; then
  308. "Could not extract SSH backup"
  309. exit 1
  310. fi
  311. rm "$_builddir/backup-${_random}.tar.gz"
  312. }
  313. sshUpgrade() {
  314. debug "${FUNCNAME[0]}"
  315. if [[ -f "$_sysupgrade_bin_gz" ]]; then
  316. local _source="$_sysupgrade_bin_gz"
  317. local _source_fname="$_sysupgrade_bin_gz_fname"
  318. elif [[ -f "$_sysupgrade_bin" ]]; then
  319. local _source="$_sysupgrade_bin"
  320. local _source_fname="$_sysupgrade_bin_fname"
  321. else
  322. echo "Could not find upgrade file"
  323. exit 1
  324. fi
  325. echo "Copying \"$_source\" to $_ssh_upgrade_path/tmp/"
  326. debug "scp \"$_source\" \"$_ssh_upgrade_path\":\"/tmp/$_source_fname\""
  327. # shellcheck disable=SC2140
  328. if ! scp "$_source" "$_ssh_upgrade_path":"/tmp/$_source_fname"; then
  329. echo "Could not access the --ssh-upgrade PATH"
  330. exit 1
  331. fi
  332. echo "Executing remote sysupgrade"
  333. debug "ssh \"$_ssh_upgrade_path\" \"sysupgrade -F /tmp/$_source_fname\""
  334. # shellcheck disable=SC2029
  335. ssh "$_ssh_upgrade_path" "sysupgrade -F /tmp/$_source_fname"
  336. }
  337. __main() {
  338. parseInput "$@"
  339. setDefaults
  340. setVars
  341. installPrerequisites
  342. acquireImageBuilder
  343. extractImageBuilder
  344. rm -rf "$_ssh_backup_path"
  345. [[ -v _ssh_backup_path ]] && sshBackup "$_ssh_backup_path"
  346. if makeImage; then
  347. [[ -n $_ssh_upgrade_path ]] && sshUpgrade
  348. [[ -n $_flash_dev ]] && flashImage
  349. fi
  350. }
  351. __main "$@"
  352. exit $?