-
Notifications
You must be signed in to change notification settings - Fork 19
/
hardware.py
134 lines (105 loc) · 3.66 KB
/
hardware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# SPDX-License-Identifier: GPL-2.0-only
import constants
import util
import re
import os.path
from xcp import logger
import xen.lowlevel.xc as xc
XC = xc.xc()
PHYSINFO = XC.physinfo()
XENINFO = XC.xeninfo()
################################################################################
# Functions to get characteristics of the host. Can work in a VM too, to aid
# with developer testing.
def VTSupportEnabled():
""" Checks if VT support is present. Uses /sys/hypervisor to do so,
expecting a single line in this file with a space separated list of
capabilities. """
f = open(constants.HYPERVISOR_CAPS_FILE, 'r')
caps = ""
try:
caps = f.readline()
finally:
f.close()
return "hvm-3.0-x86_32" in caps.strip().split(" ")
def VM_getHostTotalMemoryKB():
# Use /proc/meminfo to get this. It has a MemFree entry with the value we
# need. The format is lines like this "XYZ: 123 kB".
meminfo = {}
f = open("/proc/meminfo", "r")
try:
for line in f:
k, v = line.split(":")
meminfo[k.strip()] = int(v.strip()[:-3])
finally:
f.close()
return meminfo['MemTotal']
def PhysHost_getHostTotalMemoryKB():
if PHYSINFO is None or 'total_memory' not in PHYSINFO:
raise RuntimeError("Unable to determine host memory")
return PHYSINFO['total_memory']
def VM_getSerialConfig():
return None
def PhysHost_getSerialConfig():
if XENINFO is None or 'xen_commandline' not in XENINFO:
return None
m = re.match(r'.*(com\d=\S+)', XENINFO['xen_commandline'])
return m and m.group(1) or None
def PhysHost_getHostTotalCPUs():
if PHYSINFO is None or 'nr_cpus' not in PHYSINFO:
raise RuntimeError("Unable to determine number of CPUs")
return PHYSINFO['nr_cpus']
getHostTotalMemoryKB = PhysHost_getHostTotalMemoryKB
getSerialConfig = PhysHost_getSerialConfig
getHostTotalCPUs = PhysHost_getHostTotalCPUs
def useVMHardwareFunctions():
global getHostTotalMemoryKB, getSerialConfig
getHostTotalMemoryKB = VM_getHostTotalMemoryKB
getSerialConfig = VM_getSerialConfig
def is_serialConsole(console):
return console.startswith('hvc') or console.startswith('ttyS')
class SerialPort:
def __init__(self, idv, dev=None, port=None, baud='9600', data='8',
parity='n', stop='1', term='vt102', extra=''):
if not dev:
dev = "hvc0"
if not port:
port = "com%d" % (idv+1)
self.id = idv
self.dev = dev
self.port = port
self.baud = baud
self.data = data
self.parity = parity
self.stop = stop
self.term = term
self.extra = extra
@classmethod
def from_string(cls, console):
"""Create instance from Xen console parameter (e.g. com1=115200,8n1)"""
port = 'com1'
baud = '9600'
data = '8'
parity = 'n'
stop = '1'
extra = ''
m = re.match(r'(com\d+)=(\d+)(?:/\d+)?(?:,(\d)(.)?(\d)?)?((?:,.*)*)$', console)
if m:
port = m.group(1)
baud = m.group(2)
if m.group(3):
data = m.group(3)
if m.group(4):
parity = m.group(4)
if m.group(5):
stop = m.group(5)
if m.group(6):
extra = m.group(6)
return cls(0, None, port, baud, data, parity, stop, extra=extra)
def __repr__(self):
return "<SerialPort: %s>" % self.xenFmt()
def kernelFmt(self):
return self.dev
def xenFmt(self):
return "%s=%s,%s%s%s%s" % (self.port, self.baud, self.data,
self.parity, self.stop, self.extra)