5 # Written by Erik Troan <ewt@redhat.com>
8 # Elliot Lee <sopwith@cuc.edu>
9 # Miguel de Icaza <miguel@nuclecu.unam.mx>
10 # Christian 'Dr. Disk' Hechelmann <drdisk@ds9.au.s.shuttle.de>
11 # Michael K. Johnson <johnsonm@redhat.com>
12 # Pierre Habraken <Pierre.Habraken@ujf-grenoble.fr>
13 # Jakub Jelinek <jakub@redhat.com>
14 # Carlo Arenas Belon (carenas@chasqui.lared.net.pe>
15 # Keith Owens <kaos@ocs.com.au>
16 # Bernhard Rosenkraenzer <bero@redhat.com>
17 # Matt Wilson <msw@redhat.com>
18 # Trond Eivind Glomsrød <teg@redhat.com>
19 # Jeremy Katz <katzj@redhat.com>
20 # Preston Brown <pbrown@redhat.com>
21 # Bill Nottingham <notting@redhat.com>
22 # Guillaume Cottenceau <gc@mandrakesoft.com>
25 PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH
32 target="cdtree/boot/isolinux/initrd.img"
33 kernel=2.6.10-1.770_FC3
41 modulefile=/etc/modules.conf
45 PRESCSIMODS="scsi_mod sd_mod unknown"
49 if [ -f /etc/udev/udev.conf ]; then
54 [ -x /sbin/udev.static ] || USE_UDEV=
58 echo "usage: `basename $0` [--version] [-v] [-f] [--preload <module>]" >&2
59 echo " [--omit-scsi-modules] [--omit-raid-modules] [--omit-lvm-modules]" >&2
60 echo " [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]" >&2
61 echo " [--builtin=<module>] [--nopivot] <initrd-image> <kernel-version>" >&2
63 echo " (ex: `basename $0` /boot/initrd-2.2.5-15.img 2.2.5-15)" >&2
68 if [ ! -f "/lib/modules/$kernel/modules.dep" ]; then
69 echo "No dep file found for kernel $kernel" >&2
73 [ -n "$verbose" ] && echo -n "Looking for deps of module $1"
74 deps=$(awk 'BEGIN { searched=ARGV[2]; ARGV[2]=""; rc=1 } \
75 function modname(filename) { match(filename, /\/([^\/]+)\.k?o:?$/, ret); return ret[1] } \
76 function show() { if (orig == searched) { print dep; orig=""; rc=0; exit } } \
78 orig=modname($1); dep=""; \
79 if ($2) { for (i = 2; i <= NF; i++) { dep=sprintf("%s %s", dep, modname($i)); } } } \
80 /^ / { dep=sprintf("%s %s", dep, modname($1)); } \
81 END { show(); exit(rc) }' /lib/modules/$kernel/modules.dep $1)
82 [ -n "$verbose" ] && echo -e "\t$deps"
88 if [ $1 == "--skiperrors" ]; then
89 skiperrors=--skiperrors
95 if [ "$modName" = "off" -o "$modName" = "null" ]; then
99 if [ $(echo $modName | cut -b1) = "-" ]; then
100 skiperrors=--skiperrors
101 modName=$(echo $modName | cut -b2-)
104 if echo $builtins | egrep -q '(^| )'$modName'( |$)' ; then
105 [ -n "$verbose" ] && echo "module $modName assumed to be built in"
111 if [ "$modName" = "i2o_block" ]; then
115 elif [ "$modName" = "ppa" ]; then
117 findmodule parport_pc
119 elif [ "$modName" = "sbp2" ]; then
130 for modExt in o.gz o ko ; do
131 if [ -d /lib/modules/$kernel/updates ]; then
132 fmPath=`(cd /lib/modules/$kernel/updates; echo find . -name $modName.$modExt -type f | /sbin/nash --quiet) | /bin/awk {'print $1; exit;'}`
135 if [ -f /lib/modules/$kernel/updates/$fmPath ]; then
136 fmPath=updates/$fmPath
140 fmPath=`(cd /lib/modules/$kernel; echo find . -name $modName.$modExt -type f | /sbin/nash --quiet) | /bin/awk {'print $1; exit;'}`
141 if [ -f /lib/modules/$kernel/$fmPath ]; then
146 if [ ! -f /lib/modules/$kernel/$fmPath ]; then
147 if [ -n "$skiperrors" ]; then
151 # ignore the absence of the scsi modules
152 for n in $PRESCSIMODS; do
153 if [ "$n" = "$modName" ]; then
158 if [ -n "$allowmissing" ]; then
159 echo "WARNING: No module $modName found for kernel $kernel, continuing anyway" >&2
163 echo "No module $modName found for kernel $kernel, aborting." >&2
167 # only need to add each module once
168 if ! echo $MODULES | grep -q "$fmPath" 2>/dev/null ; then
169 MODULES="$MODULES $fmPath"
174 if [ "$#" != "2" ];then
175 echo "usage: inst <file> <destination>"
178 [ -n "$verbose" ] && echo "$1 -> $2"
182 while [ $# -gt 0 ]; do
185 if echo $1 | grep -q '=' ; then
186 fstab=`echo $1 | sed 's/^--fstab=//'`
198 if echo $1 | grep -q '=' ; then
199 modname=`echo $1 | sed 's/^--with=//'`
205 basicmodules="$basicmodules $modname"
209 if echo $1 | grep -q '=' ; then
210 modname=`echo $1 | sed 's/^--builtin=//'`
215 builtins="$builtins $modname"
219 echo "mkinitrd: version $VERSION"
243 if echo $1 | grep -q '=' ; then
244 modname=`echo $1 | sed 's/^--preload=//'`
249 PREMODS="$PREMODS $modname"
271 if [ -z "$target" ]; then
273 elif [ -z "$kernel" ]; then
284 if [ -z "$target" -o -z "$kernel" ]; then
288 if [ -n "$img_vers" ]; then
289 target="$target-$kernel"
292 if [ -z "$force" -a -f $target ]; then
293 echo "$target already exists." >&2
297 if [ ! -d /lib/modules/$kernel ]; then
298 echo "/lib/modules/$kernel is not a directory." >&2
302 if [ $UID != 0 ]; then
303 echo "mkinitrd must be run as root"
307 kernelmajor=`echo $kernel | cut -d . -f 1,2`
309 if [ "$kernelmajor" == "2.4" ]; then
310 if [ -n "$verbose" ]; then echo "Creating old-style initrd"; fi
313 if [ -n "$verbose" ]; then echo "Creating initramfs"; fi
314 modulefile=/etc/modprobe.conf
319 # if we're not using udev, don't set any of the other bits
320 [ -z "$USE_UDEV" ] && UDEV_TMPFS= && UDEV_KEEP_DEV=
322 # find a temporary directory which doesn't use tmpfs
324 for t in /tmp /var/tmp /root ${PWD}; do
325 if [ ! -d $t ]; then continue; fi
326 if ! echo access -w $t | /sbin/nash --quiet; then continue; fi
328 fs=$(df -T $t 2>/dev/null | awk '{line=$1;} END {printf $2;}')
329 if [ "$fs" != "tmpfs" ]; then
335 if [ -z "$TMPDIR" ]; then
336 echo "no temporary directory could be found" >&2
340 if [ $TMPDIR = "/root" -o $TMPDIR = "${PWD}" ]; then
341 echo "WARNING: using $TMPDIR for temporary files" >&2
344 for n in $PREMODS; do
349 if [ -n "$withusb" ]; then
350 # If / or /boot is on a USB device include the driver. With root by
351 # label we could still get some odd behaviors
352 for fs in / /boot ; do
353 esc=$(echo $fs | sed 's,/,\\/,g')
354 dev=$(mount | awk "/ on ${esc} / { print \$1 }" | sed 's/[0-9]*$//' | cut -d/ -f3)
355 if [ "$(echo $dev | cut -c1-2)" = sd ]; then
356 if [ `which kudzu 2>/dev/null` ]; then
357 host=$(kudzu --probe -b scsi |
358 gawk '/^device: '${dev}'/,/^host:/ { if (/^host/) { print $2; exit; } }')
359 if [ -d /proc/scsi/usb-storage-${host} -o -f /proc/scsi/usb-storage/${host} ]; then
367 if [ -n "$needusb" ]; then
368 drivers=$(awk '/^alias[[:space:]]+usb-controller[0-9]* / { print $3}' < $modulefile)
369 if [ -n "$drivers" ]; then
370 for driver in $drivers; do
375 findmodule usb-storage
379 if [ -z "$noscsi" ]; then
380 if [ ! -f $modulefile ]; then
381 modulefile=/etc/conf.modules
384 if [ -f $modulefile ]; then
385 scsimodules=`grep "alias[[:space:]]\+scsi_hostadapter" $modulefile | grep -v '^[ ]*#' | LC_ALL=C sort -u | awk '{ print $3 }'`
387 if [ -n "$scsimodules" ]; then
388 for n in $PRESCSIMODS; do
392 for n in $scsimodules; do
393 # for now allow scsi modules to come from anywhere. There are some
394 # RAID controllers with drivers in block/
401 # If we have ide devices and module ide, do the right thing
403 if [ -n "$ide" ]; then
407 # If we use LVM, include lvm-mod
408 if [ -z "$nolvm" ]; then
409 if [ -f /proc/lvm/global ]; then
410 if grep -q '^VG:' /proc/lvm/global ; then
411 if [ "$kernelmajor" == "2.4" ]; then
419 if [ -x /sbin/dmsetup -a -e /dev/mapper/control ]; then
420 dmout=$(/sbin/dmsetup ls 2>/dev/null)
421 if [ "$dmout" != "No devices found" -a "$dmout" != "" ]; then
427 # If we have dasd devices, include the necessary modules (S/390)
428 if [ -d /proc/dasd ]; then
430 findmodule -dasd_eckd_mod
431 findmodule -dasd_fba_mod
434 if [ -z "$noraid" -a -f /proc/mdstat ]; then
435 # load appropriate raid devices if necessary -- we'll load whatever
436 # /proc/mdstat suggests
438 # note that the awk below contains a space and a tab
439 for level in $(awk '/^md[0-9][0-9]*[ ]*:/ { print $4 }' \
440 /proc/mdstat | sort -u); do
455 echo "raid level $level (in /proc/mdstat) not recognized" >&2
460 if [ -n "$startraid" ]; then
461 raiddevices=$(awk '/^md[0-9][0-9]*[ ]*:/ { print $1 }' \
466 # check to see if we need to set up a loopback filesystem
467 rootdev=$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' $fstab)
468 if echo $rootdev | cut -d/ -f3 | grep -q loop ; then
469 key="^# $(echo $rootdev | cut -d/ -f3 | tr '[a-z]' '[A-Z]'):"
470 if ! grep "$key" $fstab > /dev/null; then
471 echo "The root filesystem is on a $rootdev, but there is no magic entry in $fstab" 1>&2
472 echo "for this device. Consult the mkinitrd man page for more information" 2>&2
476 line=$(grep "$key" $fstab)
477 loopDev=$(echo $line | awk '{print $3}')
478 loopFs=$(echo $line | awk '{print $4}')
479 loopFile=$(echo $line | awk '{print $5}')
481 basicmodules="$basicmodules -loop"
482 if [ "$loopFs" = "vfat" -o "$loopFs" = "msdos" ]; then
483 basicmodules="$basicmodules -fat"
485 basicmodules="$basicmodules -${loopFs}"
486 # check if the root fs is on a logical volume
487 elif ! echo $rootdev | cut -c1-6 |grep -q "LABEL=" ; then
488 rootdev=$(echo "readlink $rootdev" | /sbin/nash --quiet)
489 major=`ls -l $rootdev | sed -e "s/.* \\([0-9]\+\\), *[0-9]\+.*/\\1/"`
490 [ "$major" != "58" ] || root_lvm=1
491 if echo $rootdev |grep -q /dev/mapper 2>/dev/null ; then root_lvm=1 ; fi
494 rootfs=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' $fstab)
495 rootopts=$(awk '{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' $fstab)
497 # in case the root filesystem is modular
498 findmodule -${rootfs}
500 if [ -n "$root_lvm" ]; then
501 if [ "$kernelmajor" == "2.4" ]; then
505 # DM requires all of these to be there in case someone used the
506 # feature. broken. (#132001)
507 findmodule -dm-mirror
509 findmodule -dm-snapshot
513 for n in $basicmodules; do
517 if [ -n "$verbose" ]; then
518 echo "Using modules: $MODULES"
524 RCFILE=$MNTIMAGE/init
526 if [ -z "$MNTIMAGE" -o -z "$IMAGE" ]; then
527 echo "Error creating temporaries. Try again" >&2
532 mkdir -p $MNTIMAGE/lib
533 mkdir -p $MNTIMAGE/bin
534 mkdir -p $MNTIMAGE/etc
535 mkdir -p $MNTIMAGE/dev
536 mkdir -p $MNTIMAGE/loopfs
537 mkdir -p $MNTIMAGE/proc
538 mkdir -p $MNTIMAGE/sys
539 mkdir -p $MNTIMAGE/sysroot
540 ln -s bin $MNTIMAGE/sbin
542 inst /sbin/nash "$MNTIMAGE/bin/nash"
543 inst /sbin/insmod.static "$MNTIMAGE/bin/insmod"
544 ln -s /sbin/nash $MNTIMAGE/sbin/modprobe
546 if [ -n "$USE_UDEV" ]; then
547 inst /sbin/udev.static $MNTIMAGE/sbin/udev
548 ln -s udev $MNTIMAGE/sbin/udevstart
549 mkdir -p $MNTIMAGE/etc/udev
550 inst /etc/udev/udev.conf $MNTIMAGE/etc/udev/udev.conf
551 ln -s /sbin/nash $MNTIMAGE/sbin/hotplug
554 for MODULE in $MODULES; do
555 if [ -x /usr/bin/strip ]; then
556 /usr/bin/strip -g $verbose /lib/modules/$kernel/$MODULE -o $MNTIMAGE/lib/$(basename $MODULE)
558 cp $verbose -a /lib/modules/$kernel/$MODULE $MNTIMAGE/lib
562 # mknod'ing the devices instead of copying them works both with and
564 mknod $MNTIMAGE/dev/console c 5 1
565 mknod $MNTIMAGE/dev/null c 1 3
566 mknod $MNTIMAGE/dev/ram b 1 1
567 mknod $MNTIMAGE/dev/systty c 4 0
569 mknod $MNTIMAGE/dev/tty$i c 4 $i
572 # FIXME -- this won't work if you're using devfs
573 if [ -n "$root_lvm" -a "$kernelmajor" == "2.4" ]; then
574 pvs=$(/sbin/pvscan | grep " PV " | /bin/awk {'print $5;'} |sed 's/"//g')
576 cp $verbose --parents -a $pv $MNTIMAGE/
579 inst /sbin/vgwrapper "$MNTIMAGE/bin/vgwrapper"
580 ln "$MNTIMAGE/bin/vgwrapper" "$MNTIMAGE/bin/vgscan"
581 ln "$MNTIMAGE/bin/vgwrapper" "$MNTIMAGE/bin/vgchange"
583 mknod $MNTIMAGE/dev/lvm b 109 0
586 if [ -n "$root_lvm" -a "$kernelmajor" == "2.6" ]; then
587 inst /sbin/lvm.static "$MNTIMAGE/bin/lvm"
588 if [ -f /etc/lvm/lvm.conf ]; then
589 cp $verbose --parents /etc/lvm/lvm.conf $MNTIMAGE/
593 echo "#!/bin/nash" > $RCFILE
596 echo "mount -t proc /proc /proc" >> $RCFILE
597 echo "setquiet" >> $RCFILE
598 echo "echo Mounted /proc filesystem" >> $RCFILE
600 if [ "$kernelmajor" != "2.4" ]; then
601 echo "echo Mounting sysfs" >> $RCFILE
602 echo "mount -t sysfs none /sys" >> $RCFILE
605 if [ -n "$USE_UDEV" ]; then
606 if [ -n "$UDEV_TMPFS" ]; then
609 mount -o mode=0755 -t tmpfs none /dev
610 mknod /dev/console c 5 1
611 mknod /dev/null c 1 3
612 mknod /dev/zero c 1 5
620 echo -n "/sbin/hotplug" > /proc/sys/kernel/hotplug
624 for MODULE in $MODULES; do
626 module=`echo $MODULE | sed "s|.*/||" | sed "s/.k\?o$//"`
627 fullmodule=`echo $MODULE | sed "s|.*/||"`
629 options=`sed -n -e "s/^options[ ][ ]*$module[ ][ ]*//p" $modulefile 2>/dev/null`
631 if [ -n "$verbose" ]; then
632 if [ -n "$options" ]; then
633 text=" with options $options"
635 echo "Loading module $module$text"
637 echo "echo \"Loading $fullmodule module\"" >> $RCFILE
638 echo "insmod /lib/$fullmodule $options" >> $RCFILE
640 # Hack - we need a delay after loading usb-storage to give things
641 # time to settle down before we start looking a block devices
642 if [ "$module" = "usb-storage" ]; then
643 echo "sleep 5" >> $RCFILE
645 if [ "$module" = "zfcp" -a -f /etc/zfcp.conf ]; then
646 echo "sleep 2" >> $RCFILE
647 cat /etc/zfcp.conf | grep -v "^#" | tr "A-Z" "a-z" | while read DEVICE SCSIID WWPN SCSILUN FCPLUN; do
648 echo "echo -n $WWPN > /sys/bus/ccw/drivers/zfcp/${DEVICE/0x/}/port_add" >>$RCFILE
649 echo "echo -n $FCPLUN > /sys/bus/ccw/drivers/zfcp/${DEVICE/0x/}/$WWPN/unit_add" >>$RCFILE
650 echo "echo -n 1 > /sys/bus/ccw/drivers/zfcp/${DEVICE/0x/}/online" >>$RCFILE
655 # HACK: module loading + device creation isn't necessarily synchronous...
656 # this will make sure that we have all of our devices before trying
657 # things like RAID or LVM
658 if [ -n "$USE_UDEV" ]; then
659 echo "/sbin/udevstart" >> $RCFILE
662 if [ -n "$startraid" ]; then
663 for dev in $raiddevices; do
664 cp -a /dev/${dev} $MNTIMAGE/dev
665 echo "raidautorun /dev/${dev}" >> $RCFILE
669 if [ -z "$USE_UDEV" ]; then
670 echo "echo Creating block devices" >> $RCFILE
671 echo "mkdevices /dev" >> $RCFILE
674 if [ -n "$loopDev" ]; then
676 cp -a $loopDev $MNTIMAGE/dev
677 cp -a $rootdev $MNTIMAGE/dev
678 echo "echo Mounting device containing loopback root filesystem" >> $RCFILE
679 echo "mount -t $loopFs $loopDev /loopfs" >> $RCFILE
680 echo "echo Setting up loopback device $rootdev" >> $RCFILE
681 echo "losetup $rootdev /loopfs$loopFile" >> $RCFILE
682 elif [ -n "$root_lvm" ]; then
683 if [ "$kernelmajor" == "2.4" ]; then
684 echo "echo Scanning logical volumes" >> $RCFILE
685 echo "vgscan" >> $RCFILE
686 echo "echo Activating logical volumes" >> $RCFILE
687 echo "vgchange -ay" >> $RCFILE
689 echo "echo Making device-mapper control node" >> $RCFILE
690 echo "mkdmnod" >> $RCFILE
691 echo "echo Scanning logical volumes" >> $RCFILE
692 echo "lvm vgscan" >> $RCFILE
693 echo "echo Activating logical volumes" >> $RCFILE
694 echo "lvm vgchange -ay" >> $RCFILE
695 echo "echo Making device nodes" >> $RCFILE
696 echo "lvm vgmknodes" >> $RCFILE
700 echo "echo Creating root device" >> $RCFILE
701 echo "mkrootdev /dev/root" >> $RCFILE
704 if [ "$kernelmajor" != "2.4" ]; then
705 echo "umount /sys" >> $RCFILE
708 if [ -n "$initramfs" ]; then
709 echo "echo Mounting root filesystem" >> $RCFILE
710 echo "mount -o $rootopts --ro -t $rootfs $rootdev /sysroot" >> $RCFILE
712 [ -n "$UDEV_KEEP_DEV" ] && echo "mount -t tmpfs --bind /dev /sysroot/dev" >> $RCFILE
714 echo "echo Switching to new root" >> $RCFILE
715 echo "switchroot /sysroot" >> $RCFILE
717 if [ -n "$pivot" ]; then
718 echo "echo 0x0100 > /proc/sys/kernel/real-root-dev" >> $RCFILE
720 echo "echo Mounting root filesystem" >> $RCFILE
721 echo "mount -o $rootopts --ro -t $rootfs $rootdev /sysroot" >> $RCFILE
723 echo "pivot_root /sysroot /sysroot/initrd" >> $RCFILE
724 echo "umount /initrd/proc" >> $RCFILE
726 echo "umount /proc" >> $RCFILE
730 [ -n "$UDEV_TMPFS" ] && echo "umount /initrd/dev" >> $RCFILE
733 (cd $MNTIMAGE; find . | cpio --quiet -c -o) > $IMAGE || exit 1
735 if [ -n "$compress" ]; then
736 gzip -9 < $IMAGE > $target || rc=1
738 cp -a $IMAGE $target || rc=1