2012-09-04 18:49:29 +02:00
|
|
|
#!/usr/bin/bash
|
|
|
|
# Init script for libvirtd on Slackware
|
|
|
|
# Written by Matteo Bernardini <ponce@slackbuilds.org>
|
|
|
|
#
|
|
|
|
# Note that a dnsmasq daemon is started by libvirtd itself to serve
|
|
|
|
# its virtual network, and possibly can conflict with a dnsmasq
|
|
|
|
# already running on the system, see
|
|
|
|
# http://wiki.libvirt.org/page/Libvirtd_and_dnsmasq
|
|
|
|
# Note also that the tun, vhost_net and kvm related modules are
|
|
|
|
# automatically loaded at start and removed at stop: edit the
|
|
|
|
# script if this behaviour conflicts with anything else running
|
|
|
|
# on your setup
|
2012-07-30 13:21:07 +02:00
|
|
|
|
|
|
|
MODULES="tun vhost_net"
|
2012-09-04 18:49:29 +02:00
|
|
|
PIDFILE="/var/run/libvirt/libvirtd.pid"
|
2012-07-30 13:21:07 +02:00
|
|
|
TIMEOUT=${TIMEOUT:-40}
|
2012-09-04 18:49:29 +02:00
|
|
|
OPTS=${OPTS:-" -v -f /etc/libvirt/libvirtd.conf -p $PIDFILE "}
|
2012-07-30 13:21:07 +02:00
|
|
|
|
|
|
|
check_running_machines() {
|
|
|
|
|
2014-12-20 06:46:35 +01:00
|
|
|
count=0
|
2012-07-30 13:21:07 +02:00
|
|
|
|
2014-12-20 06:46:35 +01:00
|
|
|
for machine in $(virsh list --name --state-running | grep -v ^$) ; do
|
|
|
|
/usr/sbin/virsh shutdown $machine
|
2012-07-30 13:21:07 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
echo -n "Waiting machines"
|
|
|
|
|
2014-12-20 06:46:35 +01:00
|
|
|
while [ $(virsh list --name --state-running | grep -v ^$ | wc -l) -gt "0" ]; do
|
|
|
|
if [ "$count" -ge "$TIMEOUT" ];then
|
2012-07-30 13:21:07 +02:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
echo -n "."
|
2014-12-20 06:46:35 +01:00
|
|
|
count=$(expr $count + 1)
|
2012-07-30 13:21:07 +02:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
2014-12-20 06:46:35 +01:00
|
|
|
if [ $(virsh list --name --state-running | grep -v ^$ | wc -l) -gt "0" ];then
|
2012-07-30 13:21:07 +02:00
|
|
|
|
|
|
|
echo -n "The following machines are still running, forcing shutdown: "
|
2014-12-20 06:46:35 +01:00
|
|
|
for machine in $(virsh list --name --state-running | grep -v ^$) ; do
|
|
|
|
/usr/sbin/virsh destroy $machine
|
|
|
|
echo -n "$machine "
|
2012-07-30 13:21:07 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
sleep 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
check_processor() {
|
|
|
|
|
|
|
|
egrep 'vmx' /proc/cpuinfo > /dev/null
|
|
|
|
|
|
|
|
if [ "$?" -eq "0" ];then
|
|
|
|
MODULES="$MODULES kvm_intel kvm"
|
|
|
|
fi
|
|
|
|
|
|
|
|
check=$?
|
|
|
|
|
|
|
|
egrep 'svm' /proc/cpuinfo > /dev/null
|
|
|
|
|
|
|
|
if [ "$?" -eq "0" ];then
|
|
|
|
MODULES="$MODULES kvm_amd kvm"
|
|
|
|
fi
|
|
|
|
|
2014-12-20 06:46:35 +01:00
|
|
|
check=$(expr $check + $?)
|
2012-07-30 13:21:07 +02:00
|
|
|
|
|
|
|
if [ $check -eq "2" ];then
|
2014-12-20 06:46:35 +01:00
|
|
|
echo "Your system does not support KVM!"
|
2012-07-30 13:21:07 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
if [ -f $PIDFILE ];then
|
|
|
|
echo "libvirt is already running..."
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-12-13 07:14:53 +01:00
|
|
|
echo "Starting libvirtd: /usr/sbin/libvirtd -d "
|
2013-11-12 01:48:16 +01:00
|
|
|
mkdir -p $(dirname $PIDFILE)
|
2012-07-30 13:21:07 +02:00
|
|
|
check_processor
|
2012-09-04 18:49:29 +02:00
|
|
|
/sbin/modprobe -a $MODULES
|
|
|
|
/usr/sbin/libvirtd -d -l $OPTS
|
2012-07-30 13:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
if [ ! -f $PIDFILE ];then
|
|
|
|
echo "libvirt is not running..."
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
check_running_machines
|
|
|
|
check_processor
|
|
|
|
echo "Stopping libvirtd..."
|
2014-12-20 06:46:35 +01:00
|
|
|
for network in $(/usr/sbin/virsh net-list | tail -n +3 | awk '{print $1}'); do
|
|
|
|
/usr/sbin/virsh net-destroy "$network"
|
2012-09-04 18:49:29 +02:00
|
|
|
done
|
2014-12-20 06:46:35 +01:00
|
|
|
kill -TERM $(cat $PIDFILE)
|
2012-09-04 18:49:29 +02:00
|
|
|
sleep 3
|
2014-12-13 07:14:53 +01:00
|
|
|
/sbin/modprobe -ra $MODULES 2>/dev/null
|
2012-07-30 13:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case $1 in
|
|
|
|
start)
|
|
|
|
start
|
|
|
|
;;
|
|
|
|
stop)
|
|
|
|
stop
|
|
|
|
;;
|
|
|
|
restart)
|
|
|
|
stop
|
|
|
|
sleep 1
|
|
|
|
start
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Usage: $0 (start|stop|restart)"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|