#!/bin/sh
# Remounts / and /usr with their options from /etc/fstab, like
# systemd-remount-fs; / without an entry, and / in a container, only becomes
# read-write.
infstab() {
	[ -r /etc/fstab ] || return 1
	while read -r spec mp rest; do
		case $spec in '' | \#*) continue ;; esac
		[ "$mp" = "$1" ] && return 0
	done </etc/fstab
	return 1
}
r=0
if [ ! -s /run/init_env/container ] && infstab /; then
	mount -o remount / || r=1
else
	case ",$(findmnt -n -o OPTIONS /)," in
	*,ro,*) mount -o remount,rw / || r=1 ;;
	esac
fi
if [ ! -s /run/init_env/container ] && mountpoint -q /usr && infstab /usr; then
	mount -o remount /usr || r=1
fi
echo "remount-root: / $(findmnt -n -o OPTIONS /)"
exit $r
