openwrtBuild 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
  169. _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
  170. else
  171. _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
  172. _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
  173. fi
  174. # Make sources directory if it does not exist
  175. [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
  176. # Remove existing ImageBuilder archives
  177. [[ -f "$_source_archive" ]] && rm "$_source_archive"
  178. echo "Downloading image archive"
  179. debug "axel -o $_source_archive $_url"
  180. if ! axel -o "$_source_archive" "$_url" > /dev/null 2>&1; then
  181. echo "Could not download Image Builder"
  182. exit 1
  183. fi
  184. }
  185. extractImageBuilder() {
  186. debug "${FUNCNAME[0]}"
  187. [[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir"
  188. if [[ ! -f "$_source_archive" ]]; then
  189. echo "Archive missing"
  190. exit 1
  191. fi
  192. echo "Extracting image archive"
  193. debug "tar -xf $_source_archive -C $_source_dir --strip-components 1"
  194. if ! tar -xf "$_source_archive" -C "$_source_dir" --strip-components 1; then
  195. echo "Extraction failed"
  196. exit 1
  197. fi
  198. }
  199. makeImage() {
  200. debug "${FUNCNAME[0]}"
  201. # move to extracted source directory
  202. if ! pushd "$_source_dir" > /dev/null 2>&1; then
  203. exit 1
  204. fi
  205. # Make bin dir
  206. [[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir"
  207. # build image
  208. echo "Running make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot"
  209. debug "make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot > make.log"
  210. if ! make -j4 image BIN_DIR="$_out_bin_dir" PROFILE="$_profile" \
  211. PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then
  212. echo "Make image failed!"
  213. exit 1
  214. fi
  215. if ! popd > /dev/null 2>&1; then
  216. exit 1
  217. fi
  218. }
  219. extractImage() {
  220. debug "${FUNCNAME[0]}" "$@"
  221. local _gz
  222. [[ $# -lt 1 ]] && echo "extractImage() requires at least one argument" && exit 1
  223. for _gz in "$@"; do
  224. [[ ! -f "$_gz" ]] && return 1
  225. debug "gunzip -qfk $_gz"
  226. if ! gunzip -qfk "$_gz"; then
  227. echo "$_gz extraction failed!"
  228. fi
  229. done
  230. }
  231. flashImage() {
  232. debug "${FUNCNAME[0]}"
  233. if [[ -z $_factory_bin && -f "$_factory_bin_gz" ]]; then
  234. extractImage "$_factory_bin_gz"
  235. fi
  236. if [[ ! -d "$_flash_dev" ]]; then
  237. echo "The device specified by --flash could not be found"
  238. exit 1
  239. fi
  240. echo "Unmounting target device $_flash_dev partitions"
  241. debug "umount $_flash_dev?*"
  242. sudo umount "$_flash_dev?*"
  243. debug "sudo dd if=\"$_factory_bin\" of=\"$_flash_dev\" bs=2M conv=fsync"
  244. if sudo dd if="$_factory_bin" of="$_flash_dev" bs=2M conv=fsync; then
  245. sync
  246. echo "Image flashed sucessfully!"
  247. else
  248. echo "dd failed!"
  249. exit 1
  250. fi
  251. }
  252. sshUpgrade() {
  253. debug "${FUNCNAME[0]}"
  254. if [[ -f "$_sysupgrade_bin_gz" ]]; then
  255. local _source="$_sysupgrade_bin_gz"
  256. local _source_fname="$_sysupgrade_bin_gz_fname"
  257. elif [[ -f "$_sysupgrade_bin" ]]; then
  258. local _source="$_sysupgrade_bin"
  259. local _source_fname="$_sysupgrade_bin_fname"
  260. else
  261. echo "Could not find upgrade file"
  262. exit 1
  263. fi
  264. echo "Copying \"$_source\" to $_ssh_upgrade_path/tmp/"
  265. debug "scp \"$_source\" \"$_ssh_upgrade_path\":\"/tmp/$_source_fname\""
  266. # shellcheck disable=SC2140
  267. if ! scp "$_source" "$_ssh_upgrade_path":"/tmp/$_source_fname"; then
  268. echo "Could not access the --ssh-upgrade PATH"
  269. exit 1
  270. fi
  271. echo "Executing remote sysupgrade"
  272. debug "ssh \"$_ssh_upgrade_path\" \"sysupgrade -F /tmp/$_source_fname\""
  273. # shellcheck disable=SC2029
  274. ssh "$_ssh_upgrade_path" "sysupgrade -F /tmp/$_source_fname"
  275. }
  276. __main() {
  277. parseInput "$@"
  278. setDefaults
  279. setVars
  280. installPrerequisites
  281. acquireImageBuilder
  282. extractImageBuilder
  283. if makeImage; then
  284. [[ -n $_ssh_upgrade_path ]] && sshUpgrade
  285. [[ -n $_flash_dev ]] && flashImage
  286. fi
  287. }
  288. __main "$@"
  289. exit $?