3dd757bf10291a9cc2e9ec3d9e31bfbbaf18338f
[genkernel.git] / gen_initramfs.sh
1 #!/bin/bash
2 # $Id$
3
4 CPIO_ARGS="--quiet -o -H newc"
5
6 # The copy_binaries function is explicitly released into the public domain to
7 # encourage wide adoption and re-use.  That means:
8 # - You may use the code of copy_binaries() as Public domain outside of genkernel
9 # - Past and future changes to copy_binaries() still need to be licensable under
10 #   <GPL v2 or later> to maintain license compatibility with genkernel itself
11 # Written by: 
12 # - Robin H. Johnson <robbat2@gentoo.org> (complete rewrite)
13 # - Richard Yao <ryao@cs.stonybrook.edu> (original concept)
14 # Usage:
15 # copy_binaries DESTDIR BINARIES...
16 copy_binaries() {
17         local destdir=$1
18         shift
19         lddtree "$@" \
20                 | tr ')(' '\n' \
21                 | awk  '/=>/{ if($3 ~ /^\//){print $3}}' \
22                 | sort \
23                 | uniq \
24                 | cpio -p --make-directories --dereference --quiet $destdir
25
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,{s,}bin/{lvm,dmsetup}}
279         fi
280         if [ -x /sbin/lvm -o -x /bin/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                 is_static "${_luks_source}" \
423                                 || gen_die "$(printf "${_luks_error_format}" "${_luks_source} not a static binary")"
424
425                 print_info 1 "Including LUKS support"
426                 cp "${_luks_source}" ${TEMP}/initramfs-luks-temp${_luks_dest}
427                 chmod +x "${TEMP}/initramfs-luks-temp${_luks_dest}"
428         fi
429
430         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
431                 || gen_die "appending cryptsetup to cpio"
432
433         cd "${TEMP}"
434         rm -r "${TEMP}/initramfs-luks-temp/"
435 }
436
437 append_firmware() {
438         if [ -z "${FIRMWARE_FILES}" -a ! -d "${FIRMWARE_DIR}" ]
439         then
440                 gen_die "specified firmware directory (${FIRMWARE_DIR}) does not exist"
441         fi
442         if [ -d "${TEMP}/initramfs-firmware-temp" ]
443         then
444                 rm -r "${TEMP}/initramfs-firmware-temp/"
445         fi
446         mkdir -p "${TEMP}/initramfs-firmware-temp/lib/firmware"
447         cd "${TEMP}/initramfs-firmware-temp"
448         if [ -n "${FIRMWARE_FILES}" ]
449         then
450                 OLD_IFS=$IFS
451                 IFS=","
452                 for i in ${FIRMWARE_FILES}
453                 do
454                         cp -L "${i}" ${TEMP}/initramfs-firmware-temp/lib/firmware/
455                 done
456                 IFS=$OLD_IFS
457         else
458                 cp -a "${FIRMWARE_DIR}"/* ${TEMP}/initramfs-firmware-temp/lib/firmware/
459         fi
460         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
461                 || gen_die "appending firmware to cpio"
462         cd "${TEMP}"
463         rm -r "${TEMP}/initramfs-firmware-temp/"
464 }
465
466 append_gpg() {
467         if [ -d "${TEMP}/initramfs-gpg-temp" ]
468         then
469                 rm -r "${TEMP}/initramfs-gpg-temp"
470         fi
471         cd ${TEMP}
472         mkdir -p "${TEMP}/initramfs-gpg-temp/sbin/"
473         if [ ! -e ${GPG_BINCACHE} ] ; then
474                 print_info 1 '          GPG: Adding support (compiling binaries)...'
475                 compile_gpg
476         fi
477         bzip2 -dc "${GPG_BINCACHE}" > "${TEMP}/initramfs-gpg-temp/sbin/gpg" ||
478                 gen_die 'Could not extract gpg binary cache!'
479         chmod a+x "${TEMP}/initramfs-gpg-temp/sbin/gpg"
480         cd "${TEMP}/initramfs-gpg-temp/"
481         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}"
482         rm -rf "${TEMP}/initramfs-gpg-temp" > /dev/null
483 }
484
485 print_list()
486 {
487         local x
488         for x in ${*}
489         do
490                 echo ${x}
491         done
492 }
493
494 append_modules() {
495         local group
496         local group_modules
497         local MOD_EXT=".ko"
498
499         print_info 2 "initramfs: >> Searching for modules..."
500         if [ "${INSTALL_MOD_PATH}" != '' ]
501         then
502           cd ${INSTALL_MOD_PATH}
503         else
504           cd /
505         fi
506
507         if [ -d "${TEMP}/initramfs-modules-${KV}-temp" ]
508         then
509                 rm -r "${TEMP}/initramfs-modules-${KV}-temp/"
510         fi
511         mkdir -p "${TEMP}/initramfs-modules-${KV}-temp/lib/modules/${KV}"
512         for i in `gen_dep_list`
513         do
514                 mymod=`find ./lib/modules/${KV} -name "${i}${MOD_EXT}" 2>/dev/null| head -n 1 `
515                 if [ -z "${mymod}" ]
516                 then
517                         print_warning 2 "Warning :: ${i}${MOD_EXT} not found; skipping..."
518                         continue;
519                 fi
520
521                 print_info 2 "initramfs: >> Copying ${i}${MOD_EXT}..."
522                 cp -ax --parents "${mymod}" "${TEMP}/initramfs-modules-${KV}-temp"
523         done
524
525         cp -ax --parents ./lib/modules/${KV}/modules* ${TEMP}/initramfs-modules-${KV}-temp 2>/dev/null
526
527         mkdir -p "${TEMP}/initramfs-modules-${KV}-temp/etc/modules"
528         for group_modules in ${!MODULES_*}; do
529                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
530                 print_list ${!group_modules} > "${TEMP}/initramfs-modules-${KV}-temp/etc/modules/${group}"
531         done
532         cd "${TEMP}/initramfs-modules-${KV}-temp/"
533         find . | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
534                         || gen_die "compressing modules cpio"
535         cd "${TEMP}"
536         rm -r "${TEMP}/initramfs-modules-${KV}-temp/"   
537 }
538
539 # check for static linked file with objdump
540 is_static() {
541         LANG="C" LC_ALL="C" objdump -T $1 2>&1 | grep "not a dynamic object" > /dev/null
542         return $?
543 }
544
545 append_auxilary() {
546         if [ -d "${TEMP}/initramfs-aux-temp" ]
547         then
548                 rm -r "${TEMP}/initramfs-aux-temp/"
549         fi
550         mkdir -p "${TEMP}/initramfs-aux-temp/etc"
551         mkdir -p "${TEMP}/initramfs-aux-temp/sbin"
552         if [ -f "${CMD_LINUXRC}" ]
553         then
554                 cp "${CMD_LINUXRC}" "${TEMP}/initramfs-aux-temp/init"
555                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC} to init"
556         else
557                 if isTrue ${NETBOOT}
558                 then
559                         cp "${GK_SHARE}/netboot/linuxrc.x" "${TEMP}/initramfs-aux-temp/init"
560                 else
561                         if [ -f "${GK_SHARE}/arch/${ARCH}/linuxrc" ]
562                         then
563                                 cp "${GK_SHARE}/arch/${ARCH}/linuxrc" "${TEMP}/initramfs-aux-temp/init"
564                         else
565                                 cp "${GK_SHARE}/defaults/linuxrc" "${TEMP}/initramfs-aux-temp/init"
566                         fi
567                 fi
568         fi
569
570         # Make sure it's executable
571         chmod 0755 "${TEMP}/initramfs-aux-temp/init"
572
573         # Make a symlink to init .. incase we are bundled inside the kernel as one
574         # big cpio.
575         cd ${TEMP}/initramfs-aux-temp
576         ln -s init linuxrc
577 #       ln ${TEMP}/initramfs-aux-temp/init ${TEMP}/initramfs-aux-temp/linuxrc
578
579         if [ -f "${GK_SHARE}/arch/${ARCH}/initrd.scripts" ]
580         then
581                 cp "${GK_SHARE}/arch/${ARCH}/initrd.scripts" "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
582         else
583                 cp "${GK_SHARE}/defaults/initrd.scripts" "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
584         fi
585
586         if [ -f "${GK_SHARE}/arch/${ARCH}/initrd.defaults" ]
587         then
588                 cp "${GK_SHARE}/arch/${ARCH}/initrd.defaults" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
589         else
590                 cp "${GK_SHARE}/defaults/initrd.defaults" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
591         fi
592
593         if [ -n "${REAL_ROOT}" ]
594         then
595                 sed -i "s:^REAL_ROOT=.*$:REAL_ROOT='${REAL_ROOT}':" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
596         fi
597
598         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
599         for group_modules in ${!MODULES_*}; do
600                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
601                 echo -n "${group} " >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
602         done
603         echo '"' >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
604
605         if [ -f "${GK_SHARE}/arch/${ARCH}/modprobe" ]
606         then
607                 cp "${GK_SHARE}/arch/${ARCH}/modprobe" "${TEMP}/initramfs-aux-temp/sbin/modprobe"
608         else
609                 cp "${GK_SHARE}/defaults/modprobe" "${TEMP}/initramfs-aux-temp/sbin/modprobe"
610         fi
611         if isTrue $CMD_DOKEYMAPAUTO
612         then
613                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initramfs-aux-temp/etc/initrd.defaults
614         fi
615         if isTrue $CMD_KEYMAP
616         then
617                 print_info 1 "        >> Copying keymaps"
618                 mkdir -p "${TEMP}/initramfs-aux-temp/lib/"
619                 cp -R "${GK_SHARE}/defaults/keymaps" "${TEMP}/initramfs-aux-temp/lib/" \
620                                 || gen_die "Error while copying keymaps"
621         fi
622
623         cd ${TEMP}/initramfs-aux-temp/sbin && ln -s ../init init
624         cd ${TEMP}
625         chmod +x "${TEMP}/initramfs-aux-temp/init"
626         chmod +x "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
627         chmod +x "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
628         chmod +x "${TEMP}/initramfs-aux-temp/sbin/modprobe"
629
630         if isTrue ${NETBOOT}
631         then
632                 cd "${GK_SHARE}/netboot/misc"
633                 cp -pPRf * "${TEMP}/initramfs-aux-temp/"
634         fi
635
636         cd "${TEMP}/initramfs-aux-temp/"
637         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
638                         || gen_die "compressing auxilary cpio"
639         cd "${TEMP}"
640         rm -r "${TEMP}/initramfs-aux-temp/"
641 }
642
643 append_data() {
644         local name=$1 var=$2
645         local func="append_${name}"
646
647         [ $# -eq 0 ] && gen_die "append_data() called with zero arguments"
648         if [ $# -eq 1 ] || isTrue ${var}
649         then
650             print_info 1 "        >> Appending ${name} cpio data..."
651             ${func} || gen_die "${func}() failed"
652         fi
653 }
654
655 create_initramfs() {
656         local compress_ext=""
657         print_info 1 "initramfs: >> Initializing..."
658
659         # Create empty cpio
660         CPIO="${TMPDIR}/initramfs-${KV}"
661         echo | cpio ${CPIO_ARGS} -F "${CPIO}" 2>/dev/null \
662                 || gen_die "Could not create empty cpio at ${CPIO}"
663
664         append_data 'base_layout'
665         append_data 'auxilary' "${BUSYBOX}"
666         append_data 'busybox' "${BUSYBOX}"
667         append_data 'lvm' "${LVM}"
668         append_data 'dmraid' "${DMRAID}"
669         append_data 'iscsi' "${ISCSI}"
670         append_data 'mdadm' "${MDADM}"
671         append_data 'luks' "${LUKS}"
672         append_data 'multipath' "${MULTIPATH}"
673         append_data 'gpg' "${GPG}"
674
675         if [ "${RAMDISKMODULES}" = '1' ]
676         then
677                 append_data 'modules'
678         else
679                 print_info 1 "initramfs: Not copying modules..."
680         fi
681
682         append_data 'zfs' "${ZFS}"
683
684         append_data 'blkid' "${DISKLABEL}"
685
686         append_data 'unionfs_fuse' "${UNIONFS}"
687
688         append_data 'splash' "${SPLASH}"
689
690         if isTrue "${FIRMWARE}" && [ -n "${FIRMWARE_DIR}" ]
691         then
692                 append_data 'firmware'
693         fi
694
695         # This should always be appended last
696         if [ "${INITRAMFS_OVERLAY}" != '' ]
697         then
698                 append_data 'overlay'
699         fi
700
701         if isTrue "${INTEGRATED_INITRAMFS}"
702         then
703                 # Explicitly do not compress if we are integrating into the kernel.
704                 # The kernel will do a better job of it than us.
705                 mv ${TMPDIR}/initramfs-${KV} ${TMPDIR}/initramfs-${KV}.cpio
706                 sed -i '/^.*CONFIG_INITRAMFS_SOURCE=.*$/d' ${KERNEL_DIR}/.config
707                 cat >>${KERNEL_DIR}/.config     <<-EOF
708                 CONFIG_INITRAMFS_SOURCE="${TMPDIR}/initramfs-${KV}.cpio${compress_ext}"
709                 CONFIG_INITRAMFS_ROOT_UID=0
710                 CONFIG_INITRAMFS_ROOT_GID=0
711                 EOF
712         else
713                 if isTrue "${COMPRESS_INITRD}"
714                 then
715                         cmd_xz=$(type -p xz)
716                         cmd_lzma=$(type -p lzma)
717                         cmd_bzip2=$(type -p bzip2)
718                         cmd_gzip=$(type -p gzip)
719                         cmd_lzop=$(type -p lzop)
720                         pkg_xz='app-arch/xz-utils'
721                         pkg_lzma='app-arch/xz-utils'
722                         pkg_bzip2='app-arch/bzip2'
723                         pkg_gzip='app-arch/gzip'
724                         pkg_lzop='app-arch/lzop'
725                         local compression
726                         case ${COMPRESS_INITRD_TYPE} in
727                                 xz|lzma|bzip2|gzip|lzop) compression=${COMPRESS_INITRD_TYPE} ;;
728                                 lzo) compression=lzop ;;
729                                 best|fastest)
730                                         for tuple in \
731                                                         'CONFIG_RD_XZ    cmd_xz    xz' \
732                                                         'CONFIG_RD_LZMA  cmd_lzma  lzma' \
733                                                         'CONFIG_RD_BZIP2 cmd_bzip2 bzip' \
734                                                         'CONFIG_RD_GZIP  cmd_gzip  gzip' \
735                                                         'CONFIG_RD_LZO   cmd_lzop  lzop'; do
736                                                 set -- ${tuple}
737                                                 kernel_option=$1
738                                                 cmd_variable_name=$2
739                                                 if grep -sq "^${kernel_option}=y" ${KERNEL_DIR}/.config && test -n "${!cmd_variable_name}" ; then
740                                                         compression=$3
741                                                         [[ ${COMPRESS_INITRD_TYPE} == best ]] && break
742                                                 fi
743                                         done
744                                         ;;
745                                 *)
746                                         gen_die "Compression '${COMPRESS_INITRD_TYPE}' unknown"
747                                         ;;
748                         esac
749
750                         # Check for actual availability
751                         cmd_variable_name=cmd_${compression}
752                         pkg_variable_name=pkg_${compression}
753                         [[ -z "${!cmd_variable_name}" ]] && gen_die "Compression '${compression}' is not available. Please install package '${!pkg_variable_name}'."
754
755                         case $compression in
756                                 xz) compress_ext='.xz' compress_cmd="${cmd_xz} -e --check=none -z -f -9" ;;
757                                 lzma) compress_ext='.lzma' compress_cmd="${cmd_lzma} -z -f -9" ;;
758                                 bzip2) compress_ext='.bz2' compress_cmd="${cmd_bzip2} -z -f -9" ;;
759                                 gzip) compress_ext='.gz' compress_cmd="${cmd_gzip} -f -9" ;;
760                                 lzop) compress_ext='.lzo' compress_cmd="${cmd_lzop} -f -9" ;;
761                         esac
762         
763                         if [ -n "${compression}" ]; then
764                                 print_info 1 "        >> Compressing cpio data (${compress_ext})..."
765                                 ${compress_cmd} "${CPIO}" || gen_die "Compression (${compress_cmd}) failed"
766                                 mv -f "${CPIO}${compress_ext}" "${CPIO}" || gen_die "Rename failed"
767                         else
768                                 print_info 1 "        >> Not compressing cpio data ..."
769                         fi
770                 fi
771         fi
772
773         if isTrue "${CMD_INSTALL}"
774         then
775                 if ! isTrue "${INTEGRATED_INITRAMFS}"
776                 then
777                         copy_image_with_preserve "initramfs" \
778                                 "${TMPDIR}/initramfs-${KV}" \
779                                 "initramfs-${KNAME}-${ARCH}-${KV}"
780                 fi
781         fi
782 }