rc.xml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <sect1 id="ch07-rc">
  2. <title>Creating the rc script</title>
  3. <para>
  4. The first main boot script is the <filename>/etc/init.d/rc</filename> script.
  5. A new file <filename>/etc/init.d/rc</filename> is created containing the
  6. following: </para>
  7. <para>
  8. <screen>
  9. <userinput>cat &gt; rc &lt;&lt; "EOF"</userinput>
  10. #!/bin/sh
  11. # Begin /etc/init.d/rc
  12. #
  13. # By Jason Pearce - jason.pearce@linux.org
  14. # Modified by Gerard Beekmans - gerard@linuxfromscratch.org
  15. # print_error_msg based on ideas by Simon Perreault -
  16. # nomis80@videotron.ca
  17. #
  18. # Include the functions declared in the /etc/init.d/functions file
  19. #
  20. source /etc/init.d/functions
  21. #
  22. # The print_error_msg function prints an error message when an unforeseen
  23. # error occurred that wasn't trapped for some reason by a evaluate_retval
  24. # call or error checking in different ways.
  25. print_error_msg()
  26. {
  27. echo
  28. $FAILURE
  29. echo -n "You should not read this error message. It means "
  30. echo "that an unforeseen error "
  31. echo -n "took place and subscript $i exited with "
  32. echo "a return value "
  33. echo -n "of $error_value for an unknown reason. If you're able "
  34. echo "to trace this error down "
  35. echo -n "to a bug in one of the files provided by this book, "
  36. echo "please be so kind to "
  37. echo -n "inform us at lfs-discuss@linuxfromscratch.org"
  38. $NORMAL
  39. echo
  40. echo
  41. echo "Press a key to continue..."
  42. read
  43. }
  44. #
  45. # If you uncomment the debug variable below none of the scripts will be
  46. # executed, just the script name and parameters will be echo'ed to the
  47. # screen so you can see how the scripts are called by rc.
  48. #
  49. # Un-comment the following for debugging.
  50. # debug=echo
  51. #
  52. # Start script or program.
  53. #
  54. startup() {
  55. $debug $*
  56. }
  57. #
  58. # Ignore CTRL-C only in this shell, so we can interrupt subprocesses.
  59. #
  60. trap ":" INT QUIT TSTP
  61. #
  62. # Now find out what the current and what the previous runlevel are. The
  63. # $RUNLEVEL variable is set by init for all it's children. This script
  64. # runs as a child of init.
  65. #
  66. runlevel=$RUNLEVEL
  67. #
  68. # Get first argument. Set new runlevel to this argument. If no runlevel
  69. # was passed to this script we won't change runlevels.
  70. #
  71. [ "$1" != "" ] &amp;&amp; runlevel=$1
  72. if [ "$runlevel" = "" ]
  73. then
  74. echo "Usage: $0 &lt;runlevel&gt;" &gt;&amp;2
  75. exit 1
  76. fi
  77. #
  78. # The same goes for $PREVLEVEL (see above for $RUNLEVEL). previous will
  79. # be set to the previous run level. If $PREVLEVEL is not set it means
  80. # that there is no previous runlevel and we'll set previous to N.
  81. #
  82. previous=$PREVLEVEL
  83. [ "$previous" = "" ] &amp;&amp; previous=N
  84. export runlevel previous
  85. #
  86. # Is there an rc directory for the new runlevel?
  87. #
  88. if [ -d /etc/rc$runlevel.d ]
  89. then
  90. #
  91. # If so, first collect all the K* scripts in the new run level.
  92. #
  93. if [ $previous != N ]
  94. then
  95. for i in /etc/rc$runlevel.d/K*
  96. do
  97. [ ! -f $i ] &amp;&amp; continue
  98. #
  99. # the suffix variable will contain the script name without the leading
  100. # Kxxx
  101. #
  102. suffix=${i#/etc/rc$runlevel.d/K[0-9][0-9][0-9]}
  103. #
  104. # If there is a start script for this K script in the previous runlevel
  105. # determine what it's full path is
  106. #
  107. previous_start=/etc/rc$previous.d/S[0-9][0-9][0-9]$suffix
  108. #
  109. # If there was no previous run level it could be that something was
  110. # started in rcS.d (sysinit level) so we'll determine the path for that
  111. # possibility as well.
  112. #
  113. sysinit_start=/etc/rcS.d/S[0-9][0-9][0-9]$suffix
  114. #
  115. # Stop the service if there is a start script in the previous run level
  116. # or in the sysinit level. If previous_start or sysinit_start do not
  117. # exist the 'continue' command is run which causes the script to abort
  118. # this iteration of the for loop and continue with the next iteration.
  119. # This boils down to that it won't run the commands after the next two
  120. # lines and start over from the top of this for loop. See man bash for
  121. # more info on this.
  122. #
  123. [ ! -f $previous_start ] &amp;&amp;
  124. [ ! -f $sysinit_start ] &amp;&amp; continue
  125. #
  126. # If we found previous_start or sysinit_start, run the K script
  127. #
  128. startup $i stop
  129. error_value=$?
  130. #
  131. # If the return value of the script is not 0, something went wrong with
  132. # error checking inside the script. the print_error_msg function will be
  133. # called and the message plus the return value of the K script will be
  134. # printed to the screen
  135. #
  136. if [ $error_value != 0 ]
  137. then
  138. print_error_msg
  139. fi
  140. done
  141. fi
  142. #
  143. # Now run the START scripts for this runlevel.
  144. #
  145. for i in /etc/rc$runlevel.d/S*
  146. do
  147. [ ! -f $i ] &amp;&amp; continue
  148. if [ $previous != N ]
  149. then
  150. #
  151. # Find start script in previous runlevel and stop script in this
  152. # runlevel.
  153. #
  154. suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9][0-9]}
  155. stop=/etc/rc$runlevel.d/K[0-9][0-9][0-9]$suffix
  156. previous_start=/etc/rc$previous.d/S[0-9][0-9][0-9]$suffix
  157. #
  158. # If there is a start script in the previous level and no stop script in
  159. # this level, we don't have to re-start the service; abort this
  160. # iteration and start the next one.
  161. #
  162. [ -f $previous_start ] &amp;&amp; [ ! -f $stop ] &amp;&amp;
  163. continue
  164. fi
  165. case "$runlevel" in
  166. 0|6)
  167. #
  168. # levels 0 and 6 are halt and reboot levels. We don't really start
  169. # anything here so we call with the 'stop' parameter
  170. #
  171. startup $i stop
  172. error_value=$?
  173. #
  174. # If the return value of the script is not 0, something went wrong with
  175. # error checking inside the script. the print_error_msg function will be
  176. # called and the message plus the return value of the K script will be
  177. # printed to the screen
  178. #
  179. if [ $error_value != 0 ]
  180. then
  181. print_error_msg
  182. fi
  183. ;;
  184. *)
  185. startup $i start
  186. error_value=$?
  187. #
  188. # If the return value of the script is not 0, something went wrong with
  189. # error checking inside the script. the print_error_msg function will be
  190. # called and the message plus the return value of the K script will be
  191. # printed to the screen
  192. #
  193. if [ $error_value != 0 ]
  194. then
  195. print_error_msg
  196. fi
  197. ;;
  198. esac
  199. done
  200. fi
  201. # End /etc/init.d/rc
  202. <userinput>EOF</userinput>
  203. </screen>
  204. </para>
  205. </sect1>