Skip to content

Instantly share code, notes, and snippets.

@whitslack
Last active September 25, 2024 22:01
Show Gist options
  • Save whitslack/6fae4f56a2e3c7be98ceafb7c542db5f to your computer and use it in GitHub Desktop.
Save whitslack/6fae4f56a2e3c7be98ceafb7c542db5f to your computer and use it in GitHub Desktop.
Initramfs /init script for Linux hibernation/swsusp resume from encrypted swap partition
#!/bin/sh
# Discussion at https://community.frame.work/t/fw16-debian-bookworm-hibernate-not-reliable/52980/11?u=whitslack
mount --no-mtab -t devtmpfs dev /dev
mount --no-mtab -t proc proc /proc
mount --no-mtab -t sysfs sysfs /sys
loadkeys dvorak
echo 51 >/sys/class/backlight/amdgpu_bl0/brightness
read -r cmdline </proc/cmdline
for arg in ${cmdline} ; do
case "${arg}" in
root=*)
root="${arg#*=}"
;;
rootflags=*)
rootflags="${arg#*=}"
;;
rootfstype=*)
rootfstype="${arg#*=}"
;;
ro)
rw=ro
;;
rw)
rw=rw
;;
data=*)
data="${arg#*=}"
;;
swap=*)
swap="${arg#*=}"
;;
init=*)
init="${arg#*=}"
;;
single)
set S
;;
esac
done
root="$(findfs "${root-PARTLABEL=Root}")"
data="$(findfs "${data-PARTLABEL=Data}")"
swap="$(findfs "${swap-PARTLABEL=Swap}")"
cryptsetup() {(
read -r printk </proc/sys/kernel/printk
echo 1 >/proc/sys/kernel/printk
command cryptsetup "${@}" ; ret=$?
printf '%s' "${printk}" >/proc/sys/kernel/printk
return "${ret}"
)}
if [ -n "${data}" -a -n "${swap}" ] ; then
until cryptsetup luksOpen --disable-locks --test-passphrase --link-vk-to-keyring @us::dmcrypt "${data}" ; do : ; done
keyctl pipe %user:dmcrypt | cryptsetup open --type plain --cipher aes-xts-plain64 --key-size 512 --key-file - --sector-size 4096 --allow-discards "${swap}" swap
stat --format='%Hr:%Lr' /dev/mapper/swap >/sys/power/resume
fi
mount --no-mtab ${rootfstype+-t "${rootfstype}"} -o "${rw:-ro}${rootflags:+,${rootflags}}" "${root:?}" /root
umount --no-mtab /sys /proc /dev
exec switch_root /root "${init:-/sbin/init}" "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment