umorpha-boxes/modules/umorpha-disk.sh

177 lines
5.9 KiB
Bash
Raw Normal View History

2023-10-31 07:43:40 +00:00
#!/hint/bash -euE
# Copyright (C) 2023 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
2023-10-31 23:18:47 +00:00
packages+=(lvm2)
2023-10-31 07:43:40 +00:00
pre_install+=(10:umorpha-disk:pre_install)
post_install+=(10:umorpha-disk:post_install)
umorpha-disk:pre_install() {
local arg_mountpoint=$1
mkdir -p "$arg_mountpoint/etc/pacman.d/hooks"
ln -sT /dev/null "$arg_mountpoint/etc/pacman.d/hooks/90-mkinitcpio-install.hook"
}
umorpha-disk:post_install() {
local arg_mountpoint=$1
rm "$arg_mountpoint/etc/pacman.d/hooks/90-mkinitcpio-install.hook"
rmdir "$arg_mountpoint/etc/pacman.d/hooks" || true
2023-10-31 08:53:18 +00:00
2023-10-31 23:18:47 +00:00
########################################################################
# Patch a bug in the libalpm mkinitcpio hook that our `hook.preset`
# below would trigger.
TMPDIR="$arg_mountpoint/usr/share/libalpm/scripts" patch "$arg_mountpoint/usr/share/libalpm/scripts/mkinitcpio" <<-'EOF'
--- /usr/share/libalpm/scripts/mkinitcpio.orig
+++ /usr/share/libalpm/scripts/mkinitcpio
@@ -39,10 +39,14 @@
unsorted_filelist+=("${ALL_uki}")
fi
if [[ -v preset_kver ]]; then
- unsorted_filelist+=("${preset_kver}")
+ if [[ "${preset_kver}" == /* && "${preset_kver}" != /usr/lib/modules/*/vmlinuz ]]; then
+ unsorted_filelist+=("${preset_kver}")
+ fi
unsorted_kernellist+=("${preset_kver}")
elif [[ -v ALL_kver ]]; then
- unsorted_filelist+=("${ALL_kver}")
+ if [[ "${ALL_kver}" == /* && "${ALL_kver}" != /usr/lib/modules/*/vmlinuz ]]; then
+ unsorted_filelist+=("${ALL_kver}")
+ fi
unsorted_kernellist+=("${ALL_kver}")
fi
done
@@ -88,8 +92,10 @@
read_preset "$pkgbase"
# always install the kernel
- for kernelfile in "${kernellist[@]}"; do
- install -Dm644 -- "$line" "${kernelfile}"
+ for kernel in "${kernellist[@]}"; do
+ if [[ "${kernel}" == /* && "${kernel}" != "/${line}" ]]; then
+ install -Dm644 -- "${line}" "${kernel}"
+ fi
done
)
EOF
2023-10-31 08:53:18 +00:00
cat >"$arg_mountpoint/usr/share/mkinitcpio/hook.preset" <<-'EOF'
#!/hint/bash
# mkinitcpio preset file for the '%PKGBASE%' package
#ALL_config="/etc/mkinitcpio.conf"
2023-10-31 23:18:47 +00:00
ALL_kver=$(grep -Fxl '%PKGBASE%' /usr/lib/modules/*/pkgbase|sed 's,pkgbase$,vmlinuz,')
2023-10-31 08:53:18 +00:00
ALL_microcode=(/boot/*-ucode.img)
PRESETS=('default' 'fallback')
#default_config="/etc/mkinitcpio.conf"
#default_image="/boot/initramfs-%PKGBASE%.img"
2023-10-31 23:18:47 +00:00
default_uki="/boot/EFI/Linux/parabola-%PKGBASE%.efi"
2023-10-31 08:53:18 +00:00
#default_options="--splash /usr/share/systemd/bootctl/splash-parabola.bmp"
#fallback_config="/etc/mkinitcpio.conf"
#fallback_image="/boot/initramfs-%PKGBASE%-fallback.img"
2023-10-31 23:18:47 +00:00
fallback_uki="/boot/EFI/Linux/parabola-%PKGBASE%-fallback.efi"
2023-10-31 08:53:18 +00:00
fallback_options="-S autodetect"
EOF
2023-10-31 23:18:47 +00:00
cat >"$arg_mountpoint/etc/mkinitcpio.conf.d/umorpha.conf" <<'EOF'
#!/hint/bash
# Copyright (C) 2023 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
# Insert 'umorpha-overlayfs' and 'lvm2' into HOOKS.
for ((i=0; i<${#HOOKS[@]}; i++)); do
if [[ ${HOOKS[i]} == filesystems ]]; then
HOOKS=("${HOOKS[@]:0:i}" umorpha-overlayfs lvm2 "${HOOKS[@]:i}" )
break
fi
done
EOF
2023-10-31 08:53:18 +00:00
cat >"$arg_mountpoint/usr/lib/initcpio/install/umorpha-overlayfs" <<-'EOF'
#!/hint/bash
# Copyright (C) 2023 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
build() {
2023-10-31 23:18:47 +00:00
add_module 'overlay'
2023-10-31 08:53:18 +00:00
2023-10-31 23:18:47 +00:00
add_runscript
2023-10-31 08:53:18 +00:00
}
help() {
2023-10-31 23:18:47 +00:00
cat <<HELPEOF
2023-10-31 08:53:18 +00:00
This hook mounts a read-only root filesystem with a persistent
overlayfs on top of it.
Boot parameters:
- overlay_root
- overlay_rootfstype
- overlay_rootflags
2023-10-31 23:18:47 +00:00
- boot
- bootfstype
- bootflags
HELPEOF
2023-10-31 08:53:18 +00:00
}
EOF
cat >"$arg_mountpoint/usr/lib/initcpio/hooks/umorpha-overlayfs" <<-'EOF'
#!/hint/bash
# Copyright (C) 2023 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
# args: errmsg
umorpha_emergency_shell() {
2023-10-31 23:18:47 +00:00
run_hookfunctions 'run_emergencyhook' 'emergency hook' $EMERGENCYHOOKS
err "$*"
echo "You are now being dropped into an emergency shell."
launch_interactive_shell
msg "Trying to continue (this will most likely fail) ..."
2023-10-31 08:53:18 +00:00
}
# args: /path/to/newroot
umorpha_mount_handler() {
2023-10-31 23:18:47 +00:00
local newroot="$1"
msg ":: mounting '$root'+'$overlay_root' on real root"
mkdir -p /run/umorpha-root/root /run/umorpha-root/overlay
local mnt_root=/run/umorpha-root/root
local mnt_overlay=/run/umorpha-root/overlay
if ! mount -t "${rootfstype:-auto}" -o "ro${rootflags:+,$rootflags}" "$root" "$mnt_root"; then
umorpha_emergency_shell "Failed to mount root '$root'"
fi
if ! mount -t "${overlay_rootfstype:-auto}" -o "${rwopt:-ro}${overlay_rootflags:+,$overlayrootflags}" "$overlay_root" "$mnt_overlay"; then
umorpha_emergency_shell "Failed to mount overlay root '$overlay_root'"
fi
mkdir -p -- "$mnt_overlay/upperdir" "$mnt_overlay/workdir"
if ! mount -t overlay -o "lowerdir=${mnt_root},upperdir=${mnt_overlay}/upperdir,workdir=${mnt_overlay}/workdir" umorpha-rootfs "$newroot"; then
umorpha_emergency_shell "Failed to mount overlayfs lowerdir='${mnt_root}' overlaydir='${mnt_overlay}'"
fi
if [ -n "$boot" ]; then
local bootdev
if bootdev=$(resolve_device "$boot"); then
case "$boot" in
'UUID='* | 'LABEL='* | 'PARTUUID='* | 'PARTLABEL='*) : ;;
*) boot="$bootdev" ;;
esac
fi
msg ":: mounting '${boot}' on /boot"
if ! mount -t "${bootfstype:-auto}" -o "${rwopt:-ro}${bootflags:+,$bootflags}" "$boot" "$newroot/boot"; then
umorpha_emergency_shell "Failed to mount boot '$boot'"
fi
fi
2023-10-31 08:53:18 +00:00
}
run_hook() {
2023-10-31 23:18:47 +00:00
export mount_handler='umorpha_mount_handler'
2023-10-31 08:53:18 +00:00
}
EOF
2023-10-31 07:43:40 +00:00
}