个人工具

“UbuntuHelp:EncryptedFilesystemLVMHowto”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第32行: 第32行:
 
Open a terminal and get root.
 
Open a terminal and get root.
  
<code><nowiki>$ sudo bash</nowiki></code>
+
<code><nowiki>$ sudo bash</nowiki></code>
  
 
Add "universe" repositories by uncommenting lines in /etc/apt/sources.list.
 
Add "universe" repositories by uncommenting lines in /etc/apt/sources.list.
第38行: 第38行:
 
Install the cryptsetup, hashalot, and initramfs-tools packages.
 
Install the cryptsetup, hashalot, and initramfs-tools packages.
  
<pre><nowiki># apt-get update
+
<pre><nowiki># apt-get update
 +
# apt-get install cryptsetup hashalot initramfs-tools</nowiki></pre>
  
 
=== Build an initrd image ===
 
=== Build an initrd image ===
第44行: 第45行:
 
Load the modules that will be needed.
 
Load the modules that will be needed.
  
<pre><nowiki># modprobe dm_crypt
+
<pre><nowiki># modprobe dm_crypt
 +
# modprobe aes_i586
 +
# modprobe sha256</nowiki></pre>
  
 
Add the following line to /etc/kernel-img.conf:
 
Add the following line to /etc/kernel-img.conf:
  
<code><nowiki>ramdisk = /usr/sbin/mkinitramfs</nowiki></code>
+
<code><nowiki>ramdisk = /usr/sbin/mkinitramfs</nowiki></code>
  
 
Add the following lines to /etc/mkinitramfs/modules:
 
Add the following lines to /etc/mkinitramfs/modules:
  
<pre><nowiki>dm_mod
+
<pre><nowiki>dm_mod
 
dm_crypt
 
dm_crypt
 
sha256
 
sha256
第60行: 第63行:
 
when the init ramdisk image is built.
 
when the init ramdisk image is built.
  
<pre><nowiki>
+
<pre><nowiki>
 +
#!/bin/sh
  
 
PREREQ=""
 
PREREQ=""
第66行: 第70行:
 
prereqs()
 
prereqs()
 
{
 
{
        echo "$PREREQ"
+
echo "$PREREQ"
 
}
 
}
  
 
case $1 in
 
case $1 in
 
prereqs)
 
prereqs)
        prereqs
+
prereqs
        exit 0
+
exit 0
        ;;
+
;;
 
esac
 
esac
  
 
if [ ! -x /sbin/cryptsetup ]; then
 
if [ ! -x /sbin/cryptsetup ]; then
        exit 0
+
exit 0
 
fi
 
fi
  
第91行: 第95行:
 
is executed during the init bootup.
 
is executed during the init bootup.
  
<pre><nowiki>
+
<pre><nowiki>
 +
#!/bin/sh
  
 
PREREQ="udev"
 
PREREQ="udev"
第97行: 第102行:
 
prereqs()
 
prereqs()
 
{
 
{
        echo "$PREREQ"
+
echo "$PREREQ"
 
}
 
}
  
 
case $1 in
 
case $1 in
 +
# get pre-requisites
 
prereqs)
 
prereqs)
        prereqs
+
prereqs
        exit 0
+
exit 0
        ;;
+
;;
 
esac
 
esac
  
第112行: 第118行:
 
modprobe -Qb sha256
 
modprobe -Qb sha256
 
if grep -q splash /proc/cmdline; then
 
if grep -q splash /proc/cmdline; then
    /bin/chvt 1
+
/bin/chvt 1
 
fi
 
fi
 
/sbin/cryptsetup luksOpen /dev/sda6 pvcrypt
 
/sbin/cryptsetup luksOpen /dev/sda6 pvcrypt
 
if grep -q splash /proc/cmdline; then
 
if grep -q splash /proc/cmdline; then
      /sbin/usplash -c &
+
/sbin/usplash -c &
      sleep 1
+
sleep 1
 
fi</nowiki></pre>
 
fi</nowiki></pre>
  
 
Make these scripts executable by root.
 
Make these scripts executable by root.
  
<pre><nowiki># chmod +x /etc/mkinitramfs/hooks/pvcrypt
+
<pre><nowiki># chmod +x /etc/mkinitramfs/hooks/pvcrypt
 +
# chmod +x /etc/mkinitramfs/scripts/local-top/pvcrypt</nowiki></pre>
  
  
 
Now build a new initrd image:
 
Now build a new initrd image:
  
<code><nowiki># update-initramfs -u ALL</nowiki></code>
+
<code><nowiki># update-initramfs -u ALL</nowiki></code>
  
  
第134行: 第141行:
 
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
 
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
  
<code><nowiki># /sbin/badblocks -c 10240 -s -w -t random -v /dev/sda6</nowiki></code>
+
<code><nowiki># /sbin/badblocks -c 10240 -s -w -t random -v /dev/sda6</nowiki></code>
  
 
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.
 
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.
  
<code><nowiki># dd if=/dev/urandom of=/dev/sda6</nowiki></code>
+
<code><nowiki># dd if=/dev/urandom of=/dev/sda6</nowiki></code>
  
 
Create the encryption key for the partition.
 
Create the encryption key for the partition.
  
<pre><nowiki># cryptsetup --verify-passphrase --verbose --hash=sha256 --cipher=aes-cbc-essiv:sha256 --key-size=256 luksFormat /dev/sda6
+
<pre><nowiki># cryptsetup --verify-passphrase --verbose --hash=sha256 --cipher=aes-cbc-essiv:sha256 --key-size=256 luksFormat /dev/sda6
 +
# cryptsetup luksOpen /dev/sda6 pvcrypt</nowiki></pre>
  
 
Make an LVM physical volume, volume group, and logical volumes on the
 
Make an LVM physical volume, volume group, and logical volumes on the
第148行: 第156行:
 
on my logical partition.
 
on my logical partition.
  
<pre><nowiki># pvcreate /dev/mapper/pvcrypt
+
<pre><nowiki># pvcreate /dev/mapper/pvcrypt
  Physical volume "/dev/mapper/pvcrypt" successfully created
+
Physical volume "/dev/mapper/pvcrypt" successfully created
  Volume group "vgcrypt" successfully created
+
# vgcreate vgcrypt /dev/mapper/pvcrypt
  Logical volume "lvroot" created
+
Volume group "vgcrypt" successfully created
  Logical volume "lvswap" created
+
# lvcreate -n lvroot -L 8G vgcrypt
  Logical volume "lvhome" created</nowiki></pre>
+
Logical volume "lvroot" created
 +
# lvcreate -n lvswap -L 1G vgcrypt
 +
Logical volume "lvswap" created
 +
# lvcreate -n lvhome -l 9938 vgcrypt
 +
Logical volume "lvhome" created</nowiki></pre>
  
 
Put a filesystem on each volume.
 
Put a filesystem on each volume.
  
<pre><nowiki># mkfs.ext3 /dev/mapper/vgcrypt-lvroot
+
<pre><nowiki># mkfs.ext3 /dev/mapper/vgcrypt-lvroot
 +
# mkfs.ext3 /dev/mapper/vgcrypt-lvhome
 +
# mkswap /dev/mapper/vgcrypt-lvswap</nowiki></pre>
  
 
Populate the encrypted volumes.
 
Populate the encrypted volumes.
  
<pre><nowiki># mkdir /mnt/root
+
<pre><nowiki># 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</nowiki></pre>
  
 
Edit /mnt/root/etc/fstab.  First change this line
 
Edit /mnt/root/etc/fstab.  First change this line
  
<code><nowiki>/dev/sda1 / ext3 defaults,errors=remount-ro 0 1</nowiki></code>
+
<code><nowiki>/dev/sda1 / ext3 defaults,errors=remount-ro 0 1</nowiki></code>
  
 
to
 
to
  
<code><nowiki>/dev/mapper/vgcrypt-lvroot / ext3 defaults,errors=remount-ro 0 1</nowiki></code>
+
<code><nowiki>/dev/mapper/vgcrypt-lvroot / ext3 defaults,errors=remount-ro 0 1</nowiki></code>
  
 
And add these lines:
 
And add these lines:
  
<pre><nowiki>/dev/mapper/vgcrypt-lvhome /home ext3 defaults 0 1
+
<pre><nowiki>/dev/mapper/vgcrypt-lvhome /home ext3 defaults 0 1
 
/dev/mapper/vgcrypt-lvswap none swap sw 0 0</nowiki></pre>
 
/dev/mapper/vgcrypt-lvswap none swap sw 0 0</nowiki></pre>
  
第179行: 第199行:
 
boot partition, /dev/sda5.
 
boot partition, /dev/sda5.
  
<pre><nowiki>title          Cryptotest
+
<pre><nowiki>title          Cryptotest
 
root            (hd0,4)
 
root            (hd0,4)
 
kernel          /vmlinuz-<your kernel version here> root=/dev/mapper/vgcrypt-lvroot ro
 
kernel          /vmlinuz-<your kernel version here> root=/dev/mapper/vgcrypt-lvroot ro
第199行: 第219行:
 
change the line that has
 
change the line that has
  
<code><nowiki># kopt=root=/dev/sda1 ro</nowiki></code>
+
<code><nowiki># kopt=root=/dev/sda1 ro</nowiki></code>
  
 
to
 
to
  
<code><nowiki># kopt=root=/dev/mapper/vgcrypt-lvroot ro</nowiki></code>
+
<code><nowiki># kopt=root=/dev/mapper/vgcrypt-lvroot ro</nowiki></code>
  
 
Then run
 
Then run
  
<code><nowiki>$sudo update-grub</nowiki></code>
+
<code><nowiki>$sudo update-grub</nowiki></code>
  
  
第213行: 第233行:
 
blank USB thumb drive.
 
blank USB thumb drive.
  
<pre><nowiki>$ sudo dd if=/dev/random of=/media/<usbdrive>/pvcrypt-$(whoami)-key bs=1 count=256
+
<pre><nowiki>$ 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</nowiki></pre>
 
$ sudo cryptsetup luksAddKey /dev/sda6 /media/<usbdrive>/pvcrypt-$(whomai)-key</nowiki></pre>
  
 
Wipe the old partition.
 
Wipe the old partition.
  
<code><nowiki># /sbin/badblocks -c 10240 -s -w -t random -v /dev/sda1</nowiki></code>
+
<code><nowiki># /sbin/badblocks -c 10240 -s -w -t random -v /dev/sda1</nowiki></code>
  
  

2007年5月24日 (四) 09:37的版本

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

by John Bindel (jbindel <at> googlemail <dot com>)

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.