]> git.pld-linux.org Git - projects/geninitrd.git/blob - geninitrd
Use xfs_admin to get xfs filesystem UUID and trim it for spaces.
[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_db ] ; then
294                                         echo "/usr/sbin/xfs_db 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 # 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 -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
761 while [ $# -gt 0 ]; do
762         case $1 in
763         --fstab=*)
764                 fstab="`echo $1 | awk -F= '{print $2;}'`"
765                 ;;
766         --fstab)
767                 fstab="$2"
768                 shift
769                 ;;
770         --modules-conf=*)
771                 modulefile="`echo $1 | awk -F= '{print $2;}'`"
772                 ;;
773         --modules-conf)
774                 modulefile="$2"
775                 shift
776                 ;;
777         --use-raidstart|--with-raidstart)
778                 USERAIDSTART="yes"
779                 ;;
780         --without-raidstart)
781                 USERAIDSTART="no"
782                 ;;
783         --use-insmod-static|--with-insmod-static)
784                 USEINSMODSTATIC="yes"
785                 ;;
786         --without-insmod-static)
787                 USEINSMODSTATIC="no"
788                 ;;
789         --without-bootsplash)
790                 BOOT_SPLASH="no"
791                 ;;
792         --without-suspend)
793                 USE_SUSPEND="no";
794                 ;;
795         --without-suspend2)
796                 USE_SUSPEND2="no";
797                 ;;
798         --lvmtoolsversion=|--lvmversion=)
799                 LVMTOOLSVERSION="`echo $1 | awk -F= '{print $2;}'`"
800                 ;;
801         --lvmtoolsversion|--lvmversion)
802                 LVMTOOLSVERSION="$2"
803                 shift
804                 ;;
805         --without-udev)
806                 USE_UDEV=
807                 ;;
808         --with-udev)
809                 USE_UDEV="yes"
810                 ;;
811         --without-dmraid)
812                 USE_DMRAID=
813                 ;;
814         --with=*)
815                 BASICMODULES="$BASICMODULES `echo $1 | awk -F= '{print $2;}'`"
816                 ;;
817         --with)
818                 BASICMODULES="$BASICMODULES $2"
819                 shift
820                 ;;
821         --version)
822                 echo "geninitrd: version $VERSION"
823                 exit 0
824                 ;;
825         -v)
826                 verbose=-v
827                 ;;
828         --nocompress)
829                 COMPRESS="no"
830                 ;;
831         --ifneeded)
832                 ifneeded=1
833                 ;;
834         -f)
835                 force=1
836                 ;;
837         --preload=*)
838                 PREMODS="$PREMODS `echo $1 | awk -F= '{print $2;}'`"
839                 ;;
840         --preload)
841                 PREMODS="$PREMODS $2"
842                 shift
843                 ;;
844         --fs=*)
845                 echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
846                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
847                 ;;
848         --fs)
849                 echo "Warning: --fs option is obsoleted. Use --initrdfs instead" 1>&2
850                 INITRDFS="$2"
851                 shift
852                 ;;
853         --initrdfs=*)
854                 INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
855                 ;;
856         --initrdfs)
857                 INITRDFS="$2"
858                 shift
859                 ;;
860         --image-version)
861                 img_vers=yes
862                 ;;
863         --ide-only-root)
864                 ide_only_root="yes"
865                 ;;
866         *)
867                 if [ -z "$target" ]; then
868                         target="$1"
869                 elif [ -z "$kernel" ]; then
870                         kernel="$1"
871                 else
872                         usage
873                 fi
874                 ;;
875         esac
876
877         shift
878 done
879
880 if [ -z "$target" -o -z "$kernel" ]; then
881         usage
882 fi
883
884 pack_version="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d",$1,$2)}'`"
885 pack_version_long="`echo "$kernel"|awk -F. '{print sprintf("%03d%03d%03d",$1,$2,$3)}'`"
886
887 if [ "x" = "x$INITRDFS" ] ; then
888         if [ "x" = "x$FS" ] ; then
889                 # default value
890                 # XXX: initramfs blocked for now
891                 if [ "1" = " 0" -a "$pack_version" -ge "002005" ] ; then
892                         INITRDFS="initramfs"
893                 else
894                         INITRDFS="rom"
895                 fi
896         else
897                 echo "Warning: FS configuration options is obsoleted. Use INITRDFS instead" 1>&2
898                 INITRDFS="$FS"
899         fi
900 fi
901
902 if [ "$pack_version" -lt "002006" ] ; then
903         USE_UDEV=
904         USE_DMRAID=
905 fi
906
907 if [ "$pack_version" -ge "002005" ] ; then
908         modext=".ko"
909         insmod="insmod"
910 fi
911
912 if is_yes "$USEINSMODSTATIC" ; then
913         insmod="insmod.static"
914         INSMOD="/sbin/insmod.static"
915         if [ "$pack_version" -lt "002005" -a -f /sbin/insmod.static.modutils ] ; then
916                 INSMOD="/sbin/insmod.static.modutils"
917         fi
918         if [ ! -f "$INSMOD" ] ; then
919                 echo "insmod.static requested but /sbin/insmod.static not found !" >&2
920                 exit 1
921         fi
922 fi
923
924 case "$INITRDFS" in
925         ext2)
926                 if [ ! -x /sbin/mke2fs ]; then
927                         echo "/sbin/mke2fs is missing" 1>&2
928                         exit 1
929                 fi
930                 ;;
931         rom)
932                 if [ ! -x /sbin/genromfs ]; then
933                         echo "/sbin/genromfs is missing" 1>&2
934                         exit 1
935                 fi
936                 ;;
937         cram)
938                 if [ ! -x /sbin/mkcramfs ]; then
939                         echo "/sbin/mkcramfs is missing" 1>&2
940                         exit 1
941                 fi
942                 ;;
943         initramfs)
944                 if [ ! -x /bin/cpio ]; then
945                         echo "/bin/cpio is missing" 1>&2
946                         exit 1
947                 fi
948                 if [ ! -x /usr/bin/find ]; then
949                         echo "/usr/bin/find is missing" 1>&2
950                         exit 1
951                 fi
952                 ;;
953         *)
954                 echo "Filesystem $INITRDFS on initrd is not supported" 1>&2
955                 exit 1
956                 ;;
957 esac
958
959 if [ -n "$img_vers" ]; then
960         target="$target-$kernel"
961 fi
962
963 if [ -z "$force" -a -f "$target" ]; then
964         echo "$target already exists." 1>&2
965         exit 1
966 fi
967
968 if [ ! -d "/lib/modules/$kernel" ]; then
969         echo "/lib/modules/$kernel is not a directory." 1>&2
970         exit 1
971 fi
972
973 if is_yes "$USE_SUSPEND"; then
974         if is_yes "$USE_SUSPEND2"; then
975                 echo "Suspend2 shouldn't be used in parallel with mainline suspend!." 1>&2
976                 exit 1
977         fi
978 fi
979
980 if [ ! -f /proc/mounts ]; then
981         echo "WARNING: /proc filesystem not mounted, may cause wrong results or failure." 1>&2
982 fi
983
984 if [ "$pack_version" -lt "002005" ]; then
985         modulefile=/etc/modules.conf
986         if [ ! -f "$modulefile" -a -f /etc/conf.modules ]; then
987                 modulefile=/etc/conf.modules
988         fi
989 else
990         modulefile=/etc/modprobe.conf
991 fi
992
993 for n in $PREMODS; do
994         findmodule "$n"
995 done
996
997 # allow forcing loading SCSI and/or IDE modules
998 if is_yes "$ADDSCSI" ; then
999         find_modules_scsi
1000 fi
1001
1002 if is_yes "$ADDIDE" ; then
1003         find_modules_ide
1004 fi
1005
1006 find_root
1007
1008 org_rootdev="$rootdev1"
1009
1010 find_modules_for "$rootdev1"
1011
1012 findmodule "-$rootFs"
1013
1014 for n in $BASICMODULES; do
1015         findmodule "$n"
1016 done
1017
1018 if is_yes "$USE_SUSPEND2"; then
1019         findmodule "-lzf"
1020 fi
1021
1022 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
1023         if [ -n "$verbose" ]; then
1024                 echo "No modules are needed -- not building initrd image."
1025         fi
1026         exit 0
1027 fi
1028
1029 if [ -n "$verbose" ]; then
1030         echo "Using modules: $MODULES"
1031 fi
1032
1033 MNTIMAGE="`mktemp -d /tmp/initrd.XXXXXX`"
1034 IMAGE="`mktemp -u /tmp/initrd.img-XXXXXX`"
1035 MNTPOINT="`mktemp -d /tmp/initrd.mnt-XXXXXX`"
1036 RCFILE="$MNTIMAGE/linuxrc"
1037
1038 if [ -f "$MNTIMAGE" ]; then
1039         echo "$MNTIMAGE already exists. Remove it and try again" 1>&2
1040         exit 1
1041 fi
1042
1043 if [ -f "$IMAGE" ]; then
1044         echo "$IMAGE already exists. Remove it and try again" 1>&2
1045         exit 1
1046 fi
1047
1048 mkdir -p "$MNTPOINT"
1049
1050
1051 mkdir -p "$MNTIMAGE"/{lib,bin,etc,dev,loopfs,var}
1052
1053
1054 # We don't need this directory, so let's save space
1055 rm -rf "$MNTPOINT"/lost+found
1056
1057 modules_install "$MODULES"
1058
1059 # mknod'ing the devices instead of copying them works both with and
1060 # without devfs...
1061 mknod "$MNTIMAGE/dev/console" c 5 1
1062 mknod "$MNTIMAGE/dev/null" c 1 3
1063 mknod "$MNTIMAGE/dev/zero" c 1 5
1064
1065 s="$RCFILE"
1066 ln -s /linuxrc $MNTIMAGE/init
1067
1068 inst /bin/initrd-busybox "$MNTIMAGE/bin/initrd-busybox"
1069 ln -s initrd-busybox "$MNTIMAGE/bin/sh"
1070 ln -s initrd-busybox "$MNTIMAGE/bin/busybox" # for older busyboxes who had /bin/busybox as EXEPATH
1071
1072 if is_yes "$USEINSMODSTATIC" ; then
1073         inst "$INSMOD" $MNTIMAGE/bin/insmod.static
1074 fi
1075
1076 cat > "$s" <<EOF
1077 #! /bin/sh
1078
1079 set -x
1080 EOF
1081 chmod 755 "$s"
1082
1083 modules_add_linuxrc "$MODULES" "$s"
1084
1085 # TODO: rewrite for busybox
1086 #if [ -n "$loopDev" ]; then
1087 #       if [ ! -d /initrd ]; then
1088 #               mkdir /initrd
1089 #       fi
1090 #
1091 #       cp -a "$loopDev" "$MNTIMAGE/dev"
1092 #       cp -a "$rootdev1" "$MNTIMAGE/dev"
1093 #       echo "echo Mounting device containing loopback root filesystem" >> "$RCFILE"
1094 #       echo "mount -t $loopFs $loopDev /loopfs" >> "$RCFILE"
1095 #       echo "echo Setting up loopback device $rootdev1" >> $RCFILE
1096 #       echo "losetup $rootdev1 /loopfs$loopFile" >> "$RCFILE"
1097 #fi
1098
1099 initrd_gen_suspend() {
1100         if [ ! -x /usr/sbin/resume ]; then
1101                 echo "/usr/sbin/resume is missing !"
1102                 exit 1
1103         fi
1104         mkdir -p $MNTIMAGE/etc
1105         mkdir -p $MNTIMAGE/dev
1106         resume_dev="$(awk '/^resume device =/ { print $4 } ' /etc/suspend.conf)"
1107         cp -HR /dev/snapshot $resume_dev $MNTIMAGE/dev
1108         inst /etc/suspend.conf $MNTIMAGE/etc/suspend.conf
1109         inst /usr/sbin/resume "$MNTIMAGE/bin/resume"
1110         echo "resume" >> "$s"
1111 }
1112
1113 initrd_gen_suspend2() {
1114         mkdir -p $MNTIMAGE/sys
1115         mkdir -p $MNTIMAGE/proc
1116 cat << 'EOF' >> "$s"
1117 mount -t proc none /proc
1118 if [ "$(awk ' /resume2=/  { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
1119 EOF
1120 cat << EOF >> "$s"
1121                 echo > /proc/suspend2/do_resume
1122 fi
1123 umount /proc
1124 EOF
1125 }
1126
1127 initrd_gen_tmpfs_dev() {
1128         tmpfs_dev=yes
1129         cat <<-EOF
1130 : 'Creating /dev'
1131 mount -o mode=0755 -t tmpfs none /dev
1132 mknod /dev/console c 5 1
1133 mknod /dev/null c 1 3
1134 mknod /dev/zero c 1 5
1135 mkdir /dev/pts
1136 mkdir /dev/shm
1137 EOF
1138 }
1139
1140 initrd_gen_udev() {
1141         [ -n "$verbose" ] && echo "Setting up udev..."
1142         mkdir -p $MNTIMAGE/sbin
1143         mkdir -p $MNTIMAGE/proc
1144         mkdir -p $MNTIMAGE/etc/udev
1145         mkdir -p $MNTIMAGE/sys
1146
1147         inst /sbin/initrd-udev $MNTIMAGE/sbin/udev
1148         ln -s udev $MNTIMAGE/sbin/udevstart
1149         inst /etc/udev/udev.conf $MNTIMAGE/etc/udev/udev.conf
1150
1151         ln -s udev $MNTIMAGE/sbin/hotplug
1152
1153         if is_yes "$USE_UDEV"; then
1154                 initrd_gen_tmpfs_dev >> "$s"
1155                 cat >> "$s" <<-EOF
1156 mount -t proc none /proc
1157 mount -t sysfs none /sys
1158 : 'Starting udev'
1159 /sbin/udevstart
1160 echo -n /sbin/hotplug > /proc/sys/kernel/hotplug
1161 umount /proc
1162 umount /sys
1163 EOF
1164         fi
1165 }
1166
1167 initrd_gen_dmraid() {
1168         if [ ! -x /sbin/dmraid-initrd ] ; then
1169                 echo "/sbin/dmraid-initrd is missing missing !"
1170                 exit 1
1171         fi
1172
1173         # AFAIR it just needs tmpfs on /dev.
1174         if [ ! -x $MNTIMAGE/sbin/udev ]; then
1175                 echo "udev is needed on target initrd for dmraid to work!"
1176                 exit 1
1177         fi
1178
1179         mkdir -p "$MNTIMAGE/sbin"
1180         inst /sbin/dmraid-initrd $MNTIMAGE/sbin/dmraid
1181         cat <<-EOF >> "$s"
1182         mount -t proc none /proc
1183         mount -t sysfs none /sys
1184         # 2 secs was enough for my system to initialize. but really this is udev issue?
1185         usleep 2000000
1186         : 'Activating Device-Mapper RAID(s)'
1187         /sbin/dmraid -ay -i
1188         umount /sys
1189         umount /proc
1190 EOF
1191 }
1192
1193
1194 initrd_gen_softraid() {
1195         [ -n "$verbose" ] && echo "Setting up mdadm..."
1196
1197         if [ ! -x /sbin/mdadm -o ! -x /sbin/initrd-mdassemble ] ; then
1198                 echo "/sbin/mdadm or /sbin/initrd-mdassemble is missing !"
1199                 exit 1
1200         fi
1201
1202         inst /sbin/initrd-mdassemble "$MNTIMAGE/bin/mdassemble"
1203
1204         # LVM on RAID case
1205         dev_list_extra=$(awk '/^DEVICE / { for (i=2; i<=NF; i++) { printf "%s ", $i; }; } ' /etc/mdadm.conf)
1206         for ex_dev in $dev_list_extra; do
1207                 echo "DEVICE $ex_dev" >> "$MNTIMAGE/etc/mdadm.conf"
1208         done
1209         do_md0=1
1210         for nr in `seq 1 $rootdev_nr`; do
1211                 eval cr_rootdev="\$rootdev${nr}"
1212                 eval cr_dev_list="\$dev_list${nr}"
1213                 [ -n "$verbose" ] && echo "Setting up array ($cr_rootdev = $cr_dev_list)"
1214
1215                 [ "$cr_rootdev" = "/dev/md0" ] && do_md0=0
1216
1217                 echo "DEVICE $cr_dev_list" >> "$MNTIMAGE/etc/mdadm.conf"
1218                 cr_dev_list_md="$(echo "$cr_dev_list" | xargs | awk ' { gsub(/ +/,",",$0); print $0; }')"
1219                 cr_md_conf=$(/sbin/mdadm --detail --brief --config=/etc/mdadm.conf $cr_rootdev)
1220                 if [ -n "$cr_md_conf" ]; then
1221                         echo "$cr_md_conf" >> "$MNTIMAGE/etc/mdadm.conf"
1222                 else
1223                         echo "ARRAY $cr_rootdev devices=$cr_dev_list_md" >> "$MNTIMAGE/etc/mdadm.conf"
1224                 fi
1225
1226                 for f in $cr_dev_list $cr_rootdev $dev_list_extra; do
1227                         # mkdir in case of devfs name
1228                         mkdir -p $MNTIMAGE/`dirname $f`
1229                         [ -e "$MNTIMAGE/$f" ] && continue
1230                         [ -n "$verbose" ] && echo "copying $f"
1231                         # this works fine with and without devfs
1232                         cp -HR $f $MNTIMAGE/$f
1233                 done
1234         done
1235
1236         echo "mdassemble" >> "$s"
1237
1238         # needed to determine md-version
1239         if [ "$do_md0" -eq 1 ] ; then
1240                 mknod $MNTIMAGE/dev/md0 b 9 0
1241         fi
1242 }
1243
1244 initrd_gen_nfs() {
1245         # use root=/dev/ram0 init=/linuxrc when starting kernel or you will
1246         # have problems like init(XX) being child process of swapper(1).
1247         [ -n "$verbose" ] && echo "Adding rootfs on NFS support to initrd (dhcp)"
1248         mknod "$MNTIMAGE/dev/urandom" c 1 8
1249         mkdir "$MNTIMAGE/newroot"
1250         mkdir "$MNTIMAGE/proc"
1251         echo "ifconfig lo 127.0.0.1 up" >> "$s"
1252         echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" >> "$s"
1253         echo "ifconfig eth0 0.0.0.0 up" >> "$s"
1254         echo "udhcpc -i eth0 -f -q -s /bin/setdhcp" >> "$s"
1255         cat << 'EOF' > "$MNTIMAGE/bin/setdhcp"
1256 #!/bin/sh
1257 [ "$1" != "bound" ] && exit
1258 [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
1259 [ -n "$subnet" ] && NETMASK="netmask $subnet"
1260 set -x
1261 ifconfig $interface $ip $BROADCAST $NETMASK up
1262 set +x
1263 if [ -n "$router" ]; then
1264         for r in $router; do
1265                 set -x
1266                 route add default gw $r dev $interface
1267                 set +x
1268         done
1269 fi
1270
1271 mount -t proc none /proc
1272 for o in $(cat /proc/cmdline); do
1273         case $o in
1274         nfsroot=*)
1275                 rootpath=${o#nfsroot=}
1276                 ;;
1277         esac
1278 done
1279 umount /proc
1280
1281 if [ -n "$rootpath" ]; then
1282         set -x
1283         mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
1284         set +x
1285 else
1286         set +x
1287         echo "Missing rootpath in what DHCP server sent to us. Failing..."
1288         echo "All seen variables are listed below:"
1289         set
1290         set -x
1291 fi
1292 EOF
1293         chmod 755 "$MNTIMAGE/bin/setdhcp"
1294         echo "cd /newroot" >> "$s"
1295         echo "pivot_root . initrd" >> "$s"
1296         echo "[ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1297         echo "exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 2>&1" >> "$s"
1298 }
1299
1300 initrd_gen_lvm() {
1301         [ -n "$verbose" ] && echo "Adding LVM support to initrd"
1302         inst /sbin/initrd-lvm $MNTIMAGE/bin/lvm
1303         mkdir -p $MNTIMAGE/etc
1304         mkdir -p $MNTIMAGE/tmp
1305         mkdir -p $MNTIMAGE/proc
1306         mkdir -p $MNTIMAGE/newroot
1307
1308         # always make /dev on tmpfs for LVM2
1309         if [ "$LVMTOOLSVERSION" = "2" ] && ! is_yes "$tmpfs_dev"; then
1310                 initrd_gen_tmpfs_dev >> "$s"
1311         fi
1312
1313         if ! is_yes "$tmpfs_dev"; then
1314                 mkdir -p $MNTIMAGE/dev/mapper
1315                 mknod $MNTIMAGE/dev/mapper/control c 10 63
1316                 for device in $PVDEVICES; do
1317                         # if LVM on RAID then device might be copied already in gen_softraid
1318                         [ -e "$MNTIMAGE/dev/$(basename $device)" ] && continue
1319                         cp -HR $device $MNTIMAGE/dev/
1320                 done
1321         fi
1322         echo "mount -t proc none /proc" >> "$s"
1323         echo "mount -t tmpfs none /tmp" >> "$s"
1324         if [ "$LVMTOOLSVERSION" = "1" ] ; then
1325                 echo "lvm vgscan -T" >> "$s"
1326                 echo "lvm vgchange -T -a y $VGVOLUME" >> "$s"
1327                 echo "umount /tmp" >> "$s"
1328                 # fail to umount
1329                 echo "umount /dev" >> "$s"
1330                 echo "umount /proc" >> "$s"
1331         else
1332                 echo "cat /etc/lvm.conf > /tmp/lvm.conf" >> "$s"
1333                 echo "global {" > "$MNTIMAGE/etc/lvm.conf"
1334                 echo "  locking_type = 0" >> "$MNTIMAGE/etc/lvm.conf"
1335                 echo "  locking_dir = \"/tmp\"" >> "$MNTIMAGE/etc/lvm.conf"
1336                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1337                 echo "devices {" >> "$MNTIMAGE/etc/lvm.conf"
1338                 echo "  sysfs_scan=0" >> "$MNTIMAGE/etc/lvm.conf"
1339                 if is_yes "$raidfound"; then
1340                         echo "  md_component_detection = 1" >> "$MNTIMAGE/etc/lvm.conf"
1341                 fi
1342                 lvm dumpconfig | awk '/filter=/' >> "$MNTIMAGE/etc/lvm.conf"
1343                 echo "}" >> "$MNTIMAGE/etc/lvm.conf"
1344
1345                 cat <<-EOF >> "$s"
1346                 : 'Making device nodes'
1347                 set +x
1348                 (
1349                 # ignore first two lines, header and empty line
1350                 read a
1351                 read a
1352                 while read major minor blocks dev; do
1353                         mknod /dev/\$dev b \$major \$minor
1354                 done
1355                 ) < /proc/partitions
1356
1357                 # disable noise from lvm accessing devices that aren't ready.
1358                 printk=\$(cat /proc/sys/kernel/printk)
1359                 echo 0 > /proc/sys/kernel/printk
1360                 set -x
1361
1362                 : 'Scanning for Volume Groups'
1363                 LVM_SYSTEM_DIR=/tmp lvm vgscan --ignorelockingfailure 2>/dev/null
1364
1365                 : 'Activating Volume Groups'
1366                 LVM_SYSTEM_DIR=/tmp lvm vgchange --ignorelockingfailure -a y $VGVOLUME 2>/dev/null
1367
1368                 echo "\$printk" > /proc/sys/kernel/printk
1369
1370                 # Find out major/minor
1371                 majmin="\$(LVM_SYSTEM_DIR=/tmp lvm lvdisplay --ignorelockingfailure -c $org_rootdev 2>/dev/null)"
1372                 majmin="\${majmin#*/}"
1373                 majmin="\${majmin#*:*:*:*:*:*:*:*:*:*:*:*}"
1374                 major="\${majmin%:*}"
1375                 minor="\${majmin#*:}"
1376                 # Pass it to kernel
1377                 val=\$((256 * \$major + \$minor))
1378                 echo \$val > /proc/sys/kernel/real-root-dev
1379                 umount /tmp
1380                 umount /proc
1381 EOF
1382         fi
1383 }
1384
1385 initrd_gen_procdata() {
1386         [ -n "$verbose" ] && echo "Adding rootfs finding based on root= option support."
1387         mkdir -p $MNTIMAGE/proc
1388 cat << 'EOF' >> "$s"
1389 set +x
1390 mount -t proc none /proc
1391 root="$(busybox awk ' /root=\/dev\// { gsub(/.*root=\/dev\//,NIL,$0); gsub(/ .*/,NIL,$0); print $0; } ' /proc/cmdline)"
1392 if [ -n "$root" ]; then
1393         rootnr="$(busybox awk -v root="$root" ' { if ($4 == root) { print 256*$1+$2; } } ' /proc/partitions)"
1394         if [ -n "$rootnr" ]; then
1395                 echo "$rootnr" > /proc/sys/kernel/real-root-dev
1396         fi
1397 fi
1398 umount /proc
1399 set -x
1400 EOF
1401 }
1402
1403 # main generation
1404
1405 if is_yes "$USE_UDEV"; then
1406         initrd_gen_udev
1407 fi
1408
1409 if is_yes "$USE_SUSPEND"; then
1410         initrd_gen_suspend
1411 fi
1412
1413 if is_yes "$USE_SUSPEND2"; then
1414         initrd_gen_suspend2
1415 fi
1416
1417 if is_yes "$USE_DMRAID"; then
1418         initrd_gen_dmraid
1419 fi
1420
1421 if is_yes "$usenfs" ; then
1422         initrd_gen_nfs
1423 elif is_yes "$USERAIDSTART" && is_yes "$raidfound" ; then
1424         initrd_gen_softraid
1425         if is_yes "$uselvm" ; then
1426                 initrd_gen_lvm
1427         else
1428                 initrd_gen_procdata
1429         fi
1430 elif is_yes "$uselvm" ; then
1431         initrd_gen_lvm
1432 else
1433         initrd_gen_procdata
1434 fi
1435
1436 if [ "$INITRDFS" = "initramfs" ]; then
1437         mkdir -p $MNTIMAGE/newroot
1438         cp -HR $org_rootdev $MNTIMAGE/dev
1439         echo "mount -t $rootFs $org_rootdev /newroot" >> "$s"
1440         echo "switch_root /newroot /sbin/init" >> "$s"
1441         # we need real file, not symlink
1442         rm -f $MNTIMAGE/init
1443         cp -a $MNTIMAGE/linuxrc $MNTIMAGE/init
1444 fi
1445
1446 chmod +x "$RCFILE"
1447
1448 (cd "$MNTIMAGE"; tar cf - .) | (cd "$MNTPOINT"; tar xf -)
1449
1450 case "$INITRDFS" in
1451         ext2)
1452                 IMAGESIZE=$(du -ks $MNTPOINT | awk '{print int(($1+1023)/1024)*1024}')
1453                 [ "$verbose" ] && echo "ext2 image size: $IMAGESIZE ($MNTPOINT)"
1454                 if [ "$IMAGESIZE" -gt 4096 ]; then
1455                         echo >&2 "$0: Your image size is larger than 4096, You should add ramdisk_size=$IMAGESIZE to your kernel commandline!"
1456                 fi
1457
1458                 dd if=/dev/zero of="$IMAGE" bs=1k count="$IMAGESIZE" 2> /dev/null
1459                 mke2fs -q -F -b 1024 -m 0 "$IMAGE" 2>/dev/null 1>&2
1460                 tune2fs -i 0 "$IMAGE" >/dev/null 2>&1
1461
1462                 tmpmnt="`mktemp -d /tmp/initrd.mnte2-XXXXXX`"
1463                 mount -o loop -t ext2 "$IMAGE" "$tmpmnt"
1464
1465                 (cd "$MNTPOINT"; tar cf - .) | (cd "$tmpmnt"; tar xf -)
1466
1467                 umount "$IMAGE"
1468                 ;;
1469         rom)
1470                 genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
1471                 ;;
1472         cram)
1473                 mkcramfs "$MNTPOINT" "$IMAGE"
1474                 ;;
1475         initramfs)
1476                 (cd $MNTPOINT ; find . | cpio -H newc -o > "$IMAGE")
1477                 ;;
1478         *)
1479                 echo "Filesystem $INITRDFS not supported by $0";
1480 esac
1481
1482 if is_yes "$COMPRESS" ; then
1483         gzip -9 < "$IMAGE" > "$target"
1484 else
1485         cp -a "$IMAGE" "$target"
1486 fi
1487
1488 if is_yes "$BOOT_SPLASH"; then
1489         if [ ! -x /bin/splash.bin ]; then
1490                 echo "Failed to execute /bin/splash.bin. Is bootsplash package installed?" 1>&2
1491         elif [ -z "$THEME" ]; then
1492                 echo "Please configure your /etc/sysconfig/bootsplash first." 1>&2
1493                 echo "Generating bootsplashes skipped." 1>&2
1494         else
1495                 if [ -n "$BOOT_SPLASH_RESOLUTIONS" ]; then
1496                         for res in $BOOT_SPLASH_RESOLUTIONS; do
1497                                 if [ -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" ]; then
1498                                         /bin/splash.bin -s -f "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg" >> "$target" && \
1499                                         [ -n "$verbose" ] && echo "Added $res $THEME theme to initrd."
1500                                 else
1501                                         echo "/etc/bootsplash/themes/$THEME/config/bootsplash-$res.cfg doesn't exist, skipped" 1>&2
1502                                 fi
1503                         done
1504                 else
1505                         echo "No BOOT_SPLASH_RESOLUTIONS specified in /etc/sysconfig/bootsplash." 1>&2
1506                         echo "Not adding bootsplash to initrd." 1>&2
1507                 fi
1508         fi
1509 fi
1510
1511 rm -rf "$MNTIMAGE" "$MNTPOINT" "$IMAGE"
1512
1513 # vim:ts=4:sw=4:noet:fdm=marker
This page took 1.403464 seconds and 3 git commands to generate.