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