rc.xml 6.8 KB

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