rc.xml 6.8 KB

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