rc.xml 6.8 KB

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