aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xm-test/lib/XmTestLib/XenDomain.py
blob: 3ba7433e2bce9d003df1f1fc577173c9dcd8ebc6 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/python
"""
 Copyright (C) International Business Machines Corp., 2005
 Author: Dan Smith <danms@us.ibm.com>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; under version 2 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""

import sys
import commands
import re
import time

from Xm import *
from arch import *
from Test import *
from config import *
from Console import *
from XenDevice import *
from DomainTracking import *


DOM0_UUID = "00000000-0000-0000-0000-000000000000"


def getDefaultKernel():
    return arch.getDefaultKernel()

def getRdPath():
    return arch.getRdPath()

def getUniqueName():
    """Get a uniqueish name for use in a domain"""
    unixtime = int(time.time())
    test_name = sys.argv[0]
    test_name = re.sub("\.test", "", test_name)
    test_name = re.sub("[\/\.]", "", test_name)
    name = "%s-%i" % (test_name, unixtime)

    return name

class XenConfig:
    """An object to help create a xen-compliant config file"""
    def __init__(self):
        self.defaultOpts = {}

        # These options need to be lists
        self.defaultOpts["disk"] = []
        self.defaultOpts["vif"]  = []

        self.opts = self.defaultOpts

    def toString(self):
        """Convert this config to a string for writing out
        to a file"""
        string = "# Xen configuration generated by xm-test\n"
        for k, v in self.opts.items():
            if isinstance(v, int):
                piece = "%s = %i" % (k, v)
            elif isinstance(v, list) and v:
                piece = "%s = %s" % (k, v)
            elif isinstance(v, str) and v:
                piece = "%s = \"%s\"" % (k, v)
            else:
                piece = None

            if piece:
                string += "%s\n" % piece

        return string

    def write(self, filename):
        """Write this config out to filename"""
        output = file(filename, "w")
        output.write(self.toString())
        output.close()

    def __str__(self):
        """When used as a string, we represent ourself by a config
        filename, which points to a temporary config that we write
        out ahead of time"""
        filename = "/tmp/xm-test.conf"
        self.write(filename)
        return filename

    def setOpt(self, name, value):
        """Set an option in the config"""
        if name in self.opts.keys() and isinstance(self.opts[name] ,
                                        list) and not isinstance(value, list):
                self.opts[name] = [value]
        # "extra" is special so append to it.
        elif name == "extra" and name in self.opts.keys():
            self.opts[name] += " %s" % (value)
        else:
            self.opts[name] = value

    def appOpt(self, name, value):
        """Append a value to a list option"""
        if name in self.opts.keys() and isinstance(self.opts[name], list):
            self.opts[name].append(value)

    def getOpt(self, name):
        """Return the value of a config option"""
        if name in self.opts.keys():
            return self.opts[name]
        else:
            return None

    def setOpts(self, opts):
        """Batch-set options from a dictionary"""
        for k, v in opts.items():
            self.setOpt(k, v)

    def clearOpts(self, name=None):
        """Clear one or all config options"""
        if name:
            self.opts[name] = self.defaultOpts[name]
        else:
            self.opts = self.defaultOpts

class DomainError(Exception):
    def __init__(self, msg, extra="", errorcode=0):
        self.msg = msg
        self.extra = extra
        try:
            self.errorcode = int(errorcode)
        except Exception, e:
            self.errorcode = -1

    def __str__(self):
        return str(self.msg)


class XenDomain:

    def __init__(self, name=None, config=None, isManaged=False):
        """Create a domain object.
        @param config: String filename of config file
        """

        if name:
            self.name = name
        else:
            self.name = getUniqueName()

        self.config = config
        self.console = None
        self.devices = {}
        self.netEnv = "bridge"

        if os.getenv("XM_MANAGED_DOMAINS"):
            isManaged = True
        self.isManaged = isManaged

        # Set domain type, either PV for ParaVirt domU or HVM for
        # FullVirt domain
        if ENABLE_HVM_SUPPORT:
            self.type = "HVM"
        else:
            self.type = "PV"

    def start(self, noConsole=False):

        if not self.isManaged:
            ret, output = traceCommand("xm create %s" % self.config)
            print self.config
        else:
            ret, output = traceCommand("xm new %s" % self.config)
            if ret != 0:
                _ret, output = traceCommand("xm delete " +
                                            self.config.getOpt("name"))
            else:
                ret, output = traceCommand("xm start " +
                                           self.config.getOpt("name"))
                addManagedDomain(self.config.getOpt("name"))

        if ret != 0:
            raise DomainError("Failed to create domain",
                              extra=output,
                              errorcode=ret)

        # HVM domains require waiting for boot
        if self.getDomainType() == "HVM":
            waitForBoot()

        # Go through device list and run console cmds
        for dev in self.devices.keys():
            self.devices[dev].execAddCmds()

        if self.console and noConsole == True:
            self.closeConsole()

        elif self.console and noConsole == False:
            return self.console

        elif not self.console and noConsole == False:
            return self.getConsole()

    def stop(self):
        prog = "xm"
        cmd = " shutdown "

        self.removeAllDevices()

        if self.console:
            self.closeConsole()

        ret, output = traceCommand(prog + cmd + self.config.getOpt("name"))

        return ret

    def destroy(self):
        prog = "xm"
        cmd = " destroy "

        self.removeAllDevices()

        if self.console:
            self.closeConsole()

        ret, output = traceCommand(prog + cmd + self.config.getOpt("name"))
        if self.isManaged:
            ret, output = traceCommand(prog + " delete " +
                                       self.config.getOpt("name"))
            delManagedDomain(self.config.getOpt("name"))

        return ret

    def getName(self):
        return self.name

    def getId(self):
        return domid(self.getName());

    def getDomainType(self):
        return self.type

    def closeConsole(self):
        # The domain closeConsole command must be called by tests, not the
        # console's close command. Once close is called, the console is
        # gone. You can't get history or anything else from it.
        if self.console:
            self.console._XmConsole__closeConsole()
            self.console = None

    def getConsole(self):
        if self.console:
            self.closeConsole()

        self.console = XmConsole(self.getName())
        # Activate the console
        self.console.sendInput("input")

        return self.console

    def newDevice(self, Device, *args):
        """Device Factory: Generic factory for creating new XenDevices.
           All device creation should be done through the XenDomain
           factory. Supply a XenDevice instance and its args and the
           constructor will be called."""
        # Make sure device with id hasn't already been added
        if self.devices.has_key(args[0]):
            raise DeviceError("Error: Domain already has device %s" % args[0])

        # Call constructor for supplied Device instance
        dargs = (self,)
        dargs += args
        dev = apply(Device, dargs)

        if self.isRunning():
            # Note: This needs to be done, XenDevice should have an attach
            #       method.
            print "Domain is running, need to attach new device to domain."

        self.devices[dev.id] = dev
        self.config.appOpt(dev.configNode, str(dev))
        return dev

    def removeDevice(self, id):
        if self.devices.has_key(id):
            self.devices[id].removeDevice()

    def removeAllDevices(self):
        for k in self.devices.keys():
            self.removeDevice(k)

    def isRunning(self):
        return isDomainRunning(self.name)

    def getNetEnv(self):
        # We need to know the network environment: bridge, NAT, or routed.
        return self.netEnv

    def getDevice(self, id):
        dev = self.devices[id]
        if dev:
            return dev
        print "Device %s not found for domain %s" % (id, self.getName())


class XmTestDomain(XenDomain):

    def __init__(self, name=None, extraConfig=None,
                 baseConfig=arch.configDefaults, isManaged=False):
        """Create a new xm-test domain
        @param name: The requested domain name
        @param extraConfig: Additional configuration options
        @param baseConfig: The initial configuration defaults to use
        """
        config = XenConfig()
        config.setOpts(baseConfig)
        if extraConfig:
            config.setOpts(extraConfig)

        if name:
            config.setOpt("name", name)
        elif not config.getOpt("name"):
            config.setOpt("name", getUniqueName())

        XenDomain.__init__(self, config.getOpt("name"), config=config,
                           isManaged=isManaged)

    def minSafeMem(self):
        return arch.minSafeMem

class XmTestNetDomain(XmTestDomain):

    def __init__(self, name=None, extraConfig=None,
                 baseConfig=arch.configDefaults):
        """Create a new xm-test domain with one network device
        @param name: The requested domain name
        @param extraConfig: Additional configuration options
        @param baseConfig: The initial configuration defaults to use
        """
        config = XenConfig()
        config.setOpts(baseConfig)
        if extraConfig:
            config.setOpts(extraConfig)

        if name:
            config.setOpt("name", name)
        elif not config.getOpt("name"):
            config.setOpt("name", getUniqueName())

        XenDomain.__init__(self, config.getOpt("name"), config=config)

        # Add one network devices to domain
        self.newDevice(XenNetDevice, "eth0")


if __name__ == "__main__":

    c = XenConfig()

    c.setOpt("foo", "bar")
    c.setOpt("foob", 1)
    opts = {"opt1" : 19,
            "opt2" : "blah"}
    c.setOpts(opts)

    c.setOpt("disk", "phy:/dev/ram0,hda1,w")
    c.appOpt("disk", "phy:/dev/ram1,hdb1,w")

    print str(c)



#    c.write("/tmp/foo.conf")

#    d = XmTestDomain();
#
#    d.start();