static int vshAdmDaemon(vshControl *ctl, unsigned int flags) { vshAdmControlPtr priv = ctl->privData; priv->dmn = virAdmDaemonOpen(priv->name, flags); if (!priv->dmn) { if (priv->wantReconnect) vshError(ctl, "%s", _("Failed to reconnect to the admin server")); else vshError(ctl, "%s", _("Failed to connect to the admin server")); return -1; } else { if (virAdmDaemonRegisterCloseCallback(priv->dmn, vshAdmCatchDisconnect, NULL, NULL) < 0) vshError(ctl, "%s", _("Unable to register disconnect callback")); if (priv->wantReconnect) vshPrint(ctl, "%s\n", _("Reconnected to the admin server")); else vshPrint(ctl, "%s\n", _("Connected to the admin server")); } return 0; }
static bool cmdVolInfo(vshControl *ctl, const vshCmd *cmd) { virStorageVolInfo info; virStorageVolPtr vol; bool ret = true; if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) return false; vshPrint(ctl, "%-15s %s\n", _("Name:"), virStorageVolGetName(vol)); if (virStorageVolGetInfo(vol, &info) == 0) { double val; const char *unit; vshPrint(ctl, "%-15s %s\n", _("Type:"), vshVolumeTypeToString(info.type)); val = vshPrettyCapacity(info.capacity, &unit); vshPrint(ctl, "%-15s %2.2lf %s\n", _("Capacity:"), val, unit); val = vshPrettyCapacity(info.allocation, &unit); vshPrint(ctl, "%-15s %2.2lf %s\n", _("Allocation:"), val, unit); } else { ret = false; } virStorageVolFree(vol); return ret; }
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; unsigned int flags = 0; if (!(origvol = vshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) goto cleanup; if (vshCommandOptBool(cmd, "prealloc-metadata")) flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA; origpool = virStoragePoolLookupByVolume(origvol); if (!origpool) { vshError(ctl, "%s", _("failed to get parent pool")); goto cleanup; } if (vshCommandOptStringReq(ctl, cmd, "newname", &name) < 0) goto cleanup; origxml = virStorageVolGetXMLDesc(origvol, 0); if (!origxml) goto cleanup; newxml = vshMakeCloneXML(origxml, name); if (!newxml) { vshPrint(ctl, "%s", _("Failed to allocate XML buffer")); goto cleanup; } newvol = virStorageVolCreateXMLFrom(origpool, (char *) newxml, origvol, flags); 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 cmdNetworkAutostart(vshControl *ctl, const vshCmd *cmd) { virNetworkPtr network; const char *name; int autostart; if (!(network = virshCommandOptNetwork(ctl, cmd, &name))) return false; autostart = !vshCommandOptBool(cmd, "disable"); if (virNetworkSetAutostart(network, autostart) < 0) { if (autostart) vshError(ctl, _("failed to mark network %s as autostarted"), name); else vshError(ctl, _("failed to unmark network %s as autostarted"), name); virNetworkFree(network); return false; } if (autostart) vshPrint(ctl, _("Network %s marked as autostarted\n"), name); else vshPrint(ctl, _("Network %s unmarked as autostarted\n"), name); virNetworkFree(network); return true; }
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 cmdSecretDefine(vshControl *ctl, const vshCmd *cmd) { const char *from = NULL; char *buffer; virSecretPtr res; char uuid[VIR_UUID_STRING_BUFLEN]; bool ret = false; if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) return false; if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) return false; if (!(res = virSecretDefineXML(ctl->conn, buffer, 0))) { vshError(ctl, _("Failed to set attributes from %s"), from); goto cleanup; } if (virSecretGetUUIDString(res, &(uuid[0])) < 0) { vshError(ctl, "%s", _("Failed to get UUID of created secret")); goto cleanup; } vshPrint(ctl, _("Secret %s created\n"), uuid); ret = true; cleanup: VIR_FREE(buffer); if (res) virSecretFree(res); return ret; }
static bool cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd) { virSecretPtr secret; VIR_AUTODISPOSE_STR base64 = NULL; unsigned char *value; size_t value_size; bool ret = false; secret = virshCommandOptSecret(ctl, cmd, NULL); if (secret == NULL) return false; value = virSecretGetValue(secret, &value_size, 0); if (value == NULL) goto cleanup; if (!(base64 = virStringEncodeBase64(value, value_size))) goto cleanup; vshPrint(ctl, "%s", base64); ret = true; cleanup: VIR_DISPOSE_N(value, value_size); virSecretFree(secret); return ret; }
static bool cmdNetworkDefine(vshControl *ctl, const vshCmd *cmd) { virNetworkPtr network; const char *from = NULL; bool ret = true; char *buffer; virshControlPtr priv = ctl->privData; if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) return false; if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) return false; network = virNetworkDefineXML(priv->conn, buffer); VIR_FREE(buffer); if (network != NULL) { vshPrint(ctl, _("Network %s defined from %s\n"), virNetworkGetName(network), from); virNetworkFree(network); } else { vshError(ctl, _("Failed to define network from %s"), from); ret = false; } return ret; }
static bool cmdNetworkDumpXML(vshControl *ctl, const vshCmd *cmd) { virNetworkPtr network; bool ret = true; char *dump; unsigned int flags = 0; int inactive; if (!(network = virshCommandOptNetwork(ctl, cmd, NULL))) return false; inactive = vshCommandOptBool(cmd, "inactive"); if (inactive) flags |= VIR_NETWORK_XML_INACTIVE; dump = virNetworkGetXMLDesc(network, flags); if (dump != NULL) { vshPrint(ctl, "%s", dump); VIR_FREE(dump); } else { ret = false; } virNetworkFree(network); return ret; }
static bool cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd) { virSecretPtr secret; char *base64; unsigned char *value; size_t value_size; bool ret = false; secret = vshCommandOptSecret(ctl, cmd, NULL); if (secret == NULL) return false; value = virSecretGetValue(secret, &value_size, 0); if (value == NULL) goto cleanup; base64_encode_alloc((char *)value, value_size, &base64); memset(value, 0, value_size); VIR_FREE(value); if (base64 == NULL) { vshError(ctl, "%s", _("Failed to allocate memory")); goto cleanup; } vshPrint(ctl, "%s", base64); memset(base64, 0, strlen(base64)); VIR_FREE(base64); ret = true; cleanup: virSecretFree(secret); return ret; }
static bool cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd) { virNodeDevicePtr dev = NULL; const char *from = NULL; bool ret = true; char *buffer; if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) return false; if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) return false; dev = virNodeDeviceCreateXML(ctl->conn, buffer, 0); VIR_FREE(buffer); if (dev != NULL) { vshPrint(ctl, _("Node device %s created from %s\n"), virNodeDeviceGetName(dev), from); virNodeDeviceFree(dev); } else { vshError(ctl, _("Failed to create node device from %s"), from); ret = false; } return ret; }
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 (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) return false; if (vshCommandOptStringReq(ctl, 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 cmdVolInfo(vshControl *ctl, const vshCmd *cmd) { virStorageVolInfo info; virStorageVolPtr vol; bool ret = true; if (!vshConnectionUsability(ctl, ctl->conn)) return false; if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) return false; vshPrint(ctl, "%-15s %s\n", _("Name:"), virStorageVolGetName(vol)); if (virStorageVolGetInfo(vol, &info) == 0) { double val; const char *unit; switch(info.type) { case VIR_STORAGE_VOL_FILE: vshPrint(ctl, "%-15s %s\n", _("Type:"), _("file")); break; case VIR_STORAGE_VOL_BLOCK: vshPrint(ctl, "%-15s %s\n", _("Type:"), _("block")); break; case VIR_STORAGE_VOL_DIR: vshPrint(ctl, "%-15s %s\n", _("Type:"), _("dir")); break; case VIR_STORAGE_VOL_NETWORK: vshPrint(ctl, "%-15s %s\n", _("Type:"), _("network")); break; default: vshPrint(ctl, "%-15s %s\n", _("Type:"), _("unknown")); } val = prettyCapacity(info.capacity, &unit); vshPrint(ctl, "%-15s %2.2lf %s\n", _("Capacity:"), val, unit); val = prettyCapacity(info.allocation, &unit); vshPrint(ctl, "%-15s %2.2lf %s\n", _("Allocation:"), val, unit); } else { ret = false; } virStorageVolFree(vol); 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 cmdVolCreateFrom(vshControl *ctl, const vshCmd *cmd) { virStoragePoolPtr pool = NULL; virStorageVolPtr newvol = NULL, inputvol = NULL; const char *from = NULL; bool ret = false; char *buffer = NULL; unsigned int flags = 0; if (!(pool = vshCommandOptPool(ctl, cmd, "pool", NULL))) goto cleanup; if (vshCommandOptBool(cmd, "prealloc-metadata")) flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA; if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) goto cleanup; if (!(inputvol = vshCommandOptVol(ctl, cmd, "vol", "inputpool", NULL))) goto cleanup; if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) { vshReportError(ctl); goto cleanup; } newvol = virStorageVolCreateXMLFrom(pool, buffer, inputvol, flags); 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 cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd) { virNodeDevicePtr dev = NULL; bool ret = false; const char *device_value = NULL; char **arr = NULL; int narr; if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0) return false; if (strchr(device_value, ',')) { narr = vshStringToArray(device_value, &arr); if (narr != 2) { vshError(ctl, _("Malformed device value '%s'"), device_value); goto cleanup; } if (!virValidateWWN(arr[0]) || !virValidateWWN(arr[1])) goto cleanup; dev = virNodeDeviceLookupSCSIHostByWWN(ctl->conn, arr[0], arr[1], 0); } else { dev = virNodeDeviceLookupByName(ctl->conn, device_value); } if (!dev) { vshError(ctl, "%s '%s'", _("Could not find matching device"), device_value); goto cleanup; } if (virNodeDeviceDestroy(dev) == 0) { vshPrint(ctl, _("Destroyed node device '%s'\n"), device_value); } else { vshError(ctl, _("Failed to destroy node device '%s'"), device_value); goto cleanup; } ret = true; cleanup: if (arr) { VIR_FREE(*arr); VIR_FREE(arr); } virNodeDeviceFree(dev); 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; }
static bool cmdNetworkInfo(vshControl *ctl, const vshCmd *cmd) { virNetworkPtr network; char uuid[VIR_UUID_STRING_BUFLEN]; int autostart; int persistent = -1; int active = -1; char *bridge = NULL; if (!(network = virshCommandOptNetwork(ctl, cmd, NULL))) return false; vshPrint(ctl, "%-15s %s\n", _("Name:"), virNetworkGetName(network)); if (virNetworkGetUUIDString(network, uuid) == 0) vshPrint(ctl, "%-15s %s\n", _("UUID:"), uuid); active = virNetworkIsActive(network); if (active >= 0) vshPrint(ctl, "%-15s %s\n", _("Active:"), active? _("yes") : _("no")); persistent = virNetworkIsPersistent(network); if (persistent < 0) vshPrint(ctl, "%-15s %s\n", _("Persistent:"), _("unknown")); else vshPrint(ctl, "%-15s %s\n", _("Persistent:"), persistent ? _("yes") : _("no")); if (virNetworkGetAutostart(network, &autostart) < 0) vshPrint(ctl, "%-15s %s\n", _("Autostart:"), _("no autostart")); else vshPrint(ctl, "%-15s %s\n", _("Autostart:"), autostart ? _("yes") : _("no")); bridge = virNetworkGetBridgeName(network); if (bridge) vshPrint(ctl, "%-15s %s\n", _("Bridge:"), bridge); VIR_FREE(bridge); virNetworkFree(network); return true; }
static bool cmdNetworkDestroy(vshControl *ctl, const vshCmd *cmd) { virNetworkPtr network; bool ret = true; const char *name; if (!(network = virshCommandOptNetwork(ctl, cmd, &name))) return false; if (virNetworkDestroy(network) == 0) { vshPrint(ctl, _("Network %s destroyed\n"), name); } else { vshError(ctl, _("Failed to destroy network %s"), name); ret = false; } virNetworkFree(network); return ret; }
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 cmdVolDumpXML(vshControl *ctl, const vshCmd *cmd) { virStorageVolPtr vol; bool ret = true; char *dump; if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", NULL))) return false; dump = virStorageVolGetXMLDesc(vol, 0); if (dump != NULL) { vshPrint(ctl, "%s", dump); VIR_FREE(dump); } else { ret = false; } virStorageVolFree(vol); return ret; }
static bool cmdVolDelete(vshControl *ctl, const vshCmd *cmd) { virStorageVolPtr vol; bool ret = true; const char *name; if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", &name))) { return false; } if (virStorageVolDelete(vol, 0) == 0) { vshPrint(ctl, _("Vol %s deleted\n"), name); } else { vshError(ctl, _("Failed to delete vol %s"), name); ret = false; } virStorageVolFree(vol); return ret; }
static bool cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd) { virSecretPtr secret; size_t value_size; const char *base64 = NULL; char *value; int res; bool ret = false; if (!(secret = vshCommandOptSecret(ctl, cmd, NULL))) return false; if (vshCommandOptStringReq(ctl, cmd, "base64", &base64) < 0) goto cleanup; if (!base64_decode_alloc(base64, strlen(base64), &value, &value_size)) { vshError(ctl, "%s", _("Invalid base64 data")); goto cleanup; } if (value == NULL) { vshError(ctl, "%s", _("Failed to allocate memory")); goto cleanup; } res = virSecretSetValue(secret, (unsigned char *)value, value_size, 0); memset(value, 0, value_size); VIR_FREE(value); if (res != 0) { vshError(ctl, "%s", _("Failed to set secret value")); goto cleanup; } vshPrint(ctl, "%s", _("Secret value set\n")); ret = true; cleanup: virSecretFree(secret); return ret; }
static bool cmdSecretDumpXML(vshControl *ctl, const vshCmd *cmd) { virSecretPtr secret; bool ret = false; char *xml; secret = virshCommandOptSecret(ctl, cmd, NULL); if (secret == NULL) return false; xml = virSecretGetXMLDesc(secret, 0); if (xml == NULL) goto cleanup; vshPrint(ctl, "%s", xml); VIR_FREE(xml); ret = true; cleanup: virSecretFree(secret); return ret; }
static bool cmdSecretUndefine(vshControl *ctl, const vshCmd *cmd) { virSecretPtr secret; bool ret = false; const char *uuid; secret = vshCommandOptSecret(ctl, cmd, &uuid); if (secret == NULL) return false; if (virSecretUndefine(secret) < 0) { vshError(ctl, _("Failed to delete secret %s"), uuid); goto cleanup; } vshPrint(ctl, _("Secret %s deleted\n"), uuid); ret = true; cleanup: virSecretFree(secret); return ret; }
static bool cmdInterfaceEdit(vshControl *ctl, const vshCmd *cmd) { bool ret = false; virInterfacePtr iface = NULL; virInterfacePtr iface_edited = NULL; unsigned int flags = VIR_INTERFACE_XML_INACTIVE; virshControlPtr priv = ctl->privData; iface = virshCommandOptInterface(ctl, cmd, NULL); if (iface == NULL) goto cleanup; #define EDIT_GET_XML virInterfaceGetXMLDesc(iface, flags) #define EDIT_NOT_CHANGED \ do { \ vshPrint(ctl, _("Interface %s XML configuration not changed.\n"), \ virInterfaceGetName(iface)); \ ret = true; \ goto edit_cleanup; \ } while (0) #define EDIT_DEFINE \ (iface_edited = virInterfaceDefineXML(priv->conn, doc_edited, 0)) #include "virsh-edit.c" vshPrint(ctl, _("Interface %s XML configuration edited.\n"), virInterfaceGetName(iface_edited)); ret = true; cleanup: if (iface) virInterfaceFree(iface); if (iface_edited) virInterfaceFree(iface_edited); return ret; }
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; }