Add license and fix inconsistent output file naming

This commit is contained in:
2020-05-20 20:05:20 -04:00
parent 6349c21c93
commit 4b05570ce3

View File

@@ -1,8 +1,26 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#
##################### # This script/function will build and flash/upgrade OpenWRT based on custom profiles
##### DEFAULTS ###### #
##################### # MIT License
# Copyright (c) 2020 Bryan Roessler
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
setDefaults() { setDefaults() {
@@ -20,24 +38,23 @@ setDefaults() {
# Custom profiles # Custom profiles
# TP-Link Archer C7 v2 dumb AP # TP-Link Archer C7 v2 dumb AP
if [[ -z $_target ]]; then if [[ "$_profile" == "tplink_archer-c7-v2" ]]; then
if [[ "$_profile" == "tplink_archer-c7-v2" ]]; then [[ -z $_version ]] && _version="19.07.3"
[[ -z $_version ]] && _version="19.07.2" export _target="ath79/generic"
[[ -z $_target ]] && _target="ath79/generic" export _factory_suffix="squashfs-factory.bin"
_packages+=("-dnsmasq" "-odhcpd") export _sysupgrade_suffix="squashfs-sysupgrade.bin"
# Raspberry Pi 4B router with USB->Ethernet dongle _packages+=("-dnsmasq" "-odhcpd" "-iptables")
elif [[ "$_profile" == "rpi-4" ]]; then # Raspberry Pi 4B router with USB->Ethernet dongle
[[ -z $_version ]] && _version="snapshot" elif [[ "$_profile" == "rpi-4" ]]; then
[[ -z $_target ]] && _target="bcm27xx/bcm2711" [[ -z $_version ]] && _version="snapshot"
_packages+=("kmod-usb-net-asix-ax88179" "luci-app-upnp" "luci-app-wireguard" \ export _target="bcm27xx/bcm2711"
"luci-app-vpn-policy-routing" "-dnsmasq" "dnsmasq-full" "luci-app-ddns") export _factory_suffix="ext4-factory.img"
fi export _sysupgrade_suffix="ext4-sysupgrade.img"
_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() { printHelpAndExit() {
@@ -51,7 +68,6 @@ If PROFILE is set and TARGET is not, buildOpenwrt can use a custom profile speci
OPTIONS OPTIONS
--version, -v OPENWRT_VERSION --version, -v OPENWRT_VERSION
--target, -t TARGET
--profile, -p PROFILE --profile, -p PROFILE
--builddir, -b PATH --builddir, -b PATH
--ssh-upgrade SSH_PATH --ssh-upgrade SSH_PATH
@@ -70,16 +86,13 @@ parseInput() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
if _input=$(getopt -o +v:t:p:b:f:dh -l version:,target:,profile:,builddir:,ssh-upgrade:,flash:,debug,help -- "$@"); then if _input=$(getopt -o +v:p:b:f:dh -l version:,profile:,builddir:,ssh-upgrade:,flash:,debug,help -- "$@"); then
eval set -- "$_input" eval set -- "$_input"
while true; do while true; do
case "$1" in case "$1" in
--version|-v) --version|-v)
shift && _version="$1" shift && _version="$1"
;; ;;
--target|-t)
shift && _target="$1"
;;
--profile|-p) --profile|-p)
shift && _profile="$1" shift && _profile="$1"
;; ;;
@@ -137,11 +150,23 @@ setVars() {
export _source_archive="$_builddir/sources/$_profile-$_version.tar.xz" export _source_archive="$_builddir/sources/$_profile-$_version.tar.xz"
export _source_dir="${_source_archive%.tar.xz}" export _source_dir="${_source_archive%.tar.xz}"
export _out_bin_dir="$_builddir/bin/$_profile-$_version/" 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}"
if [[ "$_version" == "snapshot" ]]; then
local _out_prefix="$_out_bin_dir/openwrt-${_target//\//-}-$_profile"
else
local _out_prefix="$_out_bin_dir/openwrt-$_version-${_target//\//-}-$_profile"
fi
export _factory_bin="$_out_prefix-$_factory_suffix"
export _factory_bin_fname="${_factory_bin##*/}"
export _factory_bin_gz="$_factory_bin.gz"
export _factory_bin_gz_fname="${_factory_bin_gz##*/}"
export _sysupgrade_bin="$_out_prefix-$_sysupgrade_suffix"
export _sysupgrade_bin_fname="${_sysupgrade_bin##*/}"
export _sysupgrade_bin_gz="$_sysupgrade_bin.gz"
export _sysupgrade_bin_gz_fname="${_sysupgrade_bin_gz##*/}"
} }
@@ -267,7 +292,9 @@ flashImage() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
extractImage "$_out_bin_gz" if [[ -z $_factory_bin && -f "$_factory_bin_gz" ]]; then
extractImage "$_factory_bin_gz"
fi
if [[ ! -d "$_flash_dev" ]]; then if [[ ! -d "$_flash_dev" ]]; then
echo "The device specified by --flash could not be found" echo "The device specified by --flash could not be found"
@@ -278,8 +305,8 @@ flashImage() {
debug "umount $_flash_dev?*" debug "umount $_flash_dev?*"
sudo umount "$_flash_dev?*" sudo umount "$_flash_dev?*"
debug "sudo dd if=\"$_out_bin\" of=\"$_flash_dev\" bs=2M conv=fsync" debug "sudo dd if=\"$_factory_bin\" of=\"$_flash_dev\" bs=2M conv=fsync"
if sudo dd if="$_out_bin" of="$_flash_dev" bs=2M conv=fsync; then if sudo dd if="$_factory_bin" of="$_flash_dev" bs=2M conv=fsync; then
sync sync
echo "Image flashed sucessfully!" echo "Image flashed sucessfully!"
else else
@@ -293,20 +320,29 @@ sshUpgrade() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
local _out_bin_gz_name="${_out_bin_gz##*/}" if [[ -f "$_sysupgrade_bin_gz" ]]; then
local _source="$_sysupgrade_bin_gz"
local _source_fname="$_sysupgrade_bin_gz_fname"
elif [[ -f "$_sysupgrade_bin" ]]; then
local _source="$_sysupgrade_bin"
local _source_fname="$_sysupgrade_bin_fname"
else
echo "Could not find upgrade file"
exit 1
fi
echo "Copying upgrade image to $_ssh_upgrade_path" echo "Copying \"$_source\" to $_ssh_upgrade_path/tmp/"
debug "scp \"$_out_bin_gz\" \"$_ssh_upgrade_path:/tmp/$_out_bin_gz_name\"" debug "scp \"$_source\" \"$_ssh_upgrade_path\":\"/tmp/$_source_fname\""
# shellcheck disable=SC2140 # shellcheck disable=SC2140
if ! scp "$_out_bin_gz" "$_ssh_upgrade_path":"/tmp/$_out_bin_gz_name"; then if ! scp "$_source" "$_ssh_upgrade_path":"/tmp/$_source_fname"; then
echo "Could not access the --ssh-upgrade PATH" echo "Could not access the --ssh-upgrade PATH"
exit 1 exit 1
fi fi
echo "Executing remote sysupgrade" echo "Executing remote sysupgrade"
debug "ssh \"$_ssh_upgrade_path\" \"sysupgrade -F /tmp/$_out_bin_gz_name\"" debug "ssh \"$_ssh_upgrade_path\" \"sysupgrade -F /tmp/$_source_fname\""
# shellcheck disable=SC2029
ssh "$_ssh_upgrade_path" "sysupgrade -F /tmp/$_out_bin_gz_name" #ssh "$_ssh_upgrade_path" "sysupgrade -F /tmp/$_source_fname"
} }