RHEL5 tiny linux的制作
############# RHEL5 Tiny Linux ##############
以Linux的启动流程来制作
第一步:分区,挂载,以下是分区脚本
#!/bin/bash
# Difined Variable
#
BootPart=500M
RootPart=2G
read -p "Input your disk that need to format: " Disk
# Partitons
(
/sbin/fdisk $Disk <<EOF
n
p
1
+$BootPart
n
p
2
+$RootPart
w
EOF
)&> /dev/null
sleep 3
mkfs.ext3 ${Disk}1 &> /dev/null
mkfs.ext3 ${Disk}2 &> /dev/null
mkdir /mnt/{boot,sysroot} &> /dev/null
mount ${Disk}1 /mnt/boot
第二步:安装GRUB及其配置文件,以下是处理脚本
#!/bin/bash
#
GrubRD=/mnt
read -p "Input your disk for installing grub: " Gbdisk
grub-install --root-directory=$GrubRD $Gbdisk
cd $GrubRD/boot/grub
cat >grub.conf <<EOF
timeout=5
default=0
title Welcome to use tiny linux(2.6.18)
root(hd0,0)
kernel /vmlinuz ro root=/dev/hda2
initrd /initrd.gz
EOF
#copy kernel and initrd from current system
cp /boot/vmlinuz* /$GrubRD/boot/vmlinuz
# create tmp dir
cd /tmp
TmpDir=$(mktemp -d inittemp.XXX)
cd $TmpDir
# uncompress initrd*.img and modify init
zcat /boot/initrd* | cpio -id
Oldroot=$(awk ‘/mkrootdev/ {print $NF}‘ init)
sed -i "s#${Oldroot}#/dev/hda2#" init
#compress again
find .|cpio -H newc --quiet -o |gzip -9 > $GrubRD/boot/initrd.gz
第三步: 建立根文件系统下的相关目录以及初始化脚本
#!/bin/bash
#
read -p "input real rootfs‘s path for tiny linux: " Sysroot
cd ${Sysroot:-/mnt/sysroot}
mkdir -p proc sys dev etc/rc.d lib/modules lib64 bin sbin usr/{lib,lib64} var/{log,run,lock}
cat >./etc/inittab <<EOF
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
EOF
cat >./etc/rc.d/rc.sysinit <<EOF
#!/bin/bash
#
echo -e "\tWelcome to come \033[5;31mTiny Linux\033[0m"
/bin/bash
EOF
chmod +x ./etc/rc.d/rc.sysinit
第四步:拷贝要使用的程序命令到根目录
#!/bin/bash
#
DEST=/mnt/sysroot
libcp() {
LIBPATH=${1%/*}
[ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH
[ ! -e $DEST${1} ] && cp -a $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 | grep -o "/.*lib\(64\)\{0,1\}/[^.]\{1,\}"`; do
if [ "$LIB" = "/lib64/ld-linux-x86-64" ];then
LIB=`echo $LIB | grep -o "/.*lib\(64\)\{0,1\}/[^-]\{1,\}"`
fi
libcp $LIB
done
}
read -p "Your command: " CMD
until [ $CMD == ‘q‘ ]; do
! which $CMD && echo "Wrong command" && read -p "Input again:" CMD && continue
COMMAND=` which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"`
bincp $COMMAND
echo "copy $COMMAND finished."
read -p "Continue: " CMD
done
第五步:添加运行级别0 和 6 ,使系统可以正常关机重启
1.
#!/bin/bash
#
#addlevel.sh
#
#used for adding run level
#
Tiny_etc_dir=/mnt/sysroot/etc/
level() {
grep "l$1" ${Tiny_etc_dir}/inittab
[ $? != 0 ] && sed -i "$ a l$1:$1:wait:/etc/rc.d/rc $1" ${Tiny_etc_dir}/inittab && echo -e "add runing level $1 \033[32mOK\033[0m" || echo "run level $1 has added"
}
read -p "input level number that need to add: " Lnum
until [ $Lnum == "q" ];do
case $Lnum in
[0-6])
level $Lnum
read -p "continue: " Lnum
;;
*)
echo "input number[0-6]"
read -p "continue: " Lnum
;;
esac
done
2. 建立rc脚本
#!/bin/bash
#
#create rc script
#
Tiny_etc_dir=/mnt/sysroot/etc
[ -d ${Tiny_etc_dir}/rc.d ] || mkdir -p ${Tiny_etc_dir}/rc.d
cat >${Tiny_etc_dir}/rc.d/rc <<EOF
#!/bin/bash
#
RunLevel_Dir=/etc/rc.d/rc$1
for i in ${RunLevel_Dir}/K*;do
if [ -x $i ];then
$i stop
fi
done
for i in ${RunLevel_Dir}/S*;do
if [ -x $i ];then
$i start
fi
done
EOF
chmod +x ${Tiny_etc_dir}/rc.d/rc
3. 建立init.d目录和rcN.d目录,以及对应的动作脚本和链接。
mkdir -p /mnt/sysroot/etc/{init.d,rc0.d,rc6.d}
4. 编写halt,用作关机和重启
vim /mnt/sysroot/etc/init.d/halt
#!/bin/bash
#
case $0 in
*halt)
echo "halting the system......."
/sbin/halt -p
;;
*reboot)
echo "rebooting the system......"
/sbin/reboot
;;
*)
;;
esac
chmod +x /mnt/sysroot/etc/init.d/halt
5. 使用第四部的脚本,来移动halt、reboot命令以及依赖库
6. 将halt脚本分别链接到rc0.d和rc6.d目录下
cd rc0.d
ln -sv ../init.d/halt S01halt
cd rc6.d
ln -sv ../init.d/halt S01reboot
第六步:添加终端、用户、主机名,环境变量PS1,登陆提示信息
1. 在inittab中,添加终端配置
cat >>/mnt/sysroot/etc <<EOF
#1:2345:respawn:/sbin/agetty -n -l /bin/bash 38400 tty1
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
EOF
2. 使用第四部的脚本,来移动mingetty、login命令以及依赖库
3. 修改rc.sysinit如下:
#!/bin/bash
#
echo -e "\tWelcome to come \033[1;31mTiny Linux\033[0m"
#/bin/bash
#/sbin/mingetty --loginprog=/bin/bash tty1
. /etc/sysconfig/network
[ -z $HOSTNAME -o "$HOSTNAME" = "none" ] && HOSTNAME=localhost
hostname $HOSTNAME
echo "Remount rootfs..."
mount -n -o remount,rw /
4. 登陆提示信息
cat >/mnt/sysroot/etc/issue <<EOF
Welcome to use tiny linux !
EOF
5. 添加登录用户和密码
cat >/mnt/sysroot/etc/passwd <<EOF
root:x:0:0:fist user:/root:/bin/bash
EOF
grep "root" /etc/shadow >/mnt/sysroot/etc/shadow
cat >/mnt/sysroot/etc/nsswitch.conf <<EOF
passwd: files
shadow: files
EOF
cp -a /lib64/libnss_files* /mnt/sysroot/lib64
cp -a /lib64/libnsl* /mnt/sysroot/lib64
(mkdir /mnt/sysroot/etc/security & cp -a /etc/security/limits* /mnt/sysroot/etc/security)
(mkdir /mnt/sysroot/etc/pam.d & cp /etc/pam.d/{login,system-auth} /mnt/sysroot/etc/security)
本文出自 “一切皆有可能” 博客,请务必保留此出处http://noican.blog.51cto.com/4081966/1656580
原文地址:http://noican.blog.51cto.com/4081966/1656580