[arch-general] [arch-dev-public] Migration to systemd (api mounts)
Michal Soltys
soltys at ziu.info
Fri Aug 17 20:35:50 EDT 2012
On 2012-08-15 17:11, Thomas Bächler wrote:
>
> So, with initscripts, we mount all the API file systems manually. When
> you put them in fstab as well, things fail. But when you want special
> options for those file systems, you won't get them.
>
> This very short systemd snippet showed that systemd is capable of
> parsing fstab and remounting the API file systems such that all your
> options are respected.
>
> We tried this with initscripts before, and we couldn't make it work, as
> it was too complicated with shell scripts.
As I always watch arch mailing list with interest (systemd adventures in
particular) this grabbed my attention (not arch user anymore, but
irrelevant). Leaving whole systemd vs. sysv discussion aside - it's
perfectly doable.
Not a trivial few-liner, but actually something barely bigger and not
really complex - with help of some bash features. Important thing is how
mount behaves depending if both the "device" (irrelevant with api
mounts, but the rules stand) and the mountpoint are specified - or -
only the mountpoint.
That is - assuming the logic you need is essentially:
1) if in fstab, use its options, else use standard/manual options
2) if mounted (e.g. initramfs handover), remount, else mount
Below is a fragment of bash (generally v4+ required, but can be toned
down easily) function which is also responsile for handling basic
mounts. For the record - shegrep(), ismnt() are in-bash few-liners to
avoid dependency on external commands - short loops over input and
/proc/self/mountinfo (inc. \040 conversion) respectively. The snippet
below even checks if the required stuff is compiled in the kernel
(though these days it can be just removed, hard to imagine system
without tmpfs of devtmpfs ...).
Either way, maybe this approach will be of use for you.
local _fstab _x
# $1 mount point to check against pre-read fstab
function infstab() {
local _i
for ((_i = 0; _i < ${#_fstab[@]} ; i++)); do
[[ ${_fstab[_i]} =~ ^[^#][^[:blank:]]*[[:blank:]]+([^[:blank:]]+) ]] || continue
[[ ${BASH_REMATCH[1]} = "${1// /\040}" ]] && return 0
done
return 1
}
# $1 comma separated fs to try in turn
# $2 opts
# $3 dir
function vmnt() {
local _fs _opt _dev _x
[[ -d $3 ]] || return
# comma separated list is a bit paranoid, we should just assume
# that stuff is properly compiled in kernel, API stuff in particular
IFS=, eval _fs='( $1 )'
for _x in "${_fs[@]}" ""; do
shegrep -q "[[:blank:]]$_x$" </proc/filesystems && break
done
[[ -z $_x ]] && return
# /paranoid
# use options and "device" only if not in fstab
infstab "$3" || { _opt=$2; _dev=$_x; }
if ismnt "$3"; then
_opt="-o remount${_opt:+,$_opt}"
unset _fs
else
_opt="${_opt:+-o $_opt}"
_fs="-t $_x"
fi
mount $_fs $_opt $_dev "$3"
}
# set proper time w.r.t. tz (this does not access rtc !)
# this requires /etc/localtime to make sense (file or symlink+/usr
# mounted)
if [[ $HWCLKMODE = localtime && -r /etc/localtime ]]; then
hwclock --localtime --systz --noadjfile
fi
# we assure /proc mount, then vmnt handles it (fstab opts and crap)
# hard to live without proc :) and we have stuff depending on it
[[ -d /proc/self ]] || mount -t proc -o "nosuid,noexec,nodev" proc /proc
# preread fstab(s)
mapfile -t _fstab < <(shopt -s nullglob; for _x in /etc/fsta[b] /etc/fstab.d/*; do echo "$(< "$_x")"; done)
# early mounts
# - might be handed over from initramfs, might be not
# - might have sane mount options, might have not
# - might be in fstab, might be not
vmnt proc "nosuid,noexec,nodev" /proc
vmnt sysfs "nosuid,noexec,nodev" /sys
vmnt devtmpfs,tmpfs "mode=0755,nosuid" /dev
mkdir -p -m 0755 /dev/{pts,shm}
vmnt tmpfs "mode=1777,nosuid,nodev" /dev/shm
vmnt devpts "mode=0620,gid=tty,nosuid,noexec" /dev/pts
vmnt tmpfs "mode=0755,nosuid,nodev,exec" /run
vmnt tmpfs "mode=0755,nosuid,nodev,exec" /sys/fs/cgroup
... and so on, whatever might be needed
There're also few other things that are often claimed as hard/impossible
with bash scripts - e.g. resilience against broken (hanging) rc scripts
with banal help of mkfifo or full device detach with help of pivot_root
- doing it in the same way systemd does (certainly it requires proper
initramfs generator leaving necessary stuff in /run/initramfs to pivot
to - or - something analogous or hell - even created in fly by shutdown
script).
More information about the arch-general
mailing list