#!/usr/sbin/openrc-run
### BEGIN INIT INFO
# Provides:          mountkernfs
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Mount kernel virtual file systems
# Description:       Mount the initial virtual filesystems under /sys,
#                    /proc and /run.
### END INIT INFO

description="Mount kernel virtual file systems."
extra_started_commands="reload"

depend()
{
	keyword -docker -podman -lxc -prefix -systemd-nspawn -vserver -wsl
	provide mountkernfs
}

. /lib/init/vars.sh
. /lib/init/tmpfs.sh
. /lib/init/mount-functions.sh

mount_filesystems()
{
	MNTMODE="$1"

	mount_run "$MNTMODE"
	mount_lock "$MNTMODE"
	domount "$MNTMODE" proc "" /proc proc "-onodev,noexec,nosuid"

	if grep -E -qs "securityfs\$" /proc/filesystems; then
		domount "$MNTMODE" securityfs "" /sys/kernel/security securityfs
	fi

	if grep -E -qs "sysfs\$" /proc/filesystems; then
		domount "$MNTMODE" sysfs "" /sys sysfs "-onodev,noexec,nosuid"
	fi

	if [ -d /sys/fs/pstore ]; then
		domount "$MNTMODE" pstore "" /sys/fs/pstore pstore ""
	fi

	efivarsmnt=/sys/firmware/efi/efivars
	if test -d $efivarsmnt; then
		domount "$MNTMODE" efivarfs "" $efivarsmnt none ""
	fi
}

start()
{
	mount_filesystems mount_noupdate
	return 0
}

reload()
{
	mount_filesystems remount
	return 0
}

stop()
{
	return 0
}
