ia64/xen-unstable

view tools/examples/xm_dom_control.py @ 1500:90c79ca0b23e

bitkeeper revision 1.976.1.1 (40d1bc25lvWeLx83AKQLiEILv4stIg)

bug fix to xm_dom_control
author iap10@labyrinth.cl.cam.ac.uk
date Thu Jun 17 15:43:33 2004 +0000 (2004-06-17)
parents f32df7f04499
children aab6212ef9c5
line source
1 #!/usr/bin/env python
3 import sys
4 import re
5 import string
6 import time
7 import os
8 import os.path
9 import signal
11 from xenmgr import sxp
12 from xenmgr.XendClient import server
14 # usage: xc_dom_control [command] <params>
15 #
16 # this script isn't very smart, but it'll do for now.
17 #
19 def usage (rc=0):
20 if rc:
21 out = sys.stderr
22 else:
23 out = sys.stdout
24 print >> out, """
25 Usage: %s [command] <params>
27 help -- print usage
28 stop [dom] -- pause a domain
29 start [dom] -- un-pause a domain
30 shutdown [dom] [[-w]] -- request a domain to shutdown (can specify 'all')
31 (optionally wait for complete shutdown)
32 destroy [dom] -- immediately terminate a domain
33 pincpu [dom] [cpu] -- pin a domain to the specified CPU
34 suspend [dom] [file] -- write domain's memory to a file and terminate
35 (resume by re-running xc_dom_create with -L option)
36 unwatch [dom] -- kill the auto-restart daemon for a domain
37 list -- print info about all domains
38 listvbds -- print info about all virtual block devs
39 cpu_bvtset [dom] [mcuadv] [warp] [warpl] [warpu]
40 -- set BVT scheduling parameters for domain
41 cpu_bvtslice [slice] -- set default BVT scheduler slice
42 cpu_atropos_set [dom] [period] [slice] [latency] [xtratime]
43 -- set Atropos scheduling parameters for domain
44 cpu_rrobin_slice [slice] -- set Round Robin scheduler slice
45 vif_stats [dom] [vif] -- get stats for a given network vif
46 vif_addip [dom] [vif] [ip] -- add an IP address to a given vif
47 vif_setsched [dom] [vif] [bytes] [usecs] -- rate limit vif bandwidth
48 vif_getsched [dom] [vif] -- print vif's scheduling parameters
49 vbd_add [dom] [uname] [dev] [mode] -- make disk/partition uname available to
50 domain as dev e.g. 'vbd_add 2 phy:sda3 hda1 w'
51 vbd_remove [dom] [dev] -- remove disk or partition attached as 'dev'
52 """ % sys.argv[0]
53 if rc: sys.exit(rc)
55 if len(sys.argv) < 2: usage(1)
56 cmd = sys.argv[1]
58 #todo: replace all uses of xc with the new api.
59 import Xc; xc = Xc.new()
61 rc = ''
62 dom = None
65 def auto_restart_pid_file(dom):
66 # The auto-restart daemon's pid file.
67 return '/var/run/xendomains/%d.pid' % dom
69 def auto_restart_pid(dom):
70 watcher = auto_restart_pid_file(dom)
71 if os.path.isfile(watcher):
72 fd = open(watcher,'r')
73 pid = int(fd.readline())
74 else:
75 pid = None
76 return pid
78 def auto_restart_kill(dom):
79 #todo: replace this - tell xend not to restart any more.
80 # Kill a domain's auto restart daemon.
81 pid = auto_restart_pid(dom)
82 if pid:
83 os.kill(pid, signal.SIGTERM)
86 if len( sys.argv ) > 2 and re.match('\d+$', sys.argv[2]):
87 dom = long(sys.argv[2])
89 if cmd == "help":
90 usage()
92 elif cmd == 'stop':
93 rc = server.xend_domain_stop(dom)
95 elif cmd == 'start':
96 rc = server.xend_domain_start(dom)
98 elif cmd == 'shutdown':
99 doms = []
100 shutdown = []
101 if dom != None:
102 doms = [ dom ]
103 elif sys.argv[2] == 'all':
104 doms = server.xend_domains()
105 doms.remove('0')
106 for d in doms:
107 ret = server.xend_domain_shutdown(d)
108 if ret == 0:
109 shutdown.append(d)
110 else:
111 rc = ret
113 wait = (len(sys.argv) == 4 and sys.argv[3] == "-w")
114 if wait:
115 # wait for all domains we shut down to terminate
116 for dom in shutdown:
117 while True:
118 info = server.xend_domain(dom)
119 if not info: break
120 time.sleep(1)
122 elif cmd == 'destroy':
123 rc = server.xend_domain_halt(dom)
125 elif cmd == 'pincpu':
126 if len(sys.argv) < 4: usage(1)
127 cpu = int(sys.argv[3])
128 rc = server.xend_domain_pincpu(dom, cpu)
130 elif cmd == 'list':
131 print 'Dom Name Mem(MB) CPU State Time(s)'
132 for dom in server.xend_domains():
133 info = server.xend_domain(dom)
134 d = {}
135 d['dom'] = int(dom)
136 d['name'] = sxp.child_value(info, 'name', '??')
137 d['mem'] = int(sxp.child_value(info, 'memory', '0'))
138 d['cpu'] = int(sxp.child_value(info, 'cpu', '0'))
139 d['state'] = sxp.child_value(info, 'state', '??')
140 d['cpu_time'] = float(sxp.child_value(info, 'cpu_time', '0'))
141 print ("%(dom)-4d %(name)-16s %(mem)7d %(cpu)3d %(state)5s %(cpu_time)8.2f" % d)
143 elif cmd == 'unwatch':
144 auto_restart_kill(dom)
146 elif cmd == 'listvbds':
147 print 'Dom Dev Mode Size(MB)'
148 for dom in server.xend_domains():
149 for vbd in server.xend_domain_vbds(dom):
150 info = server.xend_domain_vbd(vbd)
151 v['vbd'] = vbd
152 v['size'] = int(sxp.get_child_value(info, 'size', '0'))
153 v['mode'] = sxp.get_child_value(info, 'mode', '??')
154 vbd['size_mb'] = vbd['nr_sectors'] / 2048
155 print ('%(dom)-4d %(vbd)04x %(mode)-2s %(size)d' % v)
157 elif cmd == 'suspend':
158 if len(sys.argv) < 4: usage(1)
159 file = os.path.abspath(sys.argv[3])
160 auto_restart_kill(dom)
161 rc = server.xend_domain_save(dom, file, progress=1)
163 elif cmd == 'cpu_bvtslice':
164 if len(sys.argv) < 3: usage(1)
165 slice = sys.argv[2]
166 rc = server.xend_node_cpu_bvt_slice_set(slice)
168 elif cmd == 'cpu_bvtset':
169 if len(sys.argv) < 7: usage(1)
170 (mcuadv, warp, warpl, warpu) = map(int, sys.argv[3:7])
172 rc = server.xend_domain_cpu_bvt_set(dom, mcuadv, warp, warpl, warpu)
174 elif cmd == 'vif_stats':
175 if len(sys.argv) < 4: usage(1)
176 vif = int(sys.argv[3])
178 print server.xend_domain_vif_stats(dom, vif)
180 elif cmd == 'vif_addip':
181 if len(sys.argv) < 5: usage(1)
182 vif = int(sys.argv[3])
183 ip = sys.argv[4]
184 rc = server.xend_domain_vif_addip(dom, vif, ip)
186 elif cmd == 'vif_setsched':
187 if len(sys.argv) < 6: usage(1)
188 (vif, bytes, usecs) = map(int, sys.argv[3:6])
189 rc = server.xend_domain_vif_scheduler_set(dom, vif, bytes, usecs)
191 elif cmd == 'vif_getsched':
192 if len(sys.argv) < 4: usage(1)
193 vif = int(sys.argv[3])
194 print server.xend_domain_vif_scheduler_get(dom, vif)
196 elif cmd == 'vbd_add':
197 if len(sys.argv) < 6: usage(1)
198 uname = sys.argv[3]
199 dev = sys.argv[4]
200 mode = sys.argv[5]
201 try:
202 vbd = server.xend_domain_vbd_add(dom, uname, dev, mode)
203 except StandardError, ex:
204 print "Error:", ex
205 sys.exit(1)
206 print "Added disk/partition %s to domain %d as device %s (%x)" % (uname, dom, dev, vbd)
208 elif cmd == 'vbd_remove':
209 if len(sys.argv) < 4: usage(1)
210 dev = sys.argv[3]
211 vbd = server.xend_domain_vbd_remove(dom, dev)
212 if vbd < 0:
213 print "Failed"
214 sys.exit(1)
215 else:
216 print "Removed disk/partition attached as device %s (%x) in domain %d" % (dev, vbd, dom)
218 elif cmd == 'cpu_atropos_set': # args: dom period slice latency xtratime
219 if len(sys.argv) < 6: usage(1)
220 (period, slice, latency, xtratime) = map(int, sys.argv[3:7])
221 rc = server.xend_domain_cpu_atropos_set(
222 dom, period, slice, latency, xtratime)
224 elif cmd == 'cpu_rrobin_slice':
225 if len(sys.argv) < 3: usage(1)
226 slice = int(sys.argv[2])
227 rc = server.xend_node_rrobin_set(slice=slice)
229 else:
230 usage(1)
232 if rc != '':
233 print "return code %d" % rc