systemd: intercepting poweroff, shutdown, reboot and alert us we still have KVM QEMU guest(s) running

3 min read 27-10-2024
systemd: intercepting poweroff, shutdown, reboot and alert us we still have KVM QEMU guest(s) running

In the world of system administration, managing virtual machines (VMs) effectively is crucial, especially when it comes to ensuring that no VMs are inadvertently shut down while you're attempting to power off or reboot your system. If you are using KVM/QEMU for virtualization, it's important to intercept power management commands (like poweroff, shutdown, and reboot) to check for running guests before proceeding.

The initial problem might look something like this:

systemctl poweroff

But this command can lead to potential issues if KVM QEMU guests are running, which might cause data loss or corruption.

Understanding the Problem

When you execute commands like systemctl poweroff, systemctl shutdown, or systemctl reboot, the system does not check if there are any active QEMU guest sessions. This lack of awareness could lead to scenarios where VMs are abruptly terminated, resulting in loss of unsaved data or even filesystem corruption within the VMs.

To resolve this, we can use systemd's capabilities to intercept these commands and alert us if there are any running KVM QEMU guests.

Implementing the Solution

You can create a custom systemd service that checks for running QEMU guests whenever a power management command is issued. Here’s a step-by-step guide to setting it up.

Step 1: Create the Check Script

First, create a script that checks for running KVM QEMU guests. Save this script as /usr/local/bin/check-kvm-guest.sh:

#!/bin/bash

# Check for running KVM/QEMU guests
if pgrep -a qemu-system > /dev/null; then
    echo "Alert: There are still KVM/QEMU guests running!"
    exit 1 # Indicates that guests are running
else
    exit 0 # Indicates that no guests are running
fi

Ensure that the script is executable:

chmod +x /usr/local/bin/check-kvm-guest.sh

Step 2: Create the systemd Service

Next, create a systemd service file to use this script. Create a file named /etc/systemd/system/check-kvm-guest.service:

[Unit]
Description=Check for running KVM/QEMU guests
DefaultDependencies=no
Before=shutdown.target reboot.target poweroff.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/check-kvm-guest.sh

[Install]
WantedBy=halt.target reboot.target poweroff.target

Step 3: Enable the Service

After creating the service file, enable it with the following command:

systemctl enable check-kvm-guest.service

How It Works

When you attempt to execute a power management command, systemd will trigger the check-kvm-guest.service. This service will run your custom script to check for any active KVM/QEMU processes. If any guests are found running, the script will alert you, preventing the shutdown or reboot from occurring, thus safeguarding your virtual machines.

Additional Considerations

  1. Logging: You may want to expand the script to log the alerts to a file for auditing purposes.
  2. Notifications: Consider integrating a notification system (like sending an email or a desktop notification) to alert you when guests are still running.
  3. Graceful Shutdown: Enhance the solution by adding logic that attempts to gracefully shut down the VMs before allowing the power management command to proceed.

Conclusion

Intercepting power management commands in systemd is a powerful approach to ensuring the integrity of your KVM QEMU virtual machines. By following the steps outlined above, you can create a safety net that protects your virtual environments from abrupt shutdowns. This not only prevents potential data loss but also promotes better system management practices.

Useful Resources

By implementing this solution, you can enhance your control over system power management and avoid potential pitfalls associated with running virtual machines.