#!/bin/sh # Mount what a brintOS boot keeps in memory: /run and /var/volatile on every # boot, as /etc/fstab declares them, /dev/shm for glibc's POSIX semaphores and # shared memory, and, when the directory is read-only, a tmpfs seeded from the # image over /etc, /home, each login's home elsewhere, /var/spool, /var/cache # and /var/lib/AccountsService, so what the image ships stays visible. /etc is # covered whole: the settings daemons and useradd write a file beside the one # they replace and rename it, which needs the directory writable. A snapshot # launch on brintos.io boots with a read-only root; a writable launch keeps its # directories. Run once per boot, before anything writes to these directories. # Shell builtins wherever they will do: every program started here costs about # 0.1 s. # The mount points, read from /proc/mounts once; each mount made here is added # with seen. _mnt=' ' [ -r /proc/mounts ] && while read -r _dev _dir _rest; do _mnt="$_mnt$_dir " done /dev/null || return 1 rmdir "$_p" 2>/dev/null return 0 } # The mount point a directory is under, in _fs. Fails when the path is relative # or goes through a symlink, which the mount table cannot place. fs_of() { case $1 in /*) ;; *) return 1 ;; esac _fs=/ _q= _r=${1#/} while [ -n "$_r" ]; do _q=$_q/${_r%%/*} case $_r in */*) _r=${_r#*/} ;; *) _r= ;; esac [ -L "$_q" ] && return 1 mounted "$_q" && _fs=$_q done return 0 } # writable, probed once per file system: a snapshot refuses every write to its # root, and a writable launch none. _fs_rw=' ' _fs_ro=' ' writable_fs() { fs_of "$1" || { writable "$1"; return; } case $_fs_rw in *" $_fs "*) return 0 ;; esac case $_fs_ro in *" $_fs "*) return 1 ;; esac if writable "$1"; then _fs_rw="$_fs_rw$_fs " return 0 fi _fs_ro="$_fs_ro$_fs " return 1 } # Give $2 the mode and owner of $1. same_attr() { chown -h "$(stat -c %u:%g "$1")" "$2" && { [ -L "$2" ] || chmod "$(stat -c %a "$1")" "$2"; } } # Copy the tree under $1 into $2 with GNU tar, which reads with read(2). # busybox tar, cp and cat copy through sendfile(2), which on a file not yet # read this boot never returns or leaks a restart sentinel, so without GNU tar # the copy goes entry by entry, the file data through dd. # BRINTOS-WORKAROUND: glibc#14 remove -- when sendfile(2) no longer leaks the restart sentinel, any tar will do. seed() { if tar --version 2>/dev/null | grep -q 'GNU tar'; then tar -C "$1" -cf - . | tar -C "$2" -xpf - else (cd "$1" && find . ! -name .) | while IFS= read -r _e; do if [ -L "$1/$_e" ]; then ln -s "$(readlink "$1/$_e")" "$2/$_e" elif [ -d "$1/$_e" ]; then mkdir "$2/$_e" elif [ -f "$1/$_e" ]; then dd if="$1/$_e" of="$2/$_e" bs=64k 2>/dev/null else continue fi && same_attr "$1/$_e" "$2/$_e" done fi [ "$(cd "$1" && find . | sort)" = "$(cd "$2" && find . | sort)" ] || echo "brintos-volatile: $1: not every entry was copied" >&2 } # The directory's mode and owner, as tmpfs mount options. dir_opts() { set -- $(stat -c '%a %u %g' "$1" 2>/dev/null) [ $# = 3 ] && echo "mode=$1,uid=$2,gid=$3" } # A tmpfs of its own over a directory: mounted at a staging point, seeded, then # moved. cover_dir() { [ -d "$1" ] && ! mounted "$1" && ! writable_fs "$1" || return 0 _n=$((_n + 1)) _s=/run/brintos-volatile/dir$_n mkdir "$_s" && mount -t tmpfs -o "$(dir_opts "$1")" tmpfs "$_s" && seed "$1" "$_s" && mount --move "$_s" "$1" && seen "$1" && rmdir "$_s" } # /var/ from a copy in /var/volatile, bound over it. cover_var() { [ -d "/var/$1" ] && ! mounted "/var/$1" && ! writable_fs "/var/$1" || return 0 case $1 in */*) mkdir -p "/var/volatile/${1%/*}" || return 1 ;; esac mkdir "/var/volatile/$1" && same_attr "/var/$1" "/var/volatile/$1" && seed "/var/$1" "/var/volatile/$1" && mount --bind "/var/volatile/$1" "/var/$1" && seen "/var/$1" } mounted /run || { mount -t tmpfs -o mode=0755,nodev,nosuid,strictatime tmpfs /run && seen /run; } || exit 1 _vv= if mounted /var/volatile || { mount -t tmpfs -o mode=0755 tmpfs /var/volatile && seen /var/volatile; }; then _vv=1 fi # The 1777 directories in one mkdir. sem_open(3) and shm_open(3) are files # under /dev/shm. The kernel populates /dev and mounts nothing there. _d= [ -d /run/lock ] || _d=/run/lock [ -d /dev/shm ] || _d="$_d /dev/shm" [ -z "$_vv" ] || [ -d /var/volatile/tmp ] || _d="$_d /var/volatile/tmp" [ -z "$_d" ] || mkdir -m 1777 $_d if [ -d /dev/shm ] && ! mounted /dev/shm; then mount -t tmpfs -o mode=1777,nosuid,nodev tmpfs /dev/shm && seen /dev/shm fi [ -z "$_vv" ] || [ -d /var/volatile/log ] || mkdir /var/volatile/log [ -d /run/brintos-volatile ] || mkdir -m 0700 /run/brintos-volatile _n=0 cover_dir /etc # The whole of /home, so useradd -m can make a new login's home; a home under # it is then writable and needs no tmpfs of its own. cover_dir /home _homes= [ -r /etc/passwd ] && while IFS=: read -r _name _pw _uid _gid _gecos _home _rest; do case $_uid in '' | *[!0-9]*) continue ;; esac [ "$_uid" -eq 0 ] || { [ "$_uid" -ge 1000 ] && [ "$_uid" -lt 65534 ]; } || continue [ -n "$_home" ] && [ "$_home" != / ] && _homes="$_homes $_home" done