/** * gvir_storage_pool_create_volume: * @pool: the storage pool in which to create the volume * @conf: the configuration for the new volume * @err: Place-holder for possible errors * * Returns: (transfer full): the newly created volume. The returned object * should be unreffed with g_object_unref() when no longer needed. */ GVirStorageVol *gvir_storage_pool_create_volume (GVirStoragePool *pool, GVirConfigStorageVol *conf, GError **err) { const gchar *xml; virStorageVolPtr handle; GVirStoragePoolPrivate *priv; g_return_val_if_fail(GVIR_IS_STORAGE_POOL(pool), NULL); g_return_val_if_fail(GVIR_CONFIG_IS_STORAGE_VOL(conf), NULL); g_return_val_if_fail(err == NULL || *err == NULL, NULL); xml = gvir_config_object_to_xml(GVIR_CONFIG_OBJECT(conf)); g_return_val_if_fail(xml != NULL, NULL); priv = pool->priv; if (!(handle = virStorageVolCreateXML(priv->handle, xml, 0))) { gvir_set_error_literal(err, GVIR_STORAGE_POOL_ERROR, 0, "Failed to create volume"); return NULL; } GVirStorageVol *volume; const char *name; volume = GVIR_STORAGE_VOL(g_object_new(GVIR_TYPE_STORAGE_VOL, "handle", handle, "pool", pool, NULL)); name = gvir_storage_vol_get_name(volume); if (name == NULL) { g_object_unref(G_OBJECT(volume)); return NULL; } g_mutex_lock(priv->lock); if (priv->volumes != NULL) { g_hash_table_insert(priv->volumes, g_strdup(name), volume); } else { g_warn_if_reached(); g_object_unref(G_OBJECT(volume)); g_mutex_unlock(priv->lock); return NULL; } g_mutex_unlock(priv->lock); return g_object_ref(volume); }
static bool cmdVolCreate(vshControl *ctl, const vshCmd *cmd) { virStoragePoolPtr pool; virStorageVolPtr vol; const char *from = NULL; bool ret = true; char *buffer; if (!vshConnectionUsability(ctl, ctl->conn)) return false; if (!(pool = vshCommandOptPoolBy(ctl, cmd, "pool", NULL, VSH_BYNAME))) return false; if (vshCommandOptString(cmd, "file", &from) <= 0) { virStoragePoolFree(pool); return false; } if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) { virshReportError(ctl); virStoragePoolFree(pool); return false; } vol = virStorageVolCreateXML(pool, buffer, 0); VIR_FREE(buffer); virStoragePoolFree(pool); if (vol != NULL) { vshPrint(ctl, _("Vol %s created from %s\n"), virStorageVolGetName(vol), from); virStorageVolFree(vol); } else { vshError(ctl, _("Failed to create vol from %s"), from); ret = false; } return ret; }
Result StorageVolControlThread::createStorageVol() { Result result; result.name = QString("%1_%2").arg(task.srcConName).arg(currPoolName); QString path = task.args.path; QByteArray xmlData; QFile f; f.setFileName(path); if ( !f.open(QIODevice::ReadOnly) ) { QString msg = QString("File \"%1\"\nnot opened.").arg(path); emit errorMsg( msg, number ); result.result = false; result.err = msg; return result; }; xmlData = f.readAll(); f.close(); if (currStoragePool!=NULL) { virStoragePoolFree(currStoragePool); currStoragePool = NULL; }; currStoragePool = virStoragePoolLookupByName( *task.srcConnPtr, currPoolName.toUtf8().data()); virStorageVolPtr storageVol = virStorageVolCreateXML( currStoragePool, xmlData.data(), VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA); if ( storageVol==NULL ) { result.err = sendConnErrors(); result.result = false; return result; }; QString name = QString().fromUtf8( virStorageVolGetName(storageVol) ); result.msg.append( QString("'<b>%1</b>' StorageVol from\n\"%2\"\nis created.") .arg(name).arg(path)); virStorageVolFree(storageVol); result.result = true; return result; }
static bool cmdVolCreate(vshControl *ctl, const vshCmd *cmd) { virStoragePoolPtr pool; virStorageVolPtr vol; const char *from = NULL; bool ret = false; unsigned int flags = 0; char *buffer = NULL; if (vshCommandOptBool(cmd, "prealloc-metadata")) flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA; if (!(pool = vshCommandOptPool(ctl, cmd, "pool", NULL))) return false; if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) goto cleanup; if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) { vshSaveLibvirtError(); goto cleanup; } if ((vol = virStorageVolCreateXML(pool, buffer, flags))) { vshPrint(ctl, _("Vol %s created from %s\n"), virStorageVolGetName(vol), from); virStorageVolFree(vol); ret = true; } else { vshError(ctl, _("Failed to create vol from %s"), from); } cleanup: VIR_FREE(buffer); virStoragePoolFree(pool); return ret; }
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; }