ia64/xen-unstable

view tools/python/xen/xm/shutdown.py @ 17404:af1d20b86b02

xm: Fix xm shutdown for xendomains.

If xm shutdown command is passed the domain ID, it should still wait
for the shutdown completion of the domain if passed option --wait.

Signed-off-by: Masaki Kanno <kanno.masaki@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Apr 08 09:57:37 2008 +0100 (2008-04-08)
parents d04593aa1605
children bd9823ec7097
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 """Domain shutdown.
19 """
20 import time
22 from xen.xend import sxp
23 from opts import *
24 from main import server, serverType, SERVER_XEN_API, get_single_vm
25 from xen.xend.XendAPIConstants import *
27 RECREATING_TIMEOUT = 30
29 gopts = Opts(use="""[options] [DOM]
31 Shutdown one or more domains gracefully.
32 """)
34 gopts.opt('help', short='h',
35 fn=set_true, default=0,
36 use="Print this help.")
38 gopts.opt('all', short='a',
39 fn=set_true, default=0,
40 use="Shutdown all domains.")
42 gopts.opt('wait', short='w',
43 fn=set_true, default=0,
44 use='Wait for shutdown to complete.')
46 gopts.opt('halt', short='H',
47 fn=set_true, default=0,
48 use='Shutdown without reboot.')
50 gopts.opt('reboot', short='R',
51 fn=set_true, default=0,
52 use='Shutdown and reboot.')
54 def wait_reboot(opts, doms, rcs):
55 if serverType == SERVER_XEN_API:
56 opts.err("Cannot wait for reboot w/ XenAPI (yet)")
58 recreating = {}
59 while doms:
60 alive = server.xend.domains(0)
61 reboot = []
62 for d in doms:
63 if d in alive:
64 rc = server.xend.domain.getRestartCount(d)
65 if rc == rcs[d]: continue
66 reboot.append(d)
68 # Probably the domain is being recreated now.
69 # We have to wait just a bit for recreating the domain.
70 elif not recreating.has_key(d):
71 recreating[d] = 0
72 else:
73 recreating[d] += 1
74 if recreating[d] > RECREATING_TIMEOUT:
75 opts.info("Domain %s destroyed for failing to reboot" % d)
76 doms.remove(d)
78 for d in reboot:
79 opts.info("Domain %s rebooted" % d)
80 doms.remove(d)
81 time.sleep(1)
82 opts.info("All domains rebooted")
84 def wait_shutdown(opts, doms):
85 while doms:
86 if serverType == SERVER_XEN_API:
87 alive = [dom for dom in server.xenapi.VM.get_all()
88 if server.xenapi.VM.get_power_state(dom) ==
89 XEN_API_VM_POWER_STATE[XEN_API_VM_POWER_STATE_RUNNING]]
90 else:
91 alive = server.xend.domains(0)
92 dead = []
93 for d in doms:
94 if d in alive: continue
95 dead.append(d)
96 for d in dead:
97 opts.info("Domain %s terminated" % d)
98 doms.remove(d)
99 time.sleep(1)
100 opts.info("All domains terminated")
102 def shutdown(opts, doms, mode, wait):
103 rcs = {}
104 for d in doms:
105 if serverType == SERVER_XEN_API:
106 if mode == 'halt':
107 server.xenapi.VM.clean_shutdown(d)
108 if mode == 'reboot':
109 server.xenapi.VM.clean_reboot(d)
110 if mode == 'poweroff':
111 server.xenapi.VM.clean_shutdown(d)
112 else:
113 rcs[d] = server.xend.domain.getRestartCount(d)
114 server.xend.domain.shutdown(d, mode)
116 if wait:
117 if mode == 'reboot':
118 wait_reboot(opts, doms, rcs)
119 else:
120 wait_shutdown(opts, doms)
122 def shutdown_mode(opts):
123 if opts.vals.halt and opts.vals.reboot:
124 opts.err("Can't specify halt and reboot")
126 if opts.vals.halt:
127 return 'halt'
128 elif opts.vals.reboot:
129 return 'reboot'
130 else:
131 return 'poweroff'
133 def main_all(opts, args):
134 if serverType == SERVER_XEN_API:
135 doms = [dom for dom in server.xenapi.VM.get_all()
136 if not server.xenapi.VM.get_is_control_domain(dom)]
137 else:
138 doms = server.xend.domains(0)
139 dom0_name = sxp.child_value(server.xend.domain(0), 'name')
140 doms.remove(dom0_name)
141 mode = shutdown_mode(opts)
142 shutdown(opts, doms, mode, opts.vals.wait)
144 def main_dom(opts, args):
145 if len(args) == 0: opts.err('No domain parameter given')
146 if len(args) > 1: opts.err('No multiple domain parameters allowed')
147 dom = sxp.child_value(server.xend.domain(args[0]), 'name')
148 if serverType == SERVER_XEN_API:
149 dom = get_single_vm(dom)
150 mode = shutdown_mode(opts)
151 shutdown(opts, [ dom ], mode, opts.vals.wait)
153 def main(argv):
154 opts = gopts
155 opts.reset()
156 args = opts.parse(argv)
157 if opts.vals.help:
158 return
159 if opts.vals.all:
160 main_all(opts, args)
161 else:
162 main_dom(opts, args)