123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- #!/usr/bin/env bash
- #
- # Build and flash/upgrade OpenWRT devices
- #
- # Apache 2.0 License
- printHelpAndExit() {
- debug "${FUNCNAME[0]}"
- cat <<-'EOF'
- USAGE:
- openwrtbuilder [[OPTION] [VALUE]]...
- Run and deploy OpenWRT imagebuilder.
- OPTIONS
- --profile, -p PROFILE
- --info, -i PROFILE
- --list-profiles, -l
- --release, -r RELEASE
- --builddir, -b PATH
- --ssh-upgrade HOST
- Example: root@192.168.1.1
- --ssh-backup SSH_PATH
- (For testing, enabled by default for --ssh-upgrade)
- --flash, -f DEVICE
- Example: /dev/sdX
- --reset
- CLeanup all source and output files
- --debug, -d
- --help, -h
- EOF
- # Exit using passed exit code
- [[ -z $1 ]] && exit 0 || exit "$1"
- }
- input() {
- debug "${FUNCNAME[0]}"
- unset RESET
- if _input=$(getopt -o +v:p:i:lb:f:dh -l release:,profile:,info:,list-profiles,builddir:,ssh-upgrade:,ssh-backup:,flash:,reset,debug,help -- "$@"); then
- eval set -- "$_input"
- while true; do
- case "$1" in
- --release|-r)
- shift && RELEASE="$1"
- ;;
- --profile|-p)
- shift && PROFILE="$1"
- ;;
- --info|-i)
- shift && profileInfo "$1" && exit $?
- ;;
- --list-profile|-l)
- listProfiles && exit $?
- ;;
- --builddir|-b)
- shift && BUILDDIR="$1"
- ;;
- --ssh-upgrade)
- shift && SSH_UPGRADE_PATH="$1"
- ;;
- --ssh-backup)
- shift && SSH_BACKUP_PATH="$1"
- ;;
- --flash|-f)
- shift && FLASH_DEV="$1"
- ;;
- --reset)
- RESET=true
- ;;
- --debug|-d)
- echo "Debugging on"
- DEBUG=true
- ;;
- --help|-h)
- printHelpAndExit 0
- ;;
- --)
- shift
- break
- ;;
- esac
- shift
- done
- else
- echo "Incorrect options provided"
- printHelpAndExit 1
- fi
- }
- profiles() {
- debug "${FUNCNAME[0]}"
- # Additional packages to install for all profiles
- default_packages="\
- luci \
- luci-ssl \
- nano \
- htop \
- tcpdump \
- diffutils \
- tar \
- iperf \
- rsync " # Leave trailing whitespace
- # Set the default release
- [[ -z $RELEASE ]] && RELEASE="21.02.1"
- # Use these tools to add and parse profiles
- declare -ag PROFILES
- add_profile() {
- declare -Ag "$1"
- PROFILES+=("$1")
- }
- add_profile archer
- archer['profile']="tplink_archer-c7-v2"
- archer['target']="ath79/generic"
- archer['filesystem']="squashfs"
- # shellcheck disable=SC2034 # Indirect
- archer['packages']="\
- $default_packages \
- -dnsmasq \
- -odhcpd \
- -iptables \
- -ath10k-firmware-qca988x-ct \
- ath10k-firmware-qca988x-ct-full-htt"
- add_profile linksys
- linksys['profile']="linksys_ea8300"
- linksys['target']="ipq40xx/generic"
- linksys['filesystem']="squashfs"
- # shellcheck disable=SC2034 # Indirect
- linksys['packages']="\
- $default_packages \
- -dnsmasq \
- -odhcpd \
- -iptables"
- add_profile rpi4
- rpi4['profile']="rpi-4"
- rpi4['target']="bcm27xx/bcm2711"
- rpi4['filesystem']="ext4"
- # shellcheck disable=SC2034 # Indirect
- rpi4['packages']="\
- $default_packages \
- kmod-usb-net-asix-ax88179 \
- kmod-usb-net-rtl8152 \
- luci-app-upnp \
- luci-app-wireguard \
- luci-app-vpn-policy-routing \
- -dnsmasq \
- dnsmasq-full \
- luci-app-ddns \
- luci-app-sqm"
- add_profile r2s
- r2s['profile']="friendlyarm_nanopi-r2s"
- r2s['target']="rockchip/armv8"
- r2s['filesystem']="ext4"
- # shellcheck disable=SC2034 # Indirect
- r2s['packages']="\
- $default_packages \
- luci-app-upnp \
- luci-app-wireguard \
- luci-app-vpn-policy-routing \
- -dnsmasq \
- dnsmasq-full \
- luci-app-ddns \
- luci-app-sqm \
- luci-app-statistics \
- collectd-mod-sensors \
- collectd-mod-thermal \
- collectd-mod-conntrack \
- smcroute \
- curl \
- ethtool"
- add_profile r4s
- r4s['release']="snapshot"
- r4s['profile']="friendlyarm_nanopi-r4s"
- r4s['target']="rockchip/armv8"
- r4s['filesystem']="ext4"
- # shellcheck disable=SC2034 # Indirect
- r4s['packages']="\
- $default_packages \
- luci-app-upnp \
- luci-app-wireguard \
- luci-app-vpn-policy-routing \
- -dnsmasq \
- dnsmasq-full \
- luci-app-ddns \
- luci-app-sqm \
- luci-app-statistics \
- collectd-mod-sensors \
- collectd-mod-thermal \
- collectd-mod-conntrack \
- smcroute \
- curl \
- ethtool"
- for PNAME in "${PROFILES[@]}"; do
- declare -n ARR="$PNAME"
- local _out_prefix
- [[ ! -v ARR['release'] ]] && ARR['release']="$RELEASE"
- ARR['source_archive']="$BUILDDIR/sources/${ARR[profile]}-${ARR[release]}.tar.xz"
- ARR['source_dir']="${ARR[source_archive]%.tar.xz}"
- ARR['out_bin_dir']="$BUILDDIR/bin/${ARR[profile]}-${ARR[release]}"
- #_patches_dir="$BUILDDIR/patches/"
- #_files_dir="$BUILDDIR/files/"
- if [[ "${ARR[release]}" == "snapshot" ]]; then
- _out_prefix="${ARR[out_bin_dir]}/openwrt-${ARR[target]//\//-}-${ARR[profile]}"
- else
- _out_prefix="${ARR[out_bin_dir]}/openwrt-${ARR[release]}-${ARR[target]//\//-}-${ARR[profile]}"
- fi
- ARR['factory_img']="$_out_prefix-${ARR[filesystem]}-factory.img"
- ARR['factory_img_gz']="${ARR[factory_img]}.gz"
- ARR['sysupgrade_img']="$_out_prefix-${ARR[filesystem]}-sysupgrade.img"
- ARR['sysupgrade_img_gz']="${ARR[sysupgrade_img]}.gz"
- ARR['sysupgrade_bin']="$_out_prefix-${ARR[filesystem]}-sysupgrade.bin"
- ARR['sysupgrade_bin_fname']="${ARR[sysupgrade_bin]##*/}"
- ARR['sysupgrade_bin_gz']="${ARR[sysupgrade_bin]}.gz"
- ARR['sysupgrade_bin_gz_fname']="${ARR[sysupgrade_bin_gz]##*/}"
- done
- }
- listProfiles() {
- debug "${FUNCNAME[0]}"
- [[ ! -v PROFILES ]] && profiles
- echo "Available profiles:"
- for PNAME in "${PROFILES[@]}"; do
- declare -n ARR2="$PNAME"
- echo "$PNAME: ${ARR2[profile]}"
- done
- }
- profileInfo() {
- debug "${FUNCNAME[0]}"
- local _profile
- _profile="$1"
- [[ ! -v PROFILES ]] && profiles
- declare -n ARR3="$_profile"
- for i in "${!ARR3[@]}"; do
- echo "$i: ${ARR3[i]}"
- done
- }
- prerequisites() {
- debug "${FUNCNAME[0]}"
- local -a _pkg_list
- local _pkg_cmd
- source /etc/os-release
- if [[ "$ID" == "fedora" ]]; then
- _pkg_list=(\
- "@c-development" \
- "@development-tools" \
- "@development-libs" \
- "perl-FindBin" \
- "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
- }
- getImageBuilder() {
- debug "${FUNCNAME[0]}"
- declare -n ARR4="$PROFILE"
- local _url _filename
- if [[ "${ARR4[release]}" == "snapshot" ]]; then
- _filename="openwrt-imagebuilder-${ARR4[target]//\//-}.Linux-x86_64.tar.xz"
- _url="https://downloads.openwrt.org/snapshots/targets/${ARR4[target]}/$_filename"
- if [[ -f "${ARR4[source_archive]}" ]]; then
- if askOk "Update ImageBuilder snapshot?"; then
- rm -f "${ARR4[source_archive]}"
- else
- return 0
- fi
- fi
- else
- _filename="openwrt-imagebuilder-${ARR4[release]}-${ARR4[target]//\//-}.Linux-x86_64.tar.xz"
- _url="https://downloads.openwrt.org/releases/${ARR4[release]}/targets/${ARR4[target]}/$_filename"
- [[ -f "${ARR4[source_archive]}" ]] && return 0 # Reuse existing ImageBuilders
- fi
-
- # Make sources directory if it does not exist
- [[ ! -d "$BUILDDIR/sources" ]] && mkdir -p "$BUILDDIR/sources"
- echo "Downloading imagebuilder archive"
- debug "axel -o ${ARR4[source_archive]} $_url"
- if ! axel -o "${ARR4[source_archive]}" "$_url" > /dev/null 2>&1; then
- echo "Could not download imagebuilder archive"
- exit 1
- fi
- if [[ ! -f "${ARR4[source_archive]}" ]]; then
- echo "Archive missing"
- exit 1
- fi
- echo "Extracting image archive"
- debug "tar -xf ${ARR4[source_archive]} -C ${ARR4[source_dir]} --strip-components 1"
- if ! tar -xf "${ARR4[source_archive]}" -C "${ARR4[source_dir]}" --strip-components 1; then
- echo "Extraction failed"
- exit 1
- fi
- }
- makeImage() {
- debug "${FUNCNAME[0]}"
- declare -n ARR5="$PROFILE"
- # Reuse the existing output
- if [[ -d "${ARR5[out_bin_dir]}" ]]; then
- if askOk "${ARR5[out_bin_dir]} exists. Rebuild?"; then
- rm -rf "${ARR5[out_bin_dir]}"
- else
- return 0
- fi
- fi
- [[ ! -d "${ARR5[out_bin_dir]}" ]] && mkdir -p "${ARR5[out_bin_dir]}"
- # build image
- debug "make -j4 image BIN_DIR=${ARR5[out_bin_dir]} PROFILE=${ARR5[profile]} PACKAGES=${ARR5[packages]} FILES=$FILESDIR --directory=${ARR5[source_dir]} > make.log"
- if ! make image BIN_DIR="${ARR5[out_bin_dir]}" PROFILE="${ARR5[profile]}" PACKAGES="${ARR5[packages]}" FILES="$FILESDIR" --directory="${ARR5[source_dir]}" > make.log; then
- echo "Make image failed!"
- exit 1
- fi
- }
- sshBackup() {
- debug "${FUNCNAME[0]}"
- local _date _hostname _backup_fname
- [[ -d "$FILESDIR" ]] || mkdir -p "$FILESDIR"
- printf -v _date '%(%Y-%m-%d-%H-%M-%S)T'
- _hostname=$(ssh -qt "$SSH_BACKUP_PATH" echo -n \$HOSTNAME)
- _backup_fname="backup-$_hostname-$_date.tar.gz"
- # Make backup archive on remote
- debug "ssh -t $SSH_BACKUP_PATH sysupgrade -b /tmp/$_backup_fname"
- if ! ssh -t "$SSH_BACKUP_PATH" "sysupgrade -b /tmp/$_backup_fname"; then
- echo "SSH backup failed"
- exit 1
- fi
- # Move backup archive locally
- debug "rsync -avz --remove-source-files $SSH_BACKUP_PATH:/tmp/$_backup_fname $BUILDDIR/"
- if ! rsync -avz --remove-source-files "$SSH_BACKUP_PATH":"/tmp/$_backup_fname" "$BUILDDIR/"; then
- echo "Could not copy SSH backup"
- exit 1
- fi
- # Extract backup archive
- debug "tar -C $FILESDIR -xzf $BUILDDIR/$_backup_fname"
- if ! tar -C "$FILESDIR" -xzf "$BUILDDIR/$_backup_fname"; then
- echo "Could not extract SSH backup"
- exit 1
- fi
- rm "$BUILDDIR/$_backup_fname"
- }
- flashImage() {
- debug "${FUNCNAME[0]}"
- declare -n ARR6="$PROFILE"
- local _umount
- if [[ ! -e "$FLASH_DEV" ]]; then
- echo "The device specified by --flash could not be found"
- exit 1
- fi
- # TODO Roughly chooses the correct image
- if [[ -f "${ARR6[factory_img_gz]}" ]]; then
- img_gz="${ARR6[factory_img_gz]}"
- img="${ARR6[factory_img]}"
- elif [[ -f "${ARR6[sysupgrade_img_gz]}" ]]; then
- img_gz="${ARR6[sysupgrade_img_gz]}"
- img="${ARR6[sysupgrade_img]}"
- else
- return 1
- fi
- debug "$img_gz $img"
- debug "gunzip -qfk $img_gz"
- gunzip -qfk "$img_gz"
-
- echo "Unmounting target device $FLASH_DEV partitions"
- _umount=( "$FLASH_DEV"?* )
- debug "umount ${_umount[*]}"
- sudo umount "${_umount[@]}"
- debug "sudo dd if=\"$img\" of=\"$FLASH_DEV\" bs=2M conv=fsync"
- if sudo dd if="$img" of="$FLASH_DEV" bs=2M conv=fsync; then
- sync
- echo "Image flashed sucessfully!"
- else
- echo "dd failed!"
- exit 1
- fi
- }
- sshUpgrade() {
- debug "${FUNCNAME[0]}"
- declare -n ARR7="$PROFILE"
- echo "Copying \"${ARR7[sysupgrade_bin_gz]}\" to $SSH_UPGRADE_PATH/tmp/"
- debug "scp \"${ARR7[sysupgrade_bin_gz]}\" \"$SSH_UPGRADE_PATH\":\"/tmp/${ARR7[sysupgrade_bin_gz_fname]}\""
- # shellcheck disable=SC2140
- if ! scp "${ARR7[sysupgrade_bin_gz]}" "$SSH_UPGRADE_PATH":"/tmp/${ARR7[sysupgrade_bin_gz_fname]}"; then
- echo "Could not access the --ssh-upgrade PATH"
- exit 1
- fi
- echo "Executing remote sysupgrade"
- debug "ssh \"$SSH_UPGRADE_PATH\" \"sysupgrade -F /tmp/${ARR7[sysupgrade_bin_gz_fname]}\""
- # shellcheck disable=SC2029
- ssh "$SSH_UPGRADE_PATH" "sysupgrade -F /tmp/${ARR7[sysupgrade_bin_gz_fname]}"
- }
- debug() { "$DEBUG" && echo "Running: " "$@" ; }
- askOk() {
- local _response
- read -r -p "$* [y/N]" _response
- _response=${_response,,}
- [[ ! "$_response" =~ ^(yes|y)$ ]] && return 1
- return 0
- }
- reset() {
- askOk "Remove $FILESDIR $BUILDDIR/sources $BUILDDIR/bin?" || exit $?
- debug "rm -rf $FILESDIR $BUILDDIR/sources $BUILDDIR/bin"
- rm -rf "$FILESDIR" "${BUILDDIR:?}/sources" "${BUILDDIR:?}/bin"
- }
- main() {
- : "${DEBUG:=false}" # Set to true to enable debugging by default
- scriptdir=$(dirname "$0")
- # scriptdir="$PWD"
- : "${BUILDDIR:=$scriptdir}"
- : "${FILESDIR:=$BUILDDIR/files}"
- input "$@"
- profiles
- [[ -v RESET ]] && reset
- prerequisites
- getImageBuilder
- #copyFiles
- [[ -v SSH_BACKUP_PATH ]] && sshBackup
- if makeImage; then
- [[ -v SSH_UPGRADE_PATH ]] && sshUpgrade
- [[ -v FLASH_DEV ]] && flashImage
- fi
- }
- main "$@"
- exit $?
|