Skip to content

Commit 319d94e

Browse files
author
Dorinda Bassey
committed
examples: Add vhost-user RNG support to chroot_vm
Adds --vhost-user-rng command line option to specify a vhost-user RNG backend socket path. When provided, the VM uses the external vhost-user RNG device instead of the built-in virtio-rng implementation. Example usage: ./examples/chroot_vm \ --vhost-user-rng=/tmp/vhost-rng.sock0 \ / /bin/sh -c "head -c 32 /dev/hwrng | xxd" Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
1 parent 6b7f1df commit 319d94e

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

examples/chroot_vm.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ static void print_help(char *const name)
3838
" --log=PATH Write libkrun log to file or named pipe at PATH\n"
3939
" --color-log=PATH Write libkrun log to file or named pipe at PATH, use color\n"
4040
" --net=NET_MODE Set network mode\n"
41-
" --passt-socket=PATH Instead of starting passt, connect to passt socket at PATH"
41+
" --passt-socket=PATH Instead of starting passt, connect to passt socket at PATH\n"
42+
" --vhost-user-rng=PATH Use vhost-user RNG backend at socket PATH\n"
4243
"NET_MODE can be either TSI (default) or PASST\n"
4344
"\n"
4445
"NEWROOT: the root directory of the vm\n"
@@ -48,12 +49,23 @@ static void print_help(char *const name)
4849
);
4950
}
5051

52+
static bool check_krun_error(int err, const char *msg)
53+
{
54+
if (err) {
55+
errno = -err;
56+
perror(msg);
57+
return false;
58+
}
59+
return true;
60+
}
61+
5162
static const struct option long_options[] = {
5263
{ "help", no_argument, NULL, 'h' },
5364
{ "log", required_argument, NULL, 'L' },
5465
{ "color-log", required_argument, NULL, 'C' },
5566
{ "net_mode", required_argument, NULL, 'N' },
5667
{ "passt-socket", required_argument, NULL, 'P' },
68+
{ "vhost-user-rng", required_argument, NULL, 'V' },
5769
{ NULL, 0, NULL, 0 }
5870
};
5971

@@ -63,6 +75,7 @@ struct cmdline {
6375
uint32_t log_style;
6476
enum net_mode net_mode;
6577
char const *passt_socket_path;
78+
char const *vhost_user_rng_socket;
6679
char const *new_root;
6780
char *const *guest_argv;
6881
};
@@ -89,6 +102,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
89102
.show_help = false,
90103
.net_mode = NET_MODE_TSI,
91104
.passt_socket_path = NULL,
105+
.vhost_user_rng_socket = NULL,
92106
.new_root = NULL,
93107
.guest_argv = NULL,
94108
.log_target = KRUN_LOG_TARGET_DEFAULT,
@@ -124,6 +138,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
124138
case 'P':
125139
cmdline->passt_socket_path = optarg;
126140
break;
141+
case 'V':
142+
cmdline->vhost_user_rng_socket = optarg;
143+
break;
127144
case '?':
128145
return false;
129146
default:
@@ -249,6 +266,19 @@ int main(int argc, char *const argv[])
249266
return -1;
250267
}
251268

269+
// Configure vhost-user RNG if requested
270+
if (cmdline.vhost_user_rng_socket != NULL) {
271+
// Test sentinel-terminated array: auto-detect queue count, use custom size
272+
uint16_t custom_sizes[] = {512, 0}; // 0 = sentinel terminator
273+
274+
if (!check_krun_error(krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_RNG,
275+
cmdline.vhost_user_rng_socket, NULL, 0, custom_sizes),
276+
"Error adding vhost-user RNG device")) {
277+
return -1;
278+
}
279+
printf("Using vhost-user RNG backend at %s (custom queue size: 512)\n", cmdline.vhost_user_rng_socket);
280+
}
281+
252282
// Raise RLIMIT_NOFILE to the maximum allowed to create some room for virtio-fs
253283
getrlimit(RLIMIT_NOFILE, &rlim);
254284
rlim.rlim_cur = rlim.rlim_max;

0 commit comments

Comments
 (0)