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