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