]> git.pld-linux.org Git - projects/geninitrd.git/blob - geninitrd
1c2b099c51ff4a53356e033deed326f4e813ff37
[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_admin -u "$1" 2>/dev/null | awk -F= '{print $2}' | xargs
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_admin ] ; then
294                                         echo "/usr/sbin/xfs_admin is missing" 1>&2
295                                         exit 1
296                                 fi
297                                 get_uuid="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 # resolve /dev/dm-0 to lvm2 node
514 # which they got from /proc/partitions when rootfs is UUID=
515 _lvm2_node_resolve() {
516         local node="$1"
517
518         # redirect output to tmpfile so we won't get subshell
519         local tmp ret
520         tmp=$(mktemp) || exit 1
521
522         lvdisplay -c 2>/dev/null|awk -F: '{print $1}' > $tmp
523         while read dev; do
524                 count=$(ls -Ll $dev $node | awk '{print $5, $6}' | sort -u | wc -l)
525                 if [ "$count" = 1 ]; then
526                         ret="$dev"
527                         break
528                 fi
529         done < $tmp
530         rm -f $tmp
531
532         if [ -z "$ret" ]; then
533                 ret=$node
534         fi
535         echo "$ret"
536 }
537
538 # return true if node is lvm node
539 _check_lvm() {
540         local node="$1"
541         if [ ! -e "$node" ]; then
542                 echo "WARNING: check_lvm(): node $node doesn't exists!" 1>&2
543                 return 1
544         fi
545
546         # block-major-58 is lvm1
547         ls -lL "$node" 2> /dev/null | awk '{if (/^b/) { if ($5 == "58,") { exit 0; } else { exit 1; } } else { exit 1; }}'
548         rc=$?
549
550         if [ $rc = 0 ]; then
551                 [ -n "$verbose" ] && echo "$node is LVM1 node"
552                 # is lvm1
553                 return 0
554         fi
555
556         node=$(_lvm2_node_resolve $node)
557         /sbin/lvm lvdisplay "$node" > /dev/null 2>&1
558         rc=$?
559         if [ $rc -gt 127 ]; then
560                 # lvdisplay terminated by signal! most likely it segfaulted.
561                 echo >&2 "ERROR: Unexpected exit from 'lvdisplay $node': $rc - are your lvm tools broken?"
562                 exit 1
563         fi
564
565         if [ $rc = 0 ]; then
566                 [ -n "$verbose" ] && echo "$node is lvm2 node"
567         else
568                 [ -n "$verbose" ] && echo "$node is not any lvm node"
569         fi
570         return $rc
571 }
572
573 find_modules_for() {
574         if [ -z "$1" ]; then
575                 echo "ERROR: no argument passed to find_modules_for() - is your /etc/fstab correct?" >&2
576                 exit
577         fi
578
579         if is_yes "`echo "$1" | awk '/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:|\/dev\/nfs)/ { print "yes"; }'`"; then
580                 if [ ! -x /usr/bin/pcidev -a -z "$NFS_ETH_MODULES" ] ; then
581                         echo "ERROR: root on NFS but /usr/bin/pcidev not found." >&2
582                         echo "Please install correct pci-database package and rerun $0." >&2
583                         exit 1
584                 fi
585                 [ -z "$NFS_ETH_MODULES" ] && NFS_ETH_MODULES=$(/usr/bin/pcidev /m net | xargs)
586                 for m in $NFS_ETH_MODULES; do
587                         findmodule "$m"
588                 done
589                 findmodule "-ipv4"
590                 findmodule "nfs"
591                 usenfs="yes"
592                 echo "Remember to use \`root=/dev/ram0 init=/linuxrc' when starting kernel" >&2
593                 echo "or you will have problems like init(xx) being child process of swapper(1)." >&2
594         elif is_yes "`echo "$1" | awk '/^\/dev\/md/ { print "yes"; }'`"; then
595                 find_modules_softraid "$1"
596         elif is_yes "$(echo "$1" | awk '/^\/dev\/(sd|scsi)/ { print "yes"; }')" ; then
597                 find_modules_scsi
598         elif is_yes "`echo "$1" | awk '/^\/dev\/(hd|ide)/ { print "yes"; }'`" ; then
599                 find_modules_ide "$1"
600         elif is_yes "`echo "$1" | awk '/\/dev\/rd\// { print "yes"; }'`" ; then
601                 findmodule "DAC960"
602         elif is_yes "`echo "$1" | awk '/\/dev\/ida\// { print "yes"; }'`" ; then
603                 findmodule "cpqarray"
604         elif is_yes "`echo "$1" | awk '/\/dev\/cciss\// { print "yes"; }'`" ; then
605                 findmodule "cciss"
606         elif is_yes "`echo "$1" | awk '/\/dev\/ataraid\// { print "yes"; }'`"; then
607                 find_modules_ide
608                 findmodule "ataraid"
609                 ataraidmodules="`awk '/ataraid_hostadapter/ && ! /^[\t ]*#/ { print $3; }' $modulefile`"
610                 if -n "$ataraidmodules" ; then
611                         # FIXME: think about modules compiled in kernel
612                         echo "ERROR: ataraid_hostadapter alias not defined in $modulefile !"
613                         echo "Please set it and run $0 again."
614                         exit 1
615                 fi
616                 for n in $ataraidmodules; do
617                         findmodule "$n"
618                 done
619         # check to see if we need to set up a loopback filesystem
620         elif is_yes "`echo "$1" | awk -F/ '{print($3);}' | awk '/loop/ { print "yes"; }'`" ; then
621                 echo "Sorry, root on loop device isn't supported." 1>&2
622                 exit 1
623                 # TODO: rewrite for bsp and make nfs ready
624                 if [ ! -x /sbin/losetup ]; then
625                         echo "losetup is missing"
626                         exit 1
627                 fi
628                 key="^# $(echo $1 | awk -F/ '{print($3);}' | tr '[a-z]' '[A-Z]'):"
629                 if ! is_yes "`awk '/'$key'/ { print( "yes"); }' $fstab`" ; then
630                         echo "The root filesystem is on a $1, but there is no magic entry in $fstab" 1>&2
631                         echo "for this device. Consult the geninitrd man page for more information" 1>&2
632                         exit 1
633                 fi
634
635                 line="`awk '/'$key'/ { print $0; }' $fstab`"
636                 loopDev="$(echo $line | awk '{print $3}')"
637                 loopFs="$(echo $line | awk '{print $4}')"
638                 loopFile="$(echo $line | awk '{print $5}')"
639
640                 BASICMODULES="$BASICMODULES -loop"
641                 findmodule "-$loopFs"
642                 BASICMODULES="$BASICMODULES -${loopFs}"
643         elif _check_lvm "$1"; then
644                 node=$(_lvm2_node_resolve "$1")
645
646                 if [ ! -f /sbin/initrd-lvm -o ! -x /sbin/lvdisplay -o ! -x /sbin/pvdisplay ] ; then
647                         echo "ERROR: root on LVM but /sbin/initrd-lvm, /sbin/lvdisplay and /sbin/pvdisplay not found." >&2
648                         echo "Please install lvm(2) and lvm(2)-initrd package and rerun $0." >&2
649                         exit 1
650                 fi
651                 if [ -z "$LVMTOOLSVERSION" ] ; then
652                         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}')
653                         if [ -z "$LVMTOOLSVERSION" ] ; then
654                                 echo "ERROR: Can't determine LVM tools version. Please set LVMTOOLSVERSION" >&2
655                                 echo "and rerun $0." >&2
656                                 exit 1
657                         fi
658                 fi
659                 if [ -z "$PVDEVICES" ] ; then
660                         VGVOLUME=$(/sbin/lvdisplay -c "$node" 2> /dev/null | awk -F":" ' { print $2 } ')
661                         PVDEVICES=$(/sbin/pvdisplay -c | awk -F":" -v vg="$VGVOLUME" ' BEGIN { devices="" } { if ($2 == vg) { devices = devices " " $1 } } END { print devices } ')
662                 fi
663                 if [ -n "$PVDEVICES" ] ; then
664                         for device in $PVDEVICES; do
665                                 find_modules_for $device
666                         done
667                 else
668                         echo "ERROR: I wasn't able to find PV (via lvdisplay and pvdisplay)." >&2
669                         echo "You can try to set PVDEVICES in /etc/sysconfig/geninitrd." >&2
670                         exit 1
671                 fi
672                 if [ "$LVMTOOLSVERSION" = "2" ]; then
673                         findmodule "-dm-mod"
674                 elif [ "$LVMTOOLSVERSION" = "1" ]; then
675                         findmodule "-lvm"
676                         findmodule "-lvm-mod"
677                 else
678                         echo "ERROR: LVM version $LVMTOOLSVERSION is not supported yet." >&2
679                         exit 1
680                 fi
681                 [ -n "$verbose" ] && echo "LVM $LVMTOOLSVERSION enabled"
682                 uselvm="yes"
683         fi
684 }
685
686 firmware_install_module_pre() {
687         module="$1"
688         linuxrc="$2"
689         firmware_files="$3"
690
691         [ -n "$verbose" ] && echo "Adding Firmwares ($firmware_files) to initrd for module $module"
692         mkdir -p $MNTIMAGE/proc
693         mkdir -p $MNTIMAGE/sys
694         # firmware not yet installed
695         if [ ! -f "$MNTIMAGE/lib/firmware/firmware.sh" ]; then
696                 mkdir -p $MNTIMAGE/lib/firmware
697 cat << 'EOF' >> "$MNTIMAGE/lib/firmware/firmware.sh"
698 #!/bin/sh -e
699 echo 1 > /sys$DEVPATH/loading
700 cat "/lib/firmware/$FIRMWARE" > /sys$DEVPATH/data
701 echo 0 > /sys$DEVPATH/loading
702 exit 0
703 EOF
704         chmod 755 "$MNTIMAGE/lib/firmware/firmware.sh"
705         fi
706
707         for firmware in $firmware_files; do
708                 inst "/lib/firmware/$firmware" "$MNTIMAGE/lib/firmware/$firmware"
709         done
710
711         echo "mount -t proc none /proc" >> "$linuxrc"
712         echo "mount -t sysfs none /sys" >> "$linuxrc"
713         echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" >> "$linuxrc"
714 }
715
716 firmware_install_module_post() {
717         module="$1"
718         linuxrc="$2"
719         firmware_files="$3"
720         echo "umount /sys" >> "$linuxrc"
721         echo "umount /proc" >> "$linuxrc"
722 }
723
724 modules_install() {
725         modules="$1"
726
727         for mod in $modules; do
728                 MODULEDIR="`dirname "$mod"`"
729                 mkdir -p "$MNTIMAGE/lib/modules/$kernel/$MODULEDIR"
730                 cp -a "/lib/modules/$kernel/$mod" "$MNTIMAGE/lib/modules/$kernel/$mod"
731                 gunzip "$MNTIMAGE/lib/modules/$kernel/$mod" 2> /dev/null
732         done
733 }
734
735 modules_add_linuxrc() {
736         modules="$1"
737         linuxrc="$2"
738
739         for mod in $modules; do
740                 MODULE2="`dirname "$mod"`"
741                 NAME2=`basename "$mod" .gz`
742                 MODULE2=$MODULE2/$NAME2
743                 module="`echo $mod | awk -F/ '{ $0=$NF } /'$modext'.*$/ { gsub(/'$modext'.*/, NIL, $0); } { print $0; }'`"
744                 options="`awk '{ if($1 == "options" && $2 == "'${module}'") { for(i=3;i<=NF;i++) printf("%s ",$i); }}' "$modulefile"`"
745
746                 generic_module=$(echo "${module}" | awk ' { gsub("-", "_", $0) } { print $0; } ')
747                 sleep_var="$(eval echo \$MODULE_${generic_module}_USLEEP)"
748             firmware_var="$(eval echo \$MODULE_${generic_module}_FIRMWARE)"
749
750                 if [ -n "$verbose" ]; then
751                         echo -n "Loading module [$module] "
752                         if [ -n "$options" ] ; then
753                                 echo -n "with options [$options]"
754                         else
755                                 echo -n "without options"
756                         fi
757                         if [ -n "$sleep_var" ]; then
758                                 echo " and $sleep_var usleep."
759                         else
760                                 echo "."
761                         fi
762                 fi
763
764                 if [ -n "$firmware_var" ]; then
765                         firmware_install_module_pre "$module" "$linuxrc" "$firmware_var"
766                 fi
767                 echo "$insmod /lib/modules/$kernel/$MODULE2 $options" >> "$linuxrc"
768                 if [ -n "${sleep_var}" ]; then
769                         echo "usleep $sleep_var" >> "$linuxrc"
770                 fi
771                 if [ -n "$firmware_var" ]; then
772                         firmware_install_module_post "$module" "$linuxrc" "$firmware_var"
773                 fi
774         done
775 }
776
777 if [ -r /etc/sysconfig/geninitrd ] ; then
778         . /etc/sysconfig/geninitrd
779 fi
780
781 if [ -r /etc/sysconfig/bootsplash ] ; then
782         . /etc/sysconfig/bootsplash
783 fi
784
785 if [ ! -x /bin/initrd-busybox ] ; then
786         echo "/bin/initrd-busybox is missing !"
787         exit 1
788 fi
789
790 while [ $# -gt 0 ]; do
791         case $1 in
792         --fstab=*)
793                 fstab="`echo $1 | awk -F= '{print $2;}'`"
794                 ;;
795         --fstab)
796                 fstab="$2"
797                 shift
798                 ;;
799         --modules-conf=*)
800                 modulefile="`echo $1 | awk -F= '{print $2;}'`"
801                 ;;
802         --modules-conf)
803                 modulefile="$2"
804                 shift
805                 ;;
806         --use-raidstart|--with-raidstart)
807                 USERAIDSTART="yes"
808                 ;;
809         --without-raidstart)
810                 USERAIDSTART="no"
811                 ;;
812         --use-insmod-static|--with-insmod-static)
813                 USEINSMODSTATIC="yes"
814                 ;;
815         --without-insmod-static)
816                 USEINSMODSTATIC="no"
817                 ;;
818         --without-bootsplash)
819                 BOOT_SPLASH="no"
820                 ;;
821         --without-suspend)
822                 USE_SUSPEND="no";
823                 ;;
824         --without-suspend2)
825                 USE_SUSPEND2="no";
826                 ;;
827         --lvmtoolsversion=|--lvmversion=)
828                 LVMTOOLSVERSION="`echo $1 | awk -F= '{print $2;}'`"
829                 ;;
830         --lvmtoolsversion|--lvmversion)
831                 LVMTOOLSVERSION="$2"
832                 shift
833                 ;;
834         --without-udev)
835                 USE_UDEV=
836                 ;;
837         --with-udev)
838                 USE_UDEV="yes"
839                 ;;
840         --without-dmraid)
841                 USE_DMRAID=
842                 ;;
843         --with=*)
844                 BASICMODULES="$BASICMODULES `echo $1 | awk -F= '{print $2;}'`"
845                 ;;
846         --with)
847                 BASICMODULES="$BASICMODULES $2"
848                 shift
849                 ;;
850         --version)
851                 echo "geninitrd: version $VERSION"
852                 exit 0
853                 ;;
854         -v)
855                 verbose=-v
856                 ;;
857         --nocompress)
858                 COMPRESS="no"
859                 ;;
860         --ifneeded)
861                 ifneeded=1
862                 ;;
863         -f)
864                 force=1
865                 ;;
866         --preload=*)
867                 PREMODS="$PREMODS `echo $1 | awk -F= '{print $2;}'`"
868                 ;;
869         --preload)
870                 PREMODS="$PREMODS $2"
871                 shift
872                 ;;
873         --fs=*)
874                 echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
875                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
876                 ;;
877         --fs)
878                 echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
879                 INITRDFS="$2"
880                 shift
881                 ;;
882         --initrdfs=*)
883                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
884                 ;;
885         --initrdfs)
886                 INITRDFS="$2"
887                 shift
888                 ;;
889         --image-version)
890                 img_vers=yes
891                 ;;
892         --ide-only-root)
893                 ide_only_root="yes"
894                 ;;
895         *)
896                 if [ -z "$target" ]; then
897                         target="$1"
898                 elif [ -z "$kernel" ]; then
899                         kernel="$1"
900                 else
901                         usage
902                 fi
903                 ;;
904         esac
905
906         shift
907 done
908
909 if [ -z "$target" -o -z "$kernel" ]; then
910         usage
911 fi
912
913 pack_version="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d",$1,$2)}'`"
914 pack_version_long="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}'`"
915
916 if [ "x" = "x$INITRDFS" ] ; then
917         if [ "x" = "x$FS" ] ; then
918                 # default value
919                 # XXX: initramfs blocked for now
920                 if [ "1" = " 0" -a "$pack_version" -ge "002005" ] ; then
921                         INITRDFS="initramfs"
922                 else
923                         INITRDFS="rom"
924                 fi
925         else
926                 echo "Warning: FS configuration options is obsoleted. Use INITRDFS instead" 1>&2
927                 INITRDFS="$FS"
928         fi
929 fi
930
931 if [ "$pack_version" -lt "002006" ] ; then
932         USE_UDEV=
933         USE_DMRAID=
934 fi
935
936 if [ "$pack_version" -ge "002005" ] ; then
937         modext=".ko"
938         insmod="insmod"
939 fi
940
941 if is_yes "$USEINSMODSTATIC" ; then
942         insmod="insmod.static"
943         INSMOD="/sbin/insmod.static"
944         if [ "$pack_version" -lt "002005" -a -f /sbin/insmod.static.modutils ] ; then
945                 INSMOD="/sbin/insmod.static.modutils"
946         fi
947         if [ ! -f "$INSMOD" ] ; then
948                 echo "insmod.static requested but /sbin/insmod.static not found !" >&2
949                 exit 1
950         fi
951 fi
952
953 case "$INITRDFS" in
954         ext2)
955                 if [ ! -x /sbin/mke2fs ]; then
956                         echo "/sbin/mke2fs is missing" 1>&2
957                         exit 1
958                 fi
959                 ;;
960         rom)
961                 if [ ! -x /sbin/genromfs ]; then
962                         echo "/sbin/genromfs is missing" 1>&2
963                         exit 1
964                 fi
965                 ;;
966         cram)
967                 if [ ! -x /sbin/mkcramfs ]; then
968                         echo "/sbin/mkcramfs is missing" 1>&2
969                         exit 1
970                 fi
971                 ;;
972         initramfs)
973                 if [ ! -x /bin/cpio ]; then
974                         echo "/bin/cpio is missing" 1>&2
975                         exit 1
976                 fi
977                 if [ ! -x /usr/bin/find ]; then
978                         echo "/usr/bin/find is missing" 1>&2
979                         exit 1
980                 fi
981                 ;;
982         *)
983                 echo "Filesystem $INITRDFS on initrd is not supported" 1>&2
984                 exit 1
985                 ;;
986 esac
987
988 if [ -n "$img_vers" ]; then
989         target="$target-$kernel"
990 fi
991
992 if [ -z "$force" -a -f "$target" ]; then
993         echo "$target already exists." 1>&2
994         exit 1
995 fi
996
997 if [ ! -d "/lib/modules/$kernel" ]; then
998         echo "/lib/modules/$kernel is not a directory." 1>&2
999         exit 1
1000 fi
1001
1002 if is_yes "$USE_SUSPEND"; then
1003         if is_yes "$USE_SUSPEND2"; then
1004                 echo "Suspend2 shouldn't be used in parallel with mainline suspend!." 1>&2
1005                 exit 1
1006         fi
1007 fi
1008
1009 if [ ! -f /proc/mounts ]; then
1010         echo "WARNING: /proc filesystem not mounted, may cause wrong results or failure." 1>&2
1011 fi
1012
1013 if [ "$pack_version" -lt "002005" ]; then
1014         modulefile=/etc/modules.conf
1015         if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
1016                 modulefile=/etc/conf.modules
1017         fi
1018 else
1019         modulefile=/etc/modprobe.conf
1020 fi
1021
1022 for n in $PREMODS; do
1023         findmodule "$n"
1024 done
1025
1026 # allow forcing loading SCSI and/or IDE modules
1027 if is_yes "$ADDSCSI" ; then
1028         find_modules_scsi
1029 fi
1030
1031 if is_yes "$ADDIDE" ; then
1032         find_modules_ide
1033 fi
1034
1035 find_root
1036
1037 org_rootdev="$rootdev1"
1038
1039 find_modules_for "$rootdev1"
1040
1041 findmodule "-$rootFs"
1042
1043 for n in $BASICMODULES; do
1044         findmodule "$n"
1045 done
1046
1047 if is_yes "$USE_SUSPEND2"; then
1048         findmodule "-lzf"
1049 fi
1050
1051 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
1052         if [ -n "$verbose" ]; then
1053                 echo "No modules are needed -- not building initrd image."
1054         fi
1055         exit 0
1056 fi
1057
1058 if [ -n "$verbose" ]; then
1059         echo "Using modules: $MODULES"
1060 fi
1061
1062 MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
1063 IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
1064 MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
1065 RCFILE="$MNTIMAGE/linuxrc"
1066
1067 if [ -f "$MNTIMAGE" ]; then
1068         echo "$MNTIMAGE already exists. Remove it and try again" 1>&2
1069         exit 1
1070 fi
1071
1072 if [ -f "$IMAGE" ]; then
1073         echo "$IMAGE already exists. Remove it and try again" 1>&2
1074         exit 1
1075 fi
1076
1077 mkdir -p "$MNTPOINT"
1078
1079
1080 mkdir -p "$MNTIMAGE"/{lib,bin,etc,dev,loopfs,var}
1081
1082
1083 # We don't need this directory, so let's save space
1084 rm -rf "$MNTPOINT"/lost+found
1085
1086 modules_install "$MODULES"
1087
1088 # mknod'ing the devices instead of copying them works both with and
1089 # without devfs...
1090 mknod "$MNTIMAGE/dev/console" c 5 1
1091 mknod "$MNTIMAGE/dev/null" c 1 3
1092 mknod "$MNTIMAGE/dev/zero" c 1 5
1093
1094 s="$RCFILE"
1095 ln -s /linuxrc $MNTIMAGE/init
1096
1097 inst /bin/initrd-busybox "$MNTIMAGE/bin/initrd-busybox"
1098 ln -s initrd-busybox "$MNTIMAGE/bin/sh"
1099 ln -s initrd-busybox "$MNTIMAGE/bin/busybox" # for older busyboxes who had /bin/busybox as EXEPATH
1100
1101 if is_yes "$USEINSMODSTATIC" ; then
1102         inst "$INSMOD" $MNTIMAGE/bin/insmod.static
1103 fi
1104
1105 cat > "$s" <<EOF
1106 #! /bin/sh
1107
1108 set -x
1109 EOF
1110 chmod 755 "$s"
1111
1112 modules_add_linuxrc "$MODULES" "$s"
1113
1114 # TODO: rewrite for busybox
1115 #if [ -n "$loopDev" ]; then
1116 #       if [ ! -d /initrd ]; then
1117 #               mkdir /initrd
1118 #       fi
1119 #
1120 #       cp -a "$loopDev" "$MNTIMAGE/dev"
1121 #       cp -a "$rootdev1" "$MNTIMAGE/dev"
1122 #       echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1123 #       echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1124 #       echo "echo Setting up loopback device $rootdev1" >> $RCFILE
1125 #       echo "losetup $rootdev1 /loopfs$loopFile" >> "$RCFILE"
1126 #fi
1127
1128 initrd_gen_suspend() {
1129         if [ ! -x /usr/sbin/resume ]; then
1130                 echo "/usr/sbin/resume is missing !"
1131                 exit 1
1132         fi
1133         mkdir -p $MNTIMAGE/etc
1134         mkdir -p $MNTIMAGE/dev
1135         resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
1136         cp -HR /dev/snapshot $resume_dev $MNTIMAGE/dev
1137         inst /etc/suspend.conf $MNTIMAGE/etc/suspend.conf
1138         inst /usr/sbin/resume "$MNTIMAGE/bin/resume"
1139         echo "resume" >> "$s"
1140 }
1141
1142 initrd_gen_suspend2() {
1143         mkdir -p $MNTIMAGE/sys
1144         mkdir -p $MNTIMAGE/proc
1145 cat << 'EOF' >> "$s"
1146 mount -t proc none /proc
1147 if [ "$(awk ' /resume2=/  { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
1148 EOF
1149 cat << EOF >> "$s"
1150                 echo > /proc/suspend2/do_resume
1151 fi
1152 umount /proc
1153 EOF
1154 }
1155
1156 initrd_gen_tmpfs_dev() {
1157         tmpfs_dev=yes
1158         cat <<-EOF
1159 : 'Creating /dev'
1160 mount -o mode=0755 -t tmpfs none /dev
1161 mknod /dev/console c 5 1
1162 mknod /dev/null c 1 3
1163 mknod /dev/zero c 1 5
1164 mkdir /dev/pts
1165 mkdir /dev/shm
1166 EOF
1167 }
1168
1169 initrd_gen_udev() {
1170         [ -n "$verbose" ] && echo "Setting up udev..."
1171         mkdir -p $MNTIMAGE/sbin
1172         mkdir -p $MNTIMAGE/proc
1173         mkdir -p $MNTIMAGE/etc/udev
1174         mkdir -p $MNTIMAGE/sys
1175
1176         inst /sbin/initrd-udev $MNTIMAGE/sbin/udev
1177         ln -s udev $MNTIMAGE/sbin/udevstart
1178         inst /etc/udev/udev.conf $MNTIMAGE/etc/udev/udev.conf
1179
1180         ln -s udev $MNTIMAGE/sbin/hotplug
1181
1182         if is_yes "$USE_UDEV"; then
1183                 initrd_gen_tmpfs_dev >> "$s"
1184                 cat >> "$s" <<-EOF
1185 mount -t proc none /proc
1186 mount -t sysfs none /sys
1187 : 'Starting udev'
1188 /sbin/udevstart
1189 echo -n /sbin/hotplug > /proc/sys/kernel/hotplug
1190 umount /proc
1191 umount /sys
1192 EOF
1193         fi
1194 }
1195
1196 initrd_gen_dmraid() {
1197         if [ ! -x /sbin/dmraid-initrd ] ; then
1198                 echo "/sbin/dmraid-initrd is missing missing !"
1199                 exit 1
1200         fi
1201
1202         # AFAIR it just needs tmpfs on /dev.
1203         if [ ! -x $MNTIMAGE/sbin/udev ]; then
1204                 echo "udev is needed on target initrd for dmraid to work!"
1205                 exit 1
1206         fi
1207
1208         mkdir -p "$MNTIMAGE/sbin"
1209         inst /sbin/dmraid-initrd $MNTIMAGE/sbin/dmraid
1210         cat <<-EOF >> "$s"
1211         mount -t proc none /proc
1212         mount -t sysfs none /sys
1213         # 2 secs was enough for my system to initialize. but really this is udev issue?
1214         usleep 2000000
1215         : 'Activating Device-Mapper RAID(s)'
1216         /sbin/dmraid -ay -i
1217         umount /sys
1218         umount /proc
1219 EOF
1220 }
1221
1222
1223 initrd_gen_softraid() {
1224         [ -n "$verbose" ] && echo "Setting up mdadm..."
1225
1226         if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ] ; then
1227                 echo "/sbin/mdadm or /sbin/initrd-mdassemble is missing !"
1228                 exit 1
1229         fi
1230
1231         inst /sbin/initrd-mdassemble "$MNTIMAGE/bin/mdassemble"
1232
1233         # LVM on RAID case
1234         dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf)
1235         for ex_dev in $dev_list_extra; do
1236                 echo "DEVICE $ex_dev" >> "$MNTIMAGE/etc/mdadm.conf"
1237         done
1238         do_md0=1
1239         for nr in `seq 1 $rootdev_nr`; do
1240                 eval cr_rootdev="\$rootdev${nr}"
1241                 eval cr_dev_list="\$dev_list${nr}"
1242                 [ -n "$verbose" ] && echo "Setting up array ($cr_rootdev = $cr_dev_list)"
1243
1244                 [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
1245
1246                 echo "DEVICE $cr_dev_list" >> "$MNTIMAGE/etc/mdadm.conf"
1247                 cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
1248                 cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev)
1249                 if [ -n "$cr_md_conf" ]; then
1250                         echo "$cr_md_conf" >> "$MNTIMAGE/etc/mdadm.conf"
1251                 else
1252                         echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$MNTIMAGE/etc/mdadm.conf"
1253                 fi
1254
1255                 for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
1256                         # mkdir in case of devfs name
1257                         mkdir -p $MNTIMAGE/`dirname $f`
1258                         [ -e "$MNTIMAGE/$f" ] && continue
1259                         [ -n "$verbose" ] && echo "copying $f"
1260                         # this works fine with and without devfs
1261                         cp -HR $f $MNTIMAGE/$f
1262                 done
1263         done
1264
1265         echo "mdassemble" >> "$s"
1266
1267         # needed to determine md-version
1268         if [ "$do_md0" -eq 1 ] ; then
1269                 mknod $MNTIMAGE/dev/md0 b 9 0
1270         fi
1271 }
1272
1273 initrd_gen_nfs() {
1274         # use root=/dev/ram0 init=/linuxrc when starting kernel or you will
1275         # have problems like init(XX) being child process of swapper(1).
1276         [ -n "$verbose" ] && echo "Adding rootfs on NFS support to initrd (dhcp)"
1277         mknod "$MNTIMAGE/dev/urandom" c 1 8
1278         mkdir "$MNTIMAGE/newroot"
1279         mkdir "$MNTIMAGE/proc"
1280         echo "ifconfig lo 127.0.0.1 up" >> "$s"
1281         echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" >> "$s"
1282         echo "ifconfig eth0 0.0.0.0 up" >> "$s"
1283         echo "udhcpc -i eth0 -f -q -s /bin/setdhcp" >> "$s"
1284         cat << 'EOF' > "$MNTIMAGE/bin/setdhcp"
1285 #!/bin/sh
1286 [ "$1" != "bound" ] && exit
1287 [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
1288 [ -n "$subnet" ] && NETMASK="netmask $subnet"
1289 set -x
1290 ifconfig $interface $ip $BROADCAST $NETMASK up
1291 set +x
1292 if [ -n "$router" ]; then
1293         for r in $router; do
1294                 set -x
1295                 route add default gw $r dev $interface
1296                 set +x
1297         done
1298 fi
1299
1300 mount -t proc none /proc
1301 for o in $(cat /proc/cmdline); do
1302         case $o in
1303         nfsroot=*)
1304                 rootpath=${o#nfsroot=}
1305                 ;;
1306         esac
1307 done
1308 umount /proc
1309
1310 if [ -n "$rootpath" ]; then
1311         set -x
1312         mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
1313         set +x
1314 else
1315         set +x
1316         echo "Missing rootpath in what DHCP server sent to us. Failing..."
1317         echo "All seen variables are listed below:"
1318         set
1319         set -x
1320 fi
1321 EOF
1322         chmod 755 "$MNTIMAGE/bin/setdhcp"
1323         echo "cd /newroot" >> "$s"
1324         echo "pivot_root . initrd" >> "$s"
1325         echo "[ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1326         echo "exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1327 }
1328
1329 initrd_gen_lvm() {
1330         [ -n "$verbose" ] && echo "Adding LVM support to initrd"
1331         inst /sbin/initrd-lvm $MNTIMAGE/bin/lvm
1332         mkdir -p $MNTIMAGE/etc
1333         mkdir -p $MNTIMAGE/tmp
1334         mkdir -p $MNTIMAGE/proc
1335         mkdir -p $MNTIMAGE/newroot
1336
1337         # always make /dev on tmpfs for LVM2
1338         if [ "$LVMTOOLSVERSION" = "2" ] && ! is_yes "$tmpfs_dev"; then
1339                 initrd_gen_tmpfs_dev >> "$s"
1340         fi
1341
1342         if ! is_yes "$tmpfs_dev"; then
1343                 mkdir -p $MNTIMAGE/dev/mapper
1344                 mknod $MNTIMAGE/dev/mapper/control c 10 63
1345                 for device in $PVDEVICES; do
1346                         # if LVM on RAID then device might be copied already in gen_softraid
1347                         [ -e "$MNTIMAGE/dev/$(basename $device)" ] && continue
1348                         cp -HR $device $MNTIMAGE/dev/
1349                 done
1350         fi
1351         echo "mount -t proc none /proc" >> "$s"
1352         echo "mount -t tmpfs none /tmp" >> "$s"
1353         if [ "$LVMTOOLSVERSION" = "1" ] ; then
1354                 echo "lvm vgscan -T" >> "$s"
1355                 echo "lvm vgchange -T -a y $VGVOLUME" >> "$s"
1356                 echo "umount /tmp" >> "$s"
1357                 # fail to umount
1358                 echo "umount /dev" >> "$s"
1359                 echo "umount /proc" >> "$s"
1360         else
1361                 org_rootdev=$(_lvm2_node_resolve "$org_rootdev")
1362                 echo "cat /etc/lvm.conf > /tmp/lvm.conf" >> "$s"
1363                 echo "global {" > "$MNTIMAGE/etc/lvm.conf"
1364                 echo "  locking_type = 0" >> "$MNTIMAGE/etc/lvm.conf"
1365                 echo "  locking_dir = \"/tmp\"" >> "$MNTIMAGE/etc/lvm.conf"
1366                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1367                 echo "devices {" >> "$MNTIMAGE/etc/lvm.conf"
1368                 echo "  sysfs_scan=0" >> "$MNTIMAGE/etc/lvm.conf"
1369                 if is_yes "$raidfound"; then
1370                         echo "  md_component_detection = 1" >> "$MNTIMAGE/etc/lvm.conf"
1371                 fi
1372                 lvm dumpconfig | awk '/filter=/' >> "$MNTIMAGE/etc/lvm.conf"
1373                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1374
1375                 cat <<-EOF >> "$s"
1376                 : 'Making device nodes'
1377                 set +x
1378                 (
1379                 # ignore first two lines, header and empty line
1380                 read a
1381                 read a
1382                 while read major minor blocks dev; do
1383                         mknod /dev/\$dev b \$major \$minor
1384                 done
1385                 ) < /proc/partitions
1386
1387                 # disable noise from lvm accessing devices that aren't ready.
1388                 printk=\$(cat /proc/sys/kernel/printk)
1389                 echo 0 > /proc/sys/kernel/printk
1390                 set -x
1391
1392                 : 'Scanning for Volume Groups'
1393                 LVM_SYSTEM_DIR=/tmp lvm vgscan --ignorelockingfailure 2>/dev/null
1394
1395                 : 'Activating Volume Groups'
1396                 LVM_SYSTEM_DIR=/tmp lvm vgchange --ignorelockingfailure -a y $VGVOLUME 2>/dev/null
1397
1398                 echo "\$printk" > /proc/sys/kernel/printk
1399
1400                 # Find out major/minor
1401                 majmin="\$(LVM_SYSTEM_DIR=/tmp lvm lvdisplay --ignorelockingfailure -c $org_rootdev 2>/dev/null)"
1402                 majmin="\${majmin#*/}"
1403                 majmin="\${majmin#*:*:*:*:*:*:*:*:*:*:*:*}"
1404                 major="\${majmin%:*}"
1405                 minor="\${majmin#*:}"
1406                 # Pass it to kernel
1407                 val=\$((256 * \$major + \$minor))
1408                 echo \$val > /proc/sys/kernel/real-root-dev
1409                 umount /tmp
1410                 umount /proc
1411 EOF
1412         fi
1413 }
1414
1415 initrd_gen_procdata() {
1416         [ -n "$verbose" ] && echo "Adding rootfs finding based on root= option support."
1417         mkdir -p $MNTIMAGE/proc
1418 cat << 'EOF' >> "$s"
1419 set +x
1420 mount -t proc none /proc
1421 root="$(busybox awk ' /root=\/dev\// { gsub(/.*root=\/dev\//,NIL,$0); gsub(/ .*/,NIL,$0); print $0; } ' /proc/cmdline)"
1422 if [ -n "$root" ]; then
1423         rootnr="$(busybox awk -v root="$root" ' { if ($4 == root) { print 256*$1+$2; } } ' /proc/partitions)"
1424         if [ -n "$rootnr" ]; then
1425                 echo "$rootnr" > /proc/sys/kernel/real-root-dev
1426         fi
1427 fi
1428 umount /proc
1429 set -x
1430 EOF
1431 }
1432
1433 # main generation
1434
1435 if is_yes "$USE_UDEV"; then
1436         initrd_gen_udev
1437 fi
1438
1439 if is_yes "$USE_SUSPEND"; then
1440         initrd_gen_suspend
1441 fi
1442
1443 if is_yes "$USE_SUSPEND2"; then
1444         initrd_gen_suspend2
1445 fi
1446
1447 if is_yes "$USE_DMRAID"; then
1448         initrd_gen_dmraid
1449 fi
1450
1451 if is_yes "$usenfs" ; then
1452         initrd_gen_nfs
1453 elif is_yes "$USERAIDSTART" && is_yes "$raidfound" ; then
1454         initrd_gen_softraid
1455         if is_yes "$uselvm" ; then
1456                 initrd_gen_lvm
1457         else
1458                 initrd_gen_procdata
1459         fi
1460 elif is_yes "$uselvm" ; then
1461         initrd_gen_lvm
1462 else
1463         initrd_gen_procdata
1464 fi
1465
1466 if [ "$INITRDFS" = "initramfs" ]; then
1467         mkdir -p $MNTIMAGE/newroot
1468         cp -HR $org_rootdev $MNTIMAGE/dev
1469         echo "mount -t $rootFs $org_rootdev /newroot" >> "$s"
1470         echo "switch_root /newroot /sbin/init" >> "$s"
1471         # we need real file, not symlink
1472         rm -f $MNTIMAGE/init
1473         cp -a $MNTIMAGE/linuxrc $MNTIMAGE/init
1474 fi
1475
1476 chmod +x "$RCFILE"
1477
1478 (cd "$MNTIMAGE"; tar cf - .) | (cd "$MNTPOINT"; tar xf -)
1479
1480 case "$INITRDFS" in
1481         ext2)
1482                 IMAGESIZE=$(du -ks $MNTPOINT | awk '{print int(($1+1023)/1024)*1024}')
1483                 [ "$verbose" ] && echo "ext2 image size: $IMAGESIZE ($MNTPOINT)"
1484                 if [ "$IMAGESIZE" -gt 4096 ]; then
1485                         echo >&2 "$0: Your image size is larger than 4096, You should add ramdisk_size=$IMAGESIZE to your kernel commandline!"
1486                 fi
1487
1488                 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
1489                 mke2fs -q -F -b 1024 -m 0 "$IMAGE" 2>/dev/null 1>&2
1490                 tune2fs -i 0 "$IMAGE" >/dev/null 2>&1
1491
1492                 tmpmnt="`mktemp -d /tmp/initrd.mnte2-XXXXXX`"
1493                 mount -o loop -t ext2 "$IMAGE" "$tmpmnt"
1494
1495                 (cd "$MNTPOINT"; tar cf - .) | (cd "$tmpmnt"; tar xf -)
1496
1497                 umount "$IMAGE"
1498                 ;;
1499         rom)
1500                 genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
1501                 ;;
1502         cram)
1503                 mkcramfs "$MNTPOINT" "$IMAGE"
1504                 ;;
1505         initramfs)
1506                 (cd $MNTPOINT ; find . | cpio -H newc -o > "$IMAGE")
1507                 ;;
1508         *)
1509                 echo "Filesystem $INITRDFS not supported by $0";
1510 esac
1511
1512 if is_yes "$COMPRESS" ; then
1513         gzip -9 < "$IMAGE" > "$target"
1514 else
1515         cp -a "$IMAGE" "$target"
1516 fi
1517
1518 if is_yes "$BOOT_SPLASH"; then
1519         if [ ! -x /bin/splash.bin ]; then
1520                 echo "Failed to execute /bin/splash.bin. Is bootsplash package installed?" 1>&2
1521         elif [ -z "$THEME" ]; then
1522                 echo "Please configure your /etc/sysconfig/bootsplash first." 1>&2
1523                 echo "Generating bootsplashes skipped." 1>&2
1524         else
1525                 if [ -n "$BOOT_SPLASH_RESOLUTIONS" ]; then
1526                         for res in $BOOT_SPLASH_RESOLUTIONS; do
1527                                 if [ -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
1528                                         /bin/splash.bin -s -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> "$target" && \
1529                                         [ -n "$verbose" ] && echo "Added $res $THEME theme to initrd."
1530                                 else
1531                                         echo "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped" 1>&2
1532                                 fi
1533                         done
1534                 else
1535                         echo "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash." 1>&2
1536                         echo "Not adding bootsplash to initrd." 1>&2
1537                 fi
1538         fi
1539 fi
1540
1541 rm -rf "$MNTIMAGE" "$MNTPOINT" "$IMAGE"
1542
1543 # vim:ts=4:sw=4:noet:fdm=marker
This page took 0.118719 seconds and 2 git commands to generate.