Print this page
2594 implement graceful shutdown for local zones in zoneadm
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/zoneadm/svc-zones
+++ new/usr/src/cmd/zoneadm/svc-zones
1 1 #!/sbin/sh
2 2 #
3 3 # CDDL HEADER START
4 4 #
5 5 # The contents of this file are subject to the terms of the
6 6 # Common Development and Distribution License (the "License").
7 7 # You may not use this file except in compliance with the License.
8 8 #
9 9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 # or http://www.opensolaris.org/os/licensing.
11 11 # See the License for the specific language governing permissions
12 12 # and limitations under the License.
13 13 #
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 # When distributing Covered Code, include this CDDL HEADER in each
15 15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 # If applicable, add the following below this CDDL HEADER, with the
17 17 # fields enclosed by brackets "[]" replaced with your own identifying
18 18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 19 #
20 20 # CDDL HEADER END
21 21 #
22 22 #
23 23 # Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 +# Copyright 2014 Nexenta Systems, Inc. All rights reserved.
24 25
25 26 . /lib/svc/share/smf_include.sh
26 27
27 28 #
28 29 # Return a list of running, non-global zones for which a shutdown via
29 30 # "/sbin/init 0" may work (typically only Solaris zones.)
30 31 #
31 32 shutdown_zones()
32 33 {
33 34 zoneadm list -p | nawk -F: '{
34 35 if ($2 != "global") {
35 36 print $2
36 37 }
37 38 }'
38 39 }
39 40
40 41 [ ! -x /usr/sbin/zoneadm ] && exit 0 # SUNWzoneu not installed
41 42
42 43 if [ -z "$SMF_FMRI" ]; then
43 44 echo "this script can only be invoked by smf(5)"
44 45 exit $SMF_EXIT_ERR_NOSMF
45 46 fi
46 47
47 48 # Make sure working directory is / to prevent unmounting problems.
48 49 cd /
49 50 PATH=/usr/sbin:/usr/bin; export PATH
50 51
51 52 case "$1" in
52 53 'start')
53 54 egrep -vs '^#|^global:' /etc/zones/index || exit 0 # no local zones
54 55
55 56 #
56 57 # Boot the installed zones for which the "autoboot" zone property is
57 58 # set and invoke the sysboot hook for all other installed zones.
58 59 #
59 60 ZONES=""
60 61 for zone in `zoneadm list -pi | nawk -F: '{
61 62 if ($3 == "installed") {
62 63 print $2
63 64 }
64 65 }'`; do
65 66 zonecfg -z $zone info autoboot | grep "true" >/dev/null 2>&1
66 67 if [ $? -eq 0 ]; then
67 68 [ -z "$ZONES" ] && echo "Booting zones:\c"
68 69 ZONES=yes
69 70 echo " $zone\c"
70 71 #
71 72 # zoneadmd puts itself into its own contract so
72 73 # this service will lose sight of it. We don't
73 74 # support restart so it is OK for zoneadmd to
74 75 # to be in an orphaned contract.
75 76 #
76 77 zoneadm -z $zone boot &
77 78 else
78 79 zoneadm -z $zone sysboot &
79 80 fi
80 81 done
81 82
82 83 #
83 84 # Wait for all zoneadm processes to finish before allowing the
84 85 # start method to exit.
85 86 #
86 87 wait
87 88 [ -n "$ZONES" ] && echo .
88 89 ;;
89 90
90 91 'stop')
91 92 egrep -vs '^#|^global:' /etc/zones/index || exit 0 # no local zones
92 93 [ "`zoneadm list`" = "global" ] && exit 0 # no zones running
93 94
94 95 SVC_TIMEOUT=`svcprop -p stop/timeout_seconds $SMF_FMRI`
95 96
96 97 #
97 98 # First, try shutting down any running zones for which an "init 0" may
98 99 # work.
99 100 #
100 101 MAXSHUT=`expr 3 \* $SVC_TIMEOUT \/ 4` # 3/4 of time to zone shutdown
101 102 MAXHALT=`expr $SVC_TIMEOUT \/ 4` # rest of time goes to halt
↓ open down ↓ |
68 lines elided |
↑ open up ↑ |
102 103
103 104 zonelist=`shutdown_zones`
104 105
105 106 if [ -n "$zonelist" ]; then
106 107 SHUTDOWN=0
107 108 echo "Shutting down running zones (for up to $MAXSHUT" \
108 109 "seconds):\c"
109 110
110 111 for zone in $zonelist; do
111 112 echo " $zone\c"
112 - zlogin -S $zone /sbin/init 0 < /dev/null >&0 2>&0 &
113 + zoneadm -z $zone shutdown &
113 114 SHUTDOWN=1
114 115 done
115 116
116 117 [ $SHUTDOWN -eq 1 ] && echo "."
117 118
118 119 # Allow time for zones to shutdown cleanly
119 120
120 121 while [ $MAXSHUT -gt 0 -a "`shutdown_zones`" != "" ]; do
121 122 MAXSHUT=`expr $MAXSHUT - 1`
122 123 sleep 1 # wait a bit longer
123 124 done
124 125 fi
125 126
126 127 #
127 128 # Second, try halting any non-global zones still running
128 129 #
129 130 WAITPIDS=""
130 131 for zone in `zoneadm list`; do
131 132 if [ "$zone" != "global" ]; then
132 133 [ -z "$WAITPIDS" ] &&
133 134 echo "Zones failed to shutdown; trying to halt " \
134 135 "(for up to $MAXHALT seconds):\c"
135 136 echo " $zone\c"
136 137 zoneadm -z $zone halt &
137 138 WAITPIDS="$WAITPIDS $!"
138 139 fi
139 140 done
140 141 [ ! -z "$WAITPIDS" ] && echo .
141 142
142 143 # Wait for the 'zoneadm halt' commands to complete. We will let this
143 144 # run forever, since the restart daemon will eventually kill us off
144 145 # anyway if the halts do not complete after a certain period of time.
145 146 wait $WAITPIDS
146 147
147 148 # If the halts complete but a zone is still not shutdown, it might
148 149 # be in a state like 'shutting_down' or 'down'. So we give it some
149 150 # time to come all the way down.
150 151
151 152 while [ $MAXHALT -gt 0 -a "`zoneadm list`" != "global" ]; do
152 153 MAXHALT=`expr $MAXHALT - 1`
153 154 sleep 1 # wait a bit longer
154 155 done
155 156
156 157 # If there are any remaining file systems in zones, try to unmount them.
157 158 umountall -Z
158 159
159 160 #
160 161 # Report on zones which failed to shutdown.
161 162 #
162 163 for zone in `zoneadm list`; do
163 164 if [ "$zone" != "global" ]; then
164 165 echo "Zone '$zone' failed to halt."
165 166 fi
166 167 done
167 168 [ "`zoneadm list`" != "global" ] && exit 1 # zones still running
168 169 ;;
169 170
170 171 *)
171 172 echo "Usage: $0 { start | stop }"
172 173 exit 1
173 174 ;;
174 175 esac
175 176 exit 0
↓ open down ↓ |
53 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX