Learn how to configure your Debian 12 VM with QEMU/KVM and Virt-Manager to shut down gracefully when you close the graphical console.
Table of Contents
Problem
If you use virtual machines (VMs) for testing, development, or other purposes, you may want to save some resources and energy by shutting them down when you are not using them. However, closing the graphical console of a VM does not always mean that the VM is also powered off. Depending on the configuration of your hypervisor and guest OS, the VM may continue to run in the background, consuming CPU, memory, and disk space.
In this article, you will learn how to make your Debian 12 VM with QEMU/KVM and Virt-Manager shut down gracefully when you close the graphical console. This way, you can avoid leaving unnecessary VMs running and free up some resources for other tasks.
Prerequisites
To follow this tutorial, you will need:
- A Debian 12 VM with QEMU/KVM as hypervisor and Virt-Manager as a frontend and SPICE for graphical output.
- SSH access to the VM or a graphical console opened from Virt-Manager.
- Root privileges or sudo access on the VM.
Step 1: Enable and Start the Serial-Getty Service on the VM
This service allows you to access the VM console using the virsh command on the host. By default, this service is not enabled on Debian 12, so you need to enable it manually.
To do this, log in to the VM using SSH or the graphical console and run the following commands:
sudo systemctl enable [email protected]
sudo systemctl start [email protected]
This will enable and start the serial-getty service on the ttyS0 device, which is the default serial port for QEMU/KVM VMs. You can verify that the service is running by using the following command:
sudo systemctl status [email protected]
You should see something like this:
● [email protected] - Serial Getty on ttyS0
Loaded: loaded (/lib/systemd/system/[email protected]; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-11-10 10:15:23 UTC; 2min 15s ago
Docs: man:agetty(8)
man:systemd-getty-generator(8)
http://0pointer.de/blog/projects/serial-console.html
Main PID: 456 (agetty)
Tasks: 1 (limit: 2286)
Memory: 1.1M
CPU: 66ms
CGroup: /system.slice/system-serial\x2dgetty.slice/[email protected]
└─456 /sbin/agetty -o '-p -- \\u' --keep-baud 115200,38400,9600 ttyS0 vt220
Nov 10 10:15:23 debian12 systemd[1]: Started Serial Getty on ttyS0.
Step 2: Access the VM Console Using the Virsh Command on the Host
The virsh command is a tool for managing VMs controlled by libvirt, which is a library and API for virtualization platforms. You can use the virsh command to perform various operations on your VMs, such as starting, stopping, rebooting, creating, deleting, and more.
To access the VM console using the virsh command, you need to know the name or ID of the VM. You can list all the VMs on the host by using the following command:
virsh list --all
You should see something like this:
Id Name State
-------------------------------
3 debian12 running
In this case, the name of the VM is debian12 and the ID is 3. You can use either of them to connect to the VM console using the following command:
virsh console debian12
or
virsh console 3
You should see something like this:
Connected to domain debian12
Escape character is ^]
Debian GNU/Linux 12 debian12 ttyS0
debian12 login:
You can now log in to the VM console using your username and password. To exit the console, press Ctrl + ].
Step 3: Configure the VM to Shut Down When the Console is Closed
You need to edit the VM configuration file using the virsh command on the host. The VM configuration file is an XML file that defines the properties and settings of the VM, such as the name, memory, CPU, disks, network, and more.
To edit the VM configuration file, use the following command:
virsh edit debian12
or
virsh edit 3
This will open the VM configuration file in your default editor, which is usually vi or nano. You can change the editor by setting the EDITOR environment variable before running the virsh command.
In the VM configuration file, look for the <on_poweroff> element, which defines the action to take when the VM is powered off. By default, the value of this element is destroy, which means that the VM is terminated and its resources are freed. You need to change this value to shutdown, which means that the VM is gracefully shut down using the ACPI power button signal. The ACPI power button signal is a standard way of requesting a system to shut down by sending a signal to the operating system.
To change the value of the <on_poweroff> element, locate the following line:
<on_poweroff>destroy</on_poweroff>
and change it to:
<on_poweroff>shutdown</on_poweroff>
Save and close the file. The virsh command will validate the changes and apply them to the VM. You can verify that the changes are applied by using the following command:
virsh dumpxml debian12 | grep on_poweroff
or
virsh dumpxml 3 | grep on_poweroff
You should see something like this:
<on_poweroff>shutdown</on_poweroff>
Step 4: Test the VM Shut Down When the Console is Closed
1. Access the VM console using the virsh command on the host, as explained in step 2.
2. Log in to the VM console using your username and password.
3. Run the following command to monitor the system log:
sudo journalctl -f
4. Exit the console by pressing Ctrl + ].
5. Check the output of the journalctl command on the VM console. You should see something like this:
Nov 10 10:45:15 debian12 systemd-logind[433]: Power key pressed.
Nov 10 10:45:15 debian12 systemd-logind[433]: Powering Off...
Nov 10 10:45:15 debian12 systemd-logind[433]: System is powering down.
Nov 10 10:45:15 debian12 systemd[1]: Received SIGRTMIN+24 from PID 433 (systemd-logind).
Nov 10 10:45:15 debian12 systemd[1]: Reached target Power-Off.
Nov 10 10:45:15 debian12 systemd[1]: Shutting down.
Nov 10 10:45:15 debian12 systemd-shutdown[1]: Syncing filesystems and block devices.
Nov 10 10:45:15 debian12 systemd-shutdown[1]: Sending SIGTERM to remaining processes...
Nov 10 10:45:15 debian12 systemd-journald[433]: Journal stopped
This indicates that the VM received the ACPI power button signal and shut down gracefully.
6. Check the status of the VM on the host by using the following command:
virsh list --all
You should see something like this:
Id Name State
-------------------------------
- debian12 shut off
This indicates that the VM is powered off and its resources are freed.
Conclusion
In this article, you learned how to configure your Debian 12 VM with QEMU/KVM and Virt-Manager to shut down gracefully when you close the graphical console. This can help you save some resources and energy by avoiding leaving unnecessary VMs running. You also learned how to use the virsh command to manage your VMs from the command line.