Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1143541
  • 博文数量: 234
  • 博客积分: 5592
  • 博客等级: 大校
  • 技术积分: 1987
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-23 14:12
文章分类

全部博文(234)

文章存档

2015年(1)

2013年(4)

2012年(16)

2011年(204)

2010年(9)

分类: LINUX

2011-01-20 11:07:58

系统初始化的脚本

       上面说到rcS文件时,涉及到了/etc/init.d/rc.nic,/etc/init.d/rc.network,/etc/init.d/rc.usb,/etc/init.d/rc.local四个初始化脚本文件。下面来说说它们的作用。

5.7.1   /etc/init.d/rc.nic

此文件用来自动加载网卡对应的模块(驱动),并且删除没有用到的网卡驱动模块。

#!/bin/sh

#

# detect network cards shell 

#

dmesg  -n 1  #prevent  the messages from appearing on the console

ERR_DEV=/dev/null                # redirect verbose messages

NetcardPath=/lib/modules/2.4.20-8custom/kernel/drivers/net/

loaded=4  #the kernel already detected cardyou may want to change it

           #according to file `/proc/net/dev`

# invoked by the init script or by the user?

[ "`basename $0`" = "rc.nic" ] && MODE=init || MODE=user

 

# number of NICs already detected

NICs=$((`cat /proc/net/dev | wc -l` -$loaded))

 

# when init, skip auto probing if we already found some NIC(s)

if [ $NICs -ge 1 -a "$MODE" = "init" ]; then

  echo "Found $NICs network card(s). Skip auto probing …"

  exit 0

fi

 

# official NIC modules

NET_MODs=`find /lib/modules/\`uname -r\`/kernel/drivers/net/    \

                -type f -printf "%f\n" 2>$ERR_DEV               \

        | cut -d "." -f1`

# save the original dmesg messages first

[ -e /var/log/dmesg ] || dmesg > /var/log/dmesg

 

echo "Probing network cards … "

CUR_NICs=$NICs

for i in $NET_MODs ; do

  ORG_MODs=`cat /proc/modules | wc -l`

  ORG_NICs=$CUR_NICs

 

  echo -en "\r\t$i        \t"

  #if  modprobe fail we think that this module is not needed

  #so we delete it to save the space

  if ! modprobe $i 1>$ERR_DEV 2>$ERR_DEV

  then

        temp=`find  $NetcardPath  -name $i.o`

        if [ -f $temp ]

        then

                rm  -rf $temp

        fi

  fi

 

  CUR_MODs=`cat /proc/modules | wc -l`

  CUR_NICs=$((`cat /proc/net/dev | wc -l` -$loaded))

 

  if [ $CUR_NICs -eq $ORG_NICs ]; then  

if [ $CUR_MODs -gt $ORG_MODs ]; then  #it  is not a netcard module

                                                                             #so we delete it     

      RMMODs=`head -$(($CUR_MODs-$ORG_MODs)) /proc/modules | cut -d" " -f1`

      for m in $RMMODs ; do

        rmmod $m 1>$ERR_DEV 2>$ERR_DEV

      done

    fi

  else

    echo " ($(($CUR_NICs-$ORG_NICs)))"

  fi

done

echo -e "\r                       \rFound $(($CUR_NICs-$NICs)) card(s), done."

rmmod -as

exit $(($CUR_NICs-$NICs))

看懂这个脚本就需要你熟悉shell编程了。由于比较大,我这里不作解释。

这个脚本的内容大部分是从cdlinux上面copy过来的,但是对其进行了修改,其中最大的修改就是添加了删除没有用到的模块的功能,这主要是为了节省空间所用。

      

5.7.2   /etc/init.d/rc.network

       这个脚本主要是初始化网络配置,

#!/bin/sh

#/sbin/ifconfig eth1    218.199.20.98  up

/sbin/ifconfig eth0    192.168.0.3   up

/sbin/ifconfig lo      127.0.0.1     up

route add default gw  192.168.0.1  dev eth0

这个脚本需要根据不同的环境进行修改。接触过linux的人相信这个不难看懂。

5.7.3   /etc/init.d/rc.usb

加载usb驱动模块,当然如果你的内核是静态编译进usb模块的,那就没有必要在这里多此一举了,不过如果我们要做个网关服务器,还是把它做成动态的模块比较好,因为网关服务器基本上不需要用到u盘,我们大可删除掉usb驱动模块,以节省空间。我们之所以需要usb的驱动模块,不要忘了我们的usbinux是放在u盘上面运行的。当然并不是说要在u盘上面运行linux,一定需要内核支持usb才行,不管是硬盘,u盘,还是软盘都只是个载体,第一章已经说了,我们的文件系统是在ramdisk中的,因此只要内核和文件系统被载入内存,我们就不再需要载体(存储设备)。但是我们很多东西可以放在u盘上面,以节省ramdisk的存储空间,所以还是需要内核识别u盘,等系统启动之后再把需要的东西从u盘拷贝到ram里面。

   rc.usb内容如下: 

#!/bin/sh

##            This script is to initilize usb controller and

##                  The driver module usb-storage

##            To use usb under linux the module usbcore,scsi_mod

##                  and sd_mod are needed,in this

##                  system they are complied in kernel

 

#/sbin/usb is the small script to start or stop  usb support

/sbin/usb start

#script  rc.usb  end

这个脚本里面用到了/sbin/usb这个程序,其实并不是一个真正的程序,它是我写的一个加载和卸载usb模块的shell脚本。/sbin/usb脚本内容如下:

#!/bin/sh

#

#A simple startup script to start usb for linux

#

case "$1" in

start)

         /sbin/modprobe usbcore

              /sbin/modprobe usb-uhci

              /sbin/modprobe usb-storage

              ;;

stop)

      /sbin/rmmod    usbcore

              /sbin/rmmod    usb-uhci

              /sbin/rmmod    usb-storage

              ;;

        *)

               echo "Usage: $0 {start|stop}"

               exit 1

        esac

#script /sbin/usb  end

我之所以写了这个usb脚本,是因为我发现,启动usblinux后,当机子用过一个u盘时,即使这个uumount掉了,再使用另外一个u盘的时候就会出现问题。也许是我的哪边配置有误,不过不管怎么说,这个脚本还是有用的。

注意:虽然上面只列出了,usbcoreusb-uhci,usb-storage三个模块,要能够成功挂载u盘,还需要scsi的支持,也就是需要scsi_mod.o  sd_mode.o两个模块,我的内核是把这两个模块静态编译进内核的,因此也就不需要再手动加载了。我之所以把对scsi支持的功能静态编译进内核,是为了测试方便的原故,我测试工具使用的是vmware虚拟机,因为vmware基本上使用scsi硬盘,为了避免添加linuxrc文件,我把vmware的硬盘驱动BusLogic.o静态编译进了内核,所以上面的scsi的两个模块也要编译进内核。

5.7.4   /etc/init.d/rc.local

这个shell脚本没有什么好说的,主要是初始化本地的一些服务,比如sshdsyslogd等等

我的rc.local脚本内容如下:

    #/bin/sh

       #/sbin/sshd

/sbin/syslogd

在本地启动syslogd服务。


阅读(1229) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~