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