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 | 176 +++++++++++++++++++++++++++++++++++--------------------
35 2 files changed, 115 insertions(+), 65 deletions(-)
37 diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
38 index 8ac78222af..16bee83987 100644
39 --- a/qemu-img-cmds.hx
40 +++ b/qemu-img-cmds.hx
41 @@ -46,9 +46,9 @@ STEXI
45 - "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] if=input of=output")
46 + "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] [osize=output_size] [if=input] [of=output]")
48 -@item dd [--image-opts] [-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] [-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 c7804d63ee..ee7816e727 100644
57 @@ -4026,10 +4026,12 @@ out:
70 @@ -4108,6 +4110,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 @@ -4147,6 +4163,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 @@ -4214,84 +4231,106 @@ 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 - blk1 = img_open(image_opts, in.filename, fmt, 0, false, false);
113 + if (!(dd.flags & C_OF) && strcmp(out_fmt, "raw") != 0) {
114 + error_report("Output format must be raw when writing to stdout");
118 + if (dd.flags & C_IF) {
119 + blk1 = img_open(image_opts, in.filename, fmt, 0, false, false);
121 - drv = bdrv_find_format(out_fmt);
123 - error_report("Unknown file format");
127 - proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
130 - error_report_err(local_err);
134 - if (!drv->create_opts) {
135 - error_report("Format driver '%s' does not support image creation",
140 - if (!proto_drv->create_opts) {
141 - error_report("Protocol driver '%s' does not support image creation",
142 - proto_drv->format_name);
150 - create_opts = qemu_opts_append(create_opts, drv->create_opts);
151 - create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
153 - opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
155 - size = blk_getlength(blk1);
157 - error_report("Failed to get size for '%s'", in.filename);
158 + if (dd.flags & C_OSIZE) {
160 + } else if (dd.flags & C_IF) {
161 + size = blk_getlength(blk1);
163 + error_report("Failed to get size for '%s'", in.filename);
167 + } else if (dd.flags & C_COUNT) {
168 + size = dd.count * in.bsz;
170 + error_report("Output size must be known when reading from stdin");
175 - if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
176 + if (!(dd.flags & C_OSIZE) && dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
177 dd.count * in.bsz < size) {
178 size = dd.count * in.bsz;
181 - /* Overflow means the specified offset is beyond input image's size */
182 - if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
183 - size < in.bsz * in.offset)) {
184 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
186 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
187 - size - in.bsz * in.offset, &error_abort);
190 - ret = bdrv_create(drv, out.filename, opts, &local_err);
192 - error_reportf_err(local_err,
193 - "%s: error while creating output image: ",
198 + if (dd.flags & C_OF) {
199 + drv = bdrv_find_format(out_fmt);
201 + error_report("Unknown file format");
205 + proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
208 + error_report_err(local_err);
212 + if (!drv->create_opts) {
213 + error_report("Format driver '%s' does not support image creation",
218 + if (!proto_drv->create_opts) {
219 + error_report("Protocol driver '%s' does not support image creation",
220 + proto_drv->format_name);
224 + create_opts = qemu_opts_append(create_opts, drv->create_opts);
225 + create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
227 - blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
229 + opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
234 + /* Overflow means the specified offset is beyond input image's size */
235 + if (dd.flags & C_OSIZE) {
236 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
237 + } else if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
238 + size < in.bsz * in.offset)) {
239 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
241 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
242 + size - in.bsz * in.offset, &error_abort);
245 + ret = bdrv_create(drv, out.filename, opts, &local_err);
247 + error_reportf_err(local_err,
248 + "%s: error while creating output image: ",
254 + blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
263 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
264 @@ -4309,11 +4348,18 @@ static int img_dd(int argc, char **argv)
266 for (out_pos = 0; in_pos < size; block_count++) {
268 + size_t in_bsz = in_pos + in.bsz > size ? size - in_pos : in.bsz;
270 - if (in_pos + in.bsz > size) {
271 - in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos);
273 + in_ret = blk_pread(blk1, in_pos, in.buf, in_bsz);
275 - in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
276 + in_ret = read(STDIN_FILENO, in.buf, in_bsz);
278 + /* early EOF is considered an error */
279 + error_report("Input ended unexpectedly");
285 error_report("error while reading from input image file: %s",
286 @@ -4323,9 +4369,13 @@ static int img_dd(int argc, char **argv)
290 - out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
292 + out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
294 + out_ret = write(STDOUT_FILENO, in.buf, in_ret);
298 + if (out_ret != in_ret) {
299 error_report("error while writing to output image file: %s",