ia64/xen-unstable

view tools/xm-test/lib/XmTestLib/XenDomain.py @ 7757:139aabe357a4

Various chmod a+x.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Nov 10 12:00:09 2005 +0100 (2005-11-10)
parents ecb5997f7b25
children
line source
1 #!/usr/bin/python
2 """
3 Copyright (C) International Business Machines Corp., 2005
4 Author: Dan Smith <danms@us.ibm.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; under version 2 of the License.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 """
21 import sys
22 import commands
23 import os
24 import re
25 import time
26 import random
28 from Xm import *
29 from Test import *
30 from config import *
32 BLOCK_ROOT_DEV = "hdd1"
34 def getDefaultKernel():
35 dom0Ver = commands.getoutput("uname -r");
36 domUVer = dom0Ver.replace("xen0", "xenU");
38 return "/boot/vmlinuz-" + domUVer;
41 class DomainError(Exception):
42 def __init__(self, msg, extra="", errorcode=0):
43 self.msg = msg
44 self.extra = extra
45 try:
46 self.errorcode = int(errorcode)
47 except Exception, e:
48 self.errorcode = -1
50 def __str__(self):
51 return str(self.msg)
53 class XenDomain:
55 def __init__(self, opts={}, config="/dev/null"):
56 """Create a domain object. Optionally take a
57 dictionary of 'xm' options to use"""
59 self.domID = None;
60 self.config = config
62 if not opts.has_key("name"):
63 raise DomainError("Missing `name' option")
64 if not opts.has_key("memory"):
65 raise DomainError("Missing `memory' option")
66 if not opts.has_key("kernel"):
67 raise DomainError("Missing `kernel' option")
69 self.opts = opts
71 self.configVals = None
73 def __buildCmdLine(self):
74 c = "xm create %s" % self.config
76 for k in self.opts.keys():
77 c += " %s=%s" % (k, self.opts[k])
79 return c
81 def start(self):
83 if self.configVals:
84 self.__writeConfig("/tmp/xm-test.conf")
85 self.config = "/tmp/xm-test.conf"
87 commandLine = self.__buildCmdLine()
89 ret, output = traceCommand(commandLine);
91 try:
92 self.domID = int(domid(self.domName));
93 except:
94 self.domID = -1;
96 if ret != 0:
97 raise DomainError("Failed to create domain",
98 extra=output,
99 errorcode=ret)
101 def stop(self):
102 prog = "xm";
103 cmd = " shutdown ";
105 ret, output = traceCommand(prog + cmd + self.opts["name"]);
107 return ret;
109 def destroy(self):
110 prog = "xm";
111 cmd = " destroy ";
113 ret, output = traceCommand(prog + cmd + self.opts["name"]);
115 return ret;
117 def getName(self):
118 return self.opts["name"];
120 def getId(self):
121 return domid(self.getName());
123 def configSetVar(self, key, value):
124 if not self.configVals:
125 self.configVals = {}
127 self.configVals[key] = value
129 def configAddDisk(self, pdev, vdev, acc):
130 if not self.configVals:
131 self.configVals = {}
133 if not self.configVals.has_key("disk"):
134 self.configVals["disk"] = []
136 self.configVals["disk"].append("%s,%s,%s" % (pdev,vdev,acc))
138 def __writeConfig(self, configFileName):
140 conf = file(configFileName, "w")
142 for k,v in self.configVals.items():
143 print >>conf, "%s = %s" % (k, v)
145 conf.close()
148 class XmTestDomain(XenDomain):
150 def __getUniqueName(self):
151 #
152 # We avoid multiple duplicate names
153 # here because they stick around in xend
154 # too long
155 #
156 unixtime = int(time.time())
157 test_name = sys.argv[0]
158 test_name = re.sub("\.test", "", test_name)
159 test_name = re.sub("[\/\.]", "", test_name)
160 name = "%s-%i" % (test_name, unixtime)
162 return name
164 def __randomize(self):
165 # Random amount of memory between min and free_memory/2
166 freeMem = int(getInfo("free_memory"))
167 self.defaults["memory"] = random.randint(self.minSafeMem(),
168 freeMem/2)
170 self.defaults["vcpus"] = random.randint(1, 16)
172 self.defaults["nics"] = random.randint(0, 8)
174 if verbose:
175 print "*** Defaults after randomization:"
176 for k in self.defaults.keys():
177 print "*** %10s : %s" % (k, self.defaults[k])
179 def __prepareBlockRoot(self, rdpath):
180 image = os.path.abspath(rdpath + "/initrd.img")
181 self.configAddDisk("file:%s" % image, BLOCK_ROOT_DEV, "w")
182 self.defaults["root"] = "/dev/%s" % BLOCK_ROOT_DEV
183 del self.defaults["ramdisk"]
185 def __init__(self, name=None, extraOpts=None, config="/dev/null",
186 random=False):
188 rdpath = os.environ.get("RD_PATH")
189 if not rdpath:
190 rdpath = "../../ramdisk"
192 self.opts = {}
193 self.configVals = None
195 # Defaults
196 self.defaults = {"memory" : 64,
197 "vcpus" : 1,
198 "nics" : 0,
199 "kernel" : getDefaultKernel(),
200 "root" : "/dev/ram0",
201 "name" : name or self.__getUniqueName(),
202 "ramdisk" : rdpath + "/initrd.img"
203 }
205 self.domID = None;
206 self.config = config;
208 if random:
209 self.__randomize()
211 if USE_BLKDEV_FOR_ROOT:
212 self.__prepareBlockRoot(rdpath)
214 # Copy over defaults
215 for key in self.defaults.keys():
216 self.opts[key] = self.defaults[key]
218 # Merge in extra options
219 if extraOpts:
220 for key in extraOpts.keys():
221 self.opts[key] = extraOpts[key]
223 def start(self):
224 """We know how about how long everyone will need to wait
225 for our ramdisk to come up, so we do it here as a convenience"""
227 # for i in range(0,5):
228 # status, output = traceCommand("xm list")
230 XenDomain.start(self)
231 waitForBoot()
233 def startNow(self):
234 XenDomain.start(self)
236 def getMem(self):
237 return int(self.opts["memory"])
239 def minSafeMem(self):
240 return 16
242 if __name__ == "__main__":
244 d = XmTestDomain();
246 d.start();