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