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