LookingGlass/doc/module.rst

256 lines
6.3 KiB
ReStructuredText
Raw Normal View History

2021-07-05 17:24:30 +00:00
.. _kernel_module:
2022-09-18 23:57:49 +00:00
Kernel module
#############
This kernel module implements a basic interface to the IVSHMEM device
2022-09-19 00:16:57 +00:00
for Looking Glass in VM → VM mode.
2022-09-19 00:16:57 +00:00
Additionally in VM → host mode, it can be used to generate a shared
memory device on the host machine that supports dmabuf.
2021-07-05 17:24:30 +00:00
Prerequisites
-------------
The linux kernel headers for your kernel version are required for building.
Install them with ``apt-get``
2021-07-05 17:24:30 +00:00
.. code:: bash
apt-get install linux-headers-$(uname -r)
2021-07-05 17:24:30 +00:00
Then switch to the ``module/`` directory
.. code:: bash
cd module/
.. _module_dkms:
2022-09-18 23:57:49 +00:00
Using DKMS (recommended)
------------------------
2021-07-05 17:24:30 +00:00
You can use the kernel's DKMS feature to keep the module across upgrades.
``dkms`` must be installed.
.. code:: bash
apt-get install dkms
2021-07-05 17:24:30 +00:00
.. _module_dkms_install:
2021-07-05 17:24:30 +00:00
Installing
~~~~~~~~~~
To install the module into DKMS, run
.. code:: bash
dkms install "."
2021-07-05 17:24:30 +00:00
.. _module_dkms_loading:
Loading
~~~~~~~
2022-09-19 00:16:57 +00:00
For VM → VM, simply modprobe the module::
modprobe kvmfr
2022-09-19 00:16:57 +00:00
For VM → host with dmabuf, modprobe with the parameter
2021-07-05 17:24:30 +00:00
``static_size_mb``:
2021-07-05 17:24:30 +00:00
.. code:: bash
modprobe kvmfr static_size_mb=32
Just like above, multiple devices can be created by separating the sizes
with commas.
.. _module_manual:
2022-09-18 23:57:49 +00:00
Compiling & loading (manual)
----------------------------
To compile the module manually, run ``make`` in the module directory.
.. _module_manual_loading:
Loading
~~~~~~~
2022-09-19 00:16:57 +00:00
For VM → VM mode, run:
.. code:: bash
insmod kvmfr.ko
2022-09-19 00:16:57 +00:00
For VM → host mode with dmabuf, instead of creating a shared memory file,
load this module with the parameter ``static_size_mb``. For example, a
32 MB shared memory device can be created with:
.. code:: bash
insmod kvmfr.ko static_size_mb=32
Multiple devices can be created by separating the sizes with commas. For
example, ``static_size_mb=128,64`` would create two kvmfr devices:
``kvmfr0`` would be 128 MB and ``kvmfr1`` would be 64 MB.
.. note::
If you have already loaded an older version of the module, unload it
first. You can do this by rebooting, or with ``rmmod``:
.. code:: bash
rmmod kvmfr.ko
2021-07-05 17:24:30 +00:00
.. _module_usage:
Usage
-----
2021-07-05 17:24:30 +00:00
The module will create the ``/dev/kvmfr0`` node, which represents the KVMFR
interface. To use the interface, you need permission to access it by
either: creating a udev rule to ensure your user can read and write to
2021-09-01 21:05:39 +00:00
it, or simply change its ownership manually, i.e.:
2021-07-05 17:24:30 +00:00
.. code:: bash
sudo chown user:user /dev/kvmfr0
2021-07-05 17:24:30 +00:00
As an example, you can create a new file in ``/etc/udev/rules.d/99-kvmfr.rules``
with the following contents::
SUBSYSTEM=="kvmfr", OWNER="user", GROUP="kvm", MODE="0660"
2021-07-05 17:24:30 +00:00
(replace ``user`` with your username)
Usage with Looking Glass is simple, you only need to specify the path to
the device node, for example:
.. code:: bash
./looking-glass-client -f /dev/kvmfr0
You may also use a config file: ``~/.looking-glass-client.ini``, or
``/etc/looking-glass-client.ini``.
.. code:: ini
[app]
shmFile=/dev/kvmfr0
2021-07-05 17:24:30 +00:00
.. _module_vm_to_host:
2022-09-19 00:16:57 +00:00
VM → Host
~~~~~~~~~~~~
2022-09-19 00:16:57 +00:00
In VM → host mode, use this device in place of the shared memory file.
2021-07-05 17:24:30 +00:00
QEMU
^^^^
Add the following arguments to your ``qemu`` command line::
-device ivshmem-plain,id=shmem0,memdev=looking-glass
2021-07-05 17:24:30 +00:00
-object memory-backend-file,id=looking-glass,mem-path=/dev/kvmfr0,size=32M,share=yes
2021-07-05 17:24:30 +00:00
.. note::
2021-07-05 17:24:30 +00:00
The ``size`` argument must be the same size you passed
to the ``static_size_mb`` argument when loading the kernel module.
2021-07-05 17:24:30 +00:00
libvirt
^^^^^^^
2022-09-19 00:16:57 +00:00
Starting with QEMU 6.2 and libvirt 7.9, JSON style QEMU configuration is the
default syntax. Users running QEMU 6.2 or later **and** libvirt 7.9 or later,
2021-12-25 20:33:01 +00:00
should use this XML block to configure their VM for kvmfr:
.. code:: xml
<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>
2021-07-05 17:24:30 +00:00
.. note::
2022-09-19 00:16:57 +00:00
- The ``"size"`` tag represents the size of the shared memory device in
2021-12-25 20:33:01 +00:00
bytes. Once you determine the proper size of the device as per
:ref:`Determining Memory <libvirt_determining_memory>`, use the figure you
2021-12-25 20:33:01 +00:00
got to calculate the size in bytes:
``size_in_MB x 1024 x 1024 = size_in_bytes``
If you are running QEMU older than 6.2 or libvirt older than 7.9, please use
2021-12-25 20:33:01 +00:00
legacy syntax for IVSHMEM setup:
.. code:: xml
<qemu:commandline>
<qemu:arg value='-device'/>
<qemu:arg value='ivshmem-plain,id=shmem0,memdev=looking-glass'/>
<qemu:arg value='-object'/>
<qemu:arg value='memory-backend-file,id=looking-glass,mem-path=/dev/kvmfr0,size=32M,share=yes'/>
</qemu:commandline>
.. note::
2022-09-19 00:16:57 +00:00
- Using the legacy syntax on QEMU 6.2/libvirt 7.9 may cause QEMU to
abort with the following error message:
"``error: internal error: ... PCI: slot 1 function 0 not available for pcie-root-port, in use by ivshmem-plain``"
- Remember to add ``xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'``
to the ``<domain>`` tag.
2021-07-05 17:24:30 +00:00
Running libvirt this way violates AppArmor and cgroups policies, which will
block the VM from running. These policies must be amended to allow the VM
to start:
- For AppArmor, create ``/etc/apparmor.d/local/abstractions/libvirt-qemu`` if
it doesn't exist, and add the following::
# Looking Glass
/dev/kvmfr0 rw,
- For cgroups, edit ``/etc/libvirt/qemu.conf``, uncomment the
``cgroup_device_acl`` block, and add ``/dev/kvmfr0`` to the list.
Then restart ``libvirtd``:
2021-07-05 17:24:30 +00:00
.. code:: bash
sudo systemctl restart libvirtd.service
.. _systemd_modules_load:
systemd-modules-load
~~~~~~~~~~~~~~~~~~~~
For convenience, you may load the KVMFR module when starting your computer.
We can use the ``systemd-modules-load.service(8)`` service for this task.
2021-07-05 17:24:30 +00:00
Create the file ``/etc/modules-load.d/kvmfr.conf`` with the following
contents::
#KVMFR Looking Glass module
kvmfr
This will now run the next time you start your machine.
2022-09-19 00:16:57 +00:00
If you are running in VM → host mode, you must additionally create another file
``/etc/modprobe.d/kvmfr.conf`` to properly set the size. It should have the
2021-07-05 17:24:30 +00:00
following contents::
#KVMFR Looking Glass module
2021-07-05 17:24:30 +00:00
options kvmfr static_size_mb=32
.. note::
Don't forget to adjust ``static_size_mb`` to your needs.