1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Fri, 23 Jun 2017 12:01:43 +0200
4 Subject: [PATCH] qemu-img dd: add osize and read from/to stdin/stdout
6 Neither convert nor dd were previously able to write to or
7 read from a pipe. Particularly serializing an image file
8 into a raw stream or vice versa can be useful, but using
9 `qemu-img convert -f qcow2 -O raw foo.qcow2 /dev/stdout` in
10 a pipe will fail trying to seek.
12 While dd and convert have overlapping use cases, `dd` is a
13 simple read/write loop while convert is much more
14 sophisticated and has ways to dealing with holes and blocks
16 Since these typically can't be detected in pipes via
17 SEEK_DATA/HOLE or skipped while writing, dd seems to be the
18 better choice for implementing stdin/stdout streams.
20 This patch causes "if" and "of" to default to stdin and
21 stdout respectively, allowing only the "raw" format to be
23 Since the input can now be a pipe we have no way of
24 detecting the size of the output image to create. Since we
25 also want to support images with a size not matching the
26 dd command's "bs" parameter (which, together with "count"
27 could be used to calculate the desired size, and is already
28 used to limit it), the "osize" option is added to explicitly
29 override the output file's size.
31 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
33 qemu-img-cmds.hx | 4 +-
34 qemu-img.c | 192 ++++++++++++++++++++++++++++++++++---------------------
35 2 files changed, 122 insertions(+), 74 deletions(-)
37 diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
38 index 2fe31893cf..52042f2773 100644
39 --- a/qemu-img-cmds.hx
40 +++ b/qemu-img-cmds.hx
41 @@ -53,9 +53,9 @@ STEXI
45 - "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] if=input of=output")
46 + "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] [osize=output_size] if=input of=output")
48 -@item dd [--image-opts] [-U] [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] [skip=@var{blocks}] if=@var{input} of=@var{output}
49 +@item dd [--image-opts] [-U] [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] [skip=@var{blocks}] [osize=output_size] if=@var{input} of=@var{output}
53 diff --git a/qemu-img.c b/qemu-img.c
54 index f8a39dd195..876a3623a7 100644
57 @@ -4088,10 +4088,12 @@ out:
70 @@ -4170,6 +4172,20 @@ static int img_dd_skip(const char *arg,
74 +static int img_dd_osize(const char *arg,
75 + struct DdIo *in, struct DdIo *out,
78 + dd->osize = cvtnum(arg);
80 + if (dd->osize < 0) {
81 + error_report("invalid number: '%s'", arg);
88 static int img_dd(int argc, char **argv)
91 @@ -4210,6 +4226,7 @@ static int img_dd(int argc, char **argv)
92 { "if", img_dd_if, C_IF },
93 { "of", img_dd_of, C_OF },
94 { "skip", img_dd_skip, C_SKIP },
95 + { "osize", img_dd_osize, C_OSIZE },
98 const struct option long_options[] = {
99 @@ -4288,8 +4305,13 @@ static int img_dd(int argc, char **argv)
103 - if (!(dd.flags & C_IF && dd.flags & C_OF)) {
104 - error_report("Must specify both input and output files");
105 + if (!(dd.flags & C_IF) && (!fmt || strcmp(fmt, "raw") != 0)) {
106 + error_report("Input format must be raw when readin from stdin");
110 + if (!(dd.flags & C_OF) && strcmp(out_fmt, "raw") != 0) {
111 + error_report("Output format must be raw when writing to stdout");
115 @@ -4301,85 +4323,101 @@ static int img_dd(int argc, char **argv)
119 - blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
121 + if (dd.flags & C_IF) {
122 + blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
134 - drv = bdrv_find_format(out_fmt);
136 - error_report("Unknown file format");
137 + if (dd.flags & C_OSIZE) {
139 + } else if (dd.flags & C_IF) {
140 + size = blk_getlength(blk1);
142 + error_report("Failed to get size for '%s'", in.filename);
146 + } else if (dd.flags & C_COUNT) {
147 + size = dd.count * in.bsz;
149 + error_report("Output size must be known when reading from stdin");
153 - proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
156 - error_report_err(local_err);
160 - if (!drv->create_opts) {
161 - error_report("Format driver '%s' does not support image creation",
166 - if (!proto_drv->create_opts) {
167 - error_report("Protocol driver '%s' does not support image creation",
168 - proto_drv->format_name);
171 + if (!(dd.flags & C_OSIZE) && dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
172 + dd.count * in.bsz < size) {
173 + size = dd.count * in.bsz;
175 - create_opts = qemu_opts_append(create_opts, drv->create_opts);
176 - create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
178 - opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
179 + if (dd.flags & C_OF) {
180 + drv = bdrv_find_format(out_fmt);
182 + error_report("Unknown file format");
186 + proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
188 - size = blk_getlength(blk1);
190 - error_report("Failed to get size for '%s'", in.filename);
195 + error_report_err(local_err);
199 + if (!drv->create_opts) {
200 + error_report("Format driver '%s' does not support image creation",
205 + if (!proto_drv->create_opts) {
206 + error_report("Protocol driver '%s' does not support image creation",
207 + proto_drv->format_name);
211 + create_opts = qemu_opts_append(create_opts, drv->create_opts);
212 + create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
214 - if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
215 - dd.count * in.bsz < size) {
216 - size = dd.count * in.bsz;
218 + opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
220 - /* Overflow means the specified offset is beyond input image's size */
221 - if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
222 - size < in.bsz * in.offset)) {
223 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
225 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
226 - size - in.bsz * in.offset, &error_abort);
228 + /* Overflow means the specified offset is beyond input image's size */
229 + if (dd.flags & C_OSIZE) {
230 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
231 + } else if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
232 + size < in.bsz * in.offset)) {
233 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
235 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
236 + size - in.bsz * in.offset, &error_abort);
239 - ret = bdrv_create(drv, out.filename, opts, &local_err);
241 - error_reportf_err(local_err,
242 - "%s: error while creating output image: ",
247 + ret = bdrv_create(drv, out.filename, opts, &local_err);
249 + error_reportf_err(local_err,
250 + "%s: error while creating output image: ",
256 - /* TODO, we can't honour --image-opts for the target,
257 - * since it needs to be given in a format compatible
258 - * with the bdrv_create() call above which does not
259 - * support image-opts style.
261 - blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
262 - false, false, false);
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);
280 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
281 @@ -4397,11 +4435,17 @@ static int img_dd(int argc, char **argv)
283 for (out_pos = 0; in_pos < size; block_count++) {
286 - if (in_pos + in.bsz > size) {
287 - in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
288 + size_t in_bsz = in_pos + in.bsz > size ? size - in_pos : in.bsz;
290 + in_ret = blk_pread(blk1, in_pos, in.buf, in_bsz);
292 - in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
293 + in_ret = read(STDIN_FILENO, in.buf, in_bsz);
295 + /* early EOF is considered an error */
296 + error_report("Input ended unexpectedly");
302 error_report("error while reading from input image file: %s",
303 @@ -4411,9 +4455,13 @@ static int img_dd(int argc, char **argv)
307 - out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
309 + out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
311 + out_ret = write(STDOUT_FILENO, in.buf, in_ret);
315 + if (out_ret != in_ret) {
316 error_report("error while writing to output image file: %s",