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