个人工具

UbuntuHelp:EncryptedFilesystemLVMHowto

来自Ubuntu中文

Wikibot讨论 | 贡献2007年11月22日 (四) 12:31的版本

跳转至: 导航, 搜索

Installing Ubuntu 7.04 on an Encrypted LVM Partition For Root, Swap, and Home

by Nico Gulden

This howto describes the aspects to be noticed installing Ubuntu 7.04 on an encrypted LVM partition. It is based on the howto below written by John Bindel. It gave me the inspiration to try it with Ubuntu 7.04. The steps are basically the same as for Ubuntu 6.06.

First of all, I want to thank John Bindel for the inspiration and Ilkka Tuohela for the right hint to get everything working.

/boot is the only unencrypted partition. The other partitions all reside on one volume group in lvm. The physical volume below the volume group is configured on top an encrypted partition.

Preparation with the live-cd

Follow the steps outline below for Ubuntu 6.06 using the Ubuntu 7.04 desktop install disk. Partition your harddisc using some partition editor, fdisk or System->Administration->Gnome Partition Editor. Make one partition of 100MB for /boot which is sda1 in this example. Create an extended partition for the left space on the disc. Create a logical partition with rest of the created extended partition, sda5 in this example.

For further preparation you'll need a working internet connection. John suggested installing Ubuntu on an unencrypted primary partition. I did all necessary preparation with the live cd. First you'll need to edit your /etc/apt/sources.list and uncomment the universe repository entries, update your package list and install cryptsetup, initramfs-tools, hashalot, lvm2.

You should activate the universe repository in your /etc/apt/sources.list

# sudo aptitude update
# sudo aptitude -y install cryptsetup initramfs-tools hashalot lvm2

After installing the needed packages, your are ready for the setup of lvm2 on your hard disk. Before you can start, you need to load the following modules:

# sudo modprobe aes-i586
# sudo modprobe dm-crypt
# sudo modprobe dm-mod

Encrypt data partition

From John's Tutorial:

>>> Check the future encrypted LVM physical partition for errors. This will take several minutes for each partition. This fills each partition with pseudorandom data from the not-so-random libc pseudorandom source

# sudo /sbin/badblocks -c 10240 -s -w -t random -v /dev/sda5

Fill the partitions with random data. This may take MANY hours for the large partitions, on average 1.6M/sec of data is written to disk, so a 10GB partition might take around 2 hours, and 100GB partition might take a bit under 20 hours. . The /dev/urandom source is a good source of randomization that should prevent attackers from being able to determine where data actually resides on the encrypted filesystem, which would help them know what they should try to decrypt. The /dev/random source is even better, but it might take hundreds of years to fill the disk from it.

# sudo dd if=/dev/urandom of=/dev/sda5

<<<

In the next step you create the encryption on the logical partition and unlock the partition.

# sudo cryptsetup -y --cipher aes-cbc-essiv:sha256 --key-size 256 luksFormat /dev/sda5
WARNING!
========
This will overwrite data on /dev/sda5 irrevocably.

Are you sure? (Type uppercase yes): YES
Enter LUKS passphrase: (enter passphrase)
Verify passphrase: (repeat passphrase)

# sudo cryptsetup luksOpen /dev/sda5 pvcrypt

Create LVM

Create a physical volume and on top of that the volume group with all necessary logical volumes.

# sudo pvcreate /dev/mapper/pvcrypt
# sudo vgcreate vg /dev/mapper/pvcrypt
# sudo lvcreate -n <lvname> -L <size> vg

Create logical volumes for /usr, /home/, /tmp, /var, /opt, root and swap using the lvcreate command according to the mentioned schema. Make sure you don't use any special characters like underscores or hyphens for the logical volume name. That will generate problems in the later setup.

I got the following setup for my logical volumes.

# sudo lvdisplay -C
home       vg   -wi-ao  10,00G
swap       vg   -wi-ao 512,00M
opt        vg   -wi-ao 200,00M
root       vg   -wi-ao 300,00M
tmp        vg   -wi-ao 200,00M
usr        vg   -wi-ao   4,00G
var        vg   -wi-ao   1,00G

Put a filesystem on each logical volume. Replace <lvname> with each logical volume name you used creating you logical volumes.

# sudo mkswap /dev/mapper/vg-swap
# sudo mkfs.ext3 /dev/mapper/vg-<lvname>

Install Ubuntu

Begin the installation of Ubuntu by clicking on the install icon on the desktop of the livecd. Follow the steps in the wizard until step four where it comes to the preparation of the hard disk. Choose manual partitioning, because you already have created all necessary partitions. You just need to map the partitions to the appropriate mount points, choose a filesystem and let the installer create the filesystem. MAKE SURE YOU MAP /dev/sda1 to /boot

The next steps will continue just as an usual installation.

Final preparation

After the installation using the wizard, there are some steps to fulfill in order to let your fresh installed ubuntu come up correctly.

The following steps have to be followed:

  • Mount partitions & chroot
  • Install missing software to fresh installed ubuntu
  • Customize /etc/crypttab, /etc/fstab, /boot/grub/menu.lst
  • Rebuild ramdisk

Mount Partitions & chroot

# cd /mnt
# sudo mkdir root
# sudo mount -t ext3 /dev/mapper/vg-root /mnt/root
# sudo mount -t ext3 /dev/sda1 /mnt/root/boot
# sudo mount -t ext3 /dev/mapper/vg-home /mnt/root/home
# sudo mount -t ext3 /dev/mapper/vg-opt /mnt/root/opt
# sudo mount -t ext3 /dev/mapper/vg-usr /mnt/root/usr
# sudo mount -t ext3 /dev/mapper/vg-var /mnt/root/var
# sudo mount -t ext3 /dev/mapper/vg-tmp /mnt/root/tmp

Chroot

# sudo chroot /mnt/root
#(chroot) sudo mount -t proc proc /proc
#(chroot) sudo mount -t sysfs sys /sys

Install missing software

#(chroot) sudo aptitude update
#(chroot) sudo aptitude install cryptsetup hashalot initramfs-tools lvm2

Customize files

Edit /etc/crypttab and enter the following line.

# <target name>		<source device>	<key file> 	<options>
pvcrypt		/dev/sda5	none		luks,retry=1,lvm=vg

You recognize the target name and the source device name. It comes from the cryptsetup luksOpen /dev/sda5 pvcrypt command. With the options you tell cryptsetup that it shall use the volume group name vg in this example.

According to the example your /etc/fstab should look like this

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            		/proc           proc    defaults        0       0
/dev/mapper/vg-root 		/               ext3    defaults,errors=remount-ro 0       1
/dev/sda1			/boot		ext3	defaults	0	2
/dev/mapper/vg-home		/home           ext3    defaults        0       2
/dev/mapper/vg-opt		/opt            ext3    defaults        0       2
/dev/mapper/vg-tmp 		/tmp            ext3    defaults        0       2
/dev/mapper/vg-usr	 	/usr            ext3    defaults        0       2
/dev/mapper/vg-var	 	/var            ext3    defaults        0       2
/dev/mapper/vg-swap 		none            swap    sw              0       0
/dev/scd0       		/media/cdrom0   udf,iso9660 user,noauto     0       0
/dev/fd0		        /media/floppy0  auto    rw,user,noauto  0       0

Your /boot/grub/menu.lst should have the following or similar entry

title	Ubuntu
root	(hd0,0)
kernel	/vmlinuz-2.6.20-16-generic root=/dev/mapper/vg-root ro quiet
initrd	/initrd.img-2.6.20-16-generic
quiet
savedefault

Rebuild ramdisk

Edit the file /etc/initramfs-tools/modules to make sure the correct modules are included in the ramdisk

aes-i586
dm-crypt
dm-mod
sha256

Run the following command to rebuild the ramdisk.

# sudo update-initramfs -k all -c

After building the ramdisk it should be checked.

#(chroot) cd /tmp
#(chroot) sudo mkdir foo
#(chroot) cd foo/
#(chroot) sudo zcat /boot/initrd.img-$(uname -r)|cpio -iv
#(chroot) sudo cat /tmp/foo/conf/conf.d/cryptroot

If initramfs-cryptsetup did not understand the setup, the /tmp/foo/conf/conf.d/cryptroot file is completely missing and it does not make even sense to try booting. The lvm lines here are just names it expects to be available after setup and really don't matter in your case.

If everything went fine and the /tmp/foo/conf/conf.d/cryptroot is present you should give a try and reboot. Right at the beginning of the boot process you should be asked for the physical volume password.

Booting fails

If booting fails and you need to rebuild the ramdisk or fix some configuration files, follow these steps. I assume you know the details, therefore they are just outline.

  • Boot the ubuntu live cd
  • Uncomment the universe repository in the sources.list, update the package list and install cryptsetup, initramfs-tools, hashalot, lvm2
  • modprobe the modules aes-i586, dm-crypt and dm-mod
  • Unlock the physical volume: cryptsetup luksOpen /dev/sda5 pvcrypt
  • Detect the logical volume: vgscan; vgchange -a y vg
  • Mount the partitions to /mnt/root
  • Chroot into /mnt/root and mount proc and sysfs
  • Make your changes

Some Notes

  • Suspend or suspend2 don't work with this configuration. If you have a working configuration with suspend or suspend2, please append to this article or post a separate one. There might be some people out there looking for it. Thanks.


Installing Ubuntu 6.06 on an Encrypted LVM Partition For Root, Swap, and Home

by John Bindel ([email protected])

This how-to explains the process of installing Ubuntu 6.06 on an encrypted LVM partition. One encrypted physical partition is used, and the logical volume manager is used to manage the operating system volumes. Only /boot is unencrypted.

Install

Boot the Ubuntu 6.06 desktop install disk.

Partition the disk with System->Administration->Gnome Partition Editor

Make 3GB ext3 primary partition (See NOTES at the bottom if you want to save room and use the server version, which only needs 600 MB). Make an extended partition with the rest. Make an ext3 logical partition of 200MB for /boot which is sda5 in this example. Make an ext3 logical parition with the rest as sda6. Click "apply" and then close the tool. It would be nice to install directly to encrypted LVM volumes, but the desktop installer does not seem to be capable of that. It fails after the manual paritioning step when I have tried.

Double-click on "Install".

Choose to manually edit partitions. Make no changes to partitions and click "forward". Set /dev/sda5 as the /boot mount point and /dev/sda1 as the "/" mount point. Clear all other mount points. Select reformat on these two and click "forward". Then click "install".

Click "continue" when asked about not having a swap partition. We'll add one later.

Let the system install and then reboot.

Open a terminal and get root.

$ sudo bash

Add "universe" repositories by uncommenting lines in /etc/apt/sources.list.

Install the cryptsetup, hashalot, and initramfs-tools packages.

# apt-get update
# apt-get install cryptsetup hashalot initramfs-tools

Build an initrd image

Load the modules that will be needed.

# modprobe dm_crypt
# modprobe aes_i586
# modprobe sha256

Add the following line to /etc/kernel-img.conf:

ramdisk = /usr/sbin/mkinitramfs

Add the following lines to /etc/mkinitramfs/modules:

dm_mod
dm_crypt
sha256
aes_i586

Create file /etc/mkinitramfs/hooks/pvcrypt. This script is executed when the init ramdisk image is built.

#!/bin/sh

PREREQ=""

prereqs()
{
echo "$PREREQ"
}

case $1 in
prereqs)
prereqs
exit 0
;;
esac

if [ ! -x /sbin/cryptsetup ]; then
exit 0
fi

. /usr/share/initramfs-tools/hook-functions

mkdir ${DESTDIR}/etc/console
cp /etc/console/boottime.kmap.gz ${DESTDIR}/etc/console
copy_exec /bin/loadkeys /bin
copy_exec /usr/bin/chvt /bin
copy_exec /sbin/cryptsetup /sbin

Create file /etc/mkinitramfs/scripts/local-top/pvcrypt. This script is executed during the init bootup.

#!/bin/sh

PREREQ="udev"

prereqs()
{
echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
prereqs
exit 0
;;
esac

/bin/loadkeys /etc/console/boottime.kmap.gz
modprobe -Qb dm_crypt
modprobe -Qb aes_i586
modprobe -Qb sha256
if grep -q splash /proc/cmdline; then
/bin/chvt 1
fi
/sbin/cryptsetup luksOpen /dev/sda6 pvcrypt
if grep -q splash /proc/cmdline; then
/sbin/usplash -c &
sleep 1
fi

Make these scripts executable by root.

# chmod +x /etc/mkinitramfs/hooks/pvcrypt
# chmod +x /etc/mkinitramfs/scripts/local-top/pvcrypt


Now build a new initrd image:

# update-initramfs -u ALL


Encrypt the data partition

Check the future encrypted LVM physical partition for errors. This will take several minutes for each partition. This fills each partition with pseudorandom data from the not-so-random libc pseudorandom sourc

# /sbin/badblocks -c 10240 -s -w -t random -v /dev/sda6

Fill the partitions with random data. This may take 4 hours for the large partition. The /dev/urandom source is a good source of randomization that should prevent attackers from being able to determine where data actually resides on the encrypted filesystem, which would help them know what they should try to decrypt. The /dev/random source is even better, but it might take hundreds of years to fill the disk from it.

# dd if=/dev/urandom of=/dev/sda6

Create the encryption key for the partition.

# cryptsetup --verify-passphrase --verbose --hash=sha256 --cipher=aes-cbc-essiv:sha256 --key-size=256 luksFormat /dev/sda6
# cryptsetup luksOpen /dev/sda6 pvcrypt

Make an LVM physical volume, volume group, and logical volumes on the encrypted partition. The size of 9938 happens to be how much is left on my logical partition.

# pvcreate /dev/mapper/pvcrypt
Physical volume "/dev/mapper/pvcrypt" successfully created
# vgcreate vgcrypt /dev/mapper/pvcrypt
Volume group "vgcrypt" successfully created
# lvcreate -n lvroot -L 8G vgcrypt
Logical volume "lvroot" created
# lvcreate -n lvswap -L 1G vgcrypt
Logical volume "lvswap" created
# lvcreate -n lvhome -l 9938 vgcrypt
Logical volume "lvhome" created

Put a filesystem on each volume.

# mkfs.ext3 /dev/mapper/vgcrypt-lvroot
# mkfs.ext3 /dev/mapper/vgcrypt-lvhome
# mkswap /dev/mapper/vgcrypt-lvswap

Populate the encrypted volumes.

# mkdir /mnt/root
# mkdir /mnt/home
# mount /dev/mapper/vgcrypt-lvroot /mnt/root
# mount /dev/mapper/vgcrypt-lvhome /mnt/home
# cp -ax / /mnt/root
# rm -rf /mnt/root/home/*
# cp -ax /home/* /mnt/home

Edit /mnt/root/etc/fstab. First change this line

/dev/sda1 / ext3 defaults,errors=remount-ro 0 1

to

/dev/mapper/vgcrypt-lvroot / ext3 defaults,errors=remount-ro 0 1

And add these lines:

/dev/mapper/vgcrypt-lvhome /home	ext3	defaults			0	1
/dev/mapper/vgcrypt-lvswap none	swap	sw				0	0

Add this to the bottom of /boot/grub/menu.lst. (hd0,4) refers to the boot partition, /dev/sda5.

title           Cryptotest
root            (hd0,4)
kernel          /vmlinuz-<your kernel version here> root=/dev/mapper/vgcrypt-lvroot ro
initrd          /initrd.img-<your kernel version here>
savedefault
boot

Check the kernel version with uname -r. For example, it may be "2.6.15-26-server".

Reboot to test by typing sudo reboot. Press ESC to enter the GRUB menu and select Cryptotest. The boot process will stop waiting for the encrypted partition's passphrase. The prompt will probably be hard to see because of a lot of debugging information on the console.

If all goes well, then continue. Otherwise seek help or figure out what went wrong.

Edit /boot/grub/menu.lst to remove the lines we added at the end, and change the line that has

# kopt=root=/dev/sda1 ro

to

# kopt=root=/dev/mapper/vgcrypt-lvroot ro

Then run

$sudo update-grub


Finally make a backup key file for unencrypting the drive. Insert a blank USB thumb drive.

$ sudo dd if=/dev/random of=/media/<usbdrive>/pvcrypt-$(whoami)-key bs=1 count=256
$ sudo cryptsetup luksAddKey /dev/sda6 /media/<usbdrive>/pvcrypt-$(whomai)-key

Wipe the old partition.

# /sbin/badblocks -c 10240 -s -w -t random -v /dev/sda1


Notes

Those who do not want a large leftover partition on sda1 can make one as small as 600MB and install the server version of Ubuntu instead, or even make the /boot partition 600MB and just install the root partition there initially.

Gnome will popup a notice that it has found an encrypted partition. Cancel the dialog. It seems that /sbin/partprobe causes this dialog to popup. Any advice on getting rid of this would be appreciated.