Skip to content

Instantly share code, notes, and snippets.

@Ruakij
Last active June 14, 2024 09:29
Show Gist options
  • Save Ruakij/dd40b3d7cacf5d0f196d1116771b6e42 to your computer and use it in GitHub Desktop.
Save Ruakij/dd40b3d7cacf5d0f196d1116771b6e42 to your computer and use it in GitHub Desktop.
Looking-glass kvmfr setup Arch

Looking-glass kvmfr setup Arch

More in-depth setup-guide for usage of kvmfr (see https://looking-glass.io/docs/B6/module/)

This setup was done on EndeavourOs (Arch) on Kernel 6.1.12-arch1-1.
QEMU-version 7.2 and libvirt version 9.0.0 .
Running cGroups Policy (if you have AppArmor, some things are different)

Check respective sections in looking-glass docs for other versions (specifcally QEMU <6.2 and libvirt <7.9)


1. Prerequisites

1.1. VM

You need a VM which is fully set-up with graphics-driver and the looking-glass host-program.

Documentation about that can be found here: https://looking-glass.io/docs/B6/install/


1.2. Packages

You need the following packages

  • sudo (or do things as root)
  • git
  • dkms
  • linux-headers (same version as kernel, probably already installed)

1.3. MEM-size

This guide uses the default 32MB size, which is fine for 1080p.

For 1440p or 2160p you need to use 64MB.

See this section to determine how much you need.


Formula

pixel size for SDR is 4
pixel size for HDR is 8

width x height x pixel size x 2 = frame bytes
frame bytes / 1024 / 1024 = frame megabytes
frame megabytes + 10 MiB = total megabytes

Round up to nearest power of 2. (32, 64, 128, ..)

2. Setup

2.1. kvmfr

Clone repository and enter module folder

git clone https://github.com/gnif/LookingGlass
cd LookingGlass/module

Compile and install module with dmks

sudo dkms install "."

Setup udev-rule for module to set permissions
Deviate from docs to make user-independent

sudo cat > /etc/udev/rules.d/99-kvmfr.rules <<EOF
SUBSYSTEM=="kvmfr", OWNER="libvirt-qemu", GROUP="kvm", MODE="0666"
EOF

Setup auto-load of module

sudo cat > /etc/modules-load.d/kvmfr.conf <<EOF
# 3. KVMFR Looking Glass module
kvmfr
EOF

sudo cat > /etc/modprobe.d/kvmfr.conf <<EOF
#KVMFR Looking Glass module
options kvmfr static_size_mb=32
EOF

3.1. libvirt-qemu

3.1.1. Setup cgroups-policy to allow qemu access to device

  1. Open /etc/libvirt/qemu.conf
  2. Find cgroup_device_acl
  3. Uncomment everything in that block and add "/dev/kvmfr0"

Looks something like this:

# 4. This is the basic set of devices allowed / required by
# 5. all virtual machines.
#
# 6. As well as this, any configured block backed disks,
# 7. all sound device, and all PTY devices are allowed.
#                                                                                                          
# 1. This will only need setting if newer QEMU suddenly
# 2. wants some device we don't already know about.
#
cgroup_device_acl = [
    "/dev/null", "/dev/full", "/dev/zero",
    "/dev/random", "/dev/urandom",
    "/dev/ptmx", "/dev/kvm",
    "/dev/kvmfr0"
]
  1. Save file and restart libvirtd
    sudo systemctl restart libvirtd

2.0.1. Setup VM to use MEM-device

  1. Open virtual machine manager

  2. Your VM -> Details -> XML

  3. First line has to be

<domain xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0" type="kvm">
  1. Go to the bottom and add this block
<qemu:commandline>
  <qemu:arg value='-device'/>
  <qemu:arg value='{"driver":"ivshmem-plain","id":"shmem0","memdev":"looking-glass"}'/>
  <qemu:arg value='-object'/>
  <qemu:arg value='{"qom-type":"memory-backend-file","id":"looking-glass","mem-path":"/dev/kvmfr0","size":33554432,"share":true}'/>
</qemu:commandline>

size is your MEM-size in bytes (looking-glass docs)
Formula: MEM-size x 1024 x 1024


  1. Save and check if everything is still there
    libvirt might replace " with &quot;, thats fine

2.1. Looking-Glass Client

You might not have to specify the mem-device as it already detects the correct one

Feels free to add a config-file or manually add the -f Option if not


3. Testing

  1. Load module manually
sudo modprobe kvmfr static_size_mb=32
  1. Check kernel-log for output of module
sudo dmesg | grep kvmfr

Should return this:

kvmfr: creating 1 static devices


  1. Check created node and permissions
ls -la /dev/kvmfr*

Should return this:

crw-rw-rw- 1 libvirt-qemu kvm 510, 0 23. Feb 10:00 /dev/kvmfr0


  1. Now try to start the VM and then looking-glass
@KennyHarrer
Copy link

Hello. I followed your tutorial exactly and got an error.

Error starting domain: internal error: QEMU unexpectedly closed the monitor (vm='win11'): 2023-09-19T05:09:11.699319Z qemu-system-x86_64: -device {"driver":"ivshmem-plain","id":"shmem0","memdev":"shmmem-shmem0","bus":"pci.16","addr":"0x1"}: Duplicate device ID 'shmem0'

Traceback (most recent call last):
  File "/usr/share/virt-manager/virtManager/asyncjob.py", line 72, in cb_wrapper
    callback(asyncjob, *args, **kwargs)
  File "/usr/share/virt-manager/virtManager/asyncjob.py", line 108, in tmpcb
    callback(*args, **kwargs)
  File "/usr/share/virt-manager/virtManager/object/libvirtobject.py", line 57, in newfn
    ret = fn(self, *args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/share/virt-manager/virtManager/object/domain.py", line 1402, in startup
    self._backend.create()
  File "/usr/lib/python3.11/site-packages/libvirt.py", line 1373, in create
    raise libvirtError('virDomainCreate() failed')
libvirt.libvirtError: internal error: QEMU unexpectedly closed the monitor (vm='win11'): 2023-09-19T05:09:11.699319Z qemu-system-x86_64: -device {"driver":"ivshmem-plain","id":"shmem0","memdev":"shmmem-shmem0","bus":"pci.16","addr":"0x1"}: Duplicate device ID 'shmem0'```

@Ruakij
Copy link
Author

Ruakij commented Sep 19, 2023

Duplicate device ID 'shmem0'

Check if you have a "shmem" device definition elsewhere in your file. Maybe you just forgot to remove the shmem device from the guide? (https://looking-glass.io/docs/B6/install/#ivshmem)
Could also be one of qemu features adding this which i am not using.

If that doesnt work, i am not sure if you can simply change the id as i assume the host application in the VM uses the first shmem device, but maybe that can be overwritten in a config.

@KennyHarrer
Copy link

KennyHarrer commented Sep 19, 2023

here is a link to my full config: https://hastebin.com/share/oqugefetij.xml

I tried changing shamem0 to shamem1 and nothing changed.

the module loads

k@archlinux ~/D/L/module (master)> sudo dmesg | grep kvmfr
[    1.695873] kvmfr: loading out-of-tree module taints kernel.
[    1.695875] kvmfr: module verification failed: signature and/or required key missing - tainting kernel
[    1.695969] kvmfr: creating 1 static devices
[    1.700224] kvmfr: kvmfr_module_init: module loaded

permissions match

k@archlinux ~/D/L/module (master)> ls -la /dev/kvmfr*
crw-rw-rw- 1 libvirt-qemu kvm 236, 0 Sep 19 09:18 /dev/kvmfr0

versions

libvirtd (libvirt) 9.7.0
QEMU emulator version 8.1.0
Kernel: 6.5.3-arch1-1

When I remove qemu:commandline lines the vm will boot.

@gnif
Copy link

gnif commented Jun 14, 2024

Please note that this is all outdated, please see
https://looking-glass.io/docs/bleeding/install_libvirt/#ivshmem

@Ruakij
Copy link
Author

Ruakij commented Jun 14, 2024

Please note that this is all outdated, please see https://looking-glass.io/docs/bleeding/install_libvirt/#ivshmem

I have checked https://looking-glass.io/docs/B7-rc1-30-d060e375/ivshmem_kvmfr/ and its mostly the same, what do you think needs to be adjusted?
I found the cgroups and apparmor section a nice addition, but not required for Arch afaik.
Havent tested if the qemu.conf still needs to be adjusted.
Maybe loading via systemd could be nice, but modprobe.d should do the same anyways.

To be fair, this guide here isnt really needed anymore as the official one now nicely includes most things.

@gnif
Copy link

gnif commented Jun 14, 2024

You already mentioned the things missing, and yes, cgroups is also required for arch. The reason I have posted that this is outdated is people are landing in the LG support discord after following this finding that they have a broken setup.

Please in future if our documentation is missing steps, or needs changes, suggest them to the project. If you want to maintain an arch specific version, feel free to do so on the community maintained LG Wiki where we are able to edit/add/fix mistakes and errors as the project changes and matures.

@Ruakij
Copy link
Author

Ruakij commented Jun 14, 2024

You already mentioned the things missing, and yes, cgroups is also required for arch. The reason I have posted that this is outdated is people are landing in the LG support discord after following this finding that they have a broken setup.

I have just tested this setup and it works perfectly fine. I wouldnt expect this to work anywhere, but its fine for Arch or probably anything that doesnt use AppArmor.
I can understand users just wanting things to work and simply following the next-best guide, but saying this is "all" outdated..

When i suggested a small doc-fix, i didnt even know it got fixed as you commited it yourself and closed the PR with no comment. I didnt even know about it because Github doesnt link Commits back to PRs if they are mentioned.
Such communication or lack thereof is really discouraging.

@gnif
Copy link

gnif commented Jun 14, 2024

When i suggested a small doc-fix, i didnt even know it got fixed as you commited it yourself and closed the PR with no comment

Where?
Edit: Sorry, I typed your name wrong, found it:
gnif/LookingGlass#1061

I always try to ensure authors get credit even if it's just for a typo:

Ie:

I do this because minor commits like this where the committer is making a tiny change and is not already in the AUTHORS file (a requirement of this project) doesn't really warrant the extra commit to add this entry.

I am sorry that I didn't mention you directly there, but please note that I closed two PR's at once as both PR's were minor in themselves and needed to be merged.

Such communication or lack thereof is really discouraging.

Please note that the fact that your PR was closed by means of a code commit is a very common and valid means of communication in the software development world. I could have just as easily have made the changes, and then manually closed the PR leaving it completely unattributed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment