Skip to content

Conversation

jakecorrenti
Copy link
Member

Currently, libkrun forces the user to manually change the source code if they are not building with EFI=1 and want to use the serial console instead of the virtio-console.

Provide an API, krun_set_serial_console, allowing the user to specify whether the serial console should be enabled. The serial console remains the default on EFI=1 use cases.

tylerfanelli
tylerfanelli previously approved these changes Jun 26, 2025
Copy link
Member

@tylerfanelli tylerfanelli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, though I wonder if it makes sense for this to be krun_set_console and the input to be an enum with two supported options:

KRUN_CONSOLE_SERIAL
KRUN_CONSOLE_VIRTIO

Leaves open the possibility of adding other options. Just something to consider.

@slp
Copy link
Collaborator

slp commented Jun 26, 2025

LGTM, though I wonder if it makes sense for this to be krun_set_console and the input to be an enum with two supported options:

KRUN_CONSOLE_SERIAL
KRUN_CONSOLE_VIRTIO

Leaves open the possibility of adding other options. Just something to consider.

It's even more complex. You might want to enable one of them, or both. But, in addition of that, for the non-EFI case and when the kernel command line is not specified with krun_set_kernel, we need to specify which one is going to be put along with console= in the kernel command line.

Perhaps something like...

KRUN_CONSOLE_SERIAL
KRUN_CONSOLE_VIRTIO
KRUN_CONSOLE_BOTH_DEFAULT_SERIAL
KRUN_CONSOLE_BOTH_DEFAULT_VIRTIO

...?

Copy link
Collaborator

@slp slp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rethink about the configuration options.

@mtjhrc
Copy link
Collaborator

mtjhrc commented Jun 26, 2025

LGTM, though I wonder if it makes sense for this to be krun_set_console and the input to be an enum with two supported options:

KRUN_CONSOLE_SERIAL
KRUN_CONSOLE_VIRTIO

Leaves open the possibility of adding other options. Just something to consider.

It's even more complex. You might want to enable one of them, or both. But, in addition of that, for the non-EFI case and when the kernel command line is not specified with krun_set_kernel, we need to specify which one is going to be put along with console= in the kernel command line.

Perhaps something like...

KRUN_CONSOLE_SERIAL
KRUN_CONSOLE_VIRTIO
KRUN_CONSOLE_BOTH_DEFAULT_SERIAL
KRUN_CONSOLE_BOTH_DEFAULT_VIRTIO

Yeah I agree we should be more flexible whether you want both, but I'm not really a fan of having an enum like this.

I feel like ideally we should not have a console at all and let the user attach SERIAL/VIRTIO console(s) explicitly.

Perhaps we could retrofit it to the current API... Can we maybe add an krun_no_implicit_consoles(ctx)? And then add functions for explicitly attaching the consoles? In the v2 API the krun_no_implicit_consoles(ctx) could then be the default.
Perhaps something like this:

int32_t ctx = krun_create_ctx();
krun_no_implicit_consoles(ctx);
krun_add_serial_console(/*console id*/ 1, ...);
krun_add_virtio_console(/*console id*/ 2, ...);
krun_set_kernel_console(/*console id*/ 1); // console= in the kernel command line.

This would also make future API for attaching multiple virtio-console(s) nicer.

Though this requires bigger changes in libkrun, I feel like it makes the API much more sane for the users (and extensible for us!).

@slp
Copy link
Collaborator

slp commented Jun 26, 2025

Perhaps we could retrofit it to the current API... Can we maybe add an krun_no_implicit_consoles(ctx)? And then add functions for explicitly attaching the consoles? In the v2 API the krun_no_implicit_consoles(ctx) could then be the default. Perhaps something like this:

int32_t ctx = krun_create_ctx();
krun_no_implicit_consoles(ctx);
krun_add_serial_console(/*console id*/ 1, ...);
krun_add_virtio_console(/*console id*/ 2, ...);
krun_set_kernel_console(/*console id*/ 1); // console= in the kernel command line.

This would also make future API for attaching multiple virtio-console(s) nicer.

Though this requires bigger changes in libkrun, I feel like it makes the API much more sane for the users (and extensible for us!).

Yes, this is probably the way. We know we're going to need a much more flexible console configuration for Android support (Android is a heavy user of virtio-console to communicate the guests with various processes in the host), and this would be a step in the right direction.

That said, perhaps it'd be safer to wait a bit to see exactly what the requirements are on the Android side and then propose an API?

@mtjhrc
Copy link
Collaborator

mtjhrc commented Jun 26, 2025

That said, perhaps it'd be safer to wait a bit to see exactly what the requirements are on the Android side and then propose an API?

Yeah maybe, but I don't think it will really be a problem. For now we could just have the same functionality as we already do just make it more explicit. We can also have multiple krun_add_virtio_console_* functions with different arguments. The current console auto-configuration (which automatically configures the stdout/stdin/stderr as separate ports) could still exist but just be e.g. krun_add_virtio_console_default(int console_id).

Details of how to configure the console(s) for Android can still be figured out later. We could add a function like krun_add_virtio_serial(..) (I don't like the misleading QEMU name, but not sure how to call it). If we need to configure a multiport console with specific ports we can just have e.g. krun_add_virtio_console_multiport(int port_id) and configure the ports using multiple calls to krun_add_virtio_console_port(int console_id, const char* name, int port_fd)...

@jakecorrenti jakecorrenti marked this pull request as draft July 17, 2025 19:39
@jakecorrenti jakecorrenti force-pushed the serial-console-api branch 4 times, most recently from 0d63be1 to 25d3650 Compare July 28, 2025 21:10
@jakecorrenti jakecorrenti changed the title include: vmm: add krun_set_serial_console API include: vmm: add granular console APIs Jul 31, 2025
@jakecorrenti jakecorrenti force-pushed the serial-console-api branch 10 times, most recently from a171452 to 0b7aa34 Compare August 6, 2025 18:32
@jakecorrenti jakecorrenti force-pushed the serial-console-api branch 2 times, most recently from 59c54a0 to e337372 Compare August 6, 2025 18:55
@jakecorrenti jakecorrenti marked this pull request as ready for review August 6, 2025 19:29
@jakecorrenti
Copy link
Member Author

This should work. I did my best to test it on macOS and x86. I spent more time than I'd like to admit going back and forth on how this should all look, but I tried making a sane starting point that allows for future expansion (for Android, for example). If it doesn't work for someone, or I completely over looked something, let me know :)

@jakecorrenti jakecorrenti changed the title include: vmm: add granular console APIs Add granular console APIs Aug 11, 2025
@jakecorrenti jakecorrenti force-pushed the serial-console-api branch 4 times, most recently from 413b2b8 to 4523fe0 Compare August 13, 2025 12:56
Adds a new `krun_disable_implicit_console` API. This allows the user to
prevent libkrun from implicitly adding a console to their guest.

Signed-off-by: Jake Correnti <[email protected]>
Adds the `krun_set_kernel_console` API, which allows the user to specify
the value of `console=` in the kernel commandline.

Signed-off-by: Jake Correnti <[email protected]>
Copy link
Collaborator

@mtjhrc mtjhrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I added a suggestion to clarify the doc comment a bit more though

Adds the new `krun_add_virtio_console_default` and
`krun_add_serial_console_default` APIs. They allow the user to manually
create a console device that will then be attached to the guest.

Signed-off-by: Jake Correnti <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants