openwrtBuild 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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" "luci-ssl" "nano" "htop" "tcpdump" "diffutils" "tar" "iperf")
  56. # Exit if no profile specified
  57. [[ -z $_profile ]] && echo "You must specify a target profile (device)" && printHelpAndExit 1
  58. # By default use latest release
  59. [[ -z $_version ]] && _version="21.02.1"
  60. # Custom profiles
  61. # TP-Link Archer C7v2 WAP (dumb AP) w/ legacy drivers for better performance
  62. if [[ "$_profile" == "archer" ]]; then
  63. _profile="tplink_archer-c7-v2"
  64. _target="ath79/generic"
  65. _filesystem="squashfs"
  66. _packages+=("-dnsmasq" \
  67. "-odhcpd" \
  68. "-iptables" \
  69. "-ath10k-firmware-qca988x-ct" \
  70. "ath10k-firmware-qca988x-ct-full-htt")
  71. # Linksys EA8300 (dumb AP)
  72. elif [[ "$_profile" == "linksys" ]]; then
  73. _profile="linksys_ea8300"
  74. _target="ipq40xx/generic"
  75. _filesystem="squashfs"
  76. _packages+=("-dnsmasq" \
  77. "-odhcpd" \
  78. "-iptables" \
  79. )
  80. # Raspberry Pi 4B router with USB->Ethernet dongle
  81. elif [[ "$_profile" == "rpi-4" ]]; then
  82. _target="bcm27xx/bcm2711"
  83. _filesystem="ext4"
  84. _packages+=("kmod-usb-net-asix-ax88179" \
  85. "kmod-usb-net-rtl8152" \
  86. "luci-app-upnp" \
  87. "luci-app-wireguard" \
  88. "luci-app-vpn-policy-routing" \
  89. "-dnsmasq" \
  90. "dnsmasq-full" \
  91. "luci-app-ddns" \
  92. "luci-app-sqm")
  93. # NanoPi R2S router
  94. elif [[ "$_profile" == "r2s" ]]; then
  95. _profile="friendlyarm_nanopi-r2s"
  96. _target="rockchip/armv8"
  97. _filesystem="ext4"
  98. _packages+=("luci-app-upnp" \
  99. "luci-app-wireguard" \
  100. "luci-app-vpn-policy-routing" \
  101. "-dnsmasq" \
  102. "dnsmasq-full" \
  103. "luci-app-ddns" \
  104. "luci-app-sqm" \
  105. "luci-app-statistics" \
  106. "collectd-mod-sensors" \
  107. "collectd-mod-thermal" \
  108. "collectd-mod-conntrack" \
  109. "smcroute" \
  110. "curl" \
  111. "ethtool")
  112. elif [[ "$_profile" == "r4s" ]]; then
  113. _version="snapshot"
  114. _profile="friendlyarm_nanopi-r4s"
  115. _target="rockchip/armv8"
  116. _filesystem="ext4"
  117. _packages+=("luci-app-upnp" \
  118. "luci-app-wireguard" \
  119. "luci-app-vpn-policy-routing" \
  120. "-dnsmasq" \
  121. "dnsmasq-full" \
  122. "luci-app-ddns" \
  123. "luci-app-sqm" \
  124. "luci-app-statistics" \
  125. "collectd-mod-sensors" \
  126. "collectd-mod-thermal" \
  127. "collectd-mod-conntrack" \
  128. "smcroute" \
  129. "curl" \
  130. "ethtool")
  131. fi
  132. }
  133. parseInput() {
  134. debug "${FUNCNAME[0]}"
  135. if _input=$(getopt -o +v:p:b:f:dh -l version:,profile:,builddir:,ssh-upgrade:,ssh-backup:,flash:,debug,help -- "$@"); then
  136. eval set -- "$_input"
  137. while true; do
  138. case "$1" in
  139. --version|-v)
  140. shift && _version="$1"
  141. ;;
  142. --profile|-p)
  143. shift && _profile="$1"
  144. ;;
  145. --builddir|-b)
  146. shift && _builddir="$1"
  147. ;;
  148. --ssh-upgrade)
  149. shift && _ssh_upgrade_path="$1"
  150. ;;
  151. --ssh-backup)
  152. shift && _ssh_backup_path="$1"
  153. ;;
  154. --flash|-f)
  155. shift && _flash_dev="$1"
  156. ;;
  157. --debug|-d)
  158. echo "Debugging on"
  159. _debug="true"
  160. ;;
  161. --help|-h)
  162. printHelpAndExit 0
  163. ;;
  164. --)
  165. shift
  166. break
  167. ;;
  168. esac
  169. shift
  170. done
  171. else
  172. echo "Incorrect options provided"
  173. printHelpAndExit 1
  174. fi
  175. }
  176. debug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
  177. setVars() {
  178. debug "${FUNCNAME[0]}"
  179. getOS () {
  180. debug "${FUNCNAME[0]}"
  181. if [[ -f /etc/os-release ]]; then
  182. # shellcheck disable=SC1091
  183. source /etc/os-release
  184. export ID="$ID"
  185. echo "Detected platform: $ID"
  186. else
  187. echo "Cannot detect OS!"
  188. exit 1
  189. fi
  190. }
  191. getOS
  192. export _source_archive="$_builddir/sources/$_profile-$_version.tar.xz"
  193. export _source_dir="${_source_archive%.tar.xz}"
  194. export _out_bin_dir="$_builddir/bin/$_profile-$_version/"
  195. export _patches_dir="$_builddir/patches/"
  196. export _files_dir="$_builddir/files/"
  197. if [[ "$_version" == "snapshot" ]]; then
  198. local _out_prefix="$_out_bin_dir/openwrt-${_target//\//-}-$_profile"
  199. else
  200. local _out_prefix="$_out_bin_dir/openwrt-$_version-${_target//\//-}-$_profile"
  201. fi
  202. export _factory_bin="$_out_prefix-$_filesystem-factory.bin"
  203. export _factory_bin_fname="${_factory_bin##*/}"
  204. export _factory_bin_gz="$_factory_bin.gz"
  205. export _factory_bin_gz_fname="${_factory_bin_gz##*/}"
  206. export _sysupgrade_bin="$_out_prefix-$_filesystem-sysupgrade.bin"
  207. export _sysupgrade_bin_fname="${_sysupgrade_bin##*/}"
  208. export _sysupgrade_bin_gz="$_sysupgrade_bin.gz"
  209. export _sysupgrade_bin_gz_fname="${_sysupgrade_bin_gz##*/}"
  210. }
  211. installPrerequisites() {
  212. debug "${FUNCNAME[0]}"
  213. local -a _pkg_list
  214. local _pkg_cmd
  215. if [[ "$ID" == "fedora" ]]; then
  216. _pkg_list=("@c-development" "@development-tools" "@development-libs" "perl-FindBin" "zlib-static" "elfutils-libelf-devel" "gawk" "unzip" "file" "wget" "python3" "python2" "axel")
  217. _pkg_cmd="dnf"
  218. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  219. _pkg_list=("build-essential" "libncurses5-dev" "libncursesw5-dev" "zlib1g-dev" "gawk" "git" "gettext" "libssl-dev" "xsltproc" "wget" "unzip" "python" "axel")
  220. _pkg_cmd="apt-get"
  221. fi
  222. echo "Installing dependencies"
  223. debug "sudo $_pkg_cmd -y install ${_pkg_list[*]}"
  224. if ! sudo "$_pkg_cmd" -y install "${_pkg_list[@]}" > /dev/null 2>&1; then
  225. echo "Warning: Problem installing prerequisites"
  226. return 1
  227. fi
  228. }
  229. acquireImageBuilder() {
  230. debug "${FUNCNAME[0]}"
  231. local _url _filename
  232. if [[ "$_version" == "snapshot" ]]; then
  233. # Remove existing ImageBuilders
  234. [[ -f "$_source_archive" ]] && rm "$_source_archive"
  235. _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
  236. _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
  237. else
  238. # Reuse existing ImageBuilders
  239. [[ -f "$_source_archive" ]] && return 0
  240. _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
  241. _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
  242. fi
  243. # Make sources directory if it does not exist
  244. [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
  245. echo "Downloading image archive"
  246. debug "axel -o $_source_archive $_url"
  247. if ! axel -o "$_source_archive" "$_url" > /dev/null 2>&1; then
  248. echo "Could not download Image Builder"
  249. exit 1
  250. fi
  251. }
  252. extractImageBuilder() {
  253. debug "${FUNCNAME[0]}"
  254. [[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir"
  255. if [[ ! -f "$_source_archive" ]]; then
  256. echo "Archive missing"
  257. exit 1
  258. fi
  259. echo "Extracting image archive"
  260. debug "tar -xf $_source_archive -C $_source_dir --strip-components 1"
  261. if ! tar -xf "$_source_archive" -C "$_source_dir" --strip-components 1; then
  262. echo "Extraction failed"
  263. exit 1
  264. fi
  265. }
  266. # copyFiles() {
  267. # debug "${FUNCNAME[0]}"
  268. # declare -l _this_files_dir="$_files_dir/$_profile"
  269. # [[ ! -d "$_files_dir" ]] && return
  270. # $_profile == "r2s"
  271. # }
  272. makeImage() {
  273. debug "${FUNCNAME[0]}"
  274. # move to extracted source directory
  275. if ! pushd "$_source_dir" > /dev/null 2>&1; then
  276. exit 1
  277. fi
  278. # Make bin dir
  279. [[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir"
  280. # build image
  281. echo "Running make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot"
  282. debug "make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot > make.log"
  283. if ! make image BIN_DIR="$_out_bin_dir" PROFILE="$_profile" PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then
  284. echo "Make image failed!"
  285. exit 1
  286. fi
  287. if ! popd > /dev/null 2>&1; then
  288. exit 1
  289. fi
  290. }
  291. extractImage() {
  292. debug "${FUNCNAME[0]}" "$@"
  293. local _gz
  294. [[ $# -lt 1 ]] && echo "extractImage() requires at least one argument" && exit 1
  295. for _gz in "$@"; do
  296. [[ ! -f "$_gz" ]] && return 1
  297. debug "gunzip -qfk $_gz"
  298. if ! gunzip -qfk "$_gz"; then
  299. echo "$_gz extraction failed!"
  300. fi
  301. done
  302. }
  303. flashImage() {
  304. debug "${FUNCNAME[0]}"
  305. if [[ -z $_factory_bin && -f "$_factory_bin_gz" ]]; then
  306. extractImage "$_factory_bin_gz"
  307. fi
  308. if [[ ! -e "$_flash_dev" ]]; then
  309. echo "The device specified by --flash could not be found"
  310. exit 1
  311. fi
  312. echo "Unmounting target device $_flash_dev partitions"
  313. debug "umount $_flash_dev?*"
  314. sudo umount "$_flash_dev"?*
  315. debug "sudo dd if=\"$_factory_bin\" of=\"$_flash_dev\" bs=2M conv=fsync"
  316. if sudo dd if="$_factory_bin" of="$_flash_dev" bs=2M conv=fsync; then
  317. sync
  318. echo "Image flashed sucessfully!"
  319. else
  320. echo "dd failed!"
  321. exit 1
  322. fi
  323. }
  324. sshBackup() {
  325. debug "${FUNCNAME[0]}"
  326. local _source="$1"
  327. local _random="$RANDOM"
  328. if ! ssh -t "$_source" "sysupgrade -b /tmp/backup-${_random}.tar.gz"; then
  329. echo "SSH backup failed"
  330. exit 1
  331. fi
  332. if ! scp "$_source":/tmp/backup-"${_random}".tar.gz "$_builddir"; then
  333. echo "Could not copy SSH backup"
  334. exit 1
  335. fi
  336. if ! ssh -t "$_source" "rm -f /tmp/backup-${_random}.tar.gz"; then
  337. echo "Could not remove /tmp/backup-${_random}.tar.gz from $_source"
  338. fi
  339. [[ -d "$_filesroot" ]] && rm -rf "$_filesroot"
  340. mkdir -p "$_filesroot"
  341. if ! tar xzf "$_builddir/backup-${_random}.tar.gz" etc/ -C "$_filesroot"; then
  342. "Could not extract SSH backup"
  343. exit 1
  344. fi
  345. rm "$_builddir/backup-${_random}.tar.gz"
  346. }
  347. sshUpgrade() {
  348. debug "${FUNCNAME[0]}"
  349. if [[ -f "$_sysupgrade_bin_gz" ]]; then
  350. local _source="$_sysupgrade_bin_gz"
  351. local _source_fname="$_sysupgrade_bin_gz_fname"
  352. elif [[ -f "$_sysupgrade_bin" ]]; then
  353. local _source="$_sysupgrade_bin"
  354. local _source_fname="$_sysupgrade_bin_fname"
  355. else
  356. echo "Could not find upgrade file"
  357. exit 1
  358. fi
  359. echo "Copying \"$_source\" to $_ssh_upgrade_path/tmp/"
  360. debug "scp \"$_source\" \"$_ssh_upgrade_path\":\"/tmp/$_source_fname\""
  361. # shellcheck disable=SC2140
  362. if ! scp "$_source" "$_ssh_upgrade_path":"/tmp/$_source_fname"; then
  363. echo "Could not access the --ssh-upgrade PATH"
  364. exit 1
  365. fi
  366. echo "Executing remote sysupgrade"
  367. debug "ssh \"$_ssh_upgrade_path\" \"sysupgrade -F /tmp/$_source_fname\""
  368. # shellcheck disable=SC2029
  369. ssh "$_ssh_upgrade_path" "sysupgrade -F /tmp/$_source_fname"
  370. }
  371. __main() {
  372. parseInput "$@"
  373. setDefaults
  374. setVars
  375. installPrerequisites
  376. acquireImageBuilder
  377. extractImageBuilder
  378. #copyFiles
  379. rm -rf "$_ssh_backup_path"
  380. [[ -v _ssh_backup_path ]] && sshBackup "$_ssh_backup_path"
  381. if makeImage; then
  382. [[ -v _ssh_upgrade_path ]] && sshUpgrade
  383. [[ -v _flash_dev ]] && flashImage
  384. fi
  385. }
  386. __main "$@"
  387. exit $?