rc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/bin/bash
  2. ########################################################################
  3. # Begin rc
  4. #
  5. # 描述 : 主要运行级别控制脚本
  6. #
  7. # 作者 : Gerard Beekmans - gerard@linuxfromscratch.org
  8. # : DJ Lucas - dj@linuxfromscratch.org
  9. # 更新者 : Bruce Dubbs - bdubbs@linuxfromscratch.org
  10. #
  11. # 版本 : LFS 7.0
  12. #
  13. ########################################################################
  14. . /lib/lsb/init-functions
  15. print_error_msg()
  16. {
  17. log_failure_msg
  18. # $i is set when called
  19. MSG="FAILURE:\n\nYou should not be reading this error message.\n\n"
  20. MSG="${MSG}It means that an unforeseen error took place in\n"
  21. MSG="${MSG}${i},\n"
  22. MSG="${MSG}which exited with a return value of ${error_value}.\n"
  23. MSG="${MSG}If you're able to track this error down to a bug in one of\n"
  24. MSG="${MSG}the files provided by the ${DISTRO_MINI} book,\n"
  25. MSG="${MSG}please be so kind to inform us at ${DISTRO_CONTACT}.\n"
  26. log_failure_msg "${MSG}"
  27. log_info_msg "Press Enter to continue..."
  28. wait_for_user
  29. }
  30. check_script_status()
  31. {
  32. # $i is set when called
  33. if [ ! -f ${i} ]; then
  34. log_warning_msg "${i} is not a valid symlink."
  35. SCRIPT_STAT="1"
  36. fi
  37. if [ ! -x ${i} ]; then
  38. log_warning_msg "${i} is not executable, skipping."
  39. SCRIPT_STAT="1"
  40. fi
  41. }
  42. run()
  43. {
  44. if [ -z $interactive ]; then
  45. ${1} ${2}
  46. return $?
  47. fi
  48. while true; do
  49. read -p "Run ${1} ${2} (Yes/no/continue)? " -n 1 runit
  50. echo
  51. case ${runit} in
  52. c | C)
  53. interactive=""
  54. ${i} ${2}
  55. ret=${?}
  56. break;
  57. ;;
  58. n | N)
  59. return 0
  60. ;;
  61. y | Y)
  62. ${i} ${2}
  63. ret=${?}
  64. break
  65. ;;
  66. esac
  67. done
  68. return $ret
  69. }
  70. # 读入所有本地设置和覆盖选项
  71. [ -r /etc/sysconfig/rc.site ] && source /etc/sysconfig/rc.site
  72. DISTRO=${DISTRO:-"Linux From Scratch"}
  73. DISTRO_CONTACT=${DISTRO_CONTACT:-"lfs-dev@linuxfromscratch.org (Registration required)"}
  74. DISTRO_MINI=${DISTRO_MINI:-"LFS"}
  75. IPROMPT=${IPROMPT:-"no"}
  76. # 这三个信号不会使得该脚本退出
  77. trap "" INT QUIT TSTP
  78. [ "${1}" != "" ] && runlevel=${1}
  79. if [ "${runlevel}" == "" ]; then
  80. echo "Usage: ${0} <runlevel>" >&2
  81. exit 1
  82. fi
  83. previous=${PREVLEVEL}
  84. [ "${previous}" == "" ] && previous=N
  85. if [ ! -d /etc/rc.d/rc${runlevel}.d ]; then
  86. log_info_msg "/etc/rc.d/rc${runlevel}.d does not exist.\n"
  87. exit 1
  88. fi
  89. if [ "$runlevel" == "6" -o "$runlevel" == "0" ]; then IPROMPT="no"; fi
  90. # 注意: ${LOGLEVEL:-7} 中, ':-7' 应读作 '冒号' '杠' '七', 不是负七。
  91. if [ "$runlevel" == "S" ]; then
  92. [ -r /etc/sysconfig/console ] && source /etc/sysconfig/console
  93. dmesg -n "${LOGLEVEL:-7}"
  94. fi
  95. if [ "${IPROMPT}" == "yes" -a "${runlevel}" == "S" ]; then
  96. # 发行版欢迎消息字符串的总长度,不计转义代码
  97. wlen=${wlen:-$(echo "Welcome to ${DISTRO}" | wc -c )}
  98. welcome_message=${welcome_message:-"Welcome to ${INFO}${DISTRO}${NORMAL}"}
  99. # 交互字符串的总长度,不计转义代码
  100. ilen=${ilen:-$(echo "Press 'I' to enter interactive startup" | wc -c )}
  101. i_message=${i_message:-"Press '${FAILURE}I${NORMAL}' to enter interactive startup"}
  102. # dcol 和 icol 是消息前应有的空格数,这些空格使得消息居中。
  103. # itime 是等待用户输入按键的时间。
  104. wcol=$(( ( ${COLUMNS} - ${wlen} ) / 2 ))
  105. icol=$(( ( ${COLUMNS} - ${ilen} ) / 2 ))
  106. itime=${itime:-"3"}
  107. echo -e "\n\n"
  108. echo -e "\\033[${wcol}G${welcome_message}"
  109. echo -e "\\033[${icol}G${i_message}${NORMAL}"
  110. echo ""
  111. read -t "${itime}" -n 1 interactive 2>&1 > /dev/null
  112. fi
  113. # 将用户输入变成小写
  114. [ "${interactive}" == "I" ] && interactive="i"
  115. [ "${interactive}" != "i" ] && interactive=""
  116. # 如果是从运行级别 S 切换而来,读入状态文件
  117. [ -r /var/run/interactive ] && source /var/run/interactive
  118. # 试图停止那些在上一运行级别启动,而在该运行级别中禁用的服务
  119. if [ "${previous}" != "N" ]; then
  120. for i in $(ls -v /etc/rc.d/rc${runlevel}.d/K* 2> /dev/null)
  121. do
  122. check_script_status
  123. if [ "${SCRIPT_STAT}" == "1" ]; then
  124. SCRIPT_STAT="0"
  125. continue
  126. fi
  127. suffix=${i#/etc/rc.d/rc$runlevel.d/K[0-9][0-9]}
  128. prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
  129. sysinit_start=/etc/rc.d/rcS.d/S[0-9][0-9]$suffix
  130. if [ "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
  131. if [ ! -f ${prev_start} -a ! -f ${sysinit_start} ]; then
  132. MSG="WARNING:\n\n${i} can't be "
  133. MSG="${MSG}executed because it was not "
  134. MSG="${MSG}not started in the previous "
  135. MSG="${MSG}runlevel (${previous})."
  136. log_warning_msg "$MSG"
  137. continue
  138. fi
  139. fi
  140. run ${i} stop
  141. error_value=${?}
  142. if [ "${error_value}" != "0" ]; then print_error_msg; fi
  143. done
  144. fi
  145. if [ "${previous}" == "N" ]; then export IN_BOOT=1; fi
  146. if [ "$runlevel" == "6" -a -n "${FASTBOOT}" ]; then
  147. touch /fastboot
  148. fi
  149. # 启动该运行级别启用的所有服务
  150. for i in $( ls -v /etc/rc.d/rc${runlevel}.d/S* 2> /dev/null)
  151. do
  152. if [ "${previous}" != "N" ]; then
  153. suffix=${i#/etc/rc.d/rc$runlevel.d/S[0-9][0-9]}
  154. stop=/etc/rc.d/rc$runlevel.d/K[0-9][0-9]$suffix
  155. prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
  156. [ -f ${prev_start} -a ! -f ${stop} ] && continue
  157. fi
  158. check_script_status
  159. if [ "${SCRIPT_STAT}" == "1" ]; then
  160. SCRIPT_STAT="0"
  161. continue
  162. fi
  163. case ${runlevel} in
  164. 0|6)
  165. run ${i} stop
  166. ;;
  167. *)
  168. run ${i} start
  169. ;;
  170. esac
  171. error_value=${?}
  172. if [ "${error_value}" != "0" ]; then print_error_msg; fi
  173. done
  174. # 对于运行级别 S,保存交互状态,否则删除状态文件
  175. if [ "${runlevel}" == "S" -a "${interactive}" == "i" ]; then
  176. echo "interactive=\"i\"" > /var/run/interactive
  177. else
  178. rm -f /var/run/interactive 2> /dev/null
  179. fi
  180. # 仅为初次引导复制引导日志
  181. if [ "${previous}" == "N" -a "${runlevel}" != "S" ]; then
  182. cat $BOOTLOG >> /var/log/boot.log
  183. # 标记引导结束
  184. echo "--------" >> /var/log/boot.log
  185. # 删除临时文件
  186. rm -f $BOOTLOG 2> /dev/null
  187. fi
  188. # End rc