ia64/xen-unstable

view tools/python/xen/xend/osdep.py @ 18107:f8221241d187

xend: portability cleanup

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jul 18 14:14:12 2008 +0100 (2008-07-18)
parents 40e7329105fa
children 0bf73f557f41
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
22 _scripts_dir = {
23 "Linux": "/etc/xen/scripts",
24 "SunOS": "/usr/lib/xen/scripts",
25 }
27 _xend_autorestart = {
28 "NetBSD": True,
29 "Linux": True,
30 "SunOS": False,
31 }
33 _pygrub_path = {
34 "SunOS": "/usr/lib/xen/bin/pygrub"
35 }
37 _vif_script = {
38 "SunOS": "vif-vnic"
39 }
41 def _linux_balloon_stat(label):
42 """Returns the value for the named label, or None if an error occurs."""
44 PROC_XEN_BALLOON = '/proc/xen/balloon'
45 f = file(PROC_XEN_BALLOON, 'r')
46 try:
47 for line in f:
48 keyvalue = line.split(':')
49 if keyvalue[0] == 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 _solaris_balloon_stat(label):
60 """Returns the value for the named label, or None if an error occurs."""
62 import fcntl
63 import array
64 DEV_XEN_BALLOON = '/dev/xen/balloon'
65 BLN_IOCTL_CURRENT = 0x42410001
66 BLN_IOCTL_TARGET = 0x42410002
67 BLN_IOCTL_LOW = 0x42410003
68 BLN_IOCTL_HIGH = 0x42410004
69 BLN_IOCTL_LIMIT = 0x42410005
70 label_to_ioctl = { 'Current allocation' : BLN_IOCTL_CURRENT,
71 'Requested target' : BLN_IOCTL_TARGET,
72 'Low-mem balloon' : BLN_IOCTL_LOW,
73 'High-mem balloon' : BLN_IOCTL_HIGH,
74 'Xen hard limit' : BLN_IOCTL_LIMIT }
76 f = file(DEV_XEN_BALLOON, 'r')
77 try:
78 values = array.array('L', [0])
79 if fcntl.ioctl(f.fileno(), label_to_ioctl[label], values, 1) == 0:
80 return values[0]
81 else:
82 return None
83 finally:
84 f.close()
86 _balloon_stat = {
87 "SunOS": _solaris_balloon_stat
88 }
90 def _linux_get_cpuinfo():
91 cpuinfo = {}
92 f = file('/proc/cpuinfo', 'r')
93 try:
94 p = -1
95 d = {}
96 for line in f:
97 keyvalue = line.split(':')
98 if len(keyvalue) != 2:
99 continue
100 key = keyvalue[0].strip()
101 val = keyvalue[1].strip()
102 if key == 'processor':
103 if p != -1:
104 cpuinfo[p] = d
105 p = int(val)
106 d = {}
107 else:
108 d[key] = val
109 cpuinfo[p] = d
110 return cpuinfo
111 finally:
112 f.close()
114 _get_cpuinfo = {
115 }
117 def _get(var, default=None):
118 return var.get(os.uname()[0], default)
120 scripts_dir = _get(_scripts_dir, "/etc/xen/scripts")
121 xend_autorestart = _get(_xend_autorestart)
122 pygrub_path = _get(_pygrub_path, "/usr/bin/pygrub")
123 vif_script = _get(_vif_script, "vif-bridge")
124 lookup_balloon_stat = _get(_balloon_stat, _linux_balloon_stat)
125 get_cpuinfo = _get(_get_cpuinfo, _linux_get_cpuinfo)