1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Mon, 6 Apr 2020 12:16:40 +0200
4 Subject: [PATCH] PVE: [Up] qemu-img dd: add osize and read from/to
7 Neither convert nor dd were previously able to write to or
8 read from a pipe. Particularly serializing an image file
9 into a raw stream or vice versa can be useful, but using
10 `qemu-img convert -f qcow2 -O raw foo.qcow2 /dev/stdout` in
11 a pipe will fail trying to seek.
13 While dd and convert have overlapping use cases, `dd` is a
14 simple read/write loop while convert is much more
15 sophisticated and has ways to dealing with holes and blocks
17 Since these typically can't be detected in pipes via
18 SEEK_DATA/HOLE or skipped while writing, dd seems to be the
19 better choice for implementing stdin/stdout streams.
21 This patch causes "if" and "of" to default to stdin and
22 stdout respectively, allowing only the "raw" format to be
24 Since the input can now be a pipe we have no way of
25 detecting the size of the output image to create. Since we
26 also want to support images with a size not matching the
27 dd command's "bs" parameter (which, together with "count"
28 could be used to calculate the desired size, and is already
29 used to limit it), the "osize" option is added to explicitly
30 override the output file's size.
32 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
33 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
34 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
36 qemu-img-cmds.hx | 4 +-
37 qemu-img.c | 202 ++++++++++++++++++++++++++++++-----------------
38 2 files changed, 133 insertions(+), 73 deletions(-)
40 diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
41 index 1b1dab5b17..d1616c045a 100644
42 --- a/qemu-img-cmds.hx
43 +++ b/qemu-img-cmds.hx
44 @@ -58,9 +58,9 @@ SRST
48 - "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] if=input of=output")
49 + "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] [osize=output_size] if=input of=output")
51 -.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=BLOCK_SIZE] [count=BLOCKS] [skip=BLOCKS] if=INPUT of=OUTPUT
52 +.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=BLOCK_SIZE] [count=BLOCKS] [skip=BLOCKS] [osize=OUTPUT_SIZE] if=INPUT of=OUTPUT
56 diff --git a/qemu-img.c b/qemu-img.c
57 index 25d427edd1..220e6ec577 100644
60 @@ -4899,10 +4899,12 @@ static int img_bitmap(int argc, char **argv)
73 @@ -4978,6 +4980,19 @@ static int img_dd_skip(const char *arg,
77 +static int img_dd_osize(const char *arg,
78 + struct DdIo *in, struct DdIo *out,
81 + dd->osize = cvtnum("size", arg);
83 + if (dd->osize < 0) {
90 static int img_dd(int argc, char **argv)
93 @@ -5018,6 +5033,7 @@ static int img_dd(int argc, char **argv)
94 { "if", img_dd_if, C_IF },
95 { "of", img_dd_of, C_OF },
96 { "skip", img_dd_skip, C_SKIP },
97 + { "osize", img_dd_osize, C_OSIZE },
100 const struct option long_options[] = {
101 @@ -5093,91 +5109,112 @@ static int img_dd(int argc, char **argv)
105 - if (!(dd.flags & C_IF && dd.flags & C_OF)) {
106 - error_report("Must specify both input and output files");
107 + if (!(dd.flags & C_IF) && (!fmt || strcmp(fmt, "raw") != 0)) {
108 + error_report("Input format must be raw when readin from stdin");
113 - blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
117 + if (!(dd.flags & C_OF) && strcmp(out_fmt, "raw") != 0) {
118 + error_report("Output format must be raw when writing to stdout");
123 - drv = bdrv_find_format(out_fmt);
125 - error_report("Unknown file format");
129 - proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
130 + if (dd.flags & C_IF) {
131 + blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
135 - error_report_err(local_err);
139 - if (!drv->create_opts) {
140 - error_report("Format driver '%s' does not support image creation",
145 - if (!proto_drv->create_opts) {
146 - error_report("Protocol driver '%s' does not support image creation",
147 - proto_drv->format_name);
155 - create_opts = qemu_opts_append(create_opts, drv->create_opts);
156 - create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
158 - opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
160 - size = blk_getlength(blk1);
162 - error_report("Failed to get size for '%s'", in.filename);
163 + if (dd.flags & C_OSIZE) {
165 + } else if (dd.flags & C_IF) {
166 + size = blk_getlength(blk1);
168 + error_report("Failed to get size for '%s'", in.filename);
172 + } else if (dd.flags & C_COUNT) {
173 + size = dd.count * in.bsz;
175 + error_report("Output size must be known when reading from stdin");
180 - if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
181 + if (!(dd.flags & C_OSIZE) && dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
182 dd.count * in.bsz < size) {
183 size = dd.count * in.bsz;
186 - /* Overflow means the specified offset is beyond input image's size */
187 - if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
188 - size < in.bsz * in.offset)) {
189 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
191 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
192 - size - in.bsz * in.offset, &error_abort);
194 + if (dd.flags & C_OF) {
195 + drv = bdrv_find_format(out_fmt);
197 + error_report("Unknown file format");
201 + proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
203 - ret = bdrv_create(drv, out.filename, opts, &local_err);
205 - error_reportf_err(local_err,
206 - "%s: error while creating output image: ",
212 + error_report_err(local_err);
216 + if (!drv->create_opts) {
217 + error_report("Format driver '%s' does not support image creation",
222 + if (!proto_drv->create_opts) {
223 + error_report("Protocol driver '%s' does not support image creation",
224 + proto_drv->format_name);
228 + create_opts = qemu_opts_append(create_opts, drv->create_opts);
229 + create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
231 - /* TODO, we can't honour --image-opts for the target,
232 - * since it needs to be given in a format compatible
233 - * with the bdrv_create() call above which does not
234 - * support image-opts style.
236 - blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
237 - false, false, false);
238 + opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
243 + /* Overflow means the specified offset is beyond input image's size */
244 + if (dd.flags & C_OSIZE) {
245 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
246 + } else if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
247 + size < in.bsz * in.offset)) {
248 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
250 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
251 + size - in.bsz * in.offset, &error_abort);
254 + ret = bdrv_create(drv, out.filename, opts, &local_err);
256 + error_reportf_err(local_err,
257 + "%s: error while creating output image: ",
263 + /* TODO, we can't honour --image-opts for the target,
264 + * since it needs to be given in a format compatible
265 + * with the bdrv_create() call above which does not
266 + * support image-opts style.
268 + blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
269 + false, false, false);
277 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
278 @@ -5194,20 +5231,43 @@ static int img_dd(int argc, char **argv)
279 in.buf = g_new(uint8_t, in.bsz);
281 for (out_pos = 0; in_pos < size; ) {
282 + int in_ret, out_ret;
283 int bytes = (in_pos + in.bsz > size) ? size - in_pos : in.bsz;
285 - ret = blk_pread(blk1, in_pos, bytes, in.buf, 0);
288 + in_ret = blk_pread(blk1, in_pos, bytes, in.buf, 0);
293 + in_ret = read(STDIN_FILENO, in.buf, bytes);
295 + /* early EOF is considered an error */
296 + error_report("Input ended unexpectedly");
302 error_report("error while reading from input image file: %s",
304 + strerror(-in_ret));
310 - ret = blk_pwrite(blk2, out_pos, bytes, in.buf, 0);
313 + out_ret = blk_pwrite(blk2, out_pos, in_ret, in.buf, 0);
314 + if (out_ret == 0) {
318 + out_ret = write(STDOUT_FILENO, in.buf, in_ret);
321 + if (out_ret != in_ret) {
322 error_report("error while writing to output image file: %s",
324 + strerror(-out_ret));