rc/rc
2024-07-19 21:01:30 +02:00

171 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
#
# /etc/rc: system boot script
#
printf "%s\n" "The system is coming up. Please wait."
# Load configuration
. /etc/rc.conf
# Load functions
. /etc/rc.functions
# Setup a working environment with eudev
# - if /dev is not mounted - mount as a devtmpfs (CONFIG_DEVTMPFS=y)
# - if /dev is mounted (e.g. due to handover from initramfs or
# CONFIG_DEVTMPFS_MOUNT=y), remount with specific options
# - some video drivers require exec access in /dev, thus it's set here
# - for completness, we add few sanity limits (2k non-empty files, 16k inodes)
printinfo "\nMounting /proc.."
if ! /bin/mountpoint -q /proc; then
/bin/mount -t proc none /proc || { printf "$(BOLD "$(RED)")" "[ERROR]"; exit 1; }
fi
printinfo "\nMounting /sys.."
if ! /bin/mountpoint -q /sys; then
/bin/mount -t sysfs none /sys || { printf "$(BOLD "$(RED)")" "[ERROR]"; exit 1; }
fi
printinfo "\nMounting /run.."
if ! /bin/mountpoint -q /run ; then
/bin/mount -n -t tmpfs -o mode="0755,nosuid,nodev,exec" tmpfs /run || { printf "$(BOLD "$(RED)")" "[ERROR]"; exit 1; }
fi
printinfo "\nMounting /dev.."
UDEVOPTS="exec,nosuid,noatime,mode=0755,nr_blocks=2048,nr_inodes=16384"
if ! /bin/mountpoint -q /dev; then
/bin/mount -n -t devtmpfs -o ${UDEVOPTS} dev /dev || { printf "$(BOLD "$(RED)")" "[ERROR]"; exit 1; }
else
/bin/mount -n -o remount,${UDEVOPTS} dev /dev || { printf "$(BOLD "$(RED)")" "[ERROR]"; exit 1; }
fi
printinfo "\nMounting /dev/pts.."
if ! /bin/mountpoint -q /dev/pts; then
/bin/mkdir -m 755 /dev/pts
/bin/mount -t devpts -o noexec,nosuid,gid=tty,mode=0620 devpts /dev/pts || { printf "$(BOLD "$(RED)")" "[ERROR]"; exit 1; }
fi
printinfo "\nMounting /dev/shm.."
if ! /bin/mountpoint -q /dev/shm; then
/bin/mkdir -m 1777 /dev/shm
/bin/mount shm -t tmpfs -o defaults,exec,rw /dev/shm || { printf "$(BOLD "$(RED)")" "[ERROR]"; exit 1; }
fi
printinfo "\nStarting udev.."
/sbin/start_udev || { printf "$(BOLD "$(RED)")" "[ERROR]"; exit 1; }
# Create device-mapper device nodes and scan for LVM volume groups
if [ -x /sbin/lvm ]; then
printinfo "Found lvm, creating device nodes and lvm volumes"
/sbin/vgscan --mknodes --ignorelockingfailure || printf "$(BOLD "$(RED)")" "[ERROR]"
/sbin/vgchange --sysinit -a y || printf "$(BOLD "$(RED)")" "[ERROR]"
fi
# Mount root read-only
printinfo "\nMounting / in read-only mode.."
/bin/mount -o remount,ro / || { printf "$(BOLD "$(RED)")" "[ERROR]"; exit 1; }
if [ -f /forcefsck ]; then
printinfo "\n/forcefsck detected, setting force flag to fsck"
FORCEFSCK="-f"
fi
# Check filesystems
printinfo "\nChecking filesystems.."
/sbin/fsck $FORCEFSCK -A -T -C -a
if [ $? -gt 1 ]; then
printf "\n%s$(BOLD "$(RED)")%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \
"***************" " FILESYSTEM CHECK FAILED " "******************" \
"* *" \
"* Please repair manually and reboot. Note that the root *" \
"* file system is currently mounted read-only. To remount *" \
"* it read-write type: mount -n -o remount,rw / *" \
"* When you exit the maintenance shell the system will *" \
"* reboot automatically. *" \
"* *" \
"************************************************************"
/sbin/sulogin -p
printf "%s\n" "Automatic reboot in progress..."
/bin/umount -a -r
/bin/mount -o remount,ro /
/sbin/reboot -f
exit 0
fi
# Mount local filesystems
printinfo "\nMounting / in read-write mode.."
/bin/mount -o remount,rw / || printf "$(BOLD "$(RED)")" "[ERROR]"
printinfo "\nMounting other filesystems (excl. network devices)..\t"
/bin/mount -a -O no_netdev || printf "$(BOLD "$(RED)")" "[ERROR]"
# Create user dir in /run
printinfo "\nCreating /run/user.."
/bin/mkdir -m 0755 /run/user || printf "$(BOLD "$(RED)")" "[ERROR]"
# Activate swap
printinfo "\nMounting swap.."
/sbin/swapon -a || printf "$(BOLD "$(RED)")" "[ERROR]"
# Clean up misc files
printinfo "\nDeleting temporary files.."
: > /run/utmp || printf "$(BOLD "$(RED)")" "[ERROR]"
/bin/rm -rf /forcefsck /fastboot /etc/nologin /etc/shutdownpid || printf "$(BOLD "$(RED)")" "[ERROR]"
(cd /var/lock && /usr/bin/find . ! -type d -delete) || printf "$(BOLD "$(RED)")" "[ERROR]"
(cd /tmp && /usr/bin/find . ! -name . -delete) || printf "$(BOLD "$(RED)")" "[ERROR]"
/bin/mkdir -m 1777 /tmp/.ICE-unix /tmp/.X11-unix || printf "$(BOLD "$(RED)")" "[ERROR]"
# Set kernel variables
printinfo "\nSetting kernel variables.."
/sbin/sysctl -p > /dev/null || printf "$(BOLD "$(RED)")" "[ERROR]"
# Update shared library links
printinfo "\nUpdating shared library links.."
/sbin/ldconfig || printf "$(BOLD "$(RED)")" "[ERROR]"
# Configure host name
if [ "$HOSTNAME" ]; then
printf "\n$(BOLD "%s") %s\n" "Hostname:" "$HOSTNAME"
/bin/hostname "$HOSTNAME"
fi
# Load random seed
/bin/cat /var/lib/urandom/seed > /dev/urandom
# Configure system clock
if [ "$TIMEZONE" ]; then
/bin/ln -snf /usr/share/zoneinfo/"$TIMEZONE" /etc/localtime
fi
/sbin/hwclock --hctosys
# Default language
printf "$(BOLD "%s") %s\n" "LANG:" "${LANG:-C.UTF-8}"
export LANG
# Load console font
if [ "$FONT" ]; then
printf "$(BOLD "%s") %s\n" "Font:" "$FONT"
/usr/bin/setfont "$FONT"
fi
# Load console keymap
if [ "$KEYMAP" ]; then
printf "$(BOLD "%s") %s\n" "Keyboard:" "$KEYMAP"
/usr/bin/loadkeys -q "$KEYMAP"
fi
# Screen timeout, defaults to 15m
printf "$(BOLD "%s") %s\n" "Screen Timeout:" "${BLANKTIME:-15}"
/usr/bin/setterm -blank "${BLANKTIME:-15}"
# Run module initialization script
if [ -x /etc/rc.modules ]; then
/etc/rc.modules
fi
# Save boot messages
/bin/dmesg > /var/log/boot
# End of file