umorpha-boxes/bin/umorpha-install

356 lines
11 KiB
Plaintext
Raw Permalink Normal View History

2023-10-31 23:18:47 +00:00
#!/usr/bin/env bash
2024-03-04 22:49:23 +00:00
# Copyright (C) 2023-2024 Umorpha Systems
2023-10-31 23:18:47 +00:00
# SPDX-License-Identifier: AGPL-3.0-or-later
2023-11-04 07:12:14 +00:00
# Dependencies:
2023-11-08 05:07:45 +00:00
# - /usr/bin/env (coreutils, busybox)
# - cat (coreutils, busybox)
2024-03-04 22:49:23 +00:00
# - dd (coreutils, busybox--only needed fo typ=uboot-sunxi)
2023-11-08 05:07:45 +00:00
# - install (coreutils, busybox)
# - mkdir (coreutils, busybox)
# - mktemp (coreutils, busybox)
# - rm (coreutils, busybox)
# - rmdir (coreutils, busybox)
# - losetup (optional) (util-linux; the busybox one doesn't work)
# - umount (util-linux, busybox)
# - blkid (util-linux)
# - mount (util-linux)
# - bash (bash)
2024-03-04 22:49:23 +00:00
# - sgdisk (gptfdisk--only needed fo typ=uboot-sunxi)
2023-11-08 05:07:45 +00:00
# - systemd-repart (systemd)
# - lvcreate (lvm2)
2023-12-08 03:55:03 +00:00
# - lvresize (lvm2)
2023-11-08 05:07:45 +00:00
# - pvcreate (lvm2)
# - pvs (lvm2)
2023-11-08 05:07:45 +00:00
# - vgchange (lvm2)
# - vgcreate (lvm2)
2023-11-09 08:20:55 +00:00
# - vgremove (lvm2)
# - vgs (lvm2)
2023-11-08 05:07:45 +00:00
# - pv (pv)
# - mkfs.ext4 (e2fsprogs)
# - mkfs.vfat (dosfstools)
# - arch-chroot (arch-install-scripts)
2023-12-06 23:46:11 +00:00
# - umorpha-mount
2023-11-04 07:12:14 +00:00
#
# Dependencies of arch-chroot (as of v28):
# - cat (coreutils, busybox)
# - install (coreutils, busybox)
# - ln (coreutils, busybox)
# - touch (coreutils, busybox)
2023-11-08 05:07:45 +00:00
# - chroot (coreutils; the busybox one doesn't work)
# - readlink (coreutils; the busybox one doesn't work)
2023-11-04 07:12:14 +00:00
# - mountpoint (util-linux, busybox)
# - umount (util-linux, busybox)
# - unshare (util-linux, busybox)
# - mount (util-linux)
# - grep (grep, busybox)
# - bash (bash)
2023-11-05 07:51:34 +00:00
#
2023-12-06 23:46:11 +00:00
# Dependencies of umorpha-mount:
# - sh (bash, busybox)
# - getopt (util-linux, busybox)
# - mkdir (coreutils, busybox)
# - mount (util-linux)
#
2023-11-05 07:51:34 +00:00
# Of those, these are the ones that aren't already in an initcpio:
2023-11-08 05:07:45 +00:00
# - arch-chroot (arch-install-scripts)
# - bash (bash)
# - chroot (coreutils)
# - readlink (coreutils)
# - pv (pv)
# - systemd-repart (systemd)
# - mkfs.ext4 (e2fsprogs)
# - mkfs.vfat (dosfstools)
# - lvcreate (lvm2)
2023-12-08 03:55:03 +00:00
# - lvresize (lvm2)
2023-11-08 05:07:45 +00:00
# - pvcreate (lvm2)
# - pvs (lvm2)
2023-11-08 05:07:45 +00:00
# - vgchange (lvm2)
# - vgcreate (lvm2)
2023-11-09 08:20:55 +00:00
# - vgremove (lvm2)
# - vgs (lvm2)
2023-11-05 07:51:34 +00:00
2023-10-31 23:18:47 +00:00
set -euE -o pipefail
2023-11-08 22:02:54 +00:00
v() {
2023-11-09 21:03:55 +00:00
printf >&2 ' $ %s\n' "${*@Q}"
2023-11-08 22:02:54 +00:00
"$@"
}
in_array() {
local needle haystack straw
needle=$1
haystack=("${@:2}")
for straw in "${haystack[@]}"; do
if [[ "$needle" == "$straw" ]]; then
return 0
fi
done
return 1
}
2023-10-31 23:18:47 +00:00
main() {
local typ device rootfs slot
2023-11-13 04:52:10 +00:00
typ=$1
rootfs=$2
device=$3
slot='a'
2023-10-31 23:18:47 +00:00
2023-11-13 04:52:10 +00:00
echo ":: Installing '${rootfs}' to '${device}' for '${typ}'"
2023-11-09 21:55:25 +00:00
if [[ "$rootfs" != *rootfs* ]]; then
echo >&2 "!! Filename '${rootfs}' does not look like a rootfs filename"
return 2
fi
2023-11-13 04:52:10 +00:00
case "$typ" in
2024-03-04 22:49:23 +00:00
uefi|bios|uboot-sunxi) :;;
2023-11-13 04:52:10 +00:00
*)
echo >&2 "!! Install type '${typ}' is not valid"
return 2
;;
esac
2023-10-31 23:18:47 +00:00
2023-12-01 17:28:10 +00:00
########################################################################
2023-10-31 23:18:47 +00:00
cleanup=()
2023-11-09 21:55:25 +00:00
_do_cleanup() {
2023-12-01 17:28:10 +00:00
if [[ $? == 0 ]]; then
2023-12-05 15:32:32 +00:00
echo ":: Cleaning up..."
2023-12-01 17:28:10 +00:00
else
echo >&2 "!! ERROR! Cleaning up..."
fi
2023-11-09 21:55:25 +00:00
for (( i=${#cleanup[@]}-1; i >= 0; i-- )); do
eval "${cleanup[$i]}"
done
}
trap '_do_cleanup' EXIT
2023-10-31 23:18:47 +00:00
local tmpdir
tmpdir="$(mktemp -dt "${0##*/}.XXXXXXXXXX")"
cleanup+="rmdir -- ${tmpdir@Q}"
########################################################################
2023-11-08 05:07:45 +00:00
local is_loop=false
2023-10-31 23:18:47 +00:00
if [[ -f "$device" ]]; then
echo " file '${device}' is not a block device, setting up a loopback device..."
2023-11-08 22:02:54 +00:00
device=$(v losetup --find --show --partscan -- "$device")
2023-10-31 23:18:47 +00:00
echo " ... '${device}'"
2023-11-08 22:02:54 +00:00
cleanup+=("v losetup --detach ${device@Q}")
2023-11-08 05:07:45 +00:00
is_loop=true
2023-10-31 23:18:47 +00:00
fi
local devicep="$device"
if [[ "$devicep" == *[0-9] ]]; then
devicep+='p'
fi
########################################################################
echo ":: Setting up GPT"
2023-11-13 04:52:10 +00:00
n=0
2023-10-31 23:18:47 +00:00
cat >"$tmpdir/00-esp.conf" <<-EOF
[Partition]
2024-03-04 22:49:23 +00:00
# EFI System Partition, specified by UEFI
2023-10-31 23:18:47 +00:00
Type=esp
Format=vfat
Label=Umorpha EFI System Partition
SizeMinBytes=512M
SizeMaxBytes=512M
EOF
2023-11-13 04:52:10 +00:00
n_esp=$((++n))
2024-03-07 17:49:23 +00:00
sector_size=''
2024-03-04 22:49:23 +00:00
case "$typ" in
bios)
cat >"$tmpdir/05-bios.conf" <<-EOF
[Partition]
# GRUB BIOS Boot partition, specified by GRUB
Type=21686148-6449-6E6F-744E-656564454649
Label=Umorpha BIOS Boot Partition
SizeMinBytes=1M
SizeMaxBytes=1M
EOF
n_bios=$((++n))
;;
2024-03-07 17:49:23 +00:00
uboot-sunxi)
sector_size=512
# Commented out because `BegOffset=` and `EndOffset=`
# aren't real systemd-repart options.
#
#cat >"$tmpdir/05-uboot.conf" <<-EOF
# [Partition]
# # U-Boot Environment partition, specified by U-Boot
# Type=3DE21764-95BD-54BD-A5C3-4ABE786F38A8
# Label=Umorpha U-Boot Partition
# BegOffset=8192b
# EndOffset=1M
#EOF
#n_uboot=$((++n))
;;
2024-03-04 22:49:23 +00:00
esac
2023-10-31 23:18:47 +00:00
cat >"$tmpdir/10-lvm.conf" <<-EOF
[Partition]
2024-03-04 22:49:23 +00:00
# LVM2 partition, specified by ???
2023-10-31 23:18:47 +00:00
# https://en.wikipedia.org/w/index.php?title=User_talk:Claunia&diff=prev&oldid=1182832243
Type=E6D6D379-F507-44C2-A23C-238F2A3DF928
Label=Umorpha LVM Partition
EOF
2023-11-13 04:52:10 +00:00
n_lvm=$((++n))
2023-10-31 23:18:47 +00:00
2023-11-08 22:02:54 +00:00
v systemd-repart \
2023-10-31 23:18:47 +00:00
--empty=allow --dry-run=no \
--no-pager \
--definitions="$tmpdir" \
2024-03-07 17:49:23 +00:00
${sector_size:+--sector-size="$sector_size"} \
2023-10-31 23:18:47 +00:00
"$device"
rm -- "$tmpdir"/*.conf
2024-03-04 22:49:23 +00:00
if [[ $typ == uboot-sunxi ]]; then
# Shrink the number of entries in the partition table
# to make room for U-Boot; the location that sunxi
# chips need us to put U-Boot at is by default
# occupied by the partition table.
#
# def+min table location is LBA=2, def+min entry size
# is 128b, def number of entries is 128, which means
# that it ranges from 1KiB to 17KiB.
#
# U-Boot needs to go at 8KiB, meaning that if we want
# leave the partition table at the default location it
# can be a maximum of 7KiB.
#
# 7KiB / (128b/entry) = 56entries
v sgdisk --resize-table=56 "$device"
# TODO: I'd like to set up a partition for U-Boot, to
# make it clear that it's there (instead of putting it
# in unpartitioned space). But the default
# FirstUsableLBA is set to 1MiB, and so sgdisk will
# silently bump the partition's start LBA to 1MiB.
# And I don't see a way to have sgdisk change the
# FirstUsableLBA.
#
#v sgdisk \
# --new=3:$((8*1024/512)):$((1024*1024/512)) \
# --typecode=3DE21764-95BD-54BD-A5C3-4ABE786F38A8 \
# --change-name=3:'Umorpha U-Boot Partition' \
# --sort
# "$device"
fi
2023-10-31 23:18:47 +00:00
########################################################################
echo ":: Setting up LVM"
if ! in_array "${devicep}${n_lvm}" $(pvs --noheadings --options=pv_name); then
v pvcreate "${devicep}${n_lvm}"
fi
if ! in_array vg_umorpha $(vgs --noheadings --options=vg_name); then
v vgcreate vg_umorpha "${devicep}${n_lvm}"
fi
2023-11-08 05:07:45 +00:00
if $is_loop; then
2023-11-08 22:02:54 +00:00
cleanup+=("v vgchange --activate=n vg_umorpha")
2023-11-08 05:07:45 +00:00
fi
2023-10-31 23:18:47 +00:00
2023-11-08 05:07:45 +00:00
# Use short `-c` instead of `--format` so this works with busybox.
size="$(stat -c '%s' -- "$rootfs")"
if [[ -e /dev/mapper/vg_umorpha-lv_root_$slot ]]; then
# Ugg, lvresize exits with non-zero for no-ops, which we can't
# even really do a pre-flight check for since it might round a
# size up.
v lvresize --size="${size}b" "/dev/mapper/vg_umorpha-lv_root_$slot" || true
else
v lvcreate --zero=y --wipesignatures=y --yes --name="lv_root_$slot" \
--size="${size}b" vg_umorpha
fi
2023-10-31 23:18:47 +00:00
v pv -- "$rootfs" >"/dev/mapper/vg_umorpha-lv_root_$slot"
if ! [[ -e /dev/mapper/vg_umorpha-lv_root_overlay ]]; then
v lvcreate --zero=y --wipesignatures=y --yes \
--name=lv_root_overlay --size=1G vg_umorpha
fi
if [[ -z "$(blkid --output=export /dev/mapper/vg_umorpha-lv_root_overlay | sed -n s/^TYPE=//p)" ]]; then
v mkfs.ext4 /dev/mapper/vg_umorpha-lv_root_overlay
fi
2023-10-31 23:18:47 +00:00
########################################################################
echo ":: Configuring boot"
local selector_boot selector_root selector_overlay
2023-11-13 04:52:10 +00:00
selector_boot="$( blkid --output=export "${devicep}${n_esp}" | grep ^UUID=)"
selector_root="$( blkid --output=export "/dev/mapper/vg_umorpha-lv_root_$slot" | grep ^UUID=)"
2023-10-31 23:18:47 +00:00
selector_overlay="$(blkid --output=export /dev/mapper/vg_umorpha-lv_root_overlay | grep ^UUID=)"
2023-11-08 22:02:54 +00:00
v declare -p selector_boot selector_root selector_overlay
2023-10-31 23:18:47 +00:00
2023-12-05 23:29:02 +00:00
v umorpha-mount \
--root="/dev/mapper/vg_umorpha-lv_root_$slot:auto:ro" \
--overlay='/dev/mapper/vg_umorpha-lv_root_overlay:auto:rw' \
--boot="${devicep}${n_esp}:auto:rw" \
--basedir="$tmpdir"
cleanup+=("rmdir -- ${tmpdir@Q}/root")
2023-12-07 05:06:07 +00:00
cleanup+=("v umount ${tmpdir@Q}/root")
cleanup+=("rmdir -- ${tmpdir@Q}/overlay")
cleanup+=("v umount ${tmpdir@Q}/overlay")
cleanup+=("rmdir -- ${tmpdir@Q}/mnt")
cleanup+=("v umount ${tmpdir@Q}/mnt")
cleanup+=("v umount ${tmpdir@Q}/mnt/boot")
2023-10-31 23:18:47 +00:00
2023-11-13 04:52:10 +00:00
case "$typ" in
2024-03-04 22:49:23 +00:00
uefi|uboot-sunxi)
if [[ $typ == uboot-sunxi ]]; then
v dd if="$(echo "$tmpdir/mnt"/usr/lib/u-boot/*/u-boot-sunxi-with-spl.bin)" of="$device" conv=notrunc bs=512 seek=16
fi
2023-11-13 04:52:10 +00:00
mkdir -p -- "$tmpdir/mnt/etc/kernel"
2023-12-05 23:29:02 +00:00
echo "root=$selector_root overlay=$selector_overlay boot=$selector_boot rw" >"$tmpdir/mnt/etc/kernel/cmdline"
2023-11-13 04:52:10 +00:00
mkdir -p -- "$tmpdir/mnt/boot/EFI/Linux"
2024-03-04 22:49:23 +00:00
bootloader_src=$(echo "$tmpdir"/mnt/usr/lib/systemd/boot/efi/systemd-boot*.efi)
bootloader_dst=${bootloader_src##*-}
2024-03-07 17:49:23 +00:00
bootloader_dst="$tmpdir/mnt/boot/EFI/BOOT/${bootloader_dst^^}"
2024-03-04 22:49:23 +00:00
v install -D "$bootloader_src" "$bootloader_dst"
2023-11-13 04:52:10 +00:00
;;
bios)
v arch-chroot -- "$tmpdir/mnt" grub-install --target=i386-pc "$device"
2023-11-30 16:12:36 +00:00
install -Dm600 /dev/stdin "$tmpdir/mnt/boot/grub/grub.cfg" <<-EOF
insmod part_gpt
insmod part_msdos
if [ -s \$prefix/grubenv ]; then
load_env
fi
if [ -s \$prefix/grub_a.cfg ]; then
configfile \$prefix/grub_a.cfg
fi
if [ -s \$prefix/grub_b.cfg ]; then
configfile \$prefix/grub_b.cfg
fi
EOF
2023-11-13 04:52:10 +00:00
{
for krnl in "$tmpdir/mnt"/usr/lib/modules/*/vmlinuz; do
pkgbase=$(cat -- ${krnl%vmlinuz}pkgbase)
cat <<-EOF
menuentry 'Parabola GNU/Linux-libre, slot ${slot}, ${pkgbase} kernel'
2023-11-13 04:52:10 +00:00
set search --no-floppy --fs-uuid --set=root ${selector_boot#UUID=}
echo 'Loading ${pkgbase} kernel...'
2023-12-05 23:29:02 +00:00
linux /vmlinuz-${pkgbase} root=$selector_root overlay=$selector_overlay boot=$selector_boot rw
2023-11-13 04:52:10 +00:00
echo 'Loading initial ramdisk...'
initrd /initramfs-${pkgbase}.img
2023-11-13 13:57:48 +00:00
echo 'Booting...'
boot
2023-11-13 04:52:10 +00:00
EOF
done
} | install -Dm600 /dev/stdin "$tmpdir/mnt/boot/grub/grub_${slot}.cfg"
2023-11-13 04:52:10 +00:00
;;
esac
2023-10-31 23:18:47 +00:00
2024-03-07 17:49:23 +00:00
if ! v arch-chroot -- "$tmpdir/mnt" sh -c "printf '%s\n' usr/lib/modules/*/vmlinuz | /usr/share/libalpm/scripts/mkinitcpio install"; then
v arch-chroot -- "$tmpdir/mnt" sh -c "MKINITCPIO_PROCESS_PRESET=linux-libre-fallback bash -x /bin/mkinitcpio -k /usr/lib/modules/6.7.4-gnu-1/vmlinuz -U /boot/EFI/Linux/parabola-linux-libre-fallback.efi -S autodetect --microcode '/boot/*-ucode.img'"
fi
2023-11-08 22:02:54 +00:00
v arch-chroot -- "$tmpdir/mnt" ldconfig
v arch-chroot -- "$tmpdir/mnt" systemd-machine-id-setup
v arch-chroot -- "$tmpdir/mnt" update-ca-trust
2023-10-31 23:18:47 +00:00
}
main "$@"