Compare commits

...

45 Commits

Author SHA1 Message Date
0dfd58b9ef Separate bindirs 2023-01-13 14:41:23 -05:00
36d392c2f2 Integrate BINDIR 2023-01-13 14:32:19 -05:00
4fd858d635 Ensure srcdir and bindir 2023-01-13 11:48:47 -05:00
d342206f0a New directory layout 2023-01-13 11:40:45 -05:00
00b82452f4 Derp 2023-01-13 11:29:55 -05:00
97d7c84b58 Catch return 2023-01-13 11:27:34 -05:00
29ea95db65 Allow continue from failed profile 2023-01-13 10:59:41 -05:00
41900c4e02 Fix duplicate output 2023-01-13 10:51:00 -05:00
ed331cc581 Remove shellcheck disable 2023-01-13 10:47:39 -05:00
cf7b9e45c0 Use SEED_URL 2023-01-13 10:46:37 -05:00
0c1f60971e Prettify help 2023-01-13 10:44:01 -05:00
00c70b9da0 Simplify resetAll() 2023-01-13 10:36:20 -05:00
d14d8f9eb8 Fix reset 2023-01-13 10:26:03 -05:00
f3579cb9a7 Separate profile vars 2023-01-13 10:18:49 -05:00
94e6545fcc More multi-profile work 2023-01-12 17:08:21 -05:00
a4efc9e7d1 More multi-profile work 2023-01-12 16:44:59 -05:00
860631cf7d More multi-profile work 2023-01-12 16:40:39 -05:00
3683296110 More multi-profile work 2023-01-12 16:37:53 -05:00
3ee6de5fb1 More multi-profile work 2023-01-12 16:17:14 -05:00
0bd32d5d7c More multi-profile work 2023-01-12 14:42:39 -05:00
64081a1dda Work on --from-source 2023-01-11 16:15:53 -05:00
08601a3284 Work on --from-source 2023-01-11 16:11:14 -05:00
4cb4e85e4e Work on --from-source 2023-01-11 16:07:38 -05:00
2009725f33 Allow per-profile reset 2023-01-11 15:47:17 -05:00
93eb9afad1 Allow per-profile reset 2023-01-11 15:43:44 -05:00
14e8fcb814 Multi-profile support 2023-01-11 15:13:08 -05:00
dde5d6260a Multi-profile support 2023-01-11 15:11:54 -05:00
3ee0afda05 Multi-profile support 2023-01-11 15:10:52 -05:00
3f4b9042c9 Multi-profile support 2023-01-11 15:08:45 -05:00
c6158f241e Multi-profile support 2023-01-11 15:07:53 -05:00
22dd0f25f9 Multi-profile support 2023-01-11 15:04:18 -05:00
4447ccdd56 Multi-profile support 2023-01-11 14:59:47 -05:00
8ce8ea92fe Multi-profile support 2023-01-11 14:57:04 -05:00
2b1e94da87 Multi-profile support 2023-01-11 14:55:29 -05:00
295855e0ae Multi-profile support 2023-01-11 14:53:00 -05:00
e876247ccd Multi-profile support 2023-01-11 14:47:31 -05:00
b381222022 Multi-profile support 2023-01-11 14:45:59 -05:00
de5f6f02f3 Multi-profile support 2023-01-11 14:42:10 -05:00
b00195dbeb Multi-profile support 2023-01-11 14:39:24 -05:00
ac878b82ee Multi-profile support 2023-01-11 14:36:41 -05:00
242cdb02b8 Allow cli options to override 2023-01-11 14:31:21 -05:00
b9848a177a First stab at multi-profile support 2023-01-11 14:25:52 -05:00
724755d762 Catch input error 2023-01-11 14:02:17 -05:00
6856791364 Rewrite dependency logic for --from-source 2023-01-11 13:48:43 -05:00
3ab01b9d3d Update profiles 2023-01-11 10:21:16 -05:00
2 changed files with 466 additions and 190 deletions

View File

@@ -1,29 +1,29 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# #
# Copyright 2022 Bryan C. Roessler # Copyright 2022-23 Bryan C. Roessler
# #
# Build and flash/upgrade OpenWRT devices # Build and deploy OpenWRT images
# #
# Apache 2.0 License # Apache 2.0 License
# Set default release # Set default release
: "${RELEASE:="22.03.2"}" : "${RELEASE:="22.03.3"}"
printHelp() { printHelp() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
cat <<-'EOF' cat <<-'EOF'
Run and deploy OpenWRT images using the Image Builder. Build and deploy OpenWRT images
USAGE: USAGE:
openwrtbuilder [OPTION [VALUE]]... openwrtbuilder [OPTION [VALUE]] -p PROFILE [-p PROFILE2]...
OPTIONS OPTIONS
--profile, -p PROFILE --profile, -p PROFILE
--info, -i PROFILE --info, -i (print profile info)
--list-profiles, -l --list-profiles, -l
--release, -r, --version, -v RELEASE_VERSION ("snapshot", "22.03.3", etc.) --release, --version, -r, -v RELEASE ("snapshot", "22.03.3")
--builddir, -b PATH --buildroot, -b PATH
--ssh-upgrade HOST --ssh-upgrade HOST
Example: root@192.168.1.1 Example: root@192.168.1.1
--ssh-backup SSH_PATH --ssh-backup SSH_PATH
@@ -38,29 +38,120 @@ EOF
} }
init() {
debug "${FUNCNAME[0]}"
declare -g ID RPM_MGR SCRIPTDIR
debug || echo "To enable debugging output, use --debug or -d"
# Save the script directory
SCRIPTDIR="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit $? ; pwd -P)"
if [[ -e "/etc/os-release" ]]; then
source "/etc/os-release"
else
err "/etc/os-release not found"
err "Your OS is unsupported"
printHelp
exit 1
fi
debug "Detected host platform: $ID"
# normalize distro ID
case "$ID" in
debian|arch)
;;
centos|fedora)
if hash dnf &>/dev/null; then
RPM_MGR="dnf"
elif hash yum &>/dev/null; then
RPM_MGR="yum"
fi
;;
rhel)
ID="centos"
;;
linuxmint|neon|*ubuntu*)
ID="ubuntu"
;;
*suse*)
ID="suse"
;;
raspbian)
ID="debian"
;;
*)
echo "Autodetecting distro, this may be unreliable"
if hash dnf &>/dev/null; then
ID="fedora"
RPM_MGR="dnf"
elif hash yum &>/dev/null; then
ID="centos"
RPM_MGR="yum"
elif hash apt &>/dev/null; then
ID="ubuntu"
elif hash pacman &>/dev/null; then
ID="arch"
else
return 1
fi
;;
esac
debug "Using host platform: $ID"
# Set distro-specific functions
case "$ID" in
fedora|centos)
pkg_install(){ sudo "$RPM_MGR" install -y "$@"; }
;;
debian|ubuntu)
pkg_install(){ sudo apt-get install -y -q0 "$@"; }
;;
suse)
pkg_install(){ sudo zypper --non-interactive -q install --force --no-confirm "$@"; }
;;
arch)
pkg_install(){ sudo pacman -S --noconfirm --needed "$@"; }
;;
esac
}
readInput() { readInput() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
unset RESET unset RESET
if _input=$(getopt -o +r:v:p:i:lb:f:dh -l release:,version:,profile:,info:,list-profiles,builddir:,ssh-upgrade:,ssh-backup:,flash:,reset,debug,help -- "$@"); then declare -ga PROFILES
declare -g PROFILE_INFO
declare long_opts
long_opts='release:,version:,profile:,info:,list-profiles,buildroot:,'
long_opts+='source,ssh-upgrade:,ssh-backup:,flash:,reset,debug,help'
if _input=$(getopt -o +r:v:p:i:lb:sf:dh -l $long_opts -- "$@"); then
eval set -- "$_input" eval set -- "$_input"
while true; do while true; do
case "$1" in case "$1" in
--release|-r|--version|-v) --release|-r|--version|-v)
shift && RELEASE="$1" shift && declare -g USER_RELEASE="$1"
;; ;;
--profile|-p) --profile|-p)
shift && PROFILE="$1" shift && PROFILES+=("$1")
;; ;;
--info|-i) --info|-i)
shift && PROFILE="$1" && PROFILE_INFO=1 && exit $? PROFILE_INFO=1
;; ;;
--list-profiles|-l) --list-profiles|-l)
listProfiles && exit $? listProfiles && exit $?
;; ;;
--builddir|-b) --buildroot|-b)
shift && BUILDDIR="$1" shift && BUILDROOT="$1"
;;
--source|-s)
FROM_SOURCE=1
;; ;;
--ssh-upgrade) --ssh-upgrade)
shift && SSH_UPGRADE_PATH="$1" shift && SSH_UPGRADE_PATH="$1"
@@ -101,113 +192,200 @@ listProfiles() {
} }
installHostDependencies() { installDependencies() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
local -a _pkg_list declare -a pkg_list
local _pkg_cmd
source /etc/os-release # TODO please contribute your platform here
if (( FROM_SOURCE )); then
if [[ "$ID" =~ ^(fedora)$ ]]; then # For building from source with make
_pkg_list=(\ # https://openwrt.org/docs/guide-developer/toolchain/install-buildsystem
"@c-development" \ case "$ID" in
"@development-tools" \ fedora|centos)
"@development-libs" \ pkg_list+=(
"perl-FindBin" \ "bash-completion"
"zlib-static" \ "bzip2"
"elfutils-libelf-devel" \ "gcc"
"gawk" \ "gcc-c++"
"unzip" \ "git"
"file" \ "make"
"wget" \ "ncurses-devel"
"python3" \ "patch"
"python2" \ "rsync"
"axel" \ "tar"
"unzip"
"wget"
"which"
"diffutils"
"python2"
"python3"
"perl-base"
"perl-Data-Dumper"
"perl-File-Compare"
"perl-File-Copy"
"perl-FindBin"
"perl-Thread-Queue"
) )
_pkg_cmd="dnf" ;;
elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then debian|ubuntu)
_pkg_list=(\ pkg_list+=(
"build-essential" \ "build-essential"
"libncurses5-dev" \ "clang"
"libncursesw5-dev" \ "flex"
"zlib1g-dev" \ "g++"
"gawk" \ "gawk"
"git" \ "gcc-multilib"
"gettext" \ "gettext"
"libssl-dev" \ "git"
"xsltproc" \ "libncurses5-dev"
"wget" \ "libssl-dev"
"unzip" \ "python3-distutils"
"python" \ "rsync"
"axel" \ "unzip"
"zlib1g-dev"
"file"
"wget"
) )
_pkg_cmd="apt-get" ;;
arch)
pkg_list+=(
"base-devel"
"autoconf"
"automake"
"bash"
"binutils"
"bison"
"bzip2"
"fakeroot"
"file"
"findutils"
"flex"
"gawk"
"gcc"
"gettext"
"git"
"grep"
"groff"
"gzip"
"libelf"
"libtool"
"libxslt"
"m4"
"make"
"ncurses"
"openssl"
"patch"
"pkgconf"
"python"
"rsync"
"sed"
"texinfo"
"time"
"unzip"
"util-linux"
"wget"
"which"
"zlib"
)
;;
esac
else
# For Imagebuilder
case "$ID" in
fedora|centos)
pkg_list+=(
"@c-development"
"@development-tools"
"@development-libs"
"perl-FindBin"
"zlib-static"
"elfutils-libelf-devel"
"gawk"
"unzip"
"file"
"wget"
"python3"
"python2"
"axel"
)
;;
debian|ubuntu)
pkg_list+=(
"build-essential"
"libncurses5-dev"
"libncursesw5-dev"
"zlib1g-dev"
"gawk"
"git"
"gettext"
"libssl-dev"
"xsltproc"
"wget"
"unzip"
"python"
"axel"
)
;;
esac
fi fi
debug "Installing dependencies" pkg_install "${pkg_list[@]}"
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 dependencies"
return 1
fi
} }
getImageBuilder() { getImageBuilder() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
local _url _filename _dl_tool declare dl_tool
if [[ "${P_ARR[release]}" == "snapshot" ]]; then if [[ -f "$IB_ARCHIVE" ]]; then
_filename="openwrt-imagebuilder-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz" echo "Image Builder $IB_ARCHIVE exists"
_url="https://downloads.openwrt.org/snapshots/targets/${P_ARR[target]}/$_filename" if askOk "Redownload Image Builder archive?"; then
if [[ -f "${P_ARR[source_archive]}" ]]; then rm -f "$IB_ARCHIVE"
if askOk "Update ImageBuilder snapshot?"; then
rm -f "${P_ARR[source_archive]}"
else else
return 0 return 0
fi fi
fi fi
else
_filename="openwrt-imagebuilder-${P_ARR[release]}-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
_url="https://downloads.openwrt.org/releases/${P_ARR[release]}/targets/${P_ARR[target]}/$_filename"
[[ -f "${P_ARR[source_archive]}" ]] && return 0 # Reuse existing ImageBuilders
fi
# Make sources directory if it does not exist
[[ ! -d "$BUILDDIR/sources" ]] && mkdir -p "$BUILDDIR/sources"
if hash axel &>/dev/null; then if hash axel &>/dev/null; then
_dl_tool="axel" dl_tool="axel"
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
#_dl_tool="curl" # TODO remove echo "Downloading Image Builder archive using $dl_tool"
echo "Downloading imagebuilder archive using $_dl_tool" debug "$dl_tool -o $IB_ARCHIVE $IB_URL"
if ! "$dl_tool" -o "$IB_ARCHIVE" "$IB_URL"; then
debug "$_dl_tool -o ${P_ARR[source_archive]} $_url" echo "Could not download Image Builder archive"
if ! "$_dl_tool" -o "${P_ARR[source_archive]}" "$_url"; then return 1
echo "Could not download imagebuilder archive"
exit 1
fi fi
if [[ ! -f "${P_ARR[source_archive]}" ]]; then if [[ ! -f "$IB_ARCHIVE" ]]; then
echo "Archive missing" echo "Archive missing"
exit 1 return 1
fi fi
echo "Extracting image archive"
[[ ! -d "${P_ARR[source_dir]}" ]] && mkdir -p "${P_ARR[source_dir]}" # if hash sha256sum &>/dev/null; then
debug "tar -xf ${P_ARR[source_archive]} -C ${P_ARR[source_dir]} --strip-components 1" # echo "Verifying checksums"
if ! tar -xf "${P_ARR[source_archive]}" -C "${P_ARR[source_dir]}" --strip-components 1; then # debug "$dl_tool -s "${P_ARR[sha256_url]}" | grep $filename | cut -f1 -d' '"
# sha256sum=$($dl_tool -s "${P_ARR[sha256_url]}" |grep "$filename" |cut -f1 -d' ')
# debug "Downloaded sha256sum: $sha256sum"
# fi
echo "Extracting Image Builder archive"
[[ ! -d "$BUILDDIR" ]] && mkdir -p "$BUILDDIR"
debug "tar -xf $IB_ARCHIVE -C $BUILDDIR --strip-components 1"
if ! tar -xf "$IB_ARCHIVE" -C "$BUILDDIR" --strip-components 1; then
echo "Extraction failed" echo "Extraction failed"
exit 1 return 1
fi fi
} }
@@ -216,10 +394,10 @@ addRepos() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
if [[ -v P_ARR[repo] ]]; then if [[ -v P_ARR[repo] ]]; then
if ! grep -q "${P_ARR[repo]}" "${P_ARR[source_dir]}/repositories.conf"; then if ! grep -q "${P_ARR[repo]}" "$BUILDDIR/repositories.conf"; then
echo "${P_ARR[repo]}" >> "${P_ARR[source_dir]}/repositories.conf" echo "${P_ARR[repo]}" >> "$BUILDDIR/repositories.conf"
fi fi
sed -i '/option check_signature/d' "${P_ARR[source_dir]}/repositories.conf" sed -i '/option check_signature/d' "$BUILDDIR/repositories.conf"
fi fi
} }
@@ -244,7 +422,8 @@ sshBackup() {
# Move backup archive locally # Move backup archive locally
debug "rsync -avz --remove-source-files $SSH_BACKUP_PATH:/tmp/$_backup_fname $BUILDDIR/" 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 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
@@ -264,24 +443,24 @@ makeImage() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
# Reuse the existing output # Reuse the existing output
if [[ -d "${P_ARR[out_bin_dir]}" ]]; then if [[ -d "$THIS_BINDIR" ]]; then
if askOk "${P_ARR[out_bin_dir]} exists. Rebuild?"; then if askOk "$THIS_BINDIR exists. Rebuild?"; then
rm -rf "${P_ARR[out_bin_dir]}" rm -rf "$THIS_BINDIR"
else else
return 0 return 0
fi fi
fi fi
[[ ! -d "${P_ARR[out_bin_dir]}" ]] && mkdir -p "${P_ARR[out_bin_dir]}" [[ -d "$BUILDDIR" ]] || mkdir -p "$BUILDDIR"
if ! make image \ if ! make image \
BIN_DIR="${P_ARR[out_bin_dir]}" \ BIN_DIR="$THIS_BINDIR" \
PROFILE="${P_ARR[profile]}" \ PROFILE="${P_ARR[profile]}" \
PACKAGES="${P_ARR[packages]}" \ PACKAGES="${P_ARR[packages]}" \
FILES="$FILESDIR" \ FILES="${FILESDIR}" \
--directory="${P_ARR[source_dir]}" \ --directory="$BUILDDIR" \
--jobs=$(( $(nproc) - 1 )) \ --jobs="$(nproc)" \
> make.log; then > "$BUILDDIR/make.log"; then
echo "Make image failed!" echo "Make image failed!"
exit 1 exit 1
fi fi
@@ -291,20 +470,20 @@ makeImage() {
flashImage() { flashImage() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
local _umount declare img img_gz partitions
if [[ ! -e "$FLASH_DEV" ]]; then if [[ ! -e "$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
# TODO Roughly chooses the correct image # TODO Roughly choose the correct image
if [[ -f "${P_ARR[factory_img_gz]}" ]]; then if [[ -f "$FACTORYIMGGZ" ]]; then
img_gz="${P_ARR[factory_img_gz]}" img_gz="$FACTORYIMGGZ"
img="${P_ARR[factory_img]}" img="$FACTORYIMG"
elif [[ -f "${P_ARR[sysupgrade_img_gz]}" ]]; then elif [[ -f "$SYSUPGRADEIMGGZ" ]]; then
img_gz="${P_ARR[sysupgrade_img_gz]}" img_gz="$SYSUPGRADEIMGGZ"
img="${P_ARR[sysupgrade_img]}" img="$SYSUPGRADEIMG"
else else
return 1 return 1
fi fi
@@ -315,9 +494,9 @@ flashImage() {
gunzip -qfk "$img_gz" gunzip -qfk "$img_gz"
echo "Unmounting target device $FLASH_DEV partitions" echo "Unmounting target device $FLASH_DEV partitions"
_umount=( "$FLASH_DEV"?* ) partitions=( "$FLASH_DEV"?* )
debug "umount ${_umount[*]}" debug "umount ${partitions[*]}"
sudo umount "${_umount[@]}" sudo umount "${partitions[@]}"
debug "sudo dd if=\"$img\" of=\"$FLASH_DEV\" bs=2M conv=fsync" 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 if sudo dd if="$img" of="$FLASH_DEV" bs=2M conv=fsync; then
@@ -333,46 +512,92 @@ flashImage() {
sshUpgrade() { sshUpgrade() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
echo "Copying \"${P_ARR[sysupgrade_bin_gz]}\" to $SSH_UPGRADE_PATH/tmp/" echo "Copying '$SYSUPGRADEIMGGZ' to $SSH_UPGRADE_PATH/tmp/"
debug "scp \"${P_ARR[sysupgrade_bin_gz]}\" \"$SSH_UPGRADE_PATH\":\"/tmp/${P_ARR[sysupgrade_bin_gz_fname]}\"" debug "scp \"$SYSUPGRADEIMGGZ\" \"$SSH_UPGRADE_PATH\":\"/tmp/$SYSUPGRADEIMGGZFNAME\""
# shellcheck disable=SC2140
if ! scp "${P_ARR[sysupgrade_bin_gz]}" "$SSH_UPGRADE_PATH":"/tmp/${P_ARR[sysupgrade_bin_gz_fname]}"; then if ! scp "$SYSUPGRADEIMGGZ" "$SSH_UPGRADE_PATH":"/tmp/$SYSUPGRADEIMGGZFNAME"; 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/${P_ARR[sysupgrade_bin_gz_fname]}\"" debug "ssh \"$SSH_UPGRADE_PATH\" \"sysupgrade -F /tmp/$SYSUPGRADEIMGGZFNAME\""
# shellcheck disable=SC2029 # shellcheck disable=SC2029
ssh "$SSH_UPGRADE_PATH" "sysupgrade -F /tmp/${P_ARR[sysupgrade_bin_gz_fname]}" ssh "$SSH_UPGRADE_PATH" "sysupgrade -F /tmp/$SYSUPGRADEIMGGZFNAME"
} }
debug() { (( DEBUG )) && echo "Running: $*"; } fromSource() {
debug "${FUNCNAME[0]}"
declare src_url="https://github.com/openwrt/openwrt.git"
declare src_dir="$SRCDIR/openwrt"
declare -a pkg_list
echo "Building from source is under development"
if [[ ! -d "$src_dir" ]]; then
mkdir -p "$src_dir"
git clone "$src_url" "$src_dir"
fi
pushd "$src_dir" || return 1
if [[ ${P_ARR[release]} == "snapshot" ]]; then
git checkout master
else
git checkout "v$RELEASE"
fi
./scripts/feeds update -a
./scripts/feeds install -a
# Grab the release config.seed
k_options=$(curl -s "$SEED_URL")
debug "$k_options"
make distclean
make download
make -j"$(nproc)" world
popd || return 1
exit # TODO exit here for fromSource() testing
}
debug() { (( DEBUG )) && echo "Debug: $*"; }
askOk() { askOk() {
local _response local _response
read -r -p "$* [y/N]" _response read -r -p "$* [y/N]" _response
_response=${_response,,} _response=${_response,,}
[[ ! "$_response" =~ ^(yes|y)$ ]] && return 1 [[ "$_response" =~ ^(yes|y)$ ]]
return 0
} }
reset() { resetAll() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
askOk "Remove $FILESDIR $BUILDDIR/sources $BUILDDIR/bin?" || exit $? askOk "Remove $SRCDIR and $BINDIR?" || exit $?
debug "rm -rf $FILESDIR $BUILDDIR/sources $BUILDDIR/bin" debug "rm -rf $SRCDIR $BINDIR"
rm -rf "$FILESDIR" "${BUILDDIR:?}/sources" "${BUILDDIR:?}/bin" rm -rf "$SRCDIR" "$BINDIR"
}
resetProfile() {
debug "${FUNCNAME[0]}"
askOk "Remove $BUILDDIR and $THIS_BINDIR?" || exit $?
debug "rm -rf $BUILDDIR $THIS_BINDIR"
rm -rf "$BUILDDIR" "$THIS_BINDIR"
} }
loadProfiles() { loadProfiles() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
declare -g SCRIPTDIR PFILE declare -g PFILE
# https://stackoverflow.com/a/4774063 # https://stackoverflow.com/a/4774063
SCRIPTDIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit $? ; pwd -P )"
PFILE="$SCRIPTDIR/profiles" PFILE="$SCRIPTDIR/profiles"
# shellcheck source=./profiles # shellcheck source=./profiles
! source "$PFILE" && echo "profiles file missing!" && return 1 ! source "$PFILE" && echo "profiles file missing!" && return 1
@@ -382,56 +607,106 @@ loadProfiles() {
main() { main() {
debug "${FUNCNAME[0]}" debug "${FUNCNAME[0]}"
init
loadProfiles loadProfiles
readInput "$@" readInput "$@"
[[ ! ${!PROFILE@a} = A ]] && echo "Profile does not exist" && return 1 # Fallback to SCRIPTDIR if BUILDROOT has not been set
declare -gn P_ARR="$PROFILE" declare -g BUILDROOT="${BUILDROOT:=$SCRIPTDIR}"
declare _out_prefix [[ $BUILDROOT == "/" ]] && echo "Invalid --buildroot" && exit 1
declare -g FILESDIR="${FILESDIR:=$BUILDROOT/src/files}"
declare -g SRCDIR="$BUILDROOT/src" # input/build
declare -g BINDIR="$BUILDROOT/bin" # output
: "${BUILDDIR:=$SCRIPTDIR}" for dir in "$SRCDIR" "$BINDIR"; do
: "${FILESDIR:=$BUILDDIR/files}" [[ -d "$dir" ]] || mkdir -p "$dir"
done
: "${P_ARR[release]:=$RELEASE}" # Allow --reset without a profile
: "${P_ARR[source_archive]:=$BUILDDIR/sources/${P_ARR[profile]}-${P_ARR[release]}.tar.xz}" if [[ ${#PROFILES} -lt 1 ]]; then
: "${P_ARR[source_dir]:=${P_ARR[source_archive]%.tar.xz}}" if (( RESET )); then
: "${P_ARR[out_bin_dir]:=$BUILDDIR/bin/${P_ARR[profile]}-${P_ARR[release]}}" resetAll
exit
if [[ "${P_ARR[release]}" == "snapshot" ]]; then
_out_prefix="${P_ARR[out_bin_dir]}/openwrt-${P_ARR[target]//\//-}-${P_ARR[profile]}"
else else
_out_prefix="${P_ARR[out_bin_dir]}/openwrt-${P_ARR[release]}-${P_ARR[target]//\//-}-${P_ARR[profile]}" echo "No profile supplied" && return 1
fi
fi fi
: "${P_ARR[factory_img]:=$_out_prefix-${P_ARR[filesystem]}-factory.img}" installDependencies
: "${P_ARR[factory_img_gz]:=${P_ARR[factory_img]}.gz}"
: "${P_ARR[sysupgrade_img]:=$_out_prefix-${P_ARR[filesystem]}-sysupgrade.img}"
: "${P_ARR[sysupgrade_img_gz]:=${P_ARR[sysupgrade_img]}.gz}"
: "${P_ARR[sysupgrade_bin]:=$_out_prefix-${P_ARR[filesystem]}-sysupgrade.img}"
: "${P_ARR[sysupgrade_bin_fname]:=${P_ARR[sysupgrade_bin]##*/}}"
: "${P_ARR[sysupgrade_bin_gz]:=${P_ARR[sysupgrade_bin]}.gz}"
: "${P_ARR[sysupgrade_bin_gz_fname]:=${P_ARR[sysupgrade_bin_gz]##*/}}"
(( RESET )) && reset for profile in "${PROFILES[@]}"; do
debug "Starting profile: $profile"
if [[ ! ${!profile@a} = A ]]; then
echo "Profile '$profile' does not exist"
return 1
fi
# Store profile settings in P_ARR
declare -gn P_ARR="$profile"
# release precedence: user input>profile>env>hardcode
declare -g RELEASE="${USER_RELEASE:=${P_ARR[release]:=$RELEASE}}"
declare -g BUILDDIR="$SRCDIR/${P_ARR[profile]}-$RELEASE"
declare -g IB_ARCHIVE="$SRCDIR/${P_ARR[profile]}-$RELEASE.tar.xz"
declare -g FILESYSTEM="${P_ARR[filesystem]:="squashfs"}"
declare -g THIS_BINDIR="$BINDIR/${P_ARR[profile]}-$RELEASE"
if [[ "$RELEASE" == "snapshot" ]]; then
declare url_prefix="https://downloads.openwrt.org/snapshots/targets/${P_ARR[target]}"
declare url_filename="openwrt-imagebuilder-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
declare img_prefix="$BUILDDIR/openwrt-${P_ARR[target]//\//-}-${P_ARR[profile]}-$FILESYSTEM"
else
declare url_prefix="https://downloads.openwrt.org/releases/$RELEASE/targets/${P_ARR[target]}"
declare url_filename="openwrt-imagebuilder-$RELEASE-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
declare img_prefix="$BUILDDIR/openwrt-$RELEASE-${P_ARR[target]//\//-}-${P_ARR[profile]}-$FILESYSTEM"
fi
declare -g IB_URL="$url_prefix/$url_filename"
declare -g SHA256_URL="$url_prefix/sha256sums"
declare -g SEED_URL="$url_prefix/config.buildinfo"
declare -g FACTORYIMG="$img_prefix-factory.img"
declare -g FACTORYIMGGZ="$img_prefix-factory.img.gz"
declare -g SYSUPGRADEIMG="$img_prefix-sysupgrade.img"
declare -g SYSUPGRADEIMGGZ="$img_prefix-sysupgrade.img.gz"
declare -g SYSUPGRADEIMGGZFNAME="${SYSUPGRADEIMGGZ##*/}"
if (( DEBUG )) || (( PROFILE_INFO )); then if (( DEBUG )) || (( PROFILE_INFO )); then
for x in "${!P_ARR[@]}"; do printf "[%s]=%s\n" "$x" "${P_ARR[$x]}"; done echo "Profile settings:"
for x in "${!P_ARR[@]}"; do printf "%s=%s\n" "$x" "${P_ARR[$x]}"; done
echo "Build settings:"
cat <<- EOF
ALIAS: $profile
BUILDROOT: $BUILDROOT
BUILDDIR: $BUILDDIR
SRCDIR: $SRCDIR
BINDIR: $BINDIR
THIS_BINDIR: $THIS_BINDIR
TARGET: ${P_ARR[target]}
PROFILE: ${P_ARR[profile]}
RELEASE: $RELEASE
FILESYSTEM: $FILESYSTEM
IB_URL: $IB_URL
IB_ARCHIVE: $IB_ARCHIVE
EOF
fi fi
installHostDependencies (( RESET )) && resetProfile
getImageBuilder (( FROM_SOURCE )) && fromSource
getImageBuilder || return $?
addRepos addRepos
#copyFiles #copyFiles
[[ -v SSH_BACKUP_PATH ]] && sshBackup [[ -v SSH_BACKUP_PATH ]] && sshBackup
if makeImage; then if makeImage; then
[[ -v SSH_UPGRADE_PATH ]] && sshUpgrade [[ -v SSH_UPGRADE_PATH ]] && sshUpgrade
[[ -v FLASH_DEV ]] && flashImage [[ -v FLASH_DEV ]] && flashImage
fi fi
done
} }
main "$@" main "$@"

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck disable=SC2034 # shellcheck disable=SC2034
# This file contains a set of device profiles for openwrtbuilder # This file contains a set of device profiles for openwrtbuilder
# bash doesn't like nested arrays so we use strings instead
# Packages to install for all profiles # Packages to install for all profiles
default_packages="\ default_packages="\
@@ -99,12 +100,12 @@ r4s['packages']="\
usbutils \ usbutils \
kmod-usb-storage \ kmod-usb-storage \
kmod-usb-storage-uas \ kmod-usb-storage-uas \
kmod-fs-btrfs \
btrfs-progs \ btrfs-progs \
block-mount \ block-mount \
smcroute \ smcroute \
curl \ curl \
ethtool \ ethtool \
kmod-tun \
ca-bundle" ca-bundle"
declare -Ag ax6000_stock declare -Ag ax6000_stock