Initial import from 0.5a version.
[secure-linux-project.git] / encrypt / initrd.sh
1 #!/bin/sh
2 ##############################################
3 # Script for Secure Linux Project            #
4 # Copyright(c) 2005, 2006 by Roland Haeder   #
5 ##############################################
6 # Purpose: Create initrd-image               #
7 ##############################################
8 # This software is licensed under the GNU    #
9 # General Public License Version 2 or either #
10 # and comes with ABSOLUTELY NO WARRANTY      #
11 # neither implied nor explicit.              #
12 ##############################################
13
14 . ./.settings.sh || exit 3
15
16 if test -e $MULTI_KEY; then
17         echo "$0: Keyfile found."
18  else
19         echo "$0: Keyfile not found! Run gen.sh first."
20         exit 2
21 fi
22
23 if ! test -e $BOOT_MOUNT; then
24         echo "$0: Please run asset.sh first!"
25         exit 2
26 fi
27
28 echo "$0: Stage 1 - Unmounting old devices ..."
29 umount $INITRD_LOOP
30 losetup -d $INITRD_LOOP
31 umount $BOOT_DEVICE
32 mount $BOOT_DEVICE $BOOT_MOUNT
33 echo "$0: Stage 2 - done."
34
35 if ! test -e $BOOT_MOUNT/initrd && ! test -e $BOOT_MOUNT/initrd.gz; then
36         echo "$0: Stage 2 - Setting up initrd with e2fs ..."
37         head -c 8m /dev/zero > $BOOT_MOUNT/initrd
38         mke2fs -F -m0 -b 1024 -L "SLP 0.4a" $VERBOSE $BOOT_MOUNT/initrd
39         mkdir $VERBOSE $BASEDIR/initrd
40         CHK_LOOP="0"
41         echo "$0: Stage 2 - done."
42 else
43         if test -e $BOOT_MOINT/initrd.gz; then
44                 echo "$0: Stage 2 - Uncompressing initrd.gz ..."
45                 gunzip $VERBOSE 
46                 echo "$0: Stage 2 - done."
47         else
48                 echo "$0: Stage 2 - skipped (me2fs)."
49         fi
50 fi
51
52 echo "$0: Stage 3 - Setting up loop-device ..."
53 losetup $INITRD_LOOP $BOOT_MOUNT/initrd
54 echo "$0: Stage 3 - done."
55
56 if test "$CHK_LOOP" == "1"; then
57         echo "$0: Stage 4 - Checking fs on initrd ..."
58         e2fsck -pv $INITRD_LOOP
59         echo "$0: Stage 4 - done"
60 else
61         echo "$0: Stage 4 - skipped (e2fsck)."
62 fi
63
64 echo "$0: Stage 5 - Initializing initrd & copy process ..."
65 mkdir $VERBOSE $BASEDIR/initrd
66 mount $INITRD_LOOP $BASEDIR/initrd
67 cp $VERBOSE $BASEDIR/.stick_seed $BASEDIR/initrd/.seed
68 cd $BASEDIR/initrd
69 echo "$0: Stage 5 - done."
70
71 echo "$0: Stage 6 - Creating directories ..."
72 mkdir $VERBOSE -p {bin,dev,lib,$MNT/{$KEYS_DIR,new-root,boot,stick,stick2},usr/{bin,sbin,lib},proc,sbin} || exit 2
73 echo "$0: Stage 6 - done."
74
75 echo "$0: Stage 7 - Copying device files ..."
76 cp $VERBOSE $UPDATE -a /dev/{console,hd{a*,b*,c*,d*},tty,null,sda,urandom,md?} dev || exit 2
77 echo "$0: Stage 7 - done."
78
79 echo "$0: Stage 8 - Copying programs ..."
80 cp $VERBOSE $UPDATE `which mount sh umount cat sleep sync dd uname grep sed mknod ln ls` /sbin/{modprobe*,depmod*} bin || exit 2
81 cp $VERBOSE $UPDATE `which losetup pivot_root insmod insmod.modutils mkswap swapon swapoff mdadm mdrun fsck fsck.ext2 fsck.ext2 mdrun mdadmin` sbin || exit 2
82 cp $VERBOSE $UPDATE `which chroot` usr/sbin || exit 2
83 cp $VERBOSE $UPDATE `which test md5sum cut gpg tail uuencode` usr/bin || exit 2
84 echo "$0: Stage 8 - done."
85
86 echo "$0: Stage 9 - Copying libraries ..."
87 cp $VERBOSE $UPDATE -a /lib/lib{usb-0.1.so.4*,readline.so.5*,bz2.so*,resolv*,uuid.so.1*,ncurses.so.2.5.4,ld-2.3.6.so,c-2.3.6.so,c.so.6,blkid.so.1*,selinux.so.1,sepol.so.1,m-2.3.2.so,m.so.6,rt*,ext2fs.so.2*,pthread*,acl.so.1*,attr.so.1*,se*} lib || exit 2
88 cp $VERBOSE $UPDATE -a /usr/lib{libz.so*} usr/lib || exit 2
89 cp $VERBOSE $UPDATE -a /lib/ lib/ || exit 2
90 echo "$0: Stage 9 - done."
91
92 # Copy our scripts
93 echo "$0: Stage 10 - Copying SLP scripts and stick-secret.gpg ..."
94 cp $VERBOSE $UPDATE $BASEDIR/setup/keys/stick-secret.gpg $BASEDIR/{source/{decrypt.sh,linuxrc,swap.sh},.local.sh} $BASEDIR/initrd || exit 2
95 mkdir --parents --$VERBOSE $BASEDIR/initrd/lib/modules/$KERN_VER/ || exit 2
96 cp $VERBOSE $UPDATE $BASEDIR/source/lib/modules/$KERN_VER/loop.* $BASEDIR/initrd/lib/modules/$KERN_VER/ || exit 2
97 echo "$0: Stage 10 - done."
98
99 # Prepare directories for gpg
100 echo "$0: Stage 11 - Preparing .gnupg ..."
101 mkdir --parents $VERBOSE $BASEDIR/initrd/root/.gnupg
102 echo "$0: Stage 11 - done."
103
104 # Create lots of loop-back devices (we need loop-aes compiled with "max_loop = 8" here!)
105 echo "$0: Stage 12 - Creating loop-devices ..."
106 for idx in `seq 0 16`; do
107         if ! test -e "dev/loop$idx"; then
108                 mknod --mode=660 "dev/loop$idx" b 7 $idx
109         fi
110 done
111 echo "$0: Stage 12 - done."
112
113 echo "$0: Stage 13 - Setting symbolic links ..."
114 cd lib
115 ln $VERBOSE -sf ld-2.3.6.so ld-linux.so.2 || exit 2
116 ln $VERBOSE -sf libdl-2.3.6 libdl.so.2 || exit 2
117 ln $VERBOSE -sf libncurses.so.2.5.4 libncurses.so.5 || exit 2
118 cd ../sbin
119 ln $VERBOSE -sf /linuxrc init
120 cd ../root
121 # To prevent filling up the initrd while development
122 ln $VERBOSE -sf /dev/null .bash_history || exit 2
123 cd ../bin
124 ln $VERBOSE -sf sh bash || exit 2
125 cd ..
126 echo "$0: Stage 13 - done."
127
128 if test -e "/boot/vmlinuz-$KERN_VER"; then
129         if test -e "/boot/System.map-$KERN_VER"; then
130                 echo "$0: Stage 14 - Copying kernel/System.map ..."
131                 KERN_FOUND="1"
132                 cp $VERBOSE $UPDATE "/boot/vmlinuz-$KERN_VER" "/boot/System.map-$KERN_VER" $BOOT_MOUNT/ || exit 2
133                 echo "$0: Stage 14 - done."
134         else
135                 echo "$0: Stage 14 - No System.map found for version $KERN_VER."
136         fi
137 else
138         echo "$0: Stage 14 -No kernel found for version $KERN_VER."
139 fi
140
141 if test "$KERN_FOUND" == "0"; then
142         if test -e "/usr/src/linux-$KERN_VER"; then
143                 cd "/usr/src/linux-$KERN_VER"
144                 make menuconfig
145                 if test -e .config; then
146                         # Build kernel and modules and install them
147                         make dep bzImage modules modules_install || exit 1
148                         # Copy kernel/System.map to initrd
149                         echo "$0: Stage 14 - Copying compiled kernel/System.amp ..."
150                         cp $VERBOSE System.map $BOOT_MOUNT/System.map-$KERN_VER || exit 2
151                         cp $VERBOSE arch/i386/boot/bzImage $BOOT_MOUNT/vmlinuz-$KERN_VER || exit 2
152                         echo "$0: Stage 14 - done."
153                  else
154                         echo "FAILED: Compilation of kernel v$KERN_VER"
155                         exit 2
156                 fi
157         else
158                 echo "FAILED: Cannot find build-directory /usr/src/linux-$KERN_VER!"
159                 exit 2
160         fi
161 fi
162
163 # Copy RAID modules to initrd
164 echo "$0: Stage 15 - Copying kernel modules ..."
165 cp $VERBOSE --parents /lib/modules/$KERN_VER/kernel/drivers/md/{raid5,xor,md}.* $BASEDIR/initrd/ || exit 2
166 if test -e "/usr/src/modules/loop-aes/"; then
167         # Install loop-aes module
168         cd /usr/src/modules/loop-aes/
169         # Build loop-aes and install it
170         make clean all install || exit 1
171         # Generate path on initrd
172         mkdir $VERBOSE $BASEDIR/initrd/lib/modules/$KERN_BER/{kernel/block,block} || exit 2
173         if test -e "loop.ko"; then
174                 # Kernel version >= 2.6.*
175                 cp loop.ko $BASEDIR/initrd/lib/modules/$KERN_BER/block/ || exit 2
176         else
177                 # Kernel version <= 2.4.*
178                 cp loop.o $BASEDIR/initrd/lib/modules/$KERN_BER/block/ || exit 2
179         fi
180 else
181         # Copy legacy loop.o
182         cp $VERBOSE --parents /lib/modules/$KERN_VER/kernel/driversblock/loop.o $BASEDIR/initrd || exit 2
183 fi
184 cd $BASEDIR
185 echo "$0: Stage 15 - done."
186
187 if test "$UMOUNT_INITRD" == "1"; then
188         echo "$0: Removing initrd and loop ..."
189         cd ..
190         umount $INITRD_LOOP
191         losetup -d $INITRD_LOOP
192 fi
193
194 echo
195 echo "All done now."
196 echo
197 echo "You may want to execute cpio.sh to copy all your data to the encrypted"
198 echo "disk in $BASEDIR/root."