linux命令:系统裁剪之二 insmod装载模块
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux命令:系统裁剪之二 insmod装载模块相关的知识,希望对你有一定的参考价值。
Linux系统裁剪笔记之二
1.什么裁剪?
本篇文章的主要目的是让笔者和读者更深的认识Linux系统的运作方式,大致内容就是把Linux拆开自己一个个组件来组装,然后完成一个微型的Linux系统.下面,让我们来实现吧..写的不好的地方请指教.
2.原理
大家都知道,操作系统的启动流程是(主要是Linux):POST—>Bios—>MBR—kernel-->initrd-->sbin/init,
POST,BIOS都不是我们管的,所以这里我们从MBR开始,Linux的系统引导先主要是用的grub这个软件,grub引导系统了,然后启动内核,内核调用initrd来实现最基本的操作系统,
3.实验 (所有操作均在虚拟机上实现,且定义硬盘时选IDE)
环境:1).在宿主机上安装一台红帽子Linux5.4,/dev/sda上安装操作系统;
2).在宿主机上新建一个新硬盘:/dev/hda,且分区为512M的sdb1,1024 M的sdb2;
3).另建立一台测试虚拟机,将原有的硬盘删除,再将硬盘/dev/hda的新增到该测试虚拟机;
系统裁剪之一,已经完成了一个单一系统的启动,接下来需要达到以下功能模块的装载:
1、实现关机和重启
2、实现能够修改主机名
3、运行对应服务脚本
4、启动终端;(虚拟终端或图形终端)
5、运行用户,可以通过用户登录增加删减用户;
6、定义单用户维护模式
7、装载网卡驱动,启用网络功能;
8、提供一个web服务器
1.单用户模式无根文件系统无写入权限时使用该命令改为可读写
mount -n -o remount,rw / 挂载时不更新/etc/mtab文件;
2.单用户模式启用关机命令,(复制halp boot相关命令用户库文件 )
[[email protected] ~]# cat /mnt/sysroot/etc/rc.d/rc.sysdone #加可执行权限
#!/bin/bash
sync
sleep 2
sync
exec /sbin/halt -p
[[email protected] ~]# cat /mnt/sysroot/etc/inittab#加可执行权限
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc.sysdone #字母l数字0
3.单用户模式启用重启命令
[[email protected] ~]# cat /mnt/sysroot/etc/rc.d/rc.sysreboot #加可执行权限
#!/bin/bash
sync
sleep 2
sync
exec /sbin/reboot
[[email protected] ~]# cat /mnt/sysroot/etc/inittab#加可执行权限
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc.sysdone
l6:6:wait:/etc/rc.d/rc.sysreboot
3.单用户模式启用关机和重启命令 (sysroot1.gz)
[[email protected] sysroot]# vim /mnt/sysroot/etc/rc.d/init.d/halt
#!/bin/bash
case $0 in
*reboot)
COMMAND=‘/sbin/halt -p‘ ;;
*halt)
COMMAND=‘/sbin/reboot‘ ;;
*)
echo "Only call this script by *reboot OR *halt;"
esac
case $1 in
start)
;;
stop)
;;
*)
echo "Usage : `basename $0` {start|stop}";;
esac
exec $COMMAND
[[email protected] sysroot]# chmod +x etc/rc.d/init.d/halt
[[email protected] sysroot]# ll etc/rc.d/init.d/halt
-rwxr-xr-x 1 root root 259 Nov 24 14:23 etc/rc.d/init.d/halt
[[email protected] rc.d]# ls /mnt/sysroot/etc/rc.d
init.d rc.reboot rc.sysdone rc.sysinit rc.sysreboot
[[email protected] rc.d]# ls init.d/
halt
[[email protected] rc0.d]# ln -sv ../init.d/halt S99halt
create symbolic link `S99halt‘ to `../init.d/halt‘
[[email protected] rc6.d]# ln -sv ../init.d/halt S99reboot
create symbolic link `S99reboot‘ to `../init.d/halt‘
[[email protected] rc.d]# vim rc
#!/bin/bash
#
for I in /etc/rc.d/rc$RUNLEVEL.d/K*; do
$I stop
done
for I in /etc/rc.d/rc$RUNLEVEL.d/S*; do
$I start
done
[[email protected] etc]#vim /mnt/sysroot/etc/inittab
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l6:6:wait:/etc/rc.d/rc 6
[[email protected] etc]#vim /mnt/sysroot/etc/rc.d/init.d/tserver
#!/bin/bash
#chkconfig: 35 77 22
#description: test service script
. /etc/rc.d/init.d/functions
prog=tserver
lockfile=/var/lock/subsys/$prog
start(){
touch $lockfile
[ $? -eq 0 ] &&success "starting $prog" || failure "Starting $prog"
}
stop(){
rm -f $lockfile
[ $? -eq 0 ] &&success "stopping $prog" || failure "Stopping $prog"
}
status(){
if [ -f $lockfile ];then
echo "Running..."
else
echo "Stopped..."
fi
}
usage() {
echo "Usage:$prog {start|stop|status|restart}"
}
case $1 in
start)
start ;;
stop)
stop ;;
restart)
stop
start ;;
status)
status ;;
*)
usage
exit 1 ;;
esac
[[email protected] sysroot]# vim /mnt/sysroot/etc/fstab
/dev/hda2 / ext3 defaults 0 0
/dev/hda1 /boot ext3 defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
4).新增新增内核控制模块;新增内核控制模块;
[[email protected] sysroot]# vi /mnt/sysroot/etc/rc.d/rc.sysinit
#!/bin/bash
#
echo -e "Welcome to \033[34mJack\033[0m Linux xxxxx"
echo "Remount rootfs..."
mount -n -o remount,rw /
echo "Set the hostname..."
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
[ -z $HOSTNAME -o "HOSTNAME" == ‘(none)‘ ] && HOSTNAME=localhost
/bin/hostname $HOSTNAME
[[email protected] sysroot]# cat etc/sysconfig/network
HOSTNAME=miniLinux.jack.com
[[email protected] sysroot]# chroot /mnt/sysroot/ #小系统中验证是否有问题
bash-3.2# /etc/rc.d/rc.sysinit
Welcome to Jack Linux xxxxx
Remount rootfs...
Set the hostname...
bash-3.2# exit
以下图中报错是缺少sh的链接
[[email protected] bin]# pwd
/mnt/sysroot/bin
[[email protected] bin]# ln -sv ./bash sh
[[email protected] ~]# vim /mnt/sysroot/etc/inittab
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l3:3:wait:/etc/rc.d/rc 3
l6:6:wait:/etc/rc.d/rc 6
1:2345:respawn:/sbin/agetty -n -l /bin/bash 38400 tty1
2:2345:respawn:/sbin/agetty -n -l /bin/bash 38400 tty2
3:2345:respawn:/sbin/agetty -n -l /bin/bash 38400 tty3
以下图片中系统文件错乱时修复方法:打包所有文件后格式化该盘再恢复文件
[[email protected] sysroot]# cd /mnt/sysroot/
[[email protected] sysroot]# ls
[[email protected] sysroot]# find . | cpio -H newc --quiet -o |gzip -9 > /root/sysroot.gz #打包
[[email protected] ~]# umount /dev/hda2
[[email protected] ~]# fuser -km /dev/hda2
[[email protected] ~]# umount /dev/hda2
[[email protected] ~]# mke2fs -j /dev/hda2
[[email protected] ~]#mount /dev/hda2 /mnt/sysroot/
[[email protected] sysroot]# zcat /root/sysroot.gz |cpio -id #恢复
[[email protected] sysroot]# ls
bin boot dev etc halt home lib lost+found media mnt opt proc root sbin sys tmp usr var
[[email protected] sysroot]#vim etc/rc.d/init.d/functions #被调用的函数 (sysroot2.gz)
#
SCREEN=`stty -F /dev/console size`
COLUMNS=${SCREEN#* }
let "SPA_COL=$COLUMNS-12"
RED=‘\033[1;31m‘
GREEN=‘\033[32m‘
YELLOW=‘\033[33m‘
BLUE=‘\033[34m‘
NORMAL=‘\033[0m‘
success(){
string=$1
let "RT_SPA=$SPA_COL-${#string}"
echo -n "$string"
for I in `seq 1 $RT_SPA` ;do
echo -n " "
done
echo -e "[ ${GREEN}OK${NORMAL} ]"
}
failure(){
string=$1
let "RT_SPA=[$SPA_COL-${#string}"
echo -n "$string"
for I in `seq 1 $RT_SPA` ;do
echo -n " "
done
echo -e "[ ${RED}Failure${NORMAL} ]"
}
[[email protected] sysroot]# vim etc/rc.d/rc.sysinit
#!/bin/bash
#
echo -e "Welcome to \033[1;31mJack\033[0m Linux xxxxx"
echo "Remount rootfs..."
mount -n -o remount,rw /
echo "Set the hostname..."
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
[ -z $HOSTNAME -o "HOSTNAME" == ‘(none)‘ ] && HOSTNAME=localhost
/bin/hostname $HOSTNAME
echo "Initalizing network device ..."
/sbin/insmod /lib/modules/mii.ko
/sbin/insmod /lib/modules/pcnet32.ko
/sbin/insmod /lib/modules/e1000.ko
5).新增启动网络模块,请确认网卡型号 (sysroot.netmod.3.gz)
[[email protected] sysroot]# modinfo mii #加载网卡模块PCNET32
[[email protected] sysroot]# cp /lib/modules/2.6.18-164.el5PAE/kernel/drivers/net/mii.ko lib/modules
[[email protected] sysroot]# modinfo pcnet32
[[email protected] sysroot]# cp /lib/modules/2.6.18-164.el5PAE/kernel/drivers/net/pcnet32.ko lib/modules
[[email protected] ~]# modinfo e1000 #加载网卡模块e1000
[[email protected] sysroot]# cp /lib/modules/2.6.18-164.el5PAE/kernel/drivers/net/e1000/e1000.ko lib/modules
[[email protected] init.d]# vim /mnt/sysroot/etc/rc.d/init.d/network
#!/bin/bash
#
# chkconfig:35 09 90
# description: network service
prog=network
. /etc/rc.d/init.d/functions
CONF=/etc/sysconfig/network-scripts/ifcfg-eth0
. $CONF
NETMASK=22
start() {
ifconfig eth0 $IPADDR/$NETMASK up
[ -z $GATEWAY ] && route add default gw $GATEWAY
return 0
}
stop() {
ifconfig eth0 down
}
status() {
ifconfig eth0
}
usage() {
echo "$prog: {start|stop|restart|status|"
}
case $1 in
start)
start
success "Config network eth0." ;;
stop)
stop
success "Stop network card eth0." ;;
restart)
stop
start
success "Restart network card eth0" ;;
status)
status ;;
*)
usage
exit 1 ;;
esac
[[email protected] sysroot]# vi /mnt/sysroot/etc/rc.d/rc.sysinit
#!/bin/bash
#
. /etc/rc.d/init.d/functions
echo -e "Welcome to \033[1;31mJack\033[0m Linux xxxxx"
echo "Remount rootfs..."
mount -n -o remount,rw /
[ $? -eq 0 ] && success "Remount rootfs" || failure "Remount rootfs"
mount -a
[ $? -eq 0 ] && success "Mount others filesystem" || failure "Mount others filesystem"
#grep -v "\<swap|proc|sysfs\>" /etc/fstab |awk ‘{print $1}‘ |while read LINE; do awk ‘{print $1}‘ /proc/mounts |grep "^$LINE$"; done
echo "Set the hostname..."
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
[ -z $HOSTNAME -o "HOSTNAME" == ‘(none)‘ ] && HOSTNAME=localhost
/bin/hostname $HOSTNAME
[ $? -eq 0 ] && success "Set the hostname" || failure "Set the hostname"
echo "Initalizing network device ..."
/sbin/insmod /lib/modules/e1000.ko
[ $? -eq 0 ] && success "Initializing network device" || failure "Initializing network device"
ifconfig lo 127.0.0.1/8
[ $? -eq 0 ] && success "Activating loopback network" || failure "Activating loopback network"
ifconfig eth0 10.119.131.208/22 up
[ $? -eq 0 ] && success "Activating eth0 network" || failure "Activating eth0 network"
以下图片中启动报错,查找N小时才找到的问题是:将目录el5写错成e15
6).新增内核控制模块 (sysroot.kernel.4.gz)
[[email protected] ~]# vi /mnt/boot/grub/grub.conf #加上quiet开机时将不加输出详细信息
default=0
timeout=5
title Jack Linux (2.6.18)
root(hd0,0)
kernel /vmlinuz ro root=/dev/hda2 quiet
initrd /initrd.gz
[[email protected] ~]# cat /mnt/sysroot/etc/sysctl.conf
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
[[email protected] ~]#cat /proc/sys/net/ipv4/ip_forward
1
[[email protected] sysroot]# cat etc/rc.d/rc.sysinit #加上以下两行,以开机时启动内核设置文件
...
sysctl -p &> /dev/null
[ $? -eq 0 ] && success "Set Kernel Parameter" || failure "Set Kernel Parameter"
附:vi中set命令
:syntax on #开启语法高亮显示
:set ai #让vi自动对齐
:set noai #取消自动对齐
:set showmatch #高亮显示
:set noshowmatch #不高亮显示
:set tabstop=4 #设置制表停止位(tabstop)的长度
:set shiftwidth=4 #当使用移动(shift)命令时移动的字符数
:set cindent #在C编码时,自动完成indent缩进内嵌代码块
:set smartindent #为C程序提供自动缩进
:set ff=unix #将文件格式转为unix格式
本文出自 “学linux历程” 博客,请务必保留此出处http://woyaoxuelinux.blog.51cto.com/5663865/1882001
以上是关于linux命令:系统裁剪之二 insmod装载模块的主要内容,如果未能解决你的问题,请参考以下文章