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,且分区为20M的sdb1,512M的sdb2;
3).另建立一台测试虚拟机,将原有的硬盘删除,再将硬盘/dev/hda的新增到该测试虚拟机;
4).系统服务启动时显示开启状态;
5).新增启动网络模块;
6).新增内核控制模块;
1.单用户模式无根文件系统无写入权限时使用该命令改为可读写
mount -n -o remount,rw /
2.单用户模式启用关机命令,(复制halp boot相关命令用户库文件 )
[root@test ~]# cat /mnt/sysroot/etc/rc.d/rc.sysdone #加可执行权限
#!/bin/bash
sync
sleep 2
sync
exec /sbin/halt -p
[root@test ~]# cat /mnt/sysroot/etc/inittab#加可执行权限
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc.sysdone
3.单用户模式启用重启命令
[root@test ~]# cat /mnt/sysroot/etc/rc.d/rc.sysreboot #加可执行权限
#!/bin/bash
sync
sleep 2
sync
exec /sbin/reboot
[root@test ~]# 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)
[root@test 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
[root@test sysroot]# chmod +x etc/rc.d/init.d/halt
[root@test 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
[root@test rc.d]# ls /mnt/sysroot/etc/rc.d
init.d rc.reboot rc.sysdone rc.sysinit rc.sysreboot
[root@test rc.d]# ls init.d/
halt
[root@test rc0.d]# ln -sv ../init.d/halt S99halt
create symbolic link `S99halt‘ to `../init.d/halt‘
[root@test rc6.d]# ln -sv ../init.d/halt S99reboot
create symbolic link `S99reboot‘ to `../init.d/halt‘
[root@test 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
[root@test 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
[root@test 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
[root@test 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).新增新增内核控制模块;新增内核控制模块;
[root@test 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
[root@test sysroot]# cat etc/sysconfig/network
HOSTNAME=miniLinux.jack.com
[root@test 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的链接
[root@miniLinux bin]# pwd
/mnt/sysroot/bin
[root@miniLinux bin]# ln -sv ./bash sh
[root@test ~]# 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
以下图片中系统文件错乱时修复方法:打包所有文件后格式化该盘再恢复文件
[root@test sysroot]# cd /mnt/sysroot/
[root@test sysroot]# ls
[root@test sysroot]# find . | cpio -H newc --quiet -o |gzip -9 > /root/sysroot.gz #打包
[root@test ~]# umount /dev/hda2
[root@test ~]# fuser -km /dev/hda2
[root@test ~]# umount /dev/hda2
[root@test ~]# mke2fs -j /dev/hda2
[root@test ~]#mount /dev/hda2 /mnt/sysroot/
[root@test sysroot]# zcat /root/sysroot.gz |cpio -id #恢复
[root@test sysroot]# ls
bin boot dev etc halt home lib lost+found media mnt opt proc root sbin sys tmp usr var
[root@test sysroot]#vim etc/rc.d/init.d/functions #被调用的函数 (sysroot2.gz)
#
SCREEN=`stty -F /dev/console size`
COLUMNS=${SCREEN#* }
SPA_COL=$[$COLUMNS-12]
RED=‘\033[1;31m‘
GREEN=‘\033[32m‘
YELLOW=‘\033[33m‘
BLUE=‘\033[34m‘
NORMAL=‘\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${NORMAL} ]"
}
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}Failure${NORMAL} ]"
}
[root@test 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)
[root@test sysroot]# modinfo mii #加载网卡模块PCNET32
[root@test sysroot]# cp /lib/modules/2.6.18-164.el5PAE/kernel/drivers/net/mii.ko lib/modules
[root@test sysroot]# modinfo pcnet32
[root@test sysroot]# cp /lib/modules/2.6.18-164.el5PAE/kernel/drivers/net/pcnet32.ko lib/modules
[root@test ~]# modinfo e1000 #加载网卡模块e1000
[root@test sysroot]# cp /lib/modules/2.6.18-164.el5PAE/kernel/drivers/net/e1000/e1000.ko lib/modules
[root@test 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
[root@test 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 device" || failure "Activating loopback network device"
以下图片中启动报错,查找N小时才找到的问题是:将目录el5写错成e15
6).新增内核控制模块 (sysroot.kernel.4.gz)
[root@test ~]# 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
[root@test ~]# cat /mnt/sysroot/etc/sysctl.conf
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
[root@test ~]#cat /proc/sys/net/ipv4/ip_forward
1
[root@test 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格式
----end--
原文地址:http://wangfx.blog.51cto.com/1697877/1876845