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