一、成功做好一个小Linux的前提准备
1、挂载一块10G的IDE磁盘,并划分两个分区:(分别为/dev/hda1 +20M、/dev/hda2 +512M)
# fdisk /dev/hda
# partprobe /dev/hda
# mke2fs -j /dev/hda1 格式化/dev/hda1
# mke2fs -j /dev/hda2 格式化/dev/hda2
# mkdir /mnt/{boot,sysroot} 创建两个目录
# mount /dev/hda1 /mnt/boot 把/dev/hda1挂载到/mnt/boot下
# mount /dev/hda2 /mnt/sysroot 把/dev/hda2挂载到/mnt/sysroot下
# mount 查看挂载情况
2、# cp /boot/vmlinuz-2.6.18-308.el5 /mnt/boot/vmlinuz 复制内核
# mkdir test 创建test目录
# cd test/ 切换到test目录下
# zcat /boot/initrd-2.6.18-308.el5.img | cpio -id
进入init并编辑、修改里面的内容(前面带#的为要注释掉的信息)
vim init 编辑脚本内容如下:
#!/bin/nash
mount -t proc /proc /proc
setquiet
echo Mounting proc filesystem
echo Mounting sysfs filesystem
mount -t sysfs /sys /sys
echo Creating /dev
mount -o mode=0755 -t tmpfs /dev /dev
mkdir /dev/pts
mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
mkdir /dev/shm
mkdir /dev/mapper
echo Creating initial device nodes
......
insmod /lib/mptbase.ko
echo "Loading mptscsih.ko module"
insmod /lib/mptscsih.ko
echo "Loading mptspi.ko module"
insmod /lib/mptspi.ko
echo "Loading libata.ko module"
insmod /lib/libata.ko
echo "Loading ata_piix.ko module"
insmod /lib/ata_piix.ko
#echo "Loading dm-mod.ko module"
#insmod /lib/dm-mod.ko
#echo "Loading dm-log.ko module"
#insmod /lib/dm-log.ko
#echo "Loading dm-mirror.ko module"
#insmod /lib/dm-mirror.ko
#echo "Loading dm-zero.ko module"
#insmod /lib/dm-zero.ko
#echo "Loading dm-snapshot.ko module"
#insmod /lib/dm-snapshot.ko
#echo "Loading dm-mem-cache.ko module"
#insmod /lib/dm-mem-cache.ko
#echo "Loading dm-region_hash.ko module"
#insmod /lib/dm-region_hash.ko
#echo "Loading dm-message.ko module"
#insmod /lib/dm-message.ko
#echo "Loading dm-raid45.ko module"
#insmod /lib/dm-raid45.ko
echo Waiting for driver initialization.
stabilized --hash --interval 1000 /proc/scsi/scsi
mkblkdevs
echo Scanning and configuring dmraid supported devices
echo Scanning logical volumes
lvm vgscan --ignorelockingfailure
echo Activating logical volumes
lvm vgchange -ay --ignorelockingfailure vol0
resume LABEL=SWAP-sda3
echo Creating root device.
mkrootdev -t ext3 -o defaults,ro /dev/hda2 (此处的/dev/hda2为修改后的内容)
echo Mounting root filesystem.
mount /sysroot
echo Setting up other filesystems.
setuproot
echo Switching to new root and running init.
switchroot
把上述文件归档压缩到/mnt/boot/目录下
find . | cpio -H newc --quiet -o | gzip -9 > /mnt/boot/initrd.gz
(newc指的是新规范的方式归档,支持更多的文件数量;--quiet指静默模式;-o指创建档案文件;gzip -9 指压缩的级别)
3、安装grub并配置grub下的文件
# grub-install --root-directory=/mnt /dev/hda
# ls /mnt/boot/ 检验boot下是否有grub
编辑grub下的配置文件
# vim /mnt/boot/grub/grub.conf 内容如下:
default=0
timeout=3
title Mylinux
root (hd0,0)
kernel /vmlinuz
initrd /initrd.gz
5、切换到/mnt/sysroot目录下,创建几个etc目录下的文件
# cd /mnt/sysroot 切换到/mnt/sysroot目录下
# mkdir etc/rc.d/init.d bin sbin proc dev lib root mnt media var/{log,run,lock/subys,tmp} usr/{bin,sbin,local} tmp home opt boot -pv 创建一些需要用到的目录
6、编辑etc/inittab文件
vim etc/inittab 内容如下:
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
7、编辑etc/rc.d/rc.sysinit文件
vim etc/rc.d/rc.sysinit 内容如下:
#!/bin/bash
#
echo -e "\tWelcome to \033[34mMylinux\033[0m system"
/bin/bash
# chmod +x etc/rc.d/rc.sysinit 为其添加执行权限
8、编辑bincp.sh文件
vim bincp.sh 内容如下:
#!/bin/bash
#
DEST=/mnt/sysroot
libcp() {
LIBPATH=${1%/*}
[ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH
[ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished."
}
bincp() {
CMDPATH=${1%/*}
[ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH
[ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH
for LIB in `ldd $1 | egrep -o "/.*lib(64){0,1}/[^[:space:]]{1,}"`; do
libcp $LIB
done
}
read -p "Your command: " CMD
until [ $CMD == 'q' ]; do
! which $CMD && echo "Wrong command" && read -p "Input againe:" CMD && continue
COMMAND=` which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"`
bincp $COMMAND
echo "copy $COMMAND finished."
read -p "Continue: " CMD
done
# ./bincp.sh 测试bincp.sh (测试时输入 init ls bash q)
# sync(同步几次)
9、关闭虚拟机,挂载一块IDE磁盘大小为10G(可以用虚拟机内以存在的)
新建一个虚拟机,即为小Linux系统
二、需要小Linux配合的一些工作
# ./bincp.sh 执行脚本后添加 touch mkdir rm mv cp cat mount umount vi vim chmod chown ping ifconfig insmod modprobe rmmod route halt reboot shutdown hostname sleep sync q 以便以下的操作
1、实现开机关机功能
help exec:切断电源能一并关闭父进程,exec是以当前bash直接替换为这个进程,以子进程替换父进程
# cd /mnt/sysroot 切换到/mnt/sysroot目录下
vim 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
# chmod +x etc/rc.d/init.d/halt 添加执行权限
# cd etc/rc.d
# mkdir rc0.d 创建rc0.d目录
# ln -sv ../init.d/halt S99halt 进入rc0.d目录创建链接
# cd rc6.d/
# ln -sv ../init.d/halt S99reboot 进入rc6.d目录创建链接
编辑vim etc/inittab文件
在配置文件内添加 :l0:0:wait:/etc/rc.d/rc 0
l6:6:wait:/etc/rc.d/rc 6
创建脚本:etc/rc.d/rc
vim etc/rc.d/rc 编辑内容如下:
RUNLEVEL =$1
#!/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
2、挂载文件系统
编辑/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
在/mnt/sysroot/et/rc.d/rc.sysinit下添加一些内容:
echo "Remount rootfs...."
mount -n -o remount,rw /
mount -a
3、function函数的添加
vim /etc/rc.d/init.d/functions 此脚本可用于控制服务脚本的信息显示,其内容如下:
SCREEN=`stty -F /dev/console size`
COLUMNS=${SCREEN#* }
SPA_COL=$[$COLUMNS-14]
RED='\033[31m'
GREEN='\033[31m'
YELLOW='\033[31m'
BLUE='\033[31m'
success() {
string=$1
RT_SPA=$[$SPA_COL-${#string}]
echo -n "String"
for I in `seq 1 $RT_SPA`;do
echo -n " "
done
echo -e "[ ${GERRN}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}FAILED${NORMAL} ]"
}
# chmod +x /mnt/sysroot/rc.d/init.d/functios 添加执行权限
让脚本变得更漂亮一些,可以修改成如下内容:
vim etc/rc.d/init.d/tserver 修改脚本内容:
# chkconfig: 35 66 33
# description: test service script
#!/bin/bash
#
./etc/rc.d/init.d/functions
prog=`basename $0`
lockfile=/var/lock/subsys/$prog
start() {
touch $lockfile
[ $? -eq 0 ] } && success "Starting $prog" ||failure "Staring $prog"
stop() {
echo "Stopping $prog..."
[ $? -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
chmod +x inin.d/tserver 添加执行权限
init.d/tserver start 测试
4、为系统添加主机名和IP
1、进行模块的移植
# cd /mnt/sysroot
# mkdir lib/moudules
# modinfo pcnet32 # modinfo mii
# cp /lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko /mnt/sysroot/modules
# cp /lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko /mnt/sysroot/modules
2、添加主机名,装载内核模块
vim etc/rc.d/rc.sysinit 编辑脚本内容如下:
#!/bin/bash
#
echo -e "\tWelcome to \033[34mMylinux\033[0m system"
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 "Initializing network device..."
/sbin/insmod /lib/modules/mii.ko
/sbin/insmod /lib/modules/pcent32.ko
3、定义IP地址
# mkdir -pv /mnt/sysroot/etc/sysconfig 创建目录
# vim etc/sysconfig/network-scripts/ifcfg-etho 修改配置文件内容如下:
EDVICE=eth0
BOOTPROTO=static
IPADDR=172.16.59.1
NETMASK=255.255.0.0
GATEWAY=172.16.0.1
ONBOOT=yes
4、编辑脚本来实现IP的添加
vim etc/rc.d/init.d/network 内容如下:
#!/bin/bash
#
# chkconfig: 35 09 90 启动级别是09 关闭级别是90
# description: network service
prog=network
./etc/rc.d/init.d/functions
CONF=/etc/sysconfig/network-scripts/ifcfg-eth0
. $CONF
start() {
ifconfig etho $IPADDR/$NETWORK
[ -z $GATEWAY ] && route adddefault gw $GATEWAY
}
stop() {
ifconfig eth0
}
usage() {
echo "$prog: {start|stop|restart|status}"
}
case $1 in
start)
start
success "Config network etho"
;;
stop)
stop
success "Stop network card eth0"
;;
restart)
stop
start
success "Restart network card etho"
;;
status)
status
;;
*)
usage
exit
# chmod +x etc/rc.d/init.d/network 为其添加执行权限
# cd etc/rc.d/rc0.d
# ln -sv ../init.d/network K90netwrok 创建链接
# cd ../rc6.d/
# ln -sv ../init.d/network K90netwrok
# cd ../rc3.d/
# ln -sv ../init.d/network S09netwrok
注意:0和6级别不需开启网络服务