#!/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 dbus virtlockd after ntp-client ntpd nfs nfsmount portmap rpc.statd iptables ip6tables ebtables corosync sanlock cgconfig xenconsoled samba } 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} "$@" 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 }' } libvirtd_dom_shutdown_all(){ #Request the specified shutdown to all active domains. #$1 == The shutdown mode: none/managedsave/shutdown/... local mode= local dom_id= for mode in ${LIBVIRTD_BACKENDS}; do for dom_id in `libvirtd_dom_list "${mode}"` ; do vm_name="$(libvirtd_virsh ${mode} domname ${dom_id} | head -n 1)" einfo " ${vm_name}" libvirtd_virsh ${mode} ${1} ${dom_id} > /dev/null done done } libvirtd_any_dom_running() { local count= local mode= for mode in ${LIBVIRTD_BACKENDS}; do count=`libvirtd_dom_count "${mode}"` if [ ${count} != "0" ]; then return 0; fi done return 1 } libvirtd_any_net_running() { local count= local mode= for mode in ${LIBVIRTD_BACKENDS}; do count=`libvirtd_net_count "${mode}"` if [ "${count}" != "0" ]; then return 0; fi done return 1 } 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" if [ "${LIBVIRTD_BACKENDS}" == "" ]; then eerror "You must specifiy the used libvirtd backend" eend -1 fi start-stop-daemon --start \ --env KRB5_KTNAME=/etc/libvirt/krb5.tab \ --exec /usr/sbin/libvirtd -- -d ${LIBVIRTD_OPTS} eend $? } stop() { local counter= local vm_name= local net_name= local mode= ebegin "Stopping libvirtd" # try to shutdown all domains if [ "${LIBVIRTD_KVM_SHUTDOWN}" != "none" ] \ && libvirtd_any_dom_running ; then einfo " Shutting down domain(s):" libvirtd_dom_shutdown_all ${LIBVIRTD_KVM_SHUTDOWN} if [ -n "${LIBVIRTD_KVM_SHUTDOWN_MAXWAIT}" ] ; then counter="${LIBVIRTD_KVM_SHUTDOWN_MAXWAIT}" else counter=500 fi if [ "${LIBVIRTD_KVM_SHUTDOWN}" = "shutdown" ]; then einfo " Waiting ${counter} seconds while domains shutdown ..." dom_count= while libvirtd_any_dom_running && [ ${counter} -gt 0 ] ; do sleep 1 counter=$((${counter} - 1)) echo -n "." done echo fi for mode in ${LIBVIRTD_BACKENDS}; do if [ `libvirtd_dom_count "${mode}"` != "0" ] ; then ewarn " !!! Some guests are still running..." fi if [ "${LIBVIRTD_KVM_NO_MANAGEDSAVE}" == "yes" ]; then eerror " ... remaining quests wlll be killed !!!" else if [ "${LIBVIRTD_KVM_SHUTDOWN}" != "managedsave" ]; then einfo " Shutting down with managedsave:" libvirtd_dom_shutdown_all "managedsave" fi fi done fi if [ "${LIBVIRTD_KVM_NET_SHUTDOWN}" != "no" ] \ && libvirtd_any_net_running; then einfo " Shutting down network(s):" for mode in ${LIBVIRTD_BACKENDS}; do for net_name in `libvirtd_net_list "${mode}"`; do einfo " ${net_name}" libvirtd_virsh ${mode} net-destroy ${net_name} > /dev/null done done if libvirtd_any_net_running; 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 }