[6370fa6] | 1 | <sect1 id="ch07-rc">
|
---|
| 2 | <title>Creating the rc script</title>
|
---|
| 3 |
|
---|
| 4 | <para>
|
---|
| 5 | The first main bootscript is the <filename>/etc/init.d/rc</filename> script.
|
---|
| 6 | Create a new file <filename>/etc/init.d/rc</filename> containing the
|
---|
| 7 | following: </para>
|
---|
| 8 |
|
---|
| 9 | <literallayout>
|
---|
| 10 |
|
---|
| 11 | <userinput>cat > rc << "EOF"</userinput>
|
---|
| 12 | #!/bin/sh
|
---|
| 13 | # Begin /etc/init.d/rc
|
---|
| 14 | #
|
---|
| 15 | # By Jason Pearce - jason.pearce@linux.org
|
---|
| 16 | # Modified by Gerard Beekmans - gerard@linuxfromscratch.org
|
---|
| 17 | # print_error_msg based on ideas by Simon Perreault - nomis80@yahoo.com
|
---|
| 18 |
|
---|
| 19 | #
|
---|
| 20 | # Include the functions declared in the /etc/init.d/functions file
|
---|
| 21 | #
|
---|
| 22 |
|
---|
| 23 | source /etc/init.d/functions
|
---|
| 24 |
|
---|
| 25 | #
|
---|
| 26 | # The print_error_msg function prints an error message when an unforseen
|
---|
| 27 | # error occured that wasn't trapped for some reason by a evaluate_retval
|
---|
| 28 | # call or error checking in different ways.
|
---|
| 29 |
|
---|
| 30 | print_error_msg()
|
---|
| 31 | {
|
---|
| 32 |
|
---|
| 33 | echo
|
---|
| 34 | $FAILURE
|
---|
| 35 | echo -n "You should not read this error message. It means "
|
---|
| 36 | echo "that an unforseen error "
|
---|
| 37 | echo -n "took place and subscript $i exited with "
|
---|
| 38 | echo "a return value "
|
---|
| 39 | echo -n "of $error_value for an unknown reason. If you're able "
|
---|
| 40 | echo "to trace this error down "
|
---|
| 41 | echo -n "to a bug in one of the files provided by this book, "
|
---|
| 42 | echo "please be so kind to "
|
---|
| 43 | echo -n "inform us at lfs-discuss@linuxfromscratch.org"
|
---|
| 44 | $NORMAL
|
---|
| 45 | echo
|
---|
| 46 | echo
|
---|
| 47 | echo "Press a key to continue..."
|
---|
| 48 | read
|
---|
| 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 |
|
---|
| 65 | $debug $*
|
---|
| 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 |
|
---|
| 88 | [ "$1" != "" ] && runlevel=$1
|
---|
| 89 | if [ "$runlevel" = "" ]
|
---|
| 90 | then
|
---|
| 91 | echo "Usage: $0 <runlevel>" >&2
|
---|
| 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
|
---|
| 102 | [ "$previous" = "" ] && previous=N
|
---|
| 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
|
---|
| 122 | [ ! -f $i ] && continue
|
---|
| 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 |
|
---|
| 153 | [ ! -f $previous_start ] &&
|
---|
| 154 | [ ! -f $sysinit_start ] && continue
|
---|
| 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 |
|
---|
| 170 | if [ $error_value != 0 ]
|
---|
| 171 | then
|
---|
| 172 | print_error_msg
|
---|
| 173 | fi
|
---|
| 174 |
|
---|
| 175 | done
|
---|
| 176 | fi
|
---|
| 177 |
|
---|
| 178 | #
|
---|
| 179 | # Now run the START scripts for this runlevel.
|
---|
| 180 | #
|
---|
| 181 |
|
---|
| 182 | for i in /etc/rc$runlevel.d/S*
|
---|
| 183 | do
|
---|
| 184 | [ ! -f $i ] && continue
|
---|
| 185 |
|
---|
| 186 | if [ $previous != N ]
|
---|
| 187 | then
|
---|
| 188 | #
|
---|
| 189 | # Find start script in previous runlevel and stop script in this
|
---|
| 190 | # runlevel.
|
---|
| 191 | #
|
---|
| 192 |
|
---|
| 193 | suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9][0-9]}
|
---|
| 194 | stop=/etc/rc$runlevel.d/K[0-9][0-9][0-9]$suffix
|
---|
| 195 | previous_start=/etc/rc$previous.d/S[0-9][0-9][0-9]$suffix
|
---|
| 196 | #
|
---|
| 197 | # If there is a start script in the previous level and no stop script in
|
---|
| 198 | # this level, we don't have to re-start the service; abort this
|
---|
| 199 | # iteration and start the next one.
|
---|
| 200 | #
|
---|
| 201 |
|
---|
| 202 | [ -f $previous_start ] && [ ! -f $stop ] &&
|
---|
| 203 | continue
|
---|
| 204 | fi
|
---|
| 205 |
|
---|
| 206 | case "$runlevel" in
|
---|
| 207 | 0|6)
|
---|
| 208 |
|
---|
| 209 | #
|
---|
| 210 | # levels 0 and 6 are halt and reboot levels. We don't really start
|
---|
| 211 | # anything here so we call with the 'stop' parameter
|
---|
| 212 | #
|
---|
| 213 |
|
---|
| 214 | startup $i stop
|
---|
| 215 | error_value=$?
|
---|
| 216 | #
|
---|
| 217 | # If the return value of the script is not 0, something went wrong with
|
---|
| 218 | # error checking inside the script. the print_error_msg function will be
|
---|
| 219 | # called and the message plus the return value of the K script will be
|
---|
| 220 | # printed to the screen
|
---|
| 221 | #
|
---|
| 222 |
|
---|
| 223 | if [ $error_value != 0 ]
|
---|
| 224 | then
|
---|
| 225 | print_error_msg
|
---|
| 226 | fi
|
---|
| 227 | ;;
|
---|
| 228 | *)
|
---|
| 229 | startup $i start
|
---|
| 230 | error_value=$?
|
---|
| 231 | #
|
---|
| 232 | # If the return value of the script is not 0, something went wrong with
|
---|
| 233 | # error checking inside the script. the print_error_msg function will be
|
---|
| 234 | # called and the message plus the return value of the K script will be
|
---|
| 235 | # printed to the screen
|
---|
| 236 | #
|
---|
| 237 |
|
---|
| 238 | if [ $error_value != 0 ]
|
---|
| 239 | then
|
---|
| 240 | print_error_msg
|
---|
| 241 | fi
|
---|
| 242 | ;;
|
---|
| 243 | esac
|
---|
| 244 | done
|
---|
| 245 | fi
|
---|
| 246 |
|
---|
| 247 | # End /etc/init.d/rc
|
---|
| 248 | <userinput>EOF</userinput>
|
---|
| 249 |
|
---|
| 250 | </literallayout>
|
---|
| 251 |
|
---|
| 252 | </sect1>
|
---|
| 253 |
|
---|