Changed LVM configuration detection to not error.
[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} >> "${DEBUGFILE}" 2>&1 ||
10                 gen_die "Could not zero initrd-${KV}"
11         mke2fs -F -N750 -q "${TMPDIR}/initrd-${KV}" >> "${DEBUGFILE}" 2>&1 ||
12                 gen_die "Could not format initrd-${KV}!"
13         mount -t ext2 -o loop "${TMPDIR}/initrd-${KV}" "${TEMP}/initrd-mount" >> "${DEBUGFILE}" 2>&1 ||
14                 gen_die 'Could not mount the initrd filesystem!'
15 }
16
17 create_initrd_unmount_loop()
18 {
19         cd ${TEMP}
20         umount "${TEMP}/initrd-mount" ||
21                 gen_die 'Could not unmount initrd system!'
22 }
23
24 move_initrd_to_loop()
25 {
26         cd "${TEMP}/initrd-temp"
27         mv * "${TEMP}/initrd-mount" >> ${DEBUGFILE} 2>&1
28 }
29
30 # check for static linked file with objdump
31 is_static() {
32         objdump -T $1 2>&1 | grep "not a dynamic object" > /dev/null
33         return $?
34 }
35
36 create_base_initrd_sys() {
37         rm -rf "${TEMP}/initrd-temp" > /dev/null
38         mkdir -p ${TEMP}/initrd-temp/dev
39         mkdir -p ${TEMP}/initrd-temp/bin
40         mkdir -p ${TEMP}/initrd-temp/etc
41         mkdir -p ${TEMP}/initrd-temp/usr
42         mkdir -p ${TEMP}/initrd-temp/proc
43         mkdir -p ${TEMP}/initrd-temp/temp
44         mkdir -p ${TEMP}/initrd-temp/sys
45         mkdir -p ${TEMP}/initrd-temp/.initrd
46         mkdir -p ${TEMP}/initrd-temp/var/lock/dmraid
47         ln -s bin ${TEMP}/initrd-temp/sbin
48         ln -s ../bin ${TEMP}/initrd-temp/usr/bin
49         ln -s ../bin ${TEMP}/initrd-temp/usr/sbin
50
51         echo "/dev/ram0     /           ext2    defaults        0 0" > ${TEMP}/initrd-temp/etc/fstab
52         echo "proc          /proc       proc    defaults    0 0" >> ${TEMP}/initrd-temp/etc/fstab
53
54         if [ "${NODEVFSD}" = '' ]
55         then
56                 echo "REGISTER        .*           MKOLDCOMPAT" > ${TEMP}/initrd-temp/etc/devfsd.conf
57                 echo "UNREGISTER      .*           RMOLDCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
58                 echo "REGISTER        .*           MKNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
59                 echo "UNREGISTER      .*           RMNEWCOMPAT" >> ${TEMP}/initrd-temp/etc/devfsd.conf
60         fi
61
62         # SGI LiveCDs need the following binary (no better place for it than here)
63         # getdvhoff is a DEPEND of genkernel, so it *should* exist
64         if [ ${BUILD_INITRAMFS} -eq 1 ]
65         then
66                 [ -e /usr/lib/getdvhoff/getdvhoff ] \
67                         && cp /usr/lib/getdvhoff/getdvhoff ${TEMP}/initrd-temp/bin \
68                         || gen_die "sys-boot/getdvhoff not merged!"
69         fi
70
71         cd ${TEMP}/initrd-temp/dev
72         MAKEDEV std
73         MAKEDEV console
74
75         if [ "${DISKLABEL}" -eq '1' ]; then
76                 cp "${BLKID_BINCACHE}" "${TEMP}/initrd-temp/bin/blkid.bz2" ||
77                         gen_die 'Could not copy blkid from bincache!'
78                 bunzip2 "${TEMP}/initrd-temp/bin/blkid.bz2" ||
79                         gen_die 'Could not uncompress blkid!'
80                 chmod +x "${TEMP}/initrd-temp/bin/blkid"
81         fi
82
83         cp "${BUSYBOX_BINCACHE}" "${TEMP}/initrd-temp/bin/busybox.bz2" ||
84                 gen_die 'Could not copy busybox from bincache!'
85         bunzip2 "${TEMP}/initrd-temp/bin/busybox.bz2" ||
86                 gen_die 'Could not uncompress busybox!'
87         chmod +x "${TEMP}/initrd-temp/bin/busybox"
88
89         if [ "${NOINITRDMODULES}" = '' ]
90         then
91                 if [ "${PAT}" -gt "4" ]
92                 then
93                         cp "${MODULE_INIT_TOOLS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" ||
94                                 gen_die 'Could not copy insmod.static from bincache!'
95                 else
96                         cp "${MODUTILS_BINCACHE}" "${TEMP}/initrd-temp/bin/insmod.static.bz2" ||
97                                 gen_die 'Could not copy insmod.static from bincache'
98                 fi
99
100                 bunzip2 "${TEMP}/initrd-temp/bin/insmod.static.bz2" ||
101                         gen_die 'Could not uncompress insmod.static!'
102                 mv "${TEMP}/initrd-temp/bin/insmod.static" "${TEMP}/initrd-temp/bin/insmod"
103                 chmod +x "${TEMP}/initrd-temp/bin/insmod"
104         fi
105
106         # devfsd
107         if [ "${DEVFS}" -eq '1' ]
108         then
109                 cp "${DEVFSD_BINCACHE}" "${TEMP}/initrd-temp/bin/devfsd.bz2" || gen_die 'Could not copy devfsd executable from bincache!'
110                 bunzip2 "${TEMP}/initrd-temp/bin/devfsd.bz2" || gen_die 'Could not uncompress devfsd!'
111                 chmod +x "${TEMP}/initrd-temp/bin/devfsd"
112         fi
113
114         # udev
115 #       if [ "${UDEV}" -eq '1' ]
116 #       then
117 #               /bin/tar -jxpf "${UDEV_BINCACHE}" -C "${TEMP}/initrd-temp" || gen_die 'Could not extract udev binary cache!'
118 #               if [ ! -e "${TEMP}/initrd-temp/bin/udevstart" ]
119 #               then
120 #                   ln -sf "./udev" "${TEMP}/initrd-temp/bin/udevstart" || gen_die 'Could not symlink udev -> udevstart!'
121 #               fi
122                 
123 #               if [ ! -e "${TEMP}/initrd-temp/bin/udevsend" ]
124 #               then
125 #                   ln -sf "./udev" "${TEMP}/initrd-temp/bin/udevsend" || gen_die 'Could not symlink udev -> udevsend!'
126 #               fi
127 #       fi
128         
129         #unionfs modules
130         if [ "${UNIONFS}" -eq '1' ]
131         then
132                 print_info 1 'UNIONFS MODULES: Adding support (compiling)...'
133                 compile_unionfs_modules
134                 /bin/tar -jxpf "${UNIONFS_MODULES_BINCACHE}" -C "${TEMP}/initrd-temp" ||
135                         gen_die "Could not extract unionfs modules binary cache!";
136         fi
137         
138         #unionfs utils
139         if [ "${UNIONFS}" -eq '1' ]
140         then
141                 print_info 1 'UNIONFS TOOLS: Adding support (compiling)...'
142                 compile_unionfs_utils
143                 /bin/tar -jxpf "${UNIONFS_BINCACHE}" -C "${TEMP}/initrd-temp" ||
144                         gen_die "Could not extract unionfs tools binary cache!";
145         fi
146
147         # Suspend
148         if [ "${SUSPEND}" = '1' ]
149         then
150                 print_info 1 'SUSPEND: Adding support (compiling binaries)...'
151                 compile_suspend
152                 /bin/tar -jxpf "${SUSPEND_BINCACHE}" -C "${TEMP}/initrd-temp" ||
153                         gen_die "Could not extract suspend binary cache!"
154                 mkdir -p "${TEMP}/initrd-temp/etc"
155                 cp -f /etc/suspend.conf "${TEMP}/initrd-temp/etc" ||
156                         gen_die 'Could not copy /etc/suspend.conf'
157         fi
158
159         # DMRAID 
160         if [ "${DMRAID}" = '1' ]
161         then
162                 print_info 1 'DMRAID: Adding support (compiling binaries)...'
163                 compile_dmraid
164                 /bin/tar -jxpf "${DMRAID_BINCACHE}" -C "${TEMP}/initrd-temp" ||
165                         gen_die "Could not extract dmraid binary cache!";
166         fi
167
168         # LVM2
169         if [ "${LVM2}" = '1' ]
170         then
171                 if [ -e '/sbin/lvm' ] && ldd /sbin/lvm|grep -q 'not a dynamic executable';
172                 then
173                         print_info 1 'LVM2: Adding support (using local static binaries)...'
174                         cp /sbin/lvm "${TEMP}/initrd-temp/bin/lvm" ||
175                                 gen_die 'Could not copy over lvm!'
176                 else
177                         print_info 1 'LVM2: Adding support (compiling binaries)...'
178                         compile_lvm2
179
180                         /bin/tar -jxpf "${LVM2_BINCACHE}" -C "${TEMP}/initrd-temp" ||
181                                 gen_die "Could not extract lvm2 binary cache!";
182                         mv ${TEMP}/initrd-temp/bin/lvm.static ${TEMP}/initrd-temp/bin/lvm ||
183                                 gen_die 'LVM2 error: Could not move lvm.static to lvm!'
184                 fi
185                 for i in vgchange vgscan; do
186                         ln  ${TEMP}/initrd-temp/bin/lvm ${TEMP}/initrd-temp/bin/$i ||
187                                 gen_die "LVM2 error: Could not link ${i}!"
188                 done
189                 mkdir -p ${TEMP}/initrd-temp/etc/lvm
190                 if [ -x /sbin/lvm ]
191                 then
192                         lvm dumpconfig 2>&1 > /dev/null || gen_die 'Could not copy over lvm.conf!'
193                         ret=$?
194                         if [ ${ret} != 0 ]
195                         then
196                                 cp /etc/lvm/lvm.conf "${TEMP}/initramfs-lvm2-temp/etc/lvm/" ||
197                                         gen_die 'Could not copy over lvm.conf!'
198                         else
199                                 gen_die 'Could not copy over lvm.conf!'
200                         fi
201                 fi
202         fi
203         
204         # EVMS2
205         if [ "${EVMS2}" = '1' ]
206         then
207                 if [ -e '/sbin/evms_activate' ]
208                 then
209                         print_info 1 'EVMS2: Adding support...' 
210                         mkdir -p ${TEMP}/initrd-temp/lib
211                         mkdir -p ${TEMP}/initrd-temp/sbin
212                         mkdir -p ${TEMP}/initrd-temp/etc
213                         mkdir -p ${TEMP}/initrd-temp/bin
214                         cp -a /lib/ld-* "${TEMP}/initrd-temp/lib" \
215                                 || gen_die 'Could not copy files for EVMS2!'
216                         if [ -n "`ls /lib/libgcc_s*`" ]
217                         then
218                                 cp -a /lib/libgcc_s* "${TEMP}/initramfs-evms2-temp/lib" \
219                                         || gen_die 'Could not copy files for EVMS2!'
220                         fi
221                         cp -a /lib/libc-* /lib/libc.* "${TEMP}/initrd-temp/lib" \
222                                 || gen_die 'Could not copy files for EVMS2!'
223                         cp -a /lib/libdl-* /lib/libdl.* "${TEMP}/initrd-temp/lib" \
224                                 || gen_die 'Could not copy files for EVMS2!'
225                         cp -a /lib/libpthread* "${TEMP}/initrd-temp/lib" \
226                                 || gen_die 'Could not copy files for EVMS2!'
227                         cp -a /lib/libuuid*so* "${TEMP}/initrd-temp/lib" \
228                                 || gen_die 'Could not copy files for EVMS2!'
229                         cp -a /lib/libevms*so* "${TEMP}/initrd-temp/lib" \
230                                 || gen_die 'Could not copy files for EVMS2!'
231                         cp -a /lib/evms "${TEMP}/initrd-temp/lib" \
232                                 || gen_die 'Could not copy files for EVMS2!'
233                         cp -a /lib/evms/* "${TEMP}/initrd-temp/lib/evms" \
234                                 || gen_die 'Could not copy files for EVMS2!'
235                         cp -a /etc/evms.conf "${TEMP}/initrd-temp/etc" \
236                                 || gen_die 'Could not copy files for EVMS2!'
237                         cp /sbin/evms_activate "${TEMP}/initrd-temp/sbin" \
238                                 || gen_die 'Could not copy over evms_activate!'
239                         # Fix EVMS2 complaining that it cant find the swap utilities.
240                         # These are not required in the initrd
241                         for swap_libs in "${TEMP}/initrd-temp/lib/evms/*/swap*.so"
242                         do
243                                 rm ${swap_libs}
244                         done
245                 fi
246         fi      
247
248         for i in '[' ash basename cat chroot clear cp dirname echo env false find \
249         grep gunzip gzip ln ls loadkmap losetup lsmod mdev mkdir mknod more mount \
250         mv pivot_root ps awk pwd rm rmdir rmmod sed sh sleep tar test touch true \
251         umount uname xargs yes zcat chmod chown cut kill killall; do
252                 rm -f ${TEMP}/initrd-temp/bin/$i > /dev/null
253                 ln  ${TEMP}/initrd-temp/bin/busybox ${TEMP}/initrd-temp/bin/$i ||
254                         gen_die "Busybox error: could not link ${i}!"
255         done
256
257         if isTrue ${LUKS}
258         then
259                 if is_static /bin/cryptsetup
260                 then
261                         print_info 1 "Including LUKS support"
262                         rm -f ${TEMP}/initrd-temp/sbin/cryptsetup
263                         cp /bin/cryptsetup ${TEMP}/initrd-temp/sbin/cryptsetup
264                         chmod +x "${TEMP}/initrd-temp/sbin/cryptsetup"
265                 else
266                         print_info 1 "LUKS support requires static cryptsetup at /bin/cryptsetup"
267                         print_info 1 "Not including LUKS support"
268                 fi
269         fi
270 }
271
272 print_list() {
273         local x
274         for x in ${*}
275         do
276                 echo ${x}
277         done
278 }
279
280 create_initrd_modules() {
281         local group
282         local group_modules
283         
284         if [ "${PAT}" -gt "4" ]
285         then
286                 MOD_EXT=".ko"
287         else
288                 MOD_EXT=".o"
289         fi
290
291         print_info 2 "initrd: >> Searching for modules..."
292
293         if [ "${INSTALL_MOD_PATH}" != '' ]
294         then
295                 cd ${INSTALL_MOD_PATH}
296         else
297                 cd /
298         fi
299                                                                                                         
300         for i in `gen_dep_list`
301         do
302                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" 2>/dev/null| head -n 1`
303                 if [ -z "${mymod}" ]
304                 then
305                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
306                         continue;
307                 fi
308                 print_info 2 "initrd: >> Copying ${i}${MOD_EXT}..."
309                 cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
310         done
311
312         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initrd-temp 2>/dev/null
313
314         mkdir -p "${TEMP}/initrd-temp/etc/modules"
315         for group_modules in ${!MODULES_*}; do
316                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
317                 print_list ${!group_modules} > "${TEMP}/initrd-temp/etc/modules/${group}"
318         done
319 }
320
321 create_initrd_aux() {
322         if [ -f "${CMD_LINUXRC}" ]
323         then
324                 cp "${CMD_LINUXRC}" "${TEMP}/initrd-temp/linuxrc"
325                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC}"
326         else    
327                 if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
328                 then
329                         cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initrd-temp/linuxrc"
330                 else
331                         cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
332                 fi
333         fi
334
335         # Make sure it's executable
336         chmod 0755 "${TEMP}/initrd-temp/linuxrc"
337
338         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
339         then
340                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
341         else    
342                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
343         fi
344
345         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
346         then
347                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
348         else
349                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
350         fi
351         
352         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initrd-temp/etc/initrd.defaults" 
353         for group_modules in ${!MODULES_*}; do
354                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
355                 echo -n "${group} " >> "${TEMP}/initrd-temp/etc/initrd.defaults"
356         done
357         echo '"' >> "${TEMP}/initrd-temp/etc/initrd.defaults"   
358
359         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
360         then
361                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
362         else
363                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
364         fi
365         if isTrue $CMD_DOKEYMAPAUTO
366         then
367                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
368         fi
369         mkdir -p "${TEMP}/initrd-temp/lib/keymaps"
370         /bin/tar -C "${TEMP}/initrd-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
371         if isTrue $CMD_SLOWUSB
372         then
373                 echo 'MY_HWOPTS="${MY_HWOPTS} slowusb"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
374         fi
375
376         cd ${TEMP}/initrd-temp/sbin && ln -s ../linuxrc init
377         cd ${OLDPWD}
378         chmod +x "${TEMP}/initrd-temp/linuxrc"
379         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
380         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
381         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
382 }
383
384 calc_initrd_size() {
385         local TEST
386         cd ${TEMP}/initrd-temp/
387         TEST=`du -sk 2> /dev/null` 
388         echo $TEST | cut "-d " -f1
389 }
390
391 create_initrd() {
392         local MOD_EXT
393
394         print_info 1 "initrd: >> Initializing..."
395         create_base_initrd_sys
396
397         if [ "${NOINITRDMODULES}" = '' ]
398         then
399                 print_info 1 "        >> Copying modules..."
400                 create_initrd_modules
401         else
402                 print_info 1 "initrd: Not copying modules..."
403         fi
404
405         print_info 1 "        >> Copying auxilary files..."
406         create_initrd_aux
407
408         INITRD_CALC_SIZE=`calc_initrd_size`
409         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 250`
410         print_info 1 "        :: Size is at ${INITRD_SIZE}K"
411         print_info 1 "        >> Creating loopback filesystem..."
412         create_initrd_loop ${INITRD_SIZE}
413
414         print_info 1 "        >> Moving initrd files to the loopback..."
415         move_initrd_to_loop
416
417         print_info 1 "        >> Cleaning up and compressing the initrd..."
418         create_initrd_unmount_loop
419
420         if [ "${COMPRESS_INITRD}" ]
421         then
422                 gzip -f -9 ${TMPDIR}/initrd-${KV}
423                 mv ${TMPDIR}/initrd-${KV}.gz ${TMPDIR}/initrd-${KV}
424         fi
425
426         if [ "${BOOTSPLASH}" -eq "1" ]
427         then
428                 if [ -x /sbin/splash ]
429                 then
430                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash.conf ] && source /etc/conf.d/bootsplash.conf
431                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash ] && source /etc/conf.d/bootsplash
432                         [ -z "${BOOTSPLASH_THEME}" ] && BOOTSPLASH_THEME=default
433                         print_info 1 "        >> Installing bootsplash [ using the ${BOOTSPLASH_THEME} theme ]..."
434                         for bootRes in '800x600' '1024x768' '1280x1024' '1600x1200'
435                         do
436                                 if [ -f "/etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg" ]
437                                 then
438                                         /sbin/splash -s -f /etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg >> ${TMPDIR}/initrd-${KV} ||
439                                                 gen_die "Error: could not copy ${bootRes} bootsplash!"
440                                 else
441                                         print_warning 1 "splash: Did not find a bootsplash for the ${bootRes} resolution..."
442                                 fi
443                         done
444                 else
445                         print_warning 1 '        >> No bootsplash detected; skipping!'
446                 fi
447         fi
448         if ! isTrue "${CMD_NOINSTALL}"
449         then
450                 copy_image_with_preserve "initrd" \
451                         "${TMPDIR}/initrd-${KV}" \
452                         "initrd-${KNAME}-${ARCH}-${KV}"
453         fi
454
455         # Pegasos hack for merging the initrd into the kernel at compile time
456         [ "${KERNEL_MAKE_DIRECTIVE}" == 'zImage.initrd' -a "${GENERATE_Z_IMAGE}" = '1' ] ||
457                 [ "${KERNEL_MAKE_DIRECTIVE_2}" == 'zImage.initrd' -a "${GENERATE_Z_IMAGE}" = '1' ] &&
458                 cp ${TMPDIR}/initrd-${KV} ${KERNEL_DIR}/arch/${ARCH}/boot/images/ramdisk.image.gz &&
459                 rm ${TMPDIR}/initrd-${KV}
460
461         # Mips also mimics Pegasos to merge the initrd into the kernel
462         [ ${BUILD_INITRAMFS} -eq 1 ] \
463                 && cp ${TMPDIR}/initrd-${KV} ${KERNEL_DIR}/mips/ramdisk/initrd.img.gz
464 }