ia64/xen-unstable

view tools/python/xen/xend/osdep.py @ 19628:23f9857f642f

tools: get rid of hardcoded config dirs

Remove *all* hardcoded "/etc/xen" strings in python code.

Additionally, it removes pygrub_path from osdep.py. Its use has been
replaced with auxbin.pathTo("pygrub").

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed May 20 15:27:30 2009 +0100 (2009-05-20)
parents e2de0e760a0d
children
line source
1 #!/usr/bin/env python
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of version 2.1 of the GNU Lesser General Public
5 # License as published by the Free Software Foundation.
6 #
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser General Public
13 # License along with this library; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 #
17 # Copyright 2006 Sun Microsystems, Inc. All rights reserved.
18 # Use is subject to license terms.
20 import os
21 import commands
23 _xend_autorestart = {
24 "NetBSD": True,
25 "Linux": True,
26 "SunOS": False,
27 }
29 _vif_script = {
30 "SunOS": "vif-vnic"
31 }
33 PROC_XEN_BALLOON = '/proc/xen/balloon'
34 SYSFS_XEN_MEMORY = '/sys/devices/system/xen_memory/xen_memory0'
36 def _linux_balloon_stat_proc(label):
37 """Returns the value for the named label, or None if an error occurs."""
39 xend2linux_labels = { 'current' : 'Current allocation',
40 'target' : 'Requested target',
41 'low-balloon' : 'Low-mem balloon',
42 'high-balloon' : 'High-mem balloon',
43 'limit' : 'Xen hard limit' }
45 f = file(PROC_XEN_BALLOON, 'r')
46 try:
47 for line in f:
48 keyvalue = line.split(':')
49 if keyvalue[0] == xend2linux_labels[label]:
50 values = keyvalue[1].split()
51 if values[0].isdigit():
52 return int(values[0])
53 else:
54 return None
55 return None
56 finally:
57 f.close()
59 def _linux_balloon_stat_sysfs(label):
60 sysfiles = { 'target' : 'target_kb',
61 'current' : 'info/current_kb',
62 'low-balloon' : 'info/low_kb',
63 'high-balloon' : 'info/high_kb',
64 'limit' : 'info/hard_limit_kb' }
66 name = os.path.join(SYSFS_XEN_MEMORY, sysfiles[label])
67 f = file(name, 'r')
69 val = f.read().strip()
70 if val.isdigit():
71 return int(val)
72 return None
74 def _linux_balloon_stat(label):
75 if os.access(PROC_XEN_BALLOON, os.F_OK):
76 return _linux_balloon_stat_proc(label)
77 elif os.access(SYSFS_XEN_MEMORY, os.F_OK):
78 return _linux_balloon_stat_sysfs(label)
80 return None
82 def _solaris_balloon_stat(label):
83 """Returns the value for the named label, or None if an error occurs."""
85 import fcntl
86 import array
87 DEV_XEN_BALLOON = '/dev/xen/balloon'
88 BLN_IOCTL_CURRENT = 0x42410001
89 BLN_IOCTL_TARGET = 0x42410002
90 BLN_IOCTL_LOW = 0x42410003
91 BLN_IOCTL_HIGH = 0x42410004
92 BLN_IOCTL_LIMIT = 0x42410005
93 label_to_ioctl = { 'current' : BLN_IOCTL_CURRENT,
94 'target' : BLN_IOCTL_TARGET,
95 'low-balloon' : BLN_IOCTL_LOW,
96 'high-balloon' : BLN_IOCTL_HIGH,
97 'limit' : BLN_IOCTL_LIMIT }
99 f = file(DEV_XEN_BALLOON, 'r')
100 try:
101 values = array.array('L', [0])
102 if fcntl.ioctl(f.fileno(), label_to_ioctl[label], values, 1) == 0:
103 return values[0]
104 else:
105 return None
106 finally:
107 f.close()
109 _balloon_stat = {
110 "SunOS": _solaris_balloon_stat
111 }
113 def _linux_get_cpuinfo():
114 cpuinfo = {}
115 f = file('/proc/cpuinfo', 'r')
116 try:
117 p = -1
118 d = {}
119 for line in f:
120 keyvalue = line.split(':')
121 if len(keyvalue) != 2:
122 continue
123 key = keyvalue[0].strip()
124 val = keyvalue[1].strip()
125 if key == 'processor':
126 if p != -1:
127 cpuinfo[p] = d
128 p = int(val)
129 d = {}
130 else:
131 d[key] = val
132 cpuinfo[p] = d
133 return cpuinfo
134 finally:
135 f.close()
137 def _solaris_get_cpuinfo():
138 cpuinfo = {}
140 # call kstat to extrace specific cpu_info output
141 cmd = "/usr/bin/kstat -p -c misc -m cpu_info"
142 kstatoutput = commands.getoutput (cmd)
144 # walk each line
145 for kstatline in kstatoutput.split('\n'):
147 # split the line on
148 # module:cpu #:module#:name value
149 (module, cpunum, combo, namevalue) = kstatline.split (":")
151 # check to see if this cpunum is already a key. If not,
152 # initialize an empty hash table
153 if not cpuinfo.has_key (int(cpunum)):
154 cpuinfo[int(cpunum)] = {}
156 # split the namevalue output on whitespace
157 data = namevalue.split()
159 # the key will be data[0]
160 key = data[0]
162 # check the length of the data list. If it's larger than
163 # 2, join the rest of the list together with a space.
164 # Otherwise, value is just data[1]
165 if len (data) > 2:
166 value = ' '.join (data[1:])
167 else:
168 value = data[1]
170 # add this key/value pair to the cpuhash
171 cpuinfo[int(cpunum)][key] = value
173 # Translate Solaris tokens into what Xend expects
174 for key in cpuinfo.keys():
175 cpuinfo[key]["flags"] = ""
176 cpuinfo[key]["model name"] = cpuinfo[key]["brand"]
177 cpuinfo[key]["cpu MHz"] = cpuinfo[key]["clock_MHz"]
179 # return the hash table
180 return cpuinfo
182 _get_cpuinfo = {
183 "SunOS": _solaris_get_cpuinfo
184 }
186 def _default_prefork(name):
187 pass
189 def _default_postfork(ct, abandon=False):
190 pass
192 # call this for long-running processes that should survive a xend
193 # restart
194 def _solaris_prefork(name):
195 from xen.lowlevel import process
196 return process.activate(name)
198 def _solaris_postfork(ct, abandon=False):
199 from xen.lowlevel import process
200 process.clear(ct)
201 if abandon:
202 process.abandon_latest()
204 _get_prefork = {
205 "SunOS": _solaris_prefork
206 }
208 _get_postfork = {
209 "SunOS": _solaris_postfork
210 }
212 def _get(var, default=None):
213 return var.get(os.uname()[0], default)
215 xend_autorestart = _get(_xend_autorestart)
216 vif_script = _get(_vif_script, "vif-bridge")
217 lookup_balloon_stat = _get(_balloon_stat, _linux_balloon_stat)
218 get_cpuinfo = _get(_get_cpuinfo, _linux_get_cpuinfo)
219 prefork = _get(_get_prefork, _default_prefork)
220 postfork = _get(_get_postfork, _default_postfork)