]> git.proxmox.com Git - pve-qemu.git/blob - debian/patches/pve/0012-PVE-Up-qemu-img-dd-add-osize-and-read-from-to-stdin-.patch
bump version to 3.0.0-1
[pve-qemu.git] / debian / patches / pve / 0012-PVE-Up-qemu-img-dd-add-osize-and-read-from-to-stdin-.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] PVE: [Up] qemu-img dd: add osize and read from/to
5 stdin/stdout
6
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.
12
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
16 of zeroes.
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.
20
21 This patch causes "if" and "of" to default to stdin and
22 stdout respectively, allowing only the "raw" format to be
23 used in these cases.
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.
31
32 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
33 ---
34 qemu-img-cmds.hx | 4 +-
35 qemu-img.c | 192 ++++++++++++++++++++++++++++++++++---------------------
36 2 files changed, 122 insertions(+), 74 deletions(-)
37
38 diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
39 index 1526f327a5..0ea4b6ffb2 100644
40 --- a/qemu-img-cmds.hx
41 +++ b/qemu-img-cmds.hx
42 @@ -56,9 +56,9 @@ STEXI
43 ETEXI
44
45 DEF("dd", img_dd,
46 - "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] if=input of=output")
47 + "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] [osize=output_size] if=input of=output")
48 STEXI
49 -@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}
50 +@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}
51 ETEXI
52
53 DEF("info", img_info,
54 diff --git a/qemu-img.c b/qemu-img.c
55 index 4438e0c2c9..f46eefce4f 100644
56 --- a/qemu-img.c
57 +++ b/qemu-img.c
58 @@ -4302,10 +4302,12 @@ out:
59 #define C_IF 04
60 #define C_OF 010
61 #define C_SKIP 020
62 +#define C_OSIZE 040
63
64 struct DdInfo {
65 unsigned int flags;
66 int64_t count;
67 + int64_t osize;
68 };
69
70 struct DdIo {
71 @@ -4384,6 +4386,20 @@ static int img_dd_skip(const char *arg,
72 return 0;
73 }
74
75 +static int img_dd_osize(const char *arg,
76 + struct DdIo *in, struct DdIo *out,
77 + struct DdInfo *dd)
78 +{
79 + dd->osize = cvtnum(arg);
80 +
81 + if (dd->osize < 0) {
82 + error_report("invalid number: '%s'", arg);
83 + return 1;
84 + }
85 +
86 + return 0;
87 +}
88 +
89 static int img_dd(int argc, char **argv)
90 {
91 int ret = 0;
92 @@ -4424,6 +4440,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 },
97 { NULL, NULL, 0 }
98 };
99 const struct option long_options[] = {
100 @@ -4502,8 +4519,13 @@ static int img_dd(int argc, char **argv)
101 arg = NULL;
102 }
103
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");
108 + ret = -1;
109 + goto out;
110 + }
111 + if (!(dd.flags & C_OF) && strcmp(out_fmt, "raw") != 0) {
112 + error_report("Output format must be raw when writing to stdout");
113 ret = -1;
114 goto out;
115 }
116 @@ -4515,85 +4537,101 @@ static int img_dd(int argc, char **argv)
117 goto out;
118 }
119
120 - blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
121 - force_share);
122 + if (dd.flags & C_IF) {
123 + blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
124 + force_share);
125
126 - if (!blk1) {
127 - ret = -1;
128 - goto out;
129 + if (!blk1) {
130 + ret = -1;
131 + goto out;
132 + }
133 }
134
135 - drv = bdrv_find_format(out_fmt);
136 - if (!drv) {
137 - error_report("Unknown file format");
138 + if (dd.flags & C_OSIZE) {
139 + size = dd.osize;
140 + } else if (dd.flags & C_IF) {
141 + size = blk_getlength(blk1);
142 + if (size < 0) {
143 + error_report("Failed to get size for '%s'", in.filename);
144 + ret = -1;
145 + goto out;
146 + }
147 + } else if (dd.flags & C_COUNT) {
148 + size = dd.count * in.bsz;
149 + } else {
150 + error_report("Output size must be known when reading from stdin");
151 ret = -1;
152 goto out;
153 }
154 - proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
155
156 - if (!proto_drv) {
157 - error_report_err(local_err);
158 - ret = -1;
159 - goto out;
160 - }
161 - if (!drv->create_opts) {
162 - error_report("Format driver '%s' does not support image creation",
163 - drv->format_name);
164 - ret = -1;
165 - goto out;
166 - }
167 - if (!proto_drv->create_opts) {
168 - error_report("Protocol driver '%s' does not support image creation",
169 - proto_drv->format_name);
170 - ret = -1;
171 - goto out;
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;
175 }
176 - create_opts = qemu_opts_append(create_opts, drv->create_opts);
177 - create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
178
179 - opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
180 + if (dd.flags & C_OF) {
181 + drv = bdrv_find_format(out_fmt);
182 + if (!drv) {
183 + error_report("Unknown file format");
184 + ret = -1;
185 + goto out;
186 + }
187 + proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
188
189 - size = blk_getlength(blk1);
190 - if (size < 0) {
191 - error_report("Failed to get size for '%s'", in.filename);
192 - ret = -1;
193 - goto out;
194 - }
195 + if (!proto_drv) {
196 + error_report_err(local_err);
197 + ret = -1;
198 + goto out;
199 + }
200 + if (!drv->create_opts) {
201 + error_report("Format driver '%s' does not support image creation",
202 + drv->format_name);
203 + ret = -1;
204 + goto out;
205 + }
206 + if (!proto_drv->create_opts) {
207 + error_report("Protocol driver '%s' does not support image creation",
208 + proto_drv->format_name);
209 + ret = -1;
210 + goto out;
211 + }
212 + create_opts = qemu_opts_append(create_opts, drv->create_opts);
213 + create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
214
215 - if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
216 - dd.count * in.bsz < size) {
217 - size = dd.count * in.bsz;
218 - }
219 + opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
220
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);
225 - } else {
226 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
227 - size - in.bsz * in.offset, &error_abort);
228 - }
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);
235 + } else {
236 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
237 + size - in.bsz * in.offset, &error_abort);
238 + }
239
240 - ret = bdrv_create(drv, out.filename, opts, &local_err);
241 - if (ret < 0) {
242 - error_reportf_err(local_err,
243 - "%s: error while creating output image: ",
244 - out.filename);
245 - ret = -1;
246 - goto out;
247 - }
248 + ret = bdrv_create(drv, out.filename, opts, &local_err);
249 + if (ret < 0) {
250 + error_reportf_err(local_err,
251 + "%s: error while creating output image: ",
252 + out.filename);
253 + ret = -1;
254 + goto out;
255 + }
256
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.
261 - */
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.
268 + */
269 + blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
270 + false, false, false);
271
272 - if (!blk2) {
273 - ret = -1;
274 - goto out;
275 + if (!blk2) {
276 + ret = -1;
277 + goto out;
278 + }
279 }
280
281 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
282 @@ -4611,11 +4649,17 @@ static int img_dd(int argc, char **argv)
283
284 for (out_pos = 0; in_pos < size; block_count++) {
285 int in_ret, out_ret;
286 -
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;
290 + if (blk1) {
291 + in_ret = blk_pread(blk1, in_pos, in.buf, in_bsz);
292 } else {
293 - in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz);
294 + in_ret = read(STDIN_FILENO, in.buf, in_bsz);
295 + if (in_ret == 0) {
296 + /* early EOF is considered an error */
297 + error_report("Input ended unexpectedly");
298 + ret = -1;
299 + goto out;
300 + }
301 }
302 if (in_ret < 0) {
303 error_report("error while reading from input image file: %s",
304 @@ -4625,9 +4669,13 @@ static int img_dd(int argc, char **argv)
305 }
306 in_pos += in_ret;
307
308 - out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
309 + if (blk2) {
310 + out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0);
311 + } else {
312 + out_ret = write(STDOUT_FILENO, in.buf, in_ret);
313 + }
314
315 - if (out_ret < 0) {
316 + if (out_ret != in_ret) {
317 error_report("error while writing to output image file: %s",
318 strerror(-out_ret));
319 ret = -1;
320 --
321 2.11.0
322