]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/nash/mkinitrd
2a36352de167db0c6ac06d76e15071d4cc73c989
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
39 modulefile
=/etc
/modules.conf
43 PRESCSIMODS
="scsi_mod sd_mod unknown"
47 echo "usage: `basename $0` [--version] [-v] [-f] [--preload <module>]" >&2
48 echo " [--omit-scsi-modules] [--omit-raid-modules] [--omit-lvm-modules]" >&2
49 echo " [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]" >&2
50 echo " [--builtin=<module>] [--nopivot] <initrd-image> <kernel-version>" >&2
52 echo " (ex: `basename $0` /boot/initrd-2.2.5-15.img 2.2.5-15)" >&2
57 if [ ! -f "/lib/modules/$kernel/modules.dep" ]; then
58 echo "No dep file found for kernel $kernel" >&2
62 [ -n "$verbose" ] && echo -n "Looking for deps of module $1"
63 deps
=$
(gawk
'BEGIN { searched=ARGV[2]; ARGV[2]=""; rc=1 } \
64 function modname(filename) { match(filename, /\/([^\/]+)\.k?o/, ret); return ret[1] } \
65 function show() { if (orig == searched) { print dep; orig=""; rc=0; exit } } \
68 if ($2) { dep=modname($2) } else { dep="" } } \
69 /^ / { dep=sprintf("%s %s", dep, modname($1)); } \
70 END { show(); exit(rc) }' /lib
/modules
/$kernel/modules.dep
$1)
71 [ -n "$verbose" ] && echo -e "\t$deps"
77 if [ $1 == "--skiperrors" ]; then
78 skiperrors
=--skiperrors
84 if [ "$modName" = "off" -o "$modName" = "null" ]; then
88 if [ $
(echo $modName | cut
-b1) = "-" ]; then
89 skiperrors
=--skiperrors
90 modName
=$
(echo $modName | cut
-b2-)
93 if echo $builtins |
grep -E -q '(^| )'$modName'( |$)' ; then
94 [ -n "$verbose" ] && echo "module $modName assumed to be built in"
100 if [ "$modName" = "i2o_block" ]; then
104 elif [ "$modName" = "ppa" ]; then
106 findmodule parport_pc
108 elif [ "$modName" = "sbp2" ]; then
119 for modExt
in o.gz o ko
; do
120 if [ -d /lib
/modules
/$kernel/updates
]; then
121 fmPath
=`(cd /lib/modules/$kernel/updates; echo find . -name $modName.$modExt -type f | /sbin/nash --quiet) | gawk {'print $1; exit;'}`
124 if [ -f /lib
/modules
/$kernel/updates
/$fmPath ]; then
125 fmPath
=updates
/$fmPath
129 fmPath
=`(cd /lib/modules/$kernel; echo find . -name $modName.$modExt -type f | /sbin/nash --quiet) | gawk {'print $1; exit;'}`
130 if [ -f /lib
/modules
/$kernel/$fmPath ]; then
135 if [ ! -f /lib
/modules
/$kernel/$fmPath ]; then
136 if [ -n "$skiperrors" ]; then
140 # ignore the absence of the scsi modules
141 for n
in $PRESCSIMODS; do
142 if [ "$n" = "$modName" ]; then
147 echo "No module $modName found for kernel $kernel, aborting." >&2
151 # only need to add each module once
152 if ! echo $MODULES |
grep -q "$fmPath" 2>/dev
/null
; then
153 MODULES
="$MODULES $fmPath"
158 if [ "$#" != "2" ];then
159 echo "usage: inst <file> <destination>"
162 [ -n "$verbose" ] && echo "$1 -> $2"
166 while [ $# -gt 0 ]; do
169 if echo $1 |
grep -q '=' ; then
170 fstab
=`echo $1 | sed 's/^--fstab=//'`
182 if echo $1 |
grep -q '=' ; then
183 modname
=`echo $1 | sed 's/^--with=//'`
189 basicmodules
="$basicmodules $modname"
193 if echo $1 |
grep -q '=' ; then
194 modname
=`echo $1 | sed 's/^--builtin=//'`
199 builtins
="$builtins $modname"
203 echo "mkinitrd: version $VERSION"
227 if echo $1 |
grep -q '=' ; then
228 modname
=`echo $1 | sed 's/^--preload=//'`
233 PREMODS
="$PREMODS $modname"
249 if [ -z "$target" ]; then
251 elif [ -z "$kernel" ]; then
262 if [ -z "$target" -o -z "$kernel" ]; then
266 if [ -n "$img_vers" ]; then
267 target
="$target-$kernel"
270 if [ -z "$force" -a -f $target ]; then
271 echo "$target already exists." >&2
275 if [ ! -d /lib
/modules
/$kernel ]; then
276 echo "/lib/modules/$kernel is not a directory." >&2
280 if [ $UID != 0 ]; then
281 echo "mkinitrd must be run as root"
285 # find a temporary directory which doesn't use tmpfs
287 for t
in /tmp
/var
/tmp
/root
${PWD}; do
288 if [ ! -d $t ]; then continue; fi
289 if ! echo access
-w $t |
/sbin
/nash
--quiet; then continue; fi
291 fs
=$
(df
-T $t 2>/dev
/null | gawk
'{line=$1;} END {printf $2;}')
292 if [ "$fs" != "tmpfs" ]; then
298 if [ -z "$TMPDIR" ]; then
299 echo "no temporary directory could be found" >&2
303 if [ $TMPDIR = "/root" -o $TMPDIR = "${PWD}" ]; then
304 echo "WARNING: using $TMPDIR for temporary files" >&2
307 for n
in $PREMODS; do
312 if [ -n "$withusb" ]; then
313 # If / or /boot is on a USB device include the driver. With root by
314 # label we could still get some odd behaviors
315 for fs
in / /boot
; do
316 esc
=$
(echo $fs |
sed 's,/,\\/,g')
317 dev
=$
(mount | gawk
"/ on ${esc} / { print \$1 }" |
sed 's/[0-9]*$//' | cut
-d/ -f3)
318 if [ "$(echo $dev | cut -c1-2)" = sd
]; then
319 if [ `which kudzu 2>/dev/null` ]; then
320 host=$
(kudzu
--probe -b scsi |
321 gawk
'/^device: '${dev}'/,/^host:/ { if (/^host/) { print $2; exit; } }')
322 if [ -d /proc
/scsi
/usb-storage-
${host} ]; then
330 if [ -n "$needusb" ]; then
331 drivers
=$
(gawk
'/^alias usb-controller[0-9]* / { print $3}' < /etc
/modules.conf
)
332 if [ -n "$drivers" ]; then
333 for driver
in $drivers; do
338 findmodule usb-storage
342 if [ -z "$noscsi" ]; then
343 if [ ! -f $modulefile ]; then
344 modulefile
=/etc
/conf.modules
347 if [ -f $modulefile ]; then
348 scsimodules
=`grep "alias[[:space:]]scsi_hostadapter" $modulefile | grep -v '^[ ]*#' | LC_ALL=C sort -u | gawk '{ print $3 }'`
350 if [ -n "$scsimodules" ]; then
351 for n
in $PRESCSIMODS; do
355 for n
in $scsimodules; do
356 # for now allow scsi modules to come from anywhere. There are some
357 # RAID controllers with drivers in block/
364 # If we have ide devices and module ide, do the right thing
366 if [ -n "$ide" ]; then
370 # If we use LVM, include lvm-mod
371 if [ -z "$nolvm" ]; then
372 if [ -f /proc
/lvm
/global
]; then
373 if grep -q '^VG:' /proc
/lvm
/global
; then
379 # If we have dasd devices, include the necessary modules (S/390)
380 if [ -d /proc
/dasd
]; then
382 findmodule
-dasd_eckd_mod
383 findmodule
-dasd_fba_mod
386 if [ -z "$noraid" -a -f /proc
/mdstat
]; then
387 # load appropriate raid devices if necessary -- we'll load whatever
388 # /proc/mdstat suggests
390 # note that the gawk below contains a space and a tab
391 for level
in $
(gawk
'/^md[0-9][0-9]*[ ]*:/ { print $4 }' \
392 /proc
/mdstat |
sort -u); do
403 echo "raid level $level (in /proc/mdstat) not recognized" >&2
408 if [ -n "$startraid" ]; then
409 raiddevices
=$
(gawk
'/^md[0-9][0-9]*[ ]*:/ { print $1 }' \
414 # check to see if we need to set up a loopback filesystem
415 rootdev
=$
(gawk
'/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' $fstab)
416 if echo $rootdev | cut
-d/ -f3 |
grep -q loop
; then
417 key
="^# $(echo $rootdev | cut -d/ -f3 | tr '[a-z]' '[A-Z]'):"
418 if ! grep "$key" $fstab > /dev
/null
; then
419 echo "The root filesystem is on a $rootdev, but there is no magic entry in $fstab" 1>&2
420 echo "for this device. Consult the mkinitrd man page for more information" 2>&2
424 line
=$
(grep "$key" $fstab)
425 loopDev
=$
(echo $line | gawk
'{print $3}')
426 loopFs
=$
(echo $line | gawk
'{print $4}')
427 loopFile
=$
(echo $line | gawk
'{print $5}')
429 basicmodules
="$basicmodules -loop"
430 if [ "$loopFs" = "vfat" -o "$loopFs" = "msdos" ]; then
431 basicmodules
="$basicmodules -fat"
433 basicmodules
="$basicmodules -${loopFs}"
434 # check if the root fs is on a logical volume
435 elif ! echo $rootdev | cut
-c1-6 |
grep -q "LABEL=" ; then
436 rootdev
=$
(echo "readlink $rootdev" |
/sbin
/nash
--quiet)
437 major
=`ls -l $rootdev | sed -e "s/.* \\([0-9]\+\\), *[0-9]\+.*/\\1/"`
438 [ "$major" != "58" ] || root_lvm
=1
441 rootfs
=$
(gawk
'{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $3; }}' $fstab)
442 rootopts
=$
(gawk
'{ if ($1 !~ /^[ \t]*#/ && $2 == "/") { print $4; }}' $fstab)
444 # in case the root filesystem is modular
445 findmodule
-${rootfs}
447 if [ -n "$root_lvm" ]; then
451 for n
in $basicmodules; do
455 if [ -n "$verbose" ]; then
456 echo "Using modules: $MODULES"
460 MNTIMAGE
=`mktemp -d ${TMPDIR}/initrd.XXXXXX`
461 IMAGE
=`mktemp ${TMPDIR}/initrd.img.XXXXXX`
462 MNTPOINT
=`mktemp -d ${TMPDIR}/initrd.mnt.XXXXXX`
463 RCFILE
=$MNTIMAGE/linuxrc
465 if [ -z "$MNTIMAGE" -o -z "$IMAGE" -o -z "$MNTPOINT" ]; then
466 echo "Error creating temporaries. Try again" >&2
470 dd if=/dev
/zero of
=$IMAGE bs
=1k count
=$IMAGESIZE 2> /dev
/null ||
exit 1
472 LODEV
=$
(echo findlodev |
/sbin
/nash
--quiet)
474 if [ -z "$LODEV" ]; then
475 rm -rf $MNTIMAGE $MNTPOINT $IMAGE
476 echo "All of your loopback devices are in use." >&2
480 losetup
${LODEV} $IMAGE ||
exit 1
482 # We have to "echo y |" so that it doesn't complain about $IMAGE not
483 # being a block device
484 echo y | mke2fs
$LODEV $IMAGESIZE >/dev
/null
2>/dev
/null
485 tune2fs
-i0 $LODEV >/dev
/null
487 if [ -n "$verbose" ]; then
488 echo "Using loopback device $LODEV"
492 mount
-t ext2
$LODEV $MNTPOINT ||
{
493 echo "Can't get a loopback device"
498 mkdir
-p $MNTIMAGE/lib
499 mkdir
-p $MNTIMAGE/bin
500 mkdir
-p $MNTIMAGE/etc
501 mkdir
-p $MNTIMAGE/dev
502 mkdir
-p $MNTIMAGE/loopfs
503 mkdir
-p $MNTIMAGE/proc
504 mkdir
-p $MNTIMAGE/sysroot
505 ln -s bin
$MNTIMAGE/sbin
507 # We don't need this directory, so let's save space
508 rm -rf $MNTPOINT/lost
+found
510 inst
/sbin
/nash
"$MNTIMAGE/bin/nash"
511 inst
/sbin
/insmod.static
"$MNTIMAGE/bin/insmod"
512 ln -s /sbin
/nash
$MNTIMAGE/sbin
/modprobe
514 for MODULE
in $MODULES; do
515 cp $verbose -a /lib
/modules
/$kernel/$MODULE $MNTIMAGE/lib
518 # mknod'ing the devices instead of copying them works both with and
520 mknod
$MNTIMAGE/dev
/console c
5 1
521 mknod
$MNTIMAGE/dev
/null c
1 3
522 mknod
$MNTIMAGE/dev
/ram b
1 1
523 mknod
$MNTIMAGE/dev
/systty c
4 0
525 mknod
$MNTIMAGE/dev
/tty
$i c
4 $i
528 # FIXME -- this won't work if you're using devfs
529 if [ -n "$root_lvm" ]; then
530 pvs
=$
(/sbin
/pvscan |
grep " PV " | gawk
{'print $5;'} |
sed 's/"//g')
532 cp $verbose --parents -a $pv $MNTIMAGE/
535 inst
/sbin
/vgwrapper
"$MNTIMAGE/bin/vgwrapper"
536 ln "$MNTIMAGE/bin/vgwrapper" "$MNTIMAGE/bin/vgscan"
537 ln "$MNTIMAGE/bin/vgwrapper" "$MNTIMAGE/bin/vgchange"
539 mknod
$MNTIMAGE/dev
/lvm b
109 0
542 echo "#!/bin/nash" > $RCFILE
545 for MODULE
in $MODULES; do
547 module
=`echo $MODULE | sed "s|.*/||" | sed "s/.k\?o$//"`
548 fullmodule
=`echo $MODULE | sed "s|.*/||"`
550 options
=`sed -n -e "s/^options[ ][ ]*$module[ ][ ]*//p" $modulefile 2>/dev/null`
552 if [ -n "$verbose" ]; then
553 if [ -n "$options" ]; then
554 text
=" with options $options"
556 echo "Loading module $module$text"
558 echo "echo \"Loading $fullmodule module\"" >> $RCFILE
559 echo "insmod /lib/$fullmodule $options" >> $RCFILE
561 # Hack - we need a delay after loading usb-storage to give things
562 # time to settle down before we start looking a block devices
563 if [ "$module" = "usb-storage" ]; then
564 echo "sleep 5" >> $RCFILE
568 echo "echo Mounting /proc filesystem" >> $RCFILE
569 echo "mount -t proc /proc /proc" >> $RCFILE
571 if [ -n "$startraid" ]; then
572 for dev
in $raiddevices; do
573 cp -a /dev
/${dev} $MNTIMAGE/dev
574 echo "raidautorun /dev/${dev}" >> $RCFILE
578 echo "echo Creating block devices" >> $RCFILE
579 echo "mkdevices /dev" >> $RCFILE
581 if [ -n "$loopDev" ]; then
583 cp -a $loopDev $MNTIMAGE/dev
584 cp -a $rootdev $MNTIMAGE/dev
585 echo "echo Mounting device containing loopback root filesystem" >> $RCFILE
586 echo "mount -t $loopFs $loopDev /loopfs" >> $RCFILE
587 echo "echo Setting up loopback device $rootdev" >> $RCFILE
588 echo "losetup $rootdev /loopfs$loopFile" >> $RCFILE
589 elif [ -n "$root_lvm" ]; then
590 echo "echo Scanning logical volumes" >> $RCFILE
591 echo "vgscan" >> $RCFILE
592 echo "echo Activating logical volumes" >> $RCFILE
593 echo "vgchange -ay" >> $RCFILE
595 echo "echo Creating root device" >> $RCFILE
596 echo "mkrootdev /dev/root" >> $RCFILE
600 if [ -n "$pivot" ]; then
601 echo "echo 0x0100 > /proc/sys/kernel/real-root-dev" >> $RCFILE
603 echo "echo Mounting root filesystem" >> $RCFILE
604 echo "mount -o $rootopts --ro -t $rootfs $rootdev /sysroot" >> $RCFILE
606 echo "pivot_root /sysroot /sysroot/initrd" >> $RCFILE
607 echo "umount /initrd/proc" >> $RCFILE
609 echo "umount /proc" >> $RCFILE
614 (cd $MNTIMAGE; tar cf
- .
) |
(cd $MNTPOINT; tar xf
-) ||
exit 1
619 if [ -n "$compress" ]; then
620 gzip -9 < $IMAGE > $target || rc
=1
622 cp -a $IMAGE $target || rc
=1
624 rm -rf $MNTIMAGE $MNTPOINT $IMAGE