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