提交 258e06c8 编写于 作者: D Daniel P. Berrange

Remove all use of virRun in storage code

To make it easier to dynamically change the command line ARGV,
switch all storage code over to use virCommandPtr APIs for
running programs
Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
上级 0f9ad736
...@@ -57,7 +57,6 @@ ...@@ -57,7 +57,6 @@
#include "storage_backend.h" #include "storage_backend.h"
#include "logging.h" #include "logging.h"
#include "virfile.h" #include "virfile.h"
#include "command.h"
#if WITH_STORAGE_LVM #if WITH_STORAGE_LVM
# include "storage_backend_logical.h" # include "storage_backend_logical.h"
...@@ -1418,7 +1417,7 @@ virStorageBackendStablePath(virStoragePoolObjPtr pool, ...@@ -1418,7 +1417,7 @@ virStorageBackendStablePath(virStoragePoolObjPtr pool,
*/ */
int int
virStorageBackendRunProgRegex(virStoragePoolObjPtr pool, virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
const char *const*prog, virCommandPtr cmd,
int nregex, int nregex,
const char **regex, const char **regex,
int *nvars, int *nvars,
...@@ -1433,7 +1432,6 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool, ...@@ -1433,7 +1432,6 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
int maxReg = 0, i, j; int maxReg = 0, i, j;
int totgroups = 0, ngroup = 0, maxvars = 0; int totgroups = 0, ngroup = 0, maxvars = 0;
char **groups; char **groups;
virCommandPtr cmd = NULL;
/* Compile all regular expressions */ /* Compile all regular expressions */
if (VIR_ALLOC_N(reg, nregex) < 0) { if (VIR_ALLOC_N(reg, nregex) < 0) {
...@@ -1470,7 +1468,6 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool, ...@@ -1470,7 +1468,6 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
goto cleanup; goto cleanup;
} }
cmd = virCommandNewArgs(prog);
virCommandSetOutputFD(cmd, &fd); virCommandSetOutputFD(cmd, &fd);
if (virCommandRunAsync(cmd, NULL) < 0) { if (virCommandRunAsync(cmd, NULL) < 0) {
goto cleanup; goto cleanup;
...@@ -1541,7 +1538,6 @@ cleanup: ...@@ -1541,7 +1538,6 @@ cleanup:
regfree(&reg[i]); regfree(&reg[i]);
VIR_FREE(reg); VIR_FREE(reg);
virCommandFree(cmd);
VIR_FORCE_FCLOSE(list); VIR_FORCE_FCLOSE(list);
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
...@@ -1562,7 +1558,7 @@ cleanup: ...@@ -1562,7 +1558,7 @@ cleanup:
*/ */
int int
virStorageBackendRunProgNul(virStoragePoolObjPtr pool, virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
const char **prog, virCommandPtr cmd,
size_t n_columns, size_t n_columns,
virStorageBackendListVolNulFunc func, virStorageBackendListVolNulFunc func,
void *data) void *data)
...@@ -1573,7 +1569,6 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool, ...@@ -1573,7 +1569,6 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
char **v; char **v;
int ret = -1; int ret = -1;
int i; int i;
virCommandPtr cmd = NULL;
if (n_columns == 0) if (n_columns == 0)
return -1; return -1;
...@@ -1585,7 +1580,6 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool, ...@@ -1585,7 +1580,6 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
for (i = 0; i < n_columns; i++) for (i = 0; i < n_columns; i++)
v[i] = NULL; v[i] = NULL;
cmd = virCommandNewArgs(prog);
virCommandSetOutputFD(cmd, &fd); virCommandSetOutputFD(cmd, &fd);
if (virCommandRunAsync(cmd, NULL) < 0) { if (virCommandRunAsync(cmd, NULL) < 0) {
goto cleanup; goto cleanup;
...@@ -1622,9 +1616,9 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool, ...@@ -1622,9 +1616,9 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
} }
} }
if (feof (fp) < 0) { if (feof(fp) < 0) {
virReportSystemError(errno, virReportSystemError(errno, "%s",
_("read error on pipe to '%s'"), prog[0]); _("read error on pipe"));
goto cleanup; goto cleanup;
} }
...@@ -1633,7 +1627,6 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool, ...@@ -1633,7 +1627,6 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
for (i = 0; i < n_columns; i++) for (i = 0; i < n_columns; i++)
VIR_FREE(v[i]); VIR_FREE(v[i]);
VIR_FREE(v); VIR_FREE(v);
virCommandFree(cmd);
VIR_FORCE_FCLOSE(fp); VIR_FORCE_FCLOSE(fp);
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
# include <stdint.h> # include <stdint.h>
# include "internal.h" # include "internal.h"
# include "storage_conf.h" # include "storage_conf.h"
# include "command.h"
typedef char * (*virStorageBackendFindPoolSources)(virConnectPtr conn, const char *srcSpec, unsigned int flags); typedef char * (*virStorageBackendFindPoolSources)(virConnectPtr conn, const char *srcSpec, unsigned int flags);
typedef int (*virStorageBackendCheckPool)(virConnectPtr conn, virStoragePoolObjPtr pool, bool *active); typedef int (*virStorageBackendCheckPool)(virConnectPtr conn, virStoragePoolObjPtr pool, bool *active);
...@@ -141,7 +142,7 @@ typedef int (*virStorageBackendListVolNulFunc)(virStoragePoolObjPtr pool, ...@@ -141,7 +142,7 @@ typedef int (*virStorageBackendListVolNulFunc)(virStoragePoolObjPtr pool,
void *data); void *data);
int virStorageBackendRunProgRegex(virStoragePoolObjPtr pool, int virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
const char *const*prog, virCommandPtr cmd,
int nregex, int nregex,
const char **regex, const char **regex,
int *nvars, int *nvars,
...@@ -149,7 +150,7 @@ int virStorageBackendRunProgRegex(virStoragePoolObjPtr pool, ...@@ -149,7 +150,7 @@ int virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
void *data, const char *cmd_to_ignore); void *data, const char *cmd_to_ignore);
int virStorageBackendRunProgNul(virStoragePoolObjPtr pool, int virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
const char **prog, virCommandPtr cmd,
size_t n_columns, size_t n_columns,
virStorageBackendListVolNulFunc func, virStorageBackendListVolNulFunc func,
void *data); void *data);
......
...@@ -269,17 +269,20 @@ virStorageBackendDiskReadPartitions(virStoragePoolObjPtr pool, ...@@ -269,17 +269,20 @@ virStorageBackendDiskReadPartitions(virStoragePoolObjPtr pool,
* - normal metadata 100027630080 100030242304 2612736 * - normal metadata 100027630080 100030242304 2612736
* *
*/ */
const char *prog[] = { virCommandPtr cmd = virCommandNewArgList(PARTHELPER,
PARTHELPER, pool->def->source.devices[0].path, NULL, pool->def->source.devices[0].path,
}; NULL);
int ret;
pool->def->allocation = pool->def->capacity = pool->def->available = 0; pool->def->allocation = pool->def->capacity = pool->def->available = 0;
return virStorageBackendRunProgNul(pool, ret = virStorageBackendRunProgNul(pool,
prog, cmd,
6, 6,
virStorageBackendDiskMakeVol, virStorageBackendDiskMakeVol,
vol); vol);
virCommandFree(cmd);
return ret;
} }
static int static int
...@@ -299,15 +302,19 @@ virStorageBackendDiskMakePoolGeometry(virStoragePoolObjPtr pool, ...@@ -299,15 +302,19 @@ virStorageBackendDiskMakePoolGeometry(virStoragePoolObjPtr pool,
static int static int
virStorageBackendDiskReadGeometry(virStoragePoolObjPtr pool) virStorageBackendDiskReadGeometry(virStoragePoolObjPtr pool)
{ {
const char *prog[] = { virCommandPtr cmd = virCommandNewArgList(PARTHELPER,
PARTHELPER, pool->def->source.devices[0].path, "-g", NULL, pool->def->source.devices[0].path,
}; "-g",
NULL);
int ret;
return virStorageBackendRunProgNul(pool, ret = virStorageBackendRunProgNul(pool,
prog, cmd,
3, 3,
virStorageBackendDiskMakePoolGeometry, virStorageBackendDiskMakePoolGeometry,
NULL); NULL);
virCommandFree(cmd);
return ret;
} }
static int static int
...@@ -379,15 +386,13 @@ virStorageBackendDiskBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -379,15 +386,13 @@ virStorageBackendDiskBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED,
bool ok_to_mklabel = false; bool ok_to_mklabel = false;
int ret = -1; int ret = -1;
/* eg parted /dev/sda mklabel msdos */ /* eg parted /dev/sda mklabel msdos */
const char *prog[] = { virCommandPtr cmd = virCommandNewArgList(PARTED,
PARTED,
pool->def->source.devices[0].path, pool->def->source.devices[0].path,
"mklabel", "mklabel",
"--script", "--script",
((pool->def->source.format == VIR_STORAGE_POOL_DISK_DOS) ? "msdos" : ((pool->def->source.format == VIR_STORAGE_POOL_DISK_DOS) ? "msdos" :
virStoragePoolFormatDiskTypeToString(pool->def->source.format)), virStoragePoolFormatDiskTypeToString(pool->def->source.format)),
NULL, NULL);
};
virCheckFlags(VIR_STORAGE_POOL_BUILD_OVERWRITE | virCheckFlags(VIR_STORAGE_POOL_BUILD_OVERWRITE |
VIR_STORAGE_POOL_BUILD_NO_OVERWRITE, ret); VIR_STORAGE_POOL_BUILD_NO_OVERWRITE, ret);
...@@ -419,9 +424,10 @@ virStorageBackendDiskBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -419,9 +424,10 @@ virStorageBackendDiskBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED,
} }
if (ok_to_mklabel) if (ok_to_mklabel)
ret = virRun(prog, NULL); ret = virCommandRun(cmd, NULL);
error: error:
virCommandFree(cmd);
return ret; return ret;
} }
...@@ -628,20 +634,13 @@ virStorageBackendDiskCreateVol(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -628,20 +634,13 @@ virStorageBackendDiskCreateVol(virConnectPtr conn ATTRIBUTE_UNUSED,
virStorageVolDefPtr vol) virStorageVolDefPtr vol)
{ {
int res = -1; int res = -1;
char *start = NULL;
char *end = NULL;
char *partFormat; char *partFormat;
unsigned long long startOffset = 0, endOffset = 0; unsigned long long startOffset = 0, endOffset = 0;
const char *cmdargv[] = { virCommandPtr cmd = virCommandNewArgList(PARTED,
PARTED,
pool->def->source.devices[0].path, pool->def->source.devices[0].path,
"mkpart", "mkpart",
"--script", "--script",
NULL /*partFormat*/, NULL);
NULL /*start*/,
NULL /*end*/,
NULL
};
if (vol->target.encryption != NULL) { if (vol->target.encryption != NULL) {
virStorageReportError(VIR_ERR_CONFIG_UNSUPPORTED, virStorageReportError(VIR_ERR_CONFIG_UNSUPPORTED,
...@@ -653,7 +652,7 @@ virStorageBackendDiskCreateVol(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -653,7 +652,7 @@ virStorageBackendDiskCreateVol(virConnectPtr conn ATTRIBUTE_UNUSED,
if (virStorageBackendDiskPartFormat(pool, vol, &partFormat) != 0) { if (virStorageBackendDiskPartFormat(pool, vol, &partFormat) != 0) {
return -1; return -1;
} }
cmdargv[4] = partFormat; virCommandAddArg(cmd, partFormat);
if (virStorageBackendDiskPartBoundries(pool, &startOffset, if (virStorageBackendDiskPartBoundries(pool, &startOffset,
&endOffset, &endOffset,
...@@ -661,15 +660,10 @@ virStorageBackendDiskCreateVol(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -661,15 +660,10 @@ virStorageBackendDiskCreateVol(virConnectPtr conn ATTRIBUTE_UNUSED,
goto cleanup; goto cleanup;
} }
if (virAsprintf(&start, "%lluB", startOffset) < 0 || virCommandAddArgFormat(cmd, "%lluB", startOffset);
virAsprintf(&end, "%lluB", endOffset) < 0) { virCommandAddArgFormat(cmd, "%lluB", endOffset);
virReportOOMError();
goto cleanup;
}
cmdargv[5] = start;
cmdargv[6] = end;
if (virRun(cmdargv, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; goto cleanup;
/* wait for device node to show up */ /* wait for device node to show up */
...@@ -690,8 +684,7 @@ virStorageBackendDiskCreateVol(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -690,8 +684,7 @@ virStorageBackendDiskCreateVol(virConnectPtr conn ATTRIBUTE_UNUSED,
cleanup: cleanup:
VIR_FREE(partFormat); VIR_FREE(partFormat);
VIR_FREE(start); virCommandFree(cmd);
VIR_FREE(end);
return res; return res;
} }
......
...@@ -251,10 +251,10 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSE ...@@ -251,10 +251,10 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSE
.sources = NULL .sources = NULL
} }
}; };
const char *prog[] = { SHOWMOUNT, "--no-headers", "--exports", NULL, NULL };
virStoragePoolSourcePtr source = NULL; virStoragePoolSourcePtr source = NULL;
char *retval = NULL; char *retval = NULL;
unsigned int i; unsigned int i;
virCommandPtr cmd = NULL;
virCheckFlags(0, NULL); virCheckFlags(0, NULL);
...@@ -270,9 +270,14 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSE ...@@ -270,9 +270,14 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSE
} }
state.host = source->hosts[0].name; state.host = source->hosts[0].name;
prog[3] = source->hosts[0].name;
if (virStorageBackendRunProgRegex(NULL, prog, 1, regexes, vars, cmd = virCommandNewArgList(SHOWMOUNT,
"--no-headers",
"--exports",
source->hosts[0].name,
NULL);
if (virStorageBackendRunProgRegex(NULL, cmd, 1, regexes, vars,
virStorageBackendFileSystemNetFindPoolSourcesFunc, virStorageBackendFileSystemNetFindPoolSourcesFunc,
&state, NULL) < 0) &state, NULL) < 0)
goto cleanup; goto cleanup;
...@@ -289,7 +294,7 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSE ...@@ -289,7 +294,7 @@ virStorageBackendFileSystemNetFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSE
VIR_FREE(state.list.sources); VIR_FREE(state.list.sources);
virStoragePoolSourceFree(source); virStoragePoolSourceFree(source);
virCommandFree(cmd);
return retval; return retval;
} }
...@@ -337,63 +342,16 @@ virStorageBackendFileSystemIsMounted(virStoragePoolObjPtr pool) { ...@@ -337,63 +342,16 @@ virStorageBackendFileSystemIsMounted(virStoragePoolObjPtr pool) {
*/ */
static int static int
virStorageBackendFileSystemMount(virStoragePoolObjPtr pool) { virStorageBackendFileSystemMount(virStoragePoolObjPtr pool) {
char *src; char *src = NULL;
const char **mntargv;
/* 'mount -t auto' doesn't seem to auto determine nfs (or cifs), /* 'mount -t auto' doesn't seem to auto determine nfs (or cifs),
* while plain 'mount' does. We have to craft separate argvs to * while plain 'mount' does. We have to craft separate argvs to
* accommodate this */ * accommodate this */
int netauto = (pool->def->type == VIR_STORAGE_POOL_NETFS && bool netauto = (pool->def->type == VIR_STORAGE_POOL_NETFS &&
pool->def->source.format == VIR_STORAGE_POOL_NETFS_AUTO); pool->def->source.format == VIR_STORAGE_POOL_NETFS_AUTO);
int glusterfs = (pool->def->type == VIR_STORAGE_POOL_NETFS && bool glusterfs = (pool->def->type == VIR_STORAGE_POOL_NETFS &&
pool->def->source.format == VIR_STORAGE_POOL_NETFS_GLUSTERFS); pool->def->source.format == VIR_STORAGE_POOL_NETFS_GLUSTERFS);
virCommandPtr cmd = NULL;
int source_index; int ret = -1;
const char *netfs_auto_argv[] = {
MOUNT,
NULL, /* source path */
pool->def->target.path,
NULL,
};
const char *fs_argv[] = {
MOUNT,
"-t",
pool->def->type == VIR_STORAGE_POOL_FS ?
virStoragePoolFormatFileSystemTypeToString(pool->def->source.format) :
virStoragePoolFormatFileSystemNetTypeToString(pool->def->source.format),
NULL, /* Fill in shortly - careful not to add extra fields
before this */
pool->def->target.path,
NULL,
};
const char *glusterfs_argv[] = {
MOUNT,
"-t",
pool->def->type == VIR_STORAGE_POOL_FS ?
virStoragePoolFormatFileSystemTypeToString(pool->def->source.format) :
virStoragePoolFormatFileSystemNetTypeToString(pool->def->source.format),
NULL,
"-o",
"direct-io-mode=1",
pool->def->target.path,
NULL,
};
if (netauto) {
mntargv = netfs_auto_argv;
source_index = 1;
} else if (glusterfs) {
mntargv = glusterfs_argv;
source_index = 3;
} else {
mntargv = fs_argv;
source_index = 3;
}
int ret;
if (pool->def->type == VIR_STORAGE_POOL_NETFS) { if (pool->def->type == VIR_STORAGE_POOL_NETFS) {
if (pool->def->source.nhost != 1) { if (pool->def->source.nhost != 1) {
...@@ -441,14 +399,41 @@ virStorageBackendFileSystemMount(virStoragePoolObjPtr pool) { ...@@ -441,14 +399,41 @@ virStorageBackendFileSystemMount(virStoragePoolObjPtr pool) {
return -1; return -1;
} }
} }
mntargv[source_index] = src;
if (virRun(mntargv, NULL) < 0) { if (netauto)
VIR_FREE(src); cmd = virCommandNewArgList(MOUNT,
return -1; src,
} pool->def->target.path,
NULL);
else if (glusterfs)
cmd = virCommandNewArgList( MOUNT,
"-t",
(pool->def->type == VIR_STORAGE_POOL_FS ?
virStoragePoolFormatFileSystemTypeToString(pool->def->source.format) :
virStoragePoolFormatFileSystemNetTypeToString(pool->def->source.format)),
src,
"-o",
"direct-io-mode=1",
pool->def->target.path,
NULL);
else
cmd = virCommandNewArgList(MOUNT,
"-t",
(pool->def->type == VIR_STORAGE_POOL_FS ?
virStoragePoolFormatFileSystemTypeToString(pool->def->source.format) :
virStoragePoolFormatFileSystemNetTypeToString(pool->def->source.format)),
src,
pool->def->target.path,
NULL);
if (virCommandRun(cmd, NULL) < 0)
goto cleanup;
ret = 0;
cleanup:
virCommandFree(cmd);
VIR_FREE(src); VIR_FREE(src);
return 0; return ret;
} }
/** /**
...@@ -462,8 +447,8 @@ virStorageBackendFileSystemMount(virStoragePoolObjPtr pool) { ...@@ -462,8 +447,8 @@ virStorageBackendFileSystemMount(virStoragePoolObjPtr pool) {
*/ */
static int static int
virStorageBackendFileSystemUnmount(virStoragePoolObjPtr pool) { virStorageBackendFileSystemUnmount(virStoragePoolObjPtr pool) {
const char *mntargv[3]; virCommandPtr cmd = NULL;
int ret; int ret = -1;
if (pool->def->type == VIR_STORAGE_POOL_NETFS) { if (pool->def->type == VIR_STORAGE_POOL_NETFS) {
if (pool->def->source.nhost != 1) { if (pool->def->source.nhost != 1) {
...@@ -497,14 +482,17 @@ virStorageBackendFileSystemUnmount(virStoragePoolObjPtr pool) { ...@@ -497,14 +482,17 @@ virStorageBackendFileSystemUnmount(virStoragePoolObjPtr pool) {
return 0; return 0;
} }
mntargv[0] = UMOUNT; cmd = virCommandNewArgList(UMOUNT,
mntargv[1] = pool->def->target.path; pool->def->target.path,
mntargv[2] = NULL; NULL);
if (virRun(mntargv, NULL) < 0) { if (virCommandRun(cmd, NULL) < 0)
return -1; goto cleanup;
}
return 0; ret = 0;
cleanup:
virCommandFree(cmd);
return ret;
} }
#endif /* WITH_STORAGE_FS */ #endif /* WITH_STORAGE_FS */
......
...@@ -151,31 +151,32 @@ virStorageBackendISCSISession(virStoragePoolObjPtr pool, ...@@ -151,31 +151,32 @@ virStorageBackendISCSISession(virStoragePoolObjPtr pool,
int vars[] = { int vars[] = {
2, 2,
}; };
const char *const prog[] = {
ISCSIADM, "--mode", "session", NULL
};
char *session = NULL; char *session = NULL;
virCommandPtr cmd = virCommandNewArgList(ISCSIADM, "--mode", "session", NULL);
/* Note that we ignore the exitstatus. Older versions of iscsiadm tools /* Note that we ignore the exitstatus. Older versions of iscsiadm tools
* returned an exit status of > 0, even if they succeeded. We will just * returned an exit status of > 0, even if they succeeded. We will just
* rely on whether session got filled in properly. * rely on whether session got filled in properly.
*/ */
if (virStorageBackendRunProgRegex(pool, if (virStorageBackendRunProgRegex(pool,
prog, cmd,
1, 1,
regexes, regexes,
vars, vars,
virStorageBackendISCSIExtractSession, virStorageBackendISCSIExtractSession,
&session, NULL) < 0) &session, NULL) < 0)
return NULL; goto cleanup;
if (session == NULL && if (session == NULL &&
!probe) { !probe) {
virStorageReportError(VIR_ERR_INTERNAL_ERROR, virStorageReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot find session")); "%s", _("cannot find session"));
return NULL; goto cleanup;
} }
cleanup:
virCommandFree(cmd);
return session; return session;
} }
...@@ -279,42 +280,52 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn, ...@@ -279,42 +280,52 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
char **ifacename) char **ifacename)
{ {
int ret = -1, exitstatus = -1; int ret = -1, exitstatus = -1;
char temp_ifacename[32]; char *temp_ifacename;
const char *const cmdargv1[] = { virCommandPtr cmd = NULL;
ISCSIADM, "--mode", "iface", "--interface",
temp_ifacename, "--op", "new", NULL
};
const char *const cmdargv2[] = {
ISCSIADM, "--mode", "iface", "--interface", temp_ifacename,
"--op", "update", "--name", "iface.initiatorname", "--value",
initiatoriqn, NULL
};
snprintf(temp_ifacename, sizeof(temp_ifacename), "libvirt-iface-%08llx", if (virAsprintf(&temp_ifacename,
(unsigned long long)virRandomBits(30)); "libvirt-iface-%08llx",
(unsigned long long)virRandomBits(30)) < 0) {
virReportOOMError();
return -1;
}
VIR_DEBUG("Attempting to create interface '%s' with IQN '%s'", VIR_DEBUG("Attempting to create interface '%s' with IQN '%s'",
&temp_ifacename[0], initiatoriqn); temp_ifacename, initiatoriqn);
cmd = virCommandNewArgList(ISCSIADM,
"--mode", "iface",
"--interface", temp_ifacename,
"--op", "new",
NULL);
/* Note that we ignore the exitstatus. Older versions of iscsiadm /* Note that we ignore the exitstatus. Older versions of iscsiadm
* tools returned an exit status of > 0, even if they succeeded. * tools returned an exit status of > 0, even if they succeeded.
* We will just rely on whether the interface got created * We will just rely on whether the interface got created
* properly. */ * properly. */
if (virRun(cmdargv1, &exitstatus) < 0) { if (virCommandRun(cmd, &exitstatus) < 0) {
virStorageReportError(VIR_ERR_INTERNAL_ERROR, virStorageReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to run command '%s' to create new iscsi interface"), _("Failed to run command '%s' to create new iscsi interface"),
cmdargv1[0]); ISCSIADM);
goto out; goto cleanup;
} }
virCommandFree(cmd);
cmd = virCommandNewArgList(ISCSIADM,
"--mode", "iface",
"--interface", temp_ifacename,
"--op", "update",
"--name", "iface.initiatorname",
"--value",
initiatoriqn,
NULL);
/* Note that we ignore the exitstatus. Older versions of iscsiadm tools /* Note that we ignore the exitstatus. Older versions of iscsiadm tools
* returned an exit status of > 0, even if they succeeded. We will just * returned an exit status of > 0, even if they succeeded. We will just
* rely on whether iface file got updated properly. */ * rely on whether iface file got updated properly. */
if (virRun(cmdargv2, &exitstatus) < 0) { if (virCommandRun(cmd, &exitstatus) < 0) {
virStorageReportError(VIR_ERR_INTERNAL_ERROR, virStorageReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to run command '%s' to update iscsi interface with IQN '%s'"), _("Failed to run command '%s' to update iscsi interface with IQN '%s'"),
cmdargv2[0], initiatoriqn); ISCSIADM, initiatoriqn);
goto out; goto cleanup;
} }
/* Check again to make sure the interface was created. */ /* Check again to make sure the interface was created. */
...@@ -322,7 +333,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn, ...@@ -322,7 +333,7 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
VIR_DEBUG("Failed to find interface '%s' with IQN '%s' " VIR_DEBUG("Failed to find interface '%s' with IQN '%s' "
"after attempting to create it", "after attempting to create it",
&temp_ifacename[0], initiatoriqn); &temp_ifacename[0], initiatoriqn);
goto out; goto cleanup;
} else { } else {
VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully", VIR_DEBUG("Interface '%s' with IQN '%s' was created successfully",
*ifacename, initiatoriqn); *ifacename, initiatoriqn);
...@@ -330,7 +341,9 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn, ...@@ -330,7 +341,9 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
ret = 0; ret = 0;
out: cleanup:
virCommandFree(cmd);
VIR_FREE(temp_ifacename);
if (ret != 0) if (ret != 0)
VIR_FREE(*ifacename); VIR_FREE(*ifacename);
return ret; return ret;
...@@ -426,14 +439,14 @@ static int ...@@ -426,14 +439,14 @@ static int
virStorageBackendISCSIRescanLUNs(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED, virStorageBackendISCSIRescanLUNs(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
const char *session) const char *session)
{ {
const char *const cmdargv[] = { virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
ISCSIADM, "--mode", "session", "-r", session, "-R", NULL, "--mode", "session",
}; "-r", session,
"-R",
if (virRun(cmdargv, NULL) < 0) NULL);
return -1; int ret = virCommandRun(cmd, NULL);
virCommandFree(cmd);
return 0; return ret;
} }
struct virStorageBackendISCSITargetList { struct virStorageBackendISCSITargetList {
...@@ -501,24 +514,25 @@ virStorageBackendISCSIScanTargets(const char *portal, ...@@ -501,24 +514,25 @@ virStorageBackendISCSIScanTargets(const char *portal,
"^\\s*(\\S+)\\s+(\\S+)\\s*$" "^\\s*(\\S+)\\s+(\\S+)\\s*$"
}; };
int vars[] = { 2 }; int vars[] = { 2 };
const char *const cmdsendtarget[] = {
ISCSIADM, "--mode", "discovery", "--type", "sendtargets",
"--portal", portal, NULL
};
struct virStorageBackendISCSITargetList list; struct virStorageBackendISCSITargetList list;
int i; size_t i;
int ret = -1;
virCommandPtr cmd = virCommandNewArgList(ISCSIADM,
"--mode", "discovery",
"--type", "sendtargets",
"--portal", portal,
NULL);
memset(&list, 0, sizeof(list)); memset(&list, 0, sizeof(list));
if (virStorageBackendRunProgRegex(NULL, /* No pool for callback */ if (virStorageBackendRunProgRegex(NULL, /* No pool for callback */
cmdsendtarget, cmd,
1, 1,
regexes, regexes,
vars, vars,
virStorageBackendISCSIGetTargets, virStorageBackendISCSIGetTargets,
&list, NULL) < 0) { &list, NULL) < 0)
return -1; goto cleanup;
}
for (i = 0 ; i < list.ntargets ; i++) { for (i = 0 ; i < list.ntargets ; i++) {
/* We have to ignore failure, because we can't undo /* We have to ignore failure, because we can't undo
...@@ -542,7 +556,10 @@ virStorageBackendISCSIScanTargets(const char *portal, ...@@ -542,7 +556,10 @@ virStorageBackendISCSIScanTargets(const char *portal,
VIR_FREE(list.targets); VIR_FREE(list.targets);
} }
return 0; ret = 0;
cleanup:
virCommandFree(cmd);
return ret;
} }
......
...@@ -48,17 +48,16 @@ static int ...@@ -48,17 +48,16 @@ static int
virStorageBackendLogicalSetActive(virStoragePoolObjPtr pool, virStorageBackendLogicalSetActive(virStoragePoolObjPtr pool,
int on) int on)
{ {
const char *cmdargv[4]; int ret;
virCommandPtr cmd =
cmdargv[0] = VGCHANGE; virCommandNewArgList(VGCHANGE,
cmdargv[1] = on ? "-aly" : "-aln"; on ? "-aly" : "-aln",
cmdargv[2] = pool->def->source.name; pool->def->source.name,
cmdargv[3] = NULL; NULL);
if (virRun(cmdargv, NULL) < 0) ret = virCommandRun(cmd, NULL);
return -1; virCommandFree(cmd);
return ret;
return 0;
} }
...@@ -296,24 +295,31 @@ virStorageBackendLogicalFindLVs(virStoragePoolObjPtr pool, ...@@ -296,24 +295,31 @@ virStorageBackendLogicalFindLVs(virStoragePoolObjPtr pool,
int vars[] = { int vars[] = {
8 8
}; };
const char *prog[] = { int ret = -1;
LVS, "--separator", "#", "--noheadings", "--units", "b", virCommandPtr cmd;
"--unbuffered", "--nosuffix", "--options",
"lv_name,origin,uuid,devices,segtype,stripes,seg_size,vg_extent_size", cmd = virCommandNewArgList(LVS,
pool->def->source.name, NULL "--separator", "#",
}; "--noheadings",
"--units", "b",
"--unbuffered",
"--nosuffix",
"--options", "lv_name,origin,uuid,devices,segtype,stripes,seg_size,vg_extent_size",
pool->def->source.name,
NULL);
if (virStorageBackendRunProgRegex(pool, if (virStorageBackendRunProgRegex(pool,
prog, cmd,
1, 1,
regexes, regexes,
vars, vars,
virStorageBackendLogicalMakeVol, virStorageBackendLogicalMakeVol,
vol, "lvs") < 0) { vol, "lvs") < 0)
return -1; goto cleanup;
}
return 0; ret = 0;
cleanup:
virCommandFree(cmd);
return ret;
} }
static int static int
...@@ -405,8 +411,7 @@ virStorageBackendLogicalFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -405,8 +411,7 @@ virStorageBackendLogicalFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
int vars[] = { int vars[] = {
2 2
}; };
const char *const prog[] = { PVS, "--noheadings", "-o", "pv_name,vg_name", NULL }; virCommandPtr cmd;
const char *const scanprog[] = { VGSCAN, NULL };
char *retval = NULL; char *retval = NULL;
virStoragePoolSourceList sourceList; virStoragePoolSourceList sourceList;
int i; int i;
...@@ -418,17 +423,25 @@ virStorageBackendLogicalFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -418,17 +423,25 @@ virStorageBackendLogicalFindPoolSources(virConnectPtr conn ATTRIBUTE_UNUSED,
* that might be hanging around, so if this fails for some reason, the * that might be hanging around, so if this fails for some reason, the
* worst that happens is that scanning doesn't pick everything up * worst that happens is that scanning doesn't pick everything up
*/ */
if (virRun(scanprog, NULL) < 0) { cmd = virCommandNew(VGSCAN);
if (virCommandRun(cmd, NULL) < 0)
VIR_WARN("Failure when running vgscan to refresh physical volumes"); VIR_WARN("Failure when running vgscan to refresh physical volumes");
} virCommandFree(cmd);
memset(&sourceList, 0, sizeof(sourceList)); memset(&sourceList, 0, sizeof(sourceList));
sourceList.type = VIR_STORAGE_POOL_LOGICAL; sourceList.type = VIR_STORAGE_POOL_LOGICAL;
if (virStorageBackendRunProgRegex(NULL, prog, 1, regexes, vars, cmd = virCommandNewArgList(PVS,
"--noheadings",
"-o", "pv_name,vg_name",
NULL);
if (virStorageBackendRunProgRegex(NULL, cmd, 1, regexes, vars,
virStorageBackendLogicalFindPoolSourcesFunc, virStorageBackendLogicalFindPoolSourcesFunc,
&sourceList, "pvs") < 0) &sourceList, "pvs") < 0) {
virCommandFree(cmd);
return NULL; return NULL;
}
virCommandFree(cmd);
retval = virStoragePoolSourceListFormat(&sourceList); retval = virStoragePoolSourceListFormat(&sourceList);
if (retval == NULL) { if (retval == NULL) {
...@@ -483,26 +496,20 @@ virStorageBackendLogicalBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -483,26 +496,20 @@ virStorageBackendLogicalBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED,
virStoragePoolObjPtr pool, virStoragePoolObjPtr pool,
unsigned int flags) unsigned int flags)
{ {
const char **vgargv; virCommandPtr vgcmd;
const char *pvargv[3]; int fd;
int n = 0, i, fd;
char zeros[PV_BLANK_SECTOR_SIZE]; char zeros[PV_BLANK_SECTOR_SIZE];
int ret = -1;
size_t i;
virCheckFlags(0, -1); virCheckFlags(0, -1);
memset(zeros, 0, sizeof(zeros)); memset(zeros, 0, sizeof(zeros));
if (VIR_ALLOC_N(vgargv, 3 + pool->def->source.ndevice) < 0) { vgcmd = virCommandNewArgList(VGCREATE, pool->def->source.name, NULL);
virReportOOMError();
return -1;
}
vgargv[n++] = VGCREATE;
vgargv[n++] = pool->def->source.name;
pvargv[0] = PVCREATE;
pvargv[2] = NULL;
for (i = 0 ; i < pool->def->source.ndevice ; i++) { for (i = 0 ; i < pool->def->source.ndevice ; i++) {
virCommandPtr pvcmd;
/* /*
* LVM requires that the first sector is blanked if using * LVM requires that the first sector is blanked if using
* a whole disk as a PV. So we just blank them out regardless * a whole disk as a PV. So we just blank them out regardless
...@@ -539,25 +546,27 @@ virStorageBackendLogicalBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -539,25 +546,27 @@ virStorageBackendLogicalBuildPool(virConnectPtr conn ATTRIBUTE_UNUSED,
* Initialize the physical volume because vgcreate is not * Initialize the physical volume because vgcreate is not
* clever enough todo this for us :-( * clever enough todo this for us :-(
*/ */
vgargv[n++] = pool->def->source.devices[i].path; pvcmd = virCommandNewArgList(PVCREATE,
pvargv[1] = pool->def->source.devices[i].path; pool->def->source.devices[i].path,
if (virRun(pvargv, NULL) < 0) NULL);
if (virCommandRun(pvcmd, NULL) < 0) {
virCommandFree(pvcmd);
goto cleanup; goto cleanup;
} }
virCommandFree(pvcmd);
vgargv[n] = NULL; virCommandAddArg(vgcmd, pool->def->source.devices[i].path);
}
/* Now create the volume group itself */ /* Now create the volume group itself */
if (virRun(vgargv, NULL) < 0) if (virCommandRun(vgcmd, NULL) < 0)
goto cleanup; goto cleanup;
VIR_FREE(vgargv); ret = 0;
return 0;
cleanup: cleanup:
VIR_FREE(vgargv); virCommandFree(vgcmd);
return -1; return ret;
} }
...@@ -579,33 +588,42 @@ virStorageBackendLogicalRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -579,33 +588,42 @@ virStorageBackendLogicalRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
int vars[] = { int vars[] = {
2 2
}; };
const char *prog[] = { virCommandPtr cmd = NULL;
VGS, "--separator", ":", "--noheadings", "--units", "b", "--unbuffered", int ret = -1;
"--nosuffix", "--options", "vg_size,vg_free",
pool->def->source.name, NULL
};
virFileWaitForDevices(); virFileWaitForDevices();
/* Get list of all logical volumes */ /* Get list of all logical volumes */
if (virStorageBackendLogicalFindLVs(pool, NULL) < 0) { if (virStorageBackendLogicalFindLVs(pool, NULL) < 0)
virStoragePoolObjClearVols(pool); goto cleanup;
return -1;
} cmd = virCommandNewArgList(VGS,
"--separator", ":",
"--noheadings",
"--units", "b",
"--unbuffered",
"--nosuffix",
"--options", "vg_size,vg_free",
pool->def->source.name,
NULL);
/* Now get basic volgrp metadata */ /* Now get basic volgrp metadata */
if (virStorageBackendRunProgRegex(pool, if (virStorageBackendRunProgRegex(pool,
prog, cmd,
1, 1,
regexes, regexes,
vars, vars,
virStorageBackendLogicalRefreshPoolFunc, virStorageBackendLogicalRefreshPoolFunc,
NULL, "vgs") < 0) { NULL, "vgs") < 0)
virStoragePoolObjClearVols(pool); goto cleanup;
return -1;
}
return 0; ret = 0;
cleanup:
virCommandFree(cmd);
if (ret < 0)
virStoragePoolObjClearVols(pool);
return ret;
} }
/* /*
...@@ -628,31 +646,37 @@ virStorageBackendLogicalDeletePool(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -628,31 +646,37 @@ virStorageBackendLogicalDeletePool(virConnectPtr conn ATTRIBUTE_UNUSED,
virStoragePoolObjPtr pool, virStoragePoolObjPtr pool,
unsigned int flags) unsigned int flags)
{ {
const char *cmdargv[] = { virCommandPtr cmd = NULL;
VGREMOVE, "-f", pool->def->source.name, NULL size_t i;
}; int ret = -1;
const char *pvargv[3];
int i, error;
virCheckFlags(0, -1); virCheckFlags(0, -1);
/* first remove the volume group */ /* first remove the volume group */
if (virRun(cmdargv, NULL) < 0) cmd = virCommandNewArgList(VGREMOVE,
return -1; "-f", pool->def->source.name,
NULL);
if (virCommandRun(cmd, NULL) < 0)
goto cleanup;
virCommandFree(cmd);
/* now remove the pv devices and clear them out */ /* now remove the pv devices and clear them out */
error = 0; ret = 0;
pvargv[0] = PVREMOVE;
pvargv[2] = NULL;
for (i = 0 ; i < pool->def->source.ndevice ; i++) { for (i = 0 ; i < pool->def->source.ndevice ; i++) {
pvargv[1] = pool->def->source.devices[i].path; cmd = virCommandNewArgList(PVREMOVE,
if (virRun(pvargv, NULL) < 0) { pool->def->source.devices[i].path,
error = -1; NULL);
if (virCommandRun(cmd, NULL) < 0) {
ret = -1;
break; break;
} }
virCommandFree(cmd);
cmd = NULL;
} }
return error; cleanup:
virCommandFree(cmd);
return ret;
} }
...@@ -669,16 +693,7 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn, ...@@ -669,16 +693,7 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
virStorageVolDefPtr vol) virStorageVolDefPtr vol)
{ {
int fdret, fd = -1; int fdret, fd = -1;
char size[100]; virCommandPtr cmd = NULL;
const char *cmdargvnew[] = {
LVCREATE, "--name", vol->name, "-L", size,
pool->def->source.name, NULL
};
const char *cmdargvsnap[] = {
LVCREATE, "--name", vol->name, "-L", size,
"-s", vol->backingStore.path, NULL
};
const char **cmdargv = cmdargvnew;
if (vol->target.encryption != NULL) { if (vol->target.encryption != NULL) {
virStorageReportError(VIR_ERR_CONFIG_UNSUPPORTED, virStorageReportError(VIR_ERR_CONFIG_UNSUPPORTED,
...@@ -687,15 +702,6 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn, ...@@ -687,15 +702,6 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
return -1; return -1;
} }
if (vol->backingStore.path) {
cmdargv = cmdargvsnap;
}
snprintf(size, sizeof(size)-1, "%lluK", VIR_DIV_UP(vol->capacity, 1024));
size[sizeof(size)-1] = '\0';
vol->type = VIR_STORAGE_VOL_BLOCK;
if (vol->target.path != NULL) { if (vol->target.path != NULL) {
/* A target path passed to CreateVol has no meaning */ /* A target path passed to CreateVol has no meaning */
VIR_FREE(vol->target.path); VIR_FREE(vol->target.path);
...@@ -708,8 +714,18 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn, ...@@ -708,8 +714,18 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
return -1; return -1;
} }
if (virRun(cmdargv, NULL) < 0) cmd = virCommandNewArgList(LVCREATE,
return -1; "--name", vol->name,
NULL);
virCommandAddArg(cmd, "-L");
virCommandAddArgFormat(cmd, "%lluK", VIR_DIV_UP(vol->capacity, 1024));
if (vol->backingStore.path)
virCommandAddArgPair(cmd, "-s", vol->backingStore.path);
else
virCommandAddArg(cmd, pool->def->source.name);
if (virCommandRun(cmd, NULL) < 0)
goto cleanup;
if ((fdret = virStorageBackendVolOpen(vol->target.path)) < 0) if ((fdret = virStorageBackendVolOpen(vol->target.path)) < 0)
goto cleanup; goto cleanup;
...@@ -752,6 +768,7 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn, ...@@ -752,6 +768,7 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
cleanup: cleanup:
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
virStorageBackendLogicalDeleteVol(conn, pool, vol, 0); virStorageBackendLogicalDeleteVol(conn, pool, vol, 0);
virCommandFree(cmd);
return -1; return -1;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册