openwrtBuild 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. setDefaults() {
  26. debug "${FUNCNAME[0]}"
  27. export _target _factory_suffix _sysupgrade_suffix _profile _builddir _debug _filesroot
  28. declare -ag _packages
  29. [[ -z $_debug ]] && _debug="false" # Set to true to enable debugging by default
  30. [[ -z $_builddir ]] && _builddir="$PWD"
  31. [[ -z $_filesroot ]] && _filesroot="$_builddir/files/"
  32. # Additional packages for all profiles
  33. _packages+=("luci" "nano" "htop" "tcpdump" "diffutils")
  34. # If no profile is specified, use the TP-Link Archer C7 v2 dumb AP
  35. [[ -z $_profile ]] && _profile="tplink_archer-c7-v2"
  36. # Custom profiles
  37. # TP-Link Archer C7 v2 dumb AP
  38. if [[ "$_profile" == "tplink_archer-c7-v2" ]]; then
  39. [[ -z $_version ]] && _version="19.07.3"
  40. _target="ath79/generic"
  41. _factory_suffix="squashfs-factory.bin"
  42. _sysupgrade_suffix="squashfs-sysupgrade.bin"
  43. _packages+=("-dnsmasq" "-odhcpd" "-iptables")
  44. # Raspberry Pi 4B router with USB->Ethernet dongle
  45. elif [[ "$_profile" == "rpi-4" ]]; then
  46. [[ -z $_version ]] && _version="snapshot"
  47. _target="bcm27xx/bcm2711"
  48. _factory_suffix="ext4-factory.img"
  49. _sysupgrade_suffix="ext4-sysupgrade.img"
  50. _packages+=("kmod-usb-net-asix-ax88179" "luci-app-upnp" "luci-app-wireguard" \
  51. "luci-app-vpn-policy-routing" "-dnsmasq" "dnsmasq-full" "luci-app-ddns" "luci-app-sqm")
  52. fi
  53. }
  54. printHelpAndExit() {
  55. debug "${FUNCNAME[0]}"
  56. cat <<-'EOF'
  57. USAGE:
  58. openwrtBuild [[OPTION] [VALUE]]...
  59. If PROFILE is set and TARGET is not, buildOpenwrt can use a custom profile specified in DEFAULTS
  60. OPTIONS
  61. --profile, -p PROFILE
  62. --version, -v OPENWRT_VERSION
  63. --builddir, -b PATH
  64. --ssh-upgrade SSH_PATH
  65. Example: root@192.168.1.1
  66. --flash, -f DEVICE
  67. Example: /dev/sdX
  68. --debug, -d
  69. --help, -h
  70. EOF
  71. # Exit using passed exit code
  72. [[ -z $1 ]] && exit 0 || exit "$1"
  73. }
  74. parseInput() {
  75. debug "${FUNCNAME[0]}"
  76. if _input=$(getopt -o +v:p:b:f:dh -l version:,profile:,builddir:,ssh-upgrade:,flash:,debug,help -- "$@"); then
  77. eval set -- "$_input"
  78. while true; do
  79. case "$1" in
  80. --version|-v)
  81. shift && _version="$1"
  82. ;;
  83. --profile|-p)
  84. shift && _profile="$1"
  85. ;;
  86. --builddir|-b)
  87. shift && _builddir="$1"
  88. ;;
  89. --ssh-upgrade)
  90. shift && _ssh_upgrade_path="$1"
  91. ;;
  92. --flash|-f)
  93. shift && _flash_dev="$1"
  94. ;;
  95. --debug|-d)
  96. echo "Debugging on"
  97. _debug="true"
  98. ;;
  99. --help|-h)
  100. printHelpAndExit 0
  101. ;;
  102. --)
  103. shift
  104. break
  105. ;;
  106. esac
  107. shift
  108. done
  109. else
  110. echo "Incorrect options provided"
  111. printHelpAndExit 1
  112. fi
  113. }
  114. debug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
  115. setVars() {
  116. debug "${FUNCNAME[0]}"
  117. getOS () {
  118. debug "${FUNCNAME[0]}"
  119. if [[ -f /etc/os-release ]]; then
  120. # shellcheck disable=SC1091
  121. source /etc/os-release
  122. export ID="$ID"
  123. echo "Detected platform: $ID"
  124. else
  125. echo "Cannot detect OS!"
  126. exit 1
  127. fi
  128. }
  129. getOS
  130. export _source_archive="$_builddir/sources/$_profile-$_version.tar.xz"
  131. export _source_dir="${_source_archive%.tar.xz}"
  132. export _out_bin_dir="$_builddir/bin/$_profile-$_version/"
  133. if [[ "$_version" == "snapshot" ]]; then
  134. local _out_prefix="$_out_bin_dir/openwrt-${_target//\//-}-$_profile"
  135. else
  136. local _out_prefix="$_out_bin_dir/openwrt-$_version-${_target//\//-}-$_profile"
  137. fi
  138. export _factory_bin="$_out_prefix-$_factory_suffix"
  139. export _factory_bin_fname="${_factory_bin##*/}"
  140. export _factory_bin_gz="$_factory_bin.gz"
  141. export _factory_bin_gz_fname="${_factory_bin_gz##*/}"
  142. export _sysupgrade_bin="$_out_prefix-$_sysupgrade_suffix"
  143. export _sysupgrade_bin_fname="${_sysupgrade_bin##*/}"
  144. export _sysupgrade_bin_gz="$_sysupgrade_bin.gz"
  145. export _sysupgrade_bin_gz_fname="${_sysupgrade_bin_gz##*/}"
  146. }
  147. installPrerequisites() {
  148. debug "${FUNCNAME[0]}"
  149. local -a _pkg_list
  150. local _pkg_cmd
  151. if [[ "$ID" == "fedora" ]]; then
  152. _pkg_list=("@c-development" "@development-tools" "@development-libs" "zlib-static" "elfutils-libelf-devel" "gawk" "unzip" "file" "wget" "python3" "python2" "axel")
  153. _pkg_cmd="dnf"
  154. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  155. _pkg_list=("build-essential" "libncurses5-dev" "libncursesw5-dev" "zlib1g-dev" "gawk" "git" "gettext" "libssl-dev" "xsltproc" "wget" "unzip" "python" "axel")
  156. _pkg_cmd="apt-get"
  157. fi
  158. echo "Installing dependencies"
  159. debug "sudo $_pkg_cmd -y install ${_pkg_list[*]}"
  160. if ! sudo "$_pkg_cmd" -y install "${_pkg_list[@]}" > /dev/null 2>&1; then
  161. echo "Warning: Problem installing prerequisites"
  162. return 1
  163. fi
  164. }
  165. acquireImageBuilder() {
  166. debug "${FUNCNAME[0]}"
  167. local _url _filename
  168. if [[ "$_version" == "snapshot" ]]; then
  169. # Remove existing ImageBuilders
  170. [[ -f "$_source_archive" ]] && rm "$_source_archive"
  171. _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
  172. _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
  173. else
  174. # Reuse existing ImageBuilders
  175. [[ -f "$_source_archive" ]] && return 0
  176. _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
  177. _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
  178. fi
  179. # Make sources directory if it does not exist
  180. [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
  181. echo "Downloading image archive"
  182. debug "axel -o $_source_archive $_url"
  183. if ! axel -o "$_source_archive" "$_url" > /dev/null 2>&1; then
  184. echo "Could not download Image Builder"
  185. exit 1
  186. fi
  187. }
  188. extractImageBuilder() {
  189. debug "${FUNCNAME[0]}"
  190. [[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir"
  191. if [[ ! -f "$_source_archive" ]]; then
  192. echo "Archive missing"
  193. exit 1
  194. fi
  195. echo "Extracting image archive"
  196. debug "tar -xf $_source_archive -C $_source_dir --strip-components 1"
  197. if ! tar -xf "$_source_archive" -C "$_source_dir" --strip-components 1; then
  198. echo "Extraction failed"
  199. exit 1
  200. fi
  201. }
  202. makeImage() {
  203. debug "${FUNCNAME[0]}"
  204. # move to extracted source directory
  205. if ! pushd "$_source_dir" > /dev/null 2>&1; then
  206. exit 1
  207. fi
  208. # Make bin dir
  209. [[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir"
  210. # build image
  211. echo "Running make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot"
  212. debug "make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot > make.log"
  213. if ! make -j4 image BIN_DIR="$_out_bin_dir" PROFILE="$_profile" \
  214. PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then
  215. echo "Make image failed!"
  216. exit 1
  217. fi
  218. if ! popd > /dev/null 2>&1; then
  219. exit 1
  220. fi
  221. }
  222. extractImage() {
  223. debug "${FUNCNAME[0]}" "$@"
  224. local _gz
  225. [[ $# -lt 1 ]] && echo "extractImage() requires at least one argument" && exit 1
  226. for _gz in "$@"; do
  227. [[ ! -f "$_gz" ]] && return 1
  228. debug "gunzip -qfk $_gz"
  229. if ! gunzip -qfk "$_gz"; then
  230. echo "$_gz extraction failed!"
  231. fi
  232. done
  233. }
  234. flashImage() {
  235. debug "${FUNCNAME[0]}"
  236. if [[ -z $_factory_bin && -f "$_factory_bin_gz" ]]; then
  237. extractImage "$_factory_bin_gz"
  238. fi
  239. if [[ ! -d "$_flash_dev" ]]; then
  240. echo "The device specified by --flash could not be found"
  241. exit 1
  242. fi
  243. echo "Unmounting target device $_flash_dev partitions"
  244. debug "umount $_flash_dev?*"
  245. sudo umount "$_flash_dev?*"
  246. debug "sudo dd if=\"$_factory_bin\" of=\"$_flash_dev\" bs=2M conv=fsync"
  247. if sudo dd if="$_factory_bin" of="$_flash_dev" bs=2M conv=fsync; then
  248. sync
  249. echo "Image flashed sucessfully!"
  250. else
  251. echo "dd failed!"
  252. exit 1
  253. fi
  254. }
  255. sshUpgrade() {
  256. debug "${FUNCNAME[0]}"
  257. if [[ -f "$_sysupgrade_bin_gz" ]]; then
  258. local _source="$_sysupgrade_bin_gz"
  259. local _source_fname="$_sysupgrade_bin_gz_fname"
  260. elif [[ -f "$_sysupgrade_bin" ]]; then
  261. local _source="$_sysupgrade_bin"
  262. local _source_fname="$_sysupgrade_bin_fname"
  263. else
  264. echo "Could not find upgrade file"
  265. exit 1
  266. fi
  267. echo "Copying \"$_source\" to $_ssh_upgrade_path/tmp/"
  268. debug "scp \"$_source\" \"$_ssh_upgrade_path\":\"/tmp/$_source_fname\""
  269. # shellcheck disable=SC2140
  270. if ! scp "$_source" "$_ssh_upgrade_path":"/tmp/$_source_fname"; then
  271. echo "Could not access the --ssh-upgrade PATH"
  272. exit 1
  273. fi
  274. echo "Executing remote sysupgrade"
  275. debug "ssh \"$_ssh_upgrade_path\" \"sysupgrade -F /tmp/$_source_fname\""
  276. # shellcheck disable=SC2029
  277. ssh "$_ssh_upgrade_path" "sysupgrade -F /tmp/$_source_fname"
  278. }
  279. __main() {
  280. parseInput "$@"
  281. setDefaults
  282. setVars
  283. installPrerequisites
  284. acquireImageBuilder
  285. extractImageBuilder
  286. if makeImage; then
  287. [[ -n $_ssh_upgrade_path ]] && sshUpgrade
  288. [[ -n $_flash_dev ]] && flashImage
  289. fi
  290. }
  291. __main "$@"
  292. exit $?