home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd2.bin / suse / inst-sys / lib / YaST2 / bin / mk_initrd < prev    next >
Encoding:
Text File  |  2000-03-30  |  5.2 KB  |  214 lines

  1. #! /bin/bash
  2.  
  3. # Script to create an initial ram disk.
  4. #
  5. # usage:
  6. # mk_initrd [root_dir]
  7. #
  8. #   root_dir: the directory the root partition is mounted to (defaults to "/")
  9. #
  10. #   As an alternative, you can specify the root device via the environment
  11. #   variable rootdev (e.g. "rootdev=/dev/hda mk_initrd").
  12. #
  13. # on errors:
  14. #   exit code > 0
  15. #
  16. # Version 1.08
  17. #
  18. # Author: Steffen Winterfeldt <wfeldt@suse.de>
  19. # (c) 1999 SuSE GmbH
  20. # (c) 1999 Heiko Eiâ–€feldt bugfix, add module parameter passing
  21.  
  22. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  23. # general configurable parameters
  24.  
  25. # the kernel images to use; must be in $boot_dir
  26. kernels="vmlinuz vmlinuz.suse"
  27.  
  28. # initial ram disks (corresponding to $kernels); dto. in $boot_dir
  29. initrds="initrd initrd.suse"
  30.  
  31. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  32. # adapt these only if needed
  33.  
  34. # *full* paths
  35. boot_dir=/boot
  36. static_shell=/bin/ash.static
  37. static_insmod=/sbin/insmod.static
  38.  
  39. # initrd size
  40. image_blocks=1500
  41. image_inodes=100
  42.  
  43. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  44. # should be nothing to change below...
  45.  
  46. tmp_mnt=/tmp/mnt$$
  47. tmp_msg=/tmp/msg$$
  48. tmp_magic=/tmp/magic$$
  49. lx_rc=$tmp_mnt/linuxrc
  50. is_mounted=
  51.  
  52. clean_up () {
  53.   [ "$is_mounted" ] && umount $tmp_mnt
  54.   rm -f $tmp_initrd $tmp_initrd.gz $tmp_msg $tmp_magic
  55.   [ -d $tmp_mnt ] && rmdir $tmp_mnt
  56. }
  57.  
  58. error () {
  59.   echo "$2"
  60.   clean_up
  61.   exit $1
  62. }
  63.  
  64. # write an updated magic file
  65. write_magic () {
  66. cat <<-blub >$tmp_magic
  67.     0x202    string        HdrS        Linux/i386 Kernel
  68.     >0x206    short        0x0105        setupV1 zImage
  69.     >0x206    short        0x0200        setupV2
  70.     >>0x211    byte        0        zImage
  71.     >>0x211    byte        1        bzImage
  72.     >(0x20e.s+0x205)    byte    32
  73.     >>(0x20e.s+0x200)    string    >\0    "%5.5s"
  74.     >(0x20e.s+0x206)    byte    32
  75.     >>(0x20e.s+0x200)    string    >\0    "%6.6s"
  76.     >(0x20e.s+0x207)    byte    32
  77.     >>(0x20e.s+0x200)    string    >\0    "%7.7s"
  78.     >(0x20e.s+0x208)    byte    32
  79.     >>(0x20e.s+0x200)    string    >\0    "%8.8s"
  80.     >(0x20e.s+0x209)    byte    32
  81.     >>(0x20e.s+0x200)    string    >\0    "%9.9s"
  82.     >(0x20e.s+0x20a)    byte    32
  83.     >>(0x20e.s+0x200)    string    >\0    "%10.10s"
  84. blub
  85. }
  86.  
  87. [ "$1" = -r ] && shift
  88.  
  89. root_dir="$1"
  90. [ "$root_dir" ] || root_dir=/
  91.  
  92. tmp_initrd=$root_dir/tmp/initrd$$
  93.  
  94. if [ "$root_dir" != / ] ; then
  95.   LD_LIBRARY_PATH=$root_dir/usr/lib
  96.   PATH=$root_dir/usr/bin:$PATH
  97.   [ -x "$root_dir$static_shell" ] && static_shell="$root_dir$static_shell"
  98.   [ -x "$root_dir$static_insmod" ] && static_insmod="$root_dir$static_insmod"
  99. fi
  100.  
  101. x1=`mount 2>/dev/null | grep "on $root_dir "`
  102. x2=`echo \`echo "$x1" | wc -l\``
  103. x3=`echo "$x1" | cut -f 1 -d " "`
  104.  
  105. if [ -z "$rootdev" ] ; then
  106.     [ "$x2" = 1 -a -b "$x3" ] && rootdev="$x3"
  107. fi
  108.  
  109. [ "$rootdev" ] || error 1 "usage: mk_initrd [root_dir]"
  110.  
  111. echo "using \"$rootdev\" as root device (mounted on \"$root_dir\")"
  112.  
  113. [ -f "$root_dir/etc/rc.config" ] && . $root_dir/etc/rc.config
  114.  
  115. modules="$INITRD_MODULES"
  116.  
  117. ( cd $root_dir$boot_dir ; rm -f $initrds )
  118. [ "$modules" ] || error 0 "no initrd required"
  119.  
  120. initrd_a=($initrds)
  121.  
  122. file=file
  123. [ -x /usr/bin/file ] && file=/usr/bin/file
  124. [ -x $root_dir/usr/bin/file ] && file=$root_dir/usr/bin/file
  125.  
  126. kernel_idx=0
  127. for k in $kernels ; do
  128.  
  129.   kk="$root_dir$boot_dir/$k"
  130.  
  131.   if [ -f "$kk" ] ; then
  132.  
  133.     write_magic
  134.  
  135.     vv=`$file -m $tmp_magic $kk | cut -d \" -f 2`
  136.  
  137.     ii="$root_dir$boot_dir/${initrd_a[$kernel_idx]}"
  138.  
  139.     echo
  140.     echo "creating initrd \"$ii\" for kernel \"$kk\" ($vv)"
  141.  
  142.     [ -d "$root_dir/lib/modules/$vv/misc" ] || error 2 "no version \"$vv\" modules found"
  143.  
  144.     mkdir $tmp_mnt
  145.  
  146.     dd if=/dev/zero of=$tmp_initrd bs=1k count=$image_blocks 2>/dev/null
  147.     mke2fs -q -F -b 1024 -m 0 -N $image_inodes $tmp_initrd 2>/dev/null
  148.     tune2fs -i 0 $tmp_initrd >/dev/null 2>&1
  149.  
  150.     mount -oloop $tmp_initrd $tmp_mnt 2>/dev/null || { 
  151.       if [ -f /lib/loop.o ] ; then
  152.         insmod /lib/loop.o
  153.         mount -oloop $tmp_initrd $tmp_mnt 2>/dev/null || {
  154.           error 3 "failed to mount image"
  155.         }
  156.       else
  157.         error 3 "failed to mount image"
  158.       fi
  159.     }
  160.     is_mounted=1
  161.  
  162.     rmdir $tmp_mnt/lost+found
  163.     mkdir $tmp_mnt/{bin,dev}
  164.     cp -a $root_dir/dev/{tty1,tty2,zero,null,ram0,ram1,ram2,ram,ramdisk,fb0,console} $tmp_mnt/dev
  165.     cp $static_shell $tmp_mnt/bin/sh 2>/dev/null || error 4 "no static shell"
  166.     cp $static_insmod $tmp_mnt/bin/insmod 2>/dev/null || error 5 "no static insmod"
  167.  
  168.     echo '#! /bin/sh' >$lx_rc
  169.     echo >>$lx_rc
  170.     echo "export PATH=/bin" >>$lx_rc
  171.     echo >>$lx_rc
  172.  
  173.     chmod 755 $lx_rc
  174.  
  175.     for i in $modules; do
  176.       x=`cd $root_dir ; find lib/modules/$vv/ -name $i.o`
  177.       if [ "$x" ] ; then
  178.         echo "module $i is \"/$x\""
  179.         tar -C $root_dir -cf - $x 2>/dev/null | tar -C $tmp_mnt -xpf - 2>/dev/null
  180.         if [ $? != 0 ] ; then
  181.           echo "failed to add module \"/$x\""
  182.           error 6 "initrd too small"
  183.         fi
  184.         modparms=`grep "^[     ]*options $i" "$root_dir"/etc/modules.conf`
  185.         modparms="${modparms#* * }"
  186.         echo "echo \"Loading module $i $modparms ...\"" >>$lx_rc
  187.         echo "-> insmod $i $modparms"
  188.         echo "insmod $i $modparms" >>$lx_rc
  189.         echo >>$lx_rc
  190.       else
  191.         error 7 "no such module: $i"
  192.       fi
  193.     done
  194.  
  195.     umount $tmp_mnt
  196.     is_mounted=
  197.  
  198.     gzip -9 $tmp_initrd
  199.  
  200.     cp $tmp_initrd.gz $ii || error 8 "failed to install initrd"
  201.  
  202.     kernel_idx=$((kernel_idx+1))
  203.  
  204.     clean_up
  205.  
  206.   else
  207.  
  208.     echo "no kernel image \"$k\""
  209.  
  210.   fi
  211.  
  212. done
  213.  
  214.