5 # by Jacek Konieczny <jajcus@pld.org.pl>
7 # based on geninitrd written by Erik Troan <ewt@redhat.com>
9 # Elliot Lee <sopwith@cuc.edu>
10 # Miguel de Icaza <miguel@nuclecu.unam.mx>
11 # Christian 'Dr. Disk' Hechelmann <drdisk@ds9.au.s.shuttle.de>
12 # Michael K. Johnson <johnsonm@redhat.com>
13 # Pierre Habraken <Pierre.Habraken@ujf-grenoble.fr>
14 # Jakub Jelinek <jj@ultra.linux.cz>
15 # Carlo Arenas Belon (carenas@chasqui.lared.net.pe>
22 . /etc/rc.d/init.d/functions
30 if [ -f /etc/sysconfig/initrd ] ; then
31 . /etc/sysconfig/initrd
40 modulefile=/etc/modules.conf
41 if [ `uname -m` = "ia64" ]; then
46 PRESCSIMODS="scsi_mod sd_mod unknown"
47 PREIDEMODS="ide-mod ide-probe-mod ide-disk"
51 echo "usage: `basename $0` [--version] [-v] [-f] [--ifneeded] [--preload <module>]" >&2
52 echo " [--omit-scsi-modules] [--omit-raid-modules] [--omit-ide-modules] [--with=<module>]" >&2
53 echo " [--image-version] [--fstab=<fstab>] [--nocompress] [--fs=rom|ext2|cram]" >&2
54 echo " <initrd-image> <kernel-version>" >&2
55 echo " (ex: `basename $0` /boot/initrd-2.2.5-15.img 2.2.5-15)" >&2
62 if [ $(echo $modName | cut -b1) = "-" ]; then
64 modName=$(echo $modName | cut -b2-)
67 if [ "$modName" = "pluto" ]; then
71 if [ "$modName" = "fcal" ]; then
76 fmPath="$1"/"$modName".o
78 fmPath=`(cd /lib/modules/$kernel; echo */$modName.o)`
81 if [ "$1" = "fs" ] && [ ! -f /lib/modules/$kernel/$fmPath ]; then
82 fmPath="$1"/"${modName}fs".o
85 if [ ! -f /lib/modules/$kernel/$fmPath ]; then
86 if [ -n "$skiperrors" ]; then
90 # ignore the absence of the scsi modules
91 for n in $PRESCSIMODS; do
92 if [ "$n" = "$modName" ]; then
97 # ignore the absence of the ide modules
98 for n in $PREIDEMODS; do
99 if [ "$n" = "$modName" ]; then
104 echo "No module $modName found for kernel $kernel" >&2
108 # only need to add each module once
109 if echo $MODULES | grep $fmPath >/dev/null 2>&1 ; then : ; else
110 MODULES="$MODULES $fmPath"
115 if [ "$#" != "2" ];then
116 echo "usage: inst <file> <destination>"
119 [ -n "$verbose" ] && echo "$1 -> $2"
124 while [ $# -gt 0 ]; do
127 if echo $1 | grep '=' >/dev/null ; then
128 fstab=`echo $1 | sed 's/^--with=//'`
136 if echo $1 | grep '=' >/dev/null ; then
137 modname=`echo $1 | sed 's/^--with=//'`
143 BASICMODULES="$BASICMODULES $modname"
147 echo "geninitrd: version $VERSION"
167 if echo $1 | grep '=' >/dev/null ; then
168 modname=`echo $1 | sed 's/^--preload=//'`
173 PREMODS="$PREMODS $modname"
186 if echo $1 | grep '=' >/dev/null ; then
187 FS=`echo $1 | sed 's/^--fs=//'`
197 if [ -z "$target" ]; then
199 elif [ -z "$kernel" ]; then
212 if [ ! -x /sbin/losetup ]; then
213 echo "losetup is missing"
218 if [ ! -x /sbin/genromfs ]; then
219 echo "genromfs is missing"
224 if [ ! -x /sbin/mkcramfs ]; then
225 echo "mkcramfs is missing"
230 echo "Filesystem $FS not supported"
235 if [ -z "$target" -o -z "$kernel" ]; then
239 if [ -n "$img_vers" ]; then
240 target="$target-$kernel"
243 if [ -z "$force" -a -f $target ]; then
244 echo "$target already exists." >&2
248 if [ ! -d /lib/modules/$kernel ]; then
249 echo "/lib/modules/$kernel is not a directory." >&2
253 for n in $PREMODS; do
257 rootdev=$(awk '{ if ($2 == "/") { print $1; }}' $fstab)
258 if is_yes "$PROBESCSI" && echo "$rootdev" | grep -q "^/dev/sd" ; then
260 for n in $PRESCSIMODS; do
264 if [ ! -f $modulefile ]; then
265 modulefile=/etc/conf.modules
267 if [ -f $modulefile ]; then
268 scsimodules=`grep scsi_hostadapter $modulefile | grep -v '^[ ]*#' | LC_ALL=C sort -u | awk '{ print $3 }'`
269 for n in $scsimodules; do
270 # for now allow scsi modules to come from anywhere. There are some
271 # RAID controllers with drivers in block/
277 if is_yes "$PROBEIDE" && echo "$rootdev" | grep -q "^/dev/hd" ; then
278 for n in $PREIDEMODS; do
283 if is_yes "$PROBERAID" ; then
284 # load appropriate raid devices if necessary
285 if grep '^/dev/md' $fstab | grep -v noauto >/dev/null 2>&1 ; then
286 for number in $(grep '^[ ]*raid-level' /etc/raidtab |
287 awk '{print $2}' | LC_ALL=C sort -u) ; do
290 findmodule "" raid$number
293 echo "raid level $number (in /etc/raidtab) not recognized" >&2
300 # check to see if we need to set up a loopback filesystem
301 if echo $rootdev | cut -d/ -f3 | grep loop >/dev/null; then
302 key="^# $(echo $rootdev | cut -d/ -f3 | tr '[a-z]' '[A-Z]'):"
303 if ! grep "$key" $fstab > /dev/null; then
304 echo "The root filesystem is on a $rootdev, but there is no magic entry in $fstab" 1>&2
305 echo "for this device. Consult the geninitrd man page for more information" 2>&2
309 line=$(grep "$key" $fstab)
310 loopDev=$(echo $line | awk '{print $3}')
311 loopFs=$(echo $line | awk '{print $4}')
312 loopFile=$(echo $line | awk '{print $5}')
314 BASICMODULES="$BASICMODULES -loop"
315 findmodule fs "-$loopFs"
316 BASICMODULES="$BASICMODULES -${loopFs}"
317 else # Check for other filesystems
318 rootFs=`awk '{if ($2 == "/") {print $3}}' $fstab`
319 findmodule fs "-$rootFs"
322 for n in $BASICMODULES; do
326 if [ -n "$ifneeded" -a -z "$MODULES" ]; then
327 if [ -n "$verbose" ]; then
328 echo "No modules are needed -- not building initrd image."
333 if [ -n "$verbose" ]; then
334 echo "Using modules: $MODULES"
337 MNTIMAGE=/tmp/initrd.$$
338 IMAGE=/tmp/initrd.img-$$
339 MNTPOINT=/tmp/initrd.mnt-$$
340 RCFILE=$MNTIMAGE/linuxrc
342 if [ -f $MNTIMAGE ]; then
343 echo "$MNTIMAGE already exists. Remove it and try again" >&2
347 if [ -f $IMAGE ]; then
348 echo "$IMAGE already exists. Remove it and try again" >&2
352 if [ "$FS" = "ext2" ] ; then
353 dd if=/dev/zero of=$IMAGE bs=1k count=$IMAGESIZE 2> /dev/null
355 if [ -e /dev/.devfsd ]; then
361 for devnum in 0 1 2 3 4 5 6 7 8; do
362 if losetup ${LOOPDEV}${devnum} $IMAGE 2>/dev/null ; then break; fi
365 if [ "$devnum" = "8" ]; then
366 rm -rf $MNTPOINT $IMAGE
367 echo "All of your loopback devices are in use!" >&2
371 LODEV=${LOOPDEV}${devnum}
373 # We have to "echo y |" so that it doesn't complain about $IMAGE not
374 # being a block device
375 echo y | mke2fs $LODEV $IMAGESIZE >/dev/null 2>/dev/null
377 if [ -n "$verbose" ]; then
378 echo "Using loopback device $LODEV"
382 mount -t ext2 $LODEV $MNTPOINT || {
383 echo "Can't get a loopback device"
391 mkdir -p $MNTIMAGE/{lib,bin,etc,dev,loopfs}
394 # We don't need this directory, so let's save space
395 rm -rf $MNTPOINT/lost+found
397 inst /bin/ash.static "$MNTIMAGE/bin/sh"
398 inst /sbin/insmod.static "$MNTIMAGE/bin/insmod"
400 for MODULE in $MODULES; do
401 cp $verbose -a /lib/modules/$kernel/$MODULE $MNTIMAGE/lib
404 # mknod'ing the devices instead of copying them works both with and
406 mknod $MNTIMAGE/dev/console c 5 1
407 mknod $MNTIMAGE/dev/null c 1 3
408 mknod $MNTIMAGE/dev/ram b 1 1
409 mknod $MNTIMAGE/dev/systty c 4 0
411 mknod $MNTIMAGE/dev/tty$i c 4 1
414 echo "#!/bin/sh" > $RCFILE
417 for MODULE in $MODULES; do
418 module=`echo $MODULE | sed "s|.*/||" | sed "s/.o$//"`
420 options=`sed -n -e "s/^options[ ][ ]*$module[ ][ ]*//p" $modulefile`
422 if [ -n "$verbose" ]; then
423 echo "Loading module $module with options $options"
425 echo "echo \"Loading $module module\"" >> $RCFILE
426 echo "insmod /lib/$module.o $options" >> $RCFILE
429 if [ -n "$loopDev" ]; then
430 if [ ! -d /initrd ]; then
434 cp -a $loopDev $MNTIMAGE/dev
435 cp -a $rootdev $MNTIMAGE/dev
436 echo "echo Mounting device containing loopback root filesystem" >> $RCFILE
437 echo "mount -t $loopFs $loopDev /loopfs" >> $RCFILE
438 echo "echo Setting up loopback device $rootdev" >> $RCFILE
439 echo "losetup $rootdev /loopfs$loopFile" >> $RCFILE
444 (cd $MNTIMAGE; tar cf - .) | (cd $MNTPOINT; tar xf -)
452 genromfs -f "$IMAGE" -d "$MNTPOINT" -V "PLD initrd for kernel $kernel"
455 mkcramfs "$MNTPOINT" "$IMAGE"
459 echo "Filesystem $FS not supported by $0";
462 if is_yes "$COMPRESS" ; then
463 gzip -9 < $IMAGE > $target
467 rm -rf $MNTIMAGE $MNTPOINT $IMAGE