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 [].
23 PATH
=/usr
/local
/sbin
:/usr
/local
/bin
:/sbin
:/usr
/sbin
:/bin
:/usr
/bin
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
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
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"
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
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
75 if test -d /sys
/fs
/cgroup
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)
93 echo "${RC}: LVM2: Scanning for new volume groups"
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
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
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
125 if test $status -gt 1
127 if test $status -eq 32
129 echo "fsck(8) canceled by user request." 1>&2
131 if test $status -ge 4
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
141 unset -v FSCK_MAX_INST forcefsck_flag status
143 echo "Executing \`/bin/sh' as emergency shell ..." 1>&2
147 echo "The system will be restarted now."
149 mount
-v -n -o remount
,ro
/
156 echo "*** Checking local file system(s)"
157 fsck
$forcefsck_flag -A -C -R -T -a
159 unset -v FSCK_MAX_INST forcefsck_flag
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
172 tr -s '[:space:]' '[\n*]' < /proc
/cmdline | \
173 while IFS
='=' read -r variable value
177 # Set variable value avoiding possible code execution
178 eval "$variable=\${value}"
183 if test -f /etc
/rc.conf
185 while IFS
='=' read -r variable value
189 if test -z "$variable"
191 eval "$variable=\${value}"
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
213 echo "${RC}: Setting system time from the hardware clock to UTC"
214 hwclock --hctosys --utc
216 echo "${RC}: The system time was already set to local time" 1>&2
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 ..."
227 mount
-v -n -o remount
,rw
/
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 ..."
242 # Update kernel modules
244 if test -s "/lib/modules/$(uname -r)/modules.dep"
246 echo "${RC}: Refreshing kernel module list: depmod --quick"
249 echo "${RC}: Generating kernel module dependency list: 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
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
274 # Set kernel parameters at runtime
276 echo "${RC}: Setting kernel parameters: sysctl -q --system"
279 # Start the loopback device
281 echo "${RC}: Starting loopback device ..."
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
287 elif ifconfig
--version
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
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
314 if test -e /etc
/isapnp.conf
316 echo "${RC}: Setting ISA PnP devices using /etc/isapnp.conf"
317 isapnp
/etc
/isapnp.conf
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
333 /etc
/rc.d
/rc.alsa start
335 if test "$RC_ESPEAKUP" != 0
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"
349 echo "${RC}: Setting screen blanking interval: setterm -blank $RC_BLANKTIME"
350 setterm
-blank "$RC_BLANKTIME"
351 unset -v RC_BLANKTIME