Encrypted Swap
Encrypted swap setup on Debian bullseye/bookworm
Prerequisites
Ensure the following are installed:
- lvm2
- cryptsetup
Disable and overwrite existing swap partition
This is only necessary if the partition was previously used for a swap partition.
- Use fdisk to determine which partition is the swap partition. For rest of this guide, this is represented by /dev/sdaX.
swapoff /dev/sdaX
- Comment out the swap line in /etc/fstab
- Overwrite the existing swap partition data
dd if=/dev/urandom of=/dev/sdaX
Set up LVM on the partition
- Use fdisk to change partition type to Linux LVM (type code is 8E).
pvcreate /dev/sdaX
vgcreate swaps /dev/sdaX
- Find out the available size of the volume group using vgdisplay (example: 976.00 MiB).
lvcreate -L 976.00M -n swap swaps
Set up encryption
- Add to /etc/crypttab:
cryptswap /dev/mapper/swaps-swap /dev/urandom swap
- Edit fstab:
/dev/mapper/cryptswap none swap sw 0 0
This should be as simple as changing the first item in the old fstab swap line - Disable hibernate (can't restore from a randomly encrypted swap!)
systemctl mask hibernate.target hybrid-sleep.target
rm /etc/initramfs-tools/conf.d/resume
update-initramfs -u
- Reboot and verify with
free
that the swap is available.
Sources:
https://www.howtogeek.com/howto/40702/how-to-manage-and-use-lvm-logical-volume-management-in-ubuntu/
https://www.supertechcrew.com/linux-create-encrypted-swap/
https://serverfault.com/a/1031460
https://unix.stackexchange.com/questions/590518/boot-blocks-with-gave-up-waiting-for-suspend-resume-device