[PATCH]1/3 OpenVZ driver : Cleanups

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

 



Cleanup patch, also adds utils.c and utils.h to Makefile.am

-- 
Shuveb Hussain

Unix is very user friendly. It is just a 
little choosy about who its friends are
http://www.binarykarma.com
Index: include/libvirt/virterror.h
===================================================================
RCS file: /data/cvs/libvirt/include/libvirt/virterror.h,v
retrieving revision 1.27
diff -u -a -r1.27 virterror.h
--- include/libvirt/virterror.h	13 Jul 2007 08:26:57 -0000	1.27
+++ include/libvirt/virterror.h	19 Jul 2007 12:48:04 -0000
@@ -51,6 +51,7 @@
     VIR_FROM_NET,       /* Error when operating on a network */
     VIR_FROM_TEST,	/* Error from test driver */
     VIR_FROM_REMOTE,	/* Error from remote driver */
+    VIR_FROM_OPENVZ,    /* Error from OpenVZ driver */
 } virErrorDomain;
 
 
Index: src/Makefile.am
===================================================================
RCS file: /data/cvs/libvirt/src/Makefile.am,v
retrieving revision 1.45
diff -u -a -r1.45 Makefile.am
--- src/Makefile.am	17 Jul 2007 13:27:26 -0000	1.45
+++ src/Makefile.am	19 Jul 2007 12:48:04 -0000
@@ -49,7 +49,8 @@
 		qemu_driver.c qemu_driver.h 			\
 		qemu_conf.c qemu_conf.h					\
 		openvz_conf.c openvz_conf.h				\
-		openvz_driver.c openvz_driver.h 			
+		openvz_driver.c openvz_driver.h 		\
+		util.c util.h
 
 SERVER_SOURCES = 						\
 		../qemud/protocol.h ../qemud/protocol.c		\
Index: src/openvz_conf.c
===================================================================
RCS file: /data/cvs/libvirt/src/openvz_conf.c,v
retrieving revision 1.2
diff -u -a -r1.2 openvz_conf.c
--- src/openvz_conf.c	17 Jul 2007 14:40:26 -0000	1.2
+++ src/openvz_conf.c	19 Jul 2007 12:48:04 -0000
@@ -57,7 +57,8 @@
                      errmsg, info);
 }
 
-struct openvz_vm *openvzFindVMByID(const struct openvz_driver *driver, int id) {
+struct openvz_vm 
+*openvzFindVMByID(const struct openvz_driver *driver, int id) {
     struct openvz_vm *vm = driver->vms;
 
     while (vm) {
@@ -69,7 +70,8 @@
     return NULL;
 }
 
-struct openvz_vm *openvzFindVMByUUID(const struct openvz_driver *driver,
+struct openvz_vm 
+*openvzFindVMByUUID(const struct openvz_driver *driver,
                                    const unsigned char *uuid) {
     struct openvz_vm *vm = driver->vms;
 
@@ -82,7 +84,8 @@
     return NULL;
 }
 
-struct openvz_vm *openvzFindVMByName(const struct openvz_driver *driver,
+struct openvz_vm 
+*openvzFindVMByName(const struct openvz_driver *driver,
                                    const char *name) {
     struct  openvz_vm *vm = driver->vms;
 
@@ -96,7 +99,8 @@
 }
 
 /* Free all memory associated with a struct openvz_vm object */
-void openvzFreeVMDef(struct openvz_vm_def *def) {
+void 
+openvzFreeVMDef(struct openvz_vm_def *def) {
     struct ovz_quota *quota = def->fs.quota;
     struct ovz_ip *ip = def->net.ips;
     struct ovz_ns *ns = def->net.ns;
@@ -124,8 +128,9 @@
  * Parses a libvirt XML definition of a guest, and populates the
  * the openvz_vm struct with matching data about the guests config
  */
-static struct openvz_vm_def *openvzParseXML(virConnectPtr conn,
-                                                xmlDocPtr xml) {
+static struct openvz_vm_def 
+*openvzParseXML(virConnectPtr conn,
+                        xmlDocPtr xml) {
     xmlNodePtr root = NULL;
     xmlChar *prop = NULL;
     xmlXPathContextPtr ctxt = NULL;
@@ -249,7 +254,7 @@
     FILE *fp;
     int veid, ret;
     char status[16];
-    char uuidstr[(VIR_UUID_BUFLEN * 2) + 1];
+    char uuidstr[VIR_UUID_STRING_BUFLEN];
     struct openvz_vm *vm;
     struct openvz_vm  **pnext;
     struct openvz_driver *driver;
@@ -296,15 +301,9 @@
         
         snprintf(vmdef->name, OPENVZ_NAME_MAX,  "%i", veid);
         openvzGetVPSUUID(veid, uuidstr);
-        ret = sscanf(uuidstr, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
-                    (unsigned int *)&vmdef->uuid[0], (unsigned int *)&vmdef->uuid[1], (unsigned int *)&vmdef->uuid[2], 
-                    (unsigned int *)&vmdef->uuid[3], (unsigned int *)&vmdef->uuid[4], (unsigned int *)&vmdef->uuid[5], 
-                    (unsigned int *)&vmdef->uuid[6], (unsigned int *)&vmdef->uuid[7], (unsigned int *)&vmdef->uuid[8], 
-                    (unsigned int *)&vmdef->uuid[9], (unsigned int *)&vmdef->uuid[10], (unsigned int *)&vmdef->uuid[11], 
-                    (unsigned int *)&vmdef->uuid[12], (unsigned int *)&vmdef->uuid[13], (unsigned int *)&vmdef->uuid[14],
-                    (unsigned int *)&vmdef->uuid[15]);
+        ret = virUUIDParse(uuidstr, vmdef->uuid);
 
-        if(ret != 16) {
+        if(ret == -1) {
             error(conn, VIR_ERR_INTERNAL_ERROR, "UUID in config file malformed");
             return NULL;
         }
@@ -315,7 +314,8 @@
     return vm;
 }
 
-char *openvzLocateConfDir(void)
+static char 
+*openvzLocateConfDir(void)
 {
     const char *conf_dir_list[] = {"/etc/vz/conf", "/usr/local/etc/conf", NULL};
     int i=0;
@@ -330,9 +330,8 @@
 }
 
 /* Richard Steven's classic readline() function */
-
-static
-int openvz_readline(int fd, char *ptr, int maxlen)
+int 
+openvz_readline(int fd, char *ptr, int maxlen)
 {
     int n, rc;
     char c;
@@ -356,7 +355,8 @@
     return n;
 }
 
-int openvzGetVPSUUID(int vpsid, char *uuidbuf)
+static int 
+openvzGetVPSUUID(int vpsid, char *uuidbuf)
 {
     char conf_file[PATH_MAX];
     char line[1024];
@@ -385,7 +385,7 @@
 
         sscanf(line, "%s %s\n", iden, uuid);
         if(!strcmp(iden, "#UUID:")) {
-            strncpy(uuidbuf, uuid, (VIR_UUID_BUFLEN * 2) +1);
+            strncpy(uuidbuf, uuid, VIR_UUID_STRING_BUFLEN);
             break;
         }
     }
@@ -396,10 +396,11 @@
  * assign if not present.
  */
 
-int openvzSetUUID(int vpsid)
+static int 
+openvzSetUUID(int vpsid)
 {
     char conf_file[PATH_MAX];
-    char uuid[(VIR_UUID_BUFLEN * 2) + 1];
+    char uuid[VIR_UUID_STRING_BUFLEN];
     unsigned char new_uuid[VIR_UUID_BUFLEN];
     char *conf_dir;
     int fd, ret, i;
@@ -418,7 +419,7 @@
 
     if(uuid[0] == (int)NULL) {
         virUUIDGenerate(new_uuid);
-        bzero(uuid, (VIR_UUID_BUFLEN * 2) + 1);
+        bzero(uuid, VIR_UUID_STRING_BUFLEN);
         for(i = 0; i < VIR_UUID_BUFLEN; i ++)
             sprintf(uuid + (i * 2), "%02x", (unsigned char)new_uuid[i]);
     
Index: src/openvz_conf.h
===================================================================
RCS file: /data/cvs/libvirt/src/openvz_conf.h,v
retrieving revision 1.1
diff -u -a -r1.1 openvz_conf.h
--- src/openvz_conf.h	17 Jul 2007 13:27:26 -0000	1.1
+++ src/openvz_conf.h	19 Jul 2007 12:48:04 -0000
@@ -94,8 +94,8 @@
     struct openvz_vm *next;
 };
 
-char *openvzLocateConfDir(void);
-int readline(int fd, char *ptr, int maxlen);
+static char *openvzLocateConfDir(void);
+int openvz_readline(int fd, char *ptr, int maxlen);
 static void error (virConnectPtr conn, virErrorNumber code, const char *info);
 struct openvz_vm *openvzFindVMByID(const struct openvz_driver *driver, int id); 
 struct openvz_vm *openvzFindVMByUUID(const struct openvz_driver *driver, 
@@ -108,7 +108,7 @@
                                             const char *displayName);
 struct openvz_vm *openvzGetVPSInfo(virConnectPtr conn);
 void openvzGenerateUUID(unsigned char *uuid);
-int openvzGetVPSUUID(int vpsid, char *uuidbuf);
-int openvzSetUUID(int vpsid);
+static int openvzGetVPSUUID(int vpsid, char *uuidbuf);
+static int openvzSetUUID(int vpsid);
 int openvzAssignUUIDs(void);
 #endif /* OPENVZ_CONF_H */
Index: src/openvz_driver.c
===================================================================
RCS file: /data/cvs/libvirt/src/openvz_driver.c,v
retrieving revision 1.1
diff -u -a -r1.1 openvz_driver.c
--- src/openvz_driver.c	17 Jul 2007 13:27:26 -0000	1.1
+++ src/openvz_driver.c	19 Jul 2007 12:48:04 -0000
@@ -52,6 +52,7 @@
 
 #include "event.h"
 #include "buf.h"
+#include "util.h"
 #include "openvz_driver.h"
 #include "openvz_conf.h"
 
@@ -284,6 +285,11 @@
         if (strcmp(name, "openvz:///system")) 
             return VIR_DRV_OPEN_DECLINED;
     }
+    /* See if we are running an OpenVZ enabled kernel */
+    if(access("/proc/vz/veinfo", F_OK) == -1 || 
+                access("/proc/user_beancounters", F_OK) == -1) {
+        return VIR_DRV_OPEN_DECLINED;
+    }
 
     conn->privateData = &ovz_driver;
 
@@ -323,19 +329,25 @@
 
 static int openvzListDomains(virConnectPtr conn, int *ids, int nids) {
     int got = 0;
-    int veid;
-    FILE *fp;
+    int veid, pid, outfd, errfd;
+    int ret;
+    char buf[32];
+    const char *cmd[] = {VZLIST, "-ovpsid", "-H" , NULL};
 
-    if((fp = popen(VZLIST " -o vpsid -H 2> /dev/null", "r")) == NULL){
-        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not popen " VZLIST);
+    ret = virExec(conn, (char **)cmd, &pid, &outfd, &errfd);
+    if(ret == -1) {
+        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
         return (int)NULL;
     }
 
-    while(!(feof(fp)) && got < nids){
-        fscanf(fp, "%d\n", &veid);
+    while(got < nids){
+        ret = openvz_readline(outfd, buf, 32);
+        if(!ret) break;
+        sscanf(buf, "%d", &veid);
         ids[got] = veid;
         got ++;
     }
+    waitpid(pid, NULL, 0);
 
     return got;
 }
@@ -347,23 +359,27 @@
 static int openvzListDefinedDomains(virConnectPtr conn,
                             char **const names, int nnames) {
     int got = 0;
-    FILE *fp;
-    int veid;
+    int veid, pid, outfd, errfd, ret;
     char vpsname[OPENVZ_NAME_MAX];
+    char buf[32];
+    const char *cmd[] = {VZLIST, "-ovpsid", "-H", NULL};
 
     /* the -S options lists only stopped domains */
-    if((fp = popen(VZLIST " -S -o vpsid -H 2> /dev/null", "r")) == NULL){
-        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not popen " VZLIST);
+    ret = virExec(conn, (char **)cmd, &pid, &outfd, &errfd);
+    if(ret == -1) {
+        error(conn, VIR_ERR_INTERNAL_ERROR, "Could not exec " VZLIST);
         return (int)NULL;
     }
 
-    while(!(feof(fp)) && got < nnames){
-        fscanf(fp, "%d\n", &veid);
+    while(got < nnames){
+        ret = openvz_readline(outfd, buf, 32);
+        if(!ret) break;
+        sscanf(buf, "%d\n", &veid);
         sprintf(vpsname, "%d", veid);
         names[got] = strdup(vpsname);
         got ++;
     }
-
+    waitpid(pid, NULL, 0);
     return got;
 }
 
--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]