openwrtBuild 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/env bash
  2. #####################
  3. ##### DEFAULTS ######
  4. #####################
  5. setDefaults() {
  6. runDebug "${FUNCNAME[0]}"
  7. [[ -z $_debug ]] && _debug="true" # Set to true to enable debugging by default
  8. [[ -z $_builddir ]] && _builddir="$PWD"
  9. [[ -z $_filesroot ]] && _filesroot="$_builddir/files/"
  10. # Global default packages for all profiles
  11. declare -a _packages=("luci" "nano" "htop" "tcpdump" "diffutils")
  12. # If no profile is specified, use the TP-Link Archer C7 v2
  13. [[ -z $_profile ]] && _profile="tplink_archer-c7-v2"
  14. # PROFILES
  15. # TP-Link Archer C7 v2
  16. if [[ "$_profile" == "tplink_archer-c7-v2" ]]; then
  17. [[ -z $_version ]] && _version="19.07.2"
  18. [[ -z $_target ]] && _target="ath79/generic"
  19. # Raspberry Pi 4
  20. elif [[ "$_profile" == "rpi-4" ]]; then
  21. [[ -z $_version ]] && _version="snapshot"
  22. [[ -z $_target ]] && _target="bcm27xx/bcm2711"
  23. packages+=("kmod-usb-net-asix-ax88179" "luci-app-upnp" "luci-app-wireguard" \
  24. "luci-app-vpn-policy-routing" "-dnsmasq" "dnsmasq-full")
  25. fi
  26. }
  27. #####################
  28. ##### FUNCTIONS #####
  29. #####################
  30. printHelpAndExit () {
  31. runDebug "${FUNCNAME[0]}"
  32. cat <<-'EOF'
  33. USAGE:
  34. buildOpenWRT [[OPTION] [VALUE]]...
  35. OPTIONS
  36. --version, -v OPENWRT_VERSION
  37. --target, -t TARGET
  38. --profile, -p PROFILE
  39. --builddir, -b PATH
  40. --ssh-backup SSH path
  41. Example: root@192.168.1.1
  42. --debug, -d
  43. --help, -h
  44. EOF
  45. # Exit using passed exit code
  46. [[ -z $1 ]] && exit 0 || exit "$1"
  47. }
  48. parseInput () {
  49. runDebug "${FUNCNAME[0]}"
  50. if _input=$(getopt -o +v:t:p:b:dh -l version:,target:,profile:,builddir:,ssh-backup:debug,help -- "$@"); then
  51. eval set -- "$_input"
  52. while true; do
  53. case "$1" in
  54. --version|-v)
  55. shift && _version="$1"
  56. ;;
  57. --target|-t)
  58. shift && _target="$1"
  59. ;;
  60. --profile|-p)
  61. shift && _profile="$1"
  62. ;;
  63. --builddir|-b)
  64. shift && _builddir="$1"
  65. ;;
  66. --ssh-backup)
  67. shift && _ssh_backup="$1"
  68. ;;
  69. --debug|-d)
  70. echo "Debugging on"
  71. _debug="true"
  72. ;;
  73. --help|-h)
  74. _printHelpAndExit 0
  75. ;;
  76. --)
  77. shift
  78. break
  79. ;;
  80. esac
  81. shift
  82. done
  83. else
  84. echo "Incorrect options provided"
  85. printHelpAndExit 1
  86. fi
  87. }
  88. runDebug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
  89. installPrerequisites () {
  90. runDebug "${FUNCNAME[0]}"
  91. getOS () {
  92. runDebug "${FUNCNAME[0]}"
  93. if [[ -f /etc/os-release ]]; then
  94. source /etc/os-release
  95. else
  96. echo "Cannot detect OS!"
  97. exit 1
  98. fi
  99. }
  100. getOS
  101. if [[ "$ID" == "fedora" ]]; then
  102. if ! sudo dnf -y install @c-development @development-tools @development-libs zlib-static elfutils-libelf-devel gawk unzip file wget python3 python2 > /dev/null 2>&1; then
  103. echo "Warning: Problem installing prerequisites"
  104. fi
  105. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  106. if ! sudo apt-get -y install build-essential libncurses5-dev libncursesw5-dev zlib1g-dev gawk git gettext libssl-dev xsltproc wget unzip python > /dev/null 2>&1; then
  107. echo "Warning: Problem installing prerequisites"
  108. fi
  109. fi
  110. }
  111. acquireImageBuilder () {
  112. runDebug "${FUNCNAME[0]}"
  113. local _url
  114. if [[ "$_version" == "snapshot" ]]; then
  115. _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
  116. _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
  117. else
  118. _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
  119. _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
  120. fi
  121. # Make sources directory if it does not exist
  122. [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
  123. if [[ ! -f "$_builddir/sources/$_filename" ]]; then
  124. echo "Downloading $_url to $_builddir/sources"
  125. if ! wget -q -P "$_builddir/sources" "$_url"; then
  126. echo "Could not download Image Builder"
  127. exit 1
  128. fi
  129. else
  130. echo "Image builder already exists, skipping download..."
  131. fi
  132. }
  133. extractImageBuilder () {
  134. runDebug "${FUNCNAME[0]}"
  135. if [[ -f "$_builddir/sources/$_filename" ]]; then
  136. if ! tar -xf "$_builddir/sources/$_filename" -C "$_builddir/sources/"; then
  137. echo "Extraction failed"
  138. exit 1
  139. fi
  140. fi
  141. }
  142. makeImage () {
  143. runDebug "${FUNCNAME[0]}"
  144. # move to extracted source directory
  145. if ! pushd "$_builddir/sources/${_filename%.tar.xz}" > /dev/null 2>&1; then
  146. exit 1
  147. fi
  148. # Make bin dir
  149. [[ ! -d "$_builddir/bin" ]] && mkdir -p "$_builddir/bin"
  150. # build image
  151. if ! make -j4 image BIN_DIR="$_builddir/bin/$_profile/" PROFILE="$_profile" \
  152. PACKAGES="${_packages[*]}" FILES="$_filesroot"; then
  153. echo "Make image failed!"
  154. exit 1
  155. fi
  156. if ! popd > /dev/null 2>&1; then
  157. exit 1
  158. fi
  159. }
  160. #sshBackup () {
  161. #
  162. # runDebug "${FUNCNAME[0]}"
  163. #
  164. # # Make files directory if it does not exist
  165. # [[ ! -d "$_filesroot" ]] && mkdir -p "$_filesroot"
  166. #
  167. # for fd in "${_backedup[@]}"; do
  168. # _dir="${fd%/*}/"
  169. # [[ ! -d "$_filesroot$_dir" ]] && mkdir -p "$_filesroot/$_dir"
  170. # if ! scp -rp "$_ssh_backup:$fd" "$_filesroot/$_dir"; then
  171. # echo "Did not successfully backup files from --ssh-backup"
  172. # echo "Exiting now to prevent data loss!"
  173. # exit 1
  174. # fi
  175. # done
  176. #}
  177. # TODO
  178. #flashImage () {
  179. #
  180. # if ! scp -rp "$_bin_path" "$_ssh_backup:/tmp/$_bin_name"; then
  181. # echo "Could not copy update file to device!"
  182. # exit 1
  183. # fi
  184. #
  185. #3 # shellcheck disable=SC2029
  186. # if ! ssh "$_ssh_backup" "sysupgrade -v /tmp/$_bin_name"; then
  187. #3 echo "sysupgrade failed!"
  188. # exit 1
  189. # fi
  190. #}
  191. __main () {
  192. parseInput "$@"
  193. setDefaults
  194. getOS
  195. installPrerequisites
  196. acquireImageBuilder
  197. extractImageBuilder
  198. [[ -n $_ssh_backup ]] && sshBackup
  199. makeImage
  200. #flashImage # TODO
  201. exit 0
  202. }
  203. __main "$@"