summaryrefslogtreecommitdiff
blob: 10765f5c0c3cccb193e79f79fba5bf69e1b40733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 $?
}