From: Bamvor Jian Zhang Subject: [PATCH] [PATCH] libxl: Add API to retrieve domain console tty Date: Mon, 21 May 2012 19:07:20 +0800 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: jfehlig@suse.com, Ian.Jackson@eu.citrix.com, Ian.Campbell@citrix.com, bjzhang@suse.com List-Id: xen-devel@lists.xenproject.org This api retrieve domain console from xenstore. With this new api, it is easy to implement "virsh console" command in libvirt libxl driver. Signed-off-by: Bamvor Jian Zhang diff -r f8279258e3c9 -r a1dc373628e4 tools/libxl/libxl.c --- a/tools/libxl/libxl.c Mon May 14 17:15:36 2012 +0100 +++ b/tools/libxl/libxl.c Mon May 21 18:51:17 2012 +0800 @@ -1552,6 +1552,61 @@ out: return rc; } +int libxl_console_get_tty(libxl_ctx *ctx, uint32_t domid, int cons_num, + libxl_console_type type, char **path) +{ + GC_INIT(ctx); + char *dom_path = 0; + char *tty_path = 0; + char *tty = 0; + + dom_path = libxl__xs_get_dompath(gc, domid); + switch (type) { + case LIBXL_CONSOLE_TYPE_SERIAL: + tty_path = libxl__sprintf(gc, "%s/serial/0/tty", dom_path); + break; + case LIBXL_CONSOLE_TYPE_PV: + if (cons_num == 0) + tty_path = libxl__sprintf(gc, "%s/console/tty", dom_path); + else + tty_path = libxl__sprintf(gc, "%s/device/console/%d/tty", dom_path, cons_num); + break; + default: + goto out; + } + + tty = libxl__xs_read(gc, XBT_NULL, tty_path); + *path = strdup(tty); + +out: + GC_FREE; + return ERROR_FAIL; +} + +int libxl_primary_console_get_tty(libxl_ctx *ctx, uint32_t domid_vm, char **path) +{ + GC_INIT(ctx); + uint32_t stubdomid = libxl_get_stubdom_id(ctx, domid_vm); + int rc; + if (stubdomid) + rc = libxl_console_get_tty(ctx, stubdomid, + STUBDOM_CONSOLE_SERIAL, LIBXL_CONSOLE_TYPE_PV, path); + else { + switch (libxl__domain_type(gc, domid_vm)) { + case LIBXL_DOMAIN_TYPE_HVM: + rc = libxl_console_get_tty(ctx, domid_vm, 0, LIBXL_CONSOLE_TYPE_SERIAL, path); + break; + case LIBXL_DOMAIN_TYPE_PV: + rc = libxl_console_get_tty(ctx, domid_vm, 0, LIBXL_CONSOLE_TYPE_PV, path); + break; + default: + abort(); + } + } + GC_FREE; + return rc; +} + static int libxl__append_disk_list_of_type(libxl__gc *gc, uint32_t domid, diff -r f8279258e3c9 -r a1dc373628e4 tools/libxl/libxl.h --- a/tools/libxl/libxl.h Mon May 14 17:15:36 2012 +0100 +++ b/tools/libxl/libxl.h Mon May 21 18:51:17 2012 +0800 @@ -594,6 +594,15 @@ int libxl_console_exec(libxl_ctx *ctx, u * case of HVM guests, and before libxl_run_bootloader in case of PV * guests using pygrub. */ int libxl_primary_console_exec(libxl_ctx *ctx, uint32_t domid_vm); +/* libxl_console_get_tty retrieves the specified domain's console tty path + * and stores it in path. Caller is responsible for freeing the memory. + */ +int libxl_console_get_tty(libxl_ctx *ctx, uint32_t domid, int cons_num, + libxl_console_type type, char **path); +/* libxl_primary_console_get_tty retrieves the specified domain's console tty + * path and stores it in path. Caller is responsible for freeing the memory. + */ +int libxl_primary_console_get_tty(libxl_ctx *ctx, uint32_t domid_vm, char **path); /* May be called with info_r == NULL to check for domain's existance */ int libxl_domain_info(libxl_ctx*, libxl_dominfo *info_r,