RHEL5 tiny linux的制作
############# RHEL5 Tiny Linux ##############
以Linux的启动流程来制作第一步:分区,挂载,以下是分区脚本
#!/bin/bash# Difined Variable#BootPart=500MRootPart=2Gread -p "Input your disk that need to format: " Disk# Partitons
(/sbin/fdisk $Disk <<EOFnp1+$BootPart
np2+$RootPart
wEOF)&> /dev/nullsleep 3
mkfs.ext3 ${Disk}1 &> /dev/null
mkfs.ext3 ${Disk}2 &> /dev/nullmkdir /mnt/{boot,sysroot} &> /dev/null
mount ${Disk}1 /mnt/boot
第二步:安装GRUB及其配置文件,以下是处理脚本#!/bin/bash
#GrubRD=/mntread -p "Input your disk for installing grub: " Gbdiskgrub-install --root-directory=$GrubRD $Gbdiskcd $GrubRD/boot/grubcat >grub.conf <<EOFtimeout=5default=0title Welcome to use tiny linux(2.6.18) root(hd0,0) kernel /vmlinuz ro root=/dev/hda2 initrd /initrd.gzEOF#copy kernel and initrd from current system
cp /boot/vmlinuz* /$GrubRD/boot/vmlinuz# create tmp dircd /tmpTmpDir=$(mktemp -d inittemp.XXX)cd $TmpDir# uncompress initrd*.img and modify init zcat /boot/initrd* | cpio -idOldroot=$(awk '/mkrootdev/ {print $NF}' init)sed -i "s#${Oldroot}#/dev/hda2#" init#compress againfind .|cpio -H newc --quiet -o |gzip -9 > $GrubRD/boot/initrd.gz第三步: 建立根文件系统下的相关目录以及初始化脚本
#!/bin/bash
#read -p "input real rootfs's path for tiny linux: " Sysrootcd ${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 <<EOFid:3:initdefault:si::sysinit:/etc/rc.d/rc.sysinitEOFcat >./etc/rc.d/rc.sysinit <<EOF
#!/bin/bash#echo -e "\tWelcome to come \033[5;31mTiny Linux\033[0m"/bin/bashEOFchmod +x ./etc/rc.d/rc.sysinit第四步:拷贝要使用的程序命令到根目录
#!/bin/bash#DEST=/mnt/sysrootlibcp() { 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$CMDPATHfor 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: " CMDdone第五步:添加运行级别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 ;; esacdone 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.dcat >${Tiny_etc_dir}/rc.d/rc <<EOF #!/bin/bash#RunLevel_Dir=/etc/rc.d/rc$1for i in ${RunLevel_Dir}/K*;do if [ -x $i ];then $i stop fidonefor i in ${RunLevel_Dir}/S*;do
if [ -x $i ];then $i start fidoneEOFchmod +x ${Tiny_etc_dir}/rc.d/rc3. 建立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 ;;*) ;;esacchmod +x /mnt/sysroot/etc/init.d/halt5. 使用第四部的脚本,来移动halt、reboot命令以及依赖库6. 将halt脚本分别链接到rc0.d和rc6.d目录下
cd rc0.dln -sv ../init.d/halt S01haltcd rc6.dln -sv ../init.d/halt S01reboot第六步:添加终端、用户、主机名,环境变量PS1,登陆提示信息
1. 在inittab中,添加终端配置cat >>/mnt/sysroot/etc <<EOF#1:2345:respawn:/sbin/agetty -n -l /bin/bash 38400 tty11:2345:respawn:/sbin/mingetty tty12:2345:respawn:/sbin/mingetty tty23:2345:respawn:/sbin/mingetty tty34:2345:respawn:/sbin/mingetty tty45:2345:respawn:/sbin/mingetty tty56:2345:respawn:/sbin/mingetty tty6EOF2. 使用第四部的脚本,来移动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=localhosthostname $HOSTNAMEecho "Remount rootfs..."mount -n -o remount,rw /4. 登陆提示信息cat >/mnt/sysroot/etc/issue <<EOFWelcome to use tiny linux !EOF5. 添加登录用户和密码cat >/mnt/sysroot/etc/passwd <<EOFroot:x:0:0:fist user:/root:/bin/bashEOFgrep "root" /etc/shadow >/mnt/sysroot/etc/shadow
cat >/mnt/sysroot/etc/nsswitch.conf <<EOF
passwd: filesshadow: filesEOFcp -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)