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>
35 qemu-img-cmds.hx | 4 +-
36 qemu-img.c | 191 +++++++++++++++++++++++++++++------------------
37 2 files changed, 121 insertions(+), 74 deletions(-)
39 diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
40 index b3620f29e5..e70ef3dc91 100644
41 --- a/qemu-img-cmds.hx
42 +++ b/qemu-img-cmds.hx
43 @@ -58,9 +58,9 @@ SRST
47 - "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] if=input of=output")
48 + "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] [osize=output_size] if=input of=output")
50 -.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=BLOCK_SIZE] [count=BLOCKS] [skip=BLOCKS] if=INPUT of=OUTPUT
51 +.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=BLOCK_SIZE] [count=BLOCKS] [skip=BLOCKS] [osize=OUTPUT_SIZE] if=INPUT of=OUTPUT
55 diff --git a/qemu-img.c b/qemu-img.c
56 index 7e6666b5f7..44cf942bd2 100644
59 @@ -4897,10 +4897,12 @@ static int img_bitmap(int argc, char **argv)
72 @@ -4976,6 +4978,19 @@ static int img_dd_skip(const char *arg,
76 +static int img_dd_osize(const char *arg,
77 + struct DdIo *in, struct DdIo *out,
80 + dd->osize = cvtnum("size", arg);
82 + if (dd->osize < 0) {
89 static int img_dd(int argc, char **argv)
92 @@ -5016,6 +5031,7 @@ static int img_dd(int argc, char **argv)
93 { "if", img_dd_if, C_IF },
94 { "of", img_dd_of, C_OF },
95 { "skip", img_dd_skip, C_SKIP },
96 + { "osize", img_dd_osize, C_OSIZE },
99 const struct option long_options[] = {
100 @@ -5094,8 +5110,13 @@ static int img_dd(int argc, char **argv)
104 - if (!(dd.flags & C_IF && dd.flags & C_OF)) {
105 - error_report("Must specify both input and output files");
106 + if (!(dd.flags & C_IF) && (!fmt || strcmp(fmt, "raw") != 0)) {
107 + error_report("Input format must be raw when readin from stdin");
111 + if (!(dd.flags & C_OF) && strcmp(out_fmt, "raw") != 0) {
112 + error_report("Output format must be raw when writing to stdout");
116 @@ -5107,85 +5128,101 @@ static int img_dd(int argc, char **argv)
120 - blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
122 + if (dd.flags & C_IF) {
123 + blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
135 - drv = bdrv_find_format(out_fmt);
137 - error_report("Unknown file format");
138 + if (dd.flags & C_OSIZE) {
140 + } else if (dd.flags & C_IF) {
141 + size = blk_getlength(blk1);
143 + error_report("Failed to get size for '%s'", in.filename);
147 + } else if (dd.flags & C_COUNT) {
148 + size = dd.count * in.bsz;
150 + error_report("Output size must be known when reading from stdin");
154 - proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
157 - error_report_err(local_err);
161 - if (!drv->create_opts) {
162 - error_report("Format driver '%s' does not support image creation",
167 - if (!proto_drv->create_opts) {
168 - error_report("Protocol driver '%s' does not support image creation",
169 - proto_drv->format_name);
172 + if (!(dd.flags & C_OSIZE) && dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
173 + dd.count * in.bsz < size) {
174 + size = dd.count * in.bsz;
176 - create_opts = qemu_opts_append(create_opts, drv->create_opts);
177 - create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
179 - opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
180 + if (dd.flags & C_OF) {
181 + drv = bdrv_find_format(out_fmt);
183 + error_report("Unknown file format");
187 + proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
189 - size = blk_getlength(blk1);
191 - error_report("Failed to get size for '%s'", in.filename);
196 + error_report_err(local_err);
200 + if (!drv->create_opts) {
201 + error_report("Format driver '%s' does not support image creation",
206 + if (!proto_drv->create_opts) {
207 + error_report("Protocol driver '%s' does not support image creation",
208 + proto_drv->format_name);
212 + create_opts = qemu_opts_append(create_opts, drv->create_opts);
213 + create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
215 - if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
216 - dd.count * in.bsz < size) {
217 - size = dd.count * in.bsz;
219 + opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
221 - /* Overflow means the specified offset is beyond input image's size */
222 - if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
223 - size < in.bsz * in.offset)) {
224 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
226 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
227 - size - in.bsz * in.offset, &error_abort);
229 + /* Overflow means the specified offset is beyond input image's size */
230 + if (dd.flags & C_OSIZE) {
231 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
232 + } else if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
233 + size < in.bsz * in.offset)) {
234 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
236 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
237 + size - in.bsz * in.offset, &error_abort);
240 - ret = bdrv_create(drv, out.filename, opts, &local_err);
242 - error_reportf_err(local_err,
243 - "%s: error while creating output image: ",
248 + ret = bdrv_create(drv, out.filename, opts, &local_err);
250 + error_reportf_err(local_err,
251 + "%s: error while creating output image: ",
257 - /* TODO, we can't honour --image-opts for the target,
258 - * since it needs to be given in a format compatible
259 - * with the bdrv_create() call above which does not
260 - * support image-opts style.
262 - blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
263 - false, false, false);
264 + /* TODO, we can't honour --image-opts for the target,
265 + * since it needs to be given in a format compatible
266 + * with the bdrv_create() call above which does not
267 + * support image-opts style.
269 + blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
270 + false, false, false);
281 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
282 @@ -5203,11 +5240,17 @@ static int img_dd(int argc, char **argv)
284 for (out_pos = 0; in_pos < size; block_count++) {
287 - if (in_pos + in.bsz > size) {
288 - in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
289 + size_t in_bsz = in_pos + in.bsz > size ? size - in_pos : in.bsz;
291 + in_ret = blk_pread(blk1, in_pos, in.buf, in_bsz);
293 - in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
294 + in_ret = read(STDIN_FILENO, in.buf, in_bsz);
296 + /* early EOF is considered an error */
297 + error_report("Input ended unexpectedly");
303 error_report("error while reading from input image file: %s",
304 @@ -5217,9 +5260,13 @@ static int img_dd(int argc, char **argv)
308 - out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
310 + out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
312 + out_ret = write(STDOUT_FILENO, in.buf, in_ret);
316 + if (out_ret != in_ret) {
317 error_report("error while writing to output image file: %s",