[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
|
---|
[f874424] | 21 | MSG="FAILURE:\n\nYou should not be reading this error message.\n\n"
|
---|
| 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"
|
---|
| 25 |
|
---|
| 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
|
---|
[f874424] | 39 | log_warning_msg "${i} is not a valid symlink."
|
---|
[0cda898] | 40 | continue
|
---|
| 41 | fi
|
---|
| 42 |
|
---|
| 43 | if [ ! -x ${i} ]; then
|
---|
[f874424] | 44 | log_warning_msg "${i} is not executable, skipping."
|
---|
[0cda898] | 45 | continue
|
---|
| 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"}
|
---|
| 87 | DISTRO_CONTACT=${DISTRO_CONTACT:-"lfs-dev@linuxfromscratch.org (Registration required)"}
|
---|
| 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
|
---|
[f874424] | 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
|
---|
[d163ada] | 112 | if [ "$runlevel" == "S" ]; then
|
---|
| 113 | [ -r /etc/sysconfig/console ] && source /etc/sysconfig/console
|
---|
| 114 | dmesg -n "${LOGLEVEL:-7}"
|
---|
| 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
|
---|
| 145 | [ -r /var/run/interactive ] && source /var/run/interactive
|
---|
| 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
|
---|
| 153 |
|
---|
| 154 | suffix=${i#/etc/rc.d/rc$runlevel.d/K[0-9][0-9]}
|
---|
| 155 | prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
|
---|
[f874424] | 156 | sysinit_start=/etc/rc.d/rcS.d/S[0-9][0-9]$suffix
|
---|
[0cda898] | 157 |
|
---|
[9813d5e3] | 158 | if [ "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
|
---|
| 159 | if [ ! -f ${prev_start} -a ! -f ${sysinit_start} ]; then
|
---|
[f874424] | 160 | MSG="WARNING:\n\n${i} can't be "
|
---|
| 161 | MSG="${MSG}executed because it was not "
|
---|
| 162 | MSG="${MSG}not started in the previous "
|
---|
| 163 | MSG="${MSG}runlevel (${previous})."
|
---|
| 164 | log_warning_msg "$MSG"
|
---|
[0cda898] | 165 | continue
|
---|
| 166 | fi
|
---|
| 167 | fi
|
---|
[f874424] | 168 |
|
---|
| 169 | run ${i} stop
|
---|
[0cda898] | 170 | error_value=${?}
|
---|
| 171 |
|
---|
[f874424] | 172 | if [ "${error_value}" != "0" ]; then print_error_msg; fi
|
---|
[0cda898] | 173 | done
|
---|
[1c48007] | 174 | fi
|
---|
| 175 |
|
---|
[f874424] | 176 | if [ "${previous}" == "N" ]; then export IN_BOOT=1; fi
|
---|
| 177 |
|
---|
[9813d5e3] | 178 | if [ "$runlevel" == "6" -a -n "${FASTBOOT}" ]; then
|
---|
[f874424] | 179 | touch /fastboot
|
---|
[13e7d79] | 180 | fi
|
---|
[0cda898] | 181 |
|
---|
[f874424] | 182 |
|
---|
| 183 | # Start all functions in this runlevel
|
---|
[0cda898] | 184 | for i in $( ls -v /etc/rc.d/rc${runlevel}.d/S* 2> /dev/null)
|
---|
[1c48007] | 185 | do
|
---|
[0cda898] | 186 | if [ "${previous}" != "N" ]; then
|
---|
| 187 | suffix=${i#/etc/rc.d/rc$runlevel.d/S[0-9][0-9]}
|
---|
| 188 | stop=/etc/rc.d/rc$runlevel.d/K[0-9][0-9]$suffix
|
---|
| 189 | prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
|
---|
| 190 |
|
---|
[9813d5e3] | 191 | [ -f ${prev_start} -a ! -f ${stop} ] && continue
|
---|
[0cda898] | 192 | fi
|
---|
| 193 |
|
---|
| 194 | check_script_status
|
---|
| 195 |
|
---|
| 196 | case ${runlevel} in
|
---|
| 197 | 0|6)
|
---|
[f874424] | 198 | run ${i} stop
|
---|
[0cda898] | 199 | ;;
|
---|
| 200 | *)
|
---|
[f874424] | 201 | run ${i} start
|
---|
[0cda898] | 202 | ;;
|
---|
| 203 | esac
|
---|
[f874424] | 204 |
|
---|
[0cda898] | 205 | error_value=${?}
|
---|
| 206 |
|
---|
[f874424] | 207 | if [ "${error_value}" != "0" ]; then print_error_msg; fi
|
---|
[1c48007] | 208 | done
|
---|
| 209 |
|
---|
[9813d5e3] | 210 | # Store interactive variable on switch from runlevel S and remove if not
|
---|
| 211 | if [ "${runlevel}" == "S" -a "${interactive}" == "i" ]; then
|
---|
| 212 | echo "interactive=\"i\"" > /var/run/interactive
|
---|
| 213 | else
|
---|
| 214 | rm -f /var/run/interactive 2> /dev/null
|
---|
| 215 | fi
|
---|
| 216 |
|
---|
[f874424] | 217 | # Copy the boot log on initial boot only
|
---|
[a64fd5a] | 218 | if [ "${previous}" == "N" -a "${runlevel}" != "S" ]; then
|
---|
[cba2d4e] | 219 | cat $BOOTLOG >> /var/log/boot.log
|
---|
[a64fd5a] | 220 |
|
---|
| 221 | # Mark the end of boot
|
---|
| 222 | echo "--------" >> /var/log/boot.log
|
---|
| 223 |
|
---|
| 224 | # Remove the temporary file
|
---|
[cba2d4e] | 225 | rm -f $BOOTLOG 2> /dev/null
|
---|
[f874424] | 226 | fi
|
---|
| 227 |
|
---|
[0cda898] | 228 | # End rc
|
---|