[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PATCH v2 3/3] qemu-img.c: clean up created file on img_cre
From: |
Daniel Henrique Barboza |
Subject: |
[Qemu-block] [PATCH v2 3/3] qemu-img.c: clean up created file on img_create failure |
Date: |
Fri, 22 Mar 2019 14:52:41 -0300 |
When using a non-UTF8 secret to create a volume using qemu-img, the
following error happens:
$ qemu-img create -f luks --object
secret,id=vol_1_encrypt0,file=vol_resize_pool.vol_1.secret.qzVQrI -o
key-secret=vol_1_encrypt0 /var/tmp/pool_target/vol_1 10240K
Formatting '/var/tmp/pool_target/vol_1', fmt=luks size=10485760
key-secret=vol_1_encrypt0
qemu-img: /var/tmp/pool_target/vol_1: Data from secret vol_1_encrypt0 is not
valid UTF-8
However, the created file /var/tmp/pool_target/vol_1 is left behind in the
file system after the failure. This behavior can be observed when creating
the volume using Libvirt, via 'virsh vol-create', and then getting "volume
target path already exist" errors when trying to re-create the volume.
The volume file is created inside block_crypto_co_create_opts_luks, in
block/crypto.c. If the bdrv_create_file() call is successful but any
succeeding step fails*, the existing 'fail' label does not take into
account the created file, leaving it behind.
This patch changes img_create to check if @filename is an existing file
before bdrv_img_create is called. In case of failure, if @filename didn't
exist before, check again for its existence and, if affirmative, erase it
by calling bdrv_delete_file.
* in our case, block_crypto_co_create_generic calls qcrypto_block_create,
which calls qcrypto_block_luks_create, and this function fails when
calling qcrypto_secret_lookup_as_utf8.
Reported-by: Srikanth Aithal <address@hidden>
Suggested-by: Daniel P. Berrangé <address@hidden>
Signed-off-by: Daniel Henrique Barboza <address@hidden>
---
qemu-img.c | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/qemu-img.c b/qemu-img.c
index 5fac840742..03b139b4ac 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -422,11 +422,12 @@ static int img_create(int argc, char **argv)
uint64_t img_size = -1;
const char *fmt = "raw";
const char *base_fmt = NULL;
- const char *filename;
+ const char *filename, *path;
const char *base_filename = NULL;
char *options = NULL;
Error *local_err = NULL;
bool quiet = false;
+ bool file_already_existed = false;
int flags = 0;
for(;;) {
@@ -529,6 +530,15 @@ static int img_create(int argc, char **argv)
error_exit("Unexpected argument: %s", argv[optind]);
}
+ /*
+ * Check if 'filename' represents a local file that already
+ * exists in the file system prior to bdrv_img_create. Strip
+ * the leading 'file:' from the filename if it exists.
+ */
+ path = filename;
+ strstart(path, "file:", &path);
+ file_already_existed = bdrv_path_is_regular_file(path);
+
bdrv_img_create(filename, fmt, base_filename, base_fmt,
options, img_size, flags, quiet, &local_err);
if (local_err) {
@@ -541,6 +551,23 @@ static int img_create(int argc, char **argv)
fail:
g_free(options);
+ /*
+ * If an error occurred and we ended up creating a bogus
+ * 'filename' file, delete it
+ */
+ if (!file_already_existed && bdrv_path_is_regular_file(path)) {
+
+ int ret = bdrv_delete_file(path, fmt, &local_err);
+ /*
+ * ENOTSUP will happen if the block driver doesn't support
+ * 'bdrv_co_delete_file'. ENOENT will happen if the file
+ * doesn't exist. Both are predictable and shouldn't be
+ * reported back to the user.
+ */
+ if ((ret < 0) && (ret != -ENOTSUP) && (ret != -ENOENT)) {
+ error_reportf_err(local_err, "%s: ", filename);
+ }
+ }
return 1;
}
--
2.20.1