aboutsummaryrefslogtreecommitdiffstats
path: root/tools/examples/xc_dom_control.py
blob: ea97a45f0ec5a7e8dc5d500efad45ade80f4bbfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python

# usage: xc_dom_control [command] <params>
#
# this script isn't very smart, but it'll do for now.
#

def usage ():
    print >>sys.stderr, """
Usage: %s [command] <params>

  stop      [dom]        -- pause a domain
  start     [dom]        -- un-pause a domain
  shutdown  [dom] [[-w]] -- request a domain to shutdown (can specify 'all')
                            (optionally wait for complete shutdown)
  destroy   [dom]        -- immediately terminate a domain
  pincpu    [dom] [cpu]  -- pin a domain to the specified CPU
  suspend   [dom] [file] -- write domain's memory to a file and terminate
			    (resume by re-running xc_dom_create with -L option)
  unwatch   [dom]        -- kill the auto-restart daemon for a domain
  list                   -- print info about all domains
  listvbds               -- print info about all virtual block devs
  cpu_bvtset [dom] [mcuadv] [warp] [warpl] [warpu]
                         -- set BVT scheduling parameters for domain
  cpu_bvtslice [slice]   -- set default BVT scheduler slice
  cpu_atropos_set [dom] [period] [slice] [latency] [xtratime]
                         -- set Atropos scheduling parameters for domain
  cpu_rrobin_slice [slice] -- set Round Robin scheduler slice
  vif_stats [dom] [vif]  -- get stats for a given network vif
  vif_addip [dom] [vif] [ip]  -- add an IP address to a given vif
  vif_setsched [dom] [vif] [bytes] [usecs] -- rate limit vif bandwidth
  vif_getsched [dom] [vif] -- print vif's scheduling parameters
  vbd_add [dom] [uname] [dev] [mode] -- make disk/partition uname available to 
                            domain as dev e.g. 'vbd_add 2 phy:sda3 hda1 w'
  vbd_remove [dom] [dev] -- remove disk or partition attached as 'dev' 
""" % sys.argv[0]

import Xc, sys, re, string, time, os, signal

if len(sys.argv) < 2:
    usage()
    sys.exit(-1)

cmd = sys.argv[1]


xc = Xc.new()
rc = ''
dom = None


if len( sys.argv ) > 2 and re.match('\d+$', sys.argv[2]):
    dom = long(sys.argv[2])

if cmd == 'stop':
    rc = xc.domain_stop( dom=dom )

elif cmd == 'start':
    rc = xc.domain_start( dom=dom )    

elif cmd == 'shutdown':
    list = []
    if dom != None:
        rc = xc.domain_destroy( dom=dom, force=0 )
        list.append(dom)
    elif sys.argv[2] == 'all':
        for i in xc.domain_getinfo():
            if i['dom'] != 0: # don't shutdown dom0!
                ret = xc.domain_destroy( dom=i['dom'], force=0 )
                if ret !=0: rc = ret
                else: list.append(i['dom'])

    if len(sys.argv) == 4 and sys.argv[3] == "-w":
        # wait for all domains we shut down to terminate
        for dom in list:
            while True:
                info = xc.domain_getinfo(dom,1)
                if not ( info != [] and info[0]['dom'] == dom ): break
                time.sleep(1)

elif cmd == 'destroy':
    rc = xc.domain_destroy( dom=dom, force=1 )    

elif cmd == 'pincpu':

    if len(sys.argv) < 4:
        usage()
        sys.exit(-1)

    cpu = int(sys.argv[3])
    
    rc = xc.domain_pincpu( dom, cpu )

elif cmd == 'list':
    print 'Dom  Name             Mem(kb)  CPU  State  Time(s)'
    for domain in xc.domain_getinfo():

	run = (domain['running'] and 'r') or '-'		# domain['running'] ? run='r' : run='-'
	stop = (domain['stopped'] and 's') or '-'		# domain['stopped'] ? stop='s': stop='-'

        domain['state'] = run + stop
        domain['cpu_time'] = domain['cpu_time']/1e8

        print "%(dom)-4d %(name)-16s %(mem_kb)7d %(cpu)3d %(state)5s %(cpu_time)8d" % domain

elif cmd == 'unwatch':

    # the auto-restart daemon's pid file
    watcher = '/var/run/xendomains/%d.pid' % dom

    if os.path.isfile(watcher):
        fd = open(watcher,'r')
        pid = int(fd.readline())
        os.kill(pid, signal.SIGTERM)

elif cmd == 'listvbds':
    print 'Dom   Dev   Perm   Size(MB)'
    
    for vbd in xc.vbd_probe():
        vbd['size_mb'] = vbd['nr_sectors'] / 2048
        vbd['perm'] = (vbd['writeable'] and 'w') or 'r'
        print '%(dom)-4d  %(vbd)04x  %(perm)-1s      %(size_mb)d' % vbd

elif cmd == 'suspend':
    if len(sys.argv) < 4:
        usage()
        sys.exit(-1)

    file = sys.argv[3]

    # the auto-restart daemon's pid file
    watcher = '/var/run/xendomains/%d.pid' % dom

    if os.path.isfile(watcher):
        fd = open(watcher,'r')
        pid = int(fd.readline())
        os.kill(pid, signal.SIGTERM)

    xc.domain_stop( dom=dom )
    rc = xc.linux_save( dom=dom, state_file=file, progress=1)
    if rc == 0 : xc.domain_destroy( dom=dom, force=1 )

elif cmd == 'cpu_bvtslice':
    if len(sys.argv) < 3:
        usage()
        sys.exit(-1)

    slice = dom # first int argument is in "dom" (!)

    rc = xc.bvtsched_global_set(ctx_allow=slice)

elif cmd == 'cpu_bvtset':
    if len(sys.argv) < 7:
        usage()
        sys.exit(-1)

    mcuadv = int(sys.argv[3])
    warp   = int(sys.argv[4])
    warpl  = int(sys.argv[5])
    warpu  = int(sys.argv[6])

    rc = xc.bvtsched_domain_set(dom=dom, mcuadv=mcuadv, warp=warp,
                                warpl=warpl, warpu=warpu)
elif cmd == 'vif_stats':
    if len(sys.argv) < 4:
        usage()
        sys.exit(-1)

    vif = int(sys.argv[3])

    print xc.vif_stats_get(dom=dom, vif=vif)

elif cmd == 'vif_addip':
    if len(sys.argv) < 5:
        usage()
        sys.exit(-1)

    vif = int(sys.argv[3])
    ip  = sys.argv[4]

    # XXX This function should be moved to Xc once we sort out the VFR
    import xenctl.utils
    xenctl.utils.setup_vfr_rules_for_vif( dom, vif, ip )

elif cmd == 'vif_setsched':
    if len(sys.argv) < 6:
        usage()
        sys.exit(-1)

    vif = int(sys.argv[3])
    credit_bytes = int(sys.argv[4])
    credit_usecs = int(sys.argv[5])

    rc = xc.xc_vif_scheduler_set(dom=dom, vif=vif, 
				 credit_bytes=credit_bytes, 
				 credit_usecs=credit_usecs)

elif cmd == 'vif_getsched':
    if len(sys.argv) < 4:
        usage()
        sys.exit(-1)

    vif = int(sys.argv[3])

    print xc.vif_scheduler_get(dom=dom, vif=vif)


elif cmd == 'vbd_add':
    import xenctl.utils
    
    xenctl.utils.VBD_EXPERT_LEVEL = 0 # sets the allowed level of potentially unsafe mappings

    if len(sys.argv) < 6:
	usage()
	sys.exit(1)

    uname = sys.argv[3]
    dev = sys.argv[4]
    mode = sys.argv[5]

    writeable = 0
    if mode == 'rw' or mode == 'w':
	writeable = 1;

    segments = xenctl.utils.lookup_disk_uname(uname)

    if not segments:
        print "Lookup Failed"
        sys.exit(1)

    if xenctl.utils.vd_extents_validate(segments,writeable) < 0:
	print "That mapping is too unsafe for the current VBD expertise level"
	sys.exit(1)

    virt_dev = xenctl.utils.blkdev_name_to_number(dev)

    xc.vbd_create(dom,virt_dev,writeable)

    if xc.vbd_setextents( dom, virt_dev, segments ):
	print "Error populating VBD vbd=%d\n" % virt_dev
	sys.exit(1)

    print "Added disk/partition %s to domain %d as device %s (%x)" % (uname, dom, dev, virt_dev)

elif cmd == 'vbd_remove':
    import xenctl.utils

    if len(sys.argv) < 4:
	usage()
	sys.exit(1)

    dev = sys.argv[3]
    virt_dev = xenctl.utils.blkdev_name_to_number(dev)

    if not xc.vbd_destroy(dom,virt_dev):
	print "Removed disk/partition attached as device %s (%x) in domain %d" % (dev, virt_dev, dom)
    else:
	print "Failed"
	sys.exit(1)

elif cmd == 'cpu_atropos_set': # args: dom period slice latency xtratime
    if len(sys.argv) < 6:
        usage()
        sys.exit(1)

    (period, slice, latency, xtratime) = map(lambda x: int(x), sys.argv[3:7])
    
    rc = xc.atropos_domain_set(dom, period, slice, latency, xtratime)

elif cmd == 'cpu_rrobin_slice':
    rc = xc.rrobin_global_set(slice=int(sys.argv[2]))

else:
    usage()
    sys.exit(-1)

if rc != '':
    print "return code %d" % rc