]> git.pld-linux.org Git - projects/geninitrd.git/blob - geninitrd
Detect broken tool (lvdisplay) and fail early instead of generating invalid initrd
[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 | \
194                       awk '{sub(/^\/lib\/modules\/[^\/]*\//,"");print}')
195                 addmodule $mod "$skiperrors"
196         done
197 }
198
199 inst() {
200         if [ "$#" != "2" ];then
201                 echo "usage: inst <file> <destination>"
202                 return
203         fi
204         [ -n "$verbose" ] && echo "$1 -> $2"
205         cp "$1" "$2"
206 }
207
208 get_label_ext2 () {
209         /sbin/e2label $1 2> /dev/null
210 }
211
212 get_uuid_ext2 () {
213         /sbin/tune2fs -l $1 2> /dev/null | awk -F: '/UUID:/ {gsub(" ",""); print $2}'
214 }
215
216 get_label_xfs () {
217         /usr/sbin/xfs_db -x -p xfs_admin -c label -r "$1"|awk -F= '{sub("^\"","", $2); sub("\"$", "", $2); print $2}'
218
219 }
220
221 get_uuid_xfs () {
222         /usr/sbin/xfs_db -x -p xfs_admin -c uuid -r "$1"|awk -F= '{print $2}'
223 }
224
225 find_root() {
226         eval `awk '/^[\t ]*#/ {next} {if ( $2 == "/" ) {print "rootdev=\"" $1 "\"\nrootFs=\"" $3 "\""}}' $fstab`
227         case $rootdev in
228         LABEL=*)
229                 if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
230                         rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
231                         if [ -n "$rootdev2" ] ; then
232                                 rootdev=$rootdev2
233                                 rootdev_found=1
234                         fi
235                 fi
236                 if [ "$rootdev_found." != "1." ] ; then
237                         case $rootFs in
238                         ext2|ext3)
239                                 if [ ! -x /sbin/e2label ] ; then
240                                         echo "/sbin/e2label is missing" 1>&2
241                                         exit 1
242                                 fi
243                                 get_label="get_label_ext2"
244                                 ;;
245                         xfs)
246                                 if [ ! -x /usr/sbin/xfs_db ] ; then
247                                         echo "/usr/sbin/xfs_db is missing" 1>&2
248                                         exit 1
249                                 fi
250                                 get_label="get_label_xfs"
251                                 ;;
252                         *)
253                                 echo "LABEL on $rootFs in not supported by geninitrd" 1>&2
254                                 exit 1
255                                 ;;
256                         esac
257                         if [ ! -r /proc/partitions ] ; then
258                                 echo '/proc/partitions is not readable'; 1>&2
259                                 exit 1
260                         fi
261                         label=${rootdev#"LABEL="}
262                         for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
263                                 if [ -r $dev ] && [ "$label" = "`$get_label $dev`" ] ; then
264                                         if [ -n "$verbose" ] ; then
265                                                 echo "Using $dev as device for rootfs"
266                                         fi
267                                         rootdev=$dev
268                                         rootdev_found=1
269                                         break
270                                 fi
271                         done
272                         if [ "$rootdev_found." != "1." ] ; then
273                                 echo "geninitrd can't find real device for LABEL=$label" 1>&2
274                                 exit 1
275                         fi
276                 fi
277                 ;;
278         UUID=*)
279                 if [ -x /sbin/findfs -a \( "$rootFs." = "ext2." -o "$rootFs." = "ext3." \) ] ; then
280                         rootdev2="`/sbin/findfs $rootdev 2>/dev/null`"
281                         if [ -n "$rootdev2" ] ; then
282                                 rootdev=$rootdev2
283                                 rootdev_found=1
284                         fi
285                 fi
286                 if [ "$rootdev_found." != "1." ] ; then
287                         case $rootFs in
288                         ext2|ext3)
289                                 if [ ! -x /sbin/tune2fs ] ; then
290                                         echo "/sbin/tune2fs is missing" 1>&2
291                                         exit 1
292                                 fi
293                                 get_uuid="get_uuid_ext2"
294                                 ;;
295                         xfs)
296                                 if [ ! -x /usr/sbin/xfs_db ] ; then
297                                         echo "/usr/sbin/xfs_db is missing" 1>&2
298                                         exit 1
299                                 fi
300                                 get_label="get_uuid_xfs"
301                                 ;;
302                         *)
303                                 echo "UUID on $rootFs in not supported by geninitrd" 1>&2
304                                 exit 1
305                                 ;;
306                         esac
307                         if [ ! -r /proc/partitions ] ; then
308                                 echo '/proc/partitions is not readable'; 1>&2
309                                 exit 1
310                         fi
311                         uuid=${rootdev#"UUID="}
312                         for dev in `awk 'BEGIN {getline;getline} {print "/dev/" $4}' /proc/partitions` ; do
313                                 if [ -r $dev ] && [ "$uuid" = "`$get_uuid $dev`" ] ; then
314                                         if [ -n "$verbose" ] ; then
315                                                 echo "Using $dev as device for rootfs"
316                                         fi
317                                         rootdev=$dev
318                                         rootdev_found=1
319                                         break
320                                 fi
321                         done
322                         if [ "$rootdev_found" != 1 ] ; then
323                                 echo "geninitrd can't find real device for UUID=$uuid" 1>&2
324                                 exit 1
325                         fi
326                 fi
327                 ;;
328         esac
329         rootdev1=${rootdev}
330 }
331
332 find_modules_softraid() {
333         if [ -f /etc/mdadm.conf ] ; then
334             [ -n "$verbose" ] && echo "Finding RAID details using mdadm for rootdev=$1"
335             eval `/sbin/mdadm -v --examine  --scan --config=/etc/mdadm.conf | \
336                   awk -v rootdev="$1" '
337         BEGIN {
338                 found = "no";
339                 dev_list = "";
340                 raidlevel = ""
341                 rootdev_devfs = rootdev;
342                 if (rootdev ~ /\/dev\/md\/[0-9]/) {
343                     gsub(/\/dev\/md\//,"/dev/md",rootdev_devfs);
344                 }
345         }
346
347         /^ARRAY/ {
348             if (($2 == rootdev) || ($2 == rootdev_devfs)) {
349                 raidlevel=$3;
350                 gsub(/level=/,NUL,raidlevel);
351                 if (raidlevel ~ /^raid([0-6]|10)/) {
352                     gsub(/raid/,NUL,raidlevel);
353                 };
354                 found="yes";
355                 getline x;
356                 if (x ~ /devices=/) {
357                         dev_list = x;
358                         gsub(".*devices=", NUL, dev_list);
359                         gsub(",", " ", dev_list);
360                 }
361             }
362         }
363  
364         END {
365                 print "raidfound=" found;
366                 print "raidlevel=" raidlevel;
367                 print "dev_list=\"" dev_list "\"";
368         }'`
369         fi
370
371         if [ "$raidfound" != "yes" -a -f /etc/raidtab ]; then
372                 echo "ERROR: raidtools are not longer supported. Please migrate to mdadm setup!" 1>&2
373                 exit 1
374         fi
375         
376         if is_yes "$raidfound" ; then
377                 case "$raidlevel" in
378                 [01456]|10)
379                         findmodule "raid$raidlevel"
380                         ;;
381                 linear)
382                         findmodule "linear"
383                         ;;
384                 *)
385                         echo "raid level $number (in mdadm config) not recognized" 1>&2
386                         ;;
387                 esac
388         else
389                 echo "ERROR: RAID devices not found for \"$1\", check your configuration!" 1>&2
390                 exit 1
391         fi
392
393         rootdev_nr=$(( $rootdev_nr + 1 ))
394         eval "rootdev${rootdev_nr}=\"$1\""
395         eval "dev_list${rootdev_nr}=\"${dev_list}\""
396         
397         for device in $dev_list; do
398                 find_modules_for $device
399         done
400 }
401
402 find_modules_scsi() {
403         for n in $PRESCSIMODS; do
404                 if [ "X$n" = "Xunknown" ] ; then
405                         if [ -f "$modulefile" ]; then
406                                 scsimodules="`awk '/scsi_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
407                                 for n in $scsimodules; do
408                         # for now allow scsi modules to come from anywhere. There are some
409                         # RAID controllers with drivers in block
410                                         findmodule "$n"
411                                 done
412                         fi
413                 else
414                         findmodule "$n"
415                 fi
416         done
417 }
418
419 find_modules_ide() {
420         typeset rootdev
421
422         rootdev="$(echo "$1" | awk ' { gsub(/\/dev\//,NUL); gsub(/[0-9].*/, NUL); print $0 } ')"
423         if [ "$pack_version_long" -lt "002004021" ]; then
424             [ -n "$verbose" ] && echo "Finding IDE modules for kernels <= 2.4.20"
425             for n in $PREIDEMODSOLD; do
426                 findmodule "$n"
427             done
428         else
429             tryauto=1
430             for n in $PREIDEMODS; do
431                 if [ "X$n" = "Xunknown" ] ; then
432                     if [ -f "$modulefile" ]; then
433                          [ -n "$verbose" ] && echo "Finding IDE modules using ide_hostadapter"
434                         idemodules="`awk '/ide_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
435                         for na in $idemodules; do
436                             tryauto=0;
437                             findmodule "$na"
438                         done
439                     fi
440
441                     if [ "$tryauto" -eq 1 ]; then
442                       if [ -r /usr/share/pci-database/ide.pci -a -r /proc/bus/pci/devices  ]; then
443                         [ -n "$verbose" ] && echo "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                         if [ -z "${searchpciid}" ]; then
454                             searchpciid="$(awk ' { print $2 } ' /proc/bus/pci/devices)"
455                         fi
456                         idemodules=""
457                         for nb in $searchpciid; do
458                             eval `awk -v pciid="$nb" 'BEGIN {
459 }
460                 
461 {
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                         if [ -z "$idemodules" ]; then
490                             echo "WARNING: rootfs on IDE device but no related modules found, loading ide-generic."
491                             idemodules="ide-generic"
492                         fi
493                         for nd in $idemodules; do
494                             findmodule "-$nd"
495                         done
496                       else
497                          [ -r /usr/share/pci-database/ide.pci ] || echo "WARNING: /usr/share/pci-database/ide.pci missing."
498                          [ -r /proc/bus/pci/devices ] || echo "WARNING: /proc/bus/pci/devices missing."
499                          echo "Automatic IDE modules finding not available."
500                       fi
501                     fi
502                 else
503                         findmodule "$n"
504                 fi
505             done
506         fi
507 }
508
509 # return true if node is lvm node
510 _check_lvm() {
511         local node="$1"
512         if [ ! -e "$node" ]; then
513                 return 1
514         fi
515
516         # block-major-58 is lvm1
517         ls -lL "$node" 2> /dev/null | awk '{if (/^b/) { if ($5 == "58,") { exit 0; } else { exit 1; } } else { exit 1; }}'
518         rc=$?
519
520         if [ $rc = 0 ]; then
521                 [ -n "$verbose" ] && echo "$node is LVM1 node"
522                 # is lvm1
523                 return 0
524         fi
525
526         /sbin/lvm lvdisplay "$node" > /dev/null 2>&1
527         rc=$?
528         if [ $rc -gt 127 ]; then
529                 # lvdisplay terminated by signal! most likely it segfaulted.
530                 echo >&2 "ERROR: Unexpected exit from 'lvdisplay $node': $rc - are your lvm tools broken?"
531                 exit 1
532         fi
533
534         if [ $rc = 0 ]; then
535                 [ -n "$verbose" ] && echo "$node is lvm2 node"
536         else
537                 [ -n "$verbose" ] && echo "$node is not any lvm node"
538         fi
539         return $rc
540 }
541
542 find_modules_for() {
543         if [ -z "$1" ]; then
544                 echo "ERROR: no argument passed to find_modules_for() - is your /etc/fstab correct?" >&2
545                 exit
546         elif is_yes "`echo "$1" | awk '/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:|\/dev\/nfs)/ { print "yes"; }'`"; then
547                 if [ ! -x /usr/bin/pcidev -a -z "$NFS_ETH_MODULES" ] ; then
548                     echo "ERROR: root on NFS but /usr/bin/pcidev not found." >&2
549                     echo "Please install correct pci-database package and rerun $0." >&2
550                     exit 1
551                 fi
552                 [ -z "$NFS_ETH_MODULES" ] && NFS_ETH_MODULES=$(/usr/bin/pcidev /m net | xargs)
553                 for m in $NFS_ETH_MODULES; do
554                     findmodule "$m"
555                 done
556                 findmodule "-ipv4"
557                 findmodule "nfs"
558                 usenfs="yes"
559                 echo "Remember to use \`root=/dev/ram0 init=/linuxrc' when starting kernel" >&2
560                 echo "or you will have problems like init(xx) being child process of swapper(1)." >&2
561         elif is_yes "`echo "$1" | awk '/^\/dev\/md/ { print "yes"; }'`"; then
562                 find_modules_softraid "$1"
563         elif is_yes "$(echo "$1" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')" ; then
564                 find_modules_scsi
565         elif is_yes "`echo "$1" | awk '/^\/dev\/(hd|ide)/ { print "yes"; }'`" ; then
566                 find_modules_ide "$1"
567         elif is_yes "`echo "$1" | awk '/\/dev\/rd\// { print "yes"; }'`" ; then
568                 findmodule "DAC960"
569         elif is_yes "`echo "$1" | awk '/\/dev\/ida\// { print "yes"; }'`" ; then
570                 findmodule "cpqarray"
571         elif is_yes "`echo "$1" | awk '/\/dev\/cciss\// { print "yes"; }'`" ; then
572                 findmodule "cciss"
573         elif is_yes "`echo "$1" | awk '/\/dev\/ataraid\// { print "yes"; }'`"; then
574                 find_modules_ide
575                 findmodule "ataraid"
576                 ataraidmodules="`awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
577                 if -n "$ataraidmodules" ; then
578                         # FIXME: think about modules compiled in kernel
579                         echo "ERROR: ataraid_hostadapter alias not defined in $modulefile !"
580                         echo "Please set it and run $0 again."
581                         exit 1
582                 fi
583                 for n in $ataraidmodules; do
584                         findmodule "$n"
585                 done
586         # check to see if we need to set up a loopback filesystem
587         elif is_yes "`echo "$1" | awk -F/ '{print($3);}' | awk '/loop/ { print "yes"; }'`" ; then
588                 echo "Sorry, root on loop device isn't supported." 1>&2
589                 exit 1
590                 # TODO: rewrite for bsp and make nfs ready
591                 if [ ! -x /sbin/losetup ]; then
592                         echo "losetup is missing"
593                         exit 1
594                 fi
595                 key="^# $(echo $1 | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
596                 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`" ; then
597                         echo "The root filesystem is on a $1, but there is no magic entry in $fstab" 1>&2
598                         echo "for this device. Consult the geninitrd man page for more information" 1>&2
599                         exit 1
600                 fi
601
602                 line="`awk '/'$key'/ { print $0; }' $fstab`"
603                 loopDev="$(echo $line | awk '{print $3}')"
604                 loopFs="$(echo $line | awk '{print $4}')"
605                 loopFile="$(echo $line | awk '{print $5}')"
606
607                 BASICMODULES="$BASICMODULES -loop"
608                 findmodule "-$loopFs"
609                 BASICMODULES="$BASICMODULES -${loopFs}"
610         elif _check_lvm "$1"; then
611                 if [ ! -f /sbin/initrd-lvm -o ! -x /sbin/lvdisplay -o ! -x /sbin/pvdisplay ] ; then
612                         echo "ERROR: root on LVM but /sbin/initrd-lvm, /sbin/lvdisplay and /sbin/pvdisplay not found." >&2
613                         echo "Please install lvm(2) and lvm(2)-initrd package and rerun $0." >&2
614                         exit 1
615                 fi
616                 if [ -z "$LVMTOOLSVERSION" ] ; then 
617                     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}')
618                     if [ -z "$LVMTOOLSVERSION" ] ; then
619                             echo "ERROR: Can't determine LVM tools version. Please set LVMTOOLSVERSION" >&2
620                             echo "and rerun $0." >&2
621                             exit 1
622                     fi
623                 fi
624                 if [ -z "$PVDEVICES" ] ; then
625                     VGVOLUME=$(/sbin/lvdisplay -c "$1" 2> /dev/null | awk -F":" ' { print $2 } ')
626                     PVDEVICES=$(/sbin/pvdisplay -c | awk -F":" -v vg="$VGVOLUME" ' BEGIN { devices="" } { if ($2 == vg) { devices = devices " " $1 } } END { print devices } ')
627                 fi
628                 if [ -n "$PVDEVICES" ] ; then
629                         for device in $PVDEVICES; do
630                                 find_modules_for $device
631                         done
632                 else
633                         echo "ERROR: I wasn't able to find PV (via lvdisplay and pvdisplay)." >&2 
634                         echo "You can try to set PVDEVICES in /etc/sysconfig/geninitrd." >&2
635                         exit 1
636                 fi
637                 if [ "$LVMTOOLSVERSION" = "2" ]; then
638                     findmodule "-dm-mod"
639                 elif [ "$LVMTOOLSVERSION" = "1" ]; then
640                     findmodule "-lvm"
641                     findmodule "-lvm-mod"
642                 else
643                         echo "ERROR: LVM version $LVMTOOLSVERSION is not supported yet." >&2
644                         exit 1
645                 fi
646                 [ -n "$verbose" ] && echo "LVM $LVMTOOLSVERSION enabled"
647                 uselvm="yes"
648         fi
649 }
650
651 modules_install() {
652         modules="$1"
653
654         for mod in $modules; do
655                 MODULEDIR="`my_dirname "$mod"`"
656                 mkdir -p "$MNTIMAGE/lib/modules/$kernel/$MODULEDIR"
657                 cp $verbose -a "/lib/modules/$kernel/$mod" "$MNTIMAGE/lib/modules/$kernel/$mod"
658                 gunzip "$MNTIMAGE/lib/modules/$kernel/$mod" 2> /dev/null
659         done
660 }
661
662 modules_add_linuxrc() {
663         modules="$1"
664         linuxrc="$2"
665  
666         for mod in $modules; do
667                 MODULE2="`my_dirname "$mod"`"
668                 NAME2=`basename "$mod" .gz`
669                 MODULE2=$MODULE2/$NAME2
670                 module="`echo $mod | awk -F/ '{ $0=$NF } /'$modext'.*$/ { gsub(/'$modext'.*/, NIL, $0); } { print $0; }'`"
671                 options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"
672
673                 sleep_module=$(echo "${module}" | awk ' { gsub("-", "_", $0) } { print $0; } ')
674                 sleep_var="$(eval echo \$MODULE_${sleep_module}_USLEEP)"
675
676                 if [ -n "$verbose" ]; then
677                         echo -n "Loading module [$module] "
678                         if [ -n "$options" ] ; then
679                                 echo -n "with options [$options]"
680                         else
681                                 echo -n "without options"
682                         fi
683                         if [ -n "$sleep_var" ]; then
684                                 echo " and $sleep_var usleep."
685                         else
686                                 echo "."
687                         fi
688                 fi
689                 echo "$insmod /lib/modules/$kernel/$MODULE2 $options" >> "$linuxrc"
690                 if [ -n "${sleep_var}" ]; then
691                         echo "usleep $sleep_var" >> "$linuxrc"
692                 fi
693         done
694 }
695
696 if [ -r /etc/sysconfig/geninitrd ] ; then
697         . /etc/sysconfig/geninitrd
698 fi
699
700 if [ -r /etc/sysconfig/bootsplash ] ; then
701         . /etc/sysconfig/bootsplash
702 fi
703
704 if [ ! -x /bin/initrd-busybox ] ; then 
705         echo "/bin/initrd-busybox is missing !"
706         exit 1
707 fi
708 case "$(uname -m)" in
709     ia64|amd64|x86_64|sparc64)
710         IMAGESIZE=3000
711         ;;
712     *)
713         IMAGESIZE=1500
714         ;;
715 esac
716
717 while [ $# -gt 0 ]; do
718         case $1 in
719         --fstab=*)
720                 fstab="`echo $1 | awk -F= '{print $2;}'`"
721                 ;;
722         --fstab)
723                 fstab="$2"
724                 shift
725                 ;;
726         --modules-conf=*)
727                 modulefile="`echo $1 | awk -F= '{print $2;}'`"
728                 ;;
729         --modules-conf)
730                 modulefile="$2"
731                 shift
732                 ;;
733         --use-raidstart|--with-raidstart)
734                 USERAIDSTART="yes"
735                 ;;
736         --without-raidstart)
737                 USERAIDSTART="no"
738                 ;;
739         --use-insmod-static|--with-insmod-static)
740                 USEINSMODSTATIC="yes"
741                 ;;
742         --without-insmod-static)
743                 USEINSMODSTATIC="no"
744                 ;;
745         --without-bootsplash)
746                 BOOT_SPLASH="no"
747                 ;;
748         --without-suspend)
749                 USE_SUSPEND="no";
750                 ;;
751         --without-suspend2)
752                 USE_SUSPEND2="no";
753                 ;;
754         --lvmtoolsversion=|--lvmversion=)
755                 LVMTOOLSVERSION="`echo $1 | awk -F= '{print $2;}'`"
756                 ;;
757         --lvmtoolsversion|--lvmversion)
758                 LVMTOOLSVERSION="$2"
759                 shift
760                 ;;
761         --without-udev)
762                 USE_UDEV=
763                 ;;
764         --without-dmraid)
765                 USE_DMRAID=
766                 ;;
767         --with=*)
768                 BASICMODULES="$BASICMODULES `echo $1 | awk -F= '{print $2;}'`"
769                 ;;
770         --with)
771                 BASICMODULES="$BASICMODULES $2"
772                 shift
773                 ;;
774         --version)
775                 echo "geninitrd: version $VERSION"
776                 exit 0
777                 ;;
778         -v)
779                 verbose=-v
780                 ;;
781         --nocompress)
782                 COMPRESS="no"
783                 ;;
784         --ifneeded)
785                 ifneeded=1
786                 ;;
787         -f)
788                 force=1
789                 ;;
790         --preload=*)
791                 PREMODS="$PREMODS `echo $1 | awk -F= '{print $2;}'`"
792                 ;;
793         --preload)
794                 PREMODS="$PREMODS $2"
795                 shift
796                 ;;
797         --fs=*)
798                 echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
799                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
800                 ;;
801         --fs)
802                 echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
803                 INITRDFS="$2"
804                 shift
805                 ;;
806         --initrdfs=*)
807                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
808                 ;;
809         --initrdfs)
810                 INITRDFS="$2"
811                 shift
812                 ;;
813         --image-version)
814                 img_vers=yes
815                 ;;
816         --ide-only-root)
817                 ide_only_root="yes"
818                 ;;
819         *)
820                 if [ -z "$target" ]; then
821                         target="$1"
822                 elif [ -z "$kernel" ]; then
823                         kernel="$1"
824                 else
825                         usage
826                 fi
827                 ;;
828         esac
829
830         shift
831 done
832
833 if [ -z "$target" -o -z "$kernel" ]; then
834         usage
835 fi
836
837 pack_version="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d",$1,$2)}'`"
838 pack_version_long="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}'`"
839
840 if [ "x" = "x$INITRDFS" ] ; then
841         if [ "x" = "x$FS" ] ; then
842                 # default value
843                 # XXX: initramfs blocked for now
844                 if [ "1" = " 0" -a "$pack_version" -ge "002005" ] ; then
845                     INITRDFS="initramfs"
846                 else
847                     INITRDFS="rom"
848                 fi
849         else
850                 echo "Warning: FS configuration options is obsoleted. Use INITRDFS instead" 1>&2
851                 INITRDFS="$FS"
852         fi
853 fi
854
855 if [ "$pack_version" -lt "002006" ] ; then
856         USE_UDEV=
857         USE_DMRAID=
858 fi
859 [ -z "$USE_UDEV" ] && UDEV_TMPFS=
860     
861 if [ "$pack_version" -ge "002005" ] ; then
862         modext=".ko"
863         insmod="insmod"
864 fi
865
866 if is_yes "$USEINSMODSTATIC" ; then
867         insmod="insmod.static"
868         INSMOD="/sbin/insmod.static"
869         if [ "$pack_version" -lt "002005" -a -f /sbin/insmod.static.modutils ] ; then
870                 INSMOD="/sbin/insmod.static.modutils"
871         fi
872         if [ ! -f "$INSMOD" ] ; then
873                 echo "insmod.static requested but /sbin/insmod.static not found !" >&2
874                 exit 1
875         fi
876 fi
877
878 case "$INITRDFS" in
879         ext2)
880                 if [ ! -x /sbin/mke2fs ]; then
881                         echo "/sbin/mke2fs is missing" 1>&2
882                         exit 1
883                 fi
884                 ;;
885         rom)
886                 if [ ! -x /sbin/genromfs ]; then
887                         echo "/sbin/genromfs is missing" 1>&2
888                         exit 1
889                 fi
890                 ;;
891         cram)
892                 if [ ! -x /sbin/mkcramfs ]; then
893                         echo "/sbin/mkcramfs is missing" 1>&2
894                         exit 1
895                 fi
896                 ;;
897         initramfs)
898                 if [ ! -x /bin/cpio ]; then
899                         echo "/bin/cpio is missing" 1>&2
900                         exit 1
901                 fi
902                 if [ ! -x /usr/bin/find ]; then
903                         echo "/usr/bin/find is missing" 1>&2
904                         exit 1
905                 fi
906                 ;;
907         *)
908                 echo "Filesystem $INITRDFS on initrd is not supported" 1>&2
909                 exit 1
910                 ;;
911 esac
912
913 if [ -n "$img_vers" ]; then
914         target="$target-$kernel"
915 fi
916
917 if [ -z "$force" -a -f "$target" ]; then
918         echo "$target already exists." 1>&2
919         exit 1
920 fi
921
922 if [ ! -d "/lib/modules/$kernel" ]; then
923         echo "/lib/modules/$kernel is not a directory." 1>&2
924         exit 1
925 fi
926
927 if is_yes "$USE_SUSPEND"; then
928         if is_yes "$USE_SUSPEND2"; then
929                 echo "Suspend2 shouldn't be used in parallel with mainline suspend!." 1>&2
930                 exit 1
931         fi
932 fi
933
934 if [ ! -f /proc/mounts ]; then
935         echo "WARNING: /proc filesystem not mounted, may cause wrong results or failure." 1>&2
936 fi
937
938 if [ "$pack_version" -lt "002005" ]; then
939         modulefile=/etc/modules.conf
940         if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
941                 modulefile=/etc/conf.modules
942         fi
943 else
944         modulefile=/etc/modprobe.conf
945 fi
946  
947 for n in $PREMODS; do
948         findmodule "$n"
949 done
950
951 # allow forcing loading SCSI and/or IDE modules
952 if is_yes "$ADDSCSI" ; then
953         find_modules_scsi
954 fi
955
956 if is_yes "$ADDIDE" ; then
957         find_modules_ide
958 fi
959
960 find_root
961
962 org_rootdev="$rootdev1"
963
964 find_modules_for "$rootdev1"
965
966 findmodule "-$rootFs"
967
968 for n in $BASICMODULES; do
969         findmodule "$n"
970 done
971
972 if is_yes "$USE_SUSPEND2"; then
973         findmodule "-lzf"
974 fi
975
976 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
977         if [ -n "$verbose" ]; then
978                 echo "No modules are needed -- not building initrd image."
979         fi
980         exit 0
981 fi
982
983 if [ -n "$verbose" ]; then
984         echo "Using modules: $MODULES"
985 fi
986
987 MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
988 IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
989 MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
990 RCFILE="$MNTIMAGE/linuxrc"
991
992 if [ -f "$MNTIMAGE" ]; then
993         echo "$MNTIMAGE already exists. Remove it and try again" 1>&2
994         exit 1
995 fi
996
997 if [ -f "$IMAGE" ]; then
998         echo "$IMAGE already exists. Remove it and try again" 1>&2
999         exit 1
1000 fi
1001
1002 if [ "$INITRDFS" = "ext2" ] ; then
1003         dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
1004
1005         # We have to "echo y |" so that it doesn't complain about $IMAGE not
1006         # being a block device
1007         echo y | mke2fs -F "$IMAGE" "$IMAGESIZE" >/dev/null 2>/dev/null
1008
1009         mkdir -p "$MNTPOINT"
1010         mount -o loop -t ext2 "$IMAGE" "$MNTPOINT"
1011 else
1012         mkdir -p "$MNTPOINT"
1013 fi
1014
1015
1016 mkdir -p "$MNTIMAGE"/{lib,bin,etc,dev,loopfs,var}
1017
1018
1019 # We don't need this directory, so let's save space
1020 rm -rf "$MNTPOINT"/lost+found
1021
1022 modules_install "$MODULES"
1023
1024 # mknod'ing the devices instead of copying them works both with and
1025 # without devfs...
1026 mknod "$MNTIMAGE/dev/console" c 5 1
1027 mknod "$MNTIMAGE/dev/null" c 1 3
1028 mknod "$MNTIMAGE/dev/zero" c 1 5
1029
1030 s="$RCFILE"
1031 ln -s /linuxrc $MNTIMAGE/init
1032
1033 inst /bin/initrd-busybox "$MNTIMAGE/bin/sh"
1034 ln -s sh "$MNTIMAGE/bin/busybox"
1035
1036 if is_yes "$USEINSMODSTATIC" ; then
1037         inst "$INSMOD" $MNTIMAGE/bin/insmod.static
1038 fi
1039
1040 cat > "$s" <<EOF
1041 #! /bin/sh
1042
1043 set -x
1044 EOF
1045 chmod 755 "$s"
1046
1047 modules_add_linuxrc "$MODULES" "$s"
1048
1049 # TODO: rewrite for busybox
1050 #if [ -n "$loopDev" ]; then
1051 #       if [ ! -d /initrd ]; then
1052 #               mkdir /initrd
1053 #       fi
1054 #
1055 #       cp -a "$loopDev" "$MNTIMAGE/dev"
1056 #       cp -a "$rootdev1" "$MNTIMAGE/dev"
1057 #       echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1058 #       echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1059 #       echo "echo Setting up loopback device $rootdev1" >> $RCFILE
1060 #       echo "losetup $rootdev1 /loopfs$loopFile" >> "$RCFILE"
1061 #fi
1062
1063 initrd_gen_suspend() {
1064         if [ ! -x /usr/sbin/resume ]; then
1065                 echo "/usr/sbin/resume is missing !"
1066                 exit 1
1067         fi
1068         mkdir -p $MNTIMAGE/etc
1069         mkdir -p $MNTIMAGE/dev
1070         resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
1071         cp -HR /dev/snapshot $resume_dev $MNTIMAGE/dev
1072         inst /etc/suspend.conf $MNTIMAGE/etc/suspend.conf
1073         inst /usr/sbin/resume "$MNTIMAGE/bin/resume"
1074         echo "resume" >> "$s"
1075 }
1076
1077 initrd_gen_suspend2() {
1078         mkdir -p $MNTIMAGE/sys
1079         mkdir -p $MNTIMAGE/proc
1080 cat << EOF >> "$s"
1081 mount -t proc none /proc
1082 if [ "\$(awk ' /resume2=/  { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
1083 EOF
1084 cat << EOF >> "$s"
1085                 echo > /proc/suspend2/do_resume
1086 fi
1087 umount /proc
1088 EOF
1089 }
1090
1091 initrd_gen_udev() {
1092         [ -n "$verbose" ] && echo "Setting up udev..."
1093         mkdir -p $MNTIMAGE/sbin
1094         mkdir -p $MNTIMAGE/proc
1095         mkdir -p $MNTIMAGE/etc/udev
1096         
1097         inst /sbin/initrd-udev $MNTIMAGE/sbin/udev
1098         ln -s udev $MNTIMAGE/sbin/udevstart
1099         inst /etc/udev/udev.conf $MNTIMAGE/etc/udev/udev.conf
1100
1101         ln -s udev $MNTIMAGE/sbin/hotplug
1102
1103         if is_yes "$USE_UDEV"; then
1104                 if is_yes "$UDEV_TMPFS"; then
1105                     cat >> "$s" <<-EOF
1106 echo Creating /dev
1107 mount -o mode=0755 -t tmpfs none /dev
1108 mknod /dev/console c 5 1
1109 mknod /dev/null c 1 3
1110 mknod /dev/zero c 1 5
1111 mkdir /dev/pts
1112 mkdir /dev/shm
1113 EOF
1114                 fi
1115                 cat >> "$s" <<-EOF
1116 mount -t proc none /proc
1117 echo Starting udev
1118 /sbin/udevstart
1119 echo -n "/sbin/hotplug" > /proc/sys/kernel/hotplug
1120 umount /proc
1121 EOF
1122         fi
1123 }
1124
1125 initrd_gen_dmraid() {
1126    if [ ! -x /sbin/dmraid-initrd ] ; then
1127        echo "/sbin/dmraid-initrd is missing missing !"
1128        exit 1
1129    fi
1130
1131    if [ ! -x $MNTIMAGE/sbin/udev ]; then
1132        echo "udev is needed on target initrd for dmraid to work!"
1133        exit 1
1134    fi
1135
1136    mkdir -p "$MNTIMAGE/sbin"
1137    inst /sbin/dmraid-initrd $MNTIMAGE/sbin/dmraid
1138 cat <<-EOF >> "$s"
1139         mount -t proc none /proc
1140         mount -t sysfs none /sys
1141         # 2 secs was enough for my system to initialize. but really this is udev issue?
1142         usleep 2000000
1143         /sbin/dmraid -ay -i
1144         umount /sys
1145         umount /proc
1146 EOF
1147 }
1148
1149
1150 initrd_gen_softraid() {
1151         [ -n "$verbose" ] && echo "Setting up mdadm..."
1152         
1153         if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ] ; then
1154                 echo "/sbin/mdadm or /sbin/initrd-mdassemble is missing !"
1155                 exit 1
1156         fi
1157
1158         inst /sbin/initrd-mdassemble "$MNTIMAGE/bin/mdassemble"
1159
1160         # LVM on RAID case
1161         dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf)
1162         for ex_dev in $dev_list_extra; do
1163                 echo "DEVICE $ex_dev" >> "$MNTIMAGE/etc/mdadm.conf"
1164         done
1165         do_md0=1
1166         for nr in `seq 1 $rootdev_nr`; do
1167             eval cr_rootdev="\$rootdev${nr}"
1168             eval cr_dev_list="\$dev_list${nr}"
1169             [ -n "$verbose" ] && echo "Setting up array ($cr_rootdev = $cr_dev_list)"
1170
1171             [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
1172             
1173             echo "DEVICE $cr_dev_list" >> "$MNTIMAGE/etc/mdadm.conf"
1174             cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
1175             cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev)
1176             if [ -n "$cr_md_conf" ]; then
1177                     echo "$cr_md_conf" >> "$MNTIMAGE/etc/mdadm.conf"
1178             else
1179                     echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$MNTIMAGE/etc/mdadm.conf"
1180             fi
1181
1182             for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
1183                 # mkdir in case of devfs name
1184                 mkdir -p $MNTIMAGE/`my_dirname $f`
1185                 [ -e "$MNTIMAGE/$f" ] && continue
1186                 [ -n "$verbose" ] && echo "copying $f"
1187                 # this works fine with and without devfs
1188                 cp -HR $f $MNTIMAGE/$f
1189             done
1190         done
1191
1192         echo "mdassemble" >> "$s"
1193
1194         # needed to determine md-version
1195         if [ "$do_md0" -eq 1 ] ; then
1196                 mknod $MNTIMAGE/dev/md0 b 9 0
1197         fi
1198 }
1199
1200 initrd_gen_nfs() {
1201         # use root=/dev/ram0 init=/linuxrc when starting kernel or you will
1202         # have problems like init(XX) being child process of swapper(1).
1203         [ -n "$verbose" ] && echo "Adding rootfs on NFS support to initrd (dhcp)"
1204         mknod "$MNTIMAGE/dev/urandom" c 1 8
1205         mkdir "$MNTIMAGE/newroot"
1206         echo "ifconfig lo 127.0.0.1 up" >> "$s"
1207         echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" >> "$s"
1208         echo "ifconfig eth0 0.0.0.0 up" >> "$s"
1209         echo "udhcpc -i eth0 -f -q -s /bin/setdhcp"  >> "$s"
1210         cat << EOF > "$MNTIMAGE/bin/setdhcp"
1211 #!/bin/sh
1212 [ "\$1" != "bound" ] && exit
1213 [ -n "\$broadcast" ] && BROADCAST="broadcast \$broadcast"
1214 [ -n "\$subnet" ] && NETMASK="netmask \$subnet"
1215 set -x
1216 ifconfig \$interface \$ip \$BROADCAST \$NETMASK up
1217 set +x
1218 if [ -n "\$router" ]; then
1219     for r in \$router; do
1220         set -x
1221         route add default gw \$r dev \$interface
1222         set +x
1223     done
1224 fi
1225 if [ -n "\$rootpath" ]; then
1226     set -x
1227     mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 \$rootpath /newroot
1228     set +x
1229 else
1230     set +x
1231     echo "Missing rootpath in what DHCP server sent to us. Failing..."
1232     echo "All seen variables are listed below:"
1233     set
1234     set -x
1235 fi
1236 EOF
1237         chmod 755 "$MNTIMAGE/bin/setdhcp"
1238         echo "cd /newroot" >> "$s"
1239         echo "pivot_root . initrd" >> "$s"
1240         echo "[ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1241         echo "exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1242 }
1243
1244 initrd_gen_lvm() {
1245         [ -n "$verbose" ] && echo "Adding LVM support to initrd"
1246         inst /sbin/initrd-lvm $MNTIMAGE/bin/lvm
1247         mkdir -p $MNTIMAGE/etc
1248         mkdir -p $MNTIMAGE/tmp
1249         mkdir -p $MNTIMAGE/proc
1250         mkdir -p $MNTIMAGE/newroot
1251         if ! is_yes "$USE_UDEV"; then
1252                 mkdir -p $MNTIMAGE/dev/mapper
1253                 mknod $MNTIMAGE/dev/mapper/control c 10 63
1254                 for device in $PVDEVICES; do
1255                         # if LVM on RAID then device might be copied already in gen_softraid
1256                         [ -e "$MNTIMAGE/dev/$(basename $device)" ] && continue
1257                         cp -HR $device $MNTIMAGE/dev/
1258                 done
1259         fi
1260         echo "mount -t proc none /proc" >> "$s"
1261         echo "mount -t tmpfs none /tmp" >> "$s"
1262         if [ "$LVMTOOLSVERSION" = "1" ] ; then
1263                 echo "lvm vgscan -T" >> "$s"
1264                 echo "lvm vgchange -T -a y $VGVOLUME" >> "$s"
1265                 echo "umount /tmp" >> "$s"
1266                 # fail to umount
1267                 echo "umount /dev" >> "$s"
1268                 echo "umount /proc" >> "$s"
1269         else
1270                 echo "cat /etc/lvm.conf > /tmp/lvm.conf" >> "$s"
1271                 echo "global {" > "$MNTIMAGE/etc/lvm.conf"
1272                 echo "  locking_type = 0" >> "$MNTIMAGE/etc/lvm.conf"
1273                 echo "  locking_dir = \"/tmp\"" >> "$MNTIMAGE/etc/lvm.conf"
1274                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1275                 echo "devices {" >> "$MNTIMAGE/etc/lvm.conf"
1276                 echo "  sysfs_scan=0" >> "$MNTIMAGE/etc/lvm.conf"
1277                 if is_yes "$raidfound"; then
1278                         echo "  md_component_detection = 1" >> "$MNTIMAGE/etc/lvm.conf"
1279                 fi
1280                 lvm dumpconfig | awk '/filter=/' >> "$MNTIMAGE/etc/lvm.conf"
1281                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1282                 echo "LVM_SYSTEM_DIR=/tmp lvm vgscan --ignorelockingfailure" >> "$s"
1283                 echo "LVM_SYSTEM_DIR=/tmp lvm vgchange --ignorelockingfailure -a y $VGVOLUME" >> "$s"
1284                 echo "LVM_SYSTEM_DIR=/tmp lvm vgscan --ignorelockingfailure --mknodes" >> "$s"
1285                 # Find out major/minor
1286                 echo "majmin=\"\`LVM_SYSTEM_DIR=/tmp lvm lvdisplay --ignorelockingfailure -c $org_rootdev\`\"" >> "$s"
1287                 echo "majmin=\"\${majmin#*/}\"" >> "$s"
1288                 echo "majmin=\"\${majmin#*:*:*:*:*:*:*:*:*:*:*:*}\"" >> "$s"
1289                 echo "major=\"\${majmin%:*}\"" >> "$s"
1290                 echo "minor=\"\${majmin#*:}\"" >> "$s"
1291                 # Pass it to kernel
1292                 echo "val=\$((256 * \$major + \$minor))" >> "$s"
1293                 echo "echo \$val > /proc/sys/kernel/real-root-dev" >> "$s"
1294                 echo "umount /tmp" >> "$s"
1295                 echo "umount /proc" >> "$s"
1296         fi
1297 }
1298
1299 initrd_gen_procdata() {
1300         [ -n "$verbose" ] && echo "Adding rootfs finding based on root= option support."
1301         mkdir -p $MNTIMAGE/proc
1302 cat << EOF >> "$s"
1303 set +x
1304 mount -t proc none /proc
1305 root="\$(busybox awk ' /root=\/dev\// { gsub(/.*root=\/dev\//,NIL,\$0); gsub(/ .*/,NIL,\$0); print \$0; } ' /proc/cmdline)"
1306 if [ -n "\$root" ]; then
1307     rootnr="\$(busybox awk -v root="\$root" ' { if (\$4 == root) { print 256*\$1+\$2; } } ' /proc/partitions)"
1308     if [ -n "\$rootnr" ]; then
1309         echo "\$rootnr" > /proc/sys/kernel/real-root-dev
1310     fi
1311 fi
1312 umount /proc
1313 set -x
1314 EOF
1315 }
1316
1317 # main generation
1318
1319 if is_yes "$USE_UDEV"; then
1320         initrd_gen_udev
1321 fi
1322
1323 if is_yes "$USE_SUSPEND"; then
1324         initrd_gen_suspend
1325 fi
1326
1327 if is_yes "$USE_SUSPEND2"; then
1328         initrd_gen_suspend2
1329 fi
1330
1331 if is_yes "$USE_DMRAID"; then
1332         initrd_gen_dmraid
1333 fi
1334
1335 if is_yes "$usenfs" ; then
1336         initrd_gen_nfs
1337 elif is_yes "$USERAIDSTART" && is_yes "$raidfound" ; then
1338         initrd_gen_softraid
1339         if is_yes "$uselvm" ; then
1340                 initrd_gen_lvm
1341         else
1342                 initrd_gen_procdata
1343         fi
1344 elif is_yes "$uselvm" ; then
1345         initrd_gen_lvm
1346 else
1347         initrd_gen_procdata
1348 fi
1349
1350 if [ "$INITRDFS" = "initramfs" ]; then
1351         mkdir -p $MNTIMAGE/newroot
1352         cp -HR $org_rootdev $MNTIMAGE/dev
1353         echo "mount -t $rootFs $org_rootdev /newroot" >> "$s"
1354         echo "switch_root /newroot /sbin/init" >> "$s"
1355         # we need real file, not symlink
1356         rm -f $MNTIMAGE/init
1357         cp -a $MNTIMAGE/linuxrc $MNTIMAGE/init
1358 fi
1359
1360 chmod +x "$RCFILE"
1361
1362 (cd "$MNTIMAGE"; tar cf - .) | (cd "$MNTPOINT"; tar xf -)
1363
1364 case "$INITRDFS" in
1365         ext2)
1366                 umount "$IMAGE"
1367                 ;;
1368         rom)
1369                 genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
1370                 ;;
1371         cram)
1372                 mkcramfs "$MNTPOINT" "$IMAGE"
1373                 ;;
1374         initramfs)
1375                 (cd $MNTPOINT ; find . | cpio -H newc -o > "$IMAGE")
1376                 ;;
1377         *)
1378                 echo "Filesystem $INITRDFS not supported by $0";
1379 esac
1380
1381 if is_yes "$COMPRESS" ; then
1382         gzip -9 < "$IMAGE" > "$target"
1383 else
1384         cp -a "$IMAGE" "$target"
1385 fi
1386
1387 if is_yes "$BOOT_SPLASH"; then
1388         if [ ! -x /bin/splash.bin ]; then
1389                 echo "Failed to execute /bin/splash.bin. Is bootsplash package installed?" 1>&2
1390         elif [ -z "$THEME" ]; then
1391                 echo "Please configure your /etc/sysconfig/bootsplash first." 1>&2
1392                 echo "Generating bootsplashes skipped." 1>&2
1393         else
1394             if [ -n "$BOOT_SPLASH_RESOLUTIONS" ]; then
1395                 for res in $BOOT_SPLASH_RESOLUTIONS; do
1396                         if [ -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
1397                                 /bin/splash.bin -s -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> "$target" && \
1398                                 [ -n "$verbose" ] && echo "Added $res $THEME theme to initrd."
1399                         else
1400                                 echo "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped" 1>&2
1401                         fi
1402                 done
1403              else
1404                  echo "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash." 1>&2
1405                  echo "Not adding bootsplash to initrd." 1>&2
1406              fi
1407         fi
1408 fi
1409
1410 rm -rf "$MNTIMAGE" "$MNTPOINT" "$IMAGE"
This page took 0.129585 seconds and 3 git commands to generate.