summaryrefslogtreecommitdiff
blob: f13845c6a9b0316df10736623b5b3b78ad1bbd89 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/sbin/runscript

description="Virtual Machine Management daemon (libvirt)"
extra_started_commands="reload halt"
description_halt="Stops the libvirt daemon without stopping your VMs"
description_reload="Restarts the libvirt daemon without stopping your VMs"

depend() {
    USE_FLAG_FIREWALLD
    use USE_FLAG_AVAHI USE_FLAG_ISCSI USE_FLAG_RBD dbus virtlockd
    after ntp-client ntpd nfs nfsmount portmap rpc.statd iptables ip6tables ebtables corosync sanlock cgconfig xenconsoled
}

libvirtd_virsh() {
    local mode=$1
    shift

    # Silence errors because virsh always throws an error about
    # not finding the hypervisor version when connecting to libvirtd
    LC_ALL=C virsh -c ${mode}:///system "$@" 2>/dev/null
}

libvirtd_dom_list() {
    # Make sure that it wouldn't be confused if the domain name
    # contains the word running.
    libvirtd_virsh $1 list | awk '$3 == "running" { print $1 }'
}

libvirtd_dom_count() {
    # Make sure that it wouldn't be confused if the domain name
    # contains the word running.
    libvirtd_virsh $1 list | awk 'BEGIN { count = 0 } \
        $3 == "running" { count++ } \
        END { print count }'
}

libvirtd_net_list() {
    # The purpose of the awk is to avoid networks with 'active' in the name
    libvirtd_virsh $1 net-list | awk '$2 == "active" { print $1 }'
}

libvirtd_net_count() {
    # The purpose of the awk is to avoid networks with 'active' in the name
    libvirtd_virsh $1 net-list | awk 'BEGIN { count = 0 } \
        $2 == "active" { count++ } \
        END { print count }'
}


start() {
    # Test configuration directories in /etc/libvirt/ to be either not
    # present or a directory, i.e. not a regular file, bug #532892
    for dir in lxc nwfilter qemu storage; do
      if [ -f /etc/libvirt/$dir ]; then
        eerror "/etc/libvirt/$dir was created as a regular file. It must be either"
        eerror "a directory or not present for libvirtd to start up successfully."
        return 1
      fi
    done

    ebegin "Starting libvirtd"
    start-stop-daemon --start \
        --env KRB5_KTNAME=/etc/libvirt/krb5.tab \
        --exec /usr/sbin/libvirtd -- -d ${LIBVIRTD_OPTS}
    eend $?
}

stop() {
    local policy=
    local counter=
    local net_policy=
    local vm_name=
    local net_name=
    local dom_id=

    ebegin "Stopping libvirtd"

    if [ "${RC_CMD}" = "restart" -a -n "${LIBVIRTD_KVM_RESTART}" ] ; then
        policy="${LIBVIRTD_KVM_RESTART}"
    else
        policy="${LIBVIRTD_KVM_SHUTDOWN}"
    fi

    # sanitize policy:
    if [ "${policy}" != "none" -a "${policy}" != "managedsave" -a "${policy}" != "shutdown" ] ; then
        if [ -n "${policy}" ] ; then
            eerror " !!! Invalid policy \"${policy}\" specified in LIBVIRTD_KVM_SHUTDOWN/RESTART"
        fi
        if [ "${RC_CMD}" = "restart" ] ; then
          einfo " Using default (restart) policy \"none\" for domains"
          policy="none"
        else
          einfo " Using default policy \"shutdown\" for domains"
          policy="shutdown"
        fi
    fi

    if [ -n "${LIBVIRTD_KVM_SHUTDOWN_MAXWAIT}" ] ; then
        counter="${LIBVIRTD_KVM_SHUTDOWN_MAXWAIT}"
    else
        counter=500
    fi

    if [ "${RC_CMD}" = "restart" -a -n "${LIBVIRTD_KVM_NET_RESTART}" ] ; then
        net_policy="${LIBVIRTD_KVM_NET_RESTART}"
    else
        net_policy="${LIBVIRTD_KVM_NET_SHUTDOWN}"
    fi

    # try to shutdown all (KVM/Qemu) domains
    if [ "${policy}" != "none" ] \
        && [ "$(libvirtd_dom_count qemu)" != "0" ] ; then

        einfo " Shutting down domain(s):"
        for dom_id in $(libvirtd_dom_list qemu) ; do
            vm_name="$(libvirtd_virsh qemu domname ${dom_id} | head -n 1)"
            einfo "   ${vm_name}"
            libvirtd_virsh qemu ${policy} ${dom_id} > /dev/null
        done

        if [ "${policy}" = "shutdown" ]; then
            einfo " Waiting ${counter} seconds while domains shutdown ..."
            DOM_COUNT="$(libvirtd_dom_count qemu)"
            while [ ${DOM_COUNT} -gt 0 ] && [ ${counter} -gt 0 ] ; do
                DOM_COUNT="$(libvirtd_dom_count qemu)"
                sleep 1
                counter=$((${counter} - 1))
                echo -n "."
            done
        fi

        if [ "$(libvirtd_dom_count qemu)" != "0" ] ; then
            eerror " !!! Some guests are still running, stopping anyway"
        fi
    fi

    # try to shutdown all networks
    if [ "${net_policy}" != "no" ] \
        && [ "$(libvirtd_net_count qemu)" != "0" ]; then

        einfo " Shutting down network(s):"
        for net_name in $(libvirtd_net_list qemu); do
            einfo "   ${net_name}"
            libvirtd_virsh qemu net-destroy ${net_name} > /dev/null
        done

        if [ "$(libvirtd_net_count qemu)" != "0" ]; then
            eerror " !!! Some networks are still active, stopping anyway"
        fi
    fi

    # Now actually stop the daemon
    start-stop-daemon --stop --quiet --exec \
        /usr/sbin/libvirtd --pidfile=/var/run/libvirtd.pid
    eend $?
}

halt() {
    ebegin "Stopping libvirtd without shutting down your VMs"
    start-stop-daemon --stop --quiet --exec \
        /usr/sbin/libvirtd --pidfile=/var/run/libvirtd.pid
    eend $?
}

reload() {
    halt
    start
}