bootscripts: rc.startup: Always wait for udev events to finish
[dragora.git] / archive / etc / rc.d / rc.startup
blob746ac345cdd0523ebf6f1f878adf51beac5c68a7
1 #! /bin/sh -
3 # Boot-time system initialization script
5 # Copyright (c) 2017-2021 Matias Fonzo, <selk@dragora.org>.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 RC="[${0##*/}]" # To reflect the name of the script between [].
20 umask 022
21 IFS='
23 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
24 LC_ALL=C
26 ### Mount virtual file systems
28 echo "${RC}: Mounting kernel based file systems"
29 mount -v -n -o nosuid,noexec,nodev -t proc proc /proc
30 mount -v -n -o nosuid,noexec,nodev -t sysfs sysfs /sys
31 if ! mountpoint -q /dev
32 then
33 mount -v -n -t devtmpfs devtmpfs /dev
35 mkdir -p /dev/pts /dev/shm /run
36 mount -v -n -o mode=0620,gid=5 -t devpts devpts /dev/pts
37 mount -v -n -o defaults -t tmpfs none /dev/shm
38 mount -v -n -o defaults -t tmpfs tmpfs /run
39 mkdir -p /run/lock
40 chmod 1777 /run/lock /dev/shm
42 ### ^ End Of 'Mount virtual file systems'
44 ### Start the dynamic device management
46 # Udev handles the uevents itself, so we don't need to have
47 # the kernel call out to any binary in response to them
48 : > /sys/kernel/uevent_helper
49 : > /proc/sys/kernel/hotplug
51 echo "${RC}: Starting event managing daemon: udevd --daemon"
52 udevd --daemon
54 # Request device uevents to replay events at system coldplug
55 udevadm trigger --type=subsystems --action=add
56 udevadm trigger --type=devices --action=add
57 udevadm trigger --type=devices --action=change
59 echo "${RC}: Waiting for pending udev events: udevadm settle --timeout=80"
60 udevadm settle --timeout=80
62 ### ^ End Of 'Start the dynamic device management'
64 # Re-mount the root filesystem in *read-only mode*, if needed
65 if test -w /
66 then
67 echo "${RC}: Remounting root filesystem in read-only mode"
68 mount -v -n -o remount,ro /
71 ### Mount control groups
73 if grep -q cgroup /proc/filesystems
74 then
75 if test -d /sys/fs/cgroup
76 then
77 echo "${RC}: Mounting Cgroup controllers (version 1)"
79 # Cgroup controllers v1 must be mounted against a tmpfs(5)
80 mount -v -n -t tmpfs cgroup_root /sys/fs/cgroup
82 # Comount all v1 controllers against the same hierarchy
83 mount -v -n -t cgroup cgroup /sys/fs/cgroup
87 ### ^ End Of 'Mount control groups'
89 ### Initialization of the Logical Volume Manager (version 2)
91 if lvm version
92 then
93 echo "${RC}: LVM2: Scanning for new volume groups"
94 modprobe dm-mod
95 vgscan --mknodes --ignorelockingfailure 2> /dev/null && \
96 vgchange --ignorelockingfailure -a y
99 ### ^ End Of 'Initialization of the Logical Volume Manager (version 2)'
101 # Activate swap partition if any available
102 swapon -a
104 ### Set system clock (1/2)
106 # Avoid unnecessary fsck runs at boot time, especially for
107 # already existing (multiple) systems not designed for UTC
108 hwclock --hctosys --localtime
110 ### Perform file system checks
112 echo "${RC}: Performing file system checks"
113 if test ! -f /fastboot
114 then
115 # Limit number of filesystem checkers using the max. number of processors
116 FSCK_MAX_INST=$(nproc 2> /dev/null) || FSCK_MAX_INST=1
118 # Force checks if the regular file '/forcefsck' exists
119 test -f /forcefsck && forcefsck_flag=-f
121 echo "*** Checking root file system"
122 fsck $forcefsck_flag -a -C
123 status=$?
125 if test $status -gt 1
126 then
127 if test $status -eq 32
128 then
129 echo "fsck(8) canceled by user request." 1>&2
130 else
131 if test $status -ge 4
132 then
133 printf '%s' 1>&2 \
134 "^ Return status = $status
135 "'A maintenance shell will be started ...
137 If you do not know how to correct the file system errors,
138 please see the filesystem-specific checker manual pages for
139 further details.
141 unset -v FSCK_MAX_INST forcefsck_flag status
142 sulogin || {
143 echo "Executing \`/bin/sh' as emergency shell ..." 1>&2
144 /bin/sh -
147 echo "The system will be restarted now."
148 umount -v -n -a -r
149 mount -v -n -o remount,ro /
150 reboot -n -f;
151 exit 99
154 unset -v status
156 echo "*** Checking local file system(s)"
157 fsck $forcefsck_flag -A -C -R -T -a
159 unset -v FSCK_MAX_INST forcefsck_flag
160 else
161 echo "WARNING: /fastboot is present: Ignoring checks" 1>&2
164 ### ^ End Of 'Perform file system checks'
166 ### Parse config file
168 # The given values on /proc/cmdline have priority
169 # over those present at "/etc/rc.conf"
170 if grep -q -m 1 RC_ /proc/cmdline
171 then
172 tr -s '[:space:]' '[\n*]' < /proc/cmdline | \
173 while IFS='=' read -r variable value
175 case $variable in
176 RC_?*)
177 # Set variable value avoiding possible code execution
178 eval "$variable=\${value}"
180 esac
181 done
183 if test -f /etc/rc.conf
184 then
185 while IFS='=' read -r variable value
187 case $variable in
188 RC_?*)
189 if test -z "$variable"
190 then
191 eval "$variable=\${value}"
194 esac
195 done < /etc/rc.conf
198 # Use default values if the following variables are not defined
200 RC_HWCLOCK="${RC_HWCLOCK:-utc}"
201 RC_HOSTNAME="${RC_HOSTNAME:-dragora}"
202 RC_ESPEAKUP="${RC_ESPEAKUP:-0}"
203 RC_EVOICE="${RC_EVOICE:-default}"
204 RC_KEYMAP="${RC_KEYMAP:-qwerty/us}"
205 RC_BLANKTIME="${RC_BLANKTIME:-15}"
207 ### ^ End Of 'Parse config file'
209 ### Set system clock (2/2)
211 if test "$RC_HWCLOCK" = utc
212 then
213 echo "${RC}: Setting system time from the hardware clock to UTC"
214 hwclock --hctosys --utc
215 else
216 echo "${RC}: The system time was already set to local time" 1>&2
218 unset -v RC_HWCLOCK
220 ### ^ End Of 'Set system clock'
222 # Re-mount the root filesystem in *read-write mode*
224 echo "${RC}: Remounting root filesystem in read-write mode ..."
225 if ! test -w /
226 then
227 mount -v -n -o remount,rw /
228 else
229 echo " The root filesystem is already in read-write mode." 1>&2
232 # Mount local file systems only
234 echo "${RC}: Mounting of local file systems declared in /etc/fstab"
235 mount -v -a -t no,proc,sysfs,devtmpfs,shm,devpts,usbfs,cifs,smbfs,nfs
237 # Activate swap partitions or swap files
239 echo "${RC}: Activating swap devices, if any ..."
240 swapon -v -a
242 # Update kernel modules
244 if test -s "/lib/modules/$(uname -r)/modules.dep"
245 then
246 echo "${RC}: Refreshing kernel module list: depmod --quick"
247 depmod --quick
248 else
249 echo "${RC}: Generating kernel module dependency list: depmod --all"
250 depmod --all
253 # Re-create hierarchy for temporary files (if needed)
255 mkdir -p /tmp /tmp/.font-unix /tmp/.ICE-unix /tmp/.X11-unix
256 chmod 1777 /tmp /tmp/.font-unix /tmp/.ICE-unix /tmp/.X11-unix
258 # Re-create file records for logins and logouts
259 touch \
260 /var/log/btmp /var/log/lastlog /var/log/faillog /var/log/wtmp /var/run/utmp
261 chgrp utmp /var/log/lastlog /var/run/utmp
262 chmod 664 /var/log/lastlog /var/run/utmp
263 chmod 600 /var/log/btmp
265 # Seek and destroy temporary files
267 rm -f /etc/nologin /fastboot /forcefsck
269 # Set default system host name
271 echo "$RC_HOSTNAME" > /proc/sys/kernel/hostname
272 unset -v RC_HOSTNAME
274 # Set kernel parameters at runtime
276 echo "${RC}: Setting kernel parameters: sysctl -q --system"
277 sysctl -q --system
279 # Start the loopback device
281 echo "${RC}: Starting loopback device ..."
282 if ip -Version
283 then
284 ip addr del 127.0.0.1/8 dev lo 2> /dev/null
285 ip addr add 127.0.0.1/8 dev lo brd + scope host
286 ip link set lo up
287 elif ifconfig --version
288 then
289 ifconfig lo up
292 # Initialize the random number generator, see random(4)
294 echo "${RC}: Initializing random number generator"
295 echo " /etc/random-seed <-> /dev/urandom ..."
297 touch /etc/random-seed
298 if test -s /etc/random-seed
299 then
300 cat /etc/random-seed > /dev/urandom
303 # Get the pool size from /proc or use a default value
304 size="$(cat /proc/sys/kernel/random/poolsize)" || size=512
306 # Save seed file containing the whole entropy pool
307 dd if=/dev/urandom of=/etc/random-seed count=1 bs=$size
308 chmod 600 /etc/random-seed; unset -v size
310 ### Configure ISA Plug-and-Play devices (legacy)
312 if command -v isapnp > /dev/null && command -v pnpdump > /dev/null
313 then
314 if test -e /etc/isapnp.conf
315 then
316 echo "${RC}: Setting ISA PnP devices using /etc/isapnp.conf"
317 isapnp /etc/isapnp.conf
318 else
319 echo "${RC}: Creating resource information for ISA PnP devices"
320 pnpdump > /etc/isapnp.conf
322 echo "${RC}: Setting devices from the new /etc/isapnp.conf"
323 isapnp /etc/isapnp.conf
327 ### ^ End Of 'Configure ISA Plug-and-Play devices (legacy)'
329 # Start sound or speech engine
331 if test -x /etc/rc.d/rc.alsa
332 then
333 /etc/rc.d/rc.alsa start
335 if test "$RC_ESPEAKUP" != 0
336 then
337 echo "${RC}: Activating software synthesizer: espeakup -V $RC_EVOICE"
338 modprobe speakup_soft
339 espeakup -V "$RC_EVOICE"
341 unset -v RC_ESPEAKUP RC_EVOICE
343 # Load keyboard map, set terminal attributes
345 echo "${RC}: Loading keyboard map: loadkeys $RC_KEYMAP"
346 loadkeys "$RC_KEYMAP"
347 unset -v RC_KEYMAP
349 echo "${RC}: Setting screen blanking interval: setterm -blank $RC_BLANKTIME"
350 setterm -blank "$RC_BLANKTIME"
351 unset -v RC_BLANKTIME