#!/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, sets the # hostname from /etc/hostname and the machine id, 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 start a daemon in the background: init waits for this # script before it starts the console. LOG=/run/brintos-sysinit _log= _run= # Shell builtins only: every program started here costs about 0.1 s. mounted() { while read -r _dev _dir _rest; do [ "$_dir" = "$1" ] && return 0 done 2>/dev/null "$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 } # machine-id(5): 32 lowercase hex digits. A writable root keeps the first id it # is given. A read-only root is a snapshot every launch shares, so each boot # makes its own in /run and binds it over the empty /etc/machine-id the image # ships. machine_id() { if writable /etc; then _id=$(cat /etc/machine-id 2>/dev/null) case $_id in *[!0-9a-f]*) ;; *) [ ${#_id} = 32 ] && return 0 ;; esac _id=$(tr -d - < /proc/sys/kernel/random/uuid) && printf '%s\n' "$_id" > /etc/machine-id else _id=$(tr -d - < /proc/sys/kernel/random/uuid) && printf '%s\n' "$_id" > /run/machine-id && chmod 0444 /run/machine-id && mount --bind /run/machine-id /etc/machine-id fi } # chown only what root does not already own as asked: root owns what it makes # on a tmpfs. own() { [ "$1:$2" = root:root ] && [ -O "$3" ] && [ -G "$3" ] || chown "$1:$2" "$3" } # Whether link $1 already reaches $2: -ef answers without a program when the # target exists. linked() { case $2 in /*) [ -L "$1" ] && [ "$1" -ef "$2" ] && return 0 ;; esac [ "$(readlink "$1")" = "$2" ] } # /etc/default/volatiles, in populate-volatile.sh's format: # , type d, f, l or b. # The shell strips the comments and splits the fields, and an entry starts only # the programs that change something. A new file is 0644 under umask 022. It is # made with true, not :, whose failed redirection would end the script. volatiles() { _rc=0 for _f in /etc/default/volatiles/*; do [ -f "$_f" ] || continue while read -r _l; do set -f set -- ${_l%%#*} set +f [ $# -ge 6 ] || continue _t=$1 _u=$2 _g=$3 _m=$4 _p=$5 _s=$6 [ -L "$_p" ] && [ ! -e "$_p" ] && _p=$(readlink -f "$_p") case $_t in d) [ -e "$_p" ] || { mkdir -p -m "$_m" "$_p" && own "$_u" "$_g" "$_p"; } ;; f) [ -e "$_p" ] || { if [ "$_s" = none ]; then true > "$_p"; else cp "$_s" "$_p"; fi && own "$_u" "$_g" "$_p" && { [ "$_s:$_m" = none:0644 ] || chmod "$_m" "$_p"; } } ;; l) [ -d "$_p" ] && [ ! -L "$_p" ] || linked "$_p" "$_s" || ln -sfn "$_s" "$_p" ;; b) mounted "$_p" || mount --bind "$_s" "$_p" ;; *) false ;; esac || { echo "brintos-sysinit: $_f: $_t $_p failed" >&2 _rc=1 } done < "$_f" done return $_rc } if [ -e /proc/mounts ]; then note proc kernel else mount -t proc proc /proc note proc "rc=$?" fi # The kernel makes the console busybox init's controlling tty, and a ^C there # makes init run its ::ctrlaltdel action. setsid -c gives the console to a # session that ends at once, until the console shell takes it. if read -r _comm 2>/dev/null &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