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