direct-io.hg

view tools/xm-test/lib/XmTestLib/XenDomain.py @ 9682:addc26c4b4a6

Small Memory fix patch #2: I forgot to change the XmTestDomain method
minSafeMem from 16 to 32.

Signed-off-by: Daniel Stekloff <dsteklof@us.ibm.com>
author stekloff@dyn9047022152.beaverton.ibm.com
date Wed Apr 19 22:58:16 2006 +0100 (2006-04-19)
parents d218dd060612
children 47dede68bbed
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
27 from Xm import *
28 from Test import *
29 from config import *
31 BLOCK_ROOT_DEV = "hda"
33 def getDeviceModel():
34 """Get the path to the device model based on
35 the architecture reported in uname"""
36 arch = os.uname()[4]
37 if re.search("64", arch):
38 return "/usr/lib64/xen/bin/qemu-dm"
39 else:
40 return "/usr/lib/xen/bin/qemu-dm"
42 def getDefaultKernel():
43 """Get the path to the default DomU kernel"""
44 dom0Ver = commands.getoutput("uname -r");
45 domUVer = dom0Ver.replace("xen0", "xenU");
47 return "/boot/vmlinuz-" + domUVer;
49 def getUniqueName():
50 """Get a uniqueish name for use in a domain"""
51 unixtime = int(time.time())
52 test_name = sys.argv[0]
53 test_name = re.sub("\.test", "", test_name)
54 test_name = re.sub("[\/\.]", "", test_name)
55 name = "%s-%i" % (test_name, unixtime)
57 return name
59 def getRdPath():
60 rdpath = os.environ.get("RD_PATH")
61 if not rdpath:
62 rdpath = "../../ramdisk"
63 rdpath = os.path.abspath(rdpath)
65 return rdpath
67 ParavirtDefaults = {"memory" : 64,
68 "vcpus" : 1,
69 "kernel" : getDefaultKernel(),
70 "root" : "/dev/ram0",
71 "ramdisk" : getRdPath() + "/initrd.img"
72 }
73 HVMDefaults = {"memory" : 64,
74 "vcpus" : 1,
75 "acpi" : 0,
76 "apic" : 0,
77 "disk" : ["file:%s/disk.img,ioemu:%s,w!" %
78 (getRdPath(), BLOCK_ROOT_DEV)],
79 "kernel" : "/usr/lib/xen/boot/hvmloader",
80 "builder" : "hvm",
81 "sdl" : 0,
82 "vnc" : 0,
83 "vncviewer" : 0,
84 "nographic" : 1,
85 "serial" : "pty",
86 "device_model" : getDeviceModel()
87 }
89 if ENABLE_HVM_SUPPORT:
90 configDefaults = HVMDefaults
91 else:
92 configDefaults = ParavirtDefaults
94 class XenConfig:
95 """An object to help create a xen-compliant config file"""
96 def __init__(self):
97 self.defaultOpts = {}
99 # These options need to be lists
100 self.defaultOpts["disk"] = []
101 self.defaultOpts["vif"] = []
102 self.defaultOpts["vtpm"] = []
104 self.opts = self.defaultOpts
106 def toString(self):
107 """Convert this config to a string for writing out
108 to a file"""
109 string = "# Xen configuration generated by xm-test\n"
110 for k, v in self.opts.items():
111 if isinstance(v, int):
112 piece = "%s = %i" % (k, v)
113 elif isinstance(v, list) and v:
114 piece = "%s = %s" % (k, v)
115 elif isinstance(v, str) and v:
116 piece = "%s = \"%s\"" % (k, v)
117 else:
118 piece = None
120 if piece:
121 string += "%s\n" % piece
123 return string
125 def write(self, filename):
126 """Write this config out to filename"""
127 output = file(filename, "w")
128 output.write(self.toString())
129 output.close()
131 def __str__(self):
132 """When used as a string, we represent ourself by a config
133 filename, which points to a temporary config that we write
134 out ahead of time"""
135 filename = "/tmp/xm-test.conf"
136 self.write(filename)
137 return filename
139 def setOpt(self, name, value):
140 """Set an option in the config"""
141 if name in self.opts.keys() and isinstance(self.opts[name], list) and not isinstance(value, list):
142 self.opts[name] = [value]
143 else:
144 self.opts[name] = value
146 def appOpt(self, name, value):
147 """Append a value to a list option"""
148 if name in self.opts.keys() and isinstance(self.opts[name], list):
149 self.opts[name].append(value)
151 def getOpt(self, name):
152 """Return the value of a config option"""
153 if name in self.opts.keys():
154 return self.opts[name]
155 else:
156 return None
158 def setOpts(self, opts):
159 """Batch-set options from a dictionary"""
160 for k, v in opts.items():
161 self.setOpt(k, v)
163 def clearOpts(self, name=None):
164 """Clear one or all config options"""
165 if name:
166 self.opts[name] = self.defaultOpts[name]
167 else:
168 self.opts = self.defaultOpts
170 class DomainError(Exception):
171 def __init__(self, msg, extra="", errorcode=0):
172 self.msg = msg
173 self.extra = extra
174 try:
175 self.errorcode = int(errorcode)
176 except Exception, e:
177 self.errorcode = -1
179 def __str__(self):
180 return str(self.msg)
183 class XenDomain:
185 def __init__(self, name=None, config=None):
186 """Create a domain object.
187 @param config: String filename of config file
188 """
190 if name:
191 self.name = name
192 else:
193 self.name = getUniqueName()
195 self.config = config
197 def start(self):
199 ret, output = traceCommand("xm create %s" % self.config)
201 if ret != 0:
202 raise DomainError("Failed to create domain",
203 extra=output,
204 errorcode=ret)
206 def stop(self):
207 prog = "xm"
208 cmd = " shutdown "
210 ret, output = traceCommand(prog + cmd + self.config.getOpt("name"))
212 return ret
214 def destroy(self):
215 prog = "xm"
216 cmd = " destroy "
218 ret, output = traceCommand(prog + cmd + self.config.getOpt("name"))
220 return ret
222 def getName(self):
223 return self.name
225 def getId(self):
226 return domid(self.getName());
229 class XmTestDomain(XenDomain):
231 def __init__(self, name=None, extraConfig=None, baseConfig=configDefaults):
232 """Create a new xm-test domain
233 @param name: The requested domain name
234 @param extraConfig: Additional configuration options
235 @param baseConfig: The initial configuration defaults to use
236 """
237 config = XenConfig()
238 config.setOpts(baseConfig)
239 if extraConfig:
240 config.setOpts(extraConfig)
242 if name:
243 config.setOpt("name", name)
244 elif not config.getOpt("name"):
245 config.setOpt("name", getUniqueName())
247 XenDomain.__init__(self, config.getOpt("name"), config=config)
249 def start(self):
250 XenDomain.start(self)
251 if ENABLE_HVM_SUPPORT:
252 waitForBoot()
254 def minSafeMem(self):
255 return 32
257 if __name__ == "__main__":
259 c = XenConfig()
261 c.setOpt("foo", "bar")
262 c.setOpt("foob", 1)
263 opts = {"opt1" : 19,
264 "opt2" : "blah"}
265 c.setOpts(opts)
267 c.setOpt("disk", "phy:/dev/ram0,hda1,w")
268 c.appOpt("disk", "phy:/dev/ram1,hdb1,w")
270 print str(c)
274 # c.write("/tmp/foo.conf")
276 # d = XmTestDomain();
277 #
278 # d.start();