123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #!/usr/bin/env bash
- #####################
- ##### DEFAULTS ######
- #####################
- setDefaults() {
- runDebug "${FUNCNAME[0]}"
- [[ -z $_debug ]] && _debug="true" # Set to true to enable debugging by default
- [[ -z $_builddir ]] && _builddir="$PWD"
- [[ -z $_filesroot ]] && _filesroot="$_builddir/files/"
- # Global default packages for all profiles
- declare -a _packages=("luci" "nano" "htop" "tcpdump" "diffutils")
- # If no profile is specified, use the TP-Link Archer C7 v2
- [[ -z $_profile ]] && _profile="tplink_archer-c7-v2"
- # PROFILES
- # TP-Link Archer C7 v2
- if [[ "$_profile" == "tplink_archer-c7-v2" ]]; then
- [[ -z $_version ]] && _version="19.07.2"
- [[ -z $_target ]] && _target="ath79/generic"
- # Raspberry Pi 4
- elif [[ "$_profile" == "rpi-4" ]]; then
- [[ -z $_version ]] && _version="snapshot"
- [[ -z $_target ]] && _target="bcm27xx/bcm2711"
- packages+=("kmod-usb-net-asix-ax88179" "luci-app-upnp" "luci-app-wireguard" \
- "luci-app-vpn-policy-routing" "-dnsmasq" "dnsmasq-full")
- fi
- }
- #####################
- ##### FUNCTIONS #####
- #####################
- printHelpAndExit () {
- runDebug "${FUNCNAME[0]}"
- cat <<-'EOF'
- USAGE:
- buildOpenWRT [[OPTION] [VALUE]]...
- OPTIONS
- --version, -v OPENWRT_VERSION
- --target, -t TARGET
- --profile, -p PROFILE
- --builddir, -b PATH
- --ssh-backup SSH path
- Example: root@192.168.1.1
- --debug, -d
- --help, -h
- EOF
- # Exit using passed exit code
- [[ -z $1 ]] && exit 0 || exit "$1"
- }
- parseInput () {
- runDebug "${FUNCNAME[0]}"
- if _input=$(getopt -o +v:t:p:b:dh -l version:,target:,profile:,builddir:,ssh-backup:debug,help -- "$@"); then
- eval set -- "$_input"
- while true; do
- case "$1" in
- --version|-v)
- shift && _version="$1"
- ;;
- --target|-t)
- shift && _target="$1"
- ;;
- --profile|-p)
- shift && _profile="$1"
- ;;
- --builddir|-b)
- shift && _builddir="$1"
- ;;
- --ssh-backup)
- shift && _ssh_backup="$1"
- ;;
- --debug|-d)
- echo "Debugging on"
- _debug="true"
- ;;
- --help|-h)
- _printHelpAndExit 0
- ;;
- --)
- shift
- break
- ;;
- esac
- shift
- done
- else
- echo "Incorrect options provided"
- printHelpAndExit 1
- fi
- }
- runDebug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
- installPrerequisites () {
- runDebug "${FUNCNAME[0]}"
- getOS () {
- runDebug "${FUNCNAME[0]}"
- if [[ -f /etc/os-release ]]; then
- source /etc/os-release
- else
- echo "Cannot detect OS!"
- exit 1
- fi
- }
- getOS
- if [[ "$ID" == "fedora" ]]; then
- 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
- echo "Warning: Problem installing prerequisites"
- fi
- elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
- 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
- echo "Warning: Problem installing prerequisites"
- fi
- fi
- }
- acquireImageBuilder () {
- runDebug "${FUNCNAME[0]}"
- local _url
- if [[ "$_version" == "snapshot" ]]; then
- _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
- _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename"
- else
- _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz"
- _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename"
- fi
- # Make sources directory if it does not exist
- [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
- if [[ ! -f "$_builddir/sources/$_filename" ]]; then
- echo "Downloading $_url to $_builddir/sources"
- if ! wget -q -P "$_builddir/sources" "$_url"; then
- echo "Could not download Image Builder"
- exit 1
- fi
- else
- echo "Image builder already exists, skipping download..."
- fi
- }
- extractImageBuilder () {
- runDebug "${FUNCNAME[0]}"
- if [[ -f "$_builddir/sources/$_filename" ]]; then
- if ! tar -xf "$_builddir/sources/$_filename" -C "$_builddir/sources/"; then
- echo "Extraction failed"
- exit 1
- fi
- fi
- }
- makeImage () {
- runDebug "${FUNCNAME[0]}"
- # move to extracted source directory
- if ! pushd "$_builddir/sources/${_filename%.tar.xz}" > /dev/null 2>&1; then
- exit 1
- fi
- # Make bin dir
- [[ ! -d "$_builddir/bin" ]] && mkdir -p "$_builddir/bin"
- # build image
- if ! make -j4 image BIN_DIR="$_builddir/bin/$_profile/" PROFILE="$_profile" \
- PACKAGES="${_packages[*]}" FILES="$_filesroot"; then
- echo "Make image failed!"
- exit 1
- fi
- if ! popd > /dev/null 2>&1; then
- exit 1
- fi
- }
- #sshBackup () {
- #
- # runDebug "${FUNCNAME[0]}"
- #
- # # Make files directory if it does not exist
- # [[ ! -d "$_filesroot" ]] && mkdir -p "$_filesroot"
- #
- # for fd in "${_backedup[@]}"; do
- # _dir="${fd%/*}/"
- # [[ ! -d "$_filesroot$_dir" ]] && mkdir -p "$_filesroot/$_dir"
- # if ! scp -rp "$_ssh_backup:$fd" "$_filesroot/$_dir"; then
- # echo "Did not successfully backup files from --ssh-backup"
- # echo "Exiting now to prevent data loss!"
- # exit 1
- # fi
- # done
- #}
- # TODO
- #flashImage () {
- #
- # if ! scp -rp "$_bin_path" "$_ssh_backup:/tmp/$_bin_name"; then
- # echo "Could not copy update file to device!"
- # exit 1
- # fi
- #
- #3 # shellcheck disable=SC2029
- # if ! ssh "$_ssh_backup" "sysupgrade -v /tmp/$_bin_name"; then
- #3 echo "sysupgrade failed!"
- # exit 1
- # fi
- #}
- __main () {
- parseInput "$@"
- setDefaults
- getOS
- installPrerequisites
- acquireImageBuilder
- extractImageBuilder
- [[ -n $_ssh_backup ]] && sshBackup
- makeImage
- #flashImage # TODO
- exit 0
- }
- __main "$@"
|