openwrtBuild 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #!/usr/bin/env bash
  2. source ./functions.sh
  3. #####################
  4. ##### DEFAULTS ######
  5. #####################
  6. setDefaults() {
  7. debug "${FUNCNAME[0]}"
  8. [[ -z $_debug ]] && _debug="true" # Set to true to enable debugging by default
  9. [[ -z $_builddir ]] && _builddir="$PWD"
  10. [[ -z $_filesroot ]] && _filesroot="$_builddir/files/"
  11. # Global default packages for all profiles
  12. declare -ag _packages=("luci" "nano" "htop" "tcpdump" "diffutils")
  13. # If no profile is specified, use the TP-Link Archer C7 v2
  14. [[ -z $_profile ]] && _profile="tplink_archer-c7-v2"
  15. # PROFILES
  16. # TP-Link Archer C7 v2
  17. if [[ "$_profile" == "tplink_archer-c7-v2" ]]; then
  18. [[ -z $_version ]] && _version="19.07.2"
  19. [[ -z $_target ]] && _target="ath79/generic"
  20. # Raspberry Pi 4
  21. elif [[ "$_profile" == "rpi-4" ]]; then
  22. [[ -z $_version ]] && _version="snapshot"
  23. [[ -z $_target ]] && _target="bcm27xx/bcm2711"
  24. _packages+=("kmod-usb-net-asix-ax88179" "luci-app-upnp" "luci-app-wireguard" \
  25. "luci-app-vpn-policy-routing" "-dnsmasq" "dnsmasq-full" "luci-app-ddns")
  26. fi
  27. }
  28. #####################
  29. ##### FUNCTIONS #####
  30. #####################
  31. printHelpAndExit() {
  32. debug "${FUNCNAME[0]}"
  33. cat <<-'EOF'
  34. USAGE:
  35. buildOpenWRT [[OPTION] [VALUE]]...
  36. OPTIONS
  37. --version, -v OPENWRT_VERSION
  38. --target, -t TARGET
  39. --profile, -p PROFILE
  40. --builddir, -b PATH
  41. --ssh-upgrade SSH_PATH
  42. Example: root@192.168.1.1
  43. --flash, -f DEVICE
  44. Example: /dev/sdX
  45. --debug, -d
  46. --help, -h
  47. EOF
  48. # Exit using passed exit code
  49. [[ -z $1 ]] && exit 0 || exit "$1"
  50. }
  51. parseInput() {
  52. debug "${FUNCNAME[0]}"
  53. if _input=$(getopt -o +v:t:p:b:f:dh -l version:,target:,profile:,builddir:,ssh-upgrade:,flash:,debug,help -- "$@"); then
  54. eval set -- "$_input"
  55. while true; do
  56. case "$1" in
  57. --version|-v)
  58. shift && _version="$1"
  59. ;;
  60. --target|-t)
  61. shift && _target="$1"
  62. ;;
  63. --profile|-p)
  64. shift && _profile="$1"
  65. ;;
  66. --builddir|-b)
  67. shift && _builddir="$1"
  68. ;;
  69. --ssh-upgrade)
  70. shift && _ssh_upgrade_path="$1"
  71. ;;
  72. --flash|-f)
  73. shift && _flash_dev="$1"
  74. ;;
  75. --debug|-d)
  76. echo "Debugging on"
  77. _debug="true"
  78. ;;
  79. --help|-h)
  80. _printHelpAndExit 0
  81. ;;
  82. --)
  83. shift
  84. break
  85. ;;
  86. esac
  87. shift
  88. done
  89. else
  90. echo "Incorrect options provided"
  91. printHelpAndExit 1
  92. fi
  93. }
  94. debug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
  95. setVars() {
  96. debug "${FUNCNAME[0]}"
  97. getOS () {
  98. debug "${FUNCNAME[0]}"
  99. if [[ -f /etc/os-release ]]; then
  100. source /etc/os-release
  101. export ID="$ID"
  102. echo "Detected platform: $ID"
  103. else
  104. echo "Cannot detect OS!"
  105. exit 1
  106. fi
  107. }
  108. getOS
  109. export _source_archive="$_builddir/sources/$_profile-$_version.tar.xz"
  110. export _source_dir="${_source_archive%.tar.xz}"
  111. export _out_bin_dir="$_builddir/bin/$_profile-$_version/"
  112. export _out_bin_gz="$_out_bin_dir/openwrt-${_target//\//-}-$_profile-ext4-factory.img.gz"
  113. export _out_bin="${_out_bin_gz%.gz}"
  114. }
  115. installPrerequisites() {
  116. debug "${FUNCNAME[0]}"
  117. local -a _pkg_list
  118. local _pkg_cmd
  119. if [[ "$ID" == "fedora" ]]; then
  120. _pkg_list=("@c-development" "@development-tools" "@development-libs" "zlib-static" "elfutils-libelf-devel" "gawk" "unzip" "file" "wget" "python3" "python2" "axel")
  121. _pkg_cmd="dnf"
  122. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  123. _pkg_list=("build-essential" "libncurses5-dev" "libncursesw5-dev" "zlib1g-dev" "gawk" "git" "gettext" "libssl-dev" "xsltproc" "wget" "unzip" "python" "axel")
  124. _pkg_cmd="apt-get"
  125. fi
  126. echo "Installing dependencies"
  127. debug "sudo $_pkg_cmd -y install ${_pkg_list[*]}"
  128. if ! sudo "$_pkg_cmd" -y install "${_pkg_list[@]}" > /dev/null 2>&1; then
  129. echo "Warning: Problem installing prerequisites"
  130. return 1
  131. fi
  132. }
  133. acquireImageBuilder() {
  134. debug "${FUNCNAME[0]}"
  135. local _url _filename
  136. if [[ "$_version" == "snapshot" ]]; then
  137. _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
  138. _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
  139. else
  140. _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
  141. _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
  142. fi
  143. # Make sources directory if it does not exist
  144. [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
  145. # Remove existing ImageBuilder archives
  146. [[ -f "$_source_archive" ]] && rm "$_source_archive"
  147. echo "Downloading image archive"
  148. debug "axel -o $_source_archive $_url"
  149. if ! axel -o "$_source_archive" "$_url" > /dev/null 2>&1; then
  150. echo "Could not download Image Builder"
  151. exit 1
  152. fi
  153. }
  154. extractImageBuilder() {
  155. debug "${FUNCNAME[0]}"
  156. [[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir"
  157. if [[ ! -f "$_source_archive" ]]; then
  158. echo "Archive missing"
  159. exit 1
  160. fi
  161. echo "Extracting image archive"
  162. debug "tar -xf $_source_archive -C $_source_dir --strip-components 1"
  163. if ! tar -xf "$_source_archive" -C "$_source_dir" --strip-components 1; then
  164. echo "Extraction failed"
  165. exit 1
  166. fi
  167. }
  168. makeImage() {
  169. debug "${FUNCNAME[0]}"
  170. # move to extracted source directory
  171. if ! pushd "$_source_dir" > /dev/null 2>&1; then
  172. exit 1
  173. fi
  174. # Make bin dir
  175. [[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir"
  176. # build image
  177. echo "Running make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot"
  178. debug "make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot > make.log"
  179. if ! make -j4 image BIN_DIR="$_out_bin_dir" PROFILE="$_profile" \
  180. PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then
  181. echo "Make image failed!"
  182. exit 1
  183. fi
  184. if ! popd > /dev/null 2>&1; then
  185. exit 1
  186. fi
  187. }
  188. flashImage() {
  189. debug "${FUNCNAME[0]}"
  190. extractImage "$_out_bin_gz"
  191. if [[ ! -d "$_flash_dev" ]]; then
  192. echo "The device specified by --flash could not be found"
  193. exit 1
  194. fi
  195. echo "Unmounting target device $_flash_dev partitions"
  196. debug "umount $_flash_dev?*"
  197. sudo umount "$_flash_dev?*"
  198. debug "sudo dd if=\"$_out_bin\" of=\"$_flash_dev\" bs=2M conv=fsync"
  199. if sudo dd if="$_out_bin" of="$_flash_dev" bs=2M conv=fsync; then
  200. sync
  201. echo "Image flashed sucessfully!"
  202. else
  203. echo "dd failed!"
  204. exit 1
  205. fi
  206. }
  207. sshUpgrade() {
  208. debug "${FUNCNAME[0]}"
  209. local _out_bin_gz_name="${_out_bin_gz##*/}"
  210. echo "Copying upgrade image to $_ssh_upgrade_path"
  211. debug "scp \"$_out_bin_gz\" \"$_ssh_upgrade_path:/tmp/$_out_bin_gz_name\""
  212. # shellcheck disable=SC2140
  213. if ! scp "$_out_bin_gz" "$_ssh_upgrade_path":"/tmp/$_out_bin_gz_name"; then
  214. echo "Could not access the --ssh-upgrade PATH"
  215. exit 1
  216. fi
  217. echo "Executing remote sysupgrade"
  218. debug "ssh \"$_ssh_upgrade_path\" \"sysupgrade -F /tmp/$_out_bin_gz_name\""
  219. # shellcheck disable=SC2029
  220. ssh "$_ssh_upgrade_path" "sysupgrade -F /tmp/$_out_bin_gz_name"
  221. }
  222. __main() {
  223. parseInput "$@"
  224. setDefaults
  225. setVars
  226. installPrerequisites
  227. acquireImageBuilder
  228. extractImageBuilder
  229. makeImage
  230. [[ -n $_ssh_upgrade_path ]] && sshUpgrade
  231. [[ -n $_flash_dev ]] && flashImage
  232. }
  233. __main "$@"
  234. exit $?