def main(): dev_type = os.environ.get('hostdev_scsi') if dev_type is None: # No translation requested for the given VM return _check_supported(dev_type) domxml = hooking.read_domxml() dom = _find_element_by_tag_name(domxml, 'domain') devs = _find_element_by_tag_name(dom, 'devices') index = _BASE_INDEX for dev in _get_child_nodes(devs): if not _is_scsi_dev(dev): continue dev_info = _parse_devs_xml(dev) new_dev = _make_disk_device(domxml, dev_info, dev_type, index) devs.replaceChild(new_dev, dev) index += 1 hooking.write_domxml(domxml)
def test_write_domxml(self, monkeypatch): with temporaryPath() as path: monkeypatch.setenv('_hook_domxml', path) hooking.write_domxml(minidom.parseString(self._EXPECTED_XML)) with io.open(path, 'r') as src: found_xml = src.read() assert xml_equal(found_xml, self._EXPECTED_XML)
def test_roundtrip_domxml(self, monkeypatch): with temporaryPath() as path: os.environ['_hook_domxml'] = path monkeypatch.setenv('_hook_domxml', path) hooking.write_domxml(minidom.parseString(self._EXPECTED_XML)) domxml = hooking.read_domxml() assert xml_equal(domxml.toprettyxml(), self._EXPECTED_XML)
def main(): dev = os.environ.get('boot_hostdev') if dev: domxml = hooking.read_domxml() # Free boot order 1, move all existing ones one up for boot in domxml.getElementsByTagName('boot'): order = int(boot.getAttribute('order')) boot.setAttribute('order', str(order + 1)) # Find specified hostdev, set order = 1 usr_dev = get_user_device_attrs(dev) for hostdev in domxml.getElementsByTagName('hostdev'): xml_dev = get_xml_device_attrs(hostdev) if xml_dev == usr_dev: boot = domxml.createElement('boot') boot.setAttribute('order', '1') hostdev.appendChild(boot) hooking.write_domxml(domxml) break
def main(): dev = os.environ.get('boot_hostdev') if dev: domxml = hooking.read_domxml() # Free boot order 1, move all existing ones one up for boot in domxml.getElementsByTagName('boot'): order = int(boot.getAttribute('order')) boot.setAttribute('order', str(order + 1)) # Find specified hostdev, set order = 1 usr_dev = get_user_device_attrs(dev) for hostdev in domxml.getElementsByTagName('hostdev'): xml_dev = get_xml_device_attrs(hostdev) if xml_dev == usr_dev: boot = domxml.createElement('boot') boot.setAttribute('order', '1') hostdev.appendChild(boot) hooking.write_domxml(domxml) return raise RuntimeError("Could not find device %s" % dev)
def main(): log_console = os.environ.get('log_console') hooking.log(f'log_console: "{log_console}"') if log_console == 'off': return log_console_vm_regexp = os.environ.get('log_console_vm_regexp') hooking.log(f'log_console_vm_regexp: "{log_console_vm_regexp}"') domxml = hooking.read_domxml() vm_name = domxml.getElementsByTagName('name')[0].firstChild.data hooking.log(f'vm_name: {vm_name}') if (log_console != 'on' and (not log_console_vm_regexp or not re.match(log_console_vm_regexp, vm_name))): return devices = domxml.getElementsByTagName('devices') serial_devices = [ child for child in devices[0].childNodes if child.localName == 'serial' ] hooking.log(f'serial_devices: {serial_devices}') if not serial_devices: return serial0 = serial_devices[0] hooking.log(f'before: [{serial0.toprettyxml()}]') log = domxml.createElement('log') log.setAttribute('file', f'/var/log/libvirt/qemu/{vm_name}-console.log') log.setAttribute('append', 'on') serial0.appendChild(log) hooking.log(f'after: [{serial0.toprettyxml()}]') hooking.write_domxml(domxml)