]> git.pld-linux.org Git - projects/geninitrd.git/blob - geninitrd
found some explanation of numeric root= params (used by lilo)
[projects/geninitrd.git] / geninitrd
1 #!/bin/sh
2
3 # geninitrd
4 #
5 #       by PLD Linux Team
6 #
7 # based on mkinitrd from RedHat Linux
8 #
9
10 GENINITRD_RCSID='$Revision$ $Date::                            $'
11 R=${GENINITRD_RCSID#* *}; VERSION=${R%% *}
12 PROGRAM=${0##*/}
13
14 . /etc/rc.d/init.d/functions
15 . /lib/geninitrd/functions
16 . /etc/sysconfig/system
17
18 # list of geninitrd modules which need setup routine after commandline args parsing
19 GENINITRD_MODS=""
20 COMPRESS=yes
21 STRIP=/usr/bin/strip
22 target=""
23 kernel=""
24 force=""
25 verbose=""
26 MODULES=""
27 img_vers=""
28 fstab=/etc/fstab
29 modext=.o
30 rootdev_nr=0
31 # device node for rootfs from fstab
32 rootdev=""
33
34 # internal variables
35 # is /dev on tmpfs
36 dev_mounted=no
37 # is /proc mounted
38 proc_mounted=no
39 # is /sys mounted
40 sys_mounted=no
41 # is /tmp mounted on tmpfs
42 tmp_mounted=no
43
44 # are /dev nodes already created from /proc/devices info?
45 proc_partitions=no
46
47 usage() {
48         echo "Usage: $PROGRAM [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
49         echo "       [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
50         echo "       [--compress=yes|xz|lzma|bzip2|gzip|lzo]"
51         echo "       [--nostrip ] [--strip PATH/strip] [--strip=PATH/strip]"
52         echo "       [--initrdfs=rom|initramfs|ext2|cram] [--modules-conf=<modules.conf>]"
53         echo "       [--with-bootsplash] [--without-bootsplash]"
54         echo "       [--with-fbsplash] [--without-fbsplash]"
55         echo "       [--with-fbcondecor] [--without-fbcondecor]"
56         echo "       [--lvmtoolsversion=1|2] [--with-udev] [--without-udev]"
57         echo "       [--with-suspend] [--without-suspend]"
58         echo "       [--with-tuxonice] [--without-tuxonice]"
59         echo "       [--without-dmraid]"
60         echo "       [--with-multipath=DEVPATH] [--without-multipath]"
61         echo "       [--without-blkid] [--without-luks]"
62         echo "       <initrd-image> <kernel-version>"
63         echo ""
64         echo "Example:"
65
66         local kdir kver dir=${target:-/boot}
67         for kdir in /lib/modules/*; do
68                 [ -d $kdir ] || continue
69                 kver=${kdir##*/}
70                 echo "  $0 -f --initrdfs=initramfs $dir/initrd-$kver.gz $kver $verbose"
71         done | sort -V
72 }
73
74 msg() {
75         echo "$PROGRAM: $*"
76 }
77
78 warn() {
79         msg "WARNING: $*" >&2
80 }
81
82 debug() {
83         [ -n "$verbose" ] && msg "$*" >&3
84 }
85
86 # aborts program abnormally
87 die() {
88         local rc=${2:-1}
89         msg "ERROR: $1" >&2
90         exit $rc
91 }
92
93 # find program from specified paths
94 find_tool() {
95         local x
96         for x in "$@"; do
97                 if [ -x "$x" ]; then
98                         echo $x
99                         debug "find_tool: found $x"
100                         return 0
101                 fi
102         done
103         debug "find_tool: did not find any of: $@"
104         return 1
105 }
106
107 # loads geninitrd modules
108 geninitrd_load_mods() {
109         local mod
110         for mod in "$@"; do
111                 if [ ! -f /lib/geninitrd/mod-$mod.sh ]; then
112                         die "$mod geninitrd module can't be loaded"
113                 fi
114                 . /lib/geninitrd/mod-$mod.sh
115
116                 GENINITRD_MODS="$GENINITRD_MODS $mod"
117         done
118 }
119
120 # setup geninitrd modules
121 geninitrd_setup_mods() {
122         local mod rcsid
123
124         for mod in $GENINITRD_MODS; do
125                 eval rcsid=$(echo \$$mod | LC_ALL=C tr '[a-z]' '[A-Z]')_RCSID
126                 debug "# $rcsid (mod-$mod)"
127
128                 # some mods want init
129                 if type setup_mod_$mod > /dev/null; then
130                         eval setup_mod_$mod
131                 fi
132         done
133 }
134
135 # append text to /linuxrc
136 # takes STDIN as input
137 add_linuxrc() {
138         cat >> "$RCFILE"
139 }
140
141 # generate code to mount /dev on tmpfs and create initial nodes
142 # can be called multiple times. /dev is cleaned up (umounted) automatically at
143 # the end of script.
144 mount_dev() {
145         # we already generated tmpfs code; return
146         if is_yes "$dev_mounted"; then
147                 return
148         fi
149
150         dev_mounted=yes
151
152         busybox_applet mount mknod mkdir
153         add_linuxrc <<-EOF
154                 : 'Creating /dev'
155                 if ! mount -t devtmpfs -o mode=0755,nosuid devtmpfs /dev > /dev/null 2>&1; then
156                         mount -o mode=0755,nosuid -t tmpfs tmpfs /dev
157                         mknod /dev/console c 5 1
158                         mknod /dev/null c 1 3
159                         mknod /dev/zero c 1 5
160                         mknod /dev/random c 1 8
161                         mknod /dev/snapshot c 10 231
162                         mknod /dev/urandom c 1 9
163                         mknod /dev/ptmx c 5 2
164                         mknod /dev/kmsg c 1 11
165                 fi
166                 mkdir /dev/pts
167                 mkdir /dev/shm
168         EOF
169 }
170
171 # generate code to mount /proc on initrd
172 # can be called multiple times
173 mount_proc() {
174         if is_yes "$proc_mounted"; then
175                 return
176         fi
177
178         proc_mounted=yes
179     if [ "$INITRDFS" = "initramfs" ]; then
180                 # /proc is mounted with initramfs 2.6.22.14 kernel
181                 # XXX: remove when it is clear why proc was already mounted
182                 echo "[ -f /proc/cmdline ] || mount -t proc none /proc" | add_linuxrc
183         else
184                 echo "mount -t proc none /proc" | add_linuxrc
185         fi
186 }
187
188 # generate code to mount /sys on initrd
189 # can be called multiple times
190 mount_sys() {
191         if is_yes "$sys_mounted"; then
192                 return
193         fi
194
195         sys_mounted=yes
196         echo "mount -t sysfs none /sys" | add_linuxrc
197 }
198
199 # generate code to mount /tmp on initrd
200 # can be called multiple times
201 mount_tmp() {
202     if [ "$INITRDFS" = "initramfs" ]; then
203                 # initramfs is read-write filesystem, no need for tmpfs
204                 return
205         fi
206
207         if is_yes "$tmp_mounted"; then
208                 return
209         fi
210
211         tmp_mounted=yes
212         echo "mount -t tmpfs none /tmp" | add_linuxrc
213 }
214
215 # generate code to mount /run on initrd
216 # can be called multiple times
217 mount_run() {
218         if is_yes "$run_mounted"; then
219                 return
220         fi
221
222         run_mounted=yes
223         echo "mount -t tmpfs run /run" | add_linuxrc
224 }
225
226 # unmount all mountpoints mounted by geninitrd
227 umount_all() {
228
229         add_linuxrc <<-'EOF'
230         : Last shell before umounting all and giving control over to real init.
231         debugshell
232         EOF
233
234         if is_yes "$run_mounted"; then
235                 echo 'mount --bind /run /newroot/run' | add_linuxrc
236                 echo 'umount /run' | add_linuxrc
237                 run_mounted=no
238         fi
239         if is_yes "$dev_mounted"; then
240                 echo 'umount /dev' | add_linuxrc
241                 dev_mounted=no
242         fi
243         if is_yes "$sys_mounted"; then
244                 echo 'umount /sys' | add_linuxrc
245                 sys_mounted=no
246         fi
247         if is_yes "$tmp_mounted"; then
248                 echo 'umount /tmp' | add_linuxrc
249                 tmp_mounted=no
250         fi
251         if is_yes "$proc_mounted"; then
252                 echo 'umount /proc' | add_linuxrc
253                 proc_mounted=no
254         fi
255 }
256
257
258 # Checks if busybox has support for APPLET(s)
259 # Exits from geninitrd if the support is not present.
260 #
261 # NB! XXX do not output to STDOUT, it will appear in initrd images in some cases!
262 busybox_applet() {
263         local err=0 applet
264
265         if [ -z "$busybox_functions" ]; then
266                 local tmp=$($busybox 2>&1)
267
268                 # BusyBox v1.1.3 says applet not found if it's not called 'busybox'.
269                 if [[ "$tmp" = *applet\ not\ found* ]]; then
270                         local t=$(mktemp -d)
271                         ln -s $busybox $t/busybox
272                         local tmp=$($t/busybox 2>&1)
273                         rm -rf $t
274                 fi
275
276                 busybox_functions=$(echo "$tmp" | \
277                         sed -ne '/Currently defined functions:/,$p' | \
278                         xargs | sed -e 's,.*Currently defined functions: ,,'
279                 )
280         fi
281         for applet in $*; do
282                 local have
283                 # try cache
284                 eval have='$'busybox_have_$applet
285                 if [ -z "$have" ]; then
286                         have=$(echo "$busybox_functions" | egrep -c "( |^)$applet(,|$)")
287                         if [ "$have" = 0 ]; then
288                                 warn "This setup requires busybox-initrd compiled with applet '$applet' support"
289                                 err=1
290                         fi
291                         eval busybox_have_$applet=$have
292                 fi
293         done
294         if [ $err = 1 ]; then
295                 die "Aborted"
296         fi
297 }
298
299 # Extract the .config file from a kernel image
300 # uses extract-ikconfig from kernel sources (scripts/extract-ikconfig)
301 ikconfig() {
302         local kofile=$(modinfo -k $kernel -n configs 2> /dev/null)
303         if [ -n "$kofile" ]; then
304                 /lib/geninitrd/extract-ikconfig $kofile
305                 return
306         fi
307
308         # see if config available as separate file
309         if [ -f /boot/config-$kernel ]; then
310            cat /boot/config-$kernel
311            return
312         fi
313
314         # finally try vmlinuz itself
315         /lib/geninitrd/extract-ikconfig /boot/vmlinuz-$kernel
316 }
317
318 # Finds module dependencies
319 #
320 # @param        $module
321 #
322 # Outputs full path to module and it's dependencies
323 find_depmod() {
324         local module="$1"
325         local skiperrors=0
326
327         # if module is prefixed with dash, we should ignore errors if the module
328         # can't be found.
329         if [ ${module#-} != $module ]; then
330                 skiperrors=1
331                 module=${module#-}
332         fi
333
334         # This works when user has module-init-tools installed even on 2.4 kernels
335         local modprobe
336         modprobe=$(modprobe --set-version $kernel --show-depends $module --ignore-install 2>&1)
337
338         if [ $? != 0 ]; then
339                 if [ $skiperrors = 1 ]; then
340                         return 0
341                 fi
342                 echo >&2 "$modprobe"
343
344                 if ! is_no "$EXIT_IF_MISSING"; then
345                         die "$module: module not found for $kernel kernel"
346                 fi
347
348                 warn "$module: module not found for $kernel kernel"
349                 warn "If $module isn't compiled in kernel then this initrd may not start your system."
350         fi
351
352         echo "$modprobe" | \
353         while read insmod modpath options; do
354                 [ "$insmod" = "insmod" ] && echo $modpath
355         done
356         return 0
357 }
358
359 find_firmware() {
360         local module="$1"
361
362         # no firmware support in 2.4 kernels
363         if [ "$kernel_version_long" -lt "002005048" ]; then
364                 return
365         fi
366         echo -n $(NEW_MODINFO=1 modinfo -k $kernel -F firmware $module 2>/dev/null | xargs)
367 }
368
369 # @param        $module
370 find_module() {
371         local mod depmod module=$1
372
373         depmod=$(find_depmod $module) || exit 1
374         for mod in $depmod; do
375                 mod=${mod#/lib/modules/$kernel/}
376
377                 # add each module only once
378                 local m have=0
379                 for m in $MODULES; do
380                         [ $m = $mod ] && have=1
381                 done
382                 if [ $have = 0 ]; then
383                         MODULES="$MODULES $mod"
384                 fi
385         done
386 }
387
388 # install a file to temporary mount image.
389 # it will operate recursively (copying directories)
390 # and will symlink destinations if source is symlink.
391 inst() {
392         if [ $# -lt 2 ]; then
393                 die 'Usage: inst <file> [<file>] <destination>'
394         fi
395
396         local src i=0 c=$(($# - 1))
397         while [ $i -lt $c ]; do
398                 src="$src $1"
399                 i=$((i + 1))
400                 shift
401         done
402         local dest=$1
403         set -- $src
404         local parentDir=$(dirname $DESTDIR$dest)
405         [ ! -d "$parentDir" ] && (debug "+ mkdir -p $parentDir"; mkdir -p $parentDir)
406         debug "+ cp $* $DESTDIR$dest"
407         cp -HR "$@" "$DESTDIR$dest"
408 }
409
410 inst_d() {
411         if [ $# = 0 ]; then
412                 die 'Usage: inst_d <destination> <destination>'
413         fi
414         for dir in "$@"; do
415                 install -d "$DESTDIR$dir"
416         done
417 }
418
419 # install executable and it's shared libraries
420 inst_exec() {
421         if [ $# -lt 2 ]; then
422                 die "Invalid params ($@), Usage: inst_exec <file>[, <file>] <destination>"
423         fi
424         local src i=0 c=$(($# - 1))
425         while [ $i -lt $c ]; do
426                 src="$src $1"
427                 i=$((i + 1))
428                 shift
429         done
430         local dest=$1
431         set -- $src
432
433         inst "$@" $dest
434
435         local obj lib libs
436         for obj in "$@"; do
437                 case "$obj" in
438                         /lib/ld-linux.so.2 | /lib64/ld-linux-x86-64.so.2)
439                         continue
440                 esac
441
442                 libs=$(ldd "$obj" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
443                 for lib in $libs; do
444                         if [ ! -f "$DESTDIR/$_lib/${lib##*/}" ]; then
445                                 inst_d /$_lib
446                                 inst_exec $lib /$_lib
447                         fi
448                 done
449         done
450
451         # hack for uclibc linked binaries requiring this fixed path
452         # XXX: shouldn't rpath be used here instead so th
453         if [ -f $DESTDIR/$_lib/libc.so.0 ]; then
454                 local lib=$DESTDIR/$_lib/libc.so.0
455                 lib=$(ldd "$lib" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
456                 local libdir=$(cd $(dirname "$lib"); pwd)
457                 if [ ! -e $DESTDIR$libdir ]; then
458                         libdir=$(dirname "$libdir")
459                         inst_d $libdir
460                         debug "+ ln -s /$_lib $DESTDIR$libdir"
461                         ln -s /$_lib $DESTDIR$libdir
462                 fi
463         fi
464 }
465
466 # output modules.conf / modprobe.conf
467 modprobe_conf() {
468         echo "$modprobe_conf_cache"
469 }
470
471 # return options for MODULE
472 # @param $1 module name
473 modprobe_options() {
474         local module=$1
475         local options=$(modprobe_conf | awk -vmodule="$module" '{ if ($1 == "options" && $2 == module) { for(i=3;i<=NF;i++) printf("%s ",$i); }}')
476         echo ${options# }
477 }
478
479 #
480 # defaults to modprobe -c if not told otherwise, this means include statements
481 # work from there.
482 cache_modprobe_conf() {
483         if [ "$kernel_version" -lt "002005" ]; then
484                 modulefile=/etc/modules.conf
485                 if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
486                         modulefile=/etc/conf.modules
487                 fi
488         fi
489
490         if [ -n "$modulefile" ]; then
491                 debug "Using $modulefile for modules config"
492                 modprobe_conf_cache=$(cat $modulefile | awk '!/^[\t ]*#/ { print }')
493
494         else
495                 debug "Using modprobe -c to get modules config"
496                 modprobe_conf_cache=$(modprobe -c --set-version $kernel | awk '!/^[\t ]*#/ { print }')
497         fi
498 }
499
500 # find modules for $devpath
501 find_modules_for_devpath() {
502         local devpath="$1"
503         if [ -z "$devpath" ]; then
504                 die "No argument passed to find_modules_for_devpath() - is your /etc/fstab correct?"
505         fi
506
507         if [[ "$devpath" = /dev/dm-* ]]; then
508                 # /dev/dm-3 -> /dev/mapper/sil_ahbgadcbchfc3
509                 devpath=$(dm_node "$devpath")
510         fi
511
512         if [ -L "$devpath" ] && ! is_lvm "$devpath" && ! is_luks "$devpath"; then
513                 # sanitize things like:
514                 # /dev/block/104:2 -> /dev/cciss/c0d0p2
515                 devpath=$(readlink -f "$devpath")
516         fi
517
518         debug "Finding modules for device path $devpath"
519
520         if is_luks "$devpath"; then
521                 find_modules_luks "$devpath"
522                 return
523         fi
524
525         if is_nfs "$devpath"; then
526                 find_modules_nfs "$devpath"
527                 return
528         fi
529
530         if is_md "$devpath"; then
531                 find_modules_md "$devpath"
532                 return
533         fi
534
535         if is_multipath "$devpath"; then
536                 if find_modules_multipath "$devpath"; then
537                         return
538                 fi
539
540                 # fallback
541         fi
542
543         if is_dmraid "$devpath"; then
544                 if find_modules_dmraid "$devpath"; then
545                         return
546                 fi
547                 # fallback
548         fi
549
550         if is_scsi "$devpath"; then
551                 find_modules_scsi "$devpath"
552                 return
553         fi
554
555         if is_ide "$devpath"; then
556                 find_modules_ide "$devpath"
557                 return
558         fi
559
560         if [[ "$devpath" == /dev/rd/* ]]; then
561                 find_module "DAC960"
562                 rootdev_add=/dev/rd/
563                 return
564         fi
565
566         if [[ "$devpath" == /dev/ida/* ]]; then
567                 find_module "cpqarray"
568                 rootdev_add=/dev/ida/
569                 return
570         fi
571
572         if [[ "$devpath" == /dev/cciss/* ]]; then
573                 rootdev_add=/dev/cciss/
574
575                 # load hpsa for future kernels, cciss for backwards compat
576                 if [ "$kernel_version_long" -ge "003000000" ]; then
577                         find_module "hpsa" "-cciss"
578                         find_modules_scsi "$devpath"
579                 else
580                         find_module "cciss"
581                 fi
582
583                 return
584         fi
585
586         if [[ "$devpath" == /dev/ataraid/* ]]; then
587                 find_modules_ide
588                 find_module "ataraid"
589                 ataraidmodules=$(modprobe_conf | awk '/ataraid_hostadapter/ { print $3 }')
590                 if [ -n "$ataraidmodules" ]; then
591                         # FIXME: think about modules compiled in kernel
592                         die "ataraid_hostadapter alias not defined in modprobe.conf! Please set it and run $PROGRAM again."
593                 fi
594                 for n in $ataraidmodules; do
595                         find_module "$n"
596                 done
597                 rootdev_add=/dev/ataraid/
598                 return
599         fi
600
601         # check to see if we need to set up a loopback filesystem
602         if [[ "$devpath" == /dev/loop*  ]]; then
603                 die "Sorry, root on loop device isn't supported."
604                 # TODO: rewrite for bsp and make nfs ready
605                 if [ ! -x /sbin/losetup ]; then
606                         die "losetup is missing"
607                 fi
608                 key="^# $(echo $devpath | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
609                 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`"; then
610                         die "The root filesystem is on a $devpath, but there is no magic entry in $fstab for this device. Consult the $PROGRAM man page for more information"
611                 fi
612
613                 line="`awk '/'$key'/ { print $0; }' $fstab`"
614                 loopDev="$(echo $line | awk '{print $3}')"
615                 loopFs="$(echo $line | awk '{print $4}')"
616                 loopFile="$(echo $line | awk '{print $5}')"
617
618                 BASICMODULES="$BASICMODULES -loop"
619                 find_module "-$loopFs"
620                 BASICMODULES="$BASICMODULES -${loopFs}"
621                 return
622         fi
623
624         if is_lvm "$devpath"; then
625                 find_modules_lvm "$devpath"
626                 return
627         fi
628 }
629
630 firmware_install_module() {
631         local module="$1"
632         local firmware_files="$2"
633
634         debug "Adding Firmwares ($firmware_files) to initrd for module $module"
635         # firmware not yet installed
636         if [ ! -f "$DESTDIR/lib/firmware/firmware.sh" ]; then
637                 inst_d /lib/firmware
638 cat << 'EOF' >> "$DESTDIR/lib/firmware/firmware.sh"
639 #!/bin/sh -e
640 echo 1 > /sys$DEVPATH/loading
641 cat "/lib/firmware/$FIRMWARE" > /sys$DEVPATH/data
642 echo 0 > /sys$DEVPATH/loading
643 exit 0
644 EOF
645                 chmod 755 "$DESTDIR/lib/firmware/firmware.sh"
646
647                 # setup firmware loader agent
648                 echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" | add_linuxrc
649         fi
650
651         for firmware in $firmware_files; do
652                 if [ -f "/lib/firmware/$kernel/$firmware" ]; then
653                         FIRMWAREDIR=${firmware%/*}
654                         [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR
655                         inst /lib/firmware/$kernel/$firmware /lib/firmware/$firmware
656                 elif [ -f "/lib/firmware/$firmware" ]; then
657                         FIRMWAREDIR=${firmware%/*}
658                         [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR
659                         inst /lib/firmware/$firmware /lib/firmware/$firmware
660                 else
661                         warn "Possible missing firmware file /lib/firmware/$firmware or /lib/firmware/$kernel/$firmware for module $module."
662                 fi
663         done
664
665         mount_sys
666 }
667
668 modules_install() {
669         local modules="$1"
670         local mod
671
672         for mod in $modules; do
673                 MODULEDIR=${mod%/*}
674                 inst_d "/lib/modules/$kernel/$MODULEDIR"
675                 cp -a "/lib/modules/$kernel/$mod" "$DESTDIR/lib/modules/$kernel/$mod"
676                 gunzip "$DESTDIR/lib/modules/$kernel/$mod" 2> /dev/null
677                 if [ "$STRIP" ] && [ -x "$STRIP" ]; then
678                         $STRIP -g --remove-section=.comment "$DESTDIR/lib/modules/$kernel/${mod%.gz}"
679                 fi
680         done
681 }
682
683 modules_add_linuxrc() {
684         local mod modpath
685
686         for mod in "$@"; do
687                 # module path without optional compression
688                 modpath=${mod%.gz}
689
690                 # name of the module
691                 local module=${modpath##*/}; module=${module%$modext}
692                 local options=$(modprobe_options "$module")
693                 local genericname=$(echo $module | tr - _)
694                 local usleep=$(eval echo \$MODULE_${genericname}_USLEEP)
695                 local firmware=$(eval echo \$MODULE_${genericname}_FIRMWARE)
696
697                 if [ "$module" = "scsi_mod" -a "$kernel_version_long" -ge "002006030" ]; then
698                         options="scan=sync $options"
699                 fi
700
701                 if [ -n "$verbose" ]; then
702                         s=""
703                         if [ "$options" ]; then
704                                 s="$s with options [$options]"
705                         fi
706                         if [ "$usleep" ]; then
707                                 s="$s and $usleep usleep"
708                         fi
709                         debug "Loading module [$module]$s"
710                 fi
711
712                 if [ -n "$firmware" ]; then
713                         firmware_install_module "$module" "$firmware"
714                 else
715                         for file in $(find_firmware "$module"); do
716                                 firmware_install_module "$module" "$file"
717                         done
718                 fi
719
720                 echo "insmod /lib/modules/$kernel/$modpath $options" | add_linuxrc
721                 if [ -n "$usleep" ]; then
722                         echo "usleep $usleep" | add_linuxrc
723                 fi
724                 if [ "$module" = "scsi_wait_scan" ]; then
725                         if [ "$(busybox_applet rmmod 2>/dev/null; echo $?)" = 0 ]; then
726                                 echo "rmmod scsi_wait_scan" | add_linuxrc
727                         fi
728                 fi
729
730         done
731 }
732
733 # Generates /dev nodes based on /proc/partitions information.
734 # Needs /proc mounted.
735 # Can be called multiple times.
736 initrd_gen_devices() {
737         if is_yes "$proc_partitions"; then
738                 return
739         fi
740         proc_partitions=yes
741
742         mount_dev
743         add_linuxrc <<-'EOF'
744                 : 'Making device nodes'
745                 cat /proc/partitions | (
746                         # ignore first two lines: header, empty line
747                         read b; read b
748
749                         while read major minor blocks dev rest; do
750                                 node=/dev/$dev
751                                 mkdir -p ${node%/*}
752                                 [ -e $node ] || mknod $node b $major $minor
753                         done
754                 )
755         EOF
756 }
757
758
759 initrd_gen_setrootdev() {
760         debug "Adding rootfs finding based on kernel cmdline root= option support."
761         busybox_applet ls
762         debug "Current /proc/partitions:\n$(sed -e 's,^,> ,' /proc/partitions)"
763         add_linuxrc <<-'EOF'
764                 if [ "${ROOT##/dev/}" != "${ROOT}" ]; then
765                         rootnr="$(busybox awk -v rootnode="${ROOT##/dev/}" '$4 == rootnode { print 256 * $1 + $2 }' /proc/partitions)"
766                         # fallback to ls
767                         if [ -z "$rootnr" ]; then
768                                 rootnr="$(busybox ls -lL ${ROOT} | busybox awk '{if (/^b/) { print 256 * $3 + $4; }}')"
769                         fi
770                         if [ -n "$rootnr" ]; then
771                                 echo "$rootnr" > /proc/sys/kernel/real-root-dev
772                         fi
773                 fi
774         EOF
775 }
776
777 initrd_gen_initramfs_switchroot() {
778         inst_d /newroot
779         if [ "$rootdev" = "/dev/nfs" ]; then
780                 echo "rootfs on NFS root=/dev/nfs"
781         else
782                 [ ! -e "$DESTDIR/$rootdev" ] && inst $rootdev $rootdev
783         fi
784
785         # parse 'root=xxx' kernel commandline
786         # We support passing root as hda3 /dev/hda3 0303 0x0303 and 303
787
788         # from lilo-23.2/readme/README:
789         # root=<device> changes the root device. This overrides settings that may
790         # have been made in the boot image and on the LILO command line. <device> is
791         # either the hexadecimal device number or the full path name of the device,
792         # e.g. /dev/hda3 [*]
793         #
794         #  *  The device names are hard-coded in the kernel. Therefore, only the
795         #         "standard" names are supported and some less common devices may not be
796         #         recognized. In those cases, only numbers can be used.
797         add_linuxrc <<-'EOF'
798                 device=
799                 eval "$(busybox awk -v root="$ROOT" '
800                         BEGIN {
801                                 num_pattern_short = "[0-9a-f][0-9a-f][0-9a-f]";
802                                 num_pattern = "[0-9a-f]" num_pattern_short;
803                                 dev_pattern = "[hms][a-z][a-z]([0-9])+";
804                                 partition = "";
805                                 min = -1; maj = -1;
806
807                                 # see if we have /dev/hdX or hdX, we can just take partition name
808                                 if (root ~ "^\/dev\/" dev_pattern "$" || root ~ "^" dev_pattern "$") {
809                                         partition = root
810                                         sub("^/dev/", "", partition);
811
812                                 } else {
813                                         # unify values first
814                                         if (root ~ "^" num_pattern_short "$")  {
815                                                 # change "303" => "0x0303"
816                                                 root = "0x0" root
817                                         } else if (root ~ "^" num_pattern "$")  {
818                                                 # change "0303" => "0x0303"
819                                                 root = "0x" root
820                                         }
821
822                                         maj = sprintf("%d", "0x" substr(root, 3, 2));
823                                         min = sprintf("%d", "0x" substr(root, 5, 2));
824                                 }
825                         }
826
827                         partition && $4 == partition { maj = $1; min = $2; }
828                         $1 == maj && $2 == min { partition = $4; }
829
830                         END {
831                                 if (maj >= 0 && min >= 0) {
832                                         printf("maj=%s; min=%s;\n", maj, min);
833                                 }
834                                 if (partition) {
835                                         printf("device=/dev/%s;\n", partition);
836                                 }
837                         }
838                         ' /proc/partitions)"
839
840                 if [ -z "$device" ]; then
841                         device=$ROOT
842                 fi
843
844                 if [ "$device" -a ! -b $device ]; then
845                         mknod $device b $maj $min
846                 fi
847
848                 [ -n "$ROOTFSFLAGS" ] && ROOTFSFLAGS="-o $ROOTFSFLAGS"
849
850                 mount -t $ROOTFS -r $device $ROOTFSFLAGS /newroot || echo "Mount of rootfs failed."
851                 init=$INIT
852                 if [ -z "$init" -o ! -x "/newroot$init" ]; then
853                         init=/sbin/init
854                 fi
855         EOF
856
857         umount_all
858         busybox_applet switch_root
859         add_linuxrc <<-'EOF'
860                 [ ! -e /newroot/dev/console ] && mknod -m 660 /newroot/dev/console c 5 1
861                 exec switch_root /newroot $init ${1:+"$@"}
862
863                 echo "Error! initramfs should not reach this place."
864                 echo "It probably means you've got old version of busybox, with broken"
865                 echo "initramfs support. Trying to boot anyway, but won't promise anything."
866
867                 exec chroot /newroot $init ${1:+"$@"}
868
869                 echo "Failed to chroot!"
870                 debugshell
871         EOF
872         # we need /init being real file, not symlink, otherwise the initramfs will
873         # not be ran by pid 1 which is required for switch_root
874         mv $DESTDIR/linuxrc $DESTDIR/init
875         ln -s init $DESTDIR/linuxrc
876 }
877
878 # find if $symbol exists in System.map $mapfile
879 sym_exists() {
880         local mapfile="$1"
881         local symbol="$2"
882         if [ ! -f $mapfile ]; then
883                 # missing mapfile (not a pld kernel?)
884                 return 1
885         fi
886
887         awk -vc=1 -vsymbol="$symbol" '$2 == "T" && $3 == symbol {c = 0} END {exit c}' $mapfile
888 }
889
890 # find best compressor (or forced one) for initrd
891 find_compressor() {
892         local mode="$1"
893         # fastest initrd decompression speed is first
894         local compressors='lzo gzip xz lzma bzip2'
895
896         # a specified one, take it
897         if ! is_yes "$mode"; then
898                 compressors="$mode"
899         fi
900
901         debug "finding compressor: $compressors (via $mode)"
902         # check for compressor validity
903         local c prog map=/boot/System.map-$kernel
904         for c in $compressors; do
905                 case $c in
906                 xz)
907                         sym=unxz
908                         prog=/usr/bin/xz
909                         ;;
910                 lzma)
911                         sym=unlzma
912                         prog=/usr/bin/xz
913                         ;;
914                 bzip2)
915                         sym=bzip2
916                         prog=/usr/bin/bzip2
917                         ;;
918                 gzip)
919                         sym=gunzip
920                         prog=/bin/gzip
921                         ;;
922                 lzo)
923                         sym=unlzo
924                         prog=/usr/bin/lzop
925                         ;;
926                 none|no)
927                         # any existing sym will work
928                         sym=initrd_load
929                         prog=/bin/cat
930                         ;;
931                 *)
932                         die "Unknown compressor $c"
933                         ;;
934                 esac
935                 if sym_exists $map $sym && [ -x $prog ]; then
936                         echo $c
937                         return
938                 fi
939         done
940
941         debug "using gzip for compressor (fallback)"
942         echo gzip
943 }
944
945 # compresses kernel image image
946 # in function so we could retry with other compressor on failure
947 compress_image() {
948         local compressor="$1" IMAGE="$2" target="$3" tmp
949         tmp=$(mktemp "$target".XXXXXX) || die "mktemp failed"
950
951         case "$compressor" in
952         xz)
953                 # don't use -9 here since kernel won't understand it
954                 xz --format=xz --check=crc32 --lzma2=preset=6e,dict=1MiB < "$IMAGE" > "$tmp" || return $?
955                 ;;
956         lzma)
957                 xz --format=lzma -9 < "$IMAGE" > "$tmp" || return $?
958                 ;;
959         bzip2)
960                 bzip2 -9 < "$IMAGE" > "$tmp" || return $?
961                 ;;
962         gzip)
963                 gzip -9 < "$IMAGE" > "$tmp" || return $?
964                 ;;
965         lzo)
966                 lzop -9 < "$IMAGE" > "$tmp" || return $?
967                 ;;
968         none|no)
969                 cat < "$IMAGE" > "$tmp" || return $?
970                 ;;
971         esac
972
973         mv -f "$tmp" "$target"
974 }
975
976 if [ -r /etc/sysconfig/geninitrd ]; then
977         . /etc/sysconfig/geninitrd
978 fi
979
980 if [ ! -f /proc/mounts ]; then
981         warn "/proc filesystem not mounted, may cause wrong results or failure."
982 fi
983
984 geninitrd_load_mods ide luks multipath dmraid lvm md blkid udev tuxonice suspend fbsplash condecor bootsplash uvesafb nfs sata scsi usbkbd
985
986 while [ $# -gt 0 ]; do
987         case $1 in
988         --fstab=*)
989                 fstab=${1#--fstab=}
990                 ;;
991         --fstab)
992                 fstab=$2
993                 shift
994                 ;;
995         --modules-conf=*)
996                 modulefile=${1#--modules-conf=}
997                 ;;
998         --modules-conf)
999                 modulefile=$2
1000                 shift
1001                 ;;
1002         --with-bootsplash)
1003                 BOOT_SPLASH=yes
1004                 ;;
1005         --without-bootsplash)
1006                 BOOT_SPLASH=no
1007                 ;;
1008         --with-fbsplash)
1009                 FB_SPLASH=yes
1010                 ;;
1011         --without-fbsplash)
1012                 FB_SPLASH=no
1013                 ;;
1014         --with-fbcondecor)
1015                 FB_CON_DECOR=yes
1016                 ;;
1017         --without-fbcondecor)
1018                 FB_CON_DECOR=no
1019                 ;;
1020         --with-suspend)
1021                 USE_SUSPEND=yes
1022                 ;;
1023         --without-suspend)
1024                 USE_SUSPEND=no
1025                 ;;
1026         --with-suspend2 | --with-tuxonice)
1027                 USE_TUXONICE=yes
1028                 ;;
1029         --without-suspend2 | --without-tuxonice)
1030                 USE_TUXONICE=no
1031                 ;;
1032         --lvmversion=*)
1033                 LVMTOOLSVERSION=${1#--lvmversion=}
1034                 ;;
1035         --lvmtoolsversion=*)
1036                 LVMTOOLSVERSION=${1#--lvmtoolsversion=}
1037                 ;;
1038         --lvmtoolsversion|--lvmversion)
1039                 LVMTOOLSVERSION=$2
1040                 shift
1041                 ;;
1042         --without-udev)
1043                 USE_UDEV=no
1044                 ;;
1045         --with-udev)
1046                 USE_UDEV=yes
1047                 ;;
1048         --without-dmraid)
1049                 USE_DMRAID=no
1050                 ;;
1051         --without-multipath)
1052                 USE_MULTIPATH=no
1053                 ;;
1054         --with-multipath=*)
1055                 USE_MULTIPATH=${1#--with-multipath=}
1056                 ;;
1057         --without-blkid)
1058                 USE_BLKID=no
1059                 ;;
1060         --without-luks)
1061                 USE_LUKS=no
1062                 ;;
1063         --with=*)
1064                 BASICMODULES="$BASICMODULES ${1#--with=}"
1065                 ;;
1066         --with)
1067                 BASICMODULES="$BASICMODULES $2"
1068                 shift
1069                 ;;
1070         --version)
1071                 echo "$PROGRAM: version $VERSION"
1072                 exit 0
1073                 ;;
1074         -v)
1075                 verbose=-v
1076                 exec 3>&1
1077                 ;;
1078         --compress)
1079                 COMPRESS=$2
1080                 ;;
1081         --compress=*)
1082                 COMPRESS="${1#--compress=}"
1083                 ;;
1084         --nocompress)
1085                 COMPRESS=no
1086                 ;;
1087         --nostrip)
1088                 STRIP=
1089                 ;;
1090         --strip=*)
1091                 STRIP="${1#--strip=}"
1092                 ;;
1093         --strip)
1094                 STRIP=$2
1095                 shift
1096                 ;;
1097         --ifneeded)
1098                 ifneeded=1
1099                 ;;
1100         -f)
1101                 force=1
1102                 ;;
1103         --preload=*)
1104                 PREMODS="$PREMODS ${1#--preload=}"
1105                 ;;
1106         --preload)
1107                 PREMODS="$PREMODS $2"
1108                 shift
1109                 ;;
1110         --fs=* | --fs)
1111                 die "--fs option is obsoleted. Use --initrdfs instead"
1112                 ;;
1113         --initrdfs=*)
1114                 INITRDFS=${1#--initrdfs=}
1115                 ;;
1116         --initrdfs)
1117                 INITRDFS=$2
1118                 shift
1119                 ;;
1120         --image-version)
1121                 img_vers=yes
1122                 ;;
1123         --ide-only-root)
1124                 ide_only_root="yes"
1125                 ;;
1126         *)
1127                 if [ -z "$target" ]; then
1128                         target="$1"
1129                 elif [ -z "$kernel" ]; then
1130                         kernel="$1"
1131                 else
1132                         usage
1133                         exit 1
1134                 fi
1135                 ;;
1136         esac
1137
1138         shift
1139 done
1140
1141 if [ -z "$target" -o -z "$kernel" ]; then
1142         usage
1143         exit 1
1144 fi
1145
1146 # main()
1147 if [ "$(id -u)" != 0 ]; then
1148         die "You need to be root to generate initrd"
1149 fi
1150
1151 if [ -d /lib64 -a -d /usr/lib64 ]; then
1152         _lib=lib64
1153 else
1154         _lib=lib
1155 fi
1156
1157 initrd_dir=/usr/$_lib/initrd
1158 kernel_version=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d",$1,$2)}')
1159 kernel_version_long=$(echo "$kernel" | awk -F. '{gsub(/[_-].*/, "", $0); print sprintf("%03d%03d%03d",$1,$2,$3)}')
1160
1161 debug "# $GENINITRD_RCSID (geninitrd)"
1162 debug "Using _lib: $_lib"
1163 debug "Using initrd_dir: $initrd_dir"
1164
1165 busybox=$(find_tool $initrd_dir/busybox $initrd_dir/initrd-busybox /bin/initrd-busybox) || die "Couldn't find busybox suitable for initrd"
1166
1167 # we setup mods after parsing command line args
1168 geninitrd_setup_mods
1169
1170 if [ ! -f /boot/vmlinuz-"$kernel" ]; then
1171         warn "/boot/vmlinuz-$kernel doesn't exist, is your /boot mounted?"
1172 fi
1173
1174 if [ -z "$INITRDFS" ]; then
1175         if [ -n "$FS" ]; then
1176                 # FS= can came only via /etc/sysconfig/geninitrd likely?
1177                 die "FS configuration option is obsoleted. Use INITRDFS instead"
1178         fi
1179
1180         # default value
1181         if [ "$kernel_version" -ge "002005" ]; then
1182                 INITRDFS="initramfs"
1183         else
1184                 INITRDFS="rom"
1185         fi
1186 fi
1187
1188 case "$INITRDFS" in
1189   ext2)
1190         [ -x /sbin/mke2fs ] || die "/sbin/mke2fs is missing"
1191         ;;
1192   rom|romfs)
1193         [ -x /sbin/genromfs ] || die "/sbin/genromfs is missing"
1194         ;;
1195   cram|cramfs)
1196         [ -x /sbin/mkcramfs ] || die "/sbin/mkcramfs is missing"
1197         ;;
1198   initramfs)
1199         [ -x /bin/cpio ] || die "/bin/cpio is missing"
1200         [ -x /usr/bin/find ] || die "/usr/bin/find is missing"
1201         ;;
1202   *)
1203         die "Filesystem $INITRDFS on initrd is not supported"
1204         ;;
1205 esac
1206
1207 if [ -L "$target" ]; then
1208         target=$(readlink -f "$target")
1209 fi
1210
1211 if [ -n "$img_vers" ]; then
1212         target="$target-$kernel"
1213 fi
1214
1215 if [ -z "$force" -a -f "$target" ]; then
1216         die "$target already exists."
1217 fi
1218
1219 if [ ! -d "/lib/modules/$kernel" ]; then
1220         die "/lib/modules/$kernel is not a directory."
1221 fi
1222
1223 if [ "$kernel_version" -ge "002005" ]; then
1224         modext=".ko"
1225 fi
1226
1227 cache_modprobe_conf
1228
1229 for n in $PREMODS; do
1230         find_module "$n"
1231 done
1232
1233 if [ "$FBMODULE" ]; then
1234         find_module "$FBMODULE"
1235 fi
1236
1237 # autodetect USB keyboards
1238 find_modules_usbkbd
1239
1240 # allow forcing loading SCSI and/or IDE modules
1241 # XXX: where ADDSCSI cames from? drop?
1242 if is_yes "$ADDSCSI"; then
1243         find_modules_scsi
1244 fi
1245
1246 # autodetect SATA modules
1247 find_modules_sata
1248
1249 # XXX: where ADDIDE cames from? drop?
1250 if is_yes "$ADDIDE"; then
1251         find_modules_ide
1252 fi
1253
1254 if is_yes "$USE_SUSPEND"; then
1255         find_modules_suspend
1256 fi
1257
1258 find_root "$fstab" || exit
1259 debug "Using $rootdev as device for rootfs"
1260
1261 find_modules_for_devpath "$rootdev"
1262
1263 # if USE_MULTIPATH is path to device, scan that too
1264 # this is to bootstrap multipath setup into initrd.
1265 if ! is_no "$USE_MULTIPATH" && ! is_yes "$USE_MULTIPATH"; then
1266         find_modules_multipath $USE_MULTIPATH
1267 fi
1268
1269 find_module "-$rootFs"
1270
1271 for n in $BASICMODULES; do
1272         find_module "$n"
1273 done
1274
1275 if is_yes "$USE_TUXONICE"; then
1276         find_module "-lzf"
1277 fi
1278
1279 find_modules_fbsplash
1280
1281 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
1282         debug "No modules are needed -- not building initrd image."
1283         exit 0
1284 fi
1285
1286 debug "Building initrd..."
1287 DESTDIR=$(mktemp -d -t initrd.XXXXXX) || die "mktemp failed"
1288 RCFILE="$DESTDIR/linuxrc"
1289 > "$RCFILE"
1290 chmod a+rx "$RCFILE"
1291 ln -s linuxrc $DESTDIR/init
1292
1293 # create dirs that we really need
1294 inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,run,sys}
1295
1296 modules_install "$MODULES"
1297
1298 # mknod'ing the devices instead of copying them works both with and
1299 # without devfs...
1300 mknod "$DESTDIR/dev/console" c 5 1
1301 mknod "$DESTDIR/dev/null" c 1 3
1302 mknod "$DESTDIR/dev/zero" c 1 5
1303 mknod "$DESTDIR/dev/random" c 1 8
1304 mknod "$DESTDIR/dev/urandom" c 1 9
1305
1306 inst_exec $busybox /bin/busybox
1307 ln -s busybox $DESTDIR/bin/sh
1308 # for older busyboxes who had /bin/initrd-busybox as EXEPATH
1309 ln -s busybox $DESTDIR/bin/initrd-busybox
1310
1311 add_linuxrc <<EOF
1312 #!/bin/sh
1313 # initrd generated by:
1314 # $GENINITRD_RCSID
1315
1316 EOF
1317 mount_proc
1318 add_linuxrc <<-EOF
1319         # builtin defaults from geninitrd
1320         ROOT=$rootdev
1321         ROOTFS=$rootFs
1322 EOF
1323 add_linuxrc <<-'EOF'
1324         read CMDLINE < /proc/cmdline
1325
1326         for arg in $CMDLINE; do
1327                 if [ "${arg}" = "debuginitrd" ]; then
1328                         DEBUGINITRD=yes
1329                 fi
1330                 if [ "${arg##debuginitrd=}" != "${arg}" ]; then
1331                         DEBUGINITRD=${arg##debuginitrd=}
1332                 fi
1333                 if [ "${arg##root=}" != "${arg}" ]; then
1334                         ROOT=${arg##root=}
1335                 fi
1336                 if [ "${arg##rootfsflags=}" != "${arg}" ]; then
1337                         ROOTFSFLAGS=${arg##rootfsflags=}
1338                 fi
1339                 if [ "${arg##init=}" != "${arg}" ]; then
1340                         INIT=${arg##init=}
1341                 fi
1342         done
1343
1344         # make debugshell() invoke subshell if $DEBUGINITRD=sh
1345         if [ "$DEBUGINITRD" = "sh" ]; then
1346                 debugshell() {
1347 EOF
1348 if is_yes "$RUN_SULOGIN_ON_ERR"; then
1349 add_linuxrc <<-'EOF'
1350         echo "debug shell disabled by /etc/sysconfig/system: RUN_SULOGIN_ON_ERR setting"
1351 EOF
1352 else
1353 add_linuxrc <<-'EOF'
1354         sh
1355 EOF
1356 fi
1357 add_linuxrc <<-'EOF'
1358                 }
1359         else
1360                 debugshell() {
1361                         :
1362                 }
1363         fi
1364
1365         if [ "$DEBUGINITRD" ]; then
1366                 set -x
1367         fi
1368 EOF
1369
1370 modules_add_linuxrc $MODULES
1371
1372 # TODO: rewrite for busybox
1373 #if [ -n "$loopDev" ]; then
1374 #       if [ ! -d /initrd ]; then
1375 #               mkdir /initrd
1376 #       fi
1377 #
1378 #       cp -a "$loopDev" "$DESTDIR/dev"
1379 #       cp -a "$rootdev" "$DESTDIR/dev"
1380 #       echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1381 #       echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1382 #       echo "echo Setting up loopback device $rootdev" >> $RCFILE
1383 #       echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE"
1384 #fi
1385
1386 if is_yes "$USE_UDEV"; then
1387         initrd_gen_udev
1388 fi
1389
1390 find_modules_uvesafb
1391 initrd_gen_uvesafb
1392
1393 initrd_gen_luks
1394 initrd_gen_dmraid
1395 initrd_gen_multipath
1396 initrd_gen_blkid
1397
1398 if is_yes "$have_nfs"; then
1399         initrd_gen_nfs
1400 else
1401         initrd_gen_md
1402         initrd_gen_lvm
1403         initrd_gen_luks
1404         initrd_gen_setrootdev
1405 fi
1406
1407 initrd_gen_tuxonice
1408 initrd_gen_suspend
1409
1410 # additional devs always needed
1411 [ ! -e "$DESTDIR/$rootdev_add" ] && inst $rootdev_add /dev
1412
1413 initrd_gen_stop_udevd
1414 initrd_gen_stop_uvesafb
1415
1416 # clean up env
1417 add_linuxrc <<-'EOF'
1418         ifs=$IFS
1419         IFS="
1420         "
1421         for i in $(export -p); do
1422                 i=${i#declare -x } # ksh/bash
1423                 i=${i#export } # busybox
1424
1425                 case "$i" in
1426                 *=*)
1427                         : ;;
1428                 *)
1429                         continue ;;
1430                 esac
1431
1432                 i=${i%%=*}
1433
1434                 [ -z "$i" ] && continue
1435
1436                 case "$i" in
1437                         ROOT|PATH|HOME|TERM)
1438                                 :
1439                                 ;;
1440                         *)
1441                                 unset $i
1442                                 ;;
1443                 esac
1444         done
1445         IFS=$ifs
1446 EOF
1447
1448 if [ "$INITRDFS" = "initramfs" ]; then
1449         initrd_gen_initramfs_switchroot
1450 else
1451         umount_all
1452 fi
1453
1454 initrd_gen_fbsplash
1455 initrd_gen_fbcondecor
1456
1457 debug "Current /linuxrc:\n$(sed -e 's,^,> ,' $DESTDIR/linuxrc)"
1458
1459 IMAGE=$(mktemp -t initrd.img-XXXXXX) || die "mktemp failed"
1460
1461 IMAGESIZE=$(du -ks $DESTDIR | awk '{print int(($1+1023+512)/1024)*1024}')
1462 debug "image size: $IMAGESIZE KiB ($DESTDIR)"
1463
1464 debug "Creating $INITRDFS image $IMAGE"
1465 case "$INITRDFS" in
1466   ext2)
1467         dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
1468         # NOTE: ext2 label is max 16 chars
1469         mke2fs -q -F -b 1024 -m 0 -L "PLD/$kernel" "$IMAGE" 2>/dev/null 1>&2
1470         tune2fs -i 0 "$IMAGE" >/dev/null 2>&1
1471
1472         local tmpmnt=$(mktemp -d -t initrd.mnt-XXXXXX)
1473         debug "Mounting ext2 image $IMAGE to $tmpmnt"
1474         mount -o loop -t ext2 "$IMAGE" "$tmpmnt" || die "mount failed, check dmesg(1)"
1475         # We don't need this directory, so let's save space
1476         rm -rf "$tmpmnt"/lost+found
1477
1478         debug "Copy recursively $DESTDIR -> $tmpmnt"
1479         cp -a $DESTDIR/* $tmpmnt
1480         umount "$IMAGE"
1481         rmdir "$tmpmnt"
1482
1483         ;;
1484   rom|romfs)
1485         genromfs -f "$IMAGE" -d "$DESTDIR" -V "PLD Linux/$kernel (geninitrd/$VERSION)"
1486         ;;
1487   cram|cramfs)
1488         mkcramfs "$DESTDIR" "$IMAGE"
1489         ;;
1490   initramfs)
1491         (cd $DESTDIR; find . | cpio --quiet -H newc -o > "$IMAGE")
1492         ;;
1493   *)
1494         die "Filesystem $INITRDFS not supported by $PROGRAM"
1495 esac
1496
1497 CONFIG_BLK_DEV_RAM_SIZE=$(ikconfig | awk -F= '/^CONFIG_BLK_DEV_RAM_SIZE/{print $2}')
1498 if [ -z "$CONFIG_BLK_DEV_RAM_SIZE" ]; then
1499         CONFIG_BLK_DEV_RAM_SIZE=4096
1500         warn "No CONFIG_BLK_DEV_RAM_SIZE detected, fallback to $CONFIG_BLK_DEV_RAM_SIZE"
1501 fi
1502
1503 if [ "$IMAGESIZE" -gt $CONFIG_BLK_DEV_RAM_SIZE ]; then
1504         warn "Your image size is larger than $CONFIG_BLK_DEV_RAM_SIZE, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!"
1505 fi
1506
1507 if ! is_no "$COMPRESS"; then
1508         compressor=$(find_compressor "$COMPRESS")
1509         debug "Compressing $target with $compressor"
1510
1511         # TODO: the image name (specified from kernel.spec) already contains
1512         # extension, which is .gz most of the time.
1513         compress_image "$compressor" "$IMAGE" "$target" || {
1514                 if [ $compressor != gzip ]; then
1515                         warn "Could not compress with $compressor, retrying with gzip"
1516                         compress_image gzip "$IMAGE" "$target" || die "compress failed with gzip" $?
1517                 else
1518                         die "Could not compress image with $compressor"
1519                 fi
1520         }
1521 else
1522         cp -a "$IMAGE" "$target"
1523 fi
1524
1525 # XXX. check if bootsplash can output data to tmp dir not directly to initramfs image.
1526 initrd_gen_bootsplash "$target"
1527
1528 rm -rf "$DESTDIR" "$IMAGE"
1529
1530 # vim:ts=4:sw=4:noet:fdm=marker
This page took 0.150365 seconds and 4 git commands to generate.