[d163ada] | 1 | #!/bin/bash
|
---|
[1c48007] | 2 | ########################################################################
|
---|
[0cda898] | 3 | # Begin rc
|
---|
[1c48007] | 4 | #
|
---|
| 5 | # Description : Main Run Level Control Script
|
---|
| 6 | #
|
---|
| 7 | # Authors : Gerard Beekmans - gerard@linuxfromscratch.org
|
---|
[f874424] | 8 | # : DJ Lucas - dj@linuxfromscratch.org
|
---|
[0cda898] | 9 | # Update : Bruce Dubbs - bdubbs@linuxfromscratch.org
|
---|
[1c48007] | 10 | #
|
---|
[0cda898] | 11 | # Version : LFS 7.0
|
---|
[1c48007] | 12 | #
|
---|
| 13 | ########################################################################
|
---|
| 14 |
|
---|
[f874424] | 15 | . /lib/lsb/init-functions
|
---|
[0cda898] | 16 |
|
---|
[2f0d64d] | 17 | print_error_msg()
|
---|
[0cda898] | 18 | {
|
---|
[f874424] | 19 | log_failure_msg
|
---|
[0cda898] | 20 | # $i is set when called
|
---|
[a44ae577] | 21 | MSG="FAILURE:\n\nYou should not be reading this error message.\n\n"
|
---|
[f874424] | 22 | MSG="${MSG}It means that an unforeseen error took place in\n"
|
---|
| 23 | MSG="${MSG}${i},\n"
|
---|
| 24 | MSG="${MSG}which exited with a return value of ${error_value}.\n"
|
---|
[a44ae577] | 25 |
|
---|
[f874424] | 26 | MSG="${MSG}If you're able to track this error down to a bug in one of\n"
|
---|
[e6b6261] | 27 | MSG="${MSG}the files provided by the ${DISTRO_MINI} book,\n"
|
---|
| 28 | MSG="${MSG}please be so kind to inform us at ${DISTRO_CONTACT}.\n"
|
---|
[f874424] | 29 | log_failure_msg "${MSG}"
|
---|
| 30 |
|
---|
| 31 | log_info_msg "Press Enter to continue..."
|
---|
[0cda898] | 32 | wait_for_user
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[2f0d64d] | 35 | check_script_status()
|
---|
[0cda898] | 36 | {
|
---|
| 37 | # $i is set when called
|
---|
| 38 | if [ ! -f ${i} ]; then
|
---|
[a44ae577] | 39 | log_warning_msg "${i} is not a valid symlink."
|
---|
[b875e19] | 40 | SCRIPT_STAT="1"
|
---|
[0cda898] | 41 | fi
|
---|
| 42 |
|
---|
| 43 | if [ ! -x ${i} ]; then
|
---|
[f874424] | 44 | log_warning_msg "${i} is not executable, skipping."
|
---|
[b875e19] | 45 | SCRIPT_STAT="1"
|
---|
[0cda898] | 46 | fi
|
---|
| 47 | }
|
---|
[1c48007] | 48 |
|
---|
[2f0d64d] | 49 | run()
|
---|
[f874424] | 50 | {
|
---|
| 51 | if [ -z $interactive ]; then
|
---|
| 52 | ${1} ${2}
|
---|
| 53 | return $?
|
---|
| 54 | fi
|
---|
| 55 |
|
---|
| 56 | while true; do
|
---|
| 57 | read -p "Run ${1} ${2} (Yes/no/continue)? " -n 1 runit
|
---|
| 58 | echo
|
---|
| 59 |
|
---|
| 60 | case ${runit} in
|
---|
| 61 | c | C)
|
---|
| 62 | interactive=""
|
---|
| 63 | ${i} ${2}
|
---|
| 64 | ret=${?}
|
---|
| 65 | break;
|
---|
| 66 | ;;
|
---|
| 67 |
|
---|
| 68 | n | N)
|
---|
| 69 | return 0
|
---|
| 70 | ;;
|
---|
| 71 |
|
---|
| 72 | y | Y)
|
---|
| 73 | ${i} ${2}
|
---|
| 74 | ret=${?}
|
---|
| 75 | break
|
---|
| 76 | ;;
|
---|
| 77 | esac
|
---|
| 78 | done
|
---|
| 79 |
|
---|
| 80 | return $ret
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | # Read any local settings/overrides
|
---|
| 84 | [ -r /etc/sysconfig/rc.site ] && source /etc/sysconfig/rc.site
|
---|
| 85 |
|
---|
| 86 | DISTRO=${DISTRO:-"Linux From Scratch"}
|
---|
[fb651b9] | 87 | DISTRO_CONTACT=${DISTRO_CONTACT:-"lfs-dev@lists.linuxfromscratch.org (Registration required)"}
|
---|
[f874424] | 88 | DISTRO_MINI=${DISTRO_MINI:-"LFS"}
|
---|
[701d877e] | 89 | IPROMPT=${IPROMPT:-"no"}
|
---|
[1c48007] | 90 |
|
---|
| 91 | # These 3 signals will not cause our script to exit
|
---|
| 92 | trap "" INT QUIT TSTP
|
---|
| 93 |
|
---|
| 94 | [ "${1}" != "" ] && runlevel=${1}
|
---|
| 95 |
|
---|
[9813d5e3] | 96 | if [ "${runlevel}" == "" ]; then
|
---|
[0cda898] | 97 | echo "Usage: ${0} <runlevel>" >&2
|
---|
| 98 | exit 1
|
---|
[1c48007] | 99 | fi
|
---|
| 100 |
|
---|
| 101 | previous=${PREVLEVEL}
|
---|
[9813d5e3] | 102 | [ "${previous}" == "" ] && previous=N
|
---|
[1c48007] | 103 |
|
---|
[0cda898] | 104 | if [ ! -d /etc/rc.d/rc${runlevel}.d ]; then
|
---|
[a44ae577] | 105 | log_info_msg "/etc/rc.d/rc${runlevel}.d does not exist.\n"
|
---|
[0cda898] | 106 | exit 1
|
---|
[1c48007] | 107 | fi
|
---|
| 108 |
|
---|
[9813d5e3] | 109 | if [ "$runlevel" == "6" -o "$runlevel" == "0" ]; then IPROMPT="no"; fi
|
---|
| 110 |
|
---|
| 111 | # Note: In ${LOGLEVEL:-7}, it is ':' 'dash' '7', not minus 7
|
---|
[a44ae577] | 112 | if [ "$runlevel" == "S" ]; then
|
---|
| 113 | [ -r /etc/sysconfig/console ] && source /etc/sysconfig/console
|
---|
| 114 | dmesg -n "${LOGLEVEL:-7}"
|
---|
[d163ada] | 115 | fi
|
---|
[f874424] | 116 |
|
---|
[642e2e9] | 117 | if [ "${IPROMPT}" == "yes" -a "${runlevel}" == "S" ]; then
|
---|
[701d877e] | 118 | # The total length of the distro welcome string, without escape codes
|
---|
| 119 | wlen=${wlen:-$(echo "Welcome to ${DISTRO}" | wc -c )}
|
---|
| 120 | welcome_message=${welcome_message:-"Welcome to ${INFO}${DISTRO}${NORMAL}"}
|
---|
[f874424] | 121 |
|
---|
[701d877e] | 122 | # The total length of the interactive string, without escape codes
|
---|
| 123 | ilen=${ilen:-$(echo "Press 'I' to enter interactive startup" | wc -c )}
|
---|
| 124 | i_message=${i_message:-"Press '${FAILURE}I${NORMAL}' to enter interactive startup"}
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | # dcol and icol are spaces before the message to center the message
|
---|
| 128 | # on screen. itime is the amount of wait time for the user to press a key
|
---|
[f874424] | 129 | wcol=$(( ( ${COLUMNS} - ${wlen} ) / 2 ))
|
---|
| 130 | icol=$(( ( ${COLUMNS} - ${ilen} ) / 2 ))
|
---|
[701d877e] | 131 | itime=${itime:-"3"}
|
---|
[f874424] | 132 |
|
---|
[9813d5e3] | 133 | echo -e "\n\n"
|
---|
[f874424] | 134 | echo -e "\\033[${wcol}G${welcome_message}"
|
---|
| 135 | echo -e "\\033[${icol}G${i_message}${NORMAL}"
|
---|
| 136 | echo ""
|
---|
| 137 | read -t "${itime}" -n 1 interactive 2>&1 > /dev/null
|
---|
| 138 | fi
|
---|
| 139 |
|
---|
[9813d5e3] | 140 | # Make lower case
|
---|
| 141 | [ "${interactive}" == "I" ] && interactive="i"
|
---|
| 142 | [ "${interactive}" != "i" ] && interactive=""
|
---|
| 143 |
|
---|
| 144 | # Read the state file if it exists from runlevel S
|
---|
[02776dff] | 145 | [ -r /run/interactive ] && source /run/interactive
|
---|
[9813d5e3] | 146 |
|
---|
[0cda898] | 147 | # Attempt to stop all services started by the previous runlevel,
|
---|
[1c48007] | 148 | # and killed in this runlevel
|
---|
| 149 | if [ "${previous}" != "N" ]; then
|
---|
[0cda898] | 150 | for i in $(ls -v /etc/rc.d/rc${runlevel}.d/K* 2> /dev/null)
|
---|
| 151 | do
|
---|
| 152 | check_script_status
|
---|
[b875e19] | 153 | if [ "${SCRIPT_STAT}" == "1" ]; then
|
---|
| 154 | SCRIPT_STAT="0"
|
---|
| 155 | continue
|
---|
| 156 | fi
|
---|
[0cda898] | 157 |
|
---|
| 158 | suffix=${i#/etc/rc.d/rc$runlevel.d/K[0-9][0-9]}
|
---|
| 159 | prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
|
---|
[f874424] | 160 | sysinit_start=/etc/rc.d/rcS.d/S[0-9][0-9]$suffix
|
---|
[0cda898] | 161 |
|
---|
[9813d5e3] | 162 | if [ "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
|
---|
| 163 | if [ ! -f ${prev_start} -a ! -f ${sysinit_start} ]; then
|
---|
[f874424] | 164 | MSG="WARNING:\n\n${i} can't be "
|
---|
| 165 | MSG="${MSG}executed because it was not "
|
---|
| 166 | MSG="${MSG}not started in the previous "
|
---|
| 167 | MSG="${MSG}runlevel (${previous})."
|
---|
| 168 | log_warning_msg "$MSG"
|
---|
[0cda898] | 169 | continue
|
---|
| 170 | fi
|
---|
| 171 | fi
|
---|
[f874424] | 172 |
|
---|
| 173 | run ${i} stop
|
---|
[0cda898] | 174 | error_value=${?}
|
---|
| 175 |
|
---|
[f874424] | 176 | if [ "${error_value}" != "0" ]; then print_error_msg; fi
|
---|
[0cda898] | 177 | done
|
---|
[1c48007] | 178 | fi
|
---|
| 179 |
|
---|
[f874424] | 180 | if [ "${previous}" == "N" ]; then export IN_BOOT=1; fi
|
---|
| 181 |
|
---|
[9813d5e3] | 182 | if [ "$runlevel" == "6" -a -n "${FASTBOOT}" ]; then
|
---|
[f874424] | 183 | touch /fastboot
|
---|
[13e7d79] | 184 | fi
|
---|
[0cda898] | 185 |
|
---|
[f874424] | 186 |
|
---|
| 187 | # Start all functions in this runlevel
|
---|
[0cda898] | 188 | for i in $( ls -v /etc/rc.d/rc${runlevel}.d/S* 2> /dev/null)
|
---|
[1c48007] | 189 | do
|
---|
[0cda898] | 190 | if [ "${previous}" != "N" ]; then
|
---|
| 191 | suffix=${i#/etc/rc.d/rc$runlevel.d/S[0-9][0-9]}
|
---|
| 192 | stop=/etc/rc.d/rc$runlevel.d/K[0-9][0-9]$suffix
|
---|
| 193 | prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
|
---|
| 194 |
|
---|
[9813d5e3] | 195 | [ -f ${prev_start} -a ! -f ${stop} ] && continue
|
---|
[0cda898] | 196 | fi
|
---|
| 197 |
|
---|
| 198 | check_script_status
|
---|
[b875e19] | 199 | if [ "${SCRIPT_STAT}" == "1" ]; then
|
---|
| 200 | SCRIPT_STAT="0"
|
---|
| 201 | continue
|
---|
| 202 | fi
|
---|
[0cda898] | 203 |
|
---|
| 204 | case ${runlevel} in
|
---|
| 205 | 0|6)
|
---|
[f874424] | 206 | run ${i} stop
|
---|
[0cda898] | 207 | ;;
|
---|
| 208 | *)
|
---|
[f874424] | 209 | run ${i} start
|
---|
[0cda898] | 210 | ;;
|
---|
| 211 | esac
|
---|
[f874424] | 212 |
|
---|
[0cda898] | 213 | error_value=${?}
|
---|
| 214 |
|
---|
[f874424] | 215 | if [ "${error_value}" != "0" ]; then print_error_msg; fi
|
---|
[1c48007] | 216 | done
|
---|
| 217 |
|
---|
[9813d5e3] | 218 | # Store interactive variable on switch from runlevel S and remove if not
|
---|
| 219 | if [ "${runlevel}" == "S" -a "${interactive}" == "i" ]; then
|
---|
[02776dff] | 220 | echo "interactive=\"i\"" > /run/interactive
|
---|
[9813d5e3] | 221 | else
|
---|
[02776dff] | 222 | rm -f /run/interactive 2> /dev/null
|
---|
[9813d5e3] | 223 | fi
|
---|
| 224 |
|
---|
[f874424] | 225 | # Copy the boot log on initial boot only
|
---|
[a64fd5a] | 226 | if [ "${previous}" == "N" -a "${runlevel}" != "S" ]; then
|
---|
[cba2d4e] | 227 | cat $BOOTLOG >> /var/log/boot.log
|
---|
[a44ae577] | 228 |
|
---|
[a64fd5a] | 229 | # Mark the end of boot
|
---|
| 230 | echo "--------" >> /var/log/boot.log
|
---|
[a44ae577] | 231 |
|
---|
[a64fd5a] | 232 | # Remove the temporary file
|
---|
[cba2d4e] | 233 | rm -f $BOOTLOG 2> /dev/null
|
---|
[f874424] | 234 | fi
|
---|
| 235 |
|
---|
[0cda898] | 236 | # End rc
|
---|