Statistics
| Branch: | Tag: | Revision:

root / doc / examples / ganeti-kvm-poweroff.initd.in @ f7b769b1

History | View | Annotate | Download (1.9 kB)

1
#!/bin/bash
2
# ganeti kvm instance poweroff
3
# based on skeleton from Debian GNU/Linux
4
### BEGIN INIT INFO
5
# Provides:          ganeti-kvm-poweroff
6
# Required-Start:
7
# Required-Stop:     drbd qemu-kvm $local_fs
8
# Default-Start:
9
# Default-Stop: 0 1 6
10
# Short-Description: Poweroff Ganeti KVM instances
11
# Description: Sends system_powerdown command to Ganeti instances, otherwise
12
# they will be killed.
13
### END INIT INFO
14

    
15
shopt -s nullglob
16

    
17
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
18
DESC="Ganeti KVM instance poweroff "
19

    
20
. /lib/lsb/init-functions
21

    
22
CONTROL_PATH="@LOCALSTATEDIR@/run/ganeti/kvm-hypervisor/ctrl"
23
SCRIPTNAME="@SYSCONFDIR@/init.d/ganeti-kvm-poweroff"
24
TIMEOUT=60
25

    
26
do_kvm_poweroff () {
27
    # shutdown VMs and remove sockets of those not running
28
    for vm_monitor in $CONTROL_PATH/*.monitor; do
29
        if ! echo system_powerdown | \
30
            socat -U UNIX:$vm_monitor STDIO > /dev/null 2>&1; then
31
            # remove disconnected socket
32
            rm -f $vm_monitor
33
        fi
34
    done
35

    
36
    log_action_begin_msg "Waiting VMs to poweroff"
37
    waiting=true
38
    remaning=$TIMEOUT
39
    while $waiting && [ $remaning -ne 0 ]; do
40
        if [[ -z "$(find $CONTROL_PATH -name '*.monitor')" ]]; then
41
            break
42
        fi
43

    
44
        echo -n "."
45
        for vm_monitor in $CONTROL_PATH/*.monitor; do
46
            if ! echo | socat -U UNIX:$vm_monitor STDIO > /dev/null 2>&1; then
47
                rm -rf $vm_monitor
48
            fi
49
        done
50

    
51
        sleep 5
52
        let remaining-=5 1
53
    done
54

    
55
    if [[ -z "$(find $CONTROL_PATH -name '*.monitor')" ]]; then
56
        log_action_end_msg 0
57
    else
58
        log_action_end_msg 1 "some VMs did not shutdown"
59
    fi
60
}
61

    
62
case "$1" in
63
  start)
64
    # No-op
65
    ;;
66
  restart|reload|force-reload)
67
    echo "Error: argument '$1' not supported" >&2
68
    exit 3
69
    ;;
70
  stop)
71
    do_kvm_poweroff
72
    ;;
73
  *)
74
    echo "Usage: $0 start|stop" >&2
75
    exit 3
76
    ;;
77
esac