scripts/make-root.sh

238 lines
4.9 KiB
Bash
Executable File

#!/bin/sh
# Exit on any errors
set -e
errormsg () {
echo "$1" >&2
exit 1
}
# Check deps
depcheck() {
for x in $@; do
if ! which "$x" >/dev/null 2>&1; then
errormsg "Missing dependency $x, exiting."
fi
done
}
splitargs() {
echo "$1" | sed 's/ /\n/g'
}
# BWRAP "chrooting"
wrapped() {
bwrap \
--new-session \
--die-with-parent \
--unshare-pid \
--unshare-uts \
--unshare-ipc \
--bind "$tmpdir" / \
--dev-bind /dev /dev \
--ro-bind /sys /sys \
--proc /proc \
--tmpfs /tmp \
--tmpfs /run \
-- $@
}
depcheck "fallocate" \
"sfdisk" \
"mkfs.vfat" \
"mkfs.ext4" \
"pvcreate" \
"vgcreate" \
"lvcreate" \
"vgchange" \
"mkfs.btrfs" \
"mktemp" \
"mount" \
"mkdir" \
"cat" \
"install" \
"apk" \
"bwrap"
custom=false
while getopts ':a:i:b:t:s:p:c' opt; do
case $opt in
(a) arch="$OPTARG";;
(b) svcs="$svcs $OPTARG";;
(c) custom=true;;
(i) image="$OPTARG";;
(p) pkgs="$pkgs $OPTARG";;
(s) size="$OPTARG";;
(t) ttys="$ttys $OPTARG";;
esac
done
: "${image:=nnd-rootfs}"
: "${arch:=x86_64}"
: "${svcs:=bundle.core bundle.net}"
: "${size:=1G}"
# Set up file as loopback
fallocate -v -l "$size" "$image"
if ! [ -r "$image" ]; then
errormsg "Something went wrong while preallocating sparse image"
fi
loopback="$(losetup --show -P -f "$image")"
if ! [ -r "$loopback" ]; then
errormsg "Something went wrong while setting up loopback image"
fi
# Create partition table
sfdisk "$loopback" <<EOT
label: gpt
size=64MiB,name=nnd-efi,type=U
size=256MiB,name=nnd-boot,type=L
name=nnd-root,type=L
EOT
# Format partitions
mkfs.vfat -n nnd-efi "${loopback}p1"
mkfs.ext4 -L nnd-boot "${loopback}p2"
VGN="nnd"
LVN="rootlv"
pvcreate "${loopback}p3"
vgcreate "$VGN" "${loopback}p3"
lvcreate "$VGN" -n "$LVN" -l '100%FREE'
vgchange -a y "$VGN"
mkfs.btrfs -L nnd-root "/dev/$VGN/$LVN"
# Set up chroot
tmpdir="$(mktemp -d "nnd-image.XXXXXXXXXX")"
if [ -z "$tmpdir" ]; then
errormsg "Something went wrong during working directory preparation, bailing out"
fi
mount -v -t btrfs "/dev/$VGN/$LVN" "$tmpdir"
mkdir -p "$tmpdir/boot"
mount -v -t ext4 "${loopback}p2" "$tmpdir/boot"
mkdir -p "$tmpdir/boot/efi"
mount -v -t vfat "${loopback}p1" "$tmpdir/boot/efi"
install -Dm444 <(
cat <<EOF
https://packages.redxen.eu/nnd/main
https://dl-cdn.alpinelinux.org/alpine/latest-stable/main
https://dl-cdn.alpinelinux.org/alpine/latest-stable/community
@edge https://dl-cdn.alpinelinux.org/alpine/edge/main
@edge https://dl-cdn.alpinelinux.org/alpine/edge/community
@edge https://dl-cdn.alpinelinux.org/alpine/edge/testing
EOF
) "$tmpdir"/etc/apk/repositories
# Set up bootstrap rootfs
apk add \
-v \
--initdb \
--allow-untrusted \
--root "$tmpdir" \
--repositories-file "$tmpdir/etc/apk/repositories" \
--arch "$arch" \
lfsbase \
busybox \
nnd-signkey \
alpine-keys \
apk-tools
## Add btrfs and lvm to required initrd modules
# TODO: Only append and don't replace
install -Dm444 <(
cat <<EOF
features="ata base lvm btrfs ext4 cdrom keymap kms mmc nvme raid scsi usb virtio"
EOF
) "$tmpdir"/etc/mkinitfs/mkinitfs.conf
## Provide temporary override for grub, TODO: provide our own grub cfgs eventually
install -Dm444 <(
cat <<EOF
GRUB_DISTRIBUTOR="nnd"
GRUB_TIMEOUT=2
GRUB_DISABLE_SUBMENU=y
GRUB_DISABLE_RECOVERY=true
GRUB_CMDLINE_LINUX_DEFAULT="loglevel=3"
GRUB_CMDLINE_LINUX="root=LABEL=nnd-root rootfstype=btrfs"
EOF
) "$tmpdir"/etc/default/grub
# Set up default resolv.conf (in case no DNS server is configured via DHCP or otherwise)
## TODO: Maybe distribute this via packages?
install -Dm644 <(
cat <<EOF
nameserver 2606:4700:4700::1111
nameserver 2606:4700:4700::1001
nameserver 1.1.1.1
nameserver 1.0.0.1
EOF
) "$tmpdir"/etc/resolv.conf
# Set up rest of rootfs
wrapped apk add -v \
nnd \
linux-full \
nnd-s6-linux-init-default \
nnd-s6-services \
grub-efi \
iproute2 \
bcnm@edge \
mdevd \
mdev-conf \
utmps \
findmnt \
dosfstools \
e2fsprogs \
btrfs-progs \
$pkgs
# Install grub to ESP
wrapped grub-install --removable --efi-directory /boot/efi --boot-directory /boot
# Set-up s6
# Set-up extra tty if given
if [ ! -z "$ttys" ]; then
splitargs "$ttys" | \
while read -r tty; do
[ -z "$tty" ] && continue
cp -rv "$tmpdir"/usr/share/nnd/s6/dist/rc/getty.tty1 "$tmpdir"/etc/s6/rc/getty."$tty"
sed -i 's/tty1/'"$tty"'/g' "$tmpdir"/etc/s6/rc/getty."$tty"/run
done
fi
# Set-up services
splitargs "$svcs" | \
while read -r bundle; do
[ -z "$bundle" ] && continue
touch "$tmpdir"/etc/s6/rc/default/contents.d/"$bundle"
done
wrapped nnd-s6 dist
wrapped nnd-s6 generate
wrapped nnd-s6 swap
# Unlock root login
wrapped passwd -ud root
# Provide basic shell for customization
if "$custom"; then
echo "You are now entering your image's shell."
echo "Any customizations made here will remain in the image."
echo "Upon exit, the image will be packed."
wrapped sh || true # Handle graceful exit
fi
# Cleanup
umount -Rv "$tmpdir"
vgchange -a n "$VGN"
rmdir -v "$tmpdir"
losetup -d "$loopback"