Files
aero3/scripts/lib/common.sh
T

100 lines
3.9 KiB
Bash
Raw Normal View History

2026-06-19 10:02:12 -04:00
#!/usr/bin/env bash
# scripts/lib/common.sh — shared helpers sourced by every stage.
set -Eeuo pipefail
# Resolve repo top from this file's location, then load config + versions.
_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AEROS_TOP="$(cd "$_LIB_DIR/../.." && pwd)"
# shellcheck source=/dev/null
source "$AEROS_TOP/config/aeros.conf"
# shellcheck source=/dev/null
source "$AEROS_TOP/config/versions.sh"
# ---- pretty logging ---------------------------------------------------------
if [[ -t 1 ]]; then
C_RESET=$'\e[0m'; C_BLUE=$'\e[1;34m'; C_GREEN=$'\e[1;32m'
C_YELLOW=$'\e[1;33m'; C_RED=$'\e[1;31m'; C_CYAN=$'\e[1;36m'
else
C_RESET=""; C_BLUE=""; C_GREEN=""; C_YELLOW=""; C_RED=""; C_CYAN=""
fi
log() { printf '%s>>%s %s\n' "$C_BLUE" "$C_RESET" "$*" >&2; }
ok() { printf '%s ✓%s %s\n' "$C_GREEN" "$C_RESET" "$*" >&2; }
warn() { printf '%s !!%s %s\n' "$C_YELLOW" "$C_RESET" "$*" >&2; }
die() { printf '%sxx%s %s\n' "$C_RED" "$C_RESET" "$*" >&2; exit 1; }
step() { printf '\n%s== %s ==%s\n' "$C_CYAN" "$*" "$C_RESET" >&2; }
# ---- guards -----------------------------------------------------------------
require_cmd() { command -v "$1" >/dev/null 2>&1 || die "missing host command: $1"; }
require_root(){ [[ $EUID -eq 0 ]] || die "this stage must run as root (use sudo)"; }
deny_root() { [[ $EUID -ne 0 ]] || die "do NOT run this stage as root"; }
# Stamp files mark a stage/sub-step as complete so re-runs are idempotent.
_stamp_dir() { mkdir -p "$AEROS_ROOT/.stamps"; echo "$AEROS_ROOT/.stamps"; }
stamped() { [[ -f "$(_stamp_dir)/$1" ]]; }
stamp() { touch "$(_stamp_dir)/$1"; }
# run a build step once: once <name> <command...>
once() {
local name="$1"; shift
if stamped "$name"; then ok "skip $name (done)"; return 0; fi
log "build $name"
"$@"
stamp "$name"
ok "done $name"
}
# ---- sources ----------------------------------------------------------------
# unpack <pkg-name> → extracts tarball into current dir, echoes src dir path
unpack() {
local pkg="$1" tb dir
tb="$AEROS_SOURCES/$(pkg_tarball "$pkg")"
[[ -f "$tb" ]] || die "source not fetched: $tb (run ./build.sh fetch)"
dir="$(pkg_srcdir "$pkg")"
rm -rf "$dir"
log "extract $(basename "$tb")"
case "$tb" in
*.tar.xz|*.txz) tar -xf "$tb" ;;
*.tar.gz|*.tgz) tar -xzf "$tb" ;;
*.tar.bz2) tar -xjf "$tb" ;;
*) die "unknown archive type: $tb" ;;
esac
# Some archives (gitlab) extract to a differently-named dir; normalize.
if [[ ! -d "$dir" ]]; then
dir="$(tar -tf "$tb" 2>/dev/null | head -1 | cut -d/ -f1)"
fi
[[ -d "$dir" ]] || die "extracted dir not found for $pkg"
printf '%s\n' "$dir"
}
# ---- chroot helpers ---------------------------------------------------------
mount_vkfs() {
require_root
local r="$AEROS_ROOT"
# ensure the virtual-fs mount points exist (stages 03-08 all rely on this)
mkdir -p "$r"/dev "$r"/dev/pts "$r"/dev/shm "$r"/proc "$r"/sys "$r"/run
mountpoint -q "$r/dev" || mount --bind /dev "$r/dev"
mountpoint -q "$r/dev/pts" || mount -t devpts devpts -o gid=5,mode=0620 "$r/dev/pts"
mountpoint -q "$r/proc" || mount -t proc proc "$r/proc"
mountpoint -q "$r/sys" || mount -t sysfs sysfs "$r/sys"
mountpoint -q "$r/run" || mount -t tmpfs tmpfs "$r/run"
if [[ -h "$r/dev/shm" ]]; then mkdir -p "$r/$(readlink "$r/dev/shm")"
else mountpoint -q "$r/dev/shm" || mount -t tmpfs -o nosuid,nodev tmpfs "$r/dev/shm"; fi
}
umount_vkfs() {
require_root
local r="$AEROS_ROOT"
for m in dev/shm dev/pts run sys proc dev; do
mountpoint -q "$r/$m" && umount -l "$r/$m" || true
done
}
# run a command string inside the chroot with a clean env
in_chroot() {
require_root
chroot "$AEROS_ROOT" /usr/bin/env -i \
HOME=/root TERM="$TERM" PS1='(aeros chroot) \u:\w\$ ' \
PATH=/usr/bin:/usr/sbin \
MAKEFLAGS="-j${AEROS_JOBS}" \
/bin/bash --login -c "$*"
}
export -f log ok warn die step