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