ia64/xen-unstable

view tools/python/xen/xend/osdep.py @ 18129:0bf73f557f41

xend balloon: portability cleanup

Move the linux specific labels to osdep where they
belong. Modification on Solaris code ok'd by SUN (Ryan Scott).

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jul 22 11:55:06 2008 +0100 (2008-07-22)
parents f8221241d187
children 17911073a90a
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 xend2linux_labels = { 'current' : 'Current allocation',
45 'target' : 'Requested target',
46 'low-balloon' : 'Low-mem balloon',
47 'high-balloon' : 'High-mem balloon',
48 'limit' : 'Xen hard limit' }
50 PROC_XEN_BALLOON = '/proc/xen/balloon'
51 f = file(PROC_XEN_BALLOON, 'r')
52 try:
53 for line in f:
54 keyvalue = line.split(':')
55 if keyvalue[0] == xend2linux_labels[label]:
56 values = keyvalue[1].split()
57 if values[0].isdigit():
58 return int(values[0])
59 else:
60 return None
61 return None
62 finally:
63 f.close()
65 def _solaris_balloon_stat(label):
66 """Returns the value for the named label, or None if an error occurs."""
68 import fcntl
69 import array
70 DEV_XEN_BALLOON = '/dev/xen/balloon'
71 BLN_IOCTL_CURRENT = 0x42410001
72 BLN_IOCTL_TARGET = 0x42410002
73 BLN_IOCTL_LOW = 0x42410003
74 BLN_IOCTL_HIGH = 0x42410004
75 BLN_IOCTL_LIMIT = 0x42410005
76 label_to_ioctl = { 'current' : BLN_IOCTL_CURRENT,
77 'target' : BLN_IOCTL_TARGET,
78 'low-balloon' : BLN_IOCTL_LOW,
79 'high-balloon' : BLN_IOCTL_HIGH,
80 'limit' : BLN_IOCTL_LIMIT }
82 f = file(DEV_XEN_BALLOON, 'r')
83 try:
84 values = array.array('L', [0])
85 if fcntl.ioctl(f.fileno(), label_to_ioctl[label], values, 1) == 0:
86 return values[0]
87 else:
88 return None
89 finally:
90 f.close()
92 _balloon_stat = {
93 "SunOS": _solaris_balloon_stat
94 }
96 def _linux_get_cpuinfo():
97 cpuinfo = {}
98 f = file('/proc/cpuinfo', 'r')
99 try:
100 p = -1
101 d = {}
102 for line in f:
103 keyvalue = line.split(':')
104 if len(keyvalue) != 2:
105 continue
106 key = keyvalue[0].strip()
107 val = keyvalue[1].strip()
108 if key == 'processor':
109 if p != -1:
110 cpuinfo[p] = d
111 p = int(val)
112 d = {}
113 else:
114 d[key] = val
115 cpuinfo[p] = d
116 return cpuinfo
117 finally:
118 f.close()
120 _get_cpuinfo = {
121 }
123 def _get(var, default=None):
124 return var.get(os.uname()[0], default)
126 scripts_dir = _get(_scripts_dir, "/etc/xen/scripts")
127 xend_autorestart = _get(_xend_autorestart)
128 pygrub_path = _get(_pygrub_path, "/usr/bin/pygrub")
129 vif_script = _get(_vif_script, "vif-bridge")
130 lookup_balloon_stat = _get(_balloon_stat, _linux_balloon_stat)
131 get_cpuinfo = _get(_get_cpuinfo, _linux_get_cpuinfo)