Skip to content

Commit 04363c0

Browse files
authored
Merge pull request #2456 from afbjorklund/audio-default
Add default value for audio driver
2 parents ce73395 + af048c4 commit 04363c0

File tree

6 files changed

+87
-5
lines changed

6 files changed

+87
-5
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Optional feature enablers:
5656
- [`experimental/net-user-v2`](./experimental/net-user-v2.yaml): [experimental] user-v2 network
5757
to enable VM-to-VM communication without root privilege
5858
- [`experimental/vnc`](./experimental/vnc.yaml): [experimental] use vnc display and xorg server
59+
- [`experimental/alsa`](./experimental/alsa.yaml): [experimental] use alsa and default audio device
5960

6061
Lost+found:
6162
- ~`centos`~: Removed in Lima v0.8.0, as CentOS 8 reached [EOL](https://www.centos.org/centos-linux-eol/).

examples/default.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ audio:
297297
# QEMU audiodev, e.g., "none", "coreaudio", "pa", "alsa", "oss".
298298
# VZ driver, use "vz" as device name
299299
# Choosing "none" will mute the audio output, and not play any sound.
300+
# Choosing "default" will pick a suitable of: coreudio, pa, dsound, oss.
301+
# As of QEMU v6.2 the default is to create a disconnected sound device
302+
# that is still visible in the guest but not connected to the host.
300303
# 🟢 Builtin default: ""
301304
device: null
302305

examples/experimental/alsa.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# A template to run ubuntu using device: default
2+
# This template requires Lima v0.23.0 or later.
3+
images:
4+
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
5+
- location: "https://cloud-images.ubuntu.com/releases/24.04/release-20240423/ubuntu-24.04-server-cloudimg-amd64.img"
6+
arch: "x86_64"
7+
digest: "sha256:32a9d30d18803da72f5936cf2b7b9efcb4d0bb63c67933f17e3bdfd1751de3f3"
8+
- location: "https://cloud-images.ubuntu.com/releases/24.04/release-20240423/ubuntu-24.04-server-cloudimg-arm64.img"
9+
arch: "aarch64"
10+
digest: "sha256:c841bac00925d3e6892d979798103a867931f255f28fefd9d5e07e3e22d0ef22"
11+
# Fallback to the latest release image.
12+
# Hint: run `limactl prune` to invalidate the cache
13+
- location: "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img"
14+
arch: "x86_64"
15+
- location: "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-arm64.img"
16+
arch: "aarch64"
17+
18+
mounts:
19+
- location: "~"
20+
- location: "/tmp/lima"
21+
writable: true
22+
23+
vmType: "qemu"
24+
audio:
25+
device: "default"
26+
27+
provision:
28+
- mode: system
29+
script: |
30+
#!/bin/bash
31+
set -eux -o pipefail
32+
test -e /lib/modules/$(uname -r)/kernel/sound/pci/hda/snd-hda-intel.ko* && exit 0
33+
apt-get install -y linux-modules-extra-$(uname -r)
34+
modprobe snd-hda-intel
35+
- mode: system
36+
script: |
37+
#!/bin/bash
38+
set -eux -o pipefail
39+
command -v aplay >/dev/null 2>&1 && exit 0
40+
apt-get install -y --no-install-recommends alsa-utils
41+
probes:
42+
- description: "alsa to be installed"
43+
script: |
44+
#!/bin/bash
45+
set -eux -o pipefail
46+
if ! timeout 30s bash -c "until command -v aplay >/dev/null 2>&1; do sleep 3; done"; then
47+
echo >&2 "alsa is not installed yet"
48+
exit 1
49+
fi
50+
hint: See "/var/log/cloud-init-output.log" in the guest
51+
message: |
52+
To get a list of all available audio devices:
53+
$ sudo aplay -L
54+
To test the audio device, use something like:
55+
$ sudo speaker-test -c2 -twav

pkg/qemu/qemu.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,19 @@ func qemuMachine(arch limayaml.Arch) string {
463463
return "virt"
464464
}
465465

466+
// audioDevice returns the default audio device.
467+
func audioDevice() string {
468+
switch runtime.GOOS {
469+
case "darwin":
470+
return "coreaudio"
471+
case "linux":
472+
return "pa" // pulseaudio
473+
case "windows":
474+
return "dsound"
475+
}
476+
return "oss"
477+
}
478+
466479
func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err error) {
467480
y := cfg.LimaYAML
468481
exe, args, err = Exe(*y.Arch)
@@ -762,6 +775,9 @@ func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err er
762775
id := "default"
763776
// audio device
764777
audiodev := *y.Audio.Device
778+
if audiodev == "default" {
779+
audiodev = audioDevice()
780+
}
765781
audiodev += fmt.Sprintf(",id=%s", id)
766782
args = append(args, "-audiodev", audiodev)
767783
// audio controller

pkg/vz/vm_darwin.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@ func attachFolderMounts(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineCo
574574
}
575575

576576
func attachAudio(driver *driver.BaseDriver, config *vz.VirtualMachineConfiguration) error {
577-
if *driver.Yaml.Audio.Device == "vz" {
577+
switch *driver.Yaml.Audio.Device {
578+
case "vz", "default":
578579
outputStream, err := vz.NewVirtioSoundDeviceHostOutputStreamConfiguration()
579580
if err != nil {
580581
return err
@@ -587,8 +588,12 @@ func attachAudio(driver *driver.BaseDriver, config *vz.VirtualMachineConfigurati
587588
config.SetAudioDevicesVirtualMachineConfiguration([]vz.AudioDeviceConfiguration{
588589
soundDeviceConfiguration,
589590
})
591+
return nil
592+
case "", "none":
593+
return nil
594+
default:
595+
return fmt.Errorf("unexpected audio device %q", *driver.Yaml.Audio.Device)
590596
}
591-
return nil
592597
}
593598

594599
func attachOtherDevices(_ *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration) error {

pkg/vz/vz_driver_darwin.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ func (l *LimaVzDriver) Validate() error {
131131
}
132132
}
133133

134-
audioDevice := *l.Yaml.Audio.Device
135-
if audioDevice != "" && audioDevice != "vz" {
136-
logrus.Warnf("field `audio.device` must be %q for VZ driver , got %q", "vz", audioDevice)
134+
switch audioDevice := *l.Yaml.Audio.Device; audioDevice {
135+
case "":
136+
case "vz", "default", "none":
137+
default:
138+
logrus.Warnf("field `audio.device` must be \"vz\", \"default\", or \"none\" for VZ driver, got %q", audioDevice)
137139
}
138140

139141
switch videoDisplay := *l.Yaml.Video.Display; videoDisplay {

0 commit comments

Comments
 (0)