Beispiel #1
0
VolumeWrap::VolumeWrap(PoolWrap *parent,
                       virStorageVolPtr volume_ptr,
                       virConnectPtr connect):
    PackageOwner<PoolWrap::PackageDefinition>(parent),
    ManagedObject(package().data_Volume),
    _volume_ptr(volume_ptr), _conn(connect),
    _lvm_name(""), _has_lvm_child(false),
    _wrap_parent(parent)
{
    const char *volume_key;
    char *volume_path;
    const char *volume_name;

    volume_key = virStorageVolGetKey(_volume_ptr);
    if (volume_key == NULL) {
        REPORT_ERR(_conn, "Error getting storage volume key\n");
        throw 1;
    }
    _volume_key = volume_key;

    volume_path = virStorageVolGetPath(_volume_ptr);
    if (volume_path == NULL) {
        REPORT_ERR(_conn, "Error getting volume path\n");
        throw 1;
    }
    _volume_path = volume_path;

    volume_name = virStorageVolGetName(_volume_ptr);
    if (volume_name == NULL) {
        REPORT_ERR(_conn, "Error getting volume name\n");
        throw 1;
    }
    _volume_name = volume_name;

    _data.setProperty("key", _volume_key);
    _data.setProperty("path", _volume_path);
    _data.setProperty("name", _volume_name);
    _data.setProperty("childLVMName", _lvm_name);
    _data.setProperty("storagePool", parent->objectID());

    // Set defaults
    _data.setProperty("capacity", (uint64_t)0);
    _data.setProperty("allocation", (uint64_t)0);

    addData(_data);

    printf("done\n");
}
const gchar *gvir_storage_vol_get_path(GVirStorageVol *vol, GError **error)
{
    const char *path;

    g_return_val_if_fail(GVIR_IS_STORAGE_VOL(vol), NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);

    if (!(path = virStorageVolGetPath(vol->priv->handle))) {
        gvir_set_error(error, GVIR_STORAGE_VOL_ERROR, 0,
                       "Failed to get storage_vol path on %p",
                       vol->priv->handle);
        return NULL;
    }

    return path;
}
Result StorageVolControlThread::getAllStorageVolList()
{
    Result result;
    result.name = QString("%1_%2").arg(task.srcConName).arg(currPoolName);
    QStringList storageVolList;
    if (currStoragePool!=NULL) {
        virStoragePoolFree(currStoragePool);
        currStoragePool = NULL;
    };
    currStoragePool = virStoragePoolLookupByName(
                *task.srcConnPtr, currPoolName.toUtf8().data());
    if ( currStoragePool!=NULL && keep_alive ) {
        virStorageVolPtr *storageVol = NULL;
        // flags: extra flags; not used yet, so callers should always pass 0
        unsigned int flags = 0;
        int ret = virStoragePoolListAllVolumes(
                    currStoragePool, &storageVol, flags);
        if ( ret<0 ) {
            result.err = sendConnErrors();
            result.result = false;
            result.msg = storageVolList;
            return result;
        };

        // therefore correctly to use for() command, because storageVol[0] can not exist.
        for (int i = 0; i < ret; i++) {
            QString type, capacity, allocation;
            virStorageVolInfo info;
            if ( virStorageVolGetInfo(storageVol[i], &info)+1 ) {
                switch (info.type) {
                case VIR_STORAGE_VOL_FILE:
                    type.append("file");
                    break;
                case VIR_STORAGE_VOL_BLOCK:
                    type.append("block");
                    break;
                case VIR_STORAGE_VOL_DIR:
                    type.append("dir");
                    break;
                case VIR_STORAGE_VOL_NETWORK:
                    type.append("net");
                    break;
                default:
                    type.append("-");
                    break;
                };
                allocation.append(QString("%1").arg(info.allocation));
                capacity.append(QString("%1").arg(info.capacity));
            } else {
                sendConnErrors();
                type.append("-");
                allocation.append("-");
                capacity.append("-");
            };
            QStringList currentAttr;
            currentAttr<< QString::fromUtf8( virStorageVolGetName(storageVol[i]) )
                       << QString::fromUtf8( virStorageVolGetPath(storageVol[i]) )
                       << QString( type )
                       << QString( allocation )
                       << QString( capacity );;
            storageVolList.append(currentAttr);
            //qDebug()<<currentAttr<<"Volume";
            virStorageVolFree(storageVol[i]);
        };
        free(storageVol);
    } else
        result.err = sendConnErrors();
    result.result = true;
    result.msg = storageVolList;
    return result;
}
Beispiel #4
0
void
VolumeWrap::checkForLVMPool()
{
    char *real_path = NULL;
    const char *pool_sources_xml;

    pool_sources_xml = _wrap_parent->getPoolSourcesXml();

    if (pool_sources_xml) {
        xmlDocPtr doc;
        xmlNodePtr cur;

        doc = xmlParseMemory(pool_sources_xml, strlen(pool_sources_xml));

        if (doc == NULL ) {
            return;
        }

        cur = xmlDocGetRootElement(doc);

        if (cur == NULL) {
            xmlFreeDoc(doc);
            return;
        }

        xmlChar *path = NULL;
        xmlChar *name = NULL;

        cur = cur->xmlChildrenNode;
        while (cur != NULL) {
            xmlNodePtr source;
            if ((!xmlStrcmp(cur->name, (const xmlChar *) "source"))) {
                source = cur->xmlChildrenNode;
                while (source != NULL) {
                    if ((!xmlStrcmp(source->name, (const xmlChar *) "device"))) {
                        path = xmlGetProp(source, (const xmlChar *) "path");
                    }

                    if ((!xmlStrcmp(source->name, (const xmlChar *) "name"))) {
                        name = xmlNodeListGetString(doc, source->xmlChildrenNode, 1);
                    }

                source = source->next;
                }
                if (name && path) {
                    virStorageVolPtr vol;

                    printf ("xml returned device name %s, path %s; volume path is %s\n", name, path, _volume_path.c_str());
                    vol = virStorageVolLookupByPath(_conn, (char *) path);
                    if (vol != NULL) {
                        real_path = virStorageVolGetPath(vol);
                        if (real_path && strcmp(real_path, _volume_path.c_str()) == 0) {
                            printf ("found matching storage volume associated with pool!\n");
                            _lvm_name.assign((char *) name);
                            _has_lvm_child = true;
                        }
                    }
                    xmlFree(path);
                    xmlFree(name);
                    path = NULL;
                    name = NULL;
                }
            }
            cur = cur->next;
        }
        xmlFreeDoc(doc);
    }
}
Beispiel #5
0
static bool
cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;
    char *xml;
    const char *name, *capacityStr = NULL, *allocationStr = NULL, *format = NULL;
    const char *snapshotStrVol = NULL, *snapshotStrFormat = NULL;
    unsigned long long capacity, allocation = 0;
    virBuffer buf = VIR_BUFFER_INITIALIZER;

    if (!vshConnectionUsability(ctl, ctl->conn))
        return false;

    if (!(pool = vshCommandOptPoolBy(ctl, cmd, "pool", NULL,
                                     VSH_BYNAME)))
        return false;

    if (vshCommandOptString(cmd, "name", &name) <= 0)
        goto cleanup;

    if (vshCommandOptString(cmd, "capacity", &capacityStr) <= 0)
        goto cleanup;

    if (vshVolSize(capacityStr, &capacity) < 0) {
        vshError(ctl, _("Malformed size %s"), capacityStr);
        goto cleanup;
    }

    if (vshCommandOptString(cmd, "allocation", &allocationStr) > 0 &&
        vshVolSize(allocationStr, &allocation) < 0) {
        vshError(ctl, _("Malformed size %s"), allocationStr);
        goto cleanup;
    }

    if (vshCommandOptString(cmd, "format", &format) < 0 ||
        vshCommandOptString(cmd, "backing-vol", &snapshotStrVol) < 0 ||
        vshCommandOptString(cmd, "backing-vol-format",
                            &snapshotStrFormat) < 0) {
        vshError(ctl, "%s", _("missing argument"));
        goto cleanup;
    }


    virBufferAddLit(&buf, "<volume>\n");
    virBufferAsprintf(&buf, "  <name>%s</name>\n", name);
    virBufferAsprintf(&buf, "  <capacity>%llu</capacity>\n", capacity);
    if (allocationStr)
        virBufferAsprintf(&buf, "  <allocation>%llu</allocation>\n", allocation);

    if (format) {
        virBufferAddLit(&buf, "  <target>\n");
        virBufferAsprintf(&buf, "    <format type='%s'/>\n",format);
        virBufferAddLit(&buf, "  </target>\n");
    }

    /* Convert the snapshot parameters into backingStore XML */
    if (snapshotStrVol) {
        /* Lookup snapshot backing volume.  Try the backing-vol
         *  parameter as a name */
        vshDebug(ctl, VSH_ERR_DEBUG,
                 "%s: Look up backing store volume '%s' as name\n",
                 cmd->def->name, snapshotStrVol);
        virStorageVolPtr snapVol = virStorageVolLookupByName(pool, snapshotStrVol);
        if (snapVol)
                vshDebug(ctl, VSH_ERR_DEBUG,
                         "%s: Backing store volume found using '%s' as name\n",
                         cmd->def->name, snapshotStrVol);

        if (snapVol == NULL) {
            /* Snapshot backing volume not found by name.  Try the
             *  backing-vol parameter as a key */
            vshDebug(ctl, VSH_ERR_DEBUG,
                     "%s: Look up backing store volume '%s' as key\n",
                     cmd->def->name, snapshotStrVol);
            snapVol = virStorageVolLookupByKey(ctl->conn, snapshotStrVol);
            if (snapVol)
                vshDebug(ctl, VSH_ERR_DEBUG,
                         "%s: Backing store volume found using '%s' as key\n",
                         cmd->def->name, snapshotStrVol);
        }
        if (snapVol == NULL) {
            /* Snapshot backing volume not found by key.  Try the
             *  backing-vol parameter as a path */
            vshDebug(ctl, VSH_ERR_DEBUG,
                     "%s: Look up backing store volume '%s' as path\n",
                     cmd->def->name, snapshotStrVol);
            snapVol = virStorageVolLookupByPath(ctl->conn, snapshotStrVol);
            if (snapVol)
                vshDebug(ctl, VSH_ERR_DEBUG,
                         "%s: Backing store volume found using '%s' as path\n",
                         cmd->def->name, snapshotStrVol);
        }
        if (snapVol == NULL) {
            vshError(ctl, _("failed to get vol '%s'"), snapshotStrVol);
            goto cleanup;
        }

        char *snapshotStrVolPath;
        if ((snapshotStrVolPath = virStorageVolGetPath(snapVol)) == NULL) {
            virStorageVolFree(snapVol);
            goto cleanup;
        }

        /* Create XML for the backing store */
        virBufferAddLit(&buf, "  <backingStore>\n");
        virBufferAsprintf(&buf, "    <path>%s</path>\n",snapshotStrVolPath);
        if (snapshotStrFormat)
            virBufferAsprintf(&buf, "    <format type='%s'/>\n",snapshotStrFormat);
        virBufferAddLit(&buf, "  </backingStore>\n");

        /* Cleanup snapshot allocations */
        VIR_FREE(snapshotStrVolPath);
        virStorageVolFree(snapVol);
    }

    virBufferAddLit(&buf, "</volume>\n");

    if (virBufferError(&buf)) {
        vshPrint(ctl, "%s", _("Failed to allocate XML buffer"));
        goto cleanup;
    }
    xml = virBufferContentAndReset(&buf);
    vol = virStorageVolCreateXML(pool, xml, 0);
    VIR_FREE(xml);
    virStoragePoolFree(pool);

    if (vol != NULL) {
        vshPrint(ctl, _("Vol %s created\n"), name);
        virStorageVolFree(vol);
        return true;
    } else {
        vshError(ctl, _("Failed to create vol %s"), name);
        return false;
    }

 cleanup:
    virBufferFreeAndReset(&buf);
    virStoragePoolFree(pool);
    return false;
}