Skip to content

Commit 3aef948

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 9e0456d commit 3aef948

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

examples/chroot_vm.c

Lines changed: 19 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"
@@ -66,6 +67,7 @@ static const struct option long_options[] = {
6667
{ "net_mode", required_argument, NULL, 'N' },
6768
{ "passt-socket", required_argument, NULL, 'P' },
6869
{ "vhost-user-rng", required_argument, NULL, 'V' },
70+
{ "vhost-user-snd", required_argument, NULL, 'S' },
6971
{ NULL, 0, NULL, 0 }
7072
};
7173

@@ -76,6 +78,7 @@ struct cmdline {
7678
enum net_mode net_mode;
7779
char const *passt_socket_path;
7880
char const *vhost_user_rng_socket;
81+
char const *vhost_user_snd_socket;
7982
char const *new_root;
8083
char *const *guest_argv;
8184
};
@@ -103,6 +106,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
103106
.net_mode = NET_MODE_TSI,
104107
.passt_socket_path = NULL,
105108
.vhost_user_rng_socket = NULL,
109+
.vhost_user_snd_socket = NULL,
106110
.new_root = NULL,
107111
.guest_argv = NULL,
108112
.log_target = KRUN_LOG_TARGET_DEFAULT,
@@ -141,6 +145,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
141145
case 'V':
142146
cmdline->vhost_user_rng_socket = optarg;
143147
break;
148+
case 'S':
149+
cmdline->vhost_user_snd_socket = optarg;
150+
break;
144151
case '?':
145152
return false;
146153
default:
@@ -279,6 +286,18 @@ int main(int argc, char *const argv[])
279286
printf("Using vhost-user RNG backend at %s (custom queue size: 512)\n", cmdline.vhost_user_rng_socket);
280287
}
281288

289+
// Configure vhost-user sound if requested
290+
if (cmdline.vhost_user_snd_socket != NULL) {
291+
if (!check_krun_error(krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_SND,
292+
cmdline.vhost_user_snd_socket, NULL,
293+
KRUN_VHOST_USER_SND_NUM_QUEUES,
294+
KRUN_VHOST_USER_SND_QUEUE_SIZES),
295+
"Error adding vhost-user sound device")) {
296+
return -1;
297+
}
298+
printf("Using vhost-user sound backend at %s\n", cmdline.vhost_user_snd_socket);
299+
}
300+
282301
// Raise RLIMIT_NOFILE to the maximum allowed to create some room for virtio-fs
283302
getrlimit(RLIMIT_NOFILE, &rlim);
284303
rlim.rlim_cur = rlim.rlim_max;

0 commit comments

Comments
 (0)