You are not logged in.

#1 2015-12-18 01:55:58

Nohack
Member
Registered: 2014-10-14
Posts: 8

[SOLVED] Run qemu without root for vga passthrough?

I'm having trouble running qemu without sudo. I'm getting this error message when running without sudo:

qemu-system-x86_64: -device vfio-pci,host=01:00.0: vfio: error opening /dev/vfio/1: Permission denied
qemu-system-x86_64: -device vfio-pci,host=01:00.0: vfio: failed to get group 1
qemu-system-x86_64: -device vfio-pci,host=01:00.0: Device initialization failed

I've tried to google, found various solutions, but nothing seems to work. I'm not using libvirt, but running qemu directly from the command line.
For the record, vga passthrough works when running as root, the only issue is that sound doesn't work because of pulseadio, the daemon only runs per user, not for root.

Here's the script I use to launch qemu:

#!/bin/bash
# -soundhw hda \
export QEMU_AUDIO_DRV="pa"
qemu-system-x86_64 \
  -enable-kvm \
  -cpu host,kvm=off,check \
  -smp sockets=1,cores=4,threads=2 \
  -m 6144 \
  -net nic \
  -net bridge,br=br0 \
  -vga none \
  -device vfio-pci,host=01:00.0 \
  -device vfio-pci,host=01:00.1 \
  -drive if=pflash,format=raw,readonly,file=/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd \
  -drive if=pflash,format=raw,file=/tmp/my_vars.fd \
  ./disk.qcow2

Should I maybe try to use libvirt? The arch wiki is really lacking information on how to use it, and I can't find any good tutorials, I don't really know what benefits I get from using it.

Any help is appreciated!

Last edited by Nohack (2015-12-19 18:48:33)

Offline

#2 2015-12-18 11:28:41

Lone_Wolf
Forum Moderator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 11,971

Re: [SOLVED] Run qemu without root for vga passthrough?

Check
https://wiki.archlinux.org/index.php/PC … to_devices
https://wiki.archlinux.org/index.php/PC … ermissions

Lower on that page you'll find an example that doesn't use libvirtd.

Last edited by Lone_Wolf (2015-12-18 11:31:21)


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.


(A works at time B)  && (time C > time B ) ≠  (A works at time C)

Offline

#3 2015-12-18 14:54:58

Nohack
Member
Registered: 2014-10-14
Posts: 8

Re: [SOLVED] Run qemu without root for vga passthrough?

Lone_Wolf wrote:

Check
https://wiki.archlinux.org/index.php/PC … to_devices
https://wiki.archlinux.org/index.php/PC … ermissions

Lower on that page you'll find an example that doesn't use libvirtd.

Thanks, but I've already tried all of it. I don't really understand the udev rule, on the wiki it says:

This should allow you to set qemu user and group to something a little safer than root:root in /etc/libvirtd/qemu.conf

So I don't really know if this influences anything but libvirt, which I'm not using? I'm also not sure what "YOUR_VFIO_GROUPS" is? Is it a group I have to create, or is it the "users" groups? After this the article just tells me to put root:root in the "/etc/libvirt/qemu.conf" and specifies that there may be a safer way of doing this, I thought that the udev rule should make this safer??

At this point, the wiki article gets really confusing. Don't get me wrong, it was super easy to follow on how to isolate the graphics card etc. for that I think the article is great, but everything else is really confusing.

As you point out, there is two examples using the command line, there is one that doesn't use sudo, which gives me the error message I posted earlier, and a second example that uses sudo, but gives me pulseadio errors because of the "-soundhw hda" option, and the fact that pulseaudio is only available for me and not root.

So I really don't know what to do. I might try to setup the virtual machine using virt-manager as described on this blog "http://vfio.blogspot.no/", but it seems unnecessary because I've got everything working already, everything except sound..

Offline

#4 2015-12-18 15:58:12

Lone_Wolf
Forum Moderator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 11,971

Re: [SOLVED] Run qemu without root for vga passthrough?

I agree the "YOUR_VFIO_GROUPS" is unclear and i don't understand it also.
let's forget about the udev rule for now and focus on the other option.

I do get the impresssion that even if you don't run libvirtd or virt-manager  , you do need to to set up libvirt permisssions.

please post ls -l /dev/vfio and your /etc/libvirt/qemu.conf


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.


(A works at time B)  && (time C > time B ) ≠  (A works at time C)

Offline

#5 2015-12-19 03:44:44

Nohack
Member
Registered: 2014-10-14
Posts: 8

Re: [SOLVED] Run qemu without root for vga passthrough?

Lone_Wolf wrote:

I agree the "YOUR_VFIO_GROUPS" is unclear and i don't understand it also.
let's forget about the udev rule for now and focus on the other option.

I do get the impresssion that even if you don't run libvirtd or virt-manager  , you do need to to set up libvirt permisssions.

please post ls -l /dev/vfio and your /etc/libvirt/qemu.conf

I actually managed to get sound through steam inhome streaming by connecting an audio capable monitor with hdmi. So everything I wanted to do is now working, but I'm not really satisfied with running qemu as root, so I still want to get this fixed.
Anyways, here is the output of /dev/vfio:

total 0
crw-rw-rw- 1 root root 251,   0 Dec 18 01:11 1
crw-rw-rw- 1 root root  10, 196 Dec 18 01:11 vfio

And here is my qemu.conf file:
https://gist.github.com/anonymous/94cc7577b748712d50ad

I've also tried a few thing from this thread "https://bbs.archlinux.org/viewtopic.php?id=162768", I did this:

echo "options vfio_iommu_type1 allow_unsafe_interrupts=1" > /etc/modprobe.d/vfio_iommu_type1.conf

Because I found it somewhat related to my issue, but it didn't change anything.

Offline

#6 2015-12-19 04:17:52

tom.ty89
Member
Registered: 2012-11-15
Posts: 897

Re: [SOLVED] Run qemu without root for vga passthrough?

The owner/group in the udev rule simply needs to be whichever used to run qemu/kvm,  for example, nobody:kvm by default, or nohack:nohack that you specified in your qemu.conf, so:
SUBSYSTEM=="vfio", MODE="0660", GROUP="nohack", or
SUBSYSTEM=="vfio", OWNER="nohack"

If "nohack" is the current user, this will work too:
SUBSYSTEM=="vfio", TAG+="uaccess"
This is what I use since I just run qemu/kvm in a terminal.

You can optionall add a KERNEL match make sure the rule to only apply a specifc device, for example, KERNEL=="1" for /dev/vfio/1 in your case.

AFAIK you'll need to adjust the memory limit with /etc/security/limits.conf (or a conf in limits.d) for non-root user too, but I am not sure if that's necessary for use case with libvirt.

Offline

#7 2015-12-19 18:47:36

Nohack
Member
Registered: 2014-10-14
Posts: 8

Re: [SOLVED] Run qemu without root for vga passthrough?

@tom.ty89 Thanks a lot, it's finally working without sudo!

I added this to "/etc/udev/rules.d/10-qemu-hw-users.rules":

SUBSYSTEM=="vfio", OWNER="nohack"

And added this to "/etc/security/limits.conf":

...
nohack    hard    memlock        9000000
nohack    soft     memlock        9000000
...

Unfortunately I can't get sound to work, I'm not getting errors anymore when adding "-soundhw hda" to my qemu command, but I'm getting weird issues with the nvidia drivers, and I have to reinstall them every time I remove or add the option back, I don't know why this is happening. I can't even see a new sound device being added, and no sound is passed back to the host. It doesn't really matter as I'm getting sound through hdmi anyways, so I'm marking this thread as solved. But if anyone have any experience with qemu and sound, I'd greatly appreciate some help.

Thanks again both of you for taking the time to help me out!

Offline

Board footer

Powered by FluxBB