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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#
# Copyright 2009 Red Hat, Inc.
# Cole Robinson <crobinso@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
import VirtualDevice
import NodeDeviceParser
import logging
from virtinst import _gettext as _
from XMLBuilderDomain import _xml_property
class VirtualHostDevice(VirtualDevice.VirtualDevice):
_virtual_device_type = VirtualDevice.VirtualDevice.VIRTUAL_DEV_HOSTDEV
def device_from_node(conn, name=None, nodedev=None):
"""
Convert the passed device name to a VirtualHostDevice
instance, with proper error reporting. Name can be any of the
values accepted by NodeDeviceParser.lookupNodeName. If a node
device name is not specified, a virtinst.NodeDevice instance can
be passed in to create a dev from.
@param conn: libvirt.virConnect instance to perform the lookup on
@param name: optional libvirt node device name to lookup
@param nodedev: optional L{virtinst.NodeDevice} instance to use
@rtype: L{virtinst.VirtualHostDevice} instance
"""
if not name and not nodedev:
raise ValueError(_("'name' or 'nodedev' required."))
if nodedev:
nodeinst = nodedev
else:
nodeinst = NodeDeviceParser.lookupNodeName(conn, name)
if isinstance(nodeinst, NodeDeviceParser.PCIDevice):
return VirtualHostDevicePCI(conn, nodedev=nodeinst)
elif isinstance(nodeinst, NodeDeviceParser.USBDevice):
return VirtualHostDeviceUSB(conn, nodedev=nodeinst)
elif isinstance(nodeinst, NodeDeviceParser.NetDevice):
parentname = nodeinst.parent
try:
return VirtualHostDevice.device_from_node(conn,
name=parentname)
except:
logging.exception("Fetching net parent device failed.")
raise ValueError(_("Node device type '%s' cannot be attached to "
" guest.") % nodeinst.device_type)
device_from_node = staticmethod(device_from_node)
def __init__(self, conn, nodedev=None,
parsexml=None, parsexmlnode=None, caps=None):
"""
@param conn: Connection the device/guest will be installed on
@type conn: libvirt.virConnect
@param nodedev: Optional NodeDevice instance for device being
attached to the guest
@type nodedev: L{virtinst.NodeDeviceParser.NodeDevice}
"""
VirtualDevice.VirtualDevice.__init__(self, conn, parsexml,
parsexmlnode, caps)
self._mode = None
self._type = None
self._managed = None
self._nodedev = nodedev
self._vendor = None
self._product = None
self._bus = None
self._device = None
self._domain = "0x0"
self._slot = None
self._function = None
if self._is_parse():
return
self.managed = True
if self.is_xen():
self.managed = False
def get_mode(self):
return self._mode
def set_mode(self, val):
self._mode = val
mode = _xml_property(get_mode, set_mode,
xpath="./@mode")
def get_type(self):
return self._type
def set_type(self, val):
self._type = val
type = _xml_property(get_type, set_type,
xpath="./@type")
def get_managed(self):
return self._managed
def set_managed(self, val):
self._managed = bool(val)
managed = _xml_property(get_managed, set_managed,
get_converter=lambda s, x: bool(x == "yes"),
set_converter=lambda s, x: x and "yes" or "no",
xpath="./@managed")
def get_vendor(self):
return self._vendor
def set_vendor(self, val):
self._vendor = val
vendor = _xml_property(get_vendor, set_vendor,
xpath="./source/vendor/@id")
def get_product(self):
return self._product
def set_product(self, val):
self._product = val
product = _xml_property(get_product, set_product,
xpath="./source/product/@id")
def get_device(self):
return self._device
def set_device(self, val):
self._device = val
device = _xml_property(get_device, set_device,
xpath="./source/address/@device")
def get_bus(self):
return self._bus
def set_bus(self, val):
self._bus = val
bus = _xml_property(get_bus, set_bus,
xpath="./source/address/@bus")
def get_function(self):
return self._function
def set_function(self, val):
self._function = val
function = _xml_property(get_function, set_function,
xpath="./source/address/@function")
def get_domain(self):
return self._domain
def set_domain(self, val):
self._domain = val
domain = _xml_property(get_domain, set_domain,
xpath="./source/address/@domain")
def get_slot(self):
return self._slot
def set_slot(self, val):
self._slot = val
slot = _xml_property(get_slot, set_slot,
xpath="./source/address/@slot")
def _get_source_xml(self):
raise NotImplementedError("Must be implemented in subclass")
def setup(self, conn=None):
"""
Unused
@param conn: libvirt virConnect instance to use (defaults to devices
connection)
"""
ignore = conn
def _get_xml_config(self):
xml = (" <hostdev mode='%s' type='%s' managed='%s'>\n" % \
(self.mode, self.type, self.managed and "yes" or "no"))
xml += " <source>\n"
xml += self._get_source_xml()
xml += " </source>\n"
xml += " </hostdev>"
return xml
class VirtualHostDeviceUSB(VirtualHostDevice):
def __init__(self, conn, nodedev=None):
VirtualHostDevice.__init__(self, conn, nodedev)
self.mode = "subsystem"
self.type = "usb"
self._set_from_nodedev(self._nodedev)
def _set_from_nodedev(self, nodedev):
if not nodedev:
return
if not isinstance(nodedev, NodeDeviceParser.USBDevice):
raise ValueError(_("'nodedev' must be a USBDevice instance."))
self.vendor = nodedev.vendor_id
self.product = nodedev.product_id
if not (self.vendor or self.product):
self.bus = nodedev.bus
self.device = nodedev.device
def _get_source_xml(self):
xml = ""
found = False
if self.vendor and self.product:
xml += " <vendor id='%s'/>\n" % self.vendor
xml += " <product id='%s'/>\n" % self.product
found = True
if self.bus and self.device:
xml += " <address bus='%s' device='%s'/>\n" % (self.bus,
self.device)
found = True
if not found:
raise RuntimeError(_("'vendor' and 'product', or 'bus' and "
" 'device' are required."))
return xml
class VirtualHostDevicePCI(VirtualHostDevice):
def __init__(self, conn, nodedev=None):
VirtualHostDevice.__init__(self, conn, nodedev)
self.mode = "subsystem"
self.type = "pci"
self._set_from_nodedev(self._nodedev)
def _set_from_nodedev(self, nodedev):
if not nodedev:
return
if not isinstance(nodedev, NodeDeviceParser.PCIDevice):
raise ValueError(_("'nodedev' must be a PCIDevice instance."))
self.domain = nodedev.domain
self.bus = nodedev.bus
self.slot = nodedev.slot
self.function = nodedev.function
def _get_source_xml(self):
if not (self.domain and self.bus and self.slot and self.function):
raise RuntimeError(_("'domain', 'bus', 'slot', and 'function' "
"must be specified."))
xml = " <address domain='%s' bus='%s' slot='%s' function='%s'/>\n"
return xml % (self.domain, self.bus, self.slot, self.function)
|