diff --git a/ChangeLog b/ChangeLog index 380e78f0f3..132032c95e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Wed Feb 20 10:23:27 EST 2008 Daniel P. Berrange + + * src/virsh.c: Added convenience methods for creating pools + and volumes without XML + * src/buf.c, src/buf.h, src/libvirt_sym.version: Export the + virBuffer* methods to virsh + * src/xend_internal.c, src/xm_intenral.c, src/xml.c, + src/qemu_conf.c + * src/util.c, src/util.h: Export virStrToLong_ull to virsh + Wed Feb 20 10:22:27 EST 2008 Daniel P. Berrange * src/virsh.c: Added commands for all storage APIs diff --git a/src/buf.c b/src/buf.c index fe3ead55b9..72d9f528a6 100644 --- a/src/buf.c +++ b/src/buf.c @@ -61,7 +61,7 @@ virBufferGrow(virBufferPtr buf, unsigned int len) * Returns 0 successful, -1 in case of internal or API error. */ int -virBufferAdd(virBufferPtr buf, const char *str, int len) +__virBufferAdd(virBufferPtr buf, const char *str, int len) { unsigned int needSize; @@ -97,7 +97,7 @@ virBufferAdd(virBufferPtr buf, const char *str, int len) * Returns 0 if successful, -1 in the case of error. */ int -virBufferAddChar (virBufferPtr buf, char c) +__virBufferAddChar (virBufferPtr buf, char c) { unsigned int needSize; @@ -190,7 +190,7 @@ virBufferContentAndFree (virBufferPtr buf) * Returns 0 successful, -1 in case of internal or API error. */ int -virBufferVSprintf(virBufferPtr buf, const char *format, ...) +__virBufferVSprintf(virBufferPtr buf, const char *format, ...) { int size, count, grow_size; va_list locarg, argptr; @@ -198,6 +198,11 @@ virBufferVSprintf(virBufferPtr buf, const char *format, ...) if ((format == NULL) || (buf == NULL)) { return (-1); } + + if (buf->size == 0 && + virBufferGrow(buf, 100) < 0) + return -1; + size = buf->size - buf->use - 1; va_start(argptr, format); va_copy(locarg, argptr); diff --git a/src/buf.h b/src/buf.h index 275cd40f44..fc15fe1286 100644 --- a/src/buf.h +++ b/src/buf.h @@ -29,15 +29,19 @@ struct _virBuffer { virBufferPtr virBufferNew(unsigned int size); void virBufferFree(virBufferPtr buf); char *virBufferContentAndFree(virBufferPtr buf); -int virBufferAdd(virBufferPtr buf, const char *str, int len); -int virBufferAddChar(virBufferPtr buf, char c); -int virBufferVSprintf(virBufferPtr buf, const char *format, ...) +int __virBufferAdd(virBufferPtr buf, const char *str, int len); +int __virBufferAddChar(virBufferPtr buf, char c); +int __virBufferVSprintf(virBufferPtr buf, const char *format, ...) ATTRIBUTE_FORMAT(printf, 2, 3); int virBufferStrcat(virBufferPtr buf, ...); int virBufferEscapeString(virBufferPtr buf, const char *format, const char *str); int virBufferURIEncodeString (virBufferPtr buf, const char *str); #define virBufferAddLit(buf_, literal_string_) \ - virBufferAdd (buf_, "" literal_string_ "", sizeof literal_string_ - 1) + __virBufferAdd (buf_, "" literal_string_ "", sizeof literal_string_ - 1) + +#define virBufferAdd(b,s,l) __virBufferAdd((b),(s),(l)) +#define virBufferAddChar(b,c) __virBufferAddChar((b),(c)) +#define virBufferVSprintf(b,f,...) __virBufferVSprintf((b),(f), __VA_ARGS__) #endif /* __VIR_BUFFER_H__ */ diff --git a/src/libvirt_sym.version b/src/libvirt_sym.version index 995d16f89b..3c46a745c7 100644 --- a/src/libvirt_sym.version +++ b/src/libvirt_sym.version @@ -178,6 +178,11 @@ __virFileReadAll; __virStrToLong_i; + __virStrToLong_ull; + + __virBufferVSprintf; + __virBufferAdd; + __virBufferAddChar; local: *; }; diff --git a/src/qemu_conf.c b/src/qemu_conf.c index e39c7bcce8..ac1cc1eb32 100644 --- a/src/qemu_conf.c +++ b/src/qemu_conf.c @@ -2818,7 +2818,7 @@ char *qemudGenerateXML(virConnectPtr conn, if (virBufferAddLit(buf, " \n") < 0) goto no_memory; - if (virBufferVSprintf(buf, " \n") < 0) + if (virBufferAddLit(buf, " \n") < 0) goto no_memory; disk = disk->next; @@ -2889,7 +2889,7 @@ char *qemudGenerateXML(virConnectPtr conn, } } - if (virBufferVSprintf(buf, " \n") < 0) + if (virBufferAddLit(buf, " \n") < 0) goto no_memory; net = net->next; @@ -2974,7 +2974,7 @@ char *qemudGenerateNetworkXML(virConnectPtr conn, if (!buf) goto no_memory; - if (virBufferVSprintf(buf, "\n") < 0) + if (virBufferAddLit(buf, "\n") < 0) goto no_memory; if (virBufferVSprintf(buf, " %s\n", def->name) < 0) diff --git a/src/util.c b/src/util.c index f082984e90..19131a793f 100644 --- a/src/util.c +++ b/src/util.c @@ -613,7 +613,7 @@ virStrToLong_ll(char const *s, char **end_ptr, int base, long long *result) /* Just like virStrToLong_i, above, but produce an "unsigned long long" value. */ int -virStrToLong_ull(char const *s, char **end_ptr, int base, unsigned long long *result) +__virStrToLong_ull(char const *s, char **end_ptr, int base, unsigned long long *result) { unsigned long long val; char *p; diff --git a/src/util.h b/src/util.h index da1f5b0db1..7080ed0e03 100644 --- a/src/util.h +++ b/src/util.h @@ -70,9 +70,10 @@ int virStrToLong_ll(char const *s, char **end_ptr, int base, long long *result); -int virStrToLong_ull(char const *s, - char **end_ptr, - int base, - unsigned long long *result); +int __virStrToLong_ull(char const *s, + char **end_ptr, + int base, + unsigned long long *result); +#define virStrToLong_ull(s,e,b,r) __virStrToLong_ull((s),(e),(b),(r)) #endif /* __VIR_UTIL_H__ */ diff --git a/src/virsh.c b/src/virsh.c index cee99c5d2b..5a5f04e4fc 100644 --- a/src/virsh.c +++ b/src/virsh.c @@ -48,6 +48,7 @@ #include "console.h" #include "util.h" +#include "buf.h" static char *progname; @@ -240,6 +241,9 @@ static vshCmdOpt *vshCommandOpt(vshCmd * cmd, const char *name); static int vshCommandOptInt(vshCmd * cmd, const char *name, int *found); static char *vshCommandOptString(vshCmd * cmd, const char *name, int *found); +#if 0 +static int vshCommandOptStringList(vshCmd * cmd, const char *name, char ***data); +#endif static int vshCommandOptBool(vshCmd * cmd, const char *name); #define VSH_BYID (1 << 1) @@ -2851,6 +2855,100 @@ cmdPoolCreate(vshControl * ctl, vshCmd * cmd) return ret; } +/* + * "pool-create-as" command + */ +static vshCmdInfo info_pool_create_as[] = { + {"syntax", "pool-create-as "}, + {"help", gettext_noop("create a pool from a set of args")}, + {"desc", gettext_noop("Create a pool.")}, + {NULL, NULL} +}; + +static vshCmdOptDef opts_pool_create_as[] = { + {"name", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("name of the pool")}, + {"type", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("type of the pool")}, + {"source-host", VSH_OT_DATA, 0, gettext_noop("source-host for underlying storage")}, + {"source-path", VSH_OT_DATA, 0, gettext_noop("source path for underlying storage")}, + {"source-dev", VSH_OT_DATA, 0, gettext_noop("source device for underlying storage")}, + {"target", VSH_OT_DATA, 0, gettext_noop("target for underlying storage")}, + {NULL, 0, 0, NULL} +}; + + +static int +cmdPoolCreateAs(vshControl * ctl, vshCmd * cmd) +{ + virStoragePoolPtr pool; + int found; + char *name, *type, *srcHost, *srcPath, *srcDev, *target; + virBuffer buf; + + memset(&buf, 0, sizeof(buf)); + + if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) + return FALSE; + + name = vshCommandOptString(cmd, "name", &found); + if (!found) + goto cleanup; + type = vshCommandOptString(cmd, "type", &found); + if (!found) + goto cleanup; + + srcHost = vshCommandOptString(cmd, "source-host", &found); + srcPath = vshCommandOptString(cmd, "source-path", &found); + srcDev = vshCommandOptString(cmd, "source-dev", &found); + target = vshCommandOptString(cmd, "target", &found); + + if (virBufferVSprintf(&buf, "\n", type) < 0) + goto cleanup; + if (virBufferVSprintf(&buf, " %s\n", name) < 0) + goto cleanup; + if (srcHost || srcPath || srcDev) { + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + if (srcHost && + virBufferVSprintf(&buf, " \n", srcHost) < 0) + goto cleanup; + if (srcPath && + virBufferVSprintf(&buf, " \n", srcPath) < 0) + goto cleanup; + if (srcDev && + virBufferVSprintf(&buf, " \n", srcDev) < 0) + goto cleanup; + + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + } + if (target) { + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + if (virBufferVSprintf(&buf, " %s\n", target) < 0) + goto cleanup; + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + } + if (virBufferAddLit(&buf, "\n") < 0) + goto cleanup; + + pool = virStoragePoolCreateXML(ctl->conn, buf.content, 0); + free (buf.content); + + if (pool != NULL) { + vshPrint(ctl, _("Pool %s created\n"), name); + virStoragePoolFree(pool); + return TRUE; + } else { + vshError(ctl, FALSE, _("Failed to create pool %s"), name); + return FALSE; + } + + cleanup: + free(buf.content); + return FALSE; +} + /* * "pool-define" command @@ -2900,6 +2998,101 @@ cmdPoolDefine(vshControl * ctl, vshCmd * cmd) } +/* + * "pool-define-as" command + */ +static vshCmdInfo info_pool_define_as[] = { + {"syntax", "pool-define-as "}, + {"help", gettext_noop("define a pool from a set of args")}, + {"desc", gettext_noop("Define a pool.")}, + {NULL, NULL} +}; + +static vshCmdOptDef opts_pool_define_as[] = { + {"name", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("name of the pool")}, + {"type", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("type of the pool")}, + {"source-host", VSH_OT_DATA, 0, gettext_noop("source-host for underlying storage")}, + {"source-path", VSH_OT_DATA, 0, gettext_noop("source path for underlying storage")}, + {"source-dev", VSH_OT_DATA, 0, gettext_noop("source device for underlying storage")}, + {"target", VSH_OT_DATA, 0, gettext_noop("target for underlying storage")}, + {NULL, 0, 0, NULL} +}; + + +static int +cmdPoolDefineAs(vshControl * ctl, vshCmd * cmd) +{ + virStoragePoolPtr pool; + int found; + char *name, *type, *srcHost, *srcPath, *srcDev, *target; + virBuffer buf; + + memset(&buf, 0, sizeof(buf)); + + if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) + return FALSE; + + name = vshCommandOptString(cmd, "name", &found); + if (!found) + goto cleanup; + type = vshCommandOptString(cmd, "type", &found); + if (!found) + goto cleanup; + + srcHost = vshCommandOptString(cmd, "source-host", &found); + srcPath = vshCommandOptString(cmd, "source-path", &found); + srcDev = vshCommandOptString(cmd, "source-dev", &found); + target = vshCommandOptString(cmd, "target", &found); + + if (virBufferVSprintf(&buf, "\n", type) < 0) + goto cleanup; + if (virBufferVSprintf(&buf, " %s\n", name) < 0) + goto cleanup; + if (srcHost || srcPath || srcDev) { + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + if (srcHost && + virBufferVSprintf(&buf, " %s\n", srcHost) < 0) + goto cleanup; + if (srcPath && + virBufferVSprintf(&buf, " %s\n", srcPath) < 0) + goto cleanup; + if (srcDev && + virBufferVSprintf(&buf, " %s\n", srcDev) < 0) + goto cleanup; + + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + } + if (target) { + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + if (virBufferVSprintf(&buf, " %s\n", target) < 0) + goto cleanup; + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + } + if (virBufferAddLit(&buf, "\n") < 0) + goto cleanup; + + pool = virStoragePoolDefineXML(ctl->conn, buf.content, 0); + free (buf.content); + + if (pool != NULL) { + vshPrint(ctl, _("Pool %s defined\n"), name); + virStoragePoolFree(pool); + return TRUE; + } else { + vshError(ctl, FALSE, _("Failed to define pool %s"), name); + return FALSE; + } + + cleanup: + free(buf.content); + return FALSE; +} + + /* * "pool-build" command */ @@ -3391,6 +3584,131 @@ cmdPoolStart(vshControl * ctl, vshCmd * cmd) } +/* + * "vol-create-as" command + */ +static vshCmdInfo info_vol_create_as[] = { + {"syntax", "create-as "}, + {"help", gettext_noop("create a vol from a set of as")}, + {"desc", gettext_noop("Create a vol.")}, + {NULL, NULL} +}; + +static vshCmdOptDef opts_vol_create_as[] = { + {"pool", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("pool name")}, + {"name", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("name of the vol")}, + {"capacity", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("size of the vol with optional k,M,G,T suffix")}, + {"allocation", VSH_OT_DATA, 0, gettext_noop("initial allocation size with optional k,M,G,T suffix")}, + {"format", VSH_OT_DATA, 0, gettext_noop("file format type raw,bochs,qcow,qcow2,vmdk")}, + {NULL, 0, 0, NULL} +}; + +static int cmdVolSize(const char *data, unsigned long long *val) +{ + char *end; + if (virStrToLong_ull(data, &end, 10, val) < 0) + return -1; + + if (end && *end) { + /* Delibrate fallthrough cases here :-) */ + switch (*end) { + case 'T': + *val *= 1024; + case 'G': + *val *= 1024; + case 'M': + *val *= 1024; + case 'k': + *val *= 1024; + break; + default: + return -1; + } + end++; + if (*end) + return -1; + } + return 0; +} + +static int +cmdVolCreateAs(vshControl * ctl, vshCmd * cmd) +{ + virStoragePoolPtr pool; + virStorageVolPtr vol; + int found; + char *name, *capacityStr, *allocationStr, *format; + unsigned long long capacity, allocation = 0; + virBuffer buf; + + memset(&buf, 0, sizeof(buf)); + + if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) + return FALSE; + + if (!(pool = vshCommandOptPoolBy(ctl, cmd, "pool", NULL, + VSH_BYNAME))) + return FALSE; + + name = vshCommandOptString(cmd, "name", &found); + if (!found) + goto cleanup; + + capacityStr = vshCommandOptString(cmd, "capacity", &found); + if (!found) + goto cleanup; + if (cmdVolSize(capacityStr, &capacity) < 0) + vshError(ctl, FALSE, _("Malformed size %s"), capacityStr); + + allocationStr = vshCommandOptString(cmd, "allocation", &found); + if (allocationStr && + cmdVolSize(allocationStr, &allocation) < 0) + vshError(ctl, FALSE, _("Malformed size %s"), allocationStr); + + format = vshCommandOptString(cmd, "format", &found); + + if (virBufferAddLit(&buf, "\n") < 0) + goto cleanup; + if (virBufferVSprintf(&buf, " %s\n", name) < 0) + goto cleanup; + if (virBufferVSprintf(&buf, " %llu\n", capacity) < 0) + goto cleanup; + if (allocationStr && + virBufferVSprintf(&buf, " %llu\n", allocation) < 0) + goto cleanup; + + if (format) { + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + if (format) + if (virBufferVSprintf(&buf, " \n",format) < 0) + goto cleanup; + if (virBufferAddLit(&buf, " \n") < 0) + goto cleanup; + } + if (virBufferAddLit(&buf, "\n") < 0) + goto cleanup; + + vol = virStorageVolCreateXML(pool, buf.content, 0); + free (buf.content); + virStoragePoolFree(pool); + + if (vol != NULL) { + vshPrint(ctl, _("Vol %s created\n"), name); + virStorageVolFree(vol); + return TRUE; + } else { + vshError(ctl, FALSE, _("Failed to create vol %s"), name); + return FALSE; + } + + cleanup: + free(buf.content); + virStoragePoolFree(pool); + return FALSE; +} + + /* * "pool-undefine" command */ @@ -4802,7 +5120,9 @@ static vshCmdDef commands[] = { {"pool-autostart", cmdPoolAutostart, opts_pool_autostart, info_pool_autostart}, {"pool-build", cmdPoolBuild, opts_pool_build, info_pool_build}, {"pool-create", cmdPoolCreate, opts_pool_create, info_pool_create}, + {"pool-create-as", cmdPoolCreateAs, opts_pool_create_as, info_pool_create_as}, {"pool-define", cmdPoolDefine, opts_pool_define, info_pool_define}, + {"pool-define-as", cmdPoolDefineAs, opts_pool_define_as, info_pool_define_as}, {"pool-destroy", cmdPoolDestroy, opts_pool_destroy, info_pool_destroy}, {"pool-delete", cmdPoolDelete, opts_pool_delete, info_pool_delete}, {"pool-dumpxml", cmdPoolDumpXML, opts_pool_dumpxml, info_pool_dumpxml}, @@ -4831,6 +5151,7 @@ static vshCmdDef commands[] = { {"uri", cmdURI, NULL, info_uri}, {"vol-create", cmdVolCreate, opts_vol_create, info_vol_create}, + {"vol-create-as", cmdVolCreateAs, opts_vol_create_as, info_vol_create_as}, {"vol-delete", cmdVolDelete, opts_vol_delete, info_vol_delete}, {"vol-dumpxml", cmdVolDumpXML, opts_vol_dumpxml, info_vol_dumpxml}, {"vol-info", cmdVolInfo, opts_vol_info, info_vol_info}, @@ -5071,6 +5392,32 @@ vshCommandOptString(vshCmd * cmd, const char *name, int *found) return arg && arg->data && *arg->data ? arg->data : NULL; } +#if 0 +static int +vshCommandOptStringList(vshCmd * cmd, const char *name, char ***data) +{ + vshCmdOpt *arg = cmd->opts; + char **val = NULL; + int nval = 0; + + while (arg) { + if (arg->def && STREQ(arg->def->name, name)) { + char **tmp = realloc(val, sizeof(*tmp) * (nval+1)); + if (!tmp) { + free(val); + return -1; + } + val = tmp; + val[nval++] = arg->data; + } + arg = arg->next; + } + + *data = val; + return nval; +} +#endif + /* * Returns TRUE/FALSE if the option exists */ diff --git a/src/xend_internal.c b/src/xend_internal.c index 6dbd4fb936..bfd793557e 100644 --- a/src/xend_internal.c +++ b/src/xend_internal.c @@ -1440,7 +1440,7 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root, virBufferVSprintf(&buf, " %s\n", tmp); } else if (sexpr_has(root, "domain/bootloader")) { bootloader = 1; - virBufferVSprintf(&buf, " \n"); + virBufferAddLit(&buf, " \n"); } tmp = sexpr_node(root, "domain/bootloader_args"); if (tmp != NULL && bootloader) { @@ -1464,12 +1464,12 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root, max_mem = cur_mem; virBufferVSprintf(&buf, " %d\n", max_mem); if ((cur_mem >= MIN_XEN_GUEST_SIZE) && (cur_mem != max_mem)) - virBufferVSprintf(&buf, " %d\n", - cur_mem); + virBufferVSprintf(&buf, " %d\n", + cur_mem); - virBufferVSprintf(&buf, " %d\n", sexpr_int(root, "domain/vcpus")); @@ -1646,7 +1646,7 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root, } } else { /* This case is the cdrom device only */ - virBufferVSprintf(&buf, " \n"); + virBufferAddLit(&buf, " \n"); } virBufferVSprintf(&buf, " \n", dst); @@ -1654,9 +1654,9 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root, /* XXX should we force mode == r, if cdrom==1, or assume xend has already done this ? */ if ((mode != NULL) && (!strcmp(mode, "r"))) - virBufferVSprintf(&buf, " \n"); + virBufferAddLit(&buf, " \n"); else if ((mode != NULL) && (!strcmp(mode, "w!"))) - virBufferVSprintf(&buf, " \n"); + virBufferAddLit(&buf, " \n"); virBufferAddLit(&buf, " \n"); bad_parse: @@ -1667,12 +1667,12 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root, tmp2 = sexpr_node(node, "device/vif/script"); tmp = sexpr_node(node, "device/vif/bridge"); if ((tmp2 && strstr(tmp2, "bridge")) || tmp) { - virBufferVSprintf(&buf, " \n"); + virBufferAddLit(&buf, " \n"); if (tmp != NULL) virBufferVSprintf(&buf, " \n", tmp); } else { - virBufferVSprintf(&buf, " \n"); + virBufferAddLit(&buf, " \n"); } tmp = sexpr_node(node, "device/vif/vifname"); diff --git a/src/xm_internal.c b/src/xm_internal.c index ec2e7919f0..711774bd27 100644 --- a/src/xm_internal.c +++ b/src/xm_internal.c @@ -667,7 +667,7 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) { val = MIN_XEN_GUEST_SIZE * 2; virBufferVSprintf(buf, " %ld\n", val * 1024); - virBufferVSprintf(buf, " 0)) { - virBufferVSprintf(&buf, "(bootloader)"); + virBufferAddLit(&buf, "(bootloader)"); /* * if using a bootloader, the kernel and initrd strings are not * significant and should be discarded