rc.xml 6.8 KB

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