Skip to content

Instantly share code, notes, and snippets.

@geki-yaba
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geki-yaba/905dac9ccbfe448d121b to your computer and use it in GitHub Desktop.
Save geki-yaba/905dac9ccbfe448d121b to your computer and use it in GitHub Desktop.
vdevd openrc init script
# /etc/conf.d/vdevd
VDEV_BIN="/sbin/vdevd"
VDEV_OPTS=""
VDEV_CONFIG="/etc/vdev/vdevd.conf"
VDEV_MOUNTPOINT="/dev"
#!/sbin/openrc-run
description="vdev device manager"
extra_commands="once"
depend()
{
provide dev
need dev-mount sysfs
before checkfs fsck
}
# load an ini file as a set of namespaced environment variables, echoing them to stdout
# $1 is the path to the file
# $2 is the namespace to be prepended to each variable name
vdevd_source_config()
{
local FILE_PATH NAMESPACE line KEY VALUE
FILE_PATH=$1
NAMESPACE=$2
/bin/cat $FILE_PATH | /bin/sed -r "s/.*\[.*\].*//g" | \
while read line; do
KEY=$(echo $line | /bin/sed -r "s/(^.*)=.*/\1/g");
VALUE=$(echo $line | /bin/sed -r "s/^.*=(.*)$/\1/g");
if [ -n "$KEY" ]; then
echo "${NAMESPACE}${KEY}=${VALUE}"
fi
done
}
vdevd_tests()
{
if [ ! -x ${VDEV_BIN} ]; then
eerror "Daemon not executable"
return 1
fi
# system-wide vdevd needs /sys to be mounted
if [ ! -d /sys/class/ ]; then
eerror "Daemon requires a mounted sysfs, not started"
return 1
fi
# system-wide vdevd needs "modern" sysfs
if [ -d /sys/class/mem/null -a ! -L /sys/class/mem/null ] || \
[ -e /sys/block -a ! -e /sys/class/block ]; then
ewarn "CONFIG_SYSFS_DEPRECATED must not be selected"
ewarn "Booting will continue in 30 seconds but many things will be broken"
sleep 30
fi
# load vdev config variables as vdev_config_*
vdevd_source_config "${VDEV_CONFIG}" "vdev_config_"
# config sanity check
if [ -z "$vdev_config_pidfile" ]; then
eerror "No PID file defined in ${VDEV_CONFIG}. Please set the pidfile= directive."
eerror "You will be unable stop or restart vdevd with this script."
return 1
fi
return 0
}
start()
{
vdevd_tests || return 1
# clear uevent helper--vdevd subsumes its role
if [ -w /sys/kernel/uevent_helper ]; then
echo "" > /sys/kernel/uevent_helper
fi
# set the SELinux context for devices created in the initramfs
[ -x /sbin/restorecon ] && /sbin/restorecon -R ${VDEV_MOUNTPOINT}
# make sure log directory exists...
if [ -n "${vdev_config_logfile}" ]; then
vdev_log_dir=$(echo "${vdev_config_logfile}" | sed -r 's/[^/]+$//g')
if [ -n "${vdev_log_dir}" ]; then
mkdir -p "${vdev_log_dir}"
fi
fi
ebegin "Starting ${description}"
start-stop-daemon --start \
--exec ${VDEV_BIN} \
--pidfile ${vdev_config_pidfile} \
--make-pidfile \
-- -c ${VDEV_CONFIG} ${VDEV_OPTS} ${VDEV_MOUNTPOINT}
# --quiet --background
# --user ${VDEV_USER:-nobody} \
# --group ${VDEV_GROUP:-nobody} \
eend $?
}
stop()
{
vdevd_tests || return 1
ebegin "Stopping ${description}"
start-stop-daemon --stop \
--exec ${VDEV_BIN} \
--pidfile ${vdev_config_pidfile} \
--retry SIGTERM/5
eend $?
}
once()
{
ebegin "Run once ${description}"
elog "(Re)seeding device files"
${VDEV_BIN} -c ${VDEV_CONFIG} --once ${VDEV_OPTS} ${VDEV_MOUNTPOINT}
eend $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment