Bump version to 3.4.30
[genkernel.git] / gen_initramfs.sh
1 #!/bin/bash
2 # $Id$
3
4 CPIO_ARGS="--quiet -o -H newc"
5
6 copy_binaries() {
7         local destdir=$1
8         shift
9
10         for binary in "$@"; do
11                 [[ -e "${binary}" ]] \
12                                 || gen_die "Binary ${binary} could not be found"
13
14                 if LC_ALL=C lddtree "${binary}" 2>&1 | fgrep -q 'not found'; then
15                         gen_die "Binary ${binary} is linked to missing libraries and may need to be re-built"
16                 fi
17
18                 lddtree "${binary}" \
19                                 | tr ')(' '\n' \
20                                 | awk  '/=>/{ if($3 ~ /^\//){print $3}}' \
21                                 | sort \
22                                 | uniq \
23                                 | cpio -p --make-directories --dereference --quiet "${destdir}" \
24                                 || gen_die "Binary ${f} or some of its library dependencies could not be copied"
25         done
26 }
27
28 append_base_layout() {
29         if [ -d "${TEMP}/initramfs-base-temp" ]
30         then
31                 rm -rf "${TEMP}/initramfs-base-temp" > /dev/null
32         fi
33
34         mkdir -p ${TEMP}/initramfs-base-temp/dev
35         mkdir -p ${TEMP}/initramfs-base-temp/bin
36         mkdir -p ${TEMP}/initramfs-base-temp/etc
37         mkdir -p ${TEMP}/initramfs-base-temp/usr
38         mkdir -p ${TEMP}/initramfs-base-temp/lib
39         mkdir -p ${TEMP}/initramfs-base-temp/mnt
40         mkdir -p ${TEMP}/initramfs-base-temp/run
41         mkdir -p ${TEMP}/initramfs-base-temp/sbin
42         mkdir -p ${TEMP}/initramfs-base-temp/proc
43         mkdir -p ${TEMP}/initramfs-base-temp/temp
44         mkdir -p ${TEMP}/initramfs-base-temp/tmp
45         mkdir -p ${TEMP}/initramfs-base-temp/sys
46         mkdir -p ${TEMP}/initramfs-temp/.initrd
47         mkdir -p ${TEMP}/initramfs-base-temp/var/lock/dmraid
48         mkdir -p ${TEMP}/initramfs-base-temp/sbin
49         mkdir -p ${TEMP}/initramfs-base-temp/usr/bin
50         mkdir -p ${TEMP}/initramfs-base-temp/usr/sbin
51         ln -s  lib  ${TEMP}/initramfs-base-temp/lib64
52
53         echo "/dev/ram0     /           ext2    defaults        0 0" > ${TEMP}/initramfs-base-temp/etc/fstab
54         echo "proc          /proc       proc    defaults    0 0" >> ${TEMP}/initramfs-base-temp/etc/fstab
55
56         cd ${TEMP}/initramfs-base-temp/dev
57         mknod -m 660 console c 5 1
58         mknod -m 660 null c 1 3
59         mknod -m 660 zero c 1 5
60         mknod -m 600 tty0 c 4 0
61         mknod -m 600 tty1 c 4 1
62         mknod -m 600 ttyS0 c 4 64
63
64         date -u '+%Y%m%d-%H%M%S' > ${TEMP}/initramfs-base-temp/etc/build_date
65
66         cd "${TEMP}/initramfs-base-temp/"
67         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
68                         || gen_die "compressing baselayout cpio"
69         cd "${TEMP}"
70         rm -rf "${TEMP}/initramfs-base-temp" > /dev/null
71 }
72
73 append_busybox() {
74         if [ -d "${TEMP}/initramfs-busybox-temp" ]
75         then
76                 rm -rf "${TEMP}/initramfs-busybox-temp" > /dev/null
77         fi
78
79         mkdir -p "${TEMP}/initramfs-busybox-temp/bin/" 
80         tar -xjf "${BUSYBOX_BINCACHE}" -C "${TEMP}/initramfs-busybox-temp/bin" busybox ||
81                 gen_die 'Could not extract busybox bincache!'
82         chmod +x "${TEMP}/initramfs-busybox-temp/bin/busybox"
83
84         mkdir -p "${TEMP}/initramfs-busybox-temp/usr/share/udhcpc/"
85         cp "${GK_SHARE}/defaults/udhcpc.scripts" ${TEMP}/initramfs-busybox-temp/usr/share/udhcpc/default.script
86         chmod +x "${TEMP}/initramfs-busybox-temp/usr/share/udhcpc/default.script"
87
88         # Set up a few default symlinks
89         for i in ${BUSYBOX_APPLETS:-[ ash sh mount uname echo cut cat}; do
90                 rm -f ${TEMP}/initramfs-busybox-temp/bin/$i > /dev/null
91                 ln -s busybox ${TEMP}/initramfs-busybox-temp/bin/$i ||
92                         gen_die "Busybox error: could not link ${i}!"
93         done
94
95         cd "${TEMP}/initramfs-busybox-temp/"
96         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
97                         || gen_die "compressing busybox cpio"
98         cd "${TEMP}"
99         rm -rf "${TEMP}/initramfs-busybox-temp" > /dev/null
100 }
101
102 append_blkid(){
103         if [ -d "${TEMP}/initramfs-blkid-temp" ]
104         then
105                 rm -r "${TEMP}/initramfs-blkid-temp/"
106         fi
107         cd ${TEMP}
108         mkdir -p "${TEMP}/initramfs-blkid-temp/sbin/"
109         [ "${DISKLABEL}" = '1' ] && { /bin/bzip2 -dc "${BLKID_BINCACHE}" > "${TEMP}/initramfs-blkid-temp/sbin/blkid" ||
110                 gen_die "Could not extract blkid binary cache!"; }
111         chmod a+x "${TEMP}/initramfs-blkid-temp/sbin/blkid"
112         cd "${TEMP}/initramfs-blkid-temp/"
113         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
114                         || gen_die "compressing blkid cpio"
115         cd "${TEMP}"
116         rm -rf "${TEMP}/initramfs-blkid-temp" > /dev/null
117 }
118
119 #append_fuse() {
120 #       if [ -d "${TEMP}/initramfs-fuse-temp" ]
121 #       then
122 #               rm -r "${TEMP}/initramfs-fuse-temp"
123 #       fi
124 #       cd ${TEMP}
125 #       mkdir -p "${TEMP}/initramfs-fuse-temp/lib/"
126 #       tar -C "${TEMP}/initramfs-fuse-temp/lib/" -xjf "${FUSE_BINCACHE}"
127 #       cd "${TEMP}/initramfs-fuse-temp/"
128 #       find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
129 #                       || gen_die "compressing fuse cpio"
130 #       rm -rf "${TEMP}/initramfs-fuse-temp" > /dev/null
131 #}
132
133 append_unionfs_fuse() {
134         if [ -d "${TEMP}/initramfs-unionfs-fuse-temp" ]
135         then
136                 rm -r "${TEMP}/initramfs-unionfs-fuse-temp"
137         fi
138         cd ${TEMP}
139         mkdir -p "${TEMP}/initramfs-unionfs-fuse-temp/sbin/"
140         bzip2 -dc "${UNIONFS_FUSE_BINCACHE}" > "${TEMP}/initramfs-unionfs-fuse-temp/sbin/unionfs" ||
141                 gen_die 'Could not extract unionfs-fuse binary cache!'
142         chmod a+x "${TEMP}/initramfs-unionfs-fuse-temp/sbin/unionfs"
143         cd "${TEMP}/initramfs-unionfs-fuse-temp/"
144         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
145                         || gen_die "compressing unionfs fuse cpio"
146         cd "${TEMP}"
147         rm -rf "${TEMP}/initramfs-unionfs-fuse-temp" > /dev/null
148 }
149
150 #append_suspend(){
151 #       if [ -d "${TEMP}/initramfs-suspend-temp" ];
152 #       then
153 #               rm -r "${TEMP}/initramfs-suspend-temp/"
154 #       fi
155 #       print_info 1 'SUSPEND: Adding support (compiling binaries)...'
156 #       compile_suspend
157 #       mkdir -p "${TEMP}/initramfs-suspend-temp/"
158 #       /bin/tar -jxpf "${SUSPEND_BINCACHE}" -C "${TEMP}/initramfs-suspend-temp" ||
159 #               gen_die "Could not extract suspend binary cache!"
160 #       mkdir -p "${TEMP}/initramfs-suspend-temp/etc"
161 #       cp -f /etc/suspend.conf "${TEMP}/initramfs-suspend-temp/etc" ||
162 #               gen_die 'Could not copy /etc/suspend.conf'
163 #       cd "${TEMP}/initramfs-suspend-temp/"
164 #       find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
165 #                       || gen_die "compressing suspend cpio"
166 #       rm -r "${TEMP}/initramfs-suspend-temp/"
167 #}
168
169 append_multipath(){
170         if [ -d "${TEMP}/initramfs-multipath-temp" ]
171         then
172                 rm -r "${TEMP}/initramfs-multipath-temp"
173         fi
174         print_info 1 '  Multipath support being added'
175         mkdir -p "${TEMP}"/initramfs-multipath-temp/{bin,etc,sbin,lib}/
176
177         # Copy files
178         copy_binaries "${TEMP}/initramfs-multipath-temp" /sbin/{multipath,kpartx,mpath_prio_*,devmap_name,dmsetup} /lib64/udev/scsi_id /bin/mountpoint
179
180         if [ -x /sbin/multipath ]
181         then
182                 cp /etc/multipath.conf "${TEMP}/initramfs-multipath-temp/etc/" || gen_die 'could not copy /etc/multipath.conf please check this'
183         fi
184         # /etc/scsi_id.config does not exist in newer udevs
185         # copy it optionally.
186         if [ -x /sbin/scsi_id -a -f /etc/scsi_id.config ]
187         then
188                 cp /etc/scsi_id.config "${TEMP}/initramfs-multipath-temp/etc/" || gen_die 'could not copy scsi_id.config'
189         fi
190         cd "${TEMP}/initramfs-multipath-temp"
191         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
192                         || gen_die "compressing multipath cpio"
193         cd "${TEMP}"
194         rm -r "${TEMP}/initramfs-multipath-temp/"
195 }
196
197 append_dmraid(){
198         if [ -d "${TEMP}/initramfs-dmraid-temp" ]
199         then
200                 rm -r "${TEMP}/initramfs-dmraid-temp/"
201         fi
202         print_info 1 'DMRAID: Adding support (compiling binaries)...'
203         compile_dmraid
204         mkdir -p "${TEMP}/initramfs-dmraid-temp/"
205         /bin/tar -jxpf "${DMRAID_BINCACHE}" -C "${TEMP}/initramfs-dmraid-temp" ||
206                 gen_die "Could not extract dmraid binary cache!";
207         cd "${TEMP}/initramfs-dmraid-temp/"
208         RAID456=`find . -type f -name raid456.ko`
209         if [ -n "${RAID456}" ]
210         then
211                 cd "${RAID456/raid456.ko/}"
212                 ln -sf raid456.kp raid45.ko
213                 cd "${TEMP}/initramfs-dmraid-temp/"
214         fi
215         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
216                         || gen_die "compressing dmraid cpio"
217         cd "${TEMP}"
218         rm -r "${TEMP}/initramfs-dmraid-temp/"
219 }
220
221 append_iscsi(){
222         if [ -d "${TEMP}/initramfs-iscsi-temp" ]
223         then
224                 rm -r "${TEMP}/initramfs-iscsi-temp/"
225         fi
226         print_info 1 'iSCSI: Adding support (compiling binaries)...'
227         compile_iscsi
228         cd ${TEMP}
229         mkdir -p "${TEMP}/initramfs-iscsi-temp/bin/"
230         /bin/bzip2 -dc "${ISCSI_BINCACHE}" > "${TEMP}/initramfs-iscsi-temp/bin/iscsistart" ||
231                 gen_die "Could not extract iscsi binary cache!"
232         chmod a+x "${TEMP}/initramfs-iscsi-temp/bin/iscsistart"
233         cd "${TEMP}/initramfs-iscsi-temp/"
234         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
235                         || gen_die "compressing iscsi cpio"
236         cd "${TEMP}"
237         rm -rf "${TEMP}/initramfs-iscsi-temp" > /dev/null
238 }
239
240 append_lvm(){
241         if [ -d "${TEMP}/initramfs-lvm-temp" ]
242         then
243                 rm -r "${TEMP}/initramfs-lvm-temp/"
244         fi
245         cd ${TEMP}
246         mkdir -p "${TEMP}/initramfs-lvm-temp/bin/"
247         mkdir -p "${TEMP}/initramfs-lvm-temp/etc/lvm/"
248         if false && [ -e '/sbin/lvm.static' ]
249         then
250                 print_info 1 '          LVM: Adding support (using local static binary /sbin/lvm.static)...'
251                 cp /sbin/lvm.static "${TEMP}/initramfs-lvm-temp/bin/lvm" ||
252                         gen_die 'Could not copy over lvm!'
253                 # See bug 382555
254                 if [ -e '/sbin/dmsetup.static' ]
255                 then
256                         cp /sbin/dmsetup.static "${TEMP}/initramfs-lvm-temp/bin/dmsetup"
257                 fi
258         elif false && [ -e '/sbin/lvm' ] && LC_ALL="C" ldd /sbin/lvm|grep -q 'not a dynamic executable'
259         then
260                 print_info 1 '          LVM: Adding support (using local static binary /sbin/lvm)...'
261                 cp /sbin/lvm "${TEMP}/initramfs-lvm-temp/bin/lvm" ||
262                         gen_die 'Could not copy over lvm!'
263                 # See bug 382555
264                 if [ -e '/sbin/dmsetup' ] && LC_ALL="C" ldd /sbin/dmsetup | grep -q 'not a dynamic executable'
265                 then
266                         cp /sbin/dmsetup "${TEMP}/initramfs-lvm-temp/bin/dmsetup"
267                 fi
268         else
269                 print_info 1 '          LVM: Adding support (compiling binaries)...'
270                 compile_lvm
271                 /bin/tar -jxpf "${LVM_BINCACHE}" -C "${TEMP}/initramfs-lvm-temp" ||
272                         gen_die "Could not extract lvm binary cache!";
273                 mv ${TEMP}/initramfs-lvm-temp/sbin/lvm.static ${TEMP}/initramfs-lvm-temp/bin/lvm ||
274                         gen_die 'LVM error: Could not move lvm.static to lvm!'
275                 # See bug 382555
276                 mv ${TEMP}/initramfs-lvm-temp/sbin/dmsetup.static ${TEMP}/initramfs-lvm-temp/bin/dmsetup ||
277                         gen_die 'LVM error: Could not move dmsetup.static to dmsetup!'
278                 rm -rf  ${TEMP}/initramfs-lvm-temp/{lib,share,man,include,sbin/{lvm,dmsetup}}
279         fi
280         if [ -x /sbin/lvm ]
281         then
282 #               lvm dumpconfig 2>&1 > /dev/null || gen_die 'Could not copy over lvm.conf!'
283 #               ret=$?
284 #               if [ ${ret} != 0 ]
285 #               then
286                         cp /etc/lvm/lvm.conf "${TEMP}/initramfs-lvm-temp/etc/lvm/" ||
287                                 gen_die 'Could not copy over lvm.conf!'
288 #               else
289 #                       gen_die 'Could not copy over lvm.conf!'
290 #               fi
291         fi
292         cd "${TEMP}/initramfs-lvm-temp/"
293         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
294                         || gen_die "compressing lvm cpio"
295         cd "${TEMP}"
296         rm -r "${TEMP}/initramfs-lvm-temp/"
297 }
298
299 append_mdadm(){
300         if [ -d "${TEMP}/initramfs-mdadm-temp" ]
301         then
302                 rm -r "${TEMP}/initramfs-mdadm-temp/"
303         fi
304         cd ${TEMP}
305         mkdir -p "${TEMP}/initramfs-mdadm-temp/etc/"
306         mkdir -p "${TEMP}/initramfs-mdadm-temp/sbin/"
307         if [ "${MDADM}" = '1' ]
308         then
309                 if [ -n "${MDADM_CONFIG}" ]
310                 then
311                         if [ -f "${MDADM_CONFIG}" ]
312                         then
313                                 cp -a "${MDADM_CONFIG}" "${TEMP}/initramfs-mdadm-temp/etc/mdadm.conf" \
314                                 || gen_die "Could not copy mdadm.conf!"
315                         else
316                                 gen_die 'sl${MDADM_CONFIG} does not exist!'
317                         fi
318                 else
319                         print_info 1 '          MDADM: Skipping inclusion of mdadm.conf'
320                 fi
321
322                 if [ -e '/sbin/mdadm' ] && LC_ALL="C" ldd /sbin/mdadm | grep -q 'not a dynamic executable' \
323                 && [ -e '/sbin/mdmon' ] && LC_ALL="C" ldd /sbin/mdmon | grep -q 'not a dynamic executable'
324                 then
325                         print_info 1 '          MDADM: Adding support (using local static binaries /sbin/mdadm and /sbin/mdmon)...'
326                         cp /sbin/mdadm /sbin/mdmon "${TEMP}/initramfs-mdadm-temp/sbin/" ||
327                                 gen_die 'Could not copy over mdadm!'
328                 else
329                         print_info 1 '          MDADM: Adding support (compiling binaries)...'
330                         compile_mdadm
331                         /bin/tar -jxpf "${MDADM_BINCACHE}" -C "${TEMP}/initramfs-mdadm-temp" ||
332                                 gen_die "Could not extract mdadm binary cache!";
333                 fi
334         fi
335         cd "${TEMP}/initramfs-mdadm-temp/"
336         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
337                         || gen_die "compressing mdadm cpio"
338         cd "${TEMP}"
339         rm -rf "${TEMP}/initramfs-mdadm-temp" > /dev/null
340 }
341
342 append_zfs(){
343         if [ -d "${TEMP}/initramfs-zfs-temp" ]
344         then
345                 rm -r "${TEMP}/initramfs-zfs-temp"
346         fi
347
348         mkdir -p "${TEMP}/initramfs-zfs-temp/etc/zfs/"
349
350         # Copy files to /etc/zfs
351         for i in /etc/zfs/{zdev.conf,zpool.cache}
352         do
353                 cp -a "${i}" "${TEMP}/initramfs-zfs-temp/etc/zfs" \
354                         || gen_die "Could not copy file ${i} for ZFS"
355         done
356
357         # Copy binaries
358         copy_binaries "${TEMP}/initramfs-zfs-temp" /sbin/{mount.zfs,zfs,zpool}
359
360         cd "${TEMP}/initramfs-zfs-temp/"
361         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
362                         || gen_die "compressing zfs cpio"
363         cd "${TEMP}"
364         rm -rf "${TEMP}/initramfs-zfs-temp" > /dev/null
365 }
366
367 append_splash(){
368         splash_geninitramfs=`which splash_geninitramfs 2>/dev/null`
369         if [ -x "${splash_geninitramfs}" ]
370         then
371                 [ -z "${SPLASH_THEME}" ] && [ -e /etc/conf.d/splash ] && source /etc/conf.d/splash
372                 [ -z "${SPLASH_THEME}" ] && SPLASH_THEME=default
373                 print_info 1 "  >> Installing splash [ using the ${SPLASH_THEME} theme ]..."
374                 if [ -d "${TEMP}/initramfs-splash-temp" ]
375                 then
376                         rm -r "${TEMP}/initramfs-splash-temp/"
377                 fi
378                 mkdir -p "${TEMP}/initramfs-splash-temp"
379                 cd /
380                 local tmp=""
381                 [ -n "${SPLASH_RES}" ] && tmp="-r ${SPLASH_RES}"
382                 splash_geninitramfs -c "${TEMP}/initramfs-splash-temp" ${tmp} ${SPLASH_THEME} || gen_die "Could not build splash cpio archive"
383                 if [ -e "/usr/share/splashutils/initrd.splash" ]; then
384                         mkdir -p "${TEMP}/initramfs-splash-temp/etc"
385                         cp -f "/usr/share/splashutils/initrd.splash" "${TEMP}/initramfs-splash-temp/etc"
386                 fi
387                 cd "${TEMP}/initramfs-splash-temp/"
388                 find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
389                         || gen_die "compressing splash cpio"
390                 cd "${TEMP}"
391                 rm -r "${TEMP}/initramfs-splash-temp/"
392         else
393                 print_warning 1 '               >> No splash detected; skipping!'
394         fi
395 }
396
397 append_overlay(){
398         cd ${INITRAMFS_OVERLAY}
399         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
400                         || gen_die "compressing overlay cpio"
401 }
402
403 append_luks() {
404         local _luks_error_format="LUKS support cannot be included: %s.  Please emerge sys-fs/cryptsetup[static]."
405         local _luks_source=/sbin/cryptsetup
406         local _luks_dest=/sbin/cryptsetup
407
408         if [ -d "${TEMP}/initramfs-luks-temp" ]
409         then
410                 rm -r "${TEMP}/initramfs-luks-temp/"
411         fi
412
413         mkdir -p "${TEMP}/initramfs-luks-temp/lib/luks/"
414         mkdir -p "${TEMP}/initramfs-luks-temp/sbin"
415         cd "${TEMP}/initramfs-luks-temp"
416
417         if isTrue ${LUKS}
418         then
419                 [ -x "${_luks_source}" ] \
420                                 || gen_die "$(printf "${_luks_error_format}" "no file ${_luks_source}")"
421
422                 print_info 1 "Including LUKS support"
423                 copy_binaries "${TEMP}/initramfs-luks-temp/" /sbin/cryptsetup
424         fi
425
426         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
427                 || gen_die "appending cryptsetup to cpio"
428
429         cd "${TEMP}"
430         rm -r "${TEMP}/initramfs-luks-temp/"
431 }
432
433 append_firmware() {
434         if [ -z "${FIRMWARE_FILES}" -a ! -d "${FIRMWARE_DIR}" ]
435         then
436                 gen_die "specified firmware directory (${FIRMWARE_DIR}) does not exist"
437         fi
438         if [ -d "${TEMP}/initramfs-firmware-temp" ]
439         then
440                 rm -r "${TEMP}/initramfs-firmware-temp/"
441         fi
442         mkdir -p "${TEMP}/initramfs-firmware-temp/lib/firmware"
443         cd "${TEMP}/initramfs-firmware-temp"
444         if [ -n "${FIRMWARE_FILES}" ]
445         then
446                 OLD_IFS=$IFS
447                 IFS=","
448                 for i in ${FIRMWARE_FILES}
449                 do
450                         cp -L "${i}" ${TEMP}/initramfs-firmware-temp/lib/firmware/
451                 done
452                 IFS=$OLD_IFS
453         else
454                 cp -a "${FIRMWARE_DIR}"/* ${TEMP}/initramfs-firmware-temp/lib/firmware/
455         fi
456         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
457                 || gen_die "appending firmware to cpio"
458         cd "${TEMP}"
459         rm -r "${TEMP}/initramfs-firmware-temp/"
460 }
461
462 append_gpg() {
463         if [ -d "${TEMP}/initramfs-gpg-temp" ]
464         then
465                 rm -r "${TEMP}/initramfs-gpg-temp"
466         fi
467         cd ${TEMP}
468         mkdir -p "${TEMP}/initramfs-gpg-temp/sbin/"
469         if [ ! -e ${GPG_BINCACHE} ] ; then
470                 print_info 1 '          GPG: Adding support (compiling binaries)...'
471                 compile_gpg
472         fi
473         bzip2 -dc "${GPG_BINCACHE}" > "${TEMP}/initramfs-gpg-temp/sbin/gpg" ||
474                 gen_die 'Could not extract gpg binary cache!'
475         chmod a+x "${TEMP}/initramfs-gpg-temp/sbin/gpg"
476         cd "${TEMP}/initramfs-gpg-temp/"
477         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
478         rm -rf "${TEMP}/initramfs-gpg-temp" > /dev/null
479 }
480
481 print_list()
482 {
483         local x
484         for x in ${*}
485         do
486                 echo ${x}
487         done
488 }
489
490 append_modules() {
491         local group
492         local group_modules
493         local MOD_EXT=".ko"
494
495         print_info 2 "initramfs: >> Searching for modules..."
496         if [ "${INSTALL_MOD_PATH}" != '' ]
497         then
498           cd ${INSTALL_MOD_PATH}
499         else
500           cd /
501         fi
502
503         if [ -d "${TEMP}/initramfs-modules-${KV}-temp" ]
504         then
505                 rm -r "${TEMP}/initramfs-modules-${KV}-temp/"
506         fi
507         mkdir -p "${TEMP}/initramfs-modules-${KV}-temp/lib/modules/${KV}"
508         for i in `gen_dep_list`
509         do
510                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" 2>/dev/null| head -n 1 `
511                 if [ -z "${mymod}" ]
512                 then
513                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
514                         continue;
515                 fi
516
517                 print_info 2 "initramfs: >> Copying ${i}${MOD_EXT}..."
518                 cp -ax --parents "${mymod}" "${TEMP}/initramfs-modules-${KV}-temp"
519         done
520
521         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initramfs-modules-${KV}-temp 2>/dev/null
522
523         mkdir -p "${TEMP}/initramfs-modules-${KV}-temp/etc/modules"
524         for group_modules in ${!MODULES_*}; do
525                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
526                 print_list ${!group_modules} > "${TEMP}/initramfs-modules-${KV}-temp/etc/modules/${group}"
527         done
528         cd "${TEMP}/initramfs-modules-${KV}-temp/"
529         find . | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
530                         || gen_die "compressing modules cpio"
531         cd "${TEMP}"
532         rm -r "${TEMP}/initramfs-modules-${KV}-temp/"   
533 }
534
535 # check for static linked file with objdump
536 is_static() {
537         LANG="C" LC_ALL="C" objdump -T $1 2>&1 | grep "not a dynamic object" > /dev/null
538         return $?
539 }
540
541 append_auxilary() {
542         if [ -d "${TEMP}/initramfs-aux-temp" ]
543         then
544                 rm -r "${TEMP}/initramfs-aux-temp/"
545         fi
546         mkdir -p "${TEMP}/initramfs-aux-temp/etc"
547         mkdir -p "${TEMP}/initramfs-aux-temp/sbin"
548         if [ -f "${CMD_LINUXRC}" ]
549         then
550                 cp "${CMD_LINUXRC}" "${TEMP}/initramfs-aux-temp/init"
551                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC} to init"
552         else
553                 if isTrue ${NETBOOT}
554                 then
555                         cp "${GK_SHARE}/netboot/linuxrc.x" "${TEMP}/initramfs-aux-temp/init"
556                 else
557                         if [ -f "${GK_SHARE}/arch/${ARCH}/linuxrc" ]
558                         then
559                                 cp "${GK_SHARE}/arch/${ARCH}/linuxrc" "${TEMP}/initramfs-aux-temp/init"
560                         else
561                                 cp "${GK_SHARE}/defaults/linuxrc" "${TEMP}/initramfs-aux-temp/init"
562                         fi
563                 fi
564         fi
565
566         # Make sure it's executable
567         chmod 0755 "${TEMP}/initramfs-aux-temp/init"
568
569         # Make a symlink to init .. incase we are bundled inside the kernel as one
570         # big cpio.
571         cd ${TEMP}/initramfs-aux-temp
572         ln -s init linuxrc
573 #       ln ${TEMP}/initramfs-aux-temp/init ${TEMP}/initramfs-aux-temp/linuxrc
574
575         if [ -f "${GK_SHARE}/arch/${ARCH}/initrd.scripts" ]
576         then
577                 cp "${GK_SHARE}/arch/${ARCH}/initrd.scripts" "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
578         else
579                 cp "${GK_SHARE}/defaults/initrd.scripts" "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
580         fi
581
582         if [ -f "${GK_SHARE}/arch/${ARCH}/initrd.defaults" ]
583         then
584                 cp "${GK_SHARE}/arch/${ARCH}/initrd.defaults" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
585         else
586                 cp "${GK_SHARE}/defaults/initrd.defaults" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
587         fi
588
589         if [ -n "${REAL_ROOT}" ]
590         then
591                 sed -i "s:^REAL_ROOT=.*$:REAL_ROOT='${REAL_ROOT}':" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
592         fi
593
594         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
595         for group_modules in ${!MODULES_*}; do
596                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
597                 echo -n "${group} " >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
598         done
599         echo '"' >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
600
601         if [ -f "${GK_SHARE}/arch/${ARCH}/modprobe" ]
602         then
603                 cp "${GK_SHARE}/arch/${ARCH}/modprobe" "${TEMP}/initramfs-aux-temp/sbin/modprobe"
604         else
605                 cp "${GK_SHARE}/defaults/modprobe" "${TEMP}/initramfs-aux-temp/sbin/modprobe"
606         fi
607         if isTrue $CMD_DOKEYMAPAUTO
608         then
609                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initramfs-aux-temp/etc/initrd.defaults
610         fi
611         if isTrue $CMD_KEYMAP
612         then
613                 print_info 1 "        >> Copying keymaps"
614                 mkdir -p "${TEMP}/initramfs-aux-temp/lib/"
615                 cp -R "${GK_SHARE}/defaults/keymaps" "${TEMP}/initramfs-aux-temp/lib/" \
616                                 || gen_die "Error while copying keymaps"
617         fi
618
619         cd ${TEMP}/initramfs-aux-temp/sbin && ln -s ../init init
620         cd ${TEMP}
621         chmod +x "${TEMP}/initramfs-aux-temp/init"
622         chmod +x "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
623         chmod +x "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
624         chmod +x "${TEMP}/initramfs-aux-temp/sbin/modprobe"
625
626         if isTrue ${NETBOOT}
627         then
628                 cd "${GK_SHARE}/netboot/misc"
629                 cp -pPRf * "${TEMP}/initramfs-aux-temp/"
630         fi
631
632         cd "${TEMP}/initramfs-aux-temp/"
633         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
634                         || gen_die "compressing auxilary cpio"
635         cd "${TEMP}"
636         rm -r "${TEMP}/initramfs-aux-temp/"
637 }
638
639 append_data() {
640         local name=$1 var=$2
641         local func="append_${name}"
642
643         [ $# -eq 0 ] && gen_die "append_data() called with zero arguments"
644         if [ $# -eq 1 ] || isTrue ${var}
645         then
646             print_info 1 "        >> Appending ${name} cpio data..."
647             ${func} || gen_die "${func}() failed"
648         fi
649 }
650
651 create_initramfs() {
652         local compress_ext=""
653         print_info 1 "initramfs: >> Initializing..."
654
655         # Create empty cpio
656         CPIO="${TMPDIR}/initramfs-${KV}"
657         echo | cpio ${CPIO_ARGS} -F "${CPIO}" 2>/dev/null \
658                 || gen_die "Could not create empty cpio at ${CPIO}"
659
660         append_data 'base_layout'
661         append_data 'auxilary' "${BUSYBOX}"
662         append_data 'busybox' "${BUSYBOX}"
663         append_data 'lvm' "${LVM}"
664         append_data 'dmraid' "${DMRAID}"
665         append_data 'iscsi' "${ISCSI}"
666         append_data 'mdadm' "${MDADM}"
667         append_data 'luks' "${LUKS}"
668         append_data 'multipath' "${MULTIPATH}"
669         append_data 'gpg' "${GPG}"
670
671         if [ "${RAMDISKMODULES}" = '1' ]
672         then
673                 append_data 'modules'
674         else
675                 print_info 1 "initramfs: Not copying modules..."
676         fi
677
678         append_data 'zfs' "${ZFS}"
679
680         append_data 'blkid' "${DISKLABEL}"
681
682         append_data 'unionfs_fuse' "${UNIONFS}"
683
684         append_data 'splash' "${SPLASH}"
685
686         if isTrue "${FIRMWARE}" && [ -n "${FIRMWARE_DIR}" ]
687         then
688                 append_data 'firmware'
689         fi
690
691         # This should always be appended last
692         if [ "${INITRAMFS_OVERLAY}" != '' ]
693         then
694                 append_data 'overlay'
695         fi
696
697         if isTrue "${INTEGRATED_INITRAMFS}"
698         then
699                 # Explicitly do not compress if we are integrating into the kernel.
700                 # The kernel will do a better job of it than us.
701                 mv ${TMPDIR}/initramfs-${KV} ${TMPDIR}/initramfs-${KV}.cpio
702                 sed -i '/^.*CONFIG_INITRAMFS_SOURCE=.*$/d' ${KERNEL_DIR}/.config
703                 cat >>${KERNEL_DIR}/.config     <<-EOF
704                 CONFIG_INITRAMFS_SOURCE="${TMPDIR}/initramfs-${KV}.cpio${compress_ext}"
705                 CONFIG_INITRAMFS_ROOT_UID=0
706                 CONFIG_INITRAMFS_ROOT_GID=0
707                 EOF
708         else
709                 if isTrue "${COMPRESS_INITRD}"
710                 then
711                         cmd_xz=$(type -p xz)
712                         cmd_lzma=$(type -p lzma)
713                         cmd_bzip2=$(type -p bzip2)
714                         cmd_gzip=$(type -p gzip)
715                         cmd_lzop=$(type -p lzop)
716                         pkg_xz='app-arch/xz-utils'
717                         pkg_lzma='app-arch/xz-utils'
718                         pkg_bzip2='app-arch/bzip2'
719                         pkg_gzip='app-arch/gzip'
720                         pkg_lzop='app-arch/lzop'
721                         local compression
722                         case ${COMPRESS_INITRD_TYPE} in
723                                 xz|lzma|bzip2|gzip|lzop) compression=${COMPRESS_INITRD_TYPE} ;;
724                                 lzo) compression=lzop ;;
725                                 best|fastest)
726                                         for tuple in \
727                                                         'CONFIG_RD_XZ    cmd_xz    xz' \
728                                                         'CONFIG_RD_LZMA  cmd_lzma  lzma' \
729                                                         'CONFIG_RD_BZIP2 cmd_bzip2 bzip' \
730                                                         'CONFIG_RD_GZIP  cmd_gzip  gzip' \
731                                                         'CONFIG_RD_LZO   cmd_lzop  lzop'; do
732                                                 set -- ${tuple}
733                                                 kernel_option=$1
734                                                 cmd_variable_name=$2
735                                                 if grep -sq "^${kernel_option}=y" ${KERNEL_DIR}/.config && test -n "${!cmd_variable_name}" ; then
736                                                         compression=$3
737                                                         [[ ${COMPRESS_INITRD_TYPE} == best ]] && break
738                                                 fi
739                                         done
740                                         ;;
741                                 *)
742                                         gen_die "Compression '${COMPRESS_INITRD_TYPE}' unknown"
743                                         ;;
744                         esac
745
746                         # Check for actual availability
747                         cmd_variable_name=cmd_${compression}
748                         pkg_variable_name=pkg_${compression}
749                         [[ -z "${!cmd_variable_name}" ]] && gen_die "Compression '${compression}' is not available. Please install package '${!pkg_variable_name}'."
750
751                         case $compression in
752                                 xz) compress_ext='.xz' compress_cmd="${cmd_xz} -e --check=none -z -f -9" ;;
753                                 lzma) compress_ext='.lzma' compress_cmd="${cmd_lzma} -z -f -9" ;;
754                                 bzip2) compress_ext='.bz2' compress_cmd="${cmd_bzip2} -z -f -9" ;;
755                                 gzip) compress_ext='.gz' compress_cmd="${cmd_gzip} -f -9" ;;
756                                 lzop) compress_ext='.lzo' compress_cmd="${cmd_lzop} -f -9" ;;
757                         esac
758         
759                         if [ -n "${compression}" ]; then
760                                 print_info 1 "        >> Compressing cpio data (${compress_ext})..."
761                                 ${compress_cmd} "${CPIO}" || gen_die "Compression (${compress_cmd}) failed"
762                                 mv -f "${CPIO}${compress_ext}" "${CPIO}" || gen_die "Rename failed"
763                         else
764                                 print_info 1 "        >> Not compressing cpio data ..."
765                         fi
766                 fi
767         fi
768
769         if isTrue "${CMD_INSTALL}"
770         then
771                 if ! isTrue "${INTEGRATED_INITRAMFS}"
772                 then
773                         copy_image_with_preserve "initramfs" \
774                                 "${TMPDIR}/initramfs-${KV}" \
775                                 "initramfs-${KNAME}-${ARCH}-${KV}"
776                 fi
777         fi
778 }