root / qemu-img.c @ dbfe06c6
History | View | Annotate | Download (46.8 kB)
1 | ea2384d3 | bellard | /*
|
---|---|---|---|
2 | fb43f4dd | bellard | * QEMU disk image utility
|
3 | 5fafdf24 | ths | *
|
4 | 68d0f70e | bellard | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | ea2384d3 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | ea2384d3 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | ea2384d3 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | ea2384d3 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | ea2384d3 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | ea2384d3 | bellard | * furnished to do so, subject to the following conditions:
|
12 | ea2384d3 | bellard | *
|
13 | ea2384d3 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | ea2384d3 | bellard | * all copies or substantial portions of the Software.
|
15 | ea2384d3 | bellard | *
|
16 | ea2384d3 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | ea2384d3 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | ea2384d3 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | ea2384d3 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | ea2384d3 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | ea2384d3 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | ea2384d3 | bellard | * THE SOFTWARE.
|
23 | ea2384d3 | bellard | */
|
24 | faf07963 | pbrook | #include "qemu-common.h" |
25 | 9ea2ea71 | Kevin Wolf | #include "qemu-option.h" |
26 | 53f76e58 | Kevin Wolf | #include "qemu-error.h" |
27 | f7b4a940 | aliguori | #include "osdep.h" |
28 | dc786bc9 | Jes Sorensen | #include "sysemu.h" |
29 | ec36ba14 | ths | #include "block_int.h" |
30 | 9230eaf6 | aliguori | #include <stdio.h> |
31 | ea2384d3 | bellard | |
32 | e8445331 | bellard | #ifdef _WIN32
|
33 | e8445331 | bellard | #include <windows.h> |
34 | e8445331 | bellard | #endif
|
35 | e8445331 | bellard | |
36 | c227f099 | Anthony Liguori | typedef struct img_cmd_t { |
37 | 153859be | Stuart Brady | const char *name; |
38 | 153859be | Stuart Brady | int (*handler)(int argc, char **argv); |
39 | c227f099 | Anthony Liguori | } img_cmd_t; |
40 | 153859be | Stuart Brady | |
41 | 137519ce | aurel32 | /* Default to cache=writeback as data integrity is not important for qemu-tcg. */
|
42 | adfe078e | Stefan Hajnoczi | #define BDRV_O_FLAGS BDRV_O_CACHE_WB
|
43 | 661a0f71 | Federico Simoncelli | #define BDRV_DEFAULT_CACHE "writeback" |
44 | 137519ce | aurel32 | |
45 | ea2384d3 | bellard | static void format_print(void *opaque, const char *name) |
46 | ea2384d3 | bellard | { |
47 | ea2384d3 | bellard | printf(" %s", name);
|
48 | ea2384d3 | bellard | } |
49 | ea2384d3 | bellard | |
50 | d2c639d6 | blueswir1 | /* Please keep in synch with qemu-img.texi */
|
51 | 3f379ab1 | pbrook | static void help(void) |
52 | ea2384d3 | bellard | { |
53 | e00291c0 | Paolo Bonzini | const char *help_msg = |
54 | e00291c0 | Paolo Bonzini | "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n" |
55 | 3f020d70 | malc | "usage: qemu-img command [command options]\n"
|
56 | 3f020d70 | malc | "QEMU disk image utility\n"
|
57 | 3f020d70 | malc | "\n"
|
58 | 3f020d70 | malc | "Command syntax:\n"
|
59 | 153859be | Stuart Brady | #define DEF(option, callback, arg_string) \
|
60 | 153859be | Stuart Brady | " " arg_string "\n" |
61 | 153859be | Stuart Brady | #include "qemu-img-cmds.h" |
62 | 153859be | Stuart Brady | #undef DEF
|
63 | 153859be | Stuart Brady | #undef GEN_DOCS
|
64 | 3f020d70 | malc | "\n"
|
65 | 3f020d70 | malc | "Command parameters:\n"
|
66 | 3f020d70 | malc | " 'filename' is a disk image filename\n"
|
67 | 3f020d70 | malc | " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
|
68 | 661a0f71 | Federico Simoncelli | " 'cache' is the cache mode used to write the output disk image, the valid\n"
|
69 | 92196b2f | Stefan Hajnoczi | " options are: 'none', 'writeback' (default), 'writethrough', 'directsync'\n"
|
70 | 92196b2f | Stefan Hajnoczi | " and 'unsafe'\n"
|
71 | 3f020d70 | malc | " 'size' is the disk image size in bytes. Optional suffixes\n"
|
72 | 3f020d70 | malc | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
|
73 | 3f020d70 | malc | " and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
|
74 | 3f020d70 | malc | " 'output_filename' is the destination disk image filename\n"
|
75 | 3f020d70 | malc | " 'output_fmt' is the destination format\n"
|
76 | 3f020d70 | malc | " 'options' is a comma separated list of format specific options in a\n"
|
77 | 3f020d70 | malc | " name=value format. Use -o ? for an overview of the options supported by the\n"
|
78 | 3f020d70 | malc | " used format\n"
|
79 | 3f020d70 | malc | " '-c' indicates that target image must be compressed (qcow format only)\n"
|
80 | 3f020d70 | malc | " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
|
81 | 3f020d70 | malc | " match exactly. The image doesn't need a working backing file before\n"
|
82 | 3f020d70 | malc | " rebasing in this case (useful for renaming the backing file)\n"
|
83 | 3f020d70 | malc | " '-h' with or without a command shows this help and lists the supported formats\n"
|
84 | 6b837bc4 | Jes Sorensen | " '-p' show progress of command (only certain commands)\n"
|
85 | a22f123c | Kevin Wolf | " '-S' indicates the consecutive number of bytes that must contain only zeros\n"
|
86 | a22f123c | Kevin Wolf | " for qemu-img to create a sparse image during conversion\n"
|
87 | 3f020d70 | malc | "\n"
|
88 | 3f020d70 | malc | "Parameters to snapshot subcommand:\n"
|
89 | 3f020d70 | malc | " 'snapshot' is the name of the snapshot to create, apply or delete\n"
|
90 | 3f020d70 | malc | " '-a' applies a snapshot (revert disk to saved state)\n"
|
91 | 3f020d70 | malc | " '-c' creates a snapshot\n"
|
92 | 3f020d70 | malc | " '-d' deletes a snapshot\n"
|
93 | e00291c0 | Paolo Bonzini | " '-l' lists all snapshots in the given image\n";
|
94 | e00291c0 | Paolo Bonzini | |
95 | e00291c0 | Paolo Bonzini | printf("%s\nSupported formats:", help_msg);
|
96 | ea2384d3 | bellard | bdrv_iterate_format(format_print, NULL);
|
97 | ea2384d3 | bellard | printf("\n");
|
98 | ea2384d3 | bellard | exit(1);
|
99 | ea2384d3 | bellard | } |
100 | ea2384d3 | bellard | |
101 | ea2384d3 | bellard | #if defined(WIN32)
|
102 | ea2384d3 | bellard | /* XXX: put correct support for win32 */
|
103 | ea2384d3 | bellard | static int read_password(char *buf, int buf_size) |
104 | ea2384d3 | bellard | { |
105 | ea2384d3 | bellard | int c, i;
|
106 | ea2384d3 | bellard | printf("Password: ");
|
107 | ea2384d3 | bellard | fflush(stdout); |
108 | ea2384d3 | bellard | i = 0;
|
109 | ea2384d3 | bellard | for(;;) {
|
110 | ea2384d3 | bellard | c = getchar(); |
111 | ea2384d3 | bellard | if (c == '\n') |
112 | ea2384d3 | bellard | break;
|
113 | ea2384d3 | bellard | if (i < (buf_size - 1)) |
114 | ea2384d3 | bellard | buf[i++] = c; |
115 | ea2384d3 | bellard | } |
116 | ea2384d3 | bellard | buf[i] = '\0';
|
117 | ea2384d3 | bellard | return 0; |
118 | ea2384d3 | bellard | } |
119 | ea2384d3 | bellard | |
120 | ea2384d3 | bellard | #else
|
121 | ea2384d3 | bellard | |
122 | ea2384d3 | bellard | #include <termios.h> |
123 | ea2384d3 | bellard | |
124 | ea2384d3 | bellard | static struct termios oldtty; |
125 | ea2384d3 | bellard | |
126 | ea2384d3 | bellard | static void term_exit(void) |
127 | ea2384d3 | bellard | { |
128 | ea2384d3 | bellard | tcsetattr (0, TCSANOW, &oldtty);
|
129 | ea2384d3 | bellard | } |
130 | ea2384d3 | bellard | |
131 | ea2384d3 | bellard | static void term_init(void) |
132 | ea2384d3 | bellard | { |
133 | ea2384d3 | bellard | struct termios tty;
|
134 | ea2384d3 | bellard | |
135 | ea2384d3 | bellard | tcgetattr (0, &tty);
|
136 | ea2384d3 | bellard | oldtty = tty; |
137 | ea2384d3 | bellard | |
138 | ea2384d3 | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
139 | ea2384d3 | bellard | |INLCR|IGNCR|ICRNL|IXON); |
140 | ea2384d3 | bellard | tty.c_oflag |= OPOST; |
141 | ea2384d3 | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
142 | ea2384d3 | bellard | tty.c_cflag &= ~(CSIZE|PARENB); |
143 | ea2384d3 | bellard | tty.c_cflag |= CS8; |
144 | ea2384d3 | bellard | tty.c_cc[VMIN] = 1;
|
145 | ea2384d3 | bellard | tty.c_cc[VTIME] = 0;
|
146 | 3b46e624 | ths | |
147 | ea2384d3 | bellard | tcsetattr (0, TCSANOW, &tty);
|
148 | ea2384d3 | bellard | |
149 | ea2384d3 | bellard | atexit(term_exit); |
150 | ea2384d3 | bellard | } |
151 | ea2384d3 | bellard | |
152 | 3f379ab1 | pbrook | static int read_password(char *buf, int buf_size) |
153 | ea2384d3 | bellard | { |
154 | ea2384d3 | bellard | uint8_t ch; |
155 | ea2384d3 | bellard | int i, ret;
|
156 | ea2384d3 | bellard | |
157 | ea2384d3 | bellard | printf("password: ");
|
158 | ea2384d3 | bellard | fflush(stdout); |
159 | ea2384d3 | bellard | term_init(); |
160 | ea2384d3 | bellard | i = 0;
|
161 | ea2384d3 | bellard | for(;;) {
|
162 | ea2384d3 | bellard | ret = read(0, &ch, 1); |
163 | ea2384d3 | bellard | if (ret == -1) { |
164 | ea2384d3 | bellard | if (errno == EAGAIN || errno == EINTR) {
|
165 | ea2384d3 | bellard | continue;
|
166 | ea2384d3 | bellard | } else {
|
167 | ea2384d3 | bellard | ret = -1;
|
168 | ea2384d3 | bellard | break;
|
169 | ea2384d3 | bellard | } |
170 | ea2384d3 | bellard | } else if (ret == 0) { |
171 | ea2384d3 | bellard | ret = -1;
|
172 | ea2384d3 | bellard | break;
|
173 | ea2384d3 | bellard | } else {
|
174 | ea2384d3 | bellard | if (ch == '\r') { |
175 | ea2384d3 | bellard | ret = 0;
|
176 | ea2384d3 | bellard | break;
|
177 | ea2384d3 | bellard | } |
178 | ea2384d3 | bellard | if (i < (buf_size - 1)) |
179 | ea2384d3 | bellard | buf[i++] = ch; |
180 | ea2384d3 | bellard | } |
181 | ea2384d3 | bellard | } |
182 | ea2384d3 | bellard | term_exit(); |
183 | ea2384d3 | bellard | buf[i] = '\0';
|
184 | ea2384d3 | bellard | printf("\n");
|
185 | ea2384d3 | bellard | return ret;
|
186 | ea2384d3 | bellard | } |
187 | ea2384d3 | bellard | #endif
|
188 | ea2384d3 | bellard | |
189 | 4ac8aacd | Jes Sorensen | static int print_block_option_help(const char *filename, const char *fmt) |
190 | 4ac8aacd | Jes Sorensen | { |
191 | 4ac8aacd | Jes Sorensen | BlockDriver *drv, *proto_drv; |
192 | 4ac8aacd | Jes Sorensen | QEMUOptionParameter *create_options = NULL;
|
193 | 4ac8aacd | Jes Sorensen | |
194 | 4ac8aacd | Jes Sorensen | /* Find driver and parse its options */
|
195 | 4ac8aacd | Jes Sorensen | drv = bdrv_find_format(fmt); |
196 | 4ac8aacd | Jes Sorensen | if (!drv) {
|
197 | 15654a6d | Jes Sorensen | error_report("Unknown file format '%s'", fmt);
|
198 | 4ac8aacd | Jes Sorensen | return 1; |
199 | 4ac8aacd | Jes Sorensen | } |
200 | 4ac8aacd | Jes Sorensen | |
201 | 4ac8aacd | Jes Sorensen | proto_drv = bdrv_find_protocol(filename); |
202 | 4ac8aacd | Jes Sorensen | if (!proto_drv) {
|
203 | 15654a6d | Jes Sorensen | error_report("Unknown protocol '%s'", filename);
|
204 | 4ac8aacd | Jes Sorensen | return 1; |
205 | 4ac8aacd | Jes Sorensen | } |
206 | 4ac8aacd | Jes Sorensen | |
207 | 4ac8aacd | Jes Sorensen | create_options = append_option_parameters(create_options, |
208 | 4ac8aacd | Jes Sorensen | drv->create_options); |
209 | 4ac8aacd | Jes Sorensen | create_options = append_option_parameters(create_options, |
210 | 4ac8aacd | Jes Sorensen | proto_drv->create_options); |
211 | 4ac8aacd | Jes Sorensen | print_option_help(create_options); |
212 | 4ac8aacd | Jes Sorensen | free_option_parameters(create_options); |
213 | 4ac8aacd | Jes Sorensen | return 0; |
214 | 4ac8aacd | Jes Sorensen | } |
215 | 4ac8aacd | Jes Sorensen | |
216 | 75c23805 | bellard | static BlockDriverState *bdrv_new_open(const char *filename, |
217 | 9bc378c1 | Sheng Yang | const char *fmt, |
218 | f163d073 | Stefan Hajnoczi | int flags)
|
219 | 75c23805 | bellard | { |
220 | 75c23805 | bellard | BlockDriverState *bs; |
221 | 75c23805 | bellard | BlockDriver *drv; |
222 | 75c23805 | bellard | char password[256]; |
223 | b9eaf9ec | Kevin Wolf | int ret;
|
224 | 75c23805 | bellard | |
225 | b9eaf9ec | Kevin Wolf | bs = bdrv_new("image");
|
226 | ad717139 | Kevin Wolf | |
227 | 75c23805 | bellard | if (fmt) {
|
228 | 75c23805 | bellard | drv = bdrv_find_format(fmt); |
229 | c2abccec | MORITA Kazutaka | if (!drv) {
|
230 | 15654a6d | Jes Sorensen | error_report("Unknown file format '%s'", fmt);
|
231 | c2abccec | MORITA Kazutaka | goto fail;
|
232 | c2abccec | MORITA Kazutaka | } |
233 | 75c23805 | bellard | } else {
|
234 | 75c23805 | bellard | drv = NULL;
|
235 | 75c23805 | bellard | } |
236 | b9eaf9ec | Kevin Wolf | |
237 | b9eaf9ec | Kevin Wolf | ret = bdrv_open(bs, filename, flags, drv); |
238 | b9eaf9ec | Kevin Wolf | if (ret < 0) { |
239 | b9eaf9ec | Kevin Wolf | error_report("Could not open '%s': %s", filename, strerror(-ret));
|
240 | c2abccec | MORITA Kazutaka | goto fail;
|
241 | 75c23805 | bellard | } |
242 | b9eaf9ec | Kevin Wolf | |
243 | 75c23805 | bellard | if (bdrv_is_encrypted(bs)) {
|
244 | 75c23805 | bellard | printf("Disk image '%s' is encrypted.\n", filename);
|
245 | c2abccec | MORITA Kazutaka | if (read_password(password, sizeof(password)) < 0) { |
246 | 15654a6d | Jes Sorensen | error_report("No password given");
|
247 | c2abccec | MORITA Kazutaka | goto fail;
|
248 | c2abccec | MORITA Kazutaka | } |
249 | c2abccec | MORITA Kazutaka | if (bdrv_set_key(bs, password) < 0) { |
250 | 15654a6d | Jes Sorensen | error_report("invalid password");
|
251 | c2abccec | MORITA Kazutaka | goto fail;
|
252 | c2abccec | MORITA Kazutaka | } |
253 | 75c23805 | bellard | } |
254 | 75c23805 | bellard | return bs;
|
255 | c2abccec | MORITA Kazutaka | fail:
|
256 | c2abccec | MORITA Kazutaka | if (bs) {
|
257 | c2abccec | MORITA Kazutaka | bdrv_delete(bs); |
258 | c2abccec | MORITA Kazutaka | } |
259 | c2abccec | MORITA Kazutaka | return NULL; |
260 | 75c23805 | bellard | } |
261 | 75c23805 | bellard | |
262 | c2abccec | MORITA Kazutaka | static int add_old_style_options(const char *fmt, QEMUOptionParameter *list, |
263 | eec77d9e | Jes Sorensen | const char *base_filename, |
264 | eec77d9e | Jes Sorensen | const char *base_fmt) |
265 | efa84d43 | Kevin Wolf | { |
266 | efa84d43 | Kevin Wolf | if (base_filename) {
|
267 | efa84d43 | Kevin Wolf | if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
|
268 | 15654a6d | Jes Sorensen | error_report("Backing file not supported for file format '%s'",
|
269 | 15654a6d | Jes Sorensen | fmt); |
270 | c2abccec | MORITA Kazutaka | return -1; |
271 | efa84d43 | Kevin Wolf | } |
272 | efa84d43 | Kevin Wolf | } |
273 | efa84d43 | Kevin Wolf | if (base_fmt) {
|
274 | efa84d43 | Kevin Wolf | if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
|
275 | 15654a6d | Jes Sorensen | error_report("Backing file format not supported for file "
|
276 | 15654a6d | Jes Sorensen | "format '%s'", fmt);
|
277 | c2abccec | MORITA Kazutaka | return -1; |
278 | efa84d43 | Kevin Wolf | } |
279 | efa84d43 | Kevin Wolf | } |
280 | c2abccec | MORITA Kazutaka | return 0; |
281 | efa84d43 | Kevin Wolf | } |
282 | efa84d43 | Kevin Wolf | |
283 | ea2384d3 | bellard | static int img_create(int argc, char **argv) |
284 | ea2384d3 | bellard | { |
285 | eec77d9e | Jes Sorensen | int c, ret = 0; |
286 | 1da7cfbd | Jes Sorensen | uint64_t img_size = -1;
|
287 | ea2384d3 | bellard | const char *fmt = "raw"; |
288 | 9230eaf6 | aliguori | const char *base_fmt = NULL; |
289 | ea2384d3 | bellard | const char *filename; |
290 | ea2384d3 | bellard | const char *base_filename = NULL; |
291 | 9ea2ea71 | Kevin Wolf | char *options = NULL; |
292 | 3b46e624 | ths | |
293 | ea2384d3 | bellard | for(;;) {
|
294 | 9ea2ea71 | Kevin Wolf | c = getopt(argc, argv, "F:b:f:he6o:");
|
295 | b8fb60da | Jes Sorensen | if (c == -1) { |
296 | ea2384d3 | bellard | break;
|
297 | b8fb60da | Jes Sorensen | } |
298 | ea2384d3 | bellard | switch(c) {
|
299 | ef87394c | Jes Sorensen | case '?': |
300 | ea2384d3 | bellard | case 'h': |
301 | ea2384d3 | bellard | help(); |
302 | ea2384d3 | bellard | break;
|
303 | 9230eaf6 | aliguori | case 'F': |
304 | 9230eaf6 | aliguori | base_fmt = optarg; |
305 | 9230eaf6 | aliguori | break;
|
306 | ea2384d3 | bellard | case 'b': |
307 | ea2384d3 | bellard | base_filename = optarg; |
308 | ea2384d3 | bellard | break;
|
309 | ea2384d3 | bellard | case 'f': |
310 | ea2384d3 | bellard | fmt = optarg; |
311 | ea2384d3 | bellard | break;
|
312 | ea2384d3 | bellard | case 'e': |
313 | 9d42e15d | Markus Armbruster | error_report("option -e is deprecated, please use \'-o "
|
314 | eec77d9e | Jes Sorensen | "encryption\' instead!");
|
315 | eec77d9e | Jes Sorensen | return 1; |
316 | d8871c5a | ths | case '6': |
317 | 9d42e15d | Markus Armbruster | error_report("option -6 is deprecated, please use \'-o "
|
318 | eec77d9e | Jes Sorensen | "compat6\' instead!");
|
319 | eec77d9e | Jes Sorensen | return 1; |
320 | 9ea2ea71 | Kevin Wolf | case 'o': |
321 | 9ea2ea71 | Kevin Wolf | options = optarg; |
322 | 9ea2ea71 | Kevin Wolf | break;
|
323 | ea2384d3 | bellard | } |
324 | ea2384d3 | bellard | } |
325 | 9230eaf6 | aliguori | |
326 | b50cbabc | MORITA Kazutaka | /* Get the filename */
|
327 | b8fb60da | Jes Sorensen | if (optind >= argc) {
|
328 | b50cbabc | MORITA Kazutaka | help(); |
329 | b8fb60da | Jes Sorensen | } |
330 | b50cbabc | MORITA Kazutaka | filename = argv[optind++]; |
331 | b50cbabc | MORITA Kazutaka | |
332 | 1da7cfbd | Jes Sorensen | /* Get image size, if specified */
|
333 | 1da7cfbd | Jes Sorensen | if (optind < argc) {
|
334 | 70b4f4bb | Jes Sorensen | int64_t sval; |
335 | e36b3695 | Markus Armbruster | char *end;
|
336 | e36b3695 | Markus Armbruster | sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B); |
337 | e36b3695 | Markus Armbruster | if (sval < 0 || *end) { |
338 | 15654a6d | Jes Sorensen | error_report("Invalid image size specified! You may use k, M, G or "
|
339 | 1da7cfbd | Jes Sorensen | "T suffixes for ");
|
340 | 15654a6d | Jes Sorensen | error_report("kilobytes, megabytes, gigabytes and terabytes.");
|
341 | 1da7cfbd | Jes Sorensen | ret = -1;
|
342 | 1da7cfbd | Jes Sorensen | goto out;
|
343 | 1da7cfbd | Jes Sorensen | } |
344 | 1da7cfbd | Jes Sorensen | img_size = (uint64_t)sval; |
345 | 1da7cfbd | Jes Sorensen | } |
346 | 1da7cfbd | Jes Sorensen | |
347 | 4ac8aacd | Jes Sorensen | if (options && !strcmp(options, "?")) { |
348 | 4ac8aacd | Jes Sorensen | ret = print_block_option_help(filename, fmt); |
349 | 4ac8aacd | Jes Sorensen | goto out;
|
350 | 4ac8aacd | Jes Sorensen | } |
351 | 4ac8aacd | Jes Sorensen | |
352 | f88e1a42 | Jes Sorensen | ret = bdrv_img_create(filename, fmt, base_filename, base_fmt, |
353 | f88e1a42 | Jes Sorensen | options, img_size, BDRV_O_FLAGS); |
354 | c2abccec | MORITA Kazutaka | out:
|
355 | c2abccec | MORITA Kazutaka | if (ret) {
|
356 | c2abccec | MORITA Kazutaka | return 1; |
357 | c2abccec | MORITA Kazutaka | } |
358 | ea2384d3 | bellard | return 0; |
359 | ea2384d3 | bellard | } |
360 | ea2384d3 | bellard | |
361 | e076f338 | Kevin Wolf | /*
|
362 | e076f338 | Kevin Wolf | * Checks an image for consistency. Exit codes:
|
363 | e076f338 | Kevin Wolf | *
|
364 | e076f338 | Kevin Wolf | * 0 - Check completed, image is good
|
365 | e076f338 | Kevin Wolf | * 1 - Check not completed because of internal errors
|
366 | e076f338 | Kevin Wolf | * 2 - Check completed, image is corrupted
|
367 | e076f338 | Kevin Wolf | * 3 - Check completed, image has leaked clusters, but is good otherwise
|
368 | e076f338 | Kevin Wolf | */
|
369 | 1585969c | aliguori | static int img_check(int argc, char **argv) |
370 | 1585969c | aliguori | { |
371 | 1585969c | aliguori | int c, ret;
|
372 | 1585969c | aliguori | const char *filename, *fmt; |
373 | 1585969c | aliguori | BlockDriverState *bs; |
374 | e076f338 | Kevin Wolf | BdrvCheckResult result; |
375 | 1585969c | aliguori | |
376 | 1585969c | aliguori | fmt = NULL;
|
377 | 1585969c | aliguori | for(;;) {
|
378 | 1585969c | aliguori | c = getopt(argc, argv, "f:h");
|
379 | b8fb60da | Jes Sorensen | if (c == -1) { |
380 | 1585969c | aliguori | break;
|
381 | b8fb60da | Jes Sorensen | } |
382 | 1585969c | aliguori | switch(c) {
|
383 | ef87394c | Jes Sorensen | case '?': |
384 | 1585969c | aliguori | case 'h': |
385 | 1585969c | aliguori | help(); |
386 | 1585969c | aliguori | break;
|
387 | 1585969c | aliguori | case 'f': |
388 | 1585969c | aliguori | fmt = optarg; |
389 | 1585969c | aliguori | break;
|
390 | 1585969c | aliguori | } |
391 | 1585969c | aliguori | } |
392 | b8fb60da | Jes Sorensen | if (optind >= argc) {
|
393 | 1585969c | aliguori | help(); |
394 | b8fb60da | Jes Sorensen | } |
395 | 1585969c | aliguori | filename = argv[optind++]; |
396 | 1585969c | aliguori | |
397 | adfe078e | Stefan Hajnoczi | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS); |
398 | c2abccec | MORITA Kazutaka | if (!bs) {
|
399 | c2abccec | MORITA Kazutaka | return 1; |
400 | c2abccec | MORITA Kazutaka | } |
401 | e076f338 | Kevin Wolf | ret = bdrv_check(bs, &result); |
402 | e076f338 | Kevin Wolf | |
403 | e076f338 | Kevin Wolf | if (ret == -ENOTSUP) {
|
404 | 15654a6d | Jes Sorensen | error_report("This image format does not support checks");
|
405 | e076f338 | Kevin Wolf | bdrv_delete(bs); |
406 | e076f338 | Kevin Wolf | return 1; |
407 | e076f338 | Kevin Wolf | } |
408 | e076f338 | Kevin Wolf | |
409 | e076f338 | Kevin Wolf | if (!(result.corruptions || result.leaks || result.check_errors)) {
|
410 | e076f338 | Kevin Wolf | printf("No errors were found on the image.\n");
|
411 | e076f338 | Kevin Wolf | } else {
|
412 | e076f338 | Kevin Wolf | if (result.corruptions) {
|
413 | e076f338 | Kevin Wolf | printf("\n%d errors were found on the image.\n"
|
414 | e076f338 | Kevin Wolf | "Data may be corrupted, or further writes to the image "
|
415 | e076f338 | Kevin Wolf | "may corrupt it.\n",
|
416 | e076f338 | Kevin Wolf | result.corruptions); |
417 | e076f338 | Kevin Wolf | } |
418 | e076f338 | Kevin Wolf | |
419 | e076f338 | Kevin Wolf | if (result.leaks) {
|
420 | e076f338 | Kevin Wolf | printf("\n%d leaked clusters were found on the image.\n"
|
421 | e076f338 | Kevin Wolf | "This means waste of disk space, but no harm to data.\n",
|
422 | e076f338 | Kevin Wolf | result.leaks); |
423 | e076f338 | Kevin Wolf | } |
424 | e076f338 | Kevin Wolf | |
425 | e076f338 | Kevin Wolf | if (result.check_errors) {
|
426 | e076f338 | Kevin Wolf | printf("\n%d internal errors have occurred during the check.\n",
|
427 | e076f338 | Kevin Wolf | result.check_errors); |
428 | 1585969c | aliguori | } |
429 | 1585969c | aliguori | } |
430 | 1585969c | aliguori | |
431 | 1585969c | aliguori | bdrv_delete(bs); |
432 | e076f338 | Kevin Wolf | |
433 | e076f338 | Kevin Wolf | if (ret < 0 || result.check_errors) { |
434 | e076f338 | Kevin Wolf | printf("\nAn error has occurred during the check: %s\n"
|
435 | e076f338 | Kevin Wolf | "The check is not complete and may have missed error.\n",
|
436 | e076f338 | Kevin Wolf | strerror(-ret)); |
437 | c2abccec | MORITA Kazutaka | return 1; |
438 | c2abccec | MORITA Kazutaka | } |
439 | e076f338 | Kevin Wolf | |
440 | e076f338 | Kevin Wolf | if (result.corruptions) {
|
441 | e076f338 | Kevin Wolf | return 2; |
442 | e076f338 | Kevin Wolf | } else if (result.leaks) { |
443 | e076f338 | Kevin Wolf | return 3; |
444 | e076f338 | Kevin Wolf | } else {
|
445 | e076f338 | Kevin Wolf | return 0; |
446 | e076f338 | Kevin Wolf | } |
447 | 1585969c | aliguori | } |
448 | 1585969c | aliguori | |
449 | ea2384d3 | bellard | static int img_commit(int argc, char **argv) |
450 | ea2384d3 | bellard | { |
451 | 661a0f71 | Federico Simoncelli | int c, ret, flags;
|
452 | 661a0f71 | Federico Simoncelli | const char *filename, *fmt, *cache; |
453 | ea2384d3 | bellard | BlockDriverState *bs; |
454 | ea2384d3 | bellard | |
455 | ea2384d3 | bellard | fmt = NULL;
|
456 | 661a0f71 | Federico Simoncelli | cache = BDRV_DEFAULT_CACHE; |
457 | ea2384d3 | bellard | for(;;) {
|
458 | 661a0f71 | Federico Simoncelli | c = getopt(argc, argv, "f:ht:");
|
459 | b8fb60da | Jes Sorensen | if (c == -1) { |
460 | ea2384d3 | bellard | break;
|
461 | b8fb60da | Jes Sorensen | } |
462 | ea2384d3 | bellard | switch(c) {
|
463 | ef87394c | Jes Sorensen | case '?': |
464 | ea2384d3 | bellard | case 'h': |
465 | ea2384d3 | bellard | help(); |
466 | ea2384d3 | bellard | break;
|
467 | ea2384d3 | bellard | case 'f': |
468 | ea2384d3 | bellard | fmt = optarg; |
469 | ea2384d3 | bellard | break;
|
470 | 661a0f71 | Federico Simoncelli | case 't': |
471 | 661a0f71 | Federico Simoncelli | cache = optarg; |
472 | 661a0f71 | Federico Simoncelli | break;
|
473 | ea2384d3 | bellard | } |
474 | ea2384d3 | bellard | } |
475 | b8fb60da | Jes Sorensen | if (optind >= argc) {
|
476 | ea2384d3 | bellard | help(); |
477 | b8fb60da | Jes Sorensen | } |
478 | ea2384d3 | bellard | filename = argv[optind++]; |
479 | ea2384d3 | bellard | |
480 | 661a0f71 | Federico Simoncelli | flags = BDRV_O_RDWR; |
481 | c3993cdc | Stefan Hajnoczi | ret = bdrv_parse_cache_flags(cache, &flags); |
482 | 661a0f71 | Federico Simoncelli | if (ret < 0) { |
483 | 661a0f71 | Federico Simoncelli | error_report("Invalid cache option: %s", cache);
|
484 | 661a0f71 | Federico Simoncelli | return -1; |
485 | 661a0f71 | Federico Simoncelli | } |
486 | 661a0f71 | Federico Simoncelli | |
487 | 661a0f71 | Federico Simoncelli | bs = bdrv_new_open(filename, fmt, flags); |
488 | c2abccec | MORITA Kazutaka | if (!bs) {
|
489 | c2abccec | MORITA Kazutaka | return 1; |
490 | c2abccec | MORITA Kazutaka | } |
491 | ea2384d3 | bellard | ret = bdrv_commit(bs); |
492 | ea2384d3 | bellard | switch(ret) {
|
493 | ea2384d3 | bellard | case 0: |
494 | ea2384d3 | bellard | printf("Image committed.\n");
|
495 | ea2384d3 | bellard | break;
|
496 | ea2384d3 | bellard | case -ENOENT:
|
497 | 15654a6d | Jes Sorensen | error_report("No disk inserted");
|
498 | ea2384d3 | bellard | break;
|
499 | ea2384d3 | bellard | case -EACCES:
|
500 | 15654a6d | Jes Sorensen | error_report("Image is read-only");
|
501 | ea2384d3 | bellard | break;
|
502 | ea2384d3 | bellard | case -ENOTSUP:
|
503 | 15654a6d | Jes Sorensen | error_report("Image is already committed");
|
504 | ea2384d3 | bellard | break;
|
505 | ea2384d3 | bellard | default:
|
506 | 15654a6d | Jes Sorensen | error_report("Error while committing image");
|
507 | ea2384d3 | bellard | break;
|
508 | ea2384d3 | bellard | } |
509 | ea2384d3 | bellard | |
510 | ea2384d3 | bellard | bdrv_delete(bs); |
511 | c2abccec | MORITA Kazutaka | if (ret) {
|
512 | c2abccec | MORITA Kazutaka | return 1; |
513 | c2abccec | MORITA Kazutaka | } |
514 | ea2384d3 | bellard | return 0; |
515 | ea2384d3 | bellard | } |
516 | ea2384d3 | bellard | |
517 | f6a00aa1 | Dmitry Konishchev | /*
|
518 | f6a00aa1 | Dmitry Konishchev | * Checks whether the sector is not a zero sector.
|
519 | f6a00aa1 | Dmitry Konishchev | *
|
520 | f6a00aa1 | Dmitry Konishchev | * Attention! The len must be a multiple of 4 * sizeof(long) due to
|
521 | f6a00aa1 | Dmitry Konishchev | * restriction of optimizations in this function.
|
522 | f6a00aa1 | Dmitry Konishchev | */
|
523 | ea2384d3 | bellard | static int is_not_zero(const uint8_t *sector, int len) |
524 | ea2384d3 | bellard | { |
525 | f6a00aa1 | Dmitry Konishchev | /*
|
526 | f6a00aa1 | Dmitry Konishchev | * Use long as the biggest available internal data type that fits into the
|
527 | f6a00aa1 | Dmitry Konishchev | * CPU register and unroll the loop to smooth out the effect of memory
|
528 | f6a00aa1 | Dmitry Konishchev | * latency.
|
529 | f6a00aa1 | Dmitry Konishchev | */
|
530 | f6a00aa1 | Dmitry Konishchev | |
531 | ea2384d3 | bellard | int i;
|
532 | f6a00aa1 | Dmitry Konishchev | long d0, d1, d2, d3;
|
533 | f6a00aa1 | Dmitry Konishchev | const long * const data = (const long *) sector; |
534 | f6a00aa1 | Dmitry Konishchev | |
535 | f6a00aa1 | Dmitry Konishchev | len /= sizeof(long); |
536 | f6a00aa1 | Dmitry Konishchev | |
537 | f6a00aa1 | Dmitry Konishchev | for(i = 0; i < len; i += 4) { |
538 | f6a00aa1 | Dmitry Konishchev | d0 = data[i + 0];
|
539 | f6a00aa1 | Dmitry Konishchev | d1 = data[i + 1];
|
540 | f6a00aa1 | Dmitry Konishchev | d2 = data[i + 2];
|
541 | f6a00aa1 | Dmitry Konishchev | d3 = data[i + 3];
|
542 | f6a00aa1 | Dmitry Konishchev | |
543 | f6a00aa1 | Dmitry Konishchev | if (d0 || d1 || d2 || d3) {
|
544 | ea2384d3 | bellard | return 1; |
545 | f6a00aa1 | Dmitry Konishchev | } |
546 | ea2384d3 | bellard | } |
547 | f6a00aa1 | Dmitry Konishchev | |
548 | ea2384d3 | bellard | return 0; |
549 | ea2384d3 | bellard | } |
550 | ea2384d3 | bellard | |
551 | f58c7b35 | ths | /*
|
552 | f58c7b35 | ths | * Returns true iff the first sector pointed to by 'buf' contains at least
|
553 | f58c7b35 | ths | * a non-NUL byte.
|
554 | f58c7b35 | ths | *
|
555 | f58c7b35 | ths | * 'pnum' is set to the number of sectors (including and immediately following
|
556 | f58c7b35 | ths | * the first one) that are known to be in the same allocated/unallocated state.
|
557 | f58c7b35 | ths | */
|
558 | ea2384d3 | bellard | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
559 | ea2384d3 | bellard | { |
560 | ea2384d3 | bellard | int v, i;
|
561 | ea2384d3 | bellard | |
562 | ea2384d3 | bellard | if (n <= 0) { |
563 | ea2384d3 | bellard | *pnum = 0;
|
564 | ea2384d3 | bellard | return 0; |
565 | ea2384d3 | bellard | } |
566 | ea2384d3 | bellard | v = is_not_zero(buf, 512);
|
567 | ea2384d3 | bellard | for(i = 1; i < n; i++) { |
568 | ea2384d3 | bellard | buf += 512;
|
569 | ea2384d3 | bellard | if (v != is_not_zero(buf, 512)) |
570 | ea2384d3 | bellard | break;
|
571 | ea2384d3 | bellard | } |
572 | ea2384d3 | bellard | *pnum = i; |
573 | ea2384d3 | bellard | return v;
|
574 | ea2384d3 | bellard | } |
575 | ea2384d3 | bellard | |
576 | 3e85c6fd | Kevin Wolf | /*
|
577 | a22f123c | Kevin Wolf | * Like is_allocated_sectors, but if the buffer starts with a used sector,
|
578 | a22f123c | Kevin Wolf | * up to 'min' consecutive sectors containing zeros are ignored. This avoids
|
579 | a22f123c | Kevin Wolf | * breaking up write requests for only small sparse areas.
|
580 | a22f123c | Kevin Wolf | */
|
581 | a22f123c | Kevin Wolf | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, |
582 | a22f123c | Kevin Wolf | int min)
|
583 | a22f123c | Kevin Wolf | { |
584 | a22f123c | Kevin Wolf | int ret;
|
585 | a22f123c | Kevin Wolf | int num_checked, num_used;
|
586 | a22f123c | Kevin Wolf | |
587 | a22f123c | Kevin Wolf | if (n < min) {
|
588 | a22f123c | Kevin Wolf | min = n; |
589 | a22f123c | Kevin Wolf | } |
590 | a22f123c | Kevin Wolf | |
591 | a22f123c | Kevin Wolf | ret = is_allocated_sectors(buf, n, pnum); |
592 | a22f123c | Kevin Wolf | if (!ret) {
|
593 | a22f123c | Kevin Wolf | return ret;
|
594 | a22f123c | Kevin Wolf | } |
595 | a22f123c | Kevin Wolf | |
596 | a22f123c | Kevin Wolf | num_used = *pnum; |
597 | a22f123c | Kevin Wolf | buf += BDRV_SECTOR_SIZE * *pnum; |
598 | a22f123c | Kevin Wolf | n -= *pnum; |
599 | a22f123c | Kevin Wolf | num_checked = num_used; |
600 | a22f123c | Kevin Wolf | |
601 | a22f123c | Kevin Wolf | while (n > 0) { |
602 | a22f123c | Kevin Wolf | ret = is_allocated_sectors(buf, n, pnum); |
603 | a22f123c | Kevin Wolf | |
604 | a22f123c | Kevin Wolf | buf += BDRV_SECTOR_SIZE * *pnum; |
605 | a22f123c | Kevin Wolf | n -= *pnum; |
606 | a22f123c | Kevin Wolf | num_checked += *pnum; |
607 | a22f123c | Kevin Wolf | if (ret) {
|
608 | a22f123c | Kevin Wolf | num_used = num_checked; |
609 | a22f123c | Kevin Wolf | } else if (*pnum >= min) { |
610 | a22f123c | Kevin Wolf | break;
|
611 | a22f123c | Kevin Wolf | } |
612 | a22f123c | Kevin Wolf | } |
613 | a22f123c | Kevin Wolf | |
614 | a22f123c | Kevin Wolf | *pnum = num_used; |
615 | a22f123c | Kevin Wolf | return 1; |
616 | a22f123c | Kevin Wolf | } |
617 | a22f123c | Kevin Wolf | |
618 | a22f123c | Kevin Wolf | /*
|
619 | 3e85c6fd | Kevin Wolf | * Compares two buffers sector by sector. Returns 0 if the first sector of both
|
620 | 3e85c6fd | Kevin Wolf | * buffers matches, non-zero otherwise.
|
621 | 3e85c6fd | Kevin Wolf | *
|
622 | 3e85c6fd | Kevin Wolf | * pnum is set to the number of sectors (including and immediately following
|
623 | 3e85c6fd | Kevin Wolf | * the first one) that are known to have the same comparison result
|
624 | 3e85c6fd | Kevin Wolf | */
|
625 | 3e85c6fd | Kevin Wolf | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, |
626 | 3e85c6fd | Kevin Wolf | int *pnum)
|
627 | 3e85c6fd | Kevin Wolf | { |
628 | 3e85c6fd | Kevin Wolf | int res, i;
|
629 | 3e85c6fd | Kevin Wolf | |
630 | 3e85c6fd | Kevin Wolf | if (n <= 0) { |
631 | 3e85c6fd | Kevin Wolf | *pnum = 0;
|
632 | 3e85c6fd | Kevin Wolf | return 0; |
633 | 3e85c6fd | Kevin Wolf | } |
634 | 3e85c6fd | Kevin Wolf | |
635 | 3e85c6fd | Kevin Wolf | res = !!memcmp(buf1, buf2, 512);
|
636 | 3e85c6fd | Kevin Wolf | for(i = 1; i < n; i++) { |
637 | 3e85c6fd | Kevin Wolf | buf1 += 512;
|
638 | 3e85c6fd | Kevin Wolf | buf2 += 512;
|
639 | 3e85c6fd | Kevin Wolf | |
640 | 3e85c6fd | Kevin Wolf | if (!!memcmp(buf1, buf2, 512) != res) { |
641 | 3e85c6fd | Kevin Wolf | break;
|
642 | 3e85c6fd | Kevin Wolf | } |
643 | 3e85c6fd | Kevin Wolf | } |
644 | 3e85c6fd | Kevin Wolf | |
645 | 3e85c6fd | Kevin Wolf | *pnum = i; |
646 | 3e85c6fd | Kevin Wolf | return res;
|
647 | 3e85c6fd | Kevin Wolf | } |
648 | 3e85c6fd | Kevin Wolf | |
649 | 80ee15a6 | Kevin Wolf | #define IO_BUF_SIZE (2 * 1024 * 1024) |
650 | ea2384d3 | bellard | |
651 | ea2384d3 | bellard | static int img_convert(int argc, char **argv) |
652 | ea2384d3 | bellard | { |
653 | eec77d9e | Jes Sorensen | int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors; |
654 | 661a0f71 | Federico Simoncelli | int progress = 0, flags; |
655 | 661a0f71 | Federico Simoncelli | const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename; |
656 | b50cbabc | MORITA Kazutaka | BlockDriver *drv, *proto_drv; |
657 | c2abccec | MORITA Kazutaka | BlockDriverState **bs = NULL, *out_bs = NULL; |
658 | 96b8f136 | ths | int64_t total_sectors, nb_sectors, sector_num, bs_offset; |
659 | 96b8f136 | ths | uint64_t bs_sectors; |
660 | c2abccec | MORITA Kazutaka | uint8_t * buf = NULL;
|
661 | ea2384d3 | bellard | const uint8_t *buf1;
|
662 | faea38e7 | bellard | BlockDriverInfo bdi; |
663 | b50cbabc | MORITA Kazutaka | QEMUOptionParameter *param = NULL, *create_options = NULL; |
664 | a18953fb | Kevin Wolf | QEMUOptionParameter *out_baseimg_param; |
665 | efa84d43 | Kevin Wolf | char *options = NULL; |
666 | 51ef6727 | edison | const char *snapshot_name = NULL; |
667 | 6b837bc4 | Jes Sorensen | float local_progress;
|
668 | a22f123c | Kevin Wolf | int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ |
669 | ea2384d3 | bellard | |
670 | ea2384d3 | bellard | fmt = NULL;
|
671 | ea2384d3 | bellard | out_fmt = "raw";
|
672 | 661a0f71 | Federico Simoncelli | cache = "unsafe";
|
673 | f58c7b35 | ths | out_baseimg = NULL;
|
674 | eec77d9e | Jes Sorensen | compress = 0;
|
675 | ea2384d3 | bellard | for(;;) {
|
676 | a22f123c | Kevin Wolf | c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:");
|
677 | b8fb60da | Jes Sorensen | if (c == -1) { |
678 | ea2384d3 | bellard | break;
|
679 | b8fb60da | Jes Sorensen | } |
680 | ea2384d3 | bellard | switch(c) {
|
681 | ef87394c | Jes Sorensen | case '?': |
682 | ea2384d3 | bellard | case 'h': |
683 | ea2384d3 | bellard | help(); |
684 | ea2384d3 | bellard | break;
|
685 | ea2384d3 | bellard | case 'f': |
686 | ea2384d3 | bellard | fmt = optarg; |
687 | ea2384d3 | bellard | break;
|
688 | ea2384d3 | bellard | case 'O': |
689 | ea2384d3 | bellard | out_fmt = optarg; |
690 | ea2384d3 | bellard | break;
|
691 | f58c7b35 | ths | case 'B': |
692 | f58c7b35 | ths | out_baseimg = optarg; |
693 | f58c7b35 | ths | break;
|
694 | ea2384d3 | bellard | case 'c': |
695 | eec77d9e | Jes Sorensen | compress = 1;
|
696 | ea2384d3 | bellard | break;
|
697 | ea2384d3 | bellard | case 'e': |
698 | 9d42e15d | Markus Armbruster | error_report("option -e is deprecated, please use \'-o "
|
699 | eec77d9e | Jes Sorensen | "encryption\' instead!");
|
700 | eec77d9e | Jes Sorensen | return 1; |
701 | ec36ba14 | ths | case '6': |
702 | 9d42e15d | Markus Armbruster | error_report("option -6 is deprecated, please use \'-o "
|
703 | eec77d9e | Jes Sorensen | "compat6\' instead!");
|
704 | eec77d9e | Jes Sorensen | return 1; |
705 | efa84d43 | Kevin Wolf | case 'o': |
706 | efa84d43 | Kevin Wolf | options = optarg; |
707 | efa84d43 | Kevin Wolf | break;
|
708 | 51ef6727 | edison | case 's': |
709 | 51ef6727 | edison | snapshot_name = optarg; |
710 | 51ef6727 | edison | break;
|
711 | a22f123c | Kevin Wolf | case 'S': |
712 | a22f123c | Kevin Wolf | { |
713 | a22f123c | Kevin Wolf | int64_t sval; |
714 | e36b3695 | Markus Armbruster | char *end;
|
715 | e36b3695 | Markus Armbruster | sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B); |
716 | e36b3695 | Markus Armbruster | if (sval < 0 || *end) { |
717 | a22f123c | Kevin Wolf | error_report("Invalid minimum zero buffer size for sparse output specified");
|
718 | a22f123c | Kevin Wolf | return 1; |
719 | a22f123c | Kevin Wolf | } |
720 | a22f123c | Kevin Wolf | |
721 | a22f123c | Kevin Wolf | min_sparse = sval / BDRV_SECTOR_SIZE; |
722 | a22f123c | Kevin Wolf | break;
|
723 | a22f123c | Kevin Wolf | } |
724 | 6b837bc4 | Jes Sorensen | case 'p': |
725 | 6b837bc4 | Jes Sorensen | progress = 1;
|
726 | 6b837bc4 | Jes Sorensen | break;
|
727 | 661a0f71 | Federico Simoncelli | case 't': |
728 | 661a0f71 | Federico Simoncelli | cache = optarg; |
729 | 661a0f71 | Federico Simoncelli | break;
|
730 | ea2384d3 | bellard | } |
731 | ea2384d3 | bellard | } |
732 | 3b46e624 | ths | |
733 | 926c2d23 | balrog | bs_n = argc - optind - 1;
|
734 | b8fb60da | Jes Sorensen | if (bs_n < 1) { |
735 | b8fb60da | Jes Sorensen | help(); |
736 | b8fb60da | Jes Sorensen | } |
737 | 926c2d23 | balrog | |
738 | 926c2d23 | balrog | out_filename = argv[argc - 1];
|
739 | f58c7b35 | ths | |
740 | 4ac8aacd | Jes Sorensen | if (options && !strcmp(options, "?")) { |
741 | 4ac8aacd | Jes Sorensen | ret = print_block_option_help(out_filename, out_fmt); |
742 | 4ac8aacd | Jes Sorensen | goto out;
|
743 | 4ac8aacd | Jes Sorensen | } |
744 | 4ac8aacd | Jes Sorensen | |
745 | c2abccec | MORITA Kazutaka | if (bs_n > 1 && out_baseimg) { |
746 | 15654a6d | Jes Sorensen | error_report("-B makes no sense when concatenating multiple input "
|
747 | 15654a6d | Jes Sorensen | "images");
|
748 | 31ca34b8 | Jes Sorensen | ret = -1;
|
749 | 31ca34b8 | Jes Sorensen | goto out;
|
750 | c2abccec | MORITA Kazutaka | } |
751 | 926c2d23 | balrog | |
752 | 6b837bc4 | Jes Sorensen | qemu_progress_init(progress, 2.0); |
753 | 6b837bc4 | Jes Sorensen | qemu_progress_print(0, 100); |
754 | 6b837bc4 | Jes Sorensen | |
755 | 7267c094 | Anthony Liguori | bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
|
756 | 926c2d23 | balrog | |
757 | 926c2d23 | balrog | total_sectors = 0;
|
758 | 926c2d23 | balrog | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
759 | adfe078e | Stefan Hajnoczi | bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS); |
760 | c2abccec | MORITA Kazutaka | if (!bs[bs_i]) {
|
761 | 15654a6d | Jes Sorensen | error_report("Could not open '%s'", argv[optind + bs_i]);
|
762 | c2abccec | MORITA Kazutaka | ret = -1;
|
763 | c2abccec | MORITA Kazutaka | goto out;
|
764 | c2abccec | MORITA Kazutaka | } |
765 | 926c2d23 | balrog | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
766 | 926c2d23 | balrog | total_sectors += bs_sectors; |
767 | 926c2d23 | balrog | } |
768 | ea2384d3 | bellard | |
769 | 51ef6727 | edison | if (snapshot_name != NULL) { |
770 | 51ef6727 | edison | if (bs_n > 1) { |
771 | 6daf194d | Markus Armbruster | error_report("No support for concatenating multiple snapshot");
|
772 | 51ef6727 | edison | ret = -1;
|
773 | 51ef6727 | edison | goto out;
|
774 | 51ef6727 | edison | } |
775 | 51ef6727 | edison | if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) { |
776 | 6daf194d | Markus Armbruster | error_report("Failed to load snapshot");
|
777 | 51ef6727 | edison | ret = -1;
|
778 | 51ef6727 | edison | goto out;
|
779 | 51ef6727 | edison | } |
780 | 51ef6727 | edison | } |
781 | 51ef6727 | edison | |
782 | efa84d43 | Kevin Wolf | /* Find driver and parse its options */
|
783 | ea2384d3 | bellard | drv = bdrv_find_format(out_fmt); |
784 | c2abccec | MORITA Kazutaka | if (!drv) {
|
785 | 15654a6d | Jes Sorensen | error_report("Unknown file format '%s'", out_fmt);
|
786 | c2abccec | MORITA Kazutaka | ret = -1;
|
787 | c2abccec | MORITA Kazutaka | goto out;
|
788 | c2abccec | MORITA Kazutaka | } |
789 | efa84d43 | Kevin Wolf | |
790 | b50cbabc | MORITA Kazutaka | proto_drv = bdrv_find_protocol(out_filename); |
791 | c2abccec | MORITA Kazutaka | if (!proto_drv) {
|
792 | 15654a6d | Jes Sorensen | error_report("Unknown protocol '%s'", out_filename);
|
793 | c2abccec | MORITA Kazutaka | ret = -1;
|
794 | c2abccec | MORITA Kazutaka | goto out;
|
795 | c2abccec | MORITA Kazutaka | } |
796 | b50cbabc | MORITA Kazutaka | |
797 | b50cbabc | MORITA Kazutaka | create_options = append_option_parameters(create_options, |
798 | b50cbabc | MORITA Kazutaka | drv->create_options); |
799 | b50cbabc | MORITA Kazutaka | create_options = append_option_parameters(create_options, |
800 | b50cbabc | MORITA Kazutaka | proto_drv->create_options); |
801 | db08adf5 | Kevin Wolf | |
802 | efa84d43 | Kevin Wolf | if (options) {
|
803 | b50cbabc | MORITA Kazutaka | param = parse_option_parameters(options, create_options, param); |
804 | efa84d43 | Kevin Wolf | if (param == NULL) { |
805 | 15654a6d | Jes Sorensen | error_report("Invalid options for file format '%s'.", out_fmt);
|
806 | c2abccec | MORITA Kazutaka | ret = -1;
|
807 | c2abccec | MORITA Kazutaka | goto out;
|
808 | efa84d43 | Kevin Wolf | } |
809 | efa84d43 | Kevin Wolf | } else {
|
810 | b50cbabc | MORITA Kazutaka | param = parse_option_parameters("", create_options, param);
|
811 | efa84d43 | Kevin Wolf | } |
812 | efa84d43 | Kevin Wolf | |
813 | efa84d43 | Kevin Wolf | set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
|
814 | eec77d9e | Jes Sorensen | ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
|
815 | c2abccec | MORITA Kazutaka | if (ret < 0) { |
816 | c2abccec | MORITA Kazutaka | goto out;
|
817 | c2abccec | MORITA Kazutaka | } |
818 | efa84d43 | Kevin Wolf | |
819 | a18953fb | Kevin Wolf | /* Get backing file name if -o backing_file was used */
|
820 | a18953fb | Kevin Wolf | out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
821 | a18953fb | Kevin Wolf | if (out_baseimg_param) {
|
822 | a18953fb | Kevin Wolf | out_baseimg = out_baseimg_param->value.s; |
823 | a18953fb | Kevin Wolf | } |
824 | a18953fb | Kevin Wolf | |
825 | efa84d43 | Kevin Wolf | /* Check if compression is supported */
|
826 | eec77d9e | Jes Sorensen | if (compress) {
|
827 | efa84d43 | Kevin Wolf | QEMUOptionParameter *encryption = |
828 | efa84d43 | Kevin Wolf | get_option_parameter(param, BLOCK_OPT_ENCRYPT); |
829 | 41521fa4 | Kevin Wolf | QEMUOptionParameter *preallocation = |
830 | 41521fa4 | Kevin Wolf | get_option_parameter(param, BLOCK_OPT_PREALLOC); |
831 | efa84d43 | Kevin Wolf | |
832 | efa84d43 | Kevin Wolf | if (!drv->bdrv_write_compressed) {
|
833 | 15654a6d | Jes Sorensen | error_report("Compression not supported for this file format");
|
834 | c2abccec | MORITA Kazutaka | ret = -1;
|
835 | c2abccec | MORITA Kazutaka | goto out;
|
836 | efa84d43 | Kevin Wolf | } |
837 | efa84d43 | Kevin Wolf | |
838 | efa84d43 | Kevin Wolf | if (encryption && encryption->value.n) {
|
839 | 15654a6d | Jes Sorensen | error_report("Compression and encryption not supported at "
|
840 | 15654a6d | Jes Sorensen | "the same time");
|
841 | c2abccec | MORITA Kazutaka | ret = -1;
|
842 | c2abccec | MORITA Kazutaka | goto out;
|
843 | efa84d43 | Kevin Wolf | } |
844 | 41521fa4 | Kevin Wolf | |
845 | 41521fa4 | Kevin Wolf | if (preallocation && preallocation->value.s
|
846 | 41521fa4 | Kevin Wolf | && strcmp(preallocation->value.s, "off"))
|
847 | 41521fa4 | Kevin Wolf | { |
848 | 41521fa4 | Kevin Wolf | error_report("Compression and preallocation not supported at "
|
849 | 41521fa4 | Kevin Wolf | "the same time");
|
850 | 41521fa4 | Kevin Wolf | ret = -1;
|
851 | 41521fa4 | Kevin Wolf | goto out;
|
852 | 41521fa4 | Kevin Wolf | } |
853 | efa84d43 | Kevin Wolf | } |
854 | efa84d43 | Kevin Wolf | |
855 | efa84d43 | Kevin Wolf | /* Create the new image */
|
856 | efa84d43 | Kevin Wolf | ret = bdrv_create(drv, out_filename, param); |
857 | ea2384d3 | bellard | if (ret < 0) { |
858 | ea2384d3 | bellard | if (ret == -ENOTSUP) {
|
859 | 15654a6d | Jes Sorensen | error_report("Formatting not supported for file format '%s'",
|
860 | 15654a6d | Jes Sorensen | out_fmt); |
861 | 6e9ea0c0 | aurel32 | } else if (ret == -EFBIG) { |
862 | 15654a6d | Jes Sorensen | error_report("The image size is too large for file format '%s'",
|
863 | 15654a6d | Jes Sorensen | out_fmt); |
864 | ea2384d3 | bellard | } else {
|
865 | 15654a6d | Jes Sorensen | error_report("%s: error while converting %s: %s",
|
866 | 15654a6d | Jes Sorensen | out_filename, out_fmt, strerror(-ret)); |
867 | ea2384d3 | bellard | } |
868 | c2abccec | MORITA Kazutaka | goto out;
|
869 | ea2384d3 | bellard | } |
870 | 3b46e624 | ths | |
871 | 661a0f71 | Federico Simoncelli | flags = BDRV_O_RDWR; |
872 | c3993cdc | Stefan Hajnoczi | ret = bdrv_parse_cache_flags(cache, &flags); |
873 | 661a0f71 | Federico Simoncelli | if (ret < 0) { |
874 | 661a0f71 | Federico Simoncelli | error_report("Invalid cache option: %s", cache);
|
875 | 661a0f71 | Federico Simoncelli | return -1; |
876 | 661a0f71 | Federico Simoncelli | } |
877 | 661a0f71 | Federico Simoncelli | |
878 | 661a0f71 | Federico Simoncelli | out_bs = bdrv_new_open(out_filename, out_fmt, flags); |
879 | c2abccec | MORITA Kazutaka | if (!out_bs) {
|
880 | c2abccec | MORITA Kazutaka | ret = -1;
|
881 | c2abccec | MORITA Kazutaka | goto out;
|
882 | c2abccec | MORITA Kazutaka | } |
883 | ea2384d3 | bellard | |
884 | 926c2d23 | balrog | bs_i = 0;
|
885 | 926c2d23 | balrog | bs_offset = 0;
|
886 | 926c2d23 | balrog | bdrv_get_geometry(bs[0], &bs_sectors);
|
887 | bb1c0597 | Kevin Wolf | buf = qemu_blockalign(out_bs, IO_BUF_SIZE); |
888 | 926c2d23 | balrog | |
889 | eec77d9e | Jes Sorensen | if (compress) {
|
890 | c2abccec | MORITA Kazutaka | ret = bdrv_get_info(out_bs, &bdi); |
891 | c2abccec | MORITA Kazutaka | if (ret < 0) { |
892 | 15654a6d | Jes Sorensen | error_report("could not get block driver info");
|
893 | c2abccec | MORITA Kazutaka | goto out;
|
894 | c2abccec | MORITA Kazutaka | } |
895 | faea38e7 | bellard | cluster_size = bdi.cluster_size; |
896 | c2abccec | MORITA Kazutaka | if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) { |
897 | 15654a6d | Jes Sorensen | error_report("invalid cluster size");
|
898 | c2abccec | MORITA Kazutaka | ret = -1;
|
899 | c2abccec | MORITA Kazutaka | goto out;
|
900 | c2abccec | MORITA Kazutaka | } |
901 | ea2384d3 | bellard | cluster_sectors = cluster_size >> 9;
|
902 | ea2384d3 | bellard | sector_num = 0;
|
903 | 6b837bc4 | Jes Sorensen | |
904 | 6b837bc4 | Jes Sorensen | nb_sectors = total_sectors; |
905 | 6b837bc4 | Jes Sorensen | local_progress = (float)100 / |
906 | 4ee96418 | Jes Sorensen | (nb_sectors / MIN(nb_sectors, cluster_sectors)); |
907 | 6b837bc4 | Jes Sorensen | |
908 | ea2384d3 | bellard | for(;;) {
|
909 | 926c2d23 | balrog | int64_t bs_num; |
910 | 926c2d23 | balrog | int remainder;
|
911 | 926c2d23 | balrog | uint8_t *buf2; |
912 | 926c2d23 | balrog | |
913 | ea2384d3 | bellard | nb_sectors = total_sectors - sector_num; |
914 | ea2384d3 | bellard | if (nb_sectors <= 0) |
915 | ea2384d3 | bellard | break;
|
916 | ea2384d3 | bellard | if (nb_sectors >= cluster_sectors)
|
917 | ea2384d3 | bellard | n = cluster_sectors; |
918 | ea2384d3 | bellard | else
|
919 | ea2384d3 | bellard | n = nb_sectors; |
920 | 926c2d23 | balrog | |
921 | 926c2d23 | balrog | bs_num = sector_num - bs_offset; |
922 | 926c2d23 | balrog | assert (bs_num >= 0);
|
923 | 926c2d23 | balrog | remainder = n; |
924 | 926c2d23 | balrog | buf2 = buf; |
925 | 926c2d23 | balrog | while (remainder > 0) { |
926 | 926c2d23 | balrog | int nlow;
|
927 | 926c2d23 | balrog | while (bs_num == bs_sectors) {
|
928 | 926c2d23 | balrog | bs_i++; |
929 | 926c2d23 | balrog | assert (bs_i < bs_n); |
930 | 926c2d23 | balrog | bs_offset += bs_sectors; |
931 | 926c2d23 | balrog | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
932 | 926c2d23 | balrog | bs_num = 0;
|
933 | 0bfcd599 | Blue Swirl | /* printf("changing part: sector_num=%" PRId64 ", "
|
934 | 0bfcd599 | Blue Swirl | "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
|
935 | 0bfcd599 | Blue Swirl | "\n", sector_num, bs_i, bs_offset, bs_sectors); */
|
936 | 926c2d23 | balrog | } |
937 | 926c2d23 | balrog | assert (bs_num < bs_sectors); |
938 | 926c2d23 | balrog | |
939 | 926c2d23 | balrog | nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder; |
940 | 926c2d23 | balrog | |
941 | c2abccec | MORITA Kazutaka | ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow); |
942 | c2abccec | MORITA Kazutaka | if (ret < 0) { |
943 | 3fba9d81 | Stefan Hajnoczi | error_report("error while reading sector %" PRId64 ": %s", |
944 | 3fba9d81 | Stefan Hajnoczi | bs_num, strerror(-ret)); |
945 | c2abccec | MORITA Kazutaka | goto out;
|
946 | c2abccec | MORITA Kazutaka | } |
947 | 926c2d23 | balrog | |
948 | 926c2d23 | balrog | buf2 += nlow * 512;
|
949 | 926c2d23 | balrog | bs_num += nlow; |
950 | 926c2d23 | balrog | |
951 | 926c2d23 | balrog | remainder -= nlow; |
952 | 926c2d23 | balrog | } |
953 | 926c2d23 | balrog | assert (remainder == 0);
|
954 | 926c2d23 | balrog | |
955 | b8fb60da | Jes Sorensen | if (n < cluster_sectors) {
|
956 | ea2384d3 | bellard | memset(buf + n * 512, 0, cluster_size - n * 512); |
957 | b8fb60da | Jes Sorensen | } |
958 | ea2384d3 | bellard | if (is_not_zero(buf, cluster_size)) {
|
959 | c2abccec | MORITA Kazutaka | ret = bdrv_write_compressed(out_bs, sector_num, buf, |
960 | c2abccec | MORITA Kazutaka | cluster_sectors); |
961 | c2abccec | MORITA Kazutaka | if (ret != 0) { |
962 | 3fba9d81 | Stefan Hajnoczi | error_report("error while compressing sector %" PRId64
|
963 | 3fba9d81 | Stefan Hajnoczi | ": %s", sector_num, strerror(-ret));
|
964 | c2abccec | MORITA Kazutaka | goto out;
|
965 | c2abccec | MORITA Kazutaka | } |
966 | ea2384d3 | bellard | } |
967 | ea2384d3 | bellard | sector_num += n; |
968 | 6b837bc4 | Jes Sorensen | qemu_progress_print(local_progress, 100);
|
969 | ea2384d3 | bellard | } |
970 | faea38e7 | bellard | /* signal EOF to align */
|
971 | faea38e7 | bellard | bdrv_write_compressed(out_bs, 0, NULL, 0); |
972 | ea2384d3 | bellard | } else {
|
973 | f2feebbd | Kevin Wolf | int has_zero_init = bdrv_has_zero_init(out_bs);
|
974 | f2feebbd | Kevin Wolf | |
975 | f58c7b35 | ths | sector_num = 0; // total number of sectors converted so far |
976 | 6b837bc4 | Jes Sorensen | nb_sectors = total_sectors - sector_num; |
977 | 6b837bc4 | Jes Sorensen | local_progress = (float)100 / |
978 | 4ee96418 | Jes Sorensen | (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512));
|
979 | 6b837bc4 | Jes Sorensen | |
980 | ea2384d3 | bellard | for(;;) {
|
981 | ea2384d3 | bellard | nb_sectors = total_sectors - sector_num; |
982 | b8fb60da | Jes Sorensen | if (nb_sectors <= 0) { |
983 | ea2384d3 | bellard | break;
|
984 | b8fb60da | Jes Sorensen | } |
985 | b8fb60da | Jes Sorensen | if (nb_sectors >= (IO_BUF_SIZE / 512)) { |
986 | ea2384d3 | bellard | n = (IO_BUF_SIZE / 512);
|
987 | b8fb60da | Jes Sorensen | } else {
|
988 | ea2384d3 | bellard | n = nb_sectors; |
989 | b8fb60da | Jes Sorensen | } |
990 | 926c2d23 | balrog | |
991 | 926c2d23 | balrog | while (sector_num - bs_offset >= bs_sectors) {
|
992 | 926c2d23 | balrog | bs_i ++; |
993 | 926c2d23 | balrog | assert (bs_i < bs_n); |
994 | 926c2d23 | balrog | bs_offset += bs_sectors; |
995 | 926c2d23 | balrog | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
996 | 0bfcd599 | Blue Swirl | /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
|
997 | 0bfcd599 | Blue Swirl | "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
|
998 | 926c2d23 | balrog | sector_num, bs_i, bs_offset, bs_sectors); */
|
999 | 926c2d23 | balrog | } |
1000 | 926c2d23 | balrog | |
1001 | b8fb60da | Jes Sorensen | if (n > bs_offset + bs_sectors - sector_num) {
|
1002 | 926c2d23 | balrog | n = bs_offset + bs_sectors - sector_num; |
1003 | b8fb60da | Jes Sorensen | } |
1004 | 926c2d23 | balrog | |
1005 | f2feebbd | Kevin Wolf | if (has_zero_init) {
|
1006 | d032044f | Akkarit Sangpetch | /* If the output image is being created as a copy on write image,
|
1007 | d032044f | Akkarit Sangpetch | assume that sectors which are unallocated in the input image
|
1008 | d032044f | Akkarit Sangpetch | are present in both the output's and input's base images (no
|
1009 | d032044f | Akkarit Sangpetch | need to copy them). */
|
1010 | d032044f | Akkarit Sangpetch | if (out_baseimg) {
|
1011 | d032044f | Akkarit Sangpetch | if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
|
1012 | d032044f | Akkarit Sangpetch | n, &n1)) { |
1013 | d032044f | Akkarit Sangpetch | sector_num += n1; |
1014 | d032044f | Akkarit Sangpetch | continue;
|
1015 | d032044f | Akkarit Sangpetch | } |
1016 | d032044f | Akkarit Sangpetch | /* The next 'n1' sectors are allocated in the input image. Copy
|
1017 | d032044f | Akkarit Sangpetch | only those as they may be followed by unallocated sectors. */
|
1018 | d032044f | Akkarit Sangpetch | n = n1; |
1019 | 93c65b47 | aliguori | } |
1020 | 93c65b47 | aliguori | } else {
|
1021 | 93c65b47 | aliguori | n1 = n; |
1022 | f58c7b35 | ths | } |
1023 | f58c7b35 | ths | |
1024 | c2abccec | MORITA Kazutaka | ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n); |
1025 | c2abccec | MORITA Kazutaka | if (ret < 0) { |
1026 | 3fba9d81 | Stefan Hajnoczi | error_report("error while reading sector %" PRId64 ": %s", |
1027 | 3fba9d81 | Stefan Hajnoczi | sector_num - bs_offset, strerror(-ret)); |
1028 | c2abccec | MORITA Kazutaka | goto out;
|
1029 | c2abccec | MORITA Kazutaka | } |
1030 | ea2384d3 | bellard | /* NOTE: at the same time we convert, we do not write zero
|
1031 | ea2384d3 | bellard | sectors to have a chance to compress the image. Ideally, we
|
1032 | ea2384d3 | bellard | should add a specific call to have the info to go faster */
|
1033 | ea2384d3 | bellard | buf1 = buf; |
1034 | ea2384d3 | bellard | while (n > 0) { |
1035 | f58c7b35 | ths | /* If the output image is being created as a copy on write image,
|
1036 | f58c7b35 | ths | copy all sectors even the ones containing only NUL bytes,
|
1037 | 93c65b47 | aliguori | because they may differ from the sectors in the base image.
|
1038 | 93c65b47 | aliguori | |
1039 | 93c65b47 | aliguori | If the output is to a host device, we also write out
|
1040 | 93c65b47 | aliguori | sectors that are entirely 0, since whatever data was
|
1041 | 93c65b47 | aliguori | already there is garbage, not 0s. */
|
1042 | f2feebbd | Kevin Wolf | if (!has_zero_init || out_baseimg ||
|
1043 | a22f123c | Kevin Wolf | is_allocated_sectors_min(buf1, n, &n1, min_sparse)) { |
1044 | c2abccec | MORITA Kazutaka | ret = bdrv_write(out_bs, sector_num, buf1, n1); |
1045 | c2abccec | MORITA Kazutaka | if (ret < 0) { |
1046 | 3fba9d81 | Stefan Hajnoczi | error_report("error while writing sector %" PRId64
|
1047 | 3fba9d81 | Stefan Hajnoczi | ": %s", sector_num, strerror(-ret));
|
1048 | c2abccec | MORITA Kazutaka | goto out;
|
1049 | c2abccec | MORITA Kazutaka | } |
1050 | ea2384d3 | bellard | } |
1051 | ea2384d3 | bellard | sector_num += n1; |
1052 | ea2384d3 | bellard | n -= n1; |
1053 | ea2384d3 | bellard | buf1 += n1 * 512;
|
1054 | ea2384d3 | bellard | } |
1055 | 6b837bc4 | Jes Sorensen | qemu_progress_print(local_progress, 100);
|
1056 | ea2384d3 | bellard | } |
1057 | ea2384d3 | bellard | } |
1058 | c2abccec | MORITA Kazutaka | out:
|
1059 | 6b837bc4 | Jes Sorensen | qemu_progress_end(); |
1060 | c2abccec | MORITA Kazutaka | free_option_parameters(create_options); |
1061 | c2abccec | MORITA Kazutaka | free_option_parameters(param); |
1062 | bb1c0597 | Kevin Wolf | qemu_vfree(buf); |
1063 | c2abccec | MORITA Kazutaka | if (out_bs) {
|
1064 | c2abccec | MORITA Kazutaka | bdrv_delete(out_bs); |
1065 | c2abccec | MORITA Kazutaka | } |
1066 | 31ca34b8 | Jes Sorensen | if (bs) {
|
1067 | 31ca34b8 | Jes Sorensen | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
1068 | 31ca34b8 | Jes Sorensen | if (bs[bs_i]) {
|
1069 | 31ca34b8 | Jes Sorensen | bdrv_delete(bs[bs_i]); |
1070 | 31ca34b8 | Jes Sorensen | } |
1071 | c2abccec | MORITA Kazutaka | } |
1072 | 7267c094 | Anthony Liguori | g_free(bs); |
1073 | c2abccec | MORITA Kazutaka | } |
1074 | c2abccec | MORITA Kazutaka | if (ret) {
|
1075 | c2abccec | MORITA Kazutaka | return 1; |
1076 | c2abccec | MORITA Kazutaka | } |
1077 | ea2384d3 | bellard | return 0; |
1078 | ea2384d3 | bellard | } |
1079 | ea2384d3 | bellard | |
1080 | 57d1a2b6 | bellard | |
1081 | faea38e7 | bellard | static void dump_snapshots(BlockDriverState *bs) |
1082 | faea38e7 | bellard | { |
1083 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
1084 | faea38e7 | bellard | int nb_sns, i;
|
1085 | faea38e7 | bellard | char buf[256]; |
1086 | faea38e7 | bellard | |
1087 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
1088 | faea38e7 | bellard | if (nb_sns <= 0) |
1089 | faea38e7 | bellard | return;
|
1090 | faea38e7 | bellard | printf("Snapshot list:\n");
|
1091 | faea38e7 | bellard | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
1092 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
1093 | faea38e7 | bellard | sn = &sn_tab[i]; |
1094 | faea38e7 | bellard | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
1095 | faea38e7 | bellard | } |
1096 | 7267c094 | Anthony Liguori | g_free(sn_tab); |
1097 | faea38e7 | bellard | } |
1098 | faea38e7 | bellard | |
1099 | ea2384d3 | bellard | static int img_info(int argc, char **argv) |
1100 | ea2384d3 | bellard | { |
1101 | ea2384d3 | bellard | int c;
|
1102 | ea2384d3 | bellard | const char *filename, *fmt; |
1103 | ea2384d3 | bellard | BlockDriverState *bs; |
1104 | ea2384d3 | bellard | char fmt_name[128], size_buf[128], dsize_buf[128]; |
1105 | 96b8f136 | ths | uint64_t total_sectors; |
1106 | 96b8f136 | ths | int64_t allocated_size; |
1107 | 93b6b2a3 | bellard | char backing_filename[1024]; |
1108 | 93b6b2a3 | bellard | char backing_filename2[1024]; |
1109 | faea38e7 | bellard | BlockDriverInfo bdi; |
1110 | ea2384d3 | bellard | |
1111 | ea2384d3 | bellard | fmt = NULL;
|
1112 | ea2384d3 | bellard | for(;;) {
|
1113 | ea2384d3 | bellard | c = getopt(argc, argv, "f:h");
|
1114 | b8fb60da | Jes Sorensen | if (c == -1) { |
1115 | ea2384d3 | bellard | break;
|
1116 | b8fb60da | Jes Sorensen | } |
1117 | ea2384d3 | bellard | switch(c) {
|
1118 | ef87394c | Jes Sorensen | case '?': |
1119 | ea2384d3 | bellard | case 'h': |
1120 | ea2384d3 | bellard | help(); |
1121 | ea2384d3 | bellard | break;
|
1122 | ea2384d3 | bellard | case 'f': |
1123 | ea2384d3 | bellard | fmt = optarg; |
1124 | ea2384d3 | bellard | break;
|
1125 | ea2384d3 | bellard | } |
1126 | ea2384d3 | bellard | } |
1127 | b8fb60da | Jes Sorensen | if (optind >= argc) {
|
1128 | ea2384d3 | bellard | help(); |
1129 | b8fb60da | Jes Sorensen | } |
1130 | ea2384d3 | bellard | filename = argv[optind++]; |
1131 | ea2384d3 | bellard | |
1132 | adfe078e | Stefan Hajnoczi | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING); |
1133 | c2abccec | MORITA Kazutaka | if (!bs) {
|
1134 | c2abccec | MORITA Kazutaka | return 1; |
1135 | c2abccec | MORITA Kazutaka | } |
1136 | ea2384d3 | bellard | bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
|
1137 | ea2384d3 | bellard | bdrv_get_geometry(bs, &total_sectors); |
1138 | ea2384d3 | bellard | get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512); |
1139 | 4a1d5e1f | Fam Zheng | allocated_size = bdrv_get_allocated_file_size(bs); |
1140 | b8fb60da | Jes Sorensen | if (allocated_size < 0) { |
1141 | a10ea30b | blueswir1 | snprintf(dsize_buf, sizeof(dsize_buf), "unavailable"); |
1142 | b8fb60da | Jes Sorensen | } else {
|
1143 | 5fafdf24 | ths | get_human_readable_size(dsize_buf, sizeof(dsize_buf),
|
1144 | de167e41 | bellard | allocated_size); |
1145 | b8fb60da | Jes Sorensen | } |
1146 | ea2384d3 | bellard | printf("image: %s\n"
|
1147 | ea2384d3 | bellard | "file format: %s\n"
|
1148 | ec3757de | bellard | "virtual size: %s (%" PRId64 " bytes)\n" |
1149 | ea2384d3 | bellard | "disk size: %s\n",
|
1150 | 5fafdf24 | ths | filename, fmt_name, size_buf, |
1151 | ec3757de | bellard | (total_sectors * 512),
|
1152 | ea2384d3 | bellard | dsize_buf); |
1153 | b8fb60da | Jes Sorensen | if (bdrv_is_encrypted(bs)) {
|
1154 | ea2384d3 | bellard | printf("encrypted: yes\n");
|
1155 | b8fb60da | Jes Sorensen | } |
1156 | faea38e7 | bellard | if (bdrv_get_info(bs, &bdi) >= 0) { |
1157 | b8fb60da | Jes Sorensen | if (bdi.cluster_size != 0) { |
1158 | faea38e7 | bellard | printf("cluster_size: %d\n", bdi.cluster_size);
|
1159 | b8fb60da | Jes Sorensen | } |
1160 | faea38e7 | bellard | } |
1161 | 93b6b2a3 | bellard | bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
|
1162 | faea38e7 | bellard | if (backing_filename[0] != '\0') { |
1163 | 93b6b2a3 | bellard | path_combine(backing_filename2, sizeof(backing_filename2),
|
1164 | 93b6b2a3 | bellard | filename, backing_filename); |
1165 | 5fafdf24 | ths | printf("backing file: %s (actual path: %s)\n",
|
1166 | 93b6b2a3 | bellard | backing_filename, |
1167 | 93b6b2a3 | bellard | backing_filename2); |
1168 | faea38e7 | bellard | } |
1169 | faea38e7 | bellard | dump_snapshots(bs); |
1170 | ea2384d3 | bellard | bdrv_delete(bs); |
1171 | ea2384d3 | bellard | return 0; |
1172 | ea2384d3 | bellard | } |
1173 | ea2384d3 | bellard | |
1174 | f7b4a940 | aliguori | #define SNAPSHOT_LIST 1 |
1175 | f7b4a940 | aliguori | #define SNAPSHOT_CREATE 2 |
1176 | f7b4a940 | aliguori | #define SNAPSHOT_APPLY 3 |
1177 | f7b4a940 | aliguori | #define SNAPSHOT_DELETE 4 |
1178 | f7b4a940 | aliguori | |
1179 | 153859be | Stuart Brady | static int img_snapshot(int argc, char **argv) |
1180 | f7b4a940 | aliguori | { |
1181 | f7b4a940 | aliguori | BlockDriverState *bs; |
1182 | f7b4a940 | aliguori | QEMUSnapshotInfo sn; |
1183 | f7b4a940 | aliguori | char *filename, *snapshot_name = NULL; |
1184 | c2abccec | MORITA Kazutaka | int c, ret = 0, bdrv_oflags; |
1185 | f7b4a940 | aliguori | int action = 0; |
1186 | f7b4a940 | aliguori | qemu_timeval tv; |
1187 | f7b4a940 | aliguori | |
1188 | 710da702 | Kevin Wolf | bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR; |
1189 | f7b4a940 | aliguori | /* Parse commandline parameters */
|
1190 | f7b4a940 | aliguori | for(;;) {
|
1191 | f7b4a940 | aliguori | c = getopt(argc, argv, "la:c:d:h");
|
1192 | b8fb60da | Jes Sorensen | if (c == -1) { |
1193 | f7b4a940 | aliguori | break;
|
1194 | b8fb60da | Jes Sorensen | } |
1195 | f7b4a940 | aliguori | switch(c) {
|
1196 | ef87394c | Jes Sorensen | case '?': |
1197 | f7b4a940 | aliguori | case 'h': |
1198 | f7b4a940 | aliguori | help(); |
1199 | 153859be | Stuart Brady | return 0; |
1200 | f7b4a940 | aliguori | case 'l': |
1201 | f7b4a940 | aliguori | if (action) {
|
1202 | f7b4a940 | aliguori | help(); |
1203 | 153859be | Stuart Brady | return 0; |
1204 | f7b4a940 | aliguori | } |
1205 | f7b4a940 | aliguori | action = SNAPSHOT_LIST; |
1206 | f5edb014 | Naphtali Sprei | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
|
1207 | f7b4a940 | aliguori | break;
|
1208 | f7b4a940 | aliguori | case 'a': |
1209 | f7b4a940 | aliguori | if (action) {
|
1210 | f7b4a940 | aliguori | help(); |
1211 | 153859be | Stuart Brady | return 0; |
1212 | f7b4a940 | aliguori | } |
1213 | f7b4a940 | aliguori | action = SNAPSHOT_APPLY; |
1214 | f7b4a940 | aliguori | snapshot_name = optarg; |
1215 | f7b4a940 | aliguori | break;
|
1216 | f7b4a940 | aliguori | case 'c': |
1217 | f7b4a940 | aliguori | if (action) {
|
1218 | f7b4a940 | aliguori | help(); |
1219 | 153859be | Stuart Brady | return 0; |
1220 | f7b4a940 | aliguori | } |
1221 | f7b4a940 | aliguori | action = SNAPSHOT_CREATE; |
1222 | f7b4a940 | aliguori | snapshot_name = optarg; |
1223 | f7b4a940 | aliguori | break;
|
1224 | f7b4a940 | aliguori | case 'd': |
1225 | f7b4a940 | aliguori | if (action) {
|
1226 | f7b4a940 | aliguori | help(); |
1227 | 153859be | Stuart Brady | return 0; |
1228 | f7b4a940 | aliguori | } |
1229 | f7b4a940 | aliguori | action = SNAPSHOT_DELETE; |
1230 | f7b4a940 | aliguori | snapshot_name = optarg; |
1231 | f7b4a940 | aliguori | break;
|
1232 | f7b4a940 | aliguori | } |
1233 | f7b4a940 | aliguori | } |
1234 | f7b4a940 | aliguori | |
1235 | b8fb60da | Jes Sorensen | if (optind >= argc) {
|
1236 | f7b4a940 | aliguori | help(); |
1237 | b8fb60da | Jes Sorensen | } |
1238 | f7b4a940 | aliguori | filename = argv[optind++]; |
1239 | f7b4a940 | aliguori | |
1240 | f7b4a940 | aliguori | /* Open the image */
|
1241 | f163d073 | Stefan Hajnoczi | bs = bdrv_new_open(filename, NULL, bdrv_oflags);
|
1242 | c2abccec | MORITA Kazutaka | if (!bs) {
|
1243 | c2abccec | MORITA Kazutaka | return 1; |
1244 | c2abccec | MORITA Kazutaka | } |
1245 | f7b4a940 | aliguori | |
1246 | f7b4a940 | aliguori | /* Perform the requested action */
|
1247 | f7b4a940 | aliguori | switch(action) {
|
1248 | f7b4a940 | aliguori | case SNAPSHOT_LIST:
|
1249 | f7b4a940 | aliguori | dump_snapshots(bs); |
1250 | f7b4a940 | aliguori | break;
|
1251 | f7b4a940 | aliguori | |
1252 | f7b4a940 | aliguori | case SNAPSHOT_CREATE:
|
1253 | f7b4a940 | aliguori | memset(&sn, 0, sizeof(sn)); |
1254 | f7b4a940 | aliguori | pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
|
1255 | f7b4a940 | aliguori | |
1256 | f7b4a940 | aliguori | qemu_gettimeofday(&tv); |
1257 | f7b4a940 | aliguori | sn.date_sec = tv.tv_sec; |
1258 | f7b4a940 | aliguori | sn.date_nsec = tv.tv_usec * 1000;
|
1259 | f7b4a940 | aliguori | |
1260 | f7b4a940 | aliguori | ret = bdrv_snapshot_create(bs, &sn); |
1261 | b8fb60da | Jes Sorensen | if (ret) {
|
1262 | 15654a6d | Jes Sorensen | error_report("Could not create snapshot '%s': %d (%s)",
|
1263 | f7b4a940 | aliguori | snapshot_name, ret, strerror(-ret)); |
1264 | b8fb60da | Jes Sorensen | } |
1265 | f7b4a940 | aliguori | break;
|
1266 | f7b4a940 | aliguori | |
1267 | f7b4a940 | aliguori | case SNAPSHOT_APPLY:
|
1268 | f7b4a940 | aliguori | ret = bdrv_snapshot_goto(bs, snapshot_name); |
1269 | b8fb60da | Jes Sorensen | if (ret) {
|
1270 | 15654a6d | Jes Sorensen | error_report("Could not apply snapshot '%s': %d (%s)",
|
1271 | f7b4a940 | aliguori | snapshot_name, ret, strerror(-ret)); |
1272 | b8fb60da | Jes Sorensen | } |
1273 | f7b4a940 | aliguori | break;
|
1274 | f7b4a940 | aliguori | |
1275 | f7b4a940 | aliguori | case SNAPSHOT_DELETE:
|
1276 | f7b4a940 | aliguori | ret = bdrv_snapshot_delete(bs, snapshot_name); |
1277 | b8fb60da | Jes Sorensen | if (ret) {
|
1278 | 15654a6d | Jes Sorensen | error_report("Could not delete snapshot '%s': %d (%s)",
|
1279 | f7b4a940 | aliguori | snapshot_name, ret, strerror(-ret)); |
1280 | b8fb60da | Jes Sorensen | } |
1281 | f7b4a940 | aliguori | break;
|
1282 | f7b4a940 | aliguori | } |
1283 | f7b4a940 | aliguori | |
1284 | f7b4a940 | aliguori | /* Cleanup */
|
1285 | f7b4a940 | aliguori | bdrv_delete(bs); |
1286 | c2abccec | MORITA Kazutaka | if (ret) {
|
1287 | c2abccec | MORITA Kazutaka | return 1; |
1288 | c2abccec | MORITA Kazutaka | } |
1289 | 153859be | Stuart Brady | return 0; |
1290 | f7b4a940 | aliguori | } |
1291 | f7b4a940 | aliguori | |
1292 | 3e85c6fd | Kevin Wolf | static int img_rebase(int argc, char **argv) |
1293 | 3e85c6fd | Kevin Wolf | { |
1294 | c2abccec | MORITA Kazutaka | BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL; |
1295 | f163d073 | Stefan Hajnoczi | BlockDriver *old_backing_drv, *new_backing_drv; |
1296 | 3e85c6fd | Kevin Wolf | char *filename;
|
1297 | 661a0f71 | Federico Simoncelli | const char *fmt, *cache, *out_basefmt, *out_baseimg; |
1298 | 3e85c6fd | Kevin Wolf | int c, flags, ret;
|
1299 | 3e85c6fd | Kevin Wolf | int unsafe = 0; |
1300 | 6b837bc4 | Jes Sorensen | int progress = 0; |
1301 | 3e85c6fd | Kevin Wolf | |
1302 | 3e85c6fd | Kevin Wolf | /* Parse commandline parameters */
|
1303 | e53dbee0 | Kevin Wolf | fmt = NULL;
|
1304 | 661a0f71 | Federico Simoncelli | cache = BDRV_DEFAULT_CACHE; |
1305 | 3e85c6fd | Kevin Wolf | out_baseimg = NULL;
|
1306 | 3e85c6fd | Kevin Wolf | out_basefmt = NULL;
|
1307 | 3e85c6fd | Kevin Wolf | for(;;) {
|
1308 | 661a0f71 | Federico Simoncelli | c = getopt(argc, argv, "uhf:F:b:pt:");
|
1309 | b8fb60da | Jes Sorensen | if (c == -1) { |
1310 | 3e85c6fd | Kevin Wolf | break;
|
1311 | b8fb60da | Jes Sorensen | } |
1312 | 3e85c6fd | Kevin Wolf | switch(c) {
|
1313 | ef87394c | Jes Sorensen | case '?': |
1314 | 3e85c6fd | Kevin Wolf | case 'h': |
1315 | 3e85c6fd | Kevin Wolf | help(); |
1316 | 3e85c6fd | Kevin Wolf | return 0; |
1317 | e53dbee0 | Kevin Wolf | case 'f': |
1318 | e53dbee0 | Kevin Wolf | fmt = optarg; |
1319 | e53dbee0 | Kevin Wolf | break;
|
1320 | 3e85c6fd | Kevin Wolf | case 'F': |
1321 | 3e85c6fd | Kevin Wolf | out_basefmt = optarg; |
1322 | 3e85c6fd | Kevin Wolf | break;
|
1323 | 3e85c6fd | Kevin Wolf | case 'b': |
1324 | 3e85c6fd | Kevin Wolf | out_baseimg = optarg; |
1325 | 3e85c6fd | Kevin Wolf | break;
|
1326 | 3e85c6fd | Kevin Wolf | case 'u': |
1327 | 3e85c6fd | Kevin Wolf | unsafe = 1;
|
1328 | 3e85c6fd | Kevin Wolf | break;
|
1329 | 6b837bc4 | Jes Sorensen | case 'p': |
1330 | 6b837bc4 | Jes Sorensen | progress = 1;
|
1331 | 6b837bc4 | Jes Sorensen | break;
|
1332 | 661a0f71 | Federico Simoncelli | case 't': |
1333 | 661a0f71 | Federico Simoncelli | cache = optarg; |
1334 | 661a0f71 | Federico Simoncelli | break;
|
1335 | 3e85c6fd | Kevin Wolf | } |
1336 | 3e85c6fd | Kevin Wolf | } |
1337 | 3e85c6fd | Kevin Wolf | |
1338 | 9a9d9dba | Anthony Liguori | if ((optind >= argc) || (!unsafe && !out_baseimg)) {
|
1339 | 3e85c6fd | Kevin Wolf | help(); |
1340 | b8fb60da | Jes Sorensen | } |
1341 | 3e85c6fd | Kevin Wolf | filename = argv[optind++]; |
1342 | 3e85c6fd | Kevin Wolf | |
1343 | 6b837bc4 | Jes Sorensen | qemu_progress_init(progress, 2.0); |
1344 | 6b837bc4 | Jes Sorensen | qemu_progress_print(0, 100); |
1345 | 6b837bc4 | Jes Sorensen | |
1346 | 661a0f71 | Federico Simoncelli | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
|
1347 | c3993cdc | Stefan Hajnoczi | ret = bdrv_parse_cache_flags(cache, &flags); |
1348 | 661a0f71 | Federico Simoncelli | if (ret < 0) { |
1349 | 661a0f71 | Federico Simoncelli | error_report("Invalid cache option: %s", cache);
|
1350 | 661a0f71 | Federico Simoncelli | return -1; |
1351 | 661a0f71 | Federico Simoncelli | } |
1352 | 661a0f71 | Federico Simoncelli | |
1353 | 3e85c6fd | Kevin Wolf | /*
|
1354 | 3e85c6fd | Kevin Wolf | * Open the images.
|
1355 | 3e85c6fd | Kevin Wolf | *
|
1356 | 3e85c6fd | Kevin Wolf | * Ignore the old backing file for unsafe rebase in case we want to correct
|
1357 | 3e85c6fd | Kevin Wolf | * the reference to a renamed or moved backing file.
|
1358 | 3e85c6fd | Kevin Wolf | */
|
1359 | f163d073 | Stefan Hajnoczi | bs = bdrv_new_open(filename, fmt, flags); |
1360 | c2abccec | MORITA Kazutaka | if (!bs) {
|
1361 | c2abccec | MORITA Kazutaka | return 1; |
1362 | c2abccec | MORITA Kazutaka | } |
1363 | 3e85c6fd | Kevin Wolf | |
1364 | 3e85c6fd | Kevin Wolf | /* Find the right drivers for the backing files */
|
1365 | 3e85c6fd | Kevin Wolf | old_backing_drv = NULL;
|
1366 | 3e85c6fd | Kevin Wolf | new_backing_drv = NULL;
|
1367 | 3e85c6fd | Kevin Wolf | |
1368 | 3e85c6fd | Kevin Wolf | if (!unsafe && bs->backing_format[0] != '\0') { |
1369 | 3e85c6fd | Kevin Wolf | old_backing_drv = bdrv_find_format(bs->backing_format); |
1370 | 3e85c6fd | Kevin Wolf | if (old_backing_drv == NULL) { |
1371 | 15654a6d | Jes Sorensen | error_report("Invalid format name: '%s'", bs->backing_format);
|
1372 | c2abccec | MORITA Kazutaka | ret = -1;
|
1373 | c2abccec | MORITA Kazutaka | goto out;
|
1374 | 3e85c6fd | Kevin Wolf | } |
1375 | 3e85c6fd | Kevin Wolf | } |
1376 | 3e85c6fd | Kevin Wolf | |
1377 | 3e85c6fd | Kevin Wolf | if (out_basefmt != NULL) { |
1378 | 3e85c6fd | Kevin Wolf | new_backing_drv = bdrv_find_format(out_basefmt); |
1379 | 3e85c6fd | Kevin Wolf | if (new_backing_drv == NULL) { |
1380 | 15654a6d | Jes Sorensen | error_report("Invalid format name: '%s'", out_basefmt);
|
1381 | c2abccec | MORITA Kazutaka | ret = -1;
|
1382 | c2abccec | MORITA Kazutaka | goto out;
|
1383 | 3e85c6fd | Kevin Wolf | } |
1384 | 3e85c6fd | Kevin Wolf | } |
1385 | 3e85c6fd | Kevin Wolf | |
1386 | 3e85c6fd | Kevin Wolf | /* For safe rebasing we need to compare old and new backing file */
|
1387 | 3e85c6fd | Kevin Wolf | if (unsafe) {
|
1388 | 3e85c6fd | Kevin Wolf | /* Make the compiler happy */
|
1389 | 3e85c6fd | Kevin Wolf | bs_old_backing = NULL;
|
1390 | 3e85c6fd | Kevin Wolf | bs_new_backing = NULL;
|
1391 | 3e85c6fd | Kevin Wolf | } else {
|
1392 | 3e85c6fd | Kevin Wolf | char backing_name[1024]; |
1393 | 3e85c6fd | Kevin Wolf | |
1394 | 3e85c6fd | Kevin Wolf | bs_old_backing = bdrv_new("old_backing");
|
1395 | 3e85c6fd | Kevin Wolf | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
|
1396 | c2abccec | MORITA Kazutaka | ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS, |
1397 | c2abccec | MORITA Kazutaka | old_backing_drv); |
1398 | c2abccec | MORITA Kazutaka | if (ret) {
|
1399 | 15654a6d | Jes Sorensen | error_report("Could not open old backing file '%s'", backing_name);
|
1400 | c2abccec | MORITA Kazutaka | goto out;
|
1401 | 3e85c6fd | Kevin Wolf | } |
1402 | 3e85c6fd | Kevin Wolf | |
1403 | 3e85c6fd | Kevin Wolf | bs_new_backing = bdrv_new("new_backing");
|
1404 | cdbae851 | Kevin Wolf | ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS, |
1405 | c2abccec | MORITA Kazutaka | new_backing_drv); |
1406 | c2abccec | MORITA Kazutaka | if (ret) {
|
1407 | 15654a6d | Jes Sorensen | error_report("Could not open new backing file '%s'", out_baseimg);
|
1408 | c2abccec | MORITA Kazutaka | goto out;
|
1409 | 3e85c6fd | Kevin Wolf | } |
1410 | 3e85c6fd | Kevin Wolf | } |
1411 | 3e85c6fd | Kevin Wolf | |
1412 | 3e85c6fd | Kevin Wolf | /*
|
1413 | 3e85c6fd | Kevin Wolf | * Check each unallocated cluster in the COW file. If it is unallocated,
|
1414 | 3e85c6fd | Kevin Wolf | * accesses go to the backing file. We must therefore compare this cluster
|
1415 | 3e85c6fd | Kevin Wolf | * in the old and new backing file, and if they differ we need to copy it
|
1416 | 3e85c6fd | Kevin Wolf | * from the old backing file into the COW file.
|
1417 | 3e85c6fd | Kevin Wolf | *
|
1418 | 3e85c6fd | Kevin Wolf | * If qemu-img crashes during this step, no harm is done. The content of
|
1419 | 3e85c6fd | Kevin Wolf | * the image is the same as the original one at any time.
|
1420 | 3e85c6fd | Kevin Wolf | */
|
1421 | 3e85c6fd | Kevin Wolf | if (!unsafe) {
|
1422 | 3e85c6fd | Kevin Wolf | uint64_t num_sectors; |
1423 | 87a1b3e3 | Kevin Wolf | uint64_t old_backing_num_sectors; |
1424 | 87a1b3e3 | Kevin Wolf | uint64_t new_backing_num_sectors; |
1425 | 3e85c6fd | Kevin Wolf | uint64_t sector; |
1426 | cc60e327 | Kevin Wolf | int n;
|
1427 | d6771bfa | TeLeMan | uint8_t * buf_old; |
1428 | d6771bfa | TeLeMan | uint8_t * buf_new; |
1429 | 6b837bc4 | Jes Sorensen | float local_progress;
|
1430 | d6771bfa | TeLeMan | |
1431 | bb1c0597 | Kevin Wolf | buf_old = qemu_blockalign(bs, IO_BUF_SIZE); |
1432 | bb1c0597 | Kevin Wolf | buf_new = qemu_blockalign(bs, IO_BUF_SIZE); |
1433 | 3e85c6fd | Kevin Wolf | |
1434 | 3e85c6fd | Kevin Wolf | bdrv_get_geometry(bs, &num_sectors); |
1435 | 87a1b3e3 | Kevin Wolf | bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors); |
1436 | 87a1b3e3 | Kevin Wolf | bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors); |
1437 | 3e85c6fd | Kevin Wolf | |
1438 | 6b837bc4 | Jes Sorensen | local_progress = (float)100 / |
1439 | 4ee96418 | Jes Sorensen | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
|
1440 | 3e85c6fd | Kevin Wolf | for (sector = 0; sector < num_sectors; sector += n) { |
1441 | 3e85c6fd | Kevin Wolf | |
1442 | 3e85c6fd | Kevin Wolf | /* How many sectors can we handle with the next read? */
|
1443 | 3e85c6fd | Kevin Wolf | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { |
1444 | 3e85c6fd | Kevin Wolf | n = (IO_BUF_SIZE / 512);
|
1445 | 3e85c6fd | Kevin Wolf | } else {
|
1446 | 3e85c6fd | Kevin Wolf | n = num_sectors - sector; |
1447 | 3e85c6fd | Kevin Wolf | } |
1448 | 3e85c6fd | Kevin Wolf | |
1449 | 3e85c6fd | Kevin Wolf | /* If the cluster is allocated, we don't need to take action */
|
1450 | cc60e327 | Kevin Wolf | ret = bdrv_is_allocated(bs, sector, n, &n); |
1451 | cc60e327 | Kevin Wolf | if (ret) {
|
1452 | 3e85c6fd | Kevin Wolf | continue;
|
1453 | 3e85c6fd | Kevin Wolf | } |
1454 | 3e85c6fd | Kevin Wolf | |
1455 | 87a1b3e3 | Kevin Wolf | /*
|
1456 | 87a1b3e3 | Kevin Wolf | * Read old and new backing file and take into consideration that
|
1457 | 87a1b3e3 | Kevin Wolf | * backing files may be smaller than the COW image.
|
1458 | 87a1b3e3 | Kevin Wolf | */
|
1459 | 87a1b3e3 | Kevin Wolf | if (sector >= old_backing_num_sectors) {
|
1460 | 87a1b3e3 | Kevin Wolf | memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
|
1461 | 87a1b3e3 | Kevin Wolf | } else {
|
1462 | 87a1b3e3 | Kevin Wolf | if (sector + n > old_backing_num_sectors) {
|
1463 | 87a1b3e3 | Kevin Wolf | n = old_backing_num_sectors - sector; |
1464 | 87a1b3e3 | Kevin Wolf | } |
1465 | 87a1b3e3 | Kevin Wolf | |
1466 | 87a1b3e3 | Kevin Wolf | ret = bdrv_read(bs_old_backing, sector, buf_old, n); |
1467 | 87a1b3e3 | Kevin Wolf | if (ret < 0) { |
1468 | 87a1b3e3 | Kevin Wolf | error_report("error while reading from old backing file");
|
1469 | 87a1b3e3 | Kevin Wolf | goto out;
|
1470 | 87a1b3e3 | Kevin Wolf | } |
1471 | 3e85c6fd | Kevin Wolf | } |
1472 | 87a1b3e3 | Kevin Wolf | |
1473 | 87a1b3e3 | Kevin Wolf | if (sector >= new_backing_num_sectors) {
|
1474 | 87a1b3e3 | Kevin Wolf | memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
|
1475 | 87a1b3e3 | Kevin Wolf | } else {
|
1476 | 87a1b3e3 | Kevin Wolf | if (sector + n > new_backing_num_sectors) {
|
1477 | 87a1b3e3 | Kevin Wolf | n = new_backing_num_sectors - sector; |
1478 | 87a1b3e3 | Kevin Wolf | } |
1479 | 87a1b3e3 | Kevin Wolf | |
1480 | 87a1b3e3 | Kevin Wolf | ret = bdrv_read(bs_new_backing, sector, buf_new, n); |
1481 | 87a1b3e3 | Kevin Wolf | if (ret < 0) { |
1482 | 87a1b3e3 | Kevin Wolf | error_report("error while reading from new backing file");
|
1483 | 87a1b3e3 | Kevin Wolf | goto out;
|
1484 | 87a1b3e3 | Kevin Wolf | } |
1485 | 3e85c6fd | Kevin Wolf | } |
1486 | 3e85c6fd | Kevin Wolf | |
1487 | 3e85c6fd | Kevin Wolf | /* If they differ, we need to write to the COW file */
|
1488 | 3e85c6fd | Kevin Wolf | uint64_t written = 0;
|
1489 | 3e85c6fd | Kevin Wolf | |
1490 | 3e85c6fd | Kevin Wolf | while (written < n) {
|
1491 | 3e85c6fd | Kevin Wolf | int pnum;
|
1492 | 3e85c6fd | Kevin Wolf | |
1493 | 3e85c6fd | Kevin Wolf | if (compare_sectors(buf_old + written * 512, |
1494 | 60b1bd4f | Kevin Wolf | buf_new + written * 512, n - written, &pnum))
|
1495 | 3e85c6fd | Kevin Wolf | { |
1496 | 3e85c6fd | Kevin Wolf | ret = bdrv_write(bs, sector + written, |
1497 | 3e85c6fd | Kevin Wolf | buf_old + written * 512, pnum);
|
1498 | 3e85c6fd | Kevin Wolf | if (ret < 0) { |
1499 | 15654a6d | Jes Sorensen | error_report("Error while writing to COW image: %s",
|
1500 | 3e85c6fd | Kevin Wolf | strerror(-ret)); |
1501 | c2abccec | MORITA Kazutaka | goto out;
|
1502 | 3e85c6fd | Kevin Wolf | } |
1503 | 3e85c6fd | Kevin Wolf | } |
1504 | 3e85c6fd | Kevin Wolf | |
1505 | 3e85c6fd | Kevin Wolf | written += pnum; |
1506 | 3e85c6fd | Kevin Wolf | } |
1507 | 6b837bc4 | Jes Sorensen | qemu_progress_print(local_progress, 100);
|
1508 | 3e85c6fd | Kevin Wolf | } |
1509 | d6771bfa | TeLeMan | |
1510 | bb1c0597 | Kevin Wolf | qemu_vfree(buf_old); |
1511 | bb1c0597 | Kevin Wolf | qemu_vfree(buf_new); |
1512 | 3e85c6fd | Kevin Wolf | } |
1513 | 3e85c6fd | Kevin Wolf | |
1514 | 3e85c6fd | Kevin Wolf | /*
|
1515 | 3e85c6fd | Kevin Wolf | * Change the backing file. All clusters that are different from the old
|
1516 | 3e85c6fd | Kevin Wolf | * backing file are overwritten in the COW file now, so the visible content
|
1517 | 3e85c6fd | Kevin Wolf | * doesn't change when we switch the backing file.
|
1518 | 3e85c6fd | Kevin Wolf | */
|
1519 | 3e85c6fd | Kevin Wolf | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
1520 | 3e85c6fd | Kevin Wolf | if (ret == -ENOSPC) {
|
1521 | 15654a6d | Jes Sorensen | error_report("Could not change the backing file to '%s': No "
|
1522 | 15654a6d | Jes Sorensen | "space left in the file header", out_baseimg);
|
1523 | 3e85c6fd | Kevin Wolf | } else if (ret < 0) { |
1524 | 15654a6d | Jes Sorensen | error_report("Could not change the backing file to '%s': %s",
|
1525 | 3e85c6fd | Kevin Wolf | out_baseimg, strerror(-ret)); |
1526 | 3e85c6fd | Kevin Wolf | } |
1527 | 3e85c6fd | Kevin Wolf | |
1528 | 6b837bc4 | Jes Sorensen | qemu_progress_print(100, 0); |
1529 | 3e85c6fd | Kevin Wolf | /*
|
1530 | 3e85c6fd | Kevin Wolf | * TODO At this point it is possible to check if any clusters that are
|
1531 | 3e85c6fd | Kevin Wolf | * allocated in the COW file are the same in the backing file. If so, they
|
1532 | 3e85c6fd | Kevin Wolf | * could be dropped from the COW file. Don't do this before switching the
|
1533 | 3e85c6fd | Kevin Wolf | * backing file, in case of a crash this would lead to corruption.
|
1534 | 3e85c6fd | Kevin Wolf | */
|
1535 | c2abccec | MORITA Kazutaka | out:
|
1536 | 6b837bc4 | Jes Sorensen | qemu_progress_end(); |
1537 | 3e85c6fd | Kevin Wolf | /* Cleanup */
|
1538 | 3e85c6fd | Kevin Wolf | if (!unsafe) {
|
1539 | eb863add | Kevin Wolf | if (bs_old_backing != NULL) { |
1540 | eb863add | Kevin Wolf | bdrv_delete(bs_old_backing); |
1541 | eb863add | Kevin Wolf | } |
1542 | eb863add | Kevin Wolf | if (bs_new_backing != NULL) { |
1543 | eb863add | Kevin Wolf | bdrv_delete(bs_new_backing); |
1544 | eb863add | Kevin Wolf | } |
1545 | 3e85c6fd | Kevin Wolf | } |
1546 | 3e85c6fd | Kevin Wolf | |
1547 | 3e85c6fd | Kevin Wolf | bdrv_delete(bs); |
1548 | c2abccec | MORITA Kazutaka | if (ret) {
|
1549 | c2abccec | MORITA Kazutaka | return 1; |
1550 | c2abccec | MORITA Kazutaka | } |
1551 | 3e85c6fd | Kevin Wolf | return 0; |
1552 | 3e85c6fd | Kevin Wolf | } |
1553 | 3e85c6fd | Kevin Wolf | |
1554 | ae6b0ed6 | Stefan Hajnoczi | static int img_resize(int argc, char **argv) |
1555 | ae6b0ed6 | Stefan Hajnoczi | { |
1556 | ae6b0ed6 | Stefan Hajnoczi | int c, ret, relative;
|
1557 | ae6b0ed6 | Stefan Hajnoczi | const char *filename, *fmt, *size; |
1558 | ae6b0ed6 | Stefan Hajnoczi | int64_t n, total_size; |
1559 | 2a81998a | Jes Sorensen | BlockDriverState *bs = NULL;
|
1560 | ae6b0ed6 | Stefan Hajnoczi | QEMUOptionParameter *param; |
1561 | ae6b0ed6 | Stefan Hajnoczi | QEMUOptionParameter resize_options[] = { |
1562 | ae6b0ed6 | Stefan Hajnoczi | { |
1563 | ae6b0ed6 | Stefan Hajnoczi | .name = BLOCK_OPT_SIZE, |
1564 | ae6b0ed6 | Stefan Hajnoczi | .type = OPT_SIZE, |
1565 | ae6b0ed6 | Stefan Hajnoczi | .help = "Virtual disk size"
|
1566 | ae6b0ed6 | Stefan Hajnoczi | }, |
1567 | ae6b0ed6 | Stefan Hajnoczi | { NULL }
|
1568 | ae6b0ed6 | Stefan Hajnoczi | }; |
1569 | ae6b0ed6 | Stefan Hajnoczi | |
1570 | e80fec7f | Kevin Wolf | /* Remove size from argv manually so that negative numbers are not treated
|
1571 | e80fec7f | Kevin Wolf | * as options by getopt. */
|
1572 | e80fec7f | Kevin Wolf | if (argc < 3) { |
1573 | e80fec7f | Kevin Wolf | help(); |
1574 | e80fec7f | Kevin Wolf | return 1; |
1575 | e80fec7f | Kevin Wolf | } |
1576 | e80fec7f | Kevin Wolf | |
1577 | e80fec7f | Kevin Wolf | size = argv[--argc]; |
1578 | e80fec7f | Kevin Wolf | |
1579 | e80fec7f | Kevin Wolf | /* Parse getopt arguments */
|
1580 | ae6b0ed6 | Stefan Hajnoczi | fmt = NULL;
|
1581 | ae6b0ed6 | Stefan Hajnoczi | for(;;) {
|
1582 | ae6b0ed6 | Stefan Hajnoczi | c = getopt(argc, argv, "f:h");
|
1583 | ae6b0ed6 | Stefan Hajnoczi | if (c == -1) { |
1584 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1585 | ae6b0ed6 | Stefan Hajnoczi | } |
1586 | ae6b0ed6 | Stefan Hajnoczi | switch(c) {
|
1587 | ef87394c | Jes Sorensen | case '?': |
1588 | ae6b0ed6 | Stefan Hajnoczi | case 'h': |
1589 | ae6b0ed6 | Stefan Hajnoczi | help(); |
1590 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1591 | ae6b0ed6 | Stefan Hajnoczi | case 'f': |
1592 | ae6b0ed6 | Stefan Hajnoczi | fmt = optarg; |
1593 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1594 | ae6b0ed6 | Stefan Hajnoczi | } |
1595 | ae6b0ed6 | Stefan Hajnoczi | } |
1596 | e80fec7f | Kevin Wolf | if (optind >= argc) {
|
1597 | ae6b0ed6 | Stefan Hajnoczi | help(); |
1598 | ae6b0ed6 | Stefan Hajnoczi | } |
1599 | ae6b0ed6 | Stefan Hajnoczi | filename = argv[optind++]; |
1600 | ae6b0ed6 | Stefan Hajnoczi | |
1601 | ae6b0ed6 | Stefan Hajnoczi | /* Choose grow, shrink, or absolute resize mode */
|
1602 | ae6b0ed6 | Stefan Hajnoczi | switch (size[0]) { |
1603 | ae6b0ed6 | Stefan Hajnoczi | case '+': |
1604 | ae6b0ed6 | Stefan Hajnoczi | relative = 1;
|
1605 | ae6b0ed6 | Stefan Hajnoczi | size++; |
1606 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1607 | ae6b0ed6 | Stefan Hajnoczi | case '-': |
1608 | ae6b0ed6 | Stefan Hajnoczi | relative = -1;
|
1609 | ae6b0ed6 | Stefan Hajnoczi | size++; |
1610 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1611 | ae6b0ed6 | Stefan Hajnoczi | default:
|
1612 | ae6b0ed6 | Stefan Hajnoczi | relative = 0;
|
1613 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1614 | ae6b0ed6 | Stefan Hajnoczi | } |
1615 | ae6b0ed6 | Stefan Hajnoczi | |
1616 | ae6b0ed6 | Stefan Hajnoczi | /* Parse size */
|
1617 | ae6b0ed6 | Stefan Hajnoczi | param = parse_option_parameters("", resize_options, NULL); |
1618 | ae6b0ed6 | Stefan Hajnoczi | if (set_option_parameter(param, BLOCK_OPT_SIZE, size)) {
|
1619 | ae6b0ed6 | Stefan Hajnoczi | /* Error message already printed when size parsing fails */
|
1620 | 2a81998a | Jes Sorensen | ret = -1;
|
1621 | 2a81998a | Jes Sorensen | goto out;
|
1622 | ae6b0ed6 | Stefan Hajnoczi | } |
1623 | ae6b0ed6 | Stefan Hajnoczi | n = get_option_parameter(param, BLOCK_OPT_SIZE)->value.n; |
1624 | ae6b0ed6 | Stefan Hajnoczi | free_option_parameters(param); |
1625 | ae6b0ed6 | Stefan Hajnoczi | |
1626 | ae6b0ed6 | Stefan Hajnoczi | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR); |
1627 | c2abccec | MORITA Kazutaka | if (!bs) {
|
1628 | 2a81998a | Jes Sorensen | ret = -1;
|
1629 | 2a81998a | Jes Sorensen | goto out;
|
1630 | c2abccec | MORITA Kazutaka | } |
1631 | ae6b0ed6 | Stefan Hajnoczi | |
1632 | ae6b0ed6 | Stefan Hajnoczi | if (relative) {
|
1633 | ae6b0ed6 | Stefan Hajnoczi | total_size = bdrv_getlength(bs) + n * relative; |
1634 | ae6b0ed6 | Stefan Hajnoczi | } else {
|
1635 | ae6b0ed6 | Stefan Hajnoczi | total_size = n; |
1636 | ae6b0ed6 | Stefan Hajnoczi | } |
1637 | ae6b0ed6 | Stefan Hajnoczi | if (total_size <= 0) { |
1638 | 15654a6d | Jes Sorensen | error_report("New image size must be positive");
|
1639 | c2abccec | MORITA Kazutaka | ret = -1;
|
1640 | c2abccec | MORITA Kazutaka | goto out;
|
1641 | ae6b0ed6 | Stefan Hajnoczi | } |
1642 | ae6b0ed6 | Stefan Hajnoczi | |
1643 | ae6b0ed6 | Stefan Hajnoczi | ret = bdrv_truncate(bs, total_size); |
1644 | ae6b0ed6 | Stefan Hajnoczi | switch (ret) {
|
1645 | ae6b0ed6 | Stefan Hajnoczi | case 0: |
1646 | ae6b0ed6 | Stefan Hajnoczi | printf("Image resized.\n");
|
1647 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1648 | ae6b0ed6 | Stefan Hajnoczi | case -ENOTSUP:
|
1649 | 15654a6d | Jes Sorensen | error_report("This image format does not support resize");
|
1650 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1651 | ae6b0ed6 | Stefan Hajnoczi | case -EACCES:
|
1652 | 15654a6d | Jes Sorensen | error_report("Image is read-only");
|
1653 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1654 | ae6b0ed6 | Stefan Hajnoczi | default:
|
1655 | 15654a6d | Jes Sorensen | error_report("Error resizing image (%d)", -ret);
|
1656 | ae6b0ed6 | Stefan Hajnoczi | break;
|
1657 | ae6b0ed6 | Stefan Hajnoczi | } |
1658 | c2abccec | MORITA Kazutaka | out:
|
1659 | 2a81998a | Jes Sorensen | if (bs) {
|
1660 | 2a81998a | Jes Sorensen | bdrv_delete(bs); |
1661 | 2a81998a | Jes Sorensen | } |
1662 | c2abccec | MORITA Kazutaka | if (ret) {
|
1663 | c2abccec | MORITA Kazutaka | return 1; |
1664 | c2abccec | MORITA Kazutaka | } |
1665 | ae6b0ed6 | Stefan Hajnoczi | return 0; |
1666 | ae6b0ed6 | Stefan Hajnoczi | } |
1667 | ae6b0ed6 | Stefan Hajnoczi | |
1668 | c227f099 | Anthony Liguori | static const img_cmd_t img_cmds[] = { |
1669 | 153859be | Stuart Brady | #define DEF(option, callback, arg_string) \
|
1670 | 153859be | Stuart Brady | { option, callback }, |
1671 | 153859be | Stuart Brady | #include "qemu-img-cmds.h" |
1672 | 153859be | Stuart Brady | #undef DEF
|
1673 | 153859be | Stuart Brady | #undef GEN_DOCS
|
1674 | 153859be | Stuart Brady | { NULL, NULL, }, |
1675 | 153859be | Stuart Brady | }; |
1676 | 153859be | Stuart Brady | |
1677 | ea2384d3 | bellard | int main(int argc, char **argv) |
1678 | ea2384d3 | bellard | { |
1679 | c227f099 | Anthony Liguori | const img_cmd_t *cmd;
|
1680 | 153859be | Stuart Brady | const char *cmdname; |
1681 | ea2384d3 | bellard | |
1682 | 53f76e58 | Kevin Wolf | error_set_progname(argv[0]);
|
1683 | 53f76e58 | Kevin Wolf | |
1684 | ea2384d3 | bellard | bdrv_init(); |
1685 | ea2384d3 | bellard | if (argc < 2) |
1686 | ea2384d3 | bellard | help(); |
1687 | 153859be | Stuart Brady | cmdname = argv[1];
|
1688 | 8f9b157e | aurel32 | argc--; argv++; |
1689 | 153859be | Stuart Brady | |
1690 | 153859be | Stuart Brady | /* find the command */
|
1691 | 153859be | Stuart Brady | for(cmd = img_cmds; cmd->name != NULL; cmd++) { |
1692 | 153859be | Stuart Brady | if (!strcmp(cmdname, cmd->name)) {
|
1693 | 153859be | Stuart Brady | return cmd->handler(argc, argv);
|
1694 | 153859be | Stuart Brady | } |
1695 | ea2384d3 | bellard | } |
1696 | 153859be | Stuart Brady | |
1697 | 153859be | Stuart Brady | /* not found */
|
1698 | 153859be | Stuart Brady | help(); |
1699 | ea2384d3 | bellard | return 0; |
1700 | ea2384d3 | bellard | } |