6b20c8d1b957dc351be3ce5d73038220aef0d0ff
[genkernel.git] / gen_initrd.sh
1 #!/bin/bash
2
3 # create_initrd_loop(size)
4 create_initrd_loop() {
5         local inodes
6         [ "$#" -ne '1' ] && gen_die 'create_initrd_loop(): Not enough arguments!'
7         mkdir -p ${TEMP}/initrd-mount ||
8                 gen_die 'Could not create loopback mount directory!'
9         dd if=/dev/zero of=${TMPDIR}/initrd-${KV} bs=1k count=${1} >> "${LOGFILE}" 2>&1 ||
10                 gen_die "Could not zero initrd-${KV}"
11         mke2fs -F -N750 -q "${TMPDIR}/initrd-${KV}" >> "${LOGFILE}" 2>&1 ||
12                 gen_die "Could not format initrd-${KV}!"
13         mount -t ext2 -o loop "${TMPDIR}/initrd-${KV}" "${TEMP}/initrd-mount" >> "${LOGFILE}" 2>&1 ||
14                 gen_die 'Could not mount the initrd filesystem!'
15 }
16
17 create_initrd_unmount_loop() {
18         cd ${TEMP}
19         umount "${TEMP}/initrd-mount" ||
20                 gen_die 'Could not unmount initrd system!'
21 }
22
23 move_initrd_to_loop() {
24         cd "${TEMP}/initrd-temp"
25         mv * "${TEMP}/initrd-mount" >> ${LOGFILE} 2>&1
26 }
27
28 # check for static linked file with objdump
29 is_static() {
30         LANG="C" LC_ALL="C" objdump -T $1 2>&1 | grep "not a dynamic object" > /dev/null
31         return $?
32 }
33
34 create_base_initrd_sys() {
35         rm -rf "${TEMP}/initrd-temp" > /dev/null
36         mkdir -p ${TEMP}/initrd-temp/dev
37         mkdir -p ${TEMP}/initrd-temp/bin
38         mkdir -p ${TEMP}/initrd-temp/etc
39         mkdir -p ${TEMP}/initrd-temp/usr
40         mkdir -p ${TEMP}/initrd-temp/proc
41         mkdir -p ${TEMP}/initrd-temp/temp
42         mkdir -p ${TEMP}/initrd-temp/sys
43         mkdir -p ${TEMP}/initrd-temp/.initrd
44         mkdir -p ${TEMP}/initrd-temp/var/lock/dmraid
45         ln -s bin ${TEMP}/initrd-temp/sbin
46         ln -s ../bin ${TEMP}/initrd-temp/usr/bin
47         ln -s ../bin ${TEMP}/initrd-temp/usr/sbin
48
49         echo "/dev/ram0     /           ext2    defaults        0 0" > ${TEMP}/initrd-temp/etc/fstab
50         echo "proc          /proc       proc    defaults    0 0" >> ${TEMP}/initrd-temp/etc/fstab
51
52         cd ${TEMP}/initrd-temp/dev
53         MAKEDEV std
54         MAKEDEV console
55
56         if [ "${DISKLABEL}" -eq '1' ]; then
57                 cp "${BLKID_BINCACHE}" "${TEMP}/initrd-temp/bin/blkid.bz2" ||
58                         gen_die 'Could not copy blkid from bincache!'
59                 bunzip2 "${TEMP}/initrd-temp/bin/blkid.bz2" ||
60                         gen_die 'Could not uncompress blkid!'
61                 chmod +x "${TEMP}/initrd-temp/bin/blkid"
62         fi
63
64         tar -xjf "${BUSYBOX_BINCACHE}" -C "${TEMP}/initrd-temp/bin" busybox ||
65                 gen_die 'Could not extract busybox bincache!'
66         chmod +x "${TEMP}/initrd-temp/bin/busybox"
67
68         # DMRAID 
69         if [ "${DMRAID}" -eq '1' ]
70         then
71                 print_info 1 'DMRAID: Adding support (compiling binaries)...'
72                 compile_dmraid
73                 /bin/tar -jxpf "${DMRAID_BINCACHE}" -C "${TEMP}/initrd-temp" ||
74                         gen_die "Could not extract dmraid binary cache!";
75         fi
76
77         # LVM
78         if [ "${LVM}" -eq '1' ]
79         then
80                 if [ -e '/sbin/lvm' ] && LC_ALL="C" ldd /sbin/lvm|grep -q 'not a dynamic executable';
81                 then
82                         print_info 1 'LVM: Adding support (using local static binaries)...'
83                         cp /sbin/lvm "${TEMP}/initrd-temp/bin/lvm" ||
84                                 gen_die 'Could not copy over lvm!'
85                 else
86                         print_info 1 'LVM: Adding support (compiling binaries)...'
87                         compile_lvm
88
89                         /bin/tar -jxpf "${LVM_BINCACHE}" -C "${TEMP}/initrd-temp" ||
90                                 gen_die "Could not extract lvm binary cache!";
91                         mv ${TEMP}/initrd-temp/bin/lvm.static ${TEMP}/initrd-temp/bin/lvm ||
92                                 gen_die 'LVM error: Could not move lvm.static to lvm!'
93                 fi
94                 for i in vgchange vgscan; do
95                         ln  ${TEMP}/initrd-temp/bin/lvm ${TEMP}/initrd-temp/bin/$i ||
96                                 gen_die "LVM error: Could not link ${i}!"
97                 done
98                 mkdir -p ${TEMP}/initrd-temp/etc/lvm
99                 if [ -x /sbin/lvm ]
100                 then
101 #                       lvm dumpconfig 2>&1 > /dev/null || gen_die 'Could not copy over lvm.conf!'
102 #                       ret=$?
103 #                       if [ ${ret} != 0 ]
104 #                       then
105                                 cp /etc/lvm/lvm.conf "${TEMP}/initrd-temp/etc/lvm/" ||
106                                         gen_die 'Could not copy over lvm.conf!'
107 #                       else
108 #                               gen_die 'Could not copy over lvm.conf!'
109 #                       fi
110                 fi
111         fi
112         
113         # EVMS
114         if [ "${EVMS}" -eq '1' ]
115         then
116                 if [ -e '/sbin/evms_activate' ]
117                 then
118                         print_info 1 'EVMS: Adding support...'  
119                         mkdir -p ${TEMP}/initrd-temp/lib
120                         mkdir -p ${TEMP}/initrd-temp/sbin
121                         mkdir -p ${TEMP}/initrd-temp/etc
122                         mkdir -p ${TEMP}/initrd-temp/bin
123                         cp -a /lib/ld-* "${TEMP}/initrd-temp/lib" \
124                                 || gen_die 'Could not copy files for EVMS!'
125                         if [ -n "`ls /lib/libgcc_s*`" ]
126                         then
127                                 cp -a /lib/libgcc_s* "${TEMP}/initrd-temp/lib" \
128                                         || gen_die 'Could not copy files for EVMS!'
129                         fi
130                         cp -a /lib/libc-* /lib/libc.* "${TEMP}/initrd-temp/lib" \
131                                 || gen_die 'Could not copy files for EVMS!'
132                         cp -a /lib/libdl-* /lib/libdl.* "${TEMP}/initrd-temp/lib" \
133                                 || gen_die 'Could not copy files for EVMS!'
134                         cp -a /lib/libpthread* "${TEMP}/initrd-temp/lib" \
135                                 || gen_die 'Could not copy files for EVMS!'
136                         cp -a /lib/libuuid*so* "${TEMP}/initrd-temp/lib" \
137                                 || gen_die 'Could not copy files for EVMS!'
138                         cp -a /lib/libevms*so* "${TEMP}/initrd-temp/lib" \
139                                 || gen_die 'Could not copy files for EVMS!'
140                         cp -a /lib/evms "${TEMP}/initrd-temp/lib" \
141                                 || gen_die 'Could not copy files for EVMS!'
142                         cp -a /lib/evms/* "${TEMP}/initrd-temp/lib/evms" \
143                                 || gen_die 'Could not copy files for EVMS!'
144                         cp -a /etc/evms.conf "${TEMP}/initrd-temp/etc" \
145                                 || gen_die 'Could not copy files for EVMS!'
146                         cp /sbin/evms_activate "${TEMP}/initrd-temp/sbin" \
147                                 || gen_die 'Could not copy over evms_activate!'
148                         # Fix EVMS complaining that it cant find the swap utilities.
149                         # These are not required in the initrd
150                         for swap_libs in "${TEMP}/initrd-temp/lib/evms/*/swap*.so"
151                         do
152                                 rm ${swap_libs}
153                         done
154                 fi
155         fi      
156
157         for i in '[' ash basename cat chroot clear cp dirname echo env false find \
158         grep gunzip gzip ln ls loadkmap losetup lsmod mdev mkdir mknod more mount \
159         mv pivot_root ps awk pwd rm rmdir rmmod sed sh sleep tar test touch true \
160         umount uname xargs yes zcat chmod chown cut kill killall; do
161                 rm -f ${TEMP}/initrd-temp/bin/$i > /dev/null
162                 ln  ${TEMP}/initrd-temp/bin/busybox ${TEMP}/initrd-temp/bin/$i ||
163                         gen_die "Busybox error: could not link ${i}!"
164         done
165
166         if isTrue ${LUKS}
167         then
168                 if is_static /bin/cryptsetup
169                 then
170                         print_info 1 "Including LUKS support"
171                         rm -f ${TEMP}/initrd-temp/sbin/cryptsetup
172                         cp /bin/cryptsetup ${TEMP}/initrd-temp/sbin/cryptsetup
173                         chmod +x "${TEMP}/initrd-temp/sbin/cryptsetup"
174                 elif is_static /sbin/cryptsetup
175                 then
176                         print_info 1 "Including LUKS support"
177                         rm -f ${TEMP}/initrd-temp/sbin/cryptsetup
178                         cp /sbin/cryptsetup ${TEMP}/initrd-temp/sbin/cryptsetup
179                         chmod +x "${TEMP}/initrd-temp/sbin/cryptsetup"
180                 else
181                         print_info 1 "LUKS support requires static cryptsetup at /bin/cryptsetup or /sbin/cryptsetup"
182                         print_info 1 "Not including LUKS support"
183                 fi
184         fi
185 }
186
187 print_list() {
188         local x
189         for x in ${*}
190         do
191                 echo ${x}
192         done
193 }
194
195 create_initrd_modules() {
196         local group
197         local group_modules
198         
199         if [ "${PAT}" -gt "4" ]
200         then
201                 MOD_EXT=".ko"
202         else
203                 MOD_EXT=".o"
204         fi
205
206         print_info 2 "initrd: >> Searching for modules..."
207
208         if [ "${INSTALL_MOD_PATH}" != '' ]
209         then
210                 cd ${INSTALL_MOD_PATH}
211         else
212                 cd /
213         fi
214                                                                                                         
215         for i in `gen_dep_list`
216         do
217                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" 2>/dev/null| head -n 1`
218                 if [ -z "${mymod}" ]
219                 then
220                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
221                         continue;
222                 fi
223                 print_info 2 "initrd: >> Copying ${i}${MOD_EXT}..."
224                 cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
225         done
226
227         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initrd-temp 2>/dev/null
228
229         mkdir -p "${TEMP}/initrd-temp/etc/modules"
230         for group_modules in ${!MODULES_*}; do
231                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
232                 print_list ${!group_modules} > "${TEMP}/initrd-temp/etc/modules/${group}"
233         done
234 }
235
236 create_initrd_aux() {
237         if [ -f "${CMD_LINUXRC}" ]
238         then
239                 cp "${CMD_LINUXRC}" "${TEMP}/initrd-temp/linuxrc"
240                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC}"
241         else    
242                 if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
243                 then
244                         cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initrd-temp/linuxrc"
245                 else
246                         cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
247                 fi
248         fi
249
250         # Make sure it's executable
251         chmod 0755 "${TEMP}/initrd-temp/linuxrc"
252
253         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
254         then
255                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
256         else    
257                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
258         fi
259
260         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
261         then
262                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
263         else
264                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
265         fi
266         
267         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initrd-temp/etc/initrd.defaults" 
268         for group_modules in ${!MODULES_*}; do
269                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
270                 echo -n "${group} " >> "${TEMP}/initrd-temp/etc/initrd.defaults"
271         done
272         echo '"' >> "${TEMP}/initrd-temp/etc/initrd.defaults"   
273
274         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
275         then
276                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
277         else
278                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
279         fi
280         if isTrue $CMD_DOKEYMAPAUTO
281         then
282                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
283         fi
284         if isTrue $CMD_KEYMAP
285         then
286                 mkdir -p "${TEMP}/initrd-temp/lib/keymaps"
287                 /bin/tar -C "${TEMP}/initrd-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
288         fi
289         if isTrue $CMD_SLOWUSB
290         then
291                 echo 'MY_HWOPTS="${MY_HWOPTS} slowusb"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
292         fi
293
294         cd ${TEMP}/initrd-temp/sbin && ln -s ../linuxrc init
295         cd ${OLDPWD}
296         chmod +x "${TEMP}/initrd-temp/linuxrc"
297         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
298         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
299         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
300 }
301
302 calc_initrd_size() {
303         local TEST
304         cd ${TEMP}/initrd-temp/
305         TEST=`du -sk 2> /dev/null` 
306         echo $TEST | cut "-d " -f1
307 }
308
309 create_initrd() {
310         local MOD_EXT
311
312         print_info 1 "initrd: >> Initializing..."
313         create_base_initrd_sys
314
315         if [ "${NOINITRDMODULES}" -eq '0' ]
316         then
317                 print_info 1 "        >> Copying modules..."
318                 create_initrd_modules
319         else
320                 print_info 1 "initrd: Not copying modules..."
321         fi
322
323         print_info 1 "        >> Copying auxilary files..."
324         create_initrd_aux
325
326         INITRD_CALC_SIZE=`calc_initrd_size`
327         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 250`
328         print_info 1 "        :: Size is at ${INITRD_SIZE}K"
329         print_info 1 "        >> Creating loopback filesystem..."
330         create_initrd_loop ${INITRD_SIZE}
331
332         print_info 1 "        >> Moving initrd files to the loopback..."
333         move_initrd_to_loop
334
335         print_info 1 "        >> Cleaning up and compressing the initrd..."
336         create_initrd_unmount_loop
337
338         if [ "${COMPRESS_INITRD}" ]
339         then
340                 gzip -f -9 ${TMPDIR}/initrd-${KV}
341                 mv ${TMPDIR}/initrd-${KV}.gz ${TMPDIR}/initrd-${KV}
342         fi
343
344         if ! isTrue "${CMD_NOINSTALL}"
345         then
346                 copy_image_with_preserve "initrd" \
347                         "${TMPDIR}/initrd-${KV}" \
348                         "initrd-${KNAME}-${ARCH}-${KV}"
349         fi
350
351 #       if [ "${ENABLE_PEGASOS_HACKS}" = 'yes' ]
352 #       then
353 #               # Pegasos hack for merging the initramfs into the kernel at compile time
354 #               cp ${TMPDIR}/initrd-${KV} ${KERNEL_DIR}/arch/${ARCH}/boot/images/ramdisk.image.gz &&
355 #               rm ${TMPDIR}/initrd-${KV}
356 #       fi
357 }