void-linux-pbp-installer/mk_pbp_img

103 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
ROOT_DIR="$(dirname "$(readlink -f "$0")")"
OUT_DIR="${1:-$(pwd)}"
TMP_DIR="$(mktemp --directory)"
TMP_DIR_PFS="$(mktemp --directory)"
TMP_DIR_MOUNT="$(mktemp --directory)"
TOOLS=(git make sudo qemu-aarch64-static xz)
GIT_REPO=https://github.com/void-linux/void-mklive.git
GIT_REV=f71eb4bb81004b50884011ef5e0d00f99bc2892a
GIT_DIR="$ROOT_DIR/.void-mklive"
cleanup() {
cleanup_mounts
sudo rm -r "$TMP_DIR" "$TMP_DIR_PFS" "$TMP_DIR_MOUNT"
}
cleanup_mounts() {
sudo losetup -D
sudo umount --recursive "$TMP_DIR_MOUNT" || true
}
check_tools() {
for tool in "${TOOLS[@]}"; do
if ! command -v "$tool" &> /dev/null
then
echo "$tool could not be found"
exit 1
fi
done
}
init_void_mklive() {
if [ ! -d "$GIT_DIR" ]; then
git clone "$GIT_REPO" "$GIT_DIR"
cd "$GIT_DIR" && git checkout "$GIT_REV"
fi
cd "$GIT_DIR"
if [ "$(git rev-parse HEAD)" != "$GIT_REV" ]; then
git fetch
git checkout "$GIT_REV"
fi
make
sed -i 's/ROOTFS_TARBALL#void-/ROOTFS_TARBALL#*void-/g' "$GIT_DIR/mkimage.sh"
}
build_img() {
local rootfs_file="$TMP_DIR/rootfs.tar.xz"
local platformfs_file="$TMP_DIR/void-pinebookpro-PLATFORMFS.tar.xz"
local img="$TMP_DIR/void-pbp.img"
cd "$GIT_DIR"
sudo ./mkrootfs.sh -o "$rootfs_file" aarch64
sudo ./mkplatformfs.sh -p "dracut lvm2 cryptsetup dbus NetworkManager" -o "$platformfs_file" pinebookpro "$rootfs_file"
sudo ./mkimage.sh -B 256MiB -s 3GB -o "$img" "$platformfs_file"
# shellcheck disable=SC2024
# it is correct that the decompressed image is written as the unprivileged user
sudo xz --decompress --stdout "$img.xz" > "$img"
local loop_dev
loop_dev="$(sudo losetup --show -f -P "$img")"
sudo mount "$loop_dev"p2 "$TMP_DIR_MOUNT"
sudo mount "$loop_dev"p1 "$TMP_DIR_MOUNT"/boot
sudo cp "$ROOT_DIR/bootstrap" "$TMP_DIR_MOUNT/usr/bin/bootstrap"
sudo chmod +x "$TMP_DIR_MOUNT/bin/bootstrap"
sudo cp "$(which qemu-aarch64-static)" "$TMP_DIR_MOUNT/usr/bin/"
sudo chroot "$TMP_DIR_MOUNT" qemu-aarch64-static /bin/bash <<"EOT"
ln -s /etc/sv/dbus /etc/runit/runsvdir/default/
ln -s /etc/sv/NetworkManager /etc/runit/runsvdir/default/
ln -s /etc/sv/sshd /etc/runit/runsvdir/default/
EOT
sudo rm "$TMP_DIR_MOUNT/usr/bin/qemu-aarch64-static"
cleanup_mounts
mkdir -p "$OUT_DIR"
xz --threads=0 --compress --stdout "$img" > "$OUT_DIR/$(basename "$img.xz")"
echo "built pinebookpro image: $OUT_DIR/$(basename "$img.xz")"
}
trap cleanup 0 SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM
check_tools
init_void_mklive
build_img