openwrtBuild 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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="true" # 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")
  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. --version, -v OPENWRT_VERSION
  62. --profile, -p PROFILE
  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. source /etc/os-release
  121. export ID="$ID"
  122. echo "Detected platform: $ID"
  123. else
  124. echo "Cannot detect OS!"
  125. exit 1
  126. fi
  127. }
  128. getOS
  129. export _source_archive="$_builddir/sources/$_profile-$_version.tar.xz"
  130. export _source_dir="${_source_archive%.tar.xz}"
  131. export _out_bin_dir="$_builddir/bin/$_profile-$_version/"
  132. if [[ "$_version" == "snapshot" ]]; then
  133. local _out_prefix="$_out_bin_dir/openwrt-${_target//\//-}-$_profile"
  134. else
  135. local _out_prefix="$_out_bin_dir/openwrt-$_version-${_target//\//-}-$_profile"
  136. fi
  137. export _factory_bin="$_out_prefix-$_factory_suffix"
  138. export _factory_bin_fname="${_factory_bin##*/}"
  139. export _factory_bin_gz="$_factory_bin.gz"
  140. export _factory_bin_gz_fname="${_factory_bin_gz##*/}"
  141. export _sysupgrade_bin="$_out_prefix-$_sysupgrade_suffix"
  142. export _sysupgrade_bin_fname="${_sysupgrade_bin##*/}"
  143. export _sysupgrade_bin_gz="$_sysupgrade_bin.gz"
  144. export _sysupgrade_bin_gz_fname="${_sysupgrade_bin_gz##*/}"
  145. }
  146. installPrerequisites() {
  147. debug "${FUNCNAME[0]}"
  148. local -a _pkg_list
  149. local _pkg_cmd
  150. if [[ "$ID" == "fedora" ]]; then
  151. _pkg_list=("@c-development" "@development-tools" "@development-libs" "zlib-static" "elfutils-libelf-devel" "gawk" "unzip" "file" "wget" "python3" "python2" "axel")
  152. _pkg_cmd="dnf"
  153. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  154. _pkg_list=("build-essential" "libncurses5-dev" "libncursesw5-dev" "zlib1g-dev" "gawk" "git" "gettext" "libssl-dev" "xsltproc" "wget" "unzip" "python" "axel")
  155. _pkg_cmd="apt-get"
  156. fi
  157. echo "Installing dependencies"
  158. debug "sudo $_pkg_cmd -y install ${_pkg_list[*]}"
  159. if ! sudo "$_pkg_cmd" -y install "${_pkg_list[@]}" > /dev/null 2>&1; then
  160. echo "Warning: Problem installing prerequisites"
  161. return 1
  162. fi
  163. }
  164. acquireImageBuilder() {
  165. debug "${FUNCNAME[0]}"
  166. local _url _filename
  167. if [[ "$_version" == "snapshot" ]]; then
  168. # Remove existing ImageBuilders
  169. [[ -f "$_source_archive" ]] && rm "$_source_archive"
  170. _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
  171. _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
  172. else
  173. # Reuse existing ImageBuilders
  174. [[ -f "$_source_archive" ]] && return 0
  175. _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
  176. _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
  177. fi
  178. # Make sources directory if it does not exist
  179. [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
  180. echo "Downloading image archive"
  181. debug "axel -o $_source_archive $_url"
  182. if ! axel -o "$_source_archive" "$_url" > /dev/null 2>&1; then
  183. echo "Could not download Image Builder"
  184. exit 1
  185. fi
  186. }
  187. extractImageBuilder() {
  188. debug "${FUNCNAME[0]}"
  189. [[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir"
  190. if [[ ! -f "$_source_archive" ]]; then
  191. echo "Archive missing"
  192. exit 1
  193. fi
  194. echo "Extracting image archive"
  195. debug "tar -xf $_source_archive -C $_source_dir --strip-components 1"
  196. if ! tar -xf "$_source_archive" -C "$_source_dir" --strip-components 1; then
  197. echo "Extraction failed"
  198. exit 1
  199. fi
  200. }
  201. makeImage() {
  202. debug "${FUNCNAME[0]}"
  203. # move to extracted source directory
  204. if ! pushd "$_source_dir" > /dev/null 2>&1; then
  205. exit 1
  206. fi
  207. # Make bin dir
  208. [[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir"
  209. # build image
  210. echo "Running make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot"
  211. debug "make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot > make.log"
  212. if ! make -j4 image BIN_DIR="$_out_bin_dir" PROFILE="$_profile" \
  213. PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then
  214. echo "Make image failed!"
  215. exit 1
  216. fi
  217. if ! popd > /dev/null 2>&1; then
  218. exit 1
  219. fi
  220. }
  221. extractImage() {
  222. debug "${FUNCNAME[0]}" "$@"
  223. local _gz
  224. [[ $# -lt 1 ]] && echo "extractImage() requires at least one argument" && exit 1
  225. for _gz in "$@"; do
  226. [[ ! -f "$_gz" ]] && return 1
  227. debug "gunzip -qfk $_gz"
  228. if ! gunzip -qfk "$_gz"; then
  229. echo "$_gz extraction failed!"
  230. fi
  231. done
  232. }
  233. flashImage() {
  234. debug "${FUNCNAME[0]}"
  235. if [[ -z $_factory_bin && -f "$_factory_bin_gz" ]]; then
  236. extractImage "$_factory_bin_gz"
  237. fi
  238. if [[ ! -d "$_flash_dev" ]]; then
  239. echo "The device specified by --flash could not be found"
  240. exit 1
  241. fi
  242. echo "Unmounting target device $_flash_dev partitions"
  243. debug "umount $_flash_dev?*"
  244. sudo umount "$_flash_dev?*"
  245. debug "sudo dd if=\"$_factory_bin\" of=\"$_flash_dev\" bs=2M conv=fsync"
  246. if sudo dd if="$_factory_bin" of="$_flash_dev" bs=2M conv=fsync; then
  247. sync
  248. echo "Image flashed sucessfully!"
  249. else
  250. echo "dd failed!"
  251. exit 1
  252. fi
  253. }
  254. sshUpgrade() {
  255. debug "${FUNCNAME[0]}"
  256. if [[ -f "$_sysupgrade_bin_gz" ]]; then
  257. local _source="$_sysupgrade_bin_gz"
  258. local _source_fname="$_sysupgrade_bin_gz_fname"
  259. elif [[ -f "$_sysupgrade_bin" ]]; then
  260. local _source="$_sysupgrade_bin"
  261. local _source_fname="$_sysupgrade_bin_fname"
  262. else
  263. echo "Could not find upgrade file"
  264. exit 1
  265. fi
  266. echo "Copying \"$_source\" to $_ssh_upgrade_path/tmp/"
  267. debug "scp \"$_source\" \"$_ssh_upgrade_path\":\"/tmp/$_source_fname\""
  268. # shellcheck disable=SC2140
  269. if ! scp "$_source" "$_ssh_upgrade_path":"/tmp/$_source_fname"; then
  270. echo "Could not access the --ssh-upgrade PATH"
  271. exit 1
  272. fi
  273. echo "Executing remote sysupgrade"
  274. debug "ssh \"$_ssh_upgrade_path\" \"sysupgrade -F /tmp/$_source_fname\""
  275. # shellcheck disable=SC2029
  276. ssh "$_ssh_upgrade_path" "sysupgrade -F /tmp/$_source_fname"
  277. }
  278. __main() {
  279. parseInput "$@"
  280. setDefaults
  281. setVars
  282. installPrerequisites
  283. acquireImageBuilder
  284. extractImageBuilder
  285. if makeImage; then
  286. [[ -n $_ssh_upgrade_path ]] && sshUpgrade
  287. [[ -n $_flash_dev ]] && flashImage
  288. fi
  289. }
  290. __main "$@"
  291. exit $?