From Fedora Project Wiki

Revision as of 07:56, 20 May 2024 by Dorinda (talk | contribs)

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/39/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-39-1.5.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. 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.

   $./qemu-system-x86_64 -smp 4 -m 4G -mem-prealloc     \
    -object memory-backend-file,share=on,id=mem0,size=4096M,mem-path="/dev/shm" \
    -machine q35,memory-backend=mem0 \
    -chardev socket,id=char0,reconnect=0,path=/tmp/mysnd.socket -enable-kvm -snapshot  \
    -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 command that uses the device backend's socket

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

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