rc.startup 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #! /bin/sh -
  2. #
  3. # Boot-time system initialization script
  4. #
  5. # Copyright (c) 2017-2020 Matias Fonzo, <selk@dragora.org>.
  6. #
  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
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  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.
  18. RC="[${0##*/}]" # To reflect the name of the script between [].
  19. umask 022
  20. IFS='
  21. '
  22. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/usr/sbin:/bin:/usr/bin
  23. LC_ALL=C
  24. ### Mount virtual file systems
  25. echo "${RC}: Mounting kernel based file systems"
  26. mount -v -n -o nosuid,noexec,nodev -t proc proc /proc
  27. mount -v -n -o nosuid,noexec,nodev -t sysfs sysfs /sys
  28. mount -v -n -t devtmpfs devtmpfs /dev
  29. mkdir -p /dev/pts /dev/shm /run
  30. mount -v -n -o mode=0620,gid=5 -t devpts devpts /dev/pts
  31. mount -v -n -o defaults -t tmpfs none /dev/shm
  32. mount -v -n -o defaults -t tmpfs tmpfs /run
  33. mkdir -p /run/lock
  34. ### ^ End Of 'Mount virtual file systems'
  35. ### Start the dynamic device management
  36. # Udev handles the uevents itself, so we don't need to have
  37. # the kernel call out to any binary in response to them
  38. : > /sys/kernel/uevent_helper
  39. : > /proc/sys/kernel/hotplug
  40. echo "${RC}: Starting event managing daemon: udevd --daemon"
  41. udevd --daemon
  42. # Request device uevents to replay events at system coldplug
  43. udevadm trigger --type=subsystems --action=add
  44. udevadm trigger --type=devices --action=add
  45. udevadm trigger --type=devices --action=change
  46. echo "${RC}: Waiting for event queue: udevadm settle --timeout=120"
  47. udevadm settle --timeout=120
  48. ### ^ End Of 'Start the dynamic device management'
  49. # Re-mount the root filesystem in *read-only mode*, if needed
  50. if test -w /
  51. then
  52. echo "${RC}: Remounting root filesystem in read-only mode"
  53. mount -v -n -o remount,ro /
  54. fi
  55. ### Mount control groups
  56. if grep -q cgroup /proc/filesystems
  57. then
  58. if test -d /sys/fs/cgroup
  59. then
  60. echo "${RC}: Mounting Cgroup controllers (version 1)"
  61. # Cgroup controllers v1 must be mounted against a tmpfs(5)
  62. mount -v -n -t tmpfs cgroup_root /sys/fs/cgroup
  63. # Comount all v1 controllers against the same hierarchy
  64. mount -v -n -t cgroup cgroup /sys/fs/cgroup
  65. fi
  66. fi
  67. ### ^ End Of 'Mount control groups'
  68. ### Initialization of the Logical Volume Manager (version 2)
  69. if lvm version
  70. then
  71. echo "${RC}: LVM2: Scanning for new volume groups"
  72. modprobe dm-mod
  73. vgscan --mknodes --ignorelockingfailure 2> /dev/null && \
  74. vgchange --ignorelockingfailure -a y
  75. fi
  76. ### ^ End Of 'Initialization of the Logical Volume Manager (version 2)'
  77. # Activate swap partition if any available
  78. swapon -a
  79. ### Set system clock (1/2)
  80. # Avoid unnecessary fsck runs at boot time, especially for
  81. # already existing (multiple) systems not designed for UTC
  82. hwclock --hctosys --localtime
  83. ### Perform file system checks
  84. echo "${RC}: Performing file system checks"
  85. if test ! -f /fastboot
  86. then
  87. # Limit number of filesystem checkers using the max. number of processors
  88. FSCK_MAX_INST=$(nproc 2> /dev/null) || FSCK_MAX_INST=1
  89. # Force checks if the regular file '/forcefsck' exists
  90. test -f /forcefsck && forcefsck_flag=-f
  91. echo "*** Checking root file system"
  92. fsck $forcefsck_flag -a -C
  93. status=$?
  94. if test $status -gt 1
  95. then
  96. if test $status -eq 32
  97. then
  98. echo "fsck(8) canceled by user request." 1>&2
  99. else
  100. if test $status -ge 4
  101. then
  102. printf '%s' 1>&2 \
  103. "^ Return status = $status
  104. "'A maintenance shell will be started ...
  105. If you do not know how to correct the file system errors,
  106. please see the filesystem-specific checker manual pages for
  107. further details.
  108. '
  109. unset -v FSCK_MAX_INST forcefsck_flag status
  110. sulogin || {
  111. echo "Executing \`/bin/sh' as emergency shell ..." 1>&2
  112. /bin/sh -
  113. }
  114. fi
  115. echo "The system will be restarted now."
  116. umount -v -n -a -r
  117. mount -v -n -o remount,ro /
  118. reboot -n -f;
  119. exit 99
  120. fi
  121. fi
  122. unset -v status
  123. echo "*** Checking local file system(s)"
  124. fsck $forcefsck_flag -A -C -R -T -a
  125. unset -v FSCK_MAX_INST forcefsck_flag
  126. else
  127. echo "WARNING: /fastboot is present: Ignoring checks" 1>&2
  128. fi
  129. ### ^ End Of 'Perform file system checks'
  130. ### Parse config file
  131. # The given values on /proc/cmdline have priority
  132. # over those present at "/etc/rc.conf"
  133. if grep -q -m 1 RC_ /proc/cmdline
  134. then
  135. tr -s '[:space:]' '[\n*]' < /proc/cmdline | \
  136. while IFS='=' read -r variable value
  137. do
  138. case $variable in
  139. RC_?*)
  140. # Set variable value avoiding possible code execution
  141. eval "$variable=\${value}"
  142. ;;
  143. esac
  144. done
  145. fi
  146. if test -f /etc/rc.conf
  147. then
  148. while IFS='=' read -r variable value
  149. do
  150. case $variable in
  151. RC_?*)
  152. if test -z "$variable"
  153. then
  154. eval "$variable=\${value}"
  155. fi
  156. ;;
  157. esac
  158. done < /etc/rc.conf
  159. fi
  160. # Use default values if the following variables are not defined
  161. RC_HWCLOCK="${RC_HWCLOCK:-utc}"
  162. RC_HOSTNAME="${RC_HOSTNAME:-dragora}"
  163. RC_ESPEAKUP="${RC_ESPEAKUP:-0}"
  164. RC_EVOICE="${RC_EVOICE:-default}"
  165. RC_KEYMAP="${RC_KEYMAP:-qwerty/us}"
  166. RC_BLANKTIME="${RC_BLANKTIME:-15}"
  167. ### ^ End Of 'Parse config file'
  168. ### Set system clock (2/2)
  169. if test "$RC_HWCLOCK" = utc
  170. then
  171. echo "${RC}: Setting system time from the hardware clock to UTC"
  172. hwclock --hctosys --utc
  173. else
  174. echo "${RC}: The system time was already set to local time" 1>&2
  175. fi
  176. unset -v RC_HWCLOCK
  177. ### ^ End Of 'Set system clock'
  178. # Re-mount the root filesystem in *read-write mode*
  179. echo "${RC}: Remounting root filesystem in read-write mode ..."
  180. if ! test -w /
  181. then
  182. mount -v -n -o remount,rw /
  183. else
  184. echo " The root filesystem is already in read-write mode." 1>&2
  185. fi
  186. # Mount local file systems only
  187. echo "${RC}: Mounting of local file systems declared in /etc/fstab"
  188. mount -v -a -t no,proc,sysfs,devtmpfs,shm,devpts,usbfs,cifs,smbfs,nfs
  189. # Activate swap partitions or swap files
  190. echo "${RC}: Activating swap devices, if any ..."
  191. swapon -v -a
  192. # Update kernel modules
  193. if test -s "/lib/modules/$(uname -r)/modules.dep"
  194. then
  195. echo "${RC}: Refreshing kernel module list: depmod --quick"
  196. depmod --quick
  197. else
  198. echo "${RC}: Generating kernel module dependency list: depmod --all"
  199. depmod --all
  200. fi
  201. # Re-create hierarchy for temporary files (if needed)
  202. mkdir -p /tmp /tmp/.font-unix /tmp/.ICE-unix /tmp/.X11-unix
  203. chmod 1777 /tmp /tmp/.font-unix /tmp/.ICE-unix /tmp/.X11-unix
  204. # Re-create file records for logins and logouts
  205. touch \
  206. /var/log/btmp /var/log/lastlog /var/log/faillog /var/log/wtmp /var/run/utmp
  207. chgrp utmp /var/log/lastlog /var/run/utmp
  208. chmod 664 /var/log/lastlog /var/run/utmp
  209. chmod 600 /var/log/btmp
  210. # Seek and destroy temporary files
  211. rm -f /etc/nologin /fastboot /forcefsck
  212. # Set default system host name
  213. echo "$RC_HOSTNAME" > /proc/sys/kernel/hostname
  214. unset -v RC_HOSTNAME
  215. # Set kernel parameters at runtime
  216. echo "${RC}: Setting kernel parameters: sysctl -q --system"
  217. sysctl -q --system
  218. # Start the loopback device
  219. echo "${RC}: Starting loopback device ..."
  220. if ip -Version
  221. then
  222. ip addr del 127.0.0.1/8 dev lo 2> /dev/null
  223. ip addr add 127.0.0.1/8 dev lo brd + scope host
  224. ip link set lo up
  225. elif ifconfig --version
  226. then
  227. ifconfig lo up
  228. fi
  229. # Initialize the random number generator, see random(4)
  230. echo "${RC}: Initializing random number generator"
  231. echo " /etc/random-seed <-> /dev/urandom ..."
  232. touch /etc/random-seed
  233. if test -s /etc/random-seed
  234. then
  235. cat /etc/random-seed > /dev/urandom
  236. fi
  237. # Get the pool size from /proc or use a default value
  238. size="$(cat /proc/sys/kernel/random/poolsize)" || size=512
  239. # Save seed file containing the whole entropy pool
  240. dd if=/dev/urandom of=/etc/random-seed count=1 bs=$size
  241. unset -v size
  242. chmod 600 /etc/random-seed
  243. ### Configure ISA Plug-and-Play devices (legacy)
  244. if command -v isapnp > /dev/null && command -v pnpdump > /dev/null
  245. then
  246. if test -e /etc/isapnp.conf
  247. then
  248. echo "${RC}: Setting ISA PnP devices using /etc/isapnp.conf"
  249. isapnp /etc/isapnp.conf
  250. else
  251. echo "${RC}: Creating resource information for ISA PnP devices"
  252. pnpdump > /etc/isapnp.conf
  253. echo "${RC}: Setting devices from the new /etc/isapnp.conf"
  254. isapnp /etc/isapnp.conf
  255. fi
  256. fi
  257. ### ^ End Of 'Configure ISA Plug-and-Play devices (legacy)'
  258. # Start sound or speech engine
  259. if test -x /etc/rc.d/rc.alsa
  260. then
  261. /etc/rc.d/rc.alsa start
  262. fi
  263. if test "$RC_ESPEAKUP" != 0
  264. then
  265. echo "${RC}: Activating software synthesizer: espeakup -V $RC_EVOICE"
  266. modprobe speakup_soft
  267. espeakup -V "$RC_EVOICE"
  268. fi
  269. unset -v RC_ESPEAKUP RC_EVOICE
  270. # Load keyboard map, set terminal attributes
  271. echo "${RC}: Loading keyboard map: loadkeys $RC_KEYMAP"
  272. loadkeys "$RC_KEYMAP"
  273. unset -v RC_KEYMAP
  274. echo "${RC}: Setting screen blanking interval: setterm -blank $RC_BLANKTIME"
  275. setterm -blank "$RC_BLANKTIME"
  276. unset -v RC_BLANKTIME