Wednesday, July 15, 2009

Linux: Encrypted Filesystem on a Regular Disk File

Some notes on creating a LUKS-based encrypted filesystem on a regular disk file with Ubuntu 9.04 Jaunty.


Create the Encrypted Filesystem:

# create a 10M file
$ dd if=/dev/urandom of=testfs bs=1M count=10

# associate it with the loop device
$ losetup /dev/loop0 testfs

# encrypt it (will ask for password to use)
$ cryptsetup luksFormat /dev/loop0

# open the encrypted loop device
$ cryptsetup luksOpen /dev/loop0 testfs

# format it with ext2 (or whatever you prefer)
$ mkfs.ext2 /dev/mapper/testfs

# mount it
$ mount /dev/mapper/testfs /mnt/test

# confirm mount
$ df -h /mnt/test
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/testfs    9.2M   88K  8.7M   1% /mnt/test


Unmount the Filesystem:

# unmount it
$ umount /mnt/test

# close encryption
$ cryptsetup luksClose /dev/mapper/testfs

# release loop device
$ losetup -d /dev/loop0


Mount an Encrypted Filesystem:

# associate file with the loop device
$ losetup /dev/loop0 testfs

# open the encrypted loop device
$ cryptsetup luksOpen /dev/loop0 testfs

# mount it
$ mount /dev/mapper/testfs /mnt/test

No comments: