-
Notifications
You must be signed in to change notification settings - Fork 1
/
svc-guesthandler
91 lines (79 loc) · 2.09 KB
/
svc-guesthandler
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
#!/sbin/sh
# implement ordered VM start-up and shutdown on SmartOS | tamas@gerczei.eu
. /lib/svc/share/smf_include.sh
if [ -z $SMF_FMRI ];
then
print "this script can only be invoked by smf(5)"
exit $SMF_EXIT_ERR_NOSMF
fi
ME=${0##*/svc-}
function log {
# helper function to log arbitrary messages via syslog
logger -p daemon.notice -t $ME $@
}
case $1 in
start)
# determine the order of VMs by boot priority
ORDER=$(vmadm list -Ho uuid,tags.priority -s tags.priority tags.priority='~[^0]' | cut -d " " -f1)
log start-up order determined as: $ORDER
# start guests
for UUID in $ORDER
do
# invoke vmadm
vmadm start $UUID 2> /dev/null
if [ $? -eq 0 ]
then
# successful start, log and wait
log $UUID managed to $1
sleep ${DELAY:-0}
else
# failed to start guest
log $UUID failed to $1
fi
done
;;
stop)
# determine the order of VMs by reverse boot priority
ORDER=$(vmadm list -Ho uuid,tags.priority -s -tags.priority state=running | cut -d " " -f1)
log shutdown order determined as: $ORDER
# stop guests
for UUID in $ORDER
do
# invoke vmadm
vmadm stop $UUID 2> /dev/null
if [ $? -eq 0 ]
then
# successful stop
log $UUID managed to $1
else
# failed to stop guest
log $UUID failed to $1
fi
done
# grant the guests some time to shut down
WAIT_UNTIL=$(($(date +%s) + ${GRACE_PERIOD:-300}))
while [ $(zoneadm list | wc -l) -gt 1 ];
do
if [ $(date +%s) -le $WAIT_UNTIL ];
then
sleep ${WAIT_INTERVAL:-10}
else
exit $SMF_EXIT_ERR_FATAL
fi
done
;;
disarm)
# set the 'autoboot' attribute to 'false' for every installed guest in order to prevent automatic start-up without this script
zoneadm list -pi | while IFS=":" read ID UUID STATE remainder; do
if [[ $STATE == "installed" ]];
then
zonecfg -z $UUID 'set autoboot=false; select attr name=vm-autoboot; set value=false; end; verify; commit; exit'
fi
done
log disarmed: set autoboot=false on all zones
;;
*)
exit $SMF_EXIT_ERR_CONFIG
;;
esac
exit $SMF_EXIT_NODAEMON