From Fedora Project Wiki

No edit summary
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
vhost-device-sound
= vhost-device-sound =
 
vhost-device-sound from rust-vmm is an emulation of the virtio-sound device using the vhost-user protocol.
The vhost-device-sound crate is based on the virtio-specification standard
This device implements the virtio-sound emulation with mono and stereo audio support which can be used with either a Pipewire or Alsa audio backend for playback or capture.


`vhost-device-sound` from rust-vmm is an emulation of the virtio-sound device using the vhost-user protocol.<br>
The `vhost-device-sound` crate is based on the virtio-specification standard.<br>
This device implements the virtio-sound emulation with mono and stereo audio support which can be used with either a Pipewire or Alsa audio backend for playback or capture.<br>
This VIRTIO sound device works on VMMs that support VIRTIO device emulation. A typical use case is QEMU.
This VIRTIO sound device works on VMMs that support VIRTIO device emulation. A typical use case is QEMU.


More information about this device can be found in:
More information about this device can be found in:
<br>
<br>
1. The vhost-device-sound crate: https://crates.io/crates/vhost-device-sound  
1. The `vhost-device-sound` crate: https://crates.io/crates/vhost-device-sound  
<br>
<br>
2. The vhost-user-snd-pci device in QEMU: https://gitlab.com/qemu-project/qemu/-/blob/master/hw/virtio/vhost-user-snd-pci.c
2. The `vhost-user-snd-pci` device in QEMU: https://gitlab.com/qemu-project/qemu/-/blob/master/hw/virtio/vhost-user-snd-pci.c


== Setting up the virtual image for the system ==
== Setting up the virtual image for the system ==


1. Download a fedora image from the download site i.e: https://eu.edge.kernel.org/fedora/releases/39/Server/x86_64/images/ it's a regular persistent Fedora image, instead of booting a live iso every time.
1. Download a Fedora image from the download site i.e: https://eu.edge.kernel.org/fedora/releases/40/Server/x86_64/images/ it's a regular persistent Fedora image, instead of booting a live iso every time.
<br>
<br>
2. Start the fedora qcow image using the QEMU command:
2. Start the Fedora qcow image using the QEMU command:
     $./qemu-system-x86_64 -smp 4 -m 4G -enable-kvm \
     $ qemu-system-x86_64 -smp 4 -m 4G -enable-kvm \
       -drive file=../Fedora-Server-KVM-39-1.5.x86_64.qcow2,format=qcow2
       -drive file=../Fedora-Server-KVM-40-1.14.x86_64.qcow2,format=qcow2


At first boot, it ask for a user creation:  
At first boot, it ask for a user creation:  
Line 28: Line 27:
Then a login prompt will appear: login with your already created user name and password
Then a login prompt will appear: login with your already created user name and password


Check if the virtio_snd module is available. Run `lsmod | grep virtio_snd` to see if the module exists on your guest system. If it doesn't, you may need to install the appropriate kernel headers or a specific package that provides this module, also ensure the correct kernel version is being used.
Check if the `virtio_snd` module is available. Run `lsmod | grep virtio_snd` to see if the module exists on your guest system. If it doesn't, you may need to install the appropriate kernel headers or a specific package that provides this module, also ensure the correct kernel version is being used.
<br>
<br>
For a fedora image run this command to install the necessary modules
For a Fedora image, run this command to install the necessary modules:
     $sudo dnf install kernel-modules-extra-$(uname -r)
     $ sudo dnf install kernel-modules-extra-$(uname -r)
     $sudo modprobe virtio_snd
     $ sudo modprobe virtio_snd
     $lsmod | grep virtio_snd
     $ lsmod | grep virtio_snd
To test a simple playback or capture, install `alsa-utils` package:
    $ sudo dnf install alsa-utils -y
Now shutdown the qemu window and launch your new OS with QEMU, this time with the vhost-device-sound daemon already running in another terminal.


To test a simple playback or capture, install alsa-utils package
    $sudo dnf install alsa-utils -y
Now shutdown the qemu window and launch your new OS with QEMU, this time with the vhost-device-sound daemon already running in another terminal.
== Testing the vhost-device-sound with QEMU sound-pci device. ==
== Testing the vhost-device-sound with QEMU sound-pci device. ==


1. In one terminal install the vhost-device-sound package
1. In one terminal install the vhost-device-sound package:
     $sudo dnf install vhost-device-sound
     $ sudo dnf install vhost-device-sound
2. Run the following command to start the vhost-device-sound daemon
2. Run the following command to start the vhost-device-sound daemon:
     $vhost-device-sound --socket=/tmp/mysnd.socket --backend pipewire
     $ vhost-device-sound --socket=/tmp/mysnd.socket --backend pipewire
The alsa backend is also available for use. you can use `--help` to view options
The alsa backend is also available for use. you can use `--help` to view options


3. In another terminal run the following command to launch the OS in QEMU. which will connect the QEMU pci device frontend to the vhost-device-sound backend via the unix socket.
3. If you have a QEMU version older than 9.0.0 you need to get a newer version. You can get 9.0.0 sources with:
     $./qemu-system-x86_64 -smp 4 -m 4G -mem-prealloc    \
    $ git clone https://gitlab.com/qemu-project/qemu.git
     -object memory-backend-file,share=on,id=mem0,size=4096M,mem-path="/dev/shm" \
    $ git checkout v9.0.0
     -machine q35,memory-backend=mem0 \
Once you have a recent enough QEMU version, the following command will connect the QEMU pci device frontend to the `vhost-device-sound` backend via the unix socket:
     -chardev socket,id=char0,reconnect=0,path=/tmp/mysnd.socket -enable-kvm -snapshot \
     $ qemu-system-x86_64 -snapshot -mem-prealloc    \
     -object memory-backend-memfd,share=on,id=mem0,size=4G \
     -machine q35,memory-backend=mem0,accel=kvm \
     -chardev socket,id=char0,reconnect=0,path=/tmp/mysnd.socket  \
     -device vhost-user-snd-pci,chardev=char0        \
     -device vhost-user-snd-pci,chardev=char0        \
     -drive file=../Fedora-Server- KVM-39-1.5.x86_64.qcow2,format=qcow2
     -drive file=Fedora-Server-KVM-39-1.5.x86_64.qcow2,format=qcow2
to be specific this is the QEMU command that uses the device backend's socket
 
    $-chardev socket,id=vsnd,path=/tmp/mysnd.sock \
to be specific this is the QEMU options that tell qemu to use the device backend's socket
    -chardev socket,id=vsnd,path=/tmp/mysnd.sock \
     -device vhost-user-snd-pci,chardev=vsnd
     -device vhost-user-snd-pci,chardev=vsnd
If QEMU fails with an error like:
    Failed initializing vhost-user memory map, consider using -object memory-backend-memfd share=on
the following options will solve it:
    -object memory-backend-memfd,share=on,id=mem0,size=4G, \
    -machine q35,memory-backend=mem0,accel=kvm \
4. now the guest OS is running in QEMU, you can play a sound from the guest or perform a simple speaker-test
Notes:
qemu-kvm 9.0.0 has been released in Fedora ELN and CentOS Stream 9 but as of May 2024 they have `vhost-user-snd-pci` device disabled. A build with that device enabled should come to Fedora ELN and CentOS Stream 10 soon.

Latest revision as of 14:02, 22 May 2024

vhost-device-sound

vhost-device-sound from rust-vmm is an emulation of the virtio-sound device using the vhost-user protocol.
The vhost-device-sound crate is based on the virtio-specification standard.
This device implements the virtio-sound emulation with mono and stereo audio support which can be used with either a Pipewire or Alsa audio backend for playback or capture.
This VIRTIO sound device works on VMMs that support VIRTIO device emulation. A typical use case is QEMU.

More information about this device can be found in:
1. The vhost-device-sound crate: https://crates.io/crates/vhost-device-sound
2. The vhost-user-snd-pci device in QEMU: https://gitlab.com/qemu-project/qemu/-/blob/master/hw/virtio/vhost-user-snd-pci.c

Setting up the virtual image for the system

1. Download a Fedora image from the download site i.e: https://eu.edge.kernel.org/fedora/releases/40/Server/x86_64/images/ it's a regular persistent Fedora image, instead of booting a live iso every time.
2. Start the Fedora qcow image using the QEMU command:

    $ qemu-system-x86_64 -smp 4 -m 4G -enable-kvm \
     -drive file=../Fedora-Server-KVM-40-1.14.x86_64.qcow2,format=qcow2

At first boot, it ask for a user creation:

   Select 5 for user creation,
   Select 1 to create user
   Select the following options accordingly to set username and set password
   Press c to continue

Then a login prompt will appear: login with your already created user name and password

Check if the virtio_snd module is available. Run lsmod | grep virtio_snd to see if the module exists on your guest system. If it doesn't, you may need to install the appropriate kernel headers or a specific package that provides this module, also ensure the correct kernel version is being used.
For a Fedora image, run this command to install the necessary modules:

   $ sudo dnf install kernel-modules-extra-$(uname -r)
   $ sudo modprobe virtio_snd
   $ lsmod | grep virtio_snd

To test a simple playback or capture, install alsa-utils package:

   $ sudo dnf install alsa-utils -y

Now shutdown the qemu window and launch your new OS with QEMU, this time with the vhost-device-sound daemon already running in another terminal.

Testing the vhost-device-sound with QEMU sound-pci device.

1. In one terminal install the vhost-device-sound package:

    $ sudo dnf install vhost-device-sound

2. Run the following command to start the vhost-device-sound daemon:

    $ vhost-device-sound --socket=/tmp/mysnd.socket --backend pipewire

The alsa backend is also available for use. you can use --help to view options

3. If you have a QEMU version older than 9.0.0 you need to get a newer version. You can get 9.0.0 sources with:

   $ git clone https://gitlab.com/qemu-project/qemu.git
   $ git checkout v9.0.0

Once you have a recent enough QEMU version, the following command will connect the QEMU pci device frontend to the vhost-device-sound backend via the unix socket:

   $ qemu-system-x86_64  -snapshot -mem-prealloc     \
    -object memory-backend-memfd,share=on,id=mem0,size=4G \
    -machine q35,memory-backend=mem0,accel=kvm \
    -chardev socket,id=char0,reconnect=0,path=/tmp/mysnd.socket  \
    -device vhost-user-snd-pci,chardev=char0         \
    -drive file=Fedora-Server-KVM-39-1.5.x86_64.qcow2,format=qcow2

to be specific this is the QEMU options that tell qemu to use the device backend's socket

    -chardev socket,id=vsnd,path=/tmp/mysnd.sock \
    -device vhost-user-snd-pci,chardev=vsnd

If QEMU fails with an error like:

   Failed initializing vhost-user memory map, consider using -object memory-backend-memfd share=on

the following options will solve it:

    -object memory-backend-memfd,share=on,id=mem0,size=4G, \
    -machine q35,memory-backend=mem0,accel=kvm \

4. now the guest OS is running in QEMU, you can play a sound from the guest or perform a simple speaker-test

Notes:

qemu-kvm 9.0.0 has been released in Fedora ELN and CentOS Stream 9 but as of May 2024 they have vhost-user-snd-pci device disabled. A build with that device enabled should come to Fedora ELN and CentOS Stream 10 soon.