]>
Commit | Line | Data |
---|---|---|
23102ed6 | 1 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
3c6facff WB |
2 | From: Wolfgang Bumiller <w.bumiller@proxmox.com> |
3 | Date: Fri, 23 Jun 2017 12:01:43 +0200 | |
23102ed6 | 4 | Subject: [PATCH] qemu-img dd: add osize and read from/to stdin/stdout |
3c6facff WB |
5 | |
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. | |
11 | ||
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 | |
15 | of zeroes. | |
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. | |
19 | ||
20 | This patch causes "if" and "of" to default to stdin and | |
21 | stdout respectively, allowing only the "raw" format to be | |
22 | used in these cases. | |
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. | |
30 | ||
31 | Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com> | |
32 | --- | |
67af0fa4 WB |
33 | qemu-img-cmds.hx | 4 +- |
34 | qemu-img.c | 176 +++++++++++++++++++++++++++++++++++-------------------- | |
35 | 2 files changed, 115 insertions(+), 65 deletions(-) | |
3c6facff | 36 | |
67af0fa4 WB |
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 | |
42 | ETEXI | |
43 | ||
44 | DEF("dd", img_dd, | |
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]") | |
47 | STEXI | |
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}] | |
50 | ETEXI | |
51 | ||
52 | DEF("info", img_info, | |
3c6facff | 53 | diff --git a/qemu-img.c b/qemu-img.c |
ddbcf45e | 54 | index c7804d63ee..ee7816e727 100644 |
3c6facff WB |
55 | --- a/qemu-img.c |
56 | +++ b/qemu-img.c | |
ddbcf45e | 57 | @@ -4026,10 +4026,12 @@ out: |
3c6facff WB |
58 | #define C_IF 04 |
59 | #define C_OF 010 | |
60 | #define C_SKIP 020 | |
61 | +#define C_OSIZE 040 | |
62 | ||
63 | struct DdInfo { | |
64 | unsigned int flags; | |
65 | int64_t count; | |
66 | + int64_t osize; | |
67 | }; | |
68 | ||
69 | struct DdIo { | |
ddbcf45e | 70 | @@ -4108,6 +4110,20 @@ static int img_dd_skip(const char *arg, |
3c6facff WB |
71 | return 0; |
72 | } | |
73 | ||
74 | +static int img_dd_osize(const char *arg, | |
75 | + struct DdIo *in, struct DdIo *out, | |
76 | + struct DdInfo *dd) | |
77 | +{ | |
78 | + dd->osize = cvtnum(arg); | |
79 | + | |
80 | + if (dd->osize < 0) { | |
81 | + error_report("invalid number: '%s'", arg); | |
82 | + return 1; | |
83 | + } | |
84 | + | |
85 | + return 0; | |
86 | +} | |
87 | + | |
88 | static int img_dd(int argc, char **argv) | |
89 | { | |
90 | int ret = 0; | |
ddbcf45e | 91 | @@ -4147,6 +4163,7 @@ static int img_dd(int argc, char **argv) |
3c6facff WB |
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 }, | |
96 | { NULL, NULL, 0 } | |
97 | }; | |
98 | const struct option long_options[] = { | |
ddbcf45e | 99 | @@ -4214,84 +4231,106 @@ static int img_dd(int argc, char **argv) |
3c6facff WB |
100 | arg = NULL; |
101 | } | |
102 | ||
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"); | |
107 | ret = -1; | |
108 | goto out; | |
109 | } | |
110 | - blk1 = img_open(image_opts, in.filename, fmt, 0, false, false); | |
111 | - | |
112 | - if (!blk1) { | |
113 | + if (!(dd.flags & C_OF) && strcmp(out_fmt, "raw") != 0) { | |
114 | + error_report("Output format must be raw when writing to stdout"); | |
115 | ret = -1; | |
116 | goto out; | |
117 | } | |
118 | + if (dd.flags & C_IF) { | |
119 | + blk1 = img_open(image_opts, in.filename, fmt, 0, false, false); | |
120 | ||
121 | - drv = bdrv_find_format(out_fmt); | |
122 | - if (!drv) { | |
123 | - error_report("Unknown file format"); | |
124 | - ret = -1; | |
125 | - goto out; | |
126 | - } | |
127 | - proto_drv = bdrv_find_protocol(out.filename, true, &local_err); | |
128 | - | |
129 | - if (!proto_drv) { | |
130 | - error_report_err(local_err); | |
131 | - ret = -1; | |
132 | - goto out; | |
133 | - } | |
134 | - if (!drv->create_opts) { | |
135 | - error_report("Format driver '%s' does not support image creation", | |
136 | - drv->format_name); | |
137 | - ret = -1; | |
138 | - goto out; | |
139 | - } | |
140 | - if (!proto_drv->create_opts) { | |
141 | - error_report("Protocol driver '%s' does not support image creation", | |
142 | - proto_drv->format_name); | |
143 | - ret = -1; | |
144 | - goto out; | |
145 | + if (!blk1) { | |
146 | + ret = -1; | |
147 | + goto out; | |
148 | + } | |
149 | } | |
150 | - create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
151 | - create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
152 | - | |
153 | - opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
154 | ||
155 | - size = blk_getlength(blk1); | |
156 | - if (size < 0) { | |
157 | - error_report("Failed to get size for '%s'", in.filename); | |
158 | + if (dd.flags & C_OSIZE) { | |
159 | + size = dd.osize; | |
160 | + } else if (dd.flags & C_IF) { | |
161 | + size = blk_getlength(blk1); | |
162 | + if (size < 0) { | |
163 | + error_report("Failed to get size for '%s'", in.filename); | |
164 | + ret = -1; | |
165 | + goto out; | |
166 | + } | |
167 | + } else if (dd.flags & C_COUNT) { | |
168 | + size = dd.count * in.bsz; | |
169 | + } else { | |
170 | + error_report("Output size must be known when reading from stdin"); | |
171 | ret = -1; | |
172 | goto out; | |
173 | } | |
174 | ||
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; | |
179 | } | |
180 | ||
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); | |
185 | - } else { | |
186 | - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, | |
187 | - size - in.bsz * in.offset, &error_abort); | |
188 | - } | |
189 | ||
190 | - ret = bdrv_create(drv, out.filename, opts, &local_err); | |
191 | - if (ret < 0) { | |
192 | - error_reportf_err(local_err, | |
193 | - "%s: error while creating output image: ", | |
194 | - out.filename); | |
195 | - ret = -1; | |
196 | - goto out; | |
197 | - } | |
198 | + if (dd.flags & C_OF) { | |
199 | + drv = bdrv_find_format(out_fmt); | |
200 | + if (!drv) { | |
201 | + error_report("Unknown file format"); | |
202 | + ret = -1; | |
203 | + goto out; | |
204 | + } | |
205 | + proto_drv = bdrv_find_protocol(out.filename, true, &local_err); | |
206 | + | |
207 | + if (!proto_drv) { | |
208 | + error_report_err(local_err); | |
209 | + ret = -1; | |
210 | + goto out; | |
211 | + } | |
212 | + if (!drv->create_opts) { | |
213 | + error_report("Format driver '%s' does not support image creation", | |
214 | + drv->format_name); | |
215 | + ret = -1; | |
216 | + goto out; | |
217 | + } | |
218 | + if (!proto_drv->create_opts) { | |
219 | + error_report("Protocol driver '%s' does not support image creation", | |
220 | + proto_drv->format_name); | |
221 | + ret = -1; | |
222 | + goto out; | |
223 | + } | |
224 | + create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
225 | + create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
226 | ||
227 | - blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR, | |
228 | - false, false); | |
229 | + opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
230 | ||
231 | - if (!blk2) { | |
232 | - ret = -1; | |
233 | - goto out; | |
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); | |
240 | + } else { | |
241 | + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, | |
242 | + size - in.bsz * in.offset, &error_abort); | |
243 | + } | |
244 | + | |
245 | + ret = bdrv_create(drv, out.filename, opts, &local_err); | |
246 | + if (ret < 0) { | |
247 | + error_reportf_err(local_err, | |
248 | + "%s: error while creating output image: ", | |
249 | + out.filename); | |
250 | + ret = -1; | |
251 | + goto out; | |
252 | + } | |
253 | + | |
254 | + blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR, | |
255 | + false, false); | |
256 | + | |
257 | + if (!blk2) { | |
258 | + ret = -1; | |
259 | + goto out; | |
260 | + } | |
261 | } | |
262 | ||
263 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || | |
ddbcf45e | 264 | @@ -4309,11 +4348,18 @@ static int img_dd(int argc, char **argv) |
3c6facff WB |
265 | |
266 | for (out_pos = 0; in_pos < size; block_count++) { | |
267 | int in_ret, out_ret; | |
268 | + size_t in_bsz = in_pos + in.bsz > size ? size - in_pos : in.bsz; | |
269 | ||
270 | - if (in_pos + in.bsz > size) { | |
271 | - in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); | |
272 | + if (blk1) { | |
273 | + in_ret = blk_pread(blk1, in_pos, in.buf, in_bsz); | |
274 | } else { | |
275 | - in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); | |
276 | + in_ret = read(STDIN_FILENO, in.buf, in_bsz); | |
277 | + if (in_ret == 0) { | |
278 | + /* early EOF is considered an error */ | |
279 | + error_report("Input ended unexpectedly"); | |
280 | + ret = -1; | |
281 | + goto out; | |
282 | + } | |
283 | } | |
284 | if (in_ret < 0) { | |
285 | error_report("error while reading from input image file: %s", | |
ddbcf45e | 286 | @@ -4323,9 +4369,13 @@ static int img_dd(int argc, char **argv) |
3c6facff WB |
287 | } |
288 | in_pos += in_ret; | |
289 | ||
290 | - out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); | |
291 | + if (blk2) { | |
292 | + out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); | |
293 | + } else { | |
294 | + out_ret = write(STDOUT_FILENO, in.buf, in_ret); | |
295 | + } | |
296 | ||
297 | - if (out_ret < 0) { | |
298 | + if (out_ret != in_ret) { | |
299 | error_report("error while writing to output image file: %s", | |
300 | strerror(-out_ret)); | |
301 | ret = -1; | |
302 | -- | |
303 | 2.11.0 | |
304 |