kvm/qemu - 无需 spice 即可从 Windows 客户机向主机传输音频

kvm/qemu - 无需 spice 即可从 Windows 客户机向主机传输音频

我正在尝试使用 virt-manager 使我的 Windows 10 域与 qemu 一起工作。

一切似乎都运行良好(GPU 直通、CPU),除了当我运行 Spice 控制台来听声音时。声音有噼啪声,使用图形控制台时鼠标滞后(并且 CPU 使用率上升 10-15%),而且我似乎有丢帧现象。

我现在正在使用 Synergy,因此我不必使用 Spice 图形控制台。鼠标现在工作正常。但是当我不使用 Spice 控制台时,我没有声音。

因此,我将音频驱动程序更改为 ac97(使用 Realtek 的 Windows Vista/7 驱动程序),这解决了一些爆裂声问题,但仍然有帧丢失的问题,因为我使用的是 Spice。

我在网上读到,应该有一种方法可以让它与 ALSA 或 Pulseaudio 一起工作,而无需像 Spice 这样的图形控制台。因此,我将文件更改/etc/libvirt/qemu.conf为启用vnc_allow_host_audio/ nographics_allow_host_audio,现在使用环境变量QEMU_AUDIO_DRV=alsa或运行域QEMU_AUDIO_DRV=pa

这会产生所附日志 () 中显示的错误/var/log/libvirt/qemu/win10-gaming.log

我怎样才能让它与 ALSA/Pulseaudio 一起工作,或者更重要的是,在没有 Spice 的情况下?

更新

在过去的几天里,我尝试了一些事情,现在产生了以下错误:

2017-02-24 01:40:15.016+0000: starting up libvirt version: 1.3.1, package: 1ubuntu10.8 (Christian Ehrhardt <[email protected]> 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

我主要关注了这里的用户 DarthRevan13 (https://askubuntu.com/a/727840/656766) 建议以及此处所写的内容:https://wiki.archlinux.org/index.php/Libvirt#PulseAudio

因此我的 XML 如下所示:

<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>

值得注意的变化包括:

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

并在 XML 文件中更改<emulator>/usr/bin/kvm-spice</emulator>为。<emulator>/usr/bin/kvm</emulator>

此外,添加

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

/etc/apparmor.d/abstractions/libvirt-qemu使用以下命令重新加载 apparmor 配置文件:

sudo invoke-rc.d apparmor reload

因此,现在我可以听到声音,但是在运行域时主机音频不起作用。

答案1

我找到了这个。我今晚会测试一下,因为有同样的问题:如何在 Ubuntu 上安装具有可用音频的 KVM

我们正在尝试修复的问题如下:

  1. 让 KVM 以非 root 用户身份运行,具体来说,以您的登录名运行,以便它可以共享您的 ALSA 音频。

  2. 使 KVM 不禁用音频。

  3. 用设置适当环境变量的脚本替换 KVM 二进制文件,以便 KVM 使用您的 alsa 音频驱动程序。

操作说明如下:

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

编辑/usr/bin/kvm并粘贴此文本:

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

编辑/etc/libvirt/qemu.conf并查找以下行(记住所有这些行都位于该文件的不同部分):

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

并将 root 替换为您的本地用户名,例如: vnc_allow_host_audio = 1

user = "gurjeet"
group = "gurjeet"

)现在重新启动KVM服务:

sudo service libvirt-bin restart

现在启动您的客户操作系统并享受它的声音。

相关内容