前言
在上一篇博客中(http://freeloda.blog.51cto.com/2033581/1221075),我们添加了关机与重启功能,给主机添加了主机名,也实现在 3 级级别下运行 tserver 测试脚本,下面我们来说一下,主机增加网络功能 ^_^ ……
1. 定义系统启动过程中,显示服务启动信息的颜色脚本
从上图中我们看到,我们重新启动网卡时,右边会显示 [ ok ] 的绿色提示符,在上一博客中我们只简单的显示 tserver start …… 提示符现在不雅,下面我们就来修饰一下!
1)、编写 functions 脚本
[root@localhost ~]# vim functions #!/bin/bash # SCREEN=`stty -F /dev/console size 2>/dev/null` COLUN=${SCREEN#* } [ -z $COLUN ] && COLUN=80 SPA_COL=$[$COLUN-14] RED='\033[31m' GREEN='\033[32m' YELLOW='\033[33m' BLUE='\033[34m' RONMAL='\033[0m' success() { string=$1 RT_SPA=$[$SPA_COL-${#string}] echo -n "$string" for I in `seq 1 $RT_SPA`; do echo -n " " done echo -e "[ ${GREEN}OK${RONMAL} ]" } failure() { string=$1 RT_SPA=$[$SPA_COL-${#string}] echo -n "$string" for I in `seq 1 $RT_SPA`; do echo -n " " done echo -e "[ ${RED}FAILED${RONMAL} ]" }
2)、给脚本增加执行权限
[root@localhost ~]# chmod +x functions
3)、移植 seq 命令
[root@localhost ~]# sh addcom.sh Your command: seq /usr/bin/seq copy /usr/bin/seq finished.
4)、然后在tserver等服务脚本内调用 functions 脚本,即可实现显示服务启动的信息!
#!/bin/bash # # chkconfig: 35 55 44 # description: chris service # #调用脚本 . /etc/init.d/functions FILE=/var/lock/subsys/`basename $0` prog=`basename $0` start() { echo "starting `basename $0`..." touch $FILE [ $? -eq 0 ] && success "Starting $prog" || failure "Starting $prog" } stop() { echo "stopping `basename $0`..." rm -rf $FILE [ $? -eq 0 ] && success "Stopping $prog" || failure "Stopping $prog" } status() { if [ -e $FILE ]; then echo "Running.." else echo "stopping..." fi } usage() { echo "please insernt like start|stop|restart|status" } case $1 in start) start ;; stop) stop ;; restart) stop start ;; status) status ;; *) usage exit 1 ;; esac
5)、测试效果
开机效果:
关机效果:
有效果了吧,嘿嘿!下面我们来增加网络功能!
2. 增加网络功能
下面是重点,很多朋友没做成功都是这个原因!查看网卡信息,你自己的虚拟机支持的网卡与驱动。
具休查处方法如下:
1). 查看物理网卡的驱动信息
[root@localhost ~] # ethtool -i eth0 driver: e1000 version: 7.3.21-k8-NAPI firmware-version: bus-info: 0000:02:00.0
2). 查看网卡的物理硬件型号
[root@localhost ~] # lspci | grep -i ethernet 02:00.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
3). 查看该设备的模块信息
[root@localhost ~] # modinfo e1000 filename: /lib/modules/2.6.32-279.14.1.el6.i686/kernel/drivers/net/e1000/e1000.ko version: 7.3.21-k8-NAPI license: GPL description: Intel(R) PRO/1000 Network Driver author: Intel Corporation, <linux.nics@intel.com> srcversion: 1B96AEAA963BB7C0044BA00
案例如下:(我这里说两个例子,一个是RedHat 5.8 一个是CentOS 5.5)
ReadHat 5.8 环境
1)、 虚拟机的网卡
查找 pcnet32 模块
[root@localhost ~]# modinfo pcnet32 filename: /lib/modules/2.6.18-194.el5/kernel/drivers/net/pcnet32.ko license: GPL description: Driver for PCnet32 and PCnetPCI based ethercards author: Thomas Bogendoerfer srcversion: F81443556AAE169CBF80F55 alias: pci:v00001023d00002000sv*sd*bc02sc00i* alias: pci:v00001022d00002000sv*sd*bc*sc*i* alias: pci:v00001022d00002001sv*sd*bc*sc*i* depends: mii vermagic: 2.6.18-194.el5 SMP mod_unload gcc-4.1 parm: debug:pcnet32 debug level (int) parm: max_interrupt_work:pcnet32 maximum events handled per interrupt (int) parm: rx_copybreak:pcnet32 copy breakpoint for copy-only-tiny-frames (int) parm: tx_start_pt:pcnet32 transmit start point (0-3) (int) parm: pcnet32vlb:pcnet32 Vesa local bus (VLB) support (0/1) (int) parm: options:pcnet32 initial option setting(s) (0-15) (array of int) parm: full_duplex:pcnet32 full duplex setting(s) (1) (array of int) parm: homepna:pcnet32 mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet (array of int) module_sig: 883f3504bb6456c596239a507de7fd112a65a09f5faef1f984967c867e1149c0d1cfb295a3d944f009f528a8f96e85f55b72b3eb94966d22deef2fa7ea2 [root@localhost ~]#
查找 mii 模块
[root@localhost ~]# modinfo mii filename: /lib/modules/2.6.18-194.el5/kernel/drivers/net/mii.ko license: GPL description: MII hardware support library author: Jeff Garzik <jgarzik@pobox.com> srcversion: 16DCEDEE4B5629C222C352D depends: vermagic: 2.6.18-194.el5 SMP mod_unload gcc-4.1 module_sig: 883f3504bb6456c596239a507de7fd112e5b09f55847ebc59e54b97cff863c8097bf32f8f320e90a0a5a1b8a45b5f58a468d592bde47be53a7ca7f8 [root@localhost ~]#
2)、创建相应目录
[root@localhost ~]# cd /mnt/sysroot/ [root@localhost sysroot]# ls ./ lib/ lib64/ [root@localhost sysroot]# mkdir ./lib/modules
3)、复制相应的模块
[root@localhost sysroot]# cp -p /lib/modules/2.6.18-194.el5/kernel/drivers/net/pcnet32.ko ./lib/modules/ [root@localhost sysroot]# cp -p /lib/modules/2.6.18-194.el5/kernel/drivers/net/mii.ko ./lib/modules/ [root@localhost sysroot]# ls ./lib/modules/ mii.ko pcnet32.ko [root@localhost sysroot]#
4)、修改 rc.sysinit 文件
[root@localhost sysroot]# vim ./etc/rc.d/rc.sysinit
#增加网络功能模块
echo "insmod pcnet32 mode" /sbin/insmod /lib/modules/mii.ko /sbin/insmod /lib/modules/pcnet32.ko
5)、移植相关命令
[root@localhost ~]# sh addcom.sh Your command: insmod /sbin/insmod copy /sbin/insmod finished.
6)、增加网卡配置文件
[root@localhost ~]# mkdir -p /mnt/sysroot/etc/sysconfig/network-scripts [root@localhost ~]# vim /mnt/sysroot/etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static IPADDR=192.168.18.10 NETMASK=255.255.255.0 GATEWAY=192.168.18.254 ONBOOT=yes :wq
7)、创建相对应脚本
[root@localhost ~]# vim /mnt/sysroot/etc/init.d/network #!/bin/bash # # chkconfig: 35 09 90 # description:network servce . /etc/rc.d/init.d/functions CONF=/etc/sysconfig/network-scripts/ifcfg-eth0 . $CONF start () { NETMASK=16 ifconfig eth0 $IPADDR/$NETMASK up [ -z $GATEWAY ] && route add default gw $GATEWAY } stop() { ifconfig eth0 down } status () { ifconfig eth0 } usage() { ehco "like start,stop,restart" } case $1 in start) start success "config network eth0 " ;; stop) stop success "stop network eth0" ;; restart) stop Start success "restart network eth0" ;; status) status ;; *) usage ;; esac
8)、增加执行权限
[root@localhost ~]# chmod +x /mnt/sysroot/etc/init.d/network
9)、移植 ifconfig 命令
[root@localhost ~]# sh addcom.sh Your command: ifconfig /sbin/ifconfig copy /sbin/ifconfig finished.
10)、创建软链接
[root@localhost rc0.d]# ln -sv ../init.d/network K90network create symbolic link `K90network' to `../init.d/network' [root@localhost rc0.d]# ls K44tserver K90network S99halt [root@localhost rc6.d]# ln -sv ../init.d/network K90network create symbolic link `K90network' to `../init.d/network' [root@localhost rc6.d]# ls K44tserver K90network S99reboot [root@localhost rc3.d]# ln -sv ../init.d/network S09network create symbolic link `S09network' to `../init.d/network' [root@localhost rc3.d]# ls S09network S55tserver
11)、测试效果如下
CentOS 5.5 环境,这个是我自己测试的环境,照着做的朋友请注意!与上面不同的是,我这里的网卡和驱动如下图,
1)、查看网卡与驱动类型
2)、复制相关驱动到 /lib/modules 下如下图,
3)、修改 rc.sysinit 文件,如下图
4)、移植相关命令
[root@localhost ~]# sh addcom.sh Your command: insmod /sbin/insmod copy /sbin/insmodfinished.
5)、增加网卡配置文件
[root@localhost ~]# mkdir -p /mnt/sysroot/etc/sysconfig/network-scripts [root@localhost ~]# vim /mnt/sysroot/etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static IPADDR=192.168.18.10 NETMASK=255.255.255.0 GATEWAY=192.168.18.254 ONBOOT=yes :wq
6)、创建相对应脚本
[root@localhost ~]# vim /mnt/sysroot/etc/init.d/network #!/bin/bash # # chkconfig: 35 09 90 # description:network servce . /etc/rc.d/init.d/functions CONF=/etc/sysconfig/network-scripts/ifcfg-eth0 . $CONF start () { NETMASK=16 ifconfigeth0 $IPADDR/$NETMASK up [ -z $GATEWAY ] && route add default gw $GATEWAY } stop() { ifconfigeth0 down } status () { ifconfigeth0 } usage() { ehco "like start,stop,restart" } case$1 in start) start success "config network eth0 " ;; stop) stop success "stop network eth0" ;; restart) stop Start success "restart network eth0" ;; status) status ;; *) usage ;; esac
7)、增加执行权限
[root@localhost ~]# chmod +x /mnt/sysroot/etc/init.d/network
8)、移植 ifconfig 命令
[root@localhost ~]# sh addcom.sh Your command: ifconfig /sbin/ifconfig copy /sbin/ifconfigfinished.
9)、创建软链接
[root@localhost rc0.d]# ln -sv ../init.d/network K90network create symbolic link `K90network' to `../init.d/network' [root@localhost rc0.d]# ls K44tserver K90network S99halt [root@localhost rc6.d]# ln -sv ../init.d/network K90network create symbolic link `K90network' to `../init.d/network' [root@localhost rc6.d]# ls K44tserver K90network S99reboot [root@localhost rc3.d]# ln -sv ../init.d/network S09network create symbolic link `S09network' to `../init.d/network' [root@localhost rc3.d]# ls S09network S55tserver
10)、测试效果如下
IP地址顺利的配置上去了,嘿嘿!
11)、相关操作演示如下
为lo配置IP地址,
ping 测试,
由于我的网卡是桥接的,下面我们测试一下,ping网关
所有测试结束,嘿嘿!有兴趣的朋友可以尝试的做一下!
转载于:https://blog.51cto.com/freeloda/1221293