#!/bin/sh # Early boot, once per boot: busybox init's one ::sysinit action. Mounts what # the kernel has not, stacks a tmpfs on /tmp over the kernel's, then runs the # memory-backed directories, /etc/default/volatiles, mdev, the network and the # deferred maintainer scripts, in that order. Each step is recorded in # /run/brintos-sysinit as " ". The maintainer scripts run until a # boot completes them, and are then recorded as "done". On a read-only root they # cannot change anything, and are recorded as "read-only" without running. # # Nothing here may call setsid or cttyhack, or start a daemon in the # background: this runtime lacks the syscalls, and init waits for this script # before it starts the console. LOG=/run/brintos-sysinit _log= mounted() { awk -v m="$1" '$2 == m { f = 1 } END { exit !f }' /proc/mounts 2>/dev/null } # The log reaches /run once /run is a tmpfs; the earlier steps are kept until then. note() { _log="$_log$1 $2 " mounted /run && printf '%s' "$_log" > "$LOG" } # Run a step if its program is installed, and record how it ended. step() { _name=$1 shift if [ -x "$1" ]; then "$@" _rc=$? note "$_name" "rc=$_rc" else _rc=absent note "$_name" absent fi } # A write probe, not test -w: on a snapshot the guest's superblock stays # read-write and only the backend refuses. A mkdir that reports failure but # leaves the directory (linux#73) means the directory is writable. writable() { _p=$1/.brintos-sysinit.$$ mkdir "$_p" 2>/dev/null || [ -d "$_p" ] || return 1 rmdir "$_p" 2>/dev/null return 0 } # /etc/default/volatiles, in populate-volatile.sh's format: # , type d, f, l or b. volatiles() { _rc=0 for _f in /etc/default/volatiles/*; do [ -f "$_f" ] || continue sed 's/#.*//' "$_f" | { _bad=0 while read -r _t _u _g _m _p _s; do [ -n "$_s" ] || continue [ -L "$_p" ] && [ ! -e "$_p" ] && _p=$(readlink -f "$_p") case $_t in d) [ -e "$_p" ] || { mkdir -p "$_p" && chown "$_u:$_g" "$_p" && chmod "$_m" "$_p"; } ;; f) [ -e "$_p" ] || { if [ "$_s" = none ]; then : > "$_p"; else cp "$_s" "$_p"; fi && chown "$_u:$_g" "$_p" && chmod "$_m" "$_p" } ;; l) [ -d "$_p" ] && [ ! -L "$_p" ] || [ "$(readlink "$_p")" = "$_s" ] || ln -sfn "$_s" "$_p" ;; b) mounted "$_p" || mount --bind "$_s" "$_p" ;; *) false ;; esac || { echo "brintos-sysinit: $_f: $_t $_p failed" >&2 _bad=1 } done exit $_bad } || _rc=1 done return $_rc } if [ -e /proc/mounts ]; then note proc kernel else mount -t proc proc /proc note proc "rc=$?" fi if mounted /sys; then note sys kernel else mount -t sysfs sysfs /sys note sys "rc=$?" fi mount -t tmpfs -o mode=1777,nosuid,nodev tmpfs /tmp note tmp "rc=$?" if mounted /run; then note run mounted else mount -t tmpfs -o mode=0755,nodev,nosuid,strictatime tmpfs /run note run "rc=$?" fi step volatile /sbin/brintos-volatile if [ -d /etc/default/volatiles ]; then volatiles note volatiles "rc=$?" else note volatiles absent fi # A coldplug scan only: mdev's own init script starts it as a daemon through # start-stop-daemon -b, which needs setsid. step mdev /sbin/mdev -s step network /etc/init.d/brintos-network start # run-postinsts disables itself only by removing rc.d links, and this image has # none, so a stamp keeps it to the boots before it first succeeds. POSTINSTS_DONE=/var/lib/brintos-sysinit/run-postinsts-done if [ -e "$POSTINSTS_DONE" ]; then note run-postinsts done elif [ -x /usr/sbin/run-postinsts ] && [ -d /var/lib ] && ! writable /var/lib; then echo "brintos-sysinit: run-postinsts skipped: /var/lib is read-only" >&2 note run-postinsts read-only else step run-postinsts /usr/sbin/run-postinsts [ "$_rc" = 0 ] && mkdir -p "${POSTINSTS_DONE%/*}" 2>/dev/null && touch "$POSTINSTS_DONE" 2>/dev/null fi exit 0