Files
openwrtbuilder/openwrtbuilder
2023-01-11 14:55:29 -05:00

674 lines
18 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Copyright 2022 Bryan C. Roessler
#
# Build and flash/upgrade OpenWRT devices
#
# Apache 2.0 License
# Set default release
: "${RELEASE:="22.03.3"}"
printHelp() {
debug "${FUNCNAME[0]}"
cat <<-'EOF'
Run and deploy OpenWRT images using the Image Builder.
USAGE:
openwrtbuilder [OPTION [VALUE]] -p PROFILE [-p PROFILE2]...
OPTIONS
--profile, -p PROFILE
--info, -i (print profile info)
--list-profiles, -l
--release, -r, --version, -v RELEASE_VERSION ("snapshot", "22.03.3", etc.)
--builddir, -b PATH
--ssh-upgrade HOST
Example: root@192.168.1.1
--ssh-backup SSH_PATH
(Enabled by default for --ssh-upgrade)
--flash, -f DEVICE
Example: /dev/sdX
--reset
Cleanup all source and output files
--debug, -d
--help, -h
EOF
}
readInput() {
debug "${FUNCNAME[0]}"
unset RESET
declare -ga PROFILES
declare -g PROFILE_INFO
if _input=$(getopt -o +r:v:p:i:lb:sf:dh -l release:,version:,profile:,info:,list-profiles,builddir:,from-source,ssh-upgrade:,ssh-backup:,flash:,reset,debug,help -- "$@"); then
eval set -- "$_input"
while true; do
case "$1" in
--release|-r|--version|-v)
shift && declare -g USER_RELEASE="$1"
;;
--profile|-p)
shift && PROFILES+=("$1")
;;
--info|-i)
PROFILE_INFO=1
;;
--list-profiles|-l)
listProfiles && exit $?
;;
--builddir|-b)
shift && BUILDDIR="$1"
;;
--from-source|-s)
FROM_SOURCE=1
;;
--ssh-upgrade)
shift && SSH_UPGRADE_PATH="$1"
;;
--ssh-backup)
shift && SSH_BACKUP_PATH="$1"
;;
--flash|-f)
shift && FLASH_DEV="$1"
;;
--reset)
RESET=1
;;
--debug|-d)
echo "Debugging on"
DEBUG=1
;;
--help|-h)
printHelp && exit 0
;;
--)
shift
break
;;
esac
shift
done
else
echo "Incorrect options provided"
printHelp && exit 1
fi
}
listProfiles() {
debug "${FUNCNAME[0]}"
grep "declare -Ag" "$PFILE" | cut -d" " -f3
}
installDependencies() {
debug "${FUNCNAME[0]}"
declare -a pkg_list
# TODO please contribute your platform here
if (( FROM_SOURCE )); then
# For building from source with make
# https://openwrt.org/docs/guide-developer/toolchain/install-buildsystem
case "$ID" in
fedora|centos)
pkg_list+=(
"bash-completion"
"bzip2"
"gcc"
"gcc-c++"
"git"
"make"
"ncurses-devel"
"patch"
"rsync"
"tar"
"unzip"
"wget"
"which"
"diffutils"
"python2"
"python3"
"perl-base"
"perl-Data-Dumper"
"perl-File-Compare"
"perl-File-Copy"
"perl-FindBin"
"perl-Thread-Queue"
)
;;
debian|ubuntu)
pkg_list+=(
"build-essential"
"clang"
"flex"
"g++"
"gawk"
"gcc-multilib"
"gettext"
"git"
"libncurses5-dev"
"libssl-dev"
"python3-distutils"
"rsync"
"unzip"
"zlib1g-dev"
"file"
"wget"
)
;;
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
pkg_install "${pkg_list[@]}"
}
getImageBuilder() {
debug "${FUNCNAME[0]}"
declare url_prefix filename url dl_tool sha256sum
if [[ "${P_ARR[release]}" == "snapshot" ]]; then
url_prefix="https://downloads.openwrt.org/snapshots/targets/${P_ARR[target]}"
filename="openwrt-imagebuilder-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
url="${url_prefix}/$filename"
else
url_prefix="https://downloads.openwrt.org/releases/${P_ARR[release]}/targets/${P_ARR[target]}"
filename="openwrt-imagebuilder-${P_ARR[release]}-${P_ARR[target]//\//-}.Linux-x86_64.tar.xz"
url="${url_prefix}/$filename"
[[ -f "${P_ARR[source_archive]}" ]] && return 0 # Reuse existing ImageBuilders
fi
if [[ -f "${P_ARR[source_archive]}" ]]; then
if askOk "Update ImageBuilder ?"; then
rm -f "${P_ARR[source_archive]}"
else
return 0
fi
fi
# Make sources directory if it does not exist
[[ ! -d "$BUILDDIR/sources" ]] && mkdir -p "$BUILDDIR/sources"
if hash axel &>/dev/null; then
dl_tool="axel"
elif hash curl &>/dev/null; then
dl_tool="curl"
else
echo "Downloading the ImageBuilder requires axel or curl!"
return 1
fi
echo "Downloading imagebuilder archive using $dl_tool"
debug "$dl_tool -o ${P_ARR[source_archive]} $url"
if ! "$dl_tool" -o "${P_ARR[source_archive]}" "$url"; then
echo "Could not download imagebuilder archive"
exit 1
fi
if [[ ! -f "${P_ARR[source_archive]}" ]]; then
echo "Archive missing"
exit 1
fi
if hash sha256sum &>/dev/null; then
echo "Verifying checksums"
debug "$dl_tool -s $url_prefix/sha256sums | grep $filename | cut -f1 -d' '"
sha256sum=$($dl_tool -s "$url_prefix"/sha256sums |grep "$filename" |cut -f1 -d' ')
debug "Downloaded sha256sum: $sha256sum"
fi
echo "Extracting image archive"
[[ ! -d "${P_ARR[source_dir]}" ]] && mkdir -p "${P_ARR[source_dir]}"
debug "tar -xf ${P_ARR[source_archive]} -C ${P_ARR[source_dir]} --strip-components 1"
if ! tar -xf "${P_ARR[source_archive]}" -C "${P_ARR[source_dir]}" --strip-components 1; then
echo "Extraction failed"
exit 1
fi
}
addRepos() {
debug "${FUNCNAME[0]}"
if [[ -v P_ARR[repo] ]]; then
if ! grep -q "${P_ARR[repo]}" "${P_ARR[source_dir]}/repositories.conf"; then
echo "${P_ARR[repo]}" >> "${P_ARR[source_dir]}/repositories.conf"
fi
sed -i '/option check_signature/d' "${P_ARR[source_dir]}/repositories.conf"
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"
}
makeImage() {
debug "${FUNCNAME[0]}"
# Reuse the existing output
if [[ -d "${P_ARR[out_bin_dir]}" ]]; then
if askOk "${P_ARR[out_bin_dir]} exists. Rebuild?"; then
rm -rf "${P_ARR[out_bin_dir]}"
else
return 0
fi
fi
[[ ! -d "${P_ARR[out_bin_dir]}" ]] && mkdir -p "${P_ARR[out_bin_dir]}"
if ! make image \
BIN_DIR="${P_ARR[out_bin_dir]}" \
PROFILE="${P_ARR[profile]}" \
PACKAGES="${P_ARR[packages]}" \
FILES="$FILESDIR" \
--directory="${P_ARR[source_dir]}" \
--jobs=$(( $(nproc) - 1 )) \
> make.log; then
echo "Make image failed!"
exit 1
fi
}
flashImage() {
debug "${FUNCNAME[0]}"
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 "${P_ARR[factory_img_gz]}" ]]; then
img_gz="${P_ARR[factory_img_gz]}"
img="${P_ARR[factory_img]}"
elif [[ -f "${P_ARR[sysupgrade_img_gz]}" ]]; then
img_gz="${P_ARR[sysupgrade_img_gz]}"
img="${P_ARR[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]}"
echo "Copying \"${P_ARR[sysupgrade_bin_gz]}\" to $SSH_UPGRADE_PATH/tmp/"
debug "scp \"${P_ARR[sysupgrade_bin_gz]}\" \"$SSH_UPGRADE_PATH\":\"/tmp/${P_ARR[sysupgrade_bin_gz_fname]}\""
# shellcheck disable=SC2140
if ! scp "${P_ARR[sysupgrade_bin_gz]}" "$SSH_UPGRADE_PATH":"/tmp/${P_ARR[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/${P_ARR[sysupgrade_bin_gz_fname]}\""
# shellcheck disable=SC2029
ssh "$SSH_UPGRADE_PATH" "sysupgrade -F /tmp/${P_ARR[sysupgrade_bin_gz_fname]}"
}
fromSource() {
debug "${FUNCNAME[0]}"
declare src_url="https://github.com/openwrt/openwrt.git"
declare -a pkg_list
echo "Building from source is under development"
git clone --depth=1 "$src_url" "$BUILDDIR/sources"
pushd "$BUILDDIR/sources/$(basename "$src_url" .git)" || return 1
if [[ ${P_ARR[release]} == "snapshot" ]]; then
git checkout master
else
git checkout "${P_ARR[release]}"
fi
./scripts/feeds update -a
./scripts/feeds install -a
make distclean
make download
make -j"$(nproc)" world
popd || return 1
}
debug() { (( DEBUG )) && echo "Running: $*"; }
askOk() {
local _response
read -r -p "$* [y/N]" _response
_response=${_response,,}
[[ ! "$_response" =~ ^(yes|y)$ ]] && return 1
return 0
}
reset() {
debug "${FUNCNAME[0]}"
askOk "Remove $FILESDIR $BUILDDIR/sources $BUILDDIR/bin?" || exit $?
debug "rm -rf $FILESDIR $BUILDDIR/sources $BUILDDIR/bin"
rm -rf "$FILESDIR" "${BUILDDIR:?}/sources" "${BUILDDIR:?}/bin"
}
loadProfiles() {
debug "${FUNCNAME[0]}"
declare -g SCRIPTDIR PFILE
# https://stackoverflow.com/a/4774063
SCRIPTDIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 || exit $? ; pwd -P )"
PFILE="$SCRIPTDIR/profiles"
# shellcheck source=./profiles
! source "$PFILE" && echo "profiles file missing!" && return 1
}
init() {
debug "${FUNCNAME[0]}"
declare -g ID RPM_MGR
echo "Starting openwrtbuilder"
debug || echo "To enable debugging output, use --debug or -d"
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 $VERSION_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 and --compat may also be required"
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 $VERSION_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
}
main() {
debug "${FUNCNAME[0]}"
init
loadProfiles
readInput "$@"
(( RESET )) && reset
[[ ${#PROFILES} -lt 1 ]] && echo "No profile supplied" && return 1
installDependencies
for profile in "${PROFILES[@]}"; do
debug "Profile: $profile"
loadProfiles
[[ ! ${!profile@a} = A ]] && echo "Profile does not exist" && return 1
debug "r4s: ${r4s[*]}"
declare -gn P_ARR="$profile"
debug
debug "P_ARR: ${P_ARR[*]}"
: "${BUILDDIR:=$SCRIPTDIR}"
: "${FILESDIR:=$BUILDDIR/files}"
: "${USER_RELEASE:=P_ARR[release]:=$RELEASE}" # precedence: user input>profiles>env>hardcode
: "${P_ARR[source_archive]:=$BUILDDIR/sources/${P_ARR[profile]}-${P_ARR[release]}.tar.xz}"
: "${P_ARR[source_dir]:=${P_ARR[source_archive]%.tar.xz}}"
: "${P_ARR[out_bin_dir]:=$BUILDDIR/bin/${P_ARR[profile]}-${P_ARR[release]}}"
declare out_prefix
if [[ "${P_ARR[release]}" == "snapshot" ]]; then
out_prefix="${P_ARR[out_bin_dir]}/openwrt-${P_ARR[target]//\//-}-${P_ARR[profile]}"
else
out_prefix="${P_ARR[out_bin_dir]}/openwrt-${P_ARR[release]}-${P_ARR[target]//\//-}-${P_ARR[profile]}"
fi
: "${P_ARR[factory_img]:=$out_prefix-${P_ARR[filesystem]}-factory.img}"
: "${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]##*/}}"
if (( DEBUG )) || (( PROFILE_INFO )); then
echo "Profile settings:"
for x in "${!P_ARR[@]}"; do printf "[%s]=%s\n" "$x" "${P_ARR[$x]}"; done
fi
# Experimental
(( FROM_SOURCE )) && fromSource
debug && exit
getImageBuilder
addRepos
#copyFiles
[[ -v SSH_BACKUP_PATH ]] && sshBackup
if makeImage; then
[[ -v SSH_UPGRADE_PATH ]] && sshUpgrade
[[ -v FLASH_DEV ]] && flashImage
fi
done
}
main "$@"
exit