Files
geminux/rebuild-image.sh
T

190 lines
6.0 KiB
Bash
Raw Normal View History

#!/bin/sh
# Autonomous Geminux full image builder (boot, recovery, signed @geminux APKs, rootfs ext4 + sparse).
set -eu
BASE_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOTFS_DIR="${ROOTFS_DIR:-$BASE_DIR/rootfs}"
IMAGES_DIR="$BASE_DIR/images"
REBUILD_ALL=0
FORCE_BOOTSTRAP=0
SKIP_APKS=0
for arg in "$@"; do
case "$arg" in
--rebuild-all)
REBUILD_ALL=1
;;
--force-bootstrap)
FORCE_BOOTSTRAP=1
;;
--skip-apks)
SKIP_APKS=1
;;
--help|-h)
echo "Usage: $0 [--rebuild-all] [--force-bootstrap] [--skip-apks]"
exit 0
;;
*)
echo "Unknown option: $arg" >&2
exit 1
;;
esac
done
mkdir -p "$ROOTFS_DIR" "$IMAGES_DIR"
echo "============================================================"
echo "Rebuilding Geminux Distribution Images"
echo "============================================================"
if [ "$REBUILD_ALL" -eq 1 ] || [ ! -f "$IMAGES_DIR/boot-geminux.img" ] || \
[ ! -f "$BASE_DIR/prebuilt/kernel/boot/mt6797-gemini-pda-geminux.dtb" ]; then
"$BASE_DIR/scripts/build-kernel.sh"
fi
if [ "$REBUILD_ALL" -eq 1 ] || [ ! -x "$BASE_DIR/prebuilt/xlibre/usr/bin/Xorg" ]; then
"$BASE_DIR/scripts/build-xlibre.sh"
fi
if [ "$REBUILD_ALL" -eq 1 ] || [ ! -x "$BASE_DIR/prebuilt/suckless/usr/bin/dwm" ]; then
"$BASE_DIR/scripts/build-suckless.sh"
fi
if [ "$REBUILD_ALL" -eq 1 ] || [ ! -x "$BASE_DIR/prebuilt/dunst/usr/bin/dunst" ]; then
"$BASE_DIR/scripts/build-dunst.sh"
fi
if [ "$REBUILD_ALL" -eq 1 ] || [ ! -f "$BASE_DIR/prebuilt/geminux/usr/lib/libgemui.so" ]; then
"$BASE_DIR/scripts/build-geminux-ui.sh"
fi
if [ "$SKIP_APKS" -eq 0 ]; then
"$BASE_DIR/scripts/package-local-apks.sh"
fi
if [ "$FORCE_BOOTSTRAP" -eq 1 ]; then
"$BASE_DIR/scripts/bootstrap-rootfs.sh" "$ROOTFS_DIR" gemini-x27 --force
else
"$BASE_DIR/scripts/bootstrap-rootfs.sh" "$ROOTFS_DIR" gemini-x27
fi
echo "[*] Applying @geminux APKs, overlay, prune check, and building ext4 rootfs image..."
ROOTFS_HOST="$(realpath "$ROOTFS_DIR")"
docker run --rm --network=host \
-e HOST_UID="$(id -u)" \
-e HOST_GID="$(id -g)" \
-v "$BASE_DIR:/geminux" \
-v "$ROOTFS_HOST:/rootfs" \
alpine:3.24 sh -eu -c '
cleanup_perms() {
chown -R "$HOST_UID:$HOST_GID" /rootfs /geminux/images 2>/dev/null || true
[ ! -f /rootfs/usr/bin/Xorg ] || chmod 04755 /rootfs/usr/bin/Xorg 2>/dev/null || true
}
trap cleanup_perms EXIT
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
cp /geminux/keys/*.rsa.pub /etc/apk/keys/
apk add --no-cache e2fsprogs e2fsprogs-extra coreutils glib openssh-keygen python3 >/dev/null
/geminux/scripts/apply-geminux-overlay.sh /rootfs gemini-x27
/geminux/scripts/prune-rootfs-packages.sh /rootfs purge
chown -R 0:0 /rootfs
python3 - /rootfs <<'\''PYEOF'\''
import os
import sys
root = sys.argv[1]
db_path = os.path.join(root, "lib", "apk", "db", "installed")
def apply_meta(path, meta):
parts = meta.split(":")
if len(parts) != 3:
return
uid, gid, mode = int(parts[0]), int(parts[1]), int(parts[2], 8)
if not os.path.lexists(path):
return
os.lchown(path, uid, gid)
if not os.path.islink(path):
os.chmod(path, mode)
cur_dir = ""
last_target = None
with open(db_path, "r", encoding="utf-8", errors="replace") as f:
for raw in f:
line = raw.rstrip("\n")
if not line:
cur_dir = ""
last_target = None
elif line.startswith("F:"):
cur_dir = line[2:]
last_target = os.path.join(root, cur_dir)
elif line.startswith("R:"):
last_target = os.path.join(root, cur_dir, line[2:])
elif line.startswith(("M:", "a:")) and last_target:
apply_meta(last_target, line[2:])
PYEOF
chown -R 10000:10000 /rootfs/home/user 2>/dev/null || true
[ ! -f /rootfs/etc/shadow ] || { chown 0:42 /rootfs/etc/shadow && chmod 0640 /rootfs/etc/shadow; }
[ ! -d /rootfs/root/.ssh ] || chmod 0700 /rootfs/root/.ssh
[ ! -f /rootfs/root/.ssh/authorized_keys ] || chmod 0600 /rootfs/root/.ssh/authorized_keys
[ ! -f /rootfs/etc/sudoers.d/wheel ] || chmod 0440 /rootfs/etc/sudoers.d/wheel
chmod 0640 /rootfs/etc/doas.d/* 2>/dev/null || true
if [ -f /rootfs/usr/bin/Xorg ]; then
chown 0:0 /rootfs/usr/bin/Xorg
chmod 04755 /rootfs/usr/bin/Xorg
fi
if [ -f /rootfs/usr/libexec/dbus-daemon-launch-helper ]; then
DBUS_GID="$(awk -F: "\$1 == \"messagebus\" { print \$3; exit }" /rootfs/etc/group)"
chown "0:${DBUS_GID:-103}" /rootfs/usr/libexec/dbus-daemon-launch-helper
chmod 04750 /rootfs/usr/libexec/dbus-daemon-launch-helper
fi
if [ -d /rootfs/etc/polkit-1/rules.d ]; then
POLKIT_GID="$(awk -F: "\$1 == \"polkitd\" { print \$3; exit }" /rootfs/etc/group)"
chown "0:${POLKIT_GID:-110}" /rootfs/etc/polkit-1/rules.d
chmod 0750 /rootfs/etc/polkit-1/rules.d
chmod 0644 /rootfs/etc/polkit-1/rules.d/*.rules 2>/dev/null || true
fi
USED_KB="$(du -sk /rootfs | awk "{print \$1}")"
# Allocate exact content + ~512 MiB headroom (1 MiB aligned)
TARGET_MB=$(( (USED_KB / 1024) + 512 ))
if [ "$TARGET_MB" -lt 1024 ]; then
TARGET_MB=1024
fi
IMG_TMP="/geminux/images/geminux-rootfs.img.tmp"
IMG_OUT="/geminux/images/geminux-rootfs.img"
rm -f "$IMG_TMP"
truncate -s "${TARGET_MB}M" "$IMG_TMP"
mke2fs -t ext4 -d /rootfs -L geminux-root -O ^metadata_csum_seed -m 1 "$IMG_TMP" >/dev/null
e2fsck -fy "$IMG_TMP" >/dev/null || true
e2fsck -fn "$IMG_TMP"
mv -f "$IMG_TMP" "$IMG_OUT"
ls -lh "$IMG_OUT"
'
if command -v img2simg >/dev/null 2>&1; then
echo "[*] Generating Android sparse rootfs image..."
img2simg "$IMAGES_DIR/geminux-rootfs.img" "$IMAGES_DIR/geminux-rootfs.simg"
fi
echo "[*] Generating SHA256SUMS manifest..."
(
cd "$IMAGES_DIR"
TARGETS=""
for f in boot-geminux.img recovery-geminux.img geminux-rootfs.img geminux-rootfs.simg; do
[ -f "$f" ] && TARGETS="$TARGETS $f"
done
# shellcheck disable=SC2086
sha256sum $TARGETS > SHA256SUMS
)
cat "$IMAGES_DIR/SHA256SUMS"
echo "[+] Geminux image rebuild complete."