You are not logged in.

#1 2024-05-01 13:23:26

solskog
Member
Registered: 2020-09-05
Posts: 434

[Solved] Qemu as systemd service

Run this command from console/script works

# cat vm.sh
#!/bin/bash
/usr/bin/qemu-system-x86_64 -runas kvm -machine type=q35,accel=kvm -m 200M -drive file=vm.qcow2 -smp cpus=2 -cpu host -device vfio-pci,host=05:00.0,rombar=0

Same command/script runs as systemd service failed.

# cat vm.service
[Service]
Type=forking
TimeoutStartSec=600
User=root
Group=root
ExecStart=-vm.sh
[Install]
WantedBy=multi-user.target

Systemd service failed shows by Journalctl

qemu: VFIO_MAP_DMA failed: Cannot allocate memory
qemu: vfio_dma_map(0x5e587a3aa050, 0xc0000000, 0x1000000, 0x78e90d800000) = -12 (Cannot allocate memory)
qemu: hardware error: vfio: DMA mapping failed, unable to continue

But, run these commands from console/script AND systemd service all works

# /usr/bin/qemu-system-x86_64 -machine type=q35,accel=kvm -m 200M -drive file=vm.qcow2 -smp cpus=2 -cpu host -device vfio-pci,host=05:00.0,rombar=0
# /usr/bin/qemu-system-x86_64 -runas kvm -machine type=q35,accel=kvm -m 200M -drive file=vm.qcow2 -smp cpus=2 -cpu host

It seems to me that the combination of "-runas NON_ROOT_USER" and "-device Passtrhough" are not possible.
If you need device passthrough, run qemu as root user or if you need non-root user to run qemu, don't passthrough any device.
Any advice?
By using cronie with corret settings it works but systemd still not working.

# crontab -l
@reboot /usr/local/bin/vm.sh
# cat /etc/security/limits.conf
@kvm     soft    memlock     12000000
@kvm     hard    memlock     12000000
# gpasswd -a root kvm

The problem solved by increse LimitMEMLOCK= on systemd service file.

Last edited by solskog (2024-05-03 10:45:15)

Offline

Board footer

Powered by FluxBB