summaryrefslogtreecommitdiff
path: root/libraries/libvirt/rc.libvirt
blob: 8cda2bb939075e89dafaf59c98bd96abe502197b (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
#!/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

MODULES="tun vhost_net"
PIDFILE="/var/run/libvirt/libvirtd.pid"
NETNAME="$(ls -1 /var/lib/libvirt/network | cut -d. -f1)"
TIMEOUT=${TIMEOUT:-40}
OPTS=${OPTS:-" -v -f /etc/libvirt/libvirtd.conf -p $PIDFILE "}

check_running_machines() {

  i=0

  for j in `/usr/sbin/virsh list | grep running | awk '{print $2;}'` ; do
    /usr/sbin/virsh shutdown $j
  done

  echo -n "Waiting machines"

  while [ $(/usr/sbin/virsh list | grep running | wc -l) -gt "0" ]; do
    if [ "$i" -ge "$TIMEOUT" ];then
      break
    fi
    echo -n "."
    i=`expr $i + 1`
    sleep 1
  done

  echo ""

  if [ $(/usr/sbin/virsh list | grep running | wc -l) -gt "0" ];then

    echo -n "The following machines are still running, forcing shutdown: "
    for j in `/usr/sbin/virsh list | grep running | awk '{print $2;}'` ; do
      /usr/sbin/virsh destroy $j
      echo -n "$j "
    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

  check=`expr $check + $?`

  if [ $check -eq "2" ];then
    echo "Your systems does not support KVM!"
  fi

}

start() {
  if [ -f $PIDFILE ];then
    echo "libvirt is already running..."
    exit 1
  fi
  echo "Starting libvirtd..."
  check_processor
  /sbin/modprobe -a $MODULES
  /usr/sbin/libvirtd -d -l $OPTS
}

stop() {
  if [ ! -f $PIDFILE ];then
    echo "libvirt is not running..."
    exit 2
  fi
  check_running_machines
  check_processor
  echo "Stopping libvirtd..."
  for i in "$NETNAME"; do
    /usr/sbin/virsh net-destroy "$i"
  done
  kill -TERM `cat $PIDFILE`
  sleep 3
  /sbin/modprobe -ra $MODULES
}

case $1 in
start)
  start
  ;;
stop)
  stop
  ;;
restart)
  stop
  sleep 1
  start
  ;;
*)
  echo "Usage: $0 (start|stop|restart)"
  ;;
esac