2012-07-24 08:44:49 +00:00
|
|
|
/*
|
|
|
|
* virsh-volume.c: Commands to manage storage volume
|
|
|
|
*
|
2016-01-09 13:36:29 +00:00
|
|
|
* Copyright (C) 2005, 2007-2016 Red Hat, Inc.
|
2012-07-24 08:44:49 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-24 08:44:49 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-08-20 23:41:24 +00:00
|
|
|
#include <config.h>
|
|
|
|
#include "virsh-volume.h"
|
2017-04-10 15:06:15 +00:00
|
|
|
#include "virsh-util.h"
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2012-08-20 23:41:24 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <libxml/parser.h>
|
|
|
|
#include <libxml/xpath.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
2012-12-04 12:04:07 +00:00
|
|
|
#include "virbuffer.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2012-08-20 23:41:24 +00:00
|
|
|
#include "virfile.h"
|
|
|
|
#include "virsh-pool.h"
|
2012-12-13 18:13:21 +00:00
|
|
|
#include "virxml.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2018-09-21 14:17:23 +00:00
|
|
|
#include "vsh-table.h"
|
2019-04-01 10:14:26 +00:00
|
|
|
#include "virenum.h"
|
2012-08-20 23:41:24 +00:00
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define VIRSH_COMMON_OPT_POOL_FULL \
|
2018-01-12 13:01:33 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL(N_("pool name or uuid"), \
|
|
|
|
VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE)
|
2016-01-09 13:36:23 +00:00
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define VIRSH_COMMON_OPT_POOL_NAME \
|
2018-01-12 13:01:33 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL(N_("pool name"), \
|
|
|
|
VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE)
|
2016-01-09 13:36:23 +00:00
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define VIRSH_COMMON_OPT_POOL_OPTIONAL \
|
|
|
|
{.name = "pool", \
|
|
|
|
.type = VSH_OT_STRING, \
|
2018-01-12 13:01:33 +00:00
|
|
|
.help = N_("pool name or uuid"), \
|
|
|
|
.completer = virshStoragePoolNameCompleter, \
|
|
|
|
.completer_flags = VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE, \
|
2017-10-31 10:47:36 +00:00
|
|
|
}
|
2016-01-09 13:36:23 +00:00
|
|
|
|
2021-06-16 08:02:52 +00:00
|
|
|
#define VIRSH_COMMON_OPT_VOL_NAME(_helpstr) \
|
2017-11-03 12:09:47 +00:00
|
|
|
{.name = "vol", \
|
|
|
|
.type = VSH_OT_DATA, \
|
|
|
|
.flags = VSH_OFLAG_REQ, \
|
2021-06-16 08:02:52 +00:00
|
|
|
.help = _helpstr, \
|
2018-01-12 13:15:11 +00:00
|
|
|
.completer = virshStorageVolNameCompleter, \
|
2017-10-31 10:47:36 +00:00
|
|
|
}
|
2016-01-09 13:36:31 +00:00
|
|
|
|
2021-06-16 08:02:52 +00:00
|
|
|
#define VIRSH_COMMON_OPT_VOL_KEY(_helpstr) \
|
|
|
|
{.name = "vol", \
|
|
|
|
.type = VSH_OT_DATA, \
|
|
|
|
.flags = VSH_OFLAG_REQ, \
|
|
|
|
.help = _helpstr, \
|
|
|
|
.completer = virshStorageVolKeyCompleter, \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define VIRSH_COMMON_OPT_VOL_FULL \
|
|
|
|
VIRSH_COMMON_OPT_VOL_NAME(N_("vol name, key or path"))
|
|
|
|
|
|
|
|
#define VIRSH_COMMON_OPT_VOL_BY_KEY \
|
|
|
|
VIRSH_COMMON_OPT_VOL_KEY(N_("volume key or path"))
|
|
|
|
|
2012-08-20 23:41:24 +00:00
|
|
|
virStorageVolPtr
|
2015-06-15 16:53:58 +00:00
|
|
|
virshCommandOptVolBy(vshControl *ctl, const vshCmd *cmd,
|
|
|
|
const char *optname,
|
|
|
|
const char *pooloptname,
|
|
|
|
const char **name, unsigned int flags)
|
2012-07-24 08:44:49 +00:00
|
|
|
{
|
|
|
|
virStorageVolPtr vol = NULL;
|
2021-09-26 07:25:41 +00:00
|
|
|
g_autoptr(virshStoragePool) pool = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *n = NULL, *p = NULL;
|
2021-03-11 07:16:13 +00:00
|
|
|
virshControl *priv = ctl->privData;
|
2015-06-15 16:53:58 +00:00
|
|
|
|
|
|
|
virCheckFlags(VIRSH_BYUUID | VIRSH_BYNAME, NULL);
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
|
2012-07-24 08:44:49 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (pooloptname != NULL &&
|
|
|
|
vshCommandOptStringReq(ctl, cmd, pooloptname, &p) < 0)
|
2012-07-24 08:44:49 +00:00
|
|
|
return NULL;
|
|
|
|
|
2014-03-03 14:21:37 +00:00
|
|
|
if (p) {
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(pool = virshCommandOptPoolBy(ctl, cmd, pooloptname, name, flags)))
|
2014-03-03 14:21:37 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (virStoragePoolIsActive(pool) != 1) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("pool '%1$s' is not active"), p);
|
2014-03-03 14:21:37 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
vshDebug(ctl, VSH_ERR_DEBUG, "%s: found option <%s>: %s\n",
|
|
|
|
cmd->def->name, optname, n);
|
|
|
|
|
|
|
|
if (name)
|
|
|
|
*name = n;
|
|
|
|
|
|
|
|
/* try it by name */
|
2015-06-15 16:53:58 +00:00
|
|
|
if (pool && (flags & VIRSH_BYNAME)) {
|
2012-07-24 08:44:49 +00:00
|
|
|
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as vol name\n",
|
|
|
|
cmd->def->name, optname);
|
|
|
|
vol = virStorageVolLookupByName(pool, n);
|
|
|
|
}
|
|
|
|
/* try it by key */
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!vol && (flags & VIRSH_BYUUID)) {
|
2012-07-24 08:44:49 +00:00
|
|
|
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as vol key\n",
|
|
|
|
cmd->def->name, optname);
|
2015-06-15 16:53:58 +00:00
|
|
|
vol = virStorageVolLookupByKey(priv->conn, n);
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
/* try it by path */
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!vol && (flags & VIRSH_BYUUID)) {
|
2012-07-24 08:44:49 +00:00
|
|
|
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as vol path\n",
|
|
|
|
cmd->def->name, optname);
|
2015-06-15 16:53:58 +00:00
|
|
|
vol = virStorageVolLookupByPath(priv->conn, n);
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!vol) {
|
2013-03-22 11:32:32 +00:00
|
|
|
if (pool || !pooloptname)
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("failed to get vol '%1$s'"), n);
|
2012-07-24 08:44:49 +00:00
|
|
|
else
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("failed to get vol '%1$s', specifying --%2$s might help"),
|
|
|
|
n, pooloptname);
|
2018-04-16 15:40:23 +00:00
|
|
|
} else {
|
|
|
|
vshResetLibvirtError();
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 12:44:55 +00:00
|
|
|
/* If the pool was specified, then make sure that the returned
|
|
|
|
* volume is from the given pool */
|
|
|
|
if (pool && vol) {
|
2021-09-26 07:25:41 +00:00
|
|
|
g_autoptr(virshStoragePool) volpool = NULL;
|
2014-05-30 12:44:55 +00:00
|
|
|
|
|
|
|
if ((volpool = virStoragePoolLookupByVolume(vol))) {
|
|
|
|
if (STRNEQ(virStoragePoolGetName(volpool),
|
|
|
|
virStoragePoolGetName(pool))) {
|
|
|
|
vshResetLibvirtError();
|
|
|
|
vshError(ctl,
|
2023-03-09 14:54:58 +00:00
|
|
|
_("Requested volume '%1$s' is not in pool '%2$s'"),
|
2014-05-30 12:44:55 +00:00
|
|
|
n, virStoragePoolGetName(pool));
|
2021-09-26 08:28:06 +00:00
|
|
|
g_clear_pointer(&vol, virshStorageVolFree);
|
2014-05-30 12:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 08:44:49 +00:00
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-create-as" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_create_as[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("create a volume from a set of args")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = N_("Create a vol.")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_create_as[] = {
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_NAME,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "name",
|
|
|
|
.type = VSH_OT_DATA,
|
|
|
|
.flags = VSH_OFLAG_REQ,
|
2021-09-15 15:42:08 +00:00
|
|
|
.completer = virshCompleteEmpty,
|
2013-01-14 14:48:52 +00:00
|
|
|
.help = N_("name of the volume")
|
|
|
|
},
|
|
|
|
{.name = "capacity",
|
|
|
|
.type = VSH_OT_DATA,
|
|
|
|
.flags = VSH_OFLAG_REQ,
|
2021-09-15 15:42:08 +00:00
|
|
|
.completer = virshCompleteEmpty,
|
2013-01-14 14:48:52 +00:00
|
|
|
.help = N_("size of the vol, as scaled integer (default bytes)")
|
|
|
|
},
|
|
|
|
{.name = "allocation",
|
|
|
|
.type = VSH_OT_STRING,
|
2021-09-15 15:42:08 +00:00
|
|
|
.completer = virshCompleteEmpty,
|
2013-01-14 14:48:52 +00:00
|
|
|
.help = N_("initial allocation size, as scaled integer (default bytes)")
|
|
|
|
},
|
|
|
|
{.name = "format",
|
|
|
|
.type = VSH_OT_STRING,
|
|
|
|
.help = N_("file format type raw,bochs,qcow,qcow2,qed,vmdk")
|
|
|
|
},
|
|
|
|
{.name = "backing-vol",
|
|
|
|
.type = VSH_OT_STRING,
|
|
|
|
.help = N_("the backing volume if taking a snapshot")
|
|
|
|
},
|
|
|
|
{.name = "backing-vol-format",
|
|
|
|
.type = VSH_OT_STRING,
|
|
|
|
.help = N_("format of backing volume if taking a snapshot")
|
|
|
|
},
|
|
|
|
{.name = "prealloc-metadata",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("preallocate metadata (for qcow2 instead of full allocation)")
|
|
|
|
},
|
2016-02-10 15:48:15 +00:00
|
|
|
{.name = "print-xml",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("print XML document, but don't define/create")
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2015-06-15 16:53:58 +00:00
|
|
|
virshVolSize(const char *data, unsigned long long *val)
|
2012-07-24 08:44:49 +00:00
|
|
|
{
|
|
|
|
char *end;
|
2017-03-27 12:17:42 +00:00
|
|
|
if (virStrToLong_ullp(data, &end, 10, val) < 0)
|
2012-07-24 08:44:49 +00:00
|
|
|
return -1;
|
|
|
|
return virScaleInteger(val, end, 1, ULLONG_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 07:25:41 +00:00
|
|
|
g_autoptr(virshStoragePool) pool = NULL;
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2021-08-11 13:25:15 +00:00
|
|
|
g_autofree char *xml = NULL;
|
2016-02-10 15:48:15 +00:00
|
|
|
bool printXML = vshCommandOptBool(cmd, "print-xml");
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *name, *capacityStr = NULL, *allocationStr = NULL, *format = NULL;
|
|
|
|
const char *snapshotStrVol = NULL, *snapshotStrFormat = NULL;
|
|
|
|
unsigned long long capacity, allocation = 0;
|
2020-07-02 23:40:16 +00:00
|
|
|
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
2022-11-22 12:04:21 +00:00
|
|
|
unsigned int flags = 0;
|
2021-03-11 07:16:13 +00:00
|
|
|
virshControl *priv = ctl->privData;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2012-12-05 10:48:08 +00:00
|
|
|
if (vshCommandOptBool(cmd, "prealloc-metadata"))
|
|
|
|
flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;
|
2015-02-03 03:05:27 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(pool = virshCommandOptPool(ctl, cmd, "pool", NULL)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0)
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "capacity", &capacityStr) < 0)
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (virshVolSize(capacityStr, &capacity) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Malformed size %1$s"), capacityStr);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 12:47:56 +00:00
|
|
|
if (vshCommandOptStringQuiet(ctl, cmd, "allocation", &allocationStr) > 0 &&
|
2015-06-15 16:53:58 +00:00
|
|
|
virshVolSize(allocationStr, &allocation) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Malformed size %1$s"), allocationStr);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "format", &format) < 0 ||
|
|
|
|
vshCommandOptStringReq(ctl, cmd, "backing-vol", &snapshotStrVol) < 0 ||
|
|
|
|
vshCommandOptStringReq(ctl, cmd, "backing-vol-format",
|
|
|
|
&snapshotStrFormat) < 0)
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
virBufferAddLit(&buf, "<volume>\n");
|
2014-03-12 23:52:07 +00:00
|
|
|
virBufferAdjustIndent(&buf, 2);
|
|
|
|
virBufferAsprintf(&buf, "<name>%s</name>\n", name);
|
|
|
|
virBufferAsprintf(&buf, "<capacity>%llu</capacity>\n", capacity);
|
2012-07-24 08:44:49 +00:00
|
|
|
if (allocationStr)
|
2014-03-12 23:52:07 +00:00
|
|
|
virBufferAsprintf(&buf, "<allocation>%llu</allocation>\n", allocation);
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
if (format) {
|
2014-03-12 23:52:07 +00:00
|
|
|
virBufferAddLit(&buf, "<target>\n");
|
|
|
|
virBufferAdjustIndent(&buf, 2);
|
|
|
|
virBufferAsprintf(&buf, "<format type='%s'/>\n", format);
|
|
|
|
virBufferAdjustIndent(&buf, -2);
|
|
|
|
virBufferAddLit(&buf, "</target>\n");
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the snapshot parameters into backingStore XML */
|
|
|
|
if (snapshotStrVol) {
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) snapVol = NULL;
|
2021-08-11 13:25:15 +00:00
|
|
|
g_autofree char *snapshotStrVolPath = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
/* 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);
|
2020-07-28 17:50:28 +00:00
|
|
|
snapVol = virStorageVolLookupByName(pool, snapshotStrVol);
|
2012-07-24 08:44:49 +00:00
|
|
|
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);
|
2015-06-15 16:53:58 +00:00
|
|
|
snapVol = virStorageVolLookupByKey(priv->conn, snapshotStrVol);
|
2012-07-24 08:44:49 +00:00
|
|
|
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);
|
2015-06-15 16:53:58 +00:00
|
|
|
snapVol = virStorageVolLookupByPath(priv->conn, snapshotStrVol);
|
2012-07-24 08:44:49 +00:00
|
|
|
if (snapVol)
|
|
|
|
vshDebug(ctl, VSH_ERR_DEBUG,
|
|
|
|
"%s: Backing store volume found using '%s' as path\n",
|
|
|
|
cmd->def->name, snapshotStrVol);
|
|
|
|
}
|
|
|
|
if (snapVol == NULL) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("failed to get vol '%1$s'"), snapshotStrVol);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((snapshotStrVolPath = virStorageVolGetPath(snapVol)) == NULL) {
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create XML for the backing store */
|
2014-03-12 23:52:07 +00:00
|
|
|
virBufferAddLit(&buf, "<backingStore>\n");
|
|
|
|
virBufferAdjustIndent(&buf, 2);
|
|
|
|
virBufferAsprintf(&buf, "<path>%s</path>\n", snapshotStrVolPath);
|
2012-07-24 08:44:49 +00:00
|
|
|
if (snapshotStrFormat)
|
2014-03-12 23:52:07 +00:00
|
|
|
virBufferAsprintf(&buf, "<format type='%s'/>\n",
|
2013-11-19 22:50:56 +00:00
|
|
|
snapshotStrFormat);
|
2014-03-12 23:52:07 +00:00
|
|
|
virBufferAdjustIndent(&buf, -2);
|
|
|
|
virBufferAddLit(&buf, "</backingStore>\n");
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 23:52:07 +00:00
|
|
|
virBufferAdjustIndent(&buf, -2);
|
2012-07-24 08:44:49 +00:00
|
|
|
virBufferAddLit(&buf, "</volume>\n");
|
|
|
|
|
|
|
|
xml = virBufferContentAndReset(&buf);
|
|
|
|
|
2016-02-10 15:48:15 +00:00
|
|
|
if (printXML) {
|
|
|
|
vshPrint(ctl, "%s", xml);
|
|
|
|
} else {
|
|
|
|
if (!(vol = virStorageVolCreateXML(pool, xml, flags))) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Failed to create vol %1$s"), name);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2016-02-10 15:48:15 +00:00
|
|
|
}
|
2023-03-09 14:54:58 +00:00
|
|
|
vshPrintExtra(ctl, _("Vol %1$s created\n"), name);
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 14:26:07 +00:00
|
|
|
return true;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-create" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_create[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("create a vol from an XML file")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = N_("Create a vol.")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_create[] = {
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_NAME,
|
2016-01-09 13:36:29 +00:00
|
|
|
VIRSH_COMMON_OPT_FILE(N_("file containing an XML vol description")),
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "prealloc-metadata",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("preallocate metadata (for qcow2 instead of full allocation)")
|
|
|
|
},
|
2022-10-18 11:59:29 +00:00
|
|
|
{.name = "validate",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("validate the XML against the schema")
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolCreate(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 07:25:41 +00:00
|
|
|
g_autoptr(virshStoragePool) pool = NULL;
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *from = NULL;
|
2012-12-05 10:48:08 +00:00
|
|
|
unsigned int flags = 0;
|
2021-08-11 13:25:15 +00:00
|
|
|
g_autofree char *buffer = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2012-12-05 10:48:08 +00:00
|
|
|
if (vshCommandOptBool(cmd, "prealloc-metadata"))
|
|
|
|
flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;
|
2015-02-03 03:05:27 +00:00
|
|
|
|
2022-10-18 11:59:29 +00:00
|
|
|
if (vshCommandOptBool(cmd, "validate"))
|
|
|
|
flags |= VIR_STORAGE_VOL_CREATE_VALIDATE;
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(pool = virshCommandOptPool(ctl, cmd, "pool", NULL)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 04:10:17 +00:00
|
|
|
if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) {
|
2013-01-18 15:40:08 +00:00
|
|
|
vshSaveLibvirtError();
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 15:09:48 +00:00
|
|
|
if (!(vol = virStorageVolCreateXML(pool, buffer, flags))) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Failed to create vol from %1$s"), from);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
2013-01-18 15:40:08 +00:00
|
|
|
|
2023-03-09 14:54:58 +00:00
|
|
|
vshPrintExtra(ctl, _("Vol %1$s created from %2$s\n"),
|
2021-11-08 15:09:48 +00:00
|
|
|
virStorageVolGetName(vol), from);
|
2021-11-04 14:26:07 +00:00
|
|
|
return true;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-create-from" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_create_from[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("create a vol, using another volume as input")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = N_("Create a vol from an existing volume.")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_create_from[] = {
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_FULL,
|
2016-01-09 13:36:29 +00:00
|
|
|
VIRSH_COMMON_OPT_FILE(N_("file containing an XML vol description")),
|
2021-06-16 08:02:52 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_FULL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "inputpool",
|
|
|
|
.type = VSH_OT_STRING,
|
2021-09-16 12:52:50 +00:00
|
|
|
.completer = virshStoragePoolNameCompleter,
|
2013-01-14 14:48:52 +00:00
|
|
|
.help = N_("pool name or uuid of the input volume's pool")
|
|
|
|
},
|
|
|
|
{.name = "prealloc-metadata",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("preallocate metadata (for qcow2 instead of full allocation)")
|
|
|
|
},
|
2015-02-03 03:05:27 +00:00
|
|
|
{.name = "reflink",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("use btrfs COW lightweight copy")
|
|
|
|
},
|
2022-10-18 11:59:29 +00:00
|
|
|
{.name = "validate",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("validate the XML against the schema")
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolCreateFrom(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 07:25:41 +00:00
|
|
|
g_autoptr(virshStoragePool) pool = NULL;
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) newvol = NULL;
|
|
|
|
g_autoptr(virshStorageVol) inputvol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *from = NULL;
|
2021-08-11 13:25:15 +00:00
|
|
|
g_autofree char *buffer = NULL;
|
2012-12-05 10:48:08 +00:00
|
|
|
unsigned int flags = 0;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(pool = virshCommandOptPool(ctl, cmd, "pool", NULL)))
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2012-12-05 10:48:08 +00:00
|
|
|
if (vshCommandOptBool(cmd, "prealloc-metadata"))
|
|
|
|
flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;
|
2013-01-18 15:40:08 +00:00
|
|
|
|
2015-02-03 03:05:27 +00:00
|
|
|
if (vshCommandOptBool(cmd, "reflink"))
|
|
|
|
flags |= VIR_STORAGE_VOL_CREATE_REFLINK;
|
|
|
|
|
2022-10-18 11:59:29 +00:00
|
|
|
if (vshCommandOptBool(cmd, "validate"))
|
|
|
|
flags |= VIR_STORAGE_VOL_CREATE_VALIDATE;
|
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(inputvol = virshCommandOptVol(ctl, cmd, "vol", "inputpool", NULL)))
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 04:10:17 +00:00
|
|
|
if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) {
|
|
|
|
vshReportError(ctl);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 10:48:08 +00:00
|
|
|
newvol = virStorageVolCreateXMLFrom(pool, buffer, inputvol, flags);
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2021-11-08 15:09:48 +00:00
|
|
|
if (!newvol) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Failed to create vol from %1$s"), from);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:58 +00:00
|
|
|
vshPrintExtra(ctl, _("Vol %1$s created from input vol %2$s\n"),
|
2021-11-08 15:09:48 +00:00
|
|
|
virStorageVolGetName(newvol), virStorageVolGetName(inputvol));
|
2021-11-04 14:26:07 +00:00
|
|
|
return true;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 08:39:30 +00:00
|
|
|
static char *
|
2015-06-15 16:53:58 +00:00
|
|
|
virshMakeCloneXML(const char *origxml, const char *newname)
|
2012-07-24 08:44:49 +00:00
|
|
|
{
|
2021-08-11 11:57:15 +00:00
|
|
|
g_autoptr(xmlDoc) doc = NULL;
|
2021-08-11 11:21:18 +00:00
|
|
|
g_autoptr(xmlXPathContext) ctxt = NULL;
|
2022-12-01 08:39:30 +00:00
|
|
|
xmlNodePtr node;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2022-12-01 08:39:30 +00:00
|
|
|
if (!(doc = virXMLParseStringCtxt(origxml, _("(volume_definition)"), &ctxt)))
|
2021-08-12 07:59:20 +00:00
|
|
|
return NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2022-12-01 08:39:30 +00:00
|
|
|
if (!(node = virXPathNode("/volume/name", ctxt)))
|
2021-08-12 07:59:20 +00:00
|
|
|
return NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2022-12-01 08:39:30 +00:00
|
|
|
xmlNodeSetContent(node, (const xmlChar *)newname);
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2022-12-01 08:39:30 +00:00
|
|
|
return virXMLNodeToString(doc, doc->children);
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-clone" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_clone[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("clone a volume.")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
2016-04-19 21:34:57 +00:00
|
|
|
.data = N_("Clone an existing volume within the parent pool.")
|
2013-02-07 15:25:10 +00:00
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_clone[] = {
|
2021-06-16 08:02:52 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_FULL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "newname",
|
|
|
|
.type = VSH_OT_DATA,
|
|
|
|
.flags = VSH_OFLAG_REQ,
|
2021-09-15 15:42:08 +00:00
|
|
|
.completer = virshCompleteEmpty,
|
2013-01-14 14:48:52 +00:00
|
|
|
.help = N_("clone name")
|
|
|
|
},
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "prealloc-metadata",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("preallocate metadata (for qcow2 instead of full allocation)")
|
|
|
|
},
|
2015-02-03 03:05:27 +00:00
|
|
|
{.name = "reflink",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("use btrfs COW lightweight copy")
|
|
|
|
},
|
2022-11-30 15:48:23 +00:00
|
|
|
{.name = "print-xml",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("print XML document rather than clone the volume")
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolClone(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 07:25:41 +00:00
|
|
|
g_autoptr(virshStoragePool) origpool = NULL;
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) origvol = NULL;
|
|
|
|
g_autoptr(virshStorageVol) newvol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *name = NULL;
|
2021-08-11 13:25:15 +00:00
|
|
|
g_autofree char *origxml = NULL;
|
2022-12-01 08:39:30 +00:00
|
|
|
g_autofree char *newxml = NULL;
|
2012-12-05 10:48:08 +00:00
|
|
|
unsigned int flags = 0;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(origvol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL)))
|
2022-11-30 15:40:14 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2012-12-05 10:48:08 +00:00
|
|
|
if (vshCommandOptBool(cmd, "prealloc-metadata"))
|
|
|
|
flags |= VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;
|
|
|
|
|
2015-02-03 03:05:27 +00:00
|
|
|
if (vshCommandOptBool(cmd, "reflink"))
|
|
|
|
flags |= VIR_STORAGE_VOL_CREATE_REFLINK;
|
|
|
|
|
2012-07-24 08:44:49 +00:00
|
|
|
origpool = virStoragePoolLookupByVolume(origvol);
|
|
|
|
if (!origpool) {
|
|
|
|
vshError(ctl, "%s", _("failed to get parent pool"));
|
2022-11-30 15:40:14 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "newname", &name) < 0)
|
2022-11-30 15:40:14 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2022-11-30 15:40:14 +00:00
|
|
|
if (!(origxml = virStorageVolGetXMLDesc(origvol, 0)))
|
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2022-11-30 15:40:14 +00:00
|
|
|
if (!(newxml = virshMakeCloneXML(origxml, name))) {
|
2016-11-04 14:21:31 +00:00
|
|
|
vshError(ctl, "%s", _("Failed to allocate XML buffer"));
|
2022-11-30 15:40:14 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 15:48:23 +00:00
|
|
|
if (vshCommandOptBool(cmd, "print-xml")) {
|
|
|
|
vshPrint(ctl, "%s", newxml);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:39:30 +00:00
|
|
|
if (!(newvol = virStorageVolCreateXMLFrom(origpool, newxml, origvol, flags))) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Failed to clone vol from %1$s"),
|
2012-07-24 08:44:49 +00:00
|
|
|
virStorageVolGetName(origvol));
|
2022-11-30 15:40:14 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:58 +00:00
|
|
|
vshPrintExtra(ctl, _("Vol %1$s cloned from %2$s\n"),
|
2022-11-30 15:40:14 +00:00
|
|
|
virStorageVolGetName(newvol), virStorageVolGetName(origvol));
|
|
|
|
return true;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-upload" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_upload[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
2013-04-30 12:19:49 +00:00
|
|
|
.data = N_("upload file contents to a volume")
|
2013-02-07 15:25:10 +00:00
|
|
|
},
|
|
|
|
{.name = "desc",
|
2013-04-30 12:19:49 +00:00
|
|
|
.data = N_("Upload file contents to a volume")
|
2013-02-07 15:25:10 +00:00
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_upload[] = {
|
2021-06-16 08:02:52 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_FULL,
|
2016-01-09 13:36:29 +00:00
|
|
|
VIRSH_COMMON_OPT_FILE(N_("file")),
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "offset",
|
|
|
|
.type = VSH_OT_INT,
|
|
|
|
.help = N_("volume offset to upload to")
|
|
|
|
},
|
|
|
|
{.name = "length",
|
|
|
|
.type = VSH_OT_INT,
|
|
|
|
.help = N_("amount of data to upload")
|
|
|
|
},
|
2016-04-27 12:21:10 +00:00
|
|
|
{.name = "sparse",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("preserve sparseness of volume")
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolUpload(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
|
|
|
const char *file = NULL;
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2021-09-26 10:54:10 +00:00
|
|
|
VIR_AUTOCLOSE fd = -1;
|
2021-09-26 11:27:26 +00:00
|
|
|
g_autoptr(virshStream) st = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *name = NULL;
|
|
|
|
unsigned long long offset = 0, length = 0;
|
2021-03-11 07:16:13 +00:00
|
|
|
virshControl *priv = ctl->privData;
|
2016-04-27 12:21:10 +00:00
|
|
|
unsigned int flags = 0;
|
|
|
|
virshStreamCallbackData cbData;
|
2020-07-02 06:51:20 +00:00
|
|
|
struct stat sb;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-02 09:17:29 +00:00
|
|
|
if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0)
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2015-06-02 09:17:29 +00:00
|
|
|
if (vshCommandOptULongLongWrap(ctl, cmd, "length", &length) < 0)
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVol(ctl, cmd, "vol", "pool", &name)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "file", &file) < 0)
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
if ((fd = open(file, O_RDONLY)) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot read %1$s"), file);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 06:51:20 +00:00
|
|
|
if (fstat(fd, &sb) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("unable to stat %1$s"), file);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2020-07-02 06:51:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 12:21:10 +00:00
|
|
|
cbData.ctl = ctl;
|
|
|
|
cbData.fd = fd;
|
2020-07-02 06:51:20 +00:00
|
|
|
cbData.isBlock = !!S_ISBLK(sb.st_mode);
|
2016-04-27 12:21:10 +00:00
|
|
|
|
|
|
|
if (vshCommandOptBool(cmd, "sparse"))
|
|
|
|
flags |= VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM;
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(st = virStreamNew(priv->conn, 0))) {
|
2013-09-29 09:24:11 +00:00
|
|
|
vshError(ctl, _("cannot create a new stream"));
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2013-09-29 09:24:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 12:21:10 +00:00
|
|
|
if (virStorageVolUpload(vol, st, offset, length, flags) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot upload to volume %1$s"), name);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 12:21:10 +00:00
|
|
|
if (flags & VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM) {
|
|
|
|
if (virStreamSparseSendAll(st, virshStreamSource,
|
|
|
|
virshStreamInData,
|
|
|
|
virshStreamSourceSkip, &cbData) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot send data to volume %1$s"), name);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2016-04-27 12:21:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (virStreamSendAll(st, virshStreamSource, &cbData) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot send data to volume %1$s"), name);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2016-04-27 12:21:10 +00:00
|
|
|
}
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot close file %1$s"), file);
|
2012-07-24 08:44:49 +00:00
|
|
|
virStreamAbort(st);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virStreamFinish(st) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot close volume %1$s"), name);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 14:26:07 +00:00
|
|
|
return true;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-download" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_download[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
2013-04-30 12:19:49 +00:00
|
|
|
.data = N_("download volume contents to a file")
|
2013-02-07 15:25:10 +00:00
|
|
|
},
|
|
|
|
{.name = "desc",
|
2013-04-30 12:19:49 +00:00
|
|
|
.data = N_("Download volume contents to a file")
|
2013-02-07 15:25:10 +00:00
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_download[] = {
|
2021-06-16 08:02:52 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_FULL,
|
2021-09-17 07:58:54 +00:00
|
|
|
VIRSH_COMMON_OPT_FILE(N_("file")),
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "offset",
|
|
|
|
.type = VSH_OT_INT,
|
|
|
|
.help = N_("volume offset to download from")
|
|
|
|
},
|
|
|
|
{.name = "length",
|
|
|
|
.type = VSH_OT_INT,
|
|
|
|
.help = N_("amount of data to download")
|
|
|
|
},
|
2016-04-12 13:35:04 +00:00
|
|
|
{.name = "sparse",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("preserve sparseness of volume")
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
|
|
|
const char *file = NULL;
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
bool ret = false;
|
2021-09-26 10:54:10 +00:00
|
|
|
VIR_AUTOCLOSE fd = -1;
|
2021-09-26 11:27:26 +00:00
|
|
|
g_autoptr(virshStream) st = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *name = NULL;
|
|
|
|
unsigned long long offset = 0, length = 0;
|
|
|
|
bool created = false;
|
2021-03-11 07:16:13 +00:00
|
|
|
virshControl *priv = ctl->privData;
|
2020-07-02 06:42:44 +00:00
|
|
|
virshStreamCallbackData cbData;
|
2016-04-12 13:35:04 +00:00
|
|
|
unsigned int flags = 0;
|
2020-07-02 06:51:20 +00:00
|
|
|
struct stat sb;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-02 09:17:29 +00:00
|
|
|
if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0)
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2015-06-02 09:17:29 +00:00
|
|
|
if (vshCommandOptULongLongWrap(ctl, cmd, "length", &length) < 0)
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVol(ctl, cmd, "vol", "pool", &name)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "file", &file) < 0)
|
2012-07-24 08:44:49 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2016-04-12 13:35:04 +00:00
|
|
|
if (vshCommandOptBool(cmd, "sparse"))
|
|
|
|
flags |= VIR_STORAGE_VOL_DOWNLOAD_SPARSE_STREAM;
|
|
|
|
|
2012-07-24 08:44:49 +00:00
|
|
|
if ((fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
|
|
|
|
if (errno != EEXIST ||
|
|
|
|
(fd = open(file, O_WRONLY|O_TRUNC, 0666)) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot create %1$s"), file);
|
2012-07-24 08:44:49 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
created = true;
|
|
|
|
}
|
|
|
|
|
2020-07-02 06:51:20 +00:00
|
|
|
if (fstat(fd, &sb) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("unable to stat %1$s"), file);
|
2020-07-02 06:51:20 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2020-07-02 06:42:44 +00:00
|
|
|
cbData.ctl = ctl;
|
|
|
|
cbData.fd = fd;
|
2020-07-02 06:51:20 +00:00
|
|
|
cbData.isBlock = !!S_ISBLK(sb.st_mode);
|
2020-07-02 06:42:44 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(st = virStreamNew(priv->conn, 0))) {
|
2013-09-29 09:24:11 +00:00
|
|
|
vshError(ctl, _("cannot create a new stream"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2016-04-12 13:35:04 +00:00
|
|
|
if (virStorageVolDownload(vol, st, offset, length, flags) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot download from volume %1$s"), name);
|
2012-07-24 08:44:49 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2020-07-02 06:42:44 +00:00
|
|
|
if (virStreamSparseRecvAll(st, virshStreamSink, virshStreamSkip, &cbData) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot receive data from volume %1$s"), name);
|
2012-07-24 08:44:49 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot close file %1$s"), file);
|
2012-07-24 08:44:49 +00:00
|
|
|
virStreamAbort(st);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStreamFinish(st) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("cannot close volume %1$s"), name);
|
2012-07-24 08:44:49 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
|
2014-03-25 06:53:59 +00:00
|
|
|
cleanup:
|
2012-07-24 08:44:49 +00:00
|
|
|
if (!ret && created)
|
|
|
|
unlink(file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-delete" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_delete[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("delete a vol")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = N_("Delete a given vol.")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_delete[] = {
|
2021-06-16 08:02:52 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_FULL,
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2015-12-02 23:04:04 +00:00
|
|
|
{.name = "delete-snapshots",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("delete snapshots associated with volume (must be "
|
|
|
|
"supported by storage driver)")
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolDelete(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
bool ret = true;
|
|
|
|
const char *name;
|
2015-12-02 23:04:04 +00:00
|
|
|
bool delete_snapshots = vshCommandOptBool(cmd, "delete-snapshots");
|
|
|
|
unsigned int flags = 0;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVol(ctl, cmd, "vol", "pool", &name)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2015-12-02 23:04:04 +00:00
|
|
|
if (delete_snapshots)
|
|
|
|
flags |= VIR_STORAGE_VOL_DELETE_WITH_SNAPSHOTS;
|
|
|
|
|
|
|
|
if (virStorageVolDelete(vol, flags) == 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshPrintExtra(ctl, _("Vol %1$s deleted\n"), name);
|
2012-07-24 08:44:49 +00:00
|
|
|
} else {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Failed to delete vol %1$s"), name);
|
2012-07-24 08:44:49 +00:00
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-wipe" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_wipe[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("wipe a vol")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = N_("Ensure data previously on a volume is not accessible to future reads")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_wipe[] = {
|
2021-06-16 08:02:52 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_FULL,
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "algorithm",
|
|
|
|
.type = VSH_OT_STRING,
|
2022-03-10 15:22:24 +00:00
|
|
|
.completer = virshStorageVolWipeAlgorithmCompleter,
|
2013-01-14 14:48:52 +00:00
|
|
|
.help = N_("perform selected wiping algorithm")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
2021-07-19 11:23:29 +00:00
|
|
|
VIR_ENUM_IMPL(virshStorageVolWipeAlgorithm,
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_STORAGE_VOL_WIPE_ALG_LAST,
|
2012-07-24 08:44:49 +00:00
|
|
|
"zero", "nnsa", "dod", "bsi", "gutmann", "schneier",
|
2016-01-27 10:20:07 +00:00
|
|
|
"pfitzner7", "pfitzner33", "random", "trim");
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolWipe(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *name;
|
|
|
|
const char *algorithm_str = NULL;
|
|
|
|
int algorithm = VIR_STORAGE_VOL_WIPE_ALG_ZERO;
|
|
|
|
int funcRet;
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVol(ctl, cmd, "vol", "pool", &name)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2013-01-18 15:40:08 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "algorithm", &algorithm_str) < 0)
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
if (algorithm_str &&
|
2021-07-19 11:23:29 +00:00
|
|
|
(algorithm = virshStorageVolWipeAlgorithmTypeFromString(algorithm_str)) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Unsupported algorithm '%1$s'"), algorithm_str);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Failed to wipe vol %1$s"), name);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 14:54:58 +00:00
|
|
|
vshPrintExtra(ctl, _("Vol %1$s wiped\n"), name);
|
2021-11-04 14:26:07 +00:00
|
|
|
return true;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 15:13:41 +00:00
|
|
|
|
2019-01-20 16:04:56 +00:00
|
|
|
VIR_ENUM_DECL(virshStorageVol);
|
2015-06-15 16:53:58 +00:00
|
|
|
VIR_ENUM_IMPL(virshStorageVol,
|
virsh: use more compact VIR_ENUM_IMPL
Dan Berrange suggested that using VIR_ENUM_IMPL is more compact
than open-coding switch statements, and still just as forceful
at making us remember to update lists if we add enum values
in the future. Make this change throughout virsh.
Sure enough, doing this change caught that we missed at least
VIR_STORAGE_VOL_NETDIR.
* tools/virsh-domain-monitor.c (vshDomainIOErrorToString)
(vshDomainControlStateToString, vshDomainStateToString)
(vshDomainStateReasonToString): Change switch to enum lookup.
(cmdDomControl, cmdDominfo): Update caller.
* tools/virsh-domain.c (vshDomainVcpuStateToString)
(vshDomainEventToString, vshDomainEventDetailToString): Change
switch to enum lookup.
(vshDomainBlockJobToString, vshDomainJobToString): New functions.
(cmdVcpuinfo, cmdBlockJob, cmdDomjobinfo, cmdEvent): Update
callers.
* tools/virsh-network.c (vshNetworkEventToString): Change switch
to enum lookup.
* tools/virsh-pool.c (vshStoragePoolStateToString): New function.
(cmdPoolList, cmdPoolInfo): Update callers.
* tools/virsh-volume.c (vshVolumeTypeToString): Change switch to
enum lookup.
(cmdVolInfo, cmdVolList): Update callers.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-21 19:55:06 +00:00
|
|
|
VIR_STORAGE_VOL_LAST,
|
|
|
|
N_("file"),
|
|
|
|
N_("block"),
|
|
|
|
N_("dir"),
|
|
|
|
N_("network"),
|
2016-04-11 16:16:19 +00:00
|
|
|
N_("netdir"),
|
2019-01-20 16:30:15 +00:00
|
|
|
N_("ploop"));
|
virsh: use more compact VIR_ENUM_IMPL
Dan Berrange suggested that using VIR_ENUM_IMPL is more compact
than open-coding switch statements, and still just as forceful
at making us remember to update lists if we add enum values
in the future. Make this change throughout virsh.
Sure enough, doing this change caught that we missed at least
VIR_STORAGE_VOL_NETDIR.
* tools/virsh-domain-monitor.c (vshDomainIOErrorToString)
(vshDomainControlStateToString, vshDomainStateToString)
(vshDomainStateReasonToString): Change switch to enum lookup.
(cmdDomControl, cmdDominfo): Update caller.
* tools/virsh-domain.c (vshDomainVcpuStateToString)
(vshDomainEventToString, vshDomainEventDetailToString): Change
switch to enum lookup.
(vshDomainBlockJobToString, vshDomainJobToString): New functions.
(cmdVcpuinfo, cmdBlockJob, cmdDomjobinfo, cmdEvent): Update
callers.
* tools/virsh-network.c (vshNetworkEventToString): Change switch
to enum lookup.
* tools/virsh-pool.c (vshStoragePoolStateToString): New function.
(cmdPoolList, cmdPoolInfo): Update callers.
* tools/virsh-volume.c (vshVolumeTypeToString): Change switch to
enum lookup.
(cmdVolInfo, cmdVolList): Update callers.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-21 19:55:06 +00:00
|
|
|
|
2013-11-12 15:13:41 +00:00
|
|
|
static const char *
|
2015-06-15 16:53:58 +00:00
|
|
|
virshVolumeTypeToString(int type)
|
2013-11-12 15:13:41 +00:00
|
|
|
{
|
2015-06-15 16:53:58 +00:00
|
|
|
const char *str = virshStorageVolTypeToString(type);
|
virsh: use more compact VIR_ENUM_IMPL
Dan Berrange suggested that using VIR_ENUM_IMPL is more compact
than open-coding switch statements, and still just as forceful
at making us remember to update lists if we add enum values
in the future. Make this change throughout virsh.
Sure enough, doing this change caught that we missed at least
VIR_STORAGE_VOL_NETDIR.
* tools/virsh-domain-monitor.c (vshDomainIOErrorToString)
(vshDomainControlStateToString, vshDomainStateToString)
(vshDomainStateReasonToString): Change switch to enum lookup.
(cmdDomControl, cmdDominfo): Update caller.
* tools/virsh-domain.c (vshDomainVcpuStateToString)
(vshDomainEventToString, vshDomainEventDetailToString): Change
switch to enum lookup.
(vshDomainBlockJobToString, vshDomainJobToString): New functions.
(cmdVcpuinfo, cmdBlockJob, cmdDomjobinfo, cmdEvent): Update
callers.
* tools/virsh-network.c (vshNetworkEventToString): Change switch
to enum lookup.
* tools/virsh-pool.c (vshStoragePoolStateToString): New function.
(cmdPoolList, cmdPoolInfo): Update callers.
* tools/virsh-volume.c (vshVolumeTypeToString): Change switch to
enum lookup.
(cmdVolInfo, cmdVolList): Update callers.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-21 19:55:06 +00:00
|
|
|
return str ? _(str) : _("unknown");
|
2013-11-12 15:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-24 08:44:49 +00:00
|
|
|
/*
|
|
|
|
* "vol-info" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_info[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("storage vol information")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = N_("Returns basic information about the storage vol.")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_info[] = {
|
2021-06-16 08:02:52 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_FULL,
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2016-05-03 15:24:55 +00:00
|
|
|
{.name = "bytes",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("sizes are represented in bytes rather than pretty units")
|
|
|
|
},
|
2016-11-29 18:25:42 +00:00
|
|
|
{.name = "physical",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("return the physical size of the volume in allocation field")
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolInfo(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
|
|
|
virStorageVolInfo info;
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2016-05-03 15:24:55 +00:00
|
|
|
bool bytes = vshCommandOptBool(cmd, "bytes");
|
2016-11-29 18:25:42 +00:00
|
|
|
bool physical = vshCommandOptBool(cmd, "physical");
|
|
|
|
int rc;
|
|
|
|
unsigned int flags = 0;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
vshPrint(ctl, "%-15s %s\n", _("Name:"), virStorageVolGetName(vol));
|
|
|
|
|
2016-11-29 18:25:42 +00:00
|
|
|
if (physical)
|
|
|
|
flags |= VIR_STORAGE_VOL_GET_PHYSICAL;
|
|
|
|
|
|
|
|
if (flags)
|
|
|
|
rc = virStorageVolGetInfoFlags(vol, &info, flags);
|
|
|
|
else
|
|
|
|
rc = virStorageVolGetInfo(vol, &info);
|
|
|
|
|
2021-09-23 23:25:12 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2021-09-23 23:25:12 +00:00
|
|
|
vshPrint(ctl, "%-15s %s\n", _("Type:"),
|
|
|
|
virshVolumeTypeToString(info.type));
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2021-09-23 23:25:12 +00:00
|
|
|
if (bytes) {
|
|
|
|
vshPrint(ctl, "%-15s %llu %s\n", _("Capacity:"), info.capacity, _("bytes"));
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2021-09-23 23:25:12 +00:00
|
|
|
if (physical)
|
|
|
|
vshPrint(ctl, "%-15s %llu %s\n", _("Physical:"), info.allocation, _("bytes"));
|
|
|
|
else
|
|
|
|
vshPrint(ctl, "%-15s %llu %s\n", _("Allocation:"), info.allocation, _("bytes"));
|
2012-07-24 08:44:49 +00:00
|
|
|
} else {
|
2021-09-23 23:25:12 +00:00
|
|
|
const char *unit;
|
|
|
|
double val = vshPrettyCapacity(info.capacity, &unit);
|
|
|
|
|
|
|
|
vshPrint(ctl, "%-15s %2.2lf %s\n", _("Capacity:"), val, unit);
|
|
|
|
val = vshPrettyCapacity(info.allocation, &unit);
|
|
|
|
|
|
|
|
if (physical)
|
|
|
|
vshPrint(ctl, "%-15s %2.2lf %s\n", _("Physical:"), val, unit);
|
|
|
|
else
|
|
|
|
vshPrint(ctl, "%-15s %2.2lf %s\n", _("Allocation:"), val, unit);
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 23:25:12 +00:00
|
|
|
return true;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-resize" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_resize[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("resize a vol")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
2016-10-11 20:13:57 +00:00
|
|
|
.data = N_("Resizes a storage volume. This is safe only for storage "
|
|
|
|
"volumes not in use by an active guest.\n"
|
|
|
|
"See blockresize for live resizing.")
|
2013-02-07 15:25:10 +00:00
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_resize[] = {
|
2021-06-16 08:02:52 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_FULL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "capacity",
|
|
|
|
.type = VSH_OT_DATA,
|
|
|
|
.flags = VSH_OFLAG_REQ,
|
2021-09-15 15:42:08 +00:00
|
|
|
.completer = virshCompleteEmpty,
|
2013-01-14 14:48:52 +00:00
|
|
|
.help = N_("new capacity for the vol, as scaled integer (default bytes)")
|
|
|
|
},
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "allocate",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("allocate the new capacity, rather than leaving it sparse")
|
|
|
|
},
|
|
|
|
{.name = "delta",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("use capacity as a delta to current size, rather than the new size")
|
|
|
|
},
|
|
|
|
{.name = "shrink",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("allow the resize to shrink the volume")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolResize(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *capacityStr = NULL;
|
|
|
|
unsigned long long capacity = 0;
|
|
|
|
unsigned int flags = 0;
|
2015-05-27 14:46:01 +00:00
|
|
|
bool delta = vshCommandOptBool(cmd, "delta");
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
if (vshCommandOptBool(cmd, "allocate"))
|
|
|
|
flags |= VIR_STORAGE_VOL_RESIZE_ALLOCATE;
|
|
|
|
if (vshCommandOptBool(cmd, "shrink"))
|
|
|
|
flags |= VIR_STORAGE_VOL_RESIZE_SHRINK;
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2013-05-24 03:59:14 +00:00
|
|
|
if (vshCommandOptStringReq(ctl, cmd, "capacity", &capacityStr) < 0)
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
virSkipSpaces(&capacityStr);
|
|
|
|
if (*capacityStr == '-') {
|
|
|
|
/* The API always requires a positive value; but we allow a
|
|
|
|
* negative value for convenience. */
|
2015-05-27 14:46:01 +00:00
|
|
|
if (vshCommandOptBool(cmd, "shrink")) {
|
2012-07-24 08:44:49 +00:00
|
|
|
capacityStr++;
|
2015-05-27 14:46:01 +00:00
|
|
|
delta = true;
|
2012-07-24 08:44:49 +00:00
|
|
|
} else {
|
|
|
|
vshError(ctl, "%s",
|
2015-05-27 14:46:01 +00:00
|
|
|
_("negative size requires --shrink"));
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-27 14:46:01 +00:00
|
|
|
|
|
|
|
if (delta)
|
|
|
|
flags |= VIR_STORAGE_VOL_RESIZE_DELTA;
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (virshVolSize(capacityStr, &capacity) < 0) {
|
2023-03-09 14:54:58 +00:00
|
|
|
vshError(ctl, _("Malformed size %1$s"), capacityStr);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 15:09:48 +00:00
|
|
|
if (virStorageVolResize(vol, capacity, flags) < 0) {
|
2012-07-24 08:44:49 +00:00
|
|
|
vshError(ctl,
|
2023-03-09 14:54:58 +00:00
|
|
|
delta ? _("Failed to change size of volume '%1$s' by %2$s")
|
|
|
|
: _("Failed to change size of volume '%1$s' to %2$s"),
|
2012-07-24 08:44:49 +00:00
|
|
|
virStorageVolGetName(vol), capacityStr);
|
2021-11-04 14:26:07 +00:00
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 15:09:48 +00:00
|
|
|
vshPrintExtra(ctl,
|
2023-03-09 14:54:58 +00:00
|
|
|
delta ? _("Size of volume '%1$s' successfully changed by %2$s\n")
|
|
|
|
: _("Size of volume '%1$s' successfully changed to %2$s\n"),
|
2021-11-08 15:09:48 +00:00
|
|
|
virStorageVolGetName(vol), capacityStr);
|
2021-11-04 14:26:07 +00:00
|
|
|
return true;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-dumpxml" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_dumpxml[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("vol information in XML")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = N_("Output the vol information as an XML dump to stdout.")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_dumpxml[] = {
|
2021-06-16 08:02:52 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_FULL,
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2022-06-16 15:29:54 +00:00
|
|
|
{.name = "xpath",
|
|
|
|
.type = VSH_OT_STRING,
|
virsh: Require --xpath for *dumpxml
Historically, the dumpxml command reject any unknown arguments,
for instance:
virsh dumpxml fedora xxx
However, after v8.5.0-rc1~31 the second argument ('xxx') is
treated as an XPath, but it's not that clearly visible.
Therefore, require the --xpath switch, like this:
virsh dumpxml fedora --xpath xxx
Yes, this breaks already released virsh, but I think we can argue
that the pool of users of this particular function is very small.
We also document the argument being mandatory:
dumpxml [--inactive] [--security-info] [--update-cpu] [--migratable]
[--xpath EXPRESSION] [--wrap] domain
The sooner we do this change, the better.
The same applies for other *dumpxml functions (net-dumpxml,
pool-dumpxml, vol-dumpxl to name a few).
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2103524
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
2022-07-08 10:45:42 +00:00
|
|
|
.flags = VSH_OFLAG_REQ_OPT,
|
2022-06-16 15:29:54 +00:00
|
|
|
.completer = virshCompleteEmpty,
|
|
|
|
.help = N_("xpath expression to filter the XML document")
|
|
|
|
},
|
|
|
|
{.name = "wrap",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("wrap xpath results in an common root element"),
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolDumpXML(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2022-06-16 15:29:54 +00:00
|
|
|
bool wrap = vshCommandOptBool(cmd, "wrap");
|
|
|
|
const char *xpath = NULL;
|
|
|
|
g_autofree char *xml = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2022-06-16 15:29:54 +00:00
|
|
|
if (vshCommandOptStringQuiet(ctl, cmd, "xpath", &xpath) < 0)
|
|
|
|
return false;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2022-06-16 15:29:54 +00:00
|
|
|
if (!(xml = virStorageVolGetXMLDesc(vol, 0)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return virshDumpXML(ctl, xml, "volume", xpath, wrap);
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 15:32:57 +00:00
|
|
|
static int
|
lib: Replace qsort() with g_qsort_with_data()
While glibc provides qsort(), which usually is just a mergesort,
until sorting arrays so huge that temporary array used by
mergesort would not fit into physical memory (which in our case
is never), we are not guaranteed it'll use mergesort. The
advantage of mergesort is clear - it's stable. IOW, if we have an
array of values parsed from XML, qsort() it and produce some
output based on those values, we can then compare the output with
some expected output, line by line.
But with newer glibc this is all history. After [1], qsort() is
no longer mergesort but introsort instead, which is not stable.
This is suboptimal, because in some cases we want to preserve
order of equal items. For instance, in ebiptablesApplyNewRules(),
nwfilter rules are sorted by their priority. But if two rules
have the same priority, we want to keep them in the order they
appear in the XML. Since it's hard/needless work to identify
places where stable or unstable sorting is needed, let's just
play it safe and use stable sorting everywhere.
Fortunately, glib provides g_qsort_with_data() which indeed
implement mergesort and it's a drop in replacement for qsort(),
almost. It accepts fifth argument (pointer to opaque data), that
is passed to comparator function, which then accepts three
arguments.
We have to keep one occurance of qsort() though - in NSS module
which deliberately does not link with glib.
1: https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=03bf8357e8291857a435afcc3048e0b697b6cc04
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
2023-11-22 13:58:49 +00:00
|
|
|
virshStorageVolSorter(const void *a,
|
|
|
|
const void *b,
|
|
|
|
void *opaque G_GNUC_UNUSED)
|
2012-09-04 15:32:57 +00:00
|
|
|
{
|
|
|
|
virStorageVolPtr *va = (virStorageVolPtr *) a;
|
|
|
|
virStorageVolPtr *vb = (virStorageVolPtr *) b;
|
|
|
|
|
|
|
|
if (*va && !*vb)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!*va)
|
|
|
|
return *vb != NULL;
|
|
|
|
|
|
|
|
return vshStrcasecmp(virStorageVolGetName(*va),
|
|
|
|
virStorageVolGetName(*vb));
|
|
|
|
}
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
struct virshStorageVolList {
|
2012-09-04 15:32:57 +00:00
|
|
|
virStorageVolPtr *vols;
|
|
|
|
size_t nvols;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2021-03-11 07:16:13 +00:00
|
|
|
virshStorageVolListFree(struct virshStorageVolList *list)
|
2012-09-04 15:32:57 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in tools/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2012-09-04 15:32:57 +00:00
|
|
|
|
|
|
|
if (list && list->vols) {
|
|
|
|
for (i = 0; i < list->nvols; i++) {
|
2021-09-26 08:28:06 +00:00
|
|
|
virshStorageVolFree(list->vols[i]);
|
2012-09-04 15:32:57 +00:00
|
|
|
}
|
2021-02-03 19:32:55 +00:00
|
|
|
g_free(list->vols);
|
2012-09-04 15:32:57 +00:00
|
|
|
}
|
2021-02-03 19:32:55 +00:00
|
|
|
g_free(list);
|
2012-09-04 15:32:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static struct virshStorageVolList *
|
2015-06-15 16:53:58 +00:00
|
|
|
virshStorageVolListCollect(vshControl *ctl,
|
|
|
|
virStoragePoolPtr pool,
|
|
|
|
unsigned int flags)
|
2012-09-04 15:32:57 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
struct virshStorageVolList *list = g_new0(struct virshStorageVolList, 1);
|
Convert 'int i' to 'size_t i' in tools/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2012-09-04 15:32:57 +00:00
|
|
|
char **names = NULL;
|
|
|
|
virStorageVolPtr vol = NULL;
|
|
|
|
bool success = false;
|
|
|
|
size_t deleted = 0;
|
|
|
|
int nvols = 0;
|
|
|
|
int ret = -1;
|
|
|
|
|
2012-09-13 08:56:07 +00:00
|
|
|
/* try the list with flags support (0.10.2 and later) */
|
2012-09-04 15:32:57 +00:00
|
|
|
if ((ret = virStoragePoolListAllVolumes(pool,
|
|
|
|
&list->vols,
|
|
|
|
flags)) >= 0) {
|
|
|
|
list->nvols = ret;
|
|
|
|
goto finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if the command is actually supported */
|
2012-09-13 08:56:07 +00:00
|
|
|
if (last_error && last_error->code == VIR_ERR_NO_SUPPORT)
|
2012-09-04 15:32:57 +00:00
|
|
|
goto fallback;
|
|
|
|
|
|
|
|
/* there was an error during the call */
|
|
|
|
vshError(ctl, "%s", _("Failed to list volumes"));
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-03-25 06:53:59 +00:00
|
|
|
fallback:
|
2012-09-13 08:56:07 +00:00
|
|
|
/* fall back to old method (0.10.1 and older) */
|
2012-09-04 15:32:57 +00:00
|
|
|
vshResetLibvirtError();
|
|
|
|
|
|
|
|
/* Determine the number of volumes in the pool */
|
|
|
|
if ((nvols = virStoragePoolNumOfVolumes(pool)) < 0) {
|
|
|
|
vshError(ctl, "%s", _("Failed to list storage volumes"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-04-13 02:16:27 +00:00
|
|
|
if (nvols == 0)
|
2012-09-04 15:32:57 +00:00
|
|
|
return list;
|
|
|
|
|
|
|
|
/* Retrieve the list of volume names in the pool */
|
2020-10-05 16:50:09 +00:00
|
|
|
names = g_new0(char *, nvols);
|
2012-09-04 15:32:57 +00:00
|
|
|
if ((nvols = virStoragePoolListVolumes(pool, names, nvols)) < 0) {
|
|
|
|
vshError(ctl, "%s", _("Failed to list storage volumes"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2020-10-05 16:50:09 +00:00
|
|
|
list->vols = g_new0(virStorageVolPtr, nvols);
|
2012-09-04 15:32:57 +00:00
|
|
|
list->nvols = 0;
|
|
|
|
|
|
|
|
/* get the vols */
|
|
|
|
for (i = 0; i < nvols; i++) {
|
|
|
|
if (!(vol = virStorageVolLookupByName(pool, names[i])))
|
|
|
|
continue;
|
|
|
|
list->vols[list->nvols++] = vol;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* truncate the list for not found vols */
|
|
|
|
deleted = nvols - list->nvols;
|
|
|
|
|
2014-03-25 06:53:59 +00:00
|
|
|
finished:
|
2012-09-04 15:32:57 +00:00
|
|
|
/* sort the list */
|
lib: Replace qsort() with g_qsort_with_data()
While glibc provides qsort(), which usually is just a mergesort,
until sorting arrays so huge that temporary array used by
mergesort would not fit into physical memory (which in our case
is never), we are not guaranteed it'll use mergesort. The
advantage of mergesort is clear - it's stable. IOW, if we have an
array of values parsed from XML, qsort() it and produce some
output based on those values, we can then compare the output with
some expected output, line by line.
But with newer glibc this is all history. After [1], qsort() is
no longer mergesort but introsort instead, which is not stable.
This is suboptimal, because in some cases we want to preserve
order of equal items. For instance, in ebiptablesApplyNewRules(),
nwfilter rules are sorted by their priority. But if two rules
have the same priority, we want to keep them in the order they
appear in the XML. Since it's hard/needless work to identify
places where stable or unstable sorting is needed, let's just
play it safe and use stable sorting everywhere.
Fortunately, glib provides g_qsort_with_data() which indeed
implement mergesort and it's a drop in replacement for qsort(),
almost. It accepts fifth argument (pointer to opaque data), that
is passed to comparator function, which then accepts three
arguments.
We have to keep one occurance of qsort() though - in NSS module
which deliberately does not link with glib.
1: https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=03bf8357e8291857a435afcc3048e0b697b6cc04
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
2023-11-22 13:58:49 +00:00
|
|
|
if (list->vols && list->nvols) {
|
|
|
|
g_qsort_with_data(list->vols, list->nvols,
|
|
|
|
sizeof(*list->vols), virshStorageVolSorter, NULL);
|
|
|
|
}
|
2012-09-04 15:32:57 +00:00
|
|
|
|
|
|
|
if (deleted)
|
|
|
|
VIR_SHRINK_N(list->vols, list->nvols, deleted);
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
|
2014-03-25 06:53:59 +00:00
|
|
|
cleanup:
|
Convert 'int i' to 'size_t i' in tools/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
if (nvols > 0)
|
|
|
|
for (i = 0; i < nvols; i++)
|
|
|
|
VIR_FREE(names[i]);
|
2012-09-04 15:32:57 +00:00
|
|
|
VIR_FREE(names);
|
|
|
|
|
|
|
|
if (!success) {
|
2022-01-28 17:42:45 +00:00
|
|
|
g_clear_pointer(&list, virshStorageVolListFree);
|
2012-09-04 15:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2012-07-24 08:44:49 +00:00
|
|
|
/*
|
|
|
|
* "vol-list" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_list[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("list vols")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = N_("Returns list of vols by pool.")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_list[] = {
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_FULL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = "details",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("display extended details for volumes")
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
2019-10-14 12:44:29 +00:00
|
|
|
cmdVolList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
|
2012-07-24 08:44:49 +00:00
|
|
|
{
|
|
|
|
virStorageVolInfo volumeInfo;
|
2021-09-26 07:25:41 +00:00
|
|
|
g_autoptr(virshStoragePool) pool = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
const char *unit;
|
|
|
|
double val;
|
|
|
|
bool details = vshCommandOptBool(cmd, "details");
|
Convert 'int i' to 'size_t i' in tools/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2014-02-21 21:33:30 +00:00
|
|
|
bool ret = false;
|
2012-07-24 08:44:49 +00:00
|
|
|
struct volInfoText {
|
|
|
|
char *allocation;
|
|
|
|
char *capacity;
|
|
|
|
char *path;
|
|
|
|
char *type;
|
|
|
|
};
|
|
|
|
struct volInfoText *volInfoTexts = NULL;
|
2021-03-11 07:16:13 +00:00
|
|
|
struct virshStorageVolList *list = NULL;
|
2021-08-11 13:12:02 +00:00
|
|
|
g_autoptr(vshTable) table = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
/* Look up the pool information given to us by the user */
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(pool = virshCommandOptPool(ctl, cmd, "pool", NULL)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(list = virshStorageVolListCollect(ctl, pool, 0)))
|
2012-09-04 15:32:57 +00:00
|
|
|
goto cleanup;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2012-09-04 15:32:57 +00:00
|
|
|
if (list->nvols > 0)
|
2020-10-05 16:50:09 +00:00
|
|
|
volInfoTexts = g_new0(struct volInfoText, list->nvols);
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
/* Collect the rest of the volume information for display */
|
2012-09-04 15:32:57 +00:00
|
|
|
for (i = 0; i < list->nvols; i++) {
|
2012-07-24 08:44:49 +00:00
|
|
|
/* Retrieve volume info */
|
2012-09-04 15:32:57 +00:00
|
|
|
virStorageVolPtr vol = list->vols[i];
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
/* Retrieve the volume path */
|
|
|
|
if ((volInfoTexts[i].path = virStorageVolGetPath(vol)) == NULL) {
|
|
|
|
/* Something went wrong retrieving a volume path, cope with it */
|
2019-10-18 15:24:02 +00:00
|
|
|
volInfoTexts[i].path = g_strdup(_("unknown"));
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If requested, retrieve volume type and sizing information */
|
|
|
|
if (details) {
|
|
|
|
if (virStorageVolGetInfo(vol, &volumeInfo) != 0) {
|
|
|
|
/* Something went wrong retrieving volume info, cope with it */
|
2019-10-18 15:24:02 +00:00
|
|
|
volInfoTexts[i].allocation = g_strdup(_("unknown"));
|
|
|
|
volInfoTexts[i].capacity = g_strdup(_("unknown"));
|
|
|
|
volInfoTexts[i].type = g_strdup(_("unknown"));
|
2012-07-24 08:44:49 +00:00
|
|
|
} else {
|
|
|
|
/* Convert the returned volume info into output strings */
|
|
|
|
|
|
|
|
/* Volume type */
|
2019-10-18 15:24:02 +00:00
|
|
|
volInfoTexts[i].type = g_strdup(virshVolumeTypeToString(volumeInfo.type));
|
2012-07-24 08:44:49 +00:00
|
|
|
|
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 04:10:17 +00:00
|
|
|
val = vshPrettyCapacity(volumeInfo.capacity, &unit);
|
2019-10-22 13:26:14 +00:00
|
|
|
volInfoTexts[i].capacity = g_strdup_printf("%.2lf %s", val, unit);
|
2014-02-21 21:33:30 +00:00
|
|
|
|
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 04:10:17 +00:00
|
|
|
val = vshPrettyCapacity(volumeInfo.allocation, &unit);
|
2019-10-22 13:26:14 +00:00
|
|
|
volInfoTexts[i].allocation = g_strdup_printf("%.2lf %s", val,
|
|
|
|
unit);
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the --details option wasn't selected, we output the volume
|
|
|
|
* info using the fixed string format from previous versions to
|
|
|
|
* maintain backward compatibility.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Output basic info then return if --details option not selected */
|
|
|
|
if (!details) {
|
|
|
|
/* The old output format */
|
2018-09-21 14:17:23 +00:00
|
|
|
table = vshTableNew(_("Name"), _("Path"), NULL);
|
|
|
|
if (!table)
|
|
|
|
goto cleanup;
|
|
|
|
|
2012-09-04 15:32:57 +00:00
|
|
|
for (i = 0; i < list->nvols; i++) {
|
2018-09-21 14:17:23 +00:00
|
|
|
if (vshTableRowAppend(table,
|
|
|
|
virStorageVolGetName(list->vols[i]),
|
|
|
|
volInfoTexts[i].path,
|
|
|
|
NULL) < 0)
|
|
|
|
goto cleanup;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:17:23 +00:00
|
|
|
vshTablePrintToStdout(table, ctl);
|
|
|
|
|
2012-07-24 08:44:49 +00:00
|
|
|
/* Cleanup and return */
|
2014-02-21 21:33:30 +00:00
|
|
|
ret = true;
|
2012-07-24 08:44:49 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We only get here if the --details option was selected. */
|
|
|
|
|
2018-09-21 14:17:23 +00:00
|
|
|
/* Insert the header into table */
|
|
|
|
table = vshTableNew(_("Name"), _("Path"), _("Type"), _("Capacity"), _("Allocation"), NULL);
|
|
|
|
if (!table)
|
2014-02-21 21:33:30 +00:00
|
|
|
goto cleanup;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2018-09-21 14:17:23 +00:00
|
|
|
/* Insert the volume info rows into table */
|
2012-09-04 15:32:57 +00:00
|
|
|
for (i = 0; i < list->nvols; i++) {
|
2018-09-21 14:17:23 +00:00
|
|
|
if (vshTableRowAppend(table,
|
|
|
|
virStorageVolGetName(list->vols[i]),
|
|
|
|
volInfoTexts[i].path,
|
|
|
|
volInfoTexts[i].type,
|
|
|
|
volInfoTexts[i].capacity,
|
|
|
|
volInfoTexts[i].allocation,
|
|
|
|
NULL) < 0)
|
|
|
|
goto cleanup;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:17:23 +00:00
|
|
|
vshTablePrintToStdout(table, ctl);
|
|
|
|
|
2012-07-24 08:44:49 +00:00
|
|
|
/* Cleanup and return */
|
2014-02-21 21:33:30 +00:00
|
|
|
ret = true;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2014-03-25 06:53:59 +00:00
|
|
|
cleanup:
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
/* Safely free the memory allocated in this function */
|
2012-09-04 15:32:57 +00:00
|
|
|
if (list && list->nvols) {
|
|
|
|
for (i = 0; i < list->nvols; i++) {
|
|
|
|
/* Cleanup the memory for one volume info structure per loop */
|
|
|
|
VIR_FREE(volInfoTexts[i].path);
|
|
|
|
VIR_FREE(volInfoTexts[i].type);
|
|
|
|
VIR_FREE(volInfoTexts[i].capacity);
|
|
|
|
VIR_FREE(volInfoTexts[i].allocation);
|
|
|
|
}
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup remaining memory */
|
|
|
|
VIR_FREE(volInfoTexts);
|
2015-06-15 16:53:58 +00:00
|
|
|
virshStorageVolListFree(list);
|
2012-07-24 08:44:49 +00:00
|
|
|
|
|
|
|
/* Return the desired value */
|
2014-02-21 21:33:30 +00:00
|
|
|
return ret;
|
2012-07-24 08:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-name" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_name[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("returns the volume name for a given volume key or path")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = ""
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_name[] = {
|
2021-06-16 08:02:53 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_BY_KEY,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolName(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVolBy(ctl, cmd, "vol", NULL, NULL,
|
|
|
|
VIRSH_BYUUID)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
vshPrint(ctl, "%s\n", virStorageVolGetName(vol));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-pool" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_pool[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("returns the storage pool for a given volume key or path")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = ""
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_pool[] = {
|
2021-06-16 08:02:53 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_BY_KEY,
|
2014-11-11 09:45:24 +00:00
|
|
|
{.name = "uuid",
|
|
|
|
.type = VSH_OT_BOOL,
|
|
|
|
.help = N_("return the pool uuid rather than pool name")
|
|
|
|
},
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolPool(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 07:25:41 +00:00
|
|
|
g_autoptr(virshStoragePool) pool = NULL;
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
char uuid[VIR_UUID_STRING_BUFLEN];
|
|
|
|
|
|
|
|
/* Use the supplied string to locate the volume */
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVolBy(ctl, cmd, "vol", NULL, NULL,
|
|
|
|
VIRSH_BYUUID))) {
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up the parent storage pool for the volume */
|
|
|
|
pool = virStoragePoolLookupByVolume(vol);
|
|
|
|
if (pool == NULL) {
|
|
|
|
vshError(ctl, "%s", _("failed to get parent pool"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the requested details of the parent storage pool */
|
|
|
|
if (vshCommandOptBool(cmd, "uuid")) {
|
|
|
|
/* Retrieve and return pool UUID string */
|
|
|
|
if (virStoragePoolGetUUIDString(pool, &uuid[0]) == 0)
|
|
|
|
vshPrint(ctl, "%s\n", uuid);
|
|
|
|
} else {
|
|
|
|
/* Return the storage pool name */
|
|
|
|
vshPrint(ctl, "%s\n", virStoragePoolGetName(pool));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-key" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_key[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("returns the volume key for a given volume name or path")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = ""
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_key[] = {
|
2021-06-16 08:02:54 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_NAME(N_("volume name or path")),
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolKey(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
vshPrint(ctl, "%s\n", virStorageVolGetKey(vol));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "vol-path" command
|
|
|
|
*/
|
|
|
|
static const vshCmdInfo info_vol_path[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "help",
|
|
|
|
.data = N_("returns the volume path for a given volume name or key")
|
|
|
|
},
|
|
|
|
{.name = "desc",
|
|
|
|
.data = ""
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const vshCmdOptDef opts_vol_path[] = {
|
2021-06-16 08:02:54 +00:00
|
|
|
VIRSH_COMMON_OPT_VOL_NAME(N_("volume name or key")),
|
2016-01-09 13:36:23 +00:00
|
|
|
VIRSH_COMMON_OPT_POOL_OPTIONAL,
|
2013-01-14 14:48:52 +00:00
|
|
|
{.name = NULL}
|
2012-07-24 08:44:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
cmdVolPath(vshControl *ctl, const vshCmd *cmd)
|
|
|
|
{
|
2021-09-26 08:28:06 +00:00
|
|
|
g_autoptr(virshStorageVol) vol = NULL;
|
2021-08-11 13:25:15 +00:00
|
|
|
g_autofree char *StorageVolPath = NULL;
|
2012-07-24 08:44:49 +00:00
|
|
|
|
2015-06-15 16:53:58 +00:00
|
|
|
if (!(vol = virshCommandOptVol(ctl, cmd, "vol", "pool", NULL)))
|
2012-07-24 08:44:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if ((StorageVolPath = virStorageVolGetPath(vol)) == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
vshPrint(ctl, "%s\n", StorageVolPath);
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-23 07:19:04 +00:00
|
|
|
|
2012-08-20 23:41:24 +00:00
|
|
|
const vshCmdDef storageVolCmds[] = {
|
2013-02-07 15:25:10 +00:00
|
|
|
{.name = "vol-clone",
|
|
|
|
.handler = cmdVolClone,
|
|
|
|
.opts = opts_vol_clone,
|
|
|
|
.info = info_vol_clone,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-create-as",
|
|
|
|
.handler = cmdVolCreateAs,
|
|
|
|
.opts = opts_vol_create_as,
|
|
|
|
.info = info_vol_create_as,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-create",
|
|
|
|
.handler = cmdVolCreate,
|
|
|
|
.opts = opts_vol_create,
|
|
|
|
.info = info_vol_create,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-create-from",
|
|
|
|
.handler = cmdVolCreateFrom,
|
|
|
|
.opts = opts_vol_create_from,
|
|
|
|
.info = info_vol_create_from,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-delete",
|
|
|
|
.handler = cmdVolDelete,
|
|
|
|
.opts = opts_vol_delete,
|
|
|
|
.info = info_vol_delete,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-download",
|
|
|
|
.handler = cmdVolDownload,
|
|
|
|
.opts = opts_vol_download,
|
|
|
|
.info = info_vol_download,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-dumpxml",
|
|
|
|
.handler = cmdVolDumpXML,
|
|
|
|
.opts = opts_vol_dumpxml,
|
|
|
|
.info = info_vol_dumpxml,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-info",
|
|
|
|
.handler = cmdVolInfo,
|
|
|
|
.opts = opts_vol_info,
|
|
|
|
.info = info_vol_info,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-key",
|
|
|
|
.handler = cmdVolKey,
|
|
|
|
.opts = opts_vol_key,
|
|
|
|
.info = info_vol_key,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-list",
|
|
|
|
.handler = cmdVolList,
|
|
|
|
.opts = opts_vol_list,
|
|
|
|
.info = info_vol_list,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-name",
|
|
|
|
.handler = cmdVolName,
|
|
|
|
.opts = opts_vol_name,
|
|
|
|
.info = info_vol_name,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-path",
|
|
|
|
.handler = cmdVolPath,
|
|
|
|
.opts = opts_vol_path,
|
|
|
|
.info = info_vol_path,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-pool",
|
|
|
|
.handler = cmdVolPool,
|
|
|
|
.opts = opts_vol_pool,
|
|
|
|
.info = info_vol_pool,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-resize",
|
|
|
|
.handler = cmdVolResize,
|
|
|
|
.opts = opts_vol_resize,
|
|
|
|
.info = info_vol_resize,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-upload",
|
|
|
|
.handler = cmdVolUpload,
|
|
|
|
.opts = opts_vol_upload,
|
|
|
|
.info = info_vol_upload,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = "vol-wipe",
|
|
|
|
.handler = cmdVolWipe,
|
|
|
|
.opts = opts_vol_wipe,
|
|
|
|
.info = info_vol_wipe,
|
|
|
|
.flags = 0
|
|
|
|
},
|
|
|
|
{.name = NULL}
|
2012-07-23 07:19:04 +00:00
|
|
|
};
|