123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- #!/usr/bin/env bash
- source ./functions.sh
- #####################
- ##### DEFAULTS ######
- #####################
- setDefaults() {
- debug "${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 -ag _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"
- # Custom profiles
- # TP-Link Archer C7 v2 dumb AP
- if [[ -z $_target ]]; then
- if [[ "$_profile" == "tplink_archer-c7-v2" ]]; then
- [[ -z $_version ]] && _version="19.07.2"
- [[ -z $_target ]] && _target="ath79/generic"
- _packages+=("-dnsmasq" "-odhcpd")
- # Raspberry Pi 4B router with USB->Ethernet dongle
- 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" "luci-app-ddns")
- fi
- fi
- }
- #####################
- ##### FUNCTIONS #####
- #####################
- printHelpAndExit() {
- debug "${FUNCNAME[0]}"
- cat <<-'EOF'
- USAGE:
- openwrtBuild [[OPTION] [VALUE]]...
- If PROFILE is set and TARGET is not, buildOpenwrt can use a custom profile specified in DEFAULTS
- OPTIONS
- --version, -v OPENWRT_VERSION
- --target, -t TARGET
- --profile, -p PROFILE
- --builddir, -b PATH
- --ssh-upgrade SSH_PATH
- Example: root@192.168.1.1
- --flash, -f DEVICE
- Example: /dev/sdX
- --debug, -d
- --help, -h
- EOF
- # Exit using passed exit code
- [[ -z $1 ]] && exit 0 || exit "$1"
- }
- parseInput() {
- debug "${FUNCNAME[0]}"
- if _input=$(getopt -o +v:t:p:b:f:dh -l version:,target:,profile:,builddir:,ssh-upgrade:,flash:,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-upgrade)
- shift && _ssh_upgrade_path="$1"
- ;;
- --flash|-f)
- shift && _flash_dev="$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
- }
- debug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
- setVars() {
- debug "${FUNCNAME[0]}"
- getOS () {
- debug "${FUNCNAME[0]}"
- if [[ -f /etc/os-release ]]; then
- source /etc/os-release
- export ID="$ID"
- echo "Detected platform: $ID"
- else
- echo "Cannot detect OS!"
- exit 1
- fi
- }
- getOS
- export _source_archive="$_builddir/sources/$_profile-$_version.tar.xz"
- export _source_dir="${_source_archive%.tar.xz}"
- export _out_bin_dir="$_builddir/bin/$_profile-$_version/"
- export _out_bin_gz="$_out_bin_dir/openwrt-${_target//\//-}-$_profile-ext4-factory.img.gz"
- export _out_bin="${_out_bin_gz%.gz}"
- }
- installPrerequisites() {
- debug "${FUNCNAME[0]}"
- local -a _pkg_list
- local _pkg_cmd
- if [[ "$ID" == "fedora" ]]; then
- _pkg_list=("@c-development" "@development-tools" "@development-libs" "zlib-static" "elfutils-libelf-devel" "gawk" "unzip" "file" "wget" "python3" "python2" "axel")
- _pkg_cmd="dnf"
- elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then
- _pkg_list=("build-essential" "libncurses5-dev" "libncursesw5-dev" "zlib1g-dev" "gawk" "git" "gettext" "libssl-dev" "xsltproc" "wget" "unzip" "python" "axel")
- _pkg_cmd="apt-get"
- fi
- echo "Installing dependencies"
- debug "sudo $_pkg_cmd -y install ${_pkg_list[*]}"
- if ! sudo "$_pkg_cmd" -y install "${_pkg_list[@]}" > /dev/null 2>&1; then
- echo "Warning: Problem installing prerequisites"
- return 1
- fi
- }
- acquireImageBuilder() {
- debug "${FUNCNAME[0]}"
- local _url _filename
- 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"
- # Remove existing ImageBuilder archives
- [[ -f "$_source_archive" ]] && rm "$_source_archive"
- echo "Downloading image archive"
- debug "axel -o $_source_archive $_url"
- if ! axel -o "$_source_archive" "$_url" > /dev/null 2>&1; then
- echo "Could not download Image Builder"
- exit 1
- fi
- }
- extractImageBuilder() {
- debug "${FUNCNAME[0]}"
- [[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir"
- if [[ ! -f "$_source_archive" ]]; then
- echo "Archive missing"
- exit 1
- fi
- echo "Extracting image archive"
- debug "tar -xf $_source_archive -C $_source_dir --strip-components 1"
- if ! tar -xf "$_source_archive" -C "$_source_dir" --strip-components 1; then
- echo "Extraction failed"
- exit 1
- fi
- }
- makeImage() {
- debug "${FUNCNAME[0]}"
- # move to extracted source directory
- if ! pushd "$_source_dir" > /dev/null 2>&1; then
- exit 1
- fi
- # Make bin dir
- [[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir"
- # build image
- echo "Running make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot"
- debug "make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot > make.log"
- if ! make -j4 image BIN_DIR="$_out_bin_dir" PROFILE="$_profile" \
- PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then
- echo "Make image failed!"
- exit 1
- fi
- if ! popd > /dev/null 2>&1; then
- exit 1
- fi
- }
- flashImage() {
- debug "${FUNCNAME[0]}"
- extractImage "$_out_bin_gz"
- if [[ ! -d "$_flash_dev" ]]; then
- echo "The device specified by --flash could not be found"
- exit 1
- fi
- echo "Unmounting target device $_flash_dev partitions"
- debug "umount $_flash_dev?*"
- sudo umount "$_flash_dev?*"
- debug "sudo dd if=\"$_out_bin\" of=\"$_flash_dev\" bs=2M conv=fsync"
- if sudo dd if="$_out_bin" of="$_flash_dev" bs=2M conv=fsync; then
- sync
- echo "Image flashed sucessfully!"
- else
- echo "dd failed!"
- exit 1
- fi
- }
- sshUpgrade() {
- debug "${FUNCNAME[0]}"
- local _out_bin_gz_name="${_out_bin_gz##*/}"
- echo "Copying upgrade image to $_ssh_upgrade_path"
- debug "scp \"$_out_bin_gz\" \"$_ssh_upgrade_path:/tmp/$_out_bin_gz_name\""
- # shellcheck disable=SC2140
- if ! scp "$_out_bin_gz" "$_ssh_upgrade_path":"/tmp/$_out_bin_gz_name"; then
- echo "Could not access the --ssh-upgrade PATH"
- exit 1
- fi
- echo "Executing remote sysupgrade"
- debug "ssh \"$_ssh_upgrade_path\" \"sysupgrade -F /tmp/$_out_bin_gz_name\""
- # shellcheck disable=SC2029
- ssh "$_ssh_upgrade_path" "sysupgrade -F /tmp/$_out_bin_gz_name"
- }
- __main() {
- parseInput "$@"
- setDefaults
- setVars
- installPrerequisites
- acquireImageBuilder
- extractImageBuilder
- makeImage
- [[ -n $_ssh_upgrade_path ]] && sshUpgrade
- [[ -n $_flash_dev ]] && flashImage
- }
- __main "$@"
- exit $?
|