openwrtBuild 10 KB

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