qemu-char: add logfile facility to all chardev backends
PATCH v4 1/1 qemu-char: add logfile facility to all chardev backends
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
Currently providing such a feature forces the mgmt app to either provide 2 separate serial ports, one for logging boot messages and one for interactive console login, or to proxy all output via a separate service that can multiplex the two needs onto one serial port. While both are valid approaches, they each have their own downsides. The former causes confusion and extra setup work for VM admins creating disk images. The latter places an extra burden to re-implement much of the QEMU chardev backends logic in libvirt or even higher level mgmt apps and adds extra hops in the data transfer path.
A simpler approach that is satisfactory for many use cases is to allow the QEMU chardev backends to have a "logfile" property associated with them.
$QEMU -chardev socket,host=localhost,port=9000,\
server=on,nowait,id-charserial0,\
logfile=/var/log/libvirt/qemu/test-serial0.log
-device isa-serial,chardev=charserial0,id=serial0This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union. The ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate).
---
Changed in v4:
- Use error_abort when constructing chardev backend in gdbstub
- Describe new chardev backend options qemu-options.hx
Changed in v3:
- Pass ChardevCommon into qemu_chr_alloc() (Paolo)
- Add missing conversion of baum, msmouse and spice chardev (Paolo)
- Avoid accessing ChardevBackend->u.data and instead cast to ChardevCommon explicitly (Eric)
- Specify O_APPEND if O_TRUNC is not desired (Eric/Paolo)
backends/baum.c | 7 +- backends/msmouse.c | 6 +- gdbstub.c | 3 +- include/sysemu/char.h | 9 +- qapi-schema.json | 49 +++++++--- qemu-char.c | 243 +++++++++++++++++++++++++++++++++++++++++--------- qemu-options.hx | 48 +++++----- spice-qemu-char.c | 20 +++-- ui/console.c | 6 +- 9 files changed, 308 insertions(+), 83 deletions(-)
backends/baum.c +6 -1
@@ -566,6 +566,7 @@ static CharDriverState *chr_baum_init(const char *id, ChardevReturn *ret, Error **errp) { +ChardevCommon *common =qapi_ChardevDummy_base (backend->u.braille);BaumDriverState *baum;CharDriverState *chr;brlapi_handle_t *handle;
@@ -576,8 +577,12 @@ static CharDriverState *chr_baum_init(const char *id,#endif int tty; + chr =qemu_chr_alloc (common, errp); +if (!chr) { +return NULL ; + } baum =g_malloc0 (sizeof (BaumDriverState)); - baum->chr = chr =qemu_chr_alloc (); + baum->chr = chr; chr->opaque = baum; chr->chr_write = baum_write;
backends/msmouse.c +5 -1
@@ -68,9 +68,13 @@ static CharDriverState *qemu_chr_open_msmouse(const char *id, ChardevReturn *ret, Error **errp) { +ChardevCommon *common =qapi_ChardevDummy_base (backend->u.msmouse);CharDriverState *chr; - chr =qemu_chr_alloc (); + chr =qemu_chr_alloc (common, errp); +if (!chr) { +return NULL ; + } chr->chr_write = msmouse_chr_write; chr->chr_close = msmouse_chr_close; chr->explicit_be_open = true;
gdbstub.c +2 -1
@@ -1732,6 +1732,7 @@ int gdbserver_start(const char *device)char gdbstub_device_name[128 ];CharDriverState *chr =NULL ;CharDriverState *mon_chr; +ChardevCommon common = {0 };if (!device)return -1 ;
@@ -1768,7 +1769,7 @@ int gdbserver_start(const char *device)qemu_add_vm_change_state_handler (gdb_vm_state_change,NULL );/* Initialize a monitor terminal for gdb */ - mon_chr =qemu_chr_alloc (); + mon_chr =qemu_chr_alloc (&common, &error_abort); mon_chr->chr_write = gdb_monitor_write;monitor_init (mon_chr,0 ); }else {
include/sysemu/char.h +6 -3
@@ -77,6 +77,7 @@ struct CharDriverState {void *opaque;char *label;char *filename; +int logfd;int be_open;int fe_open;int explicit_fe_open;
@@ -89,13 +90,15 @@ struct CharDriverState { };/** -* @qemu_chr_alloc: +* qemu_chr_alloc: +* @backend: the common backend config +* @errp: pointer to a NULL-initialized error object * * Allocate and initialize a new CharDriverState. * -* Returns: a newly allocated CharDriverState. +* Returns: a newly allocated CharDriverState, or NULL on error. */ -CharDriverState *qemu_chr_alloc (void ); +CharDriverState *qemu_chr_alloc (ChardevCommon *backend,Error **errp);/** * @qemu_chr_new_from_opts:
qapi-schema.json +38 -11
121 lines not on this page · raw message
qemu-char.c +203 -40
607 lines not on this page · raw message
qemu-options.hx +28 -20
75 lines not on this page · raw message
spice-qemu-char.c +15 -5
@@ -271,13 +271,18 @@ static void spice_chr_accept_input(struct CharDriverState *chr) }static CharDriverState *chr_open (const char *subtype, -void (*set_fe_open)(struct CharDriverState *,int )) - +void (*set_fe_open)(struct CharDriverState *, +int ), +ChardevCommon *backend, +Error **errp) {CharDriverState *chr;SpiceCharDriver *s; - chr =qemu_chr_alloc (); + chr =qemu_chr_alloc (backend, errp); +if (!chr) { +return NULL ; + } s =g_malloc0 (sizeof (SpiceCharDriver)); s->chr = chr; s->active = false;
@@ -303,6 +308,7 @@ static CharDriverState *qemu_chr_open_spice_vmc(const char *id, {const char *type = backend->u.spicevmc->type;const char **psubtype =spice_server_char_device_recognized_subtypes (); +ChardevCommon *common =qapi_ChardevSpiceChannel_base (backend->u.spicevmc);for (; *psubtype !=NULL ; ++psubtype) {if (strcmp (type, *psubtype) ==0 ) {
@@ -315,7 +321,7 @@ static CharDriverState *qemu_chr_open_spice_vmc(const char *id,return NULL ; } -return chr_open (type, spice_vmc_set_fe_open); +return chr_open (type, spice_vmc_set_fe_open, common, errp); }#if SPICE_SERVER_VERSION >=0x000c02
@@ -325,6 +331,7 @@ static CharDriverState *qemu_chr_open_spice_port(const char *id, Error **errp) {const char *name = backend->u.spiceport->fqdn; +ChardevCommon *common =qapi_ChardevSpicePort_base (backend->u.spiceport);CharDriverState *chr;SpiceCharDriver *s;
@@ -333,7 +340,10 @@ static CharDriverState *qemu_chr_open_spice_port(const char *id,return NULL ; } - chr =chr_open ("port" , spice_port_set_fe_open); + chr =chr_open ("port" , spice_port_set_fe_open, common, errp); +if (!chr) { +return NULL ; + } s = chr->opaque; s->sin.portname =g_strdup (name);
ui/console.c +5 -1
@@ -1953,12 +1953,16 @@ static void text_console_do_init(CharDriverState *chr, DisplayState *ds)static CharDriverState *text_console_init (ChardevVC *vc,Error **errp) { +ChardevCommon *common =qapi_ChardevVC_base (vc);CharDriverState *chr;QemuConsole *s;unsigned width =0 ;unsigned height =0 ; - chr =qemu_chr_alloc (); + chr =qemu_chr_alloc (common, errp); +if (!chr) { +return NULL ; + }if (vc->has_width) { width = vc->width;
Signature · 1 line
2.5.0
Quoted 35 lines · Daniel P. Berrange
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union. The ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate).
Signed-off-by Daniel P. Berrange <[email protected]>---
qemu-char.c +8
-1-CharDriverState *qemu_chr_alloc (void ) +static void qemu_chr_free_common (CharDriverState *chr); + +CharDriverState *qemu_chr_alloc (ChardevCommon *backend,Error **errp) {CharDriverState *chr =g_malloc0 (sizeof (CharDriverState));qemu_mutex_init (&chr->chr_write_lock); + +if (backend->has_logfile) { +int flags =O_WRONLY |O_CREAT ; +if (backend->has_logappend && + backend->logappend) {
qemu-char.c static int sockaddr_to_str(char *dest, int max_len, · line 182
Quoted 10 lines
qemu-char.c +10
-0+ flags |=O_APPEND ; + }else { + flags |=O_TRUNC ; + } + chr->logfd =qemu_open (backend->logfile, flags,0666 ); +if (chr->logfd <0 ) { +error_setg_errno (errp, errno, +"Unable to open logfile %s" , + backend->logfile); +g_free (chr);
Are we leaking anything mutex-related by freeing chr without tearing down the just-initialized chr_write_lock?
qemu-char.c fail: · line 3606
Quoted 7 lines
qemu-char.c +6
-0+static void qemu_chr_parse_common (QemuOpts *opts,ChardevCommon *backend) +{ +const char *logfile =qemu_opt_get (opts,"logfile" ); + + backend->has_logfile = logfile !=NULL ; + backend->logfile = logfile ?g_strdup (logfile) :NULL ;
Isn't g_strdup(NULL) safe, such that you could write this as:
backend->logfile = g_strdup(logfile);
Otherwise looked okay; with the mutex question answered or fixed, you can add:
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 41 lines · Eric Blake
Quoted 40 lines · Daniel P. Berrange
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union. The ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate).
Signed-off-by Daniel P. Berrange <[email protected]>---
qemu-char.c +13
-1-CharDriverState *qemu_chr_alloc (void ) +static void qemu_chr_free_common (CharDriverState *chr); + +CharDriverState *qemu_chr_alloc (ChardevCommon *backend,Error **errp) {CharDriverState *chr =g_malloc0 (sizeof (CharDriverState));qemu_mutex_init (&chr->chr_write_lock); + +if (backend->has_logfile) { +int flags =O_WRONLY |O_CREAT ; +if (backend->has_logappend && + backend->logappend) { + flags |=O_APPEND ; + }else { + flags |=O_TRUNC ; + } + chr->logfd =qemu_open (backend->logfile, flags,0666 );
Quoted 8 lines
qemu-char.c static int sockaddr_to_str(char *dest, int max_len, · line 182
Quoted 5 lines
qemu-char.c +5
-0+if (chr->logfd <0 ) { +error_setg_errno (errp, errno, +"Unable to open logfile %s" , + backend->logfile); +g_free (chr);Are we leaking anything mutex-related by freeing chr without tearing down the just-initialized
chr_write_lock?
Not on Linux, but i think it would be a problem on Windows. This is pre-existing in the chardev code in general - qemu_chr_free() doesn't destroy the mutex.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Quoted 20 lines · Daniel P. Berrange
Quoted 19 lines · Eric Blake
Quoted 18 lines · Daniel P. Berrange
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union. The ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate).
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Daniel P. Berrange <[email protected]>
This patch fails make check, due to a segmentation fault that can be reproduced with "x86_64-softmmu/qemu-system-x86_64 -serial null".
I'm squashing in the fix:
qemu-char.c +5 -0
@@ -3827,7 +3827,12 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,error_propagate (errp, local_err); goto qapi_out; } + }else { +ChardevCommon *cc =g_new0 (ChardevCommon,1 ); +qemu_chr_parse_common (opts, cc); + backend->u.data = cc; } + ret =qmp_chardev_add (bid ? bid : id, backend, errp);if (!ret) { goto qapi_out;
Paolo