]> git.pld-linux.org Git - projects/geninitrd.git/blob - geninitrd
- more specific /dev/hd* match (according to dev-3.4-1 /dev/hda[a-l] are used) to...
[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 # TODO:
10 # - make udev start before insmods
11 # - make proper use of USE_UDEV - don't copy rules if USE_UDEV is off no!
12 #
13
14 RCSID='$Id$'
15 R=${RCSID#* * }; VERSION=${R%% *}
16 PROGRAM=${0##*/}
17
18 . /etc/rc.d/init.d/functions
19 . /etc/geninitrd/functions
20 . /etc/sysconfig/system
21
22 COMPRESS=yes
23 USERAIDSTART=yes
24 USEMDADMSTATIC=no
25 USEINSMODSTATIC=no
26 USE_SUSPEND=yes
27 USE_TUXONICE=no
28 # it should be safe to remove scsi_mod from here, but I'm not sure...
29 PRESCSIMODS="-scsi_mod unknown -sd_mod"
30 PREIDEMODS="-ide-core unknown -ide-detect -ide-disk"
31 PREIDEMODSOLD="-ide-probe -ide-probe-mod -ide-disk"
32 target=""
33 kernel=""
34 force=""
35 verbose=""
36 MODULES=""
37 img_vers=""
38 fstab=/etc/fstab
39 insmod=insmod
40 modext=.o
41 rootdev_nr=0
42 # device node for rootfs from fstab
43 rootdev=""
44 # requires bootsplash package to operate
45 BOOT_SPLASH=no
46 # requires splashutils package to operate
47 FB_SPLASH=no
48 # requires splashutils package to operate
49 FB_CON_DECOR=no
50
51 # internal variables
52 # is /dev on tmpfs
53 dev_mounted=no
54 # is /proc mounted
55 proc_mounted=no
56 # is /sys mounted
57 sys_mounted=no
58 # is /tmp mounted on tmpfs
59 tmp_mounted=no
60
61 # are /dev nodes already created from /proc/devices info?
62 proc_partitions=no
63
64 # LVM devices that should not be included in vgscan on initrd
65 lvm_ignore_devices=''
66
67 # whether v86d should be installed
68 need_v86d=0
69
70 # LVM volumes that are needed to activate
71 # VG for root dev
72 ROOTVG=""
73 # VG for suspend resume dev
74 SUSPENDVG=""
75
76 # resume device
77 resume_dev=""
78
79 # if we should init NFS at boot
80 have_nfs=no
81 # if we should init LVM at boot
82 have_lvm=no
83 # if we should init md (softraid) at boot
84 have_md=no
85
86 usage() {
87         uname_r=$(uname -r)
88         echo "usage: $PROGRAM [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
89         echo "       [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
90         echo "       [--initrdfs=rom|initramfs|ext2|cram] [--modules-conf=<modules.conf>]"
91         echo "       [--with-raidstart] [--without-raidstart] [--with-insmod-static]"
92         echo "       [--with-bootsplash] [--without-bootsplash]"
93         echo "       [--with-fbsplash] [--without-fbsplash]"
94         echo "       [--with-fbcondecor] [--without-fbcondecor]"
95         echo "       [--lvmtoolsversion=1|2] [--with-udev] [--without-udev]"
96         echo "       [--with-suspend] [--without-suspend]"
97         echo "       [--with-tuxonice] [--without-tuxonice]"
98         echo "       [--without-dmraid] [--without-multipath]"
99         echo "       [--without-blkid] [--without-luks]"
100         echo "       <initrd-image> <kernel-version>"
101         echo ""
102         echo "example:"
103         echo "  $PROGRAM -f --initrdfs=rom /boot/initrd-$uname_r.gz $uname_r"
104         exit 0
105 }
106
107 msg() {
108         echo "$PROGRAM: $*"
109 }
110
111 warn() {
112         msg "WARNING: $*" >&2
113 }
114
115 debug() {
116         [ -n "$verbose" ] && msg "$*" >&2
117 }
118
119 # aborts program abnormally
120 die() {
121         local rc=${2:-1}
122         msg "ERROR: $1" 2>&2
123         exit $rc
124 }
125
126 # append text to /linuxrc
127 # takes STDIN as input
128 add_linuxrc() {
129         cat >> "$RCFILE"
130 }
131
132 # generate code to mount /dev on tmpfs and create initial nodes
133 # can be called multiple times. /dev is cleaned up (umounted) automatically at
134 # the end of script.
135 mount_dev() {
136     if [ "$INITRDFS" = "initramfs" ]; then
137                 # initramfs is read-write filesystem, no need for tmpfs
138                 return
139         fi
140
141         # we already generated tmpfs code; return
142         if is_yes "$dev_mounted"; then
143                 return
144         fi
145
146         dev_mounted=yes
147
148         busybox_applet mount mknod mkdir
149         add_linuxrc <<-EOF
150                 : 'Creating /dev'
151                 mount -o mode=0755 -t tmpfs none /dev
152                 mknod /dev/console c 5 1
153                 mknod /dev/null c 1 3
154                 mknod /dev/zero c 1 5
155                 mkdir /dev/pts
156                 mkdir /dev/shm
157         EOF
158 }
159
160 # generate code to mount /proc on initrd
161 # can be called multiple times
162 mount_proc() {
163         if is_yes "$proc_mounted"; then
164                 return
165         fi
166
167         proc_mounted=yes
168     if [ "$INITRDFS" = "initramfs" ]; then
169                 # /proc is mounted with initramfs 2.6.22.14 kernel
170                 # XXX: remove when it is clear why proc was already mounted
171                 echo "[ -f /proc/cmdline ] || mount -t proc none /proc" | add_linuxrc
172         else
173                 echo "mount -t proc none /proc" | add_linuxrc
174         fi
175 }
176
177 # generate code to mount /sys on initrd
178 # can be called multiple times
179 mount_sys() {
180         if is_yes "$sys_mounted"; then
181                 return
182         fi
183
184         sys_mounted=yes
185         echo "mount -t sysfs none /sys" | add_linuxrc
186 }
187
188 # generate code to mount /tmp on initrd
189 # can be called multiple times
190 mount_tmp() {
191     if [ "$INITRDFS" = "initramfs" ]; then
192                 # initramfs is read-write filesystem, no need for tmpfs
193                 return
194         fi
195
196         if is_yes "$tmp_mounted"; then
197                 return
198         fi
199
200         tmp_mounted=yes
201         echo "mount -t tmpfs none /tmp" | add_linuxrc
202 }
203
204 # unmount all mountpoints mounted by geninitrd
205 umount_all() {
206
207         add_linuxrc <<-'EOF'
208         : Last shell before umounting all and giving control over to real init.
209         debugshell
210         EOF
211
212         if is_yes "$dev_mounted"; then
213                 echo 'umount /dev' | add_linuxrc
214                 dev_mounted=no
215         fi
216         if is_yes "$proc_mounted"; then
217                 echo 'umount /proc' | add_linuxrc
218                 proc_mounted=no
219         fi
220         if is_yes "$sys_mounted"; then
221                 echo 'umount /sys' | add_linuxrc
222                 sys_mounted=no
223         fi
224         if is_yes "$tmp_mounted"; then
225                 echo 'umount /tmp' | add_linuxrc
226                 tmp_mounted=no
227         fi
228 }
229
230
231 # Checks if busybox has support for APPLET(s)
232 # Exits from geninitrd if the support is not present.
233 #
234 # NB! XXX do not output to STDOUT, it will appear in initrd images in some cases!
235 busybox_applet() {
236         local err=0
237
238         if [ -z "$busybox_functions" ]; then
239                 local tmp=$(/bin/initrd-busybox 2>&1)
240
241                 # BusyBox v1.1.3 says applet not found if it's not called 'busybox'.
242                 if [[ "$tmp" = *applet\ not\ found* ]]; then
243                         local t=$(mktemp -d)
244                         ln -s /bin/initrd-busybox $t/busybox
245                         local tmp=$($t/busybox 2>&1)
246                         rm -rf $t
247                 fi
248
249                 busybox_functions=$(echo "$tmp" | \
250                         sed -ne '/Currently defined functions:/,$p' | \
251                         xargs | sed -e 's,.*Currently defined functions: ,,'
252                 )
253         fi
254         for applet in $*; do
255                 local have
256                 # try cache
257                 eval have='$'busybox_have_$applet
258                 if [ -z "$have" ]; then
259                         have=$(echo "$busybox_functions" | egrep -c "( |^)$applet(,|$)")
260                         if [ "$have" = 0 ]; then
261                                 warn "This setup requires busybox-initrd compiled with applet '$applet' support"
262                                 err=1
263                         fi
264                         eval busybox_have_$applet=$have
265                 fi
266         done
267         if [ $err = 1 ]; then
268                 die "Aborted"
269         fi
270 }
271
272 # Finds module dependencies
273 #
274 # @param        $module
275 # @param        $mode   [silent]
276 #
277 # Outputs each dependant module full path including the module itself.
278 find_depmod() {
279         local module="$1"
280         local mode="$2"
281
282         # backwards compatible, process $mode
283         local modfile=$(modinfo -k $kernel -n $module 2>/dev/null)
284         if [ ! -f "$modfile" ]; then
285                 # module not found. try aliases!
286                 local alias aliases=$(modprobe_conf | awk -vm="$module" '$1 == "alias" && $2 == m { print $3 }')
287                 if [ "$aliases" ]; then
288                         for alias in $aliases; do
289                                 debug "Try $alias alias for $module"
290                                 find_depmod "$alias"
291                         done
292                         return
293                 fi
294
295                 if [ "$mode" != "silent" ]; then
296                         warn "$module: module not found for $kernel kernel"
297                 fi
298                 if ! is_no "$EXIT_IF_MISSING"; then
299                         exit 1
300                 else
301                         warn "If $module isn't compiled in kernel then this initrd may not start your system."
302                 fi
303         fi
304
305         # This works when user has module-init-tools installed even on 2.4 kernels
306         modprobe --set-version $kernel --show-depends $module | \
307         while read insmod modpath options; do
308                 echo $modpath
309         done
310 }
311
312 find_firmware() {
313         local module="$1"
314         echo -n $(modinfo -k $kernel -F firmware $module 2>/dev/null | xargs)
315 }
316
317 findmodule() {
318         local skiperrors=""
319         local modName=$1
320
321         if [ ${modName#-} != $modName ]; then
322                 skiperrors=1
323                 modName=${modName#-}
324         fi
325
326         # what's that?
327         if [ "$modName" = "pluto" ]; then
328                 findmodule fc4
329                 findmodule soc
330         fi
331         if [ "$modName" = "fcal" ]; then
332                 findmodule fc4
333                 findmodule socal
334         fi
335
336         local mod depmod
337         if [ "$skiperrors" = 1 ]; then
338                 depmod=$(find_depmod $modName silent)
339         else
340                 depmod=$(find_depmod $modName)
341                 if [ $? != 0 ]; then
342                         exit 1
343                 fi
344         fi
345
346         for mod in $depmod; do
347                 mod=${mod#/lib/modules/$kernel/}
348
349                 # add each module only once
350                 local m have=0
351                 for m in $MODULES; do
352                         [ $m = $mod ] && have=1
353                 done
354                 if [ $have = 0 ]; then
355                         MODULES="$MODULES $mod" 
356
357                         # if we are adding uvesafb, we need v86d as well
358                         if [ "$modName" = "uvesafb" ]; then
359                                 need_v86d=yes
360                         fi                              
361                 fi
362         done
363 }
364
365 # install a file to temporary mount image.
366 # it will operate recursively (copying directories)
367 # and will symlink destinations if source is symlink.
368 inst() {
369         if [ $# -lt 2 ]; then
370                 die 'Usage: inst <file> [<file>] <destination>'
371         fi
372
373         local src i=0 c=$(($# - 1))
374         while [ $i -lt $c ]; do
375                 src="$src $1"
376                 i=$((i + 1))
377                 shift
378         done
379         local dest=$1
380         set -- $src
381         parentDir=$(dirname $DESTDIR$dest)
382         [ ! -d "$parentDir" ] && (debug "+ mkdir -p $parentDir"; mkdir -p $parentDir)
383         debug "+ cp $* $DESTDIR$dest"
384         cp -HR "$@" "$DESTDIR$dest"
385 }
386
387 inst_d() {
388         if [ $# = 0 ]; then
389                 die 'Usage: inst_d <destination> <destination>'
390         fi
391         for dir in "$@"; do
392                 install -d "$DESTDIR$dir"
393         done
394 }
395
396 # install executable and it's shared libraries
397 inst_exec() {
398         if [ $# -lt 2 ]; then
399                 die 'Usage: inst_exec <file>[, <file>] <destination>'
400         fi
401         local src i=0 c=$(($# - 1))
402         while [ $i -lt $c ]; do
403                 src="$src $1"
404                 i=$((i + 1))
405                 shift
406         done
407         local dest=$1
408         set -- $src
409
410         inst "$@" $dest
411
412         local lib libs=$(ldd "$@" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
413         for lib in $libs; do
414                 if [ ! -f "$DESTDIR/$_lib/${lib##*/}" ]; then
415                         inst_d /$_lib
416                         inst_exec $lib /$_lib
417                 fi
418         done
419
420         # hack for uclibc linked binaries requiring this fixed path
421         # XXX: shouldn't rpath be used here instead so th
422         if [ -f $DESTDIR/$_lib/libc.so.0 ]; then
423                 local lib=$DESTDIR/$_lib/libc.so.0
424                 lib=$(ldd "$lib" | awk '/statically|linux-(gate|vdso)\.so/{next} NF == 2 {print $1} /=/{print $3}' | sort -u)
425                 local libdir=$(cd $(dirname "$lib"); pwd)
426                 if [ ! -e $DESTDIR$libdir ]; then
427                         libdir=$(dirname "$libdir")
428                         inst_d $libdir
429                         debug "+ ln -s /$_lib $DESTDIR$libdir"
430                         ln -s /$_lib $DESTDIR$libdir
431                 fi
432         fi
433 }
434
435 # output modules.conf / modprobe.conf
436 modprobe_conf() {
437         echo "$modprobe_conf_cache"
438 }
439
440 #
441 # defaults to modprobe -c if not told otherwise, this means include statements
442 # work from there.
443 cache_modprobe_conf() {
444         if [ "$pack_version" -lt "002005" ]; then
445                 modulefile=/etc/modules.conf
446                 if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
447                         modulefile=/etc/conf.modules
448                 fi
449         fi
450
451         if [ -n "$modulefile" ]; then
452                 debug "Using $modulefile for modules config"
453                 modprobe_conf_cache=$(cat $modulefile)
454         else
455                 debug "Using modprobe -c to get modules config"
456                 modprobe_conf_cache=$(modprobe -c --set-version $kernel)
457         fi
458 }
459
460 find_modules_md() {
461         local found raidlevel
462
463         if [ -f /etc/mdadm.conf ]; then
464                 debug "Finding RAID details using mdadm for rootdev=$1"
465                 eval `/sbin/mdadm -v --examine --scan --config=/etc/mdadm.conf | awk -v rootdev="$1" '
466                 BEGIN {
467                         found = "no";
468                         dev_list = "";
469                         raidlevel = ""
470                         rootdev_devfs = rootdev;
471                         if (rootdev ~ /\/dev\/md\/[0-9]/) {
472                                 gsub(/\/dev\/md\//,"/dev/md",rootdev_devfs);
473                         }
474                 }
475
476                 /^ARRAY/ {
477                         if (($2 == rootdev) || ($2 == rootdev_devfs)) {
478                                 raidlevel=$3;
479                                 gsub(/level=/,NUL,raidlevel);
480                                 if (raidlevel ~ /^raid([0-6]|10)/) {
481                                         gsub(/raid/,NUL,raidlevel);
482                                 };
483                                 found="yes";
484                                 getline x;
485                                 if (x ~ /devices=/) {
486                                         dev_list = x;
487                                         gsub(".*devices=", NUL, dev_list);
488                                         gsub(",", " ", dev_list);
489                                 }
490                         }
491                 }
492
493                 END {
494                         print "have_md=" found;
495                         print "raidlevel=" raidlevel;
496                         print "dev_list=\"" dev_list "\"";
497                 }'`
498         fi
499
500         if [ "$have_md" != "yes" -a -f /etc/raidtab ]; then
501                 die "raidtools are not longer supported. Please migrate to mdadm setup!"
502         fi
503
504         if is_yes "$have_md"; then
505                 case "$raidlevel" in
506                 [01]|10)
507                         findmodule "raid$raidlevel"
508                         ;;
509                 [456])
510                         findmodule "-raid$raidlevel"
511                         findmodule "-raid456"
512                         ;;
513                 linear)
514                         findmodule "linear"
515                         ;;
516                 *)
517                         warn "raid level $number (in mdadm config) not recognized"
518                         ;;
519                 esac
520         else
521                 die "RAID devices not found for \"$1\", check your configuration!"
522         fi
523
524         rootdev_nr=$(( $rootdev_nr + 1 ))
525         eval "rootdev${rootdev_nr}=\"$1\""
526         eval "dev_list${rootdev_nr}=\"${dev_list}\""
527
528         for device in $dev_list; do
529                 find_modules_for_devpath $device
530         done
531 }
532
533 find_modules_scsi() {
534         local n
535         for n in $PRESCSIMODS; do
536                 if [ "X$n" = "Xunknown" ]; then
537                         debug "Finding SCSI modules using scsi_hostadapter"
538                         local mod scsimodules=$(modprobe_conf | awk '/scsi_hostadapter/ && ! /^[\t ]*#/ { print $3; }')
539                         for mod in $scsimodules; do
540                                 # for now allow scsi modules to come from anywhere. There are some
541                                 # RAID controllers with drivers in block
542                                 findmodule "$mod"
543                         done
544                 else
545                         findmodule "$n"
546                 fi
547         done
548         findmodule "-scsi_wait_scan"
549 }
550
551 find_modules_ide() {
552         local devpath=$1
553         # remove partition, if any
554         local disk=${devpath%[0-9]*}
555         # set blockdev for rootfs (hda, sdc, ...)
556         local rootblkdev=${disk#/dev/}
557
558         local n
559         if [ "$pack_version_long" -lt "002004021" ]; then
560                 debug "Finding IDE modules for kernels <= 2.4.20"
561                 for n in $PREIDEMODSOLD; do
562                         findmodule "$n"
563                 done
564         else
565                 local tryauto=1
566                 for n in $PREIDEMODS; do
567                         if [ "X$n" = "Xunknown" ]; then
568                                 debug "Finding IDE modules using ide_hostadapter"
569                                 local mod idemodules=$(modprobe_conf | awk '/ide_hostadapter/ && ! /^[\t ]*#/ { print $3; }')
570                                 for mod in $idemodules; do
571                                         tryauto=0;
572                                         findmodule "$mod"
573                                 done
574
575                                 if [ "$tryauto" -eq 1 ]; then
576                                         # If tryauto {{{
577                                         if [ -r /usr/share/pci-database/ide.pci -a -r /proc/bus/pci/devices ]; then
578                                                 debug "Finding IDE modules using PCI ID database"
579                                                 # Finding IDE modules using PCI ID database {{{
580                                                 if is_yes "${ide_only_root}"; then
581                                                         if [ -f /sys/block/${rootblkdev}/device/../../vendor -a -f /sys/block/${rootblkdev}/device/../../device ]; then
582                                                                 vendorid="$(awk ' { gsub(/0x/,NUL); print $0 } ' /sys/block/${rootblkdev}/device/../../vendor)"
583                                                                 deviceid="$(awk ' { gsub(/0x/,NUL); print $0 } ' /sys/block/${rootblkdev}/device/../../device)"
584                                                                 searchpciid="${vendorid}${deviceid}"
585                                                         elif [ -f /proc/ide/${rootblkdev}/../config ]; then
586                                                                 searchpciid="$(awk ' /pci bus/ { print $7$9 } ' /proc/ide/${rootblkdev}/../config)"
587                                                         fi
588                                                 fi
589
590                                                 if [ -z "${searchpciid}" ]; then
591                                                         searchpciid="$(awk ' { print $2 } ' /proc/bus/pci/devices)"
592                                                 fi
593
594                                                 idemodules=""
595
596                                                 for nb in $searchpciid; do
597                                                         eval `awk -v pciid="$nb" '{
598                                                                 gsub("\t"," ");
599                                                                 gsub("  +", " ");
600                                                                 gsub("^ ","");
601                                                                 if (/^[\t ]*#/)
602                                                                         next;
603                                                                 compmod = $1 "";        # make sure comparison type will be string
604                                                                                         # cause pci IDs are hexadecimal numeric
605                                                                 if (compmod == pciid) {
606                                                                         module=$2;
607                                                         #               min_kernel=$3;  # now in ide.pci $3,$4 = vendor and device name
608                                                         #               max_kernel=$4;  #
609                                                                         exit 0;
610                                                                 }
611                                                         }
612
613                                                         END {
614                                                                 print "module=" module "\nmin_kernel=" min_kernel "\nmax_kernel=\"" max_kernel "\"\n";
615                                                         }' /usr/share/pci-database/ide.pci`
616                                                         [ -n "$module" ] && idemodules="$idemodules $module"
617                                                 done
618                                                 if is_yes "$(awk ' /ide=reverse/ { print "yes" } ' /proc/cmdline)"; then
619                                                         new_idemodules=""
620                                                         for nc in idemodules; do
621                                                                 new_idemodules="$nc $new_idemodules"
622                                                         done
623                                                         idemodules="${new_idemodules}"
624                                                 fi
625
626                                                 if [ -z "$idemodules" ]; then
627                                                         warn "rootfs on IDE device but no related modules found, loading ide-generic."
628                                                         idemodules="ide-generic"
629                                                 fi
630
631                                                 # }}}
632                                                 for nd in $idemodules; do
633                                                         findmodule "-$nd"
634                                                 done
635                                         # }}}
636                                         # else tryauto {{{
637                                         else
638                                                 [ -r /usr/share/pci-database/ide.pci ] || warn "/usr/share/pci-database/ide.pci missing."
639                                                 [ -r /proc/bus/pci/devices ] || warn "/proc/bus/pci/devices missing."
640                                                 warn "Automatic IDE modules finding not available."
641                                         fi
642                                         # }}}
643                                 fi
644                         else
645                                 findmodule "$n"
646                         fi
647                 done
648         fi
649 }
650
651 # return true if node is lvm node
652 is_lvm_node() {
653         local node="$1"
654         if [ ! -e "$node" ]; then
655                 warn "check_lvm(): node $node doesn't exist!"
656                 return 1
657         fi
658
659         # block-major-58 is lvm1
660         ls -lL "$node" 2> /dev/null | awk '{if (/^b/) { if ($5 == "58,") { exit 0; } else { exit 1; } } else { exit 1; }}'
661         rc=$?
662
663         if [ $rc = 0 ]; then
664                 debug "LVM check: $node is LVM v1 node"
665                 # is lvm1
666                 return 0
667         fi
668
669         if [ ! -x /sbin/lvm ]; then
670                 debug "LVM check: no lvm2 tools present to check"
671                 return 0
672         fi
673
674         /sbin/lvm lvdisplay "$node" > /dev/null 2>&1
675         rc=$?
676         if [ $rc -gt 127 ]; then
677                 # lvdisplay terminated by signal! most likely it segfaulted.
678                 die "Unexpected exit from 'lvdisplay $node': $rc - are your lvm tools broken?"
679         fi
680
681         if [ $rc = 0 ]; then
682                 debug "LVM check: $node is LVM v2 node"
683         else
684                 debug "LVM check: $node is not any LVM node"
685         fi
686         return $rc
687 }
688
689
690 # return dependencies MAJOR:MINOR [MAJOR:MINOR] for DM_NAME
691 # TODO: patch `dmsetup export`
692 dm_deps() {
693         local dm_name="$1"
694         dmsetup deps $dm_name | sed -e 's/, /:/g;s/^.\+ dependencies[^:]: //;s/[()]//g;'
695 }
696
697 # export info from dmsetup
698 # param can be:
699 # - MAJOR:MINOR
700 # - /dev/dm-MINOR
701 # - /dev/mapper/DM_NAME
702 dm_export() {
703         local arg="$1"
704
705         case "$arg" in
706         *:*)
707                 local maj=${arg%:*} min=${arg#*:}
708                 dmsetup -j $maj -m $min export
709                 ;;
710         /dev/dm-*)
711                 local min=${arg#*dm-}
712                 local maj=$(awk '$2 == "device-mapper" {print $1}' /proc/devices)
713                 dm_export $maj:$min
714                 ;;
715         /dev/mapper/*)
716                 local dm_name=${arg#/dev/mapper/}
717                 dmsetup export $dm_name
718                 ;;
719         *)
720                 die "dm_export: unexpected $arg"
721                 ;;
722         esac
723 }
724
725 # find modules for $devpath
726 find_modules_for_devpath() {
727         local devpath="$1"
728         if [ -z "$devpath" ]; then
729                 die "No argument passed to find_modules_for_devpath() - is your /etc/fstab correct?"
730         fi
731
732         # /dev/dm-3 -> /dev/mapper/sil_ahbgadcbchfc3
733         case "$devpath" in
734         /dev/dm-*)
735                 devpath=$(dm_longname "$devpath")
736                 ;;
737         esac
738         debug "Finding modules for device path $devpath"
739
740         if is_luks "$devpath"; then
741                 find_modules_luks "$devpath"
742                 return
743         fi
744
745         if is_yes "`echo "$devpath" | awk '/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:|\/dev\/nfs)/ { print "yes"; }'`"; then
746                 if [ ! -x /usr/bin/pcidev -a -z "$NFS_ETH_MODULES" ]; then
747                         die "root on NFS but /usr/bin/pcidev not found. Please install correct pci-database package and rerun $PROGRAM."
748                 fi
749                 if [ ! -f /proc/bus/pci/devices ]; then
750                         warn "Remember to add network card modules in /etc/sysconfig/geninitrd, example:"
751                         warn "BASICMODULES=\"e1000 ne2k-pci mii 8139too 3c59x\""
752                 else
753                         local m
754                         [ -z "$NFS_ETH_MODULES" ] && NFS_ETH_MODULES=$(/usr/bin/pcidev /m net | xargs)
755                         warn "NOTE: Network card(s) module(s) $NFS_ETH_MODULES is for this machine"
756                         for m in $NFS_ETH_MODULES; do
757                                 findmodule "$m"
758                         done
759                 fi
760                 findmodule "-ipv4"
761                 findmodule "nfs"
762                 have_nfs=yes
763                 warn "Remember to use \`root=/dev/ram0 init=/linuxrc' when starting kernel"
764                 warn "or you will have problems like init(xx) being child process of swapper(1)."
765                 return
766         fi
767
768         if [[ "$devpath" == /dev/md* ]]; then
769                 find_modules_md "$devpath"
770                 return
771         fi
772
773         if is_multipath "$devpath"; then
774                 if find_modules_multipath "$devpath"; then
775                         return
776                 fi
777                 # fallback
778         fi
779
780         if is_dmraid "$devpath"; then
781                 if find_modules_dmraid "$devpath"; then
782                         return
783                 fi
784                 # fallback
785         fi
786
787         if is_yes "$(echo "$devpath" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')"; then
788                 find_modules_scsi
789                 return
790         fi
791
792         if is_yes "$(echo "$devpath" | awk '/^\/dev\/(hd[a-l]|ide\/)/ { print "yes"; }')"; then
793                 find_modules_ide "$devpath"
794                 return
795         fi
796
797         if [[ "$devpath" == /dev/rd/* ]]; then
798                 findmodule "DAC960"
799                 rootdev_add="/dev/rd/"
800                 return
801         fi
802
803         if [[ "$devpath" == /dev/ida/* ]]; then
804                 findmodule "cpqarray"
805                 rootdev_add="/dev/ida/"
806                 return
807         fi
808
809         if [[ "$devpath" == /dev/cciss/* ]]; then
810                 findmodule "cciss"
811                 rootdev_add="/dev/cciss/"
812                 return
813         fi
814
815         if [[ "$devpath" == /dev/ataraid/* ]]; then
816                 find_modules_ide
817                 findmodule "ataraid"
818                 ataraidmodules=$(modprobe_conf | awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }')
819                 if [ -n "$ataraidmodules" ]; then
820                         # FIXME: think about modules compiled in kernel
821                         die "ataraid_hostadapter alias not defined in modprobe.conf! Please set it and run $PROGRAM again."
822                 fi
823                 for n in $ataraidmodules; do
824                         findmodule "$n"
825                 done
826                 rootdev_add="/dev/ataraid/"
827                 return
828         fi
829
830         # check to see if we need to set up a loopback filesystem
831         if [[ "$devpath" == /dev/loop*  ]]; then
832                 die "Sorry, root on loop device isn't supported."
833                 # TODO: rewrite for bsp and make nfs ready
834                 if [ ! -x /sbin/losetup ]; then
835                         die "losetup is missing"
836                 fi
837                 key="^# $(echo $devpath | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
838                 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`"; then
839                         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"
840                 fi
841
842                 line="`awk '/'$key'/ { print $0; }' $fstab`"
843                 loopDev="$(echo $line | awk '{print $3}')"
844                 loopFs="$(echo $line | awk '{print $4}')"
845                 loopFile="$(echo $line | awk '{print $5}')"
846
847                 BASICMODULES="$BASICMODULES -loop"
848                 findmodule "-$loopFs"
849                 BASICMODULES="$BASICMODULES -${loopFs}"
850                 return
851         fi
852
853         if is_lvm_node "$devpath"; then
854                 node="$devpath"
855
856                 if [ ! -f /sbin/initrd-lvm -o ! -x /sbin/lvdisplay -o ! -x /sbin/pvdisplay ]; then
857                         die "root on LVM but /sbin/initrd-lvm, /sbin/lvdisplay and /sbin/pvdisplay not found. Please install lvm(2) and lvm(2)-initrd package and rerun $PROGRAM."
858                 fi
859                 if [ -z "$LVMTOOLSVERSION" ]; then
860                         LVMTOOLSVERSION=$(/sbin/initrd-lvm vgchange --version 2>/dev/null|head -n 1|awk '{gsub("vgchange: Logical Volume Manager ",NIL); gsub("LVM version:     ",NIL); gsub(/\..*/,NIL); print $1}')
861                         if [ -z "$LVMTOOLSVERSION" ]; then
862                                 die "Can't determine LVM tools version. Please set LVMTOOLSVERSION and rerun $PROGRAM."
863                         fi
864                 fi
865
866                 local vg=$(/sbin/lvdisplay -c "$node" 2> /dev/null | awk -F":" ' { print $2 } ')
867                 VGVOLUMES=$(echo $VGVOLUMES $vg | tr ' ' '\n' | sort -u)
868                 local pv=$(/sbin/pvdisplay -c 2>/dev/null | awk -F":" -v vg="$vg" ' BEGIN { devices="" } { if ($2 == vg) { devices = devices " " $1 } } END { print devices } ')
869                 PVDEVICES=$(echo $PVDEVICES $pv | tr ' ' '\n' | sort -u)
870
871                 if [ -n "$PVDEVICES" ]; then
872                         for device in $PVDEVICES; do
873                                 find_modules_for_devpath $device
874                         done
875                 else
876                         die "I wasn't able to find PV (via lvdisplay and pvdisplay). You can try to set PVDEVICES in /etc/sysconfig/geninitrd."
877                 fi
878                 if [ "$LVMTOOLSVERSION" = "2" ]; then
879                         findmodule "-dm-mod"
880                 elif [ "$LVMTOOLSVERSION" = "1" ]; then
881                         findmodule "-lvm"
882                         findmodule "-lvm-mod"
883                 else
884                         die "LVM version $LVMTOOLSVERSION is not supported yet."
885                 fi
886                 debug "LVM v$LVMTOOLSVERSION enabled"
887                 have_lvm=yes
888                 return
889         fi
890 }
891
892 firmware_install_module() {
893         local module="$1"
894         local firmware_files="$2"
895
896         debug "Adding Firmwares ($firmware_files) to initrd for module $module"
897         # firmware not yet installed
898         if [ ! -f "$DESTDIR/lib/firmware/firmware.sh" ]; then
899                 inst_d /lib/firmware
900 cat << 'EOF' >> "$DESTDIR/lib/firmware/firmware.sh"
901 #!/bin/sh -e
902 echo 1 > /sys$DEVPATH/loading
903 cat "/lib/firmware/$FIRMWARE" > /sys$DEVPATH/data
904 echo 0 > /sys$DEVPATH/loading
905 exit 0
906 EOF
907                 chmod 755 "$DESTDIR/lib/firmware/firmware.sh"
908         fi
909
910         for firmware in $firmware_files; do
911                 if [ ! -f "/lib/firmware/$firmware" ]; then
912                                 die "firmware file /lib/firmware/$firmware doesn't exist."
913                 else
914                         FIRMWAREDIR=${firmware%/*}
915                         [ "$FIRMWAREDIR" != "$firmware" ] && inst_d /lib/firmware/$FIRMWAREDIR
916                         inst /lib/firmware/$firmware /lib/firmware/$firmware
917                 fi
918         done
919
920         mount_sys
921         echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" | add_linuxrc
922 }
923
924 modules_install() {
925         local modules="$1"
926         local mod
927
928         for mod in $modules; do
929                 MODULEDIR=${mod%/*}
930                 inst_d "/lib/modules/$kernel/$MODULEDIR"
931                 cp -a "/lib/modules/$kernel/$mod" "$DESTDIR/lib/modules/$kernel/$mod"
932                 gunzip "$DESTDIR/lib/modules/$kernel/$mod" 2> /dev/null
933         done
934 }
935
936 modules_add_linuxrc() {
937         local mod modpath
938
939         for mod in "$@"; do
940                 # module path without optional compression
941                 modpath=${mod%.gz}
942
943                 # name of the module
944                 module=${modpath##*/}
945                 module=${module%$modext}
946
947                 options=$(modprobe_conf | awk -vmodule="$module" '{ if ($1 == "options" && $2 == module) { for(i=3;i<=NF;i++) printf("%s ",$i); }}' | xargs)
948
949                 generic_module=$(echo $module | tr - _)
950                 sleep_var=$(eval echo \$MODULE_${generic_module}_USLEEP)
951                 firmware_var=$(eval echo \$MODULE_${generic_module}_FIRMWARE)
952
953                 if [ -n "$verbose" ]; then
954                         s=""
955                         if [ "$options" ]; then
956                                 s="$s with options [$options]"
957                         fi
958                         if [ "$sleep_var" ]; then
959                                 s="$s and $sleep_var usleep"
960                         fi
961                         debug "Loading module [$module]$s"
962                 fi
963
964                 if [ -n "$firmware_var" ]; then
965                         firmware_install_module "$module" "$firmware_var"
966                 else
967                         firmware_files=$(find_firmware "$module")
968                         if [ -n "$firmware_files" ]; then
969                                 for file in $firmware_files; do
970                                         firmware_install_module "$module" "$file"
971                                 done
972                         fi
973                 fi
974
975                 echo "$insmod /lib/modules/$kernel/$modpath $options" | add_linuxrc
976                 if [ -n "$sleep_var" ]; then
977                         echo "usleep $sleep_var" | add_linuxrc
978                 fi
979         done
980 }
981
982 find_modules_suspend() {
983         if [ ! -x /usr/${_lib}/suspend/resume -a ! -x /usr/sbin/resume ]; then
984                 die "/usr/${_lib}/suspend/resume is missing!"
985         fi
986         resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
987
988         local vgvolumes=$VGVOLUMES
989         find_modules_for_devpath $resume_dev
990
991         if [ "$VGVOLUMES" != "$vgvolumes" ]; then
992                 # save our VG to be able to differenciate between rootfs VG
993                 SUSPENDVG=$VGVOLUMES
994                 VGVOLUMES=$vgvolumes
995                 debug "suspend device is on LVM"
996         fi
997 }
998
999 initrd_gen_suspend() {
1000         mknod $DESTDIR/dev/snapshot c 10 231
1001         mkdir -p $DESTDIR${resume_dev%/*}
1002         inst $resume_dev $resume_dev
1003
1004         inst /etc/suspend.conf /etc/suspend.conf
1005         if [ -x /usr/${_lib}/suspend/resume ]; then
1006                 inst_exec /usr/${_lib}/suspend/resume /bin/resume
1007         else
1008                 inst_exec /usr/sbin/resume /bin/resume
1009         fi
1010
1011         add_linuxrc <<-'EOF'
1012         resume=no
1013         for arg in $CMDLINE; do
1014                 if [ "${arg##resume=}" != "${arg}" ]; then
1015                         resume=yes
1016                 fi
1017         done
1018         if [ "$resume" = "yes" ]; then
1019                 resume
1020         fi
1021         EOF
1022 }
1023
1024 initrd_gen_tuxonice() {
1025         mount_sys
1026         add_linuxrc <<-'EOF'
1027                 resume=no
1028                 for arg in $CMDLINE; do
1029                         if [ "${arg##resume=}" != "${arg}" -o "${arg##resume2=}" != "${arg}" ]; then
1030                                 resume=yes
1031                         fi
1032                 done
1033                 if [ "$resume" = "yes" ]; then
1034                         [ -e /proc/suspend2/do_resume ] && echo > /proc/suspend2/do_resume
1035                         [ -e /sys/power/suspend2/do_resume ] && echo > /sys/power/suspend2/do_resume
1036                         [ -e /sys/power/tuxonice/do_resume ] && echo > /sys/power/tuxonice/do_resume
1037                 fi
1038         EOF
1039 }
1040
1041 initrd_gen_v86d() {
1042         debug "initrd_gen_v86d"
1043         mknod $DESTDIR/dev/mem c 1 1
1044         mknod $DESTDIR/dev/tty1 c 4 1
1045         inst_d /sbin
1046         inst_exec /sbin/v86d /sbin
1047 }
1048
1049 initrd_gen_udev() {
1050         debug "Setting up udev..."
1051         inst_d /sbin /etc/udev
1052
1053         if [ ! -x /sbin/initrd-udevd ]; then
1054                 die "/sbin/initrd-udevd not present"
1055         fi
1056
1057         inst_exec /sbin/initrd-udevd /sbin/udevd
1058         inst /etc/udev/udev.conf /etc/udev/udev.conf
1059
1060         mount_dev
1061         mount_sys
1062         add_linuxrc <<-'EOF'
1063                 : 'Starting udev'
1064                 /sbin/udevd --daemon
1065         EOF
1066
1067         inst_exec /sbin/initrd-udevadm /sbin/udevadm
1068         add_linuxrc <<-'EOF'
1069                 /sbin/udevadm trigger
1070                 /sbin/udevadm settle
1071         EOF
1072
1073 }
1074
1075 initrd_gen_stop_udevd() {
1076         busybox_applet killall
1077         add_linuxrc     <<-'EOF'
1078                 killall udevd
1079         EOF
1080 }
1081
1082
1083
1084 initrd_gen_bootsplash() {
1085         local target="$1"
1086
1087         debug "Generating bootsplash"
1088
1089         if [ ! -x /bin/splash.bin ]; then
1090                 warn "Failed to execute /bin/splash.bin. Is bootsplash package installed?"
1091                 return
1092         fi
1093
1094         if [ -r /etc/sysconfig/bootsplash ]; then
1095                 . /etc/sysconfig/bootsplash
1096         fi
1097
1098         if [ -z "$THEME" ]; then
1099                 warn "Please configure your /etc/sysconfig/bootsplash first."
1100                 warn "Generating bootsplashes skipped."
1101                 return
1102         fi
1103
1104         if [ -z "$BOOT_SPLASH_RESOLUTIONS" ]; then
1105                 warn "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash."
1106                 warn "Not adding bootsplash to initrd."
1107         fi
1108
1109         for res in $BOOT_SPLASH_RESOLUTIONS; do
1110                 if [ -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
1111                         /bin/splash.bin -s -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> "$target" && \
1112                         debug "Added $res $THEME theme to initrd."
1113                 else
1114                         warn "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped"
1115                 fi
1116         done
1117 }
1118
1119 initrd_gen_fbsplash() {
1120         debug "Generating fbsplash"
1121
1122         if [ ! -x /usr/bin/splash_geninitramfs -a ! -x /usr/sbin/splash_geninitramfs ]; then
1123                 warn "Failed to find splash_geninitramfs. Is splashutils package installed?"
1124                 return
1125         fi
1126         splash_geninitramfs_bin=/usr/sbin/splash_geninitramfs
1127         [ -f /usr/bin/splash_geninitramfs ] && splash_geninitramfs_bin=/usr/bin/splash_geninitramfs
1128
1129         if [ -r /etc/sysconfig/fbsplash ]; then
1130                 . /etc/sysconfig/fbsplash
1131         fi
1132
1133         if [ -z "$SPLASH_THEME" ]; then
1134                 warn "Please configure your /etc/sysconfig/fbsplash first."
1135                 warn "Generating fbsplashes skipped."
1136                 return
1137         fi
1138
1139         if [ -z "$FB_SPLASH_RESOLUTIONS" ]; then
1140                 warn "No FB_SPLASH_RESOLUTIONS specified in /etc/sysconfig/fbsplash."
1141                 warn "Not adding fbsplash to initramfs."
1142                 return
1143         fi
1144
1145         for res in $FB_SPLASH_RESOLUTIONS; do
1146                 if [ -f "/etc/splash/$SPLASH_THEME/$res.cfg" ]; then
1147                         $splash_geninitramfs_bin -c $DESTDIR -r $res $SPLASH_THEME && \
1148                         debug "Added $res $SPLASH_THEME theme to initramfs."
1149                 else
1150                         warn "/etc/splash/$SPLASH_THEME/$res.cfg doesn't exist, skipped"
1151                 fi
1152         done
1153 }
1154
1155 initrd_gen_fbcondecor() {
1156         debug "Generating fbcondecor"
1157
1158         if [ ! -x /usr/bin/splash_geninitramfs -a ! -x /usr/sbin/splash_geninitramfs ]; then
1159                 warn "Failed to find splash_geninitramfs. Is splashutils package installed?"
1160                 return
1161         fi
1162         splash_geninitramfs_bin=/usr/sbin/splash_geninitramfs
1163         [ -f /usr/bin/splash_geninitramfs ] && splash_geninitramfs_bin=/usr/bin/splash_geninitramfs
1164
1165         if [ -r /etc/sysconfig/splash ]; then
1166                 . /etc/sysconfig/splash
1167         fi
1168
1169         if [ -z "$SPLASH_THEME" ]; then
1170                 warn "Please configure your /etc/sysconfig/splash first."
1171                 warn "Generating of splashes skipped."
1172                 return
1173         fi
1174
1175         if [ -z "$FB_SPLASH_RESOLUTIONS" ]; then
1176                 warn "No FB_SPLASH_RESOLUTIONS specified in /etc/sysconfig/splash."
1177                 warn "Not adding fbcondecor to initramfs."
1178                 return
1179         fi
1180
1181         for res in $FB_SPLASH_RESOLUTIONS; do
1182                 if [ -f "/etc/splash/$SPLASH_THEME/$res.cfg" ]; then
1183                         $splash_geninitramfs_bin -c $DESTDIR -r $res $SPLASH_THEME && \
1184                         debug "Added $res $SPLASH_THEME theme to initramfs."
1185                 else
1186                         warn "/etc/splash/$SPLASH_THEME/$res.cfg doesn't exist, skipped"
1187                 fi
1188         done
1189 }
1190
1191 # Generates /dev nodes based on /proc/partitions information.
1192 # Needs /proc mounted.
1193 # Can be called multiple times.
1194 initrd_gen_devices() {
1195         if is_yes "$proc_partitions"; then
1196                 return
1197         fi
1198         proc_partitions=yes
1199
1200         mount_dev
1201         add_linuxrc <<-'EOF'
1202                 : 'Making device nodes'
1203                 cat /proc/partitions | (
1204                         # ignore first two lines: header, empty line
1205                         read b; read b
1206
1207                         while read major minor blocks dev rest; do
1208                                 node=/dev/$dev
1209                                 mkdir -p ${node%/*}
1210                                 [ -e $node ] || mknod $node b $major $minor
1211                         done
1212                 )
1213         EOF
1214 }
1215
1216 initrd_gen_md() {
1217         debug "Setting up mdadm..."
1218
1219         if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ]; then
1220                 die "/sbin/mdadm or /sbin/initrd-mdassemble is missing!"
1221         fi
1222
1223         inst_exec /sbin/initrd-mdassemble /bin/mdassemble
1224
1225         # LVM on RAID case
1226         dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf | xargs)
1227         if [ "$dev_list_extra" = "partitions" ]; then
1228                 # FIXME: handle this case (see man mdadm.conf)
1229                 echo "DEVICE partitions" >> "$DESTDIR/etc/mdadm.conf"
1230                 dev_list_extra=""
1231         else
1232                 for ex_dev in $dev_list_extra; do
1233                         echo "DEVICE $ex_dev" >> "$DESTDIR/etc/mdadm.conf"
1234                 done
1235         fi
1236         do_md0=1
1237         for nr in `seq 1 $rootdev_nr`; do
1238                 eval cr_rootdev="\$rootdev${nr}"
1239                 eval cr_dev_list="\$dev_list${nr}"
1240                 debug echo "Setting up array ($cr_rootdev = $cr_dev_list)"
1241
1242                 [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
1243
1244                 echo "DEVICE $cr_dev_list" >> "$DESTDIR/etc/mdadm.conf"
1245                 cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
1246                 cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev | awk ' { gsub(/spares=[0-9]+/, "", $0); print $0; }')
1247                 if [ -n "$cr_md_conf" ]; then
1248                         echo "$cr_md_conf" >> "$DESTDIR/etc/mdadm.conf"
1249                 else
1250                         echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$DESTDIR/etc/mdadm.conf"
1251                 fi
1252
1253                 for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
1254                         # mkdir in case of devfs name
1255                         inst_d $(dirname $f)
1256                         [ -e "$DESTDIR/$f" ] && continue
1257                         # this works fine with and without devfs
1258                         inst $f $f
1259                 done
1260         done
1261
1262         echo "mdassemble" | add_linuxrc
1263
1264         # needed to determine md-version
1265         if [ "$do_md0" -eq 1 ]; then
1266                 mknod $DESTDIR/dev/md0 b 9 0
1267         fi
1268 }
1269
1270 initrd_gen_nfs() {
1271         # use root=/dev/ram0 init=/linuxrc when starting kernel or you will
1272         # have problems like init(XX) being child process of swapper(1).
1273         debug "Adding rootfs on NFS support to initrd (dhcp)"
1274         mknod "$DESTDIR/dev/urandom" c 1 9
1275         mkdir "$DESTDIR/newroot"
1276         add_linuxrc <<-'EOF'
1277                 ifconfig lo 127.0.0.1 up
1278                 route add -net 127.0.0.0 mask 255.0.0.0 lo
1279                 ifconfig eth0 up
1280                 udhcpc -i eth0 -f -q -s /bin/setdhcp
1281
1282                 cd /newroot
1283                 pivot_root . initrd
1284                 [ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1
1285                 exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1
1286         EOF
1287
1288         cat <<-'EOF' > "$DESTDIR/bin/setdhcp"
1289                 #!/bin/sh
1290                 [ "$1" != "bound" ] && exit
1291                 [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
1292                 [ -n "$subnet" ] && NETMASK="netmask $subnet"
1293                 ifconfig $interface $ip $BROADCAST $NETMASK up
1294                 if [ -n "$router" ]; then
1295                         for r in $router; do
1296                                 route add default gw $r dev $interface
1297                         done
1298                 fi
1299
1300                 for o in $CMDLINE; do
1301                         case $o in
1302                         nfsroot=*)
1303                                 rootpath=${o#nfsroot=}
1304                                 ;;
1305                         esac
1306                 done
1307
1308                 if [ -n "$rootpath" ]; then
1309                         mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
1310                 else
1311                         echo "Missing rootpath in what DHCP server sent to us. Failing..."
1312                         echo "All seen variables are listed below:"
1313                         set
1314                 fi
1315         EOF
1316
1317         chmod 755 "$DESTDIR/bin/setdhcp"
1318 }
1319
1320 initrd_gen_lvm() {
1321         debug "Adding LVM support to initrd"
1322         inst_d /tmp /newroot
1323         inst_exec /sbin/initrd-lvm /bin/lvm.static
1324
1325         # always make /dev on tmpfs for LVM2
1326         if [ "$LVMTOOLSVERSION" = "2" ]; then
1327                 mount_dev
1328         fi
1329
1330         if ! is_yes "$dev_mounted"; then
1331                 inst_d /dev/mapper
1332                 mknod $DESTDIR/dev/mapper/control c 10 63
1333                 for device in $PVDEVICES; do
1334                         # if LVM on RAID then device might be copied already in gen_md
1335                         [ -e "$DESTDIR/dev/$(basename $device)" ] && continue
1336                         inst $device /dev
1337                 done
1338         fi
1339
1340         mount_tmp
1341         if [ "$LVMTOOLSVERSION" = "1" ]; then
1342                 add_linuxrc <<-EOF
1343                         lvm vgscan -T
1344                         for vg in $VGVOLUMES; do
1345                                 lvm vgchange -T -a y $vg
1346                         done
1347                 EOF
1348         else
1349                 echo "cat /etc/lvm.conf > /tmp/lvm.conf" | add_linuxrc
1350                 echo "global {" > "$DESTDIR/etc/lvm.conf"
1351                 echo "  locking_type = 0" >> "$DESTDIR/etc/lvm.conf"
1352                 echo "  locking_dir = \"/tmp\"" >> "$DESTDIR/etc/lvm.conf"
1353                 echo "}" >> "$DESTDIR/etc/lvm.conf"
1354                 echo "devices {" >> "$DESTDIR/etc/lvm.conf"
1355                 echo "  sysfs_scan=0" >> "$DESTDIR/etc/lvm.conf"
1356                 if is_yes "$have_md"; then
1357                         echo "  md_component_detection = 1" >> "$DESTDIR/etc/lvm.conf"
1358                 fi
1359                 if is_yes "$have_dmraid" || is_yes "$have_multipath"; then
1360                         echo '  types = [ "device-mapper", 254 ]' >> "$DESTDIR/etc/lvm.conf"
1361                 fi
1362                 if [ "$lvm_ignore_devices" ]; then
1363                         # TODO: think of merging with lvm dumpconfig output
1364                         echo '  filter = [' >> "$DESTDIR/etc/lvm.conf"
1365                         local dev
1366                         for dev in $lvm_ignore_devices; do
1367                                 debug "LVM v2: ignore device $dev"
1368                                 printf '  "r|^%s.*|",\n' $dev
1369                         done >> "$DESTDIR/etc/lvm.conf"
1370                         echo ']' >> "$DESTDIR/etc/lvm.conf"
1371                 fi
1372                 # XXX filter= must be on one line!
1373                 lvm dumpconfig | awk '/filter=/' >> "$DESTDIR/etc/lvm.conf"
1374                 echo "}" >> "$DESTDIR/etc/lvm.conf"
1375
1376                 initrd_gen_devices
1377
1378                 add_linuxrc <<-EOF
1379                         export ROOTDEV=$rootdev
1380                         export ROOTVG="$VGVOLUMES"
1381                         export SUSPENDVG=$SUSPENDVG
1382                 EOF
1383                 add_linuxrc <<-'EOF'
1384                         # parse rootdev from kernel commandline if it begins with /
1385                         case "$ROOT" in
1386                                 /*)
1387                                 if [ "$ROOT" != "$ROOTDEV" ]; then
1388                                         ROOTDEV=$ROOT
1389                                         echo "LVM: Using 'root=$ROOTDEV' from kernel commandline"
1390                                         local tmp=${ROOTDEV#/dev/}
1391                                         if [ "$tmp" != "$ROOTDEV" ]; then
1392                                                 ROOTVG=${tmp%/*}
1393                                                 echo "LVM: Using Volume Group '$ROOTVG' for rootfs"
1394                                         fi
1395                                 fi
1396                                 ;;
1397                         esac
1398
1399                         # skip duplicate VG
1400                         if [ "$SUSPENDVG" = "$ROOTVG" ]; then
1401                                 export VGVOLUMES="$ROOTVG"
1402                         else
1403                                 export VGVOLUMES="$SUSPENDVG $ROOTVG"
1404                         fi
1405
1406                         # disable noise from LVM accessing devices that aren't ready.
1407                         read printk < /proc/sys/kernel/printk
1408                         if [ ! "$DEBUGINITRD" ]; then
1409                                 echo 0 > /proc/sys/kernel/printk
1410                         fi
1411
1412                         export LVM_SYSTEM_DIR=/tmp
1413                         : 'Scanning for Volume Groups'
1414                         lvm.static vgscan --mknodes --ignorelockingfailure 2>/dev/null
1415
1416                         : 'Activating Volume Groups'
1417                         for vol in $VGVOLUMES; do
1418                                 lvm.static vgchange --ignorelockingfailure -a y $vol 2>/dev/null
1419                         done
1420
1421                         echo "$printk" > /proc/sys/kernel/printk
1422
1423                         # Find out major/minor
1424                         attrs="$(lvm.static lvdisplay --ignorelockingfailure -c $ROOTDEV 2>/dev/null)"
1425                         if [ "$attrs" ]; then
1426                                 majmin="${attrs#*$ROOTDEV*:*:*:*:*:*:*:*:*:*:*:*}"
1427                                 if [ "$majmin" != "$attrs" ]; then
1428                                         major="${majmin%:*}"
1429                                         minor="${majmin#*:}"
1430                                 fi
1431                         fi
1432
1433                         if [ "$major" -a "$minor" ]; then
1434                                 # Pass it to kernel
1435                                 echo $((256 * $major + $minor)) > /proc/sys/kernel/real-root-dev
1436                         else
1437                                 echo 2>&1 "Error figuring out real root device for $ROOTDEV!"
1438                                 echo 2>&1 "System will not most likely boot up! So dropping you to a shell!"
1439                                 echo 2>&1 ""
1440                                 sh
1441                         fi
1442                 EOF
1443         fi
1444 }
1445
1446 initrd_gen_blkid()
1447 {
1448         debug "Adding BLKID support to initrd"
1449         inst_exec /sbin/initrd-blkid /bin/blkid
1450         initrd_gen_devices
1451         add_linuxrc <<-'EOF'
1452                 # if built with blkid change ROOT=LABEL=something into ROOT=/dev/somethingelse -
1453                 # parsed by blkid
1454                 if [ "${ROOT##LABEL=}" != "${ROOT}" -o "${ROOT##UUID=}" != "${ROOT}" ]; then
1455                         ROOT="$(/bin/blkid -t $ROOT -o device -l)"
1456                 fi
1457         EOF
1458 }
1459
1460 initrd_gen_setrootdev() {
1461         debug "Adding rootfs finding based on kernel cmdline root= option support."
1462         add_linuxrc <<-'EOF'
1463                 if [ "${ROOT##/dev/}" != "${ROOT}" ]; then
1464                         rootnr="$(busybox awk -v rootnode="${ROOT##/dev/}" '$4 == rootnode { print 256 * $1 + $2 }' /proc/partitions)"
1465                         if [ -n "$rootnr" ]; then
1466                                 echo "$rootnr" > /proc/sys/kernel/real-root-dev
1467                         fi
1468                 fi
1469         EOF
1470 }
1471
1472 initrd_gen_initramfs_switchroot() {
1473         inst_d /newroot
1474         if [ "$rootdev" = "/dev/nfs" ]; then
1475                 echo "rootfs on NFS root=/dev/nfs"
1476         else
1477                 [ ! -e "$DESTDIR/$rootdev" ] && inst $rootdev $rootdev
1478         fi
1479
1480         # parse 'root=xxx' kernel commandline
1481         # We support passing root as hda3 /dev/hda3 0303 0x0303 and 303
1482         add_linuxrc <<-'EOF'
1483                 device=/dev/no_partition_found
1484                 eval "$(busybox awk -v c="$ROOT" '
1485                         BEGIN {
1486                                 num_pattern_short = "[0-9a-f][0-9a-f][0-9a-f]";
1487                                 num_pattern = "[0-9a-f]" num_pattern_short;
1488                                 dev_pattern = "[hms][a-z][a-z]([0-9])+";
1489                                 partition = "no_partition_found";
1490                                 min = -1; maj = -1;
1491
1492                                 sub("^0x", "", c);
1493                                 if (c ~ "^" num_pattern_short "$") sub("^", "0", c);
1494                                 if (c ~ "^" num_pattern  "$") {
1495                                         maj = sprintf("%s",substr(c,1,2));
1496                                         min = sprintf("%s",substr(c,3));
1497                                 }
1498                                 if (c ~ "^\/dev\/" dev_pattern "$") sub("^/dev/","", c);
1499                                 if (c ~ "^" dev_pattern "$") partition = c;
1500                         }
1501
1502                         $4 ~ partition { maj = $1; min = $2; }
1503                         $1 ~ maj && $2 ~ min { partition = $4; }
1504
1505                         END {
1506                                 printf("device=/dev/%s; maj=%s; min=%s;\n", partition, maj, min);
1507                         }
1508                         ' /proc/partitions)"
1509                 if [ "$device" != '/dev/no_partition_found' -a ! -b $device ]; then
1510                         mknod $device b $maj $min
1511                 fi
1512         EOF
1513
1514         add_linuxrc <<-EOF
1515                 rootdev=$rootdev
1516                 rootfs=$rootFs
1517         EOF
1518
1519         add_linuxrc <<-'EOF'
1520                 if [ "$device" = '/dev/no_partition_found' ]; then
1521                         device=$rootdev
1522                 fi
1523
1524                 mount -t $rootfs -r $device /newroot
1525                 init="$(echo "$CMDLINE" | busybox awk '/init=\// { gsub(/.*init=/,NIL,$0); gsub(/ .*/,NIL,$0); print }')"
1526                 if [ -z "$init" -o ! -x "/newroot$init" ]; then
1527                         init=/sbin/init
1528                 fi
1529         EOF
1530
1531         umount_all
1532         busybox_applet switch_root
1533         add_linuxrc <<-'EOF'
1534                 exec switch_root /newroot $init
1535
1536                 echo "Error! initramfs should not reach this place."
1537                 echo "It probably means you've got old version of busybox, with broken"
1538                 echo "initramfs support. Trying to boot anyway, but won't promise anything."
1539
1540                 exec chroot /newroot $init
1541
1542                 echo "Failed to chroot!"
1543         EOF
1544         # we need /init being real file, not symlink, otherwise the initramfs will
1545         # not be ran by pid 1 which is required for switch_root
1546         mv $DESTDIR/linuxrc $DESTDIR/init
1547         ln -s init $DESTDIR/linuxrc
1548 }
1549
1550 # main()
1551 if [ "$(id -u)" != 0 ]; then
1552         die "You need to be root to generate initrd"
1553 fi
1554
1555 if [ -f /etc/udev/udev.conf -a -x /sbin/initrd-udevd ]; then
1556         USE_UDEV=yes
1557         . /etc/udev/udev.conf
1558 fi
1559
1560 # cryptsetup luks addon
1561 . /etc/geninitrd/mod-luks.sh
1562 # dm-multipath addon
1563 . /etc/geninitrd/mod-multipath.sh
1564 # dmraid addon
1565 . /etc/geninitrd/mod-dmraid.sh
1566
1567 if [ -x /sbin/initrd-blkid ]; then
1568         USE_BLKID=yes
1569 fi
1570
1571 if [ -r /etc/sysconfig/geninitrd ]; then
1572         . /etc/sysconfig/geninitrd
1573 fi
1574
1575 if [ ! -x /bin/initrd-busybox ]; then
1576         die "/bin/initrd-busybox is missing!"
1577 fi
1578
1579 # backwards compatible
1580 if [ "$USE_SUSPEND2" ]; then
1581         USE_TUXONICE=$USE_SUSPEND2
1582         warn "USE_SUSPEND2 is deprecated, use USE_TUXONICE now instead."
1583 fi
1584
1585 while [ $# -gt 0 ]; do
1586         case $1 in
1587         --fstab=*)
1588                 fstab=${1#--fstab=}
1589                 ;;
1590         --fstab)
1591                 fstab=$2
1592                 shift
1593                 ;;
1594         --modules-conf=*)
1595                 modulefile=${1#--modules-conf=}
1596                 ;;
1597         --modules-conf)
1598                 modulefile=$2
1599                 shift
1600                 ;;
1601         --use-raidstart|--with-raidstart)
1602                 USERAIDSTART=yes
1603                 ;;
1604         --without-raidstart)
1605                 USERAIDSTART=no
1606                 ;;
1607         --use-insmod-static|--with-insmod-static)
1608                 USEINSMODSTATIC=yes
1609                 ;;
1610         --without-insmod-static)
1611                 USEINSMODSTATIC=no
1612                 ;;
1613         --with-bootsplash)
1614                 BOOT_SPLASH=yes
1615                 ;;
1616         --without-bootsplash)
1617                 BOOT_SPLASH=no
1618                 ;;
1619         --with-fbsplash)
1620                 FB_SPLASH=yes
1621                 ;;
1622         --without-fbsplash)
1623                 FB_SPLASH=no
1624                 ;;
1625         --with-fbcondecor)
1626                 FB_CON_DECOR=yes
1627                 ;;
1628         --without-fbcondecor)
1629                 FB_CON_DECOR=no
1630                 ;;
1631         --with-suspend)
1632                 USE_SUSPEND=yes
1633                 ;;
1634         --without-suspend)
1635                 USE_SUSPEND=no
1636                 ;;
1637         --with-suspend2 | --with-tuxonice)
1638                 USE_TUXONICE=yes
1639                 ;;
1640         --without-suspend2 | --without-tuxonice)
1641                 USE_TUXONICE=no
1642                 ;;
1643         --lvmversion=*)
1644                 LVMTOOLSVERSION=${1#--lvmversion=}
1645                 ;;
1646         --lvmtoolsversion=*)
1647                 LVMTOOLSVERSION=${1#--lvmtoolsversion=}
1648                 ;;
1649         --lvmtoolsversion|--lvmversion)
1650                 LVMTOOLSVERSION=$2
1651                 shift
1652                 ;;
1653         --without-udev)
1654                 USE_UDEV=no
1655                 ;;
1656         --with-udev)
1657                 USE_UDEV=yes
1658                 ;;
1659         --without-dmraid)
1660                 USE_DMRAID=no
1661                 ;;
1662         --without-multipath)
1663                 USE_MULTPATH=no
1664                 ;;
1665         --without-blkid)
1666                 USE_BLKID=no
1667                 ;;
1668         --without-luks)
1669                 USE_LUKS=no
1670                 ;;
1671         --with=*)
1672                 BASICMODULES="$BASICMODULES ${1#--with=}"
1673                 ;;
1674         --with)
1675                 BASICMODULES="$BASICMODULES $2"
1676                 shift
1677                 ;;
1678         --version)
1679                 echo "$PROGRAM: version $VERSION"
1680                 exit 0
1681                 ;;
1682         -v)
1683                 verbose=-v
1684                 ;;
1685         --nocompress)
1686                 COMPRESS=no
1687                 ;;
1688         --ifneeded)
1689                 ifneeded=1
1690                 ;;
1691         -f)
1692                 force=1
1693                 ;;
1694         --preload=*)
1695                 PREMODS="$PREMODS ${1#--preload=}"
1696                 ;;
1697         --preload)
1698                 PREMODS="$PREMODS $2"
1699                 shift
1700                 ;;
1701         --fs=*)
1702                 warn "Warning: --fs option is obsoleted. Use --initrdfs instead"
1703                 INITRDFS=${1#--fs=}
1704                 ;;
1705         --fs)
1706                 warn "Warning: --fs option is obsoleted. Use --initrdfs instead"
1707                 INITRDFS=$2
1708                 shift
1709                 ;;
1710         --initrdfs=*)
1711                 INITRDFS=${1#--initrdfs=}
1712                 ;;
1713         --initrdfs)
1714                 INITRDFS=$2
1715                 shift
1716                 ;;
1717         --image-version)
1718                 img_vers=yes
1719                 ;;
1720         --ide-only-root)
1721                 ide_only_root="yes"
1722                 ;;
1723         *)
1724                 if [ -z "$target" ]; then
1725                         target="$1"
1726                 elif [ -z "$kernel" ]; then
1727                         kernel="$1"
1728                 else
1729                         usage >&2
1730                         exit 1
1731                 fi
1732                 ;;
1733         esac
1734
1735         shift
1736 done
1737
1738 if [ -z "$target" -o -z "$kernel" ]; then
1739         usage >&2
1740         exit 1
1741 fi
1742
1743 if [ ! -f /boot/vmlinuz-"$kernel" ]; then
1744         warn "/boot/vmlinuz-$kernel doesn't exist, is your /boot mounted?"
1745 fi
1746
1747 pack_version=$(echo "$kernel" | awk -F. '{print sprintf("%03d%03d",$1,$2)}')
1748 pack_version_long=$(echo "$kernel" | awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}')
1749
1750 if [ -z "$INITRDFS" ]; then
1751         if [ -z "$FS" ]; then
1752                 # default value
1753                 if [ "$pack_version" -ge "002005" ]; then
1754                         INITRDFS="initramfs"
1755                 else
1756                         INITRDFS="rom"
1757                 fi
1758         else
1759                 warn "Warning: FS configuration options is obsoleted. Use INITRDFS instead"
1760                 INITRDFS="$FS"
1761         fi
1762 fi
1763
1764 if [ "$pack_version" -lt "002006" ]; then
1765         USE_UDEV=no
1766         USE_DMRAID=no
1767 fi
1768
1769 if [ "$pack_version" -ge "002005" ]; then
1770         modext=".ko"
1771         insmod="insmod"
1772 fi
1773
1774 if is_yes "$USEINSMODSTATIC"; then
1775         insmod="insmod.static"
1776         INSMOD="/sbin/insmod.static"
1777         if [ "$pack_version" -lt "002005" -a -f /sbin/insmod.static.modutils ]; then
1778                 INSMOD="/sbin/insmod.static.modutils"
1779         fi
1780         if [ ! -f "$INSMOD" ]; then
1781                 die "insmod.static requested but /sbin/insmod.static not found!"
1782         fi
1783 fi
1784
1785 case "$INITRDFS" in
1786         ext2)
1787                 if [ ! -x /sbin/mke2fs ]; then
1788                         die "/sbin/mke2fs is missing"
1789                 fi
1790                 ;;
1791         rom|romfs)
1792                 if [ ! -x /sbin/genromfs ]; then
1793                         die "/sbin/genromfs is missing"
1794                 fi
1795                 ;;
1796         cram|cramfs)
1797                 if [ ! -x /sbin/mkcramfs ]; then
1798                         die "/sbin/mkcramfs is missing"
1799                 fi
1800                 ;;
1801         initramfs)
1802                 if [ ! -x /bin/cpio ]; then
1803                         die "/bin/cpio is missing"
1804                 fi
1805                 if [ ! -x /usr/bin/find ]; then
1806                         die "/usr/bin/find is missing"
1807                 fi
1808                 ;;
1809         *)
1810                 die "Filesystem $INITRDFS on initrd is not supported"
1811                 ;;
1812 esac
1813
1814 if [ -L "$target" ]; then
1815         target=$(readlink -f "$target")
1816 fi
1817
1818 if [ -n "$img_vers" ]; then
1819         target="$target-$kernel"
1820 fi
1821
1822 if [ -z "$force" -a -f "$target" ]; then
1823         die "$target already exists."
1824 fi
1825
1826 if [ ! -d "/lib/modules/$kernel" ]; then
1827         die "/lib/modules/$kernel is not a directory."
1828 fi
1829
1830 if [ "$INITRDFS" != "initramfs" ]; then
1831         if is_yes "$FB_SPLASH"; then
1832                 warn "Using fbsplash requires INITRDFS=initramfs; skipping fbsplash generation"
1833                 FB_SPLASH=no
1834         fi
1835         if is_yes "$FB_CON_DECOR"; then
1836                 warn "Using fbcondecor requires INITRDFS=initramfs; skipping fbcondecor generation"
1837                 FB_CON_DECOR=no
1838         fi
1839 fi
1840
1841 if is_yes "$USE_SUSPEND" && is_yes "$USE_TUXONICE"; then
1842         warn "Tuxonice and mainline suspend are exclusive!"
1843 fi
1844
1845 if is_yes "$FB_SPLASH"  && is_yes "$BOOT_SPLASH"; then
1846         warn "bootsplash and fbsplash are exclusive!"
1847 fi
1848
1849 if [ ! -f /proc/mounts ]; then
1850         warn "/proc filesystem not mounted, may cause wrong results or failure."
1851 fi
1852
1853 if [ -d /usr/lib64 ]; then
1854         _lib=lib64
1855 else
1856         _lib=lib
1857 fi
1858 debug "Using libdir: $_lib"
1859
1860 cache_modprobe_conf
1861
1862 for n in $PREMODS; do
1863         findmodule "$n"
1864 done
1865
1866 if [ "$FBMODULE" ]; then
1867         findmodule "$FBMODULE"
1868 fi
1869
1870 # allow forcing loading SCSI and/or IDE modules
1871 if is_yes "$ADDSCSI"; then
1872         find_modules_scsi
1873 fi
1874
1875 if is_yes "$ADDIDE"; then
1876         find_modules_ide
1877 fi
1878
1879 if is_yes "$USE_SUSPEND"; then
1880         find_modules_suspend
1881 fi
1882
1883 find_root "$fstab" || exit
1884 debug "Using $rootdev as device for rootfs"
1885
1886 find_modules_for_devpath "$rootdev"
1887 [ -n "$rootdev_add" ] && find_modules_for_devpath "$rootdev_add"
1888
1889 findmodule "-$rootFs"
1890
1891 for n in $BASICMODULES; do
1892         findmodule "$n"
1893 done
1894
1895 if is_yes "$USE_TUXONICE"; then
1896         findmodule "-lzf"
1897 fi
1898
1899 if is_yes "$FB_SPLASH"; then
1900         findmodule "-evdev"
1901 fi
1902
1903 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
1904         debug "No modules are needed -- not building initrd image."
1905         exit 0
1906 fi
1907
1908 DESTDIR=$(mktemp -d -t initrd.XXXXXX) || die "mktemp failed"
1909 RCFILE="$DESTDIR/linuxrc"
1910 > "$RCFILE"
1911 chmod a+rx "$RCFILE"
1912 ln -s linuxrc $DESTDIR/init
1913
1914 # create dirs that we really need
1915 inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,sys}
1916
1917 modules_install "$MODULES"
1918
1919 # mknod'ing the devices instead of copying them works both with and
1920 # without devfs...
1921 mknod "$DESTDIR/dev/console" c 5 1
1922 mknod "$DESTDIR/dev/null" c 1 3
1923 mknod "$DESTDIR/dev/zero" c 1 5
1924
1925 inst_exec /bin/initrd-busybox /bin/initrd-busybox
1926 ln -s initrd-busybox $DESTDIR/bin/sh
1927 ln -s initrd-busybox $DESTDIR/bin/busybox # for older busyboxes who had /bin/busybox as EXEPATH
1928
1929 if is_yes "$USEINSMODSTATIC"; then
1930         inst_exec "$INSMOD" /bin/insmod.static
1931 fi
1932
1933 add_linuxrc <<EOF
1934 #!/bin/sh
1935 # initrd generated by:
1936 # $RCSID
1937
1938 EOF
1939 mount_proc
1940 add_linuxrc <<-'EOF'
1941         read CMDLINE < /proc/cmdline; export CMDLINE
1942
1943         for arg in $CMDLINE; do
1944                 if [ "${arg}" = "debuginitrd" ]; then
1945                         DEBUGINITRD=yes
1946                 fi
1947                 if [ "${arg##debuginitrd=}" != "${arg}" ]; then
1948                         DEBUGINITRD=${arg##debuginitrd=}
1949                 fi
1950                 if [ "${arg##root=}" != "${arg}" ]; then
1951                         ROOT=${arg##root=}
1952                 fi
1953         done
1954
1955         # make debugshell() invoke subshell if $DEBUGINITRD=sh
1956         if [ "$DEBUGINITRD" = "sh" ]; then
1957                 debugshell() {
1958 EOF
1959 if is_yes "$RUN_SULOGIN_ON_ERR"; then
1960 add_linuxrc <<-'EOF'
1961         echo "debug shell disabled by /etc/sysconfig/system:RUN_SULOGIN_ON_ERR setting"
1962 EOF
1963 else
1964 add_linuxrc <<-'EOF'
1965         sh
1966 EOF
1967 fi
1968 add_linuxrc <<-'EOF'
1969                 }
1970         else
1971                 debugshell() {
1972                         :
1973                 }
1974         fi
1975
1976         if [ "$DEBUGINITRD" ]; then
1977                 set -x
1978         fi
1979 EOF
1980
1981 modules_add_linuxrc $MODULES
1982
1983 # TODO: rewrite for busybox
1984 #if [ -n "$loopDev" ]; then
1985 #       if [ ! -d /initrd ]; then
1986 #               mkdir /initrd
1987 #       fi
1988 #
1989 #       cp -a "$loopDev" "$DESTDIR/dev"
1990 #       cp -a "$rootdev" "$DESTDIR/dev"
1991 #       echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1992 #       echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1993 #       echo "echo Setting up loopback device $rootdev" >> $RCFILE
1994 #       echo "losetup $rootdev /loopfs$loopFile" >> "$RCFILE"
1995 #fi
1996
1997 if is_yes "$USE_UDEV"; then
1998         initrd_gen_udev
1999 fi
2000
2001 if is_yes "$USE_TUXONICE"; then
2002         initrd_gen_tuxonice
2003 fi
2004
2005 if is_yes "$need_v86d"; then
2006         initrd_gen_v86d
2007 fi
2008
2009 if is_yes "$have_luks"; then
2010         initrd_gen_luks
2011 fi
2012
2013 if is_yes "$have_dmraid"; then
2014         initrd_gen_dmraid
2015 fi
2016
2017 if is_yes "$have_multipath"; then
2018         initrd_gen_multipath
2019 fi
2020
2021 if is_yes "$USE_BLKID"; then
2022         initrd_gen_blkid
2023 fi
2024
2025 if is_yes "$have_nfs"; then
2026         initrd_gen_nfs
2027 elif is_yes "$USERAIDSTART" && is_yes "$have_md"; then
2028         initrd_gen_md
2029         if is_yes "$have_lvm"; then
2030                 initrd_gen_lvm
2031         else
2032                 initrd_gen_setrootdev
2033         fi
2034 elif is_yes "$have_lvm"; then
2035         initrd_gen_lvm
2036 else
2037         initrd_gen_setrootdev
2038 fi
2039
2040 if is_yes "$USE_SUSPEND"; then
2041         initrd_gen_suspend
2042 fi
2043
2044 # additional devs always needed
2045 [ ! -e "$DESTDIR/$rootdev_add" ] && inst $rootdev_add /dev
2046
2047 if is_yes "$USE_UDEV"; then
2048         initrd_gen_stop_udevd
2049 fi
2050
2051 if [ "$INITRDFS" = "initramfs" ]; then
2052         initrd_gen_initramfs_switchroot
2053 else
2054         umount_all
2055 fi
2056
2057 if is_yes "$FB_SPLASH"; then
2058         initrd_gen_fbsplash
2059 fi
2060
2061 if is_yes "$FB_CON_DECOR"; then
2062         initrd_gen_fbcondecor
2063 fi
2064
2065 IMAGE=$(mktemp -t initrd.img-XXXXXX) || die "mktemp failed"
2066
2067 debug "Creating $INITRDFS image $IMAGE"
2068 case "$INITRDFS" in
2069         ext2)
2070                 IMAGESIZE=$(du -ks $DESTDIR | awk '{print int(($1+1023+512)/1024)*1024}')
2071                 debug   "ext2 image size: $IMAGESIZE ($DESTDIR)"
2072                 if [ "$IMAGESIZE" -gt 4096 ]; then
2073                         warn "Your image size is larger than 4096, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!"
2074                 fi
2075
2076                 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
2077                 mke2fs -q -F -b 1024 -m 0 "$IMAGE" 2>/dev/null 1>&2
2078                 tune2fs -i 0 "$IMAGE" >/dev/null 2>&1
2079
2080                 local tmpmnt=$(mktemp -d -t initrd.mnt-XXXXXX)
2081                 debug "Mounting ext2 image $IMAGE to $tmpmnt"
2082                 mount -o loop -t ext2 "$IMAGE" "$tmpmnt"
2083                 # We don't need this directory, so let's save space
2084                 rm -rf "$tmpmnt"/lost+found
2085
2086                 debug "Copy recursively $DESTDIR -> $tmpmnt"
2087                 cp -a $DESTDIR/* $tmpmnt
2088                 umount "$IMAGE"
2089                 rmdir "$tmpmnt"
2090
2091                 ;;
2092         rom|romfs)
2093                 genromfs -f "$IMAGE" -d "$DESTDIR" -V "PLD initrd for kernel $kernel"
2094                 IMAGESIZE=$(stat -c %s $IMAGE | awk '{print int((($1/1024)+1023)/1024)*1024}')
2095                 debug "Image size for romfs: ${IMAGESIZE}KiB ($IMAGE)"
2096                 if [ "$IMAGESIZE" -gt 4096 ]; then
2097                         warn "Your image size is larger than 4096, Be sure to boot kernel with ramdisk_size=$IMAGESIZE!"
2098                 fi
2099                 ;;
2100         cram|cramfs)
2101                 mkcramfs "$DESTDIR" "$IMAGE"
2102                 ;;
2103         initramfs)
2104                 (cd $DESTDIR; find . | cpio --quiet -H newc -o > "$IMAGE")
2105                 ;;
2106         *)
2107                 echo "Filesystem $INITRDFS not supported by $PROGRAM";
2108 esac
2109
2110 if is_yes "$COMPRESS"; then
2111         tmp=$(mktemp "$target".XXXXXX) || die "mktemp failed"
2112         debug "Compressing $target"
2113         gzip -9 < "$IMAGE" > "$tmp"
2114         mv -f "$tmp" "$target"
2115 else
2116         cp -a "$IMAGE" "$target"
2117 fi
2118
2119 # XXX. check if bootsplash can output data to tmp dir not directly to initramfs image.
2120 if is_yes "$BOOT_SPLASH"; then
2121         initrd_gen_bootsplash "$target"
2122 fi
2123
2124 rm -rf "$DESTDIR" "$IMAGE"
2125
2126 # vim:ts=4:sw=4:noet:fdm=marker
This page took 0.175175 seconds and 4 git commands to generate.