]> git.pld-linux.org Git - projects/geninitrd.git/blob - geninitrd
6be1f31bb2464bbe0cbeeb53622758a8f11d92a3
[projects/geninitrd.git] / geninitrd
1 #!/bin/sh
2
3 # geninitrd
4 #
5 #       by Jacek Konieczny <jajcus@pld-linux.org>
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
45 if [ -f /etc/udev/udev.conf -a -x /sbin/initrd-udev ]; then
46         USE_UDEV="yes"
47         UDEV_TMPFS="yes"
48         . /etc/udev/udev.conf
49 fi
50
51 if [ -x /sbin/dmraid-initrd ]; then
52         USE_DMRAID="yes"
53 fi
54
55 usage () {
56         echo "usage: `basename $0` [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
57         echo "       [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
58         echo "       [--initrdfs=rom|ext2|cram] [--modules-conf=<modules.conf>]"
59         echo "       [--with-raidstart] [--without-raidstart] [--with-insmod-static]"
60         echo "       [--without-bootsplash] [--lvmtoolsversion=1|2] [--without-udev]"
61         echo "       [--without-suspend] [--without-suspend2] [--without-dmraid]"
62         echo "       <initrd-image> <kernel-version>"
63         echo "       (ex: `basename $0` /boot/initrd-2.2.5-15.img 2.2.5-15)"
64         exit 1
65 }
66
67
68 my_dirname() {
69         echo $1|awk -F/ '{print substr($0, 0, length($0) - length($NF));}'
70 }
71
72 find_depmod () {
73         typeset mods module f level depfile first
74
75         depfile=/lib/modules/$kernel/modules.dep
76
77         if [ -f $depfile ] ; then
78                 : ok
79         else
80                 echo "Error: no $depfile ! Run depmod and rerun geninitrd." 1>&2
81                 exit 1
82         fi
83
84         # prepend / if no path given, append $modext.gz if not given,
85         # quote /
86         origmodule="$2"
87         module=$(echo "$2" | \
88                 awk '/\// {print;next} {print "/" $0}' | \
89                 awk '/\./ {print;next} {print $0 "'$modext'.gz"}' |
90                 awk '{gsub("/","\\/");print}')
91         mods=$(awk '
92 BEGIN { here = 0 }
93 /'"$module"':(.*)/ { gsub(/:/," "); gsub(/\\/," "); print; here = 1; next }
94 /:/ { here = 0 }
95 /(.*)/ { gsub(/\\/," "); if (here) print }
96 ' $depfile | xargs)
97
98         # fallback to $modext
99         if [ "$mods" = "" ] ; then
100                 module=$(echo "$module" | \
101                 awk '{gsub("\'$modext'\.gz$","\'$modext'",$0);print}')
102         fi
103         mods=$(awk '
104 BEGIN { here = 0 }
105 /'"$module"':(.*)/ { gsub(/:/," "); gsub(/\\/," "); print; here = 1; next }
106 /:/ { here = 0 }
107 /(.*)/ { gsub(/\\/," "); if (here) print }
108 ' $depfile | xargs)
109
110         if [ "$mods" = "" ] ; then
111                 if [ "$1" != silent ] ; then
112                         echo "$origmodule: module not found in $depfile" 1>&2
113                 fi
114                 if ! is_no "$EXIT_IF_MISSING" ; then
115                         exit 1
116                 else
117                         echo "If $origmodule isn't compiled in kernel then this initrd may not start your system". 1>&2
118                 fi
119         fi
120
121         level=$3
122         if [ "$level" = "" ] ; then
123                 level=0
124         fi
125         level=$(awk "BEGIN{print $level + 1}")
126         if [ $level -gt 20 ] ; then
127                 echo "$origmodule: cycle in $depfile" 1>&2
128                 exit 1
129         fi
130
131         first=
132         for f in $mods ; do
133                 if [ "$first" = "" ] ; then
134                         first=$f
135                 else
136                         find_depmod $1 $f $level
137                 fi
138         done
139
140         echo $first
141 }
142
143 addmodule() {
144         fmPath=$1
145         skiperrors=$2
146
147         if [ ! -f "/lib/modules/$kernel/$fmPath" ]; then
148                 if [ -n "$skiperrors" ]; then
149                         return
150                 fi
151
152                 echo "module $fmPath present in modules.dep, but not in filesystem (kernel = $kernel)" 1>&2
153                 exit 1
154         fi
155
156         # only need to add each module once
157         # quote /
158         tmpFmPath=$(echo $fmPath | awk '{gsub(/\//,"\\/");print}')
159         if echo "$MODULES" | awk '/'"$tmpFmPath"'/ {exit 1}' ; then
160                 MODULES="$MODULES $fmPath"
161         fi
162 }
163
164 findmodule() {
165         skiperrors=""
166         modName=$1
167
168         if [ "$(echo $modName | awk '{print(substr($0,1,1));}')" = "-" ]; then
169                 skiperrors=1
170                 modName="$(echo $modName | awk '{print(substr($0,2));}')"
171         fi
172
173         # what's that?
174         if [ "$modName" = "pluto" ]; then
175                 findmodule fc4
176                 findmodule soc
177         fi
178         if [ "$modName" = "fcal" ]; then
179                 findmodule fc4
180                 findmodule socal
181         fi
182
183         if [ -n "$skiperrors" ]; then
184                 allModulesToFind=`find_depmod silent $modName`
185         else
186                 allModulesToFind=`find_depmod normal $modName`
187                 if [ $? != 0 ] ; then
188                         exit 1
189                 fi
190         fi
191
192         for mod in $allModulesToFind ; do
193                 mod=$(echo $mod | awk '{sub(/^\/lib\/modules\/[^\/]*\//,"");print}')
194                 addmodule $mod "$skiperrors"
195         done
196 }
197
198 inst() {
199         if [ "$#" != "2" ];then
200                 echo "usage: inst <file> <destination>"
201                 return
202         fi
203         [ -n "$verbose" ] && echo "$1 -> $2"
204         cp "$1" "$2"
205 }
206
207 get_label_ext2 () {
208         /sbin/e2label $1 2> /dev/null
209 }
210
211 get_uuid_ext2 () {
212         /sbin/tune2fs -l $1 2> /dev/null | awk -F: '/UUID:/ {gsub(" ",""); print $2}'
213 }
214
215 get_label_xfs () {
216         /usr/sbin/xfs_db -x -p xfs_admin -c label -r "$1"|awk -F= '{sub("^\"","", $2); sub("\"$", "", $2); print $2}'
217
218 }
219
220 get_uuid_xfs () {
221         /usr/sbin/xfs_db -x -p xfs_admin -c uuid -r "$1"|awk -F= '{print $2}'
222 }
223
224 find_root() {
225         eval `awk '/^[\t ]*#/ {next} {if ( $2 == "/" ) {print "rootdev=\"" $1 "\"\nrootFs=\"" $3 "\""}}' $fstab`
226         case $rootdev in
227         LABEL=*)
228                 if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
229                         rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
230                         if [ -n "$rootdev2" ] ; then
231                                 rootdev=$rootdev2
232                                 rootdev_found=1
233                         fi
234                 fi
235                 if [ "$rootdev_found." != "1." ] ; then
236                         case $rootFs in
237                         ext2|ext3)
238                                 if [ ! -x /sbin/e2label ] ; then
239                                         echo "/sbin/e2label is missing" 1>&2
240                                         exit 1
241                                 fi
242                                 get_label="get_label_ext2"
243                                 ;;
244                         xfs)
245                                 if [ ! -x /usr/sbin/xfs_db ] ; then
246                                         echo "/usr/sbin/xfs_db is missing" 1>&2
247                                         exit 1
248                                 fi
249                                 get_label="get_label_xfs"
250                                 ;;
251                         *)
252                                 echo "LABEL on $rootFs in not supported by geninitrd" 1>&2
253                                 exit 1
254                                 ;;
255                         esac
256                         if [ ! -r /proc/partitions ] ; then
257                                 echo '/proc/partitions is not readable'; 1>&2
258                                 exit 1
259                         fi
260                         label=${rootdev#"LABEL="}
261                         for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
262                                 if [ -r $dev ] && [ "$label" = "`$get_label $dev`" ] ; then
263                                         if [ -n "$verbose" ] ; then
264                                                 echo "Using $dev as device for rootfs"
265                                         fi
266                                         rootdev=$dev
267                                         rootdev_found=1
268                                         break
269                                 fi
270                         done
271                         if [ "$rootdev_found." != "1." ] ; then
272                                 echo "geninitrd can't find real device for LABEL=$label" 1>&2
273                                 exit 1
274                         fi
275                 fi
276                 ;;
277         UUID=*)
278                 if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
279                         rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
280                         if [ -n "$rootdev2" ] ; then
281                                 rootdev=$rootdev2
282                                 rootdev_found=1
283                         fi
284                 fi
285                 if [ "$rootdev_found." != "1." ] ; then
286                         case $rootFs in
287                         ext2|ext3)
288                                 if [ ! -x /sbin/tune2fs ] ; then
289                                         echo "/sbin/tune2fs is missing" 1>&2
290                                         exit 1
291                                 fi
292                                 get_uuid="get_uuid_ext2"
293                                 ;;
294                         xfs)
295                                 if [ ! -x /usr/sbin/xfs_db ] ; then
296                                         echo "/usr/sbin/xfs_db is missing" 1>&2
297                                         exit 1
298                                 fi
299                                 get_label="get_uuid_xfs"
300                                 ;;
301                         *)
302                                 echo "UUID on $rootFs in not supported by geninitrd" 1>&2
303                                 exit 1
304                                 ;;
305                         esac
306                         if [ ! -r /proc/partitions ] ; then
307                                 echo '/proc/partitions is not readable'; 1>&2
308                                 exit 1
309                         fi
310                         uuid=${rootdev#"UUID="}
311                         for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
312                                 if [ -r $dev ] && [ "$uuid" = "`$get_uuid $dev`" ] ; then
313                                         if [ -n "$verbose" ] ; then
314                                                 echo "Using $dev as device for rootfs"
315                                         fi
316                                         rootdev=$dev
317                                         rootdev_found=1
318                                         break
319                                 fi
320                         done
321                         if [ "$rootdev_found" != 1 ] ; then
322                                 echo "geninitrd can't find real device for UUID=$uuid" 1>&2
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                 [ -n "$verbose" ] && echo "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 "ERROR: raidtools are not longer supported. Please migrate to mdadm setup!" 1>&2
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 "raid level $number (in mdadm config) not recognized" 1>&2
384                         ;;
385                 esac
386         else
387                 echo "ERROR: RAID devices not found for \"$1\", check your configuration!" 1>&2
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                 [ -n "$verbose" ] && echo "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                                         [ -n "$verbose" ] && echo "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                                                 [ -n "$verbose" ] && echo "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 # return true if node is lvm node
516 _check_lvm() {
517         local node="$1"
518         if [ ! -e "$node" ]; then
519                 return 1
520         fi
521
522         # block-major-58 is lvm1
523         ls -lL "$node" 2> /dev/null | awk '{if (/^b/) { if ($5 == "58,") { exit 0; } else { exit 1; } } else { exit 1; }}'
524         rc=$?
525
526         if [ $rc = 0 ]; then
527                 [ -n "$verbose" ] && echo "$node is LVM1 node"
528                 # is lvm1
529                 return 0
530         fi
531
532         /sbin/lvm lvdisplay "$node" > /dev/null 2>&1
533         rc=$?
534         if [ $rc -gt 127 ]; then
535                 # lvdisplay terminated by signal! most likely it segfaulted.
536                 echo >&2 "ERROR: Unexpected exit from 'lvdisplay $node': $rc - are your lvm tools broken?"
537                 exit 1
538         fi
539
540         if [ $rc = 0 ]; then
541                 [ -n "$verbose" ] && echo "$node is lvm2 node"
542         else
543                 [ -n "$verbose" ] && echo "$node is not any lvm node"
544         fi
545         return $rc
546 }
547
548 find_modules_for() {
549         if [ -z "$1" ]; then
550                 echo "ERROR: no argument passed to find_modules_for() - is your /etc/fstab correct?" >&2
551                 exit
552
553         elif is_yes "`echo "$1" | awk '/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:|\/dev\/nfs)/ { print "yes"; }'`"; then
554                 if [ ! -x /usr/bin/pcidev -a -z "$NFS_ETH_MODULES" ] ; then
555                         echo "ERROR: root on NFS but /usr/bin/pcidev not found." >&2
556                         echo "Please install correct pci-database package and rerun $0." >&2
557                         exit 1
558                 fi
559                 [ -z "$NFS_ETH_MODULES" ] && NFS_ETH_MODULES=$(/usr/bin/pcidev /m net | xargs)
560                 for m in $NFS_ETH_MODULES; do
561                         findmodule "$m"
562                 done
563                 findmodule "-ipv4"
564                 findmodule "nfs"
565                 usenfs="yes"
566                 echo "Remember to use \`root=/dev/ram0 init=/linuxrc' when starting kernel" >&2
567                 echo "or you will have problems like init(xx) being child process of swapper(1)." >&2
568         elif is_yes "`echo "$1" | awk '/^\/dev\/md/ { print "yes"; }'`"; then
569                 find_modules_softraid "$1"
570         elif is_yes "$(echo "$1" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')" ; then
571                 find_modules_scsi
572         elif is_yes "`echo "$1" | awk '/^\/dev\/(hd|ide)/ { print "yes"; }'`" ; then
573                 find_modules_ide "$1"
574         elif is_yes "`echo "$1" | awk '/\/dev\/rd\// { print "yes"; }'`" ; then
575                 findmodule "DAC960"
576         elif is_yes "`echo "$1" | awk '/\/dev\/ida\// { print "yes"; }'`" ; then
577                 findmodule "cpqarray"
578         elif is_yes "`echo "$1" | awk '/\/dev\/cciss\// { print "yes"; }'`" ; then
579                 findmodule "cciss"
580         elif is_yes "`echo "$1" | awk '/\/dev\/ataraid\// { print "yes"; }'`"; then
581                 find_modules_ide
582                 findmodule "ataraid"
583                 ataraidmodules="`awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
584                 if -n "$ataraidmodules" ; then
585                         # FIXME: think about modules compiled in kernel
586                         echo "ERROR: ataraid_hostadapter alias not defined in $modulefile !"
587                         echo "Please set it and run $0 again."
588                         exit 1
589                 fi
590                 for n in $ataraidmodules; do
591                         findmodule "$n"
592                 done
593         # check to see if we need to set up a loopback filesystem
594         elif is_yes "`echo "$1" | awk -F/ '{print($3);}' | awk '/loop/ { print "yes"; }'`" ; then
595                 echo "Sorry, root on loop device isn't supported." 1>&2
596                 exit 1
597                 # TODO: rewrite for bsp and make nfs ready
598                 if [ ! -x /sbin/losetup ]; then
599                         echo "losetup is missing"
600                         exit 1
601                 fi
602                 key="^# $(echo $1 | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
603                 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`" ; then
604                         echo "The root filesystem is on a $1, but there is no magic entry in $fstab" 1>&2
605                         echo "for this device. Consult the geninitrd man page for more information" 1>&2
606                         exit 1
607                 fi
608
609                 line="`awk '/'$key'/ { print $0; }' $fstab`"
610                 loopDev="$(echo $line | awk '{print $3}')"
611                 loopFs="$(echo $line | awk '{print $4}')"
612                 loopFile="$(echo $line | awk '{print $5}')"
613
614                 BASICMODULES="$BASICMODULES -loop"
615                 findmodule "-$loopFs"
616                 BASICMODULES="$BASICMODULES -${loopFs}"
617         elif _check_lvm "$1"; then
618                 if [ ! -f /sbin/initrd-lvm -o ! -x /sbin/lvdisplay -o ! -x /sbin/pvdisplay ] ; then
619                         echo "ERROR: root on LVM but /sbin/initrd-lvm, /sbin/lvdisplay and /sbin/pvdisplay not found." >&2
620                         echo "Please install lvm(2) and lvm(2)-initrd package and rerun $0." >&2
621                         exit 1
622                 fi
623                 if [ -z "$LVMTOOLSVERSION" ] ; then
624                         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}')
625                         if [ -z "$LVMTOOLSVERSION" ] ; then
626                                 echo "ERROR: Can't determine LVM tools version. Please set LVMTOOLSVERSION" >&2
627                                 echo "and rerun $0." >&2
628                                 exit 1
629                         fi
630                 fi
631                 if [ -z "$PVDEVICES" ] ; then
632                         VGVOLUME=$(/sbin/lvdisplay -c "$1" 2> /dev/null | awk -F":" ' { print $2 } ')
633                         PVDEVICES=$(/sbin/pvdisplay -c | awk -F":" -v vg="$VGVOLUME" ' BEGIN { devices="" } { if ($2 == vg) { devices = devices " " $1 } } END { print devices } ')
634                 fi
635                 if [ -n "$PVDEVICES" ] ; then
636                         for device in $PVDEVICES; do
637                                 find_modules_for $device
638                         done
639                 else
640                         echo "ERROR: I wasn't able to find PV (via lvdisplay and pvdisplay)." >&2
641                         echo "You can try to set PVDEVICES in /etc/sysconfig/geninitrd." >&2
642                         exit 1
643                 fi
644                 if [ "$LVMTOOLSVERSION" = "2" ]; then
645                         findmodule "-dm-mod"
646                 elif [ "$LVMTOOLSVERSION" = "1" ]; then
647                         findmodule "-lvm"
648                         findmodule "-lvm-mod"
649                 else
650                         echo "ERROR: LVM version $LVMTOOLSVERSION is not supported yet." >&2
651                         exit 1
652                 fi
653                 [ -n "$verbose" ] && echo "LVM $LVMTOOLSVERSION enabled"
654                 uselvm="yes"
655         fi
656 }
657
658 modules_install() {
659         modules="$1"
660
661         for mod in $modules; do
662                 MODULEDIR="`my_dirname "$mod"`"
663                 mkdir -p "$MNTIMAGE/lib/modules/$kernel/$MODULEDIR"
664                 cp $verbose -a "/lib/modules/$kernel/$mod" "$MNTIMAGE/lib/modules/$kernel/$mod"
665                 gunzip "$MNTIMAGE/lib/modules/$kernel/$mod" 2> /dev/null
666         done
667 }
668
669 modules_add_linuxrc() {
670         modules="$1"
671         linuxrc="$2"
672
673         for mod in $modules; do
674                 MODULE2="`my_dirname "$mod"`"
675                 NAME2=`basename "$mod" .gz`
676                 MODULE2=$MODULE2/$NAME2
677                 module="`echo $mod | awk -F/ '{ $0=$NF } /'$modext'.*$/ { gsub(/'$modext'.*/, NIL, $0); } { print $0; }'`"
678                 options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"
679
680                 sleep_module=$(echo "${module}" | awk ' { gsub("-", "_", $0) } { print $0; } ')
681                 sleep_var="$(eval echo \$MODULE_${sleep_module}_USLEEP)"
682
683                 if [ -n "$verbose" ]; then
684                         echo -n "Loading module [$module] "
685                         if [ -n "$options" ] ; then
686                                 echo -n "with options [$options]"
687                         else
688                                 echo -n "without options"
689                         fi
690                         if [ -n "$sleep_var" ]; then
691                                 echo " and $sleep_var usleep."
692                         else
693                                 echo "."
694                         fi
695                 fi
696
697                 echo "$insmod /lib/modules/$kernel/$MODULE2 $options" >> "$linuxrc"
698                 if [ -n "${sleep_var}" ]; then
699                         echo "usleep $sleep_var" >> "$linuxrc"
700                 fi
701         done
702 }
703
704 if [ -r /etc/sysconfig/geninitrd ] ; then
705         . /etc/sysconfig/geninitrd
706 fi
707
708 if [ -r /etc/sysconfig/bootsplash ] ; then
709         . /etc/sysconfig/bootsplash
710 fi
711
712 if [ ! -x /bin/initrd-busybox ] ; then
713         echo "/bin/initrd-busybox is missing !"
714         exit 1
715 fi
716 case "$(uname -m)" in
717 ia64|amd64|x86_64|sparc64)
718         IMAGESIZE=3000
719         ;;
720 *)
721         IMAGESIZE=1500
722         ;;
723 esac
724
725 while [ $# -gt 0 ]; do
726         case $1 in
727         --fstab=*)
728                 fstab="`echo $1 | awk -F= '{print $2;}'`"
729                 ;;
730         --fstab)
731                 fstab="$2"
732                 shift
733                 ;;
734         --modules-conf=*)
735                 modulefile="`echo $1 | awk -F= '{print $2;}'`"
736                 ;;
737         --modules-conf)
738                 modulefile="$2"
739                 shift
740                 ;;
741         --use-raidstart|--with-raidstart)
742                 USERAIDSTART="yes"
743                 ;;
744         --without-raidstart)
745                 USERAIDSTART="no"
746                 ;;
747         --use-insmod-static|--with-insmod-static)
748                 USEINSMODSTATIC="yes"
749                 ;;
750         --without-insmod-static)
751                 USEINSMODSTATIC="no"
752                 ;;
753         --without-bootsplash)
754                 BOOT_SPLASH="no"
755                 ;;
756         --without-suspend)
757                 USE_SUSPEND="no";
758                 ;;
759         --without-suspend2)
760                 USE_SUSPEND2="no";
761                 ;;
762         --lvmtoolsversion=|--lvmversion=)
763                 LVMTOOLSVERSION="`echo $1 | awk -F= '{print $2;}'`"
764                 ;;
765         --lvmtoolsversion|--lvmversion)
766                 LVMTOOLSVERSION="$2"
767                 shift
768                 ;;
769         --without-udev)
770                 USE_UDEV=
771                 ;;
772         --without-dmraid)
773                 USE_DMRAID=
774                 ;;
775         --with=*)
776                 BASICMODULES="$BASICMODULES `echo $1 | awk -F= '{print $2;}'`"
777                 ;;
778         --with)
779                 BASICMODULES="$BASICMODULES $2"
780                 shift
781                 ;;
782         --version)
783                 echo "geninitrd: version $VERSION"
784                 exit 0
785                 ;;
786         -v)
787                 verbose=-v
788                 ;;
789         --nocompress)
790                 COMPRESS="no"
791                 ;;
792         --ifneeded)
793                 ifneeded=1
794                 ;;
795         -f)
796                 force=1
797                 ;;
798         --preload=*)
799                 PREMODS="$PREMODS `echo $1 | awk -F= '{print $2;}'`"
800                 ;;
801         --preload)
802                 PREMODS="$PREMODS $2"
803                 shift
804                 ;;
805         --fs=*)
806                 echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
807                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
808                 ;;
809         --fs)
810                 echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
811                 INITRDFS="$2"
812                 shift
813                 ;;
814         --initrdfs=*)
815                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
816                 ;;
817         --initrdfs)
818                 INITRDFS="$2"
819                 shift
820                 ;;
821         --image-version)
822                 img_vers=yes
823                 ;;
824         --ide-only-root)
825                 ide_only_root="yes"
826                 ;;
827         *)
828                 if [ -z "$target" ]; then
829                         target="$1"
830                 elif [ -z "$kernel" ]; then
831                         kernel="$1"
832                 else
833                         usage
834                 fi
835                 ;;
836         esac
837
838         shift
839 done
840
841 if [ -z "$target" -o -z "$kernel" ]; then
842         usage
843 fi
844
845 pack_version="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d",$1,$2)}'`"
846 pack_version_long="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}'`"
847
848 if [ "x" = "x$INITRDFS" ] ; then
849         if [ "x" = "x$FS" ] ; then
850                 # default value
851                 # XXX: initramfs blocked for now
852                 if [ "1" = " 0" -a "$pack_version" -ge "002005" ] ; then
853                         INITRDFS="initramfs"
854                 else
855                         INITRDFS="rom"
856                 fi
857         else
858                 echo "Warning: FS configuration options is obsoleted. Use INITRDFS instead" 1>&2
859                 INITRDFS="$FS"
860         fi
861 fi
862
863 if [ "$pack_version" -lt "002006" ] ; then
864         USE_UDEV=
865         USE_DMRAID=
866 fi
867 [ -z "$USE_UDEV" ] && UDEV_TMPFS=
868
869 if [ "$pack_version" -ge "002005" ] ; then
870         modext=".ko"
871         insmod="insmod"
872 fi
873
874 if is_yes "$USEINSMODSTATIC" ; then
875         insmod="insmod.static"
876         INSMOD="/sbin/insmod.static"
877         if [ "$pack_version" -lt "002005" -a -f /sbin/insmod.static.modutils ] ; then
878                 INSMOD="/sbin/insmod.static.modutils"
879         fi
880         if [ ! -f "$INSMOD" ] ; then
881                 echo "insmod.static requested but /sbin/insmod.static not found !" >&2
882                 exit 1
883         fi
884 fi
885
886 case "$INITRDFS" in
887         ext2)
888                 IMAGESIZE=$((IMAGESIZE + 100))
889                 if [ ! -x /sbin/mke2fs ]; then
890                         echo "/sbin/mke2fs is missing" 1>&2
891                         exit 1
892                 fi
893                 ;;
894         rom)
895                 if [ ! -x /sbin/genromfs ]; then
896                         echo "/sbin/genromfs is missing" 1>&2
897                         exit 1
898                 fi
899                 ;;
900         cram)
901                 if [ ! -x /sbin/mkcramfs ]; then
902                         echo "/sbin/mkcramfs is missing" 1>&2
903                         exit 1
904                 fi
905                 ;;
906         initramfs)
907                 if [ ! -x /bin/cpio ]; then
908                         echo "/bin/cpio is missing" 1>&2
909                         exit 1
910                 fi
911                 if [ ! -x /usr/bin/find ]; then
912                         echo "/usr/bin/find is missing" 1>&2
913                         exit 1
914                 fi
915                 ;;
916         *)
917                 echo "Filesystem $INITRDFS on initrd is not supported" 1>&2
918                 exit 1
919                 ;;
920 esac
921
922 if [ -n "$img_vers" ]; then
923         target="$target-$kernel"
924 fi
925
926 if [ -z "$force" -a -f "$target" ]; then
927         echo "$target already exists." 1>&2
928         exit 1
929 fi
930
931 if [ ! -d "/lib/modules/$kernel" ]; then
932         echo "/lib/modules/$kernel is not a directory." 1>&2
933         exit 1
934 fi
935
936 if is_yes "$USE_SUSPEND"; then
937         if is_yes "$USE_SUSPEND2"; then
938                 echo "Suspend2 shouldn't be used in parallel with mainline suspend!." 1>&2
939                 exit 1
940         fi
941 fi
942
943 if [ ! -f /proc/mounts ]; then
944         echo "WARNING: /proc filesystem not mounted, may cause wrong results or failure." 1>&2
945 fi
946
947 if [ "$pack_version" -lt "002005" ]; then
948         modulefile=/etc/modules.conf
949         if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
950                 modulefile=/etc/conf.modules
951         fi
952 else
953         modulefile=/etc/modprobe.conf
954 fi
955
956 for n in $PREMODS; do
957         findmodule "$n"
958 done
959
960 # allow forcing loading SCSI and/or IDE modules
961 if is_yes "$ADDSCSI" ; then
962         find_modules_scsi
963 fi
964
965 if is_yes "$ADDIDE" ; then
966         find_modules_ide
967 fi
968
969 find_root
970
971 org_rootdev="$rootdev1"
972
973 find_modules_for "$rootdev1"
974
975 findmodule "-$rootFs"
976
977 for n in $BASICMODULES; do
978         findmodule "$n"
979 done
980
981 if is_yes "$USE_SUSPEND2"; then
982         findmodule "-lzf"
983 fi
984
985 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
986         if [ -n "$verbose" ]; then
987                 echo "No modules are needed -- not building initrd image."
988         fi
989         exit 0
990 fi
991
992 if [ -n "$verbose" ]; then
993         echo "Using modules: $MODULES"
994 fi
995
996 MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
997 IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
998 MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
999 RCFILE="$MNTIMAGE/linuxrc"
1000
1001 if [ -f "$MNTIMAGE" ]; then
1002         echo "$MNTIMAGE already exists. Remove it and try again" 1>&2
1003         exit 1
1004 fi
1005
1006 if [ -f "$IMAGE" ]; then
1007         echo "$IMAGE already exists. Remove it and try again" 1>&2
1008         exit 1
1009 fi
1010
1011 if [ "$INITRDFS" = "ext2" ] ; then
1012         dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
1013
1014         # We have to "echo y |" so that it doesn't complain about $IMAGE not
1015         # being a block device
1016         echo y | mke2fs -F "$IMAGE" "$IMAGESIZE" >/dev/null 2>/dev/null
1017
1018         mkdir -p "$MNTPOINT"
1019         mount -o loop -t ext2 "$IMAGE" "$MNTPOINT"
1020 else
1021         mkdir -p "$MNTPOINT"
1022 fi
1023
1024
1025 mkdir -p "$MNTIMAGE"/{lib,bin,etc,dev,loopfs,var}
1026
1027
1028 # We don't need this directory, so let's save space
1029 rm -rf "$MNTPOINT"/lost+found
1030
1031 modules_install "$MODULES"
1032
1033 # mknod'ing the devices instead of copying them works both with and
1034 # without devfs...
1035 mknod "$MNTIMAGE/dev/console" c 5 1
1036 mknod "$MNTIMAGE/dev/null" c 1 3
1037 mknod "$MNTIMAGE/dev/zero" c 1 5
1038
1039 s="$RCFILE"
1040 ln -s /linuxrc $MNTIMAGE/init
1041
1042 inst /bin/initrd-busybox "$MNTIMAGE/bin/sh"
1043 ln -s sh "$MNTIMAGE/bin/busybox"
1044
1045 if is_yes "$USEINSMODSTATIC" ; then
1046         inst "$INSMOD" $MNTIMAGE/bin/insmod.static
1047 fi
1048
1049 cat > "$s" <<EOF
1050 #! /bin/sh
1051
1052 set -x
1053 EOF
1054 chmod 755 "$s"
1055
1056 modules_add_linuxrc "$MODULES" "$s"
1057
1058 # TODO: rewrite for busybox
1059 #if [ -n "$loopDev" ]; then
1060 #       if [ ! -d /initrd ]; then
1061 #               mkdir /initrd
1062 #       fi
1063 #
1064 #       cp -a "$loopDev" "$MNTIMAGE/dev"
1065 #       cp -a "$rootdev1" "$MNTIMAGE/dev"
1066 #       echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1067 #       echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1068 #       echo "echo Setting up loopback device $rootdev1" >> $RCFILE
1069 #       echo "losetup $rootdev1 /loopfs$loopFile" >> "$RCFILE"
1070 #fi
1071
1072 initrd_gen_suspend() {
1073         if [ ! -x /usr/sbin/resume ]; then
1074                 echo "/usr/sbin/resume is missing !"
1075                 exit 1
1076         fi
1077         mkdir -p $MNTIMAGE/etc
1078         mkdir -p $MNTIMAGE/dev
1079         resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
1080         cp -HR /dev/snapshot $resume_dev $MNTIMAGE/dev
1081         inst /etc/suspend.conf $MNTIMAGE/etc/suspend.conf
1082         inst /usr/sbin/resume "$MNTIMAGE/bin/resume"
1083         echo "resume" >> "$s"
1084 }
1085
1086 initrd_gen_suspend2() {
1087         mkdir -p $MNTIMAGE/sys
1088         mkdir -p $MNTIMAGE/proc
1089 cat << 'EOF' >> "$s"
1090 mount -t proc none /proc
1091 if [ "$(awk ' /resume2=/  { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
1092 EOF
1093 cat << EOF >> "$s"
1094                 echo > /proc/suspend2/do_resume
1095 fi
1096 umount /proc
1097 EOF
1098 }
1099
1100 initrd_gen_udev() {
1101         [ -n "$verbose" ] && echo "Setting up udev..."
1102         mkdir -p $MNTIMAGE/sbin
1103         mkdir -p $MNTIMAGE/proc
1104         mkdir -p $MNTIMAGE/etc/udev
1105
1106         inst /sbin/initrd-udev $MNTIMAGE/sbin/udev
1107         ln -s udev $MNTIMAGE/sbin/udevstart
1108         inst /etc/udev/udev.conf $MNTIMAGE/etc/udev/udev.conf
1109
1110         ln -s udev $MNTIMAGE/sbin/hotplug
1111
1112         if is_yes "$USE_UDEV"; then
1113                 if is_yes "$UDEV_TMPFS"; then
1114                         cat >> "$s" <<-EOF
1115 echo Creating /dev
1116 mount -o mode=0755 -t tmpfs none /dev
1117 mknod /dev/console c 5 1
1118 mknod /dev/null c 1 3
1119 mknod /dev/zero c 1 5
1120 mkdir /dev/pts
1121 mkdir /dev/shm
1122 EOF
1123                 fi
1124                 cat >> "$s" <<-EOF
1125 mount -t proc none /proc
1126 echo Starting udev
1127 /sbin/udevstart
1128 echo -n "/sbin/hotplug" > /proc/sys/kernel/hotplug
1129 umount /proc
1130 EOF
1131         fi
1132 }
1133
1134 initrd_gen_dmraid() {
1135         if [ ! -x /sbin/dmraid-initrd ] ; then
1136                 echo "/sbin/dmraid-initrd is missing missing !"
1137                 exit 1
1138         fi
1139
1140         if [ ! -x $MNTIMAGE/sbin/udev ]; then
1141                 echo "udev is needed on target initrd for dmraid to work!"
1142                 exit 1
1143         fi
1144
1145         mkdir -p "$MNTIMAGE/sbin"
1146         inst /sbin/dmraid-initrd $MNTIMAGE/sbin/dmraid
1147 cat <<-EOF >> "$s"
1148         mount -t proc none /proc
1149         mount -t sysfs none /sys
1150         # 2 secs was enough for my system to initialize. but really this is udev issue?
1151         usleep 2000000
1152         echo 'Activating Device-Mapper RAID(s)'
1153         /sbin/dmraid -ay -i
1154         umount /sys
1155         umount /proc
1156 EOF
1157 }
1158
1159
1160 initrd_gen_softraid() {
1161         [ -n "$verbose" ] && echo "Setting up mdadm..."
1162
1163         if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ] ; then
1164                 echo "/sbin/mdadm or /sbin/initrd-mdassemble is missing !"
1165                 exit 1
1166         fi
1167
1168         inst /sbin/initrd-mdassemble "$MNTIMAGE/bin/mdassemble"
1169
1170         # LVM on RAID case
1171         dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf)
1172         for ex_dev in $dev_list_extra; do
1173                 echo "DEVICE $ex_dev" >> "$MNTIMAGE/etc/mdadm.conf"
1174         done
1175         do_md0=1
1176         for nr in `seq 1 $rootdev_nr`; do
1177                 eval cr_rootdev="\$rootdev${nr}"
1178                 eval cr_dev_list="\$dev_list${nr}"
1179                 [ -n "$verbose" ] && echo "Setting up array ($cr_rootdev = $cr_dev_list)"
1180
1181                 [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
1182
1183                 echo "DEVICE $cr_dev_list" >> "$MNTIMAGE/etc/mdadm.conf"
1184                 cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
1185                 cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev)
1186                 if [ -n "$cr_md_conf" ]; then
1187                         echo "$cr_md_conf" >> "$MNTIMAGE/etc/mdadm.conf"
1188                 else
1189                         echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$MNTIMAGE/etc/mdadm.conf"
1190                 fi
1191
1192                 for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
1193                         # mkdir in case of devfs name
1194                         mkdir -p $MNTIMAGE/`my_dirname $f`
1195                         [ -e "$MNTIMAGE/$f" ] && continue
1196                         [ -n "$verbose" ] && echo "copying $f"
1197                         # this works fine with and without devfs
1198                         cp -HR $f $MNTIMAGE/$f
1199                 done
1200         done
1201
1202         echo "mdassemble" >> "$s"
1203
1204         # needed to determine md-version
1205         if [ "$do_md0" -eq 1 ] ; then
1206                 mknod $MNTIMAGE/dev/md0 b 9 0
1207         fi
1208 }
1209
1210 initrd_gen_nfs() {
1211         # use root=/dev/ram0 init=/linuxrc when starting kernel or you will
1212         # have problems like init(XX) being child process of swapper(1).
1213         [ -n "$verbose" ] && echo "Adding rootfs on NFS support to initrd (dhcp)"
1214         mknod "$MNTIMAGE/dev/urandom" c 1 8
1215         mkdir "$MNTIMAGE/newroot"
1216         mkdir "$MNTIMAGE/proc"
1217         echo "ifconfig lo 127.0.0.1 up" >> "$s"
1218         echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" >> "$s"
1219         echo "ifconfig eth0 0.0.0.0 up" >> "$s"
1220         echo "udhcpc -i eth0 -f -q -s /bin/setdhcp" >> "$s"
1221         cat << 'EOF' > "$MNTIMAGE/bin/setdhcp"
1222 #!/bin/sh
1223 [ "$1" != "bound" ] && exit
1224 [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
1225 [ -n "$subnet" ] && NETMASK="netmask $subnet"
1226 set -x
1227 ifconfig $interface $ip $BROADCAST $NETMASK up
1228 set +x
1229 if [ -n "$router" ]; then
1230         for r in $router; do
1231                 set -x
1232                 route add default gw $r dev $interface
1233                 set +x
1234         done
1235 fi
1236
1237 mount -t proc none /proc
1238 for o in $(cat /proc/cmdline); do
1239         case $o in
1240         nfsroot=*)
1241                 rootpath=${o#nfsroot=}
1242                 ;;
1243         esac
1244 done
1245 umount /proc
1246
1247 if [ -n "$rootpath" ]; then
1248         set -x
1249         mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
1250         set +x
1251 else
1252         set +x
1253         echo "Missing rootpath in what DHCP server sent to us. Failing..."
1254         echo "All seen variables are listed below:"
1255         set
1256         set -x
1257 fi
1258 EOF
1259         chmod 755 "$MNTIMAGE/bin/setdhcp"
1260         echo "cd /newroot" >> "$s"
1261         echo "pivot_root . initrd" >> "$s"
1262         echo "[ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1263         echo "exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1264 }
1265
1266 initrd_gen_lvm() {
1267         [ -n "$verbose" ] && echo "Adding LVM support to initrd"
1268         inst /sbin/initrd-lvm $MNTIMAGE/bin/lvm
1269         mkdir -p $MNTIMAGE/etc
1270         mkdir -p $MNTIMAGE/tmp
1271         mkdir -p $MNTIMAGE/proc
1272         mkdir -p $MNTIMAGE/newroot
1273         if ! is_yes "$USE_UDEV"; then
1274                 mkdir -p $MNTIMAGE/dev/mapper
1275                 mknod $MNTIMAGE/dev/mapper/control c 10 63
1276                 for device in $PVDEVICES; do
1277                         # if LVM on RAID then device might be copied already in gen_softraid
1278                         [ -e "$MNTIMAGE/dev/$(basename $device)" ] && continue
1279                         cp -HR $device $MNTIMAGE/dev/
1280                 done
1281         fi
1282         echo "mount -t proc none /proc" >> "$s"
1283         echo "mount -t tmpfs none /tmp" >> "$s"
1284         if [ "$LVMTOOLSVERSION" = "1" ] ; then
1285                 echo "lvm vgscan -T" >> "$s"
1286                 echo "lvm vgchange -T -a y $VGVOLUME" >> "$s"
1287                 echo "umount /tmp" >> "$s"
1288                 # fail to umount
1289                 echo "umount /dev" >> "$s"
1290                 echo "umount /proc" >> "$s"
1291         else
1292                 echo "cat /etc/lvm.conf > /tmp/lvm.conf" >> "$s"
1293                 echo "global {" > "$MNTIMAGE/etc/lvm.conf"
1294                 echo "  locking_type = 0" >> "$MNTIMAGE/etc/lvm.conf"
1295                 echo "  locking_dir = \"/tmp\"" >> "$MNTIMAGE/etc/lvm.conf"
1296                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1297                 echo "devices {" >> "$MNTIMAGE/etc/lvm.conf"
1298                 echo "  sysfs_scan=0" >> "$MNTIMAGE/etc/lvm.conf"
1299                 if is_yes "$raidfound"; then
1300                         echo "  md_component_detection = 1" >> "$MNTIMAGE/etc/lvm.conf"
1301                 fi
1302                 lvm dumpconfig | awk '/filter=/' >> "$MNTIMAGE/etc/lvm.conf"
1303                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1304
1305                 cat <<-EOF >> "$s"
1306                 : 'Scanning for Volume Groups'
1307                 LVM_SYSTEM_DIR=/tmp lvm vgscan --ignorelockingfailure
1308                 : 'Activating Volume Groups'
1309                 LVM_SYSTEM_DIR=/tmp lvm vgchange --ignorelockingfailure -a y $VGVOLUME 2>/dev/null
1310
1311                 # Find out major/minor
1312                 majmin="\`LVM_SYSTEM_DIR=/tmp lvm lvdisplay --ignorelockingfailure -c $org_rootdev\`"
1313                 majmin="\${majmin#*/}"
1314                 majmin="\${majmin#*:*:*:*:*:*:*:*:*:*:*:*}"
1315                 major="\${majmin%:*}"
1316                 minor="\${majmin#*:}"
1317                 # Pass it to kernel
1318                 val=\$((256 * \$major + \$minor))
1319                 echo \$val > /proc/sys/kernel/real-root-dev
1320                 umount /tmp
1321                 umount /proc
1322 EOF
1323         fi
1324 }
1325
1326 initrd_gen_procdata() {
1327         [ -n "$verbose" ] && echo "Adding rootfs finding based on root= option support."
1328         mkdir -p $MNTIMAGE/proc
1329 cat << 'EOF' >> "$s"
1330 set +x
1331 mount -t proc none /proc
1332 root="$(busybox awk ' /root=\/dev\// { gsub(/.*root=\/dev\//,NIL,$0); gsub(/ .*/,NIL,$0); print $0; } ' /proc/cmdline)"
1333 if [ -n "$root" ]; then
1334         rootnr="$(busybox awk -v root="$root" ' { if ($4 == root) { print 256*$1+$2; } } ' /proc/partitions)"
1335         if [ -n "$rootnr" ]; then
1336                 echo "$rootnr" > /proc/sys/kernel/real-root-dev
1337         fi
1338 fi
1339 umount /proc
1340 set -x
1341 EOF
1342 }
1343
1344 # main generation
1345
1346 if is_yes "$USE_UDEV"; then
1347         initrd_gen_udev
1348 fi
1349
1350 if is_yes "$USE_SUSPEND"; then
1351         initrd_gen_suspend
1352 fi
1353
1354 if is_yes "$USE_SUSPEND2"; then
1355         initrd_gen_suspend2
1356 fi
1357
1358 if is_yes "$USE_DMRAID"; then
1359         initrd_gen_dmraid
1360 fi
1361
1362 if is_yes "$usenfs" ; then
1363         initrd_gen_nfs
1364 elif is_yes "$USERAIDSTART" && is_yes "$raidfound" ; then
1365         initrd_gen_softraid
1366         if is_yes "$uselvm" ; then
1367                 initrd_gen_lvm
1368         else
1369                 initrd_gen_procdata
1370         fi
1371 elif is_yes "$uselvm" ; then
1372         initrd_gen_lvm
1373 else
1374         initrd_gen_procdata
1375 fi
1376
1377 if [ "$INITRDFS" = "initramfs" ]; then
1378         mkdir -p $MNTIMAGE/newroot
1379         cp -HR $org_rootdev $MNTIMAGE/dev
1380         echo "mount -t $rootFs $org_rootdev /newroot" >> "$s"
1381         echo "switch_root /newroot /sbin/init" >> "$s"
1382         # we need real file, not symlink
1383         rm -f $MNTIMAGE/init
1384         cp -a $MNTIMAGE/linuxrc $MNTIMAGE/init
1385 fi
1386
1387 chmod +x "$RCFILE"
1388
1389 (cd "$MNTIMAGE"; tar cf - .) | (cd "$MNTPOINT"; tar xf -)
1390
1391 case "$INITRDFS" in
1392         ext2)
1393                 umount "$IMAGE"
1394                 ;;
1395         rom)
1396                 genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
1397                 ;;
1398         cram)
1399                 mkcramfs "$MNTPOINT" "$IMAGE"
1400                 ;;
1401         initramfs)
1402                 (cd $MNTPOINT ; find . | cpio -H newc -o > "$IMAGE")
1403                 ;;
1404         *)
1405                 echo "Filesystem $INITRDFS not supported by $0";
1406 esac
1407
1408 if is_yes "$COMPRESS" ; then
1409         gzip -9 < "$IMAGE" > "$target"
1410 else
1411         cp -a "$IMAGE" "$target"
1412 fi
1413
1414 if is_yes "$BOOT_SPLASH"; then
1415         if [ ! -x /bin/splash.bin ]; then
1416                 echo "Failed to execute /bin/splash.bin. Is bootsplash package installed?" 1>&2
1417         elif [ -z "$THEME" ]; then
1418                 echo "Please configure your /etc/sysconfig/bootsplash first." 1>&2
1419                 echo "Generating bootsplashes skipped." 1>&2
1420         else
1421                 if [ -n "$BOOT_SPLASH_RESOLUTIONS" ]; then
1422                         for res in $BOOT_SPLASH_RESOLUTIONS; do
1423                                 if [ -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
1424                                         /bin/splash.bin -s -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> "$target" && \
1425                                         [ -n "$verbose" ] && echo "Added $res $THEME theme to initrd."
1426                                 else
1427                                         echo "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped" 1>&2
1428                                 fi
1429                         done
1430                 else
1431                         echo "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash." 1>&2
1432                         echo "Not adding bootsplash to initrd." 1>&2
1433                 fi
1434         fi
1435 fi
1436
1437 rm -rf "$MNTIMAGE" "$MNTPOINT" "$IMAGE"
1438
1439 # vim:ts=4:sw=4:noet:fdm=marker
This page took 0.132414 seconds and 2 git commands to generate.