2008-02-20 15:34:52 +00:00
|
|
|
/*
|
|
|
|
* storage_driver.c: core driver for storage APIs
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2008 Red Hat, Inc.
|
|
|
|
* Copyright (C) 2006-2008 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
2008-04-18 08:33:23 +00:00
|
|
|
#if HAVE_PWD_H
|
2008-02-20 15:34:52 +00:00
|
|
|
#include <pwd.h>
|
2008-04-18 08:33:23 +00:00
|
|
|
#endif
|
2008-02-20 15:34:52 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-11-04 22:30:33 +00:00
|
|
|
#include "virterror_internal.h"
|
2008-11-04 23:22:06 +00:00
|
|
|
#include "datatypes.h"
|
2008-02-20 15:34:52 +00:00
|
|
|
#include "driver.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "storage_driver.h"
|
|
|
|
#include "storage_conf.h"
|
2008-06-06 11:09:57 +00:00
|
|
|
#include "memory.h"
|
2008-02-20 15:34:52 +00:00
|
|
|
#include "storage_backend.h"
|
|
|
|
|
|
|
|
#define storageLog(msg...) fprintf(stderr, msg)
|
|
|
|
|
|
|
|
static virStorageDriverStatePtr driverState;
|
|
|
|
|
|
|
|
static int storageDriverShutdown(void);
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
storageDriverAutostart(virStorageDriverStatePtr driver) {
|
2008-10-10 15:13:28 +00:00
|
|
|
unsigned int i;
|
2008-02-20 15:34:52 +00:00
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
for (i = 0 ; i < driver->pools.count ; i++) {
|
|
|
|
virStoragePoolObjPtr pool = driver->pools.objs[i];
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (pool->autostart &&
|
|
|
|
!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageBackendPtr backend;
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
|
|
|
|
storageLog("Missing backend %d",
|
|
|
|
pool->def->type);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backend->startPool &&
|
|
|
|
backend->startPool(NULL, pool) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
storageLog("Failed to autostart storage pool '%s': %s",
|
|
|
|
pool->def->name, err ? err->message : NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backend->refreshPool(NULL, pool) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
if (backend->stopPool)
|
|
|
|
backend->stopPool(NULL, pool);
|
|
|
|
storageLog("Failed to autostart storage pool '%s': %s",
|
|
|
|
pool->def->name, err ? err->message : NULL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pool->active = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virStorageStartup:
|
|
|
|
*
|
|
|
|
* Initialization function for the QEmu daemon
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
storageDriverStartup(void) {
|
|
|
|
uid_t uid = geteuid();
|
|
|
|
struct passwd *pw;
|
|
|
|
char *base = NULL;
|
|
|
|
char driverConf[PATH_MAX];
|
|
|
|
|
2008-06-06 11:09:57 +00:00
|
|
|
if (VIR_ALLOC(driverState) < 0)
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!uid) {
|
|
|
|
if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
|
|
|
|
goto out_of_memory;
|
|
|
|
} else {
|
|
|
|
if (!(pw = getpwuid(uid))) {
|
|
|
|
storageLog("Failed to find user record for uid '%d': %s",
|
|
|
|
uid, strerror(errno));
|
|
|
|
goto out_of_memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asprintf (&base, "%s/.libvirt", pw->pw_dir) == -1) {
|
|
|
|
storageLog("out of memory in asprintf");
|
|
|
|
goto out_of_memory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configuration paths are either ~/.libvirt/storage/... (session) or
|
|
|
|
* /etc/libvirt/storage/... (system).
|
|
|
|
*/
|
|
|
|
if (snprintf (driverConf, sizeof(driverConf),
|
|
|
|
"%s/storage.conf", base) == -1)
|
|
|
|
goto out_of_memory;
|
|
|
|
driverConf[sizeof(driverConf)-1] = '\0';
|
|
|
|
|
|
|
|
if (asprintf (&driverState->configDir,
|
|
|
|
"%s/storage", base) == -1)
|
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
if (asprintf (&driverState->autostartDir,
|
|
|
|
"%s/storage/autostart", base) == -1)
|
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
free(base);
|
|
|
|
base = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (virStorageLoadDriverConfig(driver, driverConf) < 0) {
|
|
|
|
virStorageDriverShutdown();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
if (virStoragePoolLoadAllConfigs(NULL,
|
|
|
|
&driverState->pools,
|
|
|
|
driverState->configDir,
|
|
|
|
driverState->autostartDir) < 0) {
|
2008-02-20 15:34:52 +00:00
|
|
|
storageDriverShutdown();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
storageDriverAutostart(driverState);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_of_memory:
|
|
|
|
storageLog("virStorageStartup: out of memory");
|
|
|
|
free(base);
|
|
|
|
free(driverState);
|
|
|
|
driverState = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virStorageReload:
|
|
|
|
*
|
|
|
|
* Function to restart the storage driver, it will recheck the configuration
|
|
|
|
* files and update its state
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
storageDriverReload(void) {
|
2008-10-10 15:13:28 +00:00
|
|
|
if (!driverState)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
virStoragePoolLoadAllConfigs(NULL,
|
|
|
|
&driverState->pools,
|
|
|
|
driverState->configDir,
|
|
|
|
driverState->autostartDir);
|
2008-02-20 15:34:52 +00:00
|
|
|
storageDriverAutostart(driverState);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virStorageActive:
|
|
|
|
*
|
|
|
|
* Checks if the storage driver is active, i.e. has an active pool
|
|
|
|
*
|
|
|
|
* Returns 1 if active, 0 otherwise
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
storageDriverActive(void) {
|
2008-10-10 15:13:28 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!driverState)
|
|
|
|
return 0;
|
|
|
|
|
2008-02-20 15:34:52 +00:00
|
|
|
/* If we've any active networks or guests, then we
|
|
|
|
* mark this driver as active
|
|
|
|
*/
|
2008-10-10 15:13:28 +00:00
|
|
|
for (i = 0 ; i < driverState->pools.count ; i++)
|
|
|
|
if (virStoragePoolObjIsActive(driverState->pools.objs[i]))
|
|
|
|
return 1;
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
/* Otherwise we're happy to deal with a shutdown */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virStorageShutdown:
|
|
|
|
*
|
|
|
|
* Shutdown the storage driver, it will stop all active storage pools
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
storageDriverShutdown(void) {
|
2008-10-10 15:13:28 +00:00
|
|
|
unsigned int i;
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (!driverState)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* shutdown active pools */
|
2008-10-10 15:13:28 +00:00
|
|
|
for (i = 0 ; i < driverState->pools.count ; i++) {
|
|
|
|
virStoragePoolObjPtr pool = driverState->pools.objs[i];
|
|
|
|
|
2008-02-20 15:34:52 +00:00
|
|
|
if (virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageBackendPtr backend;
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
|
|
|
|
storageLog("Missing backend");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backend->stopPool &&
|
|
|
|
backend->stopPool(NULL, pool) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
storageLog("Failed to stop storage pool '%s': %s",
|
2008-10-21 17:15:53 +00:00
|
|
|
pool->def->name, err ? err->message : NULL);
|
2008-02-20 15:34:52 +00:00
|
|
|
}
|
|
|
|
virStoragePoolObjClearVols(pool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free inactive pools */
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjListFree(&driverState->pools);
|
|
|
|
|
|
|
|
VIR_FREE(driverState->configDir);
|
|
|
|
VIR_FREE(driverState->autostartDir);
|
|
|
|
VIR_FREE(driverState);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static virStoragePoolPtr
|
|
|
|
storagePoolLookupByUUID(virConnectPtr conn,
|
|
|
|
const unsigned char *uuid) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStoragePoolPtr ret;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(conn, VIR_ERR_NO_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virStoragePoolPtr
|
|
|
|
storagePoolLookupByName(virConnectPtr conn,
|
|
|
|
const char *name) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, name);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStoragePoolPtr ret;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(conn, VIR_ERR_NO_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no pool with matching name"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virStoragePoolPtr
|
|
|
|
storagePoolLookupByVolume(virStorageVolPtr vol) {
|
|
|
|
return storagePoolLookupByName(vol->conn, vol->pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
static virDrvOpenStatus
|
|
|
|
storageOpen(virConnectPtr conn,
|
|
|
|
xmlURIPtr uri ATTRIBUTE_UNUSED,
|
|
|
|
virConnectAuthPtr auth ATTRIBUTE_UNUSED,
|
|
|
|
int flags ATTRIBUTE_UNUSED) {
|
|
|
|
if (!driverState)
|
|
|
|
return VIR_DRV_OPEN_DECLINED;
|
|
|
|
|
|
|
|
conn->storagePrivateData = driverState;
|
|
|
|
return VIR_DRV_OPEN_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storageClose(virConnectPtr conn) {
|
|
|
|
conn->storagePrivateData = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storageNumPools(virConnectPtr conn) {
|
|
|
|
virStorageDriverStatePtr driver
|
|
|
|
= (virStorageDriverStatePtr)conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
unsigned int i, nactive = 0;
|
|
|
|
|
|
|
|
for (i = 0 ; i < driver->pools.count ; i++)
|
|
|
|
if (virStoragePoolObjIsActive(driver->pools.objs[i]))
|
|
|
|
nactive++;
|
|
|
|
|
|
|
|
return nactive;
|
2008-02-20 15:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storageListPools(virConnectPtr conn,
|
|
|
|
char **const names,
|
|
|
|
int nnames) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)conn->storagePrivateData;
|
|
|
|
int got = 0, i;
|
2008-10-10 15:13:28 +00:00
|
|
|
|
|
|
|
for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
|
|
|
|
if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
|
|
|
|
if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("names"));
|
2008-02-20 15:34:52 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
got++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return got;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
for (i = 0 ; i < got ; i++) {
|
|
|
|
free(names[i]);
|
|
|
|
names[i] = NULL;
|
|
|
|
}
|
|
|
|
memset(names, 0, nnames);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storageNumDefinedPools(virConnectPtr conn) {
|
|
|
|
virStorageDriverStatePtr driver
|
|
|
|
= (virStorageDriverStatePtr)conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
unsigned int i, nactive = 0;
|
|
|
|
|
|
|
|
for (i = 0 ; i < driver->pools.count ; i++)
|
|
|
|
if (!virStoragePoolObjIsActive(driver->pools.objs[i]))
|
|
|
|
nactive++;
|
|
|
|
|
|
|
|
return nactive;
|
2008-02-20 15:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storageListDefinedPools(virConnectPtr conn,
|
|
|
|
char **const names,
|
|
|
|
int nnames) {
|
|
|
|
virStorageDriverStatePtr driver
|
|
|
|
= (virStorageDriverStatePtr)conn->storagePrivateData;
|
|
|
|
int got = 0, i;
|
2008-10-10 15:13:28 +00:00
|
|
|
|
|
|
|
for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
|
|
|
|
if (!virStoragePoolObjIsActive(driver->pools.objs[i])) {
|
|
|
|
if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) {
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("names"));
|
2008-02-20 15:34:52 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
got++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return got;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
for (i = 0 ; i < got ; i++) {
|
|
|
|
free(names[i]);
|
|
|
|
names[i] = NULL;
|
|
|
|
}
|
|
|
|
memset(names, 0, nnames);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-27 20:05:58 +00:00
|
|
|
static char *
|
|
|
|
storageFindPoolSources(virConnectPtr conn,
|
|
|
|
const char *type,
|
|
|
|
const char *srcSpec,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
int backend_type;
|
|
|
|
virStorageBackendPtr backend;
|
|
|
|
|
|
|
|
backend_type = virStorageBackendFromString(type);
|
|
|
|
if (backend_type < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
backend = virStorageBackendForType(backend_type);
|
|
|
|
if (backend == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (backend->findPoolSources)
|
|
|
|
return backend->findPoolSources(conn, srcSpec, flags);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 15:34:52 +00:00
|
|
|
static virStoragePoolPtr
|
|
|
|
storagePoolCreate(virConnectPtr conn,
|
|
|
|
const char *xml,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr )conn->storagePrivateData;
|
|
|
|
virStoragePoolDefPtr def;
|
|
|
|
virStoragePoolObjPtr pool;
|
|
|
|
virStoragePoolPtr ret;
|
|
|
|
virStorageBackendPtr backend;
|
|
|
|
|
|
|
|
if (!(def = virStoragePoolDefParse(conn, xml, NULL)))
|
|
|
|
return NULL;
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
if (virStoragePoolObjFindByUUID(&driver->pools, def->uuid) ||
|
|
|
|
virStoragePoolObjFindByName(&driver->pools, def->name)) {
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool already exists"));
|
2008-02-20 15:34:52 +00:00
|
|
|
virStoragePoolDefFree(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(def->type)) == NULL) {
|
|
|
|
virStoragePoolDefFree(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
if (!(pool = virStoragePoolObjAssignDef(conn, &driver->pools, def))) {
|
2008-02-20 15:34:52 +00:00
|
|
|
virStoragePoolDefFree(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-17 15:10:48 +00:00
|
|
|
if (backend->startPool &&
|
|
|
|
backend->startPool(conn, pool) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (backend->refreshPool(conn, pool) < 0) {
|
|
|
|
if (backend->stopPool)
|
|
|
|
backend->stopPool(conn, pool);
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pool->active = 1;
|
|
|
|
|
|
|
|
ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virStoragePoolPtr
|
|
|
|
storagePoolDefine(virConnectPtr conn,
|
|
|
|
const char *xml,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
|
|
|
virStorageDriverStatePtr driver
|
|
|
|
= (virStorageDriverStatePtr )conn->storagePrivateData;
|
|
|
|
virStoragePoolDefPtr def;
|
|
|
|
virStoragePoolObjPtr pool;
|
|
|
|
virStoragePoolPtr ret;
|
|
|
|
virStorageBackendPtr backend;
|
|
|
|
|
|
|
|
if (!(def = virStoragePoolDefParse(conn, xml, NULL)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(def->type)) == NULL) {
|
|
|
|
virStoragePoolDefFree(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
if (!(pool = virStoragePoolObjAssignDef(conn, &driver->pools, def))) {
|
2008-02-20 15:34:52 +00:00
|
|
|
virStoragePoolDefFree(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStoragePoolObjSaveDef(conn, driver, pool, def) < 0) {
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjRemove(&driver->pools, pool);
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = virGetStoragePool(conn, pool->def->name, pool->def->uuid);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolUndefine(virStoragePoolPtr obj) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("pool is still active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStoragePoolObjDeleteDef(obj->conn, pool) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (unlink(pool->autostartLink) < 0 && errno != ENOENT && errno != ENOTDIR)
|
|
|
|
storageLog("Failed to delete autostart link '%s': %s",
|
|
|
|
pool->autostartLink, strerror(errno));
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
VIR_FREE(pool->configFile);
|
|
|
|
VIR_FREE(pool->autostartLink);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjRemove(&driver->pools, pool);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolStart(virStoragePoolPtr obj,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("pool already active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (backend->startPool &&
|
|
|
|
backend->startPool(obj->conn, pool) < 0)
|
|
|
|
return -1;
|
|
|
|
if (backend->refreshPool(obj->conn, pool) < 0) {
|
|
|
|
if (backend->stopPool)
|
|
|
|
backend->stopPool(obj->conn, pool);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pool->active = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolBuild(virStoragePoolPtr obj,
|
|
|
|
unsigned int flags) {
|
|
|
|
virStorageDriverStatePtr driver
|
|
|
|
= (virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is already active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backend->buildPool &&
|
|
|
|
backend->buildPool(obj->conn, pool, flags) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolDestroy(virStoragePoolPtr obj) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backend->stopPool &&
|
|
|
|
backend->stopPool(obj->conn, pool) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
virStoragePoolObjClearVols(pool);
|
|
|
|
|
|
|
|
pool->active = 0;
|
|
|
|
|
|
|
|
if (pool->configFile == NULL)
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjRemove(&driver->pools, pool);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolDelete(virStoragePoolPtr obj,
|
|
|
|
unsigned int flags) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is still active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backend->deletePool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_NO_SUPPORT,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("pool does not support volume delete"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (backend->deletePool(obj->conn, pool, flags) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolRefresh(virStoragePoolPtr obj,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virStoragePoolObjClearVols(pool);
|
|
|
|
if ((ret = backend->refreshPool(obj->conn, pool)) < 0) {
|
|
|
|
if (backend->stopPool)
|
|
|
|
backend->stopPool(obj->conn, pool);
|
|
|
|
|
|
|
|
pool->active = 0;
|
|
|
|
|
|
|
|
if (pool->configFile == NULL)
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjRemove(&driver->pools, pool);
|
2008-02-20 15:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolGetInfo(virStoragePoolPtr obj,
|
|
|
|
virStoragePoolInfoPtr info) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(info, 0, sizeof(virStoragePoolInfo));
|
|
|
|
if (pool->active)
|
|
|
|
info->state = VIR_STORAGE_POOL_RUNNING;
|
|
|
|
else
|
|
|
|
info->state = VIR_STORAGE_POOL_INACTIVE;
|
|
|
|
info->capacity = pool->def->capacity;
|
|
|
|
info->allocation = pool->def->allocation;
|
|
|
|
info->available = pool->def->available;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
storagePoolDumpXML(virStoragePoolPtr obj,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virStoragePoolDefFormat(obj->conn, pool->def);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolGetAutostart(virStoragePoolPtr obj,
|
|
|
|
int *autostart) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pool->configFile) {
|
|
|
|
*autostart = 0;
|
|
|
|
} else {
|
|
|
|
*autostart = pool->autostart;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolSetAutostart(virStoragePoolPtr obj,
|
|
|
|
int autostart) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pool->configFile) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_ARG,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("pool has no config file"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
autostart = (autostart != 0);
|
|
|
|
|
|
|
|
if (pool->autostart == autostart)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (autostart) {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = virFileMakePath(driver->autostartDir))) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot create autostart directory %s: %s"),
|
|
|
|
driver->autostartDir, strerror(err));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symlink(pool->configFile, pool->autostartLink) < 0) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to create symlink '%s' to '%s': %s"),
|
|
|
|
pool->autostartLink, pool->configFile,
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (unlink(pool->autostartLink) < 0 &&
|
|
|
|
errno != ENOENT && errno != ENOTDIR) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to delete symlink '%s': %s"),
|
|
|
|
pool->autostartLink, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pool->autostart = autostart;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolNumVolumes(virStoragePoolPtr obj) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
return pool->volumes.count;
|
2008-02-20 15:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storagePoolListVolumes(virStoragePoolPtr obj,
|
|
|
|
char **const names,
|
|
|
|
int maxnames) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
|
|
|
int i, n = 0;
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(names, 0, maxnames);
|
2008-10-10 15:13:28 +00:00
|
|
|
for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
|
|
|
|
if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) {
|
2008-02-22 16:26:13 +00:00
|
|
|
virStorageReportError(obj->conn, VIR_ERR_NO_MEMORY,
|
|
|
|
"%s", _("name"));
|
2008-02-20 15:34:52 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
return n;
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
cleanup:
|
2008-10-10 15:13:28 +00:00
|
|
|
for (n = 0 ; n < maxnames ; n++)
|
|
|
|
VIR_FREE(names[i]);
|
|
|
|
|
2008-02-20 15:34:52 +00:00
|
|
|
memset(names, 0, maxnames);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virStorageVolPtr
|
|
|
|
storageVolumeLookupByName(virStoragePoolPtr obj,
|
|
|
|
const char *name) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageVolDefPtr vol;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vol = virStorageVolDefFindByName(pool, name);
|
|
|
|
|
|
|
|
if (!vol) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage vol with matching name"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virStorageVolPtr
|
|
|
|
storageVolumeLookupByKey(virConnectPtr conn,
|
|
|
|
const char *key) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
unsigned int i;
|
2008-02-20 15:34:52 +00:00
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
for (i = 0 ; i < driver->pools.count ; i++) {
|
|
|
|
if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
|
|
|
|
virStorageVolDefPtr vol =
|
|
|
|
virStorageVolDefFindByKey(driver->pools.objs[i], key);
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (vol)
|
|
|
|
return virGetStorageVol(conn,
|
2008-10-10 15:13:28 +00:00
|
|
|
driver->pools.objs[i]->def->name,
|
2008-02-20 15:34:52 +00:00
|
|
|
vol->name,
|
|
|
|
vol->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virStorageReportError(conn, VIR_ERR_INVALID_STORAGE_VOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage vol with matching key"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virStorageVolPtr
|
|
|
|
storageVolumeLookupByPath(virConnectPtr conn,
|
|
|
|
const char *path) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
unsigned int i;
|
2008-02-20 15:34:52 +00:00
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
for (i = 0 ; i < driver->pools.count ; i++) {
|
|
|
|
if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
|
2008-11-03 11:37:11 +00:00
|
|
|
virStorageVolDefPtr vol;
|
|
|
|
virStorageBackendPoolOptionsPtr options;
|
|
|
|
|
|
|
|
options = virStorageBackendPoolOptionsForType(driver->pools.objs[i]->def->type);
|
|
|
|
if (options == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (options->flags & VIR_STORAGE_BACKEND_POOL_STABLE_PATH) {
|
|
|
|
const char *stable_path;
|
|
|
|
|
|
|
|
stable_path = virStorageBackendStablePath(conn,
|
|
|
|
driver->pools.objs[i],
|
|
|
|
path);
|
|
|
|
/*
|
|
|
|
* virStorageBackendStablePath already does
|
|
|
|
* virStorageReportError if it fails; we just need to keep
|
|
|
|
* propagating the return code
|
|
|
|
*/
|
|
|
|
if (stable_path == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
vol = virStorageVolDefFindByPath(driver->pools.objs[i],
|
|
|
|
stable_path);
|
|
|
|
VIR_FREE(stable_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
vol = virStorageVolDefFindByPath(driver->pools.objs[i], path);
|
|
|
|
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (vol)
|
|
|
|
return virGetStorageVol(conn,
|
2008-10-10 15:13:28 +00:00
|
|
|
driver->pools.objs[i]->def->name,
|
2008-02-20 15:34:52 +00:00
|
|
|
vol->name,
|
|
|
|
vol->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virStorageReportError(conn, VIR_ERR_INVALID_STORAGE_VOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage vol with matching path"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static virStorageVolPtr
|
|
|
|
storageVolumeCreateXML(virStoragePoolPtr obj,
|
|
|
|
const char *xmldesc,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByUUID(&driver->pools, obj->uuid);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
|
|
|
virStorageVolDefPtr vol;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
vol = virStorageVolDefParse(obj->conn, pool->def, xmldesc, NULL);
|
|
|
|
if (vol == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (virStorageVolDefFindByName(pool, vol->name)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage vol already exists"));
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageVolDefFree(vol);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
if (VIR_REALLOC_N(pool->volumes.objs,
|
|
|
|
pool->volumes.count+1) < 0) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_NO_MEMORY, NULL);
|
|
|
|
virStorageVolDefFree(vol);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-20 15:34:52 +00:00
|
|
|
if (!backend->createVol) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_NO_SUPPORT,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool does not support volume creation"));
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageVolDefFree(vol);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backend->createVol(obj->conn, pool, vol) < 0) {
|
|
|
|
virStorageVolDefFree(vol);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
pool->volumes.objs[pool->volumes.count++] = vol;
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
return virGetStorageVol(obj->conn, pool->def->name, vol->name, vol->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storageVolumeDelete(virStorageVolPtr obj,
|
|
|
|
unsigned int flags) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStorageVolDefPtr vol;
|
|
|
|
unsigned int i;
|
2008-02-20 15:34:52 +00:00
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vol = virStorageVolDefFindByName(pool, obj->name);
|
|
|
|
|
|
|
|
if (!vol) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage vol with matching name"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backend->deleteVol) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_NO_SUPPORT,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool does not support vol deletion"));
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageVolDefFree(vol);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backend->deleteVol(obj->conn, pool, vol, flags) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-10 15:13:28 +00:00
|
|
|
for (i = 0 ; i < pool->volumes.count ; i++) {
|
|
|
|
if (pool->volumes.objs[i] == vol) {
|
|
|
|
virStorageVolDefFree(vol);
|
|
|
|
|
|
|
|
if (i < (pool->volumes.count - 1))
|
|
|
|
memmove(pool->volumes.objs + i, pool->volumes.objs + i + 1,
|
|
|
|
sizeof(*(pool->volumes.objs)) * (pool->volumes.count - (i + 1)));
|
|
|
|
|
|
|
|
if (VIR_REALLOC_N(pool->volumes.objs, pool->volumes.count - 1) < 0) {
|
|
|
|
; /* Failure to reduce memory allocation isn't fatal */
|
|
|
|
}
|
|
|
|
pool->volumes.count--;
|
|
|
|
|
2008-02-20 15:34:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
storageVolumeGetInfo(virStorageVolPtr obj,
|
|
|
|
virStorageVolInfoPtr info) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
|
|
|
virStorageVolDefPtr vol;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vol = virStorageVolDefFindByName(pool, obj->name);
|
|
|
|
|
|
|
|
if (!vol) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage vol with matching name"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (backend->refreshVol &&
|
|
|
|
backend->refreshVol(obj->conn, pool, vol) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(info, 0, sizeof(*info));
|
|
|
|
info->type = backend->volType;
|
|
|
|
info->capacity = vol->capacity;
|
|
|
|
info->allocation = vol->allocation;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
storageVolumeGetXMLDesc(virStorageVolPtr obj,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageBackendPtr backend;
|
|
|
|
virStorageVolDefPtr vol;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vol = virStorageVolDefFindByName(pool, obj->name);
|
|
|
|
|
|
|
|
if (!vol) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage vol with matching name"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((backend = virStorageBackendForType(pool->def->type)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virStorageVolDefFormat(obj->conn, pool->def, vol);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
storageVolumeGetPath(virStorageVolPtr obj) {
|
|
|
|
virStorageDriverStatePtr driver =
|
|
|
|
(virStorageDriverStatePtr)obj->conn->storagePrivateData;
|
2008-10-10 15:13:28 +00:00
|
|
|
virStoragePoolObjPtr pool = virStoragePoolObjFindByName(&driver->pools, obj->pool);
|
2008-02-20 15:34:52 +00:00
|
|
|
virStorageVolDefPtr vol;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage pool with matching uuid"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virStoragePoolObjIsActive(pool)) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INTERNAL_ERROR,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("storage pool is not active"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vol = virStorageVolDefFindByName(pool, obj->name);
|
|
|
|
|
|
|
|
if (!vol) {
|
|
|
|
virStorageReportError(obj->conn, VIR_ERR_INVALID_STORAGE_POOL,
|
2008-02-22 16:26:13 +00:00
|
|
|
"%s", _("no storage vol with matching name"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = strdup(vol->target.path);
|
|
|
|
if (ret == NULL) {
|
2008-02-22 16:26:13 +00:00
|
|
|
virStorageReportError(obj->conn, VIR_ERR_NO_MEMORY, "%s", _("path"));
|
2008-02-20 15:34:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static virStorageDriver storageDriver = {
|
2008-09-22 19:53:42 +00:00
|
|
|
.name = "storage",
|
|
|
|
.open = storageOpen,
|
|
|
|
.close = storageClose,
|
|
|
|
.numOfPools = storageNumPools,
|
|
|
|
.listPools = storageListPools,
|
|
|
|
.numOfDefinedPools = storageNumDefinedPools,
|
|
|
|
.listDefinedPools = storageListDefinedPools,
|
|
|
|
.findPoolSources = storageFindPoolSources,
|
|
|
|
.poolLookupByName = storagePoolLookupByName,
|
|
|
|
.poolLookupByUUID = storagePoolLookupByUUID,
|
|
|
|
.poolLookupByVolume = storagePoolLookupByVolume,
|
|
|
|
.poolCreateXML = storagePoolCreate,
|
|
|
|
.poolDefineXML = storagePoolDefine,
|
|
|
|
.poolBuild = storagePoolBuild,
|
|
|
|
.poolUndefine = storagePoolUndefine,
|
|
|
|
.poolCreate = storagePoolStart,
|
|
|
|
.poolDestroy = storagePoolDestroy,
|
|
|
|
.poolDelete = storagePoolDelete,
|
|
|
|
.poolRefresh = storagePoolRefresh,
|
|
|
|
.poolGetInfo = storagePoolGetInfo,
|
|
|
|
.poolGetXMLDesc = storagePoolDumpXML,
|
|
|
|
.poolGetAutostart = storagePoolGetAutostart,
|
|
|
|
.poolSetAutostart = storagePoolSetAutostart,
|
|
|
|
.poolNumOfVolumes = storagePoolNumVolumes,
|
|
|
|
.poolListVolumes = storagePoolListVolumes,
|
|
|
|
|
|
|
|
.volLookupByName = storageVolumeLookupByName,
|
|
|
|
.volLookupByKey = storageVolumeLookupByKey,
|
|
|
|
.volLookupByPath = storageVolumeLookupByPath,
|
|
|
|
.volCreateXML = storageVolumeCreateXML,
|
|
|
|
.volDelete = storageVolumeDelete,
|
|
|
|
.volGetInfo = storageVolumeGetInfo,
|
|
|
|
.volGetXMLDesc = storageVolumeGetXMLDesc,
|
|
|
|
.volGetPath = storageVolumeGetPath,
|
2008-02-20 15:34:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static virStateDriver stateDriver = {
|
2008-10-06 15:40:37 +00:00
|
|
|
.initialize = storageDriverStartup,
|
|
|
|
.cleanup = storageDriverShutdown,
|
|
|
|
.reload = storageDriverReload,
|
|
|
|
.active = storageDriverActive,
|
2008-02-20 15:34:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int storageRegister(void) {
|
|
|
|
virRegisterStorageDriver(&storageDriver);
|
|
|
|
virRegisterStateDriver(&stateDriver);
|
|
|
|
return 0;
|
|
|
|
}
|