5. Setting up the boot device

5.1. Creating the ramdisk

To begin with, chroot inside the encrypted partition and create the boot device mount point:

chroot /mnt
mkdir /loader

Then, create the initial ramdisk (initrd), which will be needed afterwards:

dd if=/dev/zero of=initrd bs=1k count=4096
mke2fs -F initrd
mkdir ramdisk
mount -o loop initrd ramdisk

Create the filesystem hierarchy and copy the required files in it:

mkdir ramdisk/{bin,dev,lib,mnt,sbin}
cp /bin/{sh,mount,umount} ramdisk/bin/
cp -a /dev/{console,hda2,loop0} ramdisk/dev/
cp /lib/{ld-linux.so.2,libc.so.6,libdl.so.2,libncurses.so.5} \
    ramdisk/lib/
cp /sbin/{losetup,pivot_root} ramdisk/sbin/

Create the init script (don't forget to replace "xxxxxxxxxx" with your chosen seed):

cat > ramdisk/sbin/init << "EOF"
#!/bin/sh

/sbin/losetup -e aes128 -S xxxxxxxxxx /dev/loop0 /dev/hda2
/bin/mount -n -t ext2 /dev/loop0 /mnt

while [ $? -ne 0 ]
do
    /sbin/losetup -d /dev/loop0
    /sbin/losetup -e aes128 -S xxxxxxxxxx /dev/loop0 /dev/hda2
    /bin/mount -n -t ext2 /dev/loop0 /mnt
done

cd /mnt
/sbin/pivot_root . loader
exec /usr/sbin/chroot . /sbin/init
EOF

chmod 755 ramdisk/sbin/init

Umount the loopback device and compress the initrd:

umount -d ramdisk
rmdir ramdisk
gzip initrd

5.2. Setting up the boot partition

Create and mount the ext2 filesystem:

mke2fs /dev/hda1
mount -t ext2 /dev/hda1 /loader

Copy the kernel compiled in Chapter 2.1 and the initial ramdisk:

cp /path/to/vmlinuz /loader/
cp /path/to/initrd.gz /loader/

Configure and run LILO:

mkdir /loader/{boot,dev,etc}
cp /boot/boot.b /loader/boot/
cp -a /dev/{hda,hda1,ram0} /loader/dev/
cat > /loader/etc/lilo.conf << EOF
lba32
boot=/dev/hda
root=/dev/ram0
vga=4
read-only
image=/vmlinuz
    label=Linux
    initrd=/initrd.gz
EOF
lilo -r /loader

5.3. Booting with a CD-ROM

You may also choose not to use /dev/hda1 as a boot device at all, but instead burn the kernel and the ramdisk on a bootable cd-rom. Download and unpack syslinux:

ftp://ftp.kernel.org/pub/linux/utils/boot/syslinux/syslinux-2.06.tar.gz

Configure isolinux:

mkdir bootcd
cp /path/to/vmlinuz bootcd/
cp /path/to/initrd.gz bootcd/
cp syslinux-2.06/isolinux.bin bootcd/
echo "DEFAULT vmlinuz initrd=initrd.gz root=/dev/ram0 vga=4" \
    > bootcd/isolinux.cfg

Create and burn the bootable cd-rom iso image:

mkisofs -o bootcd.iso -b isolinux.bin -c boot.cat \
        -no-emul-boot -boot-load-size 4 -boot-info-table \
        -J -hide-rr-moved -R bootcd/

cdrecord -dev 0,0,0 -speed 16 -v bootcd.iso