Magellan Linux

Annotation of /trunk/mkinitrd-magellan/mkinitrd.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 894 - (hide annotations) (download)
Wed Aug 5 16:24:23 2009 UTC (15 years, 3 months ago) by niro
File size: 21540 byte(s)
-install all busybox applets
-use mdev for /dev management
-use inmod from busybox
-fixed a small typo regarding v86d installation
-set DOSATA and DOPATA as default settings
-support all do/no commandline combinations
1 niro 564 #!/bin/bash
2 niro 738 # $Header: /home/cvsd/magellan-cvs/magellan-src/mkinitrd-magellan/mkinitrd.in,v 1.4 2008-06-11 22:11:40 niro Exp $
3 niro 564
4     # mkinitrd
5     #
6     # Copyright 2005 Red Hat, Inc.
7     #
8     # Written by Erik Troan <ewt@redhat.com>
9     #
10     # Contributors:
11     # Elliot Lee <sopwith@cuc.edu>
12     # Miguel de Icaza <miguel@nuclecu.unam.mx>
13     # Christian 'Dr. Disk' Hechelmann <drdisk@ds9.au.s.shuttle.de>
14     # Michael K. Johnson <johnsonm@redhat.com>
15     # Pierre Habraken <Pierre.Habraken@ujf-grenoble.fr>
16     # Jakub Jelinek <jakub@redhat.com>
17     # Carlo Arenas Belon (carenas@chasqui.lared.net.pe>
18     # Keith Owens <kaos@ocs.com.au>
19     # Bernhard Rosenkraenzer <bero@redhat.com>
20     # Matt Wilson <msw@redhat.com>
21     # Trond Eivind Glomsrød <teg@redhat.com>
22     # Jeremy Katz <katzj@redhat.com>
23     # Preston Brown <pbrown@redhat.com>
24     # Bill Nottingham <notting@redhat.com>
25     # Guillaume Cottenceau <gc@mandrakesoft.com>
26     # Peter Jones <pjones@redhat.com>
27    
28     # heavily modified for magellan-linux by Niels Rogalla <niro@magellan-linux.de>
29    
30     umask 0022
31     export MALLOC_PERTURB_=204
32    
33     PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH
34     export PATH
35    
36     VERSION=%VERSIONTAG%
37    
38     PROBE="yes"
39     MODULES=""
40     PREMODS=""
41     NOSTRIP=false
42    
43     [ -e /etc/conf.d/mkinitrd ] && . /etc/conf.d/mkinitrd
44    
45     CONFMODS="${MODULES}"
46     MODULES=""
47    
48     compress=1
49     allowmissing=""
50     target=""
51     kernel=""
52     force=""
53     verbose=""
54     img_vers=""
55     builtins=""
56     modulefile=/etc/modules.conf
57     rc=0
58    
59     IMAGESIZE=4000
60     PRESCSIMODS="sd_mod"
61     fstab="/etc/fstab"
62    
63     vg_list=""
64    
65     die() { echo "Error: $@"; exit 1; }
66     add_initrd() { echo "$@" >> ${DEST}/init || die; }
67    
68     DEST="$(mktemp -d)"
69     IMAGE="$(mktemp)"
70    
71     usage()
72     {
73     echo "usage: $(basename $0) [--version] [--help] [-v] [-f] [--preload <module>]"
74     echo " [--force-ide-probe] [--force-scsi-probe | --omit-scsi-modules]"
75     echo " [--image-version] [--force-scsi-probe | --omit-raid-modules]"
76     echo " [--with=<module>] [--force-lvm-probe | --omit-lvm-modules]"
77     echo " [--builtin=<module>] [--initramfs | --loopback] [--no-strip]"
78     echo " [--fstab=<fstab>] [--nocompress] <initrd-image> <kernel-version>"
79     echo
80     echo " (ex: $(basename $0) /boot/initrd-2.2.5-15.img 2.2.5-15)"
81    
82     exit 0
83     }
84    
85     moduledep()
86     {
87     if [ ! -f "/lib/modules/${kernel}/modules.dep" ]
88     then
89     die "No dep file found for kernel ${kernel}"
90     fi
91    
92     echo "Looking for deps of module $1"
93     deps=$(awk 'BEGIN { searched=ARGV[2]; ARGV[2]=""; rc=1 } \
94     function modname(filename) { match(filename, /\/([^\/]+)\.k?o:?$/, ret); return ret[1] } \
95     function show() { if (orig == searched) { print dep; orig=""; rc=0; exit } } \
96 niro 845 /^.*kernel/ { show(); \
97 niro 564 orig=modname($1); dep=""; \
98     if ($2) { for (i = 2; i <= NF; i++) { dep=sprintf("%s %s", dep, modname($i)); } } } \
99     /^ / { dep=sprintf("%s %s", dep, modname($1)); } \
100     END { show(); exit(rc) }' /lib/modules/${kernel}/modules.dep $1)
101     [ -n "${deps}" ] && echo ":${deps}" || echo
102     }
103    
104     findone() { /usr/bin/find "$@" | /bin/awk '{ print $1; exit; }'; }
105     findall() { /usr/bin/find "$@"; }
106    
107     findmodule()
108     {
109     skiperrors=""
110    
111     if [[ $1 = --skiperrors ]]
112     then
113     skiperrors=--skiperrors
114     shift
115     fi
116    
117     local modName=$1
118    
119     if [ "${modName}" = "off" -o "${modName}" = "null" ]
120     then
121     return
122     fi
123    
124     if [ $(echo ${modName} | cut -b1) = "-" ]
125     then
126     skiperrors=--skiperrors
127     modName=$(echo ${modName} | cut -b2-)
128     fi
129    
130     case "${MODULES} " in
131     *"/${modName}.ko "*) return ;;
132     esac
133    
134     if echo ${builtins} | egrep -q '(^| )'${modName}'( |$)'
135     then
136     echo "module ${modName} assumed to be built in"
137     return
138     fi
139    
140     # special cases
141     if [[ ${modName} = i2o_block ]]
142     then
143     findmodule i2o_core
144     findmodule -i2o_pci
145     modName="i2o_block"
146     elif [[ ${modName} = ppa ]]
147     then
148     findmodule parport
149     findmodule parport_pc
150     modName="ppa"
151     elif [[ ${modName} = sbp2 ]]
152     then
153     findmodule ieee1394
154     findmodule ohci1394
155     modName="sbp2"
156     else
157     moduledep ${modName}
158     for i in ${deps}
159     do
160     findmodule ${i}
161     done
162     fi
163    
164     for modExt in o.gz o ko
165     do
166     for modDir in /lib/modules/${kernel}/updates /lib/modules/${kernel}
167     do
168     if [ -d ${modDir} ]
169     then
170     fmPath=$(findone ${modDir} -name ${modName}.${modExt})
171     if [ -f "${fmPath}" ]
172     then
173     break 2
174     fi
175     fi
176     done
177     done
178    
179     if [ ! -f ${fmPath} ]
180     then
181     if [ -n "${skiperrors}" ]
182     then
183     return
184     fi
185    
186     # ignore the absence of the scsi modules
187     for n in ${PRESCSIMODS}
188     do
189     if [[ ${n} = ${modName} ]]
190     then
191     return
192     fi
193     done
194    
195     if [ -n "${allowmissing}" ]
196     then
197     echo "WARNING: No module ${modName} found for kernel ${kernel}, continuing anyway"
198     return
199     fi
200    
201     die "No module ${modName} found for kernel ${kernel}, aborting."
202     fi
203    
204     # only need to add each module once
205     MODULES="${MODULES} ${fmPath}"
206     }
207    
208     inst()
209     {
210     if [ "$#" != "2" ]
211     then
212     echo "usage: inst <file> <destination>"
213     return
214     fi
215     echo "$1 -> $2"
216     cp $1 $2
217     }
218    
219     findstoragedriver()
220     {
221     for device in $@
222     do
223     case " ${handledevices} " in
224     *" ${device} "*) continue ;;
225     *) handleddevices="${handleddevices} ${device}" ;;
226     esac
227    
228     if [[ "${device}" =~ "md[0-9]+" ]]
229     then
230     echo "Found RAID component ${device}"
231     handleraid "${device}"
232     continue
233     fi
234     echo "Looking for driver for device ${device}"
235     sysfs=$(findone /sys/block -name ${device} -type d)
236     [ -z "${sysfs}" ] && return
237     pushd ${sysfs} >/dev/null 2>&1
238     while [ ! -L device ]
239     do
240     [[ ${PWD} = /sys ]] && continue 2
241     cd ..
242     done
243     cd $(readlink ./device)
244     while [ ! -f modalias ]
245     do
246     [[ ${PWD} = /sys/devices ]] && continue 2
247     cd ..
248     done
249     modalias=$(cat modalias)
250     for driver in $(modprobe --set-version ${kernel} --show-depends ${modalias} 2>/dev/null| awk '{ print gensub(".*/","","g",$2) }')
251     do
252     findmodule ${driver%%.ko}
253     done
254     popd >/dev/null 2>&1
255     done
256     }
257    
258     handleraid()
259     {
260     local start=0
261    
262     if [ -n "${noraid}" -o ! -f /proc/mdstat ]
263     then
264     return 0
265     fi
266    
267     levels=$(grep "^$1[ ]*:" /proc/mdstat | \
268     awk '{ print $4 }')
269     devs=$(grep "^$1[ ]*:" /proc/mdstat | \
270     awk '{ print gensub("\\[[0-9]*\\]","","g",gensub("^md.*raid[0-9]*","","1")) }')
271    
272     for level in ${levels}
273     do
274     case ${level} in
275     linear)
276     findmodule linear
277     start=1
278     ;;
279     multipath)
280     findmodule multipath
281     start=1
282     ;;
283     raid[01456])
284     findmodule ${level}
285     start=1
286     ;;
287     *)
288     error "raid level ${level} (in /proc/mdstat) not recognized"
289     ;;
290     esac
291     done
292     findstoragedriver ${devs}
293     if [[ ${start} = 1 ]]
294     then
295     raiddevices="${raiddevices} $1"
296     fi
297     return ${start}
298     }
299    
300     handlelvordev()
301     {
302     local vg=$(lvs --noheadings -o vg_name $1 2>/dev/null)
303     if [ -n "${vg}" ]
304     then
305     vg=$(echo ${vg}) # strip whitespace
306     case " ${vg_list} " in
307     *" ${vg} "*)
308     ;;
309     *)
310     vg_list="${vg_list} ${vg}"
311     for device in $(vgdisplay -v ${vg} 2>/dev/null | sed -n 's/PV Name//p')
312     do
313     findstoragedriver ${device##/dev/}
314     done
315     ;;
316     esac
317     else
318     findstoragedriver ${1##/dev/}
319     fi
320     }
321    
322     # need r00t privs
323     [[ $(id -u) != 0 ]] && die "you must be r00t."
324    
325     while [ $# -gt 0 ]; do
326     case $1 in
327     --fstab*)
328     if echo $1 | grep -q '=' ; then
329     fstab=$(echo $1 | sed 's/^--fstab=//')
330     else
331     fstab=$2
332     shift
333     fi
334     ;;
335    
336     --with-usb) withusb=yes ;;
337    
338     --with*)
339     if echo $1 | grep -q '=' ; then
340     modname=$(echo $1 | sed 's/^--with=//')
341     else
342     modname=$2
343     shift
344     fi
345     basicmodules="${basicmodules} ${modname}"
346     ;;
347    
348     --builtin*)
349     if echo $1 | grep -q '=' ; then
350     modname=$(echo $1 | sed 's/^--builtin=//')
351     else
352     modname=$2
353     shift
354     fi
355     builtins="${builtins} ${modname}"
356     ;;
357    
358     --version)
359     echo "mkinitrd: version ${VERSION}"
360     exit 0
361     ;;
362    
363     -v) verbose=-v ;;
364     --nocompress) compress="" ;;
365     --ifneeded)
366     # legacy
367     ;;
368     -f) force=1 ;;
369    
370     --preload*)
371     if echo $1 | grep -q '=' ; then
372     modname=$(echo $1 | sed 's/^--preload=//')
373     else
374     modname=$2
375     shift
376     fi
377     PREMODS="${PREMODS} ${modname}"
378     ;;
379    
380     --force-scsi-probe) forcescsi=1 ;;
381    
382     --omit-scsi-modules)
383     PRESCSIMODS=""
384     noscsi=1
385     ;;
386     --force-raid-probe) forceraid=1 ;;
387     --omit-raid-modules) noraid=1 ;;
388     --force-lvm-probe) forcelvm=1 ;;
389     --omit-lvm-modules) nolvm=1 ;;
390     --force-ide-probe) forceide=1 ;;
391     --image-version) img_vers=yes ;;
392     --allow-missing) allowmissing=yes ;;
393     --noresume) noresume=1 ;;
394     --help) usage -n ;;
395     --initramfs) IMAGE_TYPE=initramfs ;;
396     --loopback) IMAGE_TYPE=loopback ;;
397     --no-strip) NOSTRIP=true ;;
398     *)
399     if [ -z "${target}" ]
400     then
401     target=$1
402     elif [ -z "${kernel}" ]
403     then
404     kernel=$1
405     else
406     usage
407     fi
408     ;;
409     esac
410    
411     shift
412     done
413    
414     # enforce initramfs method as default
415     [[ -z ${IMAGE_TYPE} ]] && IMAGE_TYPE=initramfs
416    
417     if [ -z "${target}" -o -z "${kernel}" ]
418     then
419     usage
420     fi
421    
422     if [ -n "${img_vers}" ]
423     then
424     target="${target}-${kernel}"
425     fi
426    
427     if [ -z "${force}" -a -f ${target} ]
428     then
429     die "${target} already exists."
430     fi
431    
432     if [ -n "${forcescsi}" -a -n "${noscsi}" ]
433     then
434     die "Can't both force scsi probe and omit scsi modules"
435     fi
436    
437     if [ -n "${forceraid}" -a -n "${noraid}" ]
438     then
439     die "Can't both force raid probe and omit raid modules"
440     fi
441    
442     if [ -n "${forcelvm}" -a -n "${nolvm}" ]
443     then
444     die "Can't both force LVM probe and omit LVM modules"
445     fi
446    
447     if [ ! -d /lib/modules/${kernel} ]
448     then
449     die "No modules available for kernel '${kernel}'."
450     fi
451    
452     ##############################
453     ####### get modules ##########
454    
455     echo "Creating ${IMAGE_TYPE} initrd image"
456     modulefile=/etc/modprobe.conf
457    
458     for n in ${PREMODS}
459     do
460     findmodule ${n}
461     done
462    
463     needusb=""
464     if [ -n "${withusb}" -a "x${PROBE}" == "xyes" ]
465     then
466     # If / or /boot is on a USB device include the driver. With root by
467     # label we could still get some odd behaviors
468     for fs in / /boot
469     do
470     esc=$(echo ${fs} | sed 's,/,\\/,g')
471     dev=$(mount | awk "/ on ${esc} / { print \$1 }" | sed 's/[0-9]*$//' | cut -d/ -f3)
472     if [ "$(echo ${dev} | cut -c1-2)" = sd ]
473     then
474     if [ $(which kudzu 2>/dev/null) ]
475     then
476     host=$(kudzu --probe -b scsi |
477     gawk '/^device: '${dev}'/,/^host:/ { if (/^host/) { print $2; exit; } }')
478     if [ -d /proc/scsi/usb-storage-${host} -o -f /proc/scsi/usb-storage/${host} ]
479     then
480     needusb=1
481     fi
482     fi
483     fi
484     done
485     fi
486    
487     if [ -n "${needusb}" -a "x${PROBE}" == "xyes" ]
488     then
489     drivers=$(awk '/^alias[[:space:]]+usb-controller[0-9]* / { print $3}' < ${modulefile})
490     if [ -n "${drivers}" ]
491     then
492     for driver in ${drivers}
493     do
494     findmodule ${driver}
495     done
496     findmodule scsi_mod
497     findmodule sd_mod
498     findmodule usb-storage
499     fi
500     fi
501    
502     if [ -n "${forcescsi}" -o -z "${noscsi}" -a "x${PROBE}" == "xyes" ]
503     then
504     if [ ! -f ${modulefile} ]
505     then
506     modulefile=/etc/conf.modules
507     fi
508    
509     if [ -f ${modulefile} ]
510     then
511     scsimodules=$(grep "alias[[:space:]]\+scsi_hostadapter" ${modulefile} | grep -v '^[ ]*#' | LC_ALL=C sort -u | awk '{ print $3 }')
512    
513     if [ -n "${scsimodules}" ]
514     then
515     for n in ${PRESCSIMODS}
516     do
517     findmodule ${n}
518     done
519    
520     for n in ${scsimodules}
521     do
522     # for now allow scsi modules to come from anywhere. There are some
523     # RAID controllers with drivers in block/
524     findmodule ${n}
525     done
526     fi
527     fi
528     fi
529    
530     # If we have ide devices and module ide, do the right thing
531     ide=/proc/ide/ide*
532     if [ -n "${forceide}" -o -n "${ide}" -a "x${PROBE}" == "xyes" ]
533     then
534     findmodule -ide-disk
535     fi
536    
537     # If we have dasd devices, include the necessary modules (S/390)
538     if [ "x${PROBE}" == "xyes" -a -d /proc/dasd ]
539     then
540     findmodule -dasd_mod
541     findmodule -dasd_eckd_mod
542     findmodule -dasd_fba_mod
543     fi
544    
545     if [ "x${PROBE}" == "xyes" ]
546     then
547     # check to see if we need to set up a loopback filesystem
548     rootdev=$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' ${fstab})
549     # check if it's root by label
550     if echo ${rootdev} | cut -c1-6 | grep -q "LABEL="
551     then
552     majmin=$(stat -c "%t:%T" /dev/root)
553     if [ -n "${majmin}" ]
554     then
555     dev=$(findall /sys/block -name dev | while read device ; do \
556     echo "${majmin}" | cmp -s ${device} && echo ${device} ; done \
557     | sed -e 's,.*/\([^/]\+\)/dev,\1,' )
558     if [ -n "${dev}" ]; then
559     echo "Found root device ${dev} for ${rootdev}"
560     rootdev=${dev}
561     fi
562     fi
563     fi
564     if echo ${rootdev} | cut -d/ -f3 | grep -q loop
565     then
566     key="^# $(echo ${rootdev} | cut -d/ -f3 | tr '[a-z]' '[A-Z]'):"
567     if ! grep "${key}" ${fstab} >> /dev/null
568     then
569     echo "The root filesystem is on a ${rootdev}, but there is no magic entry in ${fstab}"
570     echo "for this device. Consult the mkinitrd man page for more information"
571     echo "loopback root."
572     exit 1
573     fi
574    
575     line=$(grep "${key}" ${fstab})
576     loopDev=$(echo ${line} | awk '{print $3}')
577     loopFs=$(echo ${line} | awk '{print $4}')
578     loopFile=$(echo ${line} | awk '{print $5}')
579    
580     basicmodules="${basicmodules} -loop"
581     if [ "${loopFs}" = "vfat" -o "${loopFs}" = "msdos" ]
582     then
583     basicmodules="${basicmodules} -fat"
584     fi
585     basicmodules="${basicmodules} -${loopFs}"
586     # check if the root fs is on a logical volume
587     # this check must be last
588     else
589     handlelvordev ${rootdev}
590     fi
591    
592     rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' ${fstab})
593     rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' ${fstab}\
594     | sed -e 's/^r[ow],//' -e 's/,r[ow],$//' -e 's/,r[ow],/,/' \
595     -e 's/^r[ow]$/defaults/' -e 's/$/,ro/')
596    
597     # in case the root filesystem is modular
598     findmodule -${rootfs}
599    
600     # find the first swap dev which would get used for swsusp
601     swsuspdev=$(awk '/^[ \t]*[^#]/ { if ($3 == "swap") { print $1; }}' ${fstab} \
602     | head -n 1)
603     if ! echo ${swsuspdev} | cut -c1-6 | grep -q "LABEL="
604     then
605     handlelvordev ${swsupdev}
606     fi
607     fi
608    
609     # If we use LVM or dm-based raid, include dm-mod
610     # XXX: dm not really supported yet.
611     testdm=""
612     [ -n "${vg_list}" ] && testdm="yes"
613     [ -n "${forceraid}" -o -n "${forcelvm}" ] && testdm="yes"
614     [ "x${PROBE}" == "xyes" -a \
615     -n "$(test -z \"${nolvm}\" -o -z \"${noraid}\")" ] && testdm="yes"
616    
617     if [ -n "${testdm}" ]
618     then
619     if [ -x /sbin/dmsetup -a -e /dev/mapper/control ]
620     then
621     dmout=$(/sbin/dmsetup ls 2>/dev/null)
622     if [ "${dmout}" != "No devices found" -a "${dmout}" != "" ]
623     then
624     findmodule -dm-mod
625    
626     # DM requires all of these to be there in case someone used the
627     # feature. broken. (#132001)
628     findmodule -dm-mirror
629     findmodule -dm-zero
630     findmodule -dm-snapshot
631     fi
632     fi
633     fi
634    
635     for n in ${basicmodules}
636     do
637     findmodule ${n}
638     done
639    
640     for n in ${CONFMODS}
641     do
642     findmodule ${n}
643     done
644    
645     echo "Using modules:${MODULES}"
646    
647     ####### eof modules ##########
648     ##############################
649    
650    
651     # create basic filesystem layout
652 niro 894 install -d ${DEST}/{bin,dev,etc,lib,loopfs,proc,sbin,sys,sysroot,usr/bin,usr/sbin} || die
653 niro 564
654     # install busybox
655     echo "$(which busybox.mkinitrd)"
656     install -m0755 $(which busybox.mkinitrd) ${DEST}/bin/busybox || die
657     for i in '[' ash basename cat cut clear cp dirname echo env false find \
658     grep gunzip gzip ln ls loadkmap mkdir mknod more mount mv \
659     ps pwd rm rmdir sh sleep test touch true umount uname \
660     xargs yes chmod chown
661     do
662     # use soft links from now on;
663     # hardlinks in initramfs apears to be broken on newer kernels
664     # -> "request module: runaway loop modprobe binfmt-0000"
665     # see: http://www.gossamer-threads.com/lists/linux/kernel/728042
666     ln -s /bin/busybox ${DEST}/bin/${i} || die "${i}"
667     done
668    
669     # sbin related
670     for i in chroot init losetup modprobe pivot_root
671     do
672 niro 894 ln -s ${DEST}/bin/busybox ${DEST}/sbin/${i} || die "${i}"
673 niro 564 done
674    
675     # install run-init
676     echo "$(which run-init.mkinitrd)"
677     install -m0755 $(which run-init.mkinitrd) ${DEST}/sbin/run-init || die
678    
679     # install insmod.static
680 niro 894 #echo "$(which insmod.static)"
681     #install -m0755 $(which insmod.static) ${DEST}/sbin/insmod || die
682 niro 564
683 niro 703 # install v86d for uvesafb support if installed
684     if which v86d &> /dev/null
685     then
686 niro 894 echo "$(which v86d)"
687 niro 703 install -m0755 $(which v86d) ${DEST}/sbin/v86d || die
688     fi
689    
690 niro 564 # install lvm
691     if [ -n "${vg_list}" ]
692     then
693     inst /sbin/lvm.static "${DEST}/sbin/lvm"
694     if [ -f /etc/lvm/lvm.conf ]
695     then
696     cp ${verbose} --parents /etc/lvm/lvm.conf ${DEST}/
697     fi
698     fi
699    
700     # create some needed device nodes
701     mkdir ${DEST}/dev/pts || die
702     mkdir ${DEST}/dev/shm || die
703     mkdir ${DEST}/dev/mapper || die
704     mkdir ${DEST}/dev/misc || die
705     mkdir ${DEST}/dev/fb || die
706     mkdir ${DEST}/dev/vc || die
707     mknod ${DEST}/dev/console c 5 1 || die
708     mknod ${DEST}/dev/null c 1 3 || die
709     mknod ${DEST}/dev/ram b 1 1 || die
710     mknod ${DEST}/dev/rtc c 10 135 || die
711     mknod ${DEST}/dev/systty c 4 0 || die
712     mknod ${DEST}/dev/tty c 5 0 || die
713     mknod ${DEST}/dev/zero c 1 5 || die
714     for ((i=0; i < 13; i++))
715     do
716     mknod ${DEST}/dev/tty${i} c 4 ${i} || die "tty${i}"
717     done
718     for ((i=64; i < 68; i++))
719     do
720     mknod ${DEST}/dev/ttyS$((${i} - 64)) c 4 ${i} || die "ttyS$((${i} - 64))"
721     done
722    
723     # create init script
724     :> ${DEST}/init || die
725     add_initrd '#!/bin/sh'
726    
727     add_initrd 'mount -t proc proc /proc'
728     add_initrd 'mount -t sysfs sysfs /sys'
729 niro 894 add_initrd 'mount -t tmpfs tmpfs /dev -o rw,mode=0644,size=1024'
730 niro 564
731     [[ ${IMAGE_TYPE} = loopback ]] && add_initrd 'mount -t ramfs -o remount,rw /dev/ram /'
732    
733 niro 894 # install all busybox applets
734     add_initrd 'busybox --install -s'
735    
736 niro 564 # silencio
737     add_initrd 'echo 0 > /proc/sys/kernel/printk'
738    
739 niro 737 add_initrd 'echo "-- Creating initial device nodes ..."'
740 niro 894 # populate dev
741     add_initrd 'mdev -s'
742     # handle hotplug events properly
743     add_initrd 'echo /sbin/mdev > /proc/sys/kernel/hotplug'
744 niro 564
745     # loading detected modules with options
746     for MODULE in ${MODULES}
747     do
748     text=""
749     module=$(echo ${MODULE} | sed "s|.*/||" | sed "s/.k\?o$//")
750     fullmodule=$(echo ${MODULE} | sed "s|.*/||")
751    
752     options=$(sed -n -e "s/^options[ ][ ]*${module}[ ][ ]*//p" ${modulefile} 2>/dev/null)
753    
754     if [ -n "${options}" ]
755     then
756     echo "Adding module ${module}${text} with options ${options} ..."
757     else
758     echo "Adding module ${module}${text} ..."
759     fi
760    
761     add_initrd "echo \"-- Loading ${fullmodule} module\""
762     add_initrd "insmod /lib/${fullmodule} ${options}"
763    
764     # Hack - we need a delay after loading usb-storage to give things
765     # time to settle down before we start looking a block devices
766     if [ "${module}" = "usb-storage" ]
767     then
768     add_initrd 'echo Waiting 8 seconds for driver initialization.'
769     add_initrd 'sleep 8'
770     fi
771     if [ "${module}" = "zfcp" -a -f /etc/zfcp.conf ]
772     then
773     add_initrd 'echo Waiting 2 seconds for driver initialization.'
774     add_initrd 'sleep 2'
775     cat /etc/zfcp.conf | grep -v "^#" | tr "A-Z" "a-z" | while read DEVICE SCSIID WWPN SCSILUN FCPLUN
776     do
777     add_initrd "echo -n ${WWPN} > /sys/bus/ccw/drivers/zfcp/${DEVICE/0x/}/port_add"
778     add_initrd "echo -n ${FCPLUN} > /sys/bus/ccw/drivers/zfcp/${DEVICE/0x/}/${WWPN}/unit_add"
779     add_initrd "echo -n 1 > /sys/bus/ccw/drivers/zfcp/${DEVICE/0x/}/online"
780     done
781     fi
782     done
783    
784     # lvm support
785     if [ -n "${vg_list}" ]
786     then
787     add_initrd 'echo Scanning logical volumes'
788     add_initrd 'lvm vgscan --ignorelockingfailure'
789     add_initrd 'echo Activating logical volumes'
790     add_initrd "lvm vgchange -ay --ignorelockingfailure ${vg_list}"
791     fi
792    
793     # loopback support
794     if [ -n "${loopDev}" ]
795     then
796     # needed on the host system
797     install -d /initrd
798     cp -a ${loopDev} ${DEST}/dev || die
799     cp -a ${rootdev} ${DEST}/dev || die
800     add_initrd 'echo "Mounting device containing loopback root filesystem ..."'
801     add_initrd "mount -t ${loopFs} ${loopDev} /loopfs"
802     add_initrd "echo Setting up loopback device ${rootdev}"
803     add_initrd "losetup ${rootdev} /loopfs${loopFile}"
804     fi
805    
806     # software suspend
807     if [ -z "${noresume}" ]
808     then
809     add_initrd "[ -x /bin/resume ] && resume ${swsuspdev}"
810     fi
811    
812     # raid
813     if [ -n "${raiddevices}" ]
814     then
815     for dev in ${raiddevices}
816     do
817     cp -a /dev/${dev} ${DEST}/dev || die
818     add_initrd "raidautorun /dev/${dev}"
819     done
820     fi
821    
822     add_initrd 'echo "-- Mounting sysroot ..."'
823     add_initrd '. /etc/rootdev'
824     add_initrd '[ ! -e ${device} ] && mknod ${device} b ${major} ${minor}'
825     add_initrd 'echo "${device} /sysroot ${rootfs} defaults,ro 0 0" > /etc/fstab'
826 niro 738 add_initrd 'mount /sysroot || ash'
827 niro 564
828 niro 894 # unset hotplug event-manager
829     add_initrd 'echo > /proc/sys/kernel/hotplug'
830    
831 niro 564 # de-silencio
832     add_initrd 'echo 3 > /proc/sys/kernel/printk'
833    
834     add_initrd 'echo "-- Switching to real sysroot ..."'
835 niro 894 add_initrd 'umount /dev'
836 niro 564 add_initrd 'umount /sys'
837     add_initrd 'umount /proc'
838    
839     [[ ${IMAGE_TYPE} = initramfs ]] && add_initrd 'exec run-init /sysroot /sbin/init $@ </sysroot/dev/console >/sysroot/dev/console'
840     if [[ ${IMAGE_TYPE} = loopback ]]
841     then
842     add_initrd 'cd /sysroot'
843     add_initrd 'pivot_root . initrd'
844     add_initrd "exec /sbin/chroot . sh -c 'umount -n /initrd >/dev/null 2>&1;exec -a init.new /sbin/init'</dev/console >/dev/console 2>&1"
845     fi
846    
847     chmod +x ${DEST}/init || die
848    
849     # setup root block device
850     [[ -z ${rootdev} ]] && die "no known rootdev found."
851     echo "device=${rootdev}" > ${DEST}/etc/rootdev || die
852     echo "major=$(stat -c %t ${rootdev})" >> ${DEST}/etc/rootdev || die
853     echo "minor=$(stat -c %T ${rootdev})" >> ${DEST}/etc/rootdev || die
854     echo "rootfs=${rootfs}" >> ${DEST}/etc/rootdev || die
855    
856     # install detected modules
857     echo "Coping your modules to '${DEST}/lib' ..."
858     for MODULE in ${MODULES}
859     do
860     if [ -x /usr/bin/strip ] && [[ ${NOSTRIP} = false ]]
861     then
862     /usr/bin/strip -g ${verbose} ${MODULE} -o ${DEST}/lib/$(basename ${MODULE}) || die
863     else
864     cp ${verbose} -a ${MODULE} ${DEST}/lib || die
865     fi
866     done
867    
868     # create the image
869     echo "Creating initrd image '${target}' ..."
870     if [[ ${IMAGE_TYPE} = initramfs ]]
871     then
872     (cd "${DEST}" ; find . | cpio --quiet -o -H newc | gzip -9 >${IMAGE})
873     cat ${IMAGE} > ${target} || die
874     fi
875    
876     if [[ ${IMAGE_TYPE} = loopback ]]
877     then
878     MOUNTTMP="$(mktemp -d)"
879    
880     # create ext2 img
881     dd if=/dev/zero of=${IMAGE} bs=1024 count=${IMAGESIZE} &> /dev/null || die
882     mke2fs -m 0 -i 1024 -F ${IMAGE} &> /dev/null || die
883     mount -o loop ${IMAGE} ${MOUNTTMP} || die
884     mv ${DEST}/* ${MOUNTTMP} || die
885     umount ${MOUNTTMP} || die
886     gzip -9 ${IMAGE} || die
887     cat ${IMAGE}.gz > ${target} || die
888     [ -d ${MOUNTTMP} ] && rm -rf ${MOUNTTMP}
889    
890     # create inird dir for pivot_root
891     [ ! -d /initrd ] && { install -d /initrd || die; }
892     fi
893    
894     # clean up
895     [ -d ${DEST} ] && rm -rf ${DEST}
896     [ -f ${IMAGE} ] && rm -f ${IMAGE}
897    
898     # all went ok at this point
899     exit 0