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