[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] cleanup functions which exec OpenVZ tools



Patch improve code which execute external OpenVZ tools.
? src/test.xml
Index: src/openvz_driver.c
===================================================================
RCS file: /data/cvs/libvirt/src/openvz_driver.c,v
retrieving revision 1.28
diff -u -p -r1.28 openvz_driver.c
--- src/openvz_driver.c	11 Jul 2008 11:09:44 -0000	1.28
+++ src/openvz_driver.c	11 Jul 2008 15:50:22 -0000
@@ -125,6 +125,61 @@ static void cmdExecFree(char *cmdExec[])
     }
 }
 
+/* generate arguments to create OpenVZ container
+   return -1 - error
+           0 - OK
+*/
+static int openvzDomainDefineCmd(const char *args[], int maxarg, struct openvz_vm_def *vmdef)
+{
+    int narg;
+
+    if (vmdef == NULL)
+        return -1;
+
+    for (narg = 0; narg < maxarg; narg++)
+        args[narg] = NULL;
+
+    narg = 0;
+    if (narg + 4 < maxarg) {
+        args[narg++] = VZCTL;
+        args[narg++] = "--quiet";
+        args[narg++] = "create";
+        args[narg++] = vmdef->name;
+    } else
+        return -1;
+
+    if ((vmdef->fs.tmpl && *(vmdef->fs.tmpl))) {
+        if (narg + 2 >= maxarg)
+            return -1;
+        args[narg++] = "--ostemplate";
+        args[narg++] = vmdef->fs.tmpl;
+    }
+    if ((vmdef->profile && *(vmdef->profile))) {
+        if (narg + 2 >= maxarg)
+            return -1;
+        args[narg++] = "--config";
+        args[narg++] = vmdef->profile;
+    }
+    if ((vmdef->net.ips->ip && *(vmdef->net.ips->ip))) {
+       if (narg + 2 >= maxarg)
+            return -1;
+        args[narg++] = "--ipadd";
+        args[narg++] = vmdef->net.ips->ip;
+    }
+    if ((vmdef->net.hostname && *(vmdef->net.hostname))) {
+        if (narg + 2 >= maxarg)
+            return -1;
+        args[narg++] = "--hostname";
+        args[narg++] = vmdef->net.hostname;
+    }
+
+    if (args[maxarg-1] != NULL) //for sure
+        return -1;
+
+    return 0;
+}
+
+
 static virDomainPtr openvzDomainLookupByID(virConnectPtr conn,
                                    int id) {
     struct openvz_driver *driver = (struct openvz_driver *)conn->privateData;
@@ -217,12 +272,9 @@ static int openvzDomainGetInfo(virDomain
 }
 
 static int openvzDomainShutdown(virDomainPtr dom) {
-    char cmdbuf[CMDBUF_LEN];
-    int ret;
-    char *cmdExec[OPENVZ_MAX_ARG];
-    int pid, outfd, errfd;
     struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
     struct openvz_vm *vm = openvzFindVMByID(driver, dom->id);
+    const char *prog[] = {VZCTL, "--quiet", "stop", vm->vmdef->name, NULL};
 
     if (!vm) {
         openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
@@ -235,16 +287,8 @@ static int openvzDomainShutdown(virDomai
               _("domain is not in running state"));
         return -1;
     }
-    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " stop %d ", dom->id);
 
-    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
-    {
-        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
-        goto bail_out;
-    }
-
-    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
-    if(ret == -1) {
+    if (virRun(dom->conn, (char **)prog, NULL) < 0) {
         openvzError(dom->conn, VIR_ERR_INTERNAL_ERROR,
               _("Could not exec %s"), VZCTL);
         return -1;
@@ -255,20 +299,14 @@ static int openvzDomainShutdown(virDomai
     ovz_driver.num_inactive ++;
     ovz_driver.num_active --;
 
-bail_out:
-    cmdExecFree(cmdExec);
-
-    return ret;
+    return 0;
 }
 
 static int openvzDomainReboot(virDomainPtr dom,
                               unsigned int flags ATTRIBUTE_UNUSED) {
-    char cmdbuf[CMDBUF_LEN];
-    int ret;
-    char *cmdExec[OPENVZ_MAX_ARG];
-    int pid, outfd, errfd;
     struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
     struct openvz_vm *vm = openvzFindVMByID(driver, dom->id);
+    const char *prog[] = {VZCTL, "--quiet", "restart", vm->vmdef->name, NULL};
 
     if (!vm) {
         openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
@@ -281,24 +319,14 @@ static int openvzDomainReboot(virDomainP
               _("domain is not in running state"));
         return -1;
     }
-    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " restart %d ", dom->id);
 
-    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
-    {
-        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
-        goto bail_out1;
-    }
-    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
-    if(ret == -1) {
+    if (virRun(dom->conn, (char **)prog, NULL) < 0) {
         openvzError(dom->conn, VIR_ERR_INTERNAL_ERROR,
                _("Could not exec %s"), VZCTL);
         return -1;
     }
 
-bail_out1:
-    cmdExecFree(cmdExec);
-
-    return ret;
+    return 0;
 }
 
 static virDomainPtr
@@ -308,63 +336,39 @@ openvzDomainDefineXML(virConnectPtr conn
     struct openvz_vm_def *vmdef = NULL;
     struct openvz_vm *vm = NULL;
     virDomainPtr dom;
-    char cmdbuf[CMDBUF_LEN], cmdOption[CMDOP_LEN], *cmdExec[OPENVZ_MAX_ARG];
-    int ret, pid, outfd, errfd;
+    const char *prog[OPENVZ_MAX_ARG];
 
-    if (!(vmdef = openvzParseVMDef(conn, xml, NULL)))
-        goto bail_out2;
+
+    if ((vmdef = openvzParseVMDef(conn, xml, NULL)) == NULL)
+        return NULL;
 
     vm = openvzFindVMByID(driver, strtoI(vmdef->name));
     if (vm) {
         openvzLog(OPENVZ_ERR, _("Already an OPENVZ VM active with the id '%s'"),
                   vmdef->name);
-        goto bail_out2;
+        return NULL;
     }
     if (!(vm = openvzAssignVMDef(conn, driver, vmdef))) {
         openvzFreeVMDef(vmdef);
         openvzLog(OPENVZ_ERR, "%s", _("Error creating OPENVZ VM"));
     }
 
-    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " create %s", vmdef->name);
-    if ((vmdef->fs.tmpl && *(vmdef->fs.tmpl))) {
-        snprintf(cmdOption, CMDOP_LEN - 1, " --ostemplate %s", vmdef->fs.tmpl);
-        strcat(cmdbuf, cmdOption);
-    }
-    if ((vmdef->profile && *(vmdef->profile))) {
-        snprintf(cmdOption, CMDOP_LEN - 1, " --config %s", vmdef->profile);
-        strcat(cmdbuf, cmdOption);
-    }
-    if ((vmdef->net.ips->ip && *(vmdef->net.ips->ip))) {
-        snprintf(cmdOption, CMDOP_LEN - 1, " --ipadd %s", vmdef->net.ips->ip);
-        strcat(cmdbuf, cmdOption);
-    }
-    if ((vmdef->net.hostname && *(vmdef->net.hostname))) {
-        snprintf(cmdOption, CMDOP_LEN - 1, " --hostname %s", vmdef->net.hostname);
-        strcat(cmdbuf, cmdOption);
+    if (openvzDomainDefineCmd(prog, OPENVZ_MAX_ARG, vmdef) < 0) {
+        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
+                _("Error creating command for container"));
+        return NULL;
     }
 
-    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
-    {
-        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
-        goto bail_out2;
-    }
-    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
-    if(ret == -1) {
+    if (virRun(conn, (char **)prog, NULL) < 0) {
         openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                _("Could not exec %s"), VZCTL);
-        goto bail_out2;
+        return NULL;
     }
 
-    waitpid(pid, NULL, 0);
-    cmdExecFree(cmdExec);
-
     dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
     if (dom)
         dom->id = vm->vpsid;
     return dom;
-bail_out2:
-    cmdExecFree(cmdExec);
-    return NULL;
 }
 
 static virDomainPtr
@@ -375,8 +379,8 @@ openvzDomainCreateLinux(virConnectPtr co
     struct openvz_vm *vm = NULL;
     virDomainPtr dom;
     struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
-    char cmdbuf[CMDBUF_LEN], cmdOption[CMDOP_LEN], *cmdExec[OPENVZ_MAX_ARG];
-    int ret, pid, outfd, errfd;
+    const char *progcreate[OPENVZ_MAX_ARG];
+    const char *progstart[] = {VZCTL, "--quiet", "start", NULL, NULL};
 
     if (!(vmdef = openvzParseVMDef(conn, xml, NULL)))
         return NULL;
@@ -394,48 +398,21 @@ openvzDomainCreateLinux(virConnectPtr co
         return NULL;
     }
 
-    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " create %s", vmdef->name);
-    if ((vmdef->fs.tmpl && *(vmdef->fs.tmpl))) {
-        snprintf(cmdOption, CMDOP_LEN - 1, " --ostemplate %s", vmdef->fs.tmpl);
-        strcat(cmdbuf, cmdOption);
-    }
-    if ((vmdef->profile && *(vmdef->profile))) {
-        snprintf(cmdOption, CMDOP_LEN - 1, " --config %s", vmdef->profile);
-        strcat(cmdbuf, cmdOption);
-    }
-    if ((vmdef->net.ips->ip && *(vmdef->net.ips->ip))) {
-        snprintf(cmdOption, CMDOP_LEN - 1, " --ipadd %s", vmdef->net.ips->ip);
-        strcat(cmdbuf, cmdOption);
-    }
-    if ((vmdef->net.hostname && *(vmdef->net.hostname))) {
-        snprintf(cmdOption, CMDOP_LEN - 1, " --hostname %s", vmdef->net.hostname);
-        strcat(cmdbuf, cmdOption);
+    if (openvzDomainDefineCmd(progcreate, OPENVZ_MAX_ARG, vmdef) < 0) {
+        openvzError(conn, VIR_ERR_INTERNAL_ERROR,
+                _("Error creating command for container"));
+        return NULL;
     }
 
-    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
-    {
-        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
-        goto bail_out3;
-    }
-    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
-    if(ret == -1) {
+    if (virRun(conn, (char **)progcreate, NULL) < 0) {
         openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                _("Could not exec %s"), VZCTL);
         return NULL;
     }
 
-    waitpid(pid, NULL, 0);
-    cmdExecFree(cmdExec);
-
-    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " start %s ", vmdef->name);
+    progstart[3] = vmdef->name;
 
-    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
-    {
-        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
-        goto bail_out3;
-    }
-    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
-    if(ret == -1) {
+    if (virRun(conn, (char **)progstart, NULL) < 0) {
         openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                _("Could not exec %s"), VZCTL);
         return NULL;
@@ -446,28 +423,18 @@ openvzDomainCreateLinux(virConnectPtr co
     ovz_driver.num_inactive--;
     ovz_driver.num_active++;
 
-    waitpid(pid, NULL, 0);
-    cmdExecFree(cmdExec);
-
     dom = virGetDomain(conn, vm->vmdef->name, vm->vmdef->uuid);
     if (dom)
         dom->id = vm->vpsid;
     return dom;
-bail_out3:
-    cmdExecFree(cmdExec);
-    return NULL;
 }
 
 static int
 openvzDomainCreate(virDomainPtr dom)
 {
-    char cmdbuf[CMDBUF_LEN];
-    int ret;
-    char *cmdExec[OPENVZ_MAX_ARG] ;
-    int pid, outfd, errfd;
     struct openvz_driver *driver = (struct openvz_driver *)dom->conn->privateData;
     struct openvz_vm *vm = openvzFindVMByName(driver, dom->name);
-    struct openvz_vm_def *vmdef;
+    const char *prog[] = {VZCTL, "--quiet", "start", vm->vmdef->name, NULL };
 
     if (!vm) {
         openvzError(dom->conn, VIR_ERR_INVALID_DOMAIN,
@@ -481,41 +448,27 @@ openvzDomainCreate(virDomainPtr dom)
         return -1;
     }
 
-    vmdef = vm->vmdef;
-    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " start %s ", vmdef->name);
-
-    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
-    {
-        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
-        goto bail_out4;
-    }
-    ret = virExec(dom->conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
-    if(ret == -1) {
+    if (virRun(dom->conn, (char **)prog, NULL) < 0) {
         openvzError(dom->conn, VIR_ERR_INTERNAL_ERROR,
                _("Could not exec %s"), VZCTL);
         return -1;
     }
 
-    sscanf(vmdef->name, "%d", &vm->vpsid);
+    sscanf(vm->vmdef->name, "%d", &vm->vpsid);
     vm->status = VIR_DOMAIN_RUNNING;
     ovz_driver.num_inactive --;
     ovz_driver.num_active ++;
 
-    waitpid(pid, NULL, 0);
-bail_out4:
-    cmdExecFree(cmdExec);
-
-    return ret;
+    return 0;
 }
 
 static int
 openvzDomainUndefine(virDomainPtr dom)
 {
-    char cmdbuf[CMDBUF_LEN], *cmdExec[OPENVZ_MAX_ARG];
-    int ret, pid, outfd, errfd;
     virConnectPtr conn= dom->conn;
     struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
     struct openvz_vm *vm = openvzFindVMByUUID(driver, dom->uuid);
+    const char *prog[] = { VZCTL, "--quiet", "destroy", vm->vmdef->name, NULL };
 
     if (!vm) {
         openvzError(conn, VIR_ERR_INVALID_DOMAIN, _("no domain with matching uuid"));
@@ -526,25 +479,15 @@ openvzDomainUndefine(virDomainPtr dom)
         openvzError(conn, VIR_ERR_INTERNAL_ERROR, _("cannot delete active domain"));
         return -1;
     }
-    snprintf(cmdbuf, CMDBUF_LEN - 1, VZCTL " destroy %s ", vm->vmdef->name);
 
-    if((ret = convCmdbufExec(cmdbuf, cmdExec)) == -1)
-    {
-        openvzLog(OPENVZ_ERR, "%s", _("Error in parsing Options to OPENVZ"));
-        goto bail_out5;
-    }
-    ret = virExec(conn, (char **)cmdExec, &pid, -1, &outfd, &errfd);
-    if(ret == -1) {
+    if (virRun(conn, (char **)prog, NULL) < 0) {
         openvzError(conn, VIR_ERR_INTERNAL_ERROR,
                _("Could not exec %s"), VZCTL);
         return -1;
     }
 
-    waitpid(pid, NULL, 0);
     openvzRemoveInactiveVM(driver, vm);
-bail_out5:
-    cmdExecFree(cmdExec);
-    return ret;
+    return 0;
 }
 
 static int
@@ -553,7 +496,7 @@ openvzDomainSetAutostart(virDomainPtr do
     virConnectPtr conn= dom->conn;
     struct openvz_driver *driver = (struct openvz_driver *) conn->privateData;
     struct openvz_vm *vm = openvzFindVMByUUID(driver, dom->uuid);
-    const char *prog[] = { VZCTL, "set", vm->vmdef->name,
+    const char *prog[] = { VZCTL, "--quiet", "set", vm->vmdef->name,
                            "--onboot", autostart ? "yes" : "no",
                            "--save", NULL };
 

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]