support mock & domuuid for virsh

This commit is contained in:
Karel Zak 2006-05-29 15:39:31 +00:00
parent 624505349d
commit d47ddf5b67
4 changed files with 127 additions and 57 deletions

View File

@ -1,3 +1,14 @@
Mon May 29 16:33:39 CEST 2006 Karel Zak <kzak@redhat.com>
* src/virsh.c: improved vshCommandOptDomain(), added <name> to the
connect command and minor changes to Daniel B.'s patch
Fri May 26 11:40:20 EDT 2006 Daniel P. Berrange <berrange@redhat.com>
* src/virsh.c: added 'domuuid' command to display printable UUID
string for a domain. Added '--connect' argument to allow the name
of the hypervisor connection passed to virConnect to be set.
Mon May 22 15:34:20 CEST 2006 Karel Zak <kzak@redhat.com> Mon May 22 15:34:20 CEST 2006 Karel Zak <kzak@redhat.com>
* src/virsh.c: added UUID: to the dominfo command, vshPrint() refactoring, * src/virsh.c: added UUID: to the dominfo command, vshPrint() refactoring,

View File

@ -229,6 +229,9 @@ virDomainPtr virDomainLookupByID (virConnectPtr conn,
int id); int id);
virDomainPtr virDomainLookupByUUID (virConnectPtr conn, virDomainPtr virDomainLookupByUUID (virConnectPtr conn,
const unsigned char *uuid); const unsigned char *uuid);
virDomainPtr virDomainLookupByUUIDString (virConnectPtr conn,
const char *uuid);
int virDomainShutdown (virDomainPtr domain); int virDomainShutdown (virDomainPtr domain);
int virDomainReboot (virDomainPtr domain, int virDomainReboot (virDomainPtr domain,
unsigned int flags); unsigned int flags);
@ -262,6 +265,8 @@ const char * virDomainGetName (virDomainPtr domain);
unsigned int virDomainGetID (virDomainPtr domain); unsigned int virDomainGetID (virDomainPtr domain);
int virDomainGetUUID (virDomainPtr domain, int virDomainGetUUID (virDomainPtr domain,
unsigned char *uuid); unsigned char *uuid);
int virDomainGetUUIDString (virDomainPtr domain,
char *buf);
char * virDomainGetOSType (virDomainPtr domain); char * virDomainGetOSType (virDomainPtr domain);
unsigned long virDomainGetMaxMemory (virDomainPtr domain); unsigned long virDomainGetMaxMemory (virDomainPtr domain);
int virDomainSetMaxMemory (virDomainPtr domain, int virDomainSetMaxMemory (virDomainPtr domain,

View File

@ -229,6 +229,9 @@ virDomainPtr virDomainLookupByID (virConnectPtr conn,
int id); int id);
virDomainPtr virDomainLookupByUUID (virConnectPtr conn, virDomainPtr virDomainLookupByUUID (virConnectPtr conn,
const unsigned char *uuid); const unsigned char *uuid);
virDomainPtr virDomainLookupByUUIDString (virConnectPtr conn,
const char *uuid);
int virDomainShutdown (virDomainPtr domain); int virDomainShutdown (virDomainPtr domain);
int virDomainReboot (virDomainPtr domain, int virDomainReboot (virDomainPtr domain,
unsigned int flags); unsigned int flags);
@ -262,6 +265,8 @@ const char * virDomainGetName (virDomainPtr domain);
unsigned int virDomainGetID (virDomainPtr domain); unsigned int virDomainGetID (virDomainPtr domain);
int virDomainGetUUID (virDomainPtr domain, int virDomainGetUUID (virDomainPtr domain,
unsigned char *uuid); unsigned char *uuid);
int virDomainGetUUIDString (virDomainPtr domain,
char *buf);
char * virDomainGetOSType (virDomainPtr domain); char * virDomainGetOSType (virDomainPtr domain);
unsigned long virDomainGetMaxMemory (virDomainPtr domain); unsigned long virDomainGetMaxMemory (virDomainPtr domain);
int virDomainSetMaxMemory (virDomainPtr domain, int virDomainSetMaxMemory (virDomainPtr domain,

View File

@ -7,6 +7,8 @@
* *
* Daniel Veillard <veillard@redhat.com> * Daniel Veillard <veillard@redhat.com>
* Karel Zak <kzak@redhat.com> * Karel Zak <kzak@redhat.com>
* Daniel P. Berrange <berrange@redhat.com>
*
* *
* $Id$ * $Id$
*/ */
@ -154,6 +156,7 @@ typedef struct __vshCmd {
* vshControl * vshControl
*/ */
typedef struct __vshControl { typedef struct __vshControl {
char *name; /* connection name */
virConnectPtr conn; /* connection to hypervisor */ virConnectPtr conn; /* connection to hypervisor */
vshCmd *cmd; /* the current command */ vshCmd *cmd; /* the current command */
char *cmdstr; /* string with command */ char *cmdstr; /* string with command */
@ -184,11 +187,23 @@ static int vshCommandOptInt(vshCmd * cmd, const char *name, int *found);
static char *vshCommandOptString(vshCmd * cmd, const char *name, static char *vshCommandOptString(vshCmd * cmd, const char *name,
int *found); int *found);
static int vshCommandOptBool(vshCmd * cmd, const char *name); static int vshCommandOptBool(vshCmd * cmd, const char *name);
static virDomainPtr vshCommandOptDomain(vshControl * ctl, vshCmd * cmd,
const char *optname, char **name); #define VSH_DOMBYID (1 << 1)
#define VSH_DOMBYUUID (1 << 2)
#define VSH_DOMBYNAME (1 << 3)
static virDomainPtr vshCommandOptDomainBy(vshControl * ctl, vshCmd * cmd,
const char *optname, char **name, int flag);
/* default is lookup by Id, Name and UUID */
#define vshCommandOptDomain(_ctl, _cmd, _optname, _name) \
vshCommandOptDomainBy(_ctl, _cmd, _optname, _name,\
VSH_DOMBYID|VSH_DOMBYUUID|VSH_DOMBYNAME)
static void vshPrintExtra(vshControl * ctl, const char *format, ...); static void vshPrintExtra(vshControl * ctl, const char *format, ...);
static void vshDebug(vshControl * ctl, int level, const char *format, ...); static void vshDebug(vshControl * ctl, int level, const char *format, ...);
/* XXX: add batch support */
#define vshPrint(_ctl, ...) fprintf(stdout, __VA_ARGS__) #define vshPrint(_ctl, ...) fprintf(stdout, __VA_ARGS__)
static const char *vshDomainStateToString(int state); static const char *vshDomainStateToString(int state);
@ -246,7 +261,7 @@ cmdHelp(vshControl * ctl, vshCmd * cmd)
* "connect" command * "connect" command
*/ */
static vshCmdInfo info_connect[] = { static vshCmdInfo info_connect[] = {
{"syntax", "connect [--readonly]"}, {"syntax", "connect [name] [--readonly]"},
{"help", "(re)connect to hypervisor"}, {"help", "(re)connect to hypervisor"},
{"desc", {"desc",
"Connect to local hypervisor. This is build-in command after shell start up."}, "Connect to local hypervisor. This is build-in command after shell start up."},
@ -254,6 +269,7 @@ static vshCmdInfo info_connect[] = {
}; };
static vshCmdOptDef opts_connect[] = { static vshCmdOptDef opts_connect[] = {
{"name", VSH_OT_DATA, 0, "optional argument currently unused (or used for tests only)"},
{"readonly", VSH_OT_BOOL, 0, "read-only connection"}, {"readonly", VSH_OT_BOOL, 0, "read-only connection"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -262,7 +278,7 @@ static int
cmdConnect(vshControl * ctl, vshCmd * cmd) cmdConnect(vshControl * ctl, vshCmd * cmd)
{ {
int ro = vshCommandOptBool(cmd, "readonly"); int ro = vshCommandOptBool(cmd, "readonly");
if (ctl->conn) { if (ctl->conn) {
if (virConnectClose(ctl->conn) != 0) { if (virConnectClose(ctl->conn) != 0) {
vshError(ctl, FALSE, vshError(ctl, FALSE,
@ -271,10 +287,15 @@ cmdConnect(vshControl * ctl, vshCmd * cmd)
} }
ctl->conn = NULL; ctl->conn = NULL;
} }
if (ctl->name)
free(ctl->name);
ctl->name = vshCommandOptString(cmd, "name", NULL);
if (!ro) if (!ro)
ctl->conn = virConnectOpen(NULL); ctl->conn = virConnectOpen(ctl->name);
else else
ctl->conn = virConnectOpenReadOnly(NULL); ctl->conn = virConnectOpenReadOnly(ctl->name);
if (!ctl->conn) if (!ctl->conn)
vshError(ctl, FALSE, "failed to connect to the hypervisor"); vshError(ctl, FALSE, "failed to connect to the hypervisor");
@ -347,7 +368,7 @@ static vshCmdInfo info_domstate[] = {
}; };
static vshCmdOptDef opts_domstate[] = { static vshCmdOptDef opts_domstate[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -385,7 +406,7 @@ static vshCmdInfo info_suspend[] = {
}; };
static vshCmdOptDef opts_suspend[] = { static vshCmdOptDef opts_suspend[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -479,7 +500,7 @@ static vshCmdInfo info_save[] = {
}; };
static vshCmdOptDef opts_save[] = { static vshCmdOptDef opts_save[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
{"file", VSH_OT_DATA, VSH_OFLAG_REQ, "where to save the data"}, {"file", VSH_OT_DATA, VSH_OFLAG_REQ, "where to save the data"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -561,7 +582,7 @@ static vshCmdInfo info_resume[] = {
}; };
static vshCmdOptDef opts_resume[] = { static vshCmdOptDef opts_resume[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -600,7 +621,7 @@ static vshCmdInfo info_shutdown[] = {
}; };
static vshCmdOptDef opts_shutdown[] = { static vshCmdOptDef opts_shutdown[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -639,7 +660,7 @@ static vshCmdInfo info_reboot[] = {
}; };
static vshCmdOptDef opts_reboot[] = { static vshCmdOptDef opts_reboot[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -678,7 +699,7 @@ static vshCmdInfo info_destroy[] = {
}; };
static vshCmdOptDef opts_destroy[] = { static vshCmdOptDef opts_destroy[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -717,7 +738,7 @@ static vshCmdInfo info_dominfo[] = {
}; };
static vshCmdOptDef opts_dominfo[] = { static vshCmdOptDef opts_dominfo[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id or uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -817,7 +838,7 @@ static vshCmdInfo info_dumpxml[] = {
}; };
static vshCmdOptDef opts_dumpxml[] = { static vshCmdOptDef opts_dumpxml[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name, id, uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
@ -850,36 +871,29 @@ cmdDumpXML(vshControl * ctl, vshCmd * cmd)
* "domname" command * "domname" command
*/ */
static vshCmdInfo info_domname[] = { static vshCmdInfo info_domname[] = {
{"syntax", "domname <id>"}, {"syntax", "domname <domain>"},
{"help", "convert a domain Id to domain name"}, {"help", "convert a domain Id or UUID to domain name"},
{NULL, NULL} {NULL, NULL}
}; };
static vshCmdOptDef opts_domname[] = { static vshCmdOptDef opts_domname[] = {
{"id", VSH_OT_DATA, VSH_OFLAG_REQ, "domain Id"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain id or uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
static int static int
cmdDomname(vshControl * ctl, vshCmd * cmd) cmdDomname(vshControl * ctl, vshCmd * cmd)
{ {
int found;
int id = vshCommandOptInt(cmd, "id", &found);
virDomainPtr dom; virDomainPtr dom;
if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE; return FALSE;
if (!found) if (!(dom = vshCommandOptDomainBy(ctl, cmd, "domain", NULL,
VSH_DOMBYID|VSH_DOMBYUUID)))
return FALSE; return FALSE;
dom = virDomainLookupByID(ctl->conn, id); vshPrint(ctl, "%s\n", virDomainGetName(dom));
if (dom) { virDomainFree(dom);
vshPrint(ctl, "%s\n", virDomainGetName(dom));
virDomainFree(dom);
} else {
vshError(ctl, FALSE, "failed to get domain '%d'", id);
return FALSE;
}
return TRUE; return TRUE;
} }
@ -887,38 +901,67 @@ cmdDomname(vshControl * ctl, vshCmd * cmd)
* "domid" command * "domid" command
*/ */
static vshCmdInfo info_domid[] = { static vshCmdInfo info_domid[] = {
{"syntax", "domid <name>"}, {"syntax", "domid <domain>"},
{"help", "convert a domain name to domain Id"}, {"help", "convert a domain name or UUID to domain Id"},
{NULL, NULL} {NULL, NULL}
}; };
static vshCmdOptDef opts_domid[] = { static vshCmdOptDef opts_domid[] = {
{"name", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name"}, {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain name or uuid"},
{NULL, 0, 0, NULL} {NULL, 0, 0, NULL}
}; };
static int static int
cmdDomid(vshControl * ctl, vshCmd * cmd) cmdDomid(vshControl * ctl, vshCmd * cmd)
{ {
char *name = vshCommandOptString(cmd, "name", NULL);
virDomainPtr dom; virDomainPtr dom;
if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE; return FALSE;
if (!name) if (!(dom = vshCommandOptDomainBy(ctl, cmd, "domain", NULL,
VSH_DOMBYNAME|VSH_DOMBYUUID)))
return FALSE; return FALSE;
dom = virDomainLookupByName(ctl->conn, name); vshPrint(ctl, "%d\n", virDomainGetID(dom));
if (dom) { virDomainFree(dom);
vshPrint(ctl, "%d\n", virDomainGetID(dom));
virDomainFree(dom);
} else {
vshError(ctl, FALSE, "failed to get domain '%s'", name);
return FALSE;
}
return TRUE; return TRUE;
} }
/*
* "domuuid" command
*/
static vshCmdInfo info_domuuid[] = {
{"syntax", "domuuid <domain>"},
{"help", "convert a domain name or id to domain UUID"},
{NULL, NULL}
};
static vshCmdOptDef opts_domuuid[] = {
{"domain", VSH_OT_DATA, VSH_OFLAG_REQ, "domain id or name"},
{NULL, 0, 0, NULL}
};
static int
cmdDomuuid(vshControl * ctl, vshCmd * cmd)
{
virDomainPtr dom;
char uuid[37];
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE;
if (!(dom = vshCommandOptDomainBy(ctl, cmd, "domain", NULL,
VSH_DOMBYNAME|VSH_DOMBYID)))
return FALSE;
if (virDomainGetUUIDString(dom, uuid) != -1)
vshPrint(ctl, "%s\n", uuid);
else
vshError(ctl, FALSE, "failed to get domain UUID");
return TRUE;
}
/* /*
* "version" command * "version" command
*/ */
@ -1023,6 +1066,7 @@ static vshCmdDef commands[] = {
{"create", cmdCreate, opts_create, info_create}, {"create", cmdCreate, opts_create, info_create},
{"destroy", cmdDestroy, opts_destroy, info_destroy}, {"destroy", cmdDestroy, opts_destroy, info_destroy},
{"domid", cmdDomid, opts_domid, info_domid}, {"domid", cmdDomid, opts_domid, info_domid},
{"domuuid", cmdDomuuid, opts_domuuid, info_domuuid},
{"dominfo", cmdDominfo, opts_dominfo, info_dominfo}, {"dominfo", cmdDominfo, opts_dominfo, info_dominfo},
{"domname", cmdDomname, opts_domname, info_domname}, {"domname", cmdDomname, opts_domname, info_domname},
{"domstate", cmdDomstate, opts_domstate, info_domstate}, {"domstate", cmdDomstate, opts_domstate, info_domstate},
@ -1272,8 +1316,8 @@ vshCommandOptBool(vshCmd * cmd, const char *name)
static virDomainPtr static virDomainPtr
vshCommandOptDomain(vshControl * ctl, vshCmd * cmd, const char *optname, vshCommandOptDomainBy(vshControl * ctl, vshCmd * cmd, const char *optname,
char **name) char **name, int flag)
{ {
virDomainPtr dom = NULL; virDomainPtr dom = NULL;
char *n, *end = NULL; char *n, *end = NULL;
@ -1291,22 +1335,22 @@ vshCommandOptDomain(vshControl * ctl, vshCmd * cmd, const char *optname,
*name = n; *name = n;
/* try it by ID */ /* try it by ID */
id = (int) strtol(n, &end, 10); if (flag & VSH_DOMBYID) {
if (id >= 0 && end && *end == '\0') { id = (int) strtol(n, &end, 10);
vshDebug(ctl, 5, "%s: <%s> seems like domain ID\n", if (id >= 0 && end && *end == '\0') {
cmd->def->name, optname); vshDebug(ctl, 5, "%s: <%s> seems like domain ID\n",
dom = virDomainLookupByID(ctl->conn, id); cmd->def->name, optname);
dom = virDomainLookupByID(ctl->conn, id);
}
} }
/* try it by UUID */ /* try it by UUID */
if (dom==NULL && strlen(n)==36) { if (dom==NULL && (flag & VSH_DOMBYUUID) && strlen(n)==36) {
vshDebug(ctl, 5, "%s: <%s> tring as domain UUID\n", vshDebug(ctl, 5, "%s: <%s> tring as domain UUID\n",
cmd->def->name, optname); cmd->def->name, optname);
dom = virDomainLookupByUUIDString(ctl->conn, (const unsigned char *) n); dom = virDomainLookupByUUIDString(ctl->conn, n);
} }
/* try it by NAME */ /* try it by NAME */
if (!dom) { if (dom==NULL && (flag & VSH_DOMBYNAME)) {
vshDebug(ctl, 5, "%s: <%s> tring as domain NAME\n", vshDebug(ctl, 5, "%s: <%s> tring as domain NAME\n",
cmd->def->name, optname); cmd->def->name, optname);
dom = virDomainLookupByName(ctl->conn, n); dom = virDomainLookupByName(ctl->conn, n);
@ -1715,9 +1759,9 @@ vshInit(vshControl * ctl)
/* basic connection to hypervisor */ /* basic connection to hypervisor */
if (ctl->uid == 0) if (ctl->uid == 0)
ctl->conn = virConnectOpen(NULL); ctl->conn = virConnectOpen(ctl->name);
else else
ctl->conn = virConnectOpenReadOnly(NULL); ctl->conn = virConnectOpenReadOnly(ctl->name);
if (!ctl->conn) if (!ctl->conn)
vshError(ctl, TRUE, "failed to connect to the hypervisor"); vshError(ctl, TRUE, "failed to connect to the hypervisor");
@ -1867,6 +1911,7 @@ vshUsage(vshControl * ctl, const char *cmdname)
if (!cmdname) { if (!cmdname) {
fprintf(stdout, "\n%s [options] [commands]\n\n" fprintf(stdout, "\n%s [options] [commands]\n\n"
" options:\n" " options:\n"
" -c | --connect <name> optional argument currently unused (or used for tests only)\n"
" -d | --debug <num> debug level [0-5]\n" " -d | --debug <num> debug level [0-5]\n"
" -h | --help this help\n" " -h | --help this help\n"
" -q | --quiet quiet mode\n" " -q | --quiet quiet mode\n"
@ -1903,6 +1948,7 @@ vshParseArgv(vshControl * ctl, int argc, char **argv)
{"quiet", 0, 0, 'q'}, {"quiet", 0, 0, 'q'},
{"timing", 0, 0, 't'}, {"timing", 0, 0, 't'},
{"version", 0, 0, 'v'}, {"version", 0, 0, 'v'},
{"connect", 1, 0, 'c'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
@ -1957,6 +2003,9 @@ vshParseArgv(vshControl * ctl, int argc, char **argv)
case 't': case 't':
ctl->timing = TRUE; ctl->timing = TRUE;
break; break;
case 'c':
ctl->name = vshStrdup(ctl, optarg);
break;
case 'v': case 'v':
fprintf(stdout, "%s\n", VERSION); fprintf(stdout, "%s\n", VERSION);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);