Skip to contentSkip to conversation

xenconsole: add support for a console type parameter

PATCH In review Stefano Stabellini 2 participants 3 messages Aug 9, 2010 – Aug 13, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xenconsole: add support for a console type parameter

xenconsole: implement support for an explicit console type parameter

This patch adds support to xenconsole for an explicity 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.

This patch also changes the xenstore paths corresponding to pv consoles to the console prefix, leaving the serial prefix to emulated serials.

tools/console/client/main.c +57 -3
diff -r 6b28b2dac7dd tools/console/client/main.c
@@ -254,6 +254,12 @@ static int console_loop(int fd, struct x
 	return 0;
 }
 
+typedef enum {
+	CONSOLE_INVAL,
+	CONSOLE_PV,
+	CONSOLE_SERIAL,
+} console_type;
+
 int main(int argc, char **argv)
 {
 	struct termios attr;
@@ -263,15 +269,19 @@ int main(int argc, char **argv)
 	unsigned int num = 0;
 	int opt_ind=0;
 	struct option lopt[] = {
+		{ "type",     1, 0, 't' },
 		{ "num",     1, 0, 'n' },
 		{ "help",    0, 0, 'h' },
 		{ 0 },
 
 	};
-	char *dom_path = NULL, *path = NULL;
+	char *dom_path = NULL, *path = NULL, *vm_path = NULL;
+	char *uuid = NULL, ostype_path[55], *ostype = NULL;
 	int spty, xsfd;
 	struct xs_handle *xs;
 	char *end;
+	unsigned int len;
+	console_type type = CONSOLE_INVAL;
 
 	while((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
 		switch(ch) {
@@ -282,6 +292,17 @@ int main(int argc, char **argv)
 		case 'n':
 			num = atoi(optarg);
 			break;
+		case 't':
+			if (!strcmp(optarg, "serial"))
+				type = CONSOLE_SERIAL;
+			else if (!strcmp(optarg, "pv"))
+				type = CONSOLE_PV;
+			else {
+				fprintf(stderr, "Invalid type argument\n");
+				fprintf(stderr, "Console types supported are: serial, pv\n");
+				exit(EINVAL);
+			}
+			break;
 		default:
 			fprintf(stderr, "Invalid argument\n");
 			fprintf(stderr, "Try `%s --help' for more information.\n", 
@@ -314,10 +335,43 @@ int main(int argc, char **argv)
 	dom_path = xs_get_domain_path(xs, domid);
 	if (dom_path == NULL)
 		err(errno, "xs_get_domain_path()");
-	path = malloc(strlen(dom_path) + strlen("/serial/0/tty") + 5);
+	if (type == CONSOLE_INVAL) {
+		/* guess what type of console we have to connect to */
+		vm_path = malloc(strlen(dom_path) + 4);
+		if (vm_path == NULL)
+			err(ENOMEM, "malloc");
+		snprintf(vm_path, strlen(dom_path) + 4, "%s/vm", dom_path);
+		uuid = xs_read(xs, XBT_NULL, vm_path, &len);
+		if (uuid == NULL) {
+			fprintf(stderr, "No vm path for domid %d\n", domid);
+			exit(EINVAL);
+		}
+		snprintf(ostype_path, strlen(ostype_path), "%s/image/ostype", uuid);
+		ostype = xs_read(xs, XBT_NULL, ostype_path, &len);
+		if (ostype == NULL) {
+			fprintf(stderr, "No ostype for domid %d\n", domid);
+			exit(EINVAL);
+		}
+		/* default to pv console for pv guests and serial for hvm guests */
+		if (strcmp(ostype, "hvm"))
+			type = CONSOLE_PV;
+		else
+			type = CONSOLE_SERIAL;
+		free(vm_path);
+		free(uuid);
+		free(ostype);
+	}
+	path = malloc(strlen(dom_path) + strlen("/device/console/0/tty") + 5);
 	if (path == NULL)
 		err(ENOMEM, "malloc");
-	snprintf(path, strlen(dom_path) + strlen("/serial/0/tty") + 5, "%s/serial/%d/tty", dom_path, num);
+	if (type == CONSOLE_SERIAL)
+		snprintf(path, strlen(dom_path) + strlen("/serial/0/tty") + 5, "%s/serial/%d/tty", dom_path, num);
+	else {
+		if (num == 0)
+			snprintf(path, strlen(dom_path) + strlen("/console/tty") + 1, "%s/console/tty", dom_path);
+		else
+			snprintf(path, strlen(dom_path) + strlen("/device/console/%d/tty") + 5, "%s/device/console/%d/tty", dom_path, num);
+	}
 
 	/* FIXME consoled currently does not assume domain-0 doesn't have a
 	   console which is good when we break domain-0 up.  To keep us
#1 lore raw

Stefano Stabellini writes ("[Xen-devel] [PATCH 2 of 4] xenconsole: add support for a console type parameter"):

Quoted 18 lines

xenconsole: implement support for an explicit console type parameter

This patch adds support to xenconsole for an explicity 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.

This patch also changes the xenstore paths corresponding to pv consoles to the console prefix, leaving the serial prefix to emulated serials.

diff -r 6b28b2dac7dd tools/console/client/main.c

tools/console/client/main.c +0 -0

...

tools/console/client/main.c

Quoted 2 lines
tools/console/client/main.c +0 -1
-	path = malloc(strlen(dom_path) + strlen("/serial/0/tty") + 5);
  [ 27 lines to determine whether guest is HVM or PV ]

Is this really the best way to do this ? Is there no other function somewhere which will do this more sensibly ?

Perhaps xenconsole needs to become a libxl caller rather than a helper program run by xl console.

Aside from that, I'm happy with this patch.

Ian.

#2 lore raw
Quoted 26 lines · Ian Jackson

Stefano Stabellini writes ("[Xen-devel] [PATCH 2 of 4] xenconsole: add support for a console type parameter"):

Quoted 18 lines

xenconsole: implement support for an explicit console type parameter

This patch adds support to xenconsole for an explicity 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.

This patch also changes the xenstore paths corresponding to pv consoles to the console prefix, leaving the serial prefix to emulated serials.

diff -r 6b28b2dac7dd tools/console/client/main.c

tools/console/client/main.c +0 -0

...

tools/console/client/main.c

Quoted 2 lines
tools/console/client/main.c +0 -1
-	path = malloc(strlen(dom_path) + strlen("/serial/0/tty") + 5);
  [ 27 lines to determine whether guest is HVM or PV ]

Is this really the best way to do this ? Is there no other function somewhere which will do this more sensibly ?

Quoted 4 lines

Perhaps xenconsole needs to become a libxl caller rather than a helper program run by xl console.

Aside from that, I'm happy with this patch.

Please note that the 'guessing' code is not really required, I only added it to retain compatibility with xend that I didn't want to modify.