]> git.pld-linux.org Git - projects/geninitrd.git/blob - geninitrd
351ee8a86e26e52c90ba9670d7207a7ca3e4b553
[projects/geninitrd.git] / geninitrd
1 #!/bin/sh
2
3 # geninitrd
4 #
5 #       by PLD Linux Team
6 #
7 # based on mkinitrd from RedHat
8
9 RCSID='$Id$'
10 PATH=/sbin:$PATH
11 export PATH
12
13 VERSION="`echo "$RCSID"|awk '{print $3}'`"
14
15 . /etc/rc.d/init.d/functions
16
17 COMPRESS="yes"
18 # INITRDFS is set later (catch obsoleted FS option)
19 #INITRDFS="rom"
20 USERAIDSTART="yes"
21 USEMDADMSTATIC="no"
22 USEINSMODSTATIC="no"
23 USE_SUSPEND="yes"
24 USE_SUSPEND2="yes"
25 uselvm="no"
26 usenfs="no"
27 # it should be safe to remove scsi_mod from here, but I'm not sure...
28 PRESCSIMODS="-scsi_mod unknown -sd_mod"
29 PREIDEMODS="-ide-core unknown -ide-detect -ide-disk"
30 PREIDEMODSOLD="-ide-probe -ide-probe-mod -ide-disk"
31 target=""
32 kernel=""
33 force=""
34 verbose=""
35 MODULES=""
36 img_vers=""
37 modulefile=""
38 fstab="/etc/fstab"
39 insmod="insmod"
40 modext=".o"
41 rootdev_nr=0
42 # default bootsplash is off, if it have to be on, install bootsplash package
43 BOOT_SPLASH=no
44 # default same as bootsplash, if on install splashutils and some splashutils theme
45 FB_SPLASH=no
46
47 # is /dev on tmpfs. internal variable
48 tmpfs_dev=
49
50 if [ -f /etc/udev/udev.conf -a -x /sbin/initrd-udev ]; then
51         USE_UDEV="yes"
52         . /etc/udev/udev.conf
53 fi
54
55 if [ -x /sbin/dmraid-initrd ]; then
56         USE_DMRAID="yes"
57 fi
58
59 usage () {
60         echo "usage: `basename $0` [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
61         echo "       [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
62         echo "       [--initrdfs=rom|ext2|cram] [--modules-conf=<modules.conf>]"
63         echo "       [--with-raidstart] [--without-raidstart] [--with-insmod-static]"
64         echo "       [--without-bootsplash] [--without-fbsplash]"
65         echo "       [--lvmtoolsversion=1|2] [--with-udev] [--without-udev]"
66         echo "       [--without-suspend] [--without-suspend2] [--without-dmraid]"
67         echo "       <initrd-image> <kernel-version>"
68         echo "       (ex: `basename $0` /boot/initrd-2.2.5-15.img 2.2.5-15)"
69         exit 1
70 }
71
72 debug() {
73         [ -n "$verbose" ] && echo "$*"
74 }
75
76 find_depmod () {
77         typeset mods module f level depfile first
78
79         depfile=/lib/modules/$kernel/modules.dep
80
81         if [ -f $depfile ] ; then
82                 : ok
83         else
84                 echo >&2 "Error: no $depfile ! Run depmod and rerun geninitrd."
85                 exit 1
86         fi
87
88         # prepend / if no path given, append $modext.gz if not given,
89         # quote /
90         origmodule="$2"
91         module=$(echo "$2" | \
92                 awk '/\// {print;next} {print "/" $0}' | \
93                 awk '/\./ {print;next} {print $0 "'$modext'.gz"}' |
94                 awk '{gsub("/","\\/");print}')
95         mods=$(awk '
96 BEGIN { here = 0 }
97 /'"$module"':(.*)/ { gsub(/:/," "); gsub(/\\/," "); print; here = 1; next }
98 /:/ { here = 0 }
99 /(.*)/ { gsub(/\\/," "); if (here) print }
100 ' $depfile | xargs)
101
102         # fallback to $modext
103         if [ "$mods" = "" ] ; then
104                 module=$(echo "$module" | \
105                 awk '{gsub("\'$modext'\.gz$","\'$modext'",$0);print}')
106         fi
107         mods=$(awk '
108 BEGIN { here = 0 }
109 /'"$module"':(.*)/ { gsub(/:/," "); gsub(/\\/," "); print; here = 1; next }
110 /:/ { here = 0 }
111 /(.*)/ { gsub(/\\/," "); if (here) print }
112 ' $depfile | xargs)
113
114         if [ "$mods" = "" ] ; then
115                 if [ "$1" != silent ] ; then
116                         echo >&2 "$origmodule: module not found in $depfile"
117                 fi
118                 if ! is_no "$EXIT_IF_MISSING" ; then
119                         exit 1
120                 else
121                         echo >&2 "If $origmodule isn't compiled in kernel then this initrd may not start your system."
122                 fi
123         fi
124
125         level=$3
126         if [ "$level" = "" ] ; then
127                 level=0
128         fi
129         level=$((level + 1))
130         if [ $level -gt 20 ] ; then
131                 echo >&2 "$origmodule: cycle in $depfile"
132                 exit 1
133         fi
134
135         first=
136         for f in $mods ; do
137                 if [ "$first" = "" ] ; then
138                         first=$f
139                 else
140                         find_depmod $1 $f $level
141                 fi
142         done
143
144         echo $first
145 }
146
147 addmodule() {
148         fmPath=$1
149         skiperrors=$2
150
151         if [ ! -f "/lib/modules/$kernel/$fmPath" ]; then
152                 if [ -n "$skiperrors" ]; then
153                         return
154                 fi
155
156                 echo >&2 "module $fmPath present in modules.dep, but not in filesystem (kernel = $kernel)"
157                 exit 1
158         fi
159
160         # only need to add each module once
161         # quote /
162         tmpFmPath=$(echo $fmPath | awk '{gsub(/\//,"\\/");print}')
163         if echo "$MODULES" | awk '/'"$tmpFmPath"'/ {exit 1}' ; then
164                 MODULES="$MODULES $fmPath"
165         fi
166 }
167
168 findmodule() {
169         skiperrors=""
170         modName=$1
171
172         if [ ${modName#-} != ${modName} ]; then
173                 skiperrors=1
174                 modName=${modName#-}
175         fi
176
177         # what's that?
178         if [ "$modName" = "pluto" ]; then
179                 findmodule fc4
180                 findmodule soc
181         fi
182         if [ "$modName" = "fcal" ]; then
183                 findmodule fc4
184                 findmodule socal
185         fi
186
187         if [ -n "$skiperrors" ]; then
188                 allModulesToFind=$(find_depmod silent $modName)
189         else
190                 allModulesToFind=$(find_depmod normal $modName)
191                 if [ $? != 0 ]; then
192                         exit 1
193                 fi
194         fi
195
196         for mod in $allModulesToFind; do
197                 mod=$(echo $mod | awk '{sub(/^\/lib\/modules\/[^\/]*\//, ""); print}')
198                 addmodule $mod "$skiperrors"
199         done
200 }
201
202 inst() {
203         if [ "$#" != "2" ];then
204                 echo "usage: inst <file> <destination>"
205                 return
206         fi
207         debug "$1 -> $2"
208         cp "$1" "$2"
209 }
210
211 get_label_ext2 () {
212         /sbin/e2label $1 2> /dev/null
213 }
214
215 get_uuid_ext2 () {
216         /sbin/tune2fs -l $1 2> /dev/null | awk -F: '/UUID:/ {gsub(" ",""); print $2}'
217 }
218
219 get_label_xfs () {
220         /usr/sbin/xfs_db -x -p xfs_admin -c label -r "$1"|awk -F= '{sub("^\"","", $2); sub("\"$", "", $2); print $2}'
221
222 }
223
224 get_uuid_xfs () {
225         /usr/sbin/xfs_admin -u "$1" 2>/dev/null | awk -F= '{print $2}' | xargs
226 }
227
228 find_root() {
229         eval `awk '/^[\t ]*#/ {next} {if ( $2 == "/" ) {print "rootdev=\"" $1 "\"\nrootFs=\"" $3 "\""}}' $fstab`
230         case $rootdev in
231         LABEL=*)
232                 if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
233                         rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
234                         if [ -n "$rootdev2" ] ; then
235                                 rootdev=$rootdev2
236                                 rootdev_found=1
237                         fi
238                 fi
239                 if [ "$rootdev_found." != "1." ] ; then
240                         case $rootFs in
241                         ext2|ext3)
242                                 if [ ! -x /sbin/e2label ] ; then
243                                         echo >&2 "/sbin/e2label is missing"
244                                         exit 1
245                                 fi
246                                 get_label="get_label_ext2"
247                                 ;;
248                         xfs)
249                                 if [ ! -x /usr/sbin/xfs_db ] ; then
250                                         echo >&2 "/usr/sbin/xfs_db is missing"
251                                         exit 1
252                                 fi
253                                 get_label="get_label_xfs"
254                                 ;;
255                         *)
256                                 echo >&2 "LABEL on $rootFs in not supported by geninitrd"
257                                 exit 1
258                                 ;;
259                         esac
260                         if [ ! -r /proc/partitions ] ; then
261                                 echo >&2 '/proc/partitions is not readable'
262                                 exit 1
263                         fi
264                         label=${rootdev#"LABEL="}
265                         for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
266                                 if [ -r $dev ] && [ "$label" = "`$get_label $dev`" ] ; then
267                                         debug "Using $dev as device for rootfs"
268                                         rootdev=$dev
269                                         rootdev_found=1
270                                         break
271                                 fi
272                         done
273                         if [ "$rootdev_found." != "1." ] ; then
274                                 echo >&2 "geninitrd can't find real device for LABEL=$label"
275                                 exit 1
276                         fi
277                 fi
278                 ;;
279         UUID=*)
280                 if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
281                         rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
282                         if [ -n "$rootdev2" ] ; then
283                                 rootdev=$rootdev2
284                                 rootdev_found=1
285                         fi
286                 fi
287                 if [ "$rootdev_found." != "1." ] ; then
288                         case $rootFs in
289                         ext2|ext3)
290                                 if [ ! -x /sbin/tune2fs ] ; then
291                                         echo >&2 "/sbin/tune2fs is missing"
292                                         exit 1
293                                 fi
294                                 get_uuid="get_uuid_ext2"
295                                 ;;
296                         xfs)
297                                 if [ ! -x /usr/sbin/xfs_admin ] ; then
298                                         echo >&2 "/usr/sbin/xfs_admin is missing"
299                                         exit 1
300                                 fi
301                                 get_uuid="get_uuid_xfs"
302                                 ;;
303                         *)
304                                 echo >&2 "UUID on $rootFs in not supported by geninitrd"
305                                 exit 1
306                                 ;;
307                         esac
308                         if [ ! -r /proc/partitions ] ; then
309                                 echo >&2 '/proc/partitions is not readable'
310                                 exit 1
311                         fi
312                         uuid=${rootdev#"UUID="}
313                         for dev in $(awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions); do
314                                 if [ -r $dev ] && [ "$uuid" = "`$get_uuid $dev`" ] ; then
315                                         debug "Using $dev as device for rootfs"
316                                         rootdev=$dev
317                                         rootdev_found=1
318                                         break
319                                 fi
320                         done
321                         if [ "$rootdev_found" != 1 ] ; then
322                                 echo >&2 "geninitrd can't find real device for UUID=$uuid"
323                                 exit 1
324                         fi
325                 fi
326                 ;;
327         esac
328         rootdev1=${rootdev}
329 }
330
331 find_modules_softraid() {
332         if [ -f /etc/mdadm.conf ]; then
333                 debug "Finding RAID details using mdadm for rootdev=$1"
334                 eval `/sbin/mdadm -v --examine --scan --config=/etc/mdadm.conf | awk -v rootdev="$1" '
335                 BEGIN {
336                         found = "no";
337                         dev_list = "";
338                         raidlevel = ""
339                         rootdev_devfs = rootdev;
340                         if (rootdev ~ /\/dev\/md\/[0-9]/) {
341                                 gsub(/\/dev\/md\//,"/dev/md",rootdev_devfs);
342                         }
343                 }
344
345                 /^ARRAY/ {
346                         if (($2 == rootdev) || ($2 == rootdev_devfs)) {
347                                 raidlevel=$3;
348                                 gsub(/level=/,NUL,raidlevel);
349                                 if (raidlevel ~ /^raid([0-6]|10)/) {
350                                         gsub(/raid/,NUL,raidlevel);
351                                 };
352                                 found="yes";
353                                 getline x;
354                                 if (x ~ /devices=/) {
355                                         dev_list = x;
356                                         gsub(".*devices=", NUL, dev_list);
357                                         gsub(",", " ", dev_list);
358                                 }
359                         }
360                 }
361
362                 END {
363                         print "raidfound=" found;
364                         print "raidlevel=" raidlevel;
365                         print "dev_list=\"" dev_list "\"";
366                 }'`
367         fi
368
369         if [ "$raidfound" != "yes" -a -f /etc/raidtab ]; then
370                 echo >&2 "ERROR: raidtools are not longer supported. Please migrate to mdadm setup!"
371                 exit 1
372         fi
373
374         if is_yes "$raidfound" ; then
375                 case "$raidlevel" in
376                 [01456]|10)
377                         findmodule "raid$raidlevel"
378                         ;;
379                 linear)
380                         findmodule "linear"
381                         ;;
382                 *)
383                         echo >&2 "raid level $number (in mdadm config) not recognized"
384                         ;;
385                 esac
386         else
387                 echo >&2 "ERROR: RAID devices not found for \"$1\", check your configuration!"
388                 exit 1
389         fi
390
391         rootdev_nr=$(( $rootdev_nr + 1 ))
392         eval "rootdev${rootdev_nr}=\"$1\""
393         eval "dev_list${rootdev_nr}=\"${dev_list}\""
394
395         for device in $dev_list; do
396                 find_modules_for $device
397         done
398 }
399
400 find_modules_scsi() {
401         for n in $PRESCSIMODS; do
402                 if [ "X$n" = "Xunknown" ] ; then
403                         if [ -f "$modulefile" ]; then
404                                 scsimodules="`awk '/scsi_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
405                                 for n in $scsimodules; do
406                         # for now allow scsi modules to come from anywhere. There are some
407                         # RAID controllers with drivers in block
408                                         findmodule "$n"
409                                 done
410                         fi
411                 else
412                         findmodule "$n"
413                 fi
414         done
415 }
416
417 find_modules_ide() {
418         typeset rootdev
419
420         rootdev="$(echo "$1" | awk ' { gsub(/\/dev\//,NUL); gsub(/[0-9].*/, NUL); print $0 } ')"
421         if [ "$pack_version_long" -lt "002004021" ]; then
422                 debug "Finding IDE modules for kernels <= 2.4.20"
423                 for n in $PREIDEMODSOLD; do
424                         findmodule "$n"
425                 done
426         else
427                 tryauto=1
428                 for n in $PREIDEMODS; do
429                         if [ "X$n" = "Xunknown" ] ; then
430                                 if [ -f "$modulefile" ]; then
431                                         debug "Finding IDE modules using ide_hostadapter"
432                                         idemodules="`awk '/ide_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
433                                         for na in $idemodules; do
434                                                 tryauto=0;
435                                                 findmodule "$na"
436                                         done
437                                 fi
438
439                                 if [ "$tryauto" -eq 1 ]; then
440                                         # If tryauto {{{
441                                         if [ -r /usr/share/pci-database/ide.pci -a -r /proc/bus/pci/devices ]; then
442                                                 debug "Finding IDE modules using PCI ID database"
443                                                 # Finding IDE modules using PCI ID database {{{
444                                                 if is_yes "${ide_only_root}"; then
445                                                         if [ -f /sys/block/${rootdev}/device/../../vendor -a -f /sys/block/${rootdev}/device/../../device ]; then
446                                                                 vendorid="$(awk ' { gsub(/0x/,NUL); print $0 } ' /sys/block/${rootdev}/device/../../vendor)"
447                                                                 deviceid="$(awk ' { gsub(/0x/,NUL); print $0 } ' /sys/block/${rootdev}/device/../../device)"
448                                                                 searchpciid="${vendorid}${deviceid}"
449                                                         elif [ -f /proc/ide/${rootdev}/../config ]; then
450                                                                 searchpciid="$(awk ' /pci bus/ { print $7$9 } ' /proc/ide/${rootdev}/../config)"
451                                                         fi
452                                                 fi
453
454                                                 if [ -z "${searchpciid}" ]; then
455                                                         searchpciid="$(awk ' { print $2 } ' /proc/bus/pci/devices)"
456                                                 fi
457
458                                                 idemodules=""
459
460                                                 for nb in $searchpciid; do
461                                                         eval `awk -v pciid="$nb" '{
462                                                                 gsub("\t"," ");
463                                                                 gsub("  +", " ");
464                                                                 gsub("^ ","");
465                                                                 if (/^[\t ]*#/)
466                                                                         next;
467                                                                 compmod = $1 "";        # make sure comparison type will be string
468                                                                                         # cause pci IDs are hexadecimal numeric
469                                                                 if (compmod == pciid) {
470                                                                         module=$2;
471                                                         #               min_kernel=$3;  # now in ide.pci $3,$4 = vendor and device name
472                                                         #               max_kernel=$4;  #
473                                                                         exit 0;
474                                                                 }
475                                                         }
476
477                                                         END {
478                                                                 print "module=" module "\nmin_kernel=" min_kernel "\nmax_kernel=\"" max_kernel "\"\n";
479                                                         }' /usr/share/pci-database/ide.pci`
480                                                         [ -n "$module" ] && idemodules="$idemodules $module"
481                                                 done
482                                                 if is_yes "$(awk ' /ide=reverse/ { print "yes" } ' /proc/cmdline)"; then
483                                                         new_idemodules=""
484                                                         for nc in idemodules; do
485                                                                 new_idemodules="$nc $new_idemodules"
486                                                         done
487                                                         idemodules="${new_idemodules}"
488                                                 fi
489
490                                                 if [ -z "$idemodules" ]; then
491                                                         echo "WARNING: rootfs on IDE device but no related modules found, loading ide-generic."
492                                                         idemodules="ide-generic"
493                                                 fi
494
495                                                 # }}}
496                                                 for nd in $idemodules; do
497                                                         findmodule "-$nd"
498                                                 done
499                                         # }}}
500                                         # else tryauto {{{
501                                         else
502                                                 [ -r /usr/share/pci-database/ide.pci ] || echo "WARNING: /usr/share/pci-database/ide.pci missing."
503                                                 [ -r /proc/bus/pci/devices ] || echo "WARNING: /proc/bus/pci/devices missing."
504                                                 echo "Automatic IDE modules finding not available."
505                                         fi
506                                         # }}}
507                                 fi
508                         else
509                                 findmodule "$n"
510                         fi
511                 done
512         fi
513 }
514
515 # resolve /dev/dm-0 to lvm2 node
516 # which they got from /proc/partitions when rootfs is UUID=
517 _lvm2_node_resolve() {
518         local node="$1"
519
520         # redirect output to tmpfile so we won't get subshell
521         local tmp ret
522         tmp=$(mktemp) || exit 1
523
524         lvdisplay -c 2>/dev/null|awk -F: '{print $1}' > $tmp
525         while read dev; do
526                 count=$(ls -Ll $dev $node | awk '{print $5, $6}' | sort -u | wc -l)
527                 if [ "$count" = 1 ]; then
528                         ret="$dev"
529                         break
530                 fi
531         done < $tmp
532         rm -f $tmp
533
534         if [ -z "$ret" ]; then
535                 ret=$node
536         fi
537         echo "$ret"
538 }
539
540 # return true if node is lvm node
541 _check_lvm() {
542         local node="$1"
543         if [ ! -e "$node" ]; then
544                 echo >&2 "WARNING: check_lvm(): node $node doesn't exist!"
545                 return 1
546         fi
547
548         # block-major-58 is lvm1
549         ls -lL "$node" 2> /dev/null | awk '{if (/^b/) { if ($5 == "58,") { exit 0; } else { exit 1; } } else { exit 1; }}'
550         rc=$?
551
552         if [ $rc = 0 ]; then
553                 debug "$node is LVM1 node"
554                 # is lvm1
555                 return 0
556         fi
557
558         node=$(_lvm2_node_resolve $node)
559         /sbin/lvm lvdisplay "$node" > /dev/null 2>&1
560         rc=$?
561         if [ $rc -gt 127 ]; then
562                 # lvdisplay terminated by signal! most likely it segfaulted.
563                 echo >&2 "ERROR: Unexpected exit from 'lvdisplay $node': $rc - are your lvm tools broken?"
564                 exit 1
565         fi
566
567         if [ $rc = 0 ]; then
568                 debug "$node is lvm2 node"
569         else
570                 debug "$node is not any lvm node"
571         fi
572         return $rc
573 }
574
575 find_modules_for() {
576         if [ -z "$1" ]; then
577                 echo "ERROR: no argument passed to find_modules_for() - is your /etc/fstab correct?" >&2
578                 exit
579         fi
580
581         if is_yes "`echo "$1" | awk '/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:|\/dev\/nfs)/ { print "yes"; }'`"; then
582                 if [ ! -x /usr/bin/pcidev -a -z "$NFS_ETH_MODULES" ] ; then
583                         echo "ERROR: root on NFS but /usr/bin/pcidev not found." >&2
584                         echo "Please install correct pci-database package and rerun $0." >&2
585                         exit 1
586                 fi
587                 [ -z "$NFS_ETH_MODULES" ] && NFS_ETH_MODULES=$(/usr/bin/pcidev /m net | xargs)
588                 for m in $NFS_ETH_MODULES; do
589                         findmodule "$m"
590                 done
591                 findmodule "-ipv4"
592                 findmodule "nfs"
593                 usenfs="yes"
594                 echo "Remember to use \`root=/dev/ram0 init=/linuxrc' when starting kernel" >&2
595                 echo "or you will have problems like init(xx) being child process of swapper(1)." >&2
596         elif is_yes "`echo "$1" | awk '/^\/dev\/md/ { print "yes"; }'`"; then
597                 find_modules_softraid "$1"
598         elif is_yes "$(echo "$1" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')" ; then
599                 find_modules_scsi
600         elif is_yes "`echo "$1" | awk '/^\/dev\/(hd|ide)/ { print "yes"; }'`" ; then
601                 find_modules_ide "$1"
602         elif is_yes "`echo "$1" | awk '/\/dev\/rd\// { print "yes"; }'`" ; then
603                 findmodule "DAC960"
604         elif is_yes "`echo "$1" | awk '/\/dev\/ida\// { print "yes"; }'`" ; then
605                 findmodule "cpqarray"
606         elif is_yes "`echo "$1" | awk '/\/dev\/cciss\// { print "yes"; }'`" ; then
607                 findmodule "cciss"
608         elif is_yes "`echo "$1" | awk '/\/dev\/ataraid\// { print "yes"; }'`"; then
609                 find_modules_ide
610                 findmodule "ataraid"
611                 ataraidmodules="`awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
612                 if -n "$ataraidmodules" ; then
613                         # FIXME: think about modules compiled in kernel
614                         echo "ERROR: ataraid_hostadapter alias not defined in $modulefile !"
615                         echo "Please set it and run $0 again."
616                         exit 1
617                 fi
618                 for n in $ataraidmodules; do
619                         findmodule "$n"
620                 done
621         # check to see if we need to set up a loopback filesystem
622         elif is_yes "`echo "$1" | awk -F/ '{print($3);}' | awk '/loop/ { print "yes"; }'`" ; then
623                 echo >&2 "Sorry, root on loop device isn't supported."
624                 exit 1
625                 # TODO: rewrite for bsp and make nfs ready
626                 if [ ! -x /sbin/losetup ]; then
627                         echo "losetup is missing"
628                         exit 1
629                 fi
630                 key="^# $(echo $1 | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
631                 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`" ; then
632                         echo >&2 "The root filesystem is on a $1, but there is no magic entry in $fstab"
633                         echo >&2 "for this device. Consult the geninitrd man page for more information"
634                         exit 1
635                 fi
636
637                 line="`awk '/'$key'/ { print $0; }' $fstab`"
638                 loopDev="$(echo $line | awk '{print $3}')"
639                 loopFs="$(echo $line | awk '{print $4}')"
640                 loopFile="$(echo $line | awk '{print $5}')"
641
642                 BASICMODULES="$BASICMODULES -loop"
643                 findmodule "-$loopFs"
644                 BASICMODULES="$BASICMODULES -${loopFs}"
645         elif _check_lvm "$1"; then
646                 node=$(_lvm2_node_resolve "$1")
647
648                 if [ ! -f /sbin/initrd-lvm -o ! -x /sbin/lvdisplay -o ! -x /sbin/pvdisplay ] ; then
649                         echo "ERROR: root on LVM but /sbin/initrd-lvm, /sbin/lvdisplay and /sbin/pvdisplay not found." >&2
650                         echo "Please install lvm(2) and lvm(2)-initrd package and rerun $0." >&2
651                         exit 1
652                 fi
653                 if [ -z "$LVMTOOLSVERSION" ] ; then
654                         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}')
655                         if [ -z "$LVMTOOLSVERSION" ] ; then
656                                 echo "ERROR: Can't determine LVM tools version. Please set LVMTOOLSVERSION" >&2
657                                 echo "and rerun $0." >&2
658                                 exit 1
659                         fi
660                 fi
661                 if [ -z "$PVDEVICES" ] ; then
662                         VGVOLUME=$(/sbin/lvdisplay -c "$node" 2> /dev/null | awk -F":" ' { print $2 } ')
663                         PVDEVICES=$(/sbin/pvdisplay -c | awk -F":" -v vg="$VGVOLUME" ' BEGIN { devices="" } { if ($2 == vg) { devices = devices " " $1 } } END { print devices } ')
664                 fi
665                 if [ -n "$PVDEVICES" ] ; then
666                         for device in $PVDEVICES; do
667                                 find_modules_for $device
668                         done
669                 else
670                         echo "ERROR: I wasn't able to find PV (via lvdisplay and pvdisplay)." >&2
671                         echo "You can try to set PVDEVICES in /etc/sysconfig/geninitrd." >&2
672                         exit 1
673                 fi
674                 if [ "$LVMTOOLSVERSION" = "2" ]; then
675                         findmodule "-dm-mod"
676                 elif [ "$LVMTOOLSVERSION" = "1" ]; then
677                         findmodule "-lvm"
678                         findmodule "-lvm-mod"
679                 else
680                         echo "ERROR: LVM version $LVMTOOLSVERSION is not supported yet." >&2
681                         exit 1
682                 fi
683                 debug "LVM $LVMTOOLSVERSION enabled"
684                 uselvm="yes"
685         fi
686 }
687
688 firmware_install_module_pre() {
689         module="$1"
690         linuxrc="$2"
691         firmware_files="$3"
692
693         debug "Adding Firmwares ($firmware_files) to initrd for module $module"
694         mkdir -p $MNTIMAGE/proc
695         mkdir -p $MNTIMAGE/sys
696         # firmware not yet installed
697         if [ ! -f "$MNTIMAGE/lib/firmware/firmware.sh" ]; then
698                 mkdir -p $MNTIMAGE/lib/firmware
699 cat << 'EOF' >> "$MNTIMAGE/lib/firmware/firmware.sh"
700 #!/bin/sh -e
701 echo 1 > /sys$DEVPATH/loading
702 cat "/lib/firmware/$FIRMWARE" > /sys$DEVPATH/data
703 echo 0 > /sys$DEVPATH/loading
704 exit 0
705 EOF
706                 chmod 755 "$MNTIMAGE/lib/firmware/firmware.sh"
707         fi
708
709         for firmware in $firmware_files; do
710                 inst "/lib/firmware/$firmware" "$MNTIMAGE/lib/firmware/$firmware"
711         done
712
713         echo "mount -t proc none /proc" >> "$linuxrc"
714         echo "mount -t sysfs none /sys" >> "$linuxrc"
715         echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" >> "$linuxrc"
716 }
717
718 firmware_install_module_post() {
719         module="$1"
720         linuxrc="$2"
721         firmware_files="$3"
722         echo "umount /sys" >> "$linuxrc"
723         echo "umount /proc" >> "$linuxrc"
724 }
725
726 modules_install() {
727         modules="$1"
728
729         for mod in $modules; do
730                 MODULEDIR="`dirname "$mod"`"
731                 mkdir -p "$MNTIMAGE/lib/modules/$kernel/$MODULEDIR"
732                 cp -a "/lib/modules/$kernel/$mod" "$MNTIMAGE/lib/modules/$kernel/$mod"
733                 gunzip "$MNTIMAGE/lib/modules/$kernel/$mod" 2> /dev/null
734         done
735 }
736
737 modules_add_linuxrc() {
738         modules="$1"
739         linuxrc="$2"
740
741         for mod in $modules; do
742                 MODULE2="`dirname "$mod"`"
743                 NAME2=`basename "$mod" .gz`
744                 MODULE2=$MODULE2/$NAME2
745                 module="`echo $mod | awk -F/ '{ $0=$NF } /'$modext'.*$/ { gsub(/'$modext'.*/, NIL, $0); } { print $0; }'`"
746                 options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"
747
748                 generic_module=$(echo "${module}" | awk ' { gsub("-", "_", $0) } { print $0; } ')
749                 sleep_var="$(eval echo \$MODULE_${generic_module}_USLEEP)"
750                 firmware_var="$(eval echo \$MODULE_${generic_module}_FIRMWARE)"
751
752                 if [ -n "$verbose" ]; then
753                         echo -n "Loading module [$module] "
754                         if [ -n "$options" ] ; then
755                                 echo -n "with options [$options]"
756                         else
757                                 echo -n "without options"
758                         fi
759                         if [ -n "$sleep_var" ]; then
760                                 echo " and $sleep_var usleep."
761                         else
762                                 echo "."
763                         fi
764                 fi
765
766                 if [ -n "$firmware_var" ]; then
767                         firmware_install_module_pre "$module" "$linuxrc" "$firmware_var"
768                 fi
769                 echo "$insmod /lib/modules/$kernel/$MODULE2 $options" >> "$linuxrc"
770                 if [ -n "${sleep_var}" ]; then
771                         echo "usleep $sleep_var" >> "$linuxrc"
772                 fi
773                 if [ -n "$firmware_var" ]; then
774                         firmware_install_module_post "$module" "$linuxrc" "$firmware_var"
775                 fi
776         done
777 }
778
779 if [ -r /etc/sysconfig/geninitrd ] ; then
780         . /etc/sysconfig/geninitrd
781 fi
782
783 if [ -r /etc/sysconfig/bootsplash ] ; then
784         . /etc/sysconfig/bootsplash
785 fi
786
787 if [ -r /etc/sysconfig/fbsplash ] ; then
788         . /etc/sysconfig/fbsplash
789 fi
790
791 if [ ! -x /bin/initrd-busybox ] ; then
792         echo "/bin/initrd-busybox is missing !"
793         exit 1
794 fi
795
796 while [ $# -gt 0 ]; do
797         case $1 in
798         --fstab=*)
799                 fstab="`echo $1 | awk -F= '{print $2;}'`"
800                 ;;
801         --fstab)
802                 fstab="$2"
803                 shift
804                 ;;
805         --modules-conf=*)
806                 modulefile="`echo $1 | awk -F= '{print $2;}'`"
807                 ;;
808         --modules-conf)
809                 modulefile="$2"
810                 shift
811                 ;;
812         --use-raidstart|--with-raidstart)
813                 USERAIDSTART="yes"
814                 ;;
815         --without-raidstart)
816                 USERAIDSTART="no"
817                 ;;
818         --use-insmod-static|--with-insmod-static)
819                 USEINSMODSTATIC="yes"
820                 ;;
821         --without-insmod-static)
822                 USEINSMODSTATIC="no"
823                 ;;
824         --without-bootsplash)
825                 BOOT_SPLASH="no"
826                 ;;
827         --without-fbsplash)
828                 FB_SPLASH="no"
829                 ;;
830         --without-suspend)
831                 USE_SUSPEND="no";
832                 ;;
833         --without-suspend2)
834                 USE_SUSPEND2="no";
835                 ;;
836         --lvmtoolsversion=|--lvmversion=)
837                 LVMTOOLSVERSION="`echo $1 | awk -F= '{print $2;}'`"
838                 ;;
839         --lvmtoolsversion|--lvmversion)
840                 LVMTOOLSVERSION="$2"
841                 shift
842                 ;;
843         --without-udev)
844                 USE_UDEV=
845                 ;;
846         --with-udev)
847                 USE_UDEV="yes"
848                 ;;
849         --without-dmraid)
850                 USE_DMRAID=
851                 ;;
852         --with=*)
853                 BASICMODULES="$BASICMODULES `echo $1 | awk -F= '{print $2;}'`"
854                 ;;
855         --with)
856                 BASICMODULES="$BASICMODULES $2"
857                 shift
858                 ;;
859         --version)
860                 echo "geninitrd: version $VERSION"
861                 exit 0
862                 ;;
863         -v)
864                 verbose=-v
865                 ;;
866         --nocompress)
867                 COMPRESS="no"
868                 ;;
869         --ifneeded)
870                 ifneeded=1
871                 ;;
872         -f)
873                 force=1
874                 ;;
875         --preload=*)
876                 PREMODS="$PREMODS `echo $1 | awk -F= '{print $2;}'`"
877                 ;;
878         --preload)
879                 PREMODS="$PREMODS $2"
880                 shift
881                 ;;
882         --fs=*)
883                 echo >&2 "Warning: --fs option is obsoleted. Use --initrdfs instead"
884                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
885                 ;;
886         --fs)
887                 echo >&2 "Warning: --fs option is obsoleted. Use --initrdfs instead"
888                 INITRDFS="$2"
889                 shift
890                 ;;
891         --initrdfs=*)
892                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
893                 ;;
894         --initrdfs)
895                 INITRDFS="$2"
896                 shift
897                 ;;
898         --image-version)
899                 img_vers=yes
900                 ;;
901         --ide-only-root)
902                 ide_only_root="yes"
903                 ;;
904         *)
905                 if [ -z "$target" ]; then
906                         target="$1"
907                 elif [ -z "$kernel" ]; then
908                         kernel="$1"
909                 else
910                         usage
911                 fi
912                 ;;
913         esac
914
915         shift
916 done
917
918 if [ -z "$target" -o -z "$kernel" ]; then
919         usage
920 fi
921
922 pack_version="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d",$1,$2)}'`"
923 pack_version_long="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}'`"
924
925 if [ "x" = "x$INITRDFS" ] ; then
926         if [ "x" = "x$FS" ] ; then
927                 # default value
928                 if [ "$pack_version" -ge "002005" ] ; then
929                         INITRDFS="initramfs"
930                 else
931                         INITRDFS="rom"
932                 fi
933         else
934                 echo >&2 "Warning: FS configuration options is obsoleted. Use INITRDFS instead"
935                 INITRDFS="$FS"
936         fi
937 fi
938
939 if [ "$pack_version" -lt "002006" ] ; then
940         USE_UDEV=
941         USE_DMRAID=
942 fi
943
944 if [ "$pack_version" -ge "002005" ] ; then
945         modext=".ko"
946         insmod="insmod"
947 fi
948
949 if is_yes "$USEINSMODSTATIC" ; then
950         insmod="insmod.static"
951         INSMOD="/sbin/insmod.static"
952         if [ "$pack_version" -lt "002005" -a -f /sbin/insmod.static.modutils ] ; then
953                 INSMOD="/sbin/insmod.static.modutils"
954         fi
955         if [ ! -f "$INSMOD" ] ; then
956                 echo "insmod.static requested but /sbin/insmod.static not found !" >&2
957                 exit 1
958         fi
959 fi
960
961 case "$INITRDFS" in
962         ext2)
963                 if [ ! -x /sbin/mke2fs ]; then
964                         echo >&2 "/sbin/mke2fs is missing"
965                         exit 1
966                 fi
967                 ;;
968         rom)
969                 if [ ! -x /sbin/genromfs ]; then
970                         echo >&2 "/sbin/genromfs is missing"
971                         exit 1
972                 fi
973                 ;;
974         cram)
975                 if [ ! -x /sbin/mkcramfs ]; then
976                         echo >&2 "/sbin/mkcramfs is missing"
977                         exit 1
978                 fi
979                 ;;
980         initramfs)
981                 if [ ! -x /bin/cpio ]; then
982                         echo >&2 "/bin/cpio is missing"
983                         exit 1
984                 fi
985                 if [ ! -x /usr/bin/find ]; then
986                         echo >&2 "/usr/bin/find is missing"
987                         exit 1
988                 fi
989                 ;;
990         *)
991                 echo >&2 "Filesystem $INITRDFS on initrd is not supported"
992                 exit 1
993                 ;;
994 esac
995
996 if [ -n "$img_vers" ]; then
997         target="$target-$kernel"
998 fi
999
1000 if [ -z "$force" -a -f "$target" ]; then
1001         echo >&2 "$target already exists."
1002         exit 1
1003 fi
1004
1005 if [ ! -d "/lib/modules/$kernel" ]; then
1006         echo >&2 "/lib/modules/$kernel is not a directory."
1007         exit 1
1008 fi
1009
1010 if is_yes "$USE_SUSPEND"; then
1011         if is_yes "$USE_SUSPEND2"; then
1012                 echo >&2 "Suspend2 shouldn't be used in parallel with mainline suspend!."
1013                 exit 1
1014         fi
1015 fi
1016
1017 if is_yes "$FB_SPLASH"; then
1018         if is_yes "$BOOT_SPLASH"; then
1019                 echo >&2 "You can't use both bootsplash and fbsplash! Please choose one."
1020                 exit 1
1021         elif [ "$INITRDFS" != "initramfs" ]; then
1022                 echo >&2 "FB_SPLASH works only if INITRDFS is initramfs!."
1023                 exit 1
1024         fi
1025 fi
1026
1027 if [ ! -f /proc/mounts ]; then
1028         echo >&2 "WARNING: /proc filesystem not mounted, may cause wrong results or failure."
1029 fi
1030
1031 if [ "$pack_version" -lt "002005" ]; then
1032         modulefile=/etc/modules.conf
1033         if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
1034                 modulefile=/etc/conf.modules
1035         fi
1036 else
1037         modulefile=/etc/modprobe.conf
1038 fi
1039
1040 for n in $PREMODS; do
1041         findmodule "$n"
1042 done
1043
1044 # allow forcing loading SCSI and/or IDE modules
1045 if is_yes "$ADDSCSI" ; then
1046         find_modules_scsi
1047 fi
1048
1049 if is_yes "$ADDIDE" ; then
1050         find_modules_ide
1051 fi
1052
1053 find_root
1054
1055 org_rootdev="$rootdev1"
1056
1057 find_modules_for "$rootdev1"
1058
1059 findmodule "-$rootFs"
1060
1061 for n in $BASICMODULES; do
1062         findmodule "$n"
1063 done
1064
1065 if is_yes "$USE_SUSPEND2"; then
1066         findmodule "-lzf"
1067 fi
1068
1069 if is_yes "$FB_SPLASH"; then
1070         findmodule "-evdev"
1071 fi
1072 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
1073         debug "No modules are needed -- not building initrd image."
1074         exit 0
1075 fi
1076
1077 debug "Using modules: $MODULES"
1078
1079 MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
1080 IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
1081 MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
1082 RCFILE="$MNTIMAGE/linuxrc"
1083
1084 if [ -f "$MNTIMAGE" ]; then
1085         echo >&2 "$MNTIMAGE already exists. Remove it and try again"
1086         exit 1
1087 fi
1088
1089 if [ -f "$IMAGE" ]; then
1090         echo >&2 "$IMAGE already exists. Remove it and try again"
1091         exit 1
1092 fi
1093
1094 mkdir -p "$MNTPOINT"
1095
1096
1097 mkdir -p "$MNTIMAGE"/{lib,bin,etc,dev,loopfs,var}
1098
1099
1100 # We don't need this directory, so let's save space
1101 rm -rf "$MNTPOINT"/lost+found
1102
1103 modules_install "$MODULES"
1104
1105 # mknod'ing the devices instead of copying them works both with and
1106 # without devfs...
1107 mknod "$MNTIMAGE/dev/console" c 5 1
1108 mknod "$MNTIMAGE/dev/null" c 1 3
1109 mknod "$MNTIMAGE/dev/zero" c 1 5
1110
1111 s="$RCFILE"
1112 ln -s /linuxrc $MNTIMAGE/init
1113
1114 inst /bin/initrd-busybox "$MNTIMAGE/bin/initrd-busybox"
1115 ln -s initrd-busybox "$MNTIMAGE/bin/sh"
1116 ln -s initrd-busybox "$MNTIMAGE/bin/busybox" # for older busyboxes who had /bin/busybox as EXEPATH
1117
1118 if is_yes "$USEINSMODSTATIC" ; then
1119         inst "$INSMOD" $MNTIMAGE/bin/insmod.static
1120 fi
1121
1122 cat > "$s" <<EOF
1123 #! /bin/sh
1124
1125 set -x
1126 EOF
1127 chmod 755 "$s"
1128
1129 modules_add_linuxrc "$MODULES" "$s"
1130
1131 # TODO: rewrite for busybox
1132 #if [ -n "$loopDev" ]; then
1133 #       if [ ! -d /initrd ]; then
1134 #               mkdir /initrd
1135 #       fi
1136 #
1137 #       cp -a "$loopDev" "$MNTIMAGE/dev"
1138 #       cp -a "$rootdev1" "$MNTIMAGE/dev"
1139 #       echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1140 #       echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1141 #       echo "echo Setting up loopback device $rootdev1" >> $RCFILE
1142 #       echo "losetup $rootdev1 /loopfs$loopFile" >> "$RCFILE"
1143 #fi
1144
1145 initrd_gen_suspend() {
1146         if [ ! -x /usr/sbin/resume ]; then
1147                 echo "/usr/sbin/resume is missing !"
1148                 exit 1
1149         fi
1150         mkdir -p $MNTIMAGE/etc
1151         mkdir -p $MNTIMAGE/dev
1152         resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
1153         cp -HR /dev/snapshot $resume_dev $MNTIMAGE/dev
1154         inst /etc/suspend.conf $MNTIMAGE/etc/suspend.conf
1155         inst /usr/sbin/resume "$MNTIMAGE/bin/resume"
1156         echo "resume" >> "$s"
1157 }
1158
1159 initrd_gen_suspend2() {
1160         mkdir -p $MNTIMAGE/sys
1161         mkdir -p $MNTIMAGE/proc
1162 cat << 'EOF' >> "$s"
1163 mount -t proc none /proc
1164 mount -t sysfs none /sys
1165 if [ "$(awk ' /resume2=/  { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
1166 EOF
1167 cat << EOF >> "$s"
1168                 [ -e /proc/suspend2/do_resume ] && echo > /proc/suspend2/do_resume
1169                 [ -e /sys/power/suspend2/do_resume ] && echo > /sys/power/suspend2/do_resume
1170 fi
1171 umount /sys
1172 umount /proc
1173 EOF
1174 }
1175
1176 initrd_gen_tmpfs_dev() {
1177         tmpfs_dev=yes
1178         cat <<-EOF
1179 : 'Creating /dev'
1180 mount -o mode=0755 -t tmpfs none /dev
1181 mknod /dev/console c 5 1
1182 mknod /dev/null c 1 3
1183 mknod /dev/zero c 1 5
1184 mkdir /dev/pts
1185 mkdir /dev/shm
1186 EOF
1187 }
1188
1189 initrd_gen_udev() {
1190         debug "Setting up udev..."
1191         mkdir -p $MNTIMAGE/sbin
1192         mkdir -p $MNTIMAGE/proc
1193         mkdir -p $MNTIMAGE/etc/udev
1194         mkdir -p $MNTIMAGE/sys
1195
1196         inst /sbin/initrd-udev $MNTIMAGE/sbin/udev
1197         ln -s udev $MNTIMAGE/sbin/udevstart
1198         inst /etc/udev/udev.conf $MNTIMAGE/etc/udev/udev.conf
1199
1200         ln -s udev $MNTIMAGE/sbin/hotplug
1201
1202         if is_yes "$USE_UDEV"; then
1203                 initrd_gen_tmpfs_dev >> "$s"
1204                 cat >> "$s" <<-EOF
1205 mount -t proc none /proc
1206 mount -t sysfs none /sys
1207 : 'Starting udev'
1208 /sbin/udevstart
1209 echo -n /sbin/hotplug > /proc/sys/kernel/hotplug
1210 umount /proc
1211 umount /sys
1212 EOF
1213         fi
1214 }
1215
1216 initrd_gen_dmraid() {
1217         if [ ! -x /sbin/dmraid-initrd ] ; then
1218                 echo "/sbin/dmraid-initrd is missing missing !"
1219                 exit 1
1220         fi
1221
1222         # AFAIR it just needs tmpfs on /dev.
1223         if [ ! -x $MNTIMAGE/sbin/udev ]; then
1224                 echo "udev is needed on target initrd for dmraid to work!"
1225                 exit 1
1226         fi
1227
1228         mkdir -p "$MNTIMAGE/sbin"
1229         inst /sbin/dmraid-initrd $MNTIMAGE/sbin/dmraid
1230         cat <<-EOF >> "$s"
1231         mount -t proc none /proc
1232         mount -t sysfs none /sys
1233         # 2 secs was enough for my system to initialize. but really this is udev issue?
1234         usleep 2000000
1235         : 'Activating Device-Mapper RAID(s)'
1236         /sbin/dmraid -ay -i
1237         umount /sys
1238         umount /proc
1239 EOF
1240 }
1241
1242
1243 initrd_gen_softraid() {
1244         debug "Setting up mdadm..."
1245
1246         if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ] ; then
1247                 echo "/sbin/mdadm or /sbin/initrd-mdassemble is missing !"
1248                 exit 1
1249         fi
1250
1251         inst /sbin/initrd-mdassemble "$MNTIMAGE/bin/mdassemble"
1252
1253         # LVM on RAID case
1254         dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf)
1255         for ex_dev in $dev_list_extra; do
1256                 echo "DEVICE $ex_dev" >> "$MNTIMAGE/etc/mdadm.conf"
1257         done
1258         do_md0=1
1259         for nr in `seq 1 $rootdev_nr`; do
1260                 eval cr_rootdev="\$rootdev${nr}"
1261                 eval cr_dev_list="\$dev_list${nr}"
1262                 debug echo "Setting up array ($cr_rootdev = $cr_dev_list)"
1263
1264                 [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
1265
1266                 echo "DEVICE $cr_dev_list" >> "$MNTIMAGE/etc/mdadm.conf"
1267                 cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
1268                 cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev)
1269                 if [ -n "$cr_md_conf" ]; then
1270                         echo "$cr_md_conf" >> "$MNTIMAGE/etc/mdadm.conf"
1271                 else
1272                         echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$MNTIMAGE/etc/mdadm.conf"
1273                 fi
1274
1275                 for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
1276                         # mkdir in case of devfs name
1277                         mkdir -p $MNTIMAGE/`dirname $f`
1278                         [ -e "$MNTIMAGE/$f" ] && continue
1279                         debug echo "copying $f"
1280                         # this works fine with and without devfs
1281                         cp -HR $f $MNTIMAGE/$f
1282                 done
1283         done
1284
1285         echo "mdassemble" >> "$s"
1286
1287         # needed to determine md-version
1288         if [ "$do_md0" -eq 1 ] ; then
1289                 mknod $MNTIMAGE/dev/md0 b 9 0
1290         fi
1291 }
1292
1293 initrd_gen_nfs() {
1294         # use root=/dev/ram0 init=/linuxrc when starting kernel or you will
1295         # have problems like init(XX) being child process of swapper(1).
1296         debug "Adding rootfs on NFS support to initrd (dhcp)"
1297         mknod "$MNTIMAGE/dev/urandom" c 1 8
1298         mkdir "$MNTIMAGE/newroot"
1299         mkdir "$MNTIMAGE/proc"
1300         echo "ifconfig lo 127.0.0.1 up" >> "$s"
1301         echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" >> "$s"
1302         echo "ifconfig eth0 0.0.0.0 up" >> "$s"
1303         echo "udhcpc -i eth0 -f -q -s /bin/setdhcp" >> "$s"
1304         cat << 'EOF' > "$MNTIMAGE/bin/setdhcp"
1305 #!/bin/sh
1306 [ "$1" != "bound" ] && exit
1307 [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
1308 [ -n "$subnet" ] && NETMASK="netmask $subnet"
1309 set -x
1310 ifconfig $interface $ip $BROADCAST $NETMASK up
1311 set +x
1312 if [ -n "$router" ]; then
1313         for r in $router; do
1314                 set -x
1315                 route add default gw $r dev $interface
1316                 set +x
1317         done
1318 fi
1319
1320 mount -t proc none /proc
1321 for o in $(cat /proc/cmdline); do
1322         case $o in
1323         nfsroot=*)
1324                 rootpath=${o#nfsroot=}
1325                 ;;
1326         esac
1327 done
1328 umount /proc
1329
1330 if [ -n "$rootpath" ]; then
1331         set -x
1332         mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
1333         set +x
1334 else
1335         set +x
1336         echo "Missing rootpath in what DHCP server sent to us. Failing..."
1337         echo "All seen variables are listed below:"
1338         set
1339         set -x
1340 fi
1341 EOF
1342         chmod 755 "$MNTIMAGE/bin/setdhcp"
1343         echo "cd /newroot" >> "$s"
1344         echo "pivot_root . initrd" >> "$s"
1345         echo "[ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1346         echo "exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1347 }
1348
1349 initrd_gen_lvm() {
1350         debug "Adding LVM support to initrd"
1351         inst /sbin/initrd-lvm $MNTIMAGE/bin/lvm
1352         mkdir -p $MNTIMAGE/etc
1353         mkdir -p $MNTIMAGE/tmp
1354         mkdir -p $MNTIMAGE/proc
1355         mkdir -p $MNTIMAGE/newroot
1356
1357         # always make /dev on tmpfs for LVM2
1358         if [ "$LVMTOOLSVERSION" = "2" ] && ! is_yes "$tmpfs_dev"; then
1359                 initrd_gen_tmpfs_dev >> "$s"
1360         fi
1361
1362         if ! is_yes "$tmpfs_dev"; then
1363                 mkdir -p $MNTIMAGE/dev/mapper
1364                 mknod $MNTIMAGE/dev/mapper/control c 10 63
1365                 for device in $PVDEVICES; do
1366                         # if LVM on RAID then device might be copied already in gen_softraid
1367                         [ -e "$MNTIMAGE/dev/$(basename $device)" ] && continue
1368                         cp -HR $device $MNTIMAGE/dev/
1369                 done
1370         fi
1371         echo "mount -t proc none /proc" >> "$s"
1372         echo "mount -t tmpfs none /tmp" >> "$s"
1373         if [ "$LVMTOOLSVERSION" = "1" ] ; then
1374                 echo "lvm vgscan -T" >> "$s"
1375                 echo "lvm vgchange -T -a y $VGVOLUME" >> "$s"
1376                 echo "umount /tmp" >> "$s"
1377                 # fail to umount
1378                 echo "umount /dev" >> "$s"
1379                 echo "umount /proc" >> "$s"
1380         else
1381                 org_rootdev=$(_lvm2_node_resolve "$org_rootdev")
1382                 echo "cat /etc/lvm.conf > /tmp/lvm.conf" >> "$s"
1383                 echo "global {" > "$MNTIMAGE/etc/lvm.conf"
1384                 echo "  locking_type = 0" >> "$MNTIMAGE/etc/lvm.conf"
1385                 echo "  locking_dir = \"/tmp\"" >> "$MNTIMAGE/etc/lvm.conf"
1386                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1387                 echo "devices {" >> "$MNTIMAGE/etc/lvm.conf"
1388                 echo "  sysfs_scan=0" >> "$MNTIMAGE/etc/lvm.conf"
1389                 if is_yes "$raidfound"; then
1390                         echo "  md_component_detection = 1" >> "$MNTIMAGE/etc/lvm.conf"
1391                 fi
1392                 lvm dumpconfig | awk '/filter=/' >> "$MNTIMAGE/etc/lvm.conf"
1393                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1394
1395                 cat <<-EOF >> "$s"
1396                 : 'Making device nodes'
1397                 set +x
1398                 (
1399                 # ignore first two lines, header and empty line
1400                 read a
1401                 read a
1402                 while read major minor blocks dev; do
1403                         mknod /dev/\$dev b \$major \$minor
1404                 done
1405                 ) < /proc/partitions
1406
1407                 # disable noise from lvm accessing devices that aren't ready.
1408                 printk=\$(cat /proc/sys/kernel/printk)
1409                 echo 0 > /proc/sys/kernel/printk
1410                 set -x
1411
1412                 : 'Scanning for Volume Groups'
1413                 LVM_SYSTEM_DIR=/tmp lvm vgscan --ignorelockingfailure 2>/dev/null
1414
1415                 : 'Activating Volume Groups'
1416                 LVM_SYSTEM_DIR=/tmp lvm vgchange --ignorelockingfailure -a y $VGVOLUME 2>/dev/null
1417
1418                 echo "\$printk" > /proc/sys/kernel/printk
1419
1420                 # Find out major/minor
1421                 majmin="\$(LVM_SYSTEM_DIR=/tmp lvm lvdisplay --ignorelockingfailure -c $org_rootdev 2>/dev/null)"
1422                 majmin="\${majmin#*/}"
1423                 majmin="\${majmin#*:*:*:*:*:*:*:*:*:*:*:*}"
1424                 major="\${majmin%:*}"
1425                 minor="\${majmin#*:}"
1426                 # Pass it to kernel
1427                 val=\$((256 * \$major + \$minor))
1428                 echo \$val > /proc/sys/kernel/real-root-dev
1429                 umount /tmp
1430                 umount /proc
1431 EOF
1432         fi
1433 }
1434
1435 initrd_gen_procdata() {
1436         debug "Adding rootfs finding based on root= option support."
1437         mkdir -p $MNTIMAGE/proc
1438 cat << 'EOF' >> "$s"
1439 set +x
1440 mount -t proc none /proc
1441 root="$(busybox awk ' /root=\/dev\// { gsub(/.*root=\/dev\//,NIL,$0); gsub(/ .*/,NIL,$0); print $0; } ' /proc/cmdline)"
1442 if [ -n "$root" ]; then
1443         rootnr="$(busybox awk -v root="$root" ' { if ($4 == root) { print 256*$1+$2; } } ' /proc/partitions)"
1444         if [ -n "$rootnr" ]; then
1445                 echo "$rootnr" > /proc/sys/kernel/real-root-dev
1446         fi
1447 fi
1448 umount /proc
1449 set -x
1450 EOF
1451 }
1452
1453 # main generation
1454
1455 if is_yes "$USE_UDEV"; then
1456         initrd_gen_udev
1457 fi
1458
1459 if is_yes "$USE_SUSPEND"; then
1460         initrd_gen_suspend
1461 fi
1462
1463 if is_yes "$USE_SUSPEND2"; then
1464         initrd_gen_suspend2
1465 fi
1466
1467 if is_yes "$USE_DMRAID"; then
1468         initrd_gen_dmraid
1469 fi
1470
1471 if is_yes "$usenfs" ; then
1472         initrd_gen_nfs
1473 elif is_yes "$USERAIDSTART" && is_yes "$raidfound" ; then
1474         initrd_gen_softraid
1475         if is_yes "$uselvm" ; then
1476                 initrd_gen_lvm
1477         else
1478                 initrd_gen_procdata
1479         fi
1480 elif is_yes "$uselvm" ; then
1481         initrd_gen_lvm
1482 else
1483         initrd_gen_procdata
1484 fi
1485
1486 if [ "$INITRDFS" = "initramfs" ]; then
1487         mkdir -p $MNTIMAGE/newroot
1488         cp -HR $org_rootdev $MNTIMAGE/dev
1489         # Parsing root parameter
1490         # We support passing root as hda3 /dev/hda3 0303 0x0303
1491         cat << 'EOF' >> "$s"
1492 set +x
1493 mount -t proc none /proc
1494 root="$(busybox awk -v prefix="root=" ' \
1495 function separate_root ( txt ) \
1496 { \
1497     gsub(/.*root=/,NIL,txt); \
1498     gsub(/ .*/,NIL,txt); \
1499     return txt \
1500 } \
1501 BEGIN { \
1502     num_pattern = "[0-9][0-9][0-9][0-9]"; \
1503     dev_pattern = "[hms][a-z][a-z]([0-9])+"; \
1504     partition = "Metallica rocks!"; \
1505     min = -1; maj = -1; \
1506 } \
1507 $0 ~ prefix "0x" num_pattern { sub(/root=0x/,"root="); } \
1508 $0 ~ prefix num_pattern { \
1509     gsub(/.*root=/,NIL,partition); \
1510     gsub(/ .*/,NIL,partition); \
1511     partition = separate_root( $0 ); \
1512     maj = sprintf("%d",substr(partition,1,2)); \
1513     min = sprintf("%d",substr(partition,3)); \
1514 } \
1515 $0 ~ prefix "\/dev\/" dev_pattern { sub(/root=\/dev\//,"root="); } \
1516 $0 ~ prefix dev_pattern { \
1517     partition = separate_root( $0 ); \
1518 } \
1519 $4 ~ partition { maj = $1; min = $2; } \
1520 $1 ~ maj && $2 ~ min { partition = $4; } \
1521 END { print sprintf("/dev/%s %d %d", partition, maj, min); }
1522 ' /proc/cmdline /proc/partitions)"
1523 device=${root% * *}
1524 maj=${root#* }
1525 maj=${maj% *}
1526 min=${root#* * }
1527 set -x
1528 if [ ! -b $device ]; then
1529         mknod $device b $maj $min
1530 fi
1531 EOF
1532         cat << EOF >> "$s"
1533 mount -t $rootFs \$device /newroot 
1534 init="\$(busybox awk ' /init=\// { gsub(/.*init=/,NIL,\$0); gsub(/ .*/,NIL,\$0); print \$0; }  ' /proc/cmdline )"
1535 if [ -z "\$init" -o ! -x "/newroot\$init" ]; then
1536         init=/sbin/init
1537 fi
1538 umount /proc
1539 exec switch_root /newroot \$init
1540 EOF
1541         # we need real file, not symlink
1542         rm -f $MNTIMAGE/init
1543         cp -a $MNTIMAGE/linuxrc $MNTIMAGE/init
1544 fi
1545
1546 chmod +x "$RCFILE"
1547
1548 (cd "$MNTIMAGE"; tar cf - .) | (cd "$MNTPOINT"; tar xf -)
1549
1550 case "$INITRDFS" in
1551         ext2)
1552                 IMAGESIZE=$(du -ks $MNTPOINT | awk '{print int(($1+1023+512)/1024)*1024}')
1553                 debug   "ext2 image size: $IMAGESIZE ($MNTPOINT)"
1554                 if [ "$IMAGESIZE" -gt 4096 ]; then
1555                         echo >&2 "$0: Your image size is larger than 4096, You should add ramdisk_size=$IMAGESIZE to your kernel commandline!"
1556                 fi
1557
1558                 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
1559                 mke2fs -q -F -b 1024 -m 0 "$IMAGE" 2>/dev/null 1>&2
1560                 tune2fs -i 0 "$IMAGE" >/dev/null 2>&1
1561
1562                 tmpmnt="`mktemp -d /tmp/initrd.mnte2-XXXXXX`"
1563                 mount -o loop -t ext2 "$IMAGE" "$tmpmnt"
1564
1565                 (cd "$MNTPOINT"; tar cf - .) | (cd "$tmpmnt"; tar xf -)
1566
1567                 umount "$IMAGE"
1568                 ;;
1569         rom)
1570                 genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
1571                 ;;
1572         cram)
1573                 mkcramfs "$MNTPOINT" "$IMAGE"
1574                 ;;
1575         initramfs)
1576                 (cd $MNTPOINT ; find . | cpio -H newc -o > "$IMAGE")
1577                 ;;
1578         *)
1579                 echo "Filesystem $INITRDFS not supported by $0";
1580 esac
1581
1582 if is_yes "$COMPRESS" ; then
1583         gzip -9 < "$IMAGE" > "$target"
1584 else
1585         cp -a "$IMAGE" "$target"
1586 fi
1587
1588 if is_yes "$BOOT_SPLASH"; then
1589         if [ ! -x /bin/splash.bin ]; then
1590                 echo >&2 "Failed to execute /bin/splash.bin. Is bootsplash package installed?"
1591         elif [ -z "$THEME" ]; then
1592                 echo >&2 "Please configure your /etc/sysconfig/bootsplash first."
1593                 echo >&2 "Generating bootsplashes skipped."
1594         else
1595                 if [ -n "$BOOT_SPLASH_RESOLUTIONS" ]; then
1596                         for res in $BOOT_SPLASH_RESOLUTIONS; do
1597                                 if [ -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
1598                                         /bin/splash.bin -s -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> "$target" && \
1599                                         debug "Added $res $THEME theme to initrd."
1600                                 else
1601                                         echo >&2 "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped"
1602                                 fi
1603                         done
1604                 else
1605                         echo >&2 "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash."
1606                         echo >&2 "Not adding bootsplash to initrd."
1607                 fi
1608         fi
1609 fi
1610
1611 if is_yes "$FB_SPLASH"; then
1612         if [ ! -x /usr/bin/splash_geninitramfs ]; then
1613                 echo >&2 "Failed to execute /usr/bin/splash_geninitramfs. Is splashutils package installed?"
1614         elif [ -z "$SPLASH_THEME" ]; then
1615                 echo >&2 "Please configure your /etc/sysconfig/fbsplash first."
1616                 echo >&2 "Generating fbsplashes skipped."
1617         else
1618                 if [ -n "$FB_SPLASH_RESOLUTIONS" ]; then
1619                         for res in $FB_SPLASH_RESOLUTIONS; do
1620                                 if [ -f "/etc/splash/$SPLASH_THEME/$res.cfg" ]; then
1621                                         /usr/bin/splash_geninitramfs -a $target -r $res $SPLASH_THEME && \
1622                                         debug "Added $res $SPLASH_THEME theme to initramfs."
1623                                 else
1624                                         echo >&2 "/etc/splash/$SPLASH_THEME/$res.cfg doesn't exist, skipped"
1625                                 fi
1626                         done
1627                 else
1628                         echo >&2 "No FB_SPLASH_RESOLUTIONS specified in /etc/sysconfig/fbsplash."
1629                         echo >&2 "Not adding fbsplash to initramfs."
1630                 fi
1631         fi
1632 fi
1633
1634 rm -rf "$MNTIMAGE" "$MNTPOINT" "$IMAGE"
1635
1636 # vim:ts=4:sw=4:noet:fdm=marker
This page took 0.134329 seconds and 2 git commands to generate.