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