mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-10-22 22:13:18 +00:00
storage: avoid an intermediate malloc
Suggested here: https://www.redhat.com/archives/libvir-list/2011-May/msg00594.html * src/storage/storage_backend.c (virStorageBackendCreateQemuImg): Generate size inline.
This commit is contained in:
parent
f3d6754415
commit
66dc2ae61b
@ -649,11 +649,11 @@ virStorageBackendCreateQemuImg(virConnectPtr conn,
|
|||||||
unsigned int flags ATTRIBUTE_UNUSED)
|
unsigned int flags ATTRIBUTE_UNUSED)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
char *size = NULL;
|
|
||||||
char *create_tool;
|
char *create_tool;
|
||||||
int imgformat = -1;
|
int imgformat = -1;
|
||||||
virCommandPtr cmd = NULL;
|
virCommandPtr cmd = NULL;
|
||||||
bool do_encryption = (vol->target.encryption != NULL);
|
bool do_encryption = (vol->target.encryption != NULL);
|
||||||
|
unsigned long long int size_arg;
|
||||||
|
|
||||||
const char *type = virStorageFileFormatTypeToString(vol->target.format);
|
const char *type = virStorageFileFormatTypeToString(vol->target.format);
|
||||||
const char *backingType = vol->backingStore.path ?
|
const char *backingType = vol->backingStore.path ?
|
||||||
@ -757,10 +757,7 @@ virStorageBackendCreateQemuImg(virConnectPtr conn,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Size in KB */
|
/* Size in KB */
|
||||||
if (virAsprintf(&size, "%lluK", VIR_DIV_UP(vol->capacity, 1024)) < 0) {
|
size_arg = VIR_DIV_UP(vol->capacity, 1024);
|
||||||
virReportOOMError();
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* KVM is usually ahead of qemu on features, so try that first */
|
/* KVM is usually ahead of qemu on features, so try that first */
|
||||||
create_tool = virFindFileInPath("kvm-img");
|
create_tool = virFindFileInPath("kvm-img");
|
||||||
@ -798,7 +795,8 @@ virStorageBackendCreateQemuImg(virConnectPtr conn,
|
|||||||
switch (imgformat) {
|
switch (imgformat) {
|
||||||
case QEMU_IMG_BACKING_FORMAT_FLAG:
|
case QEMU_IMG_BACKING_FORMAT_FLAG:
|
||||||
virCommandAddArgList(cmd, "-F", backingType, vol->target.path,
|
virCommandAddArgList(cmd, "-F", backingType, vol->target.path,
|
||||||
size, NULL);
|
NULL);
|
||||||
|
virCommandAddArgFormat(cmd, "%lluK", size_arg);
|
||||||
|
|
||||||
if (do_encryption)
|
if (do_encryption)
|
||||||
virCommandAddArg(cmd, "-e");
|
virCommandAddArg(cmd, "-e");
|
||||||
@ -808,20 +806,23 @@ virStorageBackendCreateQemuImg(virConnectPtr conn,
|
|||||||
virCommandAddArg(cmd, "-o");
|
virCommandAddArg(cmd, "-o");
|
||||||
virCommandAddArgFormat(cmd, "backing_fmt=%s%s", backingType,
|
virCommandAddArgFormat(cmd, "backing_fmt=%s%s", backingType,
|
||||||
do_encryption ? ",encryption=on" : "");
|
do_encryption ? ",encryption=on" : "");
|
||||||
virCommandAddArgList(cmd, vol->target.path, size, NULL);
|
virCommandAddArg(cmd, vol->target.path);
|
||||||
|
virCommandAddArgFormat(cmd, "%lluK", size_arg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
VIR_INFO("Unable to set backing store format for %s with %s",
|
VIR_INFO("Unable to set backing store format for %s with %s",
|
||||||
vol->target.path, create_tool);
|
vol->target.path, create_tool);
|
||||||
|
|
||||||
virCommandAddArgList(cmd, vol->target.path, size, NULL);
|
virCommandAddArg(cmd, vol->target.path);
|
||||||
|
virCommandAddArgFormat(cmd, "%lluK", size_arg);
|
||||||
if (do_encryption)
|
if (do_encryption)
|
||||||
virCommandAddArg(cmd, "-e");
|
virCommandAddArg(cmd, "-e");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
virCommandAddArgList(cmd, "create", "-f", type,
|
virCommandAddArgList(cmd, "create", "-f", type,
|
||||||
vol->target.path, size, NULL);
|
vol->target.path, NULL);
|
||||||
|
virCommandAddArgFormat(cmd, "%lluK", size_arg);
|
||||||
|
|
||||||
if (do_encryption) {
|
if (do_encryption) {
|
||||||
if (imgformat == QEMU_IMG_BACKING_FORMAT_OPTIONS) {
|
if (imgformat == QEMU_IMG_BACKING_FORMAT_OPTIONS) {
|
||||||
@ -834,7 +835,6 @@ virStorageBackendCreateQemuImg(virConnectPtr conn,
|
|||||||
|
|
||||||
ret = virStorageBackendCreateExecCommand(pool, vol, cmd);
|
ret = virStorageBackendCreateExecCommand(pool, vol, cmd);
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FREE(size);
|
|
||||||
VIR_FREE(create_tool);
|
VIR_FREE(create_tool);
|
||||||
virCommandFree(cmd);
|
virCommandFree(cmd);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user