QEMU and KVM virtual machines often run critical workloads where tracking activity and diagnosing issues is crucial. Enabling logging allows you to capture detailed information about VM operations, errors, and performance, making troubleshooting and auditing more effective. Configuring logging for QEMU/KVM VMs can be accomplished through command-line options or by adjusting libvirt configurations, depending on your virtualization setup.

Enable QEMU/KVM Logging via Command-Line Options

Step 1: Identify the QEMU process or command used to launch your virtual machine. Logging is typically set when starting the VM, so you may need to modify your launch scripts or service files.

Step 2: Add the -D /path/to/qemu.log -d guest_errors options to your QEMU command. The -D flag specifies the log file location, and -d sets the type of debugging output you want (e.g., guest_errors, cpu, int, etc.). For example:

qemu-system-x86_64 -hda /var/lib/libvirt/images/vm.img -m 2048 -D /var/log/qemu-vm.log -d guest_errors

This command will create a log file at /var/log/qemu-vm.log containing guest error messages.

Step 3: Restart or launch your VM using the modified command. Check your specified log file to confirm that entries are being recorded as expected.

Step 4: Adjust the -d option to include additional logging categories if needed. For a full list of debug options, run:

qemu-system-x86_64 -d help

This will display all available logging categories, allowing you to tailor the log output to your troubleshooting needs.


Enable Logging in Libvirt-Managed VMs

Step 1: Access your libvirt configuration files, which are typically located in /etc/libvirt/. For system-wide logging, edit /etc/libvirt/qemu.conf.

Step 2: Locate the logging section in qemu.conf and set the following options to define log file location and verbosity:


# Enable logging for all QEMU VMs managed by libvirt
log_outputs="1:file:/var/log/libvirt/qemu/VMNAME.log"
log_filters="1:qemu"
    

Replace VMNAME with your specific VM's name. The log_outputs option sets the log destination, and log_filters adjusts what is logged. You may need to create the log directory and set appropriate permissions.

Step 3: Save your changes and restart the libvirt service to apply the new logging configuration:

sudo systemctl restart libvirtd

Step 4: Start your VM and inspect the specified log file to ensure logging is working. Review the log contents for expected entries and adjust filters as needed for more or less detail.


Capture VM Serial Console Output

Step 1: To log the serial console output of a VM, add the following argument to your QEMU command:

-serial file:/var/log/qemu-serial.log

This will direct all serial console output to /var/log/qemu-serial.log, which is helpful for capturing kernel messages and early boot issues.

Step 2: If using libvirt, you can add a serial device to your VM’s XML configuration. After editing the XML, reload the configuration and restart the VM for changes to take effect.


Enabling logging on QEMU/KVM VMs improves your ability to track, diagnose, and resolve issues quickly. Review your log files regularly and adjust settings as needed for optimal visibility and performance.