标题: gentoo 启动rc文件分析---------(1)
http://www.linuxsir.org/bbs/thread327380.html


在 前面的一篇文章《gentoo inittab》中已经说过,系统启动时自动执行脚本/sbin/rc sysinit,那么接下来我们分析一下/sbin/rc这个脚本,到底干了什么事情,首先,source了一下/sbin/function.sh,设 置了一些函数和变量,接下来我们看看下面是文件的190行部分,
argv1="$1"

if [[ ( ${RUNLEVEL} == "S" || ${RUNLEVEL} == "1" ) && ${argv1} = "sysinit" ]]
then
PATH="/bin:/sbin:/usr/bin:/usr/sbin:${PATH}"

[ -c /dev/null ] && dev_null=1 || dev_null=0
[ -c /dev/console ] && dev_console=1 || dev_console=0

${RC_DMESG_LEVEL+/bin/dmesg -n ${RC_DMESG_LEVEL}}

echo
echo -e "${GOOD}Gentoo Linux${GENTOO_VERS}; ${BRACKET}http://www.gentoo.org/${NORMAL}"
echo -e " Copyright 1999-2007 Gentoo Foundation; Distributed under the GPLv2"
echo
if [[ ${RC_INTERACTIVE} == "yes" ]] ; then
echo -e "Press ${GOOD}I${NORMAL} to enter interactive boot mode"
echo
fi




我们分析一下,首先执行/sbin/rc sysinit

argv1="$1"


上面的代码中"$1"代表脚本传入的第一个参数,那么相当于$1和sysinit相等.

if [[ ( ${RUNLEVEL} == "S" || ${RUNLEVEL} == "1" ) && ${argv1} = "sysinit" ]]



这个是一个if判断语句,如果if后面的为真,那么就执行then后面的代码,根据上面的参数传入,${argv1}="sysinit",满足了要求,就执行then后面的代码了.

PATH="/bin:/sbin:/usr/bin:/usr/sbin:${PATH}"


首先就是设置了一下PATH这个系统变量.

[ -c /dev/null ] && dev_null=1 || dev_null=0
[ -c /dev/console ] && dev_console=1 || dev_console=0

${RC_DMESG_LEVEL+/bin/dmesg -n ${RC_DMESG_LEVEL}}



上面的几句话的意思就是判断/dev/null和/dev/console是否为是字符设备,如果是的话,那么就把对应的变量设置为1,不然的话就设置为0.下面那句话,还没有弄明白,等待解决.

echo
echo -e "${GOOD}Gentoo Linux${GENTOO_VERS}; ${BRACKET}http://www.gentoo.org/${NORMAL}"
echo -e " Copyright 1999-2007 Gentoo Foundation; Distributed under the GPLv2"
echo



接下去显示一些信息,在开机的时候可以注意,可以看到这些信息.上面是定义的一些颜色是在/sbin/function.sh的最后几句话.



if [[ ${RC_INTERACTIVE} == "yes" ]] ; then
echo -e "Press ${GOOD}I${NORMAL} to enter interactive boot mode"
echo
fi



RC_INTERACTIVE这个变量是在/sbin/rc这个文件的开始进行判断是否在yes,其实默认的赋值在/etc/conf.d/rc文件里面.



上面只是个开头咯,万事开头难,我们有了一个好的开始,接下去慢慢分析来.

check_statedir /proc

ebegin "Mounting proc at /proc"
if [[ ${RC_USE_FSTAB} = "yes" ]] ; then
mntcmd=$(get_mount_fstab /proc)
else
unset mntcmd
fi
try mount -n ${mntcmd:--t proc proc /proc -o noexec,nosuid,nodev}
eend $?

profiling start




看了上面,可能就看不懂了,其实check_statedir是个函数,要看懂这句话,我们要先分析check_statedir这个函数

check_statedir() {
[ -z "$1" ] && return 0

if [ ! -d "$1" ] ; then
if ! mkdir -p "$1" &>/dev/null ; then
splash "critical" &
echo
eerror "For Gentoo to function properly, \"$1\" needs to exist."
if [[ ${RC_FORCE_AUTO} == "yes" ]] ; then
eerror "Attempting to create \"$1\" for you ..."
mount -o remount,rw /
mkdir -p "$1"
else
eerror "Please mount your root partition read/write, and execute:"
echo
eerror " # mkdir -p $1"
echo; echo
sulogin ${CONSOLE}
fi
einfo "Unmounting filesystems"
/bin/mount -a -o remount,ro &>/dev/null
einfo "Rebooting"
/sbin/reboot -f
fi
fi

return 0
}


这个函数的主要作用就是判断/proc这个目录是是否存在,如果存在,那么就返回了,如果不存在就打印一些错误信息,然后unmounting filesystems,最后reboot了.

显然这个目录是存在的,那么接下去走,显示Mounting proc at /proc

if [[ ${RC_USE_FSTAB} = "yes" ]] ; then
mntcmd=$(get_mount_fstab /proc)
else
unset mntcmd
fi


这里的RC_USE_FSTAB变量的值为no,其值是在/etc/conf.d/rc设置的.那么就没有执行对应的mntcmd=$(get_mount_fstab /proc).

try mount -n ${mntcmd:--t proc proc /proc -o noexec,nosuid,nodev}


这句的话意思就是执行mount -n -t proc proc /proc -o noexec,nosuid,nodev.在后面两句话都没有,就是判断是否有错误和一个空操作.

今天暂时先到这里,to be continue……

原文的地址: http://blog.chinaunix.net/u/15960/showart.php?id=570512