glacier-ng 4.0.0
This commit is contained in:
commit
0cf4829fa5
159 files changed
+33717
No files matched your search
@@ -0,0 +1,7 @@
|
||||
include config.mk
|
||||
|
||||
all:
|
||||
@echo "Run 'make install' to install Glacier."
|
||||
|
||||
install:
|
||||
|
||||
Executable
+791
@@ -0,0 +1,791 @@
|
||||
#!/bin/bash
|
||||
# gbuild - Glacier Package Builder
|
||||
# Compiles a source directory and produces a glacier-compliant .gpkg bundle
|
||||
# (compiled binary tree + .gpm.cfg manifest)
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Colour / logging helpers — matches glacier lg_printf() in log.c
|
||||
# LG_INFO = 0 -> [i] blue
|
||||
# LG_WARN = 1 -> [!] yellow
|
||||
# LG_ERR = 2 -> [x] red
|
||||
# LG_SUCCESS = 3 -> [✓] green (falls back to [*] if not UTF-8)
|
||||
# ---------------------------------------------------------------------------
|
||||
_COL_RESET="\033[0m"
|
||||
_COL_BLUE="\033[0;34m"
|
||||
_COL_YELLOW="\033[0;33m"
|
||||
_COL_RED="\033[0;31m"
|
||||
_COL_GREEN="\033[0;32m"
|
||||
_COL_BOLD="\033[1m"
|
||||
|
||||
# Check UTF-8 support once at startup
|
||||
if [[ "$(locale charmap 2>/dev/null)" == "UTF-8" ]]; then
|
||||
_CHECK="\xe2\x9c\x93" # ✓
|
||||
else
|
||||
_CHECK="*"
|
||||
fi
|
||||
|
||||
log_info() { printf "${_COL_BLUE}[i]${_COL_RESET} $*\n"; }
|
||||
log_warn() { printf "${_COL_YELLOW}[!]${_COL_RESET} $*\n"; }
|
||||
log_err() { printf "${_COL_RED}[x]${_COL_RESET} $*\n" >&2; }
|
||||
log_success() { printf "${_COL_GREEN}[${_CHECK}]${_COL_RESET} $*\n"; }
|
||||
log_step() { printf "\n${_COL_BOLD}==> $*${_COL_RESET}\n"; }
|
||||
|
||||
die() {
|
||||
log_err "$*"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Usage
|
||||
# ---------------------------------------------------------------------------
|
||||
usage() {
|
||||
local prog="$0"
|
||||
printf "%s: Build and package source code into a Glacier-compliant .gpkg\n" "$prog"
|
||||
printf "usage: %s [-hvs] [-n NAME] [-V VER] [-d DESC] [-m MAINT] [-c COPY]\n" "$prog"
|
||||
printf " %s [-b CMD] [-i CMD] [-o DIR] [-p DIR] [-j N]\n" "$prog"
|
||||
printf " %s [-D name:ver] [-C name:ver] <SOURCE_DIR>\n\n" "$prog"
|
||||
printf "%-35s %s\n" "{-h --help}" "Show this message"
|
||||
printf "%-35s %s\n\n" "{-v --version}" "Show the current version"
|
||||
printf "%-35s %s\n" "{-n --name} NAME" "Package name (default: basename of SOURCE_DIR)"
|
||||
printf "%-35s %s\n" "{-V --ver} VERSION" "Semantic version (default: 0.1.0)"
|
||||
printf "%-35s %s\n" "{-d --desc} DESC" "Short description (prompted if omitted)"
|
||||
printf "%-35s %s\n" "{-m --maint} MAINT" "Maintainer e-mail (prompted if omitted)"
|
||||
printf "%-35s %s\n" "{-c --copy} COPYRIGHT" "Copyright string (default: MIT)"
|
||||
printf "%-35s %s\n" "{-g --repo} REPO" "Package repo (default: local — e.g. base, core, community, nonfree)"
|
||||
printf "%-35s %s\n" "{-D --dep} name:ver" "Add a dependency, e.g. libfoo:1.0.0"
|
||||
printf "%-35s %s\n" "{-C --conf} name:ver" "Add a conflict, e.g. old-foo:0.9.0"
|
||||
printf "%-35s %s\n" "{-b --build} CMD" "Build command (default: autodetected)"
|
||||
printf "%-35s %s\n" "{-i --install} CMD" "Install command populating \$DESTDIR"
|
||||
printf "%-35s %s\n" "{-o --output} DIR" "Output directory for finished .gpkg (default: .)"
|
||||
printf "%-35s %s\n" "{-p --stage} DIR" "Staging DESTDIR to inspect after build"
|
||||
printf "%-35s %s\n" "{-j --jobs} N" "Parallel build jobs (default: nproc)"
|
||||
printf "%-35s %s\n" "{-F --flags} FLAGS" "Extra compiler flags, e.g. \"-O2 -fstack-protector-strong\""
|
||||
printf "%-35s %s\n" "{-t --toolchain} DIR" "Path to toolchain bin directory, e.g. ~/x86_64-linux-musl-cross/bin"
|
||||
printf "%-35s %s\n" "{-x --cross-prefix} P" "Toolchain prefix, e.g. x86_64-linux-musl (default: autodetected)"
|
||||
printf "%-35s %s\n" "{-u --url} URL" "Git repository URL to clone"
|
||||
printf "%-35s %s\n" "{-r --ref} REF" "Git tag, branch, or commit to check out (default: default branch)"
|
||||
printf "%-35s %s\n" "{-k --key} KEY.sec" "Sign the sidecar with this secret key (writes .sha256.sig)"
|
||||
printf "%-35s %s\n\n" "{--verbose}" "Log events more verbosely"
|
||||
printf "This program is free software.\n"
|
||||
printf "See the GNU GPL version 3 for details.\n"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Defaults
|
||||
# ---------------------------------------------------------------------------
|
||||
PKG_NAME=""
|
||||
PKG_VER="0.1.0"
|
||||
PKG_DESC=""
|
||||
PKG_MAINT=""
|
||||
PKG_COPYRIGHT="MIT"
|
||||
PKG_REPO="local"
|
||||
BUILD_CMD=""
|
||||
INSTALL_CMD=""
|
||||
OUTPUT_DIR="."
|
||||
STAGE_DIR=""
|
||||
JOBS="$(nproc 2>/dev/null || echo 4)"
|
||||
SKIP_BUILD=0
|
||||
VERBOSE=0
|
||||
DEPS=()
|
||||
CONFS=()
|
||||
EXTRA_CFLAGS=""
|
||||
TOOLCHAIN_DIR=""
|
||||
TOOLCHAIN_PREFIX=""
|
||||
REPO_URL=""
|
||||
REPO_REF=""
|
||||
SIGN_KEY=""
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Parse options — convert longopts to shortopts before getopts runs
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
# Map of --longopt -> shortopt
|
||||
declare -A _LONGOPTS=(
|
||||
[--repo]="-g"
|
||||
[--help]="-h"
|
||||
[--version]="-v"
|
||||
[--name]="-n"
|
||||
[--ver]="-V"
|
||||
[--desc]="-d"
|
||||
[--maint]="-m"
|
||||
[--copy]="-c"
|
||||
[--dep]="-D"
|
||||
[--conf]="-C"
|
||||
[--build]="-b"
|
||||
[--install]="-i"
|
||||
[--output]="-o"
|
||||
[--stage]="-p"
|
||||
[--jobs]="-j"
|
||||
[--flags]="-F"
|
||||
[--toolchain]="-t"
|
||||
[--cross-prefix]="-x"
|
||||
[--skip]="-s"
|
||||
[--verbose]="-e"
|
||||
[--url]="-u"
|
||||
[--ref]="-r"
|
||||
[--key]="-k"
|
||||
)
|
||||
|
||||
# Rewrite argv replacing any --long-opt[=val] with -s [val]
|
||||
_args=()
|
||||
for _arg in "$@"; do
|
||||
if [[ "$_arg" == --*=* ]]; then
|
||||
# --key=value -> -s value
|
||||
_key="${_arg%%=*}"
|
||||
_val="${_arg#*=}"
|
||||
_short="${_LONGOPTS[$_key]:-}"
|
||||
[[ -z "$_short" ]] && die "Unknown option: $_key"
|
||||
_args+=("$_short" "$_val")
|
||||
elif [[ "$_arg" == --* ]]; then
|
||||
# --key -> -s
|
||||
_short="${_LONGOPTS[$_arg]:-}"
|
||||
[[ -z "$_short" ]] && die "Unknown option: $_arg"
|
||||
_args+=("$_short")
|
||||
else
|
||||
_args+=("$_arg")
|
||||
fi
|
||||
done
|
||||
set -- "${_args[@]+"${_args[@]}"}"
|
||||
|
||||
# -v is --version (as documented); verbose is long-only (--verbose), mapped
|
||||
# to the otherwise unused -e above.
|
||||
while getopts ":n:V:d:m:c:g:D:C:b:i:o:p:j:F:t:x:u:r:k:svhe" opt; do
|
||||
case $opt in
|
||||
n) PKG_NAME="$OPTARG" ;;
|
||||
V) PKG_VER="$OPTARG" ;;
|
||||
d) PKG_DESC="$OPTARG" ;;
|
||||
m) PKG_MAINT="$OPTARG" ;;
|
||||
c) PKG_COPYRIGHT="$OPTARG" ;;
|
||||
g) PKG_REPO="$OPTARG" ;;
|
||||
D) DEPS+=("$OPTARG") ;;
|
||||
C) CONFS+=("$OPTARG") ;;
|
||||
b) BUILD_CMD="$OPTARG" ;;
|
||||
i) INSTALL_CMD="$OPTARG" ;;
|
||||
o) OUTPUT_DIR="$OPTARG" ;;
|
||||
p) STAGE_DIR="$OPTARG" ;;
|
||||
j) JOBS="$OPTARG" ;;
|
||||
F) EXTRA_CFLAGS="$OPTARG" ;;
|
||||
t) TOOLCHAIN_DIR="$OPTARG" ;;
|
||||
x) TOOLCHAIN_PREFIX="$OPTARG" ;;
|
||||
u) REPO_URL="$OPTARG" ;;
|
||||
r) REPO_REF="$OPTARG" ;;
|
||||
k) SIGN_KEY="$OPTARG" ;;
|
||||
s) SKIP_BUILD=1 ;;
|
||||
v)
|
||||
echo "v4.0.0"
|
||||
exit 0
|
||||
;;
|
||||
e) VERBOSE=1 ;;
|
||||
h)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
:) die "Option -$OPTARG requires an argument." ;;
|
||||
\?) die "Unknown option: -$OPTARG" ;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Locate gsha256sum (and gsign, for -k)
|
||||
#
|
||||
# Resolution order (shown for gsha256sum; gsign's variable is $GSIGN_BIN):
|
||||
# 1. $GSHA256SUM_BIN, if the caller set it explicitly
|
||||
# 2. relative to this script's own location — ../bin/gsha256sum, which
|
||||
# is where `make install` puts it relative to $PREFIX/gbuild if
|
||||
# gbuild itself is being run from $PREFIX. This is what makes a
|
||||
# project-local, non-PATH PREFIX (e.g. PREFIX=~/Projects/glacier-ng)
|
||||
# work without the caller having to export PATH themselves.
|
||||
# 3. bare `gsha256sum` on PATH, same as how git/tar/make are already
|
||||
# invoked elsewhere in this script — the fallback for a real
|
||||
# system-wide install where $PREFIX/bin IS on PATH.
|
||||
#
|
||||
# Resolved here (after option parsing, so -h/--help never needs this;
|
||||
# before any clone/build work, so a missing gsha256sum fails fast rather
|
||||
# than after minutes of building something that can't be hashed at the
|
||||
# end anyway) once, so every later call site just uses $GSHA256SUM.
|
||||
# ---------------------------------------------------------------------------
|
||||
resolve_tool() {
|
||||
local tool="$1" var="$2"
|
||||
local explicit="${!var:-}"
|
||||
if [[ -n "$explicit" ]]; then
|
||||
if [[ ! -x "$explicit" ]]; then
|
||||
die "\$$var is set to '$explicit' but that isn't executable."
|
||||
fi
|
||||
echo "$explicit"
|
||||
return
|
||||
fi
|
||||
|
||||
local script_dir
|
||||
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
# gbuild runs from the source checkout, NOT from inside $PREFIX —
|
||||
# $PREFIX is arbitrary and unrelated to where this script lives.
|
||||
# What IS guaranteed by the Makefile is that `make` (before any
|
||||
# `make install`) drops the freshly-built binary at build/bin/
|
||||
# right next to this script, in the same source tree.
|
||||
local candidate="$script_dir/build/bin/$tool"
|
||||
if [[ -x "$candidate" ]]; then
|
||||
echo "$candidate"
|
||||
return
|
||||
fi
|
||||
|
||||
if command -v "$tool" >/dev/null 2>&1; then
|
||||
echo "$tool"
|
||||
return
|
||||
fi
|
||||
|
||||
die "Could not find $tool (checked \$$var, $candidate, and PATH). Run 'make install' first, or set $var explicitly."
|
||||
}
|
||||
|
||||
GSHA256SUM="$(resolve_tool gsha256sum GSHA256SUM_BIN)" || exit 1
|
||||
GSIGN=""
|
||||
if [[ -n "$SIGN_KEY" ]]; then
|
||||
GSIGN="$(resolve_tool gsign GSIGN_BIN)" || exit 1
|
||||
# Fail before building, not after: sign a throwaway file now.
|
||||
_probe="$(mktemp)" || die "Cannot create a temporary file."
|
||||
_out="$("$GSIGN" -S -s "$SIGN_KEY" -m "$_probe" -x "$_probe.sig" 2>&1)" ||
|
||||
{ rm -f "$_probe" "$_probe.sig"; printf '%s\n' "$_out" >&2; exit 1; }
|
||||
rm -f "$_probe" "$_probe.sig"
|
||||
fi
|
||||
|
||||
[[ $# -lt 1 ]] && [[ -z "$REPO_URL" ]] && {
|
||||
log_err "usage: $0 [-hvs] [-nVdmcDCbiojpur] <SOURCE_DIR>"
|
||||
exit 1
|
||||
}
|
||||
|
||||
if [[ -z "$REPO_URL" ]]; then
|
||||
SOURCE_DIR="${1%/}"
|
||||
[[ -d "$SOURCE_DIR" ]] || die "Source directory '$SOURCE_DIR' does not exist."
|
||||
SOURCE_DIR="$(realpath "$SOURCE_DIR")"
|
||||
fi
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fill in defaults that depend on SOURCE_DIR
|
||||
# ---------------------------------------------------------------------------
|
||||
[[ -z "$PKG_NAME" ]] && [[ -n "$SOURCE_DIR" ]] && PKG_NAME="$(basename "$SOURCE_DIR")"
|
||||
[[ -z "$PKG_NAME" ]] && [[ -n "$REPO_URL" ]] && PKG_NAME="$(basename "$REPO_URL" .git)"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Validate semver format
|
||||
# ---------------------------------------------------------------------------
|
||||
validate_semver() {
|
||||
local v="$1"
|
||||
# strip optional pre-release tag (e.g. 1.2.3-alpha)
|
||||
local core="${v%%-*}"
|
||||
[[ "$core" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] ||
|
||||
die "Version '$v' is not valid semver (expected MAJOR.MINOR.PATCH[-tag])."
|
||||
}
|
||||
validate_semver "$PKG_VER"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Interactive prompts for missing required fields
|
||||
# ---------------------------------------------------------------------------
|
||||
prompt_if_empty() {
|
||||
local varname="$1"
|
||||
local prompt_text="$2"
|
||||
local default="$3"
|
||||
local current
|
||||
current="${!varname}"
|
||||
if [[ -z "$current" ]]; then
|
||||
if [[ -t 0 ]]; then
|
||||
read -rp "$(printf "${_COL_BLUE}[i]${_COL_RESET} ${prompt_text} [${default}]: ")" val
|
||||
val="${val:-$default}"
|
||||
else
|
||||
val="$default"
|
||||
fi
|
||||
printf -v "$varname" '%s' "$val"
|
||||
fi
|
||||
}
|
||||
|
||||
prompt_if_empty PKG_DESC "Short description" "A Glacier package"
|
||||
prompt_if_empty PKG_MAINT "Maintainer e-mail" "unknown@localhost"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Dependency / conflict format validation: name:MAJOR.MINOR.PATCH
|
||||
# ---------------------------------------------------------------------------
|
||||
parse_dep_or_conf() {
|
||||
local raw="$1"
|
||||
local label="$2" # "dependency" or "conflict"
|
||||
local name ver
|
||||
name="${raw%%:*}"
|
||||
ver="${raw#*:}"
|
||||
[[ "$name" =~ ^[a-zA-Z0-9_-]+$ ]] ||
|
||||
die "Invalid $label name in '$raw'. Use alphanumeric/hyphen/underscore."
|
||||
validate_semver "$ver" 2>/dev/null ||
|
||||
die "Invalid $label version in '$raw'. Expected name:MAJOR.MINOR.PATCH."
|
||||
echo "$name $ver"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Toolchain setup
|
||||
# If -t is given, locate the compiler in that directory.
|
||||
# If -x is also given, use it as the prefix (e.g. x86_64-linux-musl).
|
||||
# If -x is omitted, autodetect by scanning the toolchain bin dir for *-gcc.
|
||||
# If neither is given, fall back to the system CC/CXX.
|
||||
# ---------------------------------------------------------------------------
|
||||
setup_toolchain() {
|
||||
if [[ -z "$TOOLCHAIN_DIR" ]]; then
|
||||
# No toolchain specified — use system compiler
|
||||
CC="${CC:-cc}"
|
||||
CXX="${CXX:-c++}"
|
||||
AR="${AR:-ar}"
|
||||
export CC CXX AR
|
||||
log_info "Toolchain: system (${CC})"
|
||||
return
|
||||
fi
|
||||
|
||||
[[ -d "$TOOLCHAIN_DIR" ]] ||
|
||||
die "Toolchain directory '$TOOLCHAIN_DIR' does not exist."
|
||||
|
||||
TOOLCHAIN_DIR="$(realpath "$TOOLCHAIN_DIR")"
|
||||
|
||||
# Autodetect prefix if not supplied
|
||||
if [[ -z "$TOOLCHAIN_PREFIX" ]]; then
|
||||
local detected
|
||||
detected="$(find "$TOOLCHAIN_DIR" -maxdepth 1 -name '*-gcc' |
|
||||
head -1 | xargs basename 2>/dev/null)"
|
||||
[[ -n "$detected" ]] ||
|
||||
die "Could not autodetect toolchain prefix in '$TOOLCHAIN_DIR'. Use -x to specify it."
|
||||
TOOLCHAIN_PREFIX="${detected%-gcc}"
|
||||
log_info "Autodetected toolchain prefix: $TOOLCHAIN_PREFIX"
|
||||
fi
|
||||
|
||||
local tc_cc="$TOOLCHAIN_DIR/${TOOLCHAIN_PREFIX}-gcc"
|
||||
local tc_cxx="$TOOLCHAIN_DIR/${TOOLCHAIN_PREFIX}-g++"
|
||||
local tc_ar="$TOOLCHAIN_DIR/${TOOLCHAIN_PREFIX}-ar"
|
||||
|
||||
[[ -x "$tc_cc" ]] || die "Toolchain compiler not found: $tc_cc"
|
||||
[[ -x "$tc_ar" ]] || die "Toolchain ar not found: $tc_ar"
|
||||
|
||||
export CC="$tc_cc"
|
||||
export CXX="$tc_cxx"
|
||||
export AR="$tc_ar"
|
||||
export CROSS_COMPILE="${TOOLCHAIN_PREFIX}-"
|
||||
|
||||
# Prepend toolchain bin dir to PATH so make/cmake/meson find the
|
||||
# prefixed binaries when they search by name rather than full path
|
||||
export PATH="$TOOLCHAIN_DIR:$PATH"
|
||||
|
||||
log_info "Toolchain: $TOOLCHAIN_PREFIX"
|
||||
log_info " CC = $CC"
|
||||
log_info " CXX = $CXX"
|
||||
log_info " AR = $AR"
|
||||
}
|
||||
setup_toolchain
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Clone source from git if -u was supplied
|
||||
# ---------------------------------------------------------------------------
|
||||
clone_source() {
|
||||
local clone_dir
|
||||
clone_dir="$(mktemp -d /tmp/gbuild_clone.XXXXXXXX)"
|
||||
|
||||
if [[ -n "$REPO_REF" ]]; then
|
||||
# Try shallow clone with --branch first (works for tags and branches)
|
||||
if ! git clone --branch "$REPO_REF" --depth 1 "$REPO_URL" "$clone_dir" 2>/dev/null; then
|
||||
# Fall back to full clone + checkout for commit hashes
|
||||
# stderr: stdout is the clone dir, captured by the caller
|
||||
log_warn "Shallow clone failed, falling back to full clone for commit checkout..." >&2
|
||||
rm -rf "$clone_dir"
|
||||
clone_dir="$(mktemp -d /tmp/gbuild_clone.XXXXXXXX)"
|
||||
git clone "$REPO_URL" "$clone_dir" >&2 \
|
||||
|| die "Failed to clone $REPO_URL"
|
||||
git -C "$clone_dir" checkout "$REPO_REF" >&2 \
|
||||
|| die "Failed to check out ref $REPO_REF"
|
||||
fi
|
||||
else
|
||||
git clone --depth 1 "$REPO_URL" "$clone_dir" \
|
||||
|| die "Failed to clone $REPO_URL"
|
||||
fi
|
||||
|
||||
echo "$clone_dir"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Build system autodetection
|
||||
# ---------------------------------------------------------------------------
|
||||
detect_build_system() {
|
||||
local src="$1"
|
||||
|
||||
if [[ -f "$src/CMakeLists.txt" ]]; then
|
||||
echo "cmake"
|
||||
elif [[ -f "$src/meson.build" ]]; then
|
||||
echo "meson"
|
||||
elif [[ -f "$src/Cargo.toml" ]]; then
|
||||
echo "cargo"
|
||||
elif [[ -f "$src/go.mod" ]]; then
|
||||
echo "go"
|
||||
# configure BEFORE a bare Makefile: musl and plenty of hand-rolled
|
||||
# projects ship both, and running make without configure builds
|
||||
# with the wrong defaults (CLAUDE.md bug 8, regressed once already).
|
||||
elif [[ -f "$src/configure" ]] ||
|
||||
[[ -f "$src/configure.ac" ]]; then
|
||||
echo "autotools"
|
||||
elif [[ -f "$src/Makefile" ]] ||
|
||||
[[ -f "$src/GNUmakefile" ]]; then
|
||||
echo "make"
|
||||
else
|
||||
echo "unknown"
|
||||
fi
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Run a command, honouring VERBOSE
|
||||
# ---------------------------------------------------------------------------
|
||||
run_cmd() {
|
||||
if [[ $VERBOSE -eq 1 ]]; then
|
||||
"$@"
|
||||
else
|
||||
"$@" >/tmp/gbuild_cmd.log 2>&1 ||
|
||||
{
|
||||
log_err "Command failed: $*"
|
||||
cat /tmp/gbuild_cmd.log >&2
|
||||
exit 1
|
||||
}
|
||||
fi
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Build step
|
||||
# ---------------------------------------------------------------------------
|
||||
do_build() {
|
||||
local src="$1"
|
||||
local destdir="$2"
|
||||
local bsys
|
||||
|
||||
bsys="$(detect_build_system "$src")"
|
||||
log_info "Detected build system: $(printf "${_COL_BOLD}")${bsys}$(printf "${_COL_RESET}")"
|
||||
|
||||
# Export extra compiler flags so all build systems inherit them
|
||||
if [[ -n "$EXTRA_CFLAGS" ]]; then
|
||||
log_info "Extra compiler flags: $EXTRA_CFLAGS"
|
||||
export CFLAGS="${CFLAGS:-} $EXTRA_CFLAGS"
|
||||
export CXXFLAGS="${CXXFLAGS:-} $EXTRA_CFLAGS"
|
||||
fi
|
||||
|
||||
# --- override if user supplied either build or install command ---
|
||||
if [[ -n "$BUILD_CMD" ]] || [[ -n "$INSTALL_CMD" ]]; then
|
||||
local effective_build="${BUILD_CMD:-make}"
|
||||
local effective_install="${INSTALL_CMD:-make install}"
|
||||
|
||||
log_step "Building with custom command: $effective_build"
|
||||
pushd "$src" >/dev/null
|
||||
run_cmd bash -c "$effective_build"
|
||||
popd >/dev/null
|
||||
|
||||
log_step "Installing into staging area"
|
||||
pushd "$src" >/dev/null
|
||||
run_cmd bash -c "export DESTDIR='$destdir'; $effective_install"
|
||||
popd >/dev/null
|
||||
return
|
||||
fi
|
||||
|
||||
# --- autodetected paths ---
|
||||
case "$bsys" in
|
||||
|
||||
make)
|
||||
log_step "Building with make"
|
||||
pushd "$src" >/dev/null
|
||||
run_cmd make -j"$JOBS"
|
||||
popd >/dev/null
|
||||
|
||||
log_step "Installing into staging area (make install)"
|
||||
pushd "$src" >/dev/null
|
||||
run_cmd make install DESTDIR="$destdir"
|
||||
popd >/dev/null
|
||||
;;
|
||||
|
||||
autotools)
|
||||
log_step "Running ./configure"
|
||||
pushd "$src" >/dev/null
|
||||
[[ -f configure ]] || run_cmd autoreconf -fi
|
||||
run_cmd ./configure --prefix=/usr
|
||||
popd >/dev/null
|
||||
|
||||
log_step "Building with make"
|
||||
pushd "$src" >/dev/null
|
||||
run_cmd make -j"$JOBS"
|
||||
popd >/dev/null
|
||||
|
||||
log_step "Installing into staging area"
|
||||
pushd "$src" >/dev/null
|
||||
run_cmd make install DESTDIR="$destdir"
|
||||
popd >/dev/null
|
||||
;;
|
||||
|
||||
cmake)
|
||||
local build_dir="$src/_gbuild_cmake"
|
||||
mkdir -p "$build_dir"
|
||||
log_step "Configuring with CMake"
|
||||
run_cmd cmake -S "$src" -B "$build_dir" \
|
||||
-DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_INSTALL_PREFIX=/usr \
|
||||
-DCMAKE_C_COMPILER="$CC" \
|
||||
-DCMAKE_CXX_COMPILER="${CXX:-c++}" \
|
||||
${EXTRA_CFLAGS:+-DCMAKE_C_FLAGS="$EXTRA_CFLAGS"} \
|
||||
${EXTRA_CFLAGS:+-DCMAKE_CXX_FLAGS="$EXTRA_CFLAGS"}
|
||||
|
||||
log_step "Building with CMake"
|
||||
run_cmd cmake --build "$build_dir" --parallel "$JOBS"
|
||||
|
||||
log_step "Installing into staging area"
|
||||
run_cmd cmake --install "$build_dir" --prefix "$destdir/usr"
|
||||
;;
|
||||
|
||||
meson)
|
||||
local build_dir="$src/_gbuild_meson"
|
||||
log_step "Configuring with Meson"
|
||||
run_cmd env CC="$CC" CXX="${CXX:-c++}" \
|
||||
meson setup "$build_dir" "$src" \
|
||||
--prefix=/usr \
|
||||
--buildtype=release \
|
||||
${EXTRA_CFLAGS:+-Dc_args="$EXTRA_CFLAGS"} \
|
||||
${EXTRA_CFLAGS:+-Dcpp_args="$EXTRA_CFLAGS"}
|
||||
|
||||
log_step "Building with Ninja"
|
||||
run_cmd ninja -C "$build_dir" -j"$JOBS"
|
||||
|
||||
log_step "Installing into staging area"
|
||||
run_cmd env DESTDIR="$destdir" ninja -C "$build_dir" install
|
||||
;;
|
||||
|
||||
cargo)
|
||||
log_step "Building with Cargo (release)"
|
||||
pushd "$src" >/dev/null
|
||||
run_cmd cargo build --release
|
||||
popd >/dev/null
|
||||
|
||||
log_step "Staging Cargo binary"
|
||||
local bin_name="$PKG_NAME"
|
||||
local cargo_bin="$src/target/release/$bin_name"
|
||||
if [[ ! -f "$cargo_bin" ]]; then
|
||||
# try to find any release binary
|
||||
cargo_bin="$(find "$src/target/release" -maxdepth 1 \
|
||||
-type f -executable ! -name '*.d' | head -1)"
|
||||
fi
|
||||
[[ -f "$cargo_bin" ]] || die "Could not locate Cargo release binary."
|
||||
mkdir -p "$destdir/usr/bin"
|
||||
cp "$cargo_bin" "$destdir/usr/bin/$PKG_NAME"
|
||||
;;
|
||||
|
||||
go)
|
||||
log_step "Building with Go"
|
||||
pushd "$src" >/dev/null
|
||||
run_cmd go build -o "$PKG_NAME" ./...
|
||||
popd >/dev/null
|
||||
|
||||
log_step "Staging Go binary"
|
||||
mkdir -p "$destdir/usr/bin"
|
||||
cp "$src/$PKG_NAME" "$destdir/usr/bin/$PKG_NAME"
|
||||
;;
|
||||
|
||||
unknown)
|
||||
die "Could not detect a build system in '$src'. " \
|
||||
"Use -b BUILD_CMD and -i INSTALL_CMD to specify commands manually."
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Generate the .gpm.cfg manifest (libconfig syntax used by gl_gpm2gpkg)
|
||||
# ---------------------------------------------------------------------------
|
||||
write_manifest() {
|
||||
local manifest_path="$1"
|
||||
shift
|
||||
|
||||
# split semver + optional tag
|
||||
local ver_core="${PKG_VER%%-*}"
|
||||
local ver_tag=""
|
||||
[[ "$PKG_VER" == *-* ]] && ver_tag="${PKG_VER#*-}"
|
||||
|
||||
local maj min pat
|
||||
IFS='.' read -r maj min pat <<<"$ver_core"
|
||||
|
||||
{
|
||||
echo "// Glacier Package Manifest"
|
||||
echo "// Generated by gbuild on $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
|
||||
echo ""
|
||||
echo "PKG_NAME = \"${PKG_NAME}\";"
|
||||
if [[ -n "$ver_tag" ]]; then
|
||||
echo "PKG_VER = \"${maj}.${min}.${pat}-${ver_tag}\";"
|
||||
else
|
||||
echo "PKG_VER = \"${maj}.${min}.${pat}\";"
|
||||
fi
|
||||
echo "PKG_DESC = \"${PKG_DESC}\";"
|
||||
echo "PKG_MAINT = \"${PKG_MAINT}\";"
|
||||
echo "PKG_COPYRIGHT = \"${PKG_COPYRIGHT}\";"
|
||||
echo "PKG_REPO = \"${PKG_REPO}\";"
|
||||
echo ""
|
||||
|
||||
# Dependencies
|
||||
if [[ ${#DEPS[@]} -gt 0 ]]; then
|
||||
echo "PKG_DEPS = ("
|
||||
for raw in "${DEPS[@]}"; do
|
||||
read -r dname dver <<<"$(parse_dep_or_conf "$raw" "dependency")"
|
||||
local dmaj dmin dpat
|
||||
IFS='.' read -r dmaj dmin dpat <<<"$dver"
|
||||
echo " {"
|
||||
echo " PKG_NAME = \"${dname}\";"
|
||||
echo " PKG_VER = \"${dmaj}.${dmin}.${dpat}\";"
|
||||
echo " },"
|
||||
done
|
||||
echo ");"
|
||||
else
|
||||
echo "PKG_DEPS = ();"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# Conflicts
|
||||
if [[ ${#CONFS[@]} -gt 0 ]]; then
|
||||
echo "PKG_CONFS = ("
|
||||
for raw in "${CONFS[@]}"; do
|
||||
read -r cname cver <<<"$(parse_dep_or_conf "$raw" "conflict")"
|
||||
local cmaj cmin cpat
|
||||
IFS='.' read -r cmaj cmin cpat <<<"$cver"
|
||||
echo " {"
|
||||
echo " PKG_NAME = \"${cname}\";"
|
||||
echo " PKG_VER = \"${cmaj}.${cmin}.${cpat}\";"
|
||||
echo " },"
|
||||
done
|
||||
echo ");"
|
||||
else
|
||||
echo "PKG_CONFS = ();"
|
||||
fi
|
||||
|
||||
} >"$manifest_path"
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Main
|
||||
# ---------------------------------------------------------------------------
|
||||
log_step "gbuild — Glacier Package Builder"
|
||||
[[ -n "$REPO_URL" ]] && log_info "URL: $REPO_URL"
|
||||
[[ -n "$REPO_REF" ]] && log_info "Ref: $REPO_REF"
|
||||
log_info "Package: $PKG_NAME $PKG_VER"
|
||||
log_info "Maintainer: $PKG_MAINT"
|
||||
[[ -n "$EXTRA_CFLAGS" ]] && log_info "Flags: $EXTRA_CFLAGS"
|
||||
[[ -n "$TOOLCHAIN_PREFIX" ]] && log_info "Target: $TOOLCHAIN_PREFIX"
|
||||
|
||||
# Clone source if -u was supplied
|
||||
if [[ -n "$REPO_URL" ]]; then
|
||||
log_step "Cloning source repository"
|
||||
SOURCE_DIR="$(clone_source)"
|
||||
log_success "Cloned to $SOURCE_DIR"
|
||||
# Clean up clone dir on exit alongside WORK_DIR
|
||||
trap 'rm -rf "$WORK_DIR" "$SOURCE_DIR"' EXIT
|
||||
fi
|
||||
|
||||
[[ -z "$PKG_NAME" ]] && PKG_NAME="$(basename "$SOURCE_DIR")"
|
||||
PKG_NAME="${PKG_NAME//[^a-zA-Z0-9-]/}"
|
||||
PKG_NAME="${PKG_NAME,,}"
|
||||
[[ -z "$PKG_NAME" ]] && die "Could not derive a valid package name."
|
||||
|
||||
log_info "Source: $SOURCE_DIR"
|
||||
|
||||
# Create working directories
|
||||
WORK_DIR="$(mktemp -d /tmp/gbuild.XXXXXXXX)"
|
||||
PKG_DIR="$WORK_DIR/pkg"
|
||||
mkdir -p "$PKG_DIR"
|
||||
|
||||
# DESTDIR: use user-supplied staging dir if given, otherwise temp
|
||||
if [[ -n "$STAGE_DIR" ]]; then
|
||||
DESTDIR="$(realpath "$STAGE_DIR")"
|
||||
mkdir -p "$DESTDIR"
|
||||
log_info "Staging dir: $DESTDIR"
|
||||
# Don't clean up STAGE_DIR on exit — user wants to inspect it
|
||||
trap 'rm -rf "$WORK_DIR"' EXIT
|
||||
else
|
||||
DESTDIR="$WORK_DIR/destdir"
|
||||
mkdir -p "$DESTDIR"
|
||||
trap 'rm -rf "$WORK_DIR"' EXIT
|
||||
fi
|
||||
|
||||
# --- Build ---
|
||||
if [[ $SKIP_BUILD -eq 0 ]]; then
|
||||
do_build "$SOURCE_DIR" "$DESTDIR"
|
||||
log_success "Build complete."
|
||||
else
|
||||
log_warn "Skipping build step (-s flag set)."
|
||||
fi
|
||||
|
||||
# --- Assemble package directory ---
|
||||
# Layout inside the .gpkg dir:
|
||||
# <pkgname>-<ver>/
|
||||
# manifest.gpm.cfg <- parsed by gl_gpm2gpkg
|
||||
# files/ <- the DESTDIR tree (what gets installed)
|
||||
|
||||
PKG_FULL_NAME="${PKG_NAME}-${PKG_VER}"
|
||||
PKG_STAGE="$PKG_DIR/$PKG_FULL_NAME"
|
||||
mkdir -p "$PKG_STAGE/files"
|
||||
|
||||
log_step "Assembling package layout"
|
||||
|
||||
# Copy staged files
|
||||
if [[ $SKIP_BUILD -eq 0 ]]; then
|
||||
if [[ -n "$(ls -A "$DESTDIR" 2>/dev/null)" ]]; then
|
||||
cp -a "$DESTDIR/." "$PKG_STAGE/files/"
|
||||
log_info "Staged file tree copied."
|
||||
else
|
||||
log_warn "DESTDIR is empty — the package will contain no files."
|
||||
log_warn "Check your build/install commands."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Write manifest
|
||||
MANIFEST_PATH="$PKG_STAGE/manifest.gpm.cfg"
|
||||
write_manifest "$MANIFEST_PATH"
|
||||
log_success "Manifest written: manifest.gpm.cfg"
|
||||
|
||||
if [[ $VERBOSE -eq 1 ]]; then
|
||||
log_info "--- manifest.gpm.cfg ---"
|
||||
cat "$MANIFEST_PATH"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# --- Archive and compress into OUTPUT_DIR ---
|
||||
OUTPUT_DIR="$(realpath "$OUTPUT_DIR")"
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
FINAL_PKG="$OUTPUT_DIR/$PKG_FULL_NAME.gpkg"
|
||||
|
||||
log_step "Compressing package"
|
||||
|
||||
# Remove old output if present
|
||||
rm -f "$FINAL_PKG"
|
||||
|
||||
# Hardlinks become plain files: glacier refuses a package with a
|
||||
# hardlink entry (binutils installs ld as a hardlink to ld.bfd).
|
||||
find "$PKG_DIR/$PKG_FULL_NAME" -type f -links +1 | while IFS= read -r f; do
|
||||
cp -p "$f" "$f.unlink" && mv -f "$f.unlink" "$f" ||
|
||||
die "Cannot copy hardlink $f."
|
||||
done
|
||||
|
||||
# tar the staged package directory, compress with gzip, write to .gpkg
|
||||
# -C into the parent so the archive root is just <pkgname>-<ver>/
|
||||
tar -czf "$FINAL_PKG" -C "$PKG_DIR" "$PKG_FULL_NAME" ||
|
||||
die "Failed to create package archive."
|
||||
|
||||
log_step "Hashing package"
|
||||
SIDECAR_PATH="$FINAL_PKG.sha256"
|
||||
"$GSHA256SUM" "$FINAL_PKG" > "$SIDECAR_PATH" ||
|
||||
die "Failed to hash package archive."
|
||||
log_success "Sidecar: ${_COL_BOLD}${SIDECAR_PATH}${_COL_RESET}"
|
||||
log_info "$(cat "$SIDECAR_PATH")"
|
||||
|
||||
rm -f "$SIDECAR_PATH.sig"
|
||||
if [[ -n "$SIGN_KEY" ]]; then
|
||||
log_step "Signing sidecar"
|
||||
_out="$("$GSIGN" -S -s "$SIGN_KEY" -m "$SIDECAR_PATH" 2>&1)" ||
|
||||
{ printf '%s\n' "$_out" >&2; exit 1; }
|
||||
log_success "Signature: ${_COL_BOLD}${SIDECAR_PATH}.sig${_COL_RESET}"
|
||||
fi
|
||||
|
||||
log_step "Package ready"
|
||||
log_success "Output: ${_COL_BOLD}${FINAL_PKG}${_COL_RESET}"
|
||||
log_info "Size: $(du -sh "$FINAL_PKG" | cut -f1)"
|
||||
echo ""
|
||||
log_info "Contents:"
|
||||
tar -tzf "$FINAL_PKG" | sort | while read -r line; do
|
||||
printf " %s\n" "$line"
|
||||
done
|
||||
Executable
+642
@@ -0,0 +1,642 @@
|
||||
#!/usr/bin/luajit
|
||||
-- glacier-bootstrap: build a chrootable Everest system in TARGET from a
|
||||
-- template index and a directory of packages (GLACIER_BOOTSTRAP.txt,
|
||||
-- local mode).
|
||||
--
|
||||
-- glacier-bootstrap [-hvntV] -d PKGDIR TEMPLATE TARGET
|
||||
--
|
||||
-- 1. parse TEMPLATE (strict: anything unexpected is an error)
|
||||
-- 2. copy each PKGDIR/NAME-VER.gpkg into a private directory, hashing it
|
||||
-- on the way, against its sidecar and the template's pin if any
|
||||
-- 3. read the copies' manifests: name and version must match the
|
||||
-- template, every dependency must be listed (at least that version),
|
||||
-- and nothing listed may conflict
|
||||
-- 4. syspkg -R TARGET -n (skipped if TARGET is initialised
|
||||
-- and empty)
|
||||
-- 5. syspkg -R TARGET -l COPY... ONE transaction, strict
|
||||
-- 6. syspkg -R TARGET -y fetch the indexes of every
|
||||
-- repository TARGET's own
|
||||
-- repos.conf gives a url (skipped,
|
||||
-- with a warning, if none does)
|
||||
-- 7. -t: chroot TARGET /bin/sh -c true
|
||||
--
|
||||
-- It NEVER WRITES INTO TARGET ITSELF: every byte there comes from
|
||||
-- syspkg, run through doas (directly when already root). The syspkg is
|
||||
-- the one on PATH. The copies handed to syspkg carry the sidecar
|
||||
-- verified here (its bytes as read, which match the copy's hash and the
|
||||
-- pin), so a pin is enforced by syspkg's own sidecar check too, and a
|
||||
-- file swapped in PKGDIR afterwards is never read. A NAME-VER.gpkg
|
||||
-- .sha256.sig next to it is carried too: syspkg requires one for base
|
||||
-- (REPO_DESIGN.txt section 7) and checks it against the HOST's keyring.
|
||||
--
|
||||
-- Unprivileged: nothing here needs root, so unlike syspkg and gstore
|
||||
-- there is no root-ownership check. Its own lib/ is still found from
|
||||
-- its real location, not the current directory.
|
||||
--
|
||||
-- Exit status: 1 usage or target, 2 template, 3 verification,
|
||||
-- 4 resolution, 5 installation, 6 smoke test, 7 repositories (TARGET
|
||||
-- is installed, but its indexes could not all be fetched).
|
||||
|
||||
local ffi = require("ffi")
|
||||
|
||||
ffi.cdef[[
|
||||
char *bootstrap_realpath(const char *path, char *resolved) asm("realpath");
|
||||
]]
|
||||
|
||||
local self_dir
|
||||
do
|
||||
local buf = ffi.new("char[4096]")
|
||||
if ffi.C.bootstrap_realpath(arg[0], buf) == nil then
|
||||
io.stderr:write("glacier-bootstrap: cannot resolve own location\n")
|
||||
os.exit(1)
|
||||
end
|
||||
self_dir = ffi.string(buf):match("^(.*)/[^/]*$")
|
||||
if not self_dir or self_dir == "" then self_dir = "/" end
|
||||
end
|
||||
|
||||
package.path = self_dir .. "/?.lua"
|
||||
package.cpath = ""
|
||||
|
||||
require("lib/glacier_cdef")
|
||||
local so_dir = self_dir .. "/lib/glacier"
|
||||
local log = ffi.load(so_dir .. "/libglacier_log.so")
|
||||
local istore = ffi.load(so_dir .. "/libglacier_istoreutils.so")
|
||||
local txn = ffi.load(so_dir .. "/libglacier_transaction.so")
|
||||
local verify = ffi.load(so_dir .. "/libglacier_verify.so")
|
||||
|
||||
local EXIT_USAGE, EXIT_TEMPLATE, EXIT_VERIFY = 1, 2, 3
|
||||
local EXIT_RESOLVE, EXIT_INSTALL, EXIT_SMOKE = 4, 5, 6
|
||||
local EXIT_REPOS = 7
|
||||
|
||||
local function msg(level, fmt, ...)
|
||||
log.lg_printf(level, "%s", string.format(fmt, ...))
|
||||
end
|
||||
|
||||
local function shell_quote(s)
|
||||
return "'" .. s:gsub("'", "'\\''") .. "'"
|
||||
end
|
||||
|
||||
local function run(cmd)
|
||||
io.stdout:flush()
|
||||
local ok = os.execute(cmd)
|
||||
return ok == true or ok == 0
|
||||
end
|
||||
|
||||
-- The private directory holding verified copies; removed on every exit.
|
||||
local private_dir
|
||||
|
||||
local function finish(code)
|
||||
if private_dir then
|
||||
os.execute("rm -rf " .. shell_quote(private_dir))
|
||||
private_dir = nil
|
||||
end
|
||||
os.exit(code)
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Arguments
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local usage_line = "usage: glacier-bootstrap [-hvntV] -d PKGDIR TEMPLATE TARGET"
|
||||
|
||||
local function usage(code)
|
||||
if code ~= 0 then
|
||||
msg(2, "%s", usage_line)
|
||||
os.exit(code)
|
||||
end
|
||||
local function opt(flag, text)
|
||||
print(string.format("%-25s %s", flag, text))
|
||||
end
|
||||
print("glacier-bootstrap: Install a template's packages into a new system")
|
||||
print(usage_line)
|
||||
print()
|
||||
opt("{-h --help}", "Show this message")
|
||||
opt("{-v --version}", "Show the current version")
|
||||
opt("{-d --pkgdir} PKGDIR", "Take the template's packages from PKGDIR")
|
||||
opt("{-n --dry-run}", "Verify and resolve only, never touch TARGET")
|
||||
opt("{-t --test}", "Run a smoke test in TARGET afterwards")
|
||||
opt("{-V --verbose}", "Log events more verbosely")
|
||||
print()
|
||||
print("This program is free software.")
|
||||
print("See the GNU GPL version 3 for details.")
|
||||
os.exit(0)
|
||||
end
|
||||
|
||||
-- Long options, as the short flag they stand for.
|
||||
local long_opts = {
|
||||
["--help"] = "-h", ["--version"] = "-v", ["--pkgdir"] = "-d",
|
||||
["--dry-run"] = "-n", ["--test"] = "-t", ["--verbose"] = "-V",
|
||||
}
|
||||
|
||||
local pkgdir, dry_run, smoke, verbose
|
||||
local operands = {}
|
||||
|
||||
do
|
||||
local i = 1
|
||||
while i <= #arg do
|
||||
local a = arg[i]
|
||||
if a:sub(1, 2) == "--" and #a > 2 then
|
||||
local name, value = a:match("^(%-%-[^=]+)=(.*)$")
|
||||
if name == "--pkgdir" then
|
||||
a = "-d"
|
||||
table.insert(arg, i + 1, value)
|
||||
elseif long_opts[a] then
|
||||
a = long_opts[a]
|
||||
else
|
||||
msg(2, "Unknown option: %s", a)
|
||||
usage(EXIT_USAGE)
|
||||
end
|
||||
end
|
||||
if a:sub(1, 1) == "-" and #a > 1 then
|
||||
local flags = a:sub(2)
|
||||
for k = 1, #flags do
|
||||
local c = flags:sub(k, k)
|
||||
if c == "h" then
|
||||
usage(0)
|
||||
elseif c == "v" then
|
||||
print("v4.0.0")
|
||||
os.exit(0)
|
||||
elseif c == "n" then
|
||||
dry_run = true
|
||||
elseif c == "t" then
|
||||
smoke = true
|
||||
elseif c == "V" then
|
||||
verbose = true
|
||||
elseif c == "d" then
|
||||
if k ~= #flags then
|
||||
msg(2, "-d must come last in %s.", a)
|
||||
os.exit(EXIT_USAGE)
|
||||
end
|
||||
if pkgdir then
|
||||
msg(2, "-d given more than once.")
|
||||
os.exit(EXIT_USAGE)
|
||||
end
|
||||
i = i + 1
|
||||
pkgdir = arg[i]
|
||||
if not pkgdir or pkgdir == "" then
|
||||
msg(2, "-d needs a directory.")
|
||||
os.exit(EXIT_USAGE)
|
||||
end
|
||||
else
|
||||
msg(2, "Unknown option: -%s", c)
|
||||
usage(EXIT_USAGE)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(operands, a)
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
if not pkgdir or #operands ~= 2 then
|
||||
usage(EXIT_USAGE)
|
||||
end
|
||||
|
||||
local template_path, target = operands[1], operands[2]
|
||||
|
||||
-- syspkg -R refuses relative roots; say so here, before any work.
|
||||
if target:sub(1, 1) ~= "/" then
|
||||
msg(2, "TARGET must be an absolute path: %s", target)
|
||||
os.exit(EXIT_USAGE)
|
||||
end
|
||||
target = target:gsub("(.)/+$", "%1")
|
||||
if target == "/" then
|
||||
msg(2, "TARGET must not be /.")
|
||||
os.exit(EXIT_USAGE)
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- 1. Template
|
||||
-- ---------------------------------------------------------------------
|
||||
--
|
||||
-- # comment
|
||||
-- NAME=minimal
|
||||
-- PKG=musl 1.2.5 sha256:<64 hex> (the pin is optional)
|
||||
--
|
||||
-- Exact versions only. Unknown keys (SYS_PROFILE included, until
|
||||
-- manifests record one: ROADMAP 3.3), a repeated NAME or package, a
|
||||
-- malformed line and an empty package list are all errors.
|
||||
|
||||
local function valid_ident(s)
|
||||
return s:match("^[A-Za-z0-9._-]+$") ~= nil and s ~= "." and s ~= ".."
|
||||
end
|
||||
|
||||
local function valid_version(s)
|
||||
return s:match("^%d+%.%d+%.%d+$") ~= nil
|
||||
or s:match("^%d+%.%d+%.%d+%-[A-Za-z0-9.]+$") ~= nil
|
||||
end
|
||||
|
||||
local function parse_template(path)
|
||||
local f = io.open(path, "r")
|
||||
if not f then
|
||||
msg(2, "Cannot read template %s.", path)
|
||||
finish(EXIT_TEMPLATE)
|
||||
end
|
||||
|
||||
local t = { pkgs = {} }
|
||||
local seen = {}
|
||||
local bad = false
|
||||
local n = 0
|
||||
local function err(fmt, ...)
|
||||
msg(2, "%s:%d: " .. fmt, path, n, ...)
|
||||
bad = true
|
||||
end
|
||||
|
||||
for line in f:lines() do
|
||||
n = n + 1
|
||||
line = line:gsub("^%s+", ""):gsub("%s+$", "")
|
||||
if line ~= "" and line:sub(1, 1) ~= "#" then
|
||||
local key, value = line:match("^([A-Za-z_]+)=(.*)$")
|
||||
if not key then
|
||||
err("Expected KEY=value.")
|
||||
elseif key == "NAME" then
|
||||
if t.name then
|
||||
err("NAME given more than once.")
|
||||
elseif not valid_ident(value) then
|
||||
err("Invalid NAME: %s", value)
|
||||
else
|
||||
t.name = value
|
||||
end
|
||||
elseif key == "PKG" then
|
||||
local fields = {}
|
||||
for w in value:gmatch("%S+") do table.insert(fields, w) end
|
||||
local name, ver, pin = fields[1], fields[2], fields[3]
|
||||
if #fields < 2 or #fields > 3 then
|
||||
err("Expected PKG=NAME VERSION [sha256:HEX].")
|
||||
elseif not valid_ident(name) then
|
||||
err("Invalid package name: %s", name)
|
||||
elseif not valid_version(ver) then
|
||||
err("Invalid version for %s: %s", name, ver)
|
||||
elseif pin and not (pin:match("^sha256:%x+$") and #pin == 71) then
|
||||
err("Malformed pin for %s: %s", name, pin)
|
||||
elseif seen[name] then
|
||||
err("%s is listed twice (first on line %d).", name, seen[name])
|
||||
else
|
||||
seen[name] = n
|
||||
table.insert(t.pkgs, {
|
||||
name = name, ver = ver,
|
||||
pin = pin and pin:sub(8):lower(),
|
||||
})
|
||||
end
|
||||
else
|
||||
err("Unknown key: %s", key)
|
||||
end
|
||||
end
|
||||
end
|
||||
f:close()
|
||||
|
||||
if not t.name then
|
||||
msg(2, "%s: No NAME.", path)
|
||||
bad = true
|
||||
end
|
||||
if #t.pkgs == 0 and not bad then
|
||||
msg(2, "%s: No packages.", path)
|
||||
bad = true
|
||||
end
|
||||
if bad then finish(EXIT_TEMPLATE) end
|
||||
return t
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- 2. Verification
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- Copy `src` to `dst`, returning the sha256 of what was copied (hex),
|
||||
-- or nil.
|
||||
local function copy_hashed(src, dst)
|
||||
local i = io.open(src, "rb")
|
||||
if not i then return nil end
|
||||
local o = io.open(dst, "wb")
|
||||
if not o then i:close(); return nil end
|
||||
|
||||
local hctx = ffi.new("gl_sha256_ctx_t")
|
||||
verify.gl_sha256_init(hctx)
|
||||
while true do
|
||||
local chunk = i:read(65536)
|
||||
if not chunk then break end
|
||||
verify.gl_sha256_update(hctx, ffi.cast("const uint8_t *", chunk), #chunk)
|
||||
o:write(chunk)
|
||||
end
|
||||
i:close()
|
||||
o:close()
|
||||
|
||||
local digest = ffi.new("uint8_t[32]")
|
||||
local hex = ffi.new("char[65]")
|
||||
verify.gl_sha256_final(hctx, digest)
|
||||
verify.gl_sha256_to_hex(digest, hex)
|
||||
return ffi.string(hex)
|
||||
end
|
||||
|
||||
local function verify_all(t)
|
||||
local mk = io.popen("mktemp -d 2>/dev/null")
|
||||
private_dir = mk and mk:read("*l")
|
||||
if mk then mk:close() end
|
||||
if not private_dir or private_dir == "" then
|
||||
private_dir = nil
|
||||
msg(2, "Cannot create a private working directory.")
|
||||
finish(EXIT_VERIFY)
|
||||
end
|
||||
|
||||
local bad = false
|
||||
for _, p in ipairs(t.pkgs) do
|
||||
local file = string.format("%s-%s.gpkg", p.name, p.ver)
|
||||
local src = pkgdir .. "/" .. file
|
||||
local why
|
||||
|
||||
local sf = io.open(src .. ".sha256", "r")
|
||||
local sidecar_text = sf and (sf:read("*a") or "")
|
||||
local expected = sidecar_text and sidecar_text:match("^sha256:(%x+)%s*$")
|
||||
if sf then sf:close() end
|
||||
|
||||
local probe = io.open(src, "rb")
|
||||
if probe then probe:close() end
|
||||
|
||||
if not probe then
|
||||
why = "no such file"
|
||||
elseif not sf then
|
||||
why = "no sidecar " .. src .. ".sha256"
|
||||
elseif not expected or #expected ~= 64 then
|
||||
why = "malformed sidecar " .. src .. ".sha256"
|
||||
else
|
||||
expected = expected:lower()
|
||||
if p.pin and p.pin ~= expected then
|
||||
why = "the template pins " .. p.pin ..
|
||||
", the sidecar says " .. expected
|
||||
end
|
||||
end
|
||||
|
||||
if not why then
|
||||
p.copy = private_dir .. "/" .. file
|
||||
local got = copy_hashed(src, p.copy)
|
||||
if not got then
|
||||
why = "cannot copy it to " .. p.copy
|
||||
elseif got ~= expected then
|
||||
why = "hash mismatch (sidecar says " .. expected ..
|
||||
", file is " .. got .. ")"
|
||||
else
|
||||
-- The same bytes, not a sidecar written
|
||||
-- anew: a signature covers these.
|
||||
local o = io.open(p.copy .. ".sha256", "w")
|
||||
if not o or not o:write(sidecar_text) then
|
||||
why = "cannot write " .. p.copy .. ".sha256"
|
||||
end
|
||||
if o then o:close() end
|
||||
end
|
||||
end
|
||||
if not why then
|
||||
local sig = io.open(src .. ".sha256.sig", "rb")
|
||||
if sig then
|
||||
local bytes = sig:read("*a") or ""
|
||||
sig:close()
|
||||
local o = io.open(p.copy .. ".sha256.sig", "wb")
|
||||
if not o or not o:write(bytes) then
|
||||
why = "cannot write " .. p.copy .. ".sha256.sig"
|
||||
end
|
||||
if o then o:close() end
|
||||
end
|
||||
end
|
||||
|
||||
if why then
|
||||
msg(2, "Refusing %s: %s.", src, why)
|
||||
bad = true
|
||||
end
|
||||
end
|
||||
if bad then finish(EXIT_VERIFY) end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- 3. Resolution
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function semver_str(v)
|
||||
local s = string.format("%d.%d.%d", v.maj, v.min, v.pat)
|
||||
if v.tag ~= nil then s = s .. "-" .. ffi.string(v.tag) end
|
||||
return s
|
||||
end
|
||||
|
||||
-- Same ordering as the library (deps.c semver_cmp): tags are ignored.
|
||||
local function semver_ge(a, b)
|
||||
if a.maj ~= b.maj then return a.maj > b.maj end
|
||||
if a.min ~= b.min then return a.min > b.min end
|
||||
return a.pat >= b.pat
|
||||
end
|
||||
|
||||
local function resolve(t)
|
||||
local bad = false
|
||||
local by_name = {}
|
||||
|
||||
for _, p in ipairs(t.pkgs) do
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if istore.gl_read_pkg_manifest(p.copy, m) ~= 0 then
|
||||
msg(2, "Cannot read the manifest of %s/%s-%s.gpkg.", pkgdir, p.name, p.ver)
|
||||
bad = true
|
||||
else
|
||||
local name = ffi.string(m.pkg_name)
|
||||
local ver = semver_str(m.pkg_ver)
|
||||
p.repo = ffi.string(m.pkg_repo)
|
||||
p.semver = { maj = m.pkg_ver.maj, min = m.pkg_ver.min, pat = m.pkg_ver.pat }
|
||||
p.deps, p.confs = {}, {}
|
||||
local d = m.pkg_dependencies
|
||||
while d ~= nil and d.dep_name ~= nil do
|
||||
table.insert(p.deps, {
|
||||
name = ffi.string(d.dep_name),
|
||||
ver = { maj = d.dep_ver.maj, min = d.dep_ver.min, pat = d.dep_ver.pat },
|
||||
str = semver_str(d.dep_ver),
|
||||
})
|
||||
d = d + 1
|
||||
end
|
||||
local c = m.pkg_conflicts
|
||||
while c ~= nil and c.conf_name ~= nil do
|
||||
table.insert(p.confs, ffi.string(c.conf_name))
|
||||
c = c + 1
|
||||
end
|
||||
istore.gl_free_pkg(m)
|
||||
|
||||
if name ~= p.name or ver ~= p.ver then
|
||||
msg(2, "%s/%s-%s.gpkg is %s %s, not %s %s.",
|
||||
pkgdir, p.name, p.ver, name, ver, p.name, p.ver)
|
||||
bad = true
|
||||
end
|
||||
by_name[p.name] = p
|
||||
end
|
||||
end
|
||||
if bad then finish(EXIT_RESOLVE) end
|
||||
|
||||
for _, p in ipairs(t.pkgs) do
|
||||
for _, d in ipairs(p.deps) do
|
||||
local q = by_name[d.name]
|
||||
if not q then
|
||||
msg(2, "%s::%s requires %s >= %s, which the template does not list.",
|
||||
p.repo, p.name, d.name, d.str)
|
||||
bad = true
|
||||
elseif not semver_ge(q.semver, d.ver) then
|
||||
msg(2, "%s::%s requires %s >= %s, but the template lists %s.",
|
||||
p.repo, p.name, d.name, d.str, q.ver)
|
||||
bad = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Conflicts, by name, either direction; each pair once.
|
||||
for i, p in ipairs(t.pkgs) do
|
||||
for j = i + 1, #t.pkgs do
|
||||
local q = t.pkgs[j]
|
||||
local hit = false
|
||||
for _, c in ipairs(p.confs) do hit = hit or c == q.name end
|
||||
for _, c in ipairs(q.confs) do hit = hit or c == p.name end
|
||||
if hit then
|
||||
msg(2, "%s::%s conflicts with %s::%s.", p.repo, p.name, q.repo, q.name)
|
||||
bad = true
|
||||
end
|
||||
end
|
||||
end
|
||||
if bad then finish(EXIT_RESOLVE) end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- The target
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- Installed packages under TARGET as a set of "repo::name::ver", or nil
|
||||
-- if TARGET has no system index. The index is world-readable.
|
||||
local function target_index()
|
||||
local f = io.open(target .. "/glacier/sys/index/current", "r")
|
||||
if not f then return nil end
|
||||
local set, n = {}, 0
|
||||
for line in f:lines() do
|
||||
local repo, name, ver = line:match("^([^:]+)::([^:]+)::([^:]+)")
|
||||
if repo then
|
||||
set[repo .. "::" .. name .. "::" .. ver] = true
|
||||
n = n + 1
|
||||
end
|
||||
end
|
||||
f:close()
|
||||
return set, n
|
||||
end
|
||||
|
||||
local function exists(path)
|
||||
return run("test -e " .. shell_quote(path) .. " -o -L " .. shell_quote(path))
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Main
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local t = parse_template(template_path)
|
||||
|
||||
-- The same refusal syspkg -R makes, read-only, before any work.
|
||||
if txn.gl_check_sysroot(target) ~= 0 then
|
||||
finish(EXIT_USAGE)
|
||||
end
|
||||
|
||||
-- A fresh TARGET, or one syspkg -n initialised with nothing installed
|
||||
-- (a run that stopped at the prompt). Anything else isn't ours to add to.
|
||||
local initialised = exists(target .. "/glacier/sys")
|
||||
if initialised then
|
||||
local _, n = target_index()
|
||||
if n == nil then
|
||||
msg(2, "%s/glacier/sys exists but has no system index.", target)
|
||||
finish(EXIT_USAGE)
|
||||
elseif n > 0 then
|
||||
msg(2, "%s already has packages installed.", target)
|
||||
finish(EXIT_USAGE)
|
||||
end
|
||||
end
|
||||
|
||||
verify_all(t)
|
||||
resolve(t)
|
||||
|
||||
msg(0, "Template %s, %d package%s for %s:", t.name, #t.pkgs,
|
||||
#t.pkgs == 1 and "" or "s", target)
|
||||
for _, p in ipairs(t.pkgs) do
|
||||
print(string.format(" * %s::%s %s%s", p.repo, p.name, p.ver,
|
||||
p.pin and " (pinned)" or ""))
|
||||
end
|
||||
|
||||
if dry_run then
|
||||
msg(3, "Dry run: every package verified and resolved.")
|
||||
finish(0)
|
||||
end
|
||||
|
||||
local is_root = ffi.C.geteuid() == 0
|
||||
local as_root = is_root and "" or "doas "
|
||||
|
||||
local syspkg
|
||||
do
|
||||
local p = io.popen("command -v syspkg 2>/dev/null")
|
||||
syspkg = p and p:read("*l")
|
||||
if p then p:close() end
|
||||
if not syspkg or syspkg:sub(1, 1) ~= "/" then
|
||||
msg(2, "syspkg is not on PATH.")
|
||||
finish(EXIT_INSTALL)
|
||||
end
|
||||
end
|
||||
|
||||
local sys = as_root .. shell_quote(syspkg) .. " -R " .. shell_quote(target)
|
||||
local v = verbose and " -V" or ""
|
||||
|
||||
if not initialised then
|
||||
if not run(sys .. " -n" .. v) then
|
||||
finish(EXIT_INSTALL)
|
||||
end
|
||||
end
|
||||
|
||||
local cmd = sys .. v .. " -l"
|
||||
for _, p in ipairs(t.pkgs) do
|
||||
cmd = cmd .. " " .. shell_quote(p.copy)
|
||||
end
|
||||
if not run(cmd) then
|
||||
finish(EXIT_INSTALL)
|
||||
end
|
||||
|
||||
-- syspkg exits 0 when its prompt is declined; check what it installed.
|
||||
local installed, n = target_index()
|
||||
local missing = {}
|
||||
for _, p in ipairs(t.pkgs) do
|
||||
if not (installed and installed[p.repo .. "::" .. p.name .. "::" .. p.ver]) then
|
||||
table.insert(missing, p)
|
||||
end
|
||||
end
|
||||
if #missing == #t.pkgs and (n or 0) == 0 then
|
||||
msg(2, "Nothing was installed into %s.", target)
|
||||
finish(EXIT_INSTALL)
|
||||
elseif #missing > 0 then
|
||||
for _, p in ipairs(missing) do
|
||||
msg(2, "%s::%s %s was not installed into %s.", p.repo, p.name, p.ver, target)
|
||||
end
|
||||
finish(EXIT_INSTALL)
|
||||
end
|
||||
|
||||
-- TARGET's repositories, as its own repos.conf (from a package) gives
|
||||
-- them: syspkg -R reads TARGET's keyring for -y, and fetches into
|
||||
-- TARGET's /glacier/sys/repos. This also sets TARGET's rollback floor.
|
||||
-- A repos.conf this can't parse is left to syspkg to report.
|
||||
do
|
||||
local conf = target:gsub("/+$", "") .. "/etc/glacier/repos.conf"
|
||||
local buf = ffi.new("char[65536]")
|
||||
local st = tonumber(istore.gl_conf_repos(conf, buf, 65536))
|
||||
local remote = st ~= 0 and exists(conf)
|
||||
if st == 0 then
|
||||
for _, url in ffi.string(buf):gmatch("([^\t\n]+)\t([^\n]*)\n") do
|
||||
if url ~= "" then remote = true end
|
||||
end
|
||||
end
|
||||
if not remote then
|
||||
msg(1, "No remote repositories are configured in %s.", target)
|
||||
elseif not run(sys .. v .. " -y") then
|
||||
finish(EXIT_REPOS)
|
||||
end
|
||||
end
|
||||
|
||||
msg(3, "Bootstrapped %s into %s.", t.name, target)
|
||||
|
||||
if smoke then
|
||||
if not run(as_root .. "chroot " .. shell_quote(target) .. " /bin/sh -c true") then
|
||||
msg(2, "chroot %s /bin/sh -c true failed.", target)
|
||||
finish(EXIT_SMOKE)
|
||||
end
|
||||
msg(3, "chroot %s /bin/sh -c true succeeded.", target)
|
||||
end
|
||||
|
||||
finish(0)
|
||||
@@ -0,0 +1,307 @@
|
||||
#!/usr/bin/luajit
|
||||
|
||||
local ffi = require("ffi")
|
||||
|
||||
-- lib/ is found beside this script's REAL location (an installed
|
||||
-- /usr/bin/gpkg is a symlink into the store), never the current
|
||||
-- directory. Unprivileged, so no ownership checks (syspkg/gstore have
|
||||
-- those).
|
||||
ffi.cdef[[
|
||||
char *gpkg_realpath(const char *path, char *resolved) asm("realpath");
|
||||
]]
|
||||
local self_dir
|
||||
do
|
||||
local buf = ffi.new("char[4096]")
|
||||
if ffi.C.gpkg_realpath(arg[0], buf) == nil then
|
||||
io.stderr:write("gpkg: cannot resolve own location\n")
|
||||
os.exit(1)
|
||||
end
|
||||
self_dir = ffi.string(buf):match("^(.*)/[^/]*$")
|
||||
if not self_dir or self_dir == "" then self_dir = "/" end
|
||||
end
|
||||
package.path = self_dir .. "/?.lua"
|
||||
package.cpath = ""
|
||||
|
||||
local cargs = require("lib/cargs")
|
||||
local color = require("lib/color")
|
||||
|
||||
local glacier_cdef = require("lib/glacier_cdef")
|
||||
local common = require("lib/gpkg_common")
|
||||
|
||||
-- gpkg is a PER-USER tool only. It deliberately never constructs a
|
||||
-- GL_SCOPE_SYS context anywhere in this file -- that capability lives
|
||||
-- exclusively in syspkg. See SECURITY_DESIGN.txt section 5.
|
||||
local so_dir = self_dir .. "/lib/glacier"
|
||||
local log = ffi.load(so_dir .. "/libglacier_log.so")
|
||||
local istore = ffi.load(so_dir .. "/libglacier_istoreutils.so")
|
||||
local txn = ffi.load(so_dir .. "/libglacier_transaction.so")
|
||||
local verify = ffi.load(so_dir .. "/libglacier_verify.so")
|
||||
|
||||
local txn_name = glacier_cdef.txn_name
|
||||
|
||||
local function get_loginuid()
|
||||
local f = io.open("/proc/self/loginuid", "r")
|
||||
if f then
|
||||
local raw = f:read("*l")
|
||||
f:close()
|
||||
local n = tonumber(raw)
|
||||
if n and n ~= 4294967295 then
|
||||
return n
|
||||
end
|
||||
end
|
||||
return tonumber(ffi.C.getuid())
|
||||
end
|
||||
local argc, argv = cargs.get_argv()
|
||||
local args = {}
|
||||
|
||||
local longopts = {
|
||||
help = "-h",
|
||||
version = "-v",
|
||||
merge = "-f",
|
||||
["local"] = "-l",
|
||||
update = "-u",
|
||||
remove = "-x",
|
||||
refresh = "-y",
|
||||
verbose = "-V",
|
||||
quiet = "-q",
|
||||
["skip-installed"] = "-S",
|
||||
overwrite = "-O",
|
||||
}
|
||||
|
||||
for i = 1, argc - 1 do
|
||||
table.insert(args, ffi.string(argv[i]))
|
||||
end
|
||||
|
||||
if #args == 0 then
|
||||
log.lg_printf(2, "usage: %s [-hv] [-y] [-flux PKG] [-OSVq]", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local mode
|
||||
local pkgs = {}
|
||||
|
||||
local isVerbose = false
|
||||
local skipInstalled = false
|
||||
|
||||
-- Wire the shared module up now that log/istore/txn and argv exist.
|
||||
common.init({ log = log, istore = istore, txn = txn, verify = verify, argv = argv,
|
||||
so_dir = so_dir })
|
||||
|
||||
-- Why a user's /glacier/usr/<uid> is unusable, before any transaction
|
||||
-- starts (and before any prompt). Mirrors gl_usr_base_status_t.
|
||||
local function require_user_dirs(uidn)
|
||||
local base = "/glacier/usr/" .. tostring(uidn)
|
||||
local st = tonumber(istore.gl_usr_base_status(uidn))
|
||||
if st == 1 then
|
||||
log.lg_printf(2, "No Glacier directories for UID %s. Run doas gstore -n first.", tostring(uidn))
|
||||
elseif st == 2 then
|
||||
log.lg_printf(2, "Refusing to run: /glacier/usr is not root-owned, or is group/world-writable.")
|
||||
elseif st == 3 then
|
||||
log.lg_printf(2, "Refusing to run: %s is not a directory owned by UID %s with mode 0700.",
|
||||
base, tostring(uidn))
|
||||
end
|
||||
if st ~= 0 then os.exit(1) end
|
||||
|
||||
if not istore.gl_usr_istore_exists(ffi.C.INDEX, uidn)
|
||||
or not istore.gl_usr_istore_exists(ffi.C.STORE, uidn) then
|
||||
log.lg_printf(2, "%s is incomplete (no index or store). Ask an administrator to recreate it.", base)
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
local function handle_flag(flag, i)
|
||||
if flag == "-h" then
|
||||
log.lg_gpkg_help(argc, argv)
|
||||
elseif flag == "-v" then
|
||||
print("v4.0.0")
|
||||
elseif flag == "-V" then
|
||||
isVerbose = true
|
||||
common.set_verbose(true)
|
||||
log.lg_printf(0, "This option enables verbose logging.")
|
||||
elseif flag == "-q" then
|
||||
common.set_quiet(true)
|
||||
elseif flag == "-O" then
|
||||
common.set_overwrite(true)
|
||||
elseif flag == "-S" then
|
||||
skipInstalled = true
|
||||
common.set_skip_installed(true)
|
||||
if isVerbose then
|
||||
log.lg_printf(0, "Skipping already installed packages.")
|
||||
end
|
||||
elseif flag == "-f" or flag == "-u" then
|
||||
local uidn = get_loginuid()
|
||||
|
||||
if uidn < 1000 then
|
||||
log.lg_printf(2, "Cannot run %s as a system account.", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local names = {}
|
||||
local j = i + 1
|
||||
|
||||
while j <= #args do
|
||||
local arg = args[j]
|
||||
if arg:sub(1, 1) == "-" then break end
|
||||
table.insert(names, arg)
|
||||
j = j + 1
|
||||
end
|
||||
|
||||
if flag == "-f" and #names == 0 then
|
||||
log.lg_printf(2, "usage: %s -f PKG...", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
require_user_dirs(uidn)
|
||||
|
||||
local opts = {
|
||||
scope = ffi.C.GL_SCOPE_USR,
|
||||
uid = uidn,
|
||||
scope_label = string.format("for UID %d", uidn),
|
||||
}
|
||||
if flag == "-f" then
|
||||
common.fetch(names, opts)
|
||||
else
|
||||
common.upgrade(names, opts)
|
||||
end
|
||||
elseif flag == "-l" then
|
||||
local uidn = get_loginuid()
|
||||
|
||||
if uidn < 1000 then
|
||||
log.lg_printf(2, "Cannot run %s as a system account.", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local gpkg_paths = {}
|
||||
local j = i + 1
|
||||
|
||||
while j <= #args do
|
||||
local arg = args[j]
|
||||
if arg:sub(1, 1) == "-" then break end
|
||||
table.insert(gpkg_paths, arg)
|
||||
j = j + 1
|
||||
end
|
||||
|
||||
if #gpkg_paths == 0 then
|
||||
log.lg_printf(2, "usage: %s -l PKG...", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
require_user_dirs(uidn)
|
||||
|
||||
common.install(gpkg_paths, {
|
||||
scope = ffi.C.GL_SCOPE_USR,
|
||||
uid = uidn,
|
||||
scope_label = string.format("for UID %d", uidn),
|
||||
})
|
||||
elseif flag == "-y" then
|
||||
local uidn = get_loginuid()
|
||||
|
||||
if uidn < 1000 then
|
||||
log.lg_printf(2, "Cannot run %s as a system account.", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
if args[i + 1] and args[i + 1]:sub(1, 1) ~= "-" then
|
||||
log.lg_printf(2, "-y takes no arguments.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
require_user_dirs(uidn)
|
||||
|
||||
os.exit(common.refresh({
|
||||
scope = ffi.C.GL_SCOPE_USR,
|
||||
uid = uidn,
|
||||
scope_label = string.format("for UID %d", uidn),
|
||||
}) and 0 or 1)
|
||||
elseif flag == "-x" then
|
||||
local uidn = get_loginuid()
|
||||
|
||||
if uidn < 1000 then
|
||||
log.lg_printf(2, "Cannot run %s as a system account.", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local pkg_names = {}
|
||||
local j = i + 1
|
||||
|
||||
while j <= #args do
|
||||
local arg = args[j]
|
||||
if arg:sub(1, 1) == "-" then break end
|
||||
table.insert(pkg_names, arg)
|
||||
j = j + 1
|
||||
end
|
||||
|
||||
if #pkg_names == 0 then
|
||||
log.lg_printf(2, "usage: %s -x PKG...", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
require_user_dirs(uidn)
|
||||
|
||||
common.remove(pkg_names, {
|
||||
scope = ffi.C.GL_SCOPE_USR,
|
||||
uid = uidn,
|
||||
scope_label = string.format("for UID %d", uidn),
|
||||
})
|
||||
else
|
||||
log.lg_printf(2, "Unknown option: %s", flag)
|
||||
log.lg_printf(2, "usage: %s [-hv] [-y] [-flux PKG] [-OSVq]", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
-- Modifier flags change how an action runs but aren't actions. They are
|
||||
-- applied in a pre-pass so their position doesn't matter: -l acts (and
|
||||
-- exits) as soon as it's parsed, so without this `-lO` never saw its O
|
||||
-- and only `-Ol` worked.
|
||||
local MODIFIERS = { ["-O"] = true, ["-S"] = true, ["-V"] = true, ["-q"] = true }
|
||||
|
||||
for idx, raw in ipairs(args) do
|
||||
local a = raw
|
||||
if a:sub(1, 2) == "--" then
|
||||
a = longopts[a:sub(3)] or a
|
||||
end
|
||||
if a:sub(1, 1) == "-" and a:sub(2, 2) ~= "-" then
|
||||
for c in a:sub(2):gmatch(".") do
|
||||
if MODIFIERS["-" .. c] then
|
||||
handle_flag("-" .. c, idx)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local i = 1
|
||||
while i <= #args do
|
||||
local a = args[i]
|
||||
|
||||
-- long option conversion
|
||||
if a:sub(1, 2) == "--" then
|
||||
local name = a:sub(3)
|
||||
local short = longopts[name]
|
||||
if not short then
|
||||
log.lg_printf(2, "Unknown option: %s", a)
|
||||
log.lg_printf(2, "usage: %s [-hv] [-y] [-flux PKG] [-OSVq]", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
a = short
|
||||
end
|
||||
|
||||
-- grouped flags: -fV, -uxV, etc.
|
||||
if a:sub(1, 1) == "-" and #a > 2 and a:sub(2, 2) ~= "-" then
|
||||
for c in a:sub(2):gmatch(".") do
|
||||
if not MODIFIERS["-" .. c] then
|
||||
handle_flag("-" .. c, i)
|
||||
end
|
||||
end
|
||||
|
||||
-- single flag: -f, -V, -u, etc.
|
||||
elseif a:sub(1, 1) == "-" then
|
||||
if not MODIFIERS[a] then
|
||||
handle_flag(a, i)
|
||||
end
|
||||
else
|
||||
log.lg_printf(2, "Unknown option: %s", a)
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
i = i + 1
|
||||
end
|
||||
Executable
+354
@@ -0,0 +1,354 @@
|
||||
#!/usr/bin/luajit -E
|
||||
|
||||
-- gstore: per-user glacier directory management (GL_SCOPE_USR only —
|
||||
-- this tool never touches system scope; that is syspkg's alone).
|
||||
--
|
||||
-- `gstore -n` is the one operation that needs root: it creates
|
||||
-- /glacier/usr/<uid>, which lives in a root-owned parent so no user can
|
||||
-- create (or squat) another uid's directory. Root runs it directly with
|
||||
-- an explicit UID; a user runs it for themselves through a doas rule
|
||||
-- that pins the arguments to exactly `-n`:
|
||||
--
|
||||
-- permit nopass :wheel as root cmd /path/to/gstore args -n
|
||||
--
|
||||
-- doas is the privilege boundary (SECURITY_DESIGN.txt section 6): that
|
||||
-- rule is what stops a user passing someone else's UID. With no UID,
|
||||
-- gstore acts for the invoking user, resolved the same way gpkg resolves
|
||||
-- whose store it uses, so `doas gstore -n` always creates the directory
|
||||
-- gpkg will look for.
|
||||
--
|
||||
-- Because it can run as root, gstore follows syspkg's loading rules, not
|
||||
-- the CWD-relative convention: -E in the shebang, package.path and every
|
||||
-- ffi.load() pinned to this script's own real directory, and when root,
|
||||
-- everything loaded must be root-owned and not group/world-writable all
|
||||
-- the way up to /. KEEP THIS PREAMBLE IN SYNC WITH syspkg's. It can't be
|
||||
-- shared through a lib/ file, since it has to run before anything from
|
||||
-- lib/ is trusted.
|
||||
|
||||
local ffi = require("ffi")
|
||||
local bit = require("bit")
|
||||
|
||||
-- asm() aliases so these don't collide with glacier_cdef's own
|
||||
-- declarations of the same libc symbols (ffi.cdef refuses redefinition).
|
||||
ffi.cdef[[
|
||||
unsigned int gstore_geteuid(void) asm("geteuid");
|
||||
char *gstore_realpath(const char *path, char *resolved) asm("realpath");
|
||||
struct gstore_passwd {
|
||||
char *pw_name;
|
||||
char *pw_passwd;
|
||||
unsigned int pw_uid;
|
||||
unsigned int pw_gid;
|
||||
char *pw_gecos;
|
||||
char *pw_dir;
|
||||
char *pw_shell;
|
||||
};
|
||||
struct gstore_passwd *gstore_getpwnam(const char *name) asm("getpwnam");
|
||||
]]
|
||||
|
||||
-- Errors raised before the trust checks below have passed. Same as
|
||||
-- syspkg: reproduces lg_printf(LG_ERR, ...) byte for byte, because
|
||||
-- loading libglacier_log as root before verifying it is the hole the
|
||||
-- checks close.
|
||||
local function die(fmt, ...)
|
||||
io.stdout:write("\27[31m\27[1m[x]\27[0m\27[m " .. string.format(fmt, ...) .. "\n")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local is_root = ffi.C.gstore_geteuid() == 0
|
||||
|
||||
-- This script's own directory, symlinks resolved.
|
||||
local self_dir
|
||||
do
|
||||
local buf = ffi.new("char[4096]")
|
||||
if ffi.C.gstore_realpath(arg[0], buf) == nil then
|
||||
die("Cannot resolve own location from '%s'.", tostring(arg[0]))
|
||||
end
|
||||
self_dir = ffi.string(buf):match("^(.*)/[^/]*$")
|
||||
if not self_dir or self_dir == "" then self_dir = "/" end
|
||||
end
|
||||
|
||||
local function shell_quote(s)
|
||||
return "'" .. s:gsub("'", "'\\''") .. "'"
|
||||
end
|
||||
|
||||
local function require_root_owned(path)
|
||||
local p = io.popen("stat -L -c '%u %a' -- " .. shell_quote(path) .. " 2>/dev/null")
|
||||
local line = p and p:read("*l")
|
||||
if p then p:close() end
|
||||
|
||||
local uid, mode = (line or ""):match("^(%d+) ([0-7]+)$")
|
||||
if not uid then
|
||||
die("Cannot stat %s.", path)
|
||||
end
|
||||
if tonumber(uid) ~= 0 then
|
||||
die("Refusing to run: %s is not owned by root.", path)
|
||||
end
|
||||
if bit.band(tonumber(mode, 8), tonumber("022", 8)) ~= 0 then
|
||||
die("Refusing to run: %s is group- or world-writable.", path)
|
||||
end
|
||||
end
|
||||
|
||||
local trusted = {}
|
||||
do
|
||||
local d = self_dir
|
||||
while true do
|
||||
table.insert(trusted, d)
|
||||
if d == "/" then break end
|
||||
d = d:match("^(.*)/[^/]*$")
|
||||
if d == "" then d = "/" end
|
||||
end
|
||||
end
|
||||
|
||||
local lib_dir = self_dir .. "/lib"
|
||||
local so_dir = lib_dir .. "/glacier"
|
||||
local so = {
|
||||
log = so_dir .. "/libglacier_log.so",
|
||||
istore = so_dir .. "/libglacier_istoreutils.so",
|
||||
}
|
||||
|
||||
-- libglacier_pkg.so and libglacier_transaction.so are never ffi.load()ed
|
||||
-- directly, but istoreutils pulls them in through its $ORIGIN RUNPATH,
|
||||
-- so they run as root too.
|
||||
for _, p in ipairs({
|
||||
lib_dir, so_dir,
|
||||
lib_dir .. "/cargs.lua",
|
||||
lib_dir .. "/glacier_cdef.lua",
|
||||
so.log, so.istore,
|
||||
so_dir .. "/libglacier_pkg.so",
|
||||
so_dir .. "/libglacier_transaction.so",
|
||||
}) do
|
||||
table.insert(trusted, p)
|
||||
end
|
||||
|
||||
if is_root then
|
||||
-- Installed, lib/glacier and each .so are symlinks out of this
|
||||
-- package's store dir (lib/glacier -> /usr/lib/glacier -> links
|
||||
-- into libglacier-ng's), so the directories the loader really
|
||||
-- walks are the REAL path's ancestors. Check those too.
|
||||
local checked = {}
|
||||
local buf = ffi.new("char[4096]")
|
||||
for _, p in ipairs(trusted) do
|
||||
require_root_owned(p)
|
||||
if ffi.C.gstore_realpath(p, buf) == nil then
|
||||
die("Cannot stat %s.", p)
|
||||
end
|
||||
local d = ffi.string(buf)
|
||||
while not checked[d] do
|
||||
checked[d] = true
|
||||
require_root_owned(d)
|
||||
if d == "/" then break end
|
||||
d = d:match("^(.*)/[^/]*$")
|
||||
if d == "" then d = "/" end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
package.path = self_dir .. "/?.lua"
|
||||
package.cpath = ""
|
||||
|
||||
local cargs = require("lib/cargs")
|
||||
require("lib/glacier_cdef")
|
||||
|
||||
local log = ffi.load(so.log)
|
||||
local istore = ffi.load(so.istore)
|
||||
|
||||
local USR_ROOT = "/glacier/usr"
|
||||
|
||||
local argc, argv = cargs.get_argv()
|
||||
local args = {}
|
||||
|
||||
local usage_line = "usage: %s [-hv] [-n [UID]] [-sbx UID] [-V]"
|
||||
|
||||
local longopts = {
|
||||
help = "-h",
|
||||
version = "-v",
|
||||
sync = "-s",
|
||||
new = "-n",
|
||||
delete = "-x",
|
||||
backup = "-b",
|
||||
verbose = "-V",
|
||||
}
|
||||
|
||||
for i = 1, argc - 1 do
|
||||
table.insert(args, ffi.string(argv[i]))
|
||||
end
|
||||
|
||||
if #args == 0 then
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local isVerbose = false
|
||||
|
||||
-- Whose directory `gstore -n` with no UID is for. Same order as gpkg's
|
||||
-- get_loginuid (so the two always agree), plus DOAS_USER for when doas
|
||||
-- is the reason we're uid 0: the kernel's loginuid if one was set at
|
||||
-- login, else the user doas says invoked it, else our real uid.
|
||||
local function invoking_uid()
|
||||
local f = io.open("/proc/self/loginuid", "r")
|
||||
if f then
|
||||
local n = tonumber(f:read("*l"))
|
||||
f:close()
|
||||
if n and n ~= 4294967295 then return n end
|
||||
end
|
||||
|
||||
local doas_user = os.getenv("DOAS_USER")
|
||||
if is_root and doas_user then
|
||||
local pw = ffi.C.gstore_getpwnam(doas_user)
|
||||
if pw ~= nil then return tonumber(pw.pw_uid) end
|
||||
end
|
||||
|
||||
return tonumber(ffi.C.getuid())
|
||||
end
|
||||
|
||||
-- `-n`'s UID is optional; everything else's isn't.
|
||||
local function uid_arg(i, optional)
|
||||
local uid = args[i + 1]
|
||||
if uid == nil or uid:sub(1, 1) == "-" then
|
||||
if optional then return nil end
|
||||
log.lg_printf(2, "No UID was supplied.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
-- A uid_t: anything else would wrap around in C to some other UID.
|
||||
local uidn = tonumber(uid)
|
||||
if not uidn or uidn ~= math.floor(uidn) or uidn < 0 or uidn > 4294967294 then
|
||||
log.lg_printf(2, "Invalid UID: %s", uid)
|
||||
os.exit(1)
|
||||
end
|
||||
return uidn
|
||||
end
|
||||
|
||||
local function handle_flag(flag, i)
|
||||
if flag == "-h" then
|
||||
log.lg_gstore_help(argc, argv)
|
||||
os.exit(0)
|
||||
elseif flag == "-v" then
|
||||
print("v4.0.0")
|
||||
os.exit(0)
|
||||
elseif flag == "-V" then
|
||||
isVerbose = true
|
||||
elseif flag == "-s" then
|
||||
log.lg_printf(2, "-s is not implemented yet.")
|
||||
os.exit(1)
|
||||
elseif flag == "-n" then
|
||||
if isVerbose then
|
||||
print("Glacier v4.0.0")
|
||||
end
|
||||
|
||||
if not is_root then
|
||||
log.lg_printf(2, "Cannot create Glacier directories: Permission denied")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local uidn = uid_arg(i, true) or invoking_uid()
|
||||
if uidn == 0 then
|
||||
log.lg_printf(2, "No UID was supplied.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if uidn < 1000 then
|
||||
log.lg_printf(2, "Cannot create Glacier directories for system UIDs.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if istore.gl_init_user(uidn, isVerbose) ~= 0 then
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local base = string.format("%s/%d", USR_ROOT, uidn)
|
||||
log.lg_printf(3, "Successfully created Glacier directories for UID %d", ffi.new("int", uidn))
|
||||
log.lg_printf(3, "The new directories are located at:")
|
||||
print("\t* " .. base .. "/index")
|
||||
print("\t* " .. base .. "/store")
|
||||
print("\t* " .. base .. "/links")
|
||||
os.exit(0)
|
||||
elseif flag == "-b" then
|
||||
local uidn = uid_arg(i, false)
|
||||
local uids = tostring(uidn)
|
||||
|
||||
local tar_path = args[i + 2]
|
||||
if not tar_path then
|
||||
log.lg_printf(2, "No OUTFILE was supplied.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local base = USR_ROOT .. "/" .. uids
|
||||
|
||||
local uid_index_exists = istore.gl_usr_istore_exists(ffi.C.INDEX, uidn)
|
||||
local uid_store_exists = istore.gl_usr_istore_exists(ffi.C.STORE, uidn)
|
||||
|
||||
if not uid_index_exists and not uid_store_exists then
|
||||
log.lg_printf(2, "Index and store for UID %s do not exist.", uids)
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if not uid_index_exists and uid_store_exists then
|
||||
log.lg_printf(2, "UID %s is missing its index directory.", uids)
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if uid_index_exists and not uid_store_exists then
|
||||
log.lg_printf(2, "UID %s is missing its store directory.", uids)
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
local status = istore.gl_backup_istore(tar_path, USR_ROOT, uids)
|
||||
|
||||
if status ~= 0 then
|
||||
log.lg_printf(2, "Failed to create backup for UID %s.", uids)
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
log.lg_printf(3, "Successfully created backup for UID %s.", uids)
|
||||
log.lg_printf(3, "Archived the following directories:")
|
||||
print("\t* " .. base .. "/index")
|
||||
print("\t* " .. base .. "/store")
|
||||
print("\t* " .. base .. "/links")
|
||||
os.exit(0)
|
||||
elseif flag == "-x" then
|
||||
local uidn = uid_arg(i, false)
|
||||
|
||||
if uidn < 1000 then
|
||||
log.lg_printf(2, "Cannot delete Glacier directories for system UIDs.")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
if istore.gl_delete_user(uidn, isVerbose) == 0 then
|
||||
os.exit(0)
|
||||
end
|
||||
os.exit(1)
|
||||
else
|
||||
log.lg_printf(2, "Unknown option: %s", flag)
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
for i = 1, #args do
|
||||
local a = args[i]
|
||||
|
||||
if a:sub(1, 2) == "--" then
|
||||
local name = a:sub(3)
|
||||
local short = longopts[name]
|
||||
if not short then
|
||||
log.lg_printf(2, "Unknown option: %s", a)
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
a = short
|
||||
end
|
||||
|
||||
if a:sub(1, 1) == "-" and #a > 2 and a:sub(2, 2) ~= "-" then
|
||||
for c in a:sub(2):gmatch(".") do
|
||||
handle_flag("-" .. c, i)
|
||||
end
|
||||
elseif a:sub(1, 1) == "-" then
|
||||
handle_flag(a, i)
|
||||
elseif i > 1 and args[i - 1]:sub(1, 1) == "-" then
|
||||
-- operand of the preceding flag (UID / OUTFILE); handled there
|
||||
else
|
||||
log.lg_printf(2, "Unexpected argument: %s", a)
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
Executable
+385
@@ -0,0 +1,385 @@
|
||||
#!/usr/bin/luajit -E
|
||||
|
||||
-- syspkg: the SOLE system-scope glacier tool. Install, remove, and
|
||||
-- system store creation under GL_SCOPE_SYS. See SECURITY_DESIGN.txt
|
||||
-- sections 5 and 6.
|
||||
--
|
||||
-- Always run as root via doas (or sudo). NEVER setuid. syspkg's own
|
||||
-- job is only to refuse when euid != 0; the privilege boundary itself
|
||||
-- belongs to doas.
|
||||
--
|
||||
-- Unlike gpkg, this file does NOT follow the CWD-relative
|
||||
-- require("lib/...") / ffi.load("./lib/...") convention. As root, that
|
||||
-- convention means "load whatever lib/ is in the caller's current
|
||||
-- directory" — under a doas rule restricted to `cmd syspkg`, that is a
|
||||
-- full root escalation. So:
|
||||
-- * -E in the shebang: LuaJIT ignores LUA_PATH/LUA_CPATH/LUA_INIT.
|
||||
-- * package.path is pinned to this script's own real directory, and
|
||||
-- every .so is ffi.load()ed by absolute path from the same place.
|
||||
-- * every directory and file we load from must be root-owned and not
|
||||
-- group/world-writable, all the way up to /, or we refuse to run.
|
||||
|
||||
local ffi = require("ffi")
|
||||
local bit = require("bit")
|
||||
|
||||
-- asm() aliases so these don't collide with glacier_cdef's own
|
||||
-- declarations of the same libc symbols (ffi.cdef refuses redefinition).
|
||||
ffi.cdef[[
|
||||
unsigned int syspkg_geteuid(void) asm("geteuid");
|
||||
char *syspkg_realpath(const char *path, char *resolved) asm("realpath");
|
||||
]]
|
||||
|
||||
-- Errors raised before the trust checks below have passed. These can't
|
||||
-- go through libglacier_log's lg_printf: loading that .so as root before
|
||||
-- verifying it is exactly what the checks exist to prevent. So this
|
||||
-- reproduces lg_printf(LG_ERR, ...) byte for byte instead (color.h's
|
||||
-- COL_RED / COL_RESET, stdout, trailing newline).
|
||||
local function die(fmt, ...)
|
||||
io.stdout:write("\27[31m\27[1m[x]\27[0m\27[m " .. string.format(fmt, ...) .. "\n")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
-- Non-root may still reach -h and -v; anything that touches the system
|
||||
-- store is refused after argument parsing, below.
|
||||
local is_root = ffi.C.syspkg_geteuid() == 0
|
||||
|
||||
-- This script's own directory, symlinks resolved.
|
||||
local self_dir
|
||||
do
|
||||
local buf = ffi.new("char[4096]")
|
||||
if ffi.C.syspkg_realpath(arg[0], buf) == nil then
|
||||
die("Cannot resolve own location from '%s'.", tostring(arg[0]))
|
||||
end
|
||||
self_dir = ffi.string(buf):match("^(.*)/[^/]*$")
|
||||
if not self_dir or self_dir == "" then self_dir = "/" end
|
||||
end
|
||||
|
||||
local function shell_quote(s)
|
||||
return "'" .. s:gsub("'", "'\\''") .. "'"
|
||||
end
|
||||
|
||||
-- Refuse unless `path` is owned by root and not writable by group or
|
||||
-- other. Anything less means a non-root user could swap in code that
|
||||
-- syspkg would then run as root.
|
||||
local function require_root_owned(path)
|
||||
local p = io.popen("stat -L -c '%u %a' -- " .. shell_quote(path) .. " 2>/dev/null")
|
||||
local line = p and p:read("*l")
|
||||
if p then p:close() end
|
||||
|
||||
local uid, mode = (line or ""):match("^(%d+) ([0-7]+)$")
|
||||
if not uid then
|
||||
die("Cannot stat %s.", path)
|
||||
end
|
||||
if tonumber(uid) ~= 0 then
|
||||
die("Refusing to run: %s is not owned by root.", path)
|
||||
end
|
||||
if bit.band(tonumber(mode, 8), tonumber("022", 8)) ~= 0 then
|
||||
die("Refusing to run: %s is group- or world-writable.", path)
|
||||
end
|
||||
end
|
||||
|
||||
local trusted = {}
|
||||
do
|
||||
local d = self_dir
|
||||
while true do
|
||||
table.insert(trusted, d)
|
||||
if d == "/" then break end
|
||||
d = d:match("^(.*)/[^/]*$")
|
||||
if d == "" then d = "/" end
|
||||
end
|
||||
end
|
||||
|
||||
local lib_dir = self_dir .. "/lib"
|
||||
local so_dir = lib_dir .. "/glacier"
|
||||
local so = {
|
||||
log = so_dir .. "/libglacier_log.so",
|
||||
istore = so_dir .. "/libglacier_istoreutils.so",
|
||||
txn = so_dir .. "/libglacier_transaction.so",
|
||||
verify = so_dir .. "/libglacier_verify.so",
|
||||
}
|
||||
|
||||
-- libglacier_pkg.so is never ffi.load()ed directly, but istoreutils
|
||||
-- pulls it in through its $ORIGIN RUNPATH, so it runs as root too.
|
||||
for _, p in ipairs({
|
||||
lib_dir, so_dir,
|
||||
lib_dir .. "/cargs.lua",
|
||||
lib_dir .. "/glacier_cdef.lua",
|
||||
lib_dir .. "/gpkg_common.lua",
|
||||
lib_dir .. "/gpkg_build.lua",
|
||||
so.log, so.istore, so.txn, so.verify,
|
||||
so_dir .. "/libglacier_pkg.so",
|
||||
}) do
|
||||
table.insert(trusted, p)
|
||||
end
|
||||
|
||||
-- Only meaningful as root: unprivileged, loading a user-owned lib/ gains
|
||||
-- the caller nothing they couldn't already run themselves.
|
||||
if is_root then
|
||||
-- Installed, lib/glacier and each .so are symlinks out of this
|
||||
-- package's store dir (lib/glacier -> /usr/lib/glacier -> links
|
||||
-- into libglacier-ng's), so the directories the loader really
|
||||
-- walks are the REAL path's ancestors. Check those too.
|
||||
local checked = {}
|
||||
local buf = ffi.new("char[4096]")
|
||||
for _, p in ipairs(trusted) do
|
||||
require_root_owned(p)
|
||||
if ffi.C.syspkg_realpath(p, buf) == nil then
|
||||
die("Cannot stat %s.", p)
|
||||
end
|
||||
local d = ffi.string(buf)
|
||||
while not checked[d] do
|
||||
checked[d] = true
|
||||
require_root_owned(d)
|
||||
if d == "/" then break end
|
||||
d = d:match("^(.*)/[^/]*$")
|
||||
if d == "" then d = "/" end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
package.path = self_dir .. "/?.lua"
|
||||
package.cpath = ""
|
||||
|
||||
local cargs = require("lib/cargs")
|
||||
local glacier_cdef = require("lib/glacier_cdef")
|
||||
local common = require("lib/gpkg_common")
|
||||
|
||||
local log = ffi.load(so.log)
|
||||
local istore = ffi.load(so.istore)
|
||||
local txn = ffi.load(so.txn)
|
||||
local verify = ffi.load(so.verify)
|
||||
|
||||
local argc, argv = cargs.get_argv()
|
||||
|
||||
local usage_line = "usage: %s [-hv] [-n] [-y] [-flux PKG] [-OSVq] [-R DIR] [--strict]"
|
||||
|
||||
local longopts = {
|
||||
help = "-h",
|
||||
version = "-v",
|
||||
verbose = "-V",
|
||||
quiet = "-q",
|
||||
["skip-installed"] = "-S",
|
||||
overwrite = "-O",
|
||||
new = "-n",
|
||||
["local"] = "-l",
|
||||
merge = "-f",
|
||||
update = "-u",
|
||||
remove = "-x",
|
||||
refresh = "-y",
|
||||
root = "-R",
|
||||
strict = "--strict",
|
||||
}
|
||||
|
||||
local args = {}
|
||||
for i = 1, argc - 1 do
|
||||
table.insert(args, ffi.string(argv[i]))
|
||||
end
|
||||
|
||||
if #args == 0 then
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
-- Parse everything first, then act once: unlike gpkg, the outcome
|
||||
-- doesn't depend on whether -V came before or after -l.
|
||||
local mode
|
||||
local operands = {}
|
||||
local isVerbose = false
|
||||
local root -- -R DIR: operate on the system installed under DIR
|
||||
local strict = false -- --strict (implied by -R)
|
||||
|
||||
local function set_mode(m)
|
||||
if mode and mode ~= m then
|
||||
log.lg_printf(2, "-n, -l, -f, -u, -x and -y are mutually exclusive.")
|
||||
os.exit(1)
|
||||
end
|
||||
mode = m
|
||||
end
|
||||
|
||||
local function handle_flag(flag)
|
||||
if flag == "-h" then
|
||||
log.lg_syspkg_help(argc, argv)
|
||||
os.exit(0)
|
||||
elseif flag == "-v" then
|
||||
print("v4.0.0")
|
||||
os.exit(0)
|
||||
elseif flag == "-V" then
|
||||
isVerbose = true
|
||||
common.set_verbose(true)
|
||||
elseif flag == "-q" then
|
||||
common.set_quiet(true)
|
||||
elseif flag == "-S" then
|
||||
common.set_skip_installed(true)
|
||||
elseif flag == "-O" then
|
||||
common.set_overwrite(true)
|
||||
elseif flag == "-n" then
|
||||
set_mode("new")
|
||||
elseif flag == "-l" then
|
||||
set_mode("install")
|
||||
elseif flag == "-f" then
|
||||
set_mode("fetch")
|
||||
elseif flag == "-u" then
|
||||
set_mode("upgrade")
|
||||
elseif flag == "-x" then
|
||||
set_mode("remove")
|
||||
elseif flag == "-y" then
|
||||
set_mode("refresh")
|
||||
elseif flag == "--strict" then
|
||||
strict = true
|
||||
else
|
||||
log.lg_printf(2, "Unknown option: %s", flag)
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
common.init({ log = log, istore = istore, txn = txn, verify = verify, argv = argv,
|
||||
so_dir = so_dir })
|
||||
|
||||
-- -R takes the next argument (or --root=DIR); in a bundle like -lR it
|
||||
-- must come last.
|
||||
local function set_root(dir)
|
||||
if not dir or dir == "" then
|
||||
log.lg_printf(2, "-R needs a directory.")
|
||||
os.exit(1)
|
||||
end
|
||||
if root then
|
||||
log.lg_printf(2, "-R given more than once.")
|
||||
os.exit(1)
|
||||
end
|
||||
root = dir
|
||||
end
|
||||
|
||||
local i = 1
|
||||
while i <= #args do
|
||||
local a = args[i]
|
||||
if a:sub(1, 2) == "--" then
|
||||
local name, value = a:sub(3):match("^([^=]*)=(.*)$")
|
||||
name = name or a:sub(3)
|
||||
local short = longopts[name]
|
||||
if not short or (value and short ~= "-R") then
|
||||
log.lg_printf(2, "Unknown option: %s", a)
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
if short == "-R" then
|
||||
if not value then i = i + 1; value = args[i] end
|
||||
set_root(value)
|
||||
else
|
||||
handle_flag(short)
|
||||
end
|
||||
elseif a:sub(1, 1) == "-" and #a > 1 then
|
||||
local flags = a:sub(2)
|
||||
for k = 1, #flags do
|
||||
local c = flags:sub(k, k)
|
||||
if c == "R" then
|
||||
if k ~= #flags then
|
||||
log.lg_printf(2, "-R must come last in %s.", a)
|
||||
os.exit(1)
|
||||
end
|
||||
i = i + 1
|
||||
set_root(args[i])
|
||||
else
|
||||
handle_flag("-" .. c)
|
||||
end
|
||||
end
|
||||
elseif mode == "install" or mode == "remove" or mode == "fetch"
|
||||
or mode == "upgrade" then
|
||||
table.insert(operands, a)
|
||||
else
|
||||
log.lg_printf(2, "Unexpected argument: %s", a)
|
||||
os.exit(1)
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
|
||||
if mode and not is_root then
|
||||
log.lg_printf(2, "Cannot open system index: Permission denied")
|
||||
os.exit(1)
|
||||
end
|
||||
|
||||
-- The install root, refused here (by the library, which says why) before
|
||||
-- anything reads or writes under it: it and every directory above it
|
||||
-- must be root-owned directories that aren't group/world-writable.
|
||||
-- `prefix` is what the messages below put in front of /glacier/...
|
||||
local prefix = ""
|
||||
if root then
|
||||
if txn.gl_check_sysroot(root) ~= 0 then
|
||||
os.exit(1)
|
||||
end
|
||||
prefix = root:gsub("/+$", "")
|
||||
end
|
||||
|
||||
local function exists(path, test)
|
||||
local ok = os.execute("test " .. test .. " " .. shell_quote(path))
|
||||
return ok == true or ok == 0
|
||||
end
|
||||
|
||||
local function require_sys_istore()
|
||||
local run = ffi.string(argv[0]) .. (root and (" -R " .. root) or "")
|
||||
if not exists(prefix .. "/glacier/sys/index/current", "-e") then
|
||||
log.lg_printf(2, "No system index found. Run %s -n first.", run)
|
||||
os.exit(1)
|
||||
end
|
||||
if not exists(prefix .. "/glacier/sys/store", "-d") then
|
||||
log.lg_printf(2, "No system store found. Run %s -n first.", run)
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
|
||||
local opts = {
|
||||
scope = ffi.C.GL_SCOPE_SYS,
|
||||
uid = 0,
|
||||
scope_label = "for the system scope",
|
||||
sysroot = root,
|
||||
-- An image's /usr starts empty: anything refused there is two
|
||||
-- packages colliding or a broken package, never a distro file.
|
||||
strict = strict or root ~= nil,
|
||||
}
|
||||
|
||||
if mode == "new" then
|
||||
if #operands > 0 then
|
||||
log.lg_printf(2, "-n takes no arguments.")
|
||||
os.exit(1)
|
||||
end
|
||||
-- gl_init_sys logs its own reason (e.g. "already exists"),
|
||||
-- same as gstore -n leaves it to gl_init_user.
|
||||
if istore.gl_init_sys_at(root or "/", isVerbose) ~= 0 then
|
||||
os.exit(1)
|
||||
end
|
||||
log.lg_printf(3, "Created system glacier directories:")
|
||||
print("\t* " .. prefix .. "/glacier/sys/index")
|
||||
print("\t* " .. prefix .. "/glacier/sys/store")
|
||||
print("\t* " .. prefix .. "/glacier/sys/links")
|
||||
os.exit(0)
|
||||
elseif mode == "install" then
|
||||
if #operands == 0 then
|
||||
log.lg_printf(2, "usage: %s -l PKG...", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
require_sys_istore()
|
||||
common.install(operands, opts)
|
||||
elseif mode == "fetch" then
|
||||
if #operands == 0 then
|
||||
log.lg_printf(2, "usage: %s -f PKG...", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
require_sys_istore()
|
||||
common.fetch(operands, opts)
|
||||
elseif mode == "upgrade" then
|
||||
require_sys_istore()
|
||||
common.upgrade(operands, opts)
|
||||
elseif mode == "remove" then
|
||||
if #operands == 0 then
|
||||
log.lg_printf(2, "usage: %s -x PKG...", argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
require_sys_istore()
|
||||
common.remove(operands, opts)
|
||||
elseif mode == "refresh" then
|
||||
require_sys_istore()
|
||||
os.exit(common.refresh(opts) and 0 or 1)
|
||||
else
|
||||
log.lg_printf(2, usage_line, argv[0])
|
||||
os.exit(1)
|
||||
end
|
||||
@@ -0,0 +1,80 @@
|
||||
# Alpine test image, default user `glacier` (uid 1000, in wheel).
|
||||
#
|
||||
# Build/run with: ./run_docker.sh (Dockerfile.alpine is the default)
|
||||
#
|
||||
# Layout rules this image has to respect (see PACKAGE_SCOPES.txt §1):
|
||||
# * /app is root-owned and not group/world-writable. gstore -n and
|
||||
# syspkg run as root and refuse to load code from anything a user
|
||||
# could edit — so the source tree can't be handed to `glacier`.
|
||||
# * /glacier is NOT pre-created or chown'd. `doas /app/gstore -n`
|
||||
# creates /glacier/usr/1000 (owned by glacier); `doas /app/syspkg -n`
|
||||
# creates /glacier/sys. A user-owned /glacier/usr is refused.
|
||||
# * The .so's checked into lib/glacier/ are host (glibc) builds and
|
||||
# won't load on musl, so libglacier-ng is built from source here,
|
||||
# from the named build context `libglacier` (run_docker.sh passes it).
|
||||
|
||||
FROM alpine:latest AS libglacier-build
|
||||
|
||||
RUN apk add --no-cache build-base luajit-dev libarchive-dev libconfig-dev linux-headers
|
||||
|
||||
COPY --from=libglacier . /build
|
||||
WORKDIR /build
|
||||
RUN make clean && make
|
||||
|
||||
FROM alpine:latest
|
||||
|
||||
RUN apk update && apk add build-base git lua-dev luajit-dev doas libarchive-static libarchive-dev libconfig-dev linux-headers valgrind gdb
|
||||
RUN apk add --no-cache shadow
|
||||
RUN adduser -D -u 1000 glacier && \
|
||||
echo "glacier:password" | chpasswd && \
|
||||
addgroup glacier wheel
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
COPY --from=libglacier-build /build/build/lib/shared/libglacier_*.so /app/lib/glacier/
|
||||
COPY --from=libglacier-build /build/build/bin/gsandbox /app/lib/glacier/
|
||||
RUN chown -R root:root /app && chmod -R go-w /app
|
||||
RUN install -Dm644 /app/etc/glacier/glacier.conf /etc/glacier/glacier.conf
|
||||
|
||||
# A TEST signing key, so base packages can be installed here: syspkg
|
||||
# requires their sidecar signed by a key listed for base in
|
||||
# /etc/glacier/repos.conf (REPO_DESIGN.txt section 7). Its secret half is
|
||||
# in the image, so never trust it anywhere else. Sign with
|
||||
# `tests/mkpkg -k /root/glacier-test.sec ... base` (or gbuild -k; gbuild
|
||||
# finds gsha256sum and gsign on PATH). grepo is there too.
|
||||
COPY --from=libglacier-build /build/build/bin/gsign /build/build/bin/gsha256sum /build/build/bin/grepo /usr/local/bin/
|
||||
RUN mkdir -p /etc/glacier/keys && \
|
||||
gsign -G -p /etc/glacier/keys/glacier-test.pub -s /root/glacier-test.sec -c glacier-test >/dev/null && \
|
||||
printf 'repos = (\n\t{ name = "base"; keys = [ "glacier-test" ]; }\n);\n' > /etc/glacier/repos.conf && \
|
||||
chmod 644 /etc/glacier/repos.conf
|
||||
|
||||
RUN chmod 4755 /bin/su
|
||||
RUN chmod 4755 /usr/bin/doas
|
||||
RUN passwd -d root
|
||||
|
||||
# doas uses the LAST rule that matches.
|
||||
# 1. glacier may run anything as root (this image's dev convenience,
|
||||
# same as before) — needed for `doas /app/syspkg ...`.
|
||||
# 2. The rule Everest itself would ship: any wheel user may run
|
||||
# exactly `gstore -n` as root (no other arguments), so they can
|
||||
# create their OWN /glacier/usr/<uid> but can't name another UID.
|
||||
# With rule 1 present it adds nothing for glacier; delete rule 1 to
|
||||
# test glacier under the real, restricted setup.
|
||||
RUN printf '%s\n' \
|
||||
'permit nopass glacier' \
|
||||
'permit nopass :wheel as root cmd /app/gstore args -n' \
|
||||
> /etc/doas.conf && chmod 0400 /etc/doas.conf
|
||||
|
||||
RUN printf '%s\n' \
|
||||
'# Per-user links first, then system-scope (non-base) links. Not' \
|
||||
'# conditional on the dirs existing: /glacier is only created later,' \
|
||||
'# by syspkg -n / gstore -n, and a missing PATH entry is harmless.' \
|
||||
'GLACIER_LINKS="/glacier/usr/$(id -u)/links"' \
|
||||
'GLACIER_SYS_LINKS="/glacier/sys/links"' \
|
||||
'export PATH="$GLACIER_LINKS/bin:$GLACIER_SYS_LINKS/bin:$PATH"' \
|
||||
'export LD_LIBRARY_PATH="$GLACIER_LINKS/lib:$GLACIER_SYS_LINKS/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"' \
|
||||
> /etc/profile.d/glacier.sh
|
||||
|
||||
USER glacier
|
||||
|
||||
CMD ["/bin/sh", "-l"]
|
||||
@@ -0,0 +1,79 @@
|
||||
# Root variant of Dockerfile.alpine, for exercising syspkg.
|
||||
#
|
||||
# syspkg refuses to run as root unless every directory up to / and every
|
||||
# .lua/.so it loads is root-owned and not group/world-writable, so /app is
|
||||
# root-owned, go-w, and the default user is root.
|
||||
#
|
||||
# The .so's checked into lib/glacier/ are host (glibc) builds and won't
|
||||
# load on musl, so libglacier-ng is built from source in a first stage.
|
||||
# Its source comes from a named build context:
|
||||
#
|
||||
# ./run_docker.sh Dockerfile.alpine_root
|
||||
#
|
||||
# (which passes --build-context libglacier=../libglacier-ng; override the
|
||||
# path with LIBGLACIER_SRC).
|
||||
|
||||
FROM alpine:latest AS libglacier-build
|
||||
|
||||
# linux-headers: <linux/limits.h>, which Dockerfile.alpine is missing.
|
||||
RUN apk add --no-cache build-base luajit-dev libarchive-dev libconfig-dev linux-headers
|
||||
|
||||
COPY --from=libglacier . /build
|
||||
WORKDIR /build
|
||||
RUN make clean && make
|
||||
|
||||
FROM alpine:latest
|
||||
|
||||
# curl: https repositories and build sources.
|
||||
RUN apk update && apk add build-base curl git tar lua-dev luajit-dev libarchive-static libarchive-dev libconfig-dev linux-headers valgrind gdb doas
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
COPY --from=libglacier-build /build/build/lib/shared/libglacier_*.so /app/lib/glacier/
|
||||
COPY --from=libglacier-build /build/build/bin/gsandbox /app/lib/glacier/
|
||||
|
||||
RUN chown -R root:root /app && chmod -R go-w /app
|
||||
RUN install -Dm644 /app/etc/glacier/glacier.conf /etc/glacier/glacier.conf
|
||||
|
||||
# A TEST signing key, so base packages can be installed here: syspkg
|
||||
# requires their sidecar signed by a key listed for base in
|
||||
# /etc/glacier/repos.conf (REPO_DESIGN.txt section 7). Its secret half is
|
||||
# in the image, so never trust it anywhere else. Sign with
|
||||
# `tests/mkpkg -k /root/glacier-test.sec ... base` (or gbuild -k; gbuild
|
||||
# finds gsha256sum and gsign on PATH). grepo is there too.
|
||||
COPY --from=libglacier-build /build/build/bin/gsign /build/build/bin/gsha256sum /build/build/bin/grepo /usr/local/bin/
|
||||
RUN mkdir -p /etc/glacier/keys && \
|
||||
gsign -G -p /etc/glacier/keys/glacier-test.pub -s /root/glacier-test.sec -c glacier-test >/dev/null && \
|
||||
printf 'repos = (\n\t{ name = "base"; keys = [ "glacier-test" ]; }\n);\n' > /etc/glacier/repos.conf && \
|
||||
chmod 644 /etc/glacier/repos.conf
|
||||
|
||||
# /glacier is deliberately not pre-created: `syspkg -n` builds the whole
|
||||
# system tree, and `gstore -n` a user's /glacier/usr/<uid>, parents
|
||||
# included.
|
||||
|
||||
# An unprivileged user to exercise per-user scope (`su glacier`). The
|
||||
# wheel rule pins gstore's arguments to exactly `-n`, so a user can create
|
||||
# their OWN /glacier/usr/<uid> but can't name anyone else's UID. glacier
|
||||
# itself may run anything as root, for interactive use (`doas syspkg`);
|
||||
# user_layout_alpine.sh drops that rule to test the pinned one.
|
||||
RUN adduser -D -u 1000 glacier && addgroup glacier wheel && \
|
||||
printf '%s\n' 'permit nopass glacier as root' \
|
||||
'permit nopass :wheel as root cmd /app/gstore args -n' > /etc/doas.conf && \
|
||||
chmod 0400 /etc/doas.conf
|
||||
# /app (gpkg, syspkg, gstore, ...) on PATH: here for `docker exec` and
|
||||
# `su`, and in profile.d because Alpine's /etc/profile resets PATH.
|
||||
ENV PATH="$PATH:/app"
|
||||
RUN printf '%s\n' \
|
||||
'# Per-user links first, then system-scope (non-base) links. Not' \
|
||||
'# conditional on the dirs existing: /glacier is only created later,' \
|
||||
'# by syspkg -n / gstore -n, and a missing PATH entry is harmless.' \
|
||||
'# /app (the glacier-ng tree) last.' \
|
||||
'GLACIER_LINKS="/glacier/usr/$(id -u)/links"' \
|
||||
'GLACIER_SYS_LINKS="/glacier/sys/links"' \
|
||||
'export PATH="$GLACIER_LINKS/bin:$GLACIER_SYS_LINKS/bin:$PATH:/app"' \
|
||||
'export LD_LIBRARY_PATH="$GLACIER_LINKS/lib:$GLACIER_SYS_LINKS/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"' \
|
||||
> /etc/profile.d/glacier.sh
|
||||
|
||||
USER root
|
||||
|
||||
CMD ["/bin/sh", "-l"]
|
||||
@@ -0,0 +1,67 @@
|
||||
# Arch (glibc) test image, default user `glacier` (uid 1000, in wheel).
|
||||
# Same layout rules as Dockerfile.alpine — read its header:
|
||||
# * /app root-owned, not group/world-writable (gstore -n and syspkg run
|
||||
# as root and refuse code a user could edit);
|
||||
# * /glacier NOT pre-created (`doas /app/gstore -n`, `doas /app/syspkg -n`);
|
||||
# * libglacier-ng built from source here, from the named build context
|
||||
# `libglacier` that run_docker.sh passes.
|
||||
#
|
||||
# ./run_docker.sh Dockerfile.arch
|
||||
|
||||
FROM archlinux:latest AS base
|
||||
RUN pacman-key --init && pacman-key --populate archlinux && \
|
||||
pacman -Syu --noconfirm --needed archlinux-keyring && \
|
||||
pacman -S --noconfirm --needed base-devel libarchive libconfig && \
|
||||
pacman -Scc --noconfirm
|
||||
|
||||
FROM base AS libglacier-build
|
||||
COPY --from=libglacier . /build
|
||||
WORKDIR /build
|
||||
RUN make clean && make
|
||||
|
||||
FROM base
|
||||
RUN pacman -S --noconfirm --needed luajit opendoas git && pacman -Scc --noconfirm
|
||||
|
||||
RUN useradd -m -u 1000 -G wheel glacier && \
|
||||
echo "glacier:password" | chpasswd
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
COPY --from=libglacier-build /build/build/lib/shared/libglacier_*.so /app/lib/glacier/
|
||||
COPY --from=libglacier-build /build/build/bin/gsandbox /app/lib/glacier/
|
||||
RUN chown -R root:root /app && chmod -R go-w /app
|
||||
RUN install -Dm644 /app/etc/glacier/glacier.conf /etc/glacier/glacier.conf
|
||||
|
||||
# A TEST signing key, so base packages can be installed here: syspkg
|
||||
# requires their sidecar signed by a key listed for base in
|
||||
# /etc/glacier/repos.conf (REPO_DESIGN.txt section 7). Its secret half is
|
||||
# in the image, so never trust it anywhere else. Sign with
|
||||
# `tests/mkpkg -k /root/glacier-test.sec ... base` (or gbuild -k; gbuild
|
||||
# finds gsha256sum and gsign on PATH). grepo is there too.
|
||||
COPY --from=libglacier-build /build/build/bin/gsign /build/build/bin/gsha256sum /build/build/bin/grepo /usr/local/bin/
|
||||
RUN mkdir -p /etc/glacier/keys && \
|
||||
gsign -G -p /etc/glacier/keys/glacier-test.pub -s /root/glacier-test.sec -c glacier-test >/dev/null && \
|
||||
printf 'repos = (\n\t{ name = "base"; keys = [ "glacier-test" ]; }\n);\n' > /etc/glacier/repos.conf && \
|
||||
chmod 644 /etc/glacier/repos.conf
|
||||
|
||||
# doas uses the LAST matching rule. 1: this image's dev convenience
|
||||
# (glacier may do anything as root). 2: the rule Everest itself ships —
|
||||
# any wheel user may run exactly `gstore -n`, i.e. create their OWN
|
||||
# /glacier/usr/<uid>. Delete rule 1 to test under the real setup.
|
||||
RUN printf '%s\n' \
|
||||
'permit nopass glacier' \
|
||||
'permit nopass :wheel as root cmd /app/gstore args -n' \
|
||||
> /etc/doas.conf && chmod 0400 /etc/doas.conf
|
||||
|
||||
RUN printf '%s\n' \
|
||||
'# Per-user links first, then system-scope (non-base) links. Not' \
|
||||
'# conditional on the dirs existing: /glacier is only created later,' \
|
||||
'# by syspkg -n / gstore -n, and a missing PATH entry is harmless.' \
|
||||
'GLACIER_LINKS="/glacier/usr/$(id -u)/links"' \
|
||||
'GLACIER_SYS_LINKS="/glacier/sys/links"' \
|
||||
'export PATH="$GLACIER_LINKS/bin:$GLACIER_SYS_LINKS/bin:$PATH"' \
|
||||
'export LD_LIBRARY_PATH="$GLACIER_LINKS/lib:$GLACIER_SYS_LINKS/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"' \
|
||||
> /etc/profile.d/glacier.sh
|
||||
|
||||
USER glacier
|
||||
CMD ["/bin/sh", "-l"]
|
||||
@@ -0,0 +1,69 @@
|
||||
# Debian (glibc) test image, default user `glacier` (uid 1000, in wheel).
|
||||
# Same layout rules as Dockerfile.alpine — read its header:
|
||||
# * /app root-owned, not group/world-writable (gstore -n and syspkg run
|
||||
# as root and refuse code a user could edit);
|
||||
# * /glacier NOT pre-created (`doas /app/gstore -n`, `doas /app/syspkg -n`);
|
||||
# * libglacier-ng built from source here, from the named build context
|
||||
# `libglacier` that run_docker.sh passes.
|
||||
#
|
||||
# ./run_docker.sh Dockerfile.debian
|
||||
|
||||
FROM debian:stable AS base
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
build-essential libarchive-dev libconfig-dev \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
FROM base AS libglacier-build
|
||||
COPY --from=libglacier . /build
|
||||
WORKDIR /build
|
||||
RUN make clean && make
|
||||
|
||||
FROM base
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
luajit doas git ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Debian has no wheel group; the doas rule below is written against it.
|
||||
RUN groupadd -f wheel && useradd -m -u 1000 -G wheel glacier && \
|
||||
echo "glacier:password" | chpasswd
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
COPY --from=libglacier-build /build/build/lib/shared/libglacier_*.so /app/lib/glacier/
|
||||
COPY --from=libglacier-build /build/build/bin/gsandbox /app/lib/glacier/
|
||||
RUN chown -R root:root /app && chmod -R go-w /app
|
||||
RUN install -Dm644 /app/etc/glacier/glacier.conf /etc/glacier/glacier.conf
|
||||
|
||||
# A TEST signing key, so base packages can be installed here: syspkg
|
||||
# requires their sidecar signed by a key listed for base in
|
||||
# /etc/glacier/repos.conf (REPO_DESIGN.txt section 7). Its secret half is
|
||||
# in the image, so never trust it anywhere else. Sign with
|
||||
# `tests/mkpkg -k /root/glacier-test.sec ... base` (or gbuild -k; gbuild
|
||||
# finds gsha256sum and gsign on PATH). grepo is there too.
|
||||
COPY --from=libglacier-build /build/build/bin/gsign /build/build/bin/gsha256sum /build/build/bin/grepo /usr/local/bin/
|
||||
RUN mkdir -p /etc/glacier/keys && \
|
||||
gsign -G -p /etc/glacier/keys/glacier-test.pub -s /root/glacier-test.sec -c glacier-test >/dev/null && \
|
||||
printf 'repos = (\n\t{ name = "base"; keys = [ "glacier-test" ]; }\n);\n' > /etc/glacier/repos.conf && \
|
||||
chmod 644 /etc/glacier/repos.conf
|
||||
|
||||
# doas uses the LAST matching rule. 1: this image's dev convenience
|
||||
# (glacier may do anything as root). 2: the rule Everest itself ships —
|
||||
# any wheel user may run exactly `gstore -n`, i.e. create their OWN
|
||||
# /glacier/usr/<uid>. Delete rule 1 to test under the real setup.
|
||||
RUN printf '%s\n' \
|
||||
'permit nopass glacier' \
|
||||
'permit nopass :wheel as root cmd /app/gstore args -n' \
|
||||
> /etc/doas.conf && chmod 0400 /etc/doas.conf
|
||||
|
||||
RUN printf '%s\n' \
|
||||
'# Per-user links first, then system-scope (non-base) links. Not' \
|
||||
'# conditional on the dirs existing: /glacier is only created later,' \
|
||||
'# by syspkg -n / gstore -n, and a missing PATH entry is harmless.' \
|
||||
'GLACIER_LINKS="/glacier/usr/$(id -u)/links"' \
|
||||
'GLACIER_SYS_LINKS="/glacier/sys/links"' \
|
||||
'export PATH="$GLACIER_LINKS/bin:$GLACIER_SYS_LINKS/bin:$PATH"' \
|
||||
'export LD_LIBRARY_PATH="$GLACIER_LINKS/lib:$GLACIER_SYS_LINKS/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"' \
|
||||
> /etc/profile.d/glacier.sh
|
||||
|
||||
USER glacier
|
||||
CMD ["/bin/sh", "-l"]
|
||||
@@ -0,0 +1,32 @@
|
||||
// /etc/glacier/glacier.conf - glacier configuration (libconfig syntax).
|
||||
// Read by gpkg and syspkg. Settings have no built-in defaults: if a
|
||||
// setting an operation needs is missing, that operation refuses to run.
|
||||
|
||||
// How many backups of replaced package versions to keep, per package.
|
||||
// When `-lO` replaces an installed version, the old one is compressed
|
||||
// into <scope>/backup/<repo>/<name>/<name>-<version>.gpkg (+ .sha256)
|
||||
// and the oldest backups beyond this count are deleted. 0 = no backups.
|
||||
KEEP_OLD = 1;
|
||||
|
||||
// Building from source (BUILD_DESIGN.txt). Used only when a package is
|
||||
// built from a NAME-src repository; a build without what it needs
|
||||
// refuses. Uncomment and adapt:
|
||||
//
|
||||
// The packages every build root has (from the binary repositories).
|
||||
// BUILD_ROOT = [ "musl", "busybox", "binutils", "gcc", "make", "patch" ];
|
||||
//
|
||||
// The unprivileged user syspkg builds as (not root; owns
|
||||
// /glacier/sys/build). gpkg builds as the user running it.
|
||||
// BUILD_USER = "_gbuild";
|
||||
//
|
||||
// Traits for every build, "name" on or "-name" off. A user's own
|
||||
// /glacier/usr/<uid>/config/glacier.conf overrides these, and a
|
||||
// per-package file (/etc/glacier/config/NAME.conf, or the user's
|
||||
// config/NAME.conf: TRAITS, CFLAGS, CXXFLAGS, LDFLAGS, CONFIGURE)
|
||||
// overrides both.
|
||||
// TRAITS = [ "-x11" ];
|
||||
//
|
||||
// CFLAGS = "-O2 -pipe";
|
||||
// CXXFLAGS = "-O2 -pipe";
|
||||
// LDFLAGS = "";
|
||||
// JOBS = 4;
|
||||
@@ -0,0 +1,9 @@
|
||||
// /etc/glacier/glacier.conf - glacier configuration (libconfig syntax).
|
||||
// Read by gpkg and syspkg. Settings have no built-in defaults: if a
|
||||
// setting an operation needs is missing, that operation refuses to run.
|
||||
|
||||
// How many backups of replaced package versions to keep, per package.
|
||||
// When `-lO` replaces an installed version, the old one is compressed
|
||||
// into <scope>/backup/<repo>/<name>/<name>-<version>.gpkg (+ .sha256)
|
||||
// and the oldest backups beyond this count are deleted. 0 = no backups.
|
||||
KEEP_OLD = 1;
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef COMMON_H_
|
||||
#define COMMON_H_
|
||||
|
||||
#ifndef _POSIX_C_SOURCE
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#endif
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#endif
|
||||
#ifndef _XOPEN_SOURCE
|
||||
#define _XOPEN_SOURCE 700
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *path;
|
||||
mode_t mode;
|
||||
}
|
||||
dir_t;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
#ifndef ISTOREUTILS_H_
|
||||
#define ISTOREUTILS_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef enum {
|
||||
INDEX,
|
||||
STORE
|
||||
}
|
||||
index_store_t;
|
||||
|
||||
typedef enum {
|
||||
GL_BACKUP_OK = 0,
|
||||
GL_BACKUP_ERR_ALLOC = 1,
|
||||
GL_BACKUP_ERR_OPEN = 2,
|
||||
GL_BACKUP_ERR_ADD_INDEX = 3,
|
||||
GL_BACKUP_ERR_ADD_STORE = 4,
|
||||
GL_BACKUP_ERR_CLOSE = 5
|
||||
}
|
||||
gl_backup_status_t;
|
||||
|
||||
/*
|
||||
* GL_INSTALL_OK: installation successful
|
||||
* GL_INSTALL_ERR_OPEN: unable to open .gpkg file
|
||||
* GL_INSTALL_ERR_MANIFEST: manifest is missing or unparseable
|
||||
* GL_INSTALL_ERR_EXTRACT: unable to extract files
|
||||
* GL_INSTALL_ERR_INDEX: unable to update index
|
||||
* GL_INSTALL_ERR_ALREADY_INSTALLED: package already installed
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
GL_INSTALL_OK = 0,
|
||||
GL_INSTALL_ERR_OPEN = 1,
|
||||
GL_INSTALL_ERR_MANIFEST = 2,
|
||||
GL_INSTALL_ERR_EXTRACT = 3,
|
||||
GL_INSTALL_ERR_INDEX = 4,
|
||||
GL_INSTALL_ERR_ALREADY_INSTALLED = 5,
|
||||
}
|
||||
gl_install_status_t;
|
||||
|
||||
struct gpkg_entry {
|
||||
char *repo;
|
||||
char *pkg;
|
||||
};
|
||||
|
||||
struct gindex {
|
||||
int uid;
|
||||
int listed;
|
||||
struct gpkg_entry *entries;
|
||||
size_t count;
|
||||
};
|
||||
|
||||
bool gl_usr_istore_exists(index_store_t index_or_store, int uid);
|
||||
bool gl_sys_istore_exists(index_store_t index_or_store);
|
||||
|
||||
int gl_init_user(uid_t uid, bool isVerbose);
|
||||
|
||||
int gl_parse_index(struct gindex *idx, FILE *f);
|
||||
void gl_free_index(struct gindex *idx);
|
||||
|
||||
int gl_rebuild_index(uid_t uid, const char *out_dir);
|
||||
|
||||
gl_backup_status_t gl_backup_istore(const char *tar_path, const char *glacier_root,
|
||||
const char *uid);
|
||||
|
||||
gl_install_status_t gl_install_pkg(const char *gpkg_path, uid_t uid);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
#ifndef PKG_H_
|
||||
#define PKG_H_
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#define _XOPEN_SOURCE 700
|
||||
|
||||
#include <libconfig.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
typedef struct {
|
||||
int maj;
|
||||
int min;
|
||||
int pat;
|
||||
char *tag;
|
||||
}
|
||||
semver_t;
|
||||
|
||||
typedef struct {
|
||||
char *dep_name;
|
||||
semver_t dep_ver;
|
||||
}
|
||||
dep_t;
|
||||
|
||||
typedef struct {
|
||||
char *conf_name;
|
||||
semver_t conf_ver;
|
||||
}
|
||||
conf_t;
|
||||
|
||||
typedef struct {
|
||||
char *pkg_name;
|
||||
semver_t pkg_ver;
|
||||
char *pkg_desc;
|
||||
char *pkg_maint;
|
||||
char *pkg_copyright;
|
||||
dep_t *pkg_dependencies;
|
||||
conf_t *pkg_conflicts;
|
||||
}
|
||||
gl_pkg_t;
|
||||
|
||||
extern const dir_t gl_root;
|
||||
|
||||
extern const dir_t gl_usr;
|
||||
extern const dir_t gl_usr_index;
|
||||
extern const dir_t gl_usr_store;
|
||||
|
||||
extern const dir_t gl_sys;
|
||||
extern const dir_t gl_sys_index;
|
||||
extern const dir_t gl_sys_store;
|
||||
|
||||
extern dep_t null_dep;
|
||||
extern conf_t null_conf;
|
||||
extern gl_pkg_t empty;
|
||||
|
||||
semver_t gl_pkgver(int major, int minor, int patch);
|
||||
semver_t gl_parse_semver(const char *str);
|
||||
void gl_free_semver(semver_t *v);
|
||||
|
||||
gl_pkg_t *gl_spawn_pkg(
|
||||
char *name, semver_t *ver, char *desc,
|
||||
char *maint, char *copy, dep_t *dep,
|
||||
conf_t conf
|
||||
);
|
||||
|
||||
void gl_show_pkg_props(const gl_pkg_t *pkg);
|
||||
void gl_free_pkg(gl_pkg_t *pkg);
|
||||
|
||||
int gl_mkdir(dir_t *dir);
|
||||
char *gl_cat_dir(const dir_t *a, const dir_t *b);
|
||||
char *gl_cat_dir_and_ver(const dir_t *base, const gl_pkg_t *pkg);
|
||||
dir_t gl_return_dir(const dir_t *a, const dir_t *b, mode_t mode);
|
||||
dir_t gl_return_dir_and_ver(const dir_t *base, gl_pkg_t *pkg, mode_t mode);
|
||||
void gl_free_path(char *path);
|
||||
void gl_free_dir(dir_t *dir);
|
||||
|
||||
int gl_gpm2gpkg(const char *filename, gl_pkg_t *pkg);
|
||||
int gl_prepare_pkgdir(gl_pkg_t *pkg, bool is_syspkg);
|
||||
char *join_with_cwd(const char *path);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,160 @@
|
||||
# build-driver.sh: runs INSIDE gsandbox as /.glacier/in/driver
|
||||
# (BUILD_DESIGN.txt section 6). POSIX sh: the build root may only have
|
||||
# busybox. Everything it reads is under /.glacier/in, written by
|
||||
# gpkg/syspkg:
|
||||
#
|
||||
# env NAME, VER, BUILD, PREFIX, SOURCES, PATCHES,
|
||||
# CONFIGURE_ARGS, TRAITS, JOBS, CFLAGS, CXXFLAGS,
|
||||
# LDFLAGS (shell assignments, every value quoted)
|
||||
# bundle.grcp the recipe bundle (already checked by the library)
|
||||
# sources/FILE each source, hash already checked
|
||||
# manifest.gpm.cfg the package's manifest
|
||||
#
|
||||
# It writes /.glacier/out/NAME-VER.gpkg and nothing else counts. The
|
||||
# package is checked again outside, so nothing here is trusted.
|
||||
#
|
||||
# PREFIX is where the package's usr/ will be linked (/usr for base,
|
||||
# else /glacier/sys/links or the user's links), so the build compiles
|
||||
# in the paths its files really have. It installs into DESTDIR$PREFIX,
|
||||
# which becomes the package's usr/. During the build PREFIX is a link
|
||||
# to /usr, where the layers are, so paths taken from them (.pc files,
|
||||
# libtool archives) resolve.
|
||||
|
||||
set -eu
|
||||
. /.glacier/in/env
|
||||
|
||||
say() { printf '==> %s\n' "$*"; }
|
||||
die() { printf '==> ERROR: %s\n' "$*"; exit 1; }
|
||||
|
||||
# trait NAME: whether trait NAME is on (for build hooks).
|
||||
trait() {
|
||||
for t in $TRAITS; do
|
||||
[ "$t" = "$1" ] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
B=/build
|
||||
R=$B/recipe/$NAME-$VER
|
||||
DESTDIR=$B/dest
|
||||
mkdir -p $B/recipe $B/src $DESTDIR $B/pkg
|
||||
export DESTDIR PREFIX CFLAGS CXXFLAGS LDFLAGS
|
||||
export MAKEFLAGS="-j$JOBS"
|
||||
|
||||
case $PREFIX in
|
||||
/usr) ;;
|
||||
/?*) mkdir -p "${PREFIX%/*}" && ln -s /usr "$PREFIX" ;;
|
||||
*) die "Bad PREFIX $PREFIX" ;;
|
||||
esac
|
||||
|
||||
say "Unpacking $NAME $VER"
|
||||
tar -xzf /.glacier/in/bundle.grcp -C $B/recipe
|
||||
cd $B/src
|
||||
for f in $SOURCES; do
|
||||
case $f in
|
||||
*.tar|*.tar.gz|*.tgz|*.tar.xz|*.txz|*.tar.bz2|*.tbz2|*.tar.zst|*.tzst)
|
||||
tar -xf "/.glacier/in/sources/$f" ;;
|
||||
*)
|
||||
cp "/.glacier/in/sources/$f" . ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# The source tree: the one directory the sources unpacked into, else
|
||||
# the unpack directory itself.
|
||||
S=$B/src
|
||||
n=0
|
||||
for d in $B/src/* $B/src/.[!.]*; do
|
||||
[ -d "$d" ] && { n=$((n + 1)); one=$d; }
|
||||
done
|
||||
[ "$n" = 1 ] && S=$one
|
||||
cd "$S"
|
||||
|
||||
for p in $PATCHES; do
|
||||
say "Applying $p"
|
||||
patch -p1 < "$R/patches/$p"
|
||||
done
|
||||
|
||||
# configure_args CMD...: run CMD with CONFIGURE_ARGS appended. Those
|
||||
# are quoted words: the recipe's CONFIGURE, the trait arguments, then
|
||||
# the per-package CONFIGURE.
|
||||
configure_args() { eval "set -- \"\$@\" $CONFIGURE_ARGS"; "$@"; }
|
||||
|
||||
if [ -z "$BUILD" ]; then
|
||||
if [ -f CMakeLists.txt ]; then BUILD=cmake
|
||||
elif [ -f meson.build ]; then BUILD=meson
|
||||
# configure BEFORE a bare Makefile (CLAUDE.md bug 8)
|
||||
elif [ -f configure ] || [ -f configure.ac ]; then BUILD=autotools
|
||||
elif [ -f Makefile ] || [ -f GNUmakefile ] || [ -f makefile ]; then BUILD=make
|
||||
else die "Cannot tell how to build $NAME: set BUILD in the recipe"
|
||||
fi
|
||||
say "Build system: $BUILD"
|
||||
fi
|
||||
|
||||
case $BUILD in
|
||||
autotools)
|
||||
prepare() { [ -x configure ] || autoreconf -fi; }
|
||||
configure() { configure_args ./configure --prefix="$PREFIX" --sysconfdir=/etc --localstatedir=/var; }
|
||||
compile() { make; }
|
||||
package() { make install DESTDIR="$DESTDIR"; }
|
||||
;;
|
||||
cmake)
|
||||
prepare() { :; }
|
||||
configure() { configure_args cmake -B _build -DCMAKE_INSTALL_PREFIX="$PREFIX" -DCMAKE_BUILD_TYPE=Release; }
|
||||
compile() { cmake --build _build -j "$JOBS"; }
|
||||
package() { DESTDIR="$DESTDIR" cmake --install _build; }
|
||||
;;
|
||||
meson)
|
||||
prepare() { :; }
|
||||
configure() { configure_args meson setup _build --prefix="$PREFIX" --buildtype=release; }
|
||||
compile() { meson compile -C _build -j "$JOBS"; }
|
||||
package() { DESTDIR="$DESTDIR" meson install -C _build; }
|
||||
;;
|
||||
make)
|
||||
prepare() { :; }
|
||||
configure() { :; }
|
||||
compile() { configure_args make PREFIX="$PREFIX"; }
|
||||
package() { make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"; }
|
||||
;;
|
||||
hooks)
|
||||
prepare() { :; }
|
||||
configure() { :; }
|
||||
compile() { :; }
|
||||
package() { :; }
|
||||
;;
|
||||
*)
|
||||
die "Unknown build system $BUILD"
|
||||
;;
|
||||
esac
|
||||
|
||||
# The recipe's own hooks replace any of the four.
|
||||
[ -f "$R/build" ] && . "$R/build"
|
||||
|
||||
say "Preparing"; prepare
|
||||
say "Configuring"; configure
|
||||
say "Compiling"; compile
|
||||
say "Installing into DESTDIR"; package
|
||||
|
||||
[ -n "$(ls -A "$DESTDIR")" ] || die "$NAME installed nothing"
|
||||
|
||||
# DESTDIR$PREFIX is the package's usr/; the rest (etc/, var/) stays.
|
||||
if [ "$PREFIX" != /usr ] && [ -d "$DESTDIR$PREFIX" ]; then
|
||||
[ -e "$DESTDIR/usr" ] && die "$NAME installed into both $PREFIX and /usr"
|
||||
mv "$DESTDIR$PREFIX" "$B/usr"
|
||||
d=$DESTDIR$PREFIX
|
||||
while d=${d%/*}; [ "$d" != "$DESTDIR" ]; do
|
||||
rmdir "$d" 2>/dev/null || die "$NAME installed files beside $PREFIX: ${d#$DESTDIR}"
|
||||
done
|
||||
mv "$B/usr" "$DESTDIR/usr"
|
||||
fi
|
||||
|
||||
say "Packing $NAME-$VER.gpkg"
|
||||
mkdir -p "$B/pkg/$NAME-$VER"
|
||||
mv "$DESTDIR" "$B/pkg/$NAME-$VER/files"
|
||||
cp /.glacier/in/manifest.gpm.cfg "$B/pkg/$NAME-$VER/manifest.gpm.cfg"
|
||||
# Hardlinks become plain files: glacier refuses a package with a
|
||||
# hardlink entry.
|
||||
find "$B/pkg/$NAME-$VER" -type f -links +1 | while IFS= read -r f; do
|
||||
cp -p "$f" "$f.unlink" && mv -f "$f.unlink" "$f" || die "Cannot copy hardlink $f"
|
||||
done
|
||||
tar -czf "/.glacier/out/$NAME-$VER.gpkg" -C "$B/pkg" "$NAME-$VER"
|
||||
say "Done"
|
||||
@@ -0,0 +1,131 @@
|
||||
# build-driver.sh: runs INSIDE gsandbox as /.glacier/in/driver
|
||||
# (BUILD_DESIGN.txt section 6). POSIX sh: the build root may only have
|
||||
# busybox. Everything it reads is under /.glacier/in, written by
|
||||
# gpkg/syspkg:
|
||||
#
|
||||
# env NAME, VER, BUILD, SOURCES, PATCHES, CONFIGURE_ARGS,
|
||||
# TRAITS, JOBS, CFLAGS, CXXFLAGS, LDFLAGS (shell
|
||||
# assignments, every value quoted)
|
||||
# bundle.grcp the recipe bundle (already checked by the library)
|
||||
# sources/FILE each source, hash already checked
|
||||
# manifest.gpm.cfg the package's manifest
|
||||
#
|
||||
# It writes /.glacier/out/NAME-VER.gpkg and nothing else counts. The
|
||||
# package is checked again outside, so nothing here is trusted.
|
||||
|
||||
set -eu
|
||||
. /.glacier/in/env
|
||||
|
||||
say() { printf '==> %s\n' "$*"; }
|
||||
die() { printf '==> ERROR: %s\n' "$*"; exit 1; }
|
||||
|
||||
# trait NAME: whether trait NAME is on (for build hooks).
|
||||
trait() {
|
||||
for t in $TRAITS; do
|
||||
[ "$t" = "$1" ] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
B=/build
|
||||
R=$B/recipe/$NAME-$VER
|
||||
DESTDIR=$B/dest
|
||||
mkdir -p $B/recipe $B/src $DESTDIR $B/pkg
|
||||
export DESTDIR CFLAGS CXXFLAGS LDFLAGS
|
||||
export MAKEFLAGS="-j$JOBS"
|
||||
|
||||
say "Unpacking $NAME $VER"
|
||||
tar -xzf /.glacier/in/bundle.grcp -C $B/recipe
|
||||
cd $B/src
|
||||
for f in $SOURCES; do
|
||||
case $f in
|
||||
*.tar|*.tar.gz|*.tgz|*.tar.xz|*.txz|*.tar.bz2|*.tbz2|*.tar.zst|*.tzst)
|
||||
tar -xf "/.glacier/in/sources/$f" ;;
|
||||
*)
|
||||
cp "/.glacier/in/sources/$f" . ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# The source tree: the one directory the sources unpacked into, else
|
||||
# the unpack directory itself.
|
||||
S=$B/src
|
||||
n=0
|
||||
for d in $B/src/* $B/src/.[!.]*; do
|
||||
[ -d "$d" ] && { n=$((n + 1)); one=$d; }
|
||||
done
|
||||
[ "$n" = 1 ] && S=$one
|
||||
cd "$S"
|
||||
|
||||
for p in $PATCHES; do
|
||||
say "Applying $p"
|
||||
patch -p1 < "$R/patches/$p"
|
||||
done
|
||||
|
||||
# configure_args CMD...: run CMD with CONFIGURE_ARGS appended. Those
|
||||
# are quoted words: the recipe's CONFIGURE, the trait arguments, then
|
||||
# the per-package CONFIGURE.
|
||||
configure_args() { eval "set -- \"\$@\" $CONFIGURE_ARGS"; "$@"; }
|
||||
|
||||
if [ -z "$BUILD" ]; then
|
||||
if [ -f CMakeLists.txt ]; then BUILD=cmake
|
||||
elif [ -f meson.build ]; then BUILD=meson
|
||||
# configure BEFORE a bare Makefile (CLAUDE.md bug 8)
|
||||
elif [ -f configure ] || [ -f configure.ac ]; then BUILD=autotools
|
||||
elif [ -f Makefile ] || [ -f GNUmakefile ] || [ -f makefile ]; then BUILD=make
|
||||
else die "Cannot tell how to build $NAME: set BUILD in the recipe"
|
||||
fi
|
||||
say "Build system: $BUILD"
|
||||
fi
|
||||
|
||||
case $BUILD in
|
||||
autotools)
|
||||
prepare() { [ -x configure ] || autoreconf -fi; }
|
||||
configure() { configure_args ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var; }
|
||||
compile() { make; }
|
||||
package() { make install DESTDIR="$DESTDIR"; }
|
||||
;;
|
||||
cmake)
|
||||
prepare() { :; }
|
||||
configure() { configure_args cmake -B _build -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release; }
|
||||
compile() { cmake --build _build -j "$JOBS"; }
|
||||
package() { DESTDIR="$DESTDIR" cmake --install _build; }
|
||||
;;
|
||||
meson)
|
||||
prepare() { :; }
|
||||
configure() { configure_args meson setup _build --prefix=/usr --buildtype=release; }
|
||||
compile() { meson compile -C _build -j "$JOBS"; }
|
||||
package() { DESTDIR="$DESTDIR" meson install -C _build; }
|
||||
;;
|
||||
make)
|
||||
prepare() { :; }
|
||||
configure() { :; }
|
||||
compile() { configure_args make PREFIX=/usr; }
|
||||
package() { make install PREFIX=/usr DESTDIR="$DESTDIR"; }
|
||||
;;
|
||||
hooks)
|
||||
prepare() { :; }
|
||||
configure() { :; }
|
||||
compile() { :; }
|
||||
package() { :; }
|
||||
;;
|
||||
*)
|
||||
die "Unknown build system $BUILD"
|
||||
;;
|
||||
esac
|
||||
|
||||
# The recipe's own hooks replace any of the four.
|
||||
[ -f "$R/build" ] && . "$R/build"
|
||||
|
||||
say "Preparing"; prepare
|
||||
say "Configuring"; configure
|
||||
say "Compiling"; compile
|
||||
say "Installing into DESTDIR"; package
|
||||
|
||||
[ -n "$(ls -A "$DESTDIR")" ] || die "$NAME installed nothing"
|
||||
|
||||
say "Packing $NAME-$VER.gpkg"
|
||||
mkdir -p "$B/pkg/$NAME-$VER"
|
||||
mv "$DESTDIR" "$B/pkg/$NAME-$VER/files"
|
||||
cp /.glacier/in/manifest.gpm.cfg "$B/pkg/$NAME-$VER/manifest.gpm.cfg"
|
||||
tar -czf "/.glacier/out/$NAME-$VER.gpkg" -C "$B/pkg" "$NAME-$VER"
|
||||
say "Done"
|
||||
@@ -0,0 +1,155 @@
|
||||
# build-driver.sh: runs INSIDE gsandbox as /.glacier/in/driver
|
||||
# (BUILD_DESIGN.txt section 6). POSIX sh: the build root may only have
|
||||
# busybox. Everything it reads is under /.glacier/in, written by
|
||||
# gpkg/syspkg:
|
||||
#
|
||||
# env NAME, VER, BUILD, PREFIX, SOURCES, PATCHES,
|
||||
# CONFIGURE_ARGS, TRAITS, JOBS, CFLAGS, CXXFLAGS,
|
||||
# LDFLAGS (shell assignments, every value quoted)
|
||||
# bundle.grcp the recipe bundle (already checked by the library)
|
||||
# sources/FILE each source, hash already checked
|
||||
# manifest.gpm.cfg the package's manifest
|
||||
#
|
||||
# It writes /.glacier/out/NAME-VER.gpkg and nothing else counts. The
|
||||
# package is checked again outside, so nothing here is trusted.
|
||||
#
|
||||
# PREFIX is where the package's usr/ will be linked (/usr for base,
|
||||
# else /glacier/sys/links or the user's links), so the build compiles
|
||||
# in the paths its files really have. It installs into DESTDIR$PREFIX,
|
||||
# which becomes the package's usr/. During the build PREFIX is a link
|
||||
# to /usr, where the layers are, so paths taken from them (.pc files,
|
||||
# libtool archives) resolve.
|
||||
|
||||
set -eu
|
||||
. /.glacier/in/env
|
||||
|
||||
say() { printf '==> %s\n' "$*"; }
|
||||
die() { printf '==> ERROR: %s\n' "$*"; exit 1; }
|
||||
|
||||
# trait NAME: whether trait NAME is on (for build hooks).
|
||||
trait() {
|
||||
for t in $TRAITS; do
|
||||
[ "$t" = "$1" ] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
B=/build
|
||||
R=$B/recipe/$NAME-$VER
|
||||
DESTDIR=$B/dest
|
||||
mkdir -p $B/recipe $B/src $DESTDIR $B/pkg
|
||||
export DESTDIR PREFIX CFLAGS CXXFLAGS LDFLAGS
|
||||
export MAKEFLAGS="-j$JOBS"
|
||||
|
||||
case $PREFIX in
|
||||
/usr) ;;
|
||||
/?*) mkdir -p "${PREFIX%/*}" && ln -s /usr "$PREFIX" ;;
|
||||
*) die "Bad PREFIX $PREFIX" ;;
|
||||
esac
|
||||
|
||||
say "Unpacking $NAME $VER"
|
||||
tar -xzf /.glacier/in/bundle.grcp -C $B/recipe
|
||||
cd $B/src
|
||||
for f in $SOURCES; do
|
||||
case $f in
|
||||
*.tar|*.tar.gz|*.tgz|*.tar.xz|*.txz|*.tar.bz2|*.tbz2|*.tar.zst|*.tzst)
|
||||
tar -xf "/.glacier/in/sources/$f" ;;
|
||||
*)
|
||||
cp "/.glacier/in/sources/$f" . ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# The source tree: the one directory the sources unpacked into, else
|
||||
# the unpack directory itself.
|
||||
S=$B/src
|
||||
n=0
|
||||
for d in $B/src/* $B/src/.[!.]*; do
|
||||
[ -d "$d" ] && { n=$((n + 1)); one=$d; }
|
||||
done
|
||||
[ "$n" = 1 ] && S=$one
|
||||
cd "$S"
|
||||
|
||||
for p in $PATCHES; do
|
||||
say "Applying $p"
|
||||
patch -p1 < "$R/patches/$p"
|
||||
done
|
||||
|
||||
# configure_args CMD...: run CMD with CONFIGURE_ARGS appended. Those
|
||||
# are quoted words: the recipe's CONFIGURE, the trait arguments, then
|
||||
# the per-package CONFIGURE.
|
||||
configure_args() { eval "set -- \"\$@\" $CONFIGURE_ARGS"; "$@"; }
|
||||
|
||||
if [ -z "$BUILD" ]; then
|
||||
if [ -f CMakeLists.txt ]; then BUILD=cmake
|
||||
elif [ -f meson.build ]; then BUILD=meson
|
||||
# configure BEFORE a bare Makefile (CLAUDE.md bug 8)
|
||||
elif [ -f configure ] || [ -f configure.ac ]; then BUILD=autotools
|
||||
elif [ -f Makefile ] || [ -f GNUmakefile ] || [ -f makefile ]; then BUILD=make
|
||||
else die "Cannot tell how to build $NAME: set BUILD in the recipe"
|
||||
fi
|
||||
say "Build system: $BUILD"
|
||||
fi
|
||||
|
||||
case $BUILD in
|
||||
autotools)
|
||||
prepare() { [ -x configure ] || autoreconf -fi; }
|
||||
configure() { configure_args ./configure --prefix="$PREFIX" --sysconfdir=/etc --localstatedir=/var; }
|
||||
compile() { make; }
|
||||
package() { make install DESTDIR="$DESTDIR"; }
|
||||
;;
|
||||
cmake)
|
||||
prepare() { :; }
|
||||
configure() { configure_args cmake -B _build -DCMAKE_INSTALL_PREFIX="$PREFIX" -DCMAKE_BUILD_TYPE=Release; }
|
||||
compile() { cmake --build _build -j "$JOBS"; }
|
||||
package() { DESTDIR="$DESTDIR" cmake --install _build; }
|
||||
;;
|
||||
meson)
|
||||
prepare() { :; }
|
||||
configure() { configure_args meson setup _build --prefix="$PREFIX" --buildtype=release; }
|
||||
compile() { meson compile -C _build -j "$JOBS"; }
|
||||
package() { DESTDIR="$DESTDIR" meson install -C _build; }
|
||||
;;
|
||||
make)
|
||||
prepare() { :; }
|
||||
configure() { :; }
|
||||
compile() { configure_args make PREFIX="$PREFIX"; }
|
||||
package() { make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"; }
|
||||
;;
|
||||
hooks)
|
||||
prepare() { :; }
|
||||
configure() { :; }
|
||||
compile() { :; }
|
||||
package() { :; }
|
||||
;;
|
||||
*)
|
||||
die "Unknown build system $BUILD"
|
||||
;;
|
||||
esac
|
||||
|
||||
# The recipe's own hooks replace any of the four.
|
||||
[ -f "$R/build" ] && . "$R/build"
|
||||
|
||||
say "Preparing"; prepare
|
||||
say "Configuring"; configure
|
||||
say "Compiling"; compile
|
||||
say "Installing into DESTDIR"; package
|
||||
|
||||
[ -n "$(ls -A "$DESTDIR")" ] || die "$NAME installed nothing"
|
||||
|
||||
# DESTDIR$PREFIX is the package's usr/; the rest (etc/, var/) stays.
|
||||
if [ "$PREFIX" != /usr ] && [ -d "$DESTDIR$PREFIX" ]; then
|
||||
[ -e "$DESTDIR/usr" ] && die "$NAME installed into both $PREFIX and /usr"
|
||||
mv "$DESTDIR$PREFIX" "$B/usr"
|
||||
d=$DESTDIR$PREFIX
|
||||
while d=${d%/*}; [ "$d" != "$DESTDIR" ]; do
|
||||
rmdir "$d" 2>/dev/null || die "$NAME installed files beside $PREFIX: ${d#$DESTDIR}"
|
||||
done
|
||||
mv "$B/usr" "$DESTDIR/usr"
|
||||
fi
|
||||
|
||||
say "Packing $NAME-$VER.gpkg"
|
||||
mkdir -p "$B/pkg/$NAME-$VER"
|
||||
mv "$DESTDIR" "$B/pkg/$NAME-$VER/files"
|
||||
cp /.glacier/in/manifest.gpm.cfg "$B/pkg/$NAME-$VER/manifest.gpm.cfg"
|
||||
tar -czf "/.glacier/out/$NAME-$VER.gpkg" -C "$B/pkg" "$NAME-$VER"
|
||||
say "Done"
|
||||
@@ -0,0 +1,17 @@
|
||||
local ffi = require("ffi")
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.get_argv()
|
||||
local argc = #arg
|
||||
local argv = ffi.new("const char *[?]", argc + 1)
|
||||
|
||||
argv[0] = arg[0] or ""
|
||||
for i = 1, argc do
|
||||
argv[i] = arg[i]
|
||||
end
|
||||
|
||||
return argc + 1, argv
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,24 @@
|
||||
-- color.lua
|
||||
local M = {}
|
||||
|
||||
-- Standard colors (bold)
|
||||
M.RED = "\x1b[31m\x1b[1m"
|
||||
M.GREEN = "\x1b[32m\x1b[1m"
|
||||
M.YELLOW = "\x1b[33m\x1b[1m"
|
||||
M.BLUE = "\x1b[34m\x1b[1m"
|
||||
M.MAGENTA = "\x1b[35m\x1b[1m"
|
||||
M.CYAN = "\x1b[36m\x1b[1m"
|
||||
|
||||
-- Standard colors (not bold)
|
||||
M.RED_NB = "\x1b[31m"
|
||||
M.GREEN_NB = "\x1b[32m"
|
||||
M.YELLOW_NB = "\x1b[33m"
|
||||
M.BLUE_NB = "\x1b[34m"
|
||||
M.MAGENTA_NB = "\x1b[35m"
|
||||
M.CYAN_NB = "\x1b[36m"
|
||||
|
||||
-- Reset
|
||||
M.RESET = "\x1b[0m\x1b[m"
|
||||
|
||||
return M
|
||||
|
||||
@@ -0,0 +1,424 @@
|
||||
-- lib/glacier_cdef.lua
|
||||
--
|
||||
-- Single source of truth for every ffi.cdef() declaration used across
|
||||
-- gpkg, gstore, and the Lua test suite. These used to be hand-copied
|
||||
-- into each entry point separately, which is exactly what let a stale
|
||||
-- gl_install_pkg/gl_remove_pkg signature drift silently into gpkg
|
||||
-- during the transaction-context refactor: nothing caught it, because
|
||||
-- Lua FFI has no compiler to reject a mismatched declaration the way C
|
||||
-- would.
|
||||
--
|
||||
-- require() caches modules, so the ffi.cdef() call below runs exactly
|
||||
-- once per process no matter how many files require() this — calling
|
||||
-- ffi.cdef() twice with conflicting content would error, but that's
|
||||
-- not a concern here since there's only one copy now.
|
||||
--
|
||||
-- This module declares the C ABI only. It does not call ffi.load() for
|
||||
-- you automatically — different entry points may want the library from
|
||||
-- different explicit paths — but M.load() below gives every caller a
|
||||
-- consistent, CWD-independent way to find it if they don't need
|
||||
-- anything more specific.
|
||||
|
||||
local ffi = require("ffi")
|
||||
|
||||
ffi.cdef[[
|
||||
typedef unsigned int uid_t;
|
||||
typedef int pid_t;
|
||||
|
||||
unsigned int getuid(void);
|
||||
unsigned int getpid(void);
|
||||
uid_t geteuid(void);
|
||||
void free(void *ptr);
|
||||
char *getcwd(char *buf, size_t size);
|
||||
char *strerror(int errnum);
|
||||
|
||||
void lg_printf(int loglevel, const char *fmt, ...);
|
||||
void lg_gpkg_help(int argc, const char *argv[]);
|
||||
void lg_gstore_help(int argc, const char *argv[]);
|
||||
void lg_syspkg_help(int argc, const char *argv[]);
|
||||
|
||||
typedef enum {
|
||||
INDEX,
|
||||
STORE
|
||||
} index_store_t;
|
||||
_Bool gl_usr_istore_exists(index_store_t index_or_store, int uid);
|
||||
int gl_usr_base_status(uid_t uid);
|
||||
int gl_prune_backups(const char *dir, int keep);
|
||||
|
||||
/* --- pkg.h: glacier.conf --- */
|
||||
typedef enum {
|
||||
GL_CONF_OK = 0,
|
||||
GL_CONF_ERR_READ = 1,
|
||||
GL_CONF_ERR_MISSING = 2,
|
||||
GL_CONF_ERR_TYPE = 3,
|
||||
GL_CONF_ERR_INVALID = 4,
|
||||
GL_CONF_ERR_SPACE = 5
|
||||
} gl_conf_status_t;
|
||||
gl_conf_status_t gl_conf_get_int(const char *path, const char *key, int *out);
|
||||
gl_conf_status_t gl_conf_repo_keys(const char *path, const char *repo,
|
||||
char *out, size_t outlen);
|
||||
gl_conf_status_t gl_conf_repos(const char *path, char *out, size_t outlen);
|
||||
typedef struct {
|
||||
char *traits;
|
||||
char *cflags;
|
||||
char *cxxflags;
|
||||
char *ldflags;
|
||||
char *configure;
|
||||
int jobs;
|
||||
char *build_user;
|
||||
char *build_root;
|
||||
} gl_build_conf_t;
|
||||
typedef enum {
|
||||
GL_CONF_SYSTEM = 0,
|
||||
GL_CONF_USER = 1,
|
||||
GL_CONF_PACKAGE = 2
|
||||
} gl_conf_kind_t;
|
||||
gl_conf_status_t gl_conf_build(const char *path, gl_conf_kind_t kind,
|
||||
gl_build_conf_t *out);
|
||||
void gl_build_conf_free(gl_build_conf_t *c);
|
||||
|
||||
/* --- pkg.h: repository indexes --- */
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *ver;
|
||||
char *file;
|
||||
char *sha256;
|
||||
int64_t size;
|
||||
char *deps;
|
||||
char *confs;
|
||||
char *build_deps;
|
||||
char *traits;
|
||||
} gl_index_pkg_t;
|
||||
typedef struct {
|
||||
char *repo;
|
||||
int64_t serial;
|
||||
int64_t valid_until;
|
||||
size_t npkgs;
|
||||
gl_index_pkg_t *pkgs;
|
||||
} gl_index_t;
|
||||
typedef enum {
|
||||
GL_INDEX_OK = 0,
|
||||
GL_INDEX_ERR_FORMAT = 1,
|
||||
GL_INDEX_ERR_REPO = 2,
|
||||
GL_INDEX_ERR_NOMEM = 3
|
||||
} gl_index_status_t;
|
||||
gl_index_status_t gl_index_parse(const char *text, size_t len,
|
||||
const char *repo, gl_index_t *out);
|
||||
void gl_index_free(gl_index_t *idx);
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *ver;
|
||||
char *repo;
|
||||
char *desc;
|
||||
char *maint;
|
||||
char *copyright;
|
||||
char *sources;
|
||||
char *patches;
|
||||
char *build;
|
||||
char *configure;
|
||||
char *traits;
|
||||
char *trait_args;
|
||||
char *deps;
|
||||
char *build_deps;
|
||||
char *confs;
|
||||
} gl_recipe_t;
|
||||
typedef enum {
|
||||
GL_RECIPE_OK = 0,
|
||||
GL_RECIPE_ERR_READ = 1,
|
||||
GL_RECIPE_ERR_BUNDLE = 2,
|
||||
GL_RECIPE_ERR_FORMAT = 3,
|
||||
GL_RECIPE_ERR_NOMEM = 4
|
||||
} gl_recipe_status_t;
|
||||
gl_recipe_status_t gl_recipe_parse(const char *text, size_t len, gl_recipe_t *out);
|
||||
gl_recipe_status_t gl_recipe_read(const char *bundle, gl_recipe_t *out);
|
||||
const char *gl_recipe_error(void);
|
||||
void gl_recipe_free(gl_recipe_t *r);
|
||||
bool gl_valid_trait(const char *s);
|
||||
|
||||
/* --- transaction.h --- */
|
||||
|
||||
typedef enum {
|
||||
GL_CTX_LIVE,
|
||||
GL_CTX_STAGE
|
||||
} gl_ctx_mode_t;
|
||||
|
||||
typedef enum {
|
||||
GL_SCOPE_USR,
|
||||
GL_SCOPE_SYS
|
||||
} gl_ctx_scope_t;
|
||||
|
||||
typedef struct {
|
||||
uid_t uid;
|
||||
|
||||
char root_path[4096];
|
||||
char index_path[4096];
|
||||
char store_path[4096];
|
||||
char links_path[4096];
|
||||
|
||||
gl_ctx_mode_t mode;
|
||||
gl_ctx_scope_t scope;
|
||||
|
||||
int lock_fd;
|
||||
|
||||
int overwrite;
|
||||
|
||||
char sysroot[4096];
|
||||
|
||||
int link_refusals;
|
||||
|
||||
int strict;
|
||||
} gl_context_t;
|
||||
|
||||
typedef enum {
|
||||
GL_TXN_OK = 0,
|
||||
GL_TXN_ERR_LOCK = 1,
|
||||
GL_TXN_ERR_MKDIR = 2,
|
||||
GL_TXN_ERR_PERM = 3,
|
||||
GL_TXN_ERR_RENAME = 4,
|
||||
GL_TXN_ERR_SYNC = 5,
|
||||
GL_TXN_ERR_CLEANUP = 6,
|
||||
GL_TXN_ERR_NO_TXN = 7,
|
||||
GL_TXN_ERR_XDEV = 8,
|
||||
GL_TXN_ERR_DEPS = 9
|
||||
} gl_txn_status_t;
|
||||
|
||||
int gl_check_sysroot(const char *sysroot);
|
||||
int gl_init_live_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
|
||||
int gl_init_live_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
|
||||
const char *sysroot);
|
||||
gl_txn_status_t gl_init_stage_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
|
||||
gl_txn_status_t gl_init_stage_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
|
||||
const char *sysroot);
|
||||
gl_txn_status_t gl_begin_transaction(gl_context_t *ctx);
|
||||
int gl_scope_state_dir(const gl_context_t *ctx, const char *name, char *out);
|
||||
int gl_scope_cache_dir(const gl_context_t *ctx, const char *repo, char *out);
|
||||
int gl_scope_build_dir(const gl_context_t *ctx, const char *name, int owner, char *out);
|
||||
gl_txn_status_t gl_commit_transaction(gl_context_t *ctx);
|
||||
gl_txn_status_t gl_abort_transaction(gl_context_t *ctx);
|
||||
|
||||
/* --- istoreutils.h --- */
|
||||
|
||||
typedef enum {
|
||||
GL_INSTALL_OK = 0,
|
||||
GL_INSTALL_ERR_OPEN = 1,
|
||||
GL_INSTALL_ERR_MANIFEST = 2,
|
||||
GL_INSTALL_ERR_EXTRACT = 3,
|
||||
GL_INSTALL_ERR_INDEX = 4,
|
||||
GL_INSTALL_ERR_ALREADY_INSTALLED = 5,
|
||||
GL_INSTALL_SKIPPED = 6,
|
||||
GL_INSTALL_ERR_SCOPE = 7,
|
||||
GL_INSTALL_ERR_DEPS = 8
|
||||
} gl_install_status_t;
|
||||
|
||||
typedef enum {
|
||||
GL_REMOVE_OK = 0,
|
||||
GL_REMOVE_ERR_NOT_FOUND = 1,
|
||||
GL_REMOVE_ERR_UNLINK = 2,
|
||||
GL_REMOVE_ERR_INDEX = 3,
|
||||
GL_REMOVE_ERR_DEPS = 4
|
||||
} gl_remove_status_t;
|
||||
|
||||
typedef struct {
|
||||
char **repos;
|
||||
size_t count;
|
||||
} gl_repo_list_t;
|
||||
|
||||
int gl_init_user(uid_t uid, bool isVerbose);
|
||||
int gl_init_sys(bool isVerbose);
|
||||
int gl_init_sys_at(const char *sysroot, bool isVerbose);
|
||||
int gl_create_fs_tree(const char *root, bool isVerbose);
|
||||
int gl_delete_user(uid_t uid, bool isVerbose);
|
||||
|
||||
gl_repo_list_t gl_find_pkg_repos(gl_context_t *ctx, const char *pkg_name);
|
||||
void gl_free_repo_list(gl_repo_list_t *list);
|
||||
|
||||
gl_install_status_t gl_install_pkg(gl_context_t *ctx, const char *gpkg_path);
|
||||
gl_install_status_t gl_extract_files(const char *gpkg_path, const char *dest);
|
||||
gl_remove_status_t gl_remove_pkg(gl_context_t *ctx, const char *pkg_name, const char *pkg_repo);
|
||||
int gl_backup_pkg(gl_context_t *ctx, const char *repo, const char *name, const char *out_path);
|
||||
int gl_relink_store(gl_context_t *ctx);
|
||||
int gl_set_pkg_auto(gl_context_t *ctx, const char *repo, const char *name, bool automatic);
|
||||
int gl_set_pkg_src(gl_context_t *ctx, const char *repo, const char *name, const char *src_repo);
|
||||
int gl_pkg_src(gl_context_t *ctx, const char *repo, const char *name, char *out, size_t len);
|
||||
|
||||
/* --- deps.h (built into libglacier_istoreutils.so) --- */
|
||||
int gl_check_plan(const gl_context_t *live,
|
||||
const char *const *add_paths, size_t n_add,
|
||||
const char *const *remove_ids, size_t n_remove);
|
||||
int gl_check_txn(const gl_context_t *stage);
|
||||
|
||||
int gl_backup_istore(const char *tar_path, const char *glacier_root, const char *uid);
|
||||
|
||||
/* --- pkg.h: parsed manifests (arrays end at a NULL name) --- */
|
||||
typedef struct {
|
||||
int maj;
|
||||
int min;
|
||||
int pat;
|
||||
char *tag;
|
||||
} semver_t;
|
||||
typedef struct {
|
||||
char *dep_name;
|
||||
semver_t dep_ver;
|
||||
} dep_t;
|
||||
typedef struct {
|
||||
char *conf_name;
|
||||
semver_t conf_ver;
|
||||
} conf_t;
|
||||
typedef struct {
|
||||
char *pkg_name;
|
||||
semver_t pkg_ver;
|
||||
char *pkg_desc;
|
||||
char *pkg_maint;
|
||||
char *pkg_copyright;
|
||||
char *pkg_repo;
|
||||
dep_t *pkg_dependencies;
|
||||
conf_t *pkg_conflicts;
|
||||
char *pkg_traits;
|
||||
} gl_pkg_t;
|
||||
/* libglacier_istoreutils.so; *pkg must be zeroed, free with gl_free_pkg */
|
||||
int gl_read_pkg_manifest(const char *gpkg_path, gl_pkg_t *pkg);
|
||||
int gl_gpm2gpkg(const char *filename, gl_pkg_t *pkg);
|
||||
void gl_free_pkg(gl_pkg_t *pkg);
|
||||
|
||||
/* --- libc: process control, used by the crash/contention tests --- */
|
||||
pid_t fork(void);
|
||||
pid_t waitpid(pid_t pid, int *status, int options);
|
||||
int kill(pid_t pid, int sig);
|
||||
void _exit(int status);
|
||||
unsigned int sleep(unsigned int seconds);
|
||||
|
||||
/* --- libc: mount, used by the EXDEV test --- */
|
||||
int mount(const char *source, const char *target, const char *filesystemtype,
|
||||
unsigned long mountflags, const void *data);
|
||||
int umount2(const char *target, int flags);
|
||||
|
||||
/* --- sha256.h (libglacier_verify.so, private) --- */
|
||||
typedef struct {
|
||||
uint32_t state[8];
|
||||
uint64_t bitlen;
|
||||
uint8_t data[64];
|
||||
size_t datalen;
|
||||
} gl_sha256_ctx_t;
|
||||
|
||||
void gl_sha256_init(gl_sha256_ctx_t *ctx);
|
||||
void gl_sha256_update(gl_sha256_ctx_t *ctx, const uint8_t *data, size_t len);
|
||||
void gl_sha256_final(gl_sha256_ctx_t *ctx, uint8_t digest[32]);
|
||||
void gl_sha256_to_hex(const uint8_t digest[32], char *out);
|
||||
|
||||
/* --- sign.h (libglacier_verify.so, private) --- */
|
||||
typedef enum {
|
||||
GL_SIGN_OK = 0,
|
||||
GL_SIGN_ERR_IO = 1,
|
||||
GL_SIGN_ERR_FORMAT = 2,
|
||||
GL_SIGN_ERR_ENCRYPTED = 3,
|
||||
GL_SIGN_ERR_KEYNUM = 4,
|
||||
GL_SIGN_ERR_BAD = 5,
|
||||
GL_SIGN_ERR_TOOBIG = 6,
|
||||
GL_SIGN_ERR_RANDOM = 7,
|
||||
GL_SIGN_ERR_EXISTS = 8,
|
||||
GL_SIGN_ERR_WRITE = 9,
|
||||
} gl_sign_status_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t keynum[8];
|
||||
uint8_t key[32];
|
||||
} gl_sign_pubkey_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t keynum[8];
|
||||
uint8_t sig[64];
|
||||
} gl_sign_sig_t;
|
||||
|
||||
int gl_sign_read_pubkey(const char *path, gl_sign_pubkey_t *out);
|
||||
int gl_sign_check_seckey(const char *path);
|
||||
int gl_sign_read_sig(const char *path, gl_sign_sig_t *out);
|
||||
int gl_sign_verify(const gl_sign_pubkey_t *pub, const gl_sign_sig_t *sig,
|
||||
const uint8_t *msg, size_t len);
|
||||
int gl_sign_verify_file(const char *pubkey_path, const char *msg_path,
|
||||
const char *sig_path);
|
||||
int gl_sign_file(const char *seckey_path, const char *msg_path,
|
||||
const char *sig_path);
|
||||
int gl_sign_generate(const char *pubkey_path, const char *seckey_path,
|
||||
const char *comment);
|
||||
]]
|
||||
|
||||
local M = { ffi = ffi }
|
||||
|
||||
M.SCOPE_USR = 0
|
||||
M.SCOPE_SYS = 1
|
||||
|
||||
local TXN_NAMES = {
|
||||
[0] = "GL_TXN_OK", [1] = "GL_TXN_ERR_LOCK", [2] = "GL_TXN_ERR_MKDIR",
|
||||
[3] = "GL_TXN_ERR_PERM", [4] = "GL_TXN_ERR_RENAME", [5] = "GL_TXN_ERR_SYNC",
|
||||
[6] = "GL_TXN_ERR_CLEANUP", [7] = "GL_TXN_ERR_NO_TXN", [8] = "GL_TXN_ERR_XDEV",
|
||||
[9] = "GL_TXN_ERR_DEPS",
|
||||
}
|
||||
local INSTALL_NAMES = {
|
||||
[0] = "GL_INSTALL_OK", [1] = "GL_INSTALL_ERR_OPEN", [2] = "GL_INSTALL_ERR_MANIFEST",
|
||||
[3] = "GL_INSTALL_ERR_EXTRACT", [4] = "GL_INSTALL_ERR_INDEX",
|
||||
[5] = "GL_INSTALL_ERR_ALREADY_INSTALLED", [6] = "GL_INSTALL_SKIPPED",
|
||||
[7] = "GL_INSTALL_ERR_SCOPE", [8] = "GL_INSTALL_ERR_DEPS",
|
||||
}
|
||||
local REMOVE_NAMES = {
|
||||
[0] = "GL_REMOVE_OK", [1] = "GL_REMOVE_ERR_NOT_FOUND",
|
||||
[2] = "GL_REMOVE_ERR_UNLINK", [3] = "GL_REMOVE_ERR_INDEX",
|
||||
[4] = "GL_REMOVE_ERR_DEPS",
|
||||
}
|
||||
local SIGN_NAMES = {
|
||||
[0] = "GL_SIGN_OK", [1] = "GL_SIGN_ERR_IO", [2] = "GL_SIGN_ERR_FORMAT",
|
||||
[3] = "GL_SIGN_ERR_ENCRYPTED", [4] = "GL_SIGN_ERR_KEYNUM", [5] = "GL_SIGN_ERR_BAD",
|
||||
[6] = "GL_SIGN_ERR_TOOBIG", [7] = "GL_SIGN_ERR_RANDOM", [8] = "GL_SIGN_ERR_EXISTS",
|
||||
[9] = "GL_SIGN_ERR_WRITE",
|
||||
}
|
||||
|
||||
function M.txn_name(v) return TXN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.install_name(v) return INSTALL_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.sign_name(v) return SIGN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.remove_name(v) return REMOVE_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
|
||||
function M.new_context()
|
||||
return ffi.new("gl_context_t")
|
||||
end
|
||||
|
||||
-- Human-readable current errno, for reporting why a libc call (mount,
|
||||
-- etc.) failed rather than just that it did. Must be called immediately
|
||||
-- after the failing call, before any other libc-touching code runs.
|
||||
function M.strerror()
|
||||
local err = ffi.errno()
|
||||
return err .. " (" .. ffi.string(ffi.C.strerror(err)) .. ")"
|
||||
end
|
||||
|
||||
-- This module's own directory on disk, independent of CWD and
|
||||
-- independent of how deeply nested the require()ing script is. Lua's
|
||||
-- debug.getinfo(1, "S").source gives the path this exact file was
|
||||
-- loaded from, prefixed with "@" for a real file — not the path of
|
||||
-- whoever called into it.
|
||||
local function this_dir()
|
||||
local src = debug.getinfo(1, "S").source
|
||||
if src:sub(1, 1) == "@" then src = src:sub(2) end
|
||||
return src:match("(.*/)") or "./"
|
||||
end
|
||||
|
||||
-- Loads the combined library. Resolution order:
|
||||
-- 1. an explicit path argument
|
||||
-- 2. LIBGLACIER_SO env var
|
||||
-- 3. glacier/libglacier-ng.so relative to *this file's own location*
|
||||
-- (i.e. <project_root>/lib/glacier/libglacier-ng.so), not CWD —
|
||||
-- this is what makes M.load() work correctly no matter where the
|
||||
-- calling script was invoked from.
|
||||
function M.load(path)
|
||||
local target = path or os.getenv("LIBGLACIER_SO")
|
||||
or (this_dir() .. "glacier/libglacier-ng.so")
|
||||
local ok, lib = pcall(ffi.load, target)
|
||||
if not ok then
|
||||
io.stderr:write(string.format(
|
||||
"glacier_cdef: could not load %s (%s)\n" ..
|
||||
"Set LIBGLACIER_SO to the full path of the built .so if it isn't at the default location.\n",
|
||||
target, tostring(lib)))
|
||||
os.exit(1)
|
||||
end
|
||||
return lib
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,367 @@
|
||||
-- lib/glacier_cdef.lua
|
||||
--
|
||||
-- Single source of truth for every ffi.cdef() declaration used across
|
||||
-- gpkg, gstore, and the Lua test suite. These used to be hand-copied
|
||||
-- into each entry point separately, which is exactly what let a stale
|
||||
-- gl_install_pkg/gl_remove_pkg signature drift silently into gpkg
|
||||
-- during the transaction-context refactor: nothing caught it, because
|
||||
-- Lua FFI has no compiler to reject a mismatched declaration the way C
|
||||
-- would.
|
||||
--
|
||||
-- require() caches modules, so the ffi.cdef() call below runs exactly
|
||||
-- once per process no matter how many files require() this — calling
|
||||
-- ffi.cdef() twice with conflicting content would error, but that's
|
||||
-- not a concern here since there's only one copy now.
|
||||
--
|
||||
-- This module declares the C ABI only. It does not call ffi.load() for
|
||||
-- you automatically — different entry points may want the library from
|
||||
-- different explicit paths — but M.load() below gives every caller a
|
||||
-- consistent, CWD-independent way to find it if they don't need
|
||||
-- anything more specific.
|
||||
|
||||
local ffi = require("ffi")
|
||||
|
||||
ffi.cdef[[
|
||||
typedef unsigned int uid_t;
|
||||
typedef int pid_t;
|
||||
|
||||
unsigned int getuid(void);
|
||||
unsigned int getpid(void);
|
||||
uid_t geteuid(void);
|
||||
void free(void *ptr);
|
||||
char *getcwd(char *buf, size_t size);
|
||||
char *strerror(int errnum);
|
||||
|
||||
void lg_printf(int loglevel, const char *fmt, ...);
|
||||
void lg_gpkg_help(int argc, const char *argv[]);
|
||||
void lg_gstore_help(int argc, const char *argv[]);
|
||||
void lg_syspkg_help(int argc, const char *argv[]);
|
||||
|
||||
typedef enum {
|
||||
INDEX,
|
||||
STORE
|
||||
} index_store_t;
|
||||
_Bool gl_usr_istore_exists(index_store_t index_or_store, int uid);
|
||||
int gl_usr_base_status(uid_t uid);
|
||||
int gl_prune_backups(const char *dir, int keep);
|
||||
|
||||
/* --- pkg.h: glacier.conf --- */
|
||||
typedef enum {
|
||||
GL_CONF_OK = 0,
|
||||
GL_CONF_ERR_READ = 1,
|
||||
GL_CONF_ERR_MISSING = 2,
|
||||
GL_CONF_ERR_TYPE = 3,
|
||||
GL_CONF_ERR_INVALID = 4,
|
||||
GL_CONF_ERR_SPACE = 5
|
||||
} gl_conf_status_t;
|
||||
gl_conf_status_t gl_conf_get_int(const char *path, const char *key, int *out);
|
||||
gl_conf_status_t gl_conf_repo_keys(const char *path, const char *repo,
|
||||
char *out, size_t outlen);
|
||||
gl_conf_status_t gl_conf_repos(const char *path, char *out, size_t outlen);
|
||||
|
||||
/* --- pkg.h: repository indexes --- */
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *ver;
|
||||
char *file;
|
||||
char *sha256;
|
||||
int64_t size;
|
||||
char *deps;
|
||||
char *confs;
|
||||
} gl_index_pkg_t;
|
||||
typedef struct {
|
||||
char *repo;
|
||||
int64_t serial;
|
||||
int64_t valid_until;
|
||||
size_t npkgs;
|
||||
gl_index_pkg_t *pkgs;
|
||||
} gl_index_t;
|
||||
typedef enum {
|
||||
GL_INDEX_OK = 0,
|
||||
GL_INDEX_ERR_FORMAT = 1,
|
||||
GL_INDEX_ERR_REPO = 2,
|
||||
GL_INDEX_ERR_NOMEM = 3
|
||||
} gl_index_status_t;
|
||||
gl_index_status_t gl_index_parse(const char *text, size_t len,
|
||||
const char *repo, gl_index_t *out);
|
||||
void gl_index_free(gl_index_t *idx);
|
||||
|
||||
/* --- transaction.h --- */
|
||||
|
||||
typedef enum {
|
||||
GL_CTX_LIVE,
|
||||
GL_CTX_STAGE
|
||||
} gl_ctx_mode_t;
|
||||
|
||||
typedef enum {
|
||||
GL_SCOPE_USR,
|
||||
GL_SCOPE_SYS
|
||||
} gl_ctx_scope_t;
|
||||
|
||||
typedef struct {
|
||||
uid_t uid;
|
||||
|
||||
char root_path[4096];
|
||||
char index_path[4096];
|
||||
char store_path[4096];
|
||||
char links_path[4096];
|
||||
|
||||
gl_ctx_mode_t mode;
|
||||
gl_ctx_scope_t scope;
|
||||
|
||||
int lock_fd;
|
||||
|
||||
int overwrite;
|
||||
|
||||
char sysroot[4096];
|
||||
|
||||
int link_refusals;
|
||||
|
||||
int strict;
|
||||
} gl_context_t;
|
||||
|
||||
typedef enum {
|
||||
GL_TXN_OK = 0,
|
||||
GL_TXN_ERR_LOCK = 1,
|
||||
GL_TXN_ERR_MKDIR = 2,
|
||||
GL_TXN_ERR_PERM = 3,
|
||||
GL_TXN_ERR_RENAME = 4,
|
||||
GL_TXN_ERR_SYNC = 5,
|
||||
GL_TXN_ERR_CLEANUP = 6,
|
||||
GL_TXN_ERR_NO_TXN = 7,
|
||||
GL_TXN_ERR_XDEV = 8,
|
||||
GL_TXN_ERR_DEPS = 9
|
||||
} gl_txn_status_t;
|
||||
|
||||
int gl_check_sysroot(const char *sysroot);
|
||||
int gl_init_live_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
|
||||
int gl_init_live_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
|
||||
const char *sysroot);
|
||||
gl_txn_status_t gl_init_stage_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
|
||||
gl_txn_status_t gl_init_stage_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
|
||||
const char *sysroot);
|
||||
gl_txn_status_t gl_begin_transaction(gl_context_t *ctx);
|
||||
int gl_scope_state_dir(const gl_context_t *ctx, const char *name, char *out);
|
||||
gl_txn_status_t gl_commit_transaction(gl_context_t *ctx);
|
||||
gl_txn_status_t gl_abort_transaction(gl_context_t *ctx);
|
||||
|
||||
/* --- istoreutils.h --- */
|
||||
|
||||
typedef enum {
|
||||
GL_INSTALL_OK = 0,
|
||||
GL_INSTALL_ERR_OPEN = 1,
|
||||
GL_INSTALL_ERR_MANIFEST = 2,
|
||||
GL_INSTALL_ERR_EXTRACT = 3,
|
||||
GL_INSTALL_ERR_INDEX = 4,
|
||||
GL_INSTALL_ERR_ALREADY_INSTALLED = 5,
|
||||
GL_INSTALL_SKIPPED = 6,
|
||||
GL_INSTALL_ERR_SCOPE = 7,
|
||||
GL_INSTALL_ERR_DEPS = 8
|
||||
} gl_install_status_t;
|
||||
|
||||
typedef enum {
|
||||
GL_REMOVE_OK = 0,
|
||||
GL_REMOVE_ERR_NOT_FOUND = 1,
|
||||
GL_REMOVE_ERR_UNLINK = 2,
|
||||
GL_REMOVE_ERR_INDEX = 3,
|
||||
GL_REMOVE_ERR_DEPS = 4
|
||||
} gl_remove_status_t;
|
||||
|
||||
typedef struct {
|
||||
char **repos;
|
||||
size_t count;
|
||||
} gl_repo_list_t;
|
||||
|
||||
int gl_init_user(uid_t uid, bool isVerbose);
|
||||
int gl_init_sys(bool isVerbose);
|
||||
int gl_init_sys_at(const char *sysroot, bool isVerbose);
|
||||
int gl_create_fs_tree(const char *root, bool isVerbose);
|
||||
int gl_delete_user(uid_t uid, bool isVerbose);
|
||||
|
||||
gl_repo_list_t gl_find_pkg_repos(gl_context_t *ctx, const char *pkg_name);
|
||||
void gl_free_repo_list(gl_repo_list_t *list);
|
||||
|
||||
gl_install_status_t gl_install_pkg(gl_context_t *ctx, const char *gpkg_path);
|
||||
gl_remove_status_t gl_remove_pkg(gl_context_t *ctx, const char *pkg_name, const char *pkg_repo);
|
||||
int gl_backup_pkg(gl_context_t *ctx, const char *repo, const char *name, const char *out_path);
|
||||
int gl_relink_store(gl_context_t *ctx);
|
||||
int gl_set_pkg_auto(gl_context_t *ctx, const char *repo, const char *name, bool automatic);
|
||||
|
||||
/* --- deps.h (built into libglacier_istoreutils.so) --- */
|
||||
int gl_check_plan(const gl_context_t *live,
|
||||
const char *const *add_paths, size_t n_add,
|
||||
const char *const *remove_ids, size_t n_remove);
|
||||
int gl_check_txn(const gl_context_t *stage);
|
||||
|
||||
int gl_backup_istore(const char *tar_path, const char *glacier_root, const char *uid);
|
||||
|
||||
/* --- pkg.h: parsed manifests (arrays end at a NULL name) --- */
|
||||
typedef struct {
|
||||
int maj;
|
||||
int min;
|
||||
int pat;
|
||||
char *tag;
|
||||
} semver_t;
|
||||
typedef struct {
|
||||
char *dep_name;
|
||||
semver_t dep_ver;
|
||||
} dep_t;
|
||||
typedef struct {
|
||||
char *conf_name;
|
||||
semver_t conf_ver;
|
||||
} conf_t;
|
||||
typedef struct {
|
||||
char *pkg_name;
|
||||
semver_t pkg_ver;
|
||||
char *pkg_desc;
|
||||
char *pkg_maint;
|
||||
char *pkg_copyright;
|
||||
char *pkg_repo;
|
||||
dep_t *pkg_dependencies;
|
||||
conf_t *pkg_conflicts;
|
||||
} gl_pkg_t;
|
||||
/* libglacier_istoreutils.so; *pkg must be zeroed, free with gl_free_pkg */
|
||||
int gl_read_pkg_manifest(const char *gpkg_path, gl_pkg_t *pkg);
|
||||
void gl_free_pkg(gl_pkg_t *pkg);
|
||||
|
||||
/* --- libc: process control, used by the crash/contention tests --- */
|
||||
pid_t fork(void);
|
||||
pid_t waitpid(pid_t pid, int *status, int options);
|
||||
int kill(pid_t pid, int sig);
|
||||
void _exit(int status);
|
||||
unsigned int sleep(unsigned int seconds);
|
||||
|
||||
/* --- libc: mount, used by the EXDEV test --- */
|
||||
int mount(const char *source, const char *target, const char *filesystemtype,
|
||||
unsigned long mountflags, const void *data);
|
||||
int umount2(const char *target, int flags);
|
||||
|
||||
/* --- sha256.h (libglacier_verify.so, private) --- */
|
||||
typedef struct {
|
||||
uint32_t state[8];
|
||||
uint64_t bitlen;
|
||||
uint8_t data[64];
|
||||
size_t datalen;
|
||||
} gl_sha256_ctx_t;
|
||||
|
||||
void gl_sha256_init(gl_sha256_ctx_t *ctx);
|
||||
void gl_sha256_update(gl_sha256_ctx_t *ctx, const uint8_t *data, size_t len);
|
||||
void gl_sha256_final(gl_sha256_ctx_t *ctx, uint8_t digest[32]);
|
||||
void gl_sha256_to_hex(const uint8_t digest[32], char *out);
|
||||
|
||||
/* --- sign.h (libglacier_verify.so, private) --- */
|
||||
typedef enum {
|
||||
GL_SIGN_OK = 0,
|
||||
GL_SIGN_ERR_IO = 1,
|
||||
GL_SIGN_ERR_FORMAT = 2,
|
||||
GL_SIGN_ERR_ENCRYPTED = 3,
|
||||
GL_SIGN_ERR_KEYNUM = 4,
|
||||
GL_SIGN_ERR_BAD = 5,
|
||||
GL_SIGN_ERR_TOOBIG = 6,
|
||||
GL_SIGN_ERR_RANDOM = 7,
|
||||
GL_SIGN_ERR_EXISTS = 8,
|
||||
GL_SIGN_ERR_WRITE = 9,
|
||||
} gl_sign_status_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t keynum[8];
|
||||
uint8_t key[32];
|
||||
} gl_sign_pubkey_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t keynum[8];
|
||||
uint8_t sig[64];
|
||||
} gl_sign_sig_t;
|
||||
|
||||
int gl_sign_read_pubkey(const char *path, gl_sign_pubkey_t *out);
|
||||
int gl_sign_check_seckey(const char *path);
|
||||
int gl_sign_read_sig(const char *path, gl_sign_sig_t *out);
|
||||
int gl_sign_verify(const gl_sign_pubkey_t *pub, const gl_sign_sig_t *sig,
|
||||
const uint8_t *msg, size_t len);
|
||||
int gl_sign_verify_file(const char *pubkey_path, const char *msg_path,
|
||||
const char *sig_path);
|
||||
int gl_sign_file(const char *seckey_path, const char *msg_path,
|
||||
const char *sig_path);
|
||||
int gl_sign_generate(const char *pubkey_path, const char *seckey_path,
|
||||
const char *comment);
|
||||
]]
|
||||
|
||||
local M = { ffi = ffi }
|
||||
|
||||
M.SCOPE_USR = 0
|
||||
M.SCOPE_SYS = 1
|
||||
|
||||
local TXN_NAMES = {
|
||||
[0] = "GL_TXN_OK", [1] = "GL_TXN_ERR_LOCK", [2] = "GL_TXN_ERR_MKDIR",
|
||||
[3] = "GL_TXN_ERR_PERM", [4] = "GL_TXN_ERR_RENAME", [5] = "GL_TXN_ERR_SYNC",
|
||||
[6] = "GL_TXN_ERR_CLEANUP", [7] = "GL_TXN_ERR_NO_TXN", [8] = "GL_TXN_ERR_XDEV",
|
||||
[9] = "GL_TXN_ERR_DEPS",
|
||||
}
|
||||
local INSTALL_NAMES = {
|
||||
[0] = "GL_INSTALL_OK", [1] = "GL_INSTALL_ERR_OPEN", [2] = "GL_INSTALL_ERR_MANIFEST",
|
||||
[3] = "GL_INSTALL_ERR_EXTRACT", [4] = "GL_INSTALL_ERR_INDEX",
|
||||
[5] = "GL_INSTALL_ERR_ALREADY_INSTALLED", [6] = "GL_INSTALL_SKIPPED",
|
||||
[7] = "GL_INSTALL_ERR_SCOPE", [8] = "GL_INSTALL_ERR_DEPS",
|
||||
}
|
||||
local REMOVE_NAMES = {
|
||||
[0] = "GL_REMOVE_OK", [1] = "GL_REMOVE_ERR_NOT_FOUND",
|
||||
[2] = "GL_REMOVE_ERR_UNLINK", [3] = "GL_REMOVE_ERR_INDEX",
|
||||
[4] = "GL_REMOVE_ERR_DEPS",
|
||||
}
|
||||
local SIGN_NAMES = {
|
||||
[0] = "GL_SIGN_OK", [1] = "GL_SIGN_ERR_IO", [2] = "GL_SIGN_ERR_FORMAT",
|
||||
[3] = "GL_SIGN_ERR_ENCRYPTED", [4] = "GL_SIGN_ERR_KEYNUM", [5] = "GL_SIGN_ERR_BAD",
|
||||
[6] = "GL_SIGN_ERR_TOOBIG", [7] = "GL_SIGN_ERR_RANDOM", [8] = "GL_SIGN_ERR_EXISTS",
|
||||
[9] = "GL_SIGN_ERR_WRITE",
|
||||
}
|
||||
|
||||
function M.txn_name(v) return TXN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.install_name(v) return INSTALL_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.sign_name(v) return SIGN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.remove_name(v) return REMOVE_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
|
||||
function M.new_context()
|
||||
return ffi.new("gl_context_t")
|
||||
end
|
||||
|
||||
-- Human-readable current errno, for reporting why a libc call (mount,
|
||||
-- etc.) failed rather than just that it did. Must be called immediately
|
||||
-- after the failing call, before any other libc-touching code runs.
|
||||
function M.strerror()
|
||||
local err = ffi.errno()
|
||||
return err .. " (" .. ffi.string(ffi.C.strerror(err)) .. ")"
|
||||
end
|
||||
|
||||
-- This module's own directory on disk, independent of CWD and
|
||||
-- independent of how deeply nested the require()ing script is. Lua's
|
||||
-- debug.getinfo(1, "S").source gives the path this exact file was
|
||||
-- loaded from, prefixed with "@" for a real file — not the path of
|
||||
-- whoever called into it.
|
||||
local function this_dir()
|
||||
local src = debug.getinfo(1, "S").source
|
||||
if src:sub(1, 1) == "@" then src = src:sub(2) end
|
||||
return src:match("(.*/)") or "./"
|
||||
end
|
||||
|
||||
-- Loads the combined library. Resolution order:
|
||||
-- 1. an explicit path argument
|
||||
-- 2. LIBGLACIER_SO env var
|
||||
-- 3. glacier/libglacier-ng.so relative to *this file's own location*
|
||||
-- (i.e. <project_root>/lib/glacier/libglacier-ng.so), not CWD —
|
||||
-- this is what makes M.load() work correctly no matter where the
|
||||
-- calling script was invoked from.
|
||||
function M.load(path)
|
||||
local target = path or os.getenv("LIBGLACIER_SO")
|
||||
or (this_dir() .. "glacier/libglacier-ng.so")
|
||||
local ok, lib = pcall(ffi.load, target)
|
||||
if not ok then
|
||||
io.stderr:write(string.format(
|
||||
"glacier_cdef: could not load %s (%s)\n" ..
|
||||
"Set LIBGLACIER_SO to the full path of the built .so if it isn't at the default location.\n",
|
||||
target, tostring(lib)))
|
||||
os.exit(1)
|
||||
end
|
||||
return lib
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,368 @@
|
||||
-- lib/glacier_cdef.lua
|
||||
--
|
||||
-- Single source of truth for every ffi.cdef() declaration used across
|
||||
-- gpkg, gstore, and the Lua test suite. These used to be hand-copied
|
||||
-- into each entry point separately, which is exactly what let a stale
|
||||
-- gl_install_pkg/gl_remove_pkg signature drift silently into gpkg
|
||||
-- during the transaction-context refactor: nothing caught it, because
|
||||
-- Lua FFI has no compiler to reject a mismatched declaration the way C
|
||||
-- would.
|
||||
--
|
||||
-- require() caches modules, so the ffi.cdef() call below runs exactly
|
||||
-- once per process no matter how many files require() this — calling
|
||||
-- ffi.cdef() twice with conflicting content would error, but that's
|
||||
-- not a concern here since there's only one copy now.
|
||||
--
|
||||
-- This module declares the C ABI only. It does not call ffi.load() for
|
||||
-- you automatically — different entry points may want the library from
|
||||
-- different explicit paths — but M.load() below gives every caller a
|
||||
-- consistent, CWD-independent way to find it if they don't need
|
||||
-- anything more specific.
|
||||
|
||||
local ffi = require("ffi")
|
||||
|
||||
ffi.cdef[[
|
||||
typedef unsigned int uid_t;
|
||||
typedef int pid_t;
|
||||
|
||||
unsigned int getuid(void);
|
||||
unsigned int getpid(void);
|
||||
uid_t geteuid(void);
|
||||
void free(void *ptr);
|
||||
char *getcwd(char *buf, size_t size);
|
||||
char *strerror(int errnum);
|
||||
|
||||
void lg_printf(int loglevel, const char *fmt, ...);
|
||||
void lg_gpkg_help(int argc, const char *argv[]);
|
||||
void lg_gstore_help(int argc, const char *argv[]);
|
||||
void lg_syspkg_help(int argc, const char *argv[]);
|
||||
|
||||
typedef enum {
|
||||
INDEX,
|
||||
STORE
|
||||
} index_store_t;
|
||||
_Bool gl_usr_istore_exists(index_store_t index_or_store, int uid);
|
||||
int gl_usr_base_status(uid_t uid);
|
||||
int gl_prune_backups(const char *dir, int keep);
|
||||
|
||||
/* --- pkg.h: glacier.conf --- */
|
||||
typedef enum {
|
||||
GL_CONF_OK = 0,
|
||||
GL_CONF_ERR_READ = 1,
|
||||
GL_CONF_ERR_MISSING = 2,
|
||||
GL_CONF_ERR_TYPE = 3,
|
||||
GL_CONF_ERR_INVALID = 4,
|
||||
GL_CONF_ERR_SPACE = 5
|
||||
} gl_conf_status_t;
|
||||
gl_conf_status_t gl_conf_get_int(const char *path, const char *key, int *out);
|
||||
gl_conf_status_t gl_conf_repo_keys(const char *path, const char *repo,
|
||||
char *out, size_t outlen);
|
||||
gl_conf_status_t gl_conf_repos(const char *path, char *out, size_t outlen);
|
||||
|
||||
/* --- pkg.h: repository indexes --- */
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *ver;
|
||||
char *file;
|
||||
char *sha256;
|
||||
int64_t size;
|
||||
char *deps;
|
||||
char *confs;
|
||||
} gl_index_pkg_t;
|
||||
typedef struct {
|
||||
char *repo;
|
||||
int64_t serial;
|
||||
int64_t valid_until;
|
||||
size_t npkgs;
|
||||
gl_index_pkg_t *pkgs;
|
||||
} gl_index_t;
|
||||
typedef enum {
|
||||
GL_INDEX_OK = 0,
|
||||
GL_INDEX_ERR_FORMAT = 1,
|
||||
GL_INDEX_ERR_REPO = 2,
|
||||
GL_INDEX_ERR_NOMEM = 3
|
||||
} gl_index_status_t;
|
||||
gl_index_status_t gl_index_parse(const char *text, size_t len,
|
||||
const char *repo, gl_index_t *out);
|
||||
void gl_index_free(gl_index_t *idx);
|
||||
|
||||
/* --- transaction.h --- */
|
||||
|
||||
typedef enum {
|
||||
GL_CTX_LIVE,
|
||||
GL_CTX_STAGE
|
||||
} gl_ctx_mode_t;
|
||||
|
||||
typedef enum {
|
||||
GL_SCOPE_USR,
|
||||
GL_SCOPE_SYS
|
||||
} gl_ctx_scope_t;
|
||||
|
||||
typedef struct {
|
||||
uid_t uid;
|
||||
|
||||
char root_path[4096];
|
||||
char index_path[4096];
|
||||
char store_path[4096];
|
||||
char links_path[4096];
|
||||
|
||||
gl_ctx_mode_t mode;
|
||||
gl_ctx_scope_t scope;
|
||||
|
||||
int lock_fd;
|
||||
|
||||
int overwrite;
|
||||
|
||||
char sysroot[4096];
|
||||
|
||||
int link_refusals;
|
||||
|
||||
int strict;
|
||||
} gl_context_t;
|
||||
|
||||
typedef enum {
|
||||
GL_TXN_OK = 0,
|
||||
GL_TXN_ERR_LOCK = 1,
|
||||
GL_TXN_ERR_MKDIR = 2,
|
||||
GL_TXN_ERR_PERM = 3,
|
||||
GL_TXN_ERR_RENAME = 4,
|
||||
GL_TXN_ERR_SYNC = 5,
|
||||
GL_TXN_ERR_CLEANUP = 6,
|
||||
GL_TXN_ERR_NO_TXN = 7,
|
||||
GL_TXN_ERR_XDEV = 8,
|
||||
GL_TXN_ERR_DEPS = 9
|
||||
} gl_txn_status_t;
|
||||
|
||||
int gl_check_sysroot(const char *sysroot);
|
||||
int gl_init_live_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
|
||||
int gl_init_live_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
|
||||
const char *sysroot);
|
||||
gl_txn_status_t gl_init_stage_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
|
||||
gl_txn_status_t gl_init_stage_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
|
||||
const char *sysroot);
|
||||
gl_txn_status_t gl_begin_transaction(gl_context_t *ctx);
|
||||
int gl_scope_state_dir(const gl_context_t *ctx, const char *name, char *out);
|
||||
int gl_scope_cache_dir(const gl_context_t *ctx, const char *repo, char *out);
|
||||
gl_txn_status_t gl_commit_transaction(gl_context_t *ctx);
|
||||
gl_txn_status_t gl_abort_transaction(gl_context_t *ctx);
|
||||
|
||||
/* --- istoreutils.h --- */
|
||||
|
||||
typedef enum {
|
||||
GL_INSTALL_OK = 0,
|
||||
GL_INSTALL_ERR_OPEN = 1,
|
||||
GL_INSTALL_ERR_MANIFEST = 2,
|
||||
GL_INSTALL_ERR_EXTRACT = 3,
|
||||
GL_INSTALL_ERR_INDEX = 4,
|
||||
GL_INSTALL_ERR_ALREADY_INSTALLED = 5,
|
||||
GL_INSTALL_SKIPPED = 6,
|
||||
GL_INSTALL_ERR_SCOPE = 7,
|
||||
GL_INSTALL_ERR_DEPS = 8
|
||||
} gl_install_status_t;
|
||||
|
||||
typedef enum {
|
||||
GL_REMOVE_OK = 0,
|
||||
GL_REMOVE_ERR_NOT_FOUND = 1,
|
||||
GL_REMOVE_ERR_UNLINK = 2,
|
||||
GL_REMOVE_ERR_INDEX = 3,
|
||||
GL_REMOVE_ERR_DEPS = 4
|
||||
} gl_remove_status_t;
|
||||
|
||||
typedef struct {
|
||||
char **repos;
|
||||
size_t count;
|
||||
} gl_repo_list_t;
|
||||
|
||||
int gl_init_user(uid_t uid, bool isVerbose);
|
||||
int gl_init_sys(bool isVerbose);
|
||||
int gl_init_sys_at(const char *sysroot, bool isVerbose);
|
||||
int gl_create_fs_tree(const char *root, bool isVerbose);
|
||||
int gl_delete_user(uid_t uid, bool isVerbose);
|
||||
|
||||
gl_repo_list_t gl_find_pkg_repos(gl_context_t *ctx, const char *pkg_name);
|
||||
void gl_free_repo_list(gl_repo_list_t *list);
|
||||
|
||||
gl_install_status_t gl_install_pkg(gl_context_t *ctx, const char *gpkg_path);
|
||||
gl_remove_status_t gl_remove_pkg(gl_context_t *ctx, const char *pkg_name, const char *pkg_repo);
|
||||
int gl_backup_pkg(gl_context_t *ctx, const char *repo, const char *name, const char *out_path);
|
||||
int gl_relink_store(gl_context_t *ctx);
|
||||
int gl_set_pkg_auto(gl_context_t *ctx, const char *repo, const char *name, bool automatic);
|
||||
|
||||
/* --- deps.h (built into libglacier_istoreutils.so) --- */
|
||||
int gl_check_plan(const gl_context_t *live,
|
||||
const char *const *add_paths, size_t n_add,
|
||||
const char *const *remove_ids, size_t n_remove);
|
||||
int gl_check_txn(const gl_context_t *stage);
|
||||
|
||||
int gl_backup_istore(const char *tar_path, const char *glacier_root, const char *uid);
|
||||
|
||||
/* --- pkg.h: parsed manifests (arrays end at a NULL name) --- */
|
||||
typedef struct {
|
||||
int maj;
|
||||
int min;
|
||||
int pat;
|
||||
char *tag;
|
||||
} semver_t;
|
||||
typedef struct {
|
||||
char *dep_name;
|
||||
semver_t dep_ver;
|
||||
} dep_t;
|
||||
typedef struct {
|
||||
char *conf_name;
|
||||
semver_t conf_ver;
|
||||
} conf_t;
|
||||
typedef struct {
|
||||
char *pkg_name;
|
||||
semver_t pkg_ver;
|
||||
char *pkg_desc;
|
||||
char *pkg_maint;
|
||||
char *pkg_copyright;
|
||||
char *pkg_repo;
|
||||
dep_t *pkg_dependencies;
|
||||
conf_t *pkg_conflicts;
|
||||
} gl_pkg_t;
|
||||
/* libglacier_istoreutils.so; *pkg must be zeroed, free with gl_free_pkg */
|
||||
int gl_read_pkg_manifest(const char *gpkg_path, gl_pkg_t *pkg);
|
||||
void gl_free_pkg(gl_pkg_t *pkg);
|
||||
|
||||
/* --- libc: process control, used by the crash/contention tests --- */
|
||||
pid_t fork(void);
|
||||
pid_t waitpid(pid_t pid, int *status, int options);
|
||||
int kill(pid_t pid, int sig);
|
||||
void _exit(int status);
|
||||
unsigned int sleep(unsigned int seconds);
|
||||
|
||||
/* --- libc: mount, used by the EXDEV test --- */
|
||||
int mount(const char *source, const char *target, const char *filesystemtype,
|
||||
unsigned long mountflags, const void *data);
|
||||
int umount2(const char *target, int flags);
|
||||
|
||||
/* --- sha256.h (libglacier_verify.so, private) --- */
|
||||
typedef struct {
|
||||
uint32_t state[8];
|
||||
uint64_t bitlen;
|
||||
uint8_t data[64];
|
||||
size_t datalen;
|
||||
} gl_sha256_ctx_t;
|
||||
|
||||
void gl_sha256_init(gl_sha256_ctx_t *ctx);
|
||||
void gl_sha256_update(gl_sha256_ctx_t *ctx, const uint8_t *data, size_t len);
|
||||
void gl_sha256_final(gl_sha256_ctx_t *ctx, uint8_t digest[32]);
|
||||
void gl_sha256_to_hex(const uint8_t digest[32], char *out);
|
||||
|
||||
/* --- sign.h (libglacier_verify.so, private) --- */
|
||||
typedef enum {
|
||||
GL_SIGN_OK = 0,
|
||||
GL_SIGN_ERR_IO = 1,
|
||||
GL_SIGN_ERR_FORMAT = 2,
|
||||
GL_SIGN_ERR_ENCRYPTED = 3,
|
||||
GL_SIGN_ERR_KEYNUM = 4,
|
||||
GL_SIGN_ERR_BAD = 5,
|
||||
GL_SIGN_ERR_TOOBIG = 6,
|
||||
GL_SIGN_ERR_RANDOM = 7,
|
||||
GL_SIGN_ERR_EXISTS = 8,
|
||||
GL_SIGN_ERR_WRITE = 9,
|
||||
} gl_sign_status_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t keynum[8];
|
||||
uint8_t key[32];
|
||||
} gl_sign_pubkey_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t keynum[8];
|
||||
uint8_t sig[64];
|
||||
} gl_sign_sig_t;
|
||||
|
||||
int gl_sign_read_pubkey(const char *path, gl_sign_pubkey_t *out);
|
||||
int gl_sign_check_seckey(const char *path);
|
||||
int gl_sign_read_sig(const char *path, gl_sign_sig_t *out);
|
||||
int gl_sign_verify(const gl_sign_pubkey_t *pub, const gl_sign_sig_t *sig,
|
||||
const uint8_t *msg, size_t len);
|
||||
int gl_sign_verify_file(const char *pubkey_path, const char *msg_path,
|
||||
const char *sig_path);
|
||||
int gl_sign_file(const char *seckey_path, const char *msg_path,
|
||||
const char *sig_path);
|
||||
int gl_sign_generate(const char *pubkey_path, const char *seckey_path,
|
||||
const char *comment);
|
||||
]]
|
||||
|
||||
local M = { ffi = ffi }
|
||||
|
||||
M.SCOPE_USR = 0
|
||||
M.SCOPE_SYS = 1
|
||||
|
||||
local TXN_NAMES = {
|
||||
[0] = "GL_TXN_OK", [1] = "GL_TXN_ERR_LOCK", [2] = "GL_TXN_ERR_MKDIR",
|
||||
[3] = "GL_TXN_ERR_PERM", [4] = "GL_TXN_ERR_RENAME", [5] = "GL_TXN_ERR_SYNC",
|
||||
[6] = "GL_TXN_ERR_CLEANUP", [7] = "GL_TXN_ERR_NO_TXN", [8] = "GL_TXN_ERR_XDEV",
|
||||
[9] = "GL_TXN_ERR_DEPS",
|
||||
}
|
||||
local INSTALL_NAMES = {
|
||||
[0] = "GL_INSTALL_OK", [1] = "GL_INSTALL_ERR_OPEN", [2] = "GL_INSTALL_ERR_MANIFEST",
|
||||
[3] = "GL_INSTALL_ERR_EXTRACT", [4] = "GL_INSTALL_ERR_INDEX",
|
||||
[5] = "GL_INSTALL_ERR_ALREADY_INSTALLED", [6] = "GL_INSTALL_SKIPPED",
|
||||
[7] = "GL_INSTALL_ERR_SCOPE", [8] = "GL_INSTALL_ERR_DEPS",
|
||||
}
|
||||
local REMOVE_NAMES = {
|
||||
[0] = "GL_REMOVE_OK", [1] = "GL_REMOVE_ERR_NOT_FOUND",
|
||||
[2] = "GL_REMOVE_ERR_UNLINK", [3] = "GL_REMOVE_ERR_INDEX",
|
||||
[4] = "GL_REMOVE_ERR_DEPS",
|
||||
}
|
||||
local SIGN_NAMES = {
|
||||
[0] = "GL_SIGN_OK", [1] = "GL_SIGN_ERR_IO", [2] = "GL_SIGN_ERR_FORMAT",
|
||||
[3] = "GL_SIGN_ERR_ENCRYPTED", [4] = "GL_SIGN_ERR_KEYNUM", [5] = "GL_SIGN_ERR_BAD",
|
||||
[6] = "GL_SIGN_ERR_TOOBIG", [7] = "GL_SIGN_ERR_RANDOM", [8] = "GL_SIGN_ERR_EXISTS",
|
||||
[9] = "GL_SIGN_ERR_WRITE",
|
||||
}
|
||||
|
||||
function M.txn_name(v) return TXN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.install_name(v) return INSTALL_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.sign_name(v) return SIGN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.remove_name(v) return REMOVE_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
|
||||
function M.new_context()
|
||||
return ffi.new("gl_context_t")
|
||||
end
|
||||
|
||||
-- Human-readable current errno, for reporting why a libc call (mount,
|
||||
-- etc.) failed rather than just that it did. Must be called immediately
|
||||
-- after the failing call, before any other libc-touching code runs.
|
||||
function M.strerror()
|
||||
local err = ffi.errno()
|
||||
return err .. " (" .. ffi.string(ffi.C.strerror(err)) .. ")"
|
||||
end
|
||||
|
||||
-- This module's own directory on disk, independent of CWD and
|
||||
-- independent of how deeply nested the require()ing script is. Lua's
|
||||
-- debug.getinfo(1, "S").source gives the path this exact file was
|
||||
-- loaded from, prefixed with "@" for a real file — not the path of
|
||||
-- whoever called into it.
|
||||
local function this_dir()
|
||||
local src = debug.getinfo(1, "S").source
|
||||
if src:sub(1, 1) == "@" then src = src:sub(2) end
|
||||
return src:match("(.*/)") or "./"
|
||||
end
|
||||
|
||||
-- Loads the combined library. Resolution order:
|
||||
-- 1. an explicit path argument
|
||||
-- 2. LIBGLACIER_SO env var
|
||||
-- 3. glacier/libglacier-ng.so relative to *this file's own location*
|
||||
-- (i.e. <project_root>/lib/glacier/libglacier-ng.so), not CWD —
|
||||
-- this is what makes M.load() work correctly no matter where the
|
||||
-- calling script was invoked from.
|
||||
function M.load(path)
|
||||
local target = path or os.getenv("LIBGLACIER_SO")
|
||||
or (this_dir() .. "glacier/libglacier-ng.so")
|
||||
local ok, lib = pcall(ffi.load, target)
|
||||
if not ok then
|
||||
io.stderr:write(string.format(
|
||||
"glacier_cdef: could not load %s (%s)\n" ..
|
||||
"Set LIBGLACIER_SO to the full path of the built .so if it isn't at the default location.\n",
|
||||
target, tostring(lib)))
|
||||
os.exit(1)
|
||||
end
|
||||
return lib
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,423 @@
|
||||
-- lib/glacier_cdef.lua
|
||||
--
|
||||
-- Single source of truth for every ffi.cdef() declaration used across
|
||||
-- gpkg, gstore, and the Lua test suite. These used to be hand-copied
|
||||
-- into each entry point separately, which is exactly what let a stale
|
||||
-- gl_install_pkg/gl_remove_pkg signature drift silently into gpkg
|
||||
-- during the transaction-context refactor: nothing caught it, because
|
||||
-- Lua FFI has no compiler to reject a mismatched declaration the way C
|
||||
-- would.
|
||||
--
|
||||
-- require() caches modules, so the ffi.cdef() call below runs exactly
|
||||
-- once per process no matter how many files require() this — calling
|
||||
-- ffi.cdef() twice with conflicting content would error, but that's
|
||||
-- not a concern here since there's only one copy now.
|
||||
--
|
||||
-- This module declares the C ABI only. It does not call ffi.load() for
|
||||
-- you automatically — different entry points may want the library from
|
||||
-- different explicit paths — but M.load() below gives every caller a
|
||||
-- consistent, CWD-independent way to find it if they don't need
|
||||
-- anything more specific.
|
||||
|
||||
local ffi = require("ffi")
|
||||
|
||||
ffi.cdef[[
|
||||
typedef unsigned int uid_t;
|
||||
typedef int pid_t;
|
||||
|
||||
unsigned int getuid(void);
|
||||
unsigned int getpid(void);
|
||||
uid_t geteuid(void);
|
||||
void free(void *ptr);
|
||||
char *getcwd(char *buf, size_t size);
|
||||
char *strerror(int errnum);
|
||||
|
||||
void lg_printf(int loglevel, const char *fmt, ...);
|
||||
void lg_gpkg_help(int argc, const char *argv[]);
|
||||
void lg_gstore_help(int argc, const char *argv[]);
|
||||
void lg_syspkg_help(int argc, const char *argv[]);
|
||||
|
||||
typedef enum {
|
||||
INDEX,
|
||||
STORE
|
||||
} index_store_t;
|
||||
_Bool gl_usr_istore_exists(index_store_t index_or_store, int uid);
|
||||
int gl_usr_base_status(uid_t uid);
|
||||
int gl_prune_backups(const char *dir, int keep);
|
||||
|
||||
/* --- pkg.h: glacier.conf --- */
|
||||
typedef enum {
|
||||
GL_CONF_OK = 0,
|
||||
GL_CONF_ERR_READ = 1,
|
||||
GL_CONF_ERR_MISSING = 2,
|
||||
GL_CONF_ERR_TYPE = 3,
|
||||
GL_CONF_ERR_INVALID = 4,
|
||||
GL_CONF_ERR_SPACE = 5
|
||||
} gl_conf_status_t;
|
||||
gl_conf_status_t gl_conf_get_int(const char *path, const char *key, int *out);
|
||||
gl_conf_status_t gl_conf_repo_keys(const char *path, const char *repo,
|
||||
char *out, size_t outlen);
|
||||
gl_conf_status_t gl_conf_repos(const char *path, char *out, size_t outlen);
|
||||
typedef struct {
|
||||
char *traits;
|
||||
char *cflags;
|
||||
char *cxxflags;
|
||||
char *ldflags;
|
||||
char *configure;
|
||||
int jobs;
|
||||
char *build_user;
|
||||
char *build_root;
|
||||
} gl_build_conf_t;
|
||||
typedef enum {
|
||||
GL_CONF_SYSTEM = 0,
|
||||
GL_CONF_USER = 1,
|
||||
GL_CONF_PACKAGE = 2
|
||||
} gl_conf_kind_t;
|
||||
gl_conf_status_t gl_conf_build(const char *path, gl_conf_kind_t kind,
|
||||
gl_build_conf_t *out);
|
||||
void gl_build_conf_free(gl_build_conf_t *c);
|
||||
|
||||
/* --- pkg.h: repository indexes --- */
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *ver;
|
||||
char *file;
|
||||
char *sha256;
|
||||
int64_t size;
|
||||
char *deps;
|
||||
char *confs;
|
||||
char *build_deps;
|
||||
char *traits;
|
||||
} gl_index_pkg_t;
|
||||
typedef struct {
|
||||
char *repo;
|
||||
int64_t serial;
|
||||
int64_t valid_until;
|
||||
size_t npkgs;
|
||||
gl_index_pkg_t *pkgs;
|
||||
} gl_index_t;
|
||||
typedef enum {
|
||||
GL_INDEX_OK = 0,
|
||||
GL_INDEX_ERR_FORMAT = 1,
|
||||
GL_INDEX_ERR_REPO = 2,
|
||||
GL_INDEX_ERR_NOMEM = 3
|
||||
} gl_index_status_t;
|
||||
gl_index_status_t gl_index_parse(const char *text, size_t len,
|
||||
const char *repo, gl_index_t *out);
|
||||
void gl_index_free(gl_index_t *idx);
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *ver;
|
||||
char *repo;
|
||||
char *desc;
|
||||
char *maint;
|
||||
char *copyright;
|
||||
char *sources;
|
||||
char *patches;
|
||||
char *build;
|
||||
char *configure;
|
||||
char *traits;
|
||||
char *trait_args;
|
||||
char *deps;
|
||||
char *build_deps;
|
||||
char *confs;
|
||||
} gl_recipe_t;
|
||||
typedef enum {
|
||||
GL_RECIPE_OK = 0,
|
||||
GL_RECIPE_ERR_READ = 1,
|
||||
GL_RECIPE_ERR_BUNDLE = 2,
|
||||
GL_RECIPE_ERR_FORMAT = 3,
|
||||
GL_RECIPE_ERR_NOMEM = 4
|
||||
} gl_recipe_status_t;
|
||||
gl_recipe_status_t gl_recipe_parse(const char *text, size_t len, gl_recipe_t *out);
|
||||
gl_recipe_status_t gl_recipe_read(const char *bundle, gl_recipe_t *out);
|
||||
void gl_recipe_free(gl_recipe_t *r);
|
||||
bool gl_valid_trait(const char *s);
|
||||
|
||||
/* --- transaction.h --- */
|
||||
|
||||
typedef enum {
|
||||
GL_CTX_LIVE,
|
||||
GL_CTX_STAGE
|
||||
} gl_ctx_mode_t;
|
||||
|
||||
typedef enum {
|
||||
GL_SCOPE_USR,
|
||||
GL_SCOPE_SYS
|
||||
} gl_ctx_scope_t;
|
||||
|
||||
typedef struct {
|
||||
uid_t uid;
|
||||
|
||||
char root_path[4096];
|
||||
char index_path[4096];
|
||||
char store_path[4096];
|
||||
char links_path[4096];
|
||||
|
||||
gl_ctx_mode_t mode;
|
||||
gl_ctx_scope_t scope;
|
||||
|
||||
int lock_fd;
|
||||
|
||||
int overwrite;
|
||||
|
||||
char sysroot[4096];
|
||||
|
||||
int link_refusals;
|
||||
|
||||
int strict;
|
||||
} gl_context_t;
|
||||
|
||||
typedef enum {
|
||||
GL_TXN_OK = 0,
|
||||
GL_TXN_ERR_LOCK = 1,
|
||||
GL_TXN_ERR_MKDIR = 2,
|
||||
GL_TXN_ERR_PERM = 3,
|
||||
GL_TXN_ERR_RENAME = 4,
|
||||
GL_TXN_ERR_SYNC = 5,
|
||||
GL_TXN_ERR_CLEANUP = 6,
|
||||
GL_TXN_ERR_NO_TXN = 7,
|
||||
GL_TXN_ERR_XDEV = 8,
|
||||
GL_TXN_ERR_DEPS = 9
|
||||
} gl_txn_status_t;
|
||||
|
||||
int gl_check_sysroot(const char *sysroot);
|
||||
int gl_init_live_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
|
||||
int gl_init_live_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
|
||||
const char *sysroot);
|
||||
gl_txn_status_t gl_init_stage_context(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid);
|
||||
gl_txn_status_t gl_init_stage_context_at(gl_context_t *ctx, gl_ctx_scope_t scope, uid_t uid,
|
||||
const char *sysroot);
|
||||
gl_txn_status_t gl_begin_transaction(gl_context_t *ctx);
|
||||
int gl_scope_state_dir(const gl_context_t *ctx, const char *name, char *out);
|
||||
int gl_scope_cache_dir(const gl_context_t *ctx, const char *repo, char *out);
|
||||
int gl_scope_build_dir(const gl_context_t *ctx, const char *name, int owner, char *out);
|
||||
gl_txn_status_t gl_commit_transaction(gl_context_t *ctx);
|
||||
gl_txn_status_t gl_abort_transaction(gl_context_t *ctx);
|
||||
|
||||
/* --- istoreutils.h --- */
|
||||
|
||||
typedef enum {
|
||||
GL_INSTALL_OK = 0,
|
||||
GL_INSTALL_ERR_OPEN = 1,
|
||||
GL_INSTALL_ERR_MANIFEST = 2,
|
||||
GL_INSTALL_ERR_EXTRACT = 3,
|
||||
GL_INSTALL_ERR_INDEX = 4,
|
||||
GL_INSTALL_ERR_ALREADY_INSTALLED = 5,
|
||||
GL_INSTALL_SKIPPED = 6,
|
||||
GL_INSTALL_ERR_SCOPE = 7,
|
||||
GL_INSTALL_ERR_DEPS = 8
|
||||
} gl_install_status_t;
|
||||
|
||||
typedef enum {
|
||||
GL_REMOVE_OK = 0,
|
||||
GL_REMOVE_ERR_NOT_FOUND = 1,
|
||||
GL_REMOVE_ERR_UNLINK = 2,
|
||||
GL_REMOVE_ERR_INDEX = 3,
|
||||
GL_REMOVE_ERR_DEPS = 4
|
||||
} gl_remove_status_t;
|
||||
|
||||
typedef struct {
|
||||
char **repos;
|
||||
size_t count;
|
||||
} gl_repo_list_t;
|
||||
|
||||
int gl_init_user(uid_t uid, bool isVerbose);
|
||||
int gl_init_sys(bool isVerbose);
|
||||
int gl_init_sys_at(const char *sysroot, bool isVerbose);
|
||||
int gl_create_fs_tree(const char *root, bool isVerbose);
|
||||
int gl_delete_user(uid_t uid, bool isVerbose);
|
||||
|
||||
gl_repo_list_t gl_find_pkg_repos(gl_context_t *ctx, const char *pkg_name);
|
||||
void gl_free_repo_list(gl_repo_list_t *list);
|
||||
|
||||
gl_install_status_t gl_install_pkg(gl_context_t *ctx, const char *gpkg_path);
|
||||
gl_install_status_t gl_extract_files(const char *gpkg_path, const char *dest);
|
||||
gl_remove_status_t gl_remove_pkg(gl_context_t *ctx, const char *pkg_name, const char *pkg_repo);
|
||||
int gl_backup_pkg(gl_context_t *ctx, const char *repo, const char *name, const char *out_path);
|
||||
int gl_relink_store(gl_context_t *ctx);
|
||||
int gl_set_pkg_auto(gl_context_t *ctx, const char *repo, const char *name, bool automatic);
|
||||
int gl_set_pkg_src(gl_context_t *ctx, const char *repo, const char *name, const char *src_repo);
|
||||
int gl_pkg_src(gl_context_t *ctx, const char *repo, const char *name, char *out, size_t len);
|
||||
|
||||
/* --- deps.h (built into libglacier_istoreutils.so) --- */
|
||||
int gl_check_plan(const gl_context_t *live,
|
||||
const char *const *add_paths, size_t n_add,
|
||||
const char *const *remove_ids, size_t n_remove);
|
||||
int gl_check_txn(const gl_context_t *stage);
|
||||
|
||||
int gl_backup_istore(const char *tar_path, const char *glacier_root, const char *uid);
|
||||
|
||||
/* --- pkg.h: parsed manifests (arrays end at a NULL name) --- */
|
||||
typedef struct {
|
||||
int maj;
|
||||
int min;
|
||||
int pat;
|
||||
char *tag;
|
||||
} semver_t;
|
||||
typedef struct {
|
||||
char *dep_name;
|
||||
semver_t dep_ver;
|
||||
} dep_t;
|
||||
typedef struct {
|
||||
char *conf_name;
|
||||
semver_t conf_ver;
|
||||
} conf_t;
|
||||
typedef struct {
|
||||
char *pkg_name;
|
||||
semver_t pkg_ver;
|
||||
char *pkg_desc;
|
||||
char *pkg_maint;
|
||||
char *pkg_copyright;
|
||||
char *pkg_repo;
|
||||
dep_t *pkg_dependencies;
|
||||
conf_t *pkg_conflicts;
|
||||
char *pkg_traits;
|
||||
} gl_pkg_t;
|
||||
/* libglacier_istoreutils.so; *pkg must be zeroed, free with gl_free_pkg */
|
||||
int gl_read_pkg_manifest(const char *gpkg_path, gl_pkg_t *pkg);
|
||||
int gl_gpm2gpkg(const char *filename, gl_pkg_t *pkg);
|
||||
void gl_free_pkg(gl_pkg_t *pkg);
|
||||
|
||||
/* --- libc: process control, used by the crash/contention tests --- */
|
||||
pid_t fork(void);
|
||||
pid_t waitpid(pid_t pid, int *status, int options);
|
||||
int kill(pid_t pid, int sig);
|
||||
void _exit(int status);
|
||||
unsigned int sleep(unsigned int seconds);
|
||||
|
||||
/* --- libc: mount, used by the EXDEV test --- */
|
||||
int mount(const char *source, const char *target, const char *filesystemtype,
|
||||
unsigned long mountflags, const void *data);
|
||||
int umount2(const char *target, int flags);
|
||||
|
||||
/* --- sha256.h (libglacier_verify.so, private) --- */
|
||||
typedef struct {
|
||||
uint32_t state[8];
|
||||
uint64_t bitlen;
|
||||
uint8_t data[64];
|
||||
size_t datalen;
|
||||
} gl_sha256_ctx_t;
|
||||
|
||||
void gl_sha256_init(gl_sha256_ctx_t *ctx);
|
||||
void gl_sha256_update(gl_sha256_ctx_t *ctx, const uint8_t *data, size_t len);
|
||||
void gl_sha256_final(gl_sha256_ctx_t *ctx, uint8_t digest[32]);
|
||||
void gl_sha256_to_hex(const uint8_t digest[32], char *out);
|
||||
|
||||
/* --- sign.h (libglacier_verify.so, private) --- */
|
||||
typedef enum {
|
||||
GL_SIGN_OK = 0,
|
||||
GL_SIGN_ERR_IO = 1,
|
||||
GL_SIGN_ERR_FORMAT = 2,
|
||||
GL_SIGN_ERR_ENCRYPTED = 3,
|
||||
GL_SIGN_ERR_KEYNUM = 4,
|
||||
GL_SIGN_ERR_BAD = 5,
|
||||
GL_SIGN_ERR_TOOBIG = 6,
|
||||
GL_SIGN_ERR_RANDOM = 7,
|
||||
GL_SIGN_ERR_EXISTS = 8,
|
||||
GL_SIGN_ERR_WRITE = 9,
|
||||
} gl_sign_status_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t keynum[8];
|
||||
uint8_t key[32];
|
||||
} gl_sign_pubkey_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t keynum[8];
|
||||
uint8_t sig[64];
|
||||
} gl_sign_sig_t;
|
||||
|
||||
int gl_sign_read_pubkey(const char *path, gl_sign_pubkey_t *out);
|
||||
int gl_sign_check_seckey(const char *path);
|
||||
int gl_sign_read_sig(const char *path, gl_sign_sig_t *out);
|
||||
int gl_sign_verify(const gl_sign_pubkey_t *pub, const gl_sign_sig_t *sig,
|
||||
const uint8_t *msg, size_t len);
|
||||
int gl_sign_verify_file(const char *pubkey_path, const char *msg_path,
|
||||
const char *sig_path);
|
||||
int gl_sign_file(const char *seckey_path, const char *msg_path,
|
||||
const char *sig_path);
|
||||
int gl_sign_generate(const char *pubkey_path, const char *seckey_path,
|
||||
const char *comment);
|
||||
]]
|
||||
|
||||
local M = { ffi = ffi }
|
||||
|
||||
M.SCOPE_USR = 0
|
||||
M.SCOPE_SYS = 1
|
||||
|
||||
local TXN_NAMES = {
|
||||
[0] = "GL_TXN_OK", [1] = "GL_TXN_ERR_LOCK", [2] = "GL_TXN_ERR_MKDIR",
|
||||
[3] = "GL_TXN_ERR_PERM", [4] = "GL_TXN_ERR_RENAME", [5] = "GL_TXN_ERR_SYNC",
|
||||
[6] = "GL_TXN_ERR_CLEANUP", [7] = "GL_TXN_ERR_NO_TXN", [8] = "GL_TXN_ERR_XDEV",
|
||||
[9] = "GL_TXN_ERR_DEPS",
|
||||
}
|
||||
local INSTALL_NAMES = {
|
||||
[0] = "GL_INSTALL_OK", [1] = "GL_INSTALL_ERR_OPEN", [2] = "GL_INSTALL_ERR_MANIFEST",
|
||||
[3] = "GL_INSTALL_ERR_EXTRACT", [4] = "GL_INSTALL_ERR_INDEX",
|
||||
[5] = "GL_INSTALL_ERR_ALREADY_INSTALLED", [6] = "GL_INSTALL_SKIPPED",
|
||||
[7] = "GL_INSTALL_ERR_SCOPE", [8] = "GL_INSTALL_ERR_DEPS",
|
||||
}
|
||||
local REMOVE_NAMES = {
|
||||
[0] = "GL_REMOVE_OK", [1] = "GL_REMOVE_ERR_NOT_FOUND",
|
||||
[2] = "GL_REMOVE_ERR_UNLINK", [3] = "GL_REMOVE_ERR_INDEX",
|
||||
[4] = "GL_REMOVE_ERR_DEPS",
|
||||
}
|
||||
local SIGN_NAMES = {
|
||||
[0] = "GL_SIGN_OK", [1] = "GL_SIGN_ERR_IO", [2] = "GL_SIGN_ERR_FORMAT",
|
||||
[3] = "GL_SIGN_ERR_ENCRYPTED", [4] = "GL_SIGN_ERR_KEYNUM", [5] = "GL_SIGN_ERR_BAD",
|
||||
[6] = "GL_SIGN_ERR_TOOBIG", [7] = "GL_SIGN_ERR_RANDOM", [8] = "GL_SIGN_ERR_EXISTS",
|
||||
[9] = "GL_SIGN_ERR_WRITE",
|
||||
}
|
||||
|
||||
function M.txn_name(v) return TXN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.install_name(v) return INSTALL_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.sign_name(v) return SIGN_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
function M.remove_name(v) return REMOVE_NAMES[tonumber(v)] or ("?" .. tostring(v)) end
|
||||
|
||||
function M.new_context()
|
||||
return ffi.new("gl_context_t")
|
||||
end
|
||||
|
||||
-- Human-readable current errno, for reporting why a libc call (mount,
|
||||
-- etc.) failed rather than just that it did. Must be called immediately
|
||||
-- after the failing call, before any other libc-touching code runs.
|
||||
function M.strerror()
|
||||
local err = ffi.errno()
|
||||
return err .. " (" .. ffi.string(ffi.C.strerror(err)) .. ")"
|
||||
end
|
||||
|
||||
-- This module's own directory on disk, independent of CWD and
|
||||
-- independent of how deeply nested the require()ing script is. Lua's
|
||||
-- debug.getinfo(1, "S").source gives the path this exact file was
|
||||
-- loaded from, prefixed with "@" for a real file — not the path of
|
||||
-- whoever called into it.
|
||||
local function this_dir()
|
||||
local src = debug.getinfo(1, "S").source
|
||||
if src:sub(1, 1) == "@" then src = src:sub(2) end
|
||||
return src:match("(.*/)") or "./"
|
||||
end
|
||||
|
||||
-- Loads the combined library. Resolution order:
|
||||
-- 1. an explicit path argument
|
||||
-- 2. LIBGLACIER_SO env var
|
||||
-- 3. glacier/libglacier-ng.so relative to *this file's own location*
|
||||
-- (i.e. <project_root>/lib/glacier/libglacier-ng.so), not CWD —
|
||||
-- this is what makes M.load() work correctly no matter where the
|
||||
-- calling script was invoked from.
|
||||
function M.load(path)
|
||||
local target = path or os.getenv("LIBGLACIER_SO")
|
||||
or (this_dir() .. "glacier/libglacier-ng.so")
|
||||
local ok, lib = pcall(ffi.load, target)
|
||||
if not ok then
|
||||
io.stderr:write(string.format(
|
||||
"glacier_cdef: could not load %s (%s)\n" ..
|
||||
"Set LIBGLACIER_SO to the full path of the built .so if it isn't at the default location.\n",
|
||||
target, tostring(lib)))
|
||||
os.exit(1)
|
||||
end
|
||||
return lib
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,695 @@
|
||||
-- lib/gpkg_build.lua
|
||||
--
|
||||
-- The package builder (BUILD_DESIGN.txt): turns a recipe from a NAME-src
|
||||
-- repository into a .gpkg, built in gsandbox, which gpkg_common then
|
||||
-- installs like any fetched package. Used only through gpkg_common.
|
||||
--
|
||||
-- Nothing a build produces is trusted: the output is copied privately
|
||||
-- while hashed and its manifest (read by the library) must be exactly
|
||||
-- the one handed in. What IS trusted is what decides the build: the
|
||||
-- bundle (hash from the signed index, then checked by gl_recipe_read),
|
||||
-- each source (the recipe's SHA256), the layers (packages verified
|
||||
-- against their signed indexes) and this file and build-driver.sh.
|
||||
|
||||
local ffi = require("ffi")
|
||||
local bit = require("bit")
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Set by M.init(): FFI handles and gpkg_common's helpers.
|
||||
local log, istore, txn, h
|
||||
|
||||
function M.init(env)
|
||||
log, istore, txn = env.log, env.istore, env.txn
|
||||
h = env
|
||||
end
|
||||
|
||||
local SYSTEM_CONF = "/etc/glacier/glacier.conf"
|
||||
local SYSTEM_CONFIG_DIR = "/etc/glacier/config"
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Small helpers
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function lines(s)
|
||||
local out = {}
|
||||
for l in (s or ""):gmatch("([^\n]*)\n") do table.insert(out, l) end
|
||||
return out
|
||||
end
|
||||
|
||||
local function exists(path)
|
||||
local f = io.open(path, "rb")
|
||||
if f then f:close() return true end
|
||||
local ok = os.execute("test -e " .. h.shell_quote(path))
|
||||
return ok == true or ok == 0
|
||||
end
|
||||
|
||||
-- The exit status of a shell command line.
|
||||
local function status_of(cmd)
|
||||
local p = io.popen(cmd .. "; echo \"rc=$?\"")
|
||||
local out = p:read("*a")
|
||||
p:close()
|
||||
return tonumber(out:match("rc=(%d+)%s*$")) or 255
|
||||
end
|
||||
|
||||
local function sh_ok(cmd)
|
||||
return status_of(cmd .. " >/dev/null 2>&1") == 0
|
||||
end
|
||||
|
||||
-- A libconfig string literal.
|
||||
local function cfg_string(s)
|
||||
return '"' .. s:gsub("\\", "\\\\"):gsub('"', '\\"') .. '"'
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Settings (BUILD_DESIGN.txt section 5)
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- One configuration file's build settings, or nil if it doesn't exist.
|
||||
-- Exits if it is malformed (or, read by syspkg, untrusted).
|
||||
local conf_cache = {}
|
||||
local function read_conf(path, kind, opts)
|
||||
if conf_cache[path] ~= nil then return conf_cache[path] or nil end
|
||||
if not exists(path) then
|
||||
conf_cache[path] = false
|
||||
return nil
|
||||
end
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(path)
|
||||
end
|
||||
local c = ffi.new("gl_build_conf_t")
|
||||
if tonumber(istore.gl_conf_build(path, kind, c)) ~= 0 then
|
||||
log.lg_printf(2, "Malformed %s.", path)
|
||||
h.finish(1)
|
||||
end
|
||||
local function str(v) return v ~= nil and ffi.string(v) or nil end
|
||||
local r = {
|
||||
traits = lines(str(c.traits)), cflags = str(c.cflags),
|
||||
cxxflags = str(c.cxxflags), ldflags = str(c.ldflags),
|
||||
configure = lines(str(c.configure)),
|
||||
jobs = c.jobs > 0 and tonumber(c.jobs) or nil,
|
||||
build_user = str(c.build_user), build_root = lines(str(c.build_root)),
|
||||
path = path,
|
||||
}
|
||||
istore.gl_build_conf_free(c)
|
||||
conf_cache[path] = r
|
||||
return r
|
||||
end
|
||||
|
||||
-- The user's base directory (<base>, beside index and store).
|
||||
local function user_base(opts)
|
||||
return string.format("/glacier/usr/%d", opts.uid)
|
||||
end
|
||||
|
||||
-- The configuration layers for package `name`, lowest first:
|
||||
-- { global = {...}, package = {...} } lists of read_conf results.
|
||||
local function conf_layers(name, opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
return { sys }, read_conf(SYSTEM_CONFIG_DIR .. "/" .. name .. ".conf",
|
||||
ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
local base = user_base(opts) .. "/config"
|
||||
return { sys, read_conf(base .. "/glacier.conf", ffi.C.GL_CONF_USER, opts) },
|
||||
read_conf(base .. "/" .. name .. ".conf", ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
|
||||
-- The system's settings a build can't do without: BUILD_ROOT, and for
|
||||
-- syspkg BUILD_USER (its uid too). Exits saying which is missing.
|
||||
local function system_settings(opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if not sys then
|
||||
log.lg_printf(2, "Cannot read %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
if #sys.build_root == 0 then
|
||||
log.lg_printf(2, "BUILD_ROOT is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local s = { build_root = sys.build_root }
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
if not sys.build_user then
|
||||
log.lg_printf(2, "BUILD_USER is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local p = io.popen("id -u -- " .. h.shell_quote(sys.build_user) .. " 2>/dev/null")
|
||||
local uid = tonumber(p and p:read("*l") or "")
|
||||
if p then p:close() end
|
||||
if not uid then
|
||||
log.lg_printf(2, "BUILD_USER %s in %s is not a user.", sys.build_user,
|
||||
SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
elseif uid == 0 then
|
||||
log.lg_printf(2, "BUILD_USER in %s must not be root.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
s.build_user, s.build_uid = sys.build_user, uid
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
--- The effective traits of building `name`, whose recipe (or -src index
|
||||
--- entry) declares `declared` ("NAME D\n" lines). Returns a table
|
||||
--- { on = { trait = bool }, list = sorted { "ssl", "-ldap" }, show =
|
||||
--- "[+ssl -ldap]" (or "" with no traits) }. Exits if a per-package file
|
||||
--- names a trait the recipe doesn't declare.
|
||||
function M.traits(name, declared, opts)
|
||||
local on, names = {}, {}
|
||||
for _, l in ipairs(lines(declared)) do
|
||||
local t, d = l:match("^(%S+) ([01])$")
|
||||
on[t] = d == "1"
|
||||
table.insert(names, t)
|
||||
end
|
||||
local function apply(conf, strict)
|
||||
if not conf then return end
|
||||
for _, s in ipairs(conf.traits) do
|
||||
local neg = s:sub(1, 1) == "-"
|
||||
local t = neg and s:sub(2) or s
|
||||
if on[t] ~= nil then
|
||||
on[t] = not neg
|
||||
elseif strict then
|
||||
log.lg_printf(2, "%s: %s has no trait %s.", conf.path, name, t)
|
||||
h.finish(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
for _, g in ipairs(globals) do apply(g, false) end
|
||||
apply(pkg, true)
|
||||
|
||||
table.sort(names)
|
||||
local list, show = {}, {}
|
||||
for _, t in ipairs(names) do
|
||||
table.insert(list, on[t] and t or ("-" .. t))
|
||||
table.insert(show, (on[t] and "+" or "-") .. t)
|
||||
end
|
||||
return { on = on, list = list,
|
||||
show = #show > 0 and ("[" .. table.concat(show, " ") .. "]") or "" }
|
||||
end
|
||||
|
||||
--- Relation lines ("NAME VER[ IF]\n") with their IF resolved against
|
||||
--- traits `tr`: "NAME VER\n" for each that applies.
|
||||
function M.effective(rel, tr)
|
||||
local out = {}
|
||||
for _, l in ipairs(lines(rel)) do
|
||||
local n, v, c = l:match("^(%S+) (%S+) ?(%S*)$")
|
||||
local applies = c == "" or
|
||||
(c:sub(1, 1) == "-" and not tr.on[c:sub(2)]) or
|
||||
(c:sub(1, 1) ~= "-" and tr.on[c])
|
||||
if applies then table.insert(out, n .. " " .. v .. "\n") end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
--- Check, before the prompt, everything a build of `name` needs from
|
||||
--- the configuration (exits on a problem): BUILD_ROOT, BUILD_USER.
|
||||
function M.check(opts)
|
||||
if opts.sysroot then
|
||||
log.lg_printf(2, "Building from source under -R is not supported.")
|
||||
h.finish(1)
|
||||
end
|
||||
system_settings(opts)
|
||||
-- gsandbox and the driver decide what a build produces, which
|
||||
-- root then installs: checked like syspkg's own files, here
|
||||
-- rather than at startup, since only builds need them.
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(h.gsandbox_path)
|
||||
h.require_trusted(h.lib_dir .. "/build-driver.sh")
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- The build
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- <base>/build/NAME, made and checked by the library; owned by the
|
||||
-- builder (`owner` = BUILD_USER's uid, syspkg) or the scope.
|
||||
local function build_dir(ctx, name, owner)
|
||||
local out = ffi.new("char[4096]")
|
||||
if txn.gl_scope_build_dir(ctx, name, owner or -1, out) ~= 0 then
|
||||
h.finish(1) -- the library said why
|
||||
end
|
||||
return ffi.string(out)
|
||||
end
|
||||
|
||||
-- Remove a build's directory tree (the builder's files may be
|
||||
-- read-only to it: overlayfs's work dir is mode 000).
|
||||
local function remove_tree(path)
|
||||
os.execute("chmod -R u+rwX " .. h.shell_quote(path) .. " 2>/dev/null; rm -rf "
|
||||
.. h.shell_quote(path))
|
||||
end
|
||||
|
||||
-- Copy a file (cp, then the given mode).
|
||||
local function copy_file(src, dst, mode)
|
||||
return sh_ok("cp -- " .. h.shell_quote(src) .. " " .. h.shell_quote(dst))
|
||||
and sh_ok("chmod " .. mode .. " " .. h.shell_quote(dst))
|
||||
end
|
||||
|
||||
-- Fetch a source URL (https through curl, file:// a copy) into dest
|
||||
-- hashed: true if it arrived and has hash `hex`. Redirects are
|
||||
-- followed, https only (release tarballs are mostly served that way);
|
||||
-- the hash decides.
|
||||
local MAX_SOURCE = 4 * 1024 * 1024 * 1024
|
||||
local function fetch_source(url, dest, hex)
|
||||
local tmp = dest .. ".part"
|
||||
os.remove(tmp)
|
||||
local cmd
|
||||
local path = url:match("^file://(/.*)$")
|
||||
if path then
|
||||
cmd = "cp -- " .. h.shell_quote(path) .. " " .. h.shell_quote(tmp)
|
||||
else
|
||||
cmd = "curl -sfL --max-redirs 5 --proto =https --proto-redir =https --connect-timeout 30" ..
|
||||
" --max-filesize " .. MAX_SOURCE .. " -o " .. h.shell_quote(tmp) ..
|
||||
" -- " .. h.shell_quote(url)
|
||||
end
|
||||
if not sh_ok(cmd) then
|
||||
os.remove(tmp)
|
||||
return false, false
|
||||
end
|
||||
if h.sha256_file(tmp) ~= hex then
|
||||
os.remove(tmp)
|
||||
return false, true
|
||||
end
|
||||
return os.rename(tmp, dest) ~= nil
|
||||
end
|
||||
|
||||
-- The libconfig manifest a build must produce.
|
||||
local function manifest_text(r, deps, confs, tr)
|
||||
local function rel(text)
|
||||
local out = {}
|
||||
for n, v in text:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(out, string.format('{ PKG_NAME = "%s"; PKG_VER = "%s"; }', n, v))
|
||||
end
|
||||
return "(" .. table.concat(out, ", ") .. ")"
|
||||
end
|
||||
local t = {}
|
||||
for _, s in ipairs(tr.list) do table.insert(t, '"' .. s .. '"') end
|
||||
return table.concat({
|
||||
"PKG_NAME = " .. cfg_string(r.name) .. ";",
|
||||
"PKG_VER = " .. cfg_string(r.ver) .. ";",
|
||||
"PKG_DESC = " .. cfg_string(r.desc) .. ";",
|
||||
"PKG_MAINT = " .. cfg_string(r.maint) .. ";",
|
||||
"PKG_COPYRIGHT = " .. cfg_string(r.copyright) .. ";",
|
||||
"PKG_REPO = " .. cfg_string(r.repo) .. ";",
|
||||
"PKG_DEPS = " .. rel(deps) .. ";",
|
||||
"PKG_CONFS = " .. rel(confs) .. ";",
|
||||
"PKG_TRAITS = [ " .. table.concat(t, ", ") .. " ];",
|
||||
}, "\n") .. "\n"
|
||||
end
|
||||
|
||||
-- The driver's env file: shell assignments, every value quoted.
|
||||
local function env_text(r, settings, tr, sources, prefix)
|
||||
local q = h.shell_quote
|
||||
local args = {}
|
||||
for _, a in ipairs(lines(r.configure)) do table.insert(args, q(a)) end
|
||||
for _, l in ipairs(lines(r.trait_args)) do
|
||||
local t, side, a = l:match("^([^\t]+)\t([+-])\t(.*)$")
|
||||
if (side == "+") == (tr.on[t] == true) then table.insert(args, q(a)) end
|
||||
end
|
||||
for _, a in ipairs(settings.configure) do table.insert(args, q(a)) end
|
||||
local on = {}
|
||||
for _, s in ipairs(tr.list) do
|
||||
if s:sub(1, 1) ~= "-" then table.insert(on, s) end
|
||||
end
|
||||
local function kv(k, v) return k .. "=" .. q(v or "") .. "\n" end
|
||||
return kv("NAME", r.name) .. kv("VER", r.ver) .. kv("BUILD", r.build) ..
|
||||
kv("PREFIX", prefix) ..
|
||||
kv("SOURCES", table.concat(sources, " ")) ..
|
||||
kv("PATCHES", table.concat(lines(r.patches), " ")) ..
|
||||
kv("CONFIGURE_ARGS", table.concat(args, " ")) ..
|
||||
kv("TRAITS", table.concat(on, " ")) ..
|
||||
kv("JOBS", tostring(settings.jobs)) ..
|
||||
kv("CFLAGS", settings.cflags) .. kv("CXXFLAGS", settings.cxxflags) ..
|
||||
kv("LDFLAGS", settings.ldflags)
|
||||
end
|
||||
|
||||
-- CFLAGS/CXXFLAGS/LDFLAGS/CONFIGURE/JOBS for package `name`: the
|
||||
-- per-package value replaces the global one; the user's global replaces
|
||||
-- the system's.
|
||||
local function flags_for(name, opts)
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
local s = { configure = pkg and pkg.configure or {}, jobs = 1 }
|
||||
for _, k in ipairs({ "cflags", "cxxflags", "ldflags" }) do
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g[k] then s[k] = g[k] end
|
||||
end
|
||||
if pkg and pkg[k] then s[k] = pkg[k] end
|
||||
end
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g.jobs then s.jobs = g.jobs end
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
-- Whether the private copy `path`'s manifest (read by the library) is
|
||||
-- exactly what the build was handed.
|
||||
local function output_matches(path, r, deps, confs, tr)
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if istore.gl_read_pkg_manifest(path, m) ~= 0 then return false end
|
||||
local want_traits = #tr.list > 0 and (table.concat(tr.list, "\n") .. "\n") or ""
|
||||
local ok = ffi.string(m.pkg_name) == r.name
|
||||
and ffi.string(m.pkg_repo) == r.repo
|
||||
and string.format("%d.%d.%d", m.pkg_ver.maj, m.pkg_ver.min, m.pkg_ver.pat) == r.ver
|
||||
and h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") == deps
|
||||
and h.relation_lines(m.pkg_conflicts, "conf_name", "conf_ver") == confs
|
||||
and m.pkg_traits ~= nil and ffi.string(m.pkg_traits) == want_traits
|
||||
istore.gl_free_pkg(m)
|
||||
return ok
|
||||
end
|
||||
|
||||
-- The recipe of a verified bundle copy, as a table, or nil and why.
|
||||
local function read_recipe(path)
|
||||
local r = ffi.new("gl_recipe_t")
|
||||
if tonumber(istore.gl_recipe_read(path, r)) ~= 0 then
|
||||
return nil, ffi.string(istore.gl_recipe_error())
|
||||
end
|
||||
local t = {}
|
||||
for _, k in ipairs({ "name", "ver", "repo", "desc", "maint", "copyright",
|
||||
"sources", "patches", "build", "configure", "traits", "trait_args",
|
||||
"deps", "build_deps", "confs" }) do
|
||||
t[k] = ffi.string(r[k])
|
||||
end
|
||||
istore.gl_recipe_free(r)
|
||||
return t
|
||||
end
|
||||
|
||||
-- The packages a build stacks as layers: BUILD_ROOT, the effective
|
||||
-- BUILD_DEPS and DEPS, and everything they depend on, each from
|
||||
-- `built` (this run's own builds), `kept` (installed source builds,
|
||||
-- M.kept) or the binary repositories, in that order, at a high enough
|
||||
-- version. A list of { name, path, sha (hex) }, top first. Exits if
|
||||
-- one can't be had.
|
||||
local function layer_plan(id, root_names, build_deps, deps, indexes, built, kept)
|
||||
local plan, seen = {}, {}
|
||||
local queue = {}
|
||||
for _, rel in ipairs({ deps, build_deps }) do
|
||||
for n, v in rel:gmatch("(%S+) (%S+)\n") do table.insert(queue, { n, v }) end
|
||||
end
|
||||
for _, n in ipairs(root_names) do table.insert(queue, { n, "0.0.0" }) end
|
||||
local k = 1
|
||||
while k <= #queue do
|
||||
local n, v = queue[k][1], queue[k][2]
|
||||
k = k + 1
|
||||
if not seen[n] then
|
||||
seen[n] = true
|
||||
local b = built[n]
|
||||
if not (b and h.vercmp(b.ver, v) >= 0) then b = kept[n] end
|
||||
if b and b.path and not b.sha then b.sha = h.sha256_file(b.path) or false end
|
||||
if b and b.sha ~= false and h.vercmp(b.ver, v) >= 0 then
|
||||
table.insert(plan, { name = n, ver = b.ver, path = b.path, sha = b.sha })
|
||||
for dn, dv in b.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
else
|
||||
-- indexes.layers: base too, even for gpkg
|
||||
local r, e = h.pick_binary(indexes.layers or indexes, n)
|
||||
if not e or h.vercmp(e.ver, v) < 0 then
|
||||
log.lg_printf(2, "%s needs %s %s or later to build, and no binary repository has it.",
|
||||
id, n, v)
|
||||
h.finish(1)
|
||||
end
|
||||
table.insert(plan, { name = n, ver = e.ver, repo = r, entry = e,
|
||||
sha = e.sha256:match("^sha256:(%x+)$") })
|
||||
for dn, dv in e.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return plan
|
||||
end
|
||||
|
||||
--- Check, before the prompt, that every layer a build of plan item `it`
|
||||
--- needs can be had (exits otherwise). `planned` maps the names this
|
||||
--- run builds to { ver, deps }.
|
||||
function M.check_layers(it, indexes, planned, opts)
|
||||
local settings = system_settings(opts)
|
||||
local id = string.format("%s::%s %s", it.repo.name, it.entry.name, it.entry.ver)
|
||||
layer_plan(id, settings.build_root, M.effective(it.entry.build_deps, it.traits),
|
||||
it.deps, indexes, planned, M.kept(opts))
|
||||
end
|
||||
|
||||
--- Build plan item `it` (a -src repository's entry, with it.traits from
|
||||
--- M.traits) into a verified private copy of its .gpkg. `built` maps
|
||||
--- names this run has already built to { ver, path, sha }, for layers.
|
||||
--- Returns the copy and its sidecar text; exits on any failure.
|
||||
function M.build(it, indexes, built, opts)
|
||||
local e, r_src = it.entry, it.repo
|
||||
local id = string.format("%s::%s %s", r_src.name, e.name, e.ver)
|
||||
local settings = system_settings(opts)
|
||||
local owner = settings.build_uid
|
||||
local ctx = h.live_context(opts)
|
||||
local file_mode = opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600"
|
||||
|
||||
-- The bundle: downloaded and hashed against the signed index,
|
||||
-- copied privately, then read and checked by the library.
|
||||
local cached, wrong = h.download(ctx, r_src, e,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
local copy = cached and h.verify_and_copy(cached, e.sha256 .. "\n")
|
||||
local r, bad = nil, nil
|
||||
if copy then r, bad = read_recipe(copy) end
|
||||
if not cached and not wrong then
|
||||
log.lg_printf(2, "Cannot fetch %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
if bad then
|
||||
log.lg_printf(2, "%s has a malformed recipe bundle: %s.", id, bad)
|
||||
if cached then os.remove(cached) end
|
||||
h.finish(1)
|
||||
end
|
||||
if not r or r.name ~= e.name or r.ver ~= e.ver or r.repo .. "-src" ~= r_src.name
|
||||
or r.deps ~= e.deps or r.confs ~= e.confs or r.build_deps ~= e.build_deps
|
||||
or r.traits ~= e.traits then
|
||||
log.lg_printf(2, "%s does not match its index.", id)
|
||||
if cached then os.remove(cached) end
|
||||
h.finish(1)
|
||||
end
|
||||
if cached then os.remove(cached) end
|
||||
|
||||
local tr = it.traits
|
||||
local deps, confs = M.effective(r.deps, tr), M.effective(r.confs, tr)
|
||||
local build_deps = M.effective(r.build_deps, tr)
|
||||
local flags = flags_for(r.name, opts)
|
||||
|
||||
-- The prefix the package is built for: where its usr/ will be
|
||||
-- linked, so it finds its own files at run time. /usr for base,
|
||||
-- else the scope's links (no -R here: these are runtime paths).
|
||||
local prefix = r.repo == "base" and "/usr" or
|
||||
(ffi.string(ctx.links_path):gsub("/+$", ""))
|
||||
|
||||
-- Sources, by hash, in <base>/build/sources.
|
||||
local sources_dir = build_dir(ctx, "sources")
|
||||
local sources = {}
|
||||
for url, hex in r.sources:gmatch("(%S+) sha256:(%x+)\n") do
|
||||
local file = url:match("([^/]+)$")
|
||||
local dest = sources_dir .. "/" .. hex
|
||||
if h.sha256_file(dest) ~= hex then
|
||||
local ok, mismatch = fetch_source(url, dest, hex)
|
||||
if not ok then
|
||||
log.lg_printf(2, mismatch and "A source of %s does not match its hash."
|
||||
or "Cannot fetch the sources of %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(dest))
|
||||
end
|
||||
table.insert(sources, { file = file, path = dest })
|
||||
end
|
||||
|
||||
-- This build's input (the scope's) and work/output (the builder's).
|
||||
local tag = string.format("%s-%s-%d", r.name, r.ver, ffi.C.gpkg_build_getpid())
|
||||
local in_dir = build_dir(ctx, "in-" .. tag)
|
||||
local work = build_dir(ctx, "work-" .. tag, owner)
|
||||
local out = build_dir(ctx, "out-" .. tag, owner)
|
||||
local function cleanup()
|
||||
remove_tree(in_dir)
|
||||
remove_tree(work)
|
||||
remove_tree(out)
|
||||
end
|
||||
local function fail(fmt, ...)
|
||||
log.lg_printf(2, fmt, ...)
|
||||
cleanup()
|
||||
h.finish(1)
|
||||
end
|
||||
|
||||
-- Layers, extracted once each into <base>/build/layers by the
|
||||
-- builder: NAME-VER-<first 8 of its hash>.
|
||||
local layers_dir = build_dir(ctx, "layers", owner)
|
||||
local layer_names = {}
|
||||
local user_flag = settings.build_user and (" -u " .. h.shell_quote(settings.build_user)) or ""
|
||||
for _, l in ipairs(layer_plan(id, settings.build_root, build_deps, deps, indexes, built,
|
||||
M.kept(opts))) do
|
||||
local lname = string.format("%s-%s-%s", l.name, l.ver, l.sha:sub(1, 8))
|
||||
if not exists(layers_dir .. "/" .. lname) then
|
||||
local path = l.path
|
||||
if l.entry then
|
||||
path = h.download(ctx, l.repo, l.entry,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
if not path then fail("Cannot fetch %s::%s %s.", l.repo.name, l.name, l.ver) end
|
||||
else
|
||||
-- A package this run built: its private copy,
|
||||
-- copied where the builder can read it.
|
||||
local p = in_dir .. "/" .. lname .. ".gpkg"
|
||||
if not copy_file(l.path, p, "644") then
|
||||
fail("Cannot write %s.", p)
|
||||
end
|
||||
path = p
|
||||
end
|
||||
local tmp = string.format(".%s.%d", lname, ffi.C.gpkg_build_getpid())
|
||||
if not sh_ok(h.gsandbox .. " -X" .. user_flag .. " -p " .. h.shell_quote(path) ..
|
||||
" -d " .. h.shell_quote(layers_dir .. "/" .. tmp)) then
|
||||
fail("Cannot extract %s %s for %s.", l.name, l.ver, id)
|
||||
end
|
||||
if not os.rename(layers_dir .. "/" .. tmp, layers_dir .. "/" .. lname) then
|
||||
remove_tree(layers_dir .. "/" .. tmp)
|
||||
end
|
||||
end
|
||||
table.insert(layer_names, lname)
|
||||
end
|
||||
|
||||
-- The input: bundle, sources, manifest, env, driver.
|
||||
local function put(name, text)
|
||||
local f = io.open(in_dir .. "/" .. name, "wb")
|
||||
if not f or not f:write(text) then fail("Cannot write %s/%s.", in_dir, name) end
|
||||
f:close()
|
||||
if not sh_ok("chmod 644 " .. h.shell_quote(in_dir .. "/" .. name)) then
|
||||
fail("Cannot write %s/%s.", in_dir, name)
|
||||
end
|
||||
end
|
||||
if not copy_file(copy, in_dir .. "/bundle.grcp", "644") or
|
||||
not sh_ok("mkdir -m 755 " .. h.shell_quote(in_dir .. "/sources")) then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
local files = {}
|
||||
for _, s in ipairs(sources) do
|
||||
if not copy_file(s.path, in_dir .. "/sources/" .. s.file, "644") then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
table.insert(files, s.file)
|
||||
end
|
||||
put("manifest.gpm.cfg", manifest_text(r, deps, confs, tr))
|
||||
put("env", env_text(r, flags, tr, files, prefix))
|
||||
local df = io.open(h.lib_dir .. "/build-driver.sh", "rb")
|
||||
local driver = df and df:read("*a")
|
||||
if df then df:close() end
|
||||
if not driver then fail("Cannot read %s/build-driver.sh.", h.lib_dir) end
|
||||
put("driver", driver)
|
||||
|
||||
-- The build. Its output goes to <base>/build/logs/NAME-VER.log,
|
||||
-- and to the terminal unless -q.
|
||||
local log_path = build_dir(ctx, "logs") .. "/" .. r.name .. "-" .. r.ver .. ".log"
|
||||
local cmd = h.gsandbox .. " -R" .. user_flag .. " -L " .. h.shell_quote(layers_dir)
|
||||
for _, l in ipairs(layer_names) do cmd = cmd .. " -l " .. h.shell_quote(l) end
|
||||
cmd = cmd .. " -w " .. h.shell_quote(work) .. " -i " .. h.shell_quote(in_dir) ..
|
||||
" -o " .. h.shell_quote(out)
|
||||
log.lg_printf(0, "Building %s%s...", id, tr.show ~= "" and (" " .. tr.show) or "")
|
||||
local st
|
||||
if not h.quiet() then
|
||||
local stf = in_dir .. "/.status"
|
||||
os.execute("{ " .. cmd .. " 2>&1; echo $? > " .. h.shell_quote(stf) .. "; } | tee "
|
||||
.. h.shell_quote(log_path))
|
||||
local f = io.open(stf, "r")
|
||||
st = f and tonumber(f:read("*l")) or 255
|
||||
if f then f:close() end
|
||||
else
|
||||
st = status_of(cmd .. " > " .. h.shell_quote(log_path) .. " 2>&1")
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(log_path))
|
||||
if st ~= 0 then
|
||||
fail("Building %s failed. Log: %s", id, log_path)
|
||||
end
|
||||
|
||||
-- The output, untrusted: copied privately while hashed, then its
|
||||
-- manifest must be exactly the one handed in.
|
||||
local result = out .. "/" .. r.name .. "-" .. r.ver .. ".gpkg"
|
||||
local hex = h.sha256_file(result)
|
||||
local pkg = hex and h.verify_and_copy(result, "sha256:" .. hex .. "\n")
|
||||
if not pkg or not output_matches(pkg, r, deps, confs, tr) then
|
||||
fail("%s: the build produced another package.", id)
|
||||
end
|
||||
cleanup()
|
||||
log.lg_printf(3, "Built %s.", id)
|
||||
return pkg, "sha256:" .. hex .. "\n", hex
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Kept packages: the verified .gpkg of each installed source build,
|
||||
-- the bytes that were installed, so a later build can use it as a
|
||||
-- layer (no binary repository has it). <base>/build/packages/
|
||||
-- REPO@NAME-VER.gpkg ('@' is in no name), kept while that version is
|
||||
-- installed and built from source.
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function kept_name(repo, name, ver)
|
||||
return repo .. "@" .. name .. "-" .. ver .. ".gpkg"
|
||||
end
|
||||
|
||||
-- <base>/build/packages, or nil if there is none (not made here).
|
||||
local function kept_dir(ctx)
|
||||
local d = ffi.string(ctx.store_path):gsub("/store$", "") .. "/build/packages"
|
||||
return exists(d) and d or nil
|
||||
end
|
||||
|
||||
-- The kept packages of the installed source builds: the file names a
|
||||
-- prune leaves, and (for layer_plan) name -> { ver, path, deps }.
|
||||
local function kept_scan(opts)
|
||||
local ctx = h.live_context(opts)
|
||||
local dir = kept_dir(ctx)
|
||||
if not dir then return nil, {}, {} end
|
||||
local want, by_name = {}, {}
|
||||
for _, i in ipairs(h.installed_entries(opts)) do
|
||||
if h.built_from(ctx, i.repo, i.name) then
|
||||
local f = kept_name(i.repo, i.name, i.ver)
|
||||
local path = dir .. "/" .. f
|
||||
want[f] = true
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if exists(path) and istore.gl_read_pkg_manifest(path, m) == 0 then
|
||||
by_name[i.name] = { ver = i.ver, path = path,
|
||||
deps = h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") }
|
||||
istore.gl_free_pkg(m)
|
||||
end
|
||||
end
|
||||
end
|
||||
return dir, want, by_name
|
||||
end
|
||||
|
||||
--- The installed source builds a build can use as layers: name ->
|
||||
--- { ver, path, deps }.
|
||||
function M.kept(opts)
|
||||
local _, _, by_name = kept_scan(opts)
|
||||
return by_name
|
||||
end
|
||||
|
||||
--- After a commit: keep the private copy `path` of a package this run
|
||||
--- built and installed (repo::name ver). A failure only warns: the
|
||||
--- package is installed, and only later builds would miss it.
|
||||
function M.keep(opts, path, repo, name, ver)
|
||||
local ctx = h.live_context(opts)
|
||||
local out = ffi.new("char[4096]")
|
||||
local ok = txn.gl_scope_build_dir(ctx, "packages", -1, out) == 0
|
||||
local dest = ok and (ffi.string(out) .. "/" .. kept_name(repo, name, ver))
|
||||
if not ok or not copy_file(path, dest .. ".part",
|
||||
opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600")
|
||||
or not os.rename(dest .. ".part", dest) then
|
||||
if dest then os.remove(dest .. ".part") end
|
||||
log.lg_printf(1, "Could not keep %s::%s %s for later builds.", repo, name, ver)
|
||||
end
|
||||
end
|
||||
|
||||
--- After a commit: delete the kept packages that are no longer an
|
||||
--- installed source build (removed, replaced, or reinstalled from a
|
||||
--- binary repository).
|
||||
function M.prune_kept(opts)
|
||||
local dir, want = kept_scan(opts)
|
||||
if not dir then return end
|
||||
local p = io.popen("ls -A -- " .. h.shell_quote(dir))
|
||||
for f in p:lines() do
|
||||
if not want[f] then os.remove(dir .. "/" .. f) end
|
||||
end
|
||||
p:close()
|
||||
end
|
||||
|
||||
ffi.cdef[[
|
||||
int gpkg_build_getpid(void) asm("getpid");
|
||||
]]
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,593 @@
|
||||
-- lib/gpkg_build.lua
|
||||
--
|
||||
-- The package builder (BUILD_DESIGN.txt): turns a recipe from a NAME-src
|
||||
-- repository into a .gpkg, built in gsandbox, which gpkg_common then
|
||||
-- installs like any fetched package. Used only through gpkg_common.
|
||||
--
|
||||
-- Nothing a build produces is trusted: the output is copied privately
|
||||
-- while hashed and its manifest (read by the library) must be exactly
|
||||
-- the one handed in. What IS trusted is what decides the build: the
|
||||
-- bundle (hash from the signed index, then checked by gl_recipe_read),
|
||||
-- each source (the recipe's SHA256), the layers (packages verified
|
||||
-- against their signed indexes) and this file and build-driver.sh.
|
||||
|
||||
local ffi = require("ffi")
|
||||
local bit = require("bit")
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Set by M.init(): FFI handles and gpkg_common's helpers.
|
||||
local log, istore, txn, h
|
||||
|
||||
function M.init(env)
|
||||
log, istore, txn = env.log, env.istore, env.txn
|
||||
h = env
|
||||
end
|
||||
|
||||
local SYSTEM_CONF = "/etc/glacier/glacier.conf"
|
||||
local SYSTEM_CONFIG_DIR = "/etc/glacier/config"
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Small helpers
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function lines(s)
|
||||
local out = {}
|
||||
for l in (s or ""):gmatch("([^\n]*)\n") do table.insert(out, l) end
|
||||
return out
|
||||
end
|
||||
|
||||
local function exists(path)
|
||||
local f = io.open(path, "rb")
|
||||
if f then f:close() return true end
|
||||
local ok = os.execute("test -e " .. h.shell_quote(path))
|
||||
return ok == true or ok == 0
|
||||
end
|
||||
|
||||
-- The exit status of a shell command line.
|
||||
local function status_of(cmd)
|
||||
local p = io.popen(cmd .. "; echo \"rc=$?\"")
|
||||
local out = p:read("*a")
|
||||
p:close()
|
||||
return tonumber(out:match("rc=(%d+)%s*$")) or 255
|
||||
end
|
||||
|
||||
local function sh_ok(cmd)
|
||||
return status_of(cmd .. " >/dev/null 2>&1") == 0
|
||||
end
|
||||
|
||||
-- A libconfig string literal.
|
||||
local function cfg_string(s)
|
||||
return '"' .. s:gsub("\\", "\\\\"):gsub('"', '\\"') .. '"'
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Settings (BUILD_DESIGN.txt section 5)
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- One configuration file's build settings, or nil if it doesn't exist.
|
||||
-- Exits if it is malformed (or, read by syspkg, untrusted).
|
||||
local conf_cache = {}
|
||||
local function read_conf(path, kind, opts)
|
||||
if conf_cache[path] ~= nil then return conf_cache[path] or nil end
|
||||
if not exists(path) then
|
||||
conf_cache[path] = false
|
||||
return nil
|
||||
end
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(path)
|
||||
end
|
||||
local c = ffi.new("gl_build_conf_t")
|
||||
if tonumber(istore.gl_conf_build(path, kind, c)) ~= 0 then
|
||||
log.lg_printf(2, "Malformed %s.", path)
|
||||
h.finish(1)
|
||||
end
|
||||
local function str(v) return v ~= nil and ffi.string(v) or nil end
|
||||
local r = {
|
||||
traits = lines(str(c.traits)), cflags = str(c.cflags),
|
||||
cxxflags = str(c.cxxflags), ldflags = str(c.ldflags),
|
||||
configure = lines(str(c.configure)),
|
||||
jobs = c.jobs > 0 and tonumber(c.jobs) or nil,
|
||||
build_user = str(c.build_user), build_root = lines(str(c.build_root)),
|
||||
path = path,
|
||||
}
|
||||
istore.gl_build_conf_free(c)
|
||||
conf_cache[path] = r
|
||||
return r
|
||||
end
|
||||
|
||||
-- The user's base directory (<base>, beside index and store).
|
||||
local function user_base(opts)
|
||||
return string.format("/glacier/usr/%d", opts.uid)
|
||||
end
|
||||
|
||||
-- The configuration layers for package `name`, lowest first:
|
||||
-- { global = {...}, package = {...} } lists of read_conf results.
|
||||
local function conf_layers(name, opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
return { sys }, read_conf(SYSTEM_CONFIG_DIR .. "/" .. name .. ".conf",
|
||||
ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
local base = user_base(opts) .. "/config"
|
||||
return { sys, read_conf(base .. "/glacier.conf", ffi.C.GL_CONF_USER, opts) },
|
||||
read_conf(base .. "/" .. name .. ".conf", ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
|
||||
-- The system's settings a build can't do without: BUILD_ROOT, and for
|
||||
-- syspkg BUILD_USER (its uid too). Exits saying which is missing.
|
||||
local function system_settings(opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if not sys then
|
||||
log.lg_printf(2, "Cannot read %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
if #sys.build_root == 0 then
|
||||
log.lg_printf(2, "BUILD_ROOT is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local s = { build_root = sys.build_root }
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
if not sys.build_user then
|
||||
log.lg_printf(2, "BUILD_USER is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local p = io.popen("id -u -- " .. h.shell_quote(sys.build_user) .. " 2>/dev/null")
|
||||
local uid = tonumber(p and p:read("*l") or "")
|
||||
if p then p:close() end
|
||||
if not uid then
|
||||
log.lg_printf(2, "BUILD_USER %s in %s is not a user.", sys.build_user,
|
||||
SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
elseif uid == 0 then
|
||||
log.lg_printf(2, "BUILD_USER in %s must not be root.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
s.build_user, s.build_uid = sys.build_user, uid
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
--- The effective traits of building `name`, whose recipe (or -src index
|
||||
--- entry) declares `declared` ("NAME D\n" lines). Returns a table
|
||||
--- { on = { trait = bool }, list = sorted { "ssl", "-ldap" }, show =
|
||||
--- "[+ssl -ldap]" (or "" with no traits) }. Exits if a per-package file
|
||||
--- names a trait the recipe doesn't declare.
|
||||
function M.traits(name, declared, opts)
|
||||
local on, names = {}, {}
|
||||
for _, l in ipairs(lines(declared)) do
|
||||
local t, d = l:match("^(%S+) ([01])$")
|
||||
on[t] = d == "1"
|
||||
table.insert(names, t)
|
||||
end
|
||||
local function apply(conf, strict)
|
||||
if not conf then return end
|
||||
for _, s in ipairs(conf.traits) do
|
||||
local neg = s:sub(1, 1) == "-"
|
||||
local t = neg and s:sub(2) or s
|
||||
if on[t] ~= nil then
|
||||
on[t] = not neg
|
||||
elseif strict then
|
||||
log.lg_printf(2, "%s: %s has no trait %s.", conf.path, name, t)
|
||||
h.finish(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
for _, g in ipairs(globals) do apply(g, false) end
|
||||
apply(pkg, true)
|
||||
|
||||
table.sort(names)
|
||||
local list, show = {}, {}
|
||||
for _, t in ipairs(names) do
|
||||
table.insert(list, on[t] and t or ("-" .. t))
|
||||
table.insert(show, (on[t] and "+" or "-") .. t)
|
||||
end
|
||||
return { on = on, list = list,
|
||||
show = #show > 0 and ("[" .. table.concat(show, " ") .. "]") or "" }
|
||||
end
|
||||
|
||||
--- Relation lines ("NAME VER[ IF]\n") with their IF resolved against
|
||||
--- traits `tr`: "NAME VER\n" for each that applies.
|
||||
function M.effective(rel, tr)
|
||||
local out = {}
|
||||
for _, l in ipairs(lines(rel)) do
|
||||
local n, v, c = l:match("^(%S+) (%S+) ?(%S*)$")
|
||||
local applies = c == "" or
|
||||
(c:sub(1, 1) == "-" and not tr.on[c:sub(2)]) or
|
||||
(c:sub(1, 1) ~= "-" and tr.on[c])
|
||||
if applies then table.insert(out, n .. " " .. v .. "\n") end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
--- Check, before the prompt, everything a build of `name` needs from
|
||||
--- the configuration (exits on a problem): BUILD_ROOT, BUILD_USER.
|
||||
function M.check(opts)
|
||||
if opts.sysroot then
|
||||
log.lg_printf(2, "Building from source under -R is not supported.")
|
||||
h.finish(1)
|
||||
end
|
||||
system_settings(opts)
|
||||
-- gsandbox and the driver decide what a build produces, which
|
||||
-- root then installs: checked like syspkg's own files, here
|
||||
-- rather than at startup, since only builds need them.
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(h.gsandbox_path)
|
||||
h.require_trusted(h.lib_dir .. "/build-driver.sh")
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- The build
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- <base>/build/NAME, made and checked by the library; owned by the
|
||||
-- builder (`owner` = BUILD_USER's uid, syspkg) or the scope.
|
||||
local function build_dir(ctx, name, owner)
|
||||
local out = ffi.new("char[4096]")
|
||||
if txn.gl_scope_build_dir(ctx, name, owner or -1, out) ~= 0 then
|
||||
h.finish(1) -- the library said why
|
||||
end
|
||||
return ffi.string(out)
|
||||
end
|
||||
|
||||
-- Remove a build's directory tree (the builder's files may be
|
||||
-- read-only to it: overlayfs's work dir is mode 000).
|
||||
local function remove_tree(path)
|
||||
os.execute("chmod -R u+rwX " .. h.shell_quote(path) .. " 2>/dev/null; rm -rf "
|
||||
.. h.shell_quote(path))
|
||||
end
|
||||
|
||||
-- Copy a file (cp, then the given mode).
|
||||
local function copy_file(src, dst, mode)
|
||||
return sh_ok("cp -- " .. h.shell_quote(src) .. " " .. h.shell_quote(dst))
|
||||
and sh_ok("chmod " .. mode .. " " .. h.shell_quote(dst))
|
||||
end
|
||||
|
||||
-- Fetch a source URL (https through curl, file:// a copy) into dest
|
||||
-- hashed: true if it arrived and has hash `hex`.
|
||||
local MAX_SOURCE = 4 * 1024 * 1024 * 1024
|
||||
local function fetch_source(url, dest, hex)
|
||||
local tmp = dest .. ".part"
|
||||
os.remove(tmp)
|
||||
local cmd
|
||||
local path = url:match("^file://(/.*)$")
|
||||
if path then
|
||||
cmd = "cp -- " .. h.shell_quote(path) .. " " .. h.shell_quote(tmp)
|
||||
else
|
||||
cmd = "curl -sf --proto =https --proto-redir =https --connect-timeout 30" ..
|
||||
" --max-filesize " .. MAX_SOURCE .. " -o " .. h.shell_quote(tmp) ..
|
||||
" -- " .. h.shell_quote(url)
|
||||
end
|
||||
if not sh_ok(cmd) then
|
||||
os.remove(tmp)
|
||||
return false, false
|
||||
end
|
||||
if h.sha256_file(tmp) ~= hex then
|
||||
os.remove(tmp)
|
||||
return false, true
|
||||
end
|
||||
return os.rename(tmp, dest) ~= nil
|
||||
end
|
||||
|
||||
-- The libconfig manifest a build must produce.
|
||||
local function manifest_text(r, deps, confs, tr)
|
||||
local function rel(text)
|
||||
local out = {}
|
||||
for n, v in text:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(out, string.format('{ PKG_NAME = "%s"; PKG_VER = "%s"; }', n, v))
|
||||
end
|
||||
return "(" .. table.concat(out, ", ") .. ")"
|
||||
end
|
||||
local t = {}
|
||||
for _, s in ipairs(tr.list) do table.insert(t, '"' .. s .. '"') end
|
||||
return table.concat({
|
||||
"PKG_NAME = " .. cfg_string(r.name) .. ";",
|
||||
"PKG_VER = " .. cfg_string(r.ver) .. ";",
|
||||
"PKG_DESC = " .. cfg_string(r.desc) .. ";",
|
||||
"PKG_MAINT = " .. cfg_string(r.maint) .. ";",
|
||||
"PKG_COPYRIGHT = " .. cfg_string(r.copyright) .. ";",
|
||||
"PKG_REPO = " .. cfg_string(r.repo) .. ";",
|
||||
"PKG_DEPS = " .. rel(deps) .. ";",
|
||||
"PKG_CONFS = " .. rel(confs) .. ";",
|
||||
"PKG_TRAITS = [ " .. table.concat(t, ", ") .. " ];",
|
||||
}, "\n") .. "\n"
|
||||
end
|
||||
|
||||
-- The driver's env file: shell assignments, every value quoted.
|
||||
local function env_text(r, settings, tr, sources)
|
||||
local q = h.shell_quote
|
||||
local args = {}
|
||||
for _, a in ipairs(lines(r.configure)) do table.insert(args, q(a)) end
|
||||
for _, l in ipairs(lines(r.trait_args)) do
|
||||
local t, side, a = l:match("^([^\t]+)\t([+-])\t(.*)$")
|
||||
if (side == "+") == (tr.on[t] == true) then table.insert(args, q(a)) end
|
||||
end
|
||||
for _, a in ipairs(settings.configure) do table.insert(args, q(a)) end
|
||||
local on = {}
|
||||
for _, s in ipairs(tr.list) do
|
||||
if s:sub(1, 1) ~= "-" then table.insert(on, s) end
|
||||
end
|
||||
local function kv(k, v) return k .. "=" .. q(v or "") .. "\n" end
|
||||
return kv("NAME", r.name) .. kv("VER", r.ver) .. kv("BUILD", r.build) ..
|
||||
kv("SOURCES", table.concat(sources, " ")) ..
|
||||
kv("PATCHES", table.concat(lines(r.patches), " ")) ..
|
||||
kv("CONFIGURE_ARGS", table.concat(args, " ")) ..
|
||||
kv("TRAITS", table.concat(on, " ")) ..
|
||||
kv("JOBS", tostring(settings.jobs)) ..
|
||||
kv("CFLAGS", settings.cflags) .. kv("CXXFLAGS", settings.cxxflags) ..
|
||||
kv("LDFLAGS", settings.ldflags)
|
||||
end
|
||||
|
||||
-- CFLAGS/CXXFLAGS/LDFLAGS/CONFIGURE/JOBS for package `name`: the
|
||||
-- per-package value replaces the global one; the user's global replaces
|
||||
-- the system's.
|
||||
local function flags_for(name, opts)
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
local s = { configure = pkg and pkg.configure or {}, jobs = 1 }
|
||||
for _, k in ipairs({ "cflags", "cxxflags", "ldflags" }) do
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g[k] then s[k] = g[k] end
|
||||
end
|
||||
if pkg and pkg[k] then s[k] = pkg[k] end
|
||||
end
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g.jobs then s.jobs = g.jobs end
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
-- Whether the private copy `path`'s manifest (read by the library) is
|
||||
-- exactly what the build was handed.
|
||||
local function output_matches(path, r, deps, confs, tr)
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if istore.gl_read_pkg_manifest(path, m) ~= 0 then return false end
|
||||
local want_traits = #tr.list > 0 and (table.concat(tr.list, "\n") .. "\n") or ""
|
||||
local ok = ffi.string(m.pkg_name) == r.name
|
||||
and ffi.string(m.pkg_repo) == r.repo
|
||||
and string.format("%d.%d.%d", m.pkg_ver.maj, m.pkg_ver.min, m.pkg_ver.pat) == r.ver
|
||||
and h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") == deps
|
||||
and h.relation_lines(m.pkg_conflicts, "conf_name", "conf_ver") == confs
|
||||
and m.pkg_traits ~= nil and ffi.string(m.pkg_traits) == want_traits
|
||||
istore.gl_free_pkg(m)
|
||||
return ok
|
||||
end
|
||||
|
||||
-- The recipe of a verified bundle copy, as a table, or nil.
|
||||
local function read_recipe(path)
|
||||
local r = ffi.new("gl_recipe_t")
|
||||
if tonumber(istore.gl_recipe_read(path, r)) ~= 0 then return nil end
|
||||
local t = {}
|
||||
for _, k in ipairs({ "name", "ver", "repo", "desc", "maint", "copyright",
|
||||
"sources", "patches", "build", "configure", "traits", "trait_args",
|
||||
"deps", "build_deps", "confs" }) do
|
||||
t[k] = ffi.string(r[k])
|
||||
end
|
||||
istore.gl_recipe_free(r)
|
||||
return t
|
||||
end
|
||||
|
||||
-- The packages a build stacks as layers: BUILD_ROOT, the effective
|
||||
-- BUILD_DEPS and DEPS, and everything they depend on, each from the
|
||||
-- binary repositories (or `built`: this run's own builds). A list of
|
||||
-- { name, path, sha (hex) }, top first. Exits if one can't be had.
|
||||
local function layer_plan(id, root_names, build_deps, deps, indexes, built)
|
||||
local plan, seen = {}, {}
|
||||
local queue = {}
|
||||
for _, rel in ipairs({ deps, build_deps }) do
|
||||
for n, v in rel:gmatch("(%S+) (%S+)\n") do table.insert(queue, { n, v }) end
|
||||
end
|
||||
for _, n in ipairs(root_names) do table.insert(queue, { n, "0.0.0" }) end
|
||||
local k = 1
|
||||
while k <= #queue do
|
||||
local n, v = queue[k][1], queue[k][2]
|
||||
k = k + 1
|
||||
if not seen[n] then
|
||||
seen[n] = true
|
||||
local b = built[n]
|
||||
if b and h.vercmp(b.ver, v) >= 0 then
|
||||
table.insert(plan, { name = n, ver = b.ver, path = b.path, sha = b.sha })
|
||||
else
|
||||
local r, e = h.pick_binary(indexes, n)
|
||||
if not e or h.vercmp(e.ver, v) < 0 then
|
||||
log.lg_printf(2, "%s needs %s %s or later to build, and no binary repository has it.",
|
||||
id, n, v)
|
||||
h.finish(1)
|
||||
end
|
||||
table.insert(plan, { name = n, ver = e.ver, repo = r, entry = e,
|
||||
sha = e.sha256:match("^sha256:(%x+)$") })
|
||||
for dn, dv in e.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return plan
|
||||
end
|
||||
|
||||
--- Check, before the prompt, that every layer a build of plan item `it`
|
||||
--- needs can be had (exits otherwise). `planned` maps the names this
|
||||
--- run builds to their versions.
|
||||
function M.check_layers(it, indexes, planned, opts)
|
||||
local settings = system_settings(opts)
|
||||
local built = {}
|
||||
for n, v in pairs(planned) do built[n] = { ver = v } end
|
||||
local id = string.format("%s::%s %s", it.repo.name, it.entry.name, it.entry.ver)
|
||||
layer_plan(id, settings.build_root, M.effective(it.entry.build_deps, it.traits),
|
||||
it.deps, indexes, built)
|
||||
end
|
||||
|
||||
--- Build plan item `it` (a -src repository's entry, with it.traits from
|
||||
--- M.traits) into a verified private copy of its .gpkg. `built` maps
|
||||
--- names this run has already built to { ver, path, sha }, for layers.
|
||||
--- Returns the copy and its sidecar text; exits on any failure.
|
||||
function M.build(it, indexes, built, opts)
|
||||
local e, r_src = it.entry, it.repo
|
||||
local id = string.format("%s::%s %s", r_src.name, e.name, e.ver)
|
||||
local settings = system_settings(opts)
|
||||
local owner = settings.build_uid
|
||||
local ctx = h.live_context(opts)
|
||||
local file_mode = opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600"
|
||||
|
||||
-- The bundle: downloaded and hashed against the signed index,
|
||||
-- copied privately, then read and checked by the library.
|
||||
local cached, wrong = h.download(ctx, r_src, e,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
local copy = cached and h.verify_and_copy(cached, e.sha256 .. "\n")
|
||||
local r = copy and read_recipe(copy)
|
||||
if not cached and not wrong then
|
||||
log.lg_printf(2, "Cannot fetch %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
if not r or r.name ~= e.name or r.ver ~= e.ver or r.repo .. "-src" ~= r_src.name
|
||||
or r.deps ~= e.deps or r.confs ~= e.confs or r.build_deps ~= e.build_deps
|
||||
or r.traits ~= e.traits then
|
||||
log.lg_printf(2, "%s does not match its index.", id)
|
||||
if cached then os.remove(cached) end
|
||||
h.finish(1)
|
||||
end
|
||||
if cached then os.remove(cached) end
|
||||
|
||||
local tr = it.traits
|
||||
local deps, confs = M.effective(r.deps, tr), M.effective(r.confs, tr)
|
||||
local build_deps = M.effective(r.build_deps, tr)
|
||||
local flags = flags_for(r.name, opts)
|
||||
|
||||
-- Sources, by hash, in <base>/build/sources.
|
||||
local sources_dir = build_dir(ctx, "sources")
|
||||
local sources = {}
|
||||
for url, hex in r.sources:gmatch("(%S+) sha256:(%x+)\n") do
|
||||
local file = url:match("([^/]+)$")
|
||||
local dest = sources_dir .. "/" .. hex
|
||||
if h.sha256_file(dest) ~= hex then
|
||||
local ok, mismatch = fetch_source(url, dest, hex)
|
||||
if not ok then
|
||||
log.lg_printf(2, mismatch and "A source of %s does not match its hash."
|
||||
or "Cannot fetch the sources of %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(dest))
|
||||
end
|
||||
table.insert(sources, { file = file, path = dest })
|
||||
end
|
||||
|
||||
-- This build's input (the scope's) and work/output (the builder's).
|
||||
local tag = string.format("%s-%s-%d", r.name, r.ver, ffi.C.gpkg_build_getpid())
|
||||
local in_dir = build_dir(ctx, "in-" .. tag)
|
||||
local work = build_dir(ctx, "work-" .. tag, owner)
|
||||
local out = build_dir(ctx, "out-" .. tag, owner)
|
||||
local function cleanup()
|
||||
remove_tree(in_dir)
|
||||
remove_tree(work)
|
||||
remove_tree(out)
|
||||
end
|
||||
local function fail(fmt, ...)
|
||||
log.lg_printf(2, fmt, ...)
|
||||
cleanup()
|
||||
h.finish(1)
|
||||
end
|
||||
|
||||
-- Layers, extracted once each into <base>/build/layers by the
|
||||
-- builder: NAME-VER-<first 8 of its hash>.
|
||||
local layers_dir = build_dir(ctx, "layers", owner)
|
||||
local layer_names = {}
|
||||
local user_flag = settings.build_user and (" -u " .. h.shell_quote(settings.build_user)) or ""
|
||||
for _, l in ipairs(layer_plan(id, settings.build_root, build_deps, deps, indexes, built)) do
|
||||
local lname = string.format("%s-%s-%s", l.name, l.ver, l.sha:sub(1, 8))
|
||||
if not exists(layers_dir .. "/" .. lname) then
|
||||
local path = l.path
|
||||
if l.entry then
|
||||
path = h.download(ctx, l.repo, l.entry,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
if not path then fail("Cannot fetch %s::%s %s.", l.repo.name, l.name, l.ver) end
|
||||
else
|
||||
-- A package this run built: its private copy,
|
||||
-- copied where the builder can read it.
|
||||
local p = in_dir .. "/" .. lname .. ".gpkg"
|
||||
if not copy_file(l.path, p, "644") then
|
||||
fail("Cannot write %s.", p)
|
||||
end
|
||||
path = p
|
||||
end
|
||||
local tmp = string.format(".%s.%d", lname, ffi.C.gpkg_build_getpid())
|
||||
if not sh_ok(h.gsandbox .. " -X" .. user_flag .. " -p " .. h.shell_quote(path) ..
|
||||
" -d " .. h.shell_quote(layers_dir .. "/" .. tmp)) then
|
||||
fail("Cannot extract %s %s for %s.", l.name, l.ver, id)
|
||||
end
|
||||
if not os.rename(layers_dir .. "/" .. tmp, layers_dir .. "/" .. lname) then
|
||||
remove_tree(layers_dir .. "/" .. tmp)
|
||||
end
|
||||
end
|
||||
table.insert(layer_names, lname)
|
||||
end
|
||||
|
||||
-- The input: bundle, sources, manifest, env, driver.
|
||||
local function put(name, text)
|
||||
local f = io.open(in_dir .. "/" .. name, "wb")
|
||||
if not f or not f:write(text) then fail("Cannot write %s/%s.", in_dir, name) end
|
||||
f:close()
|
||||
if not sh_ok("chmod 644 " .. h.shell_quote(in_dir .. "/" .. name)) then
|
||||
fail("Cannot write %s/%s.", in_dir, name)
|
||||
end
|
||||
end
|
||||
if not copy_file(copy, in_dir .. "/bundle.grcp", "644") or
|
||||
not sh_ok("mkdir -m 755 " .. h.shell_quote(in_dir .. "/sources")) then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
local files = {}
|
||||
for _, s in ipairs(sources) do
|
||||
if not copy_file(s.path, in_dir .. "/sources/" .. s.file, "644") then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
table.insert(files, s.file)
|
||||
end
|
||||
put("manifest.gpm.cfg", manifest_text(r, deps, confs, tr))
|
||||
put("env", env_text(r, flags, tr, files))
|
||||
local df = io.open(h.lib_dir .. "/build-driver.sh", "rb")
|
||||
local driver = df and df:read("*a")
|
||||
if df then df:close() end
|
||||
if not driver then fail("Cannot read %s/build-driver.sh.", h.lib_dir) end
|
||||
put("driver", driver)
|
||||
|
||||
-- The build. Its output goes to <base>/build/logs/NAME-VER.log.
|
||||
local log_path = build_dir(ctx, "logs") .. "/" .. r.name .. "-" .. r.ver .. ".log"
|
||||
local cmd = h.gsandbox .. " -R" .. user_flag .. " -L " .. h.shell_quote(layers_dir)
|
||||
for _, l in ipairs(layer_names) do cmd = cmd .. " -l " .. h.shell_quote(l) end
|
||||
cmd = cmd .. " -w " .. h.shell_quote(work) .. " -i " .. h.shell_quote(in_dir) ..
|
||||
" -o " .. h.shell_quote(out)
|
||||
log.lg_printf(0, "Building %s%s...", id, tr.show ~= "" and (" " .. tr.show) or "")
|
||||
local st
|
||||
if h.verbose() then
|
||||
local stf = in_dir .. "/.status"
|
||||
os.execute("{ " .. cmd .. " 2>&1; echo $? > " .. h.shell_quote(stf) .. "; } | tee "
|
||||
.. h.shell_quote(log_path))
|
||||
local f = io.open(stf, "r")
|
||||
st = f and tonumber(f:read("*l")) or 255
|
||||
if f then f:close() end
|
||||
else
|
||||
st = status_of(cmd .. " > " .. h.shell_quote(log_path) .. " 2>&1")
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(log_path))
|
||||
if st ~= 0 then
|
||||
fail("Building %s failed. Log: %s", id, log_path)
|
||||
end
|
||||
|
||||
-- The output, untrusted: copied privately while hashed, then its
|
||||
-- manifest must be exactly the one handed in.
|
||||
local result = out .. "/" .. r.name .. "-" .. r.ver .. ".gpkg"
|
||||
local hex = h.sha256_file(result)
|
||||
local pkg = hex and h.verify_and_copy(result, "sha256:" .. hex .. "\n")
|
||||
if not pkg or not output_matches(pkg, r, deps, confs, tr) then
|
||||
fail("%s: the build produced another package.", id)
|
||||
end
|
||||
cleanup()
|
||||
log.lg_printf(3, "Built %s.", id)
|
||||
return pkg, "sha256:" .. hex .. "\n", hex
|
||||
end
|
||||
|
||||
ffi.cdef[[
|
||||
int gpkg_build_getpid(void) asm("getpid");
|
||||
]]
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,602 @@
|
||||
-- lib/gpkg_build.lua
|
||||
--
|
||||
-- The package builder (BUILD_DESIGN.txt): turns a recipe from a NAME-src
|
||||
-- repository into a .gpkg, built in gsandbox, which gpkg_common then
|
||||
-- installs like any fetched package. Used only through gpkg_common.
|
||||
--
|
||||
-- Nothing a build produces is trusted: the output is copied privately
|
||||
-- while hashed and its manifest (read by the library) must be exactly
|
||||
-- the one handed in. What IS trusted is what decides the build: the
|
||||
-- bundle (hash from the signed index, then checked by gl_recipe_read),
|
||||
-- each source (the recipe's SHA256), the layers (packages verified
|
||||
-- against their signed indexes) and this file and build-driver.sh.
|
||||
|
||||
local ffi = require("ffi")
|
||||
local bit = require("bit")
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Set by M.init(): FFI handles and gpkg_common's helpers.
|
||||
local log, istore, txn, h
|
||||
|
||||
function M.init(env)
|
||||
log, istore, txn = env.log, env.istore, env.txn
|
||||
h = env
|
||||
end
|
||||
|
||||
local SYSTEM_CONF = "/etc/glacier/glacier.conf"
|
||||
local SYSTEM_CONFIG_DIR = "/etc/glacier/config"
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Small helpers
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function lines(s)
|
||||
local out = {}
|
||||
for l in (s or ""):gmatch("([^\n]*)\n") do table.insert(out, l) end
|
||||
return out
|
||||
end
|
||||
|
||||
local function exists(path)
|
||||
local f = io.open(path, "rb")
|
||||
if f then f:close() return true end
|
||||
local ok = os.execute("test -e " .. h.shell_quote(path))
|
||||
return ok == true or ok == 0
|
||||
end
|
||||
|
||||
-- The exit status of a shell command line.
|
||||
local function status_of(cmd)
|
||||
local p = io.popen(cmd .. "; echo \"rc=$?\"")
|
||||
local out = p:read("*a")
|
||||
p:close()
|
||||
return tonumber(out:match("rc=(%d+)%s*$")) or 255
|
||||
end
|
||||
|
||||
local function sh_ok(cmd)
|
||||
return status_of(cmd .. " >/dev/null 2>&1") == 0
|
||||
end
|
||||
|
||||
-- A libconfig string literal.
|
||||
local function cfg_string(s)
|
||||
return '"' .. s:gsub("\\", "\\\\"):gsub('"', '\\"') .. '"'
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Settings (BUILD_DESIGN.txt section 5)
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- One configuration file's build settings, or nil if it doesn't exist.
|
||||
-- Exits if it is malformed (or, read by syspkg, untrusted).
|
||||
local conf_cache = {}
|
||||
local function read_conf(path, kind, opts)
|
||||
if conf_cache[path] ~= nil then return conf_cache[path] or nil end
|
||||
if not exists(path) then
|
||||
conf_cache[path] = false
|
||||
return nil
|
||||
end
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(path)
|
||||
end
|
||||
local c = ffi.new("gl_build_conf_t")
|
||||
if tonumber(istore.gl_conf_build(path, kind, c)) ~= 0 then
|
||||
log.lg_printf(2, "Malformed %s.", path)
|
||||
h.finish(1)
|
||||
end
|
||||
local function str(v) return v ~= nil and ffi.string(v) or nil end
|
||||
local r = {
|
||||
traits = lines(str(c.traits)), cflags = str(c.cflags),
|
||||
cxxflags = str(c.cxxflags), ldflags = str(c.ldflags),
|
||||
configure = lines(str(c.configure)),
|
||||
jobs = c.jobs > 0 and tonumber(c.jobs) or nil,
|
||||
build_user = str(c.build_user), build_root = lines(str(c.build_root)),
|
||||
path = path,
|
||||
}
|
||||
istore.gl_build_conf_free(c)
|
||||
conf_cache[path] = r
|
||||
return r
|
||||
end
|
||||
|
||||
-- The user's base directory (<base>, beside index and store).
|
||||
local function user_base(opts)
|
||||
return string.format("/glacier/usr/%d", opts.uid)
|
||||
end
|
||||
|
||||
-- The configuration layers for package `name`, lowest first:
|
||||
-- { global = {...}, package = {...} } lists of read_conf results.
|
||||
local function conf_layers(name, opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
return { sys }, read_conf(SYSTEM_CONFIG_DIR .. "/" .. name .. ".conf",
|
||||
ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
local base = user_base(opts) .. "/config"
|
||||
return { sys, read_conf(base .. "/glacier.conf", ffi.C.GL_CONF_USER, opts) },
|
||||
read_conf(base .. "/" .. name .. ".conf", ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
|
||||
-- The system's settings a build can't do without: BUILD_ROOT, and for
|
||||
-- syspkg BUILD_USER (its uid too). Exits saying which is missing.
|
||||
local function system_settings(opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if not sys then
|
||||
log.lg_printf(2, "Cannot read %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
if #sys.build_root == 0 then
|
||||
log.lg_printf(2, "BUILD_ROOT is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local s = { build_root = sys.build_root }
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
if not sys.build_user then
|
||||
log.lg_printf(2, "BUILD_USER is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local p = io.popen("id -u -- " .. h.shell_quote(sys.build_user) .. " 2>/dev/null")
|
||||
local uid = tonumber(p and p:read("*l") or "")
|
||||
if p then p:close() end
|
||||
if not uid then
|
||||
log.lg_printf(2, "BUILD_USER %s in %s is not a user.", sys.build_user,
|
||||
SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
elseif uid == 0 then
|
||||
log.lg_printf(2, "BUILD_USER in %s must not be root.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
s.build_user, s.build_uid = sys.build_user, uid
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
--- The effective traits of building `name`, whose recipe (or -src index
|
||||
--- entry) declares `declared` ("NAME D\n" lines). Returns a table
|
||||
--- { on = { trait = bool }, list = sorted { "ssl", "-ldap" }, show =
|
||||
--- "[+ssl -ldap]" (or "" with no traits) }. Exits if a per-package file
|
||||
--- names a trait the recipe doesn't declare.
|
||||
function M.traits(name, declared, opts)
|
||||
local on, names = {}, {}
|
||||
for _, l in ipairs(lines(declared)) do
|
||||
local t, d = l:match("^(%S+) ([01])$")
|
||||
on[t] = d == "1"
|
||||
table.insert(names, t)
|
||||
end
|
||||
local function apply(conf, strict)
|
||||
if not conf then return end
|
||||
for _, s in ipairs(conf.traits) do
|
||||
local neg = s:sub(1, 1) == "-"
|
||||
local t = neg and s:sub(2) or s
|
||||
if on[t] ~= nil then
|
||||
on[t] = not neg
|
||||
elseif strict then
|
||||
log.lg_printf(2, "%s: %s has no trait %s.", conf.path, name, t)
|
||||
h.finish(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
for _, g in ipairs(globals) do apply(g, false) end
|
||||
apply(pkg, true)
|
||||
|
||||
table.sort(names)
|
||||
local list, show = {}, {}
|
||||
for _, t in ipairs(names) do
|
||||
table.insert(list, on[t] and t or ("-" .. t))
|
||||
table.insert(show, (on[t] and "+" or "-") .. t)
|
||||
end
|
||||
return { on = on, list = list,
|
||||
show = #show > 0 and ("[" .. table.concat(show, " ") .. "]") or "" }
|
||||
end
|
||||
|
||||
--- Relation lines ("NAME VER[ IF]\n") with their IF resolved against
|
||||
--- traits `tr`: "NAME VER\n" for each that applies.
|
||||
function M.effective(rel, tr)
|
||||
local out = {}
|
||||
for _, l in ipairs(lines(rel)) do
|
||||
local n, v, c = l:match("^(%S+) (%S+) ?(%S*)$")
|
||||
local applies = c == "" or
|
||||
(c:sub(1, 1) == "-" and not tr.on[c:sub(2)]) or
|
||||
(c:sub(1, 1) ~= "-" and tr.on[c])
|
||||
if applies then table.insert(out, n .. " " .. v .. "\n") end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
--- Check, before the prompt, everything a build of `name` needs from
|
||||
--- the configuration (exits on a problem): BUILD_ROOT, BUILD_USER.
|
||||
function M.check(opts)
|
||||
if opts.sysroot then
|
||||
log.lg_printf(2, "Building from source under -R is not supported.")
|
||||
h.finish(1)
|
||||
end
|
||||
system_settings(opts)
|
||||
-- gsandbox and the driver decide what a build produces, which
|
||||
-- root then installs: checked like syspkg's own files, here
|
||||
-- rather than at startup, since only builds need them.
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(h.gsandbox_path)
|
||||
h.require_trusted(h.lib_dir .. "/build-driver.sh")
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- The build
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- <base>/build/NAME, made and checked by the library; owned by the
|
||||
-- builder (`owner` = BUILD_USER's uid, syspkg) or the scope.
|
||||
local function build_dir(ctx, name, owner)
|
||||
local out = ffi.new("char[4096]")
|
||||
if txn.gl_scope_build_dir(ctx, name, owner or -1, out) ~= 0 then
|
||||
h.finish(1) -- the library said why
|
||||
end
|
||||
return ffi.string(out)
|
||||
end
|
||||
|
||||
-- Remove a build's directory tree (the builder's files may be
|
||||
-- read-only to it: overlayfs's work dir is mode 000).
|
||||
local function remove_tree(path)
|
||||
os.execute("chmod -R u+rwX " .. h.shell_quote(path) .. " 2>/dev/null; rm -rf "
|
||||
.. h.shell_quote(path))
|
||||
end
|
||||
|
||||
-- Copy a file (cp, then the given mode).
|
||||
local function copy_file(src, dst, mode)
|
||||
return sh_ok("cp -- " .. h.shell_quote(src) .. " " .. h.shell_quote(dst))
|
||||
and sh_ok("chmod " .. mode .. " " .. h.shell_quote(dst))
|
||||
end
|
||||
|
||||
-- Fetch a source URL (https through curl, file:// a copy) into dest
|
||||
-- hashed: true if it arrived and has hash `hex`. Redirects are
|
||||
-- followed, https only (release tarballs are mostly served that way);
|
||||
-- the hash decides.
|
||||
local MAX_SOURCE = 4 * 1024 * 1024 * 1024
|
||||
local function fetch_source(url, dest, hex)
|
||||
local tmp = dest .. ".part"
|
||||
os.remove(tmp)
|
||||
local cmd
|
||||
local path = url:match("^file://(/.*)$")
|
||||
if path then
|
||||
cmd = "cp -- " .. h.shell_quote(path) .. " " .. h.shell_quote(tmp)
|
||||
else
|
||||
cmd = "curl -sfL --max-redirs 5 --proto =https --proto-redir =https --connect-timeout 30" ..
|
||||
" --max-filesize " .. MAX_SOURCE .. " -o " .. h.shell_quote(tmp) ..
|
||||
" -- " .. h.shell_quote(url)
|
||||
end
|
||||
if not sh_ok(cmd) then
|
||||
os.remove(tmp)
|
||||
return false, false
|
||||
end
|
||||
if h.sha256_file(tmp) ~= hex then
|
||||
os.remove(tmp)
|
||||
return false, true
|
||||
end
|
||||
return os.rename(tmp, dest) ~= nil
|
||||
end
|
||||
|
||||
-- The libconfig manifest a build must produce.
|
||||
local function manifest_text(r, deps, confs, tr)
|
||||
local function rel(text)
|
||||
local out = {}
|
||||
for n, v in text:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(out, string.format('{ PKG_NAME = "%s"; PKG_VER = "%s"; }', n, v))
|
||||
end
|
||||
return "(" .. table.concat(out, ", ") .. ")"
|
||||
end
|
||||
local t = {}
|
||||
for _, s in ipairs(tr.list) do table.insert(t, '"' .. s .. '"') end
|
||||
return table.concat({
|
||||
"PKG_NAME = " .. cfg_string(r.name) .. ";",
|
||||
"PKG_VER = " .. cfg_string(r.ver) .. ";",
|
||||
"PKG_DESC = " .. cfg_string(r.desc) .. ";",
|
||||
"PKG_MAINT = " .. cfg_string(r.maint) .. ";",
|
||||
"PKG_COPYRIGHT = " .. cfg_string(r.copyright) .. ";",
|
||||
"PKG_REPO = " .. cfg_string(r.repo) .. ";",
|
||||
"PKG_DEPS = " .. rel(deps) .. ";",
|
||||
"PKG_CONFS = " .. rel(confs) .. ";",
|
||||
"PKG_TRAITS = [ " .. table.concat(t, ", ") .. " ];",
|
||||
}, "\n") .. "\n"
|
||||
end
|
||||
|
||||
-- The driver's env file: shell assignments, every value quoted.
|
||||
local function env_text(r, settings, tr, sources, prefix)
|
||||
local q = h.shell_quote
|
||||
local args = {}
|
||||
for _, a in ipairs(lines(r.configure)) do table.insert(args, q(a)) end
|
||||
for _, l in ipairs(lines(r.trait_args)) do
|
||||
local t, side, a = l:match("^([^\t]+)\t([+-])\t(.*)$")
|
||||
if (side == "+") == (tr.on[t] == true) then table.insert(args, q(a)) end
|
||||
end
|
||||
for _, a in ipairs(settings.configure) do table.insert(args, q(a)) end
|
||||
local on = {}
|
||||
for _, s in ipairs(tr.list) do
|
||||
if s:sub(1, 1) ~= "-" then table.insert(on, s) end
|
||||
end
|
||||
local function kv(k, v) return k .. "=" .. q(v or "") .. "\n" end
|
||||
return kv("NAME", r.name) .. kv("VER", r.ver) .. kv("BUILD", r.build) ..
|
||||
kv("PREFIX", prefix) ..
|
||||
kv("SOURCES", table.concat(sources, " ")) ..
|
||||
kv("PATCHES", table.concat(lines(r.patches), " ")) ..
|
||||
kv("CONFIGURE_ARGS", table.concat(args, " ")) ..
|
||||
kv("TRAITS", table.concat(on, " ")) ..
|
||||
kv("JOBS", tostring(settings.jobs)) ..
|
||||
kv("CFLAGS", settings.cflags) .. kv("CXXFLAGS", settings.cxxflags) ..
|
||||
kv("LDFLAGS", settings.ldflags)
|
||||
end
|
||||
|
||||
-- CFLAGS/CXXFLAGS/LDFLAGS/CONFIGURE/JOBS for package `name`: the
|
||||
-- per-package value replaces the global one; the user's global replaces
|
||||
-- the system's.
|
||||
local function flags_for(name, opts)
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
local s = { configure = pkg and pkg.configure or {}, jobs = 1 }
|
||||
for _, k in ipairs({ "cflags", "cxxflags", "ldflags" }) do
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g[k] then s[k] = g[k] end
|
||||
end
|
||||
if pkg and pkg[k] then s[k] = pkg[k] end
|
||||
end
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g.jobs then s.jobs = g.jobs end
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
-- Whether the private copy `path`'s manifest (read by the library) is
|
||||
-- exactly what the build was handed.
|
||||
local function output_matches(path, r, deps, confs, tr)
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if istore.gl_read_pkg_manifest(path, m) ~= 0 then return false end
|
||||
local want_traits = #tr.list > 0 and (table.concat(tr.list, "\n") .. "\n") or ""
|
||||
local ok = ffi.string(m.pkg_name) == r.name
|
||||
and ffi.string(m.pkg_repo) == r.repo
|
||||
and string.format("%d.%d.%d", m.pkg_ver.maj, m.pkg_ver.min, m.pkg_ver.pat) == r.ver
|
||||
and h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") == deps
|
||||
and h.relation_lines(m.pkg_conflicts, "conf_name", "conf_ver") == confs
|
||||
and m.pkg_traits ~= nil and ffi.string(m.pkg_traits) == want_traits
|
||||
istore.gl_free_pkg(m)
|
||||
return ok
|
||||
end
|
||||
|
||||
-- The recipe of a verified bundle copy, as a table, or nil.
|
||||
local function read_recipe(path)
|
||||
local r = ffi.new("gl_recipe_t")
|
||||
if tonumber(istore.gl_recipe_read(path, r)) ~= 0 then return nil end
|
||||
local t = {}
|
||||
for _, k in ipairs({ "name", "ver", "repo", "desc", "maint", "copyright",
|
||||
"sources", "patches", "build", "configure", "traits", "trait_args",
|
||||
"deps", "build_deps", "confs" }) do
|
||||
t[k] = ffi.string(r[k])
|
||||
end
|
||||
istore.gl_recipe_free(r)
|
||||
return t
|
||||
end
|
||||
|
||||
-- The packages a build stacks as layers: BUILD_ROOT, the effective
|
||||
-- BUILD_DEPS and DEPS, and everything they depend on, each from the
|
||||
-- binary repositories (or `built`: this run's own builds). A list of
|
||||
-- { name, path, sha (hex) }, top first. Exits if one can't be had.
|
||||
local function layer_plan(id, root_names, build_deps, deps, indexes, built)
|
||||
local plan, seen = {}, {}
|
||||
local queue = {}
|
||||
for _, rel in ipairs({ deps, build_deps }) do
|
||||
for n, v in rel:gmatch("(%S+) (%S+)\n") do table.insert(queue, { n, v }) end
|
||||
end
|
||||
for _, n in ipairs(root_names) do table.insert(queue, { n, "0.0.0" }) end
|
||||
local k = 1
|
||||
while k <= #queue do
|
||||
local n, v = queue[k][1], queue[k][2]
|
||||
k = k + 1
|
||||
if not seen[n] then
|
||||
seen[n] = true
|
||||
local b = built[n]
|
||||
if b and h.vercmp(b.ver, v) >= 0 then
|
||||
table.insert(plan, { name = n, ver = b.ver, path = b.path, sha = b.sha })
|
||||
else
|
||||
local r, e = h.pick_binary(indexes, n)
|
||||
if not e or h.vercmp(e.ver, v) < 0 then
|
||||
log.lg_printf(2, "%s needs %s %s or later to build, and no binary repository has it.",
|
||||
id, n, v)
|
||||
h.finish(1)
|
||||
end
|
||||
table.insert(plan, { name = n, ver = e.ver, repo = r, entry = e,
|
||||
sha = e.sha256:match("^sha256:(%x+)$") })
|
||||
for dn, dv in e.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return plan
|
||||
end
|
||||
|
||||
--- Check, before the prompt, that every layer a build of plan item `it`
|
||||
--- needs can be had (exits otherwise). `planned` maps the names this
|
||||
--- run builds to their versions.
|
||||
function M.check_layers(it, indexes, planned, opts)
|
||||
local settings = system_settings(opts)
|
||||
local built = {}
|
||||
for n, v in pairs(planned) do built[n] = { ver = v } end
|
||||
local id = string.format("%s::%s %s", it.repo.name, it.entry.name, it.entry.ver)
|
||||
layer_plan(id, settings.build_root, M.effective(it.entry.build_deps, it.traits),
|
||||
it.deps, indexes, built)
|
||||
end
|
||||
|
||||
--- Build plan item `it` (a -src repository's entry, with it.traits from
|
||||
--- M.traits) into a verified private copy of its .gpkg. `built` maps
|
||||
--- names this run has already built to { ver, path, sha }, for layers.
|
||||
--- Returns the copy and its sidecar text; exits on any failure.
|
||||
function M.build(it, indexes, built, opts)
|
||||
local e, r_src = it.entry, it.repo
|
||||
local id = string.format("%s::%s %s", r_src.name, e.name, e.ver)
|
||||
local settings = system_settings(opts)
|
||||
local owner = settings.build_uid
|
||||
local ctx = h.live_context(opts)
|
||||
local file_mode = opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600"
|
||||
|
||||
-- The bundle: downloaded and hashed against the signed index,
|
||||
-- copied privately, then read and checked by the library.
|
||||
local cached, wrong = h.download(ctx, r_src, e,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
local copy = cached and h.verify_and_copy(cached, e.sha256 .. "\n")
|
||||
local r = copy and read_recipe(copy)
|
||||
if not cached and not wrong then
|
||||
log.lg_printf(2, "Cannot fetch %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
if not r or r.name ~= e.name or r.ver ~= e.ver or r.repo .. "-src" ~= r_src.name
|
||||
or r.deps ~= e.deps or r.confs ~= e.confs or r.build_deps ~= e.build_deps
|
||||
or r.traits ~= e.traits then
|
||||
log.lg_printf(2, "%s does not match its index.", id)
|
||||
if cached then os.remove(cached) end
|
||||
h.finish(1)
|
||||
end
|
||||
if cached then os.remove(cached) end
|
||||
|
||||
local tr = it.traits
|
||||
local deps, confs = M.effective(r.deps, tr), M.effective(r.confs, tr)
|
||||
local build_deps = M.effective(r.build_deps, tr)
|
||||
local flags = flags_for(r.name, opts)
|
||||
|
||||
-- The prefix the package is built for: where its usr/ will be
|
||||
-- linked, so it finds its own files at run time. /usr for base,
|
||||
-- else the scope's links (no -R here: these are runtime paths).
|
||||
local prefix = r.repo == "base" and "/usr" or
|
||||
(ffi.string(ctx.links_path):gsub("/+$", ""))
|
||||
|
||||
-- Sources, by hash, in <base>/build/sources.
|
||||
local sources_dir = build_dir(ctx, "sources")
|
||||
local sources = {}
|
||||
for url, hex in r.sources:gmatch("(%S+) sha256:(%x+)\n") do
|
||||
local file = url:match("([^/]+)$")
|
||||
local dest = sources_dir .. "/" .. hex
|
||||
if h.sha256_file(dest) ~= hex then
|
||||
local ok, mismatch = fetch_source(url, dest, hex)
|
||||
if not ok then
|
||||
log.lg_printf(2, mismatch and "A source of %s does not match its hash."
|
||||
or "Cannot fetch the sources of %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(dest))
|
||||
end
|
||||
table.insert(sources, { file = file, path = dest })
|
||||
end
|
||||
|
||||
-- This build's input (the scope's) and work/output (the builder's).
|
||||
local tag = string.format("%s-%s-%d", r.name, r.ver, ffi.C.gpkg_build_getpid())
|
||||
local in_dir = build_dir(ctx, "in-" .. tag)
|
||||
local work = build_dir(ctx, "work-" .. tag, owner)
|
||||
local out = build_dir(ctx, "out-" .. tag, owner)
|
||||
local function cleanup()
|
||||
remove_tree(in_dir)
|
||||
remove_tree(work)
|
||||
remove_tree(out)
|
||||
end
|
||||
local function fail(fmt, ...)
|
||||
log.lg_printf(2, fmt, ...)
|
||||
cleanup()
|
||||
h.finish(1)
|
||||
end
|
||||
|
||||
-- Layers, extracted once each into <base>/build/layers by the
|
||||
-- builder: NAME-VER-<first 8 of its hash>.
|
||||
local layers_dir = build_dir(ctx, "layers", owner)
|
||||
local layer_names = {}
|
||||
local user_flag = settings.build_user and (" -u " .. h.shell_quote(settings.build_user)) or ""
|
||||
for _, l in ipairs(layer_plan(id, settings.build_root, build_deps, deps, indexes, built)) do
|
||||
local lname = string.format("%s-%s-%s", l.name, l.ver, l.sha:sub(1, 8))
|
||||
if not exists(layers_dir .. "/" .. lname) then
|
||||
local path = l.path
|
||||
if l.entry then
|
||||
path = h.download(ctx, l.repo, l.entry,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
if not path then fail("Cannot fetch %s::%s %s.", l.repo.name, l.name, l.ver) end
|
||||
else
|
||||
-- A package this run built: its private copy,
|
||||
-- copied where the builder can read it.
|
||||
local p = in_dir .. "/" .. lname .. ".gpkg"
|
||||
if not copy_file(l.path, p, "644") then
|
||||
fail("Cannot write %s.", p)
|
||||
end
|
||||
path = p
|
||||
end
|
||||
local tmp = string.format(".%s.%d", lname, ffi.C.gpkg_build_getpid())
|
||||
if not sh_ok(h.gsandbox .. " -X" .. user_flag .. " -p " .. h.shell_quote(path) ..
|
||||
" -d " .. h.shell_quote(layers_dir .. "/" .. tmp)) then
|
||||
fail("Cannot extract %s %s for %s.", l.name, l.ver, id)
|
||||
end
|
||||
if not os.rename(layers_dir .. "/" .. tmp, layers_dir .. "/" .. lname) then
|
||||
remove_tree(layers_dir .. "/" .. tmp)
|
||||
end
|
||||
end
|
||||
table.insert(layer_names, lname)
|
||||
end
|
||||
|
||||
-- The input: bundle, sources, manifest, env, driver.
|
||||
local function put(name, text)
|
||||
local f = io.open(in_dir .. "/" .. name, "wb")
|
||||
if not f or not f:write(text) then fail("Cannot write %s/%s.", in_dir, name) end
|
||||
f:close()
|
||||
if not sh_ok("chmod 644 " .. h.shell_quote(in_dir .. "/" .. name)) then
|
||||
fail("Cannot write %s/%s.", in_dir, name)
|
||||
end
|
||||
end
|
||||
if not copy_file(copy, in_dir .. "/bundle.grcp", "644") or
|
||||
not sh_ok("mkdir -m 755 " .. h.shell_quote(in_dir .. "/sources")) then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
local files = {}
|
||||
for _, s in ipairs(sources) do
|
||||
if not copy_file(s.path, in_dir .. "/sources/" .. s.file, "644") then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
table.insert(files, s.file)
|
||||
end
|
||||
put("manifest.gpm.cfg", manifest_text(r, deps, confs, tr))
|
||||
put("env", env_text(r, flags, tr, files, prefix))
|
||||
local df = io.open(h.lib_dir .. "/build-driver.sh", "rb")
|
||||
local driver = df and df:read("*a")
|
||||
if df then df:close() end
|
||||
if not driver then fail("Cannot read %s/build-driver.sh.", h.lib_dir) end
|
||||
put("driver", driver)
|
||||
|
||||
-- The build. Its output goes to <base>/build/logs/NAME-VER.log.
|
||||
local log_path = build_dir(ctx, "logs") .. "/" .. r.name .. "-" .. r.ver .. ".log"
|
||||
local cmd = h.gsandbox .. " -R" .. user_flag .. " -L " .. h.shell_quote(layers_dir)
|
||||
for _, l in ipairs(layer_names) do cmd = cmd .. " -l " .. h.shell_quote(l) end
|
||||
cmd = cmd .. " -w " .. h.shell_quote(work) .. " -i " .. h.shell_quote(in_dir) ..
|
||||
" -o " .. h.shell_quote(out)
|
||||
log.lg_printf(0, "Building %s%s...", id, tr.show ~= "" and (" " .. tr.show) or "")
|
||||
local st
|
||||
if h.verbose() then
|
||||
local stf = in_dir .. "/.status"
|
||||
os.execute("{ " .. cmd .. " 2>&1; echo $? > " .. h.shell_quote(stf) .. "; } | tee "
|
||||
.. h.shell_quote(log_path))
|
||||
local f = io.open(stf, "r")
|
||||
st = f and tonumber(f:read("*l")) or 255
|
||||
if f then f:close() end
|
||||
else
|
||||
st = status_of(cmd .. " > " .. h.shell_quote(log_path) .. " 2>&1")
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(log_path))
|
||||
if st ~= 0 then
|
||||
fail("Building %s failed. Log: %s", id, log_path)
|
||||
end
|
||||
|
||||
-- The output, untrusted: copied privately while hashed, then its
|
||||
-- manifest must be exactly the one handed in.
|
||||
local result = out .. "/" .. r.name .. "-" .. r.ver .. ".gpkg"
|
||||
local hex = h.sha256_file(result)
|
||||
local pkg = hex and h.verify_and_copy(result, "sha256:" .. hex .. "\n")
|
||||
if not pkg or not output_matches(pkg, r, deps, confs, tr) then
|
||||
fail("%s: the build produced another package.", id)
|
||||
end
|
||||
cleanup()
|
||||
log.lg_printf(3, "Built %s.", id)
|
||||
return pkg, "sha256:" .. hex .. "\n", hex
|
||||
end
|
||||
|
||||
ffi.cdef[[
|
||||
int gpkg_build_getpid(void) asm("getpid");
|
||||
]]
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,685 @@
|
||||
-- lib/gpkg_build.lua
|
||||
--
|
||||
-- The package builder (BUILD_DESIGN.txt): turns a recipe from a NAME-src
|
||||
-- repository into a .gpkg, built in gsandbox, which gpkg_common then
|
||||
-- installs like any fetched package. Used only through gpkg_common.
|
||||
--
|
||||
-- Nothing a build produces is trusted: the output is copied privately
|
||||
-- while hashed and its manifest (read by the library) must be exactly
|
||||
-- the one handed in. What IS trusted is what decides the build: the
|
||||
-- bundle (hash from the signed index, then checked by gl_recipe_read),
|
||||
-- each source (the recipe's SHA256), the layers (packages verified
|
||||
-- against their signed indexes) and this file and build-driver.sh.
|
||||
|
||||
local ffi = require("ffi")
|
||||
local bit = require("bit")
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Set by M.init(): FFI handles and gpkg_common's helpers.
|
||||
local log, istore, txn, h
|
||||
|
||||
function M.init(env)
|
||||
log, istore, txn = env.log, env.istore, env.txn
|
||||
h = env
|
||||
end
|
||||
|
||||
local SYSTEM_CONF = "/etc/glacier/glacier.conf"
|
||||
local SYSTEM_CONFIG_DIR = "/etc/glacier/config"
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Small helpers
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function lines(s)
|
||||
local out = {}
|
||||
for l in (s or ""):gmatch("([^\n]*)\n") do table.insert(out, l) end
|
||||
return out
|
||||
end
|
||||
|
||||
local function exists(path)
|
||||
local f = io.open(path, "rb")
|
||||
if f then f:close() return true end
|
||||
local ok = os.execute("test -e " .. h.shell_quote(path))
|
||||
return ok == true or ok == 0
|
||||
end
|
||||
|
||||
-- The exit status of a shell command line.
|
||||
local function status_of(cmd)
|
||||
local p = io.popen(cmd .. "; echo \"rc=$?\"")
|
||||
local out = p:read("*a")
|
||||
p:close()
|
||||
return tonumber(out:match("rc=(%d+)%s*$")) or 255
|
||||
end
|
||||
|
||||
local function sh_ok(cmd)
|
||||
return status_of(cmd .. " >/dev/null 2>&1") == 0
|
||||
end
|
||||
|
||||
-- A libconfig string literal.
|
||||
local function cfg_string(s)
|
||||
return '"' .. s:gsub("\\", "\\\\"):gsub('"', '\\"') .. '"'
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Settings (BUILD_DESIGN.txt section 5)
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- One configuration file's build settings, or nil if it doesn't exist.
|
||||
-- Exits if it is malformed (or, read by syspkg, untrusted).
|
||||
local conf_cache = {}
|
||||
local function read_conf(path, kind, opts)
|
||||
if conf_cache[path] ~= nil then return conf_cache[path] or nil end
|
||||
if not exists(path) then
|
||||
conf_cache[path] = false
|
||||
return nil
|
||||
end
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(path)
|
||||
end
|
||||
local c = ffi.new("gl_build_conf_t")
|
||||
if tonumber(istore.gl_conf_build(path, kind, c)) ~= 0 then
|
||||
log.lg_printf(2, "Malformed %s.", path)
|
||||
h.finish(1)
|
||||
end
|
||||
local function str(v) return v ~= nil and ffi.string(v) or nil end
|
||||
local r = {
|
||||
traits = lines(str(c.traits)), cflags = str(c.cflags),
|
||||
cxxflags = str(c.cxxflags), ldflags = str(c.ldflags),
|
||||
configure = lines(str(c.configure)),
|
||||
jobs = c.jobs > 0 and tonumber(c.jobs) or nil,
|
||||
build_user = str(c.build_user), build_root = lines(str(c.build_root)),
|
||||
path = path,
|
||||
}
|
||||
istore.gl_build_conf_free(c)
|
||||
conf_cache[path] = r
|
||||
return r
|
||||
end
|
||||
|
||||
-- The user's base directory (<base>, beside index and store).
|
||||
local function user_base(opts)
|
||||
return string.format("/glacier/usr/%d", opts.uid)
|
||||
end
|
||||
|
||||
-- The configuration layers for package `name`, lowest first:
|
||||
-- { global = {...}, package = {...} } lists of read_conf results.
|
||||
local function conf_layers(name, opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
return { sys }, read_conf(SYSTEM_CONFIG_DIR .. "/" .. name .. ".conf",
|
||||
ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
local base = user_base(opts) .. "/config"
|
||||
return { sys, read_conf(base .. "/glacier.conf", ffi.C.GL_CONF_USER, opts) },
|
||||
read_conf(base .. "/" .. name .. ".conf", ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
|
||||
-- The system's settings a build can't do without: BUILD_ROOT, and for
|
||||
-- syspkg BUILD_USER (its uid too). Exits saying which is missing.
|
||||
local function system_settings(opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if not sys then
|
||||
log.lg_printf(2, "Cannot read %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
if #sys.build_root == 0 then
|
||||
log.lg_printf(2, "BUILD_ROOT is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local s = { build_root = sys.build_root }
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
if not sys.build_user then
|
||||
log.lg_printf(2, "BUILD_USER is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local p = io.popen("id -u -- " .. h.shell_quote(sys.build_user) .. " 2>/dev/null")
|
||||
local uid = tonumber(p and p:read("*l") or "")
|
||||
if p then p:close() end
|
||||
if not uid then
|
||||
log.lg_printf(2, "BUILD_USER %s in %s is not a user.", sys.build_user,
|
||||
SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
elseif uid == 0 then
|
||||
log.lg_printf(2, "BUILD_USER in %s must not be root.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
s.build_user, s.build_uid = sys.build_user, uid
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
--- The effective traits of building `name`, whose recipe (or -src index
|
||||
--- entry) declares `declared` ("NAME D\n" lines). Returns a table
|
||||
--- { on = { trait = bool }, list = sorted { "ssl", "-ldap" }, show =
|
||||
--- "[+ssl -ldap]" (or "" with no traits) }. Exits if a per-package file
|
||||
--- names a trait the recipe doesn't declare.
|
||||
function M.traits(name, declared, opts)
|
||||
local on, names = {}, {}
|
||||
for _, l in ipairs(lines(declared)) do
|
||||
local t, d = l:match("^(%S+) ([01])$")
|
||||
on[t] = d == "1"
|
||||
table.insert(names, t)
|
||||
end
|
||||
local function apply(conf, strict)
|
||||
if not conf then return end
|
||||
for _, s in ipairs(conf.traits) do
|
||||
local neg = s:sub(1, 1) == "-"
|
||||
local t = neg and s:sub(2) or s
|
||||
if on[t] ~= nil then
|
||||
on[t] = not neg
|
||||
elseif strict then
|
||||
log.lg_printf(2, "%s: %s has no trait %s.", conf.path, name, t)
|
||||
h.finish(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
for _, g in ipairs(globals) do apply(g, false) end
|
||||
apply(pkg, true)
|
||||
|
||||
table.sort(names)
|
||||
local list, show = {}, {}
|
||||
for _, t in ipairs(names) do
|
||||
table.insert(list, on[t] and t or ("-" .. t))
|
||||
table.insert(show, (on[t] and "+" or "-") .. t)
|
||||
end
|
||||
return { on = on, list = list,
|
||||
show = #show > 0 and ("[" .. table.concat(show, " ") .. "]") or "" }
|
||||
end
|
||||
|
||||
--- Relation lines ("NAME VER[ IF]\n") with their IF resolved against
|
||||
--- traits `tr`: "NAME VER\n" for each that applies.
|
||||
function M.effective(rel, tr)
|
||||
local out = {}
|
||||
for _, l in ipairs(lines(rel)) do
|
||||
local n, v, c = l:match("^(%S+) (%S+) ?(%S*)$")
|
||||
local applies = c == "" or
|
||||
(c:sub(1, 1) == "-" and not tr.on[c:sub(2)]) or
|
||||
(c:sub(1, 1) ~= "-" and tr.on[c])
|
||||
if applies then table.insert(out, n .. " " .. v .. "\n") end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
--- Check, before the prompt, everything a build of `name` needs from
|
||||
--- the configuration (exits on a problem): BUILD_ROOT, BUILD_USER.
|
||||
function M.check(opts)
|
||||
if opts.sysroot then
|
||||
log.lg_printf(2, "Building from source under -R is not supported.")
|
||||
h.finish(1)
|
||||
end
|
||||
system_settings(opts)
|
||||
-- gsandbox and the driver decide what a build produces, which
|
||||
-- root then installs: checked like syspkg's own files, here
|
||||
-- rather than at startup, since only builds need them.
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(h.gsandbox_path)
|
||||
h.require_trusted(h.lib_dir .. "/build-driver.sh")
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- The build
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- <base>/build/NAME, made and checked by the library; owned by the
|
||||
-- builder (`owner` = BUILD_USER's uid, syspkg) or the scope.
|
||||
local function build_dir(ctx, name, owner)
|
||||
local out = ffi.new("char[4096]")
|
||||
if txn.gl_scope_build_dir(ctx, name, owner or -1, out) ~= 0 then
|
||||
h.finish(1) -- the library said why
|
||||
end
|
||||
return ffi.string(out)
|
||||
end
|
||||
|
||||
-- Remove a build's directory tree (the builder's files may be
|
||||
-- read-only to it: overlayfs's work dir is mode 000).
|
||||
local function remove_tree(path)
|
||||
os.execute("chmod -R u+rwX " .. h.shell_quote(path) .. " 2>/dev/null; rm -rf "
|
||||
.. h.shell_quote(path))
|
||||
end
|
||||
|
||||
-- Copy a file (cp, then the given mode).
|
||||
local function copy_file(src, dst, mode)
|
||||
return sh_ok("cp -- " .. h.shell_quote(src) .. " " .. h.shell_quote(dst))
|
||||
and sh_ok("chmod " .. mode .. " " .. h.shell_quote(dst))
|
||||
end
|
||||
|
||||
-- Fetch a source URL (https through curl, file:// a copy) into dest
|
||||
-- hashed: true if it arrived and has hash `hex`. Redirects are
|
||||
-- followed, https only (release tarballs are mostly served that way);
|
||||
-- the hash decides.
|
||||
local MAX_SOURCE = 4 * 1024 * 1024 * 1024
|
||||
local function fetch_source(url, dest, hex)
|
||||
local tmp = dest .. ".part"
|
||||
os.remove(tmp)
|
||||
local cmd
|
||||
local path = url:match("^file://(/.*)$")
|
||||
if path then
|
||||
cmd = "cp -- " .. h.shell_quote(path) .. " " .. h.shell_quote(tmp)
|
||||
else
|
||||
cmd = "curl -sfL --max-redirs 5 --proto =https --proto-redir =https --connect-timeout 30" ..
|
||||
" --max-filesize " .. MAX_SOURCE .. " -o " .. h.shell_quote(tmp) ..
|
||||
" -- " .. h.shell_quote(url)
|
||||
end
|
||||
if not sh_ok(cmd) then
|
||||
os.remove(tmp)
|
||||
return false, false
|
||||
end
|
||||
if h.sha256_file(tmp) ~= hex then
|
||||
os.remove(tmp)
|
||||
return false, true
|
||||
end
|
||||
return os.rename(tmp, dest) ~= nil
|
||||
end
|
||||
|
||||
-- The libconfig manifest a build must produce.
|
||||
local function manifest_text(r, deps, confs, tr)
|
||||
local function rel(text)
|
||||
local out = {}
|
||||
for n, v in text:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(out, string.format('{ PKG_NAME = "%s"; PKG_VER = "%s"; }', n, v))
|
||||
end
|
||||
return "(" .. table.concat(out, ", ") .. ")"
|
||||
end
|
||||
local t = {}
|
||||
for _, s in ipairs(tr.list) do table.insert(t, '"' .. s .. '"') end
|
||||
return table.concat({
|
||||
"PKG_NAME = " .. cfg_string(r.name) .. ";",
|
||||
"PKG_VER = " .. cfg_string(r.ver) .. ";",
|
||||
"PKG_DESC = " .. cfg_string(r.desc) .. ";",
|
||||
"PKG_MAINT = " .. cfg_string(r.maint) .. ";",
|
||||
"PKG_COPYRIGHT = " .. cfg_string(r.copyright) .. ";",
|
||||
"PKG_REPO = " .. cfg_string(r.repo) .. ";",
|
||||
"PKG_DEPS = " .. rel(deps) .. ";",
|
||||
"PKG_CONFS = " .. rel(confs) .. ";",
|
||||
"PKG_TRAITS = [ " .. table.concat(t, ", ") .. " ];",
|
||||
}, "\n") .. "\n"
|
||||
end
|
||||
|
||||
-- The driver's env file: shell assignments, every value quoted.
|
||||
local function env_text(r, settings, tr, sources, prefix)
|
||||
local q = h.shell_quote
|
||||
local args = {}
|
||||
for _, a in ipairs(lines(r.configure)) do table.insert(args, q(a)) end
|
||||
for _, l in ipairs(lines(r.trait_args)) do
|
||||
local t, side, a = l:match("^([^\t]+)\t([+-])\t(.*)$")
|
||||
if (side == "+") == (tr.on[t] == true) then table.insert(args, q(a)) end
|
||||
end
|
||||
for _, a in ipairs(settings.configure) do table.insert(args, q(a)) end
|
||||
local on = {}
|
||||
for _, s in ipairs(tr.list) do
|
||||
if s:sub(1, 1) ~= "-" then table.insert(on, s) end
|
||||
end
|
||||
local function kv(k, v) return k .. "=" .. q(v or "") .. "\n" end
|
||||
return kv("NAME", r.name) .. kv("VER", r.ver) .. kv("BUILD", r.build) ..
|
||||
kv("PREFIX", prefix) ..
|
||||
kv("SOURCES", table.concat(sources, " ")) ..
|
||||
kv("PATCHES", table.concat(lines(r.patches), " ")) ..
|
||||
kv("CONFIGURE_ARGS", table.concat(args, " ")) ..
|
||||
kv("TRAITS", table.concat(on, " ")) ..
|
||||
kv("JOBS", tostring(settings.jobs)) ..
|
||||
kv("CFLAGS", settings.cflags) .. kv("CXXFLAGS", settings.cxxflags) ..
|
||||
kv("LDFLAGS", settings.ldflags)
|
||||
end
|
||||
|
||||
-- CFLAGS/CXXFLAGS/LDFLAGS/CONFIGURE/JOBS for package `name`: the
|
||||
-- per-package value replaces the global one; the user's global replaces
|
||||
-- the system's.
|
||||
local function flags_for(name, opts)
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
local s = { configure = pkg and pkg.configure or {}, jobs = 1 }
|
||||
for _, k in ipairs({ "cflags", "cxxflags", "ldflags" }) do
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g[k] then s[k] = g[k] end
|
||||
end
|
||||
if pkg and pkg[k] then s[k] = pkg[k] end
|
||||
end
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g.jobs then s.jobs = g.jobs end
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
-- Whether the private copy `path`'s manifest (read by the library) is
|
||||
-- exactly what the build was handed.
|
||||
local function output_matches(path, r, deps, confs, tr)
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if istore.gl_read_pkg_manifest(path, m) ~= 0 then return false end
|
||||
local want_traits = #tr.list > 0 and (table.concat(tr.list, "\n") .. "\n") or ""
|
||||
local ok = ffi.string(m.pkg_name) == r.name
|
||||
and ffi.string(m.pkg_repo) == r.repo
|
||||
and string.format("%d.%d.%d", m.pkg_ver.maj, m.pkg_ver.min, m.pkg_ver.pat) == r.ver
|
||||
and h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") == deps
|
||||
and h.relation_lines(m.pkg_conflicts, "conf_name", "conf_ver") == confs
|
||||
and m.pkg_traits ~= nil and ffi.string(m.pkg_traits) == want_traits
|
||||
istore.gl_free_pkg(m)
|
||||
return ok
|
||||
end
|
||||
|
||||
-- The recipe of a verified bundle copy, as a table, or nil.
|
||||
local function read_recipe(path)
|
||||
local r = ffi.new("gl_recipe_t")
|
||||
if tonumber(istore.gl_recipe_read(path, r)) ~= 0 then return nil end
|
||||
local t = {}
|
||||
for _, k in ipairs({ "name", "ver", "repo", "desc", "maint", "copyright",
|
||||
"sources", "patches", "build", "configure", "traits", "trait_args",
|
||||
"deps", "build_deps", "confs" }) do
|
||||
t[k] = ffi.string(r[k])
|
||||
end
|
||||
istore.gl_recipe_free(r)
|
||||
return t
|
||||
end
|
||||
|
||||
-- The packages a build stacks as layers: BUILD_ROOT, the effective
|
||||
-- BUILD_DEPS and DEPS, and everything they depend on, each from
|
||||
-- `built` (this run's own builds), `kept` (installed source builds,
|
||||
-- M.kept) or the binary repositories, in that order, at a high enough
|
||||
-- version. A list of { name, path, sha (hex) }, top first. Exits if
|
||||
-- one can't be had.
|
||||
local function layer_plan(id, root_names, build_deps, deps, indexes, built, kept)
|
||||
local plan, seen = {}, {}
|
||||
local queue = {}
|
||||
for _, rel in ipairs({ deps, build_deps }) do
|
||||
for n, v in rel:gmatch("(%S+) (%S+)\n") do table.insert(queue, { n, v }) end
|
||||
end
|
||||
for _, n in ipairs(root_names) do table.insert(queue, { n, "0.0.0" }) end
|
||||
local k = 1
|
||||
while k <= #queue do
|
||||
local n, v = queue[k][1], queue[k][2]
|
||||
k = k + 1
|
||||
if not seen[n] then
|
||||
seen[n] = true
|
||||
local b = built[n]
|
||||
if not (b and h.vercmp(b.ver, v) >= 0) then b = kept[n] end
|
||||
if b and b.path and not b.sha then b.sha = h.sha256_file(b.path) or false end
|
||||
if b and b.sha ~= false and h.vercmp(b.ver, v) >= 0 then
|
||||
table.insert(plan, { name = n, ver = b.ver, path = b.path, sha = b.sha })
|
||||
for dn, dv in b.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
else
|
||||
local r, e = h.pick_binary(indexes, n)
|
||||
if not e or h.vercmp(e.ver, v) < 0 then
|
||||
log.lg_printf(2, "%s needs %s %s or later to build, and no binary repository has it.",
|
||||
id, n, v)
|
||||
h.finish(1)
|
||||
end
|
||||
table.insert(plan, { name = n, ver = e.ver, repo = r, entry = e,
|
||||
sha = e.sha256:match("^sha256:(%x+)$") })
|
||||
for dn, dv in e.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return plan
|
||||
end
|
||||
|
||||
--- Check, before the prompt, that every layer a build of plan item `it`
|
||||
--- needs can be had (exits otherwise). `planned` maps the names this
|
||||
--- run builds to { ver, deps }.
|
||||
function M.check_layers(it, indexes, planned, opts)
|
||||
local settings = system_settings(opts)
|
||||
local id = string.format("%s::%s %s", it.repo.name, it.entry.name, it.entry.ver)
|
||||
layer_plan(id, settings.build_root, M.effective(it.entry.build_deps, it.traits),
|
||||
it.deps, indexes, planned, M.kept(opts))
|
||||
end
|
||||
|
||||
--- Build plan item `it` (a -src repository's entry, with it.traits from
|
||||
--- M.traits) into a verified private copy of its .gpkg. `built` maps
|
||||
--- names this run has already built to { ver, path, sha }, for layers.
|
||||
--- Returns the copy and its sidecar text; exits on any failure.
|
||||
function M.build(it, indexes, built, opts)
|
||||
local e, r_src = it.entry, it.repo
|
||||
local id = string.format("%s::%s %s", r_src.name, e.name, e.ver)
|
||||
local settings = system_settings(opts)
|
||||
local owner = settings.build_uid
|
||||
local ctx = h.live_context(opts)
|
||||
local file_mode = opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600"
|
||||
|
||||
-- The bundle: downloaded and hashed against the signed index,
|
||||
-- copied privately, then read and checked by the library.
|
||||
local cached, wrong = h.download(ctx, r_src, e,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
local copy = cached and h.verify_and_copy(cached, e.sha256 .. "\n")
|
||||
local r = copy and read_recipe(copy)
|
||||
if not cached and not wrong then
|
||||
log.lg_printf(2, "Cannot fetch %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
if not r or r.name ~= e.name or r.ver ~= e.ver or r.repo .. "-src" ~= r_src.name
|
||||
or r.deps ~= e.deps or r.confs ~= e.confs or r.build_deps ~= e.build_deps
|
||||
or r.traits ~= e.traits then
|
||||
log.lg_printf(2, "%s does not match its index.", id)
|
||||
if cached then os.remove(cached) end
|
||||
h.finish(1)
|
||||
end
|
||||
if cached then os.remove(cached) end
|
||||
|
||||
local tr = it.traits
|
||||
local deps, confs = M.effective(r.deps, tr), M.effective(r.confs, tr)
|
||||
local build_deps = M.effective(r.build_deps, tr)
|
||||
local flags = flags_for(r.name, opts)
|
||||
|
||||
-- The prefix the package is built for: where its usr/ will be
|
||||
-- linked, so it finds its own files at run time. /usr for base,
|
||||
-- else the scope's links (no -R here: these are runtime paths).
|
||||
local prefix = r.repo == "base" and "/usr" or
|
||||
(ffi.string(ctx.links_path):gsub("/+$", ""))
|
||||
|
||||
-- Sources, by hash, in <base>/build/sources.
|
||||
local sources_dir = build_dir(ctx, "sources")
|
||||
local sources = {}
|
||||
for url, hex in r.sources:gmatch("(%S+) sha256:(%x+)\n") do
|
||||
local file = url:match("([^/]+)$")
|
||||
local dest = sources_dir .. "/" .. hex
|
||||
if h.sha256_file(dest) ~= hex then
|
||||
local ok, mismatch = fetch_source(url, dest, hex)
|
||||
if not ok then
|
||||
log.lg_printf(2, mismatch and "A source of %s does not match its hash."
|
||||
or "Cannot fetch the sources of %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(dest))
|
||||
end
|
||||
table.insert(sources, { file = file, path = dest })
|
||||
end
|
||||
|
||||
-- This build's input (the scope's) and work/output (the builder's).
|
||||
local tag = string.format("%s-%s-%d", r.name, r.ver, ffi.C.gpkg_build_getpid())
|
||||
local in_dir = build_dir(ctx, "in-" .. tag)
|
||||
local work = build_dir(ctx, "work-" .. tag, owner)
|
||||
local out = build_dir(ctx, "out-" .. tag, owner)
|
||||
local function cleanup()
|
||||
remove_tree(in_dir)
|
||||
remove_tree(work)
|
||||
remove_tree(out)
|
||||
end
|
||||
local function fail(fmt, ...)
|
||||
log.lg_printf(2, fmt, ...)
|
||||
cleanup()
|
||||
h.finish(1)
|
||||
end
|
||||
|
||||
-- Layers, extracted once each into <base>/build/layers by the
|
||||
-- builder: NAME-VER-<first 8 of its hash>.
|
||||
local layers_dir = build_dir(ctx, "layers", owner)
|
||||
local layer_names = {}
|
||||
local user_flag = settings.build_user and (" -u " .. h.shell_quote(settings.build_user)) or ""
|
||||
for _, l in ipairs(layer_plan(id, settings.build_root, build_deps, deps, indexes, built,
|
||||
M.kept(opts))) do
|
||||
local lname = string.format("%s-%s-%s", l.name, l.ver, l.sha:sub(1, 8))
|
||||
if not exists(layers_dir .. "/" .. lname) then
|
||||
local path = l.path
|
||||
if l.entry then
|
||||
path = h.download(ctx, l.repo, l.entry,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
if not path then fail("Cannot fetch %s::%s %s.", l.repo.name, l.name, l.ver) end
|
||||
else
|
||||
-- A package this run built: its private copy,
|
||||
-- copied where the builder can read it.
|
||||
local p = in_dir .. "/" .. lname .. ".gpkg"
|
||||
if not copy_file(l.path, p, "644") then
|
||||
fail("Cannot write %s.", p)
|
||||
end
|
||||
path = p
|
||||
end
|
||||
local tmp = string.format(".%s.%d", lname, ffi.C.gpkg_build_getpid())
|
||||
if not sh_ok(h.gsandbox .. " -X" .. user_flag .. " -p " .. h.shell_quote(path) ..
|
||||
" -d " .. h.shell_quote(layers_dir .. "/" .. tmp)) then
|
||||
fail("Cannot extract %s %s for %s.", l.name, l.ver, id)
|
||||
end
|
||||
if not os.rename(layers_dir .. "/" .. tmp, layers_dir .. "/" .. lname) then
|
||||
remove_tree(layers_dir .. "/" .. tmp)
|
||||
end
|
||||
end
|
||||
table.insert(layer_names, lname)
|
||||
end
|
||||
|
||||
-- The input: bundle, sources, manifest, env, driver.
|
||||
local function put(name, text)
|
||||
local f = io.open(in_dir .. "/" .. name, "wb")
|
||||
if not f or not f:write(text) then fail("Cannot write %s/%s.", in_dir, name) end
|
||||
f:close()
|
||||
if not sh_ok("chmod 644 " .. h.shell_quote(in_dir .. "/" .. name)) then
|
||||
fail("Cannot write %s/%s.", in_dir, name)
|
||||
end
|
||||
end
|
||||
if not copy_file(copy, in_dir .. "/bundle.grcp", "644") or
|
||||
not sh_ok("mkdir -m 755 " .. h.shell_quote(in_dir .. "/sources")) then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
local files = {}
|
||||
for _, s in ipairs(sources) do
|
||||
if not copy_file(s.path, in_dir .. "/sources/" .. s.file, "644") then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
table.insert(files, s.file)
|
||||
end
|
||||
put("manifest.gpm.cfg", manifest_text(r, deps, confs, tr))
|
||||
put("env", env_text(r, flags, tr, files, prefix))
|
||||
local df = io.open(h.lib_dir .. "/build-driver.sh", "rb")
|
||||
local driver = df and df:read("*a")
|
||||
if df then df:close() end
|
||||
if not driver then fail("Cannot read %s/build-driver.sh.", h.lib_dir) end
|
||||
put("driver", driver)
|
||||
|
||||
-- The build. Its output goes to <base>/build/logs/NAME-VER.log.
|
||||
local log_path = build_dir(ctx, "logs") .. "/" .. r.name .. "-" .. r.ver .. ".log"
|
||||
local cmd = h.gsandbox .. " -R" .. user_flag .. " -L " .. h.shell_quote(layers_dir)
|
||||
for _, l in ipairs(layer_names) do cmd = cmd .. " -l " .. h.shell_quote(l) end
|
||||
cmd = cmd .. " -w " .. h.shell_quote(work) .. " -i " .. h.shell_quote(in_dir) ..
|
||||
" -o " .. h.shell_quote(out)
|
||||
log.lg_printf(0, "Building %s%s...", id, tr.show ~= "" and (" " .. tr.show) or "")
|
||||
local st
|
||||
if h.verbose() then
|
||||
local stf = in_dir .. "/.status"
|
||||
os.execute("{ " .. cmd .. " 2>&1; echo $? > " .. h.shell_quote(stf) .. "; } | tee "
|
||||
.. h.shell_quote(log_path))
|
||||
local f = io.open(stf, "r")
|
||||
st = f and tonumber(f:read("*l")) or 255
|
||||
if f then f:close() end
|
||||
else
|
||||
st = status_of(cmd .. " > " .. h.shell_quote(log_path) .. " 2>&1")
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(log_path))
|
||||
if st ~= 0 then
|
||||
fail("Building %s failed. Log: %s", id, log_path)
|
||||
end
|
||||
|
||||
-- The output, untrusted: copied privately while hashed, then its
|
||||
-- manifest must be exactly the one handed in.
|
||||
local result = out .. "/" .. r.name .. "-" .. r.ver .. ".gpkg"
|
||||
local hex = h.sha256_file(result)
|
||||
local pkg = hex and h.verify_and_copy(result, "sha256:" .. hex .. "\n")
|
||||
if not pkg or not output_matches(pkg, r, deps, confs, tr) then
|
||||
fail("%s: the build produced another package.", id)
|
||||
end
|
||||
cleanup()
|
||||
log.lg_printf(3, "Built %s.", id)
|
||||
return pkg, "sha256:" .. hex .. "\n", hex
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Kept packages: the verified .gpkg of each installed source build,
|
||||
-- the bytes that were installed, so a later build can use it as a
|
||||
-- layer (no binary repository has it). <base>/build/packages/
|
||||
-- REPO@NAME-VER.gpkg ('@' is in no name), kept while that version is
|
||||
-- installed and built from source.
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function kept_name(repo, name, ver)
|
||||
return repo .. "@" .. name .. "-" .. ver .. ".gpkg"
|
||||
end
|
||||
|
||||
-- <base>/build/packages, or nil if there is none (not made here).
|
||||
local function kept_dir(ctx)
|
||||
local d = ffi.string(ctx.store_path):gsub("/store$", "") .. "/build/packages"
|
||||
return exists(d) and d or nil
|
||||
end
|
||||
|
||||
-- The kept packages of the installed source builds: the file names a
|
||||
-- prune leaves, and (for layer_plan) name -> { ver, path, deps }.
|
||||
local function kept_scan(opts)
|
||||
local ctx = h.live_context(opts)
|
||||
local dir = kept_dir(ctx)
|
||||
if not dir then return nil, {}, {} end
|
||||
local want, by_name = {}, {}
|
||||
for _, i in ipairs(h.installed_entries(opts)) do
|
||||
if h.built_from(ctx, i.repo, i.name) then
|
||||
local f = kept_name(i.repo, i.name, i.ver)
|
||||
local path = dir .. "/" .. f
|
||||
want[f] = true
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if exists(path) and istore.gl_read_pkg_manifest(path, m) == 0 then
|
||||
by_name[i.name] = { ver = i.ver, path = path,
|
||||
deps = h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") }
|
||||
istore.gl_free_pkg(m)
|
||||
end
|
||||
end
|
||||
end
|
||||
return dir, want, by_name
|
||||
end
|
||||
|
||||
--- The installed source builds a build can use as layers: name ->
|
||||
--- { ver, path, deps }.
|
||||
function M.kept(opts)
|
||||
local _, _, by_name = kept_scan(opts)
|
||||
return by_name
|
||||
end
|
||||
|
||||
--- After a commit: keep the private copy `path` of a package this run
|
||||
--- built and installed (repo::name ver). A failure only warns: the
|
||||
--- package is installed, and only later builds would miss it.
|
||||
function M.keep(opts, path, repo, name, ver)
|
||||
local ctx = h.live_context(opts)
|
||||
local out = ffi.new("char[4096]")
|
||||
local ok = txn.gl_scope_build_dir(ctx, "packages", -1, out) == 0
|
||||
local dest = ok and (ffi.string(out) .. "/" .. kept_name(repo, name, ver))
|
||||
if not ok or not copy_file(path, dest .. ".part",
|
||||
opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600")
|
||||
or not os.rename(dest .. ".part", dest) then
|
||||
if dest then os.remove(dest .. ".part") end
|
||||
log.lg_printf(1, "Could not keep %s::%s %s for later builds.", repo, name, ver)
|
||||
end
|
||||
end
|
||||
|
||||
--- After a commit: delete the kept packages that are no longer an
|
||||
--- installed source build (removed, replaced, or reinstalled from a
|
||||
--- binary repository).
|
||||
function M.prune_kept(opts)
|
||||
local dir, want = kept_scan(opts)
|
||||
if not dir then return end
|
||||
local p = io.popen("ls -A -- " .. h.shell_quote(dir))
|
||||
for f in p:lines() do
|
||||
if not want[f] then os.remove(dir .. "/" .. f) end
|
||||
end
|
||||
p:close()
|
||||
end
|
||||
|
||||
ffi.cdef[[
|
||||
int gpkg_build_getpid(void) asm("getpid");
|
||||
]]
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,686 @@
|
||||
-- lib/gpkg_build.lua
|
||||
--
|
||||
-- The package builder (BUILD_DESIGN.txt): turns a recipe from a NAME-src
|
||||
-- repository into a .gpkg, built in gsandbox, which gpkg_common then
|
||||
-- installs like any fetched package. Used only through gpkg_common.
|
||||
--
|
||||
-- Nothing a build produces is trusted: the output is copied privately
|
||||
-- while hashed and its manifest (read by the library) must be exactly
|
||||
-- the one handed in. What IS trusted is what decides the build: the
|
||||
-- bundle (hash from the signed index, then checked by gl_recipe_read),
|
||||
-- each source (the recipe's SHA256), the layers (packages verified
|
||||
-- against their signed indexes) and this file and build-driver.sh.
|
||||
|
||||
local ffi = require("ffi")
|
||||
local bit = require("bit")
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Set by M.init(): FFI handles and gpkg_common's helpers.
|
||||
local log, istore, txn, h
|
||||
|
||||
function M.init(env)
|
||||
log, istore, txn = env.log, env.istore, env.txn
|
||||
h = env
|
||||
end
|
||||
|
||||
local SYSTEM_CONF = "/etc/glacier/glacier.conf"
|
||||
local SYSTEM_CONFIG_DIR = "/etc/glacier/config"
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Small helpers
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function lines(s)
|
||||
local out = {}
|
||||
for l in (s or ""):gmatch("([^\n]*)\n") do table.insert(out, l) end
|
||||
return out
|
||||
end
|
||||
|
||||
local function exists(path)
|
||||
local f = io.open(path, "rb")
|
||||
if f then f:close() return true end
|
||||
local ok = os.execute("test -e " .. h.shell_quote(path))
|
||||
return ok == true or ok == 0
|
||||
end
|
||||
|
||||
-- The exit status of a shell command line.
|
||||
local function status_of(cmd)
|
||||
local p = io.popen(cmd .. "; echo \"rc=$?\"")
|
||||
local out = p:read("*a")
|
||||
p:close()
|
||||
return tonumber(out:match("rc=(%d+)%s*$")) or 255
|
||||
end
|
||||
|
||||
local function sh_ok(cmd)
|
||||
return status_of(cmd .. " >/dev/null 2>&1") == 0
|
||||
end
|
||||
|
||||
-- A libconfig string literal.
|
||||
local function cfg_string(s)
|
||||
return '"' .. s:gsub("\\", "\\\\"):gsub('"', '\\"') .. '"'
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Settings (BUILD_DESIGN.txt section 5)
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- One configuration file's build settings, or nil if it doesn't exist.
|
||||
-- Exits if it is malformed (or, read by syspkg, untrusted).
|
||||
local conf_cache = {}
|
||||
local function read_conf(path, kind, opts)
|
||||
if conf_cache[path] ~= nil then return conf_cache[path] or nil end
|
||||
if not exists(path) then
|
||||
conf_cache[path] = false
|
||||
return nil
|
||||
end
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(path)
|
||||
end
|
||||
local c = ffi.new("gl_build_conf_t")
|
||||
if tonumber(istore.gl_conf_build(path, kind, c)) ~= 0 then
|
||||
log.lg_printf(2, "Malformed %s.", path)
|
||||
h.finish(1)
|
||||
end
|
||||
local function str(v) return v ~= nil and ffi.string(v) or nil end
|
||||
local r = {
|
||||
traits = lines(str(c.traits)), cflags = str(c.cflags),
|
||||
cxxflags = str(c.cxxflags), ldflags = str(c.ldflags),
|
||||
configure = lines(str(c.configure)),
|
||||
jobs = c.jobs > 0 and tonumber(c.jobs) or nil,
|
||||
build_user = str(c.build_user), build_root = lines(str(c.build_root)),
|
||||
path = path,
|
||||
}
|
||||
istore.gl_build_conf_free(c)
|
||||
conf_cache[path] = r
|
||||
return r
|
||||
end
|
||||
|
||||
-- The user's base directory (<base>, beside index and store).
|
||||
local function user_base(opts)
|
||||
return string.format("/glacier/usr/%d", opts.uid)
|
||||
end
|
||||
|
||||
-- The configuration layers for package `name`, lowest first:
|
||||
-- { global = {...}, package = {...} } lists of read_conf results.
|
||||
local function conf_layers(name, opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
return { sys }, read_conf(SYSTEM_CONFIG_DIR .. "/" .. name .. ".conf",
|
||||
ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
local base = user_base(opts) .. "/config"
|
||||
return { sys, read_conf(base .. "/glacier.conf", ffi.C.GL_CONF_USER, opts) },
|
||||
read_conf(base .. "/" .. name .. ".conf", ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
|
||||
-- The system's settings a build can't do without: BUILD_ROOT, and for
|
||||
-- syspkg BUILD_USER (its uid too). Exits saying which is missing.
|
||||
local function system_settings(opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if not sys then
|
||||
log.lg_printf(2, "Cannot read %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
if #sys.build_root == 0 then
|
||||
log.lg_printf(2, "BUILD_ROOT is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local s = { build_root = sys.build_root }
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
if not sys.build_user then
|
||||
log.lg_printf(2, "BUILD_USER is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local p = io.popen("id -u -- " .. h.shell_quote(sys.build_user) .. " 2>/dev/null")
|
||||
local uid = tonumber(p and p:read("*l") or "")
|
||||
if p then p:close() end
|
||||
if not uid then
|
||||
log.lg_printf(2, "BUILD_USER %s in %s is not a user.", sys.build_user,
|
||||
SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
elseif uid == 0 then
|
||||
log.lg_printf(2, "BUILD_USER in %s must not be root.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
s.build_user, s.build_uid = sys.build_user, uid
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
--- The effective traits of building `name`, whose recipe (or -src index
|
||||
--- entry) declares `declared` ("NAME D\n" lines). Returns a table
|
||||
--- { on = { trait = bool }, list = sorted { "ssl", "-ldap" }, show =
|
||||
--- "[+ssl -ldap]" (or "" with no traits) }. Exits if a per-package file
|
||||
--- names a trait the recipe doesn't declare.
|
||||
function M.traits(name, declared, opts)
|
||||
local on, names = {}, {}
|
||||
for _, l in ipairs(lines(declared)) do
|
||||
local t, d = l:match("^(%S+) ([01])$")
|
||||
on[t] = d == "1"
|
||||
table.insert(names, t)
|
||||
end
|
||||
local function apply(conf, strict)
|
||||
if not conf then return end
|
||||
for _, s in ipairs(conf.traits) do
|
||||
local neg = s:sub(1, 1) == "-"
|
||||
local t = neg and s:sub(2) or s
|
||||
if on[t] ~= nil then
|
||||
on[t] = not neg
|
||||
elseif strict then
|
||||
log.lg_printf(2, "%s: %s has no trait %s.", conf.path, name, t)
|
||||
h.finish(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
for _, g in ipairs(globals) do apply(g, false) end
|
||||
apply(pkg, true)
|
||||
|
||||
table.sort(names)
|
||||
local list, show = {}, {}
|
||||
for _, t in ipairs(names) do
|
||||
table.insert(list, on[t] and t or ("-" .. t))
|
||||
table.insert(show, (on[t] and "+" or "-") .. t)
|
||||
end
|
||||
return { on = on, list = list,
|
||||
show = #show > 0 and ("[" .. table.concat(show, " ") .. "]") or "" }
|
||||
end
|
||||
|
||||
--- Relation lines ("NAME VER[ IF]\n") with their IF resolved against
|
||||
--- traits `tr`: "NAME VER\n" for each that applies.
|
||||
function M.effective(rel, tr)
|
||||
local out = {}
|
||||
for _, l in ipairs(lines(rel)) do
|
||||
local n, v, c = l:match("^(%S+) (%S+) ?(%S*)$")
|
||||
local applies = c == "" or
|
||||
(c:sub(1, 1) == "-" and not tr.on[c:sub(2)]) or
|
||||
(c:sub(1, 1) ~= "-" and tr.on[c])
|
||||
if applies then table.insert(out, n .. " " .. v .. "\n") end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
--- Check, before the prompt, everything a build of `name` needs from
|
||||
--- the configuration (exits on a problem): BUILD_ROOT, BUILD_USER.
|
||||
function M.check(opts)
|
||||
if opts.sysroot then
|
||||
log.lg_printf(2, "Building from source under -R is not supported.")
|
||||
h.finish(1)
|
||||
end
|
||||
system_settings(opts)
|
||||
-- gsandbox and the driver decide what a build produces, which
|
||||
-- root then installs: checked like syspkg's own files, here
|
||||
-- rather than at startup, since only builds need them.
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(h.gsandbox_path)
|
||||
h.require_trusted(h.lib_dir .. "/build-driver.sh")
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- The build
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- <base>/build/NAME, made and checked by the library; owned by the
|
||||
-- builder (`owner` = BUILD_USER's uid, syspkg) or the scope.
|
||||
local function build_dir(ctx, name, owner)
|
||||
local out = ffi.new("char[4096]")
|
||||
if txn.gl_scope_build_dir(ctx, name, owner or -1, out) ~= 0 then
|
||||
h.finish(1) -- the library said why
|
||||
end
|
||||
return ffi.string(out)
|
||||
end
|
||||
|
||||
-- Remove a build's directory tree (the builder's files may be
|
||||
-- read-only to it: overlayfs's work dir is mode 000).
|
||||
local function remove_tree(path)
|
||||
os.execute("chmod -R u+rwX " .. h.shell_quote(path) .. " 2>/dev/null; rm -rf "
|
||||
.. h.shell_quote(path))
|
||||
end
|
||||
|
||||
-- Copy a file (cp, then the given mode).
|
||||
local function copy_file(src, dst, mode)
|
||||
return sh_ok("cp -- " .. h.shell_quote(src) .. " " .. h.shell_quote(dst))
|
||||
and sh_ok("chmod " .. mode .. " " .. h.shell_quote(dst))
|
||||
end
|
||||
|
||||
-- Fetch a source URL (https through curl, file:// a copy) into dest
|
||||
-- hashed: true if it arrived and has hash `hex`. Redirects are
|
||||
-- followed, https only (release tarballs are mostly served that way);
|
||||
-- the hash decides.
|
||||
local MAX_SOURCE = 4 * 1024 * 1024 * 1024
|
||||
local function fetch_source(url, dest, hex)
|
||||
local tmp = dest .. ".part"
|
||||
os.remove(tmp)
|
||||
local cmd
|
||||
local path = url:match("^file://(/.*)$")
|
||||
if path then
|
||||
cmd = "cp -- " .. h.shell_quote(path) .. " " .. h.shell_quote(tmp)
|
||||
else
|
||||
cmd = "curl -sfL --max-redirs 5 --proto =https --proto-redir =https --connect-timeout 30" ..
|
||||
" --max-filesize " .. MAX_SOURCE .. " -o " .. h.shell_quote(tmp) ..
|
||||
" -- " .. h.shell_quote(url)
|
||||
end
|
||||
if not sh_ok(cmd) then
|
||||
os.remove(tmp)
|
||||
return false, false
|
||||
end
|
||||
if h.sha256_file(tmp) ~= hex then
|
||||
os.remove(tmp)
|
||||
return false, true
|
||||
end
|
||||
return os.rename(tmp, dest) ~= nil
|
||||
end
|
||||
|
||||
-- The libconfig manifest a build must produce.
|
||||
local function manifest_text(r, deps, confs, tr)
|
||||
local function rel(text)
|
||||
local out = {}
|
||||
for n, v in text:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(out, string.format('{ PKG_NAME = "%s"; PKG_VER = "%s"; }', n, v))
|
||||
end
|
||||
return "(" .. table.concat(out, ", ") .. ")"
|
||||
end
|
||||
local t = {}
|
||||
for _, s in ipairs(tr.list) do table.insert(t, '"' .. s .. '"') end
|
||||
return table.concat({
|
||||
"PKG_NAME = " .. cfg_string(r.name) .. ";",
|
||||
"PKG_VER = " .. cfg_string(r.ver) .. ";",
|
||||
"PKG_DESC = " .. cfg_string(r.desc) .. ";",
|
||||
"PKG_MAINT = " .. cfg_string(r.maint) .. ";",
|
||||
"PKG_COPYRIGHT = " .. cfg_string(r.copyright) .. ";",
|
||||
"PKG_REPO = " .. cfg_string(r.repo) .. ";",
|
||||
"PKG_DEPS = " .. rel(deps) .. ";",
|
||||
"PKG_CONFS = " .. rel(confs) .. ";",
|
||||
"PKG_TRAITS = [ " .. table.concat(t, ", ") .. " ];",
|
||||
}, "\n") .. "\n"
|
||||
end
|
||||
|
||||
-- The driver's env file: shell assignments, every value quoted.
|
||||
local function env_text(r, settings, tr, sources, prefix)
|
||||
local q = h.shell_quote
|
||||
local args = {}
|
||||
for _, a in ipairs(lines(r.configure)) do table.insert(args, q(a)) end
|
||||
for _, l in ipairs(lines(r.trait_args)) do
|
||||
local t, side, a = l:match("^([^\t]+)\t([+-])\t(.*)$")
|
||||
if (side == "+") == (tr.on[t] == true) then table.insert(args, q(a)) end
|
||||
end
|
||||
for _, a in ipairs(settings.configure) do table.insert(args, q(a)) end
|
||||
local on = {}
|
||||
for _, s in ipairs(tr.list) do
|
||||
if s:sub(1, 1) ~= "-" then table.insert(on, s) end
|
||||
end
|
||||
local function kv(k, v) return k .. "=" .. q(v or "") .. "\n" end
|
||||
return kv("NAME", r.name) .. kv("VER", r.ver) .. kv("BUILD", r.build) ..
|
||||
kv("PREFIX", prefix) ..
|
||||
kv("SOURCES", table.concat(sources, " ")) ..
|
||||
kv("PATCHES", table.concat(lines(r.patches), " ")) ..
|
||||
kv("CONFIGURE_ARGS", table.concat(args, " ")) ..
|
||||
kv("TRAITS", table.concat(on, " ")) ..
|
||||
kv("JOBS", tostring(settings.jobs)) ..
|
||||
kv("CFLAGS", settings.cflags) .. kv("CXXFLAGS", settings.cxxflags) ..
|
||||
kv("LDFLAGS", settings.ldflags)
|
||||
end
|
||||
|
||||
-- CFLAGS/CXXFLAGS/LDFLAGS/CONFIGURE/JOBS for package `name`: the
|
||||
-- per-package value replaces the global one; the user's global replaces
|
||||
-- the system's.
|
||||
local function flags_for(name, opts)
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
local s = { configure = pkg and pkg.configure or {}, jobs = 1 }
|
||||
for _, k in ipairs({ "cflags", "cxxflags", "ldflags" }) do
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g[k] then s[k] = g[k] end
|
||||
end
|
||||
if pkg and pkg[k] then s[k] = pkg[k] end
|
||||
end
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g.jobs then s.jobs = g.jobs end
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
-- Whether the private copy `path`'s manifest (read by the library) is
|
||||
-- exactly what the build was handed.
|
||||
local function output_matches(path, r, deps, confs, tr)
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if istore.gl_read_pkg_manifest(path, m) ~= 0 then return false end
|
||||
local want_traits = #tr.list > 0 and (table.concat(tr.list, "\n") .. "\n") or ""
|
||||
local ok = ffi.string(m.pkg_name) == r.name
|
||||
and ffi.string(m.pkg_repo) == r.repo
|
||||
and string.format("%d.%d.%d", m.pkg_ver.maj, m.pkg_ver.min, m.pkg_ver.pat) == r.ver
|
||||
and h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") == deps
|
||||
and h.relation_lines(m.pkg_conflicts, "conf_name", "conf_ver") == confs
|
||||
and m.pkg_traits ~= nil and ffi.string(m.pkg_traits) == want_traits
|
||||
istore.gl_free_pkg(m)
|
||||
return ok
|
||||
end
|
||||
|
||||
-- The recipe of a verified bundle copy, as a table, or nil.
|
||||
local function read_recipe(path)
|
||||
local r = ffi.new("gl_recipe_t")
|
||||
if tonumber(istore.gl_recipe_read(path, r)) ~= 0 then return nil end
|
||||
local t = {}
|
||||
for _, k in ipairs({ "name", "ver", "repo", "desc", "maint", "copyright",
|
||||
"sources", "patches", "build", "configure", "traits", "trait_args",
|
||||
"deps", "build_deps", "confs" }) do
|
||||
t[k] = ffi.string(r[k])
|
||||
end
|
||||
istore.gl_recipe_free(r)
|
||||
return t
|
||||
end
|
||||
|
||||
-- The packages a build stacks as layers: BUILD_ROOT, the effective
|
||||
-- BUILD_DEPS and DEPS, and everything they depend on, each from
|
||||
-- `built` (this run's own builds), `kept` (installed source builds,
|
||||
-- M.kept) or the binary repositories, in that order, at a high enough
|
||||
-- version. A list of { name, path, sha (hex) }, top first. Exits if
|
||||
-- one can't be had.
|
||||
local function layer_plan(id, root_names, build_deps, deps, indexes, built, kept)
|
||||
local plan, seen = {}, {}
|
||||
local queue = {}
|
||||
for _, rel in ipairs({ deps, build_deps }) do
|
||||
for n, v in rel:gmatch("(%S+) (%S+)\n") do table.insert(queue, { n, v }) end
|
||||
end
|
||||
for _, n in ipairs(root_names) do table.insert(queue, { n, "0.0.0" }) end
|
||||
local k = 1
|
||||
while k <= #queue do
|
||||
local n, v = queue[k][1], queue[k][2]
|
||||
k = k + 1
|
||||
if not seen[n] then
|
||||
seen[n] = true
|
||||
local b = built[n]
|
||||
if not (b and h.vercmp(b.ver, v) >= 0) then b = kept[n] end
|
||||
if b and b.path and not b.sha then b.sha = h.sha256_file(b.path) or false end
|
||||
if b and b.sha ~= false and h.vercmp(b.ver, v) >= 0 then
|
||||
table.insert(plan, { name = n, ver = b.ver, path = b.path, sha = b.sha })
|
||||
for dn, dv in b.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
else
|
||||
local r, e = h.pick_binary(indexes, n)
|
||||
if not e or h.vercmp(e.ver, v) < 0 then
|
||||
log.lg_printf(2, "%s needs %s %s or later to build, and no binary repository has it.",
|
||||
id, n, v)
|
||||
h.finish(1)
|
||||
end
|
||||
table.insert(plan, { name = n, ver = e.ver, repo = r, entry = e,
|
||||
sha = e.sha256:match("^sha256:(%x+)$") })
|
||||
for dn, dv in e.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return plan
|
||||
end
|
||||
|
||||
--- Check, before the prompt, that every layer a build of plan item `it`
|
||||
--- needs can be had (exits otherwise). `planned` maps the names this
|
||||
--- run builds to { ver, deps }.
|
||||
function M.check_layers(it, indexes, planned, opts)
|
||||
local settings = system_settings(opts)
|
||||
local id = string.format("%s::%s %s", it.repo.name, it.entry.name, it.entry.ver)
|
||||
layer_plan(id, settings.build_root, M.effective(it.entry.build_deps, it.traits),
|
||||
it.deps, indexes, planned, M.kept(opts))
|
||||
end
|
||||
|
||||
--- Build plan item `it` (a -src repository's entry, with it.traits from
|
||||
--- M.traits) into a verified private copy of its .gpkg. `built` maps
|
||||
--- names this run has already built to { ver, path, sha }, for layers.
|
||||
--- Returns the copy and its sidecar text; exits on any failure.
|
||||
function M.build(it, indexes, built, opts)
|
||||
local e, r_src = it.entry, it.repo
|
||||
local id = string.format("%s::%s %s", r_src.name, e.name, e.ver)
|
||||
local settings = system_settings(opts)
|
||||
local owner = settings.build_uid
|
||||
local ctx = h.live_context(opts)
|
||||
local file_mode = opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600"
|
||||
|
||||
-- The bundle: downloaded and hashed against the signed index,
|
||||
-- copied privately, then read and checked by the library.
|
||||
local cached, wrong = h.download(ctx, r_src, e,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
local copy = cached and h.verify_and_copy(cached, e.sha256 .. "\n")
|
||||
local r = copy and read_recipe(copy)
|
||||
if not cached and not wrong then
|
||||
log.lg_printf(2, "Cannot fetch %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
if not r or r.name ~= e.name or r.ver ~= e.ver or r.repo .. "-src" ~= r_src.name
|
||||
or r.deps ~= e.deps or r.confs ~= e.confs or r.build_deps ~= e.build_deps
|
||||
or r.traits ~= e.traits then
|
||||
log.lg_printf(2, "%s does not match its index.", id)
|
||||
if cached then os.remove(cached) end
|
||||
h.finish(1)
|
||||
end
|
||||
if cached then os.remove(cached) end
|
||||
|
||||
local tr = it.traits
|
||||
local deps, confs = M.effective(r.deps, tr), M.effective(r.confs, tr)
|
||||
local build_deps = M.effective(r.build_deps, tr)
|
||||
local flags = flags_for(r.name, opts)
|
||||
|
||||
-- The prefix the package is built for: where its usr/ will be
|
||||
-- linked, so it finds its own files at run time. /usr for base,
|
||||
-- else the scope's links (no -R here: these are runtime paths).
|
||||
local prefix = r.repo == "base" and "/usr" or
|
||||
(ffi.string(ctx.links_path):gsub("/+$", ""))
|
||||
|
||||
-- Sources, by hash, in <base>/build/sources.
|
||||
local sources_dir = build_dir(ctx, "sources")
|
||||
local sources = {}
|
||||
for url, hex in r.sources:gmatch("(%S+) sha256:(%x+)\n") do
|
||||
local file = url:match("([^/]+)$")
|
||||
local dest = sources_dir .. "/" .. hex
|
||||
if h.sha256_file(dest) ~= hex then
|
||||
local ok, mismatch = fetch_source(url, dest, hex)
|
||||
if not ok then
|
||||
log.lg_printf(2, mismatch and "A source of %s does not match its hash."
|
||||
or "Cannot fetch the sources of %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(dest))
|
||||
end
|
||||
table.insert(sources, { file = file, path = dest })
|
||||
end
|
||||
|
||||
-- This build's input (the scope's) and work/output (the builder's).
|
||||
local tag = string.format("%s-%s-%d", r.name, r.ver, ffi.C.gpkg_build_getpid())
|
||||
local in_dir = build_dir(ctx, "in-" .. tag)
|
||||
local work = build_dir(ctx, "work-" .. tag, owner)
|
||||
local out = build_dir(ctx, "out-" .. tag, owner)
|
||||
local function cleanup()
|
||||
remove_tree(in_dir)
|
||||
remove_tree(work)
|
||||
remove_tree(out)
|
||||
end
|
||||
local function fail(fmt, ...)
|
||||
log.lg_printf(2, fmt, ...)
|
||||
cleanup()
|
||||
h.finish(1)
|
||||
end
|
||||
|
||||
-- Layers, extracted once each into <base>/build/layers by the
|
||||
-- builder: NAME-VER-<first 8 of its hash>.
|
||||
local layers_dir = build_dir(ctx, "layers", owner)
|
||||
local layer_names = {}
|
||||
local user_flag = settings.build_user and (" -u " .. h.shell_quote(settings.build_user)) or ""
|
||||
for _, l in ipairs(layer_plan(id, settings.build_root, build_deps, deps, indexes, built,
|
||||
M.kept(opts))) do
|
||||
local lname = string.format("%s-%s-%s", l.name, l.ver, l.sha:sub(1, 8))
|
||||
if not exists(layers_dir .. "/" .. lname) then
|
||||
local path = l.path
|
||||
if l.entry then
|
||||
path = h.download(ctx, l.repo, l.entry,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
if not path then fail("Cannot fetch %s::%s %s.", l.repo.name, l.name, l.ver) end
|
||||
else
|
||||
-- A package this run built: its private copy,
|
||||
-- copied where the builder can read it.
|
||||
local p = in_dir .. "/" .. lname .. ".gpkg"
|
||||
if not copy_file(l.path, p, "644") then
|
||||
fail("Cannot write %s.", p)
|
||||
end
|
||||
path = p
|
||||
end
|
||||
local tmp = string.format(".%s.%d", lname, ffi.C.gpkg_build_getpid())
|
||||
if not sh_ok(h.gsandbox .. " -X" .. user_flag .. " -p " .. h.shell_quote(path) ..
|
||||
" -d " .. h.shell_quote(layers_dir .. "/" .. tmp)) then
|
||||
fail("Cannot extract %s %s for %s.", l.name, l.ver, id)
|
||||
end
|
||||
if not os.rename(layers_dir .. "/" .. tmp, layers_dir .. "/" .. lname) then
|
||||
remove_tree(layers_dir .. "/" .. tmp)
|
||||
end
|
||||
end
|
||||
table.insert(layer_names, lname)
|
||||
end
|
||||
|
||||
-- The input: bundle, sources, manifest, env, driver.
|
||||
local function put(name, text)
|
||||
local f = io.open(in_dir .. "/" .. name, "wb")
|
||||
if not f or not f:write(text) then fail("Cannot write %s/%s.", in_dir, name) end
|
||||
f:close()
|
||||
if not sh_ok("chmod 644 " .. h.shell_quote(in_dir .. "/" .. name)) then
|
||||
fail("Cannot write %s/%s.", in_dir, name)
|
||||
end
|
||||
end
|
||||
if not copy_file(copy, in_dir .. "/bundle.grcp", "644") or
|
||||
not sh_ok("mkdir -m 755 " .. h.shell_quote(in_dir .. "/sources")) then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
local files = {}
|
||||
for _, s in ipairs(sources) do
|
||||
if not copy_file(s.path, in_dir .. "/sources/" .. s.file, "644") then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
table.insert(files, s.file)
|
||||
end
|
||||
put("manifest.gpm.cfg", manifest_text(r, deps, confs, tr))
|
||||
put("env", env_text(r, flags, tr, files, prefix))
|
||||
local df = io.open(h.lib_dir .. "/build-driver.sh", "rb")
|
||||
local driver = df and df:read("*a")
|
||||
if df then df:close() end
|
||||
if not driver then fail("Cannot read %s/build-driver.sh.", h.lib_dir) end
|
||||
put("driver", driver)
|
||||
|
||||
-- The build. Its output goes to <base>/build/logs/NAME-VER.log,
|
||||
-- and to the terminal unless -q.
|
||||
local log_path = build_dir(ctx, "logs") .. "/" .. r.name .. "-" .. r.ver .. ".log"
|
||||
local cmd = h.gsandbox .. " -R" .. user_flag .. " -L " .. h.shell_quote(layers_dir)
|
||||
for _, l in ipairs(layer_names) do cmd = cmd .. " -l " .. h.shell_quote(l) end
|
||||
cmd = cmd .. " -w " .. h.shell_quote(work) .. " -i " .. h.shell_quote(in_dir) ..
|
||||
" -o " .. h.shell_quote(out)
|
||||
log.lg_printf(0, "Building %s%s...", id, tr.show ~= "" and (" " .. tr.show) or "")
|
||||
local st
|
||||
if not h.quiet() then
|
||||
local stf = in_dir .. "/.status"
|
||||
os.execute("{ " .. cmd .. " 2>&1; echo $? > " .. h.shell_quote(stf) .. "; } | tee "
|
||||
.. h.shell_quote(log_path))
|
||||
local f = io.open(stf, "r")
|
||||
st = f and tonumber(f:read("*l")) or 255
|
||||
if f then f:close() end
|
||||
else
|
||||
st = status_of(cmd .. " > " .. h.shell_quote(log_path) .. " 2>&1")
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(log_path))
|
||||
if st ~= 0 then
|
||||
fail("Building %s failed. Log: %s", id, log_path)
|
||||
end
|
||||
|
||||
-- The output, untrusted: copied privately while hashed, then its
|
||||
-- manifest must be exactly the one handed in.
|
||||
local result = out .. "/" .. r.name .. "-" .. r.ver .. ".gpkg"
|
||||
local hex = h.sha256_file(result)
|
||||
local pkg = hex and h.verify_and_copy(result, "sha256:" .. hex .. "\n")
|
||||
if not pkg or not output_matches(pkg, r, deps, confs, tr) then
|
||||
fail("%s: the build produced another package.", id)
|
||||
end
|
||||
cleanup()
|
||||
log.lg_printf(3, "Built %s.", id)
|
||||
return pkg, "sha256:" .. hex .. "\n", hex
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Kept packages: the verified .gpkg of each installed source build,
|
||||
-- the bytes that were installed, so a later build can use it as a
|
||||
-- layer (no binary repository has it). <base>/build/packages/
|
||||
-- REPO@NAME-VER.gpkg ('@' is in no name), kept while that version is
|
||||
-- installed and built from source.
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function kept_name(repo, name, ver)
|
||||
return repo .. "@" .. name .. "-" .. ver .. ".gpkg"
|
||||
end
|
||||
|
||||
-- <base>/build/packages, or nil if there is none (not made here).
|
||||
local function kept_dir(ctx)
|
||||
local d = ffi.string(ctx.store_path):gsub("/store$", "") .. "/build/packages"
|
||||
return exists(d) and d or nil
|
||||
end
|
||||
|
||||
-- The kept packages of the installed source builds: the file names a
|
||||
-- prune leaves, and (for layer_plan) name -> { ver, path, deps }.
|
||||
local function kept_scan(opts)
|
||||
local ctx = h.live_context(opts)
|
||||
local dir = kept_dir(ctx)
|
||||
if not dir then return nil, {}, {} end
|
||||
local want, by_name = {}, {}
|
||||
for _, i in ipairs(h.installed_entries(opts)) do
|
||||
if h.built_from(ctx, i.repo, i.name) then
|
||||
local f = kept_name(i.repo, i.name, i.ver)
|
||||
local path = dir .. "/" .. f
|
||||
want[f] = true
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if exists(path) and istore.gl_read_pkg_manifest(path, m) == 0 then
|
||||
by_name[i.name] = { ver = i.ver, path = path,
|
||||
deps = h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") }
|
||||
istore.gl_free_pkg(m)
|
||||
end
|
||||
end
|
||||
end
|
||||
return dir, want, by_name
|
||||
end
|
||||
|
||||
--- The installed source builds a build can use as layers: name ->
|
||||
--- { ver, path, deps }.
|
||||
function M.kept(opts)
|
||||
local _, _, by_name = kept_scan(opts)
|
||||
return by_name
|
||||
end
|
||||
|
||||
--- After a commit: keep the private copy `path` of a package this run
|
||||
--- built and installed (repo::name ver). A failure only warns: the
|
||||
--- package is installed, and only later builds would miss it.
|
||||
function M.keep(opts, path, repo, name, ver)
|
||||
local ctx = h.live_context(opts)
|
||||
local out = ffi.new("char[4096]")
|
||||
local ok = txn.gl_scope_build_dir(ctx, "packages", -1, out) == 0
|
||||
local dest = ok and (ffi.string(out) .. "/" .. kept_name(repo, name, ver))
|
||||
if not ok or not copy_file(path, dest .. ".part",
|
||||
opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600")
|
||||
or not os.rename(dest .. ".part", dest) then
|
||||
if dest then os.remove(dest .. ".part") end
|
||||
log.lg_printf(1, "Could not keep %s::%s %s for later builds.", repo, name, ver)
|
||||
end
|
||||
end
|
||||
|
||||
--- After a commit: delete the kept packages that are no longer an
|
||||
--- installed source build (removed, replaced, or reinstalled from a
|
||||
--- binary repository).
|
||||
function M.prune_kept(opts)
|
||||
local dir, want = kept_scan(opts)
|
||||
if not dir then return end
|
||||
local p = io.popen("ls -A -- " .. h.shell_quote(dir))
|
||||
for f in p:lines() do
|
||||
if not want[f] then os.remove(dir .. "/" .. f) end
|
||||
end
|
||||
p:close()
|
||||
end
|
||||
|
||||
ffi.cdef[[
|
||||
int gpkg_build_getpid(void) asm("getpid");
|
||||
]]
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,694 @@
|
||||
-- lib/gpkg_build.lua
|
||||
--
|
||||
-- The package builder (BUILD_DESIGN.txt): turns a recipe from a NAME-src
|
||||
-- repository into a .gpkg, built in gsandbox, which gpkg_common then
|
||||
-- installs like any fetched package. Used only through gpkg_common.
|
||||
--
|
||||
-- Nothing a build produces is trusted: the output is copied privately
|
||||
-- while hashed and its manifest (read by the library) must be exactly
|
||||
-- the one handed in. What IS trusted is what decides the build: the
|
||||
-- bundle (hash from the signed index, then checked by gl_recipe_read),
|
||||
-- each source (the recipe's SHA256), the layers (packages verified
|
||||
-- against their signed indexes) and this file and build-driver.sh.
|
||||
|
||||
local ffi = require("ffi")
|
||||
local bit = require("bit")
|
||||
|
||||
local M = {}
|
||||
|
||||
-- Set by M.init(): FFI handles and gpkg_common's helpers.
|
||||
local log, istore, txn, h
|
||||
|
||||
function M.init(env)
|
||||
log, istore, txn = env.log, env.istore, env.txn
|
||||
h = env
|
||||
end
|
||||
|
||||
local SYSTEM_CONF = "/etc/glacier/glacier.conf"
|
||||
local SYSTEM_CONFIG_DIR = "/etc/glacier/config"
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Small helpers
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function lines(s)
|
||||
local out = {}
|
||||
for l in (s or ""):gmatch("([^\n]*)\n") do table.insert(out, l) end
|
||||
return out
|
||||
end
|
||||
|
||||
local function exists(path)
|
||||
local f = io.open(path, "rb")
|
||||
if f then f:close() return true end
|
||||
local ok = os.execute("test -e " .. h.shell_quote(path))
|
||||
return ok == true or ok == 0
|
||||
end
|
||||
|
||||
-- The exit status of a shell command line.
|
||||
local function status_of(cmd)
|
||||
local p = io.popen(cmd .. "; echo \"rc=$?\"")
|
||||
local out = p:read("*a")
|
||||
p:close()
|
||||
return tonumber(out:match("rc=(%d+)%s*$")) or 255
|
||||
end
|
||||
|
||||
local function sh_ok(cmd)
|
||||
return status_of(cmd .. " >/dev/null 2>&1") == 0
|
||||
end
|
||||
|
||||
-- A libconfig string literal.
|
||||
local function cfg_string(s)
|
||||
return '"' .. s:gsub("\\", "\\\\"):gsub('"', '\\"') .. '"'
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Settings (BUILD_DESIGN.txt section 5)
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- One configuration file's build settings, or nil if it doesn't exist.
|
||||
-- Exits if it is malformed (or, read by syspkg, untrusted).
|
||||
local conf_cache = {}
|
||||
local function read_conf(path, kind, opts)
|
||||
if conf_cache[path] ~= nil then return conf_cache[path] or nil end
|
||||
if not exists(path) then
|
||||
conf_cache[path] = false
|
||||
return nil
|
||||
end
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(path)
|
||||
end
|
||||
local c = ffi.new("gl_build_conf_t")
|
||||
if tonumber(istore.gl_conf_build(path, kind, c)) ~= 0 then
|
||||
log.lg_printf(2, "Malformed %s.", path)
|
||||
h.finish(1)
|
||||
end
|
||||
local function str(v) return v ~= nil and ffi.string(v) or nil end
|
||||
local r = {
|
||||
traits = lines(str(c.traits)), cflags = str(c.cflags),
|
||||
cxxflags = str(c.cxxflags), ldflags = str(c.ldflags),
|
||||
configure = lines(str(c.configure)),
|
||||
jobs = c.jobs > 0 and tonumber(c.jobs) or nil,
|
||||
build_user = str(c.build_user), build_root = lines(str(c.build_root)),
|
||||
path = path,
|
||||
}
|
||||
istore.gl_build_conf_free(c)
|
||||
conf_cache[path] = r
|
||||
return r
|
||||
end
|
||||
|
||||
-- The user's base directory (<base>, beside index and store).
|
||||
local function user_base(opts)
|
||||
return string.format("/glacier/usr/%d", opts.uid)
|
||||
end
|
||||
|
||||
-- The configuration layers for package `name`, lowest first:
|
||||
-- { global = {...}, package = {...} } lists of read_conf results.
|
||||
local function conf_layers(name, opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
return { sys }, read_conf(SYSTEM_CONFIG_DIR .. "/" .. name .. ".conf",
|
||||
ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
local base = user_base(opts) .. "/config"
|
||||
return { sys, read_conf(base .. "/glacier.conf", ffi.C.GL_CONF_USER, opts) },
|
||||
read_conf(base .. "/" .. name .. ".conf", ffi.C.GL_CONF_PACKAGE, opts)
|
||||
end
|
||||
|
||||
-- The system's settings a build can't do without: BUILD_ROOT, and for
|
||||
-- syspkg BUILD_USER (its uid too). Exits saying which is missing.
|
||||
local function system_settings(opts)
|
||||
local sys = read_conf(SYSTEM_CONF, ffi.C.GL_CONF_SYSTEM, opts)
|
||||
if not sys then
|
||||
log.lg_printf(2, "Cannot read %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
if #sys.build_root == 0 then
|
||||
log.lg_printf(2, "BUILD_ROOT is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local s = { build_root = sys.build_root }
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
if not sys.build_user then
|
||||
log.lg_printf(2, "BUILD_USER is not set in %s.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
local p = io.popen("id -u -- " .. h.shell_quote(sys.build_user) .. " 2>/dev/null")
|
||||
local uid = tonumber(p and p:read("*l") or "")
|
||||
if p then p:close() end
|
||||
if not uid then
|
||||
log.lg_printf(2, "BUILD_USER %s in %s is not a user.", sys.build_user,
|
||||
SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
elseif uid == 0 then
|
||||
log.lg_printf(2, "BUILD_USER in %s must not be root.", SYSTEM_CONF)
|
||||
h.finish(1)
|
||||
end
|
||||
s.build_user, s.build_uid = sys.build_user, uid
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
--- The effective traits of building `name`, whose recipe (or -src index
|
||||
--- entry) declares `declared` ("NAME D\n" lines). Returns a table
|
||||
--- { on = { trait = bool }, list = sorted { "ssl", "-ldap" }, show =
|
||||
--- "[+ssl -ldap]" (or "" with no traits) }. Exits if a per-package file
|
||||
--- names a trait the recipe doesn't declare.
|
||||
function M.traits(name, declared, opts)
|
||||
local on, names = {}, {}
|
||||
for _, l in ipairs(lines(declared)) do
|
||||
local t, d = l:match("^(%S+) ([01])$")
|
||||
on[t] = d == "1"
|
||||
table.insert(names, t)
|
||||
end
|
||||
local function apply(conf, strict)
|
||||
if not conf then return end
|
||||
for _, s in ipairs(conf.traits) do
|
||||
local neg = s:sub(1, 1) == "-"
|
||||
local t = neg and s:sub(2) or s
|
||||
if on[t] ~= nil then
|
||||
on[t] = not neg
|
||||
elseif strict then
|
||||
log.lg_printf(2, "%s: %s has no trait %s.", conf.path, name, t)
|
||||
h.finish(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
for _, g in ipairs(globals) do apply(g, false) end
|
||||
apply(pkg, true)
|
||||
|
||||
table.sort(names)
|
||||
local list, show = {}, {}
|
||||
for _, t in ipairs(names) do
|
||||
table.insert(list, on[t] and t or ("-" .. t))
|
||||
table.insert(show, (on[t] and "+" or "-") .. t)
|
||||
end
|
||||
return { on = on, list = list,
|
||||
show = #show > 0 and ("[" .. table.concat(show, " ") .. "]") or "" }
|
||||
end
|
||||
|
||||
--- Relation lines ("NAME VER[ IF]\n") with their IF resolved against
|
||||
--- traits `tr`: "NAME VER\n" for each that applies.
|
||||
function M.effective(rel, tr)
|
||||
local out = {}
|
||||
for _, l in ipairs(lines(rel)) do
|
||||
local n, v, c = l:match("^(%S+) (%S+) ?(%S*)$")
|
||||
local applies = c == "" or
|
||||
(c:sub(1, 1) == "-" and not tr.on[c:sub(2)]) or
|
||||
(c:sub(1, 1) ~= "-" and tr.on[c])
|
||||
if applies then table.insert(out, n .. " " .. v .. "\n") end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
--- Check, before the prompt, everything a build of `name` needs from
|
||||
--- the configuration (exits on a problem): BUILD_ROOT, BUILD_USER.
|
||||
function M.check(opts)
|
||||
if opts.sysroot then
|
||||
log.lg_printf(2, "Building from source under -R is not supported.")
|
||||
h.finish(1)
|
||||
end
|
||||
system_settings(opts)
|
||||
-- gsandbox and the driver decide what a build produces, which
|
||||
-- root then installs: checked like syspkg's own files, here
|
||||
-- rather than at startup, since only builds need them.
|
||||
if opts.scope == ffi.C.GL_SCOPE_SYS then
|
||||
h.require_trusted(h.gsandbox_path)
|
||||
h.require_trusted(h.lib_dir .. "/build-driver.sh")
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- The build
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
-- <base>/build/NAME, made and checked by the library; owned by the
|
||||
-- builder (`owner` = BUILD_USER's uid, syspkg) or the scope.
|
||||
local function build_dir(ctx, name, owner)
|
||||
local out = ffi.new("char[4096]")
|
||||
if txn.gl_scope_build_dir(ctx, name, owner or -1, out) ~= 0 then
|
||||
h.finish(1) -- the library said why
|
||||
end
|
||||
return ffi.string(out)
|
||||
end
|
||||
|
||||
-- Remove a build's directory tree (the builder's files may be
|
||||
-- read-only to it: overlayfs's work dir is mode 000).
|
||||
local function remove_tree(path)
|
||||
os.execute("chmod -R u+rwX " .. h.shell_quote(path) .. " 2>/dev/null; rm -rf "
|
||||
.. h.shell_quote(path))
|
||||
end
|
||||
|
||||
-- Copy a file (cp, then the given mode).
|
||||
local function copy_file(src, dst, mode)
|
||||
return sh_ok("cp -- " .. h.shell_quote(src) .. " " .. h.shell_quote(dst))
|
||||
and sh_ok("chmod " .. mode .. " " .. h.shell_quote(dst))
|
||||
end
|
||||
|
||||
-- Fetch a source URL (https through curl, file:// a copy) into dest
|
||||
-- hashed: true if it arrived and has hash `hex`. Redirects are
|
||||
-- followed, https only (release tarballs are mostly served that way);
|
||||
-- the hash decides.
|
||||
local MAX_SOURCE = 4 * 1024 * 1024 * 1024
|
||||
local function fetch_source(url, dest, hex)
|
||||
local tmp = dest .. ".part"
|
||||
os.remove(tmp)
|
||||
local cmd
|
||||
local path = url:match("^file://(/.*)$")
|
||||
if path then
|
||||
cmd = "cp -- " .. h.shell_quote(path) .. " " .. h.shell_quote(tmp)
|
||||
else
|
||||
cmd = "curl -sfL --max-redirs 5 --proto =https --proto-redir =https --connect-timeout 30" ..
|
||||
" --max-filesize " .. MAX_SOURCE .. " -o " .. h.shell_quote(tmp) ..
|
||||
" -- " .. h.shell_quote(url)
|
||||
end
|
||||
if not sh_ok(cmd) then
|
||||
os.remove(tmp)
|
||||
return false, false
|
||||
end
|
||||
if h.sha256_file(tmp) ~= hex then
|
||||
os.remove(tmp)
|
||||
return false, true
|
||||
end
|
||||
return os.rename(tmp, dest) ~= nil
|
||||
end
|
||||
|
||||
-- The libconfig manifest a build must produce.
|
||||
local function manifest_text(r, deps, confs, tr)
|
||||
local function rel(text)
|
||||
local out = {}
|
||||
for n, v in text:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(out, string.format('{ PKG_NAME = "%s"; PKG_VER = "%s"; }', n, v))
|
||||
end
|
||||
return "(" .. table.concat(out, ", ") .. ")"
|
||||
end
|
||||
local t = {}
|
||||
for _, s in ipairs(tr.list) do table.insert(t, '"' .. s .. '"') end
|
||||
return table.concat({
|
||||
"PKG_NAME = " .. cfg_string(r.name) .. ";",
|
||||
"PKG_VER = " .. cfg_string(r.ver) .. ";",
|
||||
"PKG_DESC = " .. cfg_string(r.desc) .. ";",
|
||||
"PKG_MAINT = " .. cfg_string(r.maint) .. ";",
|
||||
"PKG_COPYRIGHT = " .. cfg_string(r.copyright) .. ";",
|
||||
"PKG_REPO = " .. cfg_string(r.repo) .. ";",
|
||||
"PKG_DEPS = " .. rel(deps) .. ";",
|
||||
"PKG_CONFS = " .. rel(confs) .. ";",
|
||||
"PKG_TRAITS = [ " .. table.concat(t, ", ") .. " ];",
|
||||
}, "\n") .. "\n"
|
||||
end
|
||||
|
||||
-- The driver's env file: shell assignments, every value quoted.
|
||||
local function env_text(r, settings, tr, sources, prefix)
|
||||
local q = h.shell_quote
|
||||
local args = {}
|
||||
for _, a in ipairs(lines(r.configure)) do table.insert(args, q(a)) end
|
||||
for _, l in ipairs(lines(r.trait_args)) do
|
||||
local t, side, a = l:match("^([^\t]+)\t([+-])\t(.*)$")
|
||||
if (side == "+") == (tr.on[t] == true) then table.insert(args, q(a)) end
|
||||
end
|
||||
for _, a in ipairs(settings.configure) do table.insert(args, q(a)) end
|
||||
local on = {}
|
||||
for _, s in ipairs(tr.list) do
|
||||
if s:sub(1, 1) ~= "-" then table.insert(on, s) end
|
||||
end
|
||||
local function kv(k, v) return k .. "=" .. q(v or "") .. "\n" end
|
||||
return kv("NAME", r.name) .. kv("VER", r.ver) .. kv("BUILD", r.build) ..
|
||||
kv("PREFIX", prefix) ..
|
||||
kv("SOURCES", table.concat(sources, " ")) ..
|
||||
kv("PATCHES", table.concat(lines(r.patches), " ")) ..
|
||||
kv("CONFIGURE_ARGS", table.concat(args, " ")) ..
|
||||
kv("TRAITS", table.concat(on, " ")) ..
|
||||
kv("JOBS", tostring(settings.jobs)) ..
|
||||
kv("CFLAGS", settings.cflags) .. kv("CXXFLAGS", settings.cxxflags) ..
|
||||
kv("LDFLAGS", settings.ldflags)
|
||||
end
|
||||
|
||||
-- CFLAGS/CXXFLAGS/LDFLAGS/CONFIGURE/JOBS for package `name`: the
|
||||
-- per-package value replaces the global one; the user's global replaces
|
||||
-- the system's.
|
||||
local function flags_for(name, opts)
|
||||
local globals, pkg = conf_layers(name, opts)
|
||||
local s = { configure = pkg and pkg.configure or {}, jobs = 1 }
|
||||
for _, k in ipairs({ "cflags", "cxxflags", "ldflags" }) do
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g[k] then s[k] = g[k] end
|
||||
end
|
||||
if pkg and pkg[k] then s[k] = pkg[k] end
|
||||
end
|
||||
for _, g in ipairs(globals) do
|
||||
if g and g.jobs then s.jobs = g.jobs end
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
-- Whether the private copy `path`'s manifest (read by the library) is
|
||||
-- exactly what the build was handed.
|
||||
local function output_matches(path, r, deps, confs, tr)
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if istore.gl_read_pkg_manifest(path, m) ~= 0 then return false end
|
||||
local want_traits = #tr.list > 0 and (table.concat(tr.list, "\n") .. "\n") or ""
|
||||
local ok = ffi.string(m.pkg_name) == r.name
|
||||
and ffi.string(m.pkg_repo) == r.repo
|
||||
and string.format("%d.%d.%d", m.pkg_ver.maj, m.pkg_ver.min, m.pkg_ver.pat) == r.ver
|
||||
and h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") == deps
|
||||
and h.relation_lines(m.pkg_conflicts, "conf_name", "conf_ver") == confs
|
||||
and m.pkg_traits ~= nil and ffi.string(m.pkg_traits) == want_traits
|
||||
istore.gl_free_pkg(m)
|
||||
return ok
|
||||
end
|
||||
|
||||
-- The recipe of a verified bundle copy, as a table, or nil and why.
|
||||
local function read_recipe(path)
|
||||
local r = ffi.new("gl_recipe_t")
|
||||
if tonumber(istore.gl_recipe_read(path, r)) ~= 0 then
|
||||
return nil, ffi.string(istore.gl_recipe_error())
|
||||
end
|
||||
local t = {}
|
||||
for _, k in ipairs({ "name", "ver", "repo", "desc", "maint", "copyright",
|
||||
"sources", "patches", "build", "configure", "traits", "trait_args",
|
||||
"deps", "build_deps", "confs" }) do
|
||||
t[k] = ffi.string(r[k])
|
||||
end
|
||||
istore.gl_recipe_free(r)
|
||||
return t
|
||||
end
|
||||
|
||||
-- The packages a build stacks as layers: BUILD_ROOT, the effective
|
||||
-- BUILD_DEPS and DEPS, and everything they depend on, each from
|
||||
-- `built` (this run's own builds), `kept` (installed source builds,
|
||||
-- M.kept) or the binary repositories, in that order, at a high enough
|
||||
-- version. A list of { name, path, sha (hex) }, top first. Exits if
|
||||
-- one can't be had.
|
||||
local function layer_plan(id, root_names, build_deps, deps, indexes, built, kept)
|
||||
local plan, seen = {}, {}
|
||||
local queue = {}
|
||||
for _, rel in ipairs({ deps, build_deps }) do
|
||||
for n, v in rel:gmatch("(%S+) (%S+)\n") do table.insert(queue, { n, v }) end
|
||||
end
|
||||
for _, n in ipairs(root_names) do table.insert(queue, { n, "0.0.0" }) end
|
||||
local k = 1
|
||||
while k <= #queue do
|
||||
local n, v = queue[k][1], queue[k][2]
|
||||
k = k + 1
|
||||
if not seen[n] then
|
||||
seen[n] = true
|
||||
local b = built[n]
|
||||
if not (b and h.vercmp(b.ver, v) >= 0) then b = kept[n] end
|
||||
if b and b.path and not b.sha then b.sha = h.sha256_file(b.path) or false end
|
||||
if b and b.sha ~= false and h.vercmp(b.ver, v) >= 0 then
|
||||
table.insert(plan, { name = n, ver = b.ver, path = b.path, sha = b.sha })
|
||||
for dn, dv in b.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
else
|
||||
local r, e = h.pick_binary(indexes, n)
|
||||
if not e or h.vercmp(e.ver, v) < 0 then
|
||||
log.lg_printf(2, "%s needs %s %s or later to build, and no binary repository has it.",
|
||||
id, n, v)
|
||||
h.finish(1)
|
||||
end
|
||||
table.insert(plan, { name = n, ver = e.ver, repo = r, entry = e,
|
||||
sha = e.sha256:match("^sha256:(%x+)$") })
|
||||
for dn, dv in e.deps:gmatch("(%S+) (%S+)\n") do
|
||||
table.insert(queue, { dn, dv })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return plan
|
||||
end
|
||||
|
||||
--- Check, before the prompt, that every layer a build of plan item `it`
|
||||
--- needs can be had (exits otherwise). `planned` maps the names this
|
||||
--- run builds to { ver, deps }.
|
||||
function M.check_layers(it, indexes, planned, opts)
|
||||
local settings = system_settings(opts)
|
||||
local id = string.format("%s::%s %s", it.repo.name, it.entry.name, it.entry.ver)
|
||||
layer_plan(id, settings.build_root, M.effective(it.entry.build_deps, it.traits),
|
||||
it.deps, indexes, planned, M.kept(opts))
|
||||
end
|
||||
|
||||
--- Build plan item `it` (a -src repository's entry, with it.traits from
|
||||
--- M.traits) into a verified private copy of its .gpkg. `built` maps
|
||||
--- names this run has already built to { ver, path, sha }, for layers.
|
||||
--- Returns the copy and its sidecar text; exits on any failure.
|
||||
function M.build(it, indexes, built, opts)
|
||||
local e, r_src = it.entry, it.repo
|
||||
local id = string.format("%s::%s %s", r_src.name, e.name, e.ver)
|
||||
local settings = system_settings(opts)
|
||||
local owner = settings.build_uid
|
||||
local ctx = h.live_context(opts)
|
||||
local file_mode = opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600"
|
||||
|
||||
-- The bundle: downloaded and hashed against the signed index,
|
||||
-- copied privately, then read and checked by the library.
|
||||
local cached, wrong = h.download(ctx, r_src, e,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
local copy = cached and h.verify_and_copy(cached, e.sha256 .. "\n")
|
||||
local r, bad = nil, nil
|
||||
if copy then r, bad = read_recipe(copy) end
|
||||
if not cached and not wrong then
|
||||
log.lg_printf(2, "Cannot fetch %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
if bad then
|
||||
log.lg_printf(2, "%s has a malformed recipe bundle: %s.", id, bad)
|
||||
if cached then os.remove(cached) end
|
||||
h.finish(1)
|
||||
end
|
||||
if not r or r.name ~= e.name or r.ver ~= e.ver or r.repo .. "-src" ~= r_src.name
|
||||
or r.deps ~= e.deps or r.confs ~= e.confs or r.build_deps ~= e.build_deps
|
||||
or r.traits ~= e.traits then
|
||||
log.lg_printf(2, "%s does not match its index.", id)
|
||||
if cached then os.remove(cached) end
|
||||
h.finish(1)
|
||||
end
|
||||
if cached then os.remove(cached) end
|
||||
|
||||
local tr = it.traits
|
||||
local deps, confs = M.effective(r.deps, tr), M.effective(r.confs, tr)
|
||||
local build_deps = M.effective(r.build_deps, tr)
|
||||
local flags = flags_for(r.name, opts)
|
||||
|
||||
-- The prefix the package is built for: where its usr/ will be
|
||||
-- linked, so it finds its own files at run time. /usr for base,
|
||||
-- else the scope's links (no -R here: these are runtime paths).
|
||||
local prefix = r.repo == "base" and "/usr" or
|
||||
(ffi.string(ctx.links_path):gsub("/+$", ""))
|
||||
|
||||
-- Sources, by hash, in <base>/build/sources.
|
||||
local sources_dir = build_dir(ctx, "sources")
|
||||
local sources = {}
|
||||
for url, hex in r.sources:gmatch("(%S+) sha256:(%x+)\n") do
|
||||
local file = url:match("([^/]+)$")
|
||||
local dest = sources_dir .. "/" .. hex
|
||||
if h.sha256_file(dest) ~= hex then
|
||||
local ok, mismatch = fetch_source(url, dest, hex)
|
||||
if not ok then
|
||||
log.lg_printf(2, mismatch and "A source of %s does not match its hash."
|
||||
or "Cannot fetch the sources of %s.", id)
|
||||
h.finish(1)
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(dest))
|
||||
end
|
||||
table.insert(sources, { file = file, path = dest })
|
||||
end
|
||||
|
||||
-- This build's input (the scope's) and work/output (the builder's).
|
||||
local tag = string.format("%s-%s-%d", r.name, r.ver, ffi.C.gpkg_build_getpid())
|
||||
local in_dir = build_dir(ctx, "in-" .. tag)
|
||||
local work = build_dir(ctx, "work-" .. tag, owner)
|
||||
local out = build_dir(ctx, "out-" .. tag, owner)
|
||||
local function cleanup()
|
||||
remove_tree(in_dir)
|
||||
remove_tree(work)
|
||||
remove_tree(out)
|
||||
end
|
||||
local function fail(fmt, ...)
|
||||
log.lg_printf(2, fmt, ...)
|
||||
cleanup()
|
||||
h.finish(1)
|
||||
end
|
||||
|
||||
-- Layers, extracted once each into <base>/build/layers by the
|
||||
-- builder: NAME-VER-<first 8 of its hash>.
|
||||
local layers_dir = build_dir(ctx, "layers", owner)
|
||||
local layer_names = {}
|
||||
local user_flag = settings.build_user and (" -u " .. h.shell_quote(settings.build_user)) or ""
|
||||
for _, l in ipairs(layer_plan(id, settings.build_root, build_deps, deps, indexes, built,
|
||||
M.kept(opts))) do
|
||||
local lname = string.format("%s-%s-%s", l.name, l.ver, l.sha:sub(1, 8))
|
||||
if not exists(layers_dir .. "/" .. lname) then
|
||||
local path = l.path
|
||||
if l.entry then
|
||||
path = h.download(ctx, l.repo, l.entry,
|
||||
tonumber(opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600", 8))
|
||||
if not path then fail("Cannot fetch %s::%s %s.", l.repo.name, l.name, l.ver) end
|
||||
else
|
||||
-- A package this run built: its private copy,
|
||||
-- copied where the builder can read it.
|
||||
local p = in_dir .. "/" .. lname .. ".gpkg"
|
||||
if not copy_file(l.path, p, "644") then
|
||||
fail("Cannot write %s.", p)
|
||||
end
|
||||
path = p
|
||||
end
|
||||
local tmp = string.format(".%s.%d", lname, ffi.C.gpkg_build_getpid())
|
||||
if not sh_ok(h.gsandbox .. " -X" .. user_flag .. " -p " .. h.shell_quote(path) ..
|
||||
" -d " .. h.shell_quote(layers_dir .. "/" .. tmp)) then
|
||||
fail("Cannot extract %s %s for %s.", l.name, l.ver, id)
|
||||
end
|
||||
if not os.rename(layers_dir .. "/" .. tmp, layers_dir .. "/" .. lname) then
|
||||
remove_tree(layers_dir .. "/" .. tmp)
|
||||
end
|
||||
end
|
||||
table.insert(layer_names, lname)
|
||||
end
|
||||
|
||||
-- The input: bundle, sources, manifest, env, driver.
|
||||
local function put(name, text)
|
||||
local f = io.open(in_dir .. "/" .. name, "wb")
|
||||
if not f or not f:write(text) then fail("Cannot write %s/%s.", in_dir, name) end
|
||||
f:close()
|
||||
if not sh_ok("chmod 644 " .. h.shell_quote(in_dir .. "/" .. name)) then
|
||||
fail("Cannot write %s/%s.", in_dir, name)
|
||||
end
|
||||
end
|
||||
if not copy_file(copy, in_dir .. "/bundle.grcp", "644") or
|
||||
not sh_ok("mkdir -m 755 " .. h.shell_quote(in_dir .. "/sources")) then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
local files = {}
|
||||
for _, s in ipairs(sources) do
|
||||
if not copy_file(s.path, in_dir .. "/sources/" .. s.file, "644") then
|
||||
fail("Cannot write %s.", in_dir)
|
||||
end
|
||||
table.insert(files, s.file)
|
||||
end
|
||||
put("manifest.gpm.cfg", manifest_text(r, deps, confs, tr))
|
||||
put("env", env_text(r, flags, tr, files, prefix))
|
||||
local df = io.open(h.lib_dir .. "/build-driver.sh", "rb")
|
||||
local driver = df and df:read("*a")
|
||||
if df then df:close() end
|
||||
if not driver then fail("Cannot read %s/build-driver.sh.", h.lib_dir) end
|
||||
put("driver", driver)
|
||||
|
||||
-- The build. Its output goes to <base>/build/logs/NAME-VER.log,
|
||||
-- and to the terminal unless -q.
|
||||
local log_path = build_dir(ctx, "logs") .. "/" .. r.name .. "-" .. r.ver .. ".log"
|
||||
local cmd = h.gsandbox .. " -R" .. user_flag .. " -L " .. h.shell_quote(layers_dir)
|
||||
for _, l in ipairs(layer_names) do cmd = cmd .. " -l " .. h.shell_quote(l) end
|
||||
cmd = cmd .. " -w " .. h.shell_quote(work) .. " -i " .. h.shell_quote(in_dir) ..
|
||||
" -o " .. h.shell_quote(out)
|
||||
log.lg_printf(0, "Building %s%s...", id, tr.show ~= "" and (" " .. tr.show) or "")
|
||||
local st
|
||||
if not h.quiet() then
|
||||
local stf = in_dir .. "/.status"
|
||||
os.execute("{ " .. cmd .. " 2>&1; echo $? > " .. h.shell_quote(stf) .. "; } | tee "
|
||||
.. h.shell_quote(log_path))
|
||||
local f = io.open(stf, "r")
|
||||
st = f and tonumber(f:read("*l")) or 255
|
||||
if f then f:close() end
|
||||
else
|
||||
st = status_of(cmd .. " > " .. h.shell_quote(log_path) .. " 2>&1")
|
||||
end
|
||||
sh_ok("chmod " .. file_mode .. " " .. h.shell_quote(log_path))
|
||||
if st ~= 0 then
|
||||
fail("Building %s failed. Log: %s", id, log_path)
|
||||
end
|
||||
|
||||
-- The output, untrusted: copied privately while hashed, then its
|
||||
-- manifest must be exactly the one handed in.
|
||||
local result = out .. "/" .. r.name .. "-" .. r.ver .. ".gpkg"
|
||||
local hex = h.sha256_file(result)
|
||||
local pkg = hex and h.verify_and_copy(result, "sha256:" .. hex .. "\n")
|
||||
if not pkg or not output_matches(pkg, r, deps, confs, tr) then
|
||||
fail("%s: the build produced another package.", id)
|
||||
end
|
||||
cleanup()
|
||||
log.lg_printf(3, "Built %s.", id)
|
||||
return pkg, "sha256:" .. hex .. "\n", hex
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------
|
||||
-- Kept packages: the verified .gpkg of each installed source build,
|
||||
-- the bytes that were installed, so a later build can use it as a
|
||||
-- layer (no binary repository has it). <base>/build/packages/
|
||||
-- REPO@NAME-VER.gpkg ('@' is in no name), kept while that version is
|
||||
-- installed and built from source.
|
||||
-- ---------------------------------------------------------------------
|
||||
|
||||
local function kept_name(repo, name, ver)
|
||||
return repo .. "@" .. name .. "-" .. ver .. ".gpkg"
|
||||
end
|
||||
|
||||
-- <base>/build/packages, or nil if there is none (not made here).
|
||||
local function kept_dir(ctx)
|
||||
local d = ffi.string(ctx.store_path):gsub("/store$", "") .. "/build/packages"
|
||||
return exists(d) and d or nil
|
||||
end
|
||||
|
||||
-- The kept packages of the installed source builds: the file names a
|
||||
-- prune leaves, and (for layer_plan) name -> { ver, path, deps }.
|
||||
local function kept_scan(opts)
|
||||
local ctx = h.live_context(opts)
|
||||
local dir = kept_dir(ctx)
|
||||
if not dir then return nil, {}, {} end
|
||||
local want, by_name = {}, {}
|
||||
for _, i in ipairs(h.installed_entries(opts)) do
|
||||
if h.built_from(ctx, i.repo, i.name) then
|
||||
local f = kept_name(i.repo, i.name, i.ver)
|
||||
local path = dir .. "/" .. f
|
||||
want[f] = true
|
||||
local m = ffi.new("gl_pkg_t")
|
||||
if exists(path) and istore.gl_read_pkg_manifest(path, m) == 0 then
|
||||
by_name[i.name] = { ver = i.ver, path = path,
|
||||
deps = h.relation_lines(m.pkg_dependencies, "dep_name", "dep_ver") }
|
||||
istore.gl_free_pkg(m)
|
||||
end
|
||||
end
|
||||
end
|
||||
return dir, want, by_name
|
||||
end
|
||||
|
||||
--- The installed source builds a build can use as layers: name ->
|
||||
--- { ver, path, deps }.
|
||||
function M.kept(opts)
|
||||
local _, _, by_name = kept_scan(opts)
|
||||
return by_name
|
||||
end
|
||||
|
||||
--- After a commit: keep the private copy `path` of a package this run
|
||||
--- built and installed (repo::name ver). A failure only warns: the
|
||||
--- package is installed, and only later builds would miss it.
|
||||
function M.keep(opts, path, repo, name, ver)
|
||||
local ctx = h.live_context(opts)
|
||||
local out = ffi.new("char[4096]")
|
||||
local ok = txn.gl_scope_build_dir(ctx, "packages", -1, out) == 0
|
||||
local dest = ok and (ffi.string(out) .. "/" .. kept_name(repo, name, ver))
|
||||
if not ok or not copy_file(path, dest .. ".part",
|
||||
opts.scope == ffi.C.GL_SCOPE_SYS and "644" or "600")
|
||||
or not os.rename(dest .. ".part", dest) then
|
||||
if dest then os.remove(dest .. ".part") end
|
||||
log.lg_printf(1, "Could not keep %s::%s %s for later builds.", repo, name, ver)
|
||||
end
|
||||
end
|
||||
|
||||
--- After a commit: delete the kept packages that are no longer an
|
||||
--- installed source build (removed, replaced, or reinstalled from a
|
||||
--- binary repository).
|
||||
function M.prune_kept(opts)
|
||||
local dir, want = kept_scan(opts)
|
||||
if not dir then return end
|
||||
local p = io.popen("ls -A -- " .. h.shell_quote(dir))
|
||||
for f in p:lines() do
|
||||
if not want[f] then os.remove(dir .. "/" .. f) end
|
||||
end
|
||||
p:close()
|
||||
end
|
||||
|
||||
ffi.cdef[[
|
||||
int gpkg_build_getpid(void) asm("getpid");
|
||||
]]
|
||||
|
||||
return M
|
||||
+1745
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:9f3b8d15fac432b51e0e43e9f53a76e0d30c7d70709aa7155ce865948aeffb6a
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YQ69miZJHwvuigbWfDubPXK5wa14L1G8uF0jWSlBR8+/cIB4u3u6pfgQnZkPnGgUksCVc4ih0vNnAGfgHVLb5gQ=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:85907993773e4a02318e06d69f1bcdf3725bc999c1c43fdf1c6291ac42b8ba7f
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YUL7xALU5l0yfMDgxzNDO3fFXJHTllFW+kDYF7+0mdLrgtlRc/29aO1EYNvFJxDptZtPTOb8CAsNYzgRqd5DWwM=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:36e9ff127a41c605a30c701fa6702d1a2f2913c049cfdadd952eedc01a70431e
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:5bd439dc10872c03c15c00acb62f970d3c35de304dec10261571a922538ccf80
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:6cc45a91dca1766efd6662ca57470428cc3e739c0a2aea76ec90c3b508b3663d
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6Ybe0RMo6HD0IvrFMBEBnqwb+QqyVMYhrWlr0CPfqCnvJgyGPiA6gCNYBLRsmytmPrUB0m2NTX79EupaCD5bonws=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:078db1648cb9af697650cad0684c596266e53e0df9d7d94d2068bcde1f61392a
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YZy13VIIa20Bl0b7eRZDLilv6hMXw2Brtbr3NtdaGyXL0WYR/yAX8YubAlpeNdrnB8TsvxObx7ejTEcuF1K75AM=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:153cf53dbf30e7e9c6a21a4e5072727e33574f330a8933a390e29c11fdc5c548
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YcSrkRAfMtSLjpuZXaxh5cotv4QVIzf7iHJVxAez1gY46D8fAuDIiNx2+vx2GfOlb8FGRKE95Mxv0x0zT7kQJQg=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:ee7af7fdfeffec2c6673074273688031d06a90a83473ae46309f3b29dab3ce77
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YeR6RLhjUDY9h+SF0Zvz4onqGTNgR9J9sOZ8im/jRNspUx4eWnZSdSfozQan/o1frnMKctuHK2ynjC1A04dWjQw=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:1690478ea1427befa27b7815efbcc19e52e9905a53fbb455bf0c6e83656b74c7
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YRQt36ddu7NOEwxCiPM0u+W2kdFzeEXBsK47Yxesd+DyE91lUkEhYQOcEUXn13Y+Az9o2EhrolGAbAH/zNb31gA=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:447824813c8af135c5adcfa42a1f4ed80c7c81358c5b8df590c150941816241d
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YcxL7mKJuYuHY5d4MQlNJsgaq2dVk8j7NLXaU3N5+FwSlItkwhaPIvVTId0okv/J9eMAnB5e/w2MeDdn0NIlRAg=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:177c67f4884bfda1aef1ced8f8058056390076cef3c2cd1ed47f6399dbb90038
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YQ7ooWHWe9tzu0p+8z5Eogun2oLZB4QS+JjAuuPiyN2YX5YFWQ5WahFecX20KA3uyc+kejeer4h3Ul8tytavcwA=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:f6bfe40946be49ea85ac11d8f0171102dae187beebcbbef99e3661e82de6eede
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YQwPd28j9U0tvhCHXnTI4Y9DrWaSB8MA2ztNSTuc5wpAuvRr8P7EeBnN3ZaNJxrn4Jff0zdY37W29kf3NhZgEwE=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:2ec41f02007e2511d1fcc7937a3ae52abd764e33e76ac2b5eb0172f56fb0f9a1
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YU+3yRvSnMrd0QtQdTRcPFwDU17HApYOyYN3BFuyT0s5LMpMZ1yRi+Outn0KvuIzTP5DK+C1AdlqvuhLBOwhHQ8=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:114acd041598508725779becec06a43ec3106bab450740a102784c4ee414a39a
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YVGo5e+KvMXvHFgbLK5+FGueHLJxNyBdL2guSDKOSfpGqxTZP8iQpqwDKkgJvzNPx+dCkkucIkHf8bm1DFq4CAc=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:d6c02491a1301577259d7595871640b781044f610d08d9df9e21674031b9bc40
|
||||
@@ -0,0 +1,2 @@
|
||||
untrusted comment: verify with everest-2026.pub
|
||||
RWTALtzjJuR6YamI1pToWcyZuWGCyRHydHFYGjsjlpwjGKAzxKOBoJ+bHRHzeZ+kCWtU8nIoLSSba7UEPx/p0WymLGY+F72utwQ=
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:6671b69282c10f9929c0ae640406b46ec654b5f74db1a0368428ee59f873af9d
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:f54dac956abd9533c6e5c6815dcff0247c474c6c762ccc58d860800b026c810d
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
sha256:e5d8caaeb2686e1358d18b0fed9b0d65b9328fb28d20f95621ddbc144bd8abdd
|
||||
Executable
+43
@@ -0,0 +1,43 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# ./run_docker.sh [-b] [DOCKERFILE]
|
||||
#
|
||||
# Builds DOCKERFILE (default Dockerfile.alpine) and starts a throwaway
|
||||
# container from it.
|
||||
#
|
||||
# -b for building packages from source (BUILD_DESIGN.txt): gsandbox
|
||||
# needs user namespaces, overlayfs and a fresh /proc, which
|
||||
# Docker's seccomp and AppArmor profiles and its /proc masking
|
||||
# refuse, so those are turned off; /glacier goes on a tmpfs
|
||||
# (overlayfs can't sit on Docker's own overlay root); and the
|
||||
# recipe tree $RECIPES (default ../everest/recipes) is mounted
|
||||
# read-only at /recipes. Use it with Dockerfile.alpine_root,
|
||||
# then run /app/tests/buildenv inside (see TESTING.txt).
|
||||
|
||||
BUILD=
|
||||
while getopts bh opt; do
|
||||
case $opt in
|
||||
b) BUILD=1 ;;
|
||||
*) echo "usage: $0 [-b] [DOCKERFILE]" >&2; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
DOCKERFILE="${1:-Dockerfile.alpine}"
|
||||
TAG="glacier-$(echo $DOCKERFILE | sed 's/Dockerfile\.//')"
|
||||
|
||||
# Dockerfiles that build libglacier-ng in-container (Dockerfile.alpine_root)
|
||||
# take its source from this named context; the others ignore it.
|
||||
LIBGLACIER_SRC="${LIBGLACIER_SRC:-$(dirname "$0")/../libglacier-ng}"
|
||||
RECIPES="${RECIPES:-$(dirname "$0")/../everest/recipes}"
|
||||
|
||||
docker build -f "$DOCKERFILE" --build-context libglacier="$LIBGLACIER_SRC" -t "$TAG" . || exit 1
|
||||
|
||||
if [ -n "$BUILD" ]; then
|
||||
[ -d "$RECIPES" ] || { echo "$0: no recipe tree at $RECIPES (set RECIPES)" >&2; exit 1; }
|
||||
exec docker run -it --rm --network host \
|
||||
--security-opt seccomp=unconfined --security-opt apparmor=unconfined \
|
||||
--security-opt systempaths=unconfined --tmpfs /glacier:exec,mode=755 \
|
||||
-v "$(realpath "$RECIPES")":/recipes:ro "$TAG"
|
||||
fi
|
||||
exec docker run -it --rm --network host "$TAG"
|
||||
@@ -0,0 +1,156 @@
|
||||
#!/bin/sh
|
||||
# Fixes from the 2026-10-06 security audit:
|
||||
# - a package may not ship a top-level files/.glacier-* entry: as a
|
||||
# symlink, .glacier-fresh made syspkg truncate any file as root;
|
||||
# .glacier-overwrite granted the package -O; .glacier-manifest
|
||||
# rewrote its saved manifest;
|
||||
# - a manifest with @include is refused (libconfig would read any file
|
||||
# as root), and so is a dependency or conflict entry without a name;
|
||||
# - what syspkg shows and compares is what the LIBRARY reads from the
|
||||
# manifest: a decoy comment can't hide that a package replaces one;
|
||||
# - no CLI argument reaches a format string (gstore's argv[0], gpkg
|
||||
# -u's operand); gstore refuses a UID that would wrap in C;
|
||||
# - root deleting a user's tree (gstore -x) never follows a symlink
|
||||
# swapped in mid-walk (race test, many iterations).
|
||||
# Runs INSIDE the Dockerfile.alpine_root image:
|
||||
#
|
||||
# docker run --rm -v "$PWD/tests/audit_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
R() {
|
||||
OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed '/^rc=/d; s/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
installed() { grep -q "^$1::$2::" /glacier/sys/index/current; }
|
||||
hr() { echo; echo "== $*"; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
P=/srv/pkgs
|
||||
mkdir -p $P && chmod 755 /srv $P
|
||||
|
||||
# raw NAME VER REPO [MANIFEST_EXTRA] -> $P/NAME-VER.gpkg (+ sidecar). The
|
||||
# package tree is left at $W/NAME-VER for the caller to add to first:
|
||||
# call raw_dir, edit $D, then raw_pack.
|
||||
raw_dir() {
|
||||
W=$(mktemp -d); D=$W/$1-$2
|
||||
mkdir -p $D/files/usr/bin
|
||||
printf '%sPKG_NAME = "%s";\nPKG_VER = "%s";\nPKG_REPO = "%s";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' \
|
||||
"${4:-}" "$1" "$2" "$3" > $D/manifest.gpm.cfg
|
||||
printf '#!/bin/sh\necho %s %s\n' "$1" "$2" > $D/files/usr/bin/$1
|
||||
chmod 755 $D/files/usr/bin/$1
|
||||
RAW=$P/$1-$2.gpkg
|
||||
}
|
||||
raw_pack() {
|
||||
tar -C $W -czf $RAW "$(basename $D)"
|
||||
echo "sha256:$(sha256sum $RAW | cut -c1-64)" > $RAW.sha256
|
||||
rm -rf $W
|
||||
}
|
||||
inst() { R sh -c "echo y | syspkg -l $*"; }
|
||||
|
||||
syspkg -n >/dev/null
|
||||
echo "important root data" > /root/victim && chmod 600 /root/victim
|
||||
|
||||
hr "reserved .glacier-* names"
|
||||
raw_dir fresh 1.0.0 extra; ln -s /root/victim $D/files/.glacier-fresh; raw_pack
|
||||
inst $RAW
|
||||
check "a .glacier-fresh symlink is refused" has "[x] Refusing archive entry 'fresh-1.0.0/files/.glacier-fresh': .glacier-* names are reserved."
|
||||
check "...exit 1" rc 1
|
||||
check "...the file it pointed at is untouched" [ "$(cat /root/victim)" = "important root data" ]
|
||||
check "...nothing installed" sh -c '! grep -q "::fresh::" /glacier/sys/index/current'
|
||||
check "...no half-made version dir" [ ! -e /glacier/sys/store/extra/fresh/fresh-1.0.0 ]
|
||||
raw_dir selfo 1.0.0 extra; : > $D/files/.glacier-overwrite; raw_pack
|
||||
inst $RAW
|
||||
check "a .glacier-overwrite (self-granted -O) is refused" has "files/.glacier-overwrite': .glacier-* names are reserved."
|
||||
raw_dir man 1.0.0 extra; printf 'PKG_NAME = "x";\n' > $D/files/.glacier-manifest; raw_pack
|
||||
inst $RAW
|
||||
check "a .glacier-manifest is refused" has "files/.glacier-manifest': .glacier-* names are reserved."
|
||||
raw_dir keep 1.0.0 extra; mkdir -p $D/files/usr/lib; : > $D/files/usr/lib/.keep; : > $D/files/.hidden; raw_pack
|
||||
inst $RAW
|
||||
check "other dotfiles still install" installed extra keep
|
||||
adduser -D tester >/dev/null 2>&1
|
||||
U=$(id -u tester)
|
||||
gstore -n "$U" >/dev/null
|
||||
echo "tester data" > /srv/tester-file && chown tester /srv/tester-file
|
||||
raw_dir ufresh 1.0.0 extra; ln -s /srv/tester-file $D/files/.glacier-fresh; raw_pack
|
||||
R su tester -c "echo y | gpkg -l $RAW"
|
||||
check "gpkg refuses one too" has "files/.glacier-fresh': .glacier-* names are reserved."
|
||||
check "...leaving the user's file alone" [ "$(cat /srv/tester-file)" = "tester data" ]
|
||||
|
||||
hr "hardlinks"
|
||||
# Skipping a hardlink entry silently dropped a file (binutils' ld): a
|
||||
# package with one is refused, whole.
|
||||
raw_dir hard 1.0.0 extra; ln $D/files/usr/bin/hard $D/files/usr/bin/hard2; raw_pack
|
||||
inst $RAW
|
||||
check "a package with a hardlink entry is refused" sh -c 'echo "$0" | grep -q "\[x\] Refusing archive entry .hard-1.0.0/files/usr/bin/hard2\{0,1\}.: it is a hardlink."' "$OUT"
|
||||
check "...exit 1" rc 1
|
||||
check "...nothing installed" sh -c '! grep -q "::hard::" /glacier/sys/index/current'
|
||||
check "...no half-made version dir" [ ! -e /glacier/sys/store/extra/hard/hard-1.0.0 ]
|
||||
|
||||
hr "manifests"
|
||||
raw_dir incl 1.0.0 extra '@include "/etc/glacier/glacier.conf"
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "@include in a manifest is refused" has "[x] Cannot read the manifest of $RAW."
|
||||
check "...exit 1, before the prompt" sh -c "[ $RC = 1 ] && ! echo \"\$0\" | grep -q Proceed" "$OUT"
|
||||
check "...nothing installed" sh -c '! grep -q "::incl::" /glacier/sys/index/current'
|
||||
raw_dir incl2 1.0.0 extra ' @include "/etc/glacier/glacier.conf"
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "...indented too" has "[x] Cannot read the manifest of $RAW."
|
||||
raw_dir mail 1.0.0 extra 'PKG_MAINT = "someone@example.org";
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "an @ elsewhere (an e-mail address) is fine" installed extra mail
|
||||
raw_dir noname 1.0.0 extra; sed -i 's/PKG_DEPS = ();/PKG_DEPS = ( { PKG_VER = "1.0.0"; } );/' $D/manifest.gpm.cfg; raw_pack
|
||||
inst $RAW
|
||||
check "a dependency without a name is refused" has "[x] Cannot read the manifest of $RAW."
|
||||
raw_dir badconf 1.0.0 extra; sed -i 's#PKG_CONFS = ();#PKG_CONFS = ( { PKG_NAME = "../x"; PKG_VER = "1.0.0"; } );#' $D/manifest.gpm.cfg; raw_pack
|
||||
inst $RAW
|
||||
check "a conflict with an unsafe name is refused" has "[x] Cannot read the manifest of $RAW."
|
||||
raw_dir dup 1.0.0 extra; raw_pack
|
||||
inst $RAW
|
||||
raw_dir dup 2.0.0 extra '// PKG_REPO = "community";
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "a decoy PKG_REPO comment can't hide a replacement" has "[x] extra::dup 1.0.0 is already installed."
|
||||
check "...one version stays in the store" [ "$(ls /glacier/sys/store/extra/dup)" = "dup-1.0.0" ]
|
||||
|
||||
hr "format strings and UIDs"
|
||||
ln -s /app/gstore '/srv/gs%s%s%s%s%n'
|
||||
R '/srv/gs%s%s%s%s%n' -b "$U" /srv/b.tgz
|
||||
check "gstore -b run as a name full of %s%n works" rc 0
|
||||
check "...and prints it nowhere as a format" has "[✓] Successfully created backup for UID $U."
|
||||
# gpkg -f/-u operands as formats: tests/fetch_alpine.sh (they need a repo).
|
||||
R gstore -n 4294968296
|
||||
check "a UID above uid_t's range is refused" has "[x] Invalid UID: 4294968296"
|
||||
R gstore -x -- -1
|
||||
R gstore -x -1000
|
||||
check "a negative UID is refused" sh -c "[ $RC = 1 ]"
|
||||
check "...and the tester's directory survives" [ -d /glacier/usr/$U ]
|
||||
|
||||
hr "races: root never follows a symlink swapped into a user's tree"
|
||||
mkdir -p /root/vdir && chmod 700 /root/vdir
|
||||
lost=0
|
||||
i=0
|
||||
while [ $i -lt 40 ]; do
|
||||
j=0; while [ $j -lt 50 ]; do : > /root/vdir/f$j; j=$((j+1)); done
|
||||
[ -d /glacier/usr/$U ] || gstore -n "$U" >/dev/null
|
||||
B=/glacier/usr/$U
|
||||
su tester -c "mkdir -p $B/store/d/a/b/c; j=0; while [ \$j -lt 200 ]; do : > $B/store/d/x\$j; j=\$((j+1)); done"
|
||||
su tester -c "while :; do mv $B/store/d $B/store/.d 2>/dev/null; ln -s /root/vdir $B/store/d 2>/dev/null; rm -f $B/store/d; mv $B/store/.d $B/store/d 2>/dev/null; done" >/dev/null 2>&1 &
|
||||
SWAP=$!
|
||||
gstore -x "$U" >/dev/null 2>&1
|
||||
kill $SWAP 2>/dev/null; wait $SWAP 2>/dev/null
|
||||
[ "$(ls /root/vdir | wc -l)" = 50 ] || lost=$((lost+1))
|
||||
i=$((i+1))
|
||||
done
|
||||
check "gstore -x deleted nothing through a swapped symlink (40 runs, $lost with losses)" [ $lost = 0 ]
|
||||
|
||||
echo
|
||||
echo "FAILURES: $FAILS"
|
||||
[ $FAILS -eq 0 ]
|
||||
@@ -0,0 +1,147 @@
|
||||
#!/bin/sh
|
||||
# Fixes from the 2026-10-06 security audit:
|
||||
# - a package may not ship a top-level files/.glacier-* entry: as a
|
||||
# symlink, .glacier-fresh made syspkg truncate any file as root;
|
||||
# .glacier-overwrite granted the package -O; .glacier-manifest
|
||||
# rewrote its saved manifest;
|
||||
# - a manifest with @include is refused (libconfig would read any file
|
||||
# as root), and so is a dependency or conflict entry without a name;
|
||||
# - what syspkg shows and compares is what the LIBRARY reads from the
|
||||
# manifest: a decoy comment can't hide that a package replaces one;
|
||||
# - no CLI argument reaches a format string (gstore's argv[0], gpkg
|
||||
# -u's operand); gstore refuses a UID that would wrap in C;
|
||||
# - root deleting a user's tree (gstore -x) never follows a symlink
|
||||
# swapped in mid-walk (race test, many iterations).
|
||||
# Runs INSIDE the Dockerfile.alpine_root image:
|
||||
#
|
||||
# docker run --rm -v "$PWD/tests/audit_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
R() {
|
||||
OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed '/^rc=/d; s/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
installed() { grep -q "^$1::$2::" /glacier/sys/index/current; }
|
||||
hr() { echo; echo "== $*"; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
P=/srv/pkgs
|
||||
mkdir -p $P && chmod 755 /srv $P
|
||||
|
||||
# raw NAME VER REPO [MANIFEST_EXTRA] -> $P/NAME-VER.gpkg (+ sidecar). The
|
||||
# package tree is left at $W/NAME-VER for the caller to add to first:
|
||||
# call raw_dir, edit $D, then raw_pack.
|
||||
raw_dir() {
|
||||
W=$(mktemp -d); D=$W/$1-$2
|
||||
mkdir -p $D/files/usr/bin
|
||||
printf '%sPKG_NAME = "%s";\nPKG_VER = "%s";\nPKG_REPO = "%s";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' \
|
||||
"${4:-}" "$1" "$2" "$3" > $D/manifest.gpm.cfg
|
||||
printf '#!/bin/sh\necho %s %s\n' "$1" "$2" > $D/files/usr/bin/$1
|
||||
chmod 755 $D/files/usr/bin/$1
|
||||
RAW=$P/$1-$2.gpkg
|
||||
}
|
||||
raw_pack() {
|
||||
tar -C $W -czf $RAW "$(basename $D)"
|
||||
echo "sha256:$(sha256sum $RAW | cut -c1-64)" > $RAW.sha256
|
||||
rm -rf $W
|
||||
}
|
||||
inst() { R sh -c "echo y | syspkg -l $*"; }
|
||||
|
||||
syspkg -n >/dev/null
|
||||
echo "important root data" > /root/victim && chmod 600 /root/victim
|
||||
|
||||
hr "reserved .glacier-* names"
|
||||
raw_dir fresh 1.0.0 extra; ln -s /root/victim $D/files/.glacier-fresh; raw_pack
|
||||
inst $RAW
|
||||
check "a .glacier-fresh symlink is refused" has "[x] Refusing archive entry 'fresh-1.0.0/files/.glacier-fresh': .glacier-* names are reserved."
|
||||
check "...exit 1" rc 1
|
||||
check "...the file it pointed at is untouched" [ "$(cat /root/victim)" = "important root data" ]
|
||||
check "...nothing installed" sh -c '! grep -q "::fresh::" /glacier/sys/index/current'
|
||||
check "...no half-made version dir" [ ! -e /glacier/sys/store/extra/fresh/fresh-1.0.0 ]
|
||||
raw_dir selfo 1.0.0 extra; : > $D/files/.glacier-overwrite; raw_pack
|
||||
inst $RAW
|
||||
check "a .glacier-overwrite (self-granted -O) is refused" has "files/.glacier-overwrite': .glacier-* names are reserved."
|
||||
raw_dir man 1.0.0 extra; printf 'PKG_NAME = "x";\n' > $D/files/.glacier-manifest; raw_pack
|
||||
inst $RAW
|
||||
check "a .glacier-manifest is refused" has "files/.glacier-manifest': .glacier-* names are reserved."
|
||||
raw_dir keep 1.0.0 extra; mkdir -p $D/files/usr/lib; : > $D/files/usr/lib/.keep; : > $D/files/.hidden; raw_pack
|
||||
inst $RAW
|
||||
check "other dotfiles still install" installed extra keep
|
||||
adduser -D tester >/dev/null 2>&1
|
||||
U=$(id -u tester)
|
||||
gstore -n "$U" >/dev/null
|
||||
echo "tester data" > /srv/tester-file && chown tester /srv/tester-file
|
||||
raw_dir ufresh 1.0.0 extra; ln -s /srv/tester-file $D/files/.glacier-fresh; raw_pack
|
||||
R su tester -c "echo y | gpkg -l $RAW"
|
||||
check "gpkg refuses one too" has "files/.glacier-fresh': .glacier-* names are reserved."
|
||||
check "...leaving the user's file alone" [ "$(cat /srv/tester-file)" = "tester data" ]
|
||||
|
||||
hr "manifests"
|
||||
raw_dir incl 1.0.0 extra '@include "/etc/glacier/glacier.conf"
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "@include in a manifest is refused" has "[x] Cannot read the manifest of $RAW."
|
||||
check "...exit 1, before the prompt" sh -c "[ $RC = 1 ] && ! echo \"\$0\" | grep -q Proceed" "$OUT"
|
||||
check "...nothing installed" sh -c '! grep -q "::incl::" /glacier/sys/index/current'
|
||||
raw_dir incl2 1.0.0 extra ' @include "/etc/glacier/glacier.conf"
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "...indented too" has "[x] Cannot read the manifest of $RAW."
|
||||
raw_dir mail 1.0.0 extra 'PKG_MAINT = "someone@example.org";
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "an @ elsewhere (an e-mail address) is fine" installed extra mail
|
||||
raw_dir noname 1.0.0 extra; sed -i 's/PKG_DEPS = ();/PKG_DEPS = ( { PKG_VER = "1.0.0"; } );/' $D/manifest.gpm.cfg; raw_pack
|
||||
inst $RAW
|
||||
check "a dependency without a name is refused" has "[x] Cannot read the manifest of $RAW."
|
||||
raw_dir badconf 1.0.0 extra; sed -i 's#PKG_CONFS = ();#PKG_CONFS = ( { PKG_NAME = "../x"; PKG_VER = "1.0.0"; } );#' $D/manifest.gpm.cfg; raw_pack
|
||||
inst $RAW
|
||||
check "a conflict with an unsafe name is refused" has "[x] Cannot read the manifest of $RAW."
|
||||
raw_dir dup 1.0.0 extra; raw_pack
|
||||
inst $RAW
|
||||
raw_dir dup 2.0.0 extra '// PKG_REPO = "community";
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "a decoy PKG_REPO comment can't hide a replacement" has "[x] extra::dup 1.0.0 is already installed."
|
||||
check "...one version stays in the store" [ "$(ls /glacier/sys/store/extra/dup)" = "dup-1.0.0" ]
|
||||
|
||||
hr "format strings and UIDs"
|
||||
ln -s /app/gstore '/srv/gs%s%s%s%s%n'
|
||||
R '/srv/gs%s%s%s%s%n' -b "$U" /srv/b.tgz
|
||||
check "gstore -b run as a name full of %s%n works" rc 0
|
||||
check "...and prints it nowhere as a format" has "[✓] Successfully created backup for UID $U."
|
||||
R su tester -c 'gpkg -u "%s%s%s%s%n"'
|
||||
check "gpkg -u with %s%n as its operand" has "[x] -u is not implemented yet."
|
||||
R gstore -n 4294968296
|
||||
check "a UID above uid_t's range is refused" has "[x] Invalid UID: 4294968296"
|
||||
R gstore -x -- -1
|
||||
R gstore -x -1000
|
||||
check "a negative UID is refused" sh -c "[ $RC = 1 ]"
|
||||
check "...and the tester's directory survives" [ -d /glacier/usr/$U ]
|
||||
|
||||
hr "races: root never follows a symlink swapped into a user's tree"
|
||||
mkdir -p /root/vdir && chmod 700 /root/vdir
|
||||
lost=0
|
||||
i=0
|
||||
while [ $i -lt 40 ]; do
|
||||
j=0; while [ $j -lt 50 ]; do : > /root/vdir/f$j; j=$((j+1)); done
|
||||
[ -d /glacier/usr/$U ] || gstore -n "$U" >/dev/null
|
||||
B=/glacier/usr/$U
|
||||
su tester -c "mkdir -p $B/store/d/a/b/c; j=0; while [ \$j -lt 200 ]; do : > $B/store/d/x\$j; j=\$((j+1)); done"
|
||||
su tester -c "while :; do mv $B/store/d $B/store/.d 2>/dev/null; ln -s /root/vdir $B/store/d 2>/dev/null; rm -f $B/store/d; mv $B/store/.d $B/store/d 2>/dev/null; done" >/dev/null 2>&1 &
|
||||
SWAP=$!
|
||||
gstore -x "$U" >/dev/null 2>&1
|
||||
kill $SWAP 2>/dev/null; wait $SWAP 2>/dev/null
|
||||
[ "$(ls /root/vdir | wc -l)" = 50 ] || lost=$((lost+1))
|
||||
i=$((i+1))
|
||||
done
|
||||
check "gstore -x deleted nothing through a swapped symlink (40 runs, $lost with losses)" [ $lost = 0 ]
|
||||
|
||||
echo
|
||||
echo "FAILURES: $FAILS"
|
||||
[ $FAILS -eq 0 ]
|
||||
@@ -0,0 +1,146 @@
|
||||
#!/bin/sh
|
||||
# Fixes from the 2026-10-06 security audit:
|
||||
# - a package may not ship a top-level files/.glacier-* entry: as a
|
||||
# symlink, .glacier-fresh made syspkg truncate any file as root;
|
||||
# .glacier-overwrite granted the package -O; .glacier-manifest
|
||||
# rewrote its saved manifest;
|
||||
# - a manifest with @include is refused (libconfig would read any file
|
||||
# as root), and so is a dependency or conflict entry without a name;
|
||||
# - what syspkg shows and compares is what the LIBRARY reads from the
|
||||
# manifest: a decoy comment can't hide that a package replaces one;
|
||||
# - no CLI argument reaches a format string (gstore's argv[0], gpkg
|
||||
# -u's operand); gstore refuses a UID that would wrap in C;
|
||||
# - root deleting a user's tree (gstore -x) never follows a symlink
|
||||
# swapped in mid-walk (race test, many iterations).
|
||||
# Runs INSIDE the Dockerfile.alpine_root image:
|
||||
#
|
||||
# docker run --rm -v "$PWD/tests/audit_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
R() {
|
||||
OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed '/^rc=/d; s/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
installed() { grep -q "^$1::$2::" /glacier/sys/index/current; }
|
||||
hr() { echo; echo "== $*"; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
P=/srv/pkgs
|
||||
mkdir -p $P && chmod 755 /srv $P
|
||||
|
||||
# raw NAME VER REPO [MANIFEST_EXTRA] -> $P/NAME-VER.gpkg (+ sidecar). The
|
||||
# package tree is left at $W/NAME-VER for the caller to add to first:
|
||||
# call raw_dir, edit $D, then raw_pack.
|
||||
raw_dir() {
|
||||
W=$(mktemp -d); D=$W/$1-$2
|
||||
mkdir -p $D/files/usr/bin
|
||||
printf '%sPKG_NAME = "%s";\nPKG_VER = "%s";\nPKG_REPO = "%s";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' \
|
||||
"${4:-}" "$1" "$2" "$3" > $D/manifest.gpm.cfg
|
||||
printf '#!/bin/sh\necho %s %s\n' "$1" "$2" > $D/files/usr/bin/$1
|
||||
chmod 755 $D/files/usr/bin/$1
|
||||
RAW=$P/$1-$2.gpkg
|
||||
}
|
||||
raw_pack() {
|
||||
tar -C $W -czf $RAW "$(basename $D)"
|
||||
echo "sha256:$(sha256sum $RAW | cut -c1-64)" > $RAW.sha256
|
||||
rm -rf $W
|
||||
}
|
||||
inst() { R sh -c "echo y | syspkg -l $*"; }
|
||||
|
||||
syspkg -n >/dev/null
|
||||
echo "important root data" > /root/victim && chmod 600 /root/victim
|
||||
|
||||
hr "reserved .glacier-* names"
|
||||
raw_dir fresh 1.0.0 extra; ln -s /root/victim $D/files/.glacier-fresh; raw_pack
|
||||
inst $RAW
|
||||
check "a .glacier-fresh symlink is refused" has "[x] Refusing archive entry 'fresh-1.0.0/files/.glacier-fresh': .glacier-* names are reserved."
|
||||
check "...exit 1" rc 1
|
||||
check "...the file it pointed at is untouched" [ "$(cat /root/victim)" = "important root data" ]
|
||||
check "...nothing installed" sh -c '! grep -q "::fresh::" /glacier/sys/index/current'
|
||||
check "...no half-made version dir" [ ! -e /glacier/sys/store/extra/fresh/fresh-1.0.0 ]
|
||||
raw_dir selfo 1.0.0 extra; : > $D/files/.glacier-overwrite; raw_pack
|
||||
inst $RAW
|
||||
check "a .glacier-overwrite (self-granted -O) is refused" has "files/.glacier-overwrite': .glacier-* names are reserved."
|
||||
raw_dir man 1.0.0 extra; printf 'PKG_NAME = "x";\n' > $D/files/.glacier-manifest; raw_pack
|
||||
inst $RAW
|
||||
check "a .glacier-manifest is refused" has "files/.glacier-manifest': .glacier-* names are reserved."
|
||||
raw_dir keep 1.0.0 extra; mkdir -p $D/files/usr/lib; : > $D/files/usr/lib/.keep; : > $D/files/.hidden; raw_pack
|
||||
inst $RAW
|
||||
check "other dotfiles still install" installed extra keep
|
||||
adduser -D tester >/dev/null 2>&1
|
||||
U=$(id -u tester)
|
||||
gstore -n "$U" >/dev/null
|
||||
echo "tester data" > /srv/tester-file && chown tester /srv/tester-file
|
||||
raw_dir ufresh 1.0.0 extra; ln -s /srv/tester-file $D/files/.glacier-fresh; raw_pack
|
||||
R su tester -c "echo y | gpkg -l $RAW"
|
||||
check "gpkg refuses one too" has "files/.glacier-fresh': .glacier-* names are reserved."
|
||||
check "...leaving the user's file alone" [ "$(cat /srv/tester-file)" = "tester data" ]
|
||||
|
||||
hr "manifests"
|
||||
raw_dir incl 1.0.0 extra '@include "/etc/glacier/glacier.conf"
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "@include in a manifest is refused" has "[x] Cannot read the manifest of $RAW."
|
||||
check "...exit 1, before the prompt" sh -c "[ $RC = 1 ] && ! echo \"\$0\" | grep -q Proceed" "$OUT"
|
||||
check "...nothing installed" sh -c '! grep -q "::incl::" /glacier/sys/index/current'
|
||||
raw_dir incl2 1.0.0 extra ' @include "/etc/glacier/glacier.conf"
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "...indented too" has "[x] Cannot read the manifest of $RAW."
|
||||
raw_dir mail 1.0.0 extra 'PKG_MAINT = "someone@example.org";
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "an @ elsewhere (an e-mail address) is fine" installed extra mail
|
||||
raw_dir noname 1.0.0 extra; sed -i 's/PKG_DEPS = ();/PKG_DEPS = ( { PKG_VER = "1.0.0"; } );/' $D/manifest.gpm.cfg; raw_pack
|
||||
inst $RAW
|
||||
check "a dependency without a name is refused" has "[x] Cannot read the manifest of $RAW."
|
||||
raw_dir badconf 1.0.0 extra; sed -i 's#PKG_CONFS = ();#PKG_CONFS = ( { PKG_NAME = "../x"; PKG_VER = "1.0.0"; } );#' $D/manifest.gpm.cfg; raw_pack
|
||||
inst $RAW
|
||||
check "a conflict with an unsafe name is refused" has "[x] Cannot read the manifest of $RAW."
|
||||
raw_dir dup 1.0.0 extra; raw_pack
|
||||
inst $RAW
|
||||
raw_dir dup 2.0.0 extra '// PKG_REPO = "community";
|
||||
'; raw_pack
|
||||
inst $RAW
|
||||
check "a decoy PKG_REPO comment can't hide a replacement" has "[x] extra::dup 1.0.0 is already installed."
|
||||
check "...one version stays in the store" [ "$(ls /glacier/sys/store/extra/dup)" = "dup-1.0.0" ]
|
||||
|
||||
hr "format strings and UIDs"
|
||||
ln -s /app/gstore '/srv/gs%s%s%s%s%n'
|
||||
R '/srv/gs%s%s%s%s%n' -b "$U" /srv/b.tgz
|
||||
check "gstore -b run as a name full of %s%n works" rc 0
|
||||
check "...and prints it nowhere as a format" has "[✓] Successfully created backup for UID $U."
|
||||
# gpkg -f/-u operands as formats: tests/fetch_alpine.sh (they need a repo).
|
||||
R gstore -n 4294968296
|
||||
check "a UID above uid_t's range is refused" has "[x] Invalid UID: 4294968296"
|
||||
R gstore -x -- -1
|
||||
R gstore -x -1000
|
||||
check "a negative UID is refused" sh -c "[ $RC = 1 ]"
|
||||
check "...and the tester's directory survives" [ -d /glacier/usr/$U ]
|
||||
|
||||
hr "races: root never follows a symlink swapped into a user's tree"
|
||||
mkdir -p /root/vdir && chmod 700 /root/vdir
|
||||
lost=0
|
||||
i=0
|
||||
while [ $i -lt 40 ]; do
|
||||
j=0; while [ $j -lt 50 ]; do : > /root/vdir/f$j; j=$((j+1)); done
|
||||
[ -d /glacier/usr/$U ] || gstore -n "$U" >/dev/null
|
||||
B=/glacier/usr/$U
|
||||
su tester -c "mkdir -p $B/store/d/a/b/c; j=0; while [ \$j -lt 200 ]; do : > $B/store/d/x\$j; j=\$((j+1)); done"
|
||||
su tester -c "while :; do mv $B/store/d $B/store/.d 2>/dev/null; ln -s /root/vdir $B/store/d 2>/dev/null; rm -f $B/store/d; mv $B/store/.d $B/store/d 2>/dev/null; done" >/dev/null 2>&1 &
|
||||
SWAP=$!
|
||||
gstore -x "$U" >/dev/null 2>&1
|
||||
kill $SWAP 2>/dev/null; wait $SWAP 2>/dev/null
|
||||
[ "$(ls /root/vdir | wc -l)" = 50 ] || lost=$((lost+1))
|
||||
i=$((i+1))
|
||||
done
|
||||
check "gstore -x deleted nothing through a swapped symlink (40 runs, $lost with losses)" [ $lost = 0 ]
|
||||
|
||||
echo
|
||||
echo "FAILURES: $FAILS"
|
||||
[ $FAILS -eq 0 ]
|
||||
@@ -0,0 +1,348 @@
|
||||
#!/bin/sh
|
||||
# glacier-bootstrap, local mode (ROADMAP 1.3, GLACIER_BOOTSTRAP.txt):
|
||||
# - a template + PKGDIR become a chrootable system in T, as root and as
|
||||
# a user through doas; the host's /usr, /glacier and /etc stay
|
||||
# byte-identical; the private copies are removed;
|
||||
# - every template, verification and resolution problem is reported
|
||||
# (all of them, each with its own exit status) before T is touched;
|
||||
# - a pin is enforced, and a tampered file never reaches syspkg;
|
||||
# - T must pass syspkg -R's trust check and be fresh, or initialised
|
||||
# with nothing installed (a declined prompt can be re-run);
|
||||
# - one transaction: a syspkg refusal leaves T with nothing installed;
|
||||
# - -n never touches T; -t fails (6) when T has no working /bin/sh;
|
||||
# - repositories: T's indexes are fetched with syspkg -R T -y, from
|
||||
# T's OWN repos.conf and keys (a key the host doesn't trust), into
|
||||
# T only; a failed fetch exits 7 without "Bootstrapped"; no url is a
|
||||
# warning; a T keyring file resolving outside T is refused; -R -f
|
||||
# uses T's repositories too.
|
||||
# Runs INSIDE the Dockerfile.alpine_root image:
|
||||
#
|
||||
# docker run --rm -v "$PWD/tests/bootstrap_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
# B ARGS...: run glacier-bootstrap as root, answering the prompt with $ANS
|
||||
# (default y). Sets $OUT and $RC.
|
||||
B() {
|
||||
OUT=$( (echo "${ANS:-y}" | glacier-bootstrap "$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed 's/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
untouched() { [ -z "$(ls -A "$1")" ]; }
|
||||
|
||||
P=/srv/pkgs
|
||||
mkdir -p $P /srv/src
|
||||
|
||||
# pkg NAME VER REPO [DEPS] [CONFS] -> $P/NAME-VER.gpkg + sidecar; files
|
||||
# come from /srv/src/NAME (laid out as files/...), if it exists.
|
||||
pkg() {
|
||||
w=$(mktemp -d); d=$w/$1-$2
|
||||
mkdir -p $d/files
|
||||
[ -d /srv/src/$1 ] && cp -a /srv/src/$1/. $d/files/
|
||||
list() { l=""; sep=""; for it in $1; do l="$l$sep{ PKG_NAME = \"${it%%:*}\"; PKG_VER = \"${it#*:}\"; }"; sep=", "; done; echo "$l"; }
|
||||
printf 'PKG_NAME = "%s";\nPKG_VER = "%s";\nPKG_DESC = "t";\nPKG_MAINT = "t@e";\nPKG_COPYRIGHT = "MIT";\nPKG_REPO = "%s";\nPKG_DEPS = (%s);\nPKG_CONFS = (%s);\n' \
|
||||
"$1" "$2" "$3" "$(list "$4")" "$(list "$5")" > $d/manifest.gpm.cfg
|
||||
tar czf $P/$1-$2.gpkg -C $w $1-$2
|
||||
rm -rf $w
|
||||
sidecar $P/$1-$2.gpkg
|
||||
}
|
||||
sidecar() { printf 'sha256:%s\n' "$(sha256sum "$1" | cut -d' ' -f1)" > "$1.sha256"; gsign -S -s /root/glacier-test.sec -m "$1.sha256" >/dev/null; }
|
||||
hash() { sha256sum "$1" | cut -d' ' -f1; }
|
||||
|
||||
# A minimal system out of Alpine's own loader and busybox: alpmusl puts
|
||||
# the loader where PT_INTERP looks (/lib -> usr/lib), abox gives /bin/sh.
|
||||
mkdir -p /srv/src/alpmusl/usr/lib
|
||||
cp /lib/ld-musl-x86_64.so.1 /srv/src/alpmusl/usr/lib/
|
||||
ln -s ld-musl-x86_64.so.1 /srv/src/alpmusl/usr/lib/libc.musl-x86_64.so.1
|
||||
mkdir -p /srv/src/abox/usr/bin
|
||||
cp /bin/busybox /srv/src/abox/usr/bin/busybox
|
||||
ln -s busybox /srv/src/abox/usr/bin/sh
|
||||
mkdir -p /srv/src/abox/etc; echo "from abox" > /srv/src/abox/etc/motd
|
||||
pkg alpmusl 1.2.5 base
|
||||
pkg abox 1.37.0 base "alpmusl:1.2.5"
|
||||
mkdir -p /srv/src/tool/usr/bin; printf '#!/bin/sh\necho tool-ok\n' > /srv/src/tool/usr/bin/tool; chmod 755 /srv/src/tool/usr/bin/tool
|
||||
pkg tool 2.0.0 extra "abox:1.0.0"
|
||||
# Resolution fixtures
|
||||
pkg lib 1.0.0 extra
|
||||
pkg app 1.0.0 extra "lib:2.0.0"
|
||||
pkg needy 1.0.0 extra "ghost:1.0.0"
|
||||
pkg rival 1.0.0 extra "" "abox:0.0.0"
|
||||
# Collision: both ship /glacier/sys/links/bin/same
|
||||
mkdir -p /srv/src/col1/usr/bin /srv/src/col2/usr/bin
|
||||
echo 1 > /srv/src/col1/usr/bin/same; echo 2 > /srv/src/col2/usr/bin/same
|
||||
pkg col1 1.0.0 extra; pkg col2 1.0.0 extra
|
||||
# No shell at all
|
||||
pkg data 1.0.0 extra
|
||||
|
||||
T=/mnt/img
|
||||
mkdir -p $T
|
||||
cat > /srv/min.template <<EOF
|
||||
# a minimal system
|
||||
NAME=minimal
|
||||
PKG=alpmusl 1.2.5 sha256:$(hash $P/alpmusl-1.2.5.gpkg)
|
||||
PKG=abox 1.37.0
|
||||
PKG=tool 2.0.0
|
||||
EOF
|
||||
|
||||
snap() {
|
||||
find /usr /glacier /etc -xdev -printf '%p %y %m %U %G %s %T@ %l\n' 2>/dev/null | sort
|
||||
find /usr /glacier /etc -xdev -type f -exec sha256sum {} + 2>/dev/null | sort
|
||||
}
|
||||
snap > /tmp/before
|
||||
ls -A /tmp > /tmp/tmp-before
|
||||
|
||||
echo "== 1. bootstrap as root, with the smoke test"
|
||||
B -t -d $P /srv/min.template $T
|
||||
check "exits 0" rc 0
|
||||
check "syspkg's summary was shown" has "Proceed? (y/N)"
|
||||
check "ends with Bootstrapped" has "[✓] Bootstrapped minimal into $T."
|
||||
check "smoke test reported" has "[✓] chroot $T /bin/sh -c true succeeded."
|
||||
check "no repos.conf in T: a warning" has "[!] No remote repositories are configured in $T."
|
||||
check "no repos.conf in T: no refresh" lacks "Syncing"
|
||||
check "the pin is shown" has " * base::alpmusl 1.2.5 (pinned)"
|
||||
check "syspkg read the verified copies, not PKGDIR" lacks "staged from $P/"
|
||||
check "T's index lists all three" sh -c "grep -c :: $T/glacier/sys/index/current | grep -qx 3"
|
||||
check "chroot T runs a base program" sh -c "chroot $T /bin/sh -c 'echo ok' | grep -qx ok"
|
||||
check "chroot T runs an extra program (sys links)" sh -c "chroot $T /glacier/sys/links/bin/tool | grep -qx tool-ok"
|
||||
check "/etc was copied into T" sh -c "grep -qx 'from abox' $T/etc/motd && ! [ -L $T/etc/motd ]"
|
||||
check "T's links say runtime paths" sh -c "readlink $T/usr/bin/busybox | grep -q '^/glacier/sys/store/'"
|
||||
snap > /tmp/after
|
||||
check "host /usr, /glacier and /etc are byte-identical" cmp -s /tmp/before /tmp/after
|
||||
ls -A /tmp | grep -v '^after$' | grep -v '^tmp-after$' > /tmp/tmp-after
|
||||
check "the private copies were removed" cmp -s /tmp/tmp-before /tmp/tmp-after
|
||||
|
||||
echo "== 2. T already has packages: refused"
|
||||
B -d $P /srv/min.template $T
|
||||
check "exits 1" rc 1
|
||||
check "says so" has "[x] $T already has packages installed."
|
||||
|
||||
echo "== 3. as a user, through doas"
|
||||
echo "permit nopass glacier as root cmd /app/syspkg" >> /etc/doas.conf
|
||||
echo "permit nopass glacier as root cmd chroot" >> /etc/doas.conf
|
||||
mkdir -p /mnt/u
|
||||
OUT=$(su glacier -c "PATH=/app:\$PATH; echo y | glacier-bootstrap -t -d $P /srv/min.template /mnt/u; echo rc=\$?" 2>&1 | nocolor)
|
||||
echo "$OUT" | sed 's/^/ /'
|
||||
check "exits 0" has "rc=0"
|
||||
check "bootstrapped" has "[✓] Bootstrapped minimal into /mnt/u."
|
||||
check "smoke test passed" has "succeeded."
|
||||
check "T's store is root's" sh -c "[ \"\$(stat -c %u /mnt/u/glacier/sys/store)\" = 0 ]"
|
||||
check "the user's private copies were removed" sh -c "! find /tmp -maxdepth 1 -user glacier | grep -q ."
|
||||
|
||||
echo "== 4. template errors: all reported, exit 2, T untouched"
|
||||
mkdir -p /mnt/e
|
||||
cat > /srv/bad.template <<EOF
|
||||
NAME=bad
|
||||
SYS_PROFILE=x86_64-musl
|
||||
PKG=abox 1.37.0
|
||||
PKG=abox 1.37.0
|
||||
PKG=lib 1.0
|
||||
PKG=lib2 1.0.0 sha256:abc
|
||||
PKG=lib3
|
||||
just words
|
||||
NAME=again
|
||||
EOF
|
||||
B -d $P /srv/bad.template /mnt/e
|
||||
check "exits 2" rc 2
|
||||
check "unknown key" has "bad.template:2: Unknown key: SYS_PROFILE"
|
||||
check "duplicate package" has "bad.template:4: abox is listed twice (first on line 3)."
|
||||
check "two-part version" has "bad.template:5: Invalid version for lib: 1.0"
|
||||
check "malformed pin" has "bad.template:6: Malformed pin for lib2: sha256:abc"
|
||||
check "PKG with no version" has "bad.template:7: Expected PKG=NAME VERSION [sha256:HEX]."
|
||||
check "not KEY=value" has "bad.template:8: Expected KEY=value."
|
||||
check "second NAME" has "bad.template:9: NAME given more than once."
|
||||
check "T untouched" untouched /mnt/e
|
||||
printf 'PKG=abox 1.37.0\n' > /srv/noname.template
|
||||
B -d $P /srv/noname.template /mnt/e
|
||||
check "no NAME: exit 2" rc 2
|
||||
check "no NAME: says so" has "noname.template: No NAME."
|
||||
printf 'NAME=empty\n# nothing\n' > /srv/empty.template
|
||||
B -d $P /srv/empty.template /mnt/e
|
||||
check "no packages: says so" has "empty.template: No packages."
|
||||
B -d $P /srv/missing.template /mnt/e
|
||||
check "missing template: exit 2" rc 2
|
||||
|
||||
echo "== 5. verification: all reported, exit 3, T untouched"
|
||||
mkdir -p /srv/v
|
||||
for f in alpmusl-1.2.5 abox-1.37.0 tool-2.0.0 lib-1.0.0; do cp $P/$f.gpkg $P/$f.gpkg.sha256 /srv/v/; done
|
||||
rm /srv/v/abox-1.37.0.gpkg.sha256
|
||||
echo "sha256:nothex" > /srv/v/lib-1.0.0.gpkg.sha256
|
||||
echo junk >> /srv/v/tool-2.0.0.gpkg
|
||||
cat > /srv/v.template <<EOF
|
||||
NAME=v
|
||||
PKG=alpmusl 1.2.5 sha256:$(printf '0%.0s' $(seq 64))
|
||||
PKG=abox 1.37.0
|
||||
PKG=tool 2.0.0
|
||||
PKG=lib 1.0.0
|
||||
PKG=gone 1.0.0
|
||||
EOF
|
||||
B -d /srv/v /srv/v.template /mnt/e
|
||||
check "exits 3" rc 3
|
||||
check "pin mismatch" has "[x] Refusing /srv/v/alpmusl-1.2.5.gpkg: the template pins 0000"
|
||||
check "no sidecar" has "[x] Refusing /srv/v/abox-1.37.0.gpkg: no sidecar /srv/v/abox-1.37.0.gpkg.sha256."
|
||||
check "tampered file" has "[x] Refusing /srv/v/tool-2.0.0.gpkg: hash mismatch"
|
||||
check "malformed sidecar" has "[x] Refusing /srv/v/lib-1.0.0.gpkg: malformed sidecar"
|
||||
check "missing file" has "[x] Refusing /srv/v/gone-1.0.0.gpkg: no such file."
|
||||
check "T untouched" untouched /mnt/e
|
||||
|
||||
echo "== 6. a pin binds even when the sidecar is rewritten to match"
|
||||
mkdir -p /srv/w; cp $P/alpmusl-1.2.5.gpkg /srv/w/; echo x >> /srv/w/alpmusl-1.2.5.gpkg; sidecar /srv/w/alpmusl-1.2.5.gpkg
|
||||
printf 'NAME=w\nPKG=alpmusl 1.2.5 sha256:%s\n' "$(hash $P/alpmusl-1.2.5.gpkg)" > /srv/w.template
|
||||
B -d /srv/w /srv/w.template /mnt/e
|
||||
check "exits 3" rc 3
|
||||
check "pin vs sidecar" has "the template pins $(hash $P/alpmusl-1.2.5.gpkg), the sidecar says"
|
||||
check "T untouched" untouched /mnt/e
|
||||
|
||||
echo "== 7. resolution: all reported, exit 4, T untouched"
|
||||
mkdir -p /srv/r; cp $P/* /srv/r/
|
||||
cp $P/lib-1.0.0.gpkg /srv/r/libx-1.0.0.gpkg; sidecar /srv/r/libx-1.0.0.gpkg
|
||||
cat > /srv/r.template <<EOF
|
||||
NAME=r
|
||||
PKG=alpmusl 1.2.5
|
||||
PKG=abox 1.37.0
|
||||
PKG=libx 1.0.0
|
||||
PKG=lib 1.0.0
|
||||
PKG=app 1.0.0
|
||||
PKG=needy 1.0.0
|
||||
PKG=rival 1.0.0
|
||||
EOF
|
||||
B -d /srv/r /srv/r.template /mnt/e
|
||||
check "exits 4" rc 4
|
||||
check "file holds another package" has "[x] /srv/r/libx-1.0.0.gpkg is lib 1.0.0, not libx 1.0.0."
|
||||
sed -i '/libx/d' /srv/r.template
|
||||
B -d /srv/r /srv/r.template /mnt/e
|
||||
check "exits 4" rc 4
|
||||
check "dependency too old" has "[x] extra::app requires lib >= 2.0.0, but the template lists 1.0.0."
|
||||
check "dependency not listed" has "[x] extra::needy requires ghost >= 1.0.0, which the template does not list."
|
||||
check "conflict" has "[x] base::abox conflicts with extra::rival."
|
||||
check "T untouched" untouched /mnt/e
|
||||
|
||||
echo "== 8. -n: plan only"
|
||||
B -n -d $P /srv/min.template /mnt/e
|
||||
check "exits 0" rc 0
|
||||
check "says dry run" has "[✓] Dry run: every package verified and resolved."
|
||||
check "never asks" lacks "Proceed"
|
||||
check "T untouched" untouched /mnt/e
|
||||
|
||||
echo "== 9. target refusals (exit 1)"
|
||||
B -d $P /srv/min.template mnt/e
|
||||
check "relative: exit 1" rc 1
|
||||
check "relative: says so" has "[x] TARGET must be an absolute path: mnt/e"
|
||||
B -d $P /srv/min.template /
|
||||
check "/: refused" has "[x] TARGET must not be /."
|
||||
mkdir -p /tmp/tt
|
||||
B -d $P /srv/min.template /tmp/tt
|
||||
check "under /tmp: exit 1" rc 1
|
||||
check "under /tmp: the library says why" has "[x] Refusing install root /tmp/tt: /tmp is not root-owned, or is group/world-writable."
|
||||
B -d $P /srv/min.template /mnt/nonexistent
|
||||
check "missing: exit 1" rc 1
|
||||
B -x -d $P /srv/min.template /mnt/e
|
||||
check "unknown option: exit 1" rc 1
|
||||
check "unknown option: says so" has "[x] Unknown option: -x"
|
||||
|
||||
echo "== 10. declined prompt: exit 5, then re-runnable"
|
||||
mkdir -p /mnt/d
|
||||
ANS=n B -d $P /srv/min.template /mnt/d
|
||||
check "exits 5" rc 5
|
||||
check "says nothing was installed" has "[x] Nothing was installed into /mnt/d."
|
||||
check "T was initialised" test -e /mnt/d/glacier/sys/index/current
|
||||
B -d $P /srv/min.template /mnt/d
|
||||
check "re-run exits 0" rc 0
|
||||
check "re-run did not run -n again" lacks "Created system glacier directories"
|
||||
check "re-run installed" has "[✓] Bootstrapped minimal into /mnt/d."
|
||||
|
||||
echo "== 11. one transaction: a syspkg refusal installs nothing (exit 5)"
|
||||
mkdir -p /mnt/c
|
||||
printf 'NAME=c\nPKG=alpmusl 1.2.5\nPKG=abox 1.37.0\nPKG=col1 1.0.0\nPKG=col2 1.0.0\n' > /srv/c.template
|
||||
B -d $P /srv/c.template /mnt/c
|
||||
check "exits 5" rc 5
|
||||
check "syspkg reported the collision" has "both ship /glacier/sys/links/bin/same."
|
||||
check "T has nothing installed" sh -c "! grep -q :: /mnt/c/glacier/sys/index/current"
|
||||
check "T's /usr has no links" sh -c "[ -z \"\$(ls -A /mnt/c/usr/bin)\" ]"
|
||||
|
||||
echo "== 12. -t with no shell: exit 6"
|
||||
mkdir -p /mnt/s
|
||||
printf 'NAME=s\nPKG=data 1.0.0\n' > /srv/s.template
|
||||
B -t -d $P /srv/s.template /mnt/s
|
||||
check "exits 6" rc 6
|
||||
check "says the smoke test failed" has "[x] chroot /mnt/s /bin/sh -c true failed."
|
||||
|
||||
echo "== 13. syspkg not on PATH: exit 5"
|
||||
mkdir -p /mnt/p
|
||||
OUT=$( (echo y | PATH=/usr/bin:/bin /app/glacier-bootstrap -d $P /srv/min.template /mnt/p; echo "rc=$?") 2>&1 | nocolor)
|
||||
check "exits 5" has "rc=5"
|
||||
check "says so" has "[x] syspkg is not on PATH."
|
||||
check "T untouched" untouched /mnt/p
|
||||
|
||||
echo "== 14. repositories: T's own repos.conf and keys"
|
||||
# tkey is trusted by T's repos.conf only; the host's lists glacier-test.
|
||||
gsign -G -p /srv/tkey.pub -s /root/tkey.sec -c tkey >/dev/null
|
||||
R=/srv/repo/base
|
||||
mkdir -p $R && chmod 755 /srv/repo $R
|
||||
# repos NAME URL -> package NAME 1.0.0 (base), shipping T's keyring
|
||||
repos() {
|
||||
rm -rf /srv/src/$1; mkdir -p /srv/src/$1/etc/glacier/keys
|
||||
cp /srv/tkey.pub /srv/src/$1/etc/glacier/keys/
|
||||
printf 'repos = (\n\t{ name = "base";%s keys = [ "tkey" ]; }\n);\n' "${2:+ url = \"$2\";}" \
|
||||
> /srv/src/$1/etc/glacier/repos.conf
|
||||
pkg $1 1.0.0 base
|
||||
}
|
||||
repos trepos file://$R
|
||||
repos badrepos file:///srv/nowhere
|
||||
repos keyrepos ""
|
||||
cp $P/alpmusl-1.2.5.gpkg* $P/abox-1.37.0.gpkg* $P/trepos-1.0.0.gpkg* $R/
|
||||
pkg lib 1.0.0 base; cp $P/lib-1.0.0.gpkg* $R/
|
||||
grepo -s /root/tkey.sec -r base $R >/dev/null
|
||||
check "grepo signed the test repo" test -f $R/repo.idx.sig
|
||||
printf 'NAME=r\nPKG=alpmusl 1.2.5\nPKG=abox 1.37.0\nPKG=trepos 1.0.0\n' > /srv/repo.template
|
||||
mkdir -p /mnt/r
|
||||
B -d $P /srv/repo.template /mnt/r
|
||||
check "exits 0" rc 0
|
||||
check "synced" has "[✓] All repositories synced successfully."
|
||||
check "then Bootstrapped" has "[✓] Bootstrapped r into /mnt/r."
|
||||
check "T holds the index" cmp -s $R/repo.idx /mnt/r/glacier/sys/repos/base.idx
|
||||
check "and its signature" cmp -s $R/repo.idx.sig /mnt/r/glacier/sys/repos/base.idx.sig
|
||||
check "the index is world-readable" sh -c "[ \"\$(stat -c %a /mnt/r/glacier/sys/repos/base.idx)\" = 644 ]"
|
||||
check "the host got no index" test ! -e /glacier/sys/repos/base.idx
|
||||
OUT=$( (echo y | syspkg -R /mnt/r -f lib; echo "rc=$?") 2>&1 | nocolor); echo "$OUT" | sed 's/^/ /'
|
||||
check "syspkg -R T -f uses T's repositories" has "rc=0"
|
||||
check "and installed into T" sh -c "grep -q '^base::lib::1.0.0::' /mnt/r/glacier/sys/index/current"
|
||||
|
||||
echo "== 15. a failed fetch: exit 7, T stays installed"
|
||||
mkdir -p /mnt/f
|
||||
printf 'NAME=f\nPKG=alpmusl 1.2.5\nPKG=abox 1.37.0\nPKG=badrepos 1.0.0\n' > /srv/f.template
|
||||
B -d $P /srv/f.template /mnt/f
|
||||
check "exits 7" rc 7
|
||||
check "says why" has "[x] Cannot fetch the index for base."
|
||||
check "no Bootstrapped" lacks "Bootstrapped"
|
||||
check "T is installed" sh -c "grep -q '^base::badrepos::1.0.0' /mnt/f/glacier/sys/index/current"
|
||||
|
||||
echo "== 15b. T's repos.conf has no url: a warning, exit 0"
|
||||
mkdir -p /mnt/k
|
||||
printf 'NAME=k\nPKG=alpmusl 1.2.5\nPKG=abox 1.37.0\nPKG=keyrepos 1.0.0\n' > /srv/k.template
|
||||
B -d $P /srv/k.template /mnt/k
|
||||
check "exits 0" rc 0
|
||||
check "warns" has "[!] No remote repositories are configured in /mnt/k."
|
||||
check "no refresh" lacks "Syncing"
|
||||
check "Bootstrapped" has "[✓] Bootstrapped k into /mnt/k."
|
||||
|
||||
echo "== 16. T's keyring must resolve inside T"
|
||||
mkdir -p /srv/hostconf && cp /mnt/r/etc/glacier/repos.conf /srv/hostconf/
|
||||
rm /mnt/r/etc/glacier/repos.conf; ln -s /srv/hostconf/repos.conf /mnt/r/etc/glacier/repos.conf
|
||||
OUT=$(syspkg -R /mnt/r -y 2>&1 | nocolor); echo "$OUT" | sed 's/^/ /'
|
||||
check "repos.conf outside T refused" has "[x] Refusing /mnt/r/etc/glacier/repos.conf: it resolves outside /mnt/r."
|
||||
rm /mnt/r/etc/glacier/repos.conf; cp /srv/hostconf/repos.conf /mnt/r/etc/glacier/
|
||||
rm /mnt/r/etc/glacier/keys/tkey.pub; ln -s /srv/tkey.pub /mnt/r/etc/glacier/keys/tkey.pub
|
||||
OUT=$(syspkg -R /mnt/r -y 2>&1 | nocolor); echo "$OUT" | sed 's/^/ /'
|
||||
check "a key outside T refused" has "[x] Refusing /mnt/r/etc/glacier/keys/tkey.pub: it resolves outside /mnt/r."
|
||||
rm /mnt/r/etc/glacier/keys/tkey.pub; cp /srv/tkey.pub /mnt/r/etc/glacier/keys/
|
||||
OUT=$(syspkg -R /mnt/r -y 2>&1 | nocolor); echo "$OUT" | sed 's/^/ /'
|
||||
check "restored: syncs again" has "[✓] All repositories synced successfully."
|
||||
|
||||
echo
|
||||
echo "FAILURES: $FAILS"
|
||||
[ $FAILS -eq 0 ]
|
||||
@@ -0,0 +1,276 @@
|
||||
#!/bin/sh
|
||||
# glacier-bootstrap, local mode (ROADMAP 1.3, GLACIER_BOOTSTRAP.txt):
|
||||
# - a template + PKGDIR become a chrootable system in T, as root and as
|
||||
# a user through doas; the host's /usr, /glacier and /etc stay
|
||||
# byte-identical; the private copies are removed;
|
||||
# - every template, verification and resolution problem is reported
|
||||
# (all of them, each with its own exit status) before T is touched;
|
||||
# - a pin is enforced, and a tampered file never reaches syspkg;
|
||||
# - T must pass syspkg -R's trust check and be fresh, or initialised
|
||||
# with nothing installed (a declined prompt can be re-run);
|
||||
# - one transaction: a syspkg refusal leaves T with nothing installed;
|
||||
# - -n never touches T; -t fails (6) when T has no working /bin/sh.
|
||||
# Runs INSIDE the Dockerfile.alpine_root image:
|
||||
#
|
||||
# docker run --rm -v "$PWD/tests/bootstrap_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
# B ARGS...: run glacier-bootstrap as root, answering the prompt with $ANS
|
||||
# (default y). Sets $OUT and $RC.
|
||||
B() {
|
||||
OUT=$( (echo "${ANS:-y}" | glacier-bootstrap "$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed 's/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
untouched() { [ -z "$(ls -A "$1")" ]; }
|
||||
|
||||
P=/srv/pkgs
|
||||
mkdir -p $P /srv/src
|
||||
|
||||
# pkg NAME VER REPO [DEPS] [CONFS] -> $P/NAME-VER.gpkg + sidecar; files
|
||||
# come from /srv/src/NAME (laid out as files/...), if it exists.
|
||||
pkg() {
|
||||
w=$(mktemp -d); d=$w/$1-$2
|
||||
mkdir -p $d/files
|
||||
[ -d /srv/src/$1 ] && cp -a /srv/src/$1/. $d/files/
|
||||
list() { l=""; sep=""; for it in $1; do l="$l$sep{ PKG_NAME = \"${it%%:*}\"; PKG_VER = \"${it#*:}\"; }"; sep=", "; done; echo "$l"; }
|
||||
printf 'PKG_NAME = "%s";\nPKG_VER = "%s";\nPKG_DESC = "t";\nPKG_MAINT = "t@e";\nPKG_COPYRIGHT = "MIT";\nPKG_REPO = "%s";\nPKG_DEPS = (%s);\nPKG_CONFS = (%s);\n' \
|
||||
"$1" "$2" "$3" "$(list "$4")" "$(list "$5")" > $d/manifest.gpm.cfg
|
||||
tar czf $P/$1-$2.gpkg -C $w $1-$2
|
||||
rm -rf $w
|
||||
sidecar $P/$1-$2.gpkg
|
||||
}
|
||||
sidecar() { printf 'sha256:%s\n' "$(sha256sum "$1" | cut -d' ' -f1)" > "$1.sha256"; gsign -S -s /root/glacier-test.sec -m "$1.sha256" >/dev/null; }
|
||||
hash() { sha256sum "$1" | cut -d' ' -f1; }
|
||||
|
||||
# A minimal system out of Alpine's own loader and busybox: alpmusl puts
|
||||
# the loader where PT_INTERP looks (/lib -> usr/lib), abox gives /bin/sh.
|
||||
mkdir -p /srv/src/alpmusl/usr/lib
|
||||
cp /lib/ld-musl-x86_64.so.1 /srv/src/alpmusl/usr/lib/
|
||||
ln -s ld-musl-x86_64.so.1 /srv/src/alpmusl/usr/lib/libc.musl-x86_64.so.1
|
||||
mkdir -p /srv/src/abox/usr/bin
|
||||
cp /bin/busybox /srv/src/abox/usr/bin/busybox
|
||||
ln -s busybox /srv/src/abox/usr/bin/sh
|
||||
mkdir -p /srv/src/abox/etc; echo "from abox" > /srv/src/abox/etc/motd
|
||||
pkg alpmusl 1.2.5 base
|
||||
pkg abox 1.37.0 base "alpmusl:1.2.5"
|
||||
mkdir -p /srv/src/tool/usr/bin; printf '#!/bin/sh\necho tool-ok\n' > /srv/src/tool/usr/bin/tool; chmod 755 /srv/src/tool/usr/bin/tool
|
||||
pkg tool 2.0.0 extra "abox:1.0.0"
|
||||
# Resolution fixtures
|
||||
pkg lib 1.0.0 extra
|
||||
pkg app 1.0.0 extra "lib:2.0.0"
|
||||
pkg needy 1.0.0 extra "ghost:1.0.0"
|
||||
pkg rival 1.0.0 extra "" "abox:0.0.0"
|
||||
# Collision: both ship /glacier/sys/links/bin/same
|
||||
mkdir -p /srv/src/col1/usr/bin /srv/src/col2/usr/bin
|
||||
echo 1 > /srv/src/col1/usr/bin/same; echo 2 > /srv/src/col2/usr/bin/same
|
||||
pkg col1 1.0.0 extra; pkg col2 1.0.0 extra
|
||||
# No shell at all
|
||||
pkg data 1.0.0 extra
|
||||
|
||||
T=/mnt/img
|
||||
mkdir -p $T
|
||||
cat > /srv/min.template <<EOF
|
||||
# a minimal system
|
||||
NAME=minimal
|
||||
PKG=alpmusl 1.2.5 sha256:$(hash $P/alpmusl-1.2.5.gpkg)
|
||||
PKG=abox 1.37.0
|
||||
PKG=tool 2.0.0
|
||||
EOF
|
||||
|
||||
snap() {
|
||||
find /usr /glacier /etc -xdev -printf '%p %y %m %U %G %s %T@ %l\n' 2>/dev/null | sort
|
||||
find /usr /glacier /etc -xdev -type f -exec sha256sum {} + 2>/dev/null | sort
|
||||
}
|
||||
snap > /tmp/before
|
||||
ls -A /tmp > /tmp/tmp-before
|
||||
|
||||
echo "== 1. bootstrap as root, with the smoke test"
|
||||
B -t -d $P /srv/min.template $T
|
||||
check "exits 0" rc 0
|
||||
check "syspkg's summary was shown" has "Proceed? (y/N)"
|
||||
check "ends with Bootstrapped" has "[✓] Bootstrapped minimal into $T."
|
||||
check "smoke test reported" has "[✓] chroot $T /bin/sh -c true succeeded."
|
||||
check "the pin is shown" has " * base::alpmusl 1.2.5 (pinned)"
|
||||
check "syspkg read the verified copies, not PKGDIR" lacks "staged from $P/"
|
||||
check "T's index lists all three" sh -c "grep -c :: $T/glacier/sys/index/current | grep -qx 3"
|
||||
check "chroot T runs a base program" sh -c "chroot $T /bin/sh -c 'echo ok' | grep -qx ok"
|
||||
check "chroot T runs an extra program (sys links)" sh -c "chroot $T /glacier/sys/links/bin/tool | grep -qx tool-ok"
|
||||
check "/etc was copied into T" sh -c "grep -qx 'from abox' $T/etc/motd && ! [ -L $T/etc/motd ]"
|
||||
check "T's links say runtime paths" sh -c "readlink $T/usr/bin/busybox | grep -q '^/glacier/sys/store/'"
|
||||
snap > /tmp/after
|
||||
check "host /usr, /glacier and /etc are byte-identical" cmp -s /tmp/before /tmp/after
|
||||
ls -A /tmp | grep -v '^after$' | grep -v '^tmp-after$' > /tmp/tmp-after
|
||||
check "the private copies were removed" cmp -s /tmp/tmp-before /tmp/tmp-after
|
||||
|
||||
echo "== 2. T already has packages: refused"
|
||||
B -d $P /srv/min.template $T
|
||||
check "exits 1" rc 1
|
||||
check "says so" has "[x] $T already has packages installed."
|
||||
|
||||
echo "== 3. as a user, through doas"
|
||||
echo "permit nopass glacier as root cmd /app/syspkg" >> /etc/doas.conf
|
||||
echo "permit nopass glacier as root cmd chroot" >> /etc/doas.conf
|
||||
mkdir -p /mnt/u
|
||||
OUT=$(su glacier -c "PATH=/app:\$PATH; echo y | glacier-bootstrap -t -d $P /srv/min.template /mnt/u; echo rc=\$?" 2>&1 | nocolor)
|
||||
echo "$OUT" | sed 's/^/ /'
|
||||
check "exits 0" has "rc=0"
|
||||
check "bootstrapped" has "[✓] Bootstrapped minimal into /mnt/u."
|
||||
check "smoke test passed" has "succeeded."
|
||||
check "T's store is root's" sh -c "[ \"\$(stat -c %u /mnt/u/glacier/sys/store)\" = 0 ]"
|
||||
check "the user's private copies were removed" sh -c "! find /tmp -maxdepth 1 -user glacier | grep -q ."
|
||||
|
||||
echo "== 4. template errors: all reported, exit 2, T untouched"
|
||||
mkdir -p /mnt/e
|
||||
cat > /srv/bad.template <<EOF
|
||||
NAME=bad
|
||||
SYS_PROFILE=x86_64-musl
|
||||
PKG=abox 1.37.0
|
||||
PKG=abox 1.37.0
|
||||
PKG=lib 1.0
|
||||
PKG=lib2 1.0.0 sha256:abc
|
||||
PKG=lib3
|
||||
just words
|
||||
NAME=again
|
||||
EOF
|
||||
B -d $P /srv/bad.template /mnt/e
|
||||
check "exits 2" rc 2
|
||||
check "unknown key" has "bad.template:2: Unknown key: SYS_PROFILE"
|
||||
check "duplicate package" has "bad.template:4: abox is listed twice (first on line 3)."
|
||||
check "two-part version" has "bad.template:5: Invalid version for lib: 1.0"
|
||||
check "malformed pin" has "bad.template:6: Malformed pin for lib2: sha256:abc"
|
||||
check "PKG with no version" has "bad.template:7: Expected PKG=NAME VERSION [sha256:HEX]."
|
||||
check "not KEY=value" has "bad.template:8: Expected KEY=value."
|
||||
check "second NAME" has "bad.template:9: NAME given more than once."
|
||||
check "T untouched" untouched /mnt/e
|
||||
printf 'PKG=abox 1.37.0\n' > /srv/noname.template
|
||||
B -d $P /srv/noname.template /mnt/e
|
||||
check "no NAME: exit 2" rc 2
|
||||
check "no NAME: says so" has "noname.template: No NAME."
|
||||
printf 'NAME=empty\n# nothing\n' > /srv/empty.template
|
||||
B -d $P /srv/empty.template /mnt/e
|
||||
check "no packages: says so" has "empty.template: No packages."
|
||||
B -d $P /srv/missing.template /mnt/e
|
||||
check "missing template: exit 2" rc 2
|
||||
|
||||
echo "== 5. verification: all reported, exit 3, T untouched"
|
||||
mkdir -p /srv/v
|
||||
for f in alpmusl-1.2.5 abox-1.37.0 tool-2.0.0 lib-1.0.0; do cp $P/$f.gpkg $P/$f.gpkg.sha256 /srv/v/; done
|
||||
rm /srv/v/abox-1.37.0.gpkg.sha256
|
||||
echo "sha256:nothex" > /srv/v/lib-1.0.0.gpkg.sha256
|
||||
echo junk >> /srv/v/tool-2.0.0.gpkg
|
||||
cat > /srv/v.template <<EOF
|
||||
NAME=v
|
||||
PKG=alpmusl 1.2.5 sha256:$(printf '0%.0s' $(seq 64))
|
||||
PKG=abox 1.37.0
|
||||
PKG=tool 2.0.0
|
||||
PKG=lib 1.0.0
|
||||
PKG=gone 1.0.0
|
||||
EOF
|
||||
B -d /srv/v /srv/v.template /mnt/e
|
||||
check "exits 3" rc 3
|
||||
check "pin mismatch" has "[x] Refusing /srv/v/alpmusl-1.2.5.gpkg: the template pins 0000"
|
||||
check "no sidecar" has "[x] Refusing /srv/v/abox-1.37.0.gpkg: no sidecar /srv/v/abox-1.37.0.gpkg.sha256."
|
||||
check "tampered file" has "[x] Refusing /srv/v/tool-2.0.0.gpkg: hash mismatch"
|
||||
check "malformed sidecar" has "[x] Refusing /srv/v/lib-1.0.0.gpkg: malformed sidecar"
|
||||
check "missing file" has "[x] Refusing /srv/v/gone-1.0.0.gpkg: no such file."
|
||||
check "T untouched" untouched /mnt/e
|
||||
|
||||
echo "== 6. a pin binds even when the sidecar is rewritten to match"
|
||||
mkdir -p /srv/w; cp $P/alpmusl-1.2.5.gpkg /srv/w/; echo x >> /srv/w/alpmusl-1.2.5.gpkg; sidecar /srv/w/alpmusl-1.2.5.gpkg
|
||||
printf 'NAME=w\nPKG=alpmusl 1.2.5 sha256:%s\n' "$(hash $P/alpmusl-1.2.5.gpkg)" > /srv/w.template
|
||||
B -d /srv/w /srv/w.template /mnt/e
|
||||
check "exits 3" rc 3
|
||||
check "pin vs sidecar" has "the template pins $(hash $P/alpmusl-1.2.5.gpkg), the sidecar says"
|
||||
check "T untouched" untouched /mnt/e
|
||||
|
||||
echo "== 7. resolution: all reported, exit 4, T untouched"
|
||||
mkdir -p /srv/r; cp $P/* /srv/r/
|
||||
cp $P/lib-1.0.0.gpkg /srv/r/libx-1.0.0.gpkg; sidecar /srv/r/libx-1.0.0.gpkg
|
||||
cat > /srv/r.template <<EOF
|
||||
NAME=r
|
||||
PKG=alpmusl 1.2.5
|
||||
PKG=abox 1.37.0
|
||||
PKG=libx 1.0.0
|
||||
PKG=lib 1.0.0
|
||||
PKG=app 1.0.0
|
||||
PKG=needy 1.0.0
|
||||
PKG=rival 1.0.0
|
||||
EOF
|
||||
B -d /srv/r /srv/r.template /mnt/e
|
||||
check "exits 4" rc 4
|
||||
check "file holds another package" has "[x] /srv/r/libx-1.0.0.gpkg is lib 1.0.0, not libx 1.0.0."
|
||||
sed -i '/libx/d' /srv/r.template
|
||||
B -d /srv/r /srv/r.template /mnt/e
|
||||
check "exits 4" rc 4
|
||||
check "dependency too old" has "[x] extra::app requires lib >= 2.0.0, but the template lists 1.0.0."
|
||||
check "dependency not listed" has "[x] extra::needy requires ghost >= 1.0.0, which the template does not list."
|
||||
check "conflict" has "[x] base::abox conflicts with extra::rival."
|
||||
check "T untouched" untouched /mnt/e
|
||||
|
||||
echo "== 8. -n: plan only"
|
||||
B -n -d $P /srv/min.template /mnt/e
|
||||
check "exits 0" rc 0
|
||||
check "says dry run" has "[✓] Dry run: every package verified and resolved."
|
||||
check "never asks" lacks "Proceed"
|
||||
check "T untouched" untouched /mnt/e
|
||||
|
||||
echo "== 9. target refusals (exit 1)"
|
||||
B -d $P /srv/min.template mnt/e
|
||||
check "relative: exit 1" rc 1
|
||||
check "relative: says so" has "[x] TARGET must be an absolute path: mnt/e"
|
||||
B -d $P /srv/min.template /
|
||||
check "/: refused" has "[x] TARGET must not be /."
|
||||
mkdir -p /tmp/tt
|
||||
B -d $P /srv/min.template /tmp/tt
|
||||
check "under /tmp: exit 1" rc 1
|
||||
check "under /tmp: the library says why" has "[x] Refusing install root /tmp/tt: /tmp is not root-owned, or is group/world-writable."
|
||||
B -d $P /srv/min.template /mnt/nonexistent
|
||||
check "missing: exit 1" rc 1
|
||||
B -x -d $P /srv/min.template /mnt/e
|
||||
check "unknown option: exit 1" rc 1
|
||||
check "unknown option: says so" has "[x] Unknown option: -x"
|
||||
|
||||
echo "== 10. declined prompt: exit 5, then re-runnable"
|
||||
mkdir -p /mnt/d
|
||||
ANS=n B -d $P /srv/min.template /mnt/d
|
||||
check "exits 5" rc 5
|
||||
check "says nothing was installed" has "[x] Nothing was installed into /mnt/d."
|
||||
check "T was initialised" test -e /mnt/d/glacier/sys/index/current
|
||||
B -d $P /srv/min.template /mnt/d
|
||||
check "re-run exits 0" rc 0
|
||||
check "re-run did not run -n again" lacks "Created system glacier directories"
|
||||
check "re-run installed" has "[✓] Bootstrapped minimal into /mnt/d."
|
||||
|
||||
echo "== 11. one transaction: a syspkg refusal installs nothing (exit 5)"
|
||||
mkdir -p /mnt/c
|
||||
printf 'NAME=c\nPKG=alpmusl 1.2.5\nPKG=abox 1.37.0\nPKG=col1 1.0.0\nPKG=col2 1.0.0\n' > /srv/c.template
|
||||
B -d $P /srv/c.template /mnt/c
|
||||
check "exits 5" rc 5
|
||||
check "syspkg reported the collision" has "both ship /glacier/sys/links/bin/same."
|
||||
check "T has nothing installed" sh -c "! grep -q :: /mnt/c/glacier/sys/index/current"
|
||||
check "T's /usr has no links" sh -c "[ -z \"\$(ls -A /mnt/c/usr/bin)\" ]"
|
||||
|
||||
echo "== 12. -t with no shell: exit 6"
|
||||
mkdir -p /mnt/s
|
||||
printf 'NAME=s\nPKG=data 1.0.0\n' > /srv/s.template
|
||||
B -t -d $P /srv/s.template /mnt/s
|
||||
check "exits 6" rc 6
|
||||
check "says the smoke test failed" has "[x] chroot /mnt/s /bin/sh -c true failed."
|
||||
|
||||
echo "== 13. syspkg not on PATH: exit 5"
|
||||
mkdir -p /mnt/p
|
||||
OUT=$( (echo y | PATH=/usr/bin:/bin /app/glacier-bootstrap -d $P /srv/min.template /mnt/p; echo "rc=$?") 2>&1 | nocolor)
|
||||
check "exits 5" has "rc=5"
|
||||
check "says so" has "[x] syspkg is not on PATH."
|
||||
check "T untouched" untouched /mnt/p
|
||||
|
||||
echo
|
||||
echo "FAILURES: $FAILS"
|
||||
[ $FAILS -eq 0 ]
|
||||
@@ -0,0 +1,454 @@
|
||||
#!/bin/sh
|
||||
# Source builds through syspkg/gpkg -f/-u (BUILD_DESIGN.txt; steps 3-4):
|
||||
# - a recipe from a NAME-src repo is built in gsandbox and installed
|
||||
# like a fetched package: summary and prompt BEFORE building, the
|
||||
# patches applied, the manifest the build was handed, .glacier-src;
|
||||
# - a name served by several repositories is asked about; Enter takes
|
||||
# the first, a bad answer refuses;
|
||||
# - TRAITS: recipe defaults, /etc/glacier/glacier.conf, per-package
|
||||
# files (an unknown trait refused), the user's own files for gpkg;
|
||||
# - -u stays on source, and rebuilds when the traits change;
|
||||
# - syspkg builds as BUILD_USER (required), gpkg as the user;
|
||||
# - PREFIX: a package is built for where its usr/ is linked
|
||||
# (/glacier/sys/links, the user's links, /usr for base) and still
|
||||
# packed as usr/; during the build PREFIX resolves to the layers;
|
||||
# - kept packages: an installed source build's .gpkg is kept, and a
|
||||
# later build uses it (and what it depends on) as a layer; kept
|
||||
# only while that version is installed;
|
||||
# - build output on the terminal (and in the log) unless -q/--quiet;
|
||||
# - refusals: a source with the wrong hash, a failing build, a build
|
||||
# that produces another package, a build dependency no binary repo
|
||||
# has, an untrusted per-package file, -R.
|
||||
# Needs what sandbox_alpine.sh needs, with /glacier off Docker's overlay:
|
||||
#
|
||||
# docker run --rm --security-opt seccomp=unconfined \
|
||||
# --security-opt apparmor=unconfined --security-opt systempaths=unconfined \
|
||||
# --tmpfs /glacier:exec,mode=755 \
|
||||
# -v "$PWD/tests/build_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
R() {
|
||||
OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed '/^rc=/d; s/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
hr() { echo; echo "== $*"; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
K=/etc/glacier/keys
|
||||
CONF=/etc/glacier/repos.conf
|
||||
GCONF=/etc/glacier/glacier.conf
|
||||
XSEC=/root/extrakey.sec
|
||||
SSEC=/root/srckey.sec
|
||||
E=/srv/extra
|
||||
S=/srv/extra-src
|
||||
SRC=/srv/sources
|
||||
IDX=/glacier/sys/index/current
|
||||
BUILD=/glacier/sys/build
|
||||
BS=/srv/base-src
|
||||
mkdir -p $E $S $SRC $BS && chmod 755 /srv $E $S $SRC $BS
|
||||
|
||||
gsign -G -p $K/extrakey.pub -s $XSEC -c extrakey >/dev/null
|
||||
gsign -G -p $K/srckey.pub -s $SSEC -c srckey >/dev/null
|
||||
rconf() { # BASE-KEYS
|
||||
printf 'repos = (\n%s\n%s\n%s\n%s\n);\n' \
|
||||
" { name = \"base\"; keys = [ $1 ]; }," \
|
||||
' { name = "extra"; url = "file:///srv/extra"; keys = [ "extrakey" ]; },' \
|
||||
' { name = "extra-src"; url = "file:///srv/extra-src"; keys = [ "srckey" ]; },' \
|
||||
' { name = "base-src"; url = "file:///srv/base-src"; keys = [ "srckey" ]; }' > $CONF
|
||||
chmod 644 $CONF
|
||||
}
|
||||
rconf '"glacier-test"'
|
||||
adduser -S -D -H -s /sbin/nologin gbuild
|
||||
gconf() { printf 'KEEP_OLD = 1;\n%s\n' "$1" > $GCONF; chmod 644 $GCONF; }
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
|
||||
# The binary repo: a toolchain (Alpine's build-base, busybox for /bin/sh)
|
||||
# and a runtime dependency.
|
||||
/app/tests/mkroot -o $E -r build-base -r busybox -p busybox toolchain 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E hidep 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E loudlib 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E both 9.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E twin 1.0.0 extra >/dev/null
|
||||
|
||||
# A source tarball: hello prints "hello", "HELLO" with LOUD=1.
|
||||
mksrc() { # NAME VER [MAKEFILE-EXTRA]
|
||||
d=/tmp/src/$1-$2
|
||||
rm -rf $d && mkdir -p $d
|
||||
printf '#include <stdio.h>\nint main(void) {\n#ifdef LOUD\n\tputs("HELLO");\n#else\n\tputs("hello");\n#endif\n\treturn 0;\n}\n' > $d/hello.c
|
||||
printf 'CFLAGS += $(if $(LOUD),-DLOUD)\n%s: hello.c\n\t$(CC) $(CFLAGS) -o %s hello.c\ninstall:\n\tinstall -D -m 755 %s $(DESTDIR)$(PREFIX)/bin/%s\n%s\n' \
|
||||
"$1" "$1" "$1" "$1" "${3:-}" > $d/Makefile
|
||||
tar czf $SRC/$1-$2.tar.gz -C /tmp/src $1-$2
|
||||
sha256sum $SRC/$1-$2.tar.gz | cut -c1-64
|
||||
}
|
||||
# A recipe bundle in the -src repo: NAME VER SHA [EXTRA-FIELDS] [BUILD-FILE]
|
||||
mkrecipe() {
|
||||
t=/tmp/rcp/$1-$2
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "%s"; VER = "%s"; REPO = "extra"; DESC = "test \\"%s\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/%s-%s.tar.gz"; SHA256 = "sha256:%s"; } );\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\n%s\n' \
|
||||
"$1" "$2" "$1" "$SRC" "$1" "$2" "$3" "${4:-}" > $t/recipe
|
||||
[ -n "${5:-}" ] && printf '%s\n' "$5" > $t/build
|
||||
rmdir $t/patches 2>/dev/null
|
||||
tar czf $S/$1-$2.grcp -C /tmp/rcp $1-$2
|
||||
}
|
||||
publish() {
|
||||
grepo -s $XSEC -r extra $E | grep -v Wrote; grepo -s $SSEC -r extra-src $S | grep -v Wrote
|
||||
grepo -s $SSEC -r base-src $BS | grep -v Wrote
|
||||
}
|
||||
|
||||
# hello 1.0.0, patched to greet glacier, depending on hidep.
|
||||
H=$(mksrc hello 1.0.0)
|
||||
t=/tmp/rcp/hello-1.0.0
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "hello"; VER = "1.0.0"; REPO = "extra"; DESC = "say \\"hi\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/hello-1.0.0.tar.gz"; SHA256 = "sha256:%s"; } );\nPATCHES = [ "01-glacier.patch" ];\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\nDEPS = ( { NAME = "hidep"; VER = "1.0.0"; }, { NAME = "loudlib"; VER = "1.0.0"; IF = "loud"; } );\n' "$SRC" "$H" > $t/recipe
|
||||
cat > $t/patches/01-glacier.patch <<'EOF'
|
||||
--- a/hello.c
|
||||
+++ b/hello.c
|
||||
@@ -5,3 +5,3 @@
|
||||
#else
|
||||
- puts("hello");
|
||||
+ puts("hello, glacier");
|
||||
#endif
|
||||
EOF
|
||||
tar czf $S/hello-1.0.0.grcp -C /tmp/rcp hello-1.0.0
|
||||
mkrecipe both 1.0.0 "$(mksrc both 1.0.0)"
|
||||
mkrecipe badhash 1.0.0 "$(printf '%064d' 0)"
|
||||
mksrc badhash 1.0.0 >/dev/null
|
||||
mkrecipe broken 1.0.0 "$(mksrc broken 1.0.0 'broken: ; false')" '' 'compile() { make broken; }'
|
||||
mkrecipe evil 1.0.0 "$(mksrc evil 1.0.0)" '' 'package() {
|
||||
mkdir -p /tmp/p/evil-1.0.0/files/usr/bin
|
||||
echo x > /tmp/p/evil-1.0.0/files/usr/bin/evil
|
||||
printf "PKG_NAME = \"evil\"; PKG_VER = \"1.0.0\"; PKG_REPO = \"base\"; PKG_DEPS = (); PKG_CONFS = ();\n" > /tmp/p/evil-1.0.0/manifest.gpm.cfg
|
||||
tar czf /.glacier/out/evil-1.0.0.gpkg -C /tmp/p evil-1.0.0
|
||||
exit 0
|
||||
}'
|
||||
mkrecipe core 1.0.0 "$(mksrc core 1.0.0)" '' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR$PREFIX/share" && echo "$PREFIX" > "$DESTDIR$PREFIX/share/core.prefix"
|
||||
}'
|
||||
sed -i 's/REPO = "extra"/REPO = "base"/' /tmp/rcp/core-1.0.0/recipe
|
||||
tar czf $BS/core-1.0.0.grcp -C /tmp/rcp core-1.0.0 && rm $S/core-1.0.0.grcp
|
||||
# where: a script with its data file's path compiled in. Its prepare
|
||||
# fails unless PREFIX leads to the layers during the build.
|
||||
mkrecipe where 1.0.0 "$(mksrc where 1.0.0)" 'BUILD = "hooks";' 'prepare() { [ -x "$PREFIX/bin/make" ]; }
|
||||
package() {
|
||||
mkdir -p "$DESTDIR$PREFIX/bin" "$DESTDIR$PREFIX/share/where"
|
||||
echo here > "$DESTDIR$PREFIX/share/where/data"
|
||||
printf "#!/bin/sh\ncat %s/share/where/data\n" "$PREFIX" > "$DESTDIR$PREFIX/bin/where"
|
||||
chmod 755 "$DESTDIR$PREFIX/bin/where"
|
||||
}'
|
||||
sed -i 's/^BUILD = "make";$//' /tmp/rcp/where-1.0.0/recipe && tar czf $S/where-1.0.0.grcp -C /tmp/rcp where-1.0.0
|
||||
mkrecipe split 1.0.0 "$(mksrc split 1.0.0)" '' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR/usr/share" && echo x > "$DESTDIR/usr/share/split"
|
||||
}'
|
||||
# srclib: only a recipe, depending on hidep. srcapp needs both in its
|
||||
# layers to build.
|
||||
mkrecipe srclib 1.0.0 "$(mksrc srclib 1.0.0)" 'DEPS = ( { NAME = "hidep"; VER = "1.0.0"; } );' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR$PREFIX/share/srclib" && echo yes > "$DESTDIR$PREFIX/share/srclib/marker"
|
||||
}'
|
||||
mkrecipe srcapp 1.0.0 "$(mksrc srcapp 1.0.0)" 'DEPS = ( { NAME = "srclib"; VER = "1.0.0"; } );' 'prepare() { [ -f /usr/share/srclib/marker ] && [ -x /usr/bin/hidep ]; }'
|
||||
mkrecipe twin 1.0.0 "$(mksrc twin 1.0.0)"
|
||||
mkrecipe needy 1.0.0 "$(mksrc needy 1.0.0)" 'BUILD_DEPS = ( { NAME = "nothere"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
|
||||
syspkg -n >/dev/null
|
||||
yes_to() { sh -c "printf '$1' | $2"; }
|
||||
|
||||
hr "syspkg -f of a recipe"
|
||||
R yes_to 'y\n' 'syspkg -f hello'
|
||||
check "built and installed" has "[✓] Completed with no errors."
|
||||
check "...exit 0" rc 0
|
||||
check "the summary marks the build, its traits off by default" has " * extra-src::hello 1.0.0 [-loud] (build)"
|
||||
check "...and the binary dependency" has " * extra::hidep 1.0.0 (dependency)"
|
||||
check "...the prompt came before the build" sh -c 'echo "$0" | grep -n "Proceed\|Building extra-src" | head -1 | grep -q Proceed' "$OUT"
|
||||
check "the build is announced" has "[i] Building extra-src::hello 1.0.0 [-loud]..."
|
||||
check "...and done" has "[✓] Built extra-src::hello 1.0.0."
|
||||
check "installed as extra::hello" grep -q "^extra::hello::1.0.0::explicit$" $IDX
|
||||
check "...with its dependency, auto" grep -q "^extra::hidep::1.0.0::auto$" $IDX
|
||||
check "...not the one only loud needs" sh -c "! grep -q loudlib $IDX"
|
||||
check "...the patch applied" [ "$(/glacier/sys/links/bin/hello)" = "hello, glacier" ]
|
||||
V=/glacier/sys/store/extra/hello/hello-1.0.0
|
||||
check "...recorded as built from extra-src" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...its manifest has PKG_TRAITS" grep -qF 'PKG_TRAITS = [ "-loud" ];' $V/.glacier-manifest
|
||||
check "...and the recipe's DESC, quoted" grep -qF 'PKG_DESC = "say \"hi\"";' $V/.glacier-manifest
|
||||
check "the layer belongs to BUILD_USER" [ "$(stat -c %U $BUILD/layers/toolchain-1.0.0-*)" = gbuild ]
|
||||
check "...which owns layers/, 0700" [ "$(stat -c '%U %a' $BUILD/layers)" = "gbuild 700" ]
|
||||
check "the build's in/work/out dirs are gone" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
check "the log is kept" grep -q "==> Done" $BUILD/logs/hello-1.0.0.log
|
||||
check "...and the build's output was on the terminal" has "==> Applying 01-glacier.patch"
|
||||
check "...the sources cached by hash" [ -f $BUILD/sources/$H ]
|
||||
check "the cached bundle is deleted" [ ! -e /glacier/sys/cache/extra-src/hello-1.0.0.grcp ]
|
||||
check "the store copy is root's, as any install" [ "$(stat -c %u /glacier/sys/store/extra/hello/hello-1.0.0/usr/bin/hello)" = 0 ]
|
||||
|
||||
hr "declining builds nothing"
|
||||
rm $BUILD/logs/hello-1.0.0.log
|
||||
R yes_to 'n\n' 'syspkg -f -O hello'
|
||||
check "nothing built" sh -c "[ ! -e $BUILD/logs/hello-1.0.0.log ]"
|
||||
check "...nothing said about building" lacks "Building"
|
||||
|
||||
hr "traits"
|
||||
mkdir -p /etc/glacier/config
|
||||
printf 'TRAITS = [ "lod" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "an unknown trait in a per-package file is refused" has "[x] /etc/glacier/config/hello.conf: hello has no trait lod."
|
||||
check "...exit 1, before the prompt" sh -c "[ $RC = 1 ]" && check "...no prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud", "x11" ];'
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "the per-package file beats glacier.conf: nothing to rebuild" has "Nothing to do."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "glacier.conf turns loud on (x11, unknown to the recipe, ignored): a rebuild" has " * extra-src::hello 1.0.0 (traits) [+loud] (build)"
|
||||
check "...installed" rc 0
|
||||
check "...built with LOUD" [ "$(/glacier/sys/links/bin/hello)" = "HELLO" ]
|
||||
check "...with the dependency loud needs" grep -q "^extra::loudlib::1.0.0::auto$" $IDX
|
||||
check "...in its manifest" grep -qF '{ PKG_NAME = "loudlib"; PKG_VER = "1.0.0"; }' $V/.glacier-manifest
|
||||
check "...still marked as built from source" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...PKG_TRAITS updated" grep -qF 'PKG_TRAITS = [ "loud" ];' $V/.glacier-manifest
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "unchanged traits: nothing to do" has "Nothing to do."
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
chmod 666 /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a world-writable per-package file is refused" has "[x] Refusing /etc/glacier/config/hello.conf: /etc/glacier/config/hello.conf is not root-owned, or is group/world-writable."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
printf 'TRAITS = [ "loud" ]; COLOR = "red";\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a per-package file with an unknown key is malformed" has "[x] Malformed /etc/glacier/config/hello.conf."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
|
||||
hr "-u stays on source"
|
||||
H11=$(mksrc hello 1.1.0)
|
||||
sed "s/1\.0\.0/1.1.0/g; s/$H/$H11/" /tmp/rcp/hello-1.0.0/recipe > /tmp/r.new
|
||||
rm -rf /tmp/rcp/hello-1.1.0 && cp -a /tmp/rcp/hello-1.0.0 /tmp/rcp/hello-1.1.0 && mv /tmp/r.new /tmp/rcp/hello-1.1.0/recipe
|
||||
sed -i 's/VER = "1.1.0"; }/VER = "1.0.0"; }/g; s/VER = "1.1.0"; IF/VER = "1.0.0"; IF/' /tmp/rcp/hello-1.1.0/recipe
|
||||
tar czf $S/hello-1.1.0.grcp -C /tmp/rcp hello-1.1.0
|
||||
/app/tests/mkpkg -o $E hello 2.0.0 extra >/dev/null
|
||||
publish
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "a newer recipe is built, not extra's binary 2.0.0" has " * extra-src::hello 1.0.0 -> 1.1.0 [+loud] (build)"
|
||||
check "...installed" grep -q "^extra::hello::1.1.0::explicit$" $IDX
|
||||
check "...the old version backed up" [ -f /glacier/sys/backup/extra/hello/hello-1.0.0.gpkg ]
|
||||
check "...its new package kept for builds" [ -f $BUILD/packages/extra@hello-1.1.0.gpkg ]
|
||||
check "...the old one's deleted" [ ! -e $BUILD/packages/extra@hello-1.0.0.gpkg ]
|
||||
check "...still on source" [ "$(cat /glacier/sys/store/extra/hello/hello-1.1.0/.glacier-src)" = extra-src ]
|
||||
|
||||
hr "PREFIX"
|
||||
R yes_to 'y\n' 'syspkg -f where'
|
||||
check "built for /glacier/sys/links: finds its data through the links" [ "$(/glacier/sys/links/bin/where)" = here ]
|
||||
check "...the path compiled in" grep -q "^cat /glacier/sys/links/share/where/data$" /glacier/sys/links/bin/where
|
||||
check "...packed as usr/, as any package" [ -f /glacier/sys/store/extra/where/where-1.0.0/usr/share/where/data ]
|
||||
check "...nothing else at the top" [ "$(ls /glacier/sys/store/extra/where/where-1.0.0)" = usr ]
|
||||
R yes_to 'y\n' 'syspkg -f split'
|
||||
check "a build installing into PREFIX and /usr both fails" has "[x] Building extra-src::split 1.0.0 failed."
|
||||
check "...the log says why" grep -q "split installed into both /glacier/sys/links and /usr" $BUILD/logs/split-1.0.0.log
|
||||
|
||||
hr "kept packages"
|
||||
P=$BUILD/packages
|
||||
check "a source build's package is kept, root's 0644" [ "$(stat -c '%u %a' $P/extra@where-1.0.0.gpkg)" = "0 644" ]
|
||||
check "...byte for byte what was installed" sh -c "tar xzOf $P/extra@where-1.0.0.gpkg where-1.0.0/files/usr/bin/where | cmp -s - /glacier/sys/store/extra/where/where-1.0.0/usr/bin/where"
|
||||
check "...not a failed one" [ ! -e $P/extra@split-1.0.0.gpkg ]
|
||||
R yes_to 'y\n' 'syspkg -q -f srclib'
|
||||
check "srclib (no binary anywhere) built and installed" rc 0
|
||||
check "...-q: no build output on the terminal" lacks "==> "
|
||||
check "...but the build is still announced" has "[✓] Built extra-src::srclib 1.0.0."
|
||||
check "...and logged" grep -q "==> Done" $BUILD/logs/srclib-1.0.0.log
|
||||
R yes_to 'y\n' 'syspkg --quiet -f srcapp'
|
||||
check "--quiet too" lacks "==> "
|
||||
check "a build needing an installed source build: built" has "[✓] Built extra-src::srcapp 1.0.0."
|
||||
check "...installed" grep -q "^extra::srcapp::1.0.0::explicit$" $IDX
|
||||
check "...the kept package was its layer" sh -c "ls $BUILD/layers | grep -q '^srclib-1.0.0-'"
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/srcapp.conf
|
||||
R yes_to 'y\n' 'syspkg -u srcapp'
|
||||
check "-u rebuilds it (traits changed)" has " * extra-src::srcapp 1.0.0 (traits) [-loud] (build)"
|
||||
check "...exit 0" rc 0
|
||||
mv $P/extra@srclib-1.0.0.gpkg /root/kept.bak
|
||||
rm /etc/glacier/config/srcapp.conf
|
||||
R yes_to 'y\n' 'syspkg -u srcapp'
|
||||
mv /root/kept.bak $P/extra@srclib-1.0.0.gpkg
|
||||
check "without its kept package, srclib can't be a layer" has "[x] extra-src::srcapp 1.0.0 needs srclib 1.0.0 or later to build, and no binary repository has it."
|
||||
R yes_to 'y\n' 'syspkg -x srcapp'
|
||||
check "removing srcapp deletes its kept package" [ ! -e $P/extra@srcapp-1.0.0.gpkg ]
|
||||
check "...not srclib's, still installed" [ -f $P/extra@srclib-1.0.0.gpkg ]
|
||||
R yes_to 'y\n' 'syspkg -x srclib'
|
||||
check "removing srclib deletes its own" [ ! -e $P/extra@srclib-1.0.0.gpkg ]
|
||||
check "...others stay" [ -f $P/extra@where-1.0.0.gpkg ]
|
||||
R yes_to '2\ny\n' 'syspkg -f twin'
|
||||
check "twin built from source: kept" [ -f $P/extra@twin-1.0.0.gpkg ]
|
||||
R yes_to '1\ny\n' 'syspkg -f -O twin'
|
||||
check "the same version reinstalled from the binary repo" [ "$(/glacier/sys/links/bin/twin)" = "twin 1.0.0 says hi" ]
|
||||
check "...no longer kept" [ ! -e $P/extra@twin-1.0.0.gpkg ]
|
||||
|
||||
hr "the repository question"
|
||||
R yes_to '\ny\n' 'syspkg -f both'
|
||||
check "both repos serving a name: asked" has "[i] both is available from more than one repository:"
|
||||
check "...the binary first (repos.conf order)" has " 1) extra::both 9.0.0"
|
||||
check "...then the recipe" has " 2) extra-src::both 1.0.0 [+loud] (build)"
|
||||
check "...Enter takes the first" grep -q "^extra::both::9.0.0::explicit$" $IDX
|
||||
check "...not marked as built" [ ! -e /glacier/sys/store/extra/both/both-9.0.0/.glacier-src ]
|
||||
check "...nor kept" [ ! -e $BUILD/packages/extra@both-9.0.0.gpkg ]
|
||||
R yes_to '2\ny\n' 'syspkg -f -O both'
|
||||
check "2 builds the recipe (replacing the binary under -O)" grep -q "^extra::both::1.0.0::explicit$" $IDX
|
||||
check "...marked as built" [ "$(cat /glacier/sys/store/extra/both/both-1.0.0/.glacier-src)" = extra-src ]
|
||||
check "...and kept" [ -f $BUILD/packages/extra@both-1.0.0.gpkg ]
|
||||
R yes_to '3\n' 'syspkg -f -O both'
|
||||
check "an answer out of range is refused" has "[x] Invalid selection."
|
||||
check "...exit 1" rc 1
|
||||
R yes_to 'x\n' 'syspkg -f -O both'
|
||||
check "a non-number is refused" has "[x] Invalid selection."
|
||||
R sh -c 'syspkg -f -O both < /dev/null'
|
||||
check "end of input is refused" has "[x] Invalid selection."
|
||||
|
||||
hr "refused builds"
|
||||
R yes_to 'y\n' 'syspkg -f badhash'
|
||||
check "a source with the wrong hash" has "[x] A source of extra-src::badhash 1.0.0 does not match its hash."
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q badhash $IDX"
|
||||
R yes_to 'y\n' 'syspkg -f broken'
|
||||
check "a failing build" has "[x] Building extra-src::broken 1.0.0 failed. Log: $BUILD/logs/broken-1.0.0.log"
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q broken $IDX"
|
||||
check "...the log says why" grep -q "Error\|error" $BUILD/logs/broken-1.0.0.log
|
||||
check "...its dirs removed" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
R yes_to 'y\n' 'syspkg -f evil'
|
||||
check "a build that produces another package" has "[x] extra-src::evil 1.0.0: the build produced another package."
|
||||
check "...nothing installed" sh -c "! grep -q evil $IDX && [ ! -e /usr/bin/evil ]"
|
||||
# An index entry that disagrees with its bundle's recipe (signed all
|
||||
# the same): the recipe has a dependency the index left out.
|
||||
mkrecipe liar 1.0.0 "$(mksrc liar 1.0.0)" 'DEPS = ( { NAME = "hidep"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
SER=$(sed -n 's/^SERIAL = \([0-9]*\)L;$/\1/p' $S/repo.idx)
|
||||
awk -v s=$((SER + 1)) '/^SERIAL/ { print "SERIAL = " s "L;"; next } /NAME = "liar"/ { liar = 1 } liar && /DEPS = / { sub(/DEPS = \(.*\);/, "DEPS = ();"); liar = 0 } { print }' $S/repo.idx > /tmp/idx && mv /tmp/idx $S/repo.idx
|
||||
gsign -S -s $SSEC -m $S/repo.idx >/dev/null
|
||||
R yes_to 'y\n' 'syspkg -f liar'
|
||||
check "a recipe that disagrees with its index entry" has "[x] extra-src::liar 1.0.0 does not match its index."
|
||||
check "...nothing installed" sh -c "! grep -q liar $IDX"
|
||||
chmod 666 /app/lib/build-driver.sh
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
chmod 644 /app/lib/build-driver.sh
|
||||
check "a world-writable build driver is refused" has "[x] Refusing /app/lib/build-driver.sh: /app/lib/build-driver.sh is not root-owned, or is group/world-writable."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
R yes_to 'y\n' 'syspkg -f needy'
|
||||
check "a build dependency no binary repo has" has "[x] extra-src::needy 1.0.0 needs nothere 1.0.0 or later to build, and no binary repository has it."
|
||||
check "...refused before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ];'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER unset: refused" has "[x] BUILD_USER is not set in /etc/glacier/glacier.conf."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_USER = "gbuild";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_ROOT unset: refused" has "[x] BUILD_ROOT is not set in /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "root";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER = root: refused" has "[x] BUILD_USER in /etc/glacier/glacier.conf must not be root."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "nosuchuser";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER not a user: refused" has "[x] BUILD_USER nosuchuser in /etc/glacier/glacier.conf is not a user."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; JOBS = 0;'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "JOBS = 0: glacier.conf is malformed" has "[x] Malformed /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
mkdir -p /mnt/t && syspkg -R /mnt/t -n >/dev/null
|
||||
cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/ 2>/dev/null || { mkdir -p /mnt/t/etc/glacier && cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/; }
|
||||
R yes_to '2\ny\n' 'syspkg -R /mnt/t -f both'
|
||||
check "building under -R is refused" has "[x] Building from source under -R is not supported."
|
||||
|
||||
hr "base-src and the machine key"
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "a base build without the machine key trusted for base is refused" has '[x] base-src::core 1.0.0 [-loud] (build) needs the key "local" trusted for base.'
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
check "...having made the machine key" has "[i] Made the machine key /etc/glacier/keys/local.pub."
|
||||
check "...its public half 0644" [ "$(stat -c '%u %a' /etc/glacier/keys/local.pub)" = "0 644" ]
|
||||
check "...its secret half root's 0600" [ "$(stat -c '%u %a' /glacier/sys/keys/local.sec)" = "0 600" ]
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "...made once" lacks "Made the machine key"
|
||||
rconf '"glacier-test", "local"'
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "listed for base: built and installed" grep -q "^base::core::1.0.0::explicit$" $IDX
|
||||
check "...linked into /usr, as base is" [ "$(/usr/bin/core)" = hello ]
|
||||
check "...marked as built from base-src" [ "$(cat /glacier/sys/store/base/core/core-1.0.0/.glacier-src)" = base-src ]
|
||||
check "...built for /usr" [ "$(cat /usr/share/core.prefix)" = /usr ]
|
||||
mv /glacier/sys/keys/local.sec /root/local.sec.bak
|
||||
printf 'TRAITS = [ "loud" ];\n' > /etc/glacier/config/core.conf
|
||||
R yes_to 'y\n' 'syspkg -u core'
|
||||
rm /etc/glacier/config/core.conf
|
||||
rconf '"glacier-test"'
|
||||
mv /root/local.sec.bak /glacier/sys/keys/local.sec
|
||||
check "a missing secret half is refused" has "[x] /etc/glacier/keys/local.pub and /glacier/sys/keys/local.sec must both exist, or neither."
|
||||
|
||||
hr "hardlinks in a build's output"
|
||||
mkrecipe hard 1.0.0 "$(mksrc hard 1.0.0)" '' 'package() {
|
||||
make install DESTDIR="$DESTDIR" PREFIX="$PREFIX"
|
||||
ln "$DESTDIR$PREFIX/bin/hard" "$DESTDIR$PREFIX/bin/hard2"
|
||||
}'
|
||||
publish
|
||||
R yes_to 'y\n' 'syspkg -f hard'
|
||||
check "a build whose install makes a hardlink packs it as a file" has "[✓] Completed with no errors."
|
||||
check "...both names installed and run" sh -c '[ "$(/glacier/sys/links/bin/hard)" = hello ] && [ "$(/glacier/sys/links/bin/hard2)" = hello ]'
|
||||
|
||||
hr "gpkg: the user's own build"
|
||||
U=/glacier/usr/1000
|
||||
su glacier -c 'doas /app/gstore -n' >/dev/null 2>&1
|
||||
check "gstore -n makes the user's config dir" [ "$(stat -c '%U %a' $U/config)" = "glacier 700" ]
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud" ];'
|
||||
R su glacier -c "printf '2\ny\n' | /app/gpkg -f both"
|
||||
check "gpkg builds it" has "[✓] Built extra-src::both 1.0.0."
|
||||
check "...the system's TRAITS apply" has "[i] Building extra-src::both 1.0.0 [+loud]..."
|
||||
check "...installed for the user" grep -q "^extra::both::1.0.0::explicit$" $U/index/current
|
||||
check "...the layers are the user's" [ "$(stat -c %U $U/build/layers/toolchain-1.0.0-*)" = glacier ]
|
||||
check "...the log too" [ "$(stat -c '%U %a' $U/build/logs/both-1.0.0.log)" = "glacier 600" ]
|
||||
check "...it runs" [ "$(su glacier -c "$U/links/bin/both")" = HELLO ]
|
||||
check "...kept in the user's base, 0600" [ "$(stat -c '%U %a' $U/build/packages/extra@both-1.0.0.gpkg)" = "glacier 600" ]
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f where"
|
||||
check "gpkg shows the build's output" has "==> Packing where-1.0.0.gpkg"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -fqO where"
|
||||
check "gpkg -q (in a bundle) hides it" lacks "==> "
|
||||
check "...and rebuilds" has "[✓] Built extra-src::where 1.0.0."
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg --quiet -fO where"
|
||||
check "gpkg --quiet too" sh -c '! echo "$0" | grep -q "==> " && echo "$0" | grep -q "Built extra-src::where"' "$OUT"
|
||||
R syspkg -h
|
||||
check "syspkg -h lists -q" has "{-q --quiet} Hide build output"
|
||||
R su glacier -c "/app/gpkg -h"
|
||||
check "gpkg -h lists -q" has "{-q --quiet} Hide build output"
|
||||
check "gpkg builds for the user's links" [ "$(su glacier -c "$U/links/bin/where")" = here ]
|
||||
check "...the path compiled in" grep -q "^cat $U/links/share/where/data$" $U/links/bin/where
|
||||
su glacier -c "printf 'TRAITS = [ \"-loud\" ];\n' > $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "the user's per-package file wins: rebuilt without loud" has " * extra-src::both 1.0.0 (traits) [-loud] (build)"
|
||||
check "...installed" [ "$(su glacier -c "$U/links/bin/both")" = hello ]
|
||||
su glacier -c "printf 'TRAITS = [ \"loud\" ];\n' > $U/config/glacier.conf; rm $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "...the user's glacier.conf too" has "[+loud] (build)"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f core"
|
||||
check "gpkg never builds from base-src" has "[x] core is not in any repository."
|
||||
|
||||
hr "gpkg: base packages as build layers"
|
||||
# Everest's BUILD_ROOT names base packages (musl, busybox): a user's
|
||||
# build stacks them from base's signed index, which gpkg never
|
||||
# installs from.
|
||||
B=/srv/base
|
||||
mkdir -p $B && chmod 755 $B
|
||||
/app/tests/mkroot -o $B -r build-base -r busybox -p busybox basetc 1.0.0 base >/dev/null
|
||||
grepo -s /root/glacier-test.sec -r base $B | grep -v Wrote
|
||||
sed -i 's|{ name = "base"; keys|{ name = "base"; url = "file:///srv/base"; keys|' $CONF
|
||||
gconf 'BUILD_ROOT = [ "basetc" ]; BUILD_USER = "gbuild";'
|
||||
rm -f $U/config/glacier.conf
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -fqO where"
|
||||
check "a user's build stacks a base package as a layer" has "[✓] Built extra-src::where 1.0.0."
|
||||
check "...extracted for the user" [ "$(stat -c %U $U/build/layers/basetc-1.0.0-*)" = glacier ]
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f basetc"
|
||||
check "...but gpkg still never installs from base" has "[x] basetc is not in any repository."
|
||||
rconf '"glacier-test"'
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud" ];'
|
||||
|
||||
su glacier -c "printf 'BUILD_USER = \"x\";\n' > $U/config/glacier.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "BUILD_USER in a user's glacier.conf is malformed" has "[x] Malformed $U/config/glacier.conf."
|
||||
|
||||
echo
|
||||
if [ $FAILS -eq 0 ]; then echo "ALL PASSED"; else echo "$FAILS FAILED"; fi
|
||||
[ $FAILS -eq 0 ]
|
||||
@@ -0,0 +1,325 @@
|
||||
#!/bin/sh
|
||||
# Source builds through syspkg/gpkg -f/-u (BUILD_DESIGN.txt; steps 3-4):
|
||||
# - a recipe from a NAME-src repo is built in gsandbox and installed
|
||||
# like a fetched package: summary and prompt BEFORE building, the
|
||||
# patches applied, the manifest the build was handed, .glacier-src;
|
||||
# - a name served by several repositories is asked about; Enter takes
|
||||
# the first, a bad answer refuses;
|
||||
# - TRAITS: recipe defaults, /etc/glacier/glacier.conf, per-package
|
||||
# files (an unknown trait refused), the user's own files for gpkg;
|
||||
# - -u stays on source, and rebuilds when the traits change;
|
||||
# - syspkg builds as BUILD_USER (required), gpkg as the user;
|
||||
# - refusals: a source with the wrong hash, a failing build, a build
|
||||
# that produces another package, a build dependency no binary repo
|
||||
# has, an untrusted per-package file, -R.
|
||||
# Needs what sandbox_alpine.sh needs, with /glacier off Docker's overlay:
|
||||
#
|
||||
# docker run --rm --security-opt seccomp=unconfined \
|
||||
# --security-opt apparmor=unconfined --security-opt systempaths=unconfined \
|
||||
# --tmpfs /glacier:exec,mode=755 \
|
||||
# -v "$PWD/tests/build_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
R() {
|
||||
OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed '/^rc=/d; s/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
hr() { echo; echo "== $*"; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
K=/etc/glacier/keys
|
||||
CONF=/etc/glacier/repos.conf
|
||||
GCONF=/etc/glacier/glacier.conf
|
||||
XSEC=/root/extrakey.sec
|
||||
SSEC=/root/srckey.sec
|
||||
E=/srv/extra
|
||||
S=/srv/extra-src
|
||||
SRC=/srv/sources
|
||||
IDX=/glacier/sys/index/current
|
||||
BUILD=/glacier/sys/build
|
||||
BS=/srv/base-src
|
||||
mkdir -p $E $S $SRC $BS && chmod 755 /srv $E $S $SRC $BS
|
||||
|
||||
gsign -G -p $K/extrakey.pub -s $XSEC -c extrakey >/dev/null
|
||||
gsign -G -p $K/srckey.pub -s $SSEC -c srckey >/dev/null
|
||||
rconf() { # BASE-KEYS
|
||||
printf 'repos = (\n%s\n%s\n%s\n%s\n);\n' \
|
||||
" { name = \"base\"; keys = [ $1 ]; }," \
|
||||
' { name = "extra"; url = "file:///srv/extra"; keys = [ "extrakey" ]; },' \
|
||||
' { name = "extra-src"; url = "file:///srv/extra-src"; keys = [ "srckey" ]; },' \
|
||||
' { name = "base-src"; url = "file:///srv/base-src"; keys = [ "srckey" ]; }' > $CONF
|
||||
chmod 644 $CONF
|
||||
}
|
||||
rconf '"glacier-test"'
|
||||
adduser -S -D -H -s /sbin/nologin gbuild
|
||||
gconf() { printf 'KEEP_OLD = 1;\n%s\n' "$1" > $GCONF; chmod 644 $GCONF; }
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
|
||||
# The binary repo: a toolchain (Alpine's build-base, busybox for /bin/sh)
|
||||
# and a runtime dependency.
|
||||
/app/tests/mkroot -o $E -r build-base -r busybox -p busybox toolchain 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E hidep 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E loudlib 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E both 9.0.0 extra >/dev/null
|
||||
|
||||
# A source tarball: hello prints "hello", "HELLO" with LOUD=1.
|
||||
mksrc() { # NAME VER [MAKEFILE-EXTRA]
|
||||
d=/tmp/src/$1-$2
|
||||
rm -rf $d && mkdir -p $d
|
||||
printf '#include <stdio.h>\nint main(void) {\n#ifdef LOUD\n\tputs("HELLO");\n#else\n\tputs("hello");\n#endif\n\treturn 0;\n}\n' > $d/hello.c
|
||||
printf 'CFLAGS += $(if $(LOUD),-DLOUD)\n%s: hello.c\n\t$(CC) $(CFLAGS) -o %s hello.c\ninstall:\n\tinstall -D -m 755 %s $(DESTDIR)$(PREFIX)/bin/%s\n%s\n' \
|
||||
"$1" "$1" "$1" "$1" "${3:-}" > $d/Makefile
|
||||
tar czf $SRC/$1-$2.tar.gz -C /tmp/src $1-$2
|
||||
sha256sum $SRC/$1-$2.tar.gz | cut -c1-64
|
||||
}
|
||||
# A recipe bundle in the -src repo: NAME VER SHA [EXTRA-FIELDS] [BUILD-FILE]
|
||||
mkrecipe() {
|
||||
t=/tmp/rcp/$1-$2
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "%s"; VER = "%s"; REPO = "extra"; DESC = "test \\"%s\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/%s-%s.tar.gz"; SHA256 = "sha256:%s"; } );\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\n%s\n' \
|
||||
"$1" "$2" "$1" "$SRC" "$1" "$2" "$3" "${4:-}" > $t/recipe
|
||||
[ -n "${5:-}" ] && printf '%s\n' "$5" > $t/build
|
||||
rmdir $t/patches 2>/dev/null
|
||||
tar czf $S/$1-$2.grcp -C /tmp/rcp $1-$2
|
||||
}
|
||||
publish() {
|
||||
grepo -s $XSEC -r extra $E | grep -v Wrote; grepo -s $SSEC -r extra-src $S | grep -v Wrote
|
||||
grepo -s $SSEC -r base-src $BS | grep -v Wrote
|
||||
}
|
||||
|
||||
# hello 1.0.0, patched to greet glacier, depending on hidep.
|
||||
H=$(mksrc hello 1.0.0)
|
||||
t=/tmp/rcp/hello-1.0.0
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "hello"; VER = "1.0.0"; REPO = "extra"; DESC = "say \\"hi\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/hello-1.0.0.tar.gz"; SHA256 = "sha256:%s"; } );\nPATCHES = [ "01-glacier.patch" ];\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\nDEPS = ( { NAME = "hidep"; VER = "1.0.0"; }, { NAME = "loudlib"; VER = "1.0.0"; IF = "loud"; } );\n' "$SRC" "$H" > $t/recipe
|
||||
cat > $t/patches/01-glacier.patch <<'EOF'
|
||||
--- a/hello.c
|
||||
+++ b/hello.c
|
||||
@@ -5,3 +5,3 @@
|
||||
#else
|
||||
- puts("hello");
|
||||
+ puts("hello, glacier");
|
||||
#endif
|
||||
EOF
|
||||
tar czf $S/hello-1.0.0.grcp -C /tmp/rcp hello-1.0.0
|
||||
mkrecipe both 1.0.0 "$(mksrc both 1.0.0)"
|
||||
mkrecipe badhash 1.0.0 "$(printf '%064d' 0)"
|
||||
mksrc badhash 1.0.0 >/dev/null
|
||||
mkrecipe broken 1.0.0 "$(mksrc broken 1.0.0 'broken: ; false')" '' 'compile() { make broken; }'
|
||||
mkrecipe evil 1.0.0 "$(mksrc evil 1.0.0)" '' 'package() {
|
||||
mkdir -p /tmp/p/evil-1.0.0/files/usr/bin
|
||||
echo x > /tmp/p/evil-1.0.0/files/usr/bin/evil
|
||||
printf "PKG_NAME = \"evil\"; PKG_VER = \"1.0.0\"; PKG_REPO = \"base\"; PKG_DEPS = (); PKG_CONFS = ();\n" > /tmp/p/evil-1.0.0/manifest.gpm.cfg
|
||||
tar czf /.glacier/out/evil-1.0.0.gpkg -C /tmp/p evil-1.0.0
|
||||
exit 0
|
||||
}'
|
||||
mkrecipe core 1.0.0 "$(mksrc core 1.0.0)"
|
||||
sed -i 's/REPO = "extra"/REPO = "base"/' /tmp/rcp/core-1.0.0/recipe
|
||||
tar czf $BS/core-1.0.0.grcp -C /tmp/rcp core-1.0.0 && rm $S/core-1.0.0.grcp
|
||||
mkrecipe needy 1.0.0 "$(mksrc needy 1.0.0)" 'BUILD_DEPS = ( { NAME = "nothere"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
|
||||
syspkg -n >/dev/null
|
||||
yes_to() { sh -c "printf '$1' | $2"; }
|
||||
|
||||
hr "syspkg -f of a recipe"
|
||||
R yes_to 'y\n' 'syspkg -f hello'
|
||||
check "built and installed" has "[✓] Completed with no errors."
|
||||
check "...exit 0" rc 0
|
||||
check "the summary marks the build, its traits off by default" has " * extra-src::hello 1.0.0 [-loud] (build)"
|
||||
check "...and the binary dependency" has " * extra::hidep 1.0.0 (dependency)"
|
||||
check "...the prompt came before the build" sh -c 'echo "$0" | grep -n "Proceed\|Building extra-src" | head -1 | grep -q Proceed' "$OUT"
|
||||
check "the build is announced" has "[i] Building extra-src::hello 1.0.0 [-loud]..."
|
||||
check "...and done" has "[✓] Built extra-src::hello 1.0.0."
|
||||
check "installed as extra::hello" grep -q "^extra::hello::1.0.0::explicit$" $IDX
|
||||
check "...with its dependency, auto" grep -q "^extra::hidep::1.0.0::auto$" $IDX
|
||||
check "...not the one only loud needs" sh -c "! grep -q loudlib $IDX"
|
||||
check "...the patch applied" [ "$(/glacier/sys/links/bin/hello)" = "hello, glacier" ]
|
||||
V=/glacier/sys/store/extra/hello/hello-1.0.0
|
||||
check "...recorded as built from extra-src" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...its manifest has PKG_TRAITS" grep -qF 'PKG_TRAITS = [ "-loud" ];' $V/.glacier-manifest
|
||||
check "...and the recipe's DESC, quoted" grep -qF 'PKG_DESC = "say \"hi\"";' $V/.glacier-manifest
|
||||
check "the layer belongs to BUILD_USER" [ "$(stat -c %U $BUILD/layers/toolchain-1.0.0-*)" = gbuild ]
|
||||
check "...which owns layers/, 0700" [ "$(stat -c '%U %a' $BUILD/layers)" = "gbuild 700" ]
|
||||
check "the build's in/work/out dirs are gone" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
check "the log is kept" grep -q "==> Done" $BUILD/logs/hello-1.0.0.log
|
||||
check "...the sources cached by hash" [ -f $BUILD/sources/$H ]
|
||||
check "the cached bundle is deleted" [ ! -e /glacier/sys/cache/extra-src/hello-1.0.0.grcp ]
|
||||
check "the store copy is root's, as any install" [ "$(stat -c %u /glacier/sys/store/extra/hello/hello-1.0.0/usr/bin/hello)" = 0 ]
|
||||
|
||||
hr "declining builds nothing"
|
||||
rm $BUILD/logs/hello-1.0.0.log
|
||||
R yes_to 'n\n' 'syspkg -f -O hello'
|
||||
check "nothing built" sh -c "[ ! -e $BUILD/logs/hello-1.0.0.log ]"
|
||||
check "...nothing said about building" lacks "Building"
|
||||
|
||||
hr "traits"
|
||||
mkdir -p /etc/glacier/config
|
||||
printf 'TRAITS = [ "lod" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "an unknown trait in a per-package file is refused" has "[x] /etc/glacier/config/hello.conf: hello has no trait lod."
|
||||
check "...exit 1, before the prompt" sh -c "[ $RC = 1 ]" && check "...no prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud", "x11" ];'
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "the per-package file beats glacier.conf: nothing to rebuild" has "Nothing to do."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "glacier.conf turns loud on (x11, unknown to the recipe, ignored): a rebuild" has " * extra-src::hello 1.0.0 (traits) [+loud] (build)"
|
||||
check "...installed" rc 0
|
||||
check "...built with LOUD" [ "$(/glacier/sys/links/bin/hello)" = "HELLO" ]
|
||||
check "...with the dependency loud needs" grep -q "^extra::loudlib::1.0.0::auto$" $IDX
|
||||
check "...in its manifest" grep -qF '{ PKG_NAME = "loudlib"; PKG_VER = "1.0.0"; }' $V/.glacier-manifest
|
||||
check "...still marked as built from source" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...PKG_TRAITS updated" grep -qF 'PKG_TRAITS = [ "loud" ];' $V/.glacier-manifest
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "unchanged traits: nothing to do" has "Nothing to do."
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
chmod 666 /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a world-writable per-package file is refused" has "[x] Refusing /etc/glacier/config/hello.conf: /etc/glacier/config/hello.conf is not root-owned, or is group/world-writable."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
printf 'TRAITS = [ "loud" ]; COLOR = "red";\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a per-package file with an unknown key is malformed" has "[x] Malformed /etc/glacier/config/hello.conf."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
|
||||
hr "-u stays on source"
|
||||
H11=$(mksrc hello 1.1.0)
|
||||
sed "s/1\.0\.0/1.1.0/g; s/$H/$H11/" /tmp/rcp/hello-1.0.0/recipe > /tmp/r.new
|
||||
rm -rf /tmp/rcp/hello-1.1.0 && cp -a /tmp/rcp/hello-1.0.0 /tmp/rcp/hello-1.1.0 && mv /tmp/r.new /tmp/rcp/hello-1.1.0/recipe
|
||||
sed -i 's/VER = "1.1.0"; }/VER = "1.0.0"; }/g; s/VER = "1.1.0"; IF/VER = "1.0.0"; IF/' /tmp/rcp/hello-1.1.0/recipe
|
||||
tar czf $S/hello-1.1.0.grcp -C /tmp/rcp hello-1.1.0
|
||||
/app/tests/mkpkg -o $E hello 2.0.0 extra >/dev/null
|
||||
publish
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "a newer recipe is built, not extra's binary 2.0.0" has " * extra-src::hello 1.0.0 -> 1.1.0 [+loud] (build)"
|
||||
check "...installed" grep -q "^extra::hello::1.1.0::explicit$" $IDX
|
||||
check "...the old version backed up" [ -f /glacier/sys/backup/extra/hello/hello-1.0.0.gpkg ]
|
||||
check "...still on source" [ "$(cat /glacier/sys/store/extra/hello/hello-1.1.0/.glacier-src)" = extra-src ]
|
||||
|
||||
hr "the repository question"
|
||||
R yes_to '\ny\n' 'syspkg -f both'
|
||||
check "both repos serving a name: asked" has "[i] both is available from more than one repository:"
|
||||
check "...the binary first (repos.conf order)" has " 1) extra::both 9.0.0"
|
||||
check "...then the recipe" has " 2) extra-src::both 1.0.0 [+loud] (build)"
|
||||
check "...Enter takes the first" grep -q "^extra::both::9.0.0::explicit$" $IDX
|
||||
check "...not marked as built" [ ! -e /glacier/sys/store/extra/both/both-9.0.0/.glacier-src ]
|
||||
R yes_to '2\ny\n' 'syspkg -f -O both'
|
||||
check "2 builds the recipe (replacing the binary under -O)" grep -q "^extra::both::1.0.0::explicit$" $IDX
|
||||
check "...marked as built" [ "$(cat /glacier/sys/store/extra/both/both-1.0.0/.glacier-src)" = extra-src ]
|
||||
R yes_to '3\n' 'syspkg -f -O both'
|
||||
check "an answer out of range is refused" has "[x] Invalid selection."
|
||||
check "...exit 1" rc 1
|
||||
R yes_to 'x\n' 'syspkg -f -O both'
|
||||
check "a non-number is refused" has "[x] Invalid selection."
|
||||
R sh -c 'syspkg -f -O both < /dev/null'
|
||||
check "end of input is refused" has "[x] Invalid selection."
|
||||
|
||||
hr "refused builds"
|
||||
R yes_to 'y\n' 'syspkg -f badhash'
|
||||
check "a source with the wrong hash" has "[x] A source of extra-src::badhash 1.0.0 does not match its hash."
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q badhash $IDX"
|
||||
R yes_to 'y\n' 'syspkg -f broken'
|
||||
check "a failing build" has "[x] Building extra-src::broken 1.0.0 failed. Log: $BUILD/logs/broken-1.0.0.log"
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q broken $IDX"
|
||||
check "...the log says why" grep -q "Error\|error" $BUILD/logs/broken-1.0.0.log
|
||||
check "...its dirs removed" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
R yes_to 'y\n' 'syspkg -f evil'
|
||||
check "a build that produces another package" has "[x] extra-src::evil 1.0.0: the build produced another package."
|
||||
check "...nothing installed" sh -c "! grep -q evil $IDX && [ ! -e /usr/bin/evil ]"
|
||||
# An index entry that disagrees with its bundle's recipe (signed all
|
||||
# the same): the recipe has a dependency the index left out.
|
||||
mkrecipe liar 1.0.0 "$(mksrc liar 1.0.0)" 'DEPS = ( { NAME = "hidep"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
SER=$(sed -n 's/^SERIAL = \([0-9]*\)L;$/\1/p' $S/repo.idx)
|
||||
awk -v s=$((SER + 1)) '/^SERIAL/ { print "SERIAL = " s "L;"; next } /NAME = "liar"/ { liar = 1 } liar && /DEPS = / { sub(/DEPS = \(.*\);/, "DEPS = ();"); liar = 0 } { print }' $S/repo.idx > /tmp/idx && mv /tmp/idx $S/repo.idx
|
||||
gsign -S -s $SSEC -m $S/repo.idx >/dev/null
|
||||
R yes_to 'y\n' 'syspkg -f liar'
|
||||
check "a recipe that disagrees with its index entry" has "[x] extra-src::liar 1.0.0 does not match its index."
|
||||
check "...nothing installed" sh -c "! grep -q liar $IDX"
|
||||
chmod 666 /app/lib/build-driver.sh
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
chmod 644 /app/lib/build-driver.sh
|
||||
check "a world-writable build driver is refused" has "[x] Refusing /app/lib/build-driver.sh: /app/lib/build-driver.sh is not root-owned, or is group/world-writable."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
R yes_to 'y\n' 'syspkg -f needy'
|
||||
check "a build dependency no binary repo has" has "[x] extra-src::needy 1.0.0 needs nothere 1.0.0 or later to build, and no binary repository has it."
|
||||
check "...refused before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ];'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER unset: refused" has "[x] BUILD_USER is not set in /etc/glacier/glacier.conf."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_USER = "gbuild";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_ROOT unset: refused" has "[x] BUILD_ROOT is not set in /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "root";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER = root: refused" has "[x] BUILD_USER in /etc/glacier/glacier.conf must not be root."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "nosuchuser";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER not a user: refused" has "[x] BUILD_USER nosuchuser in /etc/glacier/glacier.conf is not a user."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; JOBS = 0;'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "JOBS = 0: glacier.conf is malformed" has "[x] Malformed /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
mkdir -p /mnt/t && syspkg -R /mnt/t -n >/dev/null
|
||||
cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/ 2>/dev/null || { mkdir -p /mnt/t/etc/glacier && cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/; }
|
||||
R yes_to '2\ny\n' 'syspkg -R /mnt/t -f both'
|
||||
check "building under -R is refused" has "[x] Building from source under -R is not supported."
|
||||
|
||||
hr "base-src and the machine key"
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "a base build without the machine key trusted for base is refused" has '[x] base-src::core 1.0.0 [-loud] (build) needs the key "local" trusted for base.'
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
check "...having made the machine key" has "[i] Made the machine key /etc/glacier/keys/local.pub."
|
||||
check "...its public half 0644" [ "$(stat -c '%u %a' /etc/glacier/keys/local.pub)" = "0 644" ]
|
||||
check "...its secret half root's 0600" [ "$(stat -c '%u %a' /glacier/sys/keys/local.sec)" = "0 600" ]
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "...made once" lacks "Made the machine key"
|
||||
rconf '"glacier-test", "local"'
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "listed for base: built and installed" grep -q "^base::core::1.0.0::explicit$" $IDX
|
||||
check "...linked into /usr, as base is" [ "$(/usr/bin/core)" = hello ]
|
||||
check "...marked as built from base-src" [ "$(cat /glacier/sys/store/base/core/core-1.0.0/.glacier-src)" = base-src ]
|
||||
mv /glacier/sys/keys/local.sec /root/local.sec.bak
|
||||
printf 'TRAITS = [ "loud" ];\n' > /etc/glacier/config/core.conf
|
||||
R yes_to 'y\n' 'syspkg -u core'
|
||||
rm /etc/glacier/config/core.conf
|
||||
rconf '"glacier-test"'
|
||||
mv /root/local.sec.bak /glacier/sys/keys/local.sec
|
||||
check "a missing secret half is refused" has "[x] /etc/glacier/keys/local.pub and /glacier/sys/keys/local.sec must both exist, or neither."
|
||||
|
||||
hr "gpkg: the user's own build"
|
||||
U=/glacier/usr/1000
|
||||
su glacier -c 'doas /app/gstore -n' >/dev/null 2>&1
|
||||
check "gstore -n makes the user's config dir" [ "$(stat -c '%U %a' $U/config)" = "glacier 700" ]
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud" ];'
|
||||
R su glacier -c "printf '2\ny\n' | /app/gpkg -f both"
|
||||
check "gpkg builds it" has "[✓] Built extra-src::both 1.0.0."
|
||||
check "...the system's TRAITS apply" has "[i] Building extra-src::both 1.0.0 [+loud]..."
|
||||
check "...installed for the user" grep -q "^extra::both::1.0.0::explicit$" $U/index/current
|
||||
check "...the layers are the user's" [ "$(stat -c %U $U/build/layers/toolchain-1.0.0-*)" = glacier ]
|
||||
check "...the log too" [ "$(stat -c '%U %a' $U/build/logs/both-1.0.0.log)" = "glacier 600" ]
|
||||
check "...it runs" [ "$(su glacier -c "$U/links/bin/both")" = HELLO ]
|
||||
su glacier -c "printf 'TRAITS = [ \"-loud\" ];\n' > $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "the user's per-package file wins: rebuilt without loud" has " * extra-src::both 1.0.0 (traits) [-loud] (build)"
|
||||
check "...installed" [ "$(su glacier -c "$U/links/bin/both")" = hello ]
|
||||
su glacier -c "printf 'TRAITS = [ \"loud\" ];\n' > $U/config/glacier.conf; rm $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "...the user's glacier.conf too" has "[+loud] (build)"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f core"
|
||||
check "gpkg never builds from base-src" has "[x] core is not in any repository."
|
||||
su glacier -c "printf 'BUILD_USER = \"x\";\n' > $U/config/glacier.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "BUILD_USER in a user's glacier.conf is malformed" has "[x] Malformed $U/config/glacier.conf."
|
||||
|
||||
echo
|
||||
if [ $FAILS -eq 0 ]; then echo "ALL PASSED"; else echo "$FAILS FAILED"; fi
|
||||
[ $FAILS -eq 0 ]
|
||||
@@ -0,0 +1,359 @@
|
||||
#!/bin/sh
|
||||
# Source builds through syspkg/gpkg -f/-u (BUILD_DESIGN.txt; steps 3-4):
|
||||
# - a recipe from a NAME-src repo is built in gsandbox and installed
|
||||
# like a fetched package: summary and prompt BEFORE building, the
|
||||
# patches applied, the manifest the build was handed, .glacier-src;
|
||||
# - a name served by several repositories is asked about; Enter takes
|
||||
# the first, a bad answer refuses;
|
||||
# - TRAITS: recipe defaults, /etc/glacier/glacier.conf, per-package
|
||||
# files (an unknown trait refused), the user's own files for gpkg;
|
||||
# - -u stays on source, and rebuilds when the traits change;
|
||||
# - syspkg builds as BUILD_USER (required), gpkg as the user;
|
||||
# - PREFIX: a package is built for where its usr/ is linked
|
||||
# (/glacier/sys/links, the user's links, /usr for base) and still
|
||||
# packed as usr/; during the build PREFIX resolves to the layers;
|
||||
# - refusals: a source with the wrong hash, a failing build, a build
|
||||
# that produces another package, a build dependency no binary repo
|
||||
# has, an untrusted per-package file, -R.
|
||||
# Needs what sandbox_alpine.sh needs, with /glacier off Docker's overlay:
|
||||
#
|
||||
# docker run --rm --security-opt seccomp=unconfined \
|
||||
# --security-opt apparmor=unconfined --security-opt systempaths=unconfined \
|
||||
# --tmpfs /glacier:exec,mode=755 \
|
||||
# -v "$PWD/tests/build_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
R() {
|
||||
OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed '/^rc=/d; s/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
hr() { echo; echo "== $*"; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
K=/etc/glacier/keys
|
||||
CONF=/etc/glacier/repos.conf
|
||||
GCONF=/etc/glacier/glacier.conf
|
||||
XSEC=/root/extrakey.sec
|
||||
SSEC=/root/srckey.sec
|
||||
E=/srv/extra
|
||||
S=/srv/extra-src
|
||||
SRC=/srv/sources
|
||||
IDX=/glacier/sys/index/current
|
||||
BUILD=/glacier/sys/build
|
||||
BS=/srv/base-src
|
||||
mkdir -p $E $S $SRC $BS && chmod 755 /srv $E $S $SRC $BS
|
||||
|
||||
gsign -G -p $K/extrakey.pub -s $XSEC -c extrakey >/dev/null
|
||||
gsign -G -p $K/srckey.pub -s $SSEC -c srckey >/dev/null
|
||||
rconf() { # BASE-KEYS
|
||||
printf 'repos = (\n%s\n%s\n%s\n%s\n);\n' \
|
||||
" { name = \"base\"; keys = [ $1 ]; }," \
|
||||
' { name = "extra"; url = "file:///srv/extra"; keys = [ "extrakey" ]; },' \
|
||||
' { name = "extra-src"; url = "file:///srv/extra-src"; keys = [ "srckey" ]; },' \
|
||||
' { name = "base-src"; url = "file:///srv/base-src"; keys = [ "srckey" ]; }' > $CONF
|
||||
chmod 644 $CONF
|
||||
}
|
||||
rconf '"glacier-test"'
|
||||
adduser -S -D -H -s /sbin/nologin gbuild
|
||||
gconf() { printf 'KEEP_OLD = 1;\n%s\n' "$1" > $GCONF; chmod 644 $GCONF; }
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
|
||||
# The binary repo: a toolchain (Alpine's build-base, busybox for /bin/sh)
|
||||
# and a runtime dependency.
|
||||
/app/tests/mkroot -o $E -r build-base -r busybox -p busybox toolchain 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E hidep 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E loudlib 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E both 9.0.0 extra >/dev/null
|
||||
|
||||
# A source tarball: hello prints "hello", "HELLO" with LOUD=1.
|
||||
mksrc() { # NAME VER [MAKEFILE-EXTRA]
|
||||
d=/tmp/src/$1-$2
|
||||
rm -rf $d && mkdir -p $d
|
||||
printf '#include <stdio.h>\nint main(void) {\n#ifdef LOUD\n\tputs("HELLO");\n#else\n\tputs("hello");\n#endif\n\treturn 0;\n}\n' > $d/hello.c
|
||||
printf 'CFLAGS += $(if $(LOUD),-DLOUD)\n%s: hello.c\n\t$(CC) $(CFLAGS) -o %s hello.c\ninstall:\n\tinstall -D -m 755 %s $(DESTDIR)$(PREFIX)/bin/%s\n%s\n' \
|
||||
"$1" "$1" "$1" "$1" "${3:-}" > $d/Makefile
|
||||
tar czf $SRC/$1-$2.tar.gz -C /tmp/src $1-$2
|
||||
sha256sum $SRC/$1-$2.tar.gz | cut -c1-64
|
||||
}
|
||||
# A recipe bundle in the -src repo: NAME VER SHA [EXTRA-FIELDS] [BUILD-FILE]
|
||||
mkrecipe() {
|
||||
t=/tmp/rcp/$1-$2
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "%s"; VER = "%s"; REPO = "extra"; DESC = "test \\"%s\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/%s-%s.tar.gz"; SHA256 = "sha256:%s"; } );\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\n%s\n' \
|
||||
"$1" "$2" "$1" "$SRC" "$1" "$2" "$3" "${4:-}" > $t/recipe
|
||||
[ -n "${5:-}" ] && printf '%s\n' "$5" > $t/build
|
||||
rmdir $t/patches 2>/dev/null
|
||||
tar czf $S/$1-$2.grcp -C /tmp/rcp $1-$2
|
||||
}
|
||||
publish() {
|
||||
grepo -s $XSEC -r extra $E | grep -v Wrote; grepo -s $SSEC -r extra-src $S | grep -v Wrote
|
||||
grepo -s $SSEC -r base-src $BS | grep -v Wrote
|
||||
}
|
||||
|
||||
# hello 1.0.0, patched to greet glacier, depending on hidep.
|
||||
H=$(mksrc hello 1.0.0)
|
||||
t=/tmp/rcp/hello-1.0.0
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "hello"; VER = "1.0.0"; REPO = "extra"; DESC = "say \\"hi\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/hello-1.0.0.tar.gz"; SHA256 = "sha256:%s"; } );\nPATCHES = [ "01-glacier.patch" ];\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\nDEPS = ( { NAME = "hidep"; VER = "1.0.0"; }, { NAME = "loudlib"; VER = "1.0.0"; IF = "loud"; } );\n' "$SRC" "$H" > $t/recipe
|
||||
cat > $t/patches/01-glacier.patch <<'EOF'
|
||||
--- a/hello.c
|
||||
+++ b/hello.c
|
||||
@@ -5,3 +5,3 @@
|
||||
#else
|
||||
- puts("hello");
|
||||
+ puts("hello, glacier");
|
||||
#endif
|
||||
EOF
|
||||
tar czf $S/hello-1.0.0.grcp -C /tmp/rcp hello-1.0.0
|
||||
mkrecipe both 1.0.0 "$(mksrc both 1.0.0)"
|
||||
mkrecipe badhash 1.0.0 "$(printf '%064d' 0)"
|
||||
mksrc badhash 1.0.0 >/dev/null
|
||||
mkrecipe broken 1.0.0 "$(mksrc broken 1.0.0 'broken: ; false')" '' 'compile() { make broken; }'
|
||||
mkrecipe evil 1.0.0 "$(mksrc evil 1.0.0)" '' 'package() {
|
||||
mkdir -p /tmp/p/evil-1.0.0/files/usr/bin
|
||||
echo x > /tmp/p/evil-1.0.0/files/usr/bin/evil
|
||||
printf "PKG_NAME = \"evil\"; PKG_VER = \"1.0.0\"; PKG_REPO = \"base\"; PKG_DEPS = (); PKG_CONFS = ();\n" > /tmp/p/evil-1.0.0/manifest.gpm.cfg
|
||||
tar czf /.glacier/out/evil-1.0.0.gpkg -C /tmp/p evil-1.0.0
|
||||
exit 0
|
||||
}'
|
||||
mkrecipe core 1.0.0 "$(mksrc core 1.0.0)" '' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR$PREFIX/share" && echo "$PREFIX" > "$DESTDIR$PREFIX/share/core.prefix"
|
||||
}'
|
||||
sed -i 's/REPO = "extra"/REPO = "base"/' /tmp/rcp/core-1.0.0/recipe
|
||||
tar czf $BS/core-1.0.0.grcp -C /tmp/rcp core-1.0.0 && rm $S/core-1.0.0.grcp
|
||||
# where: a script with its data file's path compiled in. Its prepare
|
||||
# fails unless PREFIX leads to the layers during the build.
|
||||
mkrecipe where 1.0.0 "$(mksrc where 1.0.0)" 'BUILD = "hooks";' 'prepare() { [ -x "$PREFIX/bin/make" ]; }
|
||||
package() {
|
||||
mkdir -p "$DESTDIR$PREFIX/bin" "$DESTDIR$PREFIX/share/where"
|
||||
echo here > "$DESTDIR$PREFIX/share/where/data"
|
||||
printf "#!/bin/sh\ncat %s/share/where/data\n" "$PREFIX" > "$DESTDIR$PREFIX/bin/where"
|
||||
chmod 755 "$DESTDIR$PREFIX/bin/where"
|
||||
}'
|
||||
sed -i 's/^BUILD = "make";$//' /tmp/rcp/where-1.0.0/recipe && tar czf $S/where-1.0.0.grcp -C /tmp/rcp where-1.0.0
|
||||
mkrecipe split 1.0.0 "$(mksrc split 1.0.0)" '' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR/usr/share" && echo x > "$DESTDIR/usr/share/split"
|
||||
}'
|
||||
mkrecipe needy 1.0.0 "$(mksrc needy 1.0.0)" 'BUILD_DEPS = ( { NAME = "nothere"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
|
||||
syspkg -n >/dev/null
|
||||
yes_to() { sh -c "printf '$1' | $2"; }
|
||||
|
||||
hr "syspkg -f of a recipe"
|
||||
R yes_to 'y\n' 'syspkg -f hello'
|
||||
check "built and installed" has "[✓] Completed with no errors."
|
||||
check "...exit 0" rc 0
|
||||
check "the summary marks the build, its traits off by default" has " * extra-src::hello 1.0.0 [-loud] (build)"
|
||||
check "...and the binary dependency" has " * extra::hidep 1.0.0 (dependency)"
|
||||
check "...the prompt came before the build" sh -c 'echo "$0" | grep -n "Proceed\|Building extra-src" | head -1 | grep -q Proceed' "$OUT"
|
||||
check "the build is announced" has "[i] Building extra-src::hello 1.0.0 [-loud]..."
|
||||
check "...and done" has "[✓] Built extra-src::hello 1.0.0."
|
||||
check "installed as extra::hello" grep -q "^extra::hello::1.0.0::explicit$" $IDX
|
||||
check "...with its dependency, auto" grep -q "^extra::hidep::1.0.0::auto$" $IDX
|
||||
check "...not the one only loud needs" sh -c "! grep -q loudlib $IDX"
|
||||
check "...the patch applied" [ "$(/glacier/sys/links/bin/hello)" = "hello, glacier" ]
|
||||
V=/glacier/sys/store/extra/hello/hello-1.0.0
|
||||
check "...recorded as built from extra-src" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...its manifest has PKG_TRAITS" grep -qF 'PKG_TRAITS = [ "-loud" ];' $V/.glacier-manifest
|
||||
check "...and the recipe's DESC, quoted" grep -qF 'PKG_DESC = "say \"hi\"";' $V/.glacier-manifest
|
||||
check "the layer belongs to BUILD_USER" [ "$(stat -c %U $BUILD/layers/toolchain-1.0.0-*)" = gbuild ]
|
||||
check "...which owns layers/, 0700" [ "$(stat -c '%U %a' $BUILD/layers)" = "gbuild 700" ]
|
||||
check "the build's in/work/out dirs are gone" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
check "the log is kept" grep -q "==> Done" $BUILD/logs/hello-1.0.0.log
|
||||
check "...the sources cached by hash" [ -f $BUILD/sources/$H ]
|
||||
check "the cached bundle is deleted" [ ! -e /glacier/sys/cache/extra-src/hello-1.0.0.grcp ]
|
||||
check "the store copy is root's, as any install" [ "$(stat -c %u /glacier/sys/store/extra/hello/hello-1.0.0/usr/bin/hello)" = 0 ]
|
||||
|
||||
hr "declining builds nothing"
|
||||
rm $BUILD/logs/hello-1.0.0.log
|
||||
R yes_to 'n\n' 'syspkg -f -O hello'
|
||||
check "nothing built" sh -c "[ ! -e $BUILD/logs/hello-1.0.0.log ]"
|
||||
check "...nothing said about building" lacks "Building"
|
||||
|
||||
hr "traits"
|
||||
mkdir -p /etc/glacier/config
|
||||
printf 'TRAITS = [ "lod" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "an unknown trait in a per-package file is refused" has "[x] /etc/glacier/config/hello.conf: hello has no trait lod."
|
||||
check "...exit 1, before the prompt" sh -c "[ $RC = 1 ]" && check "...no prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud", "x11" ];'
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "the per-package file beats glacier.conf: nothing to rebuild" has "Nothing to do."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "glacier.conf turns loud on (x11, unknown to the recipe, ignored): a rebuild" has " * extra-src::hello 1.0.0 (traits) [+loud] (build)"
|
||||
check "...installed" rc 0
|
||||
check "...built with LOUD" [ "$(/glacier/sys/links/bin/hello)" = "HELLO" ]
|
||||
check "...with the dependency loud needs" grep -q "^extra::loudlib::1.0.0::auto$" $IDX
|
||||
check "...in its manifest" grep -qF '{ PKG_NAME = "loudlib"; PKG_VER = "1.0.0"; }' $V/.glacier-manifest
|
||||
check "...still marked as built from source" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...PKG_TRAITS updated" grep -qF 'PKG_TRAITS = [ "loud" ];' $V/.glacier-manifest
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "unchanged traits: nothing to do" has "Nothing to do."
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
chmod 666 /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a world-writable per-package file is refused" has "[x] Refusing /etc/glacier/config/hello.conf: /etc/glacier/config/hello.conf is not root-owned, or is group/world-writable."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
printf 'TRAITS = [ "loud" ]; COLOR = "red";\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a per-package file with an unknown key is malformed" has "[x] Malformed /etc/glacier/config/hello.conf."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
|
||||
hr "-u stays on source"
|
||||
H11=$(mksrc hello 1.1.0)
|
||||
sed "s/1\.0\.0/1.1.0/g; s/$H/$H11/" /tmp/rcp/hello-1.0.0/recipe > /tmp/r.new
|
||||
rm -rf /tmp/rcp/hello-1.1.0 && cp -a /tmp/rcp/hello-1.0.0 /tmp/rcp/hello-1.1.0 && mv /tmp/r.new /tmp/rcp/hello-1.1.0/recipe
|
||||
sed -i 's/VER = "1.1.0"; }/VER = "1.0.0"; }/g; s/VER = "1.1.0"; IF/VER = "1.0.0"; IF/' /tmp/rcp/hello-1.1.0/recipe
|
||||
tar czf $S/hello-1.1.0.grcp -C /tmp/rcp hello-1.1.0
|
||||
/app/tests/mkpkg -o $E hello 2.0.0 extra >/dev/null
|
||||
publish
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "a newer recipe is built, not extra's binary 2.0.0" has " * extra-src::hello 1.0.0 -> 1.1.0 [+loud] (build)"
|
||||
check "...installed" grep -q "^extra::hello::1.1.0::explicit$" $IDX
|
||||
check "...the old version backed up" [ -f /glacier/sys/backup/extra/hello/hello-1.0.0.gpkg ]
|
||||
check "...still on source" [ "$(cat /glacier/sys/store/extra/hello/hello-1.1.0/.glacier-src)" = extra-src ]
|
||||
|
||||
hr "PREFIX"
|
||||
R yes_to 'y\n' 'syspkg -f where'
|
||||
check "built for /glacier/sys/links: finds its data through the links" [ "$(/glacier/sys/links/bin/where)" = here ]
|
||||
check "...the path compiled in" grep -q "^cat /glacier/sys/links/share/where/data$" /glacier/sys/links/bin/where
|
||||
check "...packed as usr/, as any package" [ -f /glacier/sys/store/extra/where/where-1.0.0/usr/share/where/data ]
|
||||
check "...nothing else at the top" [ "$(ls /glacier/sys/store/extra/where/where-1.0.0)" = usr ]
|
||||
R yes_to 'y\n' 'syspkg -f split'
|
||||
check "a build installing into PREFIX and /usr both fails" has "[x] Building extra-src::split 1.0.0 failed."
|
||||
check "...the log says why" grep -q "split installed into both /glacier/sys/links and /usr" $BUILD/logs/split-1.0.0.log
|
||||
|
||||
hr "the repository question"
|
||||
R yes_to '\ny\n' 'syspkg -f both'
|
||||
check "both repos serving a name: asked" has "[i] both is available from more than one repository:"
|
||||
check "...the binary first (repos.conf order)" has " 1) extra::both 9.0.0"
|
||||
check "...then the recipe" has " 2) extra-src::both 1.0.0 [+loud] (build)"
|
||||
check "...Enter takes the first" grep -q "^extra::both::9.0.0::explicit$" $IDX
|
||||
check "...not marked as built" [ ! -e /glacier/sys/store/extra/both/both-9.0.0/.glacier-src ]
|
||||
R yes_to '2\ny\n' 'syspkg -f -O both'
|
||||
check "2 builds the recipe (replacing the binary under -O)" grep -q "^extra::both::1.0.0::explicit$" $IDX
|
||||
check "...marked as built" [ "$(cat /glacier/sys/store/extra/both/both-1.0.0/.glacier-src)" = extra-src ]
|
||||
R yes_to '3\n' 'syspkg -f -O both'
|
||||
check "an answer out of range is refused" has "[x] Invalid selection."
|
||||
check "...exit 1" rc 1
|
||||
R yes_to 'x\n' 'syspkg -f -O both'
|
||||
check "a non-number is refused" has "[x] Invalid selection."
|
||||
R sh -c 'syspkg -f -O both < /dev/null'
|
||||
check "end of input is refused" has "[x] Invalid selection."
|
||||
|
||||
hr "refused builds"
|
||||
R yes_to 'y\n' 'syspkg -f badhash'
|
||||
check "a source with the wrong hash" has "[x] A source of extra-src::badhash 1.0.0 does not match its hash."
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q badhash $IDX"
|
||||
R yes_to 'y\n' 'syspkg -f broken'
|
||||
check "a failing build" has "[x] Building extra-src::broken 1.0.0 failed. Log: $BUILD/logs/broken-1.0.0.log"
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q broken $IDX"
|
||||
check "...the log says why" grep -q "Error\|error" $BUILD/logs/broken-1.0.0.log
|
||||
check "...its dirs removed" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
R yes_to 'y\n' 'syspkg -f evil'
|
||||
check "a build that produces another package" has "[x] extra-src::evil 1.0.0: the build produced another package."
|
||||
check "...nothing installed" sh -c "! grep -q evil $IDX && [ ! -e /usr/bin/evil ]"
|
||||
# An index entry that disagrees with its bundle's recipe (signed all
|
||||
# the same): the recipe has a dependency the index left out.
|
||||
mkrecipe liar 1.0.0 "$(mksrc liar 1.0.0)" 'DEPS = ( { NAME = "hidep"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
SER=$(sed -n 's/^SERIAL = \([0-9]*\)L;$/\1/p' $S/repo.idx)
|
||||
awk -v s=$((SER + 1)) '/^SERIAL/ { print "SERIAL = " s "L;"; next } /NAME = "liar"/ { liar = 1 } liar && /DEPS = / { sub(/DEPS = \(.*\);/, "DEPS = ();"); liar = 0 } { print }' $S/repo.idx > /tmp/idx && mv /tmp/idx $S/repo.idx
|
||||
gsign -S -s $SSEC -m $S/repo.idx >/dev/null
|
||||
R yes_to 'y\n' 'syspkg -f liar'
|
||||
check "a recipe that disagrees with its index entry" has "[x] extra-src::liar 1.0.0 does not match its index."
|
||||
check "...nothing installed" sh -c "! grep -q liar $IDX"
|
||||
chmod 666 /app/lib/build-driver.sh
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
chmod 644 /app/lib/build-driver.sh
|
||||
check "a world-writable build driver is refused" has "[x] Refusing /app/lib/build-driver.sh: /app/lib/build-driver.sh is not root-owned, or is group/world-writable."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
R yes_to 'y\n' 'syspkg -f needy'
|
||||
check "a build dependency no binary repo has" has "[x] extra-src::needy 1.0.0 needs nothere 1.0.0 or later to build, and no binary repository has it."
|
||||
check "...refused before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ];'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER unset: refused" has "[x] BUILD_USER is not set in /etc/glacier/glacier.conf."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_USER = "gbuild";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_ROOT unset: refused" has "[x] BUILD_ROOT is not set in /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "root";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER = root: refused" has "[x] BUILD_USER in /etc/glacier/glacier.conf must not be root."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "nosuchuser";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER not a user: refused" has "[x] BUILD_USER nosuchuser in /etc/glacier/glacier.conf is not a user."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; JOBS = 0;'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "JOBS = 0: glacier.conf is malformed" has "[x] Malformed /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
mkdir -p /mnt/t && syspkg -R /mnt/t -n >/dev/null
|
||||
cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/ 2>/dev/null || { mkdir -p /mnt/t/etc/glacier && cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/; }
|
||||
R yes_to '2\ny\n' 'syspkg -R /mnt/t -f both'
|
||||
check "building under -R is refused" has "[x] Building from source under -R is not supported."
|
||||
|
||||
hr "base-src and the machine key"
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "a base build without the machine key trusted for base is refused" has '[x] base-src::core 1.0.0 [-loud] (build) needs the key "local" trusted for base.'
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
check "...having made the machine key" has "[i] Made the machine key /etc/glacier/keys/local.pub."
|
||||
check "...its public half 0644" [ "$(stat -c '%u %a' /etc/glacier/keys/local.pub)" = "0 644" ]
|
||||
check "...its secret half root's 0600" [ "$(stat -c '%u %a' /glacier/sys/keys/local.sec)" = "0 600" ]
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "...made once" lacks "Made the machine key"
|
||||
rconf '"glacier-test", "local"'
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "listed for base: built and installed" grep -q "^base::core::1.0.0::explicit$" $IDX
|
||||
check "...linked into /usr, as base is" [ "$(/usr/bin/core)" = hello ]
|
||||
check "...marked as built from base-src" [ "$(cat /glacier/sys/store/base/core/core-1.0.0/.glacier-src)" = base-src ]
|
||||
check "...built for /usr" [ "$(cat /usr/share/core.prefix)" = /usr ]
|
||||
mv /glacier/sys/keys/local.sec /root/local.sec.bak
|
||||
printf 'TRAITS = [ "loud" ];\n' > /etc/glacier/config/core.conf
|
||||
R yes_to 'y\n' 'syspkg -u core'
|
||||
rm /etc/glacier/config/core.conf
|
||||
rconf '"glacier-test"'
|
||||
mv /root/local.sec.bak /glacier/sys/keys/local.sec
|
||||
check "a missing secret half is refused" has "[x] /etc/glacier/keys/local.pub and /glacier/sys/keys/local.sec must both exist, or neither."
|
||||
|
||||
hr "gpkg: the user's own build"
|
||||
U=/glacier/usr/1000
|
||||
su glacier -c 'doas /app/gstore -n' >/dev/null 2>&1
|
||||
check "gstore -n makes the user's config dir" [ "$(stat -c '%U %a' $U/config)" = "glacier 700" ]
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud" ];'
|
||||
R su glacier -c "printf '2\ny\n' | /app/gpkg -f both"
|
||||
check "gpkg builds it" has "[✓] Built extra-src::both 1.0.0."
|
||||
check "...the system's TRAITS apply" has "[i] Building extra-src::both 1.0.0 [+loud]..."
|
||||
check "...installed for the user" grep -q "^extra::both::1.0.0::explicit$" $U/index/current
|
||||
check "...the layers are the user's" [ "$(stat -c %U $U/build/layers/toolchain-1.0.0-*)" = glacier ]
|
||||
check "...the log too" [ "$(stat -c '%U %a' $U/build/logs/both-1.0.0.log)" = "glacier 600" ]
|
||||
check "...it runs" [ "$(su glacier -c "$U/links/bin/both")" = HELLO ]
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f where"
|
||||
check "gpkg builds for the user's links" [ "$(su glacier -c "$U/links/bin/where")" = here ]
|
||||
check "...the path compiled in" grep -q "^cat $U/links/share/where/data$" $U/links/bin/where
|
||||
su glacier -c "printf 'TRAITS = [ \"-loud\" ];\n' > $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "the user's per-package file wins: rebuilt without loud" has " * extra-src::both 1.0.0 (traits) [-loud] (build)"
|
||||
check "...installed" [ "$(su glacier -c "$U/links/bin/both")" = hello ]
|
||||
su glacier -c "printf 'TRAITS = [ \"loud\" ];\n' > $U/config/glacier.conf; rm $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "...the user's glacier.conf too" has "[+loud] (build)"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f core"
|
||||
check "gpkg never builds from base-src" has "[x] core is not in any repository."
|
||||
su glacier -c "printf 'BUILD_USER = \"x\";\n' > $U/config/glacier.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "BUILD_USER in a user's glacier.conf is malformed" has "[x] Malformed $U/config/glacier.conf."
|
||||
|
||||
echo
|
||||
if [ $FAILS -eq 0 ]; then echo "ALL PASSED"; else echo "$FAILS FAILED"; fi
|
||||
[ $FAILS -eq 0 ]
|
||||
@@ -0,0 +1,408 @@
|
||||
#!/bin/sh
|
||||
# Source builds through syspkg/gpkg -f/-u (BUILD_DESIGN.txt; steps 3-4):
|
||||
# - a recipe from a NAME-src repo is built in gsandbox and installed
|
||||
# like a fetched package: summary and prompt BEFORE building, the
|
||||
# patches applied, the manifest the build was handed, .glacier-src;
|
||||
# - a name served by several repositories is asked about; Enter takes
|
||||
# the first, a bad answer refuses;
|
||||
# - TRAITS: recipe defaults, /etc/glacier/glacier.conf, per-package
|
||||
# files (an unknown trait refused), the user's own files for gpkg;
|
||||
# - -u stays on source, and rebuilds when the traits change;
|
||||
# - syspkg builds as BUILD_USER (required), gpkg as the user;
|
||||
# - PREFIX: a package is built for where its usr/ is linked
|
||||
# (/glacier/sys/links, the user's links, /usr for base) and still
|
||||
# packed as usr/; during the build PREFIX resolves to the layers;
|
||||
# - kept packages: an installed source build's .gpkg is kept, and a
|
||||
# later build uses it (and what it depends on) as a layer; kept
|
||||
# only while that version is installed;
|
||||
# - refusals: a source with the wrong hash, a failing build, a build
|
||||
# that produces another package, a build dependency no binary repo
|
||||
# has, an untrusted per-package file, -R.
|
||||
# Needs what sandbox_alpine.sh needs, with /glacier off Docker's overlay:
|
||||
#
|
||||
# docker run --rm --security-opt seccomp=unconfined \
|
||||
# --security-opt apparmor=unconfined --security-opt systempaths=unconfined \
|
||||
# --tmpfs /glacier:exec,mode=755 \
|
||||
# -v "$PWD/tests/build_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
R() {
|
||||
OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed '/^rc=/d; s/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
hr() { echo; echo "== $*"; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
K=/etc/glacier/keys
|
||||
CONF=/etc/glacier/repos.conf
|
||||
GCONF=/etc/glacier/glacier.conf
|
||||
XSEC=/root/extrakey.sec
|
||||
SSEC=/root/srckey.sec
|
||||
E=/srv/extra
|
||||
S=/srv/extra-src
|
||||
SRC=/srv/sources
|
||||
IDX=/glacier/sys/index/current
|
||||
BUILD=/glacier/sys/build
|
||||
BS=/srv/base-src
|
||||
mkdir -p $E $S $SRC $BS && chmod 755 /srv $E $S $SRC $BS
|
||||
|
||||
gsign -G -p $K/extrakey.pub -s $XSEC -c extrakey >/dev/null
|
||||
gsign -G -p $K/srckey.pub -s $SSEC -c srckey >/dev/null
|
||||
rconf() { # BASE-KEYS
|
||||
printf 'repos = (\n%s\n%s\n%s\n%s\n);\n' \
|
||||
" { name = \"base\"; keys = [ $1 ]; }," \
|
||||
' { name = "extra"; url = "file:///srv/extra"; keys = [ "extrakey" ]; },' \
|
||||
' { name = "extra-src"; url = "file:///srv/extra-src"; keys = [ "srckey" ]; },' \
|
||||
' { name = "base-src"; url = "file:///srv/base-src"; keys = [ "srckey" ]; }' > $CONF
|
||||
chmod 644 $CONF
|
||||
}
|
||||
rconf '"glacier-test"'
|
||||
adduser -S -D -H -s /sbin/nologin gbuild
|
||||
gconf() { printf 'KEEP_OLD = 1;\n%s\n' "$1" > $GCONF; chmod 644 $GCONF; }
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
|
||||
# The binary repo: a toolchain (Alpine's build-base, busybox for /bin/sh)
|
||||
# and a runtime dependency.
|
||||
/app/tests/mkroot -o $E -r build-base -r busybox -p busybox toolchain 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E hidep 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E loudlib 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E both 9.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E twin 1.0.0 extra >/dev/null
|
||||
|
||||
# A source tarball: hello prints "hello", "HELLO" with LOUD=1.
|
||||
mksrc() { # NAME VER [MAKEFILE-EXTRA]
|
||||
d=/tmp/src/$1-$2
|
||||
rm -rf $d && mkdir -p $d
|
||||
printf '#include <stdio.h>\nint main(void) {\n#ifdef LOUD\n\tputs("HELLO");\n#else\n\tputs("hello");\n#endif\n\treturn 0;\n}\n' > $d/hello.c
|
||||
printf 'CFLAGS += $(if $(LOUD),-DLOUD)\n%s: hello.c\n\t$(CC) $(CFLAGS) -o %s hello.c\ninstall:\n\tinstall -D -m 755 %s $(DESTDIR)$(PREFIX)/bin/%s\n%s\n' \
|
||||
"$1" "$1" "$1" "$1" "${3:-}" > $d/Makefile
|
||||
tar czf $SRC/$1-$2.tar.gz -C /tmp/src $1-$2
|
||||
sha256sum $SRC/$1-$2.tar.gz | cut -c1-64
|
||||
}
|
||||
# A recipe bundle in the -src repo: NAME VER SHA [EXTRA-FIELDS] [BUILD-FILE]
|
||||
mkrecipe() {
|
||||
t=/tmp/rcp/$1-$2
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "%s"; VER = "%s"; REPO = "extra"; DESC = "test \\"%s\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/%s-%s.tar.gz"; SHA256 = "sha256:%s"; } );\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\n%s\n' \
|
||||
"$1" "$2" "$1" "$SRC" "$1" "$2" "$3" "${4:-}" > $t/recipe
|
||||
[ -n "${5:-}" ] && printf '%s\n' "$5" > $t/build
|
||||
rmdir $t/patches 2>/dev/null
|
||||
tar czf $S/$1-$2.grcp -C /tmp/rcp $1-$2
|
||||
}
|
||||
publish() {
|
||||
grepo -s $XSEC -r extra $E | grep -v Wrote; grepo -s $SSEC -r extra-src $S | grep -v Wrote
|
||||
grepo -s $SSEC -r base-src $BS | grep -v Wrote
|
||||
}
|
||||
|
||||
# hello 1.0.0, patched to greet glacier, depending on hidep.
|
||||
H=$(mksrc hello 1.0.0)
|
||||
t=/tmp/rcp/hello-1.0.0
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "hello"; VER = "1.0.0"; REPO = "extra"; DESC = "say \\"hi\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/hello-1.0.0.tar.gz"; SHA256 = "sha256:%s"; } );\nPATCHES = [ "01-glacier.patch" ];\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\nDEPS = ( { NAME = "hidep"; VER = "1.0.0"; }, { NAME = "loudlib"; VER = "1.0.0"; IF = "loud"; } );\n' "$SRC" "$H" > $t/recipe
|
||||
cat > $t/patches/01-glacier.patch <<'EOF'
|
||||
--- a/hello.c
|
||||
+++ b/hello.c
|
||||
@@ -5,3 +5,3 @@
|
||||
#else
|
||||
- puts("hello");
|
||||
+ puts("hello, glacier");
|
||||
#endif
|
||||
EOF
|
||||
tar czf $S/hello-1.0.0.grcp -C /tmp/rcp hello-1.0.0
|
||||
mkrecipe both 1.0.0 "$(mksrc both 1.0.0)"
|
||||
mkrecipe badhash 1.0.0 "$(printf '%064d' 0)"
|
||||
mksrc badhash 1.0.0 >/dev/null
|
||||
mkrecipe broken 1.0.0 "$(mksrc broken 1.0.0 'broken: ; false')" '' 'compile() { make broken; }'
|
||||
mkrecipe evil 1.0.0 "$(mksrc evil 1.0.0)" '' 'package() {
|
||||
mkdir -p /tmp/p/evil-1.0.0/files/usr/bin
|
||||
echo x > /tmp/p/evil-1.0.0/files/usr/bin/evil
|
||||
printf "PKG_NAME = \"evil\"; PKG_VER = \"1.0.0\"; PKG_REPO = \"base\"; PKG_DEPS = (); PKG_CONFS = ();\n" > /tmp/p/evil-1.0.0/manifest.gpm.cfg
|
||||
tar czf /.glacier/out/evil-1.0.0.gpkg -C /tmp/p evil-1.0.0
|
||||
exit 0
|
||||
}'
|
||||
mkrecipe core 1.0.0 "$(mksrc core 1.0.0)" '' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR$PREFIX/share" && echo "$PREFIX" > "$DESTDIR$PREFIX/share/core.prefix"
|
||||
}'
|
||||
sed -i 's/REPO = "extra"/REPO = "base"/' /tmp/rcp/core-1.0.0/recipe
|
||||
tar czf $BS/core-1.0.0.grcp -C /tmp/rcp core-1.0.0 && rm $S/core-1.0.0.grcp
|
||||
# where: a script with its data file's path compiled in. Its prepare
|
||||
# fails unless PREFIX leads to the layers during the build.
|
||||
mkrecipe where 1.0.0 "$(mksrc where 1.0.0)" 'BUILD = "hooks";' 'prepare() { [ -x "$PREFIX/bin/make" ]; }
|
||||
package() {
|
||||
mkdir -p "$DESTDIR$PREFIX/bin" "$DESTDIR$PREFIX/share/where"
|
||||
echo here > "$DESTDIR$PREFIX/share/where/data"
|
||||
printf "#!/bin/sh\ncat %s/share/where/data\n" "$PREFIX" > "$DESTDIR$PREFIX/bin/where"
|
||||
chmod 755 "$DESTDIR$PREFIX/bin/where"
|
||||
}'
|
||||
sed -i 's/^BUILD = "make";$//' /tmp/rcp/where-1.0.0/recipe && tar czf $S/where-1.0.0.grcp -C /tmp/rcp where-1.0.0
|
||||
mkrecipe split 1.0.0 "$(mksrc split 1.0.0)" '' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR/usr/share" && echo x > "$DESTDIR/usr/share/split"
|
||||
}'
|
||||
# srclib: only a recipe, depending on hidep. srcapp needs both in its
|
||||
# layers to build.
|
||||
mkrecipe srclib 1.0.0 "$(mksrc srclib 1.0.0)" 'DEPS = ( { NAME = "hidep"; VER = "1.0.0"; } );' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR$PREFIX/share/srclib" && echo yes > "$DESTDIR$PREFIX/share/srclib/marker"
|
||||
}'
|
||||
mkrecipe srcapp 1.0.0 "$(mksrc srcapp 1.0.0)" 'DEPS = ( { NAME = "srclib"; VER = "1.0.0"; } );' 'prepare() { [ -f /usr/share/srclib/marker ] && [ -x /usr/bin/hidep ]; }'
|
||||
mkrecipe twin 1.0.0 "$(mksrc twin 1.0.0)"
|
||||
mkrecipe needy 1.0.0 "$(mksrc needy 1.0.0)" 'BUILD_DEPS = ( { NAME = "nothere"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
|
||||
syspkg -n >/dev/null
|
||||
yes_to() { sh -c "printf '$1' | $2"; }
|
||||
|
||||
hr "syspkg -f of a recipe"
|
||||
R yes_to 'y\n' 'syspkg -f hello'
|
||||
check "built and installed" has "[✓] Completed with no errors."
|
||||
check "...exit 0" rc 0
|
||||
check "the summary marks the build, its traits off by default" has " * extra-src::hello 1.0.0 [-loud] (build)"
|
||||
check "...and the binary dependency" has " * extra::hidep 1.0.0 (dependency)"
|
||||
check "...the prompt came before the build" sh -c 'echo "$0" | grep -n "Proceed\|Building extra-src" | head -1 | grep -q Proceed' "$OUT"
|
||||
check "the build is announced" has "[i] Building extra-src::hello 1.0.0 [-loud]..."
|
||||
check "...and done" has "[✓] Built extra-src::hello 1.0.0."
|
||||
check "installed as extra::hello" grep -q "^extra::hello::1.0.0::explicit$" $IDX
|
||||
check "...with its dependency, auto" grep -q "^extra::hidep::1.0.0::auto$" $IDX
|
||||
check "...not the one only loud needs" sh -c "! grep -q loudlib $IDX"
|
||||
check "...the patch applied" [ "$(/glacier/sys/links/bin/hello)" = "hello, glacier" ]
|
||||
V=/glacier/sys/store/extra/hello/hello-1.0.0
|
||||
check "...recorded as built from extra-src" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...its manifest has PKG_TRAITS" grep -qF 'PKG_TRAITS = [ "-loud" ];' $V/.glacier-manifest
|
||||
check "...and the recipe's DESC, quoted" grep -qF 'PKG_DESC = "say \"hi\"";' $V/.glacier-manifest
|
||||
check "the layer belongs to BUILD_USER" [ "$(stat -c %U $BUILD/layers/toolchain-1.0.0-*)" = gbuild ]
|
||||
check "...which owns layers/, 0700" [ "$(stat -c '%U %a' $BUILD/layers)" = "gbuild 700" ]
|
||||
check "the build's in/work/out dirs are gone" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
check "the log is kept" grep -q "==> Done" $BUILD/logs/hello-1.0.0.log
|
||||
check "...the sources cached by hash" [ -f $BUILD/sources/$H ]
|
||||
check "the cached bundle is deleted" [ ! -e /glacier/sys/cache/extra-src/hello-1.0.0.grcp ]
|
||||
check "the store copy is root's, as any install" [ "$(stat -c %u /glacier/sys/store/extra/hello/hello-1.0.0/usr/bin/hello)" = 0 ]
|
||||
|
||||
hr "declining builds nothing"
|
||||
rm $BUILD/logs/hello-1.0.0.log
|
||||
R yes_to 'n\n' 'syspkg -f -O hello'
|
||||
check "nothing built" sh -c "[ ! -e $BUILD/logs/hello-1.0.0.log ]"
|
||||
check "...nothing said about building" lacks "Building"
|
||||
|
||||
hr "traits"
|
||||
mkdir -p /etc/glacier/config
|
||||
printf 'TRAITS = [ "lod" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "an unknown trait in a per-package file is refused" has "[x] /etc/glacier/config/hello.conf: hello has no trait lod."
|
||||
check "...exit 1, before the prompt" sh -c "[ $RC = 1 ]" && check "...no prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud", "x11" ];'
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "the per-package file beats glacier.conf: nothing to rebuild" has "Nothing to do."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "glacier.conf turns loud on (x11, unknown to the recipe, ignored): a rebuild" has " * extra-src::hello 1.0.0 (traits) [+loud] (build)"
|
||||
check "...installed" rc 0
|
||||
check "...built with LOUD" [ "$(/glacier/sys/links/bin/hello)" = "HELLO" ]
|
||||
check "...with the dependency loud needs" grep -q "^extra::loudlib::1.0.0::auto$" $IDX
|
||||
check "...in its manifest" grep -qF '{ PKG_NAME = "loudlib"; PKG_VER = "1.0.0"; }' $V/.glacier-manifest
|
||||
check "...still marked as built from source" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...PKG_TRAITS updated" grep -qF 'PKG_TRAITS = [ "loud" ];' $V/.glacier-manifest
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "unchanged traits: nothing to do" has "Nothing to do."
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
chmod 666 /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a world-writable per-package file is refused" has "[x] Refusing /etc/glacier/config/hello.conf: /etc/glacier/config/hello.conf is not root-owned, or is group/world-writable."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
printf 'TRAITS = [ "loud" ]; COLOR = "red";\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a per-package file with an unknown key is malformed" has "[x] Malformed /etc/glacier/config/hello.conf."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
|
||||
hr "-u stays on source"
|
||||
H11=$(mksrc hello 1.1.0)
|
||||
sed "s/1\.0\.0/1.1.0/g; s/$H/$H11/" /tmp/rcp/hello-1.0.0/recipe > /tmp/r.new
|
||||
rm -rf /tmp/rcp/hello-1.1.0 && cp -a /tmp/rcp/hello-1.0.0 /tmp/rcp/hello-1.1.0 && mv /tmp/r.new /tmp/rcp/hello-1.1.0/recipe
|
||||
sed -i 's/VER = "1.1.0"; }/VER = "1.0.0"; }/g; s/VER = "1.1.0"; IF/VER = "1.0.0"; IF/' /tmp/rcp/hello-1.1.0/recipe
|
||||
tar czf $S/hello-1.1.0.grcp -C /tmp/rcp hello-1.1.0
|
||||
/app/tests/mkpkg -o $E hello 2.0.0 extra >/dev/null
|
||||
publish
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "a newer recipe is built, not extra's binary 2.0.0" has " * extra-src::hello 1.0.0 -> 1.1.0 [+loud] (build)"
|
||||
check "...installed" grep -q "^extra::hello::1.1.0::explicit$" $IDX
|
||||
check "...the old version backed up" [ -f /glacier/sys/backup/extra/hello/hello-1.0.0.gpkg ]
|
||||
check "...its new package kept for builds" [ -f $BUILD/packages/extra@hello-1.1.0.gpkg ]
|
||||
check "...the old one's deleted" [ ! -e $BUILD/packages/extra@hello-1.0.0.gpkg ]
|
||||
check "...still on source" [ "$(cat /glacier/sys/store/extra/hello/hello-1.1.0/.glacier-src)" = extra-src ]
|
||||
|
||||
hr "PREFIX"
|
||||
R yes_to 'y\n' 'syspkg -f where'
|
||||
check "built for /glacier/sys/links: finds its data through the links" [ "$(/glacier/sys/links/bin/where)" = here ]
|
||||
check "...the path compiled in" grep -q "^cat /glacier/sys/links/share/where/data$" /glacier/sys/links/bin/where
|
||||
check "...packed as usr/, as any package" [ -f /glacier/sys/store/extra/where/where-1.0.0/usr/share/where/data ]
|
||||
check "...nothing else at the top" [ "$(ls /glacier/sys/store/extra/where/where-1.0.0)" = usr ]
|
||||
R yes_to 'y\n' 'syspkg -f split'
|
||||
check "a build installing into PREFIX and /usr both fails" has "[x] Building extra-src::split 1.0.0 failed."
|
||||
check "...the log says why" grep -q "split installed into both /glacier/sys/links and /usr" $BUILD/logs/split-1.0.0.log
|
||||
|
||||
hr "kept packages"
|
||||
P=$BUILD/packages
|
||||
check "a source build's package is kept, root's 0644" [ "$(stat -c '%u %a' $P/extra@where-1.0.0.gpkg)" = "0 644" ]
|
||||
check "...byte for byte what was installed" sh -c "tar xzOf $P/extra@where-1.0.0.gpkg where-1.0.0/files/usr/bin/where | cmp -s - /glacier/sys/store/extra/where/where-1.0.0/usr/bin/where"
|
||||
check "...not a failed one" [ ! -e $P/extra@split-1.0.0.gpkg ]
|
||||
R yes_to 'y\n' 'syspkg -f srclib'
|
||||
check "srclib (no binary anywhere) built and installed" rc 0
|
||||
R yes_to 'y\n' 'syspkg -f srcapp'
|
||||
check "a build needing an installed source build: built" has "[✓] Built extra-src::srcapp 1.0.0."
|
||||
check "...installed" grep -q "^extra::srcapp::1.0.0::explicit$" $IDX
|
||||
check "...the kept package was its layer" sh -c "ls $BUILD/layers | grep -q '^srclib-1.0.0-'"
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/srcapp.conf
|
||||
R yes_to 'y\n' 'syspkg -u srcapp'
|
||||
check "-u rebuilds it (traits changed)" has " * extra-src::srcapp 1.0.0 (traits) [-loud] (build)"
|
||||
check "...exit 0" rc 0
|
||||
mv $P/extra@srclib-1.0.0.gpkg /root/kept.bak
|
||||
rm /etc/glacier/config/srcapp.conf
|
||||
R yes_to 'y\n' 'syspkg -u srcapp'
|
||||
mv /root/kept.bak $P/extra@srclib-1.0.0.gpkg
|
||||
check "without its kept package, srclib can't be a layer" has "[x] extra-src::srcapp 1.0.0 needs srclib 1.0.0 or later to build, and no binary repository has it."
|
||||
R yes_to 'y\n' 'syspkg -x srcapp'
|
||||
check "removing srcapp deletes its kept package" [ ! -e $P/extra@srcapp-1.0.0.gpkg ]
|
||||
check "...not srclib's, still installed" [ -f $P/extra@srclib-1.0.0.gpkg ]
|
||||
R yes_to 'y\n' 'syspkg -x srclib'
|
||||
check "removing srclib deletes its own" [ ! -e $P/extra@srclib-1.0.0.gpkg ]
|
||||
check "...others stay" [ -f $P/extra@where-1.0.0.gpkg ]
|
||||
R yes_to '2\ny\n' 'syspkg -f twin'
|
||||
check "twin built from source: kept" [ -f $P/extra@twin-1.0.0.gpkg ]
|
||||
R yes_to '1\ny\n' 'syspkg -f -O twin'
|
||||
check "the same version reinstalled from the binary repo" [ "$(/glacier/sys/links/bin/twin)" = "twin 1.0.0 says hi" ]
|
||||
check "...no longer kept" [ ! -e $P/extra@twin-1.0.0.gpkg ]
|
||||
|
||||
hr "the repository question"
|
||||
R yes_to '\ny\n' 'syspkg -f both'
|
||||
check "both repos serving a name: asked" has "[i] both is available from more than one repository:"
|
||||
check "...the binary first (repos.conf order)" has " 1) extra::both 9.0.0"
|
||||
check "...then the recipe" has " 2) extra-src::both 1.0.0 [+loud] (build)"
|
||||
check "...Enter takes the first" grep -q "^extra::both::9.0.0::explicit$" $IDX
|
||||
check "...not marked as built" [ ! -e /glacier/sys/store/extra/both/both-9.0.0/.glacier-src ]
|
||||
check "...nor kept" [ ! -e $BUILD/packages/extra@both-9.0.0.gpkg ]
|
||||
R yes_to '2\ny\n' 'syspkg -f -O both'
|
||||
check "2 builds the recipe (replacing the binary under -O)" grep -q "^extra::both::1.0.0::explicit$" $IDX
|
||||
check "...marked as built" [ "$(cat /glacier/sys/store/extra/both/both-1.0.0/.glacier-src)" = extra-src ]
|
||||
check "...and kept" [ -f $BUILD/packages/extra@both-1.0.0.gpkg ]
|
||||
R yes_to '3\n' 'syspkg -f -O both'
|
||||
check "an answer out of range is refused" has "[x] Invalid selection."
|
||||
check "...exit 1" rc 1
|
||||
R yes_to 'x\n' 'syspkg -f -O both'
|
||||
check "a non-number is refused" has "[x] Invalid selection."
|
||||
R sh -c 'syspkg -f -O both < /dev/null'
|
||||
check "end of input is refused" has "[x] Invalid selection."
|
||||
|
||||
hr "refused builds"
|
||||
R yes_to 'y\n' 'syspkg -f badhash'
|
||||
check "a source with the wrong hash" has "[x] A source of extra-src::badhash 1.0.0 does not match its hash."
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q badhash $IDX"
|
||||
R yes_to 'y\n' 'syspkg -f broken'
|
||||
check "a failing build" has "[x] Building extra-src::broken 1.0.0 failed. Log: $BUILD/logs/broken-1.0.0.log"
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q broken $IDX"
|
||||
check "...the log says why" grep -q "Error\|error" $BUILD/logs/broken-1.0.0.log
|
||||
check "...its dirs removed" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
R yes_to 'y\n' 'syspkg -f evil'
|
||||
check "a build that produces another package" has "[x] extra-src::evil 1.0.0: the build produced another package."
|
||||
check "...nothing installed" sh -c "! grep -q evil $IDX && [ ! -e /usr/bin/evil ]"
|
||||
# An index entry that disagrees with its bundle's recipe (signed all
|
||||
# the same): the recipe has a dependency the index left out.
|
||||
mkrecipe liar 1.0.0 "$(mksrc liar 1.0.0)" 'DEPS = ( { NAME = "hidep"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
SER=$(sed -n 's/^SERIAL = \([0-9]*\)L;$/\1/p' $S/repo.idx)
|
||||
awk -v s=$((SER + 1)) '/^SERIAL/ { print "SERIAL = " s "L;"; next } /NAME = "liar"/ { liar = 1 } liar && /DEPS = / { sub(/DEPS = \(.*\);/, "DEPS = ();"); liar = 0 } { print }' $S/repo.idx > /tmp/idx && mv /tmp/idx $S/repo.idx
|
||||
gsign -S -s $SSEC -m $S/repo.idx >/dev/null
|
||||
R yes_to 'y\n' 'syspkg -f liar'
|
||||
check "a recipe that disagrees with its index entry" has "[x] extra-src::liar 1.0.0 does not match its index."
|
||||
check "...nothing installed" sh -c "! grep -q liar $IDX"
|
||||
chmod 666 /app/lib/build-driver.sh
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
chmod 644 /app/lib/build-driver.sh
|
||||
check "a world-writable build driver is refused" has "[x] Refusing /app/lib/build-driver.sh: /app/lib/build-driver.sh is not root-owned, or is group/world-writable."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
R yes_to 'y\n' 'syspkg -f needy'
|
||||
check "a build dependency no binary repo has" has "[x] extra-src::needy 1.0.0 needs nothere 1.0.0 or later to build, and no binary repository has it."
|
||||
check "...refused before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ];'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER unset: refused" has "[x] BUILD_USER is not set in /etc/glacier/glacier.conf."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_USER = "gbuild";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_ROOT unset: refused" has "[x] BUILD_ROOT is not set in /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "root";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER = root: refused" has "[x] BUILD_USER in /etc/glacier/glacier.conf must not be root."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "nosuchuser";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER not a user: refused" has "[x] BUILD_USER nosuchuser in /etc/glacier/glacier.conf is not a user."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; JOBS = 0;'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "JOBS = 0: glacier.conf is malformed" has "[x] Malformed /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
mkdir -p /mnt/t && syspkg -R /mnt/t -n >/dev/null
|
||||
cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/ 2>/dev/null || { mkdir -p /mnt/t/etc/glacier && cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/; }
|
||||
R yes_to '2\ny\n' 'syspkg -R /mnt/t -f both'
|
||||
check "building under -R is refused" has "[x] Building from source under -R is not supported."
|
||||
|
||||
hr "base-src and the machine key"
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "a base build without the machine key trusted for base is refused" has '[x] base-src::core 1.0.0 [-loud] (build) needs the key "local" trusted for base.'
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
check "...having made the machine key" has "[i] Made the machine key /etc/glacier/keys/local.pub."
|
||||
check "...its public half 0644" [ "$(stat -c '%u %a' /etc/glacier/keys/local.pub)" = "0 644" ]
|
||||
check "...its secret half root's 0600" [ "$(stat -c '%u %a' /glacier/sys/keys/local.sec)" = "0 600" ]
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "...made once" lacks "Made the machine key"
|
||||
rconf '"glacier-test", "local"'
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "listed for base: built and installed" grep -q "^base::core::1.0.0::explicit$" $IDX
|
||||
check "...linked into /usr, as base is" [ "$(/usr/bin/core)" = hello ]
|
||||
check "...marked as built from base-src" [ "$(cat /glacier/sys/store/base/core/core-1.0.0/.glacier-src)" = base-src ]
|
||||
check "...built for /usr" [ "$(cat /usr/share/core.prefix)" = /usr ]
|
||||
mv /glacier/sys/keys/local.sec /root/local.sec.bak
|
||||
printf 'TRAITS = [ "loud" ];\n' > /etc/glacier/config/core.conf
|
||||
R yes_to 'y\n' 'syspkg -u core'
|
||||
rm /etc/glacier/config/core.conf
|
||||
rconf '"glacier-test"'
|
||||
mv /root/local.sec.bak /glacier/sys/keys/local.sec
|
||||
check "a missing secret half is refused" has "[x] /etc/glacier/keys/local.pub and /glacier/sys/keys/local.sec must both exist, or neither."
|
||||
|
||||
hr "gpkg: the user's own build"
|
||||
U=/glacier/usr/1000
|
||||
su glacier -c 'doas /app/gstore -n' >/dev/null 2>&1
|
||||
check "gstore -n makes the user's config dir" [ "$(stat -c '%U %a' $U/config)" = "glacier 700" ]
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud" ];'
|
||||
R su glacier -c "printf '2\ny\n' | /app/gpkg -f both"
|
||||
check "gpkg builds it" has "[✓] Built extra-src::both 1.0.0."
|
||||
check "...the system's TRAITS apply" has "[i] Building extra-src::both 1.0.0 [+loud]..."
|
||||
check "...installed for the user" grep -q "^extra::both::1.0.0::explicit$" $U/index/current
|
||||
check "...the layers are the user's" [ "$(stat -c %U $U/build/layers/toolchain-1.0.0-*)" = glacier ]
|
||||
check "...the log too" [ "$(stat -c '%U %a' $U/build/logs/both-1.0.0.log)" = "glacier 600" ]
|
||||
check "...it runs" [ "$(su glacier -c "$U/links/bin/both")" = HELLO ]
|
||||
check "...kept in the user's base, 0600" [ "$(stat -c '%U %a' $U/build/packages/extra@both-1.0.0.gpkg)" = "glacier 600" ]
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f where"
|
||||
check "gpkg builds for the user's links" [ "$(su glacier -c "$U/links/bin/where")" = here ]
|
||||
check "...the path compiled in" grep -q "^cat $U/links/share/where/data$" $U/links/bin/where
|
||||
su glacier -c "printf 'TRAITS = [ \"-loud\" ];\n' > $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "the user's per-package file wins: rebuilt without loud" has " * extra-src::both 1.0.0 (traits) [-loud] (build)"
|
||||
check "...installed" [ "$(su glacier -c "$U/links/bin/both")" = hello ]
|
||||
su glacier -c "printf 'TRAITS = [ \"loud\" ];\n' > $U/config/glacier.conf; rm $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "...the user's glacier.conf too" has "[+loud] (build)"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f core"
|
||||
check "gpkg never builds from base-src" has "[x] core is not in any repository."
|
||||
su glacier -c "printf 'BUILD_USER = \"x\";\n' > $U/config/glacier.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "BUILD_USER in a user's glacier.conf is malformed" has "[x] Malformed $U/config/glacier.conf."
|
||||
|
||||
echo
|
||||
if [ $FAILS -eq 0 ]; then echo "ALL PASSED"; else echo "$FAILS FAILED"; fi
|
||||
[ $FAILS -eq 0 ]
|
||||
@@ -0,0 +1,424 @@
|
||||
#!/bin/sh
|
||||
# Source builds through syspkg/gpkg -f/-u (BUILD_DESIGN.txt; steps 3-4):
|
||||
# - a recipe from a NAME-src repo is built in gsandbox and installed
|
||||
# like a fetched package: summary and prompt BEFORE building, the
|
||||
# patches applied, the manifest the build was handed, .glacier-src;
|
||||
# - a name served by several repositories is asked about; Enter takes
|
||||
# the first, a bad answer refuses;
|
||||
# - TRAITS: recipe defaults, /etc/glacier/glacier.conf, per-package
|
||||
# files (an unknown trait refused), the user's own files for gpkg;
|
||||
# - -u stays on source, and rebuilds when the traits change;
|
||||
# - syspkg builds as BUILD_USER (required), gpkg as the user;
|
||||
# - PREFIX: a package is built for where its usr/ is linked
|
||||
# (/glacier/sys/links, the user's links, /usr for base) and still
|
||||
# packed as usr/; during the build PREFIX resolves to the layers;
|
||||
# - kept packages: an installed source build's .gpkg is kept, and a
|
||||
# later build uses it (and what it depends on) as a layer; kept
|
||||
# only while that version is installed;
|
||||
# - build output on the terminal (and in the log) unless -q/--quiet;
|
||||
# - refusals: a source with the wrong hash, a failing build, a build
|
||||
# that produces another package, a build dependency no binary repo
|
||||
# has, an untrusted per-package file, -R.
|
||||
# Needs what sandbox_alpine.sh needs, with /glacier off Docker's overlay:
|
||||
#
|
||||
# docker run --rm --security-opt seccomp=unconfined \
|
||||
# --security-opt apparmor=unconfined --security-opt systempaths=unconfined \
|
||||
# --tmpfs /glacier:exec,mode=755 \
|
||||
# -v "$PWD/tests/build_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
|
||||
FAILS=0
|
||||
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
|
||||
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
|
||||
R() {
|
||||
OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor)
|
||||
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
|
||||
echo "$OUT" | sed '/^rc=/d; s/^/ /'
|
||||
}
|
||||
has() { echo "$OUT" | grep -qF -- "$1"; }
|
||||
lacks() { ! has "$1"; }
|
||||
rc() { [ "$RC" = "$1" ]; }
|
||||
hr() { echo; echo "== $*"; }
|
||||
export PATH=/app:$PATH
|
||||
|
||||
K=/etc/glacier/keys
|
||||
CONF=/etc/glacier/repos.conf
|
||||
GCONF=/etc/glacier/glacier.conf
|
||||
XSEC=/root/extrakey.sec
|
||||
SSEC=/root/srckey.sec
|
||||
E=/srv/extra
|
||||
S=/srv/extra-src
|
||||
SRC=/srv/sources
|
||||
IDX=/glacier/sys/index/current
|
||||
BUILD=/glacier/sys/build
|
||||
BS=/srv/base-src
|
||||
mkdir -p $E $S $SRC $BS && chmod 755 /srv $E $S $SRC $BS
|
||||
|
||||
gsign -G -p $K/extrakey.pub -s $XSEC -c extrakey >/dev/null
|
||||
gsign -G -p $K/srckey.pub -s $SSEC -c srckey >/dev/null
|
||||
rconf() { # BASE-KEYS
|
||||
printf 'repos = (\n%s\n%s\n%s\n%s\n);\n' \
|
||||
" { name = \"base\"; keys = [ $1 ]; }," \
|
||||
' { name = "extra"; url = "file:///srv/extra"; keys = [ "extrakey" ]; },' \
|
||||
' { name = "extra-src"; url = "file:///srv/extra-src"; keys = [ "srckey" ]; },' \
|
||||
' { name = "base-src"; url = "file:///srv/base-src"; keys = [ "srckey" ]; }' > $CONF
|
||||
chmod 644 $CONF
|
||||
}
|
||||
rconf '"glacier-test"'
|
||||
adduser -S -D -H -s /sbin/nologin gbuild
|
||||
gconf() { printf 'KEEP_OLD = 1;\n%s\n' "$1" > $GCONF; chmod 644 $GCONF; }
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
|
||||
# The binary repo: a toolchain (Alpine's build-base, busybox for /bin/sh)
|
||||
# and a runtime dependency.
|
||||
/app/tests/mkroot -o $E -r build-base -r busybox -p busybox toolchain 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E hidep 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E loudlib 1.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E both 9.0.0 extra >/dev/null
|
||||
/app/tests/mkpkg -o $E twin 1.0.0 extra >/dev/null
|
||||
|
||||
# A source tarball: hello prints "hello", "HELLO" with LOUD=1.
|
||||
mksrc() { # NAME VER [MAKEFILE-EXTRA]
|
||||
d=/tmp/src/$1-$2
|
||||
rm -rf $d && mkdir -p $d
|
||||
printf '#include <stdio.h>\nint main(void) {\n#ifdef LOUD\n\tputs("HELLO");\n#else\n\tputs("hello");\n#endif\n\treturn 0;\n}\n' > $d/hello.c
|
||||
printf 'CFLAGS += $(if $(LOUD),-DLOUD)\n%s: hello.c\n\t$(CC) $(CFLAGS) -o %s hello.c\ninstall:\n\tinstall -D -m 755 %s $(DESTDIR)$(PREFIX)/bin/%s\n%s\n' \
|
||||
"$1" "$1" "$1" "$1" "${3:-}" > $d/Makefile
|
||||
tar czf $SRC/$1-$2.tar.gz -C /tmp/src $1-$2
|
||||
sha256sum $SRC/$1-$2.tar.gz | cut -c1-64
|
||||
}
|
||||
# A recipe bundle in the -src repo: NAME VER SHA [EXTRA-FIELDS] [BUILD-FILE]
|
||||
mkrecipe() {
|
||||
t=/tmp/rcp/$1-$2
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "%s"; VER = "%s"; REPO = "extra"; DESC = "test \\"%s\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/%s-%s.tar.gz"; SHA256 = "sha256:%s"; } );\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\n%s\n' \
|
||||
"$1" "$2" "$1" "$SRC" "$1" "$2" "$3" "${4:-}" > $t/recipe
|
||||
[ -n "${5:-}" ] && printf '%s\n' "$5" > $t/build
|
||||
rmdir $t/patches 2>/dev/null
|
||||
tar czf $S/$1-$2.grcp -C /tmp/rcp $1-$2
|
||||
}
|
||||
publish() {
|
||||
grepo -s $XSEC -r extra $E | grep -v Wrote; grepo -s $SSEC -r extra-src $S | grep -v Wrote
|
||||
grepo -s $SSEC -r base-src $BS | grep -v Wrote
|
||||
}
|
||||
|
||||
# hello 1.0.0, patched to greet glacier, depending on hidep.
|
||||
H=$(mksrc hello 1.0.0)
|
||||
t=/tmp/rcp/hello-1.0.0
|
||||
rm -rf $t && mkdir -p $t/patches
|
||||
printf 'NAME = "hello"; VER = "1.0.0"; REPO = "extra"; DESC = "say \\"hi\\""; MAINT = "m@x"; COPYRIGHT = "MIT";\nSOURCES = ( { URL = "file://%s/hello-1.0.0.tar.gz"; SHA256 = "sha256:%s"; } );\nPATCHES = [ "01-glacier.patch" ];\nBUILD = "make";\nTRAITS = ( { NAME = "loud"; DEFAULT = false; ON = [ "LOUD=1" ]; } );\nDEPS = ( { NAME = "hidep"; VER = "1.0.0"; }, { NAME = "loudlib"; VER = "1.0.0"; IF = "loud"; } );\n' "$SRC" "$H" > $t/recipe
|
||||
cat > $t/patches/01-glacier.patch <<'EOF'
|
||||
--- a/hello.c
|
||||
+++ b/hello.c
|
||||
@@ -5,3 +5,3 @@
|
||||
#else
|
||||
- puts("hello");
|
||||
+ puts("hello, glacier");
|
||||
#endif
|
||||
EOF
|
||||
tar czf $S/hello-1.0.0.grcp -C /tmp/rcp hello-1.0.0
|
||||
mkrecipe both 1.0.0 "$(mksrc both 1.0.0)"
|
||||
mkrecipe badhash 1.0.0 "$(printf '%064d' 0)"
|
||||
mksrc badhash 1.0.0 >/dev/null
|
||||
mkrecipe broken 1.0.0 "$(mksrc broken 1.0.0 'broken: ; false')" '' 'compile() { make broken; }'
|
||||
mkrecipe evil 1.0.0 "$(mksrc evil 1.0.0)" '' 'package() {
|
||||
mkdir -p /tmp/p/evil-1.0.0/files/usr/bin
|
||||
echo x > /tmp/p/evil-1.0.0/files/usr/bin/evil
|
||||
printf "PKG_NAME = \"evil\"; PKG_VER = \"1.0.0\"; PKG_REPO = \"base\"; PKG_DEPS = (); PKG_CONFS = ();\n" > /tmp/p/evil-1.0.0/manifest.gpm.cfg
|
||||
tar czf /.glacier/out/evil-1.0.0.gpkg -C /tmp/p evil-1.0.0
|
||||
exit 0
|
||||
}'
|
||||
mkrecipe core 1.0.0 "$(mksrc core 1.0.0)" '' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR$PREFIX/share" && echo "$PREFIX" > "$DESTDIR$PREFIX/share/core.prefix"
|
||||
}'
|
||||
sed -i 's/REPO = "extra"/REPO = "base"/' /tmp/rcp/core-1.0.0/recipe
|
||||
tar czf $BS/core-1.0.0.grcp -C /tmp/rcp core-1.0.0 && rm $S/core-1.0.0.grcp
|
||||
# where: a script with its data file's path compiled in. Its prepare
|
||||
# fails unless PREFIX leads to the layers during the build.
|
||||
mkrecipe where 1.0.0 "$(mksrc where 1.0.0)" 'BUILD = "hooks";' 'prepare() { [ -x "$PREFIX/bin/make" ]; }
|
||||
package() {
|
||||
mkdir -p "$DESTDIR$PREFIX/bin" "$DESTDIR$PREFIX/share/where"
|
||||
echo here > "$DESTDIR$PREFIX/share/where/data"
|
||||
printf "#!/bin/sh\ncat %s/share/where/data\n" "$PREFIX" > "$DESTDIR$PREFIX/bin/where"
|
||||
chmod 755 "$DESTDIR$PREFIX/bin/where"
|
||||
}'
|
||||
sed -i 's/^BUILD = "make";$//' /tmp/rcp/where-1.0.0/recipe && tar czf $S/where-1.0.0.grcp -C /tmp/rcp where-1.0.0
|
||||
mkrecipe split 1.0.0 "$(mksrc split 1.0.0)" '' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR/usr/share" && echo x > "$DESTDIR/usr/share/split"
|
||||
}'
|
||||
# srclib: only a recipe, depending on hidep. srcapp needs both in its
|
||||
# layers to build.
|
||||
mkrecipe srclib 1.0.0 "$(mksrc srclib 1.0.0)" 'DEPS = ( { NAME = "hidep"; VER = "1.0.0"; } );' 'package() {
|
||||
make install PREFIX="$PREFIX" DESTDIR="$DESTDIR"
|
||||
mkdir -p "$DESTDIR$PREFIX/share/srclib" && echo yes > "$DESTDIR$PREFIX/share/srclib/marker"
|
||||
}'
|
||||
mkrecipe srcapp 1.0.0 "$(mksrc srcapp 1.0.0)" 'DEPS = ( { NAME = "srclib"; VER = "1.0.0"; } );' 'prepare() { [ -f /usr/share/srclib/marker ] && [ -x /usr/bin/hidep ]; }'
|
||||
mkrecipe twin 1.0.0 "$(mksrc twin 1.0.0)"
|
||||
mkrecipe needy 1.0.0 "$(mksrc needy 1.0.0)" 'BUILD_DEPS = ( { NAME = "nothere"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
|
||||
syspkg -n >/dev/null
|
||||
yes_to() { sh -c "printf '$1' | $2"; }
|
||||
|
||||
hr "syspkg -f of a recipe"
|
||||
R yes_to 'y\n' 'syspkg -f hello'
|
||||
check "built and installed" has "[✓] Completed with no errors."
|
||||
check "...exit 0" rc 0
|
||||
check "the summary marks the build, its traits off by default" has " * extra-src::hello 1.0.0 [-loud] (build)"
|
||||
check "...and the binary dependency" has " * extra::hidep 1.0.0 (dependency)"
|
||||
check "...the prompt came before the build" sh -c 'echo "$0" | grep -n "Proceed\|Building extra-src" | head -1 | grep -q Proceed' "$OUT"
|
||||
check "the build is announced" has "[i] Building extra-src::hello 1.0.0 [-loud]..."
|
||||
check "...and done" has "[✓] Built extra-src::hello 1.0.0."
|
||||
check "installed as extra::hello" grep -q "^extra::hello::1.0.0::explicit$" $IDX
|
||||
check "...with its dependency, auto" grep -q "^extra::hidep::1.0.0::auto$" $IDX
|
||||
check "...not the one only loud needs" sh -c "! grep -q loudlib $IDX"
|
||||
check "...the patch applied" [ "$(/glacier/sys/links/bin/hello)" = "hello, glacier" ]
|
||||
V=/glacier/sys/store/extra/hello/hello-1.0.0
|
||||
check "...recorded as built from extra-src" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...its manifest has PKG_TRAITS" grep -qF 'PKG_TRAITS = [ "-loud" ];' $V/.glacier-manifest
|
||||
check "...and the recipe's DESC, quoted" grep -qF 'PKG_DESC = "say \"hi\"";' $V/.glacier-manifest
|
||||
check "the layer belongs to BUILD_USER" [ "$(stat -c %U $BUILD/layers/toolchain-1.0.0-*)" = gbuild ]
|
||||
check "...which owns layers/, 0700" [ "$(stat -c '%U %a' $BUILD/layers)" = "gbuild 700" ]
|
||||
check "the build's in/work/out dirs are gone" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
check "the log is kept" grep -q "==> Done" $BUILD/logs/hello-1.0.0.log
|
||||
check "...and the build's output was on the terminal" has "==> Applying 01-glacier.patch"
|
||||
check "...the sources cached by hash" [ -f $BUILD/sources/$H ]
|
||||
check "the cached bundle is deleted" [ ! -e /glacier/sys/cache/extra-src/hello-1.0.0.grcp ]
|
||||
check "the store copy is root's, as any install" [ "$(stat -c %u /glacier/sys/store/extra/hello/hello-1.0.0/usr/bin/hello)" = 0 ]
|
||||
|
||||
hr "declining builds nothing"
|
||||
rm $BUILD/logs/hello-1.0.0.log
|
||||
R yes_to 'n\n' 'syspkg -f -O hello'
|
||||
check "nothing built" sh -c "[ ! -e $BUILD/logs/hello-1.0.0.log ]"
|
||||
check "...nothing said about building" lacks "Building"
|
||||
|
||||
hr "traits"
|
||||
mkdir -p /etc/glacier/config
|
||||
printf 'TRAITS = [ "lod" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "an unknown trait in a per-package file is refused" has "[x] /etc/glacier/config/hello.conf: hello has no trait lod."
|
||||
check "...exit 1, before the prompt" sh -c "[ $RC = 1 ]" && check "...no prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud", "x11" ];'
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "the per-package file beats glacier.conf: nothing to rebuild" has "Nothing to do."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "glacier.conf turns loud on (x11, unknown to the recipe, ignored): a rebuild" has " * extra-src::hello 1.0.0 (traits) [+loud] (build)"
|
||||
check "...installed" rc 0
|
||||
check "...built with LOUD" [ "$(/glacier/sys/links/bin/hello)" = "HELLO" ]
|
||||
check "...with the dependency loud needs" grep -q "^extra::loudlib::1.0.0::auto$" $IDX
|
||||
check "...in its manifest" grep -qF '{ PKG_NAME = "loudlib"; PKG_VER = "1.0.0"; }' $V/.glacier-manifest
|
||||
check "...still marked as built from source" [ "$(cat $V/.glacier-src)" = extra-src ]
|
||||
check "...PKG_TRAITS updated" grep -qF 'PKG_TRAITS = [ "loud" ];' $V/.glacier-manifest
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "unchanged traits: nothing to do" has "Nothing to do."
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/hello.conf
|
||||
chmod 666 /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a world-writable per-package file is refused" has "[x] Refusing /etc/glacier/config/hello.conf: /etc/glacier/config/hello.conf is not root-owned, or is group/world-writable."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
printf 'TRAITS = [ "loud" ]; COLOR = "red";\n' > /etc/glacier/config/hello.conf
|
||||
R yes_to 'y\n' 'syspkg -u'
|
||||
check "a per-package file with an unknown key is malformed" has "[x] Malformed /etc/glacier/config/hello.conf."
|
||||
rm /etc/glacier/config/hello.conf
|
||||
|
||||
hr "-u stays on source"
|
||||
H11=$(mksrc hello 1.1.0)
|
||||
sed "s/1\.0\.0/1.1.0/g; s/$H/$H11/" /tmp/rcp/hello-1.0.0/recipe > /tmp/r.new
|
||||
rm -rf /tmp/rcp/hello-1.1.0 && cp -a /tmp/rcp/hello-1.0.0 /tmp/rcp/hello-1.1.0 && mv /tmp/r.new /tmp/rcp/hello-1.1.0/recipe
|
||||
sed -i 's/VER = "1.1.0"; }/VER = "1.0.0"; }/g; s/VER = "1.1.0"; IF/VER = "1.0.0"; IF/' /tmp/rcp/hello-1.1.0/recipe
|
||||
tar czf $S/hello-1.1.0.grcp -C /tmp/rcp hello-1.1.0
|
||||
/app/tests/mkpkg -o $E hello 2.0.0 extra >/dev/null
|
||||
publish
|
||||
R yes_to 'y\n' 'syspkg -u hello'
|
||||
check "a newer recipe is built, not extra's binary 2.0.0" has " * extra-src::hello 1.0.0 -> 1.1.0 [+loud] (build)"
|
||||
check "...installed" grep -q "^extra::hello::1.1.0::explicit$" $IDX
|
||||
check "...the old version backed up" [ -f /glacier/sys/backup/extra/hello/hello-1.0.0.gpkg ]
|
||||
check "...its new package kept for builds" [ -f $BUILD/packages/extra@hello-1.1.0.gpkg ]
|
||||
check "...the old one's deleted" [ ! -e $BUILD/packages/extra@hello-1.0.0.gpkg ]
|
||||
check "...still on source" [ "$(cat /glacier/sys/store/extra/hello/hello-1.1.0/.glacier-src)" = extra-src ]
|
||||
|
||||
hr "PREFIX"
|
||||
R yes_to 'y\n' 'syspkg -f where'
|
||||
check "built for /glacier/sys/links: finds its data through the links" [ "$(/glacier/sys/links/bin/where)" = here ]
|
||||
check "...the path compiled in" grep -q "^cat /glacier/sys/links/share/where/data$" /glacier/sys/links/bin/where
|
||||
check "...packed as usr/, as any package" [ -f /glacier/sys/store/extra/where/where-1.0.0/usr/share/where/data ]
|
||||
check "...nothing else at the top" [ "$(ls /glacier/sys/store/extra/where/where-1.0.0)" = usr ]
|
||||
R yes_to 'y\n' 'syspkg -f split'
|
||||
check "a build installing into PREFIX and /usr both fails" has "[x] Building extra-src::split 1.0.0 failed."
|
||||
check "...the log says why" grep -q "split installed into both /glacier/sys/links and /usr" $BUILD/logs/split-1.0.0.log
|
||||
|
||||
hr "kept packages"
|
||||
P=$BUILD/packages
|
||||
check "a source build's package is kept, root's 0644" [ "$(stat -c '%u %a' $P/extra@where-1.0.0.gpkg)" = "0 644" ]
|
||||
check "...byte for byte what was installed" sh -c "tar xzOf $P/extra@where-1.0.0.gpkg where-1.0.0/files/usr/bin/where | cmp -s - /glacier/sys/store/extra/where/where-1.0.0/usr/bin/where"
|
||||
check "...not a failed one" [ ! -e $P/extra@split-1.0.0.gpkg ]
|
||||
R yes_to 'y\n' 'syspkg -q -f srclib'
|
||||
check "srclib (no binary anywhere) built and installed" rc 0
|
||||
check "...-q: no build output on the terminal" lacks "==> "
|
||||
check "...but the build is still announced" has "[✓] Built extra-src::srclib 1.0.0."
|
||||
check "...and logged" grep -q "==> Done" $BUILD/logs/srclib-1.0.0.log
|
||||
R yes_to 'y\n' 'syspkg --quiet -f srcapp'
|
||||
check "--quiet too" lacks "==> "
|
||||
check "a build needing an installed source build: built" has "[✓] Built extra-src::srcapp 1.0.0."
|
||||
check "...installed" grep -q "^extra::srcapp::1.0.0::explicit$" $IDX
|
||||
check "...the kept package was its layer" sh -c "ls $BUILD/layers | grep -q '^srclib-1.0.0-'"
|
||||
printf 'TRAITS = [ "-loud" ];\n' > /etc/glacier/config/srcapp.conf
|
||||
R yes_to 'y\n' 'syspkg -u srcapp'
|
||||
check "-u rebuilds it (traits changed)" has " * extra-src::srcapp 1.0.0 (traits) [-loud] (build)"
|
||||
check "...exit 0" rc 0
|
||||
mv $P/extra@srclib-1.0.0.gpkg /root/kept.bak
|
||||
rm /etc/glacier/config/srcapp.conf
|
||||
R yes_to 'y\n' 'syspkg -u srcapp'
|
||||
mv /root/kept.bak $P/extra@srclib-1.0.0.gpkg
|
||||
check "without its kept package, srclib can't be a layer" has "[x] extra-src::srcapp 1.0.0 needs srclib 1.0.0 or later to build, and no binary repository has it."
|
||||
R yes_to 'y\n' 'syspkg -x srcapp'
|
||||
check "removing srcapp deletes its kept package" [ ! -e $P/extra@srcapp-1.0.0.gpkg ]
|
||||
check "...not srclib's, still installed" [ -f $P/extra@srclib-1.0.0.gpkg ]
|
||||
R yes_to 'y\n' 'syspkg -x srclib'
|
||||
check "removing srclib deletes its own" [ ! -e $P/extra@srclib-1.0.0.gpkg ]
|
||||
check "...others stay" [ -f $P/extra@where-1.0.0.gpkg ]
|
||||
R yes_to '2\ny\n' 'syspkg -f twin'
|
||||
check "twin built from source: kept" [ -f $P/extra@twin-1.0.0.gpkg ]
|
||||
R yes_to '1\ny\n' 'syspkg -f -O twin'
|
||||
check "the same version reinstalled from the binary repo" [ "$(/glacier/sys/links/bin/twin)" = "twin 1.0.0 says hi" ]
|
||||
check "...no longer kept" [ ! -e $P/extra@twin-1.0.0.gpkg ]
|
||||
|
||||
hr "the repository question"
|
||||
R yes_to '\ny\n' 'syspkg -f both'
|
||||
check "both repos serving a name: asked" has "[i] both is available from more than one repository:"
|
||||
check "...the binary first (repos.conf order)" has " 1) extra::both 9.0.0"
|
||||
check "...then the recipe" has " 2) extra-src::both 1.0.0 [+loud] (build)"
|
||||
check "...Enter takes the first" grep -q "^extra::both::9.0.0::explicit$" $IDX
|
||||
check "...not marked as built" [ ! -e /glacier/sys/store/extra/both/both-9.0.0/.glacier-src ]
|
||||
check "...nor kept" [ ! -e $BUILD/packages/extra@both-9.0.0.gpkg ]
|
||||
R yes_to '2\ny\n' 'syspkg -f -O both'
|
||||
check "2 builds the recipe (replacing the binary under -O)" grep -q "^extra::both::1.0.0::explicit$" $IDX
|
||||
check "...marked as built" [ "$(cat /glacier/sys/store/extra/both/both-1.0.0/.glacier-src)" = extra-src ]
|
||||
check "...and kept" [ -f $BUILD/packages/extra@both-1.0.0.gpkg ]
|
||||
R yes_to '3\n' 'syspkg -f -O both'
|
||||
check "an answer out of range is refused" has "[x] Invalid selection."
|
||||
check "...exit 1" rc 1
|
||||
R yes_to 'x\n' 'syspkg -f -O both'
|
||||
check "a non-number is refused" has "[x] Invalid selection."
|
||||
R sh -c 'syspkg -f -O both < /dev/null'
|
||||
check "end of input is refused" has "[x] Invalid selection."
|
||||
|
||||
hr "refused builds"
|
||||
R yes_to 'y\n' 'syspkg -f badhash'
|
||||
check "a source with the wrong hash" has "[x] A source of extra-src::badhash 1.0.0 does not match its hash."
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q badhash $IDX"
|
||||
R yes_to 'y\n' 'syspkg -f broken'
|
||||
check "a failing build" has "[x] Building extra-src::broken 1.0.0 failed. Log: $BUILD/logs/broken-1.0.0.log"
|
||||
check "...exit 1, nothing installed" sh -c "[ $RC = 1 ] && ! grep -q broken $IDX"
|
||||
check "...the log says why" grep -q "Error\|error" $BUILD/logs/broken-1.0.0.log
|
||||
check "...its dirs removed" [ -z "$(ls $BUILD | grep -E '^(in|work|out)-')" ]
|
||||
R yes_to 'y\n' 'syspkg -f evil'
|
||||
check "a build that produces another package" has "[x] extra-src::evil 1.0.0: the build produced another package."
|
||||
check "...nothing installed" sh -c "! grep -q evil $IDX && [ ! -e /usr/bin/evil ]"
|
||||
# An index entry that disagrees with its bundle's recipe (signed all
|
||||
# the same): the recipe has a dependency the index left out.
|
||||
mkrecipe liar 1.0.0 "$(mksrc liar 1.0.0)" 'DEPS = ( { NAME = "hidep"; VER = "1.0.0"; } );'
|
||||
publish
|
||||
SER=$(sed -n 's/^SERIAL = \([0-9]*\)L;$/\1/p' $S/repo.idx)
|
||||
awk -v s=$((SER + 1)) '/^SERIAL/ { print "SERIAL = " s "L;"; next } /NAME = "liar"/ { liar = 1 } liar && /DEPS = / { sub(/DEPS = \(.*\);/, "DEPS = ();"); liar = 0 } { print }' $S/repo.idx > /tmp/idx && mv /tmp/idx $S/repo.idx
|
||||
gsign -S -s $SSEC -m $S/repo.idx >/dev/null
|
||||
R yes_to 'y\n' 'syspkg -f liar'
|
||||
check "a recipe that disagrees with its index entry" has "[x] extra-src::liar 1.0.0 does not match its index."
|
||||
check "...nothing installed" sh -c "! grep -q liar $IDX"
|
||||
chmod 666 /app/lib/build-driver.sh
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
chmod 644 /app/lib/build-driver.sh
|
||||
check "a world-writable build driver is refused" has "[x] Refusing /app/lib/build-driver.sh: /app/lib/build-driver.sh is not root-owned, or is group/world-writable."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
R yes_to 'y\n' 'syspkg -f needy'
|
||||
check "a build dependency no binary repo has" has "[x] extra-src::needy 1.0.0 needs nothere 1.0.0 or later to build, and no binary repository has it."
|
||||
check "...refused before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ];'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER unset: refused" has "[x] BUILD_USER is not set in /etc/glacier/glacier.conf."
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
gconf 'BUILD_USER = "gbuild";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_ROOT unset: refused" has "[x] BUILD_ROOT is not set in /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "root";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER = root: refused" has "[x] BUILD_USER in /etc/glacier/glacier.conf must not be root."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "nosuchuser";'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "BUILD_USER not a user: refused" has "[x] BUILD_USER nosuchuser in /etc/glacier/glacier.conf is not a user."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; JOBS = 0;'
|
||||
R yes_to '2\ny\n' 'syspkg -f -O hello'
|
||||
check "JOBS = 0: glacier.conf is malformed" has "[x] Malformed /etc/glacier/glacier.conf."
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild";'
|
||||
mkdir -p /mnt/t && syspkg -R /mnt/t -n >/dev/null
|
||||
cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/ 2>/dev/null || { mkdir -p /mnt/t/etc/glacier && cp -a /etc/glacier/repos.conf /etc/glacier/keys /mnt/t/etc/glacier/; }
|
||||
R yes_to '2\ny\n' 'syspkg -R /mnt/t -f both'
|
||||
check "building under -R is refused" has "[x] Building from source under -R is not supported."
|
||||
|
||||
hr "base-src and the machine key"
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "a base build without the machine key trusted for base is refused" has '[x] base-src::core 1.0.0 [-loud] (build) needs the key "local" trusted for base.'
|
||||
check "...before the prompt" lacks "Proceed?"
|
||||
check "...having made the machine key" has "[i] Made the machine key /etc/glacier/keys/local.pub."
|
||||
check "...its public half 0644" [ "$(stat -c '%u %a' /etc/glacier/keys/local.pub)" = "0 644" ]
|
||||
check "...its secret half root's 0600" [ "$(stat -c '%u %a' /glacier/sys/keys/local.sec)" = "0 600" ]
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "...made once" lacks "Made the machine key"
|
||||
rconf '"glacier-test", "local"'
|
||||
R yes_to 'y\n' 'syspkg -f core'
|
||||
check "listed for base: built and installed" grep -q "^base::core::1.0.0::explicit$" $IDX
|
||||
check "...linked into /usr, as base is" [ "$(/usr/bin/core)" = hello ]
|
||||
check "...marked as built from base-src" [ "$(cat /glacier/sys/store/base/core/core-1.0.0/.glacier-src)" = base-src ]
|
||||
check "...built for /usr" [ "$(cat /usr/share/core.prefix)" = /usr ]
|
||||
mv /glacier/sys/keys/local.sec /root/local.sec.bak
|
||||
printf 'TRAITS = [ "loud" ];\n' > /etc/glacier/config/core.conf
|
||||
R yes_to 'y\n' 'syspkg -u core'
|
||||
rm /etc/glacier/config/core.conf
|
||||
rconf '"glacier-test"'
|
||||
mv /root/local.sec.bak /glacier/sys/keys/local.sec
|
||||
check "a missing secret half is refused" has "[x] /etc/glacier/keys/local.pub and /glacier/sys/keys/local.sec must both exist, or neither."
|
||||
|
||||
hr "gpkg: the user's own build"
|
||||
U=/glacier/usr/1000
|
||||
su glacier -c 'doas /app/gstore -n' >/dev/null 2>&1
|
||||
check "gstore -n makes the user's config dir" [ "$(stat -c '%U %a' $U/config)" = "glacier 700" ]
|
||||
gconf 'BUILD_ROOT = [ "toolchain" ]; BUILD_USER = "gbuild"; TRAITS = [ "loud" ];'
|
||||
R su glacier -c "printf '2\ny\n' | /app/gpkg -f both"
|
||||
check "gpkg builds it" has "[✓] Built extra-src::both 1.0.0."
|
||||
check "...the system's TRAITS apply" has "[i] Building extra-src::both 1.0.0 [+loud]..."
|
||||
check "...installed for the user" grep -q "^extra::both::1.0.0::explicit$" $U/index/current
|
||||
check "...the layers are the user's" [ "$(stat -c %U $U/build/layers/toolchain-1.0.0-*)" = glacier ]
|
||||
check "...the log too" [ "$(stat -c '%U %a' $U/build/logs/both-1.0.0.log)" = "glacier 600" ]
|
||||
check "...it runs" [ "$(su glacier -c "$U/links/bin/both")" = HELLO ]
|
||||
check "...kept in the user's base, 0600" [ "$(stat -c '%U %a' $U/build/packages/extra@both-1.0.0.gpkg)" = "glacier 600" ]
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f where"
|
||||
check "gpkg shows the build's output" has "==> Packing where-1.0.0.gpkg"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -fqO where"
|
||||
check "gpkg -q (in a bundle) hides it" lacks "==> "
|
||||
check "...and rebuilds" has "[✓] Built extra-src::where 1.0.0."
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg --quiet -fO where"
|
||||
check "gpkg --quiet too" sh -c '! echo "$0" | grep -q "==> " && echo "$0" | grep -q "Built extra-src::where"' "$OUT"
|
||||
R syspkg -h
|
||||
check "syspkg -h lists -q" has "{-q --quiet} Hide build output"
|
||||
R su glacier -c "/app/gpkg -h"
|
||||
check "gpkg -h lists -q" has "{-q --quiet} Hide build output"
|
||||
check "gpkg builds for the user's links" [ "$(su glacier -c "$U/links/bin/where")" = here ]
|
||||
check "...the path compiled in" grep -q "^cat $U/links/share/where/data$" $U/links/bin/where
|
||||
su glacier -c "printf 'TRAITS = [ \"-loud\" ];\n' > $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "the user's per-package file wins: rebuilt without loud" has " * extra-src::both 1.0.0 (traits) [-loud] (build)"
|
||||
check "...installed" [ "$(su glacier -c "$U/links/bin/both")" = hello ]
|
||||
su glacier -c "printf 'TRAITS = [ \"loud\" ];\n' > $U/config/glacier.conf; rm $U/config/both.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "...the user's glacier.conf too" has "[+loud] (build)"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -f core"
|
||||
check "gpkg never builds from base-src" has "[x] core is not in any repository."
|
||||
su glacier -c "printf 'BUILD_USER = \"x\";\n' > $U/config/glacier.conf"
|
||||
R su glacier -c "printf 'y\n' | /app/gpkg -u"
|
||||
check "BUILD_USER in a user's glacier.conf is malformed" has "[x] Malformed $U/config/glacier.conf."
|
||||
|
||||
echo
|
||||
if [ $FAILS -eq 0 ]; then echo "ALL PASSED"; else echo "$FAILS FAILED"; fi
|
||||
[ $FAILS -eq 0 ]
|
||||
Executable
+57
@@ -0,0 +1,57 @@
|
||||
#!/bin/sh
|
||||
# buildenv: set up THIS container (glacier-alpine_root, started with
|
||||
# `./run_docker.sh -b Dockerfile.alpine_root`) to build packages from
|
||||
# source, with local repositories:
|
||||
#
|
||||
# buildenv [RECIPES]
|
||||
#
|
||||
# RECIPES a directory of NAME/ recipe dirs (default
|
||||
# /recipes/extra-src, which run_docker.sh -b mounts)
|
||||
#
|
||||
# It makes, once: the system (syspkg -n), the build user gbuild, a
|
||||
# signing key "repokey", /srv/extra with a toolchain package made from
|
||||
# Alpine's build-base (tests/mkroot; Everest has no toolchain packages
|
||||
# yet), BUILD_ROOT/BUILD_USER/JOBS in /etc/glacier/glacier.conf, and
|
||||
# /etc/glacier/repos.conf listing extra and extra-src. Every run packs
|
||||
# RECIPES into /srv/extra-src (tests/mkgrcp), signs both indexes and
|
||||
# syncs, so rerun it after editing a recipe.
|
||||
|
||||
set -e
|
||||
recipes=${1:-/recipes/extra-src}
|
||||
t=$(dirname "$(realpath "$0")")
|
||||
export PATH="/app:$PATH"
|
||||
|
||||
[ -d "$recipes" ] || { echo "buildenv: no recipe directory $recipes" >&2; exit 1; }
|
||||
[ -d /glacier/sys ] || syspkg -n
|
||||
id gbuild >/dev/null 2>&1 || adduser -S -D -H gbuild
|
||||
mkdir -p /srv/extra /srv/extra-src
|
||||
chmod 755 /srv /srv/extra /srv/extra-src
|
||||
|
||||
if [ ! -f /srv/extra/toolchain-1.0.0.gpkg ]; then
|
||||
echo "Packing the toolchain (Alpine's build-base)..."
|
||||
"$t/mkroot" -o /srv/extra -r build-base -r busybox -p busybox toolchain 1.0.0 extra
|
||||
fi
|
||||
|
||||
rm -f /srv/extra-src/*.grcp
|
||||
"$t/mkgrcp" -o /srv/extra-src "$recipes"/*/
|
||||
|
||||
[ -f /root/repokey.sec ] ||
|
||||
gsign -G -p /etc/glacier/keys/repokey.pub -s /root/repokey.sec -c repokey
|
||||
grepo -s /root/repokey.sec -r extra /srv/extra
|
||||
grepo -s /root/repokey.sec -r extra-src /srv/extra-src
|
||||
|
||||
cat > /etc/glacier/repos.conf <<'EOR'
|
||||
repos = (
|
||||
{ name = "base"; keys = [ "glacier-test" ]; },
|
||||
{ name = "extra"; url = "file:///srv/extra"; keys = [ "repokey" ]; },
|
||||
{ name = "extra-src"; url = "file:///srv/extra-src"; keys = [ "repokey" ]; }
|
||||
);
|
||||
EOR
|
||||
chmod 644 /etc/glacier/repos.conf
|
||||
|
||||
c=/etc/glacier/glacier.conf
|
||||
grep -q '^BUILD_ROOT' $c || echo 'BUILD_ROOT = [ "toolchain" ];' >> $c
|
||||
grep -q '^BUILD_USER' $c || echo 'BUILD_USER = "gbuild";' >> $c
|
||||
grep -q '^JOBS' $c || echo "JOBS = $(nproc);" >> $c
|
||||
|
||||
syspkg -y
|
||||
Loaded 100 of 159 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user