-
Notifications
You must be signed in to change notification settings - Fork 268
libvirt_vm: undefine stale domain before virt-install --import #4367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code for undefining a stale domain is quite large and is placed directly within the The long comment explaining the logic can then be moved into the docstring of this new method, making the 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: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
try...except process.CmdErrorblock around this call is ineffective becausevirsh.dumpxmlis called withoutignore_status=False. The underlyingvirsh.commandfunction defaults toignore_status=True, which suppressesprocess.CmdErroron failure, making theexceptblock unreachable. To fix this,ignore_status=Falseshould be passed tovirsh.dumpxml.