summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app-emulation/libvirt/files/libvirtd.init')
-rwxr-xr-xapp-emulation/libvirt/files/libvirtd.init73
1 files changed, 73 insertions, 0 deletions
diff --git a/app-emulation/libvirt/files/libvirtd.init b/app-emulation/libvirt/files/libvirtd.init
new file mode 100755
index 0000000..10765f5
--- /dev/null
+++ b/app-emulation/libvirt/files/libvirtd.init
@@ -0,0 +1,73 @@
+#!/sbin/runscript
+
+opts="start stop status reload restart"
+
+depend() {
+ need net
+ before sshd ntp-client ntpd nfs nfsmount rsyncd portmap dhcp
+}
+
+libvirtd_virsh() {
+ # Silence errors because virsh always throws an error about
+ # not finding the hypervisor version when connecting to libvirtd
+ LC_ALL=C virsh -c qemu:///system "$@" 2>/dev/null
+}
+
+libvirtd_dom_list() {
+ libvirtd_virsh list | grep running | awk '{ print $1 }'
+}
+
+libvirtd_dom_count() {
+ libvirtd_dom_list | wc -l
+}
+
+start() {
+ ebegin "Starting libvirtd"
+ start-stop-daemon --start --quiet --exec /usr/sbin/libvirtd -- -d ${LIBVIRTD_OPTS}
+ eend $?
+}
+
+stop() {
+ ebegin "Stopping libvirtd"
+ # try to shutdown all (KVM/Qemu) domains
+ DOM_COUNT="$(libvirtd_dom_count)"
+ if [ "${LIBVIRTD_KVM_SHUTDOWN}" = "yes" ] \
+ && [ "${DOM_COUNT}" != "0" ] ; then
+
+ einfo " Shutting down domain(s):"
+ for DOM_ID in $(libvirtd_dom_list) ; do
+ NAME="$(libvirtd_virsh domname ${DOM_ID} | head -n 1)"
+ einfo " ${NAME}"
+ libvirtd_virsh shutdown ${DOM_ID} > /dev/null
+ done
+
+ if [ -n "${LIBVIRTD_KVM_SHUTDOWN_MAXWAIT}" ] ; then
+ COUNTER="${LIBVIRTD_KVM_SHUTDOWN_MAXWAIT}"
+ else
+ COUNTER=100
+ fi
+
+ einfo " Waiting ${COUNTER} seconds while domains shutdown ..."
+ DOM_COUNT="$(libvirtd_dom_count)"
+ while [ ${DOM_COUNT} -gt 0 ] && [ ${COUNTER} -gt 0 ] ; do
+ DOM_COUNT="$(libvirtd_dom_count)"
+ sleep 1
+ COUNTER=$((${COUNTER} - 1))
+ echo -n "."
+ done
+
+ DOM_COUNT="$(libvirtd_dom_count)"
+ if [ "${DOM_COUNT}" != "0" ] ; then
+ eerror " !!! Some guests are still running, stopping anyways"
+ fi
+
+ fi
+ start-stop-daemon --stop --quiet --exec /usr/sbin/libvirtd --pidfile=/var/run/libvirtd.pid
+ eend $?
+}
+
+reload() {
+ ebegin "Reloading libvirtd"
+ start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/libvirtd.pid --oknodo
+ eend $?
+}