Support module options set in /etc/module.d, amend cmdline options patch
[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 append_modprobed() {
581         local TDIR="${TEMP}/initramfs-modprobe.d-temp"
582         if [ -d "${TDIR}" ]
583         then
584                 rm -r "${TDIR}"
585         fi
586
587         mkdir -p "${TDIR}/etc/module_options/"
588
589         # Load module parameters
590         for dir in $(find "${MODPROBEDIR}"/*)
591         do
592                 while read x
593                 do
594                         case "${x}" in
595                                 options*)
596                                         module_name="$(echo "$x" | cut -d ' ' -f 2)"
597                                         [ "${module_name}" != "$(echo)" ] || continue
598                                         module_options="$(echo "$x" | cut -d ' ' -f 3-)"
599                                         [ "${module_options}" != "$(echo)" ] || continue
600                                         echo "${module_options}" >> "${TDIR}/etc/module_options/${module_name}.conf"
601                                 ;;
602                         esac
603                 done < "${dir}"
604         done
605
606         cd "${TDIR}"
607         log_future_cpio_content
608         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
609                         || gen_die "compressing modprobe.d cpio"
610
611         cd "${TEMP}"
612         rm -rf "${TDIR}" > /dev/null
613 }
614
615 # check for static linked file with objdump
616 is_static() {
617         LANG="C" LC_ALL="C" objdump -T $1 2>&1 | grep "not a dynamic object" > /dev/null
618         return $?
619 }
620
621 append_auxilary() {
622         if [ -d "${TEMP}/initramfs-aux-temp" ]
623         then
624                 rm -r "${TEMP}/initramfs-aux-temp/"
625         fi
626         mkdir -p "${TEMP}/initramfs-aux-temp/etc"
627         mkdir -p "${TEMP}/initramfs-aux-temp/sbin"
628         if [ -f "${CMD_LINUXRC}" ]
629         then
630                 cp "${CMD_LINUXRC}" "${TEMP}/initramfs-aux-temp/init"
631                 print_info 2 "        >> Copying user specified linuxrc: ${CMD_LINUXRC} to init"
632         else
633                 if isTrue ${NETBOOT}
634                 then
635                         cp "${GK_SHARE}/netboot/linuxrc.x" "${TEMP}/initramfs-aux-temp/init"
636                 else
637                         if [ -f "${GK_SHARE}/arch/${ARCH}/linuxrc" ]
638                         then
639                                 cp "${GK_SHARE}/arch/${ARCH}/linuxrc" "${TEMP}/initramfs-aux-temp/init"
640                         else
641                                 cp "${GK_SHARE}/defaults/linuxrc" "${TEMP}/initramfs-aux-temp/init"
642                         fi
643                 fi
644         fi
645
646         # Make sure it's executable
647         chmod 0755 "${TEMP}/initramfs-aux-temp/init"
648
649         # Make a symlink to init .. incase we are bundled inside the kernel as one
650         # big cpio.
651         cd ${TEMP}/initramfs-aux-temp
652         ln -s init linuxrc
653 #       ln ${TEMP}/initramfs-aux-temp/init ${TEMP}/initramfs-aux-temp/linuxrc
654
655         if [ -f "${GK_SHARE}/arch/${ARCH}/initrd.scripts" ]
656         then
657                 cp "${GK_SHARE}/arch/${ARCH}/initrd.scripts" "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
658         else
659                 cp "${GK_SHARE}/defaults/initrd.scripts" "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
660         fi
661
662         if [ -f "${GK_SHARE}/arch/${ARCH}/initrd.defaults" ]
663         then
664                 cp "${GK_SHARE}/arch/${ARCH}/initrd.defaults" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
665         else
666                 cp "${GK_SHARE}/defaults/initrd.defaults" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
667         fi
668
669         if [ -n "${REAL_ROOT}" ]
670         then
671                 sed -i "s:^REAL_ROOT=.*$:REAL_ROOT='${REAL_ROOT}':" "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
672         fi
673
674         echo -n 'HWOPTS="$HWOPTS ' >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
675         for group_modules in ${!MODULES_*}; do
676                 group="$(echo $group_modules | cut -d_ -f2 | tr "[:upper:]" "[:lower:]")"
677                 echo -n "${group} " >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
678         done
679         echo '"' >> "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
680
681         if [ -f "${GK_SHARE}/arch/${ARCH}/modprobe" ]
682         then
683                 cp "${GK_SHARE}/arch/${ARCH}/modprobe" "${TEMP}/initramfs-aux-temp/sbin/modprobe"
684         else
685                 cp "${GK_SHARE}/defaults/modprobe" "${TEMP}/initramfs-aux-temp/sbin/modprobe"
686         fi
687         if isTrue $CMD_DOKEYMAPAUTO
688         then
689                 echo 'MY_HWOPTS="${MY_HWOPTS} keymap"' >> ${TEMP}/initramfs-aux-temp/etc/initrd.defaults
690         fi
691         if isTrue $CMD_KEYMAP
692         then
693                 print_info 1 "        >> Copying keymaps"
694                 mkdir -p "${TEMP}/initramfs-aux-temp/lib/"
695                 cp -R "${GK_SHARE}/defaults/keymaps" "${TEMP}/initramfs-aux-temp/lib/" \
696                                 || gen_die "Error while copying keymaps"
697         fi
698
699         cd ${TEMP}/initramfs-aux-temp/sbin && ln -s ../init init
700         cd ${TEMP}
701         chmod +x "${TEMP}/initramfs-aux-temp/init"
702         chmod +x "${TEMP}/initramfs-aux-temp/etc/initrd.scripts"
703         chmod +x "${TEMP}/initramfs-aux-temp/etc/initrd.defaults"
704         chmod +x "${TEMP}/initramfs-aux-temp/sbin/modprobe"
705
706         if isTrue ${NETBOOT}
707         then
708                 cd "${GK_SHARE}/netboot/misc"
709                 cp -pPRf * "${TEMP}/initramfs-aux-temp/"
710         fi
711
712         cd "${TEMP}/initramfs-aux-temp/"
713         log_future_cpio_content
714         find . -print | cpio ${CPIO_ARGS} --append -F "${CPIO}" \
715                         || gen_die "compressing auxilary cpio"
716         cd "${TEMP}"
717         rm -r "${TEMP}/initramfs-aux-temp/"
718 }
719
720 append_data() {
721         local name=$1 var=$2
722         local func="append_${name}"
723
724         [ $# -eq 0 ] && gen_die "append_data() called with zero arguments"
725         if [ $# -eq 1 ] || isTrue ${var}
726         then
727             print_info 1 "        >> Appending ${name} cpio data..."
728             ${func} || gen_die "${func}() failed"
729         fi
730 }
731
732 create_initramfs() {
733         local compress_ext=""
734         print_info 1 "initramfs: >> Initializing..."
735
736         # Create empty cpio
737         CPIO="${TMPDIR}/initramfs-${KV}"
738         echo | cpio ${CPIO_ARGS} -F "${CPIO}" 2>/dev/null \
739                 || gen_die "Could not create empty cpio at ${CPIO}"
740
741         append_data 'base_layout'
742         append_data 'auxilary' "${BUSYBOX}"
743         append_data 'busybox' "${BUSYBOX}"
744         append_data 'lvm' "${LVM}"
745         append_data 'dmraid' "${DMRAID}"
746         append_data 'iscsi' "${ISCSI}"
747         append_data 'mdadm' "${MDADM}"
748         append_data 'luks' "${LUKS}"
749         append_data 'multipath' "${MULTIPATH}"
750         append_data 'gpg' "${GPG}"
751
752         if [ "${RAMDISKMODULES}" = '1' ]
753         then
754                 append_data 'modules'
755         else
756                 print_info 1 "initramfs: Not copying modules..."
757         fi
758
759         append_data 'zfs' "${ZFS}"
760
761         append_data 'blkid' "${DISKLABEL}"
762
763         append_data 'unionfs_fuse' "${UNIONFS}"
764
765         append_data 'splash' "${SPLASH}"
766
767         append_data 'modprobed'
768
769         if isTrue "${FIRMWARE}" && [ -n "${FIRMWARE_DIR}" ]
770         then
771                 append_data 'firmware'
772         fi
773
774         # This should always be appended last
775         if [ "${INITRAMFS_OVERLAY}" != '' ]
776         then
777                 append_data 'overlay'
778         fi
779
780         if isTrue "${INTEGRATED_INITRAMFS}"
781         then
782                 # Explicitly do not compress if we are integrating into the kernel.
783                 # The kernel will do a better job of it than us.
784                 mv ${TMPDIR}/initramfs-${KV} ${TMPDIR}/initramfs-${KV}.cpio
785                 sed -i '/^.*CONFIG_INITRAMFS_SOURCE=.*$/d' ${KERNEL_DIR}/.config
786                 cat >>${KERNEL_DIR}/.config     <<-EOF
787                 CONFIG_INITRAMFS_SOURCE="${TMPDIR}/initramfs-${KV}.cpio${compress_ext}"
788                 CONFIG_INITRAMFS_ROOT_UID=0
789                 CONFIG_INITRAMFS_ROOT_GID=0
790                 EOF
791         else
792                 if isTrue "${COMPRESS_INITRD}"
793                 then
794                         if [[ "$(file --brief --mime-type "${KERNEL_CONFIG}")" == application/x-gzip ]]; then
795                                 # Support --kernel-config=/proc/config.gz, mainly
796                                 local CONFGREP=zgrep
797                         else
798                                 local CONFGREP=grep
799                         fi
800
801                         cmd_xz=$(type -p xz)
802                         cmd_lzma=$(type -p lzma)
803                         cmd_bzip2=$(type -p bzip2)
804                         cmd_gzip=$(type -p gzip)
805                         cmd_lzop=$(type -p lzop)
806                         pkg_xz='app-arch/xz-utils'
807                         pkg_lzma='app-arch/xz-utils'
808                         pkg_bzip2='app-arch/bzip2'
809                         pkg_gzip='app-arch/gzip'
810                         pkg_lzop='app-arch/lzop'
811                         local compression
812                         case ${COMPRESS_INITRD_TYPE} in
813                                 xz|lzma|bzip2|gzip|lzop) compression=${COMPRESS_INITRD_TYPE} ;;
814                                 lzo) compression=lzop ;;
815                                 best|fastest)
816                                         for tuple in \
817                                                         'CONFIG_RD_XZ    cmd_xz    xz' \
818                                                         'CONFIG_RD_LZMA  cmd_lzma  lzma' \
819                                                         'CONFIG_RD_BZIP2 cmd_bzip2 bzip2' \
820                                                         'CONFIG_RD_GZIP  cmd_gzip  gzip' \
821                                                         'CONFIG_RD_LZO   cmd_lzop  lzop'; do
822                                                 set -- ${tuple}
823                                                 kernel_option=$1
824                                                 cmd_variable_name=$2
825                                                 if ${CONFGREP} -q "^${kernel_option}=y" "${KERNEL_CONFIG}" && test -n "${!cmd_variable_name}" ; then
826                                                         compression=$3
827                                                         [[ ${COMPRESS_INITRD_TYPE} == best ]] && break
828                                                 fi
829                                         done
830                                         [[ -z "${compression}" ]] && gen_die "None of the initramfs we tried are supported by your kernel (config file \"${KERNEL_CONFIG}\"), strange!?"
831                                         ;;
832                                 *)
833                                         gen_die "Compression '${COMPRESS_INITRD_TYPE}' unknown"
834                                         ;;
835                         esac
836
837                         # Check for actual availability
838                         cmd_variable_name=cmd_${compression}
839                         pkg_variable_name=pkg_${compression}
840                         [[ -z "${!cmd_variable_name}" ]] && gen_die "Compression '${compression}' is not available. Please install package '${!pkg_variable_name}'."
841
842                         case $compression in
843                                 xz) compress_ext='.xz' compress_cmd="${cmd_xz} -e --check=none -z -f -9" ;;
844                                 lzma) compress_ext='.lzma' compress_cmd="${cmd_lzma} -z -f -9" ;;
845                                 bzip2) compress_ext='.bz2' compress_cmd="${cmd_bzip2} -z -f -9" ;;
846                                 gzip) compress_ext='.gz' compress_cmd="${cmd_gzip} -f -9" ;;
847                                 lzop) compress_ext='.lzo' compress_cmd="${cmd_lzop} -f -9" ;;
848                         esac
849         
850                         if [ -n "${compression}" ]; then
851                                 print_info 1 "        >> Compressing cpio data (${compress_ext})..."
852                                 ${compress_cmd} "${CPIO}" || gen_die "Compression (${compress_cmd}) failed"
853                                 mv -f "${CPIO}${compress_ext}" "${CPIO}" || gen_die "Rename failed"
854                         else
855                                 print_info 1 "        >> Not compressing cpio data ..."
856                         fi
857                 fi
858         fi
859
860         if isTrue "${CMD_INSTALL}"
861         then
862                 if ! isTrue "${INTEGRATED_INITRAMFS}"
863                 then
864                         copy_image_with_preserve "initramfs" \
865                                 "${TMPDIR}/initramfs-${KV}" \
866                                 "initramfs-${KNAME}-${ARCH}-${KV}"
867                 fi
868         fi
869 }