]> git.proxmox.com Git - pve-qemu.git/blob - debian/patches/pve/0010-PVE-Up-qemu-img-dd-add-osize-and-read-from-to-stdin-.patch
bed5c250b189de9f8088c3925601299c37f852b6
[pve-qemu.git] / debian / patches / pve / 0010-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: Mon, 6 Apr 2020 12:16:40 +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 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
34 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
35 ---
36 qemu-img-cmds.hx | 4 +-
37 qemu-img.c | 202 ++++++++++++++++++++++++++++++-----------------
38 2 files changed, 133 insertions(+), 73 deletions(-)
39
40 diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
41 index 1b1dab5b17..d1616c045a 100644
42 --- a/qemu-img-cmds.hx
43 +++ b/qemu-img-cmds.hx
44 @@ -58,9 +58,9 @@ SRST
45 ERST
46
47 DEF("dd", img_dd,
48 - "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] if=input of=output")
49 + "dd [--image-opts] [-U] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] [skip=blocks] [osize=output_size] if=input of=output")
50 SRST
51 -.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=BLOCK_SIZE] [count=BLOCKS] [skip=BLOCKS] if=INPUT of=OUTPUT
52 +.. option:: dd [--image-opts] [-U] [-f FMT] [-O OUTPUT_FMT] [bs=BLOCK_SIZE] [count=BLOCKS] [skip=BLOCKS] [osize=OUTPUT_SIZE] if=INPUT of=OUTPUT
53 ERST
54
55 DEF("info", img_info,
56 diff --git a/qemu-img.c b/qemu-img.c
57 index bb36f42dd2..74afcb79ef 100644
58 --- a/qemu-img.c
59 +++ b/qemu-img.c
60 @@ -4826,10 +4826,12 @@ static int img_bitmap(int argc, char **argv)
61 #define C_IF 04
62 #define C_OF 010
63 #define C_SKIP 020
64 +#define C_OSIZE 040
65
66 struct DdInfo {
67 unsigned int flags;
68 int64_t count;
69 + int64_t osize;
70 };
71
72 struct DdIo {
73 @@ -4905,6 +4907,19 @@ static int img_dd_skip(const char *arg,
74 return 0;
75 }
76
77 +static int img_dd_osize(const char *arg,
78 + struct DdIo *in, struct DdIo *out,
79 + struct DdInfo *dd)
80 +{
81 + dd->osize = cvtnum("size", arg);
82 +
83 + if (dd->osize < 0) {
84 + return 1;
85 + }
86 +
87 + return 0;
88 +}
89 +
90 static int img_dd(int argc, char **argv)
91 {
92 int ret = 0;
93 @@ -4945,6 +4960,7 @@ static int img_dd(int argc, char **argv)
94 { "if", img_dd_if, C_IF },
95 { "of", img_dd_of, C_OF },
96 { "skip", img_dd_skip, C_SKIP },
97 + { "osize", img_dd_osize, C_OSIZE },
98 { NULL, NULL, 0 }
99 };
100 const struct option long_options[] = {
101 @@ -5020,91 +5036,112 @@ static int img_dd(int argc, char **argv)
102 arg = NULL;
103 }
104
105 - if (!(dd.flags & C_IF && dd.flags & C_OF)) {
106 - error_report("Must specify both input and output files");
107 + if (!(dd.flags & C_IF) && (!fmt || strcmp(fmt, "raw") != 0)) {
108 + error_report("Input format must be raw when readin from stdin");
109 ret = -1;
110 goto out;
111 }
112 -
113 - blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
114 - force_share);
115 -
116 - if (!blk1) {
117 + if (!(dd.flags & C_OF) && strcmp(out_fmt, "raw") != 0) {
118 + error_report("Output format must be raw when writing to stdout");
119 ret = -1;
120 goto out;
121 }
122
123 - drv = bdrv_find_format(out_fmt);
124 - if (!drv) {
125 - error_report("Unknown file format");
126 - ret = -1;
127 - goto out;
128 - }
129 - proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
130 + if (dd.flags & C_IF) {
131 + blk1 = img_open(image_opts, in.filename, fmt, 0, false, false,
132 + force_share);
133
134 - if (!proto_drv) {
135 - error_report_err(local_err);
136 - ret = -1;
137 - goto out;
138 - }
139 - if (!drv->create_opts) {
140 - error_report("Format driver '%s' does not support image creation",
141 - drv->format_name);
142 - ret = -1;
143 - goto out;
144 - }
145 - if (!proto_drv->create_opts) {
146 - error_report("Protocol driver '%s' does not support image creation",
147 - proto_drv->format_name);
148 - ret = -1;
149 - goto out;
150 + if (!blk1) {
151 + ret = -1;
152 + goto out;
153 + }
154 }
155 - create_opts = qemu_opts_append(create_opts, drv->create_opts);
156 - create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
157 -
158 - opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
159
160 - size = blk_getlength(blk1);
161 - if (size < 0) {
162 - error_report("Failed to get size for '%s'", in.filename);
163 + if (dd.flags & C_OSIZE) {
164 + size = dd.osize;
165 + } else if (dd.flags & C_IF) {
166 + size = blk_getlength(blk1);
167 + if (size < 0) {
168 + error_report("Failed to get size for '%s'", in.filename);
169 + ret = -1;
170 + goto out;
171 + }
172 + } else if (dd.flags & C_COUNT) {
173 + size = dd.count * in.bsz;
174 + } else {
175 + error_report("Output size must be known when reading from stdin");
176 ret = -1;
177 goto out;
178 }
179
180 - if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
181 + if (!(dd.flags & C_OSIZE) && dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz &&
182 dd.count * in.bsz < size) {
183 size = dd.count * in.bsz;
184 }
185
186 - /* Overflow means the specified offset is beyond input image's size */
187 - if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
188 - size < in.bsz * in.offset)) {
189 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
190 - } else {
191 - qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
192 - size - in.bsz * in.offset, &error_abort);
193 - }
194 + if (dd.flags & C_OF) {
195 + drv = bdrv_find_format(out_fmt);
196 + if (!drv) {
197 + error_report("Unknown file format");
198 + ret = -1;
199 + goto out;
200 + }
201 + proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
202
203 - ret = bdrv_create(drv, out.filename, opts, &local_err);
204 - if (ret < 0) {
205 - error_reportf_err(local_err,
206 - "%s: error while creating output image: ",
207 - out.filename);
208 - ret = -1;
209 - goto out;
210 - }
211 + if (!proto_drv) {
212 + error_report_err(local_err);
213 + ret = -1;
214 + goto out;
215 + }
216 + if (!drv->create_opts) {
217 + error_report("Format driver '%s' does not support image creation",
218 + drv->format_name);
219 + ret = -1;
220 + goto out;
221 + }
222 + if (!proto_drv->create_opts) {
223 + error_report("Protocol driver '%s' does not support image creation",
224 + proto_drv->format_name);
225 + ret = -1;
226 + goto out;
227 + }
228 + create_opts = qemu_opts_append(create_opts, drv->create_opts);
229 + create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
230
231 - /* TODO, we can't honour --image-opts for the target,
232 - * since it needs to be given in a format compatible
233 - * with the bdrv_create() call above which does not
234 - * support image-opts style.
235 - */
236 - blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR,
237 - false, false, false);
238 + opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
239
240 - if (!blk2) {
241 - ret = -1;
242 - goto out;
243 + /* Overflow means the specified offset is beyond input image's size */
244 + if (dd.flags & C_OSIZE) {
245 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
246 + } else if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
247 + size < in.bsz * in.offset)) {
248 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort);
249 + } else {
250 + qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
251 + size - in.bsz * in.offset, &error_abort);
252 + }
253 +
254 + ret = bdrv_create(drv, out.filename, opts, &local_err);
255 + if (ret < 0) {
256 + error_reportf_err(local_err,
257 + "%s: error while creating output image: ",
258 + out.filename);
259 + ret = -1;
260 + goto out;
261 + }
262 +
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 + }
275 }
276
277 if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz ||
278 @@ -5121,20 +5158,43 @@ static int img_dd(int argc, char **argv)
279 in.buf = g_new(uint8_t, in.bsz);
280
281 for (out_pos = 0; in_pos < size; block_count++) {
282 + int in_ret, out_ret;
283 int bytes = (in_pos + in.bsz > size) ? size - in_pos : in.bsz;
284 -
285 - ret = blk_pread(blk1, in_pos, bytes, in.buf, 0);
286 - if (ret < 0) {
287 + if (blk1) {
288 + in_ret = blk_pread(blk1, in_pos, bytes, in.buf, 0);
289 + if (in_ret == 0) {
290 + in_ret = bytes;
291 + }
292 + } else {
293 + in_ret = read(STDIN_FILENO, in.buf, bytes);
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 - strerror(-ret));
304 + strerror(-in_ret));
305 + ret = -1;
306 goto out;
307 }
308 in_pos += bytes;
309
310 - ret = blk_pwrite(blk2, out_pos, bytes, in.buf, 0);
311 - if (ret < 0) {
312 + if (blk2) {
313 + out_ret = blk_pwrite(blk2, out_pos, in_ret, in.buf, 0);
314 + if (out_ret == 0) {
315 + out_ret = in_ret;
316 + }
317 + } else {
318 + out_ret = write(STDOUT_FILENO, in.buf, in_ret);
319 + }
320 +
321 + if (out_ret != in_ret) {
322 error_report("error while writing to output image file: %s",
323 - strerror(-ret));
324 + strerror(-out_ret));
325 + ret = -1;
326 goto out;
327 }
328 out_pos += bytes;