ia64/xen-unstable

view tools/examples/xc_dom_control.py @ 1372:5a6113c65ead

bitkeeper revision 1.891.1.12 (40a248b0WTGoOa9206iWkyGN0mTPNw)

Allow forcing of IRQ trigger-type to edge or level
(NB. DANGEROUS!).
author kaf24@scramble.cl.cam.ac.uk
date Wed May 12 15:54:24 2004 +0000 (2004-05-12)
parents c8859d41ea6c
children acc04d188782
line source
1 #!/usr/bin/env python
3 # usage: xc_dom_control [command] <params>
4 #
5 # this script isn't very smart, but it'll do for now.
6 #
8 def usage ():
9 print >>sys.stderr, """
10 Usage: %s [command] <params>
12 stop [dom] -- pause a domain
13 start [dom] -- un-pause a domain
14 shutdown [dom] [[-w]] -- request a domain to shutdown (can specify 'all')
15 (optionally wait for complete shutdown)
16 destroy [dom] -- immediately terminate a domain
17 pincpu [dom] [cpu] -- pin a domain to the specified CPU
18 suspend [dom] [file] -- write domain's memory to a file and terminate
19 (resume by re-running xc_dom_create with -L option)
20 unwatch [dom] -- kill the auto-restart daemon for a domain
21 list -- print info about all domains
22 listvbds -- print info about all virtual block devs
23 cpu_bvtset [dom] [mcuadv] [warp] [warpl] [warpu]
24 -- set BVT scheduling parameters for domain
25 cpu_bvtslice [slice] -- set default BVT scheduler slice
26 cpu_atropos_set [dom] [period] [slice] [latency] [xtratime]
27 -- set Atropos scheduling parameters for domain
28 cpu_rrobin_slice [slice] -- set Round Robin scheduler slice
29 vif_stats [dom] [vif] -- get stats for a given network vif
30 vif_addip [dom] [vif] [ip] -- add an IP address to a given vif
31 vif_setsched [dom] [vif] [bytes] [usecs] -- rate limit vif bandwidth
32 vif_getsched [dom] [vif] -- print vif's scheduling parameters
33 vbd_add [dom] [uname] [dev] [mode] -- make disk/partition uname available to
34 domain as dev e.g. 'vbd_add 2 phy:sda3 hda1 w'
35 vbd_remove [dom] [dev] -- remove disk or partition attached as 'dev'
36 """ % sys.argv[0]
38 import Xc, sys, re, string, time, os, signal
40 if len(sys.argv) < 2:
41 usage()
42 sys.exit(-1)
44 cmd = sys.argv[1]
47 xc = Xc.new()
48 rc = ''
49 dom = None
52 if len( sys.argv ) > 2 and re.match('\d+$', sys.argv[2]):
53 dom = long(sys.argv[2])
55 if cmd == 'stop':
56 rc = xc.domain_stop( dom=dom )
58 elif cmd == 'start':
59 rc = xc.domain_start( dom=dom )
61 elif cmd == 'shutdown':
62 list = []
63 if dom != None:
64 rc = xc.domain_destroy( dom=dom, force=0 )
65 list.append(dom)
66 elif sys.argv[2] == 'all':
67 for i in xc.domain_getinfo():
68 if i['dom'] != 0: # don't shutdown dom0!
69 ret = xc.domain_destroy( dom=i['dom'], force=0 )
70 if ret !=0: rc = ret
71 else: list.append(i['dom'])
73 if len(sys.argv) == 4 and sys.argv[3] == "-w":
74 # wait for all domains we shut down to terminate
75 for dom in list:
76 while True:
77 info = xc.domain_getinfo(dom,1)
78 if not ( info != [] and info[0]['dom'] == dom ): break
79 time.sleep(1)
81 elif cmd == 'destroy':
82 rc = xc.domain_destroy( dom=dom, force=1 )
84 elif cmd == 'pincpu':
86 if len(sys.argv) < 4:
87 usage()
88 sys.exit(-1)
90 cpu = int(sys.argv[3])
92 rc = xc.domain_pincpu( dom, cpu )
94 elif cmd == 'list':
95 print 'Dom Name Mem(kb) CPU State Time(ms)'
96 for domain in xc.domain_getinfo():
98 run = (domain['running'] and 'r') or '-' # domain['running'] ? run='r' : run='-'
99 stop = (domain['stopped'] and 's') or '-' # domain['stopped'] ? stop='s': stop='-'
101 domain['state'] = run + stop
102 domain['cpu_time'] = domain['cpu_time']/1e6
104 print "%(dom)-4d %(name)-16s %(mem_kb)7d %(cpu)3d %(state)5s %(cpu_time)8d" % domain
106 elif cmd == 'unwatch':
108 # the auto-restart daemon's pid file
109 watcher = '/var/run/xendomains/%d.pid' % dom
111 if os.path.isfile(watcher):
112 fd = open(watcher,'r')
113 pid = int(fd.readline())
114 os.kill(pid, signal.SIGTERM)
116 elif cmd == 'listvbds':
117 print 'Dom Dev Perm Size(MB)'
119 for vbd in xc.vbd_probe():
120 vbd['size_mb'] = vbd['nr_sectors'] / 2048
121 vbd['perm'] = (vbd['writeable'] and 'w') or 'r'
122 print '%(dom)-4d %(vbd)04x %(perm)-1s %(size_mb)d' % vbd
124 elif cmd == 'suspend':
125 if len(sys.argv) < 4:
126 usage()
127 sys.exit(-1)
129 file = sys.argv[3]
131 # the auto-restart daemon's pid file
132 watcher = '/var/run/xendomains/%d.pid' % dom
134 if os.path.isfile(watcher):
135 fd = open(watcher,'r')
136 pid = int(fd.readline())
137 os.kill(pid, signal.SIGTERM)
139 xc.domain_stop( dom=dom )
141 while not xc.domain_getinfo( first_dom=dom, max_doms=1 )[0]['stopped']:
142 time.sleep(0.1);
144 rc = xc.linux_save( dom=dom, state_file=file, progress=1)
145 if rc == 0 : xc.domain_destroy( dom=dom, force=1 )
147 elif cmd == 'cpu_bvtslice':
148 if len(sys.argv) < 3:
149 usage()
150 sys.exit(-1)
152 slice = dom # first int argument is in "dom" (!)
154 rc = xc.bvtsched_global_set(ctx_allow=slice)
156 elif cmd == 'cpu_bvtset':
157 if len(sys.argv) < 7:
158 usage()
159 sys.exit(-1)
161 mcuadv = int(sys.argv[3])
162 warp = int(sys.argv[4])
163 warpl = int(sys.argv[5])
164 warpu = int(sys.argv[6])
166 rc = xc.bvtsched_domain_set(dom=dom, mcuadv=mcuadv, warp=warp,
167 warpl=warpl, warpu=warpu)
168 elif cmd == 'vif_stats':
169 if len(sys.argv) < 4:
170 usage()
171 sys.exit(-1)
173 vif = int(sys.argv[3])
175 print xc.vif_stats_get(dom=dom, vif=vif)
177 elif cmd == 'vif_addip':
178 if len(sys.argv) < 5:
179 usage()
180 sys.exit(-1)
182 vif = int(sys.argv[3])
183 ip = sys.argv[4]
185 # XXX This function should be moved to Xc once we sort out the VFR
186 import xenctl.utils
187 xenctl.utils.setup_vfr_rules_for_vif( dom, vif, ip )
189 elif cmd == 'vif_setsched':
190 if len(sys.argv) < 6:
191 usage()
192 sys.exit(-1)
194 vif = int(sys.argv[3])
195 credit_bytes = int(sys.argv[4])
196 credit_usecs = int(sys.argv[5])
198 rc = xc.xc_vif_scheduler_set(dom=dom, vif=vif,
199 credit_bytes=credit_bytes,
200 credit_usecs=credit_usecs)
202 elif cmd == 'vif_getsched':
203 if len(sys.argv) < 4:
204 usage()
205 sys.exit(-1)
207 vif = int(sys.argv[3])
209 print xc.vif_scheduler_get(dom=dom, vif=vif)
212 elif cmd == 'vbd_add':
213 import xenctl.utils
215 xenctl.utils.VBD_EXPERT_LEVEL = 0 # sets the allowed level of potentially unsafe mappings
217 if len(sys.argv) < 6:
218 usage()
219 sys.exit(1)
221 uname = sys.argv[3]
222 dev = sys.argv[4]
223 mode = sys.argv[5]
225 writeable = 0
226 if mode == 'rw' or mode == 'w':
227 writeable = 1;
229 segments = xenctl.utils.lookup_disk_uname(uname)
231 if not segments:
232 print "Lookup Failed"
233 sys.exit(1)
235 if xenctl.utils.vd_extents_validate(segments,writeable) < 0:
236 print "That mapping is too unsafe for the current VBD expertise level"
237 sys.exit(1)
239 virt_dev = xenctl.utils.blkdev_name_to_number(dev)
241 xc.vbd_create(dom,virt_dev,writeable)
243 if xc.vbd_setextents( dom, virt_dev, segments ):
244 print "Error populating VBD vbd=%d\n" % virt_dev
245 sys.exit(1)
247 print "Added disk/partition %s to domain %d as device %s (%x)" % (uname, dom, dev, virt_dev)
249 elif cmd == 'vbd_remove':
250 import xenctl.utils
252 if len(sys.argv) < 4:
253 usage()
254 sys.exit(1)
256 dev = sys.argv[3]
257 virt_dev = xenctl.utils.blkdev_name_to_number(dev)
259 if not xc.vbd_destroy(dom,virt_dev):
260 print "Removed disk/partition attached as device %s (%x) in domain %d" % (dev, virt_dev, dom)
261 else:
262 print "Failed"
263 sys.exit(1)
265 elif cmd == 'cpu_atropos_set': # args: dom period slice latency xtratime
266 if len(sys.argv) < 6:
267 usage()
268 sys.exit(1)
270 (period, slice, latency, xtratime) = map(lambda x: int(x), sys.argv[3:7])
272 rc = xc.atropos_domain_set(dom, period, slice, latency, xtratime)
274 elif cmd == 'cpu_rrobin_slice':
275 rc = xc.rrobin_global_set(slice=int(sys.argv[2]))
277 else:
278 usage()
279 sys.exit(-1)
281 if rc != '':
282 print "return code %d" % rc