aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xen/lib/xend/XendDomain.py
blob: 3aaf080c36a780d3528e354d64c35048b7dc5615 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Copyright (C) 2004 Mike Wray <mike.wray@hp.com>

"""Handler for domain operations.
 Nothing here is persistent (across reboots).
 Needs to be persistent for one uptime.
"""
import sys

from twisted.internet import defer

import xen.ext.xc; xc = xen.ext.xc.new()

import sxp
import XendRoot
xroot = XendRoot.instance()
import XendDB
import XendDomainInfo
import XendConsole
import EventServer

from xen.xend.server import SrvDaemon
xend = SrvDaemon.instance()

eserver = EventServer.instance()

__all__ = [ "XendDomain" ]
        
class XendDomain:
    """Index of all domains. Singleton.
    """
    
    dbpath = "domain"
    domain = {}
    
    def __init__(self):
        self.xconsole = XendConsole.instance()
        # Table of domain info indexed by domain id.
        self.db = XendDB.XendDB(self.dbpath)
        #self.domain = {}
        self.domain_db = self.db.fetchall("")
        if xroot.get_rebooted():
            print 'XendDomain> rebooted: removing all domain info'
            self.rm_all()
        eserver.subscribe('xend.virq', self.onVirq)
        self.initial_refresh()

    def onVirq(self, event, val):
        print 'XendDomain> virq', val
        self.reap()

    def rm_all(self):
        """Remove all domain info. Used after reboot.
        """
        for (k, v) in self.domain_db.items():
            self._delete_domain(k, notify=0)
            
    def initial_refresh(self):
        """Refresh initial domain info from domain_db.
        """
        print "initial_refresh>"
        for d in self.domain_db.values(): print 'db dom=', d
        domlist = xc.domain_getinfo()
        for d in domlist: print 'xc dom=', d
        doms = {}
        for d in domlist:
            domid = str(d['dom'])
            doms[domid] = d
        dlist = []
        for config in self.domain_db.values():
            domid = str(sxp.child_value(config, 'id'))
            print "dom=", domid, "config=", config
            if domid in doms:
                print "dom=", domid, "new"
                deferred = self._new_domain(config, doms[domid])
                dlist.append(deferred)
            else:
                print "dom=", domid, "del"
                self._delete_domain(domid)
        deferred = defer.DeferredList(dlist, fireOnOneErrback=1)
        def cbok(val):
            #print "doms:"
            #for d in self.domain.values(): print 'dom', d
            self.refresh()
            print "XendDomain>initial_refresh> doms:"
            for d in self.domain.values(): print 'dom', d
        deferred.addCallback(cbok)

    def sync(self):
        """Sync domain db to disk.
        """
        self.db.saveall("", self.domain_db)

    def sync_domain(self, dom):
        """Sync info for a domain to disk.

        dom	domain id (string)
        """
        self.db.save(dom, self.domain_db[dom])

    def close(self):
        pass

    def _new_domain(self, savedinfo, info):
        """Create a domain entry from saved info.
        """
##         console = None
##         kernel = None
##         id = sxp.child_value(info, 'id')
##         dom = int(id)
##         name = sxp.child_value(info, 'name')
##         memory = int(sxp.child_value(info, 'memory'))
##         consoleinfo = sxp.child(info, 'console')
##         if consoleinfo:
##             consoleid = sxp.child_value(consoleinfo, 'id')
##             console = self.xconsole.console_get(consoleid)
##         if dom and console is None:
##             # Try to connect a console.
##             console = self.xconsole.console_create(dom)
##         config = sxp.child(info, 'config')
##         if config:
##             image = sxp.child(info, 'image')
##             if image:
##                 image = sxp.child0(image)
##                 kernel = sxp.child_value(image, 'kernel')
##         dominfo = XendDomainInfo.XendDomainInfo(
##             config, dom, name, memory, kernel, console)
        config = sxp.child_value(savedinfo, 'config')
        deferred = XendDomainInfo.vm_recreate(config, info)
        def fn(dominfo):
            self.domain[dominfo.id] = dominfo
        deferred.addCallback(fn)
        return deferred

    def _add_domain(self, id, info, notify=1):
        self.domain[id] = info
        self.domain_db[id] = info.sxpr()
        self.sync_domain(id)
        if notify: eserver.inject('xend.domain.created', id)

    def _delete_domain(self, id, notify=1):
        if id in self.domain:
            if notify: eserver.inject('xend.domain.died', id)
            del self.domain[id]
        if id in self.domain_db:
            del self.domain_db[id]
            self.db.delete(id)

    def reap(self):
        """Go through the domains looking for ones that have crashed or stopped.
        Tidy them up.
        """
        print 'XendDomain>reap>'
        domlist = xc.domain_getinfo()
        casualties = []
        for d in domlist:
            #print 'dom', d
            dead = 0
            dead = dead or (d['crashed'] or d['shutdown'])
            dead = dead or (d['dying'] and
                            not(d['running'] or d['paused'] or d['blocked']))
            if dead:
                casualties.append(d)
        for d in casualties:
            id = str(d['dom'])
            print 'XendDomain>reap> died id=', id, d
            dominfo = self.domain.get(id)
            if not dominfo: continue
            dominfo.died()
            self.domain_destroy(id, refresh=0)
        print 'XendDomain>reap<'

    def refresh(self):
        """Refresh domain list from Xen.
        """
        domlist = xc.domain_getinfo()
        # Index the domlist by id.
        # Add entries for any domains we don't know about.
        doms = {}
        for d in domlist:
            id = str(d['dom'])
            doms[id] = d
            if id not in self.domain:
                config = None
                #image = None
                #newinfo = XendDomainInfo.XendDomainInfo(
                #    config, d['dom'], d['name'], d['mem_kb']/1024, image=image, info=d)
                deferred = XendDomainInfo.vm_recreate(config, d)
                def fn(dominfo):
                    self._add_domain(dominfo.id, dominfo)
                deferred.addCallback(fn)
        # Remove entries for domains that no longer exist.
        for d in self.domain.values():
            dominfo = doms.get(d.id)
            if dominfo:
                d.update(dominfo)
            else:
                self._delete_domain(d.id)
        self.reap()

    def refresh_domain(self, id):
        dom = int(id)
        dominfo = xc.domain_getinfo(dom, 1)
        if dominfo == [] or dominfo[0]['dom'] != dom:
            try:
                self._delete_domain(id)
            except:
                print 'refresh_domain: error'
                raise
                pass
        else:
            d = self.domain.get(id)
            if d:
                d.update(dominfo[0])

    def domain_ls(self):
        # List domains.
        # Update info from kernel first.
        self.refresh()
        return self.domain.keys()

    def domains(self):
        self.refresh()
        return self.domain.values()
    
    def domain_create(self, config):
        # Create domain, log it.
        deferred = XendDomainInfo.vm_create(config)
        def fn(dominfo):
            self._add_domain(dominfo.id, dominfo)
            return dominfo
        deferred.addCallback(fn)
        return deferred
    
    def domain_get(self, id):
        id = str(id)
        self.refresh_domain(id)
        return self.domain.get(id)
    
    def domain_unpause(self, id):
        """(Re)start domain running.
        """
        dom = int(id)
        eserver.inject('xend.domain.unpause', id)
        return xc.domain_unpause(dom=dom)
    
    def domain_pause(self, id):
        """Pause domain execution.
        """
        dom = int(id)
        eserver.inject('xend.domain.pause', id)
        return xc.domain_pause(dom=dom)
    
    def domain_shutdown(self, id, reason='poweroff'):
        """Shutdown domain (nicely).
        """
        dom = int(id)
        if dom <= 0:
            return 0
        eserver.inject('xend.domain.shutdown', [id, reason])
        val = xend.domain_shutdown(dom, reason)
        self.refresh()
        return val
    
    def domain_destroy(self, id, refresh=1):
        """Terminate domain immediately.
        """
        dom = int(id)
        if dom <= 0:
            return 0
        eserver.inject('xend.domain.destroy', id)
        val = xc.domain_destroy(dom=dom)
        if refresh: self.refresh()
        return val       

    def domain_migrate(self, id, dst):
        """Start domain migration.
        """
        # Need a cancel too?
        pass

    def domain_save(self, id, dst, progress=0):
        """Save domain state to file, destroy domain.
        """
        dom = int(id)
        dominfo = self.domain_get(id)
        if not dominfo:
            return -1
        vmconfig = sxp.to_string(dominfo.sxpr())
        self.domain_pause(id)
        eserver.inject('xend.domain.save', id)
        rc = xc.linux_save(dom=dom, state_file=dst, vmconfig=vmconfig, progress=progress)
        if rc == 0:
            self.domain_destroy(id)
        return rc
    
    def domain_restore(self, src, progress=0):
        """Restore domain from file.
        """
        dominfo = XendDomainInfo.vm_restore(src, progress=progress)
        self._add_domain(dominfo.id, dominfo)
        return dominfo
    
    #============================================================================
    # Backward compatibility stuff from here on.

    def domain_pincpu(self, dom, cpu):
        dom = int(dom)
        return xc.domain_pincpu(dom, cpu)

    def domain_cpu_bvt_set(self, dom, mcuadv, warp, warpl, warpu):
        dom = int(dom)
        return xc.bvtsched_domain_set(dom=dom, mcuadv=mcuadv,
                                      warp=warp, warpl=warpl, warpu=warpu)

    def domain_cpu_bvt_get(self, dom):
        dom = int(dom)
        return xc.bvtsched_domain_get(dom)
    
    def domain_cpu_atropos_set(self, dom, period, slice, latency, xtratime):
        dom = int(dom)
        return xc.atropos_domain_set(dom, period, slice, latency, xtratime)

    def domain_cpu_atropos_get(self, dom):
        dom = int(dom)
        return xc.atropos_domain_get(dom)

    def domain_vif_ls(self, dom):
        dominfo = self.domain_get(dom)
        if not dominfo: return None
        devs = dominfo.get_devices('vif')
        return range(0, len(devs))

    def domain_vif_get(self, dom, vif):
        dominfo = self.domain_get(dom)
        if not dominfo: return None
        return dominfo.get_device_by_index(vif)

##     def domain_vif_ip_add(self, dom, vif, ip):
##         dom = int(dom)
##         return xenctl.ip.setup_vfr_rules_for_vif(dom, vif, ip)

    def domain_vbd_ls(self, dom):
        dominfo = self.domain_get(dom)
        if not dominfo: return []
        devs = dominfo.get_devices('vbd')
        return [ sxp.child_value(v, 'dev') for v in devs ]

    def domain_vbd_get(self, dom, vbd):
        dominfo = self.domain_get(dom)
        if not dominfo: return None
        devs = dominfo.get_devices('vbd')
        for v in devs:
            if sxp.child_value(v, 'dev') == vbd:
                return v
        return None

    def domain_shadow_control(self, dom, op):
        dom = int(dom)
        return xc.shadow_control(dom, op)

    #============================================================================

def instance():
    global inst
    try:
        inst
    except:
        inst = XendDomain()
    return inst