The initrd code was still looking for udev, even though we aren't building it anymore...
[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 -N500 -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
148         # DMRAID 
149         if [ "${DMRAID}" = '1' ]
150         then
151                 print_info 1 'DMRAID: Adding support (compiling binaries)...'
152                 compile_dmraid
153                 /bin/tar -jxpf "${DMRAID_BINCACHE}" -C "${TEMP}/initrd-temp" ||
154                         gen_die "Could not extract dmraid binary cache!";
155         fi
156
157         # LVM2
158         if [ "${LVM2}" = '1' ]
159         then
160                 if [ -e '/sbin/lvm' ] && ldd /sbin/lvm|grep -q 'not a dynamic executable';
161                 then
162                         print_info 1 'LVM2: Adding support (using local static binaries)...'
163                         cp /sbin/lvm "${TEMP}/initrd-temp/bin/lvm" ||
164                                 gen_die 'Could not copy over lvm!'
165                 else
166                         print_info 1 'LVM2: Adding support (compiling binaries)...'
167                         compile_lvm2
168
169                         /bin/tar -jxpf "${LVM2_BINCACHE}" -C "${TEMP}/initrd-temp" ||
170                                 gen_die "Could not extract lvm2 binary cache!";
171                         mv ${TEMP}/initrd-temp/bin/lvm.static ${TEMP}/initrd-temp/bin/lvm ||
172                                 gen_die 'LVM2 error: Could not move lvm.static to lvm!'
173                 fi
174                 for i in vgchange vgscan; do
175                         ln  ${TEMP}/initrd-temp/bin/lvm ${TEMP}/initrd-temp/bin/$i ||
176                                 gen_die "LVM2 error: Could not link ${i}!"
177                 done
178                 mkdir -p ${TEMP}/initrd-temp/etc/lvm
179                 cp /etc/lvm/lvm.conf "${TEMP}/initrd-temp/etc/lvm/lvm.conf" ||
180                         gen_die 'Could not copy over lvm.conf!'
181         fi
182         
183         # EVMS2
184         if [ "${EVMS2}" = '1' ]
185         then
186                 if [ -e '/sbin/evms_activate' ]
187                 then
188                         print_info 1 'EVMS2: Adding support...' 
189                         mkdir -p ${TEMP}/initrd-temp/lib
190                         mkdir -p ${TEMP}/initrd-temp/sbin
191                         mkdir -p ${TEMP}/initrd-temp/etc
192                         mkdir -p ${TEMP}/initrd-temp/bin
193                         cp -a /lib/ld-* "${TEMP}/initrd-temp/lib" \
194                                 || gen_die 'Could not copy files for EVMS2!'
195                         if [ -n "`ls /lib/libgcc_s*`" ]
196                         then
197                                 cp -a /lib/libgcc_s* "${TEMP}/initramfs-evms2-temp/lib" \
198                                         || gen_die 'Could not copy files for EVMS2!'
199                         fi
200                         cp -a /lib/libc-* /lib/libc.* "${TEMP}/initrd-temp/lib" \
201                                 || gen_die 'Could not copy files for EVMS2!'
202                         cp -a /lib/libdl-* /lib/libdl.* "${TEMP}/initrd-temp/lib" \
203                                 || gen_die 'Could not copy files for EVMS2!'
204                         cp -a /lib/libpthread* "${TEMP}/initrd-temp/lib" \
205                                 || gen_die 'Could not copy files for EVMS2!'
206                         cp -a /lib/libuuid*so* "${TEMP}/initrd-temp/lib" \
207                                 || gen_die 'Could not copy files for EVMS2!'
208                         cp -a /lib/libevms*so* "${TEMP}/initrd-temp/lib" \
209                                 || gen_die 'Could not copy files for EVMS2!'
210                         cp -a /lib/evms "${TEMP}/initrd-temp/lib" \
211                                 || gen_die 'Could not copy files for EVMS2!'
212                         cp -a /lib/evms/* "${TEMP}/initrd-temp/lib/evms" \
213                                 || gen_die 'Could not copy files for EVMS2!'
214                         cp -a /etc/evms.conf "${TEMP}/initrd-temp/etc" \
215                                 || gen_die 'Could not copy files for EVMS2!'
216                         cp /sbin/evms_activate "${TEMP}/initrd-temp/sbin" \
217                                 || gen_die 'Could not copy over evms_activate!'
218                         # Fix EVMS2 complaining that it cant find the swap utilities.
219                         # These are not required in the initrd
220                         for swap_libs in "${TEMP}/initrd-temp/lib/evms/*/swap*.so"
221                         do
222                                 rm ${swap_libs}
223                         done
224                 fi
225         fi      
226
227         for i in '[' ash basename cat chroot clear cp dirname echo env false find \
228         grep gunzip gzip ln ls loadkmap losetup lsmod mkdir mknod more mount mv \
229         pivot_root ps awk pwd rm rmdir rmmod sed sh sleep tar test touch true umount uname \
230         xargs yes zcat chmod chown cut kill killall; do
231                 rm -f ${TEMP}/initrd-temp/bin/$i > /dev/null
232                 ln  ${TEMP}/initrd-temp/bin/busybox ${TEMP}/initrd-temp/bin/$i ||
233                         gen_die "Busybox error: could not link ${i}!"
234         done
235
236         if isTrue ${LUKS}
237         then
238                 if is_static /bin/cryptsetup
239                 then
240                         print_info "Including LUKS support"
241                         rm -f ${TEMP}/initrd-temp/sbin/cryptsetup
242                         cp /bin/cryptsetup ${TEMP}/initrd-temp/sbin/cryptsetup
243                         chmod +x "${TEMP}/initrd-temp/sbin/cryptsetup"
244                 else
245                         print_info "LUKS support requires static cryptsetup at /bin/cryptsetup"
246                         print_info "Not including LUKS support"
247                 fi
248         fi
249 }
250
251 print_list() {
252         local x
253         for x in ${*}
254         do
255                 echo ${x}
256         done
257 }
258
259 create_initrd_modules() {
260         local group
261         local group_modules
262         
263         if [ "${PAT}" -gt "4" ]
264         then
265                 MOD_EXT=".ko"
266         else
267                 MOD_EXT=".o"
268         fi
269
270         print_info 2 "initrd: >> Searching for modules..."
271
272         if [ "${INSTALL_MOD_PATH}" != '' ]
273         then
274                 cd ${INSTALL_MOD_PATH}
275         else
276                 cd /
277         fi
278                                                                                                         
279         for i in `gen_dep_list`
280         do
281                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" 2>/dev/null| head -n 1`
282                 if [ -z "${mymod}" ]
283                 then
284                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
285                         continue;
286                 fi
287                 print_info 2 "initrd: >> Copying ${i}${MOD_EXT}..."
288                 cp -ax --parents "${mymod}" "${TEMP}/initrd-temp"
289         done
290
291         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initrd-temp 2>/dev/null
292
293         mkdir -p "${TEMP}/initrd-temp/etc/modules"
294         for group_modules in ${!MODULES_*}; do
295                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
296                 print_list ${!group_modules} > "${TEMP}/initrd-temp/etc/modules/${group}"
297         done
298 }
299
300 create_initrd_aux() {
301         if [ -f "${CMD_LINUXRC}" ]
302         then
303                 cp "${CMD_LINUXRC}" "${TEMP}/initrd-temp/linuxrc"
304                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC}"
305         else    
306                 if [ -f "${GK_SHARE}/${ARCH}/linuxrc" ]
307                 then
308                         cp "${GK_SHARE}/${ARCH}/linuxrc" "${TEMP}/initrd-temp/linuxrc"
309                 else
310                         cp "${GK_SHARE}/generic/linuxrc" "${TEMP}/initrd-temp/linuxrc"
311                 fi
312         fi
313
314         # Make sure it's executable
315         chmod 0755 "${TEMP}/initrd-temp/linuxrc"
316
317         if [ -f "${GK_SHARE}/${ARCH}/initrd.scripts" ]
318         then
319                 cp "${GK_SHARE}/${ARCH}/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
320         else    
321                 cp "${GK_SHARE}/generic/initrd.scripts" "${TEMP}/initrd-temp/etc/initrd.scripts"
322         fi
323
324         if [ -f "${GK_SHARE}/${ARCH}/initrd.defaults" ]
325         then
326                 cp "${GK_SHARE}/${ARCH}/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
327         else
328                 cp "${GK_SHARE}/generic/initrd.defaults" "${TEMP}/initrd-temp/etc/initrd.defaults"
329         fi
330         
331         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initrd-temp/etc/initrd.defaults" 
332         for group_modules in ${!MODULES_*}; do
333                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
334                 echo -n "${group} " >> "${TEMP}/initrd-temp/etc/initrd.defaults"
335         done
336         echo '"' >> "${TEMP}/initrd-temp/etc/initrd.defaults"   
337
338         if [ -f "${GK_SHARE}/${ARCH}/modprobe" ]
339         then
340                 cp "${GK_SHARE}/${ARCH}/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
341         else
342                 cp "${GK_SHARE}/generic/modprobe" "${TEMP}/initrd-temp/sbin/modprobe"
343         fi
344         if isTrue $CMD_DOKEYMAPAUTO
345         then
346                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
347         fi
348         mkdir -p "${TEMP}/initrd-temp/lib/keymaps"
349         /bin/tar -C "${TEMP}/initrd-temp/lib/keymaps" -zxf "${GK_SHARE}/generic/keymaps.tar.gz"
350         if isTrue $CMD_SLOWUSB
351         then
352                 echo 'MY_HWOPTS="${MY_HWOPTS} slowusb"' >> ${TEMP}/initrd-temp/etc/initrd.defaults
353         fi
354
355         cd ${TEMP}/initrd-temp/sbin && ln -s ../linuxrc init
356         cd ${OLDPWD}
357         chmod +x "${TEMP}/initrd-temp/linuxrc"
358         chmod +x "${TEMP}/initrd-temp/etc/initrd.scripts"
359         chmod +x "${TEMP}/initrd-temp/etc/initrd.defaults"
360         chmod +x "${TEMP}/initrd-temp/sbin/modprobe"
361 }
362
363 calc_initrd_size() {
364         local TEST
365         cd ${TEMP}/initrd-temp/
366         TEST=`du -sk 2> /dev/null` 
367         echo $TEST | cut "-d " -f1
368 }
369
370 create_initrd() {
371         local MOD_EXT
372
373         print_info 1 "initrd: >> Initializing..."
374         create_base_initrd_sys
375
376         if [ "${NOINITRDMODULES}" = '' ]
377         then
378                 print_info 1 "        >> Copying modules..."
379                 create_initrd_modules
380         else
381                 print_info 1 "initrd: Not copying modules..."
382         fi
383
384         print_info 1 "        >> Copying auxilary files..."
385         create_initrd_aux
386
387         INITRD_CALC_SIZE=`calc_initrd_size`
388         INITRD_SIZE=`expr ${INITRD_CALC_SIZE} + 100`
389         print_info 1 "        :: Size is at ${INITRD_SIZE}K"
390         print_info 1 "        >> Creating loopback filesystem..."
391         create_initrd_loop ${INITRD_SIZE}
392
393         print_info 1 "        >> Moving initrd files to the loopback..."
394         move_initrd_to_loop
395
396         print_info 1 "        >> Cleaning up and compressing the initrd..."
397         create_initrd_unmount_loop
398
399         if [ "${COMPRESS_INITRD}" ]
400         then
401                 gzip -f -9 ${TMPDIR}/initrd-${KV}
402                 mv ${TMPDIR}/initrd-${KV}.gz ${TMPDIR}/initrd-${KV}
403         fi
404
405         if [ "${BOOTSPLASH}" -eq "1" ]
406         then
407                 if [ -x /sbin/splash ]
408                 then
409                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash.conf ] && source /etc/conf.d/bootsplash.conf
410                         [ -z "${BOOTSPLASH_THEME}" ] && [ -e /etc/conf.d/bootsplash ] && source /etc/conf.d/bootsplash
411                         [ -z "${BOOTSPLASH_THEME}" ] && BOOTSPLASH_THEME=default
412                         print_info 1 "        >> Installing bootsplash [ using the ${BOOTSPLASH_THEME} theme ]..."
413                         for bootRes in '800x600' '1024x768' '1280x1024' '1600x1200'
414                         do
415                                 if [ -f "/etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg" ]
416                                 then
417                                         /sbin/splash -s -f /etc/bootsplash/${BOOTSPLASH_THEME}/config/bootsplash-${bootRes}.cfg >> ${TMPDIR}/initrd-${KV} ||
418                                                 gen_die "Error: could not copy ${bootRes} bootsplash!"
419                                 else
420                                         print_warning 1 "splash: Did not find a bootsplash for the ${bootRes} resolution..."
421                                 fi
422                         done
423                 else
424                         print_warning 1 '        >> No bootsplash detected; skipping!'
425                 fi
426         fi
427         if ! isTrue "${CMD_NOINSTALL}"
428         then
429                 cp ${TMPDIR}/initrd-${KV} ${BOOTDIR}/initrd-${KNAME}-${ARCH}-${KV} ||
430                         gen_die 'Could not copy the initrd to ${BOOTDIR}!'
431         fi
432
433         # Pegasos hack for merging the initrd into the kernel at compile time
434         [ "${KERNEL_MAKE_DIRECTIVE}" == 'zImage.initrd' -a "${GENERATE_Z_IMAGE}" = '1' ] ||
435                 [ "${KERNEL_MAKE_DIRECTIVE_2}" == 'zImage.initrd' -a "${GENERATE_Z_IMAGE}" = '1' ] &&
436                 cp ${TMPDIR}/initrd-${KV} ${KERNEL_DIR}/arch/${ARCH}/boot/images/ramdisk.image.gz &&
437                 rm ${TMPDIR}/initrd-${KV}
438
439         # Mips also mimics Pegasos to merge the initrd into the kernel
440         [ ${BUILD_INITRAMFS} -eq 1 ] \
441                 && cp ${TMPDIR}/initrd-${KV} ${KERNEL_DIR}/mips/ramdisk/initrd.img.gz
442 }