Skip to content

Commit 263f36e

Browse files
Dorinda Basseyslp
authored andcommitted
libkrun: Add API constants and example for vhost-user console
Add public API constants for vhost-user console devices and example usage in chroot_vm. The underlying support already exists via the generic VhostUserDevice wrapper. Console devices require 4 queues for multiport support (receiveq, transmitq, control receiveq, control transmitq). Example integration: - Added --vhost-user-console option to chroot_vm - Available as /dev/hvc1 in the guest (implicit console uses hvc0) - Can be used with vhost-device-console for remote console access Example workflow: Terminal 1: Start vhost-device-console backend vhost-device-console --socket-path=/tmp/console.sock \ --socket-count=1 --tcp-port=12346 --backend=network Terminal 2: Connect to console (before starting VM) nc localhost 12346 Terminal 3: Start VM with vhost-user console chroot_vm --vhost-user-console=/tmp/console.sock0 / /bin/sh In guest: Test with echo "hello" > /dev/hvc1 Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
1 parent 609d5c9 commit 263f36e

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

examples/chroot_vm.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static void print_help(char *const name)
4343
" --vhost-user-snd=PATH Use vhost-user sound backend at socket PATH\n"
4444
" --vhost-user-vsock=PATH Use vhost-user vsock backend at socket PATH\n"
4545
" --vhost-user-can=PATH Use vhost-user CAN backend at socket PATH\n"
46+
" --vhost-user-console=PATH Use vhost-user console backend at socket PATH\n"
4647
"NET_MODE can be either TSI (default) or PASST\n"
4748
"\n"
4849
"NEWROOT: the root directory of the vm\n"
@@ -72,6 +73,7 @@ static const struct option long_options[] = {
7273
{ "vhost-user-snd", required_argument, NULL, 'S' },
7374
{ "vhost-user-vsock", required_argument, NULL, 'K' },
7475
{ "vhost-user-can", required_argument, NULL, 'A' },
76+
{ "vhost-user-console", required_argument, NULL, 'O' },
7577
{ NULL, 0, NULL, 0 }
7678
};
7779

@@ -85,6 +87,7 @@ struct cmdline {
8587
char const *vhost_user_snd_socket;
8688
char const *vhost_user_vsock_socket;
8789
char const *vhost_user_can_socket;
90+
char const *vhost_user_console_socket;
8891
char const *new_root;
8992
char *const *guest_argv;
9093
};
@@ -115,6 +118,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
115118
.vhost_user_snd_socket = NULL,
116119
.vhost_user_vsock_socket = NULL,
117120
.vhost_user_can_socket = NULL,
121+
.vhost_user_console_socket = NULL,
118122
.new_root = NULL,
119123
.guest_argv = NULL,
120124
.log_target = KRUN_LOG_TARGET_DEFAULT,
@@ -162,6 +166,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
162166
case 'A':
163167
cmdline->vhost_user_can_socket = optarg;
164168
break;
169+
case 'O':
170+
cmdline->vhost_user_console_socket = optarg;
171+
break;
165172
case '?':
166173
return false;
167174
default:
@@ -342,6 +349,19 @@ int main(int argc, char *const argv[])
342349
printf("Using vhost-user CAN backend at %s\n", cmdline.vhost_user_can_socket);
343350
}
344351

352+
// Configure vhost-user console if requested
353+
if (cmdline.vhost_user_console_socket != NULL) {
354+
if (!check_krun_error(krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_CONSOLE,
355+
cmdline.vhost_user_console_socket, NULL,
356+
KRUN_VHOST_USER_CONSOLE_NUM_QUEUES,
357+
KRUN_VHOST_USER_CONSOLE_QUEUE_SIZES),
358+
"Error adding vhost-user console device")) {
359+
return -1;
360+
}
361+
printf("Using vhost-user console backend at %s (available as /dev/hvc1 in guest)\n", cmdline.vhost_user_console_socket);
362+
printf("Test with: echo 'hello' > /dev/hvc1\n");
363+
}
364+
345365
// Raise RLIMIT_NOFILE to the maximum allowed to create some room for virtio-fs
346366
getrlimit(RLIMIT_NOFILE, &rlim);
347367
rlim.rlim_cur = rlim.rlim_max;

include/libkrun.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,20 @@ int32_t krun_set_snd_device(uint32_t ctx_id, bool enable);
751751
* Vhost-user device types.
752752
* These correspond to virtio device type IDs for devices.
753753
*/
754+
#define KRUN_VIRTIO_DEVICE_CONSOLE 3
754755
#define KRUN_VIRTIO_DEVICE_RNG 4
755756
#define KRUN_VIRTIO_DEVICE_VSOCK 19
756757
#define KRUN_VIRTIO_DEVICE_SND 25
757758
#define KRUN_VIRTIO_DEVICE_CAN 36
758759

760+
/**
761+
* Vhost-user console device default queue configuration.
762+
* Console device uses 4 queues for multiport support:
763+
* receiveq (idx 0), transmitq (idx 1), control receiveq (idx 2), control transmitq (idx 3).
764+
*/
765+
#define KRUN_VHOST_USER_CONSOLE_NUM_QUEUES 4
766+
#define KRUN_VHOST_USER_CONSOLE_QUEUE_SIZES ((uint16_t[]){128, 128, 64, 64})
767+
759768
/**
760769
* Vhost-user RNG device default queue configuration.
761770
* Use these when you want explicit defaults instead of auto-detection.

0 commit comments

Comments
 (0)