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 ${DISTRO_MINI} book,\n"
|
---|
28 | MSG="${MSG}please be so kind to inform us at ${DISTRO_CONTACT}.\n"
|
---|
29 | log_failure_msg "${MSG}"
|
---|
30 |
|
---|
31 | log_info_msg "Press Enter to continue..."
|
---|
32 | wait_for_user
|
---|
33 | }
|
---|
34 |
|
---|
35 | check_script_status()
|
---|
36 | {
|
---|
37 | # $i is set when called
|
---|
38 | if [ ! -f ${i} ]; then
|
---|
39 | log_warning_msg "${i} is not a valid symlink."
|
---|
40 | SCRIPT_STAT="1"
|
---|
41 | fi
|
---|
42 |
|
---|
43 | if [ ! -x ${i} ]; then
|
---|
44 | log_warning_msg "${i} is not executable, skipping."
|
---|
45 | SCRIPT_STAT="1"
|
---|
46 | fi
|
---|
47 | }
|
---|
48 |
|
---|
49 | run()
|
---|
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@lists.linuxfromscratch.org (Registration required)"}
|
---|
88 | DISTRO_MINI=${DISTRO_MINI:-"LFS"}
|
---|
89 | IPROMPT=${IPROMPT:-"no"}
|
---|
90 |
|
---|
91 | # These 3 signals will not cause our script to exit
|
---|
92 | trap "" INT QUIT TSTP
|
---|
93 |
|
---|
94 | [ "${1}" != "" ] && runlevel=${1}
|
---|
95 |
|
---|
96 | if [ "${runlevel}" == "" ]; then
|
---|
97 | echo "Usage: ${0} <runlevel>" >&2
|
---|
98 | exit 1
|
---|
99 | fi
|
---|
100 |
|
---|
101 | previous=${PREVLEVEL}
|
---|
102 | [ "${previous}" == "" ] && previous=N
|
---|
103 |
|
---|
104 | if [ ! -d /etc/rc.d/rc${runlevel}.d ]; then
|
---|
105 | log_info_msg "/etc/rc.d/rc${runlevel}.d does not exist.\n"
|
---|
106 | exit 1
|
---|
107 | fi
|
---|
108 |
|
---|
109 | if [ "$runlevel" == "6" -o "$runlevel" == "0" ]; then IPROMPT="no"; fi
|
---|
110 |
|
---|
111 | # Note: In ${LOGLEVEL:-7}, it is ':' 'dash' '7', not minus 7
|
---|
112 | if [ "$runlevel" == "S" ]; then
|
---|
113 | [ -r /etc/sysconfig/console ] && source /etc/sysconfig/console
|
---|
114 | dmesg -n "${LOGLEVEL:-7}"
|
---|
115 | fi
|
---|
116 |
|
---|
117 | if [ "${IPROMPT}" == "yes" -a "${runlevel}" == "S" ]; then
|
---|
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}"}
|
---|
121 |
|
---|
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
|
---|
129 | wcol=$(( ( ${COLUMNS} - ${wlen} ) / 2 ))
|
---|
130 | icol=$(( ( ${COLUMNS} - ${ilen} ) / 2 ))
|
---|
131 | itime=${itime:-"3"}
|
---|
132 |
|
---|
133 | echo -e "\n\n"
|
---|
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 |
|
---|
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 /run/interactive ] && source /run/interactive
|
---|
146 |
|
---|
147 | # Attempt to stop all services started by the previous runlevel,
|
---|
148 | # and killed in this runlevel
|
---|
149 | if [ "${previous}" != "N" ]; then
|
---|
150 | for i in $(ls -v /etc/rc.d/rc${runlevel}.d/K* 2> /dev/null)
|
---|
151 | do
|
---|
152 | check_script_status
|
---|
153 | if [ "${SCRIPT_STAT}" == "1" ]; then
|
---|
154 | SCRIPT_STAT="0"
|
---|
155 | continue
|
---|
156 | fi
|
---|
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
|
---|
160 | sysinit_start=/etc/rc.d/rcS.d/S[0-9][0-9]$suffix
|
---|
161 |
|
---|
162 | if [ "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
|
---|
163 | if [ ! -f ${prev_start} -a ! -f ${sysinit_start} ]; then
|
---|
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"
|
---|
169 | continue
|
---|
170 | fi
|
---|
171 | fi
|
---|
172 |
|
---|
173 | run ${i} stop
|
---|
174 | error_value=${?}
|
---|
175 |
|
---|
176 | if [ "${error_value}" != "0" ]; then print_error_msg; fi
|
---|
177 | done
|
---|
178 | fi
|
---|
179 |
|
---|
180 | if [ "${previous}" == "N" ]; then export IN_BOOT=1; fi
|
---|
181 |
|
---|
182 | if [ "$runlevel" == "6" -a -n "${FASTBOOT}" ]; then
|
---|
183 | touch /fastboot
|
---|
184 | fi
|
---|
185 |
|
---|
186 |
|
---|
187 | # Start all functions in this runlevel
|
---|
188 | for i in $( ls -v /etc/rc.d/rc${runlevel}.d/S* 2> /dev/null)
|
---|
189 | do
|
---|
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 |
|
---|
195 | [ -f ${prev_start} -a ! -f ${stop} ] && continue
|
---|
196 | fi
|
---|
197 |
|
---|
198 | check_script_status
|
---|
199 | if [ "${SCRIPT_STAT}" == "1" ]; then
|
---|
200 | SCRIPT_STAT="0"
|
---|
201 | continue
|
---|
202 | fi
|
---|
203 |
|
---|
204 | case ${runlevel} in
|
---|
205 | 0|6)
|
---|
206 | run ${i} stop
|
---|
207 | ;;
|
---|
208 | *)
|
---|
209 | run ${i} start
|
---|
210 | ;;
|
---|
211 | esac
|
---|
212 |
|
---|
213 | error_value=${?}
|
---|
214 |
|
---|
215 | if [ "${error_value}" != "0" ]; then print_error_msg; fi
|
---|
216 | done
|
---|
217 |
|
---|
218 | # Store interactive variable on switch from runlevel S and remove if not
|
---|
219 | if [ "${runlevel}" == "S" -a "${interactive}" == "i" ]; then
|
---|
220 | echo "interactive=\"i\"" > /run/interactive
|
---|
221 | else
|
---|
222 | rm -f /run/interactive 2> /dev/null
|
---|
223 | fi
|
---|
224 |
|
---|
225 | # Copy the boot log on initial boot only
|
---|
226 | if [ "${previous}" == "N" -a "${runlevel}" != "S" ]; then
|
---|
227 | cat $BOOTLOG >> /var/log/boot.log
|
---|
228 |
|
---|
229 | # Mark the end of boot
|
---|
230 | echo "--------" >> /var/log/boot.log
|
---|
231 |
|
---|
232 | # Remove the temporary file
|
---|
233 | rm -f $BOOTLOG 2> /dev/null
|
---|
234 | fi
|
---|
235 |
|
---|
236 | # End rc
|
---|