diff --git a/virttest/libvirt_vm.py b/virttest/libvirt_vm.py index f2eaa79811..eec1c2b68d 100644 --- a/virttest/libvirt_vm.py +++ b/virttest/libvirt_vm.py @@ -2116,6 +2116,38 @@ def create( """ error_context.context("creating '%s'" % self.name) self.destroy(free_mac_addresses=False) + # A persistent domain with our name -- left by a previous test + # (env_cleanup=no) or pre-installed during CI bootstrap -- makes the + # "virt-install --import" below fail under its default + # "--check path_in_use=on": + # Disk ... is already in use by other guests [''] + # destroy() only kills the qemu process, it does not drop the + # persistent definition, so the disk/name stays locked. Undefine it + # here. Detect nvram from the domain XML so this also works on + # UEFI-only arches (e.g. aarch64), where "virsh undefine" fails + # without --nvram. Only the definition is removed; the qcow2 image is + # left intact for --import to reuse. + if virsh.domain_exists(self.name, uri=self.connect_uri): + undefine_opts = "--managed-save" + try: + domxml = virsh.dumpxml( + self.name, extra="--inactive", uri=self.connect_uri + ).stdout_text + except process.CmdError as detail: + domxml = "" + LOG.debug( + "Could not dump XML of %s before undefine: %s", + self.name, + detail, + ) + if "