Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1185151
  • 博文数量: 128
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 4870
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-19 14:28
文章分类

全部博文(128)

文章存档

2010年(2)

2009年(22)

2008年(104)

我的朋友

分类: LINUX

2008-05-13 14:25:43

Current versions (since 7.10) of the Ubuntu alternate installation CD provide options for automatically installing to an encrypted partition. The support for this in 8.04 LTS is well integrated, so the directions here may be used for historical purposes or when the options on the installation CD are not sufficient. The alternate installation CD uses LUKS for key management, and it does some kind of disk clearing, (hopefully) writing pseudorandom data to the entire encrypted partition. I recommend using the alternate installation CD rather than manually going through the encrypted installation procedure. -- John Bindel

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  -L  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 with each logical volume name you used creating you logical volumes.

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

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.

#                 
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.
#
#                
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.

Notes on Howto have root outside of the encrypted partition

  • When creating your partitions, create an extra for your non-encrypted data (/dev/sda6 for my example). It's advisable to put it into the same logical partition as the encrypted one.

  • When creating the physical volume ('sudo pvcreate /dev/mapper/pvcrypt') also create for sda6 ('sudo pvcreate /dev/sda6')

  • Add both pv's to the vg on creation ('sudo vgcreate vg /dev/mapper/pvcrypt /dev/sda6') or extend the original group with the new pv ('sudo vgextend vg /dev/sda6').

  • Note! My lvm2 setup was reporting the wrong pv sizes. For whatever reason I had to remove all my pv's and vg, delete /dev/sda5, /dev/sda6, my extended partition, reboot and start from scratch.

  • When creating your logical volumes, the command now is sudo lvcreate -n -L vg . Use of '/dev/mapper/pvcrypt' to put the lv in the encrypted area or '/dev/sda6' in the non-encrypted area.

  • Depending on what you want secure, you may have various logical volumes in either the encrypted or non-encrypted partitions. To have an unencrypted root do this: 'sudo lvcreate -n root -L vg /dev/sda6'

  • Everything else should work the same!

Notes for making it work in Kubuntu 7.10 AMD64

I had a few problems makeing this work on Kubuntu 7.10 AMD64. I'm not sure if the problems were due to Kubuntu, 7.10, or AMD64, but here's what I did to make it work.

Problem 1: Use kernel module 'aes-x86_64' on AMD64 distro instead of 'aes-i586'

Problem 2: The /tmp/foo/conf/conf.d/cryptroot wasn't being created by the 'update-initramfs' in the last main step. Manually create it:

/etc/initramfs-tools/conf.d/cryptroot

CRYPTROOT=target=pvcrypt,source=/dev/sda5

Remember to run 'sudo update-initramfs -k all -c' This should let you boot.

Problem 3: cryptosetup is run and asks for your password 3 times when booting. The first is necessary and done in usplash environment if you don't remove the splash option in /boot/grub/menu.lst. I'm not sure what's responsible for the first run, but the 2nd and 3rd are done by the init.d scripts after pvcrypt is uncrypted by the first run. They can be avoided by commenting out the line we added to /etc/crypttab earlier.

With those 3 problems fixed, my system was working beautifully!

Notes for making it work in Xubuntu 8.04 (Hardy Heron Alpha 4) AMD64

The guide worked beautifully and everything is up an running INCLUDING SUSPEND!!!

Here are a few of the customizations I performed.

AES Kernel Module: Use the kernel module 'aes-x86_64' instead of aes-i586. Edit /etc/modprobe.d/aliases and add the line "alias aes aes-x86_64"

Suspend\Resume: I can't use the basic suspend\resume functionality due to having an ATI FireGL card so to get suspend I installed uswsusp .

To get it working do the following

# sudo apt-get remove hibernate
# sudo apt-get install uswsusp
# sudo dpkg-divert --rename --divert /usr/sbin/pmi-disabled /usr/sbin/pmi

This information was found from a blog posting "[WWW] http://blog.paulbetts.org/index.php/2007/02/11/fixing-software-suspend-hibernate-with-uswsusp-in-ubuntu-feisty-and-edgy/".

Next modify your initramfs so it doesn't try the default resume, this isn't necessary but I felt it cleaned things up some.

# sudo vi /etc/initramfs-tools/conf.d/resume

Comment out the RESUME line and then rebuilt the initramfs

# sudo sudo update-initramfs -k all -c

And there you go, an encrypted LVM setup on x86_64 with Suspend\Resume.

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

by John Bindel ()

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- root=/dev/mapper/vgcrypt-lvroot ro
    initrd          /initrd.img-
    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//pvcrypt-$(whoami)-key bs=1 count=256 
    $ sudo cryptsetup luksAddKey /dev/sda6 /media//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.

阅读(2525) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~