#!/bin/sh

# PROVIDE: libvirtd
# REQUIRE: LOGIN virtlogd
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf[.local] to enable libvirtd.
#
# libvirtd_enable (bool):       Set to 'YES' to enable
#                               Default: NO
#
# libvirtd_flags (str):         Custom additional arguments to be passed to libvirtd.
#                               For example, to make it listen on TCP ports, set it
#                               to "--listen".
#                               Default: ""
# libvirtd_hostvalidate_enable (bool): Run virt-host-validate(1) before starting
#                               libvirtd.
#                               Default: "YES"
#

. /etc/rc.subr

name=libvirtd
rcvar=libvirtd_enable

start_precmd="libvirtd_hostvalidate"

load_rc_config $name

: ${libvirtd_enable:="NO"}
: ${libvirtd_flags:=""}
: ${libvirtd_hostvalidate_enable:="YES"}

command=/usr/local/sbin/libvirtd
pidfile=/var/run/${name}.pid

command_args="--daemon --pid-file=${pidfile}"

PATH="${PATH}:/usr/local/sbin:/usr/local/bin"

extra_commands="reload"

libvirtd_hostvalidate()
{
	if checkyesno libvirtd_hostvalidate_enable; then
		# virt-host-validate(1) returns a non-zero exit code when
		# one or more checks fail. As many of the checks do not
		# prevent libvirtd from working, but only limit some
		# features (e.g. console access or pf firewalling), treat
		# failures as diagnostics rather than preventing libvirtd
		# from starting.
		/usr/local/bin/virt-host-validate || :
	fi
}

run_rc_command "$1"
