提交 d0d7708b 编写于 作者: D Daniel P. Berrange 提交者: Paolo Bonzini

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=serial0

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: NDaniel P. Berrange <berrange@redhat.com>
Message-Id: <1452516281-27519-1-git-send-email-berrange@redhat.com>
[Call qemu_chr_parse_common if cd->parse is NULL. - Paolo]
Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
上级 f1c17521
......@@ -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;
......
......@@ -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;
......
......@@ -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 {
......
......@@ -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:
......
......@@ -3093,6 +3093,21 @@
##
{ 'command': 'screendump', 'data': {'filename': 'str'} }
##
# @ChardevCommon:
#
# Configuration shared across all chardev backends
#
# @logfile: #optional The name of a logfile to save output
# @logappend: #optional true to append instead of truncate
# (default to false to truncate)
#
# Since: 2.6
##
{ 'struct': 'ChardevCommon', 'data': { '*logfile': 'str',
'*logappend': 'bool' } }
##
# @ChardevFile:
#
......@@ -3107,7 +3122,8 @@
##
{ 'struct': 'ChardevFile', 'data': { '*in' : 'str',
'out' : 'str',
'*append': 'bool' } }
'*append': 'bool' },
'base': 'ChardevCommon' }
##
# @ChardevHostdev:
......@@ -3120,7 +3136,8 @@
#
# Since: 1.4
##
{ 'struct': 'ChardevHostdev', 'data': { 'device' : 'str' } }
{ 'struct': 'ChardevHostdev', 'data': { 'device' : 'str' },
'base': 'ChardevCommon' }
##
# @ChardevSocket:
......@@ -3147,7 +3164,8 @@
'*wait' : 'bool',
'*nodelay' : 'bool',
'*telnet' : 'bool',
'*reconnect' : 'int' } }
'*reconnect' : 'int' },
'base': 'ChardevCommon' }
##
# @ChardevUdp:
......@@ -3160,7 +3178,8 @@
# Since: 1.5
##
{ 'struct': 'ChardevUdp', 'data': { 'remote' : 'SocketAddress',
'*local' : 'SocketAddress' } }
'*local' : 'SocketAddress' },
'base': 'ChardevCommon' }
##
# @ChardevMux:
......@@ -3171,7 +3190,8 @@
#
# Since: 1.5
##
{ 'struct': 'ChardevMux', 'data': { 'chardev' : 'str' } }
{ 'struct': 'ChardevMux', 'data': { 'chardev' : 'str' },
'base': 'ChardevCommon' }
##
# @ChardevStdio:
......@@ -3184,7 +3204,9 @@
#
# Since: 1.5
##
{ 'struct': 'ChardevStdio', 'data': { '*signal' : 'bool' } }
{ 'struct': 'ChardevStdio', 'data': { '*signal' : 'bool' },
'base': 'ChardevCommon' }
##
# @ChardevSpiceChannel:
......@@ -3195,7 +3217,8 @@
#
# Since: 1.5
##
{ 'struct': 'ChardevSpiceChannel', 'data': { 'type' : 'str' } }
{ 'struct': 'ChardevSpiceChannel', 'data': { 'type' : 'str' },
'base': 'ChardevCommon' }
##
# @ChardevSpicePort:
......@@ -3206,7 +3229,8 @@
#
# Since: 1.5
##
{ 'struct': 'ChardevSpicePort', 'data': { 'fqdn' : 'str' } }
{ 'struct': 'ChardevSpicePort', 'data': { 'fqdn' : 'str' },
'base': 'ChardevCommon' }
##
# @ChardevVC:
......@@ -3223,7 +3247,8 @@
{ 'struct': 'ChardevVC', 'data': { '*width' : 'int',
'*height' : 'int',
'*cols' : 'int',
'*rows' : 'int' } }
'*rows' : 'int' },
'base': 'ChardevCommon' }
##
# @ChardevRingbuf:
......@@ -3234,7 +3259,8 @@
#
# Since: 1.5
##
{ 'struct': 'ChardevRingbuf', 'data': { '*size' : 'int' } }
{ 'struct': 'ChardevRingbuf', 'data': { '*size' : 'int' },
'base': 'ChardevCommon' }
##
# @ChardevBackend:
......@@ -3243,7 +3269,8 @@
#
# Since: 1.4 (testdev since 2.2)
##
{ 'struct': 'ChardevDummy', 'data': { } }
{ 'struct': 'ChardevDummy', 'data': { },
'base': 'ChardevCommon' }
{ 'union': 'ChardevBackend', 'data': { 'file' : 'ChardevFile',
'serial' : 'ChardevHostdev',
......
此差异已折叠。
......@@ -2089,40 +2089,43 @@ The general form of a character device option is:
ETEXI
DEF("chardev", HAS_ARG, QEMU_OPTION_chardev,
"-chardev null,id=id[,mux=on|off]\n"
"-chardev null,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
"-chardev socket,id=id[,host=host],port=port[,to=to][,ipv4][,ipv6][,nodelay][,reconnect=seconds]\n"
" [,server][,nowait][,telnet][,reconnect=seconds][,mux=on|off] (tcp)\n"
"-chardev socket,id=id,path=path[,server][,nowait][,telnet][,reconnect=seconds][,mux=on|off] (unix)\n"
" [,server][,nowait][,telnet][,reconnect=seconds][,mux=on|off]\n"
" [,logfile=PATH][,logappend=on|off] (tcp)\n"
"-chardev socket,id=id,path=path[,server][,nowait][,telnet][,reconnect=seconds]\n"
" [,mux=on|off][,logfile=PATH][,logappend=on|off] (unix)\n"
"-chardev udp,id=id[,host=host],port=port[,localaddr=localaddr]\n"
" [,localport=localport][,ipv4][,ipv6][,mux=on|off]\n"
"-chardev msmouse,id=id[,mux=on|off]\n"
" [,logfile=PATH][,logappend=on|off]\n"
"-chardev msmouse,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
"-chardev vc,id=id[[,width=width][,height=height]][[,cols=cols][,rows=rows]]\n"
" [,mux=on|off]\n"
"-chardev ringbuf,id=id[,size=size]\n"
"-chardev file,id=id,path=path[,mux=on|off]\n"
"-chardev pipe,id=id,path=path[,mux=on|off]\n"
" [,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
"-chardev ringbuf,id=id[,size=size][,logfile=PATH][,logappend=on|off]\n"
"-chardev file,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
"-chardev pipe,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
#ifdef _WIN32
"-chardev console,id=id[,mux=on|off]\n"
"-chardev serial,id=id,path=path[,mux=on|off]\n"
"-chardev console,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
"-chardev serial,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
#else
"-chardev pty,id=id[,mux=on|off]\n"
"-chardev stdio,id=id[,mux=on|off][,signal=on|off]\n"
"-chardev pty,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
"-chardev stdio,id=id[,mux=on|off][,signal=on|off][,logfile=PATH][,logappend=on|off]\n"
#endif
#ifdef CONFIG_BRLAPI
"-chardev braille,id=id[,mux=on|off]\n"
"-chardev braille,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
#endif
#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|| defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
"-chardev serial,id=id,path=path[,mux=on|off]\n"
"-chardev tty,id=id,path=path[,mux=on|off]\n"
"-chardev serial,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
"-chardev tty,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
#endif
#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
"-chardev parallel,id=id,path=path[,mux=on|off]\n"
"-chardev parport,id=id,path=path[,mux=on|off]\n"
"-chardev parallel,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
"-chardev parport,id=id,path=path[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
#endif
#if defined(CONFIG_SPICE)
"-chardev spicevmc,id=id,name=name[,debug=debug]\n"
"-chardev spiceport,id=id,name=name[,debug=debug]\n"
"-chardev spicevmc,id=id,name=name[,debug=debug][,logfile=PATH][,logappend=on|off]\n"
"-chardev spiceport,id=id,name=name[,debug=debug][,logfile=PATH][,logappend=on|off]\n"
#endif
, QEMU_ARCH_ALL
)
......@@ -2158,7 +2161,12 @@ A character device may be used in multiplexing mode by multiple front-ends.
The key sequence of @key{Control-a} and @key{c} will rotate the input focus
between attached front-ends. Specify @option{mux=on} to enable this mode.
Options to each backend are described below.
Every backend supports the @option{logfile} option, which supplies the path
to a file to record all data transmitted via the backend. The @option{logappend}
option controls whether the log file will be truncated or appended to when
opened.
Further options to each backend are described below.
@item -chardev null ,id=@var{id}
A void device. This device will not emit any data, and will drop any data it
......
......@@ -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);
......
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册