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 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
#
# Copyright 2009, 2013 Red Hat, Inc.
# Cole Robinson <crobinso@redhat.com>
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
#
import logging
import os
import xml.etree.ElementTree
import virtinst
from .formats import parser_class
# Mapping of ResourceType value to device type
# http://konkretcmpi.org/cim218/CIM_ResourceAllocationSettingData.html
#
# "Other" [1]
# "Computer System" [2]
# "Processor" [3]
# "Memory" [4]
# "IDE Controller" [5]
# "Parallel SCSI HBA" [6]
# "FC HBA" [7]
# "iSCSI HBA" [8]
# "IB HCA" [9]
# "Ethernet Adapter" [10]
# "Other Network Adapter" [11]
# "I/O Slot" [12]
# "I/O Device" [13]
# "Floppy Drive" [14]
# "CD Drive" [15]
# "DVD drive" [16]
# "Disk Drive" [17]
# "Tape Drive" [18]
# "Storage Extent" [19]
# "Other storage device" [20]
# "Serial port" [21]
# "Parallel port" [22]
# "USB Controller" [23]
# "Graphics controller" [24]
# "IEEE 1394 Controller" [25]
# "Partitionable Unit" [26]
# "Base Partitionable Unit" [27]
# "Power" [28]
# "Cooling Capacity" [29]
# "Ethernet Switch Port" [30]
DEVICE_CPU = "3"
DEVICE_MEMORY = "4"
DEVICE_IDE_BUS = "5"
DEVICE_SCSI_BUS = "6"
DEVICE_ETHERNET = "10"
DEVICE_DISK = "17"
DEVICE_GRAPHICS = "24"
# AllocationUnits mapping can be found in Appendix C here:
# https://www.dmtf.org/standards/documents/CIM/DSP0004.pdf
OVF_NAMESPACES = {
"ovf": "http://schemas.dmtf.org/ovf/envelope/1",
"ovfenv": "http://schemas.dmtf.org/ovf/environment/1",
"rasd": "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
"vssd": "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData",
"vmw": "http://www.vmware.com/schema/ovf",
}
def _convert_alloc_val(ignore, val):
# This is a hack, but should we really have to decode
# allocation units = "bytes * 2^20"?
val = float(val)
if val > 100000000:
# Assume bytes
return int(round(val / 1024.0 / 1024.0))
elif val > 100000:
# Assume kilobytes
return int(round(val / 1024.0))
elif val < 32:
# Assume GiB
return int(val * 1024)
return int(val)
def _convert_bool_val(val):
if str(val).lower() == "false":
return False
elif str(val).lower() == "true":
return True
return False
def _find(_node, _xpath):
return _node.find(_xpath, namespaces=OVF_NAMESPACES)
def _findall(_node, _xpath):
return _node.findall(_xpath, namespaces=OVF_NAMESPACES)
def _text(_node):
if _node is not None:
return _node.text
def _lookup_disk_path(root, path):
"""
Map the passed HostResource ID to the actual host disk path
"""
ref = None
def _path_has_prefix(prefix):
if path.startswith(prefix):
return path[len(prefix):]
if path.startswith("ovf:" + prefix):
return path[len("ovf:" + prefix):]
return False
if _path_has_prefix("/disk/"):
disk_ref = _path_has_prefix("/disk/")
xpath = "./ovf:DiskSection/ovf:Disk[@ovf:diskId='%s']" % disk_ref
dnode = _find(root, xpath)
if dnode is None:
raise ValueError(_("Unknown disk reference id '%s' "
"for path %s.") % (path, disk_ref))
ref = dnode.attrib["{%s}fileRef" % OVF_NAMESPACES["ovf"]]
elif _path_has_prefix("/file/"):
ref = _path_has_prefix("/file/")
else:
raise ValueError(_("Unknown storage path type %s.") % path)
xpath = "./ovf:References/ovf:File[@ovf:id='%s']" % ref
refnode = _find(root, xpath)
if refnode is None:
raise ValueError(_("Unknown reference id '%s' "
"for path %s.") % (ref, path))
return refnode.attrib["{%s}href" % OVF_NAMESPACES["ovf"]]
def _import_file(conn, input_file):
"""
Parse the OVF file and generate a virtinst.Guest object from it
"""
root = xml.etree.ElementTree.parse(input_file).getroot()
vsnode = _find(root, "./ovf:VirtualSystem")
vhnode = _find(vsnode, "./ovf:VirtualHardwareSection")
# General info
name = _text(vsnode.find("./ovf:Name", OVF_NAMESPACES))
desc = _text(vsnode.find("./ovf:AnnotationSection/ovf:Annotation",
OVF_NAMESPACES))
if not desc:
desc = _text(vsnode.find("./ovf:Description", OVF_NAMESPACES))
vhxpath = "./ovf:Item[rasd:ResourceType='%s']"
vcpus = _text(_find(vhnode,
(vhxpath % DEVICE_CPU) + "/rasd:VirtualQuantity"))
mem = _text(_find(vhnode,
(vhxpath % DEVICE_MEMORY) + "/rasd:VirtualQuantity"))
alloc_mem = _text(_find(vhnode,
(vhxpath % DEVICE_MEMORY) + "/rasd:AllocationUnits"))
# Sections that we handle
# NetworkSection is ignored, since I don't have an example of
# a valid section in the wild.
parsed_sections = ["References", "DiskSection", "NetworkSection",
"VirtualSystem"]
# Check for unhandled 'required' sections
for env_node in root.findall("./"):
if any([p for p in parsed_sections if p in env_node.tag]):
continue
logging.debug("Unhandled XML section '%s'",
env_node.tag)
if not _convert_bool_val(env_node.attrib.get("required")):
continue
raise Exception(_("OVF section '%s' is listed as "
"required, but parser doesn't know "
"how to handle it.") % env_node.name)
disk_buses = {}
for node in _findall(vhnode, vhxpath % DEVICE_IDE_BUS):
instance_id = _text(_find(node, "rasd:InstanceID"))
disk_buses[instance_id] = "ide"
for node in _findall(vhnode, vhxpath % DEVICE_SCSI_BUS):
instance_id = _text(_find(node, "rasd:InstanceID"))
disk_buses[instance_id] = "scsi"
ifaces = []
for node in _findall(vhnode, vhxpath % DEVICE_ETHERNET):
iface = virtinst.DeviceInterface(conn)
# Just ignore 'source' info for now and choose the default
net_model = _text(_find(node, "rasd:ResourceSubType"))
if net_model and not net_model.isdigit():
iface.model = net_model.lower()
iface.set_default_source()
ifaces.append(iface)
disks = []
for node in _findall(vhnode, vhxpath % DEVICE_DISK):
bus_id = _text(_find(node, "rasd:Parent"))
path = _text(_find(node, "rasd:HostResource"))
bus = disk_buses.get(bus_id, "ide")
fmt = "raw"
if path:
path = _lookup_disk_path(root, path)
fmt = "vmdk"
disk = virtinst.DeviceDisk(conn)
disk.path = path
disk.driver_type = fmt
disk.bus = bus
disk.device = "disk"
disks.append(disk)
# Generate the Guest
guest = virtinst.Guest(conn)
if not name:
name = os.path.basename(input_file)
guest.name = name.replace(" ", "_")
guest.description = desc or None
if vcpus:
guest.vcpus = int(vcpus)
if mem:
guest.memory = _convert_alloc_val(alloc_mem, mem) * 1024
for dev in ifaces + disks:
guest.add_device(dev)
return guest
class ovf_parser(parser_class):
"""
Support for OVF appliance configurations.
Whitepaper: https://www.vmware.com/pdf/ovf_whitepaper_specification.pdf
Spec: https://www.dmtf.org/standards/published_documents/DSP0243_1.0.0.pdf
"""
name = "ovf"
suffix = ".ovf"
@staticmethod
def identify_file(input_file):
"""
Return True if the given file is of this format.
"""
# Small heuristic to ensure we aren't attempting to identify
# a large .zip archive or similar
if os.path.getsize(input_file) > (1024 * 1024 * 2):
return
try:
root = xml.etree.ElementTree.parse(input_file).getroot()
return root.tag == ("{%s}Envelope" % OVF_NAMESPACES["ovf"])
except Exception:
logging.debug("Error parsing OVF XML", exc_info=True)
return False
@staticmethod
def export_libvirt(conn, input_file):
logging.debug("Importing OVF XML:\n%s", open(input_file).read())
return _import_file(conn, input_file)
|