openwrtBuild 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 -ag _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. --flash, -f DEVICE
  43. Example: /dev/sdX
  44. --debug, -d
  45. --help, -h
  46. EOF
  47. # Exit using passed exit code
  48. [[ -z $1 ]] && exit 0 || exit "$1"
  49. }
  50. parseInput () {
  51. runDebug "${FUNCNAME[0]}"
  52. if _input=$(getopt -o +v:t:p:b:f:dh -l version:,target:,profile:,builddir:,ssh-backup:,flash:,debug,help -- "$@"); then
  53. eval set -- "$_input"
  54. while true; do
  55. case "$1" in
  56. --version|-v)
  57. shift && _version="$1"
  58. ;;
  59. --target|-t)
  60. shift && _target="$1"
  61. ;;
  62. --profile|-p)
  63. shift && _profile="$1"
  64. ;;
  65. --builddir|-b)
  66. shift && _builddir="$1"
  67. ;;
  68. --ssh-backup)
  69. shift && _ssh_backup="$1"
  70. ;;
  71. --flash|-f)
  72. shift && _flash_dev="$1"
  73. ;;
  74. --debug|-d)
  75. echo "Debugging on"
  76. _debug="true"
  77. ;;
  78. --help|-h)
  79. _printHelpAndExit 0
  80. ;;
  81. --)
  82. shift
  83. break
  84. ;;
  85. esac
  86. shift
  87. done
  88. else
  89. echo "Incorrect options provided"
  90. printHelpAndExit 1
  91. fi
  92. }
  93. runDebug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
  94. installPrerequisites () {
  95. runDebug "${FUNCNAME[0]}"
  96. getOS () {
  97. runDebug "${FUNCNAME[0]}"
  98. if [[ -f /etc/os-release ]]; then
  99. source /etc/os-release
  100. echo "Detected platform: $ID"
  101. else
  102. echo "Cannot detect OS!"
  103. exit 1
  104. fi
  105. }
  106. getOS
  107. echo "Installing dependencies..."
  108. if [[ "$ID" == "fedora" ]]; then
  109. if ! sudo dnf -y install @c-development @development-tools @development-libs zlib-static elfutils-libelf-devel gawk unzip file wget python3 python2 axel > /dev/null 2>&1; then
  110. echo "Warning: Problem installing prerequisites"
  111. fi
  112. elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
  113. if ! sudo apt-get -y install build-essential libncurses5-dev libncursesw5-dev zlib1g-dev gawk git gettext libssl-dev xsltproc wget unzip python axel > /dev/null 2>&1; then
  114. echo "Warning: Problem installing prerequisites"
  115. fi
  116. fi
  117. }
  118. acquireImageBuilder () {
  119. runDebug "${FUNCNAME[0]}"
  120. local _url _filename
  121. export _source_archive
  122. echo "Downloading image archive..."
  123. if [[ "$_version" == "snapshot" ]]; then
  124. _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
  125. _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
  126. else
  127. _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
  128. _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
  129. fi
  130. # Make sources directory if it does not exist
  131. [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
  132. _source_archive="$_builddir/sources/$_profile-$_version.tar.xz"
  133. # Remove existing ImageBuilder archives
  134. [[ -f "$_source_archive" ]] && rm "$_source_archive"
  135. if ! axel -o "$_source_archive" "$_url"; then
  136. echo "Could not download Image Builder"
  137. exit 1
  138. fi
  139. }
  140. extractImageBuilder () {
  141. runDebug "${FUNCNAME[0]}"
  142. export _source_dir="${_source_archive%.tar.xz}"
  143. [[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir"
  144. echo "Extracting image archive..."
  145. if [[ -f "$_source_archive" ]]; then
  146. if ! tar -xf "$_source_archive" -C "$_source_dir" --strip-components 1; then
  147. echo "Extraction failed"
  148. exit 1
  149. fi
  150. fi
  151. }
  152. makeImage () {
  153. runDebug "${FUNCNAME[0]}"
  154. export _out_bin_gz _out_bin_dir
  155. # move to extracted source directory
  156. if ! pushd "$_source_dir" > /dev/null 2>&1; then
  157. exit 1
  158. fi
  159. _out_bin_dir="$_builddir/bin/$_profile-$_version/"
  160. # Make bin dir
  161. [[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir"
  162. # build image
  163. echo "Running make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot"
  164. if ! make -j4 image BIN_DIR="$_out_bin_dir" PROFILE="$_profile" \
  165. PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then
  166. echo "Make image failed!"
  167. exit 1
  168. fi
  169. _out_bin_gz="$_out_bin_dir/openwrt-${_target//\//-}-$_profile-ext4-factory.img.gz"
  170. if [[ ! -f "$_out_bin_gz" ]]; then
  171. echo "Make output image archive not found"
  172. exit 1
  173. fi
  174. if ! popd > /dev/null 2>&1; then
  175. exit 1
  176. fi
  177. }
  178. flashImage() {
  179. runDebug "${FUNCNAME[0]}"
  180. local _out_bin="${_out_bin_gz%.gz}"
  181. if [[ -f "$_out_bin_gz" ]]; then
  182. gunzip "$_out_bin_gz"
  183. fi
  184. if [[ ! -f "$_out_bin" ]]; then
  185. echo "Cannot find output bin file"
  186. exit 1
  187. fi
  188. if [[ ! -d "$_flash_dev" ]]; then
  189. echo "The device specified by --flash could not be found"
  190. exit 1
  191. fi
  192. echo "Unmounting target device $_flash_dev partitions..."
  193. sudo umount "$_flash_dev?*"
  194. if sudo dd if="$_out_bin" of="$_flash_dev" bs=2M conv=fsync; then
  195. sync
  196. echo "Image flashed sucessfully!"
  197. else
  198. echo "dd failed!"
  199. exit 1
  200. fi
  201. }
  202. #sshBackup () {
  203. #
  204. # runDebug "${FUNCNAME[0]}"
  205. #
  206. # # Make files directory if it does not exist
  207. # [[ ! -d "$_filesroot" ]] && mkdir -p "$_filesroot"
  208. #
  209. # for fd in "${_backedup[@]}"; do
  210. # _dir="${fd%/*}/"
  211. # [[ ! -d "$_filesroot$_dir" ]] && mkdir -p "$_filesroot/$_dir"
  212. # if ! scp -rp "$_ssh_backup:$fd" "$_filesroot/$_dir"; then
  213. # echo "Did not successfully backup files from --ssh-backup"
  214. # echo "Exiting now to prevent data loss!"
  215. # exit 1
  216. # fi
  217. # done
  218. #}
  219. __main () {
  220. parseInput "$@"
  221. setDefaults
  222. installPrerequisites
  223. acquireImageBuilder
  224. extractImageBuilder
  225. #[[ -n $_ssh_backup ]] && sshBackup
  226. makeImage
  227. [[ -n $_flash_dev ]] && flashImage
  228. }
  229. __main "$@"
  230. exit $?