Files
geminux/scripts/bootstrap-rootfs-container.sh
T

218 lines
7.2 KiB
Bash
Raw Normal View History

#!/bin/sh
# Geminux rootfs bootstrap, container side.
# Runs inside alpine:3.24 with /rootfs mounted and /geminux read-only.
set -eu
ROOTFS="/rootfs"
REPO="/geminux"
PKGS_FILE="$REPO/scripts/rootfs-packages.txt"
DEVICE="${TARGET_DEVICE:-gemini-x27}"
PASSWORD="${GEMINUX_USER_PASSWORD:-geminux}"
HOST_UID="${HOST_UID:-0}"
HOST_GID="${HOST_GID:-0}"
BOOTSTRAP_VERSION="${BOOTSTRAP_VERSION:-1}"
PKG_HASH="${PKG_HASH:-unknown}"
echo "============================================================"
echo "Bootstrapping Geminux aarch64 rootfs for $DEVICE"
echo "============================================================"
echo "[1/8] Configuring pure Alpine Edge apk repositories and trusted keys..."
cat > /etc/apk/repositories <<EOF
https://dl-cdn.alpinelinux.org/alpine/edge/main
https://dl-cdn.alpinelinux.org/alpine/edge/community
https://dl-cdn.alpinelinux.org/alpine/edge/testing
EOF
mkdir -p /etc/apk/keys
cp "$REPO"/keys/alpine-devel@*.rsa.pub /etc/apk/keys/
if [ -f "$REPO/keys/geminux-local.rsa.pub" ]; then
cp "$REPO/keys/geminux-local.rsa.pub" /etc/apk/keys/
fi
mkdir -p "$ROOTFS/etc/apk/keys" "$ROOTFS/lib/apk/db" "$ROOTFS/var/cache/apk"
cp /etc/apk/repositories "$ROOTFS/etc/apk/repositories"
rm -f "$ROOTFS/etc/apk/keys/"*.rsa.pub
cp "$REPO"/keys/alpine-devel@*.rsa.pub "$ROOTFS/etc/apk/keys/"
if [ -f "$REPO/keys/geminux-local.rsa.pub" ]; then
cp "$REPO/keys/geminux-local.rsa.pub" "$ROOTFS/etc/apk/keys/"
fi
echo "[2/8] Installing host helper tools..."
apk add --no-cache python3 kmod openssl glib >/dev/null
echo "[3/8] Installing the Geminux aarch64 package set..."
PKGS="$(grep -v '^[[:space:]]*#' "$PKGS_FILE" | grep -v '^[[:space:]]*$' | tr '\n' ' ')"
if [ -f "$ROOTFS/etc/apk/world" ]; then
grep -v -E '^geminux-(kernel-modules|xlibre|suckless|dunst|ui|config)(@|=|$)' \
"$ROOTFS/etc/apk/world" > "$ROOTFS/etc/apk/world.tmp" || true
mv "$ROOTFS/etc/apk/world.tmp" "$ROOTFS/etc/apk/world"
fi
"$REPO/scripts/prune-rootfs-packages.sh" "$ROOTFS" world
if [ ! -f "$ROOTFS/lib/apk/db/installed" ]; then
# shellcheck disable=SC2086
apk --root "$ROOTFS" --arch aarch64 add --initdb --no-scripts $PKGS >/dev/null
else
# shellcheck disable=SC2086
apk --root "$ROOTFS" --arch aarch64 add --no-scripts $PKGS >/dev/null
fi
"$REPO/scripts/prune-rootfs-packages.sh" "$ROOTFS" purge
echo "[4/8] Creating system accounts and default credentials..."
PW_HASH="$(openssl passwd -6 -salt geminux "$PASSWORD")"
python3 - "$ROOTFS" "$PW_HASH" <<'PYEOF'
import os
import sys
root = sys.argv[1]
pw_hash = sys.argv[2]
def load(name):
path = os.path.join(root, "etc", name)
if not os.path.exists(path):
return []
with open(path, "r") as handle:
return [line.rstrip("\n") for line in handle if line.strip()]
def store(name, rows):
path = os.path.join(root, "etc", name)
with open(path, "w") as handle:
handle.write("\n".join(rows) + "\n")
passwd = load("passwd")
group = load("group")
shadow = load("shadow")
system_users = [
"klogd:x:100:101:klogd:/dev/null:/sbin/nologin",
"chrony:x:101:102:chrony:/dev/null:/sbin/nologin",
"messagebus:x:102:103:messagebus:/dev/null:/sbin/nologin",
"dnsmasq:x:103:104:dnsmasq:/dev/null:/sbin/nologin",
"polkitd:x:105:110:polkitd:/var/empty:/sbin/nologin",
"greetd:x:107:112:greetd:/var/lib/greetd:/sbin/nologin",
"user:x:10000:10000::/home/user:/bin/ash",
]
for entry in system_users:
name = entry.split(":")[0]
if not any(row.startswith(name + ":") for row in passwd):
passwd.append(entry)
system_groups = [
"klogd:x:101:klogd",
"chrony:x:102:chrony",
"messagebus:x:103:messagebus",
"dnsmasq:x:104:dnsmasq",
"plugdev:x:106:user",
"pipewire:x:107:user",
"seat:x:108:user",
"polkitd:x:110:polkitd",
"greetd:x:112:greetd",
"user:x:10000:",
]
for entry in system_groups:
name = entry.split(":")[0]
if not any(row.startswith(name + ":") for row in group):
group.append(entry)
member_groups = (
"wheel", "audio", "input", "netdev", "plugdev", "video", "seat",
"dialout", "tty", "pipewire",
)
for index, row in enumerate(group):
fields = row.split(":")
if fields[0] not in member_groups:
continue
members = [item for item in fields[3].split(",") if item]
if "user" not in members:
members.append("user")
fields[3] = ",".join(members)
group[index] = ":".join(fields)
for entry in system_users:
name = entry.split(":")[0]
if name == "user":
continue
if not any(row.startswith(name + ":") for row in shadow):
shadow.append(name + ":!:::::::")
if not any(row.startswith("user:") for row in shadow):
shadow.append("user:" + pw_hash + ":::::::")
for index, row in enumerate(shadow):
if row.startswith("root:"):
fields = row.split(":")
if len(fields) > 1 and fields[1] == "":
fields[1] = pw_hash
shadow[index] = ":".join(fields)
store("passwd", passwd)
store("group", group)
store("shadow", shadow)
PYEOF
mkdir -p "$ROOTFS/home/user" "$ROOTFS/etc/geminux"
chown -R 10000:10000 "$ROOTFS/home/user"
chown 0:42 "$ROOTFS/etc/shadow"
chmod 640 "$ROOTFS/etc/shadow"
chown 0:0 "$ROOTFS/etc/geminux"
chmod 0755 "$ROOTFS/etc/geminux"
echo "[5/8] Writing base system configuration..."
"$REPO/scripts/set-geminux-hostname.sh" "$ROOTFS" "$DEVICE"
mkdir -p "$ROOTFS/etc"
printf '%s\n' "LANG=en_US.UTF-8" "LC_ALL=en_US.UTF-8" > "$ROOTFS/etc/locale.conf"
if [ -f "$ROOTFS/usr/share/zoneinfo/UTC" ]; then
cp "$ROOTFS/usr/share/zoneinfo/UTC" "$ROOTFS/etc/localtime"
fi
touch "$ROOTFS/etc/apk/interactive"
mkdir -p "$ROOTFS/run/openrc" "$ROOTFS/dev/pts" "$ROOTFS/dev/shm" "$ROOTFS/tmp" "$ROOTFS/var/tmp"
touch "$ROOTFS/run/openrc/shutdowntime"
: > "$ROOTFS/dev/null"
chmod 666 "$ROOTFS/dev/null"
if [ -e "$ROOTFS/sbin/losetup" ] && [ ! -e "$ROOTFS/usr/sbin/losetup" ]; then
ln -sf /sbin/losetup "$ROOTFS/usr/sbin/losetup"
fi
if [ ! -s "$ROOTFS/etc/machine-id" ]; then
tr -d '-' < /proc/sys/kernel/random/uuid > "$ROOTFS/etc/machine-id"
printf '\n' >> "$ROOTFS/etc/machine-id"
fi
mkdir -p "$ROOTFS/etc/sudoers.d"
cat > "$ROOTFS/etc/sudoers.d/wheel" <<EOF
%wheel ALL=(ALL:ALL) ALL
EOF
chmod 440 "$ROOTFS/etc/sudoers.d/wheel"
echo "[6/8] Enabling OpenRC runlevels..."
mkdir -p "$ROOTFS/etc/runlevels/sysinit" "$ROOTFS/etc/runlevels/boot" \
"$ROOTFS/etc/runlevels/default" "$ROOTFS/etc/runlevels/shutdown"
enable_service() {
runlevel="$1"
service="$2"
if [ -e "$ROOTFS/etc/init.d/$service" ]; then
ln -sf "/etc/init.d/$service" "$ROOTFS/etc/runlevels/$runlevel/$service"
fi
}
for svc in devfs dmesg udev udev-trigger udev-settle; do
enable_service sysinit "$svc"
done
for svc in bootmisc hostname loopback modules sysctl tmpfs zram-init; do
enable_service boot "$svc"
done
for svc in cgroups chronyd dbus elogind local modemmanager networkmanager \
sshd tinydm udev-postmount wpa_supplicant; do
enable_service default "$svc"
done
for svc in killprocs mount-ro savecache; do
enable_service shutdown "$svc"
done
rm -f "$ROOTFS/etc/runlevels/boot/hwclock"
echo "[7/8] Applying the Geminux overlay and prebuilt artifacts..."
"$REPO/scripts/apply-geminux-overlay.sh" "$ROOTFS" "$DEVICE"
echo "[8/8] Finalizing rootfs ownership and bootstrap marker..."
printf '%s:%s\n' "$BOOTSTRAP_VERSION" "$PKG_HASH" > "$ROOTFS/etc/geminux/rootfs-bootstrap.done"
chown -R "$HOST_UID:$HOST_GID" "$ROOTFS"
echo "Geminux rootfs bootstrap completed for $DEVICE."