Skip to content

Improve VM lifecycle management for iface_update#6810

Open
yanglei-rh wants to merge 1 commit intoautotest:masterfrom
yanglei-rh:LIBVIRTAT-22324
Open

Improve VM lifecycle management for iface_update#6810
yanglei-rh wants to merge 1 commit intoautotest:masterfrom
yanglei-rh:LIBVIRTAT-22324

Conversation

@yanglei-rh
Copy link
Contributor

@yanglei-rh yanglei-rh commented Feb 24, 2026

1.Add some parameters to allow the framework provide guest xml
2.Restart libvirt service as a woraround for using virt-install bootup guest and create an new serial session for guest
3.Add "only Linux" to limit guest type

ID: LIBVIRTAT-22324
Signed-off-by: Lei Yang leiyang@redhat.com

Summary by CodeRabbit

  • Bug Fixes

    • Improved serial console reliability by cleaning and recreating consoles after libvirt restarts and ensuring interface checks reflect the VM's current state; avoids redundant restarts.
  • Tests

    • Enhanced VM lifecycle in tests: enable libvirt-based VM creation/termination, optional skip of reboots, and OVMF NVRAM handling for more consistent runs.

@coderabbitai
Copy link

coderabbitai bot commented Feb 24, 2026

Walkthrough

Adds libvirt-related configuration keys to the iface_update test config to enable libvirt VM creation, skip reboots, and force libvirt VM kill with OVMF NVRAM options. In test code, introduces a libvirtd_restarted flag, restarts libvirtd after MTU validation and as a workaround for non-cold updates when the VM is alive, and ensures serial console cleanup and recreation after such restarts before waiting for login and verifying interface state.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main focus of the changes: improving VM lifecycle management for the iface_update test through libvirtd restart handling and serial console cleanup.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
libvirt/tests/src/virtual_network/iface_update.py (2)

254-277: Avoid redundant libvirtd restarts when MTU already triggers one.
With iface_mtu set, you restart once in the MTU block and again here; a simple flag keeps the workaround while preventing an extra bounce.

♻️ Suggested diff
-        if iface_mtu:
+        libvirtd_restarted = False
+        if iface_mtu:
             # Do check for mtu size after start vm
             target_dev = libvirt.get_interface_details(vm_name)[0]['interface']
             cmd = "ip link show %s | grep 'mtu %s'" % (target_dev, eval(iface_mtu)['size'])
@@
             check_mtu()
             utils_libvirtd.libvirtd_restart()
+            libvirtd_restarted = True
             check_mtu()
 
         # This is a workaround to let virt-install create vm can be management by domif-setlink
         # and cleanup exists serial session, since restart libvirtd will render the original invalid
-        if not cold_update and vm.is_alive():
+        if not cold_update and vm.is_alive() and not libvirtd_restarted:
             logging.info("Restart libvirtd server")
             libvirtd = utils_libvirtd.Libvirtd()
             libvirtd.restart()
+            libvirtd_restarted = True
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@libvirt/tests/src/virtual_network/iface_update.py` around lines 254 - 277,
The code may restart libvirtd twice when iface_mtu is set (once via
utils_libvirtd.libvirtd_restart() in the MTU block and again later via
libvirtd.restart()); add a simple boolean flag (e.g., mturestarted) set to True
after calling utils_libvirtd.libvirtd_restart() inside the iface_mtu branch,
then in the later workaround before calling libvirtd.restart() check both not
cold_update and vm.is_alive() and that mturestarted is False to skip the
redundant restart; update references in this function to use the new flag around
the utils_libvirtd.libvirtd_restart() and libvirtd.restart() calls so the
restart only happens once.

323-329: Consider recreating the serial console only when libvirtd was actually restarted.
This avoids unnecessary console resets when no restart occurred.

♻️ Suggested diff
-                    # Cleanup and recreate serial console since libvirtd was restarted
-                    if vm.serial_console:
-                        vm.cleanup_serial_console()
-
-                    # Recreate serial console after libvirtd restart
-                    vm.create_serial_console()
+                    # Cleanup and recreate serial console since libvirtd was restarted
+                    if libvirtd_restarted:
+                        if vm.serial_console:
+                            vm.cleanup_serial_console()
+                        # Recreate serial console after libvirtd restart
+                        vm.create_serial_console()
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@libvirt/tests/src/virtual_network/iface_update.py` around lines 323 - 329,
The code always cleans up and recreates the serial console regardless of whether
libvirtd was restarted; change it to only run vm.cleanup_serial_console() and
vm.create_serial_console() when a libvirtd-restart condition is true (e.g.,
check a restart flag or call a detection helper such as
vm.was_libvirtd_restarted() or a local libvirtd_restarted variable). Wrap the
existing vm.serial_console check and the recreate call inside a conditional that
verifies libvirtd was actually restarted so the console is only reset after an
actual restart.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@libvirt/tests/src/virtual_network/iface_update.py`:
- Around line 254-277: The code may restart libvirtd twice when iface_mtu is set
(once via utils_libvirtd.libvirtd_restart() in the MTU block and again later via
libvirtd.restart()); add a simple boolean flag (e.g., mturestarted) set to True
after calling utils_libvirtd.libvirtd_restart() inside the iface_mtu branch,
then in the later workaround before calling libvirtd.restart() check both not
cold_update and vm.is_alive() and that mturestarted is False to skip the
redundant restart; update references in this function to use the new flag around
the utils_libvirtd.libvirtd_restart() and libvirtd.restart() calls so the
restart only happens once.
- Around line 323-329: The code always cleans up and recreates the serial
console regardless of whether libvirtd was restarted; change it to only run
vm.cleanup_serial_console() and vm.create_serial_console() when a
libvirtd-restart condition is true (e.g., check a restart flag or call a
detection helper such as vm.was_libvirtd_restarted() or a local
libvirtd_restarted variable). Wrap the existing vm.serial_console check and the
recreate call inside a conditional that verifies libvirtd was actually restarted
so the console is only reset after an actual restart.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 98ed047 and c8724e9.

📒 Files selected for processing (2)
  • libvirt/tests/cfg/virtual_network/iface_update.cfg
  • libvirt/tests/src/virtual_network/iface_update.py

@yanglei-rh
Copy link
Contributor Author

Test pass.

 (1/2) Host_RHEL.m9.u8.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.RHEL.9.8.0.x86_64.io-github-autotest-libvirt.virtual_network.iface_update.positive_test.update_link.q35: PASS (78.52 s)
 (2/2) Host_RHEL.m9.u8.ovmf.qcow2.virtio_scsi.up.virtio_net.Guest.RHEL.9.8.0.x86_64.io-github-autotest-libvirt.virtual_network.iface_update.positive_test.update_boot_order.q35: PASS (11.57 s)

1.Add some parameters to allow the framework provide guest xml
2.Restart libvirt service as a woraround for using virt-install bootup
guest and create an new serial session for guest
3.Add "only Linux" to limit guest type

Signed-off-by: Lei Yang <leiyang@redhat.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@libvirt/tests/src/virtual_network/iface_update.py`:
- Around line 273-279: The libvirtd restart is currently unconditionally run for
live updates; change the guard in the block that checks not cold_update,
vm.is_alive() and not libvirtd_restarted to also require a new explicit flag
(e.g., create_vm_libvirt) so the restart only happens when the virt-install flow
requested it; update the function or test harness to accept/pass
create_vm_libvirt, and wrap the restart logic around that flag (references:
variables cold_update, vm.is_alive(), libvirtd_restarted and calls
utils_libvirtd.Libvirtd() / libvirtd.restart()) so other live-update cases won’t
trigger a restart unless create_vm_libvirt is true.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5df6e35 and 501f553.

📒 Files selected for processing (2)
  • libvirt/tests/cfg/virtual_network/iface_update.cfg
  • libvirt/tests/src/virtual_network/iface_update.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • libvirt/tests/cfg/virtual_network/iface_update.cfg

Copy link
Contributor

@nickzhq nickzhq left a comment

Choose a reason for hiding this comment

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

LGTM.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants