Introduction
After doing some research on Google, it seemed to me that the easiest and best way to do partition encryption in Linux is with an encrypted loopback device. Truecrypt gets an honorable mention, but seems more geared towards removable media than hard disks.
By default, Linux only supports DES and XOR encryption for loopback devices, but a module called cryptoloop allows various other more modern algorithms. The Ubuntu 6.10 kernel comes with cryptoloop support compiled in, but other people may need to recompile their kernels to get this working.
Partitioning
The contents of most of my disk aren't sensitive - the files in /lib, /usr, /bin, etc. are from stock Ubuntu packages and I don't care about protecting them. The two things that I do want protected are my home directory and my /swap space. Keeping these as seperate partitions improves performance, so I chose to partition my 40 gig drive like this:
Device Boot Start End Blocks Id System /dev/hda1 * 1 1217 9775521 83 Linux /dev/hda2 1218 1461 1959930 89 Unknown /dev/hda3 1462 4864 27334597+ 89 UnknownThe first Unknown partition is for swap, and the second will be the /home filesystem. I chose 89 arbitrarily as a hex identifier that isn't in use by anything else.
Initial Setup
Here are the commands to set up the devices for the first time. Make sure the cryptoloop module is loaded before running these.
losetup -e aes /dev/loop0 /dev/hda2 mkswap /dev/hda2 swapon /dev/hda2 losetup -e aes /dev/loop1 /dev/hda3 mke2fs -j /dev/loop1 mount /dev/loop1 /homeBe sure to remember the passwords you use when running losetup for the first time!
Passwords and Decryption
For the losetup program, there's no difference between setting up an encrypted volume for the first time and unlocking it for later use. It asks for a password and uses that to encrypt and decrypt data on the device - it has no notion of the password being correct or incorrect. If you enter a different password than the one you used to create the volume, losetup won't complain, but all of your data will be scrambled.
I found the best way to get around this problem is to check the contents of the decrypted volume with the "file -s" command. If file tells you the volume has something useful on it, like a swap file or a filesystem, then your password was right. If it just tells you the device contains "data," then your password was wrong.
Automation
I don't want to have to log in as root and run those commands every time I reboot my machine, so I wrote a shell script which will do everything for me and called it /usr/local/sbin/cryptoloop.sh.
#!/bin/sh
dovolume () {
NAME=$1
LOOPDEV=$2
CRYPTDEV=$3
CONTENT=$4
MOUNTCOMMAND=$5
i=0
while ! file -s /dev/$LOOPDEV | grep -q "$CONTENT"; do
losetup -d /dev/$LOOPDEV 2>/dev/null
i=$((i+1))
[ $i -lt 4 ] || return 1
echo -n "$NAME $i "
losetup -e aes /dev/$LOOPDEV /dev/$CRYPTDEV
done
[ $i -eq 0 ] || $MOUNTCOMMAND
}
dovolume "Swap" loop0 hda2 "swap file" "swapon /dev/loop0"
dovolume "Home" loop1 hda3 "ext3 filesystem data" "mount -o
noexec,nodev,nosuid /dev/loop1 /home"
For each volume you want to decrypt, this script will give you three chances
to enter the right password. If you do, it'll run the "mount command" of
your choosing. It's also safe to run multiple times - if the devices are
already set up, then it'll just silently exit.
Login Integration
Running one command to decrypt and mount the volumes is a big improvement, but you still have to log in as root from one of the consoles to do it. Doing that is a big pain, especially if you don't have a root password set and/or you run X on startup.
Instead I configured gdm, my graphical login manager of choice, to run the script automatically whenever a user logs in. To do so, I put the following in /etc/gdm/PostLogin/Default. This runs as root, right after the user's password has been verified.
#!/bin/sh /usr/bin/aterm -e /usr/local/sbin/cryptoloop.shSubstitute your favorite terminal emulator and options as you prefer. Now after a user logs in, a terminal appears over the gdm screen with our script running in it. The user enters the passwords to decrypt the swap and home partitions, they're mounted, and the user's session runs as defined in their newly-decrypted home directory.