debuggers.hg

changeset 22040:2e08ec0028e4

tools: xenconsole[d] and libxl: multiple console support

This patch implements the new protocol for handling pv consoles and
emulated serials as described in the document docs/misc/console.txt.

The changes are:

- xenconsoled: do not write the pty under serial in xenstore if
xenconsoled is handling a consolepath;

- xenconsole: implement support for an explicit console type parameter;
the parameter can be "pv", to specify that the user wants to
connect to a pv console, or "serial", to specify that the user wants to
connect to an emulated serial. If the type parameter hasn't been
specified be the user, xenconsole tries to guess which type of console
it has to connect to, defaulting to pv console for pv guests and
emulated serial for hvm guests.

- xenconsole: use the new xenstore paths;

- libxl: rename libxl_console_constype to libxl_console_consback:
constype is used to to specify whether qemu or xenconsoled provides the
backend, so I renamed it to libxl_console_consback to make it more
obvious that we are talking about backends;

- libxl: add a new libxl_console_constype to specify if the console is
an emulated serial or a pv console;

- libxl: support the new xenconsole "type" command line parameter;

- libxl: use the "output" node under console in xenstore to tell qemu
where do we want the output of this pv console to go;

- remove the legacy "serialpath" from xenconsoled altogether

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>


Also: update the QEMU_TAG to pull in the qemu part of these changes.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
date Mon Aug 16 13:32:04 2010 +0100 (2010-08-16)
parents a55625b65336
children be41e8ee8052
files Config.mk docs/misc/console.txt tools/console/client/main.c tools/console/daemon/io.c tools/libxl/libxl.c tools/libxl/libxl.h tools/libxl/xl_cmdimpl.c tools/libxl/xl_cmdtable.c tools/ocaml/libs/xl/xl_stubs.c
line diff
     1.1 --- a/Config.mk	Mon Aug 16 09:54:02 2010 +0100
     1.2 +++ b/Config.mk	Mon Aug 16 13:32:04 2010 +0100
     1.3 @@ -167,9 +167,9 @@ endif
     1.4  # CONFIG_QEMU ?= ../qemu-xen.git
     1.5  CONFIG_QEMU ?= $(QEMU_REMOTE)
     1.6  
     1.7 -QEMU_TAG ?= 833e7e9a4c95739429dc0c803bbbf2346f9897fd
     1.8 -# Thu Jul 8 17:33:29 2010 +0100
     1.9 -# Move the xenfb pointer handler to the connected method
    1.10 +QEMU_TAG ?= 466608f3a32e1f9808acdf832a5843af37e5fcec
    1.11 +# Mon Aug 16 13:29:36 2010 +0100
    1.12 +# support multiple pv consoles for hvm guests too
    1.13  
    1.14  # Optional components
    1.15  XENSTAT_XENTOP     ?= y
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/docs/misc/console.txt	Mon Aug 16 13:32:04 2010 +0100
     2.3 @@ -0,0 +1,91 @@
     2.4 +Xen PV Console notes
     2.5 +------------------------------------------------------------------------
     2.6 +                                                      Stefano Stabellini
     2.7 +                                        stefano.stabellini@eu.citrix.com
     2.8 +
     2.9 +
    2.10 +Xen traditionally provided a single pv console to pv guests, storing the
    2.11 +relevant information in xenstore under /local/domain/$DOMID/console.
    2.12 +
    2.13 +Now many years after the introduction of the pv console we have
    2.14 +multiple pv consoles support for pv and hvm guests; multiple pv
    2.15 +console backends (qemu and xenconsoled) and emulated serial cards too.
    2.16 +
    2.17 +This document tries to describe how the whole system works and how the
    2.18 +different components interact with each others.
    2.19 +
    2.20 +The first PV console path in xenstore remains:
    2.21 +
    2.22 +/local/domain/$DOMID/console
    2.23 +
    2.24 +the other PV consoles follow the conventional xenstore device path and
    2.25 +live in:
    2.26 +
    2.27 +/local/domain/$DOMID/device/console/$DEVID.
    2.28 +
    2.29 +The output of a PV console, whether it should be a file, a pty, a
    2.30 +socket, or something else, is specified by the toolstack in the xenstore
    2.31 +node "output", under the relevant console section.
    2.32 +For example:
    2.33 +
    2.34 +# xenstore-read /local/domain/26/device/console/1/output
    2.35 +pty
    2.36 +
    2.37 +The backend chosen for a particular console is specified by the
    2.38 +toolstack in the "type" node on xenstore, under the relevant console
    2.39 +section.
    2.40 +For example:
    2.41 +
    2.42 +# xenstore-read /local/domain/26/console/type
    2.43 +xenconsoled
    2.44 +
    2.45 +The supported values are only xenconsoled or ioemu; xenconsoled has
    2.46 +several limitations: it can only be used for the first PV console and it
    2.47 +can only have a pty as output.
    2.48 +
    2.49 +If the output is a pty, backends write the device name to the "tty" node
    2.50 +in xenstore under the relevant console path.
    2.51 +
    2.52 +Emulated serials are provided by qemu-dm only to hvm guests; the number
    2.53 +of emulated serials depends on how many "-serial" command line options
    2.54 +are given to qemu. The output of a serial is specified as argument to
    2.55 +the -serial command line option to qemu. Qemu writes the tty name to
    2.56 +xenstore in the following path:
    2.57 +
    2.58 +/local/domain/$DOMID/serial/$SERIAL_NUM/tty
    2.59 +
    2.60 +
    2.61 +xenconsole is the tool to connect to a PV console or an emulated serial
    2.62 +that has a pty as output. Xenconsole takes a domid as parameter plus an
    2.63 +optional console type (pv for PV consoles or serial for emulated
    2.64 +serials) and console number. Depending on the type and console
    2.65 +number, xenconsole will look for the tty node in different xenstore
    2.66 +paths, as described above.  If the user doesn't specify the console type
    2.67 +xenconsole will try to guess: if the guest is a pv guest it defaults to
    2.68 +PV console, if the guest is an hvm guest it defaults to emulated serial.
    2.69 +
    2.70 +By default xl creates a pv console for hvm guests, plus an emulated
    2.71 +serial if the user specified 'serial = "pty"' in the VM config file.
    2.72 +Considering that xenconsole defaults to emulated serials for hvm guests,
    2.73 +executing xl create -c "domain" causes xenconsole to attach to the
    2.74 +emulated serial tty. This is most probably what the user wanted because
    2.75 +currently no bootloaders support xen pv consoles so the only way to
    2.76 +interact with a bootloader like grub over a console is to use the
    2.77 +emulated serial.
    2.78 +However the pv console is still easy to use with Linux PV on HVM guests:
    2.79 +the user just need to pass "console=hvc0" to the kernel command line and
    2.80 +then execute "xl console -t pv <domain>" to connect to it.
    2.81 +
    2.82 +When using stubdoms the serial cards are still emulated by qemu (this
    2.83 +time running in the stubdom), the number of serial cards and where the
    2.84 +output goes is still specified using qemu command line options.
    2.85 +The difference is that for each emulated serial card there must be a pv
    2.86 +console connection between the stubdom and dom0 to export the serial
    2.87 +output from the stubdom to dom0. The pv console backend for stubdom's pv
    2.88 +consoles is always ioemu because multiple pv consoles support is a
    2.89 +requirement in this case, considering that minios has its own pv console
    2.90 +too. In order to simplify the setup when using stubdoms the hvm guest
    2.91 +can only have one pv console with xenstored as backend (the stubdom
    2.92 +could provide pv console backends to the hvm guest but then it would
    2.93 +need another pv console connection for each console backend to export
    2.94 +the pty to dom0).
     3.1 --- a/tools/console/client/main.c	Mon Aug 16 09:54:02 2010 +0100
     3.2 +++ b/tools/console/client/main.c	Mon Aug 16 13:32:04 2010 +0100
     3.3 @@ -40,6 +40,7 @@
     3.4  #endif
     3.5  
     3.6  #include "xs.h"
     3.7 +#include "xenctrl.h"
     3.8  
     3.9  #define ESCAPE_CHARACTER 0x1d
    3.10  
    3.11 @@ -254,6 +255,12 @@ static int console_loop(int fd, struct x
    3.12  	return 0;
    3.13  }
    3.14  
    3.15 +typedef enum {
    3.16 +       CONSOLE_INVAL,
    3.17 +       CONSOLE_PV,
    3.18 +       CONSOLE_SERIAL,
    3.19 +} console_type;
    3.20 +
    3.21  int main(int argc, char **argv)
    3.22  {
    3.23  	struct termios attr;
    3.24 @@ -263,6 +270,7 @@ int main(int argc, char **argv)
    3.25  	unsigned int num = 0;
    3.26  	int opt_ind=0;
    3.27  	struct option lopt[] = {
    3.28 +		{ "type",     1, 0, 't' },
    3.29  		{ "num",     1, 0, 'n' },
    3.30  		{ "help",    0, 0, 'h' },
    3.31  		{ 0 },
    3.32 @@ -272,6 +280,7 @@ int main(int argc, char **argv)
    3.33  	int spty, xsfd;
    3.34  	struct xs_handle *xs;
    3.35  	char *end;
    3.36 +	console_type type = CONSOLE_INVAL;
    3.37  
    3.38  	while((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
    3.39  		switch(ch) {
    3.40 @@ -282,6 +291,17 @@ int main(int argc, char **argv)
    3.41  		case 'n':
    3.42  			num = atoi(optarg);
    3.43  			break;
    3.44 +		case 't':
    3.45 +			if (!strcmp(optarg, "serial"))
    3.46 +				type = CONSOLE_SERIAL;
    3.47 +			else if (!strcmp(optarg, "pv"))
    3.48 +				type = CONSOLE_PV;
    3.49 +			else {
    3.50 +				fprintf(stderr, "Invalid type argument\n");
    3.51 +				fprintf(stderr, "Console types supported are: serial, pv\n");
    3.52 +				exit(EINVAL);
    3.53 +			}
    3.54 +			break;
    3.55  		default:
    3.56  			fprintf(stderr, "Invalid argument\n");
    3.57  			fprintf(stderr, "Try `%s --help' for more information.\n", 
    3.58 @@ -314,10 +334,30 @@ int main(int argc, char **argv)
    3.59  	dom_path = xs_get_domain_path(xs, domid);
    3.60  	if (dom_path == NULL)
    3.61  		err(errno, "xs_get_domain_path()");
    3.62 -	path = malloc(strlen(dom_path) + strlen("/serial/0/tty") + 5);
    3.63 +	if (type == CONSOLE_INVAL) {
    3.64 +		xc_dominfo_t xcinfo;
    3.65 +		xc_interface *xc_handle = xc_interface_open(0,0,0);
    3.66 +		if (xc_handle == NULL)
    3.67 +			err(errno, "Could not open xc interface");
    3.68 +		xc_domain_getinfo(xc_handle, domid, 1, &xcinfo);
    3.69 +		/* default to pv console for pv guests and serial for hvm guests */
    3.70 +		if (xcinfo.hvm)
    3.71 +			type = CONSOLE_SERIAL;
    3.72 +		else
    3.73 +			type = CONSOLE_PV;
    3.74 +		xc_interface_close(xc_handle);
    3.75 +	}
    3.76 +	path = malloc(strlen(dom_path) + strlen("/device/console/0/tty") + 5);
    3.77  	if (path == NULL)
    3.78  		err(ENOMEM, "malloc");
    3.79 -	snprintf(path, strlen(dom_path) + strlen("/serial/0/tty") + 5, "%s/serial/%d/tty", dom_path, num);
    3.80 +	if (type == CONSOLE_SERIAL)
    3.81 +		snprintf(path, strlen(dom_path) + strlen("/serial/0/tty") + 5, "%s/serial/%d/tty", dom_path, num);
    3.82 +	else {
    3.83 +		if (num == 0)
    3.84 +			snprintf(path, strlen(dom_path) + strlen("/console/tty") + 1, "%s/console/tty", dom_path);
    3.85 +		else
    3.86 +			snprintf(path, strlen(dom_path) + strlen("/device/console/%d/tty") + 5, "%s/device/console/%d/tty", dom_path, num);
    3.87 +	}
    3.88  
    3.89  	/* FIXME consoled currently does not assume domain-0 doesn't have a
    3.90  	   console which is good when we break domain-0 up.  To keep us
     4.1 --- a/tools/console/daemon/io.c	Mon Aug 16 09:54:02 2010 +0100
     4.2 +++ b/tools/console/daemon/io.c	Mon Aug 16 13:32:04 2010 +0100
     4.3 @@ -87,8 +87,6 @@ struct domain {
     4.4  	struct buffer buffer;
     4.5  	struct domain *next;
     4.6  	char *conspath;
     4.7 -	char *serialpath;
     4.8 -	int use_consolepath;
     4.9  	int ring_ref;
    4.10  	evtchn_port_or_error_t local_port;
    4.11  	evtchn_port_or_error_t remote_port;
    4.12 @@ -440,20 +438,8 @@ static int domain_create_tty(struct doma
    4.13  		goto out;
    4.14  	}
    4.15  
    4.16 -	if (dom->use_consolepath) {
    4.17 -		success = asprintf(&path, "%s/limit", dom->conspath) !=
    4.18 -			-1;
    4.19 -		if (!success)
    4.20 -			goto out;
    4.21 -		data = xs_read(xs, XBT_NULL, path, &len);
    4.22 -		if (data) {
    4.23 -			dom->buffer.max_capacity = strtoul(data, 0, 0);
    4.24 -			free(data);
    4.25 -		}
    4.26 -		free(path);
    4.27 -	}
    4.28 -
    4.29 -	success = asprintf(&path, "%s/limit", dom->serialpath) != -1;
    4.30 +	success = asprintf(&path, "%s/limit", dom->conspath) !=
    4.31 +		-1;
    4.32  	if (!success)
    4.33  		goto out;
    4.34  	data = xs_read(xs, XBT_NULL, path, &len);
    4.35 @@ -463,7 +449,7 @@ static int domain_create_tty(struct doma
    4.36  	}
    4.37  	free(path);
    4.38  
    4.39 -	success = asprintf(&path, "%s/tty", dom->serialpath) != -1;
    4.40 +	success = (asprintf(&path, "%s/tty", dom->conspath) != -1);
    4.41  	if (!success)
    4.42  		goto out;
    4.43  	success = xs_write(xs, XBT_NULL, path, slave, strlen(slave));
    4.44 @@ -471,16 +457,6 @@ static int domain_create_tty(struct doma
    4.45  	if (!success)
    4.46  		goto out;
    4.47  
    4.48 -	if (dom->use_consolepath) {
    4.49 -		success = (asprintf(&path, "%s/tty", dom->conspath) != -1);
    4.50 -		if (!success)
    4.51 -			goto out;
    4.52 -		success = xs_write(xs, XBT_NULL, path, slave, strlen(slave));
    4.53 -		free(path);
    4.54 -		if (!success)
    4.55 -			goto out;
    4.56 -	}
    4.57 -
    4.58  	if (fcntl(dom->master_fd, F_SETFL, O_NONBLOCK) == -1)
    4.59  		goto out;
    4.60  
    4.61 @@ -524,29 +500,20 @@ static int xs_gather(struct xs_handle *x
    4.62  	va_end(ap);
    4.63  	return ret;
    4.64  }
    4.65 -
    4.66 + 
    4.67  static int domain_create_ring(struct domain *dom)
    4.68  {
    4.69  	int err, remote_port, ring_ref, rc;
    4.70  	char *type, path[PATH_MAX];
    4.71  
    4.72 -	err = xs_gather(xs, dom->serialpath,
    4.73 +	err = xs_gather(xs, dom->conspath,
    4.74  			"ring-ref", "%u", &ring_ref,
    4.75  			"port", "%i", &remote_port,
    4.76  			NULL);
    4.77 -	if (err) {
    4.78 -		err = xs_gather(xs, dom->conspath,
    4.79 -				"ring-ref", "%u", &ring_ref,
    4.80 -				"port", "%i", &remote_port,
    4.81 -				NULL);
    4.82 -		if (err)
    4.83 -			goto out;
    4.84 -		dom->use_consolepath = 1;
    4.85 -	} else
    4.86 -		dom->use_consolepath = 0;
    4.87 +	if (err)
    4.88 +		goto out;
    4.89  
    4.90 -	snprintf(path, sizeof(path), "%s/type",
    4.91 -		dom->use_consolepath ? dom->conspath: dom->serialpath);
    4.92 +	snprintf(path, sizeof(path), "%s/type", dom->conspath);
    4.93  	type = xs_read(xs, XBT_NULL, path, NULL);
    4.94  	if (type && strcmp(type, "xenconsoled") != 0) {
    4.95  		free(type);
    4.96 @@ -628,16 +595,12 @@ static bool watch_domain(struct domain *
    4.97  
    4.98  	snprintf(domid_str, sizeof(domid_str), "dom%u", dom->domid);
    4.99  	if (watch) {
   4.100 -		success = xs_watch(xs, dom->serialpath, domid_str);
   4.101 -		if (success) {
   4.102 -			success = xs_watch(xs, dom->conspath, domid_str);
   4.103 -			if (success)
   4.104 -				domain_create_ring(dom);
   4.105 -			else
   4.106 -				xs_unwatch(xs, dom->serialpath, domid_str);
   4.107 -		}
   4.108 +		success = xs_watch(xs, dom->conspath, domid_str);
   4.109 +		if (success)
   4.110 +			domain_create_ring(dom);
   4.111 +		else
   4.112 +			xs_unwatch(xs, dom->conspath, domid_str);
   4.113  	} else {
   4.114 -		success = xs_unwatch(xs, dom->serialpath, domid_str);
   4.115  		success = xs_unwatch(xs, dom->conspath, domid_str);
   4.116  	}
   4.117  
   4.118 @@ -666,14 +629,6 @@ static struct domain *create_domain(int 
   4.119  
   4.120  	dom->domid = domid;
   4.121  
   4.122 -	dom->serialpath = xs_get_domain_path(xs, dom->domid);
   4.123 -	s = realloc(dom->serialpath, strlen(dom->serialpath) +
   4.124 -		    strlen("/serial/0") + 1);
   4.125 -	if (s == NULL)
   4.126 -		goto out;
   4.127 -	dom->serialpath = s;
   4.128 -	strcat(dom->serialpath, "/serial/0");
   4.129 -
   4.130  	dom->conspath = xs_get_domain_path(xs, dom->domid);
   4.131  	s = realloc(dom->conspath, strlen(dom->conspath) +
   4.132  		    strlen("/console") + 1);
   4.133 @@ -712,7 +667,6 @@ static struct domain *create_domain(int 
   4.134  
   4.135  	return dom;
   4.136   out:
   4.137 -	free(dom->serialpath);
   4.138  	free(dom->conspath);
   4.139  	free(dom);
   4.140  	return NULL;
   4.141 @@ -755,9 +709,6 @@ static void cleanup_domain(struct domain
   4.142  	free(d->buffer.data);
   4.143  	d->buffer.data = NULL;
   4.144  
   4.145 -	free(d->serialpath);
   4.146 -	d->serialpath = NULL;
   4.147 -
   4.148  	free(d->conspath);
   4.149  	d->conspath = NULL;
   4.150  
     5.1 --- a/tools/libxl/libxl.c	Mon Aug 16 09:54:02 2010 +0100
     5.2 +++ b/tools/libxl/libxl.c	Mon Aug 16 13:32:04 2010 +0100
     5.3 @@ -952,13 +952,20 @@ out:
     5.4      return 0;
     5.5  }
     5.6  
     5.7 -int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num)
     5.8 +int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num, libxl_console_constype type)
     5.9  {
    5.10      libxl_gc gc = LIBXL_INIT_GC(ctx);
    5.11      char *p = libxl_sprintf(&gc, "%s/xenconsole", libxl_private_bindir_path());
    5.12      char *domid_s = libxl_sprintf(&gc, "%d", domid);
    5.13      char *cons_num_s = libxl_sprintf(&gc, "%d", cons_num);
    5.14 -    execl(p, p, domid_s, "--num", cons_num_s, (void *)NULL);
    5.15 +    char *cons_type_s;
    5.16 +
    5.17 +    if (type == LIBXL_CONSTYPE_PV)
    5.18 +        cons_type_s = "pv";
    5.19 +    else
    5.20 +        cons_type_s = "serial";
    5.21 +
    5.22 +    execl(p, p, domid_s, "--num", cons_num_s, "--type", cons_type_s, (void *)NULL);
    5.23      libxl_free_all(&gc);
    5.24      return ERROR_FAIL;
    5.25  }
    5.26 @@ -967,9 +974,13 @@ int libxl_primary_console_exec(libxl_ctx
    5.27  {
    5.28      uint32_t stubdomid = libxl_get_stubdom_id(ctx, domid_vm);
    5.29      if (stubdomid)
    5.30 -        return libxl_console_exec(ctx, stubdomid, 1);
    5.31 -    else
    5.32 -        return libxl_console_exec(ctx, domid_vm, 0);
    5.33 +        return libxl_console_exec(ctx, stubdomid, 1, LIBXL_CONSTYPE_PV);
    5.34 +    else {
    5.35 +        if (is_hvm(ctx, domid_vm))
    5.36 +            return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_SERIAL);
    5.37 +        else
    5.38 +            return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_PV);
    5.39 +    }
    5.40  }
    5.41  
    5.42  int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
    5.43 @@ -1540,15 +1551,22 @@ retry_transaction:
    5.44  
    5.45      for (i = 0; i < num_console; i++) {
    5.46          console[i].devid = i;
    5.47 -        console[i].constype = CONSTYPE_IOEMU;
    5.48 +        console[i].consback = LIBXL_CONSBACK_IOEMU;
    5.49          console[i].domid = domid;
    5.50 -        if (!i)
    5.51 +        if (!i) {
    5.52 +            char *filename;
    5.53 +            char *name = libxl_sprintf(&gc, "qemu-dm-%s", libxl_domid_to_name(ctx, info->domid));
    5.54 +            libxl_create_logfile(ctx, name, &filename);
    5.55 +            console[i].output = libxl_sprintf(&gc, "file:%s", filename);
    5.56              console[i].build_state = &state;
    5.57 +            free(filename);
    5.58 +        } else
    5.59 +            console[i].output = "pty";
    5.60          ret = libxl_device_console_add(ctx, domid, &console[i]);
    5.61          if (ret)
    5.62              goto out_free;
    5.63      }
    5.64 -    if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
    5.65 +    if (libxl_create_xenpv_qemu(ctx, domid, vfb, &dm_starting) < 0) {
    5.66          ret = ERROR_FAIL;
    5.67          goto out_free;
    5.68      }
    5.69 @@ -2290,7 +2308,7 @@ int libxl_device_console_add(libxl_ctx *
    5.70  
    5.71      if (console->build_state) {
    5.72          xs_transaction_t t;
    5.73 -        char **ents = (char **) libxl_calloc(&gc, 9, sizeof(char *));
    5.74 +        char **ents = (char **) libxl_calloc(&gc, 11, sizeof(char *));
    5.75          ents[0] = "console/port";
    5.76          ents[1] = libxl_sprintf(&gc, "%"PRIu32, console->build_state->console_port);
    5.77          ents[2] = "console/ring-ref";
    5.78 @@ -2298,10 +2316,12 @@ int libxl_device_console_add(libxl_ctx *
    5.79          ents[4] = "console/limit";
    5.80          ents[5] = libxl_sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT);
    5.81          ents[6] = "console/type";
    5.82 -        if (console->constype == CONSTYPE_XENCONSOLED)
    5.83 +        if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
    5.84              ents[7] = "xenconsoled";
    5.85          else
    5.86              ents[7] = "ioemu";
    5.87 +        ents[8] = "console/output";
    5.88 +        ents[9] = console->output;
    5.89  retry_transaction:
    5.90          t = xs_transaction_start(ctx->xsh);
    5.91          libxl_xs_writev(&gc, t, libxl_xs_get_dompath(&gc, console->domid), ents);
    5.92 @@ -2339,19 +2359,25 @@ retry_transaction:
    5.93      flexarray_set(back, boffset++, "protocol");
    5.94      flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
    5.95  
    5.96 -    flexarray_set(front, foffset++, "backend-id");
    5.97 -    flexarray_set(front, foffset++, libxl_sprintf(&gc, "%d", console->backend_domid));
    5.98 -    flexarray_set(front, foffset++, "state");
    5.99 -    flexarray_set(front, foffset++, libxl_sprintf(&gc, "%d", 1));
   5.100 -    flexarray_set(front, foffset++, "limit");
   5.101 -    flexarray_set(front, foffset++, libxl_sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
   5.102 -    flexarray_set(front, foffset++, "protocol");
   5.103 -    flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
   5.104 -    flexarray_set(front, foffset++, "type");
   5.105 -    if (console->constype == CONSTYPE_XENCONSOLED)
   5.106 -        flexarray_set(front, foffset++, "xenconsoled");
   5.107 -    else
   5.108 -        flexarray_set(front, foffset++, "ioemu");
   5.109 +    /* if devid == 0 do not add the frontend to device/console/ because
   5.110 +     * it has already been added to console/ */
   5.111 +    if (device.devid > 0) {
   5.112 +        flexarray_set(front, foffset++, "backend-id");
   5.113 +        flexarray_set(front, foffset++, libxl_sprintf(&gc, "%d", console->backend_domid));
   5.114 +        flexarray_set(front, foffset++, "state");
   5.115 +        flexarray_set(front, foffset++, libxl_sprintf(&gc, "%d", 1));
   5.116 +        flexarray_set(front, foffset++, "limit");
   5.117 +        flexarray_set(front, foffset++, libxl_sprintf(&gc, "%d", LIBXL_XENCONSOLE_LIMIT));
   5.118 +        flexarray_set(front, foffset++, "protocol");
   5.119 +        flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
   5.120 +        flexarray_set(front, foffset++, "type");
   5.121 +        if (console->consback == LIBXL_CONSBACK_XENCONSOLED)
   5.122 +            flexarray_set(front, foffset++, "xenconsoled");
   5.123 +        else
   5.124 +            flexarray_set(front, foffset++, "ioemu");
   5.125 +        flexarray_set(front, foffset++, "output");
   5.126 +        flexarray_set(front, foffset++, console->output);
   5.127 +    }
   5.128  
   5.129      libxl_device_generic_add(ctx, &device,
   5.130                               libxl_xs_kvs_of_flexarray(&gc, back, boffset),
   5.131 @@ -2559,13 +2585,11 @@ int libxl_cdrom_insert(libxl_ctx *ctx, u
   5.132  
   5.133  /******************************************************************************/
   5.134  static int libxl_build_xenpv_qemu_args(libxl_gc *gc,
   5.135 +                                       uint32_t domid,
   5.136                                         libxl_device_vfb *vfb,
   5.137 -                                       int num_console,
   5.138 -                                       libxl_device_console *console,
   5.139                                         libxl_device_model_info *info)
   5.140  {
   5.141      libxl_ctx *ctx = libxl_gc_owner(gc);
   5.142 -    int i = 0, j = 0, num = 0;
   5.143      memset(info, 0x00, sizeof(libxl_device_model_info));
   5.144  
   5.145      info->vnc = vfb->vnc;
   5.146 @@ -2579,46 +2603,20 @@ static int libxl_build_xenpv_qemu_args(l
   5.147          info->keymap = libxl_strdup(gc, vfb->keymap);
   5.148      info->sdl = vfb->sdl;
   5.149      info->opengl = vfb->opengl;
   5.150 -    for (i = 0; i < num_console; i++) {
   5.151 -        if (console->constype == CONSTYPE_IOEMU)
   5.152 -            num++;
   5.153 -    }
   5.154 -    if (num > 0) {
   5.155 -        uint32_t guest_domid;
   5.156 -        if (libxl_is_stubdom(ctx, vfb->domid, &guest_domid)) {
   5.157 -            char *filename;
   5.158 -            char *name = libxl_sprintf(gc, "qemu-dm-%s", _libxl_domid_to_name(gc, guest_domid));
   5.159 -            libxl_create_logfile(ctx, name, &filename);
   5.160 -            info->serial = libxl_sprintf(gc, "file:%s", filename);
   5.161 -            free(filename);
   5.162 -        } else {
   5.163 -            info->serial = "pty";
   5.164 -        }
   5.165 -        num--;
   5.166 -    }
   5.167 -    if (num > 0) {
   5.168 -        info->extra = (char **) libxl_calloc(gc, num * 2 + 1, sizeof(char *));
   5.169 -        for (j = 0; j < num * 2; j = j + 2) {
   5.170 -            info->extra[j] = "-serial";
   5.171 -            info->extra[j + 1] = "pty";
   5.172 -        }
   5.173 -        info->extra[j] = NULL;
   5.174 -    }
   5.175 -    info->domid = vfb->domid;
   5.176 -    info->dom_name = _libxl_domid_to_name(gc, vfb->domid);
   5.177 +    info->domid = domid;
   5.178 +    info->dom_name = libxl_domid_to_name(ctx, domid);
   5.179      info->device_model = libxl_abs_path(gc, "qemu-dm", libxl_libexec_path());
   5.180      info->type = XENPV;
   5.181      return 0;
   5.182  }
   5.183  
   5.184 -int libxl_create_xenpv_qemu(libxl_ctx *ctx, libxl_device_vfb *vfb,
   5.185 -                            int num_console, libxl_device_console *console,
   5.186 +int libxl_create_xenpv_qemu(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb,
   5.187                              libxl_device_model_starting **starting_r)
   5.188  {
   5.189      libxl_gc gc = LIBXL_INIT_GC(ctx);
   5.190      libxl_device_model_info info;
   5.191  
   5.192 -    libxl_build_xenpv_qemu_args(&gc, vfb, num_console, console, &info);
   5.193 +    libxl_build_xenpv_qemu_args(&gc, domid, vfb, &info);
   5.194      libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
   5.195      libxl_free_all(&gc);
   5.196      return 0;
     6.1 --- a/tools/libxl/libxl.h	Mon Aug 16 09:54:02 2010 +0100
     6.2 +++ b/tools/libxl/libxl.h	Mon Aug 16 13:32:04 2010 +0100
     6.3 @@ -335,16 +335,22 @@ typedef struct {
     6.4  } libxl_device_vkb;
     6.5  
     6.6  typedef enum {
     6.7 -    CONSTYPE_XENCONSOLED,
     6.8 -    CONSTYPE_IOEMU,
     6.9 +    LIBXL_CONSTYPE_SERIAL,
    6.10 +    LIBXL_CONSTYPE_PV,
    6.11  } libxl_console_constype;
    6.12  
    6.13 +typedef enum {
    6.14 +    LIBXL_CONSBACK_XENCONSOLED,
    6.15 +    LIBXL_CONSBACK_IOEMU,
    6.16 +} libxl_console_consback;
    6.17 +
    6.18  typedef struct {
    6.19      uint32_t backend_domid;
    6.20      uint32_t domid;
    6.21      int devid;
    6.22 -    libxl_console_constype constype;
    6.23 +    libxl_console_consback consback;
    6.24      libxl_domain_build_state *build_state;
    6.25 +    char *output;
    6.26  } libxl_device_console;
    6.27  
    6.28  typedef enum {
    6.29 @@ -542,7 +548,7 @@ int libxl_domain_setmaxmem(libxl_ctx *ct
    6.30  int libxl_set_memory_target(libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb, int enforce);
    6.31  
    6.32  int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass);
    6.33 -int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num);
    6.34 +int libxl_console_exec(libxl_ctx *ctx, uint32_t domid, int cons_num, libxl_console_constype type);
    6.35  /* libxl_primary_console_exec finds the domid and console number
    6.36   * corresponding to the primary console of the given vm, then calls
    6.37   * libxl_console_exec with the right arguments (domid might be different
    6.38 @@ -564,8 +570,7 @@ int libxl_create_device_model(libxl_ctx 
    6.39                                libxl_device_disk *disk, int num_disks,
    6.40                                libxl_device_nic *vifs, int num_vifs,
    6.41                                libxl_device_model_starting **starting_r);
    6.42 -int libxl_create_xenpv_qemu(libxl_ctx *ctx, libxl_device_vfb *vfb,
    6.43 -                            int num_console, libxl_device_console *console,
    6.44 +int libxl_create_xenpv_qemu(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb,
    6.45                              libxl_device_model_starting **starting_r);
    6.46    /* Caller must either: pass starting_r==0, or on successful
    6.47     * return pass *starting_r (which will be non-0) to
     7.1 --- a/tools/libxl/xl_cmdimpl.c	Mon Aug 16 09:54:02 2010 +0100
     7.2 +++ b/tools/libxl/xl_cmdimpl.c	Mon Aug 16 13:32:04 2010 +0100
     7.3 @@ -371,7 +371,8 @@ static void init_console_info(libxl_devi
     7.4  {
     7.5      memset(console, 0x00, sizeof(libxl_device_console));
     7.6      console->devid = dev_num;
     7.7 -    console->constype = CONSTYPE_XENCONSOLED;
     7.8 +    console->consback = LIBXL_CONSBACK_XENCONSOLED;
     7.9 +    console->output = "pty";
    7.10      if (state)
    7.11          console->build_state = state;
    7.12  }
    7.13 @@ -1457,10 +1458,10 @@ start:
    7.14          init_console_info(&console, 0, &state);
    7.15          console.domid = domid;
    7.16          if (d_config.num_vfbs)
    7.17 -            console.constype = CONSTYPE_IOEMU;
    7.18 +             console.consback = LIBXL_CONSBACK_IOEMU;
    7.19          libxl_device_console_add(&ctx, domid, &console);
    7.20          if (d_config.num_vfbs)
    7.21 -            libxl_create_xenpv_qemu(&ctx, d_config.vfbs, 1, &console, &dm_starting);
    7.22 +            libxl_create_xenpv_qemu(&ctx, domid, d_config.vfbs, &dm_starting);
    7.23      }
    7.24  
    7.25      if (dm_starting)
    7.26 @@ -1855,13 +1856,27 @@ int main_cd_insert(int argc, char **argv
    7.27  
    7.28  int main_console(int argc, char **argv)
    7.29  {
    7.30 -    int opt = 0;
    7.31 -
    7.32 -    while ((opt = getopt(argc, argv, "hn:")) != -1) {
    7.33 +    int opt = 0, num = 0;
    7.34 +    libxl_console_constype type = -1;
    7.35 +
    7.36 +    while ((opt = getopt(argc, argv, "hn:t:")) != -1) {
    7.37          switch (opt) {
    7.38          case 'h':
    7.39              help("console");
    7.40              return 0;
    7.41 +        case 't':
    7.42 +            if (!strcmp(optarg, "pv"))
    7.43 +                type = LIBXL_CONSTYPE_PV;
    7.44 +            else if (!strcmp(optarg, "serial"))
    7.45 +                type = LIBXL_CONSTYPE_SERIAL;
    7.46 +            else {
    7.47 +                fprintf(stderr, "console type supported are: pv, serial\n");
    7.48 +                return 2;
    7.49 +            }
    7.50 +            break;
    7.51 +        case 'n':
    7.52 +            num = atoi(optarg);
    7.53 +            break;
    7.54          default:
    7.55              fprintf(stderr, "option not supported\n");
    7.56              break;
    7.57 @@ -1873,7 +1888,10 @@ int main_console(int argc, char **argv)
    7.58      }
    7.59  
    7.60      find_domain(argv[optind]);
    7.61 -    libxl_primary_console_exec(&ctx, domid);
    7.62 +    if (type <= 0 && num == 0)
    7.63 +        libxl_primary_console_exec(&ctx, domid);
    7.64 +    else
    7.65 +        libxl_console_exec(&ctx, domid, num, type);
    7.66      fprintf(stderr, "Unable to attach console\n");
    7.67      return 1;
    7.68  }
     8.1 --- a/tools/libxl/xl_cmdtable.c	Mon Aug 16 09:54:02 2010 +0100
     8.2 +++ b/tools/libxl/xl_cmdtable.c	Mon Aug 16 13:32:04 2010 +0100
     8.3 @@ -86,7 +86,9 @@ struct cmd_spec cmd_table[] = {
     8.4      { "console",
     8.5        &main_console,
     8.6        "Attach to domain's console",
     8.7 -      "<Domain>",
     8.8 +      "[options] <Domain>\n"
     8.9 +      "-t <type>       console type, pv or serial\n"
    8.10 +      "-n <number>     console number"
    8.11      },
    8.12      { "vncviewer",
    8.13        &main_vncviewer,
     9.1 --- a/tools/ocaml/libs/xl/xl_stubs.c	Mon Aug 16 09:54:02 2010 +0100
     9.2 +++ b/tools/ocaml/libs/xl/xl_stubs.c	Mon Aug 16 13:32:04 2010 +0100
     9.3 @@ -232,7 +232,7 @@ static int device_console_val(caml_gc *g
     9.4  
     9.5  	c_val->backend_domid = Int_val(Field(v, 0));
     9.6  	c_val->devid = Int_val(Field(v, 1));
     9.7 -	c_val->constype = (Int_val(Field(v, 2))) + CONSTYPE_XENCONSOLED;
     9.8 +	c_val->consback = (Int_val(Field(v, 2))) + LIBXL_CONSBACK_XENCONSOLED;
     9.9  
    9.10  	CAMLreturn(0);
    9.11  }