Ubuntu – kvm/qemu – Audio from windows guest to host without spice

alsaaudiokvm-switchqemuubuntu 16.04

I am trying to get my Windows 10 domain working with qemu using virt-manager.

Everything seems to work fine (GPU passthrough, CPU), except when I am running the spice console in order to hear the sound. The sound is crackling and while using the graphical console the mouse is lagging (and CPU usage goes up 10-15%) and I seem to have framedrops.

I am using Synergy now, so I do not have to use the Spice graphics console. The mouse is working fine now. But while I am not using the spice console, I have no sound.

So, I changed the audio driver to ac97 (using the Windows Vista/7 drivers from Realtek), which solves the crackling a bit, but I still have frame drops, because I am using Spice.

I have read online, there should be a way to get it working with ALSA or Pulseaudio and without a graphics console like Spice. So, I changed the file /etc/libvirt/qemu.conf to enable vnc_allow_host_audio/nographics_allow_host_audio and am now running the domain with the environment variable QEMU_AUDIO_DRV=alsa or QEMU_AUDIO_DRV=pa.

This produces the errors shown in the attached log (/var/log/libvirt/qemu/win10-gaming.log).

How can I make it work with ALSA/Pulseaudio or more importantly without Spice?

Update

In the last few days I tried a few things, which now produces the following errors:

2017-02-24 01:40:15.016+0000: starting up libvirt version: 1.3.1, package: 1ubuntu10.8 (Christian Ehrhardt <christian.ehrhardt@canonical.com> Mon, 06 Feb 2017 14:30:46 +0100), qemu version: 2.5.0 (Debian 1:2.5+dfsg-5ubuntu10.8), hostname: lindesktop
LC_ALL=C PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin QEMU_AUDIO_DRV=pa /usr/bin/kvm-spice -name win10-gaming -S -machine pc-i440fx-xenial,accel=kvm,usb=off -cpu host,kvm=off -drive file=/usr/share/OVMF/OVMF_CODE.fd,if=pflash,format=raw,unit=0,readonly=on -drive file=/var/lib/libvirt/qemu/nvram/win10-gaming_VARS.fd,if=pflash,format=raw,unit=1 -m 8192 -mem-prealloc -mem-path /dev/hugepages/libvirt/qemu -realtime mlock=off -smp 6,sockets=1,cores=2,threads=3 -uuid eb48ea95-aa76-4dad-80c9-1902547899b0 -nographic -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/domain-win10-gaming/monitor.sock,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=discard -no-hpet -no-shutdown -global PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -boot menu=on,strict=on -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x6.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x6 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x6.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x6.0x2 -device ahci,id=sata0,bus=pci.0,addr=0x5 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x7 -drive file=/home/michael/Downloads/virtio-win-0.1.126.iso,format=raw,if=none,media=cdrom,id=drive-sata0-0-0,readonly=on -device ide-cd,bus=sata0.0,drive=drive-sata0-0-0,id=sata0-0-0 -drive file=/home/michael/Downloads/Windows.iso,format=raw,if=none,media=cdrom,id=drive-sata0-0-1,readonly=on -device ide-cd,bus=sata0.1,drive=drive-sata0-0-1,id=sata0-0-1,bootindex=2 -drive file=/media/michael/0f5fc26f-460c-4aba-9b08-975fbb63b955/win10-gaming.qcow2,format=qcow2,if=none,id=drive-virtio-disk0 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x8,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -netdev tap,fd=27,id=hostnet0,vhost=on,vhostfd=29 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:50:bc:b9,bus=pci.0,addr=0x3 -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,vgamem_mb=16,bus=pci.0,addr=0x2 -device AC97,id=sound0,bus=pci.0,addr=0x4 -device vfio-pci,host=01:00.0,id=hostdev0,bus=pci.0,addr=0xa -device vfio-pci,host=01:00.1,id=hostdev1,bus=pci.0,addr=0xb -device usb-host,hostbus=1,hostaddr=3,id=hostdev2 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x9 -msg timestamp=on
Domain id=4 is tainted: custom-argv
Domain id=4 is tainted: host-cpu
shm_open() failed: Permission denied
pulseaudio: pa_context_connect() failed
pulseaudio: Reason: Connection refused
pulseaudio: Failed to initialize PA contextaudio: Could not init `pa' audio driver
shm_open() failed: Permission denied
pulseaudio: pa_context_connect() failed
pulseaudio: Reason: Connection refused
pulseaudio: Failed to initialize PA contextaudio: Could not init `pa' audio driver
shm_open() failed: Permission denied
Failed to create secure directory (/home/michael/.pulse): Permission denied

I mainly followed, what the user DarthRevan13 over here (https://askubuntu.com/a/727840/656766) suggested and what is written here: https://wiki.archlinux.org/index.php/Libvirt#PulseAudio

So my XML looks like this:

<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
  <name>win10-gaming</name>
  <uuid>eb48ea95-aa76-4dad-80c9-1902547899b0</uuid>
  <memory unit='KiB'>8388608</memory>
  <currentMemory unit='KiB'>8388608</currentMemory>
  <memoryBacking>
    <hugepages/>
  </memoryBacking>
  <vcpu placement='static'>6</vcpu>
  <cputune>
    <vcpupin vcpu='0' cpuset='1'/>
    <vcpupin vcpu='1' cpuset='2'/>
    <vcpupin vcpu='2' cpuset='3'/>
    <vcpupin vcpu='3' cpuset='5'/>
    <vcpupin vcpu='4' cpuset='6'/>
    <vcpupin vcpu='5' cpuset='7'/>
  </cputune>
  <os>
    <type arch='x86_64' machine='pc-i440fx-xenial'>hvm</type>
    <loader readonly='yes' type='pflash'>/usr/share/OVMF/OVMF_CODE.fd</loader>
    <nvram>/var/lib/libvirt/qemu/nvram/win10-gaming_VARS.fd</nvram>
    <bootmenu enable='yes'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <kvm>
      <hidden state='on'/>
    </kvm>
  </features>
  <cpu mode='host-passthrough'>
    <topology sockets='1' cores='2' threads='3'/>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/home/michael/Downloads/virtio-win-0.1.126.iso'/>
      <target dev='sda' bus='sata'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/home/michael/Downloads/Windows.iso'/>
      <target dev='sdb' bus='sata'/>
      <readonly/>
      <boot order='2'/>
      <address type='drive' controller='0' bus='0' target='0' unit='1'/>
    </disk>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/media/michael/0f5fc26f-460c-4aba-9b08-975fbb63b955/win10-gaming.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <boot order='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='sata' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </controller>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:50:bc:b9'/>
      <source bridge='xenbr0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <sound model='ac97'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </sound>
    <video>
      <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <hostdev mode='subsystem' type='pci' managed='yes'>
      <source>
        <address domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
      </source>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0a' function='0x0'/>
    </hostdev>
    <hostdev mode='subsystem' type='pci' managed='yes'>
      <source>
        <address domain='0x0000' bus='0x01' slot='0x00' function='0x1'/>
      </source>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x0b' function='0x0'/>
    </hostdev>
    <hostdev mode='subsystem' type='usb' managed='yes'>
      <source>
        <vendor id='0x046d'/>
        <product id='0xc24e'/>
      </source>
    </hostdev>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
    </memballoon>
  </devices>
  <qemu:commandline>
    <qemu:env name='QEMU_AUDIO_DRV' value='alsa'/>
    <qemu:env name='QEMU_PA_SERVER' value='/run/user/1000/pulse/native'/>
  </qemu:commandline>
</domain>

Notable changes include:

<qemu:commandline>
    <qemu:env name='QEMU_AUDIO_DRV' value='pa'/>
    <qemu:env name='QEMU_PA_SERVER' value='/run/user/1000/pulse/native'/>
</qemu:commandline>

and changing <emulator>/usr/bin/kvm-spice</emulator> to <emulator>/usr/bin/kvm</emulator> in the XML file.

Additionally, adding

@{HOME}/.pulse/** rw,

in /etc/apparmor.d/abstractions/libvirt-qemu and reloading the apparmor profiles with:

sudo invoke-rc.d apparmor reload

So, now I can hear the sound, but my host audio does not work while running the domain.

Best Answer

I found this. I will be testing it tonight as have the same issue: How to install KVM with a working audio (on Ubuntu)

The things we are trying to fix are:

  1. Make KVM run as a non-root user, specifically, as your login, so that it can share your ALSA audio.

  2. Make KVM to not disable audio.

  3. Replace the KVM binary with a script that sets up proper environment variable, so that KVM and use your alsa audio driver.

Instructions follow:

cd /usr/bin  
sudo mv kvm kvm.bin  
sudo touch kvm  
sudo chmod +x kvm  

Edit /usr/bin/kvm and paste this text in it:

#!/bin/sh  
QEMU_AUDIO_DRV=alsa /usr/bin/kvm.bin $@

Edit /etc/libvirt/qemu.conf and look for the following lines (remember all these lines are in different sections of this file):

vnc_allow_host_audio = 0
#user = "root"
#group = "root"

and replace root with your local user name, for example: vnc_allow_host_audio = 1

user = "gurjeet"
group = "gurjeet"

) Now restart the KVM service:

sudo service libvirt-bin restart

Now launch your guest OS and enjoy its sound.

Related Question