Cleanup global scope
This commit is contained in:
112
openwrtbuilder
112
openwrtbuilder
@@ -46,9 +46,9 @@ printHelp() {
|
|||||||
--help,-h
|
--help,-h
|
||||||
|
|
||||||
EXAMPLES
|
EXAMPLES
|
||||||
./openwrtbuilder -p r4s -r snapshot --debug
|
./openwrtbuilder -p r4s -r snapshot
|
||||||
./openwrtbuilder -p ax6000_stock -r 23.03.3 --source --debug
|
./openwrtbuilder -p ax6000_stock -r 22.03.3 --source --debug
|
||||||
./openwrtbuilder -p rpi4 -r 23.03.3 --flash /dev/sdX
|
./openwrtbuilder -p rpi4 -r 22.03.3 --flash /dev/sdX
|
||||||
./openwrtbuilder -p linksys -r snapshot --ssh-upgrade root@192.168.1.1
|
./openwrtbuilder -p linksys -r snapshot --ssh-upgrade root@192.168.1.1
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
@@ -59,7 +59,7 @@ init() {
|
|||||||
|
|
||||||
declare -g ID RPM_MGR SCRIPTDIR DL_TOOL
|
declare -g ID RPM_MGR SCRIPTDIR DL_TOOL
|
||||||
|
|
||||||
debug || echo "To enable debugging output, use --debug or -d"
|
(( DEBUG )) || echo "To enable debugging output, use --debug or -d"
|
||||||
|
|
||||||
# Save the script directory
|
# Save the script directory
|
||||||
# https://stackoverflow.com/a/4774063
|
# https://stackoverflow.com/a/4774063
|
||||||
@@ -140,7 +140,7 @@ init() {
|
|||||||
elif hash curl &>/dev/null; then
|
elif hash curl &>/dev/null; then
|
||||||
DL_TOOL="curl"
|
DL_TOOL="curl"
|
||||||
else
|
else
|
||||||
echo "Downloading the Image Builder requires axel or curl!"
|
echo "Downloading the Image Builder requires axel or curl"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@@ -359,6 +359,8 @@ installDependencies() {
|
|||||||
getImageBuilder() {
|
getImageBuilder() {
|
||||||
debug "${FUNCNAME[0]}"
|
debug "${FUNCNAME[0]}"
|
||||||
|
|
||||||
|
declare url="$1"
|
||||||
|
|
||||||
if [[ -f "$IB_ARCHIVE" ]]; then
|
if [[ -f "$IB_ARCHIVE" ]]; then
|
||||||
if askOK "$IB_ARCHIVE exists. Re-download?"; then
|
if askOK "$IB_ARCHIVE exists. Re-download?"; then
|
||||||
rm -f "$IB_ARCHIVE"
|
rm -f "$IB_ARCHIVE"
|
||||||
@@ -368,8 +370,8 @@ getImageBuilder() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Downloading Image Builder archive using $DL_TOOL"
|
echo "Downloading Image Builder archive using $DL_TOOL"
|
||||||
debug "$DL_TOOL -o $IB_ARCHIVE $IB_URL"
|
debug "$DL_TOOL -o $IB_ARCHIVE $url"
|
||||||
"$DL_TOOL" -o "$IB_ARCHIVE" "$IB_URL"
|
"$DL_TOOL" -o "$IB_ARCHIVE" "$url"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -384,8 +386,7 @@ getImageBuilderChecksum() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
debug "$DL_TOOL -o $IB_SHA256_FILE $IB_SHA256_URL"
|
execute "$DL_TOOL -o $IB_SHA256_FILE $IB_SHA256_URL"
|
||||||
"$DL_TOOL" -o "$IB_SHA256_FILE" "$IB_SHA256_URL"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -404,37 +405,33 @@ addRepos() {
|
|||||||
sshBackup() {
|
sshBackup() {
|
||||||
debug "${FUNCNAME[0]}"
|
debug "${FUNCNAME[0]}"
|
||||||
|
|
||||||
local _date _hostname _backup_fname
|
declare date hostname backup_fname
|
||||||
|
|
||||||
[[ -d "$FILESDIR" ]] || mkdir -p "$FILESDIR"
|
[[ -d "$FILESDIR" ]] || mkdir -p "$FILESDIR"
|
||||||
|
|
||||||
printf -v _date '%(%Y-%m-%d-%H-%M-%S)T'
|
printf -v date '%(%Y-%m-%d-%H-%M-%S)T'
|
||||||
_hostname=$(ssh -qt "$SSH_BACKUP_PATH" echo -n \$HOSTNAME)
|
hostname=$(ssh -qt "$SSH_BACKUP_PATH" echo -n \$HOSTNAME)
|
||||||
_backup_fname="backup-$_hostname-$_date.tar.gz"
|
backup_fname="backup-$hostname-$date.tar.gz"
|
||||||
|
|
||||||
# Make backup archive on remote
|
# Make backup archive on remote
|
||||||
debug "ssh -t $SSH_BACKUP_PATH sysupgrade -b /tmp/$_backup_fname"
|
if ! execute "ssh -t $SSH_BACKUP_PATH sysupgrade -b /tmp/$backup_fname"; then
|
||||||
if ! ssh -t "$SSH_BACKUP_PATH" "sysupgrade -b /tmp/$_backup_fname"; then
|
|
||||||
echo "SSH backup failed"
|
echo "SSH backup failed"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Move backup archive locally
|
# Move backup archive locally
|
||||||
debug "rsync -avz --remove-source-files $SSH_BACKUP_PATH:/tmp/$_backup_fname $BUILDDIR/"
|
if ! execute "rsync -avz --remove-source-files $SSH_BACKUP_PATH:/tmp/$backup_fname $BUILDDIR/"; then
|
||||||
if ! rsync -avz --remove-source-files \
|
|
||||||
"$SSH_BACKUP_PATH":"/tmp/$_backup_fname" "$BUILDDIR/"; then
|
|
||||||
echo "Could not copy SSH backup"
|
echo "Could not copy SSH backup"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Extract backup archive
|
# Extract backup archive
|
||||||
debug "tar -C $FILESDIR -xzf $BUILDDIR/$_backup_fname"
|
if ! execute "tar -C $FILESDIR -xzf $BUILDDIR/$backup_fname"; then
|
||||||
if ! tar -C "$FILESDIR" -xzf "$BUILDDIR/$_backup_fname"; then
|
|
||||||
echo "Could not extract SSH backup"
|
echo "Could not extract SSH backup"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
rm "$BUILDDIR/$_backup_fname"
|
execute "rm $BUILDDIR/$backup_fname"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -452,8 +449,8 @@ makeImages() {
|
|||||||
|
|
||||||
make image \
|
make image \
|
||||||
BIN_DIR="$BINDIR" \
|
BIN_DIR="$BINDIR" \
|
||||||
PROFILE="${P_ARR[profile]}" \
|
PROFILE="$PROFILE" \
|
||||||
PACKAGES="${P_ARR[packages]:+"${P_ARR[packages]}"}" \
|
PACKAGES="$PACKAGES" \
|
||||||
FILES="${FILESDIR}" \
|
FILES="${FILESDIR}" \
|
||||||
--directory="$BUILDDIR" \
|
--directory="$BUILDDIR" \
|
||||||
--jobs="$(nproc)" \
|
--jobs="$(nproc)" \
|
||||||
@@ -526,7 +523,7 @@ sshUpgrade() {
|
|||||||
|
|
||||||
echo "Copying '$img_gz' to $ssh_path/tmp/$img_fname"
|
echo "Copying '$img_gz' to $ssh_path/tmp/$img_fname"
|
||||||
debug "scp $img_gz $ssh_path:/tmp/$img_fname"
|
debug "scp $img_gz $ssh_path:/tmp/$img_fname"
|
||||||
if ! scp "$img_gz" "$ssh_path":"/tmp/$img_fname"; then
|
if ! scp "$img_gz" "$ssh_path:/tmp/$img_fname"; then
|
||||||
echo "Could not copy $img_gz to $ssh_path:/tmp/$img_fname"
|
echo "Could not copy $img_gz to $ssh_path:/tmp/$img_fname"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
@@ -543,12 +540,11 @@ sshUpgrade() {
|
|||||||
fromSource() {
|
fromSource() {
|
||||||
debug "${FUNCNAME[0]}"
|
debug "${FUNCNAME[0]}"
|
||||||
|
|
||||||
|
declare src_dir="$BUILDROOT/src/openwrt"
|
||||||
declare src_url="https://github.com/openwrt/openwrt.git"
|
declare src_url="https://github.com/openwrt/openwrt.git"
|
||||||
declare pkg kopt opt commit gitworktreedir seed_file
|
declare pkg kopt opt commit gitworktreedir seed_file
|
||||||
declare -a make_opts config_opts
|
declare -a make_opts config_opts
|
||||||
|
|
||||||
declare src_dir="$BUILDROOT/src/openwrt"
|
|
||||||
|
|
||||||
echo "Building from source is under development"
|
echo "Building from source is under development"
|
||||||
|
|
||||||
# Update source code
|
# Update source code
|
||||||
@@ -602,7 +598,7 @@ fromSource() {
|
|||||||
config_opts+=("CONFIG_BINARY_FOLDER=\"$BINDIR\"")
|
config_opts+=("CONFIG_BINARY_FOLDER=\"$BINDIR\"")
|
||||||
|
|
||||||
# Add custom packages
|
# Add custom packages
|
||||||
for pkg in ${P_ARR[packages]}; do
|
for pkg in $PACKAGES; do
|
||||||
if [[ $pkg == -* ]]; then
|
if [[ $pkg == -* ]]; then
|
||||||
config_opts+=("CONFIG_PACKAGE_${pkg#-}=n") # remove package
|
config_opts+=("CONFIG_PACKAGE_${pkg#-}=n") # remove package
|
||||||
else
|
else
|
||||||
@@ -629,8 +625,8 @@ fromSource() {
|
|||||||
# Only compile selected target image
|
# Only compile selected target image
|
||||||
sed -i '/CONFIG_TARGET_DEVICE_/d' "$seed_file"
|
sed -i '/CONFIG_TARGET_DEVICE_/d' "$seed_file"
|
||||||
config_opts+=("CONFIG_TARGET_MULTI_PROFILE=n")
|
config_opts+=("CONFIG_TARGET_MULTI_PROFILE=n")
|
||||||
config_opts+=("CONFIG_TARGET_PROFILE=DEVICE_${P_ARR[profile]}")
|
config_opts+=("CONFIG_TARGET_PROFILE=DEVICE_$PROFILE")
|
||||||
config_opts+=("CONFIG_TARGET_${P_ARR[target]//\//_}_DEVICE_${P_ARR[profile]}=y")
|
config_opts+=("CONFIG_TARGET_${TARGET//\//_}_DEVICE_$PROFILE=y")
|
||||||
config_opts+=("CONFIG_SDK=n")
|
config_opts+=("CONFIG_SDK=n")
|
||||||
config_opts+=("CONFIG_SDK_LLVM_BPF=n")
|
config_opts+=("CONFIG_SDK_LLVM_BPF=n")
|
||||||
config_opts+=("CONFIG_IB=n")
|
config_opts+=("CONFIG_IB=n")
|
||||||
@@ -665,7 +661,7 @@ fromSource() {
|
|||||||
popd &>/dev/null || return 1
|
popd &>/dev/null || return 1
|
||||||
|
|
||||||
# Provide symlinks to images in root of BINDIR (to match Image Builder)
|
# Provide symlinks to images in root of BINDIR (to match Image Builder)
|
||||||
for image in "$BINDIR/targets/${P_ARR[target]}/"*.{img,img.gz,ubi}; do
|
for image in "$BINDIR/targets/${TARGET}/"*.{img,img.gz,ubi}; do
|
||||||
ln -fs "$image" "$BINDIR/${image##*/}"
|
ln -fs "$image" "$BINDIR/${image##*/}"
|
||||||
done
|
done
|
||||||
|
|
||||||
@@ -707,6 +703,11 @@ load() {
|
|||||||
# shellcheck disable=SC1090
|
# shellcheck disable=SC1090
|
||||||
[[ -f $source_file ]] && source "$source_file"
|
[[ -f $source_file ]] && source "$source_file"
|
||||||
}
|
}
|
||||||
|
execute() {
|
||||||
|
declare cmd="$*"
|
||||||
|
debug "$cmd" || cmd+=" &>/dev/null"
|
||||||
|
eval "${cmd[*]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
@@ -714,16 +715,20 @@ main() {
|
|||||||
|
|
||||||
init
|
init
|
||||||
|
|
||||||
# shellcheck source=./profiles
|
|
||||||
load "$SCRIPTDIR/profiles"
|
load "$SCRIPTDIR/profiles"
|
||||||
|
|
||||||
readInput "$@"
|
readInput "$@"
|
||||||
|
|
||||||
# Fallback to SCRIPTDIR if BUILDROOT has not been set
|
# Fallback to SCRIPTDIR if BUILDROOT has not been set
|
||||||
declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
|
declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
|
||||||
[[ $BUILDROOT == "/" ]] && echo "Invalid --buildroot" && exit 1
|
|
||||||
declare -g FILESDIR="${FILESDIR:=$BUILDROOT/src/files}"
|
declare -g FILESDIR="${FILESDIR:=$BUILDROOT/src/files}"
|
||||||
|
|
||||||
|
# This could be dangerous
|
||||||
|
if [[ $BUILDROOT == "/" ]]; then
|
||||||
|
echo "Invalid --buildroot"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
for dir in "$BUILDROOT/src" "$BUILDROOT/bin"; do
|
for dir in "$BUILDROOT/src" "$BUILDROOT/bin"; do
|
||||||
[[ -d "$dir" ]] || mkdir -p "$dir"
|
[[ -d "$dir" ]] || mkdir -p "$dir"
|
||||||
done
|
done
|
||||||
@@ -754,23 +759,35 @@ main() {
|
|||||||
declare -g BUILDDIR="$BUILDROOT/src/$profile/$RELEASE"
|
declare -g BUILDDIR="$BUILDROOT/src/$profile/$RELEASE"
|
||||||
declare -g BINDIR="$BUILDROOT/bin/$profile/$RELEASE"
|
declare -g BINDIR="$BUILDROOT/bin/$profile/$RELEASE"
|
||||||
declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
|
declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
|
||||||
|
declare -g TARGET="${P_ARR[target]}"
|
||||||
|
declare -g PROFILE="${P_ARR[profile]}"
|
||||||
|
declare -g PACKAGES="${P_ARR[packages]:-}"
|
||||||
|
|
||||||
|
if (( RESET )); then
|
||||||
|
askOk "Remove $BUILDDIR and $BINDIR?" || exit $?
|
||||||
|
debug "rm -rf $BUILDDIR $BINDIR"
|
||||||
|
rm -rf "$BUILDDIR" "$BINDIR"
|
||||||
|
fi
|
||||||
|
|
||||||
|
[[ -d $BUILDDIR ]] || mkdir -p "$BUILDDIR"
|
||||||
|
|
||||||
if [[ "$RELEASE" == "snapshot" ]]; then
|
if [[ "$RELEASE" == "snapshot" ]]; then
|
||||||
declare url_prefix="https://downloads.openwrt.org/snapshots/targets/${P_ARR[target]}"
|
declare url_prefix="https://downloads.openwrt.org/snapshots/targets/$TARGET"
|
||||||
declare url_filename="openwrt-imagebuilder-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
|
declare url_filename="openwrt-imagebuilder-${TARGET//\//-}.Linux-x86_64.tar.xz"
|
||||||
declare img_fname="openwrt-${P_ARR[target]//\//-}-${P_ARR[profile]}-$FILESYSTEM"
|
declare img_fname="openwrt-${TARGET//\//-}-$PROFILE-$FILESYSTEM"
|
||||||
else
|
else
|
||||||
declare url_prefix="https://downloads.openwrt.org/releases/$RELEASE/targets/${P_ARR[target]}"
|
declare url_prefix="https://downloads.openwrt.org/releases/$RELEASE/targets/$TARGET"
|
||||||
declare url_filename="openwrt-imagebuilder-$RELEASE-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
|
declare url_filename="openwrt-imagebuilder-$RELEASE-${TARGET//\//-}.Linux-x86_64.tar.xz"
|
||||||
declare img_fname="openwrt-$RELEASE-${P_ARR[target]//\//-}-${P_ARR[profile]}-$FILESYSTEM"
|
declare img_fname="openwrt-$RELEASE-${TARGET//\//-}-$PROFILE-$FILESYSTEM"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
declare ib_url="$url_prefix/$url_filename"
|
||||||
|
|
||||||
if (( FROM_SOURCE )); then
|
if (( FROM_SOURCE )); then
|
||||||
declare -g SYSUPGRADEIMGGZ="$BINDIR/targets/$img_fname-sysupgrade.img.gz"
|
declare -g SYSUPGRADEIMGGZ="$BINDIR/targets/$img_fname-sysupgrade.img.gz"
|
||||||
declare -g SEED_URL="$url_prefix/config.buildinfo"
|
declare -g SEED_URL="$url_prefix/config.buildinfo"
|
||||||
else
|
else
|
||||||
declare -g SYSUPGRADEIMGGZ="$BUILDDIR/$img_fname-sysupgrade.img.gz"
|
declare -g SYSUPGRADEIMGGZ="$BUILDDIR/$img_fname-sysupgrade.img.gz"
|
||||||
declare -g IB_URL="$url_prefix/$url_filename"
|
|
||||||
declare -g IB_ARCHIVE="$BUILDDIR/$url_filename"
|
declare -g IB_ARCHIVE="$BUILDDIR/$url_filename"
|
||||||
declare -g IB_SHA256_URL="$url_prefix/sha256sums"
|
declare -g IB_SHA256_URL="$url_prefix/sha256sums"
|
||||||
declare -g IB_SHA256_FILE="$IB_ARCHIVE.sha256sums"
|
declare -g IB_SHA256_FILE="$IB_ARCHIVE.sha256sums"
|
||||||
@@ -782,30 +799,23 @@ main() {
|
|||||||
for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
|
for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
|
||||||
echo "Build settings:"
|
echo "Build settings:"
|
||||||
cat <<- EOF
|
cat <<- EOF
|
||||||
ALIAS (\$profile, \$P_ARR)=$profile, $P_ARR
|
ALIAS (\$profile, \$P_ARR -- should match)=$profile, $P_ARR
|
||||||
BUILDROOT=$BUILDROOT
|
BUILDROOT=$BUILDROOT
|
||||||
BUILDDIR=$BUILDDIR
|
BUILDDIR=$BUILDDIR
|
||||||
BINDIR=$BINDIR
|
BINDIR=$BINDIR
|
||||||
TARGET=${P_ARR[target]}
|
TARGET=$TARGET
|
||||||
PROFILE=${P_ARR[profile]}
|
PROFILE=$PROFILE
|
||||||
RELEASE=$RELEASE
|
RELEASE=$RELEASE
|
||||||
FILESYSTEM=$FILESYSTEM
|
FILESYSTEM=$FILESYSTEM
|
||||||
SYSUPGRADEIMGGZ=$SYSUPGRADEIMGGZ
|
SYSUPGRADEIMGGZ=$SYSUPGRADEIMGGZ
|
||||||
|
ib_url=$ib_url
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if (( RESET )); then
|
|
||||||
askOk "Remove $BUILDDIR and $BINDIR?" || exit $?
|
|
||||||
debug "rm -rf $BUILDDIR $BINDIR"
|
|
||||||
rm -rf "$BUILDDIR" "$BINDIR"
|
|
||||||
fi
|
|
||||||
|
|
||||||
[[ -d $BUILDDIR ]] || mkdir -p "$BUILDDIR"
|
|
||||||
|
|
||||||
if (( FROM_SOURCE )); then
|
if (( FROM_SOURCE )); then
|
||||||
fromSource || return $?
|
fromSource || return $?
|
||||||
else
|
else
|
||||||
getImageBuilder &&
|
getImageBuilder "$ib_url" &&
|
||||||
getImageBuilderChecksum &&
|
getImageBuilderChecksum &&
|
||||||
verify "$IB_ARCHIVE" "$IB_SHA256_FILE" &&
|
verify "$IB_ARCHIVE" "$IB_SHA256_FILE" &&
|
||||||
extract "$IB_ARCHIVE" "$BUILDDIR" || return $?
|
extract "$IB_ARCHIVE" "$BUILDDIR" || return $?
|
||||||
|
|||||||
Reference in New Issue
Block a user