1
0

openvz: simplify openvzDomainDefineCmd by using virCommandPtr

This commit is contained in:
Guido Günther 2012-05-05 08:47:05 +02:00
parent 05abd1507d
commit f300c194fd

View File

@ -100,68 +100,31 @@ static void cmdExecFree(const char *cmdExec[])
/* generate arguments to create OpenVZ container /* generate arguments to create OpenVZ container
return -1 - error return -1 - error
0 - OK 0 - OK
Caller has to free the cmd
*/ */
static int static virCommandPtr
openvzDomainDefineCmd(const char *args[], openvzDomainDefineCmd(virDomainDefPtr vmdef)
int maxarg, virDomainDefPtr vmdef)
{ {
int narg; virCommandPtr cmd = virCommandNewArgList(VZCTL,
"--quiet",
for (narg = 0; narg < maxarg; narg++) "create",
args[narg] = NULL; NULL);
if (vmdef == NULL) { if (vmdef == NULL) {
openvzError(VIR_ERR_INTERNAL_ERROR, "%s", openvzError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Container is not defined")); _("Container is not defined"));
return -1; virCommandFree(cmd);
return NULL;
} }
#define ADD_ARG(thisarg) \ virCommandAddArgList(cmd, vmdef->name, "--name", vmdef->name, NULL);
do { \
if (narg >= maxarg) \
goto no_memory; \
args[narg++] = thisarg; \
} while (0)
#define ADD_ARG_LIT(thisarg) \
do { \
if (narg >= maxarg) \
goto no_memory; \
if ((args[narg++] = strdup(thisarg)) == NULL) \
goto no_memory; \
} while (0)
narg = 0;
ADD_ARG_LIT(VZCTL);
ADD_ARG_LIT("--quiet");
ADD_ARG_LIT("create");
ADD_ARG_LIT(vmdef->name);
ADD_ARG_LIT("--name");
ADD_ARG_LIT(vmdef->name);
if (vmdef->nfss == 1 && if (vmdef->nfss == 1 &&
vmdef->fss[0]->type == VIR_DOMAIN_FS_TYPE_TEMPLATE) { vmdef->fss[0]->type == VIR_DOMAIN_FS_TYPE_TEMPLATE) {
ADD_ARG_LIT("--ostemplate"); virCommandAddArgList(cmd, "--ostemplate", vmdef->fss[0]->src, NULL);
ADD_ARG_LIT(vmdef->fss[0]->src);
} }
#if 0
if ((vmdef->profile && *(vmdef->profile))) {
ADD_ARG_LIT("--config");
ADD_ARG_LIT(vmdef->profile);
}
#endif
ADD_ARG(NULL); return cmd;
return 0;
no_memory:
openvzError(VIR_ERR_INTERNAL_ERROR,
_("Could not put argument to %s"), VZCTL);
return -1;
#undef ADD_ARG
#undef ADD_ARG_LIT
} }
@ -170,8 +133,7 @@ static int openvzSetInitialConfig(virDomainDefPtr vmdef)
int ret = -1; int ret = -1;
int vpsid; int vpsid;
char * confdir = NULL; char * confdir = NULL;
const char *prog[OPENVZ_MAX_ARG]; virCommandPtr cmd = NULL;
prog[0] = NULL;
if (vmdef->nfss > 1) { if (vmdef->nfss > 1) {
openvzError(VIR_ERR_INTERNAL_ERROR, "%s", openvzError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -210,24 +172,18 @@ static int openvzSetInitialConfig(virDomainDefPtr vmdef)
_("Could not set the source dir for the filesystem")); _("Could not set the source dir for the filesystem"));
goto cleanup; goto cleanup;
} }
} } else {
else cmd = openvzDomainDefineCmd(vmdef);
{ if (virCommandRun(cmd, NULL) < 0)
if (openvzDomainDefineCmd(prog, OPENVZ_MAX_ARG, vmdef) < 0) {
VIR_ERROR(_("Error creating command for container"));
goto cleanup; goto cleanup;
}
if (virRun(prog, NULL) < 0) {
goto cleanup;
}
} }
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(confdir); VIR_FREE(confdir);
cmdExecFree(prog); virCommandFree(cmd);
return ret; return ret;
} }