home *** CD-ROM | disk | FTP | other *** search
- #! /bin/bash
-
- # Script to create an initial ram disk.
- #
- # usage:
- # mk_initrd [root_dir]
- #
- # root_dir: the directory the root partition is mounted to (defaults to "/")
- #
- # As an alternative, you can specify the root device via the environment
- # variable rootdev (e.g. "rootdev=/dev/hda mk_initrd").
- #
- # on errors:
- # exit code > 0
- #
- # Version 1.08
- #
- # Author: Steffen Winterfeldt <wfeldt@suse.de>
- # (c) 1999 SuSE GmbH
- # (c) 1999 Heiko Eiâ–€feldt bugfix, add module parameter passing
-
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # general configurable parameters
-
- # the kernel images to use; must be in $boot_dir
- kernels="vmlinuz vmlinuz.suse"
-
- # initial ram disks (corresponding to $kernels); dto. in $boot_dir
- initrds="initrd initrd.suse"
-
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # adapt these only if needed
-
- # *full* paths
- boot_dir=/boot
- static_shell=/bin/ash.static
- static_insmod=/sbin/insmod.static
-
- # initrd size
- image_blocks=1500
- image_inodes=100
-
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # should be nothing to change below...
-
- tmp_mnt=/tmp/mnt$$
- tmp_msg=/tmp/msg$$
- tmp_magic=/tmp/magic$$
- lx_rc=$tmp_mnt/linuxrc
- is_mounted=
-
- clean_up () {
- [ "$is_mounted" ] && umount $tmp_mnt
- rm -f $tmp_initrd $tmp_initrd.gz $tmp_msg $tmp_magic
- [ -d $tmp_mnt ] && rmdir $tmp_mnt
- }
-
- error () {
- echo "$2"
- clean_up
- exit $1
- }
-
- # write an updated magic file
- write_magic () {
- cat <<-blub >$tmp_magic
- 0x202 string HdrS Linux/i386 Kernel
- >0x206 short 0x0105 setupV1 zImage
- >0x206 short 0x0200 setupV2
- >>0x211 byte 0 zImage
- >>0x211 byte 1 bzImage
- >(0x20e.s+0x205) byte 32
- >>(0x20e.s+0x200) string >\0 "%5.5s"
- >(0x20e.s+0x206) byte 32
- >>(0x20e.s+0x200) string >\0 "%6.6s"
- >(0x20e.s+0x207) byte 32
- >>(0x20e.s+0x200) string >\0 "%7.7s"
- >(0x20e.s+0x208) byte 32
- >>(0x20e.s+0x200) string >\0 "%8.8s"
- >(0x20e.s+0x209) byte 32
- >>(0x20e.s+0x200) string >\0 "%9.9s"
- >(0x20e.s+0x20a) byte 32
- >>(0x20e.s+0x200) string >\0 "%10.10s"
- blub
- }
-
- [ "$1" = -r ] && shift
-
- root_dir="$1"
- [ "$root_dir" ] || root_dir=/
-
- tmp_initrd=$root_dir/tmp/initrd$$
-
- if [ "$root_dir" != / ] ; then
- LD_LIBRARY_PATH=$root_dir/usr/lib
- PATH=$root_dir/usr/bin:$PATH
- [ -x "$root_dir$static_shell" ] && static_shell="$root_dir$static_shell"
- [ -x "$root_dir$static_insmod" ] && static_insmod="$root_dir$static_insmod"
- fi
-
- x1=`mount 2>/dev/null | grep "on $root_dir "`
- x2=`echo \`echo "$x1" | wc -l\``
- x3=`echo "$x1" | cut -f 1 -d " "`
-
- if [ -z "$rootdev" ] ; then
- [ "$x2" = 1 -a -b "$x3" ] && rootdev="$x3"
- fi
-
- [ "$rootdev" ] || error 1 "usage: mk_initrd [root_dir]"
-
- echo "using \"$rootdev\" as root device (mounted on \"$root_dir\")"
-
- [ -f "$root_dir/etc/rc.config" ] && . $root_dir/etc/rc.config
-
- modules="$INITRD_MODULES"
-
- ( cd $root_dir$boot_dir ; rm -f $initrds )
- [ "$modules" ] || error 0 "no initrd required"
-
- initrd_a=($initrds)
-
- file=file
- [ -x /usr/bin/file ] && file=/usr/bin/file
- [ -x $root_dir/usr/bin/file ] && file=$root_dir/usr/bin/file
-
- kernel_idx=0
- for k in $kernels ; do
-
- kk="$root_dir$boot_dir/$k"
-
- if [ -f "$kk" ] ; then
-
- write_magic
-
- vv=`$file -m $tmp_magic $kk | cut -d \" -f 2`
-
- ii="$root_dir$boot_dir/${initrd_a[$kernel_idx]}"
-
- echo
- echo "creating initrd \"$ii\" for kernel \"$kk\" ($vv)"
-
- [ -d "$root_dir/lib/modules/$vv/misc" ] || error 2 "no version \"$vv\" modules found"
-
- mkdir $tmp_mnt
-
- dd if=/dev/zero of=$tmp_initrd bs=1k count=$image_blocks 2>/dev/null
- mke2fs -q -F -b 1024 -m 0 -N $image_inodes $tmp_initrd 2>/dev/null
- tune2fs -i 0 $tmp_initrd >/dev/null 2>&1
-
- mount -oloop $tmp_initrd $tmp_mnt 2>/dev/null || {
- if [ -f /lib/loop.o ] ; then
- insmod /lib/loop.o
- mount -oloop $tmp_initrd $tmp_mnt 2>/dev/null || {
- error 3 "failed to mount image"
- }
- else
- error 3 "failed to mount image"
- fi
- }
- is_mounted=1
-
- rmdir $tmp_mnt/lost+found
- mkdir $tmp_mnt/{bin,dev}
- cp -a $root_dir/dev/{tty1,tty2,zero,null,ram0,ram1,ram2,ram,ramdisk,fb0,console} $tmp_mnt/dev
- cp $static_shell $tmp_mnt/bin/sh 2>/dev/null || error 4 "no static shell"
- cp $static_insmod $tmp_mnt/bin/insmod 2>/dev/null || error 5 "no static insmod"
-
- echo '#! /bin/sh' >$lx_rc
- echo >>$lx_rc
- echo "export PATH=/bin" >>$lx_rc
- echo >>$lx_rc
-
- chmod 755 $lx_rc
-
- for i in $modules; do
- x=`cd $root_dir ; find lib/modules/$vv/ -name $i.o`
- if [ "$x" ] ; then
- echo "module $i is \"/$x\""
- tar -C $root_dir -cf - $x 2>/dev/null | tar -C $tmp_mnt -xpf - 2>/dev/null
- if [ $? != 0 ] ; then
- echo "failed to add module \"/$x\""
- error 6 "initrd too small"
- fi
- modparms=`grep "^[ ]*options $i" "$root_dir"/etc/modules.conf`
- modparms="${modparms#* * }"
- echo "echo \"Loading module $i $modparms ...\"" >>$lx_rc
- echo "-> insmod $i $modparms"
- echo "insmod $i $modparms" >>$lx_rc
- echo >>$lx_rc
- else
- error 7 "no such module: $i"
- fi
- done
-
- umount $tmp_mnt
- is_mounted=
-
- gzip -9 $tmp_initrd
-
- cp $tmp_initrd.gz $ii || error 8 "failed to install initrd"
-
- kernel_idx=$((kernel_idx+1))
-
- clean_up
-
- else
-
- echo "no kernel image \"$k\""
-
- fi
-
- done
-
-