Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions virttest/libvirt_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ['<name>']
# 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
Comment on lines +2133 to +2135

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The try...except process.CmdError block around this call is ineffective because virsh.dumpxml is called without ignore_status=False. The underlying virsh.command function defaults to ignore_status=True, which suppresses process.CmdError on failure, making the except block unreachable. To fix this, ignore_status=False should be passed to virsh.dumpxml.

                domxml = virsh.dumpxml(self.name, extra="--inactive", uri=self.connect_uri, ignore_status=False).stdout_text

except process.CmdError as detail:
domxml = ""
LOG.debug(
"Could not dump XML of %s before undefine: %s",
self.name,
detail,
)
if "<nvram" in domxml:
undefine_opts += " --nvram"
virsh.undefine(
self.name,
options=undefine_opts,
uri=self.connect_uri,
ignore_status=True,
)
Comment on lines +2119 to +2150

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code for undefining a stale domain is quite large and is placed directly within the create method. To improve readability and maintainability, consider extracting this logic into a new private helper method, such as _undefine_stale_domain(self).

The long comment explaining the logic can then be moved into the docstring of this new method, making the create method cleaner and the purpose of the extracted code more explicit.

For example:

    def _undefine_stale_domain(self):
        """
        Undefine a stale persistent domain to prevent virt-install failures.

        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 ['<name>']
        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 not virsh.domain_exists(self.name, uri=self.connect_uri):
            return

        undefine_opts = "--managed-save"
        try:
            domxml = virsh.dumpxml(
                self.name, extra="--inactive", uri=self.connect_uri, ignore_status=False
            ).stdout_text
        except process.CmdError as detail:
            domxml = ""
            LOG.debug(
                "Could not dump XML of %s before undefine: %s",
                self.name,
                detail,
            )
        if "<nvram" in domxml:
            undefine_opts += " --nvram"
        virsh.undefine(
            self.name,
            options=undefine_opts,
            uri=self.connect_uri,
            ignore_status=True,
        )

    @error_context.context_aware
    def create(self, ...):
        # ...
        self.destroy(free_mac_addresses=False)
        self._undefine_stale_domain()
        # ...

if name is not None:
self.name = name
if params is not None:
Expand Down