You are not logged in.

#1 2023-01-20 14:12:13

bachtiar
Member
Registered: 2005-02-08
Posts: 65

Autostart libvirt guests via systemd for a regular user

I have a QEMU guest managed through libvirt as a regular (non-root) user. To start/stop it manually, I run "virsh start/stop" as that user, or alternatively, I start them via virt-manager (via "qemu:///session" URI).

Now I want to autostart/autostop this guest as a systemd service at system boot/shutdown. I have read through libvirt wiki page as well as man page for libvirt-guests, but it looks to me as if it only supports system sessions (i.e. guests that run as root).

Is it possible to use this service to also start/stop guests running under regular users' accounts? If yes, how? And if not, what would be the analogous way of doing it?

Last edited by bachtiar (2023-01-20 14:16:10)

Offline

#2 2023-01-20 15:39:30

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: Autostart libvirt guests via systemd for a regular user

Create a libvirt-guests user service...
https://wiki.archlinux.org/title/Systemd/User


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2023-02-05 17:28:44

bachtiar
Member
Registered: 2005-02-08
Posts: 65

Re: Autostart libvirt guests via systemd for a regular user

But what should the user service unit call?

Offline

#4 2024-04-13 10:28:33

Salkay
Member
Registered: 2014-05-22
Posts: 619

Re: Autostart libvirt guests via systemd for a regular user

bachtiar wrote:

But what should the user service unit call?

Did you ever work out how to do this?

I have a specific user who runs the VMs, so I ran

$ sudo /usr/bin/runuser -u hass_user -- virsh autostart hass
Domain 'hass' marked as autostarted

$ sudo /usr/bin/runuser -u hass_user -- virsh dominfo hass
...
Autostart:      enable
...

expecting it to autostart, but it doesn't. I'm not entirely sure how to do it the systemd way, especially since the user who runs the VMs never actually logs in, so presumably has no systemd jobs running. I presume that the root systemd job is not relevant here.

Another alternative might be to put in /etc/rc.local

sudo /usr/bin/runuser -u hass_user -- virsh start foobar

EDIT:
Okay, so /etc/rc.local is deprecated in Arch. Instead, I tried putting the following in /etc/systemd/system/start_hass.service

[Unit]
Description=Start Home Assistant VM

[Service]
Type=oneshot
ExecStart=/usr/bin/runuser -u hass -- virsh start hass
TimeoutSec=0
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

then enabling the service.

On reboot, this seems to start the VM. I can connect to the service, and I can see (e.g. in htop) that the correct user is running it. However, the systemd job itself fails, and journalctl says

Apr 14 15:09:09 qi runuser[688]: error: Domain is already active

Also, if I start it like this, virsh doesn't seem to know about it.

$ sudo /usr/bin/runuser -u hass -- virsh list --all
 Id   Name   State
-----------------------
 -    hass   shut off

This doesn't seem to work fully. I'm a bit loathe to start it up this way, since this might cause other issues downstream. Surely there has to be a better way to do this. It shouldn't be uncommon, but my searching hasn't brought up anything conclusive.

Possibly we could create a --user version of /usr/lib/systemd/system/libvirtd.service, but it's full of so many dependencies that this feels like a nightmare (e.g. libvirtd.socket, libvirtd-ro.socket, libvirtd-admin.socket, virtlogd.socket, virtlockd.socket, and a bunch of system services).

Last edited by Salkay (2024-04-14 06:30:09)

Offline

Board footer

Powered by FluxBB