###################RHEL6TinyLinux#################
以Linux的启动流程来制作,预备条件:宿主机,独立硬盘(也可以用镜像文件)
第一步:分区,挂载,以下是分区脚本
#!/bin/bash
#DifinedVariable
#
BootPart=500M
RootPart=2G
read-p"Inputyourdiskthatneedtoformat:"Disk
以Linux的启动流程来制作,预备条件:宿主机,独立硬盘(也可以用镜像文件)
第一步:分区,挂载,以下是分区脚本
#!/bin/bash
#DifinedVariable
#
BootPart=500M
RootPart=2G
read-p"Inputyourdiskthatneedtoformat:"Disk
#Partitons
(
/sbin/fdisk$Disk<<EOF
n
p
1
(
/sbin/fdisk$Disk<<EOF
n
p
1
+$BootPart
n
p
2
n
p
2
+$RootPart
w
EOF
)&>/dev/null
w
EOF
)&>/dev/null
sleep3
mkfs.ext4${Disk}1&>/dev/null
mkfs.ext4${Disk}2&>/dev/null
mkfs.ext4${Disk}2&>/dev/null
mkdir/mnt/{boot,sysroot}&>/dev/null
mount${Disk}1/mnt/boot
第二步:安装GRUB及其配置文件,以下是处理脚本
#!/bin/bash
#
GrubRD=/mnt
read-p"Inputyourdiskforinstallinggrub:"Gbdisk
grub-install--root-directory=$GrubRD$Gbdisk
cd$GrubRD/boot/grub
cat>grub.conf<<EOF
timeout=5
default=0
titleWelcometousetinylinux(2.6.32)
root(hd0,0)
kernel/vmlinuzroroot=/dev/sda2selinux=0
initrd/initramfs.img
EOF
#
GrubRD=/mnt
read-p"Inputyourdiskforinstallinggrub:"Gbdisk
grub-install--root-directory=$GrubRD$Gbdisk
cd$GrubRD/boot/grub
cat>grub.conf<<EOF
timeout=5
default=0
titleWelcometousetinylinux(2.6.32)
root(hd0,0)
kernel/vmlinuzroroot=/dev/sda2selinux=0
initrd/initramfs.img
EOF
#copykernelandinitrdfromcurrentsystem
cp/boot/vmlinuz*/$GrubRD/boot/vmlinuz
cp/boot/initramfs*/$GrubRD/boot/initramfs.img
cp/boot/vmlinuz*/$GrubRD/boot/vmlinuz
cp/boot/initramfs*/$GrubRD/boot/initramfs.img
第三步:建立根文件系统下的相关目录以及初始化脚本
#!/bin/bash
#
read-p"inputrealrootfs'spathfortinylinux:"Sysroot
cd${Sysroot:-/mnt/sysroot}
mkdir-pprocsysdevetc/{init,rc.d}lib/moduleslib64binsbinusr/{lib,lib64}var/{log,run,lock}
cat>./etc/init/rcS.conf<<EOF
startonstartup
#
read-p"inputrealrootfs'spathfortinylinux:"Sysroot
cd${Sysroot:-/mnt/sysroot}
mkdir-pprocsysdevetc/{init,rc.d}lib/moduleslib64binsbinusr/{lib,lib64}var/{log,run,lock}
cat>./etc/init/rcS.conf<<EOF
startonstartup
stoponrunlevel
task
consoleoutput
exec/etc/rc.d/rc.sysinit
EOF
consoleoutput
exec/etc/rc.d/rc.sysinit
EOF
cat>./etc/rc.d/rc.sysinit<<EOF
#!/bin/bash
#
echo-e"\tWelcometocome\033[5;31mTinyLinux\033[0m"
/bin/bash
EOF
chmod+x./etc/rc.d/rc.sysinit
#!/bin/bash
#
echo-e"\tWelcometocome\033[5;31mTinyLinux\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"copylib$1finished."
}
#!/bin/bash
#
DEST=/mnt/sysroot
libcp(){
LIBPATH=${1%/*}
[!-d$DEST$LIBPATH]&&mkdir-p$DEST$LIBPATH
[!-e$DEST${1}]&&cp-a$1*$DEST$LIBPATH&&echo"copylib$1finished."
}
bincp(){
CMDPATH=${1%/*}
[!-d$DEST$CMDPATH]&&mkdir-p$DEST$CMDPATH
[!-e$DEST${1}]&&cp$1$DEST$CMDPATH
CMDPATH=${1%/*}
[!-d$DEST$CMDPATH]&&mkdir-p$DEST$CMDPATH
[!-e$DEST${1}]&&cp$1$DEST$CMDPATH
forLIBin`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
}
if["$LIB"="/lib64/ld-linux-x86-64"];then
LIB=`echo$LIB|grep-o"/.*lib\(64\)\{0,1\}/[^-]\{1,\}"`
fi
libcp$LIB
done
}
read-p"Yourcommand:"CMD
until[$CMD=='q'];do
!which$CMD&&echo"Wrongcommand"&&read-p"Inputagain:"CMD&&continue
COMMAND=`which$CMD|grep-v"^alias"|grep-o"[^[:space:]]\{1,\}"`
bincp$COMMAND
echo"copy$COMMANDfinished."
read-p"Continue:"CMD
done
until[$CMD=='q'];do
!which$CMD&&echo"Wrongcommand"&&read-p"Inputagain:"CMD&&continue
COMMAND=`which$CMD|grep-v"^alias"|grep-o"[^[:space:]]\{1,\}"`
bincp$COMMAND
echo"copy$COMMANDfinished."
read-p"Continue:"CMD
done
第五步:添加运行级别0和6,使系统可以正常关机重启
1.cp-a/etc/init/rc.conf.
2.建立rc脚本
#!/bin/bash
#
#creatercscript
#
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
foriin${RunLevel_Dir}/K*;do
if[-x$i];then
$istop
fi
done
1.cp-a/etc/init/rc.conf.
2.建立rc脚本
#!/bin/bash
#
#creatercscript
#
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
foriin${RunLevel_Dir}/K*;do
if[-x$i];then
$istop
fi
done
foriin${RunLevel_Dir}/S*;do
if[-x$i];then
$istart
fi
done
EOF
chmod+x${Tiny_etc_dir}/rc.d/rc
if[-x$i];then
$istart
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}
mkdir-p/mnt/sysroot/etc/{init.d,rc0.d,rc6.d}
4.编写halt,用作关机和重启
vim/mnt/sysroot/etc/init.d/halt
#!/bin/bash
#
case$0in
*halt)
echo"haltingthesystem......."
/sbin/halt-p
;;
*reboot)
echo"rebootingthesystem......"
/sbin/reboot
;;
*)
;;
esac
chmod+x/mnt/sysroot/etc/init.d/halt
vim/mnt/sysroot/etc/init.d/halt
#!/bin/bash
#
case$0in
*halt)
echo"haltingthesystem......."
/sbin/halt-p
;;
*reboot)
echo"rebootingthesystem......"
/sbin/reboot
;;
*)
;;
esac
chmod+x/mnt/sysroot/etc/init.d/halt
5.使用第四部的脚本,来移动halt、reboot命令以及依赖库
6.将halt脚本分别链接到rc0.d和rc6.d目录下
cdrc0.d
ln-sv../init.d/haltS01halt
cdrc6.d
ln-sv../init.d/haltS01reboot
cdrc0.d
ln-sv../init.d/haltS01halt
cdrc6.d
ln-sv../init.d/haltS01reboot
7.建立/etc/passwd文件,并在其中加入root用户条目:
cat>/mnt/sysroot/etc/passwd<<EOF
root:x:0:0:root:/root:/bin/bash
EOF
cat>/mnt/sysroot/etc/passwd<<EOF
root:x:0:0:root:/root:/bin/bash
EOF
8.拷贝/etc/passwd的依赖库
cp-a/lib64/libnss_files*/mnt/sysroot/lib64/
cp-a/lib64/libnss_files*/mnt/sysroot/lib64/
第六步:添加登陆终端
1.拷贝init程序处理过程中与终端有关的配置文件
cp-a/etc/init/{tty.conf,start-ttys.conf,prefdm.conf}/mnt/sysroot/etc/init/
1.拷贝init程序处理过程中与终端有关的配置文件
cp-a/etc/init/{tty.conf,start-ttys.conf,prefdm.conf}/mnt/sysroot/etc/init/
2.建立于登录有关的目录
mkdir/mnt/sysroot/etc/{pam.d,security,sysconfig}
mkdir/mnt/sysroot/etc/{pam.d,security,sysconfig}
3.拷贝与登录有关的配置文件
cp/etc/pam.d/{login,system-auth}/mnt/sysroot/etc/pam.d
cp/etc/security/{limits.conf,namespace.conf}/mnt/sysroot/etc/security/
cp/etc/sysconfig/init/mnt/sysroot/etc/sysconfig
cp/etc/pam.d/{login,system-auth}/mnt/sysroot/etc/pam.d
cp/etc/security/{limits.conf,namespace.conf}/mnt/sysroot/etc/security/
cp/etc/sysconfig/init/mnt/sysroot/etc/sysconfig
4.建立存放与登录pam相关的功能库文件目录
mkdir/mnt/sysroot/lib64/security
mkdir/mnt/sysroot/lib64/security
5.拷贝login中依赖的pam库文件
foriin`awk'grep-o'pam.*so'/etc/pam.d/login`;docp-a/lib64/security/$i/mnt/sysroot/lib64/security/;done
foriin`awk'grep-o'pam.*so'/etc/pam.d/login`;docp-a/lib64/security/$i/mnt/sysroot/lib64/security/;done
6.拷贝system-auth中依赖的pam库文件
foriin`awk'$3~"so$"{print$3}'/etc/pam.d/system-auth`;docp-a/lib64/security/$i/mnt/sysroot/lib64/security/;done
foriin`awk'$3~"so$"{print$3}'/etc/pam.d/system-auth`;docp-a/lib64/security/$i/mnt/sysroot/lib64/security/;done
7.cp-a/lib64/libnsl*/mnt/sysroot/lib64
8.grep^root/etc/shadow>>/mnt/sysroot/etc/shadow
9.egrep'^(passwd|shadow)'/etc/nsswitch.conf>>/mnt/sysroot/etc/nsswitch.conf(注:没有nsswitch.conf也可以)
10.使用第四步脚本,拷贝login命令及其库文件。
11.修改rc.sysinit文件,如下:
#!/bin/bash
#
#sethostname
./etc/sysconfig/network
#!/bin/bash
#
#sethostname
./etc/sysconfig/network
[-z"$HOSTNAME"-o"$HOSTNAME"=="(none)"]&&HOSTNAME=tiny.linux
/bin/hostname$HOSTNAME
/bin/hostname$HOSTNAME
#remountrootfs
mount-n-oremount,rw/
echo-e"\tWelcometocome\033[5;31mTinyLinux\033[0m"
#/bin/bash
mount-n-oremount,rw/
echo-e"\tWelcometocome\033[5;31mTinyLinux\033[0m"
#/bin/bash
12.使用第四步脚本,拷贝mount和hostname命令及其库文件。
第七步:添加单用户模式
1.建立运行级别"1"的目录
mkdir/mnt/sysroot/etc/rc.d/rc1.d
2.拷贝宿主机的/etc/rc.d/init.d/single
cp/etc/rc.d/init.d/single/mnt/sysroot/etc/rc.d/init.d
cp/etc/rc.d/rc1.d/S99single/mnt/sysroot/etc/rc.d/rc1.d
1.建立运行级别"1"的目录
mkdir/mnt/sysroot/etc/rc.d/rc1.d
2.拷贝宿主机的/etc/rc.d/init.d/single
cp/etc/rc.d/init.d/single/mnt/sysroot/etc/rc.d/init.d
cp/etc/rc.d/rc1.d/S99single/mnt/sysroot/etc/rc.d/rc1.d
3.cd/mnt/sysroot/etc/rc.d/rc1.d&&ln-sv../init.d/tserverK33tserver
4.拷贝单用户配置文件(被init程序使用的)
cp/etc/init/rcS-sulogin.conf/mnt/sysroot/etc/init/
5.使用第四步脚本,拷贝rcS-sulogin.conf文件中出现的命令及其库文件。
cp/etc/init/rcS-sulogin.conf/mnt/sysroot/etc/init/
5.使用第四步脚本,拷贝rcS-sulogin.conf文件中出现的命令及其库文件。
本文转自大角牛博客51CTO博客,原文链接http://blog.51cto.com/jingshengsun888/1306896如需转载请自行联系原作者
运维的戏子