]> git.proxmox.com Git - pve-qemu.git/blob - debian/patches/pve/0024-qemu-img-dd-add-osize-and-read-from-to-stdin-stdout.patch
bump version to 2.11.1-1
[pve-qemu.git] / debian / patches / pve / 0024-qemu-img-dd-add-osize-and-read-from-to-stdin-stdout.patch
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
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 ---
33 qemu-img-cmds.hx | 4 +-
34 qemu-img.c | 192 ++++++++++++++++++++++++++++++++++---------------------
35 2 files changed, 122 insertions(+), 74 deletions(-)
36
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
42 ETEXI
43
44 DEF("dd", img_dd,
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")
47 STEXI
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}
50 ETEXI
51
52 DEF("info", img_info,
53 diff --git a/qemu-img.c b/qemu-img.c
54 index f8a39dd195..876a3623a7 100644
55 --- a/qemu-img.c
56 +++ b/qemu-img.c
57 @@ -4088,10 +4088,12 @@ out:
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 {
70 @@ -4170,6 +4172,20 @@ static int img_dd_skip(const char *arg,
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;
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 },
96 { NULL, NULL, 0 }
97 };
98 const struct option long_options[] = {
99 @@ -4288,8 +4305,13 @@ static int img_dd(int argc, char **argv)
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 + if (!(dd.flags & C_OF) && strcmp(out_fmt, "raw") != 0) {
111 + error_report("Output format must be raw when writing to stdout");
112 ret = -1;
113 goto out;
114 }
115 @@ -4301,85 +4323,101 @@ static int img_dd(int argc, char **argv)
116 goto out;
117 }
118
119 - blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
120 - force_share);
121 + if (dd.flags & C_IF) {
122 + blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
123 + force_share);
124
125 - if (!blk1) {
126 - ret = -1;
127 - goto out;
128 + if (!blk1) {
129 + ret = -1;
130 + goto out;
131 + }
132 }
133
134 - drv = bdrv_find_format(out_fmt);
135 - if (!drv) {
136 - error_report("Unknown file format");
137 + if (dd.flags & C_OSIZE) {
138 + size = dd.osize;
139 + } else if (dd.flags & C_IF) {
140 + size = blk_getlength(blk1);
141 + if (size < 0) {
142 + error_report("Failed to get size for '%s'", in.filename);
143 + ret = -1;
144 + goto out;
145 + }
146 + } else if (dd.flags & C_COUNT) {
147 + size = dd.count * in.bsz;
148 + } else {
149 + error_report("Output size must be known when reading from stdin");
150 ret = -1;
151 goto out;
152 }
153 - proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
154
155 - if (!proto_drv) {
156 - error_report_err(local_err);
157 - ret = -1;
158 - goto out;
159 - }
160 - if (!drv->create_opts) {
161 - error_report("Format driver '%s' does not support image creation",
162 - drv->format_name);
163 - ret = -1;
164 - goto out;
165 - }
166 - if (!proto_drv->create_opts) {
167 - error_report("Protocol driver '%s' does not support image creation",
168 - proto_drv->format_name);
169 - ret = -1;
170 - goto out;
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;
174 }
175 - create_opts = qemu_opts_append(create_opts, drv->create_opts);
176 - create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
177
178 - opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
179 + if (dd.flags & C_OF) {
180 + drv = bdrv_find_format(out_fmt);
181 + if (!drv) {
182 + error_report("Unknown file format");
183 + ret = -1;
184 + goto out;
185 + }
186 + proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
187
188 - size = blk_getlength(blk1);
189 - if (size < 0) {
190 - error_report("Failed to get size for '%s'", in.filename);
191 - ret = -1;
192 - goto out;
193 - }
194 + if (!proto_drv) {
195 + error_report_err(local_err);
196 + ret = -1;
197 + goto out;
198 + }
199 + if (!drv->create_opts) {
200 + error_report("Format driver '%s' does not support image creation",
201 + drv->format_name);
202 + ret = -1;
203 + goto out;
204 + }
205 + if (!proto_drv->create_opts) {
206 + error_report("Protocol driver '%s' does not support image creation",
207 + proto_drv->format_name);
208 + ret = -1;
209 + goto out;
210 + }
211 + create_opts = qemu_opts_append(create_opts, drv->create_opts);
212 + create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
213
214 - if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
215 - dd.count * in.bsz < size) {
216 - size = dd.count * in.bsz;
217 - }
218 + opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
219
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);
224 - } else {
225 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
226 - size - in.bsz * in.offset, &error_abort);
227 - }
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);
234 + } else {
235 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
236 + size - in.bsz * in.offset, &error_abort);
237 + }
238
239 - ret = bdrv_create(drv, out.filename, opts, &local_err);
240 - if (ret < 0) {
241 - error_reportf_err(local_err,
242 - "%s: error while creating output image: ",
243 - out.filename);
244 - ret = -1;
245 - goto out;
246 - }
247 + ret = bdrv_create(drv, out.filename, opts, &local_err);
248 + if (ret < 0) {
249 + error_reportf_err(local_err,
250 + "%s: error while creating output image: ",
251 + out.filename);
252 + ret = -1;
253 + goto out;
254 + }
255
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.
260 - */
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.
267 + */
268 + blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
269 + false, false, false);
270
271 - if (!blk2) {
272 - ret = -1;
273 - goto out;
274 + if (!blk2) {
275 + ret = -1;
276 + goto out;
277 + }
278 }
279
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)
282
283 for (out_pos = 0; in_pos < size; block_count++) {
284 int in_ret, out_ret;
285 -
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;
289 + if (blk1) {
290 + in_ret = blk_pread(blk1, in_pos, in.buf, in_bsz);
291 } else {
292 - in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
293 + in_ret = read(STDIN_FILENO, in.buf, in_bsz);
294 + if (in_ret == 0) {
295 + /* early EOF is considered an error */
296 + error_report("Input ended unexpectedly");
297 + ret = -1;
298 + goto out;
299 + }
300 }
301 if (in_ret < 0) {
302 error_report("error while reading from input image file: %s",
303 @@ -4411,9 +4455,13 @@ static int img_dd(int argc, char **argv)
304 }
305 in_pos += in_ret;
306
307 - out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
308 + if (blk2) {
309 + out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
310 + } else {
311 + out_ret = write(STDOUT_FILENO, in.buf, in_ret);
312 + }
313
314 - if (out_ret < 0) {
315 + if (out_ret != in_ret) {
316 error_report("error while writing to output image file: %s",
317 strerror(-out_ret));
318 ret = -1;
319 --
320 2.11.0
321