mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-11 23:37:42 +00:00
storage: Refactor FS backend 'create' function choosing.
Break out separate functions for - Determining the supported '*-img' tool, - The tool's associated create function, - Desired function for cloning (CreateXMLFrom). This will be eventually used to unify cloning across all backends.
This commit is contained in:
parent
045176bf35
commit
1b16bf4ec7
@ -55,6 +55,12 @@ enum {
|
|||||||
BACKING_STORE_ERROR,
|
BACKING_STORE_ERROR,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
TOOL_QEMU_IMG,
|
||||||
|
TOOL_KVM_IMG,
|
||||||
|
TOOL_QCOW_CREATE,
|
||||||
|
};
|
||||||
|
|
||||||
static int cowGetBackingStore(virConnectPtr, char **,
|
static int cowGetBackingStore(virConnectPtr, char **,
|
||||||
const unsigned char *, size_t);
|
const unsigned char *, size_t);
|
||||||
static int qcowXGetBackingStore(virConnectPtr, char **,
|
static int qcowXGetBackingStore(virConnectPtr, char **,
|
||||||
@ -1363,6 +1369,77 @@ static int createQemuCreate(virConnectPtr conn,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static createFile
|
||||||
|
toolTypeToFunction(virConnectPtr conn, int tool_type)
|
||||||
|
{
|
||||||
|
switch (tool_type) {
|
||||||
|
case TOOL_KVM_IMG:
|
||||||
|
case TOOL_QEMU_IMG:
|
||||||
|
return createQemuImg;
|
||||||
|
case TOOL_QCOW_CREATE:
|
||||||
|
return createQemuCreate;
|
||||||
|
default:
|
||||||
|
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("Unknown file create tool type '%d'."),
|
||||||
|
tool_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
findImageTool(char **tool)
|
||||||
|
{
|
||||||
|
int tool_type = -1;
|
||||||
|
char *tmp = NULL;
|
||||||
|
|
||||||
|
if ((tmp = virFindFileInPath("kvm-img")) != NULL) {
|
||||||
|
tool_type = TOOL_KVM_IMG;
|
||||||
|
} else if ((tmp = virFindFileInPath("qemu-img")) != NULL) {
|
||||||
|
tool_type = TOOL_QEMU_IMG;
|
||||||
|
} else if ((tmp = virFindFileInPath("qcow-create")) != NULL) {
|
||||||
|
tool_type = TOOL_QCOW_CREATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tool)
|
||||||
|
*tool = tmp;
|
||||||
|
else
|
||||||
|
VIR_FREE(tmp);
|
||||||
|
|
||||||
|
return tool_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
static createFile
|
||||||
|
buildVolFromFunction(virConnectPtr conn,
|
||||||
|
virStorageVolDefPtr vol,
|
||||||
|
virStorageVolDefPtr inputvol)
|
||||||
|
{
|
||||||
|
int tool_type;
|
||||||
|
|
||||||
|
if (!inputvol)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
/* If either volume is a non-raw file vol, we need to use an external
|
||||||
|
* tool for converting
|
||||||
|
*/
|
||||||
|
if ((vol->type == VIR_STORAGE_VOL_FILE &&
|
||||||
|
vol->target.format != VIR_STORAGE_VOL_FILE_RAW) ||
|
||||||
|
(inputvol->type == VIR_STORAGE_VOL_FILE &&
|
||||||
|
inputvol->target.format != VIR_STORAGE_VOL_FILE_RAW)) {
|
||||||
|
|
||||||
|
if ((tool_type = findImageTool(NULL)) != -1) {
|
||||||
|
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
|
||||||
|
"%s", _("creation of non-raw file images is "
|
||||||
|
"not supported without qemu-img."));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return toolTypeToFunction(conn, tool_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return createRaw;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_virStorageBackendFileSystemVolBuild(virConnectPtr conn,
|
_virStorageBackendFileSystemVolBuild(virConnectPtr conn,
|
||||||
virStorageVolDefPtr vol,
|
virStorageVolDefPtr vol,
|
||||||
@ -1370,28 +1447,19 @@ _virStorageBackendFileSystemVolBuild(virConnectPtr conn,
|
|||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
createFile create_func;
|
createFile create_func;
|
||||||
char *create_tool;
|
int tool_type;
|
||||||
|
|
||||||
if (vol->target.format == VIR_STORAGE_VOL_FILE_RAW &&
|
if (inputvol) {
|
||||||
(!inputvol ||
|
create_func = buildVolFromFunction(conn, vol, inputvol);
|
||||||
(inputvol->type == VIR_STORAGE_VOL_BLOCK ||
|
if (!create_func)
|
||||||
inputvol->target.format == VIR_STORAGE_VOL_FILE_RAW))) {
|
return -1;
|
||||||
/* Raw file creation
|
} else if (vol->target.format == VIR_STORAGE_VOL_FILE_RAW) {
|
||||||
* Raw -> Raw copying
|
|
||||||
* Block dev -> Raw copying
|
|
||||||
*/
|
|
||||||
create_func = createRaw;
|
create_func = createRaw;
|
||||||
} else if (vol->target.format == VIR_STORAGE_VOL_FILE_DIR) {
|
} else if (vol->target.format == VIR_STORAGE_VOL_FILE_DIR) {
|
||||||
create_func = createFileDir;
|
create_func = createFileDir;
|
||||||
} else if ((create_tool = virFindFileInPath("kvm-img")) != NULL) {
|
} else if ((tool_type = findImageTool(NULL)) != -1) {
|
||||||
VIR_FREE(create_tool);
|
if ((create_func = toolTypeToFunction(conn, tool_type)) == NULL)
|
||||||
create_func = createQemuImg;
|
return -1;
|
||||||
} else if ((create_tool = virFindFileInPath("qemu-img")) != NULL) {
|
|
||||||
VIR_FREE(create_tool);
|
|
||||||
create_func = createQemuImg;
|
|
||||||
} else if ((create_tool = virFindFileInPath("qcow-create")) != NULL) {
|
|
||||||
VIR_FREE(create_tool);
|
|
||||||
create_func = createQemuCreate;
|
|
||||||
} else {
|
} else {
|
||||||
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
|
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
|
||||||
"%s", _("creation of non-raw images "
|
"%s", _("creation of non-raw images "
|
||||||
|
Loading…
Reference in New Issue
Block a user