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