#!/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/