It is sensible that if you have any sort of block device encryption implemented on your GNU/Linux system, to encrypt your swap partition too, as any decrypted data may be written in cleartext at any time to the swap.
Looking at the debian man page for "crypttab" I see an example of creating a randomly keyed swap partition on boot-up, so the key is set randomly as the boot proceeds and known only to the system itself:
# Encrypted swap device
cswap /dev/sda6 /dev/urandom cipher=aes-cbc-essiv:sha256,hash=ripemd160,size=256,swap
In this example the swap device is referred to by a conventional dev path i.e. /dev/sda6
Absolute device paths are subject to change and be re-assigned at boot-up if, say a usb drive is plugged in, for example. A user would be very unhappy if /dev/sda6
happened to be a different partition than expected and it was subsequently overwritten with random swap data!!
So the solution would seem to be: use a UUID instead of a device path (as a UUID shouldn't change), replacing /dev/sda6
with /dev/disk/by-uuid/<whatever the uuid of dev/sda6 is>
BUT ... here's the problem: Every time cryptsetup recreates the encrypted swap partition at boot time it generates a new UUID for it! Doh!
So we need to preserve the UUID of this encrypted filesystem somehow. I think cryptsetup can do this with its --offset
switch, allowing for preservation of the LUKS header and thus the UUID.
I have found this URL: https://wiki.archlinux.org/index.php/System_Encryption_with_LUKS#Using_UUIDs_with_encrypted_swap_partitions
Does anyone know how to implement the solution described for Arch Linux on the Debian OS? The init scripts referred to in the document seem not to exist on the Debian OS
Thanks!
EDIT
One could use ecryptfs to achieve the same ends (encrypted swap space) using the command:
ecryptfs-setup-swap
Without the problems that beset block device encryption.
Have a look at this AskUbuntu query
In /etc/crypttab, use /dev/disk/by-id instead of /dev/disk/by-UUID to refer to your swap partition. For example, your /etc/fstab entry for swap might be
Then the correct corresponding entry in /etc/crypttab would be something like
Notice that the device above is referred to by /dev/disk/by-id which you can find out for your drive by typing the following at the CLI:
In my /etc/crypttab, I have
Here /dev/mapper/cswap is a logical volume created by LVM, which takes care of correctly assigning logical volume names regardless of the drive letter names. It also allows me to easily resize my swap partition.
To do a swap on encrypted file over LVM partition while using
by-uuid
option is not possible ... duringinitramfs
boot stage or during most ofsystemd
boot stages.Here's why...
Or skip to "Steps" section to get started for an equally safe encrypted LVM swap space that gets created during normal bootup stage (for systemd, Debian 10).
Assumptions
You created an LVM partition named
lv_swap_encrypted
using 100% ofvg_swap
VG space as followed:such that our goal is to have the following logical diagram:
Problem
Take a look at the newly created LVM partition named
vg_swap-lv_swap_encrypted
just for use with swap (or encrypted swap):Notice that there is no UUID for that
vg_swap-lv_swap_encrypted
partition?You COULD execute
mkswap vg_swap-lv_swap_encrypted
on that LVM and obtain that UUID but then we would be skipping the much needed encryption part that we wanted.WARNING: Oh, you executed
mkswap
? Then wipe it clean by executing:Executing
dmsetup info /dev/dm-7
reveals a different UUID format:I already tried the
UUID=
option with that funky LVM value above won't work in the 2nd field of/etc/crypttab
either. Costed me a reboot here.Note: X represent VG UUID and Y is LV UUID in LVM parlance.
Availability of Device Links
During initramfs boot stage, not many symbolic links are available for use with boot-up stage. We investigate which device paths are available for encrypted swap.
No need to do
update-initramfs
or any rebuilding of initfs here.LVM Linked Device Names
In the
/dev
directory, LVM creates several symbolic links for each LV (logical volume) partition thatlvcreate
(orsystemd-lvm2
during bootup) activates.During initramfs boot stage, LVM activation creates the following block and symbolic files links:
/dev/dm-X
/dev/mapper/vg_swap-lv_swap_encrypted
/dev/vg_swap/lv_swap_encrypted
During normal kernel boot stage, OS takes care of the rest of the following links. No need to investigate by whom or where, as our focus is on link availability for encrypted swap partition DURING
initramfs
boot stage./dev/block/254:X
/dev/254:X
/dev/vg_swap-lv_swap_encrypted
/dev/lv_swap_encrypted
/dev/dm-name-vg_swap-lv_swap_encrypted
/dev/disk/by-id/dm-uuid-LVM-XXXXXXXXXXXXXXXXXXXXXXXXXYYYYYYYYYYYYYYYYYYYYYYYY YYYYYYYYYYYYYYY
/dev/dm-uuid-LVM-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY YYYY
Twenty reboots into runlevel 2 later, I've learned that it will never be possible (during
initramfs
boot stage) to use mountby-uuid
because as each time the swapper writes unencrypted data into the first couple of blocks into the encrypted partition, whatever UUID it had before its encrypted data wrote over it is now long, long gone.For normal post-bootup stage, UUID can be used in
cryptsetup
/cryptdisks_*
.Let's step back away from physical and extended partition UUID to take a look at LVM UUID here: LVM management pretty much ensures that each LVM partition is highly unique.
DANGER: In short, only danger to you is by reusing this same LVM parition for something else other than swap. If so, by the next reboot, that partition gets converted back to a swap space again and any newly installed data is gone (just be glad it's not the old and precious data).
SETUP
This arrangement of going by LVM VG/LV name would be impervious to data loss caused by addition, removal, and repartitioning of a hard drive, whether it's a swap or a filesystem.
In this
/etc/crypttab
example:swap space using memory-based randomized-key AND using VG/LV pathname, this approach will take your existing swap partition and convert it to an encryp ted swap space.
Basically, the following steps will
/etc/crypttab
/etc/fstab
mkswap
)all to re-encrypt the swap space now and after every reboots.
Disable Hibernate
To use randomized key, you should understand that hibernate feature gets no longer supported.
You should disable hibernate as well before going on further. Hibernate makes use of swap space and randomized keys effectively makes prior hibernation quite unusable.
Note: we WANT to set the physical or extend partition to SWAP to fool the systemd
initramfs
generator into NOT disabling the SUSPEND/SLEEP part.On Debian, you can execute:
Good news is that hardware suspend or sleep mode will still work.
Create LVM
These steps assumes that you have a physical or extended (and not a LVM) partiti on for a swap partition already.
Re-type the Swap Partition
Assume that
/dev/sda
is the drive name. Assume that3
is the partition number of/dev/sda
drive that swap is located on.Execute
fdisk
and uset
option to relabel theLinux swap
intoLinux LVM
by entering in31
(or you can useL
to list all options after thatt
comma nd.Create LVM Physical Volume
Create a physical volume for LVM, by executing:
Create LVM Volume Group
Create a volume group for LVM, by executing:
Nice thing about a volume group is that you can expand your swap space later on with another physical hard drive.
Create LVM Logical Volume
Create a logical volume for LVM, by executing:
Now we have a
/dev/dm-7
block file available.You will find that:
/etc/mapper/vg_swap-lv_swap_encrypted
/etc/vg_swap/lv_swap_encrypted
all points to the same
/dev/dm-7
.I will advocate using
/etc/mapper/vg_swap-lv_swap_encrypted
because it is the consistent location that most of us can expect.Not many know to look for this custom LVM Volume Group name here like
/dev/vg_swap
. Some other (like most BOFH) who create hard-to-find/etc/mapper_other
(VG name ofmapper
) to make it harder to look for.We will not be using
/dev/dm-7
because it could bedm-19
or other, depending on your partition layout.Now we have a pathway to a drive, a logical drive, but not yet an encrypted driv e with random key pointing to
/dev/mapper/vg_swap-lv_swap_encrypted
.Encrypted Swap Partition
Add or modify the following entry into
/etc/crypttab
:Then activate the encryption and create yet another dm device under
/dev/mapper
block device: Choose one of two commands below:or
A new symbolic link has been made, execute
lsblk
You could look at the UUID for that LVM partition, but that's useless at this point.
Execute
lsblk -o name,uuid,mountpoint
Newly created block device to
/dev/mapper/swap_unencrypted
now exists.Details on /dev/mapper/swap_unencrypted:
Create Swap Space
Create the swap space:
Now notice the UUID created for
swap_unencrypted
? This will always be different for each reboot.Activate Swap Space
In
/etc/fstab
, modify or add the line of the swap entry:Then execute:
Swap space is now being encrypted using a random key.
Since dm-crypt is using UUID provided by LVM LV block device, this should be a sufficient answer to a functionally secured encrypted swap space.
Conclusion
UUID cannot be used for encrypted swap partition due to schotastic nature of encrypted data that prevents identification of such partition (swap, ext4, btfs).
UUID cannot be used for unencrypted swap partition provided by
cryptsetup
because our goal is to refresh the swap space at each reboot.You could use
/dev/disk/by-id
.But LVM offers more than enough sufficient protection by the virtue of YOUR unique naming convention of each the VG and LV partitions.
Above setup provides an uniquely encrypted swap space locator which will be scrambled yet unrecoverable after each reboot (or in event of rare case, LVM remount.)
Try implementing the rest of the solution, ignoring the init file. It looks like that bit of init script is only there to protect you. Either Debian doesn't protect you in that way, or it will give you an error message when you try to boot with it that will hopefully lead you to the right place.
I'd also be careful that IIRC Debian and ArchLinux have different formats for /etc/crypttab (crazy, I know, but I moved from Ubuntu to Arch a couple years ago and eventually decided to use straight bash rather than meddle with crypttabs).
run ecryptfs-setup-swap or manually:
This Configuration uses randomly generated keys at boot and will not support Hibernation to hard disk! You Should Disable Hibernation through your respectie DE Power Management Utility and set it to Shutdown on Critical to avoid Data Loss!
Switch to an admin/root account
su root or sudo for each command
Disable Swap
swapoff -a
Locate the existing Swap Partition
lsblk
example: sda3 8:3 0 8G 0 part [SWAP]
Overwrite Old Swap
dd if=/dev/zero bs=1024000 of=/dev/sda<#>
example: dd if=/dev/zero bs=1024000 of=/dev/sda3
FSTAB setup
vim /etc/fstab
Replace old SWAP device with crypttab mapper name: /dev/mapper/cswap
OLD UUID=d03f3d8e-7888-48a5-b3cc-41de8dbbcfdc swap swap defaults 0 0
NEW
/dev/mapper/cswap none swap pri=1,defaults 0 0
Crypto Setup
ls -lF /dev/disk/by-id
Example: ata-HGST_HTS545050A7E680_TEK55D4F0BU3GV-part3 -> ../../sda3
vim /etc/crypttab
cswap /dev/disk/by-id/ata-HGST_HTS545050A7E680_TEK55D4F0BU3GV-part3 /dev/urandom swap,cipher=aes-cbc-essiv:sha256,size=256
Active Encrypted Swap
Reboot The Computer
Verify Enctypted Swap Operations
dmsetup -C info
Example: cswap 253 0 L--w 2 1 0 CRYPT-PLAIN-cswap
lsblk
Example ├─sda3 8:3 0 8G 0 part
│ └─cswap 253:0 0 8G 0 crypt [SWAP]
cat /proc/swaps
example: Filename Type Size Used Priority /dev/dm-0 partition 8385532 0 -1
If you're using GPT partition tables, then for instance
/dev/disk/by-partuuid/d1a59352-aa44-4748-85fc-4cf785702f99
is a stable device name that's not going to change even when the contents change.Not Debian but Fedora 36
Confirming that @Brian Minton's idea of using the Partition UUID held in the GUID Partition Table can be effectively used to solve this particular problem. Of course, you need a disk structure that uses GUID Partition Table, not the old-school MS-DOS partition table
For example, I want to have the volatile but encrypted swap partition on
/dev/sda3
and we can find the GUID Partition UUID by looking at/dev/disk/by-partuuid/
(or by callingblkid /dev/sda3
)And then we just need to add appropriate lines to
/etc/cryptsetup
and/etc/fstab
as shown (in orange) in the diagram below.After boot, we find the following in the
systemd
log withjournalctl
: