linux system init (by hanlray(at)gmail.com)
unix類的系統通常都有多個runlevel,一個runlevel是一組服務的運行狀態的配置:在該runlevel下哪些服務應該運行,哪些服務應該停止。一個服務的啟動、停止等動作是由一個init script(通常包括在該服務程式的package中)控制的,LSB對init script做了標準化,一個標準的init script應該支援如下action:
startstart the service stopstop the service restartstop and restart the service if the service is already running, otherwise start the service try-restartrestart the service if the service is already running reloadcause the configuration of the service to be reloaded without actually stopping and restarting the service force-reloadcause the configuration to be reloaded if the service supports this, otherwise restart the service if it is running statusprint the current status of the service
每個runlevel都對應一個目錄,裡面是一些到這些init script的symbol link,形如SNNxxx,KNNxxx,NN是一個序號,當改變到某個runlevel時(包括系統啟動進入一個runlevel時),對該runlevel目錄下的symbol links,先按序號順序執行K打頭的,執行的action為stop,再按序號順序執行S打頭的,執行的action為start。
手工管理各個runlevel是一件費力的事,LSB可以簡化這一工作,其原理是在init script裡寫入該服務的meta資訊:它依賴的服務、在哪些runlevel下啟動等,然後由工具利用這些資訊自動設定各個runlevel。下面是為xvnc寫的一個init script(/etc/init.d/vncserver):
#! /bin/sh # # Author: Willem van der Mark <wvdmark@computer.org> # # /etc/init.d/xvncserverthis Script # /usr/sbin/rcvncserverRoot-Link to this Script # /usr/X11R6/bin/vncserverProgram # # System startup script for vnc server # # LSB compatible service control script; see http://www.linuxbase.org/spec/ # # Note: This template uses functions rc_XXX defined in /etc/rc.status on # UnitedLinux (UL) based Linux distributions. If you want to base your # script on this template and ensure that it works on non UL based LSB # compliant Linux distributions, you either have to provide the rc.status # functions from UL or change the script to work without them. # # ### BEGIN INIT INFO # Provides: xvncserver # Required-Start: $remote_fs $syslog $network xdm # X-UnitedLinux-Should-Start: $network xdm # Required-Stop: $remote_fs $syslog $network xdm # X-UnitedLinux-Should-Stop: $network xdm # Default-Start: 3 5 # Default-Stop: 0 1 2 6 # Description: Start vncserver for remote control # ### END INIT INFO # Check for missing binaries VNC_WRAPPER=/usr/X11R6/bin/vncserver test -x $VNC_WRAPPER || exit 5 VNC_MASTER=/usr/X11R6/bin/Xvnc test -x $VNC_MASTER || exit 5 VNC_CONFIG=/etc/sysconfig/vncservers test -r $VNC_CONFIG || exit 5 # Shell functions sourced from /etc/rc.status: # rc_check check and set local and overall rc status # rc_status check and set local and overall rc status # rc_status -v ditto but be verbose in local rc status # rc_status -v -r ditto and clear the local rc status # rc_failed set local and overall rc status to failed # rc_failed <num> set local and overall rc status to <num><num> # rc_reset clear local rc status (overall remains) # rc_exit exit appropriate to overall rc status # rc_activechecks whether a service is activated by symlinks . /etc/rc.status # First reset status of this service rc_reset # Return values acc. to LSB for all commands but status: # 0 - success # 1 - generic or unspecified error # 2 - invalid or excess argument(s) # 3 - unimplemented feature (e.g. "reload") # 4 - insufficient privilege # 5 - program is not installed # 6 - program is not configured # 7 - program is not running # VNCSERVERS="" VNCARGS="" [ -f $VNC_CONFIG ] && . $VNC_CONFIG prog="VNC server" case "$1" in start) for display in ${VNCSERVERS} do if test -f /home/${display##*:}/.vnc/passwd ; then rm -fv /tmp/.X${display%%:*}-lock rm -fv /tmp/.X11-unix/X${display%%:*} echo -n "Starting $prog on: ${display} -- " su ${display##*:} -l -c "cd && vncserver :${display%%:*} ${VNCARGS}" [ $? -eq 0 ] && echo $rc_done_up || echo $rc_failed_up else echo -n "Vnc not initialised for user: ${display##*:}" echo $rc_failed ; fi done ;; stop) for display in ${VNCSERVERS} do echo -n "Shutting down $prog: ${display} " su ${display##*:} -c "vncserver -kill :${display%%:*}" >/dev/null 2>&1 [ $? -eq 0 ] && echo $rc_done || echo $rc_failed done ;; restart) $0 stop $0 start rc_status ;; status) echo -n "Checking for service Vnc-Server " ## Check status with checkproc(8), if process is running ## checkproc will return with exit status 0. # Return value is slightly different for the status command: # 0 - service running # 1 - service dead, but /var/run/ pid file exists # 2 - service dead, but /var/lock/ lock file exists # 3 - service not running # NOTE: checkproc returns LSB compliant status values. checkproc $VNC_MASTER rc_status -v ;; *) echo "Usage: $0 {start|stop|status|restart}" exit 1 ;; esac rc_exit #
BEGIN INIT INFO和### END INIT INFO之間的文本就是該服務的meta資訊,用install_initd/remove_initd來activate/deactivate符合LSB標準的init script: /usr/lib/lsb/install_initd /etc/init.d/vncserver
install_initd根據這些meta資訊在相應的runlevel目錄下產生symbol link,並保證其依賴關係。