]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Introduce virStoragePoolLoadAllState && virStoragePoolLoadState
authorErik Skultety <eskultet@redhat.com>
Tue, 10 Mar 2015 12:59:51 +0000 (13:59 +0100)
committerErik Skultety <eskultet@redhat.com>
Tue, 7 Apr 2015 14:22:40 +0000 (16:22 +0200)
These functions operate exactly the same as their network equivalents
virNetworkLoadAllState, virNetworkLoadState.

src/conf/storage_conf.c
src/conf/storage_conf.h
src/libvirt_private.syms
src/storage/storage_driver.c

index 2a424a71b9f33b1e33b52a693075084454b1e4f9..987bb7314b8f8c7aa1bf3629c2a91867ba381f89 100644 (file)
@@ -1860,6 +1860,100 @@ virStoragePoolObjLoad(virStoragePoolObjListPtr pools,
 }
 
 
+virStoragePoolObjPtr
+virStoragePoolLoadState(virStoragePoolObjListPtr pools,
+                        const char *stateDir,
+                        const char *name)
+{
+    char *stateFile = NULL;
+    virStoragePoolDefPtr def = NULL;
+    virStoragePoolObjPtr pool = NULL;
+    xmlDocPtr xml = NULL;
+    xmlXPathContextPtr ctxt = NULL;
+    xmlNodePtr node = NULL;
+
+    if (!(stateFile = virFileBuildPath(stateDir, name, ".xml")))
+        goto error;
+
+    if (!(xml = virXMLParseCtxt(stateFile, NULL, _("(pool state)"), &ctxt)))
+        goto error;
+
+    if (!(node = virXPathNode("//pool", ctxt))) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Could not find any 'pool' element in state file"));
+        goto error;
+    }
+
+    ctxt->node = node;
+    if (!(def = virStoragePoolDefParseXML(ctxt)))
+        goto error;
+
+    if (!STREQ(name, def->name)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Storage pool state file '%s' does not match "
+                         "pool name '%s'"),
+                       stateFile, def->name);
+        goto error;
+    }
+
+    /* create the object */
+    if (!(pool = virStoragePoolObjAssignDef(pools, def)))
+        goto error;
+
+    /* XXX: future handling of some additional useful status data,
+     * for now, if a status file for a pool exists, the pool will be marked
+     * as active
+     */
+
+    pool->active = 1;
+
+ cleanup:
+    VIR_FREE(stateFile);
+    xmlFree(xml);
+    xmlXPathFreeContext(ctxt);
+    return pool;
+
+ error:
+    virStoragePoolDefFree(def);
+    goto cleanup;
+}
+
+
+int
+virStoragePoolLoadAllState(virStoragePoolObjListPtr pools,
+                           const char *stateDir)
+{
+    DIR *dir;
+    struct dirent *entry;
+    int ret = -1;
+
+    if (!(dir = opendir(stateDir))) {
+        if (errno == ENOENT)
+            return 0;
+
+        virReportSystemError(errno, _("Failed to open dir '%s'"), stateDir);
+        return -1;
+    }
+
+    while ((ret = virDirRead(dir, &entry, stateDir)) > 0) {
+        virStoragePoolObjPtr pool;
+
+        if (entry->d_name[0] == '.')
+            continue;
+
+        if (!virFileStripSuffix(entry->d_name, ".xml"))
+            continue;
+
+        if (!(pool = virStoragePoolLoadState(pools, stateDir, entry->d_name)))
+            continue;
+        virStoragePoolObjUnlock(pool);
+    }
+
+    closedir(dir);
+    return ret;
+}
+
+
 int
 virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
                              const char *configDir,
index 8d4301951a1722996ab845b97e8c837b196a0b99..74710062d9529599485a6a798efb6309d9461f75 100644 (file)
@@ -318,6 +318,13 @@ int virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
                                  const char *configDir,
                                  const char *autostartDir);
 
+int virStoragePoolLoadAllState(virStoragePoolObjListPtr pools,
+                               const char *stateDir);
+
+virStoragePoolObjPtr
+virStoragePoolLoadState(virStoragePoolObjListPtr pools,
+                        const char *stateDir,
+                        const char *name);
 virStoragePoolObjPtr
 virStoragePoolObjFindByUUID(virStoragePoolObjListPtr pools,
                             const unsigned char *uuid);
index 9f82926747266aa2933d44befbd334722ab77d8b..b1cb9e5177c7af0946f582ef50769e5eebf2c8b4 100644 (file)
@@ -811,6 +811,7 @@ virStoragePoolFormatFileSystemNetTypeToString;
 virStoragePoolFormatFileSystemTypeToString;
 virStoragePoolGetVhbaSCSIHostParent;
 virStoragePoolLoadAllConfigs;
+virStoragePoolLoadAllState;
 virStoragePoolObjAssignDef;
 virStoragePoolObjClearVols;
 virStoragePoolObjDeleteDef;
index e2572703b2d2b99b23df4467d1427d08becd838c..839de13fd24cbd0960271b4f07d550678d0d7974 100644 (file)
@@ -197,6 +197,17 @@ storageStateInitialize(bool privileged,
     }
     driver->privileged = privileged;
 
+    if (virFileMakePath(driver->stateDir) < 0) {
+        virReportError(errno,
+                       _("cannot create directory %s"),
+                       driver->stateDir);
+        goto error;
+    }
+
+    if (virStoragePoolLoadAllState(&driver->pools,
+                                   driver->stateDir) < 0)
+        goto error;
+
     if (virStoragePoolLoadAllConfigs(&driver->pools,
                                      driver->configDir,
                                      driver->autostartDir) < 0)
@@ -245,6 +256,8 @@ storageStateReload(void)
         return -1;
 
     storageDriverLock();
+    virStoragePoolLoadAllState(&driver->pools,
+                               driver->stateDir);
     virStoragePoolLoadAllConfigs(&driver->pools,
                                  driver->configDir,
                                  driver->autostartDir);