Initial commit
This commit is contained in:
commit
5b78486868
|
@ -0,0 +1,178 @@
|
|||
#!/bin/sh
|
||||
|
||||
# Exit on any errors
|
||||
set -e
|
||||
|
||||
errormsg () {
|
||||
echo "$1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check deps
|
||||
depcheck() {
|
||||
if ! which "$1" >/dev/null 2>&1; then
|
||||
errormsg "Missing dependency $1, exiting."
|
||||
fi
|
||||
}
|
||||
|
||||
depcheck "fallocate"
|
||||
depcheck "sfdisk"
|
||||
depcheck "mkfs.vfat"
|
||||
depcheck "mkfs.ext4"
|
||||
depcheck "mkfs.btrfs"
|
||||
depcheck "mktemp"
|
||||
depcheck "mount"
|
||||
depcheck "mkdir"
|
||||
depcheck "cat"
|
||||
depcheck "install"
|
||||
depcheck "apk"
|
||||
depcheck "bwrap"
|
||||
|
||||
# BWRAP "chrooting"
|
||||
wrapped() {
|
||||
bwrap \
|
||||
--unshare-net \
|
||||
--unshare-pid \
|
||||
--unshare-uts \
|
||||
--unshare-ipc \
|
||||
--bind "$tmpdir" / \
|
||||
--dev-bind /dev /dev \
|
||||
--ro-bind /sys /sys \
|
||||
--proc /proc \
|
||||
--tmpfs /tmp \
|
||||
--tmpfs /run \
|
||||
-- $@
|
||||
}
|
||||
|
||||
image="${1:-nnd-rootfs}"
|
||||
arch="${2:-x86_64}"
|
||||
|
||||
# Set up file as loopback
|
||||
fallocate -v -l 1G "$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"
|
||||
mkfs.btrfs -L nnd-root "${loopback}p3"
|
||||
|
||||
# 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 "${loopback}p3" "$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 basic rootfs
|
||||
apk add \
|
||||
-v \
|
||||
--initdb \
|
||||
--allow-untrusted \
|
||||
--root "$tmpdir" \
|
||||
--repositories-file "$tmpdir/etc/apk/repositories" \
|
||||
--arch "$arch" \
|
||||
nnd \
|
||||
lfsbase \
|
||||
nnd-signkey \
|
||||
alpine-keys \
|
||||
linux-full \
|
||||
nnd-s6-linux-init-default \
|
||||
nnd-s6-services \
|
||||
busybox \
|
||||
grub-efi \
|
||||
iproute2 \
|
||||
bcnm@edge \
|
||||
mdevd \
|
||||
mdev-conf \
|
||||
utmps \
|
||||
findmnt \
|
||||
dosfstools \
|
||||
e2fsprogs \
|
||||
btrfs-progs
|
||||
|
||||
echo 'NOTE: It is expected that grub would fail to generate a config at this stage, "failed to get canonical path" errors are safe to ignore' >&2
|
||||
|
||||
# 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 s6
|
||||
for bundle in bundle.core bundle.net; do
|
||||
touch "$tmpdir"/etc/s6/rc/default/contents.d/"$bundle"
|
||||
done
|
||||
wrapped nnd-s6 dist
|
||||
wrapped nnd-s6 generate
|
||||
wrapped nnd-s6 swap
|
||||
|
||||
# Set-up alpine init
|
||||
|
||||
## Add btrfs to required initrd modules
|
||||
install -Dm444 <(
|
||||
cat <<EOF
|
||||
features="ata base btrfs cdrom ext4 keymap kms mmc nvme raid scsi usb virtio"
|
||||
EOF
|
||||
) "$tmpdir"/etc/mkinitfs/mkinitfs.conf
|
||||
|
||||
wrapped sh -c 'mkinitfs $(find /lib/modules -mindepth 1 -maxdepth 1 -type d)'
|
||||
|
||||
# Regenerate grub config
|
||||
|
||||
## 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
|
||||
|
||||
wrapped grub-mkconfig -o /boot/grub/grub.cfg
|
||||
wrapped grub-install --removable --efi-directory /boot/efi --boot-directory /boot
|
||||
|
||||
# Cleanup
|
||||
umount -Rv "$tmpdir"
|
||||
rmdir -v "$tmpdir"
|
||||
losetup -d "$loopback"
|
Loading…
Reference in New Issue