summaryrefslogtreecommitdiff
path: root/libraries/libvirt/rc.libvirt
blob: 18cc37405960352b3377e15f10824c8c819fb6b9 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/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"
TIMEOUT=${TIMEOUT:-300}
LIBVIRTD_PIDFILE="/var/run/libvirt/libvirtd.pid"
LIBVIRTD_OPTS=${LIBVIRT_OPTS:-" -v -f /etc/libvirt/libvirtd.conf -p $LIBVIRTD_PIDFILE "}
VIRTLOGD_PIDFILE="/var/run/libvirt/virtlogd.pid"
VIRTLOGD_OPTS=${VIRTLOGD_OPTS:-" -v -f /etc/libvirt/virtlogd.conf -p $VIRTLOGD_PIDFILE "}
VIRTLOCKD_PIDFILE="/var/run/libvirt/virtlockd.pid"
VIRTLOCKD_OPTS=${VIRTLOCKD_OPTS:-" -v -f /etc/libvirt/virtlockd.conf -p $VIRTLOCKD_PIDFILE "}

guests_reboot() {

  for machine in $(/usr/sbin/virsh list --name --state-running | grep -v ^$) ; do
    /usr/sbin/virsh reboot $machine
  done

}

guests_shutdown() {

  for machine in $(/usr/sbin/virsh list --name --state-running | grep -v ^$) ; do
    /usr/sbin/virsh shutdown $machine &
  done

  echo -n "Waiting for guests to finish shutting down"

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

  echo ""

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

    echo -n "The following guests are still running, destroying them: "
    for machine in $(/usr/sbin/virsh list --name --state-running | grep -v ^$) ; do
      /usr/sbin/virsh destroy $machine
      echo -n "$machine "
    done

    sleep 2
  fi

}


guests_managedsave() {
  # apply managedsave on running  and paused machines (as we can't distinguish between
  # the two states while managedsave is being applied, so won't know when to finish waiting)

  count=0

  for machine in $(/usr/sbin/virsh list --name | grep -v ^$) ; do
    /usr/sbin/virsh managedsave $machine &
  done

  echo -n "Waiting for managedsave to finish on all guests"

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

  echo ""

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

    echo -n "The following guests are still running, destroying them: "
    for machine in $(/usr/sbin/virsh list --name | grep -v ^$) ; do
      /usr/sbin/virsh destroy $machine
      echo -n "$machine "
    done

    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 system does not support KVM!"
  fi

}

start_libvirtd() {
  if [ -f $LIBVIRTD_PIDFILE ];then
    echo "libvirt is already running..."
    exit 1
  fi
  echo "Starting libvirtd:  /usr/sbin/libvirtd -d "
  mkdir -p $(dirname $LIBVIRTD_PIDFILE)
  check_processor
  /sbin/modprobe -a $MODULES
  /usr/sbin/libvirtd -d -l $LIBVIRTD_OPTS
}

stop_libvirtd() {
  if [ ! -f $LIBVIRTD_PIDFILE ];then
    echo "libvirt is not running..."
    exit 2
  fi
  guests_managedsave
  check_processor
  echo "Stopping libvirtd..."
  for network in $(/usr/sbin/virsh net-list | tail -n +3 | awk '{print $1}'); do
    /usr/sbin/virsh net-destroy "$network"
  done
  kill -TERM $(cat $LIBVIRTD_PIDFILE)
  sleep 3
  /sbin/modprobe -ra $MODULES 2>/dev/null
}

start_virtlogd() {
  if [ -f $VIRTLOGD_PIDFILE ];then
    echo "virtlogd is already running..."
    exit 1
  fi
  echo "Starting virtlogd:  /usr/sbin/virtlogd -d "
  mkdir -p $(dirname $VIRTLOGD_PIDFILE)
  /usr/sbin/virtlogd -d $VIRTLOGD_OPTS
}

stop_virtlogd() {
  if [ ! -f $VIRTLOGD_PIDFILE ];then
    echo "virtlogd is not running..."
    exit 2
  fi
  echo "Stopping virtlogd..."
  kill -TERM $(cat $VIRTLOGD_PIDFILE)
  sleep 1
}

start_virtlockd() {
  if [ -f $VIRTLOCKD_PIDFILE ];then
    echo "virtlockd is already running..."
    exit 1
  fi
  echo "Starting virtlockd:  /usr/sbin/virtlockd -d "
  mkdir -p $(dirname $VIRTLOCKD_PIDFILE)
  /usr/sbin/virtlockd -d $VIRTLOCKD_OPTS
}

stop_virtlockd() {
  if [ ! -f $VIRTLOCKD_PIDFILE ];then
    echo "virtlockd is not running..."
    exit 2
  fi
  echo "Stopping virtlockd..."
  kill -TERM $(cat $VIRTLOCKD_PIDFILE)
  sleep 1
}

case $1 in
start)
  start_virtlockd
  start_virtlogd
  start_libvirtd
  ;;
stop)
  stop_libvirtd
  stop_virtlogd
  stop_virtlockd
  ;;
restart)
  stop_libvirtd
  stop_virtlogd
  stop_virtlockd
  sleep 2
  start_virtlockd
  start_virtlogd
  start_libvirtd
  ;;
guests_shutdown)
  guests_shutdown
  ;;
guests_reboot)
  guests_reboot
  ;;
*)
  echo "Usage: $0 (start|stop|restart|guests_shutdown|guests_reboot)"
  ;;
esac