static virStorageVolPtr vshCommandOptVolBy(vshControl *ctl, const vshCmd *cmd, const char *optname, const char *pooloptname, const char **name, int flag) { virStorageVolPtr vol = NULL; virStoragePoolPtr pool = NULL; const char *n = NULL, *p = NULL; if (vshCommandOptString(cmd, optname, &n) <= 0) return NULL; if (pooloptname != NULL && vshCommandOptString(cmd, pooloptname, &p) < 0) { vshError(ctl, "%s", _("missing option")); return NULL; } if (p) pool = vshCommandOptPoolBy(ctl, cmd, pooloptname, name, flag); vshDebug(ctl, VSH_ERR_DEBUG, "%s: found option <%s>: %s\n", cmd->def->name, optname, n); if (name) *name = n; /* try it by name */ if (pool && (flag & VSH_BYNAME)) { vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as vol name\n", cmd->def->name, optname); vol = virStorageVolLookupByName(pool, n); } /* try it by key */ if (vol == NULL && (flag & VSH_BYUUID)) { vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as vol key\n", cmd->def->name, optname); vol = virStorageVolLookupByKey(ctl->conn, n); } /* try it by path */ if (vol == NULL && (flag & VSH_BYUUID)) { vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as vol path\n", cmd->def->name, optname); vol = virStorageVolLookupByPath(ctl->conn, n); } if (!vol) { if (pool) vshError(ctl, _("failed to get vol '%s'"), n); else vshError(ctl, _("failed to get vol '%s', specifying --%s " "might help"), n, pooloptname); } if (pool) virStoragePoolFree(pool); return vol; }
static bool cmdVolClone(vshControl *ctl, const vshCmd *cmd) { virStoragePoolPtr origpool = NULL; virStorageVolPtr origvol = NULL, newvol = NULL; const char *name = NULL; char *origxml = NULL; xmlChar *newxml = NULL; bool ret = false; if (!vshConnectionUsability(ctl, ctl->conn)) goto cleanup; if (!(origvol = vshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) goto cleanup; origpool = virStoragePoolLookupByVolume(origvol); if (!origpool) { vshError(ctl, "%s", _("failed to get parent pool")); goto cleanup; } if (vshCommandOptString(cmd, "newname", &name) <= 0) goto cleanup; origxml = virStorageVolGetXMLDesc(origvol, 0); if (!origxml) goto cleanup; newxml = makeCloneXML(origxml, name); if (!newxml) { vshPrint(ctl, "%s", _("Failed to allocate XML buffer")); goto cleanup; } newvol = virStorageVolCreateXMLFrom(origpool, (char *) newxml, origvol, 0); if (newvol != NULL) { vshPrint(ctl, _("Vol %s cloned from %s\n"), virStorageVolGetName(newvol), virStorageVolGetName(origvol)); } else { vshError(ctl, _("Failed to clone vol from %s"), virStorageVolGetName(origvol)); goto cleanup; } ret = true; cleanup: VIR_FREE(origxml); xmlFree(newxml); if (origvol) virStorageVolFree(origvol); if (newvol) virStorageVolFree(newvol); if (origpool) virStoragePoolFree(origpool); return ret; }
static bool cmdVolCreateFrom(vshControl *ctl, const vshCmd *cmd) { virStoragePoolPtr pool = NULL; virStorageVolPtr newvol = NULL, inputvol = NULL; const char *from = NULL; bool ret = false; char *buffer = NULL; if (!vshConnectionUsability(ctl, ctl->conn)) goto cleanup; if (!(pool = vshCommandOptPool(ctl, cmd, "pool", NULL))) goto cleanup; if (vshCommandOptString(cmd, "file", &from) <= 0) { goto cleanup; } if (!(inputvol = vshCommandOptVol(ctl, cmd, "vol", "inputpool", NULL))) goto cleanup; if (virFileReadAll(from, VIRSH_MAX_XML_FILE, &buffer) < 0) { virshReportError(ctl); goto cleanup; } newvol = virStorageVolCreateXMLFrom(pool, buffer, inputvol, 0); if (newvol != NULL) { vshPrint(ctl, _("Vol %s created from input vol %s\n"), virStorageVolGetName(newvol), virStorageVolGetName(inputvol)); } else { vshError(ctl, _("Failed to create vol from %s"), from); goto cleanup; } ret = true; cleanup: VIR_FREE(buffer); if (pool) virStoragePoolFree(pool); if (inputvol) virStorageVolFree(inputvol); if (newvol) virStorageVolFree(newvol); return ret; }
static bool cmdVolWipe(vshControl *ctl, const vshCmd *cmd) { virStorageVolPtr vol; bool ret = false; const char *name; const char *algorithm_str = NULL; int algorithm = VIR_STORAGE_VOL_WIPE_ALG_ZERO; int funcRet; if (!vshConnectionUsability(ctl, ctl->conn)) return false; if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", &name))) { return false; } if (vshCommandOptString(cmd, "algorithm", &algorithm_str) < 0) { vshError(ctl, "%s", _("missing argument")); goto out; } if (algorithm_str && (algorithm = virStorageVolWipeAlgorithmTypeFromString(algorithm_str)) < 0) { vshError(ctl, _("Unsupported algorithm '%s'"), algorithm_str); goto out; } if ((funcRet = virStorageVolWipePattern(vol, algorithm, 0)) < 0) { if (last_error->code == VIR_ERR_NO_SUPPORT && algorithm == VIR_STORAGE_VOL_WIPE_ALG_ZERO) funcRet = virStorageVolWipe(vol, 0); } if (funcRet < 0) { vshError(ctl, _("Failed to wipe vol %s"), name); goto out; } vshPrint(ctl, _("Vol %s wiped\n"), name); ret = true; out: virStorageVolFree(vol); return ret; }
virInterfacePtr vshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd, const char *optname, const char **name, unsigned int flags) { virInterfacePtr iface = NULL; const char *n = NULL; virCheckFlags(VSH_BYNAME | VSH_BYMAC, NULL); if (!optname) optname = "interface"; if (!vshCmdHasOption(ctl, cmd, optname)) return NULL; if (vshCommandOptString(cmd, optname, &n) <= 0) return NULL; vshDebug(ctl, VSH_ERR_INFO, "%s: found option <%s>: %s\n", cmd->def->name, optname, n); if (name) *name = n; /* try it by NAME */ if (flags & VSH_BYNAME) { vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as interface NAME\n", cmd->def->name, optname); iface = virInterfaceLookupByName(ctl->conn, n); } /* try it by MAC */ if (!iface && (flags & VSH_BYMAC)) { vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as interface MAC\n", cmd->def->name, optname); iface = virInterfaceLookupByMACString(ctl->conn, n); } if (!iface) vshError(ctl, _("failed to get interface '%s'"), n); return iface; }
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; }
}; static bool cmdNodeListDevices(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) { const char *cap_str = NULL; int i; bool tree = vshCommandOptBool(cmd, "tree"); bool ret = true; unsigned int flags = 0; char **caps = NULL; int ncaps = 0; vshNodeDeviceListPtr list = NULL; int cap_type = -1; ignore_value(vshCommandOptString(cmd, "cap", &cap_str)); if (cap_str) { if (tree) { vshError(ctl, "%s", _("Options --tree and --cap are incompatible")); return false; } ncaps = vshStringToArray(cap_str, &caps); } for (i = 0; i < ncaps; i++) { if ((cap_type = virNodeDevCapTypeFromString(caps[i])) < 0) { vshError(ctl, "%s", _("Invalid capability type")); VIR_FREE(caps); return false; }
static bool cmdVolResize(vshControl *ctl, const vshCmd *cmd) { virStorageVolPtr vol; const char *capacityStr = NULL; unsigned long long capacity = 0; unsigned int flags = 0; bool ret = false; bool delta = false; if (vshCommandOptBool(cmd, "allocate")) flags |= VIR_STORAGE_VOL_RESIZE_ALLOCATE; if (vshCommandOptBool(cmd, "delta")) { delta = true; flags |= VIR_STORAGE_VOL_RESIZE_DELTA; } if (vshCommandOptBool(cmd, "shrink")) flags |= VIR_STORAGE_VOL_RESIZE_SHRINK; if (!vshConnectionUsability(ctl, ctl->conn)) return false; if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) return false; if (vshCommandOptString(cmd, "capacity", &capacityStr) <= 0) goto cleanup; virSkipSpaces(&capacityStr); if (*capacityStr == '-') { /* The API always requires a positive value; but we allow a * negative value for convenience. */ if (delta && vshCommandOptBool(cmd, "shrink")){ capacityStr++; } else { vshError(ctl, "%s", _("negative size requires --delta and --shrink")); goto cleanup; } } if (vshVolSize(capacityStr, &capacity) < 0) { vshError(ctl, _("Malformed size %s"), capacityStr); goto cleanup; } if (virStorageVolResize(vol, capacity, flags) == 0) { vshPrint(ctl, delta ? _("Size of volume '%s' successfully changed by %s\n") : _("Size of volume '%s' successfully changed to %s\n"), virStorageVolGetName(vol), capacityStr); ret = true; } else { vshError(ctl, delta ? _("Failed to change size of volume '%s' by %s\n") : _("Failed to change size of volume '%s' to %s\n"), virStorageVolGetName(vol), capacityStr); ret = false; } cleanup: virStorageVolFree(vol); return ret; }
static bool cmdVolDownload(vshControl *ctl, const vshCmd *cmd) { const char *file = NULL; virStorageVolPtr vol = NULL; bool ret = false; int fd = -1; virStreamPtr st = NULL; const char *name = NULL; unsigned long long offset = 0, length = 0; bool created = false; if (!vshConnectionUsability(ctl, ctl->conn)) return false; if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) { vshError(ctl, _("Unable to parse integer")); return false; } if (vshCommandOptULongLong(cmd, "length", &length) < 0) { vshError(ctl, _("Unable to parse integer")); return false; } if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", &name))) return false; if (vshCommandOptString(cmd, "file", &file) < 0) { vshError(ctl, _("file must not be empty")); goto cleanup; } if ((fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) { if (errno != EEXIST || (fd = open(file, O_WRONLY|O_TRUNC, 0666)) < 0) { vshError(ctl, _("cannot create %s"), file); goto cleanup; } } else { created = true; } st = virStreamNew(ctl->conn, 0); if (virStorageVolDownload(vol, st, offset, length, 0) < 0) { vshError(ctl, _("cannot download from volume %s"), name); goto cleanup; } if (virStreamRecvAll(st, vshStreamSink, &fd) < 0) { vshError(ctl, _("cannot receive data from volume %s"), name); goto cleanup; } if (VIR_CLOSE(fd) < 0) { vshError(ctl, _("cannot close file %s"), file); virStreamAbort(st); goto cleanup; } if (virStreamFinish(st) < 0) { vshError(ctl, _("cannot close volume %s"), name); goto cleanup; } ret = true; cleanup: VIR_FORCE_CLOSE(fd); if (!ret && created) unlink(file); if (vol) virStorageVolFree(vol); if (st) virStreamFree(st); return ret; }
static bool cmdVolUpload(vshControl *ctl, const vshCmd *cmd) { const char *file = NULL; virStorageVolPtr vol = NULL; bool ret = false; int fd = -1; virStreamPtr st = NULL; const char *name = NULL; unsigned long long offset = 0, length = 0; if (!vshConnectionUsability(ctl, ctl->conn)) goto cleanup; if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) { vshError(ctl, _("Unable to parse integer")); return false; } if (vshCommandOptULongLong(cmd, "length", &length) < 0) { vshError(ctl, _("Unable to parse integer")); return false; } if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", &name))) { return false; } if (vshCommandOptString(cmd, "file", &file) < 0) { vshError(ctl, _("file must not be empty")); goto cleanup; } if ((fd = open(file, O_RDONLY)) < 0) { vshError(ctl, _("cannot read %s"), file); goto cleanup; } st = virStreamNew(ctl->conn, 0); if (virStorageVolUpload(vol, st, offset, length, 0) < 0) { vshError(ctl, _("cannot upload to volume %s"), name); goto cleanup; } if (virStreamSendAll(st, cmdVolUploadSource, &fd) < 0) { vshError(ctl, _("cannot send data to volume %s"), name); goto cleanup; } if (VIR_CLOSE(fd) < 0) { vshError(ctl, _("cannot close file %s"), file); virStreamAbort(st); goto cleanup; } if (virStreamFinish(st) < 0) { vshError(ctl, _("cannot close volume %s"), name); goto cleanup; } ret = true; cleanup: if (vol) virStorageVolFree(vol); if (st) virStreamFree(st); VIR_FORCE_CLOSE(fd); 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; }