Skip to content

Commit 567dcc0

Browse files
author
Dorinda Bassey
committed
examples: Add vhost-user sound device support to chroot_vm
Add --vhost-user-snd option to chroot_vm example, allowing VMs to use external vhost-user sound backends for audio playback and capture. Usage: ./chroot_vm --vhost-user-snd=/path/to/sound.sock ... Note: In the guest, the ALSA default device may not work without additional configuration. Use explicit device specification: aplay -D hw:0,0 /path/to/audio.wav Or create /etc/asound.conf in the guest: defaults.pcm.card 0 defaults.pcm.device 0 pcm.!default { type hw card 0 device 0 } Tested with vhost-device-sound backend using PipeWire. Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
1 parent 1948f01 commit 567dcc0

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

examples/chroot_vm.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static void print_help(char *const name)
4040
" --net=NET_MODE Set network mode\n"
4141
" --passt-socket=PATH Instead of starting passt, connect to passt socket at PATH\n"
4242
" --vhost-user-rng=PATH Use vhost-user RNG backend at socket PATH\n"
43+
" --vhost-user-snd=PATH Use vhost-user sound backend at socket PATH\n"
4344
"NET_MODE can be either TSI (default) or PASST\n"
4445
"\n"
4546
"NEWROOT: the root directory of the vm\n"
@@ -56,6 +57,7 @@ static const struct option long_options[] = {
5657
{ "net_mode", required_argument, NULL, 'N' },
5758
{ "passt-socket", required_argument, NULL, 'P' },
5859
{ "vhost-user-rng", required_argument, NULL, 'V' },
60+
{ "vhost-user-snd", required_argument, NULL, 'S' },
5961
{ NULL, 0, NULL, 0 }
6062
};
6163

@@ -66,6 +68,7 @@ struct cmdline {
6668
enum net_mode net_mode;
6769
char const *passt_socket_path;
6870
char const *vhost_user_rng_socket;
71+
char const *vhost_user_snd_socket;
6972
char const *new_root;
7073
char *const *guest_argv;
7174
};
@@ -93,6 +96,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
9396
.net_mode = NET_MODE_TSI,
9497
.passt_socket_path = NULL,
9598
.vhost_user_rng_socket = NULL,
99+
.vhost_user_snd_socket = NULL,
96100
.new_root = NULL,
97101
.guest_argv = NULL,
98102
.log_target = KRUN_LOG_TARGET_DEFAULT,
@@ -131,6 +135,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
131135
case 'V':
132136
cmdline->vhost_user_rng_socket = optarg;
133137
break;
138+
case 'S':
139+
cmdline->vhost_user_snd_socket = optarg;
140+
break;
134141
case '?':
135142
return false;
136143
default:
@@ -270,6 +277,19 @@ int main(int argc, char *const argv[])
270277
printf("Using vhost-user RNG backend at %s (custom queue size: 512)\n", cmdline.vhost_user_rng_socket);
271278
}
272279

280+
// Configure vhost-user sound if requested
281+
if (cmdline.vhost_user_snd_socket != NULL) {
282+
if (err = krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_SND,
283+
cmdline.vhost_user_snd_socket, NULL,
284+
KRUN_VHOST_USER_SND_NUM_QUEUES,
285+
KRUN_VHOST_USER_SND_QUEUE_SIZES)) {
286+
errno = -err;
287+
perror("Error adding vhost-user sound device");
288+
return -1;
289+
}
290+
printf("Using vhost-user sound backend at %s\n", cmdline.vhost_user_snd_socket);
291+
}
292+
273293
// Raise RLIMIT_NOFILE to the maximum allowed to create some room for virtio-fs
274294
getrlimit(RLIMIT_NOFILE, &rlim);
275295
rlim.rlim_cur = rlim.rlim_max;

0 commit comments

Comments
 (0)