def test_restore_disk_in_domain(self, get_uncompressed_complete_backup, build_stopped_mock_domain, tmpdir): backup = get_uncompressed_complete_backup domain = build_stopped_mock_domain src_img = backup.get_complete_path_of(backup.disks["vda"]) domain.set_storage_basedir(str(tmpdir)) dst_img = get_domain_disks_of(domain.XMLDesc(), "vda")["vda"]["src"] backup.restore_and_replace_disk_of("vda", domain, "vda") assert filecmp.cmp(src_img, dst_img) assert (get_domain_disks_of( domain.XMLDesc())["vda"]["type"] == get_domain_disks_of( backup.dom_xml)["vda"]["type"])
def gen_libvirt_snapshot_xml(self): """ Generate a xml defining the snapshot """ root_el = lxml.etree.Element("domainsnapshot") xml_tree = root_el.getroottree() descr_el = lxml.etree.Element("description") root_el.append(descr_el) descr_el.text = "Pre-backup external snapshot" disks_el = lxml.etree.Element("disks") root_el.append(disks_el) all_domain_disks = get_domain_disks_of( defusedxml.lxml.fromstring(self.dom.XMLDesc())) for d in sorted(all_domain_disks.keys()): disk_el = lxml.etree.Element("disk") disk_el.attrib["name"] = d # Skipped disks need to have an entry, with a snapshot value # explicitly set to "no", otherwise libvirt will be created a # snapshot for them. disk_el.attrib["snapshot"] = ("external" if d in self.disks else "no") disks_el.append(disk_el) return lxml.etree.tostring(xml_tree, pretty_print=True).decode()
def restore_to(self, target): if not os.path.exists(target): os.makedirs(target) # TODO: store the original images names in the definition file disks_src = get_domain_disks_of(self.dom_xml) for d in self.disks: original_img_name = os.path.basename(disks_src[d]["src"]) self.restore_disk_to(d, os.path.join(target, original_img_name)) xml_path = "{}.xml".format(os.path.join(target, self.dom_name)) with open(xml_path, "w") as xml_file: xml_file.write(self.dom_xml)
def restore_and_replace_disk_of(self, disk, domain, disk_to_replace): """ Restore a disk by replacing an old disks :param disk: disk name :param domain: domain to target :param disk_to_replace: which disk of `domain` to replace """ self._ensure_domain_not_running(domain) disk_target_path = get_domain_disks_of(domain.XMLDesc(), disk_to_replace)[disk]["src"] # TODO: restore disk with a correct extension, and not by keeping the # old disk one result = self.restore_disk_to(disk, disk_target_path) self._copy_disk_driver_with_domain(disk, domain, disk_to_replace) return result
def _get_self_domain_disks(self, *filter_dev): dom_xml = self._parse_dom_xml() return get_domain_disks_of(dom_xml, *filter_dev)
def test_get_domain_disks_of_disk_not_found(build_mock_domain): domain = build_mock_domain with pytest.raises(DiskNotFoundError): get_domain_disks_of(domain.XMLDesc(), "vda", "vdc")
def test_get_domain_disks_of(build_mock_domain): domain = build_mock_domain vda = get_domain_disks_of(domain.XMLDesc(), "vda", "vdb") assert "vda" in vda