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