Refactor and change author

This commit is contained in:
2020-05-05 22:22:02 -04:00
parent 9451ce720a
commit 188421eccd
2 changed files with 108 additions and 83 deletions

18
functions.sh Normal file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env bash
extractImage() {
debug "${FUNCNAME[0]}" "$@"
local _gz
[[ $# -lt 1 ]] && echo "extractImage() requires at least one argument" && exit 1
for _gz in "$@"; do
[[ ! -f "$_gz" ]] && return 1
debug "gunzip -qfk $_gz"
if ! gunzip -qfk "$_gz"; then
echo "$_gz extraction failed!"
fi
done
}

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
source ./functions.sh
##################### #####################
##### DEFAULTS ###### ##### DEFAULTS ######
@@ -6,7 +7,7 @@
setDefaults() { setDefaults() {
runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
[[ -z $_debug ]] && _debug="true" # Set to true to enable debugging by default [[ -z $_debug ]] && _debug="true" # Set to true to enable debugging by default
[[ -z $_builddir ]] && _builddir="$PWD" [[ -z $_builddir ]] && _builddir="$PWD"
@@ -28,7 +29,7 @@ setDefaults() {
[[ -z $_version ]] && _version="snapshot" [[ -z $_version ]] && _version="snapshot"
[[ -z $_target ]] && _target="bcm27xx/bcm2711" [[ -z $_target ]] && _target="bcm27xx/bcm2711"
_packages+=("kmod-usb-net-asix-ax88179" "luci-app-upnp" "luci-app-wireguard" \ _packages+=("kmod-usb-net-asix-ax88179" "luci-app-upnp" "luci-app-wireguard" \
"luci-app-vpn-policy-routing" "-dnsmasq" "dnsmasq-full") "luci-app-vpn-policy-routing" "-dnsmasq" "dnsmasq-full" "luci-app-ddns")
fi fi
} }
@@ -36,9 +37,9 @@ setDefaults() {
##### FUNCTIONS ##### ##### FUNCTIONS #####
##################### #####################
printHelpAndExit () { printHelpAndExit() {
runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
cat <<-'EOF' cat <<-'EOF'
USAGE: USAGE:
@@ -49,7 +50,7 @@ OPTIONS
--target, -t TARGET --target, -t TARGET
--profile, -p PROFILE --profile, -p PROFILE
--builddir, -b PATH --builddir, -b PATH
--ssh-backup SSH path --ssh-upgrade SSH_PATH
Example: root@192.168.1.1 Example: root@192.168.1.1
--flash, -f DEVICE --flash, -f DEVICE
Example: /dev/sdX Example: /dev/sdX
@@ -61,11 +62,11 @@ EOF
} }
parseInput () { parseInput() {
runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
if _input=$(getopt -o +v:t:p:b:f:dh -l version:,target:,profile:,builddir:,ssh-backup:,flash:,debug,help -- "$@"); then if _input=$(getopt -o +v:t:p:b:f:dh -l version:,target:,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
@@ -81,8 +82,8 @@ parseInput () {
--builddir|-b) --builddir|-b)
shift && _builddir="$1" shift && _builddir="$1"
;; ;;
--ssh-backup) --ssh-upgrade)
shift && _ssh_backup="$1" shift && _ssh_upgrade_path="$1"
;; ;;
--flash|-f) --flash|-f)
shift && _flash_dev="$1" shift && _flash_dev="$1"
@@ -108,19 +109,20 @@ parseInput () {
} }
runDebug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; } debug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; }
installPrerequisites () { setVars() {
runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
getOS () { getOS () {
runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
if [[ -f /etc/os-release ]]; then if [[ -f /etc/os-release ]]; then
source /etc/os-release source /etc/os-release
export ID="$ID"
echo "Detected platform: $ID" echo "Detected platform: $ID"
else else
echo "Cannot detect OS!" echo "Cannot detect OS!"
@@ -129,28 +131,45 @@ installPrerequisites () {
} }
getOS 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
echo "Installing dependencies..."
if [[ "$ID" == "fedora" ]]; then 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 axel > /dev/null 2>&1; then _pkg_list=("@c-development" "@development-tools" "@development-libs" "zlib-static" "elfutils-libelf-devel" "gawk" "unzip" "file" "wget" "python3" "python2" "axel")
echo "Warning: Problem installing prerequisites" _pkg_cmd="dnf"
fi
elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then 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 axel > /dev/null 2>&1; then _pkg_list=("build-essential" "libncurses5-dev" "libncursesw5-dev" "zlib1g-dev" "gawk" "git" "gettext" "libssl-dev" "xsltproc" "wget" "unzip" "python" "axel")
echo "Warning: Problem installing prerequisites" _pkg_cmd="apt-get"
fi 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 fi
} }
acquireImageBuilder () { acquireImageBuilder() {
runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
local _url _filename local _url _filename
export _source_archive
echo "Downloading image archive..."
if [[ "$_version" == "snapshot" ]]; then if [[ "$_version" == "snapshot" ]]; then
_filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz" _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz"
@@ -163,67 +182,59 @@ acquireImageBuilder () {
# Make sources directory if it does not exist # Make sources directory if it does not exist
[[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources" [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources"
_source_archive="$_builddir/sources/$_profile-$_version.tar.xz"
# Remove existing ImageBuilder archives # Remove existing ImageBuilder archives
[[ -f "$_source_archive" ]] && rm "$_source_archive" [[ -f "$_source_archive" ]] && rm "$_source_archive"
if ! axel -o "$_source_archive" "$_url"; then 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" echo "Could not download Image Builder"
exit 1 exit 1
fi fi
} }
extractImageBuilder () { extractImageBuilder() {
runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
export _source_dir="${_source_archive%.tar.xz}"
[[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir" [[ ! -d "$_source_dir" ]] && mkdir -p "$_source_dir"
echo "Extracting image archive..." if [[ ! -f "$_source_archive" ]]; then
if [[ -f "$_source_archive" ]]; then echo "Archive missing"
if ! tar -xf "$_source_archive" -C "$_source_dir" --strip-components 1; then exit 1
echo "Extraction failed" fi
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 fi
} }
makeImage () { makeImage() {
runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
export _out_bin_gz _out_bin_dir
# move to extracted source directory # move to extracted source directory
if ! pushd "$_source_dir" > /dev/null 2>&1; then if ! pushd "$_source_dir" > /dev/null 2>&1; then
exit 1 exit 1
fi fi
_out_bin_dir="$_builddir/bin/$_profile-$_version/"
# Make bin dir # Make bin dir
[[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir" [[ ! -d "$_out_bin_dir" ]] && mkdir -p "$_out_bin_dir"
# build image # build image
echo "Running make -j4 image BIN_DIR=$_out_bin_dir PROFILE=$_profile PACKAGES=${_packages[*]} FILES=$_filesroot" 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" \ if ! make -j4 image BIN_DIR="$_out_bin_dir" PROFILE="$_profile" \
PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then PACKAGES="${_packages[*]}" FILES="$_filesroot" > make.log; then
echo "Make image failed!" echo "Make image failed!"
exit 1 exit 1
fi fi
_out_bin_gz="$_out_bin_dir/openwrt-${_target//\//-}-$_profile-ext4-factory.img.gz"
if [[ ! -f "$_out_bin_gz" ]]; then
echo "Make output image archive not found"
exit 1
fi
if ! popd > /dev/null 2>&1; then if ! popd > /dev/null 2>&1; then
exit 1 exit 1
fi fi
@@ -232,27 +243,20 @@ makeImage () {
flashImage() { flashImage() {
runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
local _out_bin="${_out_bin_gz%.gz}" extractImage "$_out_bin_gz"
if [[ -f "$_out_bin_gz" ]]; then
gunzip "$_out_bin_gz"
fi
if [[ ! -f "$_out_bin" ]]; then
echo "Cannot find output bin file"
exit 1
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"
exit 1 exit 1
fi fi
echo "Unmounting target device $_flash_dev partitions..." echo "Unmounting target device $_flash_dev partitions"
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"
if sudo dd if="$_out_bin" of="$_flash_dev" bs=2M conv=fsync; then if sudo dd if="$_out_bin" of="$_flash_dev" bs=2M conv=fsync; then
sync sync
echo "Image flashed sucessfully!" echo "Image flashed sucessfully!"
@@ -263,34 +267,37 @@ flashImage() {
} }
#sshBackup () { sshUpgrade() {
#
# runDebug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
#
# # Make files directory if it does not exist local _out_bin_gz_name="${_out_bin_gz##*/}"
# [[ ! -d "$_filesroot" ]] && mkdir -p "$_filesroot"
# echo "Copying upgrade image to $_ssh_upgrade_path"
# for fd in "${_backedup[@]}"; do debug "scp \"$_out_bin_gz\" \"$_ssh_upgrade_path:/tmp/$_out_bin_gz_name\""
# _dir="${fd%/*}/" # shellcheck disable=SC2140
# [[ ! -d "$_filesroot$_dir" ]] && mkdir -p "$_filesroot/$_dir" if ! scp "$_out_bin_gz" "$_ssh_upgrade_path":"/tmp/$_out_bin_gz_name"; then
# if ! scp -rp "$_ssh_backup:$fd" "$_filesroot/$_dir"; then echo "Could not access the --ssh-upgrade PATH"
# echo "Did not successfully backup files from --ssh-backup" exit 1
# echo "Exiting now to prevent data loss!" fi
# exit 1
# fi echo "Executing remote sysupgrade"
# done 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 () { __main() {
parseInput "$@" parseInput "$@"
setDefaults setDefaults
setVars
installPrerequisites installPrerequisites
acquireImageBuilder acquireImageBuilder
extractImageBuilder extractImageBuilder
#[[ -n $_ssh_backup ]] && sshBackup
makeImage makeImage
[[ -n $_ssh_upgrade_path ]] && sshUpgrade
[[ -n $_flash_dev ]] && flashImage [[ -n $_flash_dev ]] && flashImage
} }