root / qemu-img.c @ 17c275d9
History | View | Annotate | Download (17.9 kB)
1 | ea2384d3 | bellard | /*
|
---|---|---|---|
2 | fb43f4dd | bellard | * QEMU disk image utility
|
3 | ea2384d3 | bellard | *
|
4 | fb43f4dd | bellard | * Copyright (c) 2003-2006 Fabrice Bellard
|
5 | ea2384d3 | bellard | *
|
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 | ea2384d3 | bellard | #include "vl.h" |
25 | ea2384d3 | bellard | |
26 | e8445331 | bellard | #ifdef _WIN32
|
27 | e8445331 | bellard | #include <windows.h> |
28 | e8445331 | bellard | #endif
|
29 | e8445331 | bellard | |
30 | ea2384d3 | bellard | void *get_mmap_addr(unsigned long size) |
31 | ea2384d3 | bellard | { |
32 | ea2384d3 | bellard | return NULL; |
33 | ea2384d3 | bellard | } |
34 | ea2384d3 | bellard | |
35 | ea2384d3 | bellard | void qemu_free(void *ptr) |
36 | ea2384d3 | bellard | { |
37 | ea2384d3 | bellard | free(ptr); |
38 | ea2384d3 | bellard | } |
39 | ea2384d3 | bellard | |
40 | ea2384d3 | bellard | void *qemu_malloc(size_t size)
|
41 | ea2384d3 | bellard | { |
42 | ea2384d3 | bellard | return malloc(size);
|
43 | ea2384d3 | bellard | } |
44 | ea2384d3 | bellard | |
45 | ea2384d3 | bellard | void *qemu_mallocz(size_t size)
|
46 | ea2384d3 | bellard | { |
47 | ea2384d3 | bellard | void *ptr;
|
48 | ea2384d3 | bellard | ptr = qemu_malloc(size); |
49 | ea2384d3 | bellard | if (!ptr)
|
50 | ea2384d3 | bellard | return NULL; |
51 | ea2384d3 | bellard | memset(ptr, 0, size);
|
52 | ea2384d3 | bellard | return ptr;
|
53 | ea2384d3 | bellard | } |
54 | ea2384d3 | bellard | |
55 | ea2384d3 | bellard | char *qemu_strdup(const char *str) |
56 | ea2384d3 | bellard | { |
57 | ea2384d3 | bellard | char *ptr;
|
58 | ea2384d3 | bellard | ptr = qemu_malloc(strlen(str) + 1);
|
59 | ea2384d3 | bellard | if (!ptr)
|
60 | ea2384d3 | bellard | return NULL; |
61 | ea2384d3 | bellard | strcpy(ptr, str); |
62 | ea2384d3 | bellard | return ptr;
|
63 | ea2384d3 | bellard | } |
64 | ea2384d3 | bellard | |
65 | ea2384d3 | bellard | void term_printf(const char *fmt, ...) |
66 | ea2384d3 | bellard | { |
67 | ea2384d3 | bellard | va_list ap; |
68 | ea2384d3 | bellard | va_start(ap, fmt); |
69 | ea2384d3 | bellard | vprintf(fmt, ap); |
70 | ea2384d3 | bellard | va_end(ap); |
71 | ea2384d3 | bellard | } |
72 | ea2384d3 | bellard | |
73 | fef30743 | ths | void term_print_filename(const char *filename) |
74 | fef30743 | ths | { |
75 | fef30743 | ths | term_printf(filename); |
76 | fef30743 | ths | } |
77 | fef30743 | ths | |
78 | ea2384d3 | bellard | void __attribute__((noreturn)) error(const char *fmt, ...) |
79 | ea2384d3 | bellard | { |
80 | ea2384d3 | bellard | va_list ap; |
81 | ea2384d3 | bellard | va_start(ap, fmt); |
82 | 57d1a2b6 | bellard | fprintf(stderr, "qemu-img: ");
|
83 | ea2384d3 | bellard | vfprintf(stderr, fmt, ap); |
84 | ea2384d3 | bellard | fprintf(stderr, "\n");
|
85 | ea2384d3 | bellard | exit(1);
|
86 | ea2384d3 | bellard | va_end(ap); |
87 | ea2384d3 | bellard | } |
88 | ea2384d3 | bellard | |
89 | ea2384d3 | bellard | static void format_print(void *opaque, const char *name) |
90 | ea2384d3 | bellard | { |
91 | ea2384d3 | bellard | printf(" %s", name);
|
92 | ea2384d3 | bellard | } |
93 | ea2384d3 | bellard | |
94 | ea2384d3 | bellard | void help(void) |
95 | ea2384d3 | bellard | { |
96 | fb43f4dd | bellard | printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2006 Fabrice Bellard\n" |
97 | 57d1a2b6 | bellard | "usage: qemu-img command [command options]\n"
|
98 | ea2384d3 | bellard | "QEMU disk image utility\n"
|
99 | ea2384d3 | bellard | "\n"
|
100 | ea2384d3 | bellard | "Command syntax:\n"
|
101 | ea2384d3 | bellard | " create [-e] [-b base_image] [-f fmt] filename [size]\n"
|
102 | ea2384d3 | bellard | " commit [-f fmt] filename\n"
|
103 | ea2384d3 | bellard | " convert [-c] [-e] [-f fmt] filename [-O output_fmt] output_filename\n"
|
104 | ea2384d3 | bellard | " info [-f fmt] filename\n"
|
105 | ea2384d3 | bellard | "\n"
|
106 | ea2384d3 | bellard | "Command parameters:\n"
|
107 | ea2384d3 | bellard | " 'filename' is a disk image filename\n"
|
108 | ea2384d3 | bellard | " 'base_image' is the read-only disk image which is used as base for a copy on\n"
|
109 | ea2384d3 | bellard | " write image; the copy on write image only stores the modified data\n"
|
110 | ea2384d3 | bellard | " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
|
111 | ea2384d3 | bellard | " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
|
112 | ea2384d3 | bellard | " and 'G' (gigabyte) are supported\n"
|
113 | ea2384d3 | bellard | " 'output_filename' is the destination disk image filename\n"
|
114 | ea2384d3 | bellard | " 'output_fmt' is the destination format\n"
|
115 | ea2384d3 | bellard | " '-c' indicates that target image must be compressed (qcow format only)\n"
|
116 | ea2384d3 | bellard | " '-e' indicates that the target image must be encrypted (qcow format only)\n"
|
117 | ea2384d3 | bellard | ); |
118 | ea2384d3 | bellard | printf("\nSupported format:");
|
119 | ea2384d3 | bellard | bdrv_iterate_format(format_print, NULL);
|
120 | ea2384d3 | bellard | printf("\n");
|
121 | ea2384d3 | bellard | exit(1);
|
122 | ea2384d3 | bellard | } |
123 | ea2384d3 | bellard | |
124 | ea2384d3 | bellard | #if defined(WIN32)
|
125 | ea2384d3 | bellard | /* XXX: put correct support for win32 */
|
126 | ea2384d3 | bellard | static int read_password(char *buf, int buf_size) |
127 | ea2384d3 | bellard | { |
128 | ea2384d3 | bellard | int c, i;
|
129 | ea2384d3 | bellard | printf("Password: ");
|
130 | ea2384d3 | bellard | fflush(stdout); |
131 | ea2384d3 | bellard | i = 0;
|
132 | ea2384d3 | bellard | for(;;) {
|
133 | ea2384d3 | bellard | c = getchar(); |
134 | ea2384d3 | bellard | if (c == '\n') |
135 | ea2384d3 | bellard | break;
|
136 | ea2384d3 | bellard | if (i < (buf_size - 1)) |
137 | ea2384d3 | bellard | buf[i++] = c; |
138 | ea2384d3 | bellard | } |
139 | ea2384d3 | bellard | buf[i] = '\0';
|
140 | ea2384d3 | bellard | return 0; |
141 | ea2384d3 | bellard | } |
142 | ea2384d3 | bellard | |
143 | ea2384d3 | bellard | #else
|
144 | ea2384d3 | bellard | |
145 | ea2384d3 | bellard | #include <termios.h> |
146 | ea2384d3 | bellard | |
147 | ea2384d3 | bellard | static struct termios oldtty; |
148 | ea2384d3 | bellard | |
149 | ea2384d3 | bellard | static void term_exit(void) |
150 | ea2384d3 | bellard | { |
151 | ea2384d3 | bellard | tcsetattr (0, TCSANOW, &oldtty);
|
152 | ea2384d3 | bellard | } |
153 | ea2384d3 | bellard | |
154 | ea2384d3 | bellard | static void term_init(void) |
155 | ea2384d3 | bellard | { |
156 | ea2384d3 | bellard | struct termios tty;
|
157 | ea2384d3 | bellard | |
158 | ea2384d3 | bellard | tcgetattr (0, &tty);
|
159 | ea2384d3 | bellard | oldtty = tty; |
160 | ea2384d3 | bellard | |
161 | ea2384d3 | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
162 | ea2384d3 | bellard | |INLCR|IGNCR|ICRNL|IXON); |
163 | ea2384d3 | bellard | tty.c_oflag |= OPOST; |
164 | ea2384d3 | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
165 | ea2384d3 | bellard | tty.c_cflag &= ~(CSIZE|PARENB); |
166 | ea2384d3 | bellard | tty.c_cflag |= CS8; |
167 | ea2384d3 | bellard | tty.c_cc[VMIN] = 1;
|
168 | ea2384d3 | bellard | tty.c_cc[VTIME] = 0;
|
169 | ea2384d3 | bellard | |
170 | ea2384d3 | bellard | tcsetattr (0, TCSANOW, &tty);
|
171 | ea2384d3 | bellard | |
172 | ea2384d3 | bellard | atexit(term_exit); |
173 | ea2384d3 | bellard | } |
174 | ea2384d3 | bellard | |
175 | ea2384d3 | bellard | int read_password(char *buf, int buf_size) |
176 | ea2384d3 | bellard | { |
177 | ea2384d3 | bellard | uint8_t ch; |
178 | ea2384d3 | bellard | int i, ret;
|
179 | ea2384d3 | bellard | |
180 | ea2384d3 | bellard | printf("password: ");
|
181 | ea2384d3 | bellard | fflush(stdout); |
182 | ea2384d3 | bellard | term_init(); |
183 | ea2384d3 | bellard | i = 0;
|
184 | ea2384d3 | bellard | for(;;) {
|
185 | ea2384d3 | bellard | ret = read(0, &ch, 1); |
186 | ea2384d3 | bellard | if (ret == -1) { |
187 | ea2384d3 | bellard | if (errno == EAGAIN || errno == EINTR) {
|
188 | ea2384d3 | bellard | continue;
|
189 | ea2384d3 | bellard | } else {
|
190 | ea2384d3 | bellard | ret = -1;
|
191 | ea2384d3 | bellard | break;
|
192 | ea2384d3 | bellard | } |
193 | ea2384d3 | bellard | } else if (ret == 0) { |
194 | ea2384d3 | bellard | ret = -1;
|
195 | ea2384d3 | bellard | break;
|
196 | ea2384d3 | bellard | } else {
|
197 | ea2384d3 | bellard | if (ch == '\r') { |
198 | ea2384d3 | bellard | ret = 0;
|
199 | ea2384d3 | bellard | break;
|
200 | ea2384d3 | bellard | } |
201 | ea2384d3 | bellard | if (i < (buf_size - 1)) |
202 | ea2384d3 | bellard | buf[i++] = ch; |
203 | ea2384d3 | bellard | } |
204 | ea2384d3 | bellard | } |
205 | ea2384d3 | bellard | term_exit(); |
206 | ea2384d3 | bellard | buf[i] = '\0';
|
207 | ea2384d3 | bellard | printf("\n");
|
208 | ea2384d3 | bellard | return ret;
|
209 | ea2384d3 | bellard | } |
210 | ea2384d3 | bellard | #endif
|
211 | ea2384d3 | bellard | |
212 | 75c23805 | bellard | static BlockDriverState *bdrv_new_open(const char *filename, |
213 | 75c23805 | bellard | const char *fmt) |
214 | 75c23805 | bellard | { |
215 | 75c23805 | bellard | BlockDriverState *bs; |
216 | 75c23805 | bellard | BlockDriver *drv; |
217 | 75c23805 | bellard | char password[256]; |
218 | 75c23805 | bellard | |
219 | 75c23805 | bellard | bs = bdrv_new("");
|
220 | 75c23805 | bellard | if (!bs)
|
221 | 75c23805 | bellard | error("Not enough memory");
|
222 | 75c23805 | bellard | if (fmt) {
|
223 | 75c23805 | bellard | drv = bdrv_find_format(fmt); |
224 | 75c23805 | bellard | if (!drv)
|
225 | 75c23805 | bellard | error("Unknown file format '%s'", fmt);
|
226 | 75c23805 | bellard | } else {
|
227 | 75c23805 | bellard | drv = NULL;
|
228 | 75c23805 | bellard | } |
229 | 75c23805 | bellard | if (bdrv_open2(bs, filename, 0, drv) < 0) { |
230 | 75c23805 | bellard | error("Could not open '%s'", filename);
|
231 | 75c23805 | bellard | } |
232 | 75c23805 | bellard | if (bdrv_is_encrypted(bs)) {
|
233 | 75c23805 | bellard | printf("Disk image '%s' is encrypted.\n", filename);
|
234 | 75c23805 | bellard | if (read_password(password, sizeof(password)) < 0) |
235 | 75c23805 | bellard | error("No password given");
|
236 | 75c23805 | bellard | if (bdrv_set_key(bs, password) < 0) |
237 | 75c23805 | bellard | error("invalid password");
|
238 | 75c23805 | bellard | } |
239 | 75c23805 | bellard | return bs;
|
240 | 75c23805 | bellard | } |
241 | 75c23805 | bellard | |
242 | ea2384d3 | bellard | static int img_create(int argc, char **argv) |
243 | ea2384d3 | bellard | { |
244 | ea2384d3 | bellard | int c, ret, encrypted;
|
245 | ea2384d3 | bellard | const char *fmt = "raw"; |
246 | ea2384d3 | bellard | const char *filename; |
247 | ea2384d3 | bellard | const char *base_filename = NULL; |
248 | ea2384d3 | bellard | int64_t size; |
249 | ea2384d3 | bellard | const char *p; |
250 | ea2384d3 | bellard | BlockDriver *drv; |
251 | ea2384d3 | bellard | |
252 | ea2384d3 | bellard | encrypted = 0;
|
253 | ea2384d3 | bellard | for(;;) {
|
254 | ea2384d3 | bellard | c = getopt(argc, argv, "b:f:he");
|
255 | ea2384d3 | bellard | if (c == -1) |
256 | ea2384d3 | bellard | break;
|
257 | ea2384d3 | bellard | switch(c) {
|
258 | ea2384d3 | bellard | case 'h': |
259 | ea2384d3 | bellard | help(); |
260 | ea2384d3 | bellard | break;
|
261 | ea2384d3 | bellard | case 'b': |
262 | ea2384d3 | bellard | base_filename = optarg; |
263 | ea2384d3 | bellard | break;
|
264 | ea2384d3 | bellard | case 'f': |
265 | ea2384d3 | bellard | fmt = optarg; |
266 | ea2384d3 | bellard | break;
|
267 | ea2384d3 | bellard | case 'e': |
268 | ea2384d3 | bellard | encrypted = 1;
|
269 | ea2384d3 | bellard | break;
|
270 | ea2384d3 | bellard | } |
271 | ea2384d3 | bellard | } |
272 | ea2384d3 | bellard | if (optind >= argc)
|
273 | ea2384d3 | bellard | help(); |
274 | ea2384d3 | bellard | filename = argv[optind++]; |
275 | ea2384d3 | bellard | size = 0;
|
276 | 75c23805 | bellard | if (base_filename) {
|
277 | 75c23805 | bellard | BlockDriverState *bs; |
278 | 75c23805 | bellard | bs = bdrv_new_open(base_filename, NULL);
|
279 | 75c23805 | bellard | bdrv_get_geometry(bs, &size); |
280 | 75c23805 | bellard | size *= 512;
|
281 | 75c23805 | bellard | bdrv_delete(bs); |
282 | 75c23805 | bellard | } else {
|
283 | ea2384d3 | bellard | if (optind >= argc)
|
284 | ea2384d3 | bellard | help(); |
285 | ea2384d3 | bellard | p = argv[optind]; |
286 | ea2384d3 | bellard | size = strtoul(p, (char **)&p, 0); |
287 | ea2384d3 | bellard | if (*p == 'M') { |
288 | ea2384d3 | bellard | size *= 1024 * 1024; |
289 | ea2384d3 | bellard | } else if (*p == 'G') { |
290 | ea2384d3 | bellard | size *= 1024 * 1024 * 1024; |
291 | ea2384d3 | bellard | } else if (*p == 'k' || *p == 'K' || *p == '\0') { |
292 | ea2384d3 | bellard | size *= 1024;
|
293 | ea2384d3 | bellard | } else {
|
294 | ea2384d3 | bellard | help(); |
295 | ea2384d3 | bellard | } |
296 | ea2384d3 | bellard | } |
297 | ea2384d3 | bellard | drv = bdrv_find_format(fmt); |
298 | ea2384d3 | bellard | if (!drv)
|
299 | ea2384d3 | bellard | error("Unknown file format '%s'", fmt);
|
300 | ea2384d3 | bellard | printf("Formating '%s', fmt=%s",
|
301 | ea2384d3 | bellard | filename, fmt); |
302 | ea2384d3 | bellard | if (encrypted)
|
303 | ea2384d3 | bellard | printf(", encrypted");
|
304 | 75c23805 | bellard | if (base_filename) {
|
305 | 75c23805 | bellard | printf(", backing_file=%s",
|
306 | ea2384d3 | bellard | base_filename); |
307 | 75c23805 | bellard | } |
308 | ec3757de | bellard | printf(", size=%" PRId64 " kB\n", (int64_t) (size / 1024)); |
309 | ea2384d3 | bellard | ret = bdrv_create(drv, filename, size / 512, base_filename, encrypted);
|
310 | ea2384d3 | bellard | if (ret < 0) { |
311 | ea2384d3 | bellard | if (ret == -ENOTSUP) {
|
312 | 3c56521b | bellard | error("Formatting or formatting option not supported for file format '%s'", fmt);
|
313 | ea2384d3 | bellard | } else {
|
314 | ea2384d3 | bellard | error("Error while formatting");
|
315 | ea2384d3 | bellard | } |
316 | ea2384d3 | bellard | } |
317 | ea2384d3 | bellard | return 0; |
318 | ea2384d3 | bellard | } |
319 | ea2384d3 | bellard | |
320 | ea2384d3 | bellard | static int img_commit(int argc, char **argv) |
321 | ea2384d3 | bellard | { |
322 | ea2384d3 | bellard | int c, ret;
|
323 | ea2384d3 | bellard | const char *filename, *fmt; |
324 | ea2384d3 | bellard | BlockDriver *drv; |
325 | ea2384d3 | bellard | BlockDriverState *bs; |
326 | ea2384d3 | bellard | |
327 | ea2384d3 | bellard | fmt = NULL;
|
328 | ea2384d3 | bellard | for(;;) {
|
329 | ea2384d3 | bellard | c = getopt(argc, argv, "f:h");
|
330 | ea2384d3 | bellard | if (c == -1) |
331 | ea2384d3 | bellard | break;
|
332 | ea2384d3 | bellard | switch(c) {
|
333 | ea2384d3 | bellard | case 'h': |
334 | ea2384d3 | bellard | help(); |
335 | ea2384d3 | bellard | break;
|
336 | ea2384d3 | bellard | case 'f': |
337 | ea2384d3 | bellard | fmt = optarg; |
338 | ea2384d3 | bellard | break;
|
339 | ea2384d3 | bellard | } |
340 | ea2384d3 | bellard | } |
341 | ea2384d3 | bellard | if (optind >= argc)
|
342 | ea2384d3 | bellard | help(); |
343 | ea2384d3 | bellard | filename = argv[optind++]; |
344 | ea2384d3 | bellard | |
345 | ea2384d3 | bellard | bs = bdrv_new("");
|
346 | ea2384d3 | bellard | if (!bs)
|
347 | ea2384d3 | bellard | error("Not enough memory");
|
348 | ea2384d3 | bellard | if (fmt) {
|
349 | ea2384d3 | bellard | drv = bdrv_find_format(fmt); |
350 | ea2384d3 | bellard | if (!drv)
|
351 | ea2384d3 | bellard | error("Unknown file format '%s'", fmt);
|
352 | ea2384d3 | bellard | } else {
|
353 | ea2384d3 | bellard | drv = NULL;
|
354 | ea2384d3 | bellard | } |
355 | ea2384d3 | bellard | if (bdrv_open2(bs, filename, 0, drv) < 0) { |
356 | ea2384d3 | bellard | error("Could not open '%s'", filename);
|
357 | ea2384d3 | bellard | } |
358 | ea2384d3 | bellard | ret = bdrv_commit(bs); |
359 | ea2384d3 | bellard | switch(ret) {
|
360 | ea2384d3 | bellard | case 0: |
361 | ea2384d3 | bellard | printf("Image committed.\n");
|
362 | ea2384d3 | bellard | break;
|
363 | ea2384d3 | bellard | case -ENOENT:
|
364 | ea2384d3 | bellard | error("No disk inserted");
|
365 | ea2384d3 | bellard | break;
|
366 | ea2384d3 | bellard | case -EACCES:
|
367 | ea2384d3 | bellard | error("Image is read-only");
|
368 | ea2384d3 | bellard | break;
|
369 | ea2384d3 | bellard | case -ENOTSUP:
|
370 | ea2384d3 | bellard | error("Image is already committed");
|
371 | ea2384d3 | bellard | break;
|
372 | ea2384d3 | bellard | default:
|
373 | ea2384d3 | bellard | error("Error while committing image");
|
374 | ea2384d3 | bellard | break;
|
375 | ea2384d3 | bellard | } |
376 | ea2384d3 | bellard | |
377 | ea2384d3 | bellard | bdrv_delete(bs); |
378 | ea2384d3 | bellard | return 0; |
379 | ea2384d3 | bellard | } |
380 | ea2384d3 | bellard | |
381 | ea2384d3 | bellard | static int is_not_zero(const uint8_t *sector, int len) |
382 | ea2384d3 | bellard | { |
383 | ea2384d3 | bellard | int i;
|
384 | ea2384d3 | bellard | len >>= 2;
|
385 | ea2384d3 | bellard | for(i = 0;i < len; i++) { |
386 | ea2384d3 | bellard | if (((uint32_t *)sector)[i] != 0) |
387 | ea2384d3 | bellard | return 1; |
388 | ea2384d3 | bellard | } |
389 | ea2384d3 | bellard | return 0; |
390 | ea2384d3 | bellard | } |
391 | ea2384d3 | bellard | |
392 | ea2384d3 | bellard | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
393 | ea2384d3 | bellard | { |
394 | ea2384d3 | bellard | int v, i;
|
395 | ea2384d3 | bellard | |
396 | ea2384d3 | bellard | if (n <= 0) { |
397 | ea2384d3 | bellard | *pnum = 0;
|
398 | ea2384d3 | bellard | return 0; |
399 | ea2384d3 | bellard | } |
400 | ea2384d3 | bellard | v = is_not_zero(buf, 512);
|
401 | ea2384d3 | bellard | for(i = 1; i < n; i++) { |
402 | ea2384d3 | bellard | buf += 512;
|
403 | ea2384d3 | bellard | if (v != is_not_zero(buf, 512)) |
404 | ea2384d3 | bellard | break;
|
405 | ea2384d3 | bellard | } |
406 | ea2384d3 | bellard | *pnum = i; |
407 | ea2384d3 | bellard | return v;
|
408 | ea2384d3 | bellard | } |
409 | ea2384d3 | bellard | |
410 | ea2384d3 | bellard | #define IO_BUF_SIZE 65536 |
411 | ea2384d3 | bellard | |
412 | ea2384d3 | bellard | static int img_convert(int argc, char **argv) |
413 | ea2384d3 | bellard | { |
414 | ea2384d3 | bellard | int c, ret, n, n1, compress, cluster_size, cluster_sectors, encrypt;
|
415 | ea2384d3 | bellard | const char *filename, *fmt, *out_fmt, *out_filename; |
416 | ea2384d3 | bellard | BlockDriver *drv; |
417 | ea2384d3 | bellard | BlockDriverState *bs, *out_bs; |
418 | ea2384d3 | bellard | int64_t total_sectors, nb_sectors, sector_num; |
419 | ea2384d3 | bellard | uint8_t buf[IO_BUF_SIZE]; |
420 | ea2384d3 | bellard | const uint8_t *buf1;
|
421 | faea38e7 | bellard | BlockDriverInfo bdi; |
422 | ea2384d3 | bellard | |
423 | ea2384d3 | bellard | fmt = NULL;
|
424 | ea2384d3 | bellard | out_fmt = "raw";
|
425 | ea2384d3 | bellard | compress = 0;
|
426 | ea2384d3 | bellard | encrypt = 0;
|
427 | ea2384d3 | bellard | for(;;) {
|
428 | ea2384d3 | bellard | c = getopt(argc, argv, "f:O:hce");
|
429 | ea2384d3 | bellard | if (c == -1) |
430 | ea2384d3 | bellard | break;
|
431 | ea2384d3 | bellard | switch(c) {
|
432 | ea2384d3 | bellard | case 'h': |
433 | ea2384d3 | bellard | help(); |
434 | ea2384d3 | bellard | break;
|
435 | ea2384d3 | bellard | case 'f': |
436 | ea2384d3 | bellard | fmt = optarg; |
437 | ea2384d3 | bellard | break;
|
438 | ea2384d3 | bellard | case 'O': |
439 | ea2384d3 | bellard | out_fmt = optarg; |
440 | ea2384d3 | bellard | break;
|
441 | ea2384d3 | bellard | case 'c': |
442 | ea2384d3 | bellard | compress = 1;
|
443 | ea2384d3 | bellard | break;
|
444 | ea2384d3 | bellard | case 'e': |
445 | ea2384d3 | bellard | encrypt = 1;
|
446 | ea2384d3 | bellard | break;
|
447 | ea2384d3 | bellard | } |
448 | ea2384d3 | bellard | } |
449 | ea2384d3 | bellard | if (optind >= argc)
|
450 | ea2384d3 | bellard | help(); |
451 | ea2384d3 | bellard | filename = argv[optind++]; |
452 | ea2384d3 | bellard | if (optind >= argc)
|
453 | ea2384d3 | bellard | help(); |
454 | ea2384d3 | bellard | out_filename = argv[optind++]; |
455 | ea2384d3 | bellard | |
456 | ea2384d3 | bellard | bs = bdrv_new_open(filename, fmt); |
457 | ea2384d3 | bellard | |
458 | ea2384d3 | bellard | drv = bdrv_find_format(out_fmt); |
459 | ea2384d3 | bellard | if (!drv)
|
460 | ea2384d3 | bellard | error("Unknown file format '%s'", fmt);
|
461 | faea38e7 | bellard | if (compress && drv != &bdrv_qcow && drv != &bdrv_qcow2)
|
462 | ea2384d3 | bellard | error("Compression not supported for this file format");
|
463 | faea38e7 | bellard | if (encrypt && drv != &bdrv_qcow && drv != &bdrv_qcow2)
|
464 | ea2384d3 | bellard | error("Encryption not supported for this file format");
|
465 | ea2384d3 | bellard | if (compress && encrypt)
|
466 | ea2384d3 | bellard | error("Compression and encryption not supported at the same time");
|
467 | ea2384d3 | bellard | bdrv_get_geometry(bs, &total_sectors); |
468 | ea2384d3 | bellard | ret = bdrv_create(drv, out_filename, total_sectors, NULL, encrypt);
|
469 | ea2384d3 | bellard | if (ret < 0) { |
470 | ea2384d3 | bellard | if (ret == -ENOTSUP) {
|
471 | 3c56521b | bellard | error("Formatting not supported for file format '%s'", fmt);
|
472 | ea2384d3 | bellard | } else {
|
473 | ea2384d3 | bellard | error("Error while formatting '%s'", out_filename);
|
474 | ea2384d3 | bellard | } |
475 | ea2384d3 | bellard | } |
476 | ea2384d3 | bellard | |
477 | ea2384d3 | bellard | out_bs = bdrv_new_open(out_filename, out_fmt); |
478 | ea2384d3 | bellard | |
479 | ea2384d3 | bellard | if (compress) {
|
480 | faea38e7 | bellard | if (bdrv_get_info(out_bs, &bdi) < 0) |
481 | faea38e7 | bellard | error("could not get block driver info");
|
482 | faea38e7 | bellard | cluster_size = bdi.cluster_size; |
483 | ea2384d3 | bellard | if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) |
484 | ea2384d3 | bellard | error("invalid cluster size");
|
485 | ea2384d3 | bellard | cluster_sectors = cluster_size >> 9;
|
486 | ea2384d3 | bellard | sector_num = 0;
|
487 | ea2384d3 | bellard | for(;;) {
|
488 | ea2384d3 | bellard | nb_sectors = total_sectors - sector_num; |
489 | ea2384d3 | bellard | if (nb_sectors <= 0) |
490 | ea2384d3 | bellard | break;
|
491 | ea2384d3 | bellard | if (nb_sectors >= cluster_sectors)
|
492 | ea2384d3 | bellard | n = cluster_sectors; |
493 | ea2384d3 | bellard | else
|
494 | ea2384d3 | bellard | n = nb_sectors; |
495 | ea2384d3 | bellard | if (bdrv_read(bs, sector_num, buf, n) < 0) |
496 | ea2384d3 | bellard | error("error while reading");
|
497 | ea2384d3 | bellard | if (n < cluster_sectors)
|
498 | ea2384d3 | bellard | memset(buf + n * 512, 0, cluster_size - n * 512); |
499 | ea2384d3 | bellard | if (is_not_zero(buf, cluster_size)) {
|
500 | faea38e7 | bellard | if (bdrv_write_compressed(out_bs, sector_num, buf,
|
501 | faea38e7 | bellard | cluster_sectors) != 0)
|
502 | ec3757de | bellard | error("error while compressing sector %" PRId64,
|
503 | ec3757de | bellard | sector_num); |
504 | ea2384d3 | bellard | } |
505 | ea2384d3 | bellard | sector_num += n; |
506 | ea2384d3 | bellard | } |
507 | faea38e7 | bellard | /* signal EOF to align */
|
508 | faea38e7 | bellard | bdrv_write_compressed(out_bs, 0, NULL, 0); |
509 | ea2384d3 | bellard | } else {
|
510 | ea2384d3 | bellard | sector_num = 0;
|
511 | ea2384d3 | bellard | for(;;) {
|
512 | ea2384d3 | bellard | nb_sectors = total_sectors - sector_num; |
513 | ea2384d3 | bellard | if (nb_sectors <= 0) |
514 | ea2384d3 | bellard | break;
|
515 | ea2384d3 | bellard | if (nb_sectors >= (IO_BUF_SIZE / 512)) |
516 | ea2384d3 | bellard | n = (IO_BUF_SIZE / 512);
|
517 | ea2384d3 | bellard | else
|
518 | ea2384d3 | bellard | n = nb_sectors; |
519 | ea2384d3 | bellard | if (bdrv_read(bs, sector_num, buf, n) < 0) |
520 | ea2384d3 | bellard | error("error while reading");
|
521 | ea2384d3 | bellard | /* NOTE: at the same time we convert, we do not write zero
|
522 | ea2384d3 | bellard | sectors to have a chance to compress the image. Ideally, we
|
523 | ea2384d3 | bellard | should add a specific call to have the info to go faster */
|
524 | ea2384d3 | bellard | buf1 = buf; |
525 | ea2384d3 | bellard | while (n > 0) { |
526 | ea2384d3 | bellard | if (is_allocated_sectors(buf1, n, &n1)) {
|
527 | ea2384d3 | bellard | if (bdrv_write(out_bs, sector_num, buf1, n1) < 0) |
528 | ea2384d3 | bellard | error("error while writing");
|
529 | ea2384d3 | bellard | } |
530 | ea2384d3 | bellard | sector_num += n1; |
531 | ea2384d3 | bellard | n -= n1; |
532 | ea2384d3 | bellard | buf1 += n1 * 512;
|
533 | ea2384d3 | bellard | } |
534 | ea2384d3 | bellard | } |
535 | ea2384d3 | bellard | } |
536 | ea2384d3 | bellard | bdrv_delete(out_bs); |
537 | ea2384d3 | bellard | bdrv_delete(bs); |
538 | ea2384d3 | bellard | return 0; |
539 | ea2384d3 | bellard | } |
540 | ea2384d3 | bellard | |
541 | 57d1a2b6 | bellard | #ifdef _WIN32
|
542 | 57d1a2b6 | bellard | static int64_t get_allocated_file_size(const char *filename) |
543 | 57d1a2b6 | bellard | { |
544 | e8445331 | bellard | typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high); |
545 | e8445331 | bellard | get_compressed_t get_compressed; |
546 | 57d1a2b6 | bellard | struct _stati64 st;
|
547 | e8445331 | bellard | |
548 | e8445331 | bellard | /* WinNT support GetCompressedFileSize to determine allocate size */
|
549 | e8445331 | bellard | get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA"); |
550 | e8445331 | bellard | if (get_compressed) {
|
551 | e8445331 | bellard | DWORD high, low; |
552 | e8445331 | bellard | low = get_compressed(filename, &high); |
553 | e8445331 | bellard | if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) |
554 | e8445331 | bellard | return (((int64_t) high) << 32) + low; |
555 | e8445331 | bellard | } |
556 | e8445331 | bellard | |
557 | 57d1a2b6 | bellard | if (_stati64(filename, &st) < 0) |
558 | 57d1a2b6 | bellard | return -1; |
559 | 57d1a2b6 | bellard | return st.st_size;
|
560 | 57d1a2b6 | bellard | } |
561 | 57d1a2b6 | bellard | #else
|
562 | 57d1a2b6 | bellard | static int64_t get_allocated_file_size(const char *filename) |
563 | 57d1a2b6 | bellard | { |
564 | 57d1a2b6 | bellard | struct stat st;
|
565 | 57d1a2b6 | bellard | if (stat(filename, &st) < 0) |
566 | 57d1a2b6 | bellard | return -1; |
567 | 57d1a2b6 | bellard | return (int64_t)st.st_blocks * 512; |
568 | 57d1a2b6 | bellard | } |
569 | 57d1a2b6 | bellard | #endif
|
570 | 57d1a2b6 | bellard | |
571 | faea38e7 | bellard | static void dump_snapshots(BlockDriverState *bs) |
572 | faea38e7 | bellard | { |
573 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
574 | faea38e7 | bellard | int nb_sns, i;
|
575 | faea38e7 | bellard | char buf[256]; |
576 | faea38e7 | bellard | |
577 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
578 | faea38e7 | bellard | if (nb_sns <= 0) |
579 | faea38e7 | bellard | return;
|
580 | faea38e7 | bellard | printf("Snapshot list:\n");
|
581 | faea38e7 | bellard | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
582 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
583 | faea38e7 | bellard | sn = &sn_tab[i]; |
584 | faea38e7 | bellard | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
585 | faea38e7 | bellard | } |
586 | faea38e7 | bellard | qemu_free(sn_tab); |
587 | faea38e7 | bellard | } |
588 | faea38e7 | bellard | |
589 | ea2384d3 | bellard | static int img_info(int argc, char **argv) |
590 | ea2384d3 | bellard | { |
591 | ea2384d3 | bellard | int c;
|
592 | ea2384d3 | bellard | const char *filename, *fmt; |
593 | ea2384d3 | bellard | BlockDriver *drv; |
594 | ea2384d3 | bellard | BlockDriverState *bs; |
595 | ea2384d3 | bellard | char fmt_name[128], size_buf[128], dsize_buf[128]; |
596 | 57d1a2b6 | bellard | int64_t total_sectors, allocated_size; |
597 | 93b6b2a3 | bellard | char backing_filename[1024]; |
598 | 93b6b2a3 | bellard | char backing_filename2[1024]; |
599 | faea38e7 | bellard | BlockDriverInfo bdi; |
600 | ea2384d3 | bellard | |
601 | ea2384d3 | bellard | fmt = NULL;
|
602 | ea2384d3 | bellard | for(;;) {
|
603 | ea2384d3 | bellard | c = getopt(argc, argv, "f:h");
|
604 | ea2384d3 | bellard | if (c == -1) |
605 | ea2384d3 | bellard | break;
|
606 | ea2384d3 | bellard | switch(c) {
|
607 | ea2384d3 | bellard | case 'h': |
608 | ea2384d3 | bellard | help(); |
609 | ea2384d3 | bellard | break;
|
610 | ea2384d3 | bellard | case 'f': |
611 | ea2384d3 | bellard | fmt = optarg; |
612 | ea2384d3 | bellard | break;
|
613 | ea2384d3 | bellard | } |
614 | ea2384d3 | bellard | } |
615 | ea2384d3 | bellard | if (optind >= argc)
|
616 | ea2384d3 | bellard | help(); |
617 | ea2384d3 | bellard | filename = argv[optind++]; |
618 | ea2384d3 | bellard | |
619 | ea2384d3 | bellard | bs = bdrv_new("");
|
620 | ea2384d3 | bellard | if (!bs)
|
621 | ea2384d3 | bellard | error("Not enough memory");
|
622 | ea2384d3 | bellard | if (fmt) {
|
623 | ea2384d3 | bellard | drv = bdrv_find_format(fmt); |
624 | ea2384d3 | bellard | if (!drv)
|
625 | ea2384d3 | bellard | error("Unknown file format '%s'", fmt);
|
626 | ea2384d3 | bellard | } else {
|
627 | ea2384d3 | bellard | drv = NULL;
|
628 | ea2384d3 | bellard | } |
629 | ea2384d3 | bellard | if (bdrv_open2(bs, filename, 0, drv) < 0) { |
630 | ea2384d3 | bellard | error("Could not open '%s'", filename);
|
631 | ea2384d3 | bellard | } |
632 | ea2384d3 | bellard | bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
|
633 | ea2384d3 | bellard | bdrv_get_geometry(bs, &total_sectors); |
634 | ea2384d3 | bellard | get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512); |
635 | 57d1a2b6 | bellard | allocated_size = get_allocated_file_size(filename); |
636 | 57d1a2b6 | bellard | if (allocated_size < 0) |
637 | de167e41 | bellard | sprintf(dsize_buf, "unavailable");
|
638 | de167e41 | bellard | else
|
639 | de167e41 | bellard | get_human_readable_size(dsize_buf, sizeof(dsize_buf),
|
640 | de167e41 | bellard | allocated_size); |
641 | ea2384d3 | bellard | printf("image: %s\n"
|
642 | ea2384d3 | bellard | "file format: %s\n"
|
643 | ec3757de | bellard | "virtual size: %s (%" PRId64 " bytes)\n" |
644 | ea2384d3 | bellard | "disk size: %s\n",
|
645 | ea2384d3 | bellard | filename, fmt_name, size_buf, |
646 | ec3757de | bellard | (total_sectors * 512),
|
647 | ea2384d3 | bellard | dsize_buf); |
648 | ea2384d3 | bellard | if (bdrv_is_encrypted(bs))
|
649 | ea2384d3 | bellard | printf("encrypted: yes\n");
|
650 | faea38e7 | bellard | if (bdrv_get_info(bs, &bdi) >= 0) { |
651 | faea38e7 | bellard | if (bdi.cluster_size != 0) |
652 | faea38e7 | bellard | printf("cluster_size: %d\n", bdi.cluster_size);
|
653 | faea38e7 | bellard | } |
654 | 93b6b2a3 | bellard | bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
|
655 | faea38e7 | bellard | if (backing_filename[0] != '\0') { |
656 | 93b6b2a3 | bellard | path_combine(backing_filename2, sizeof(backing_filename2),
|
657 | 93b6b2a3 | bellard | filename, backing_filename); |
658 | 93b6b2a3 | bellard | printf("backing file: %s (actual path: %s)\n",
|
659 | 93b6b2a3 | bellard | backing_filename, |
660 | 93b6b2a3 | bellard | backing_filename2); |
661 | faea38e7 | bellard | } |
662 | faea38e7 | bellard | dump_snapshots(bs); |
663 | ea2384d3 | bellard | bdrv_delete(bs); |
664 | ea2384d3 | bellard | return 0; |
665 | ea2384d3 | bellard | } |
666 | ea2384d3 | bellard | |
667 | ea2384d3 | bellard | int main(int argc, char **argv) |
668 | ea2384d3 | bellard | { |
669 | ea2384d3 | bellard | const char *cmd; |
670 | ea2384d3 | bellard | |
671 | ea2384d3 | bellard | bdrv_init(); |
672 | ea2384d3 | bellard | if (argc < 2) |
673 | ea2384d3 | bellard | help(); |
674 | ea2384d3 | bellard | cmd = argv[1];
|
675 | e3888186 | bellard | optind++; |
676 | ea2384d3 | bellard | if (!strcmp(cmd, "create")) { |
677 | ea2384d3 | bellard | img_create(argc, argv); |
678 | ea2384d3 | bellard | } else if (!strcmp(cmd, "commit")) { |
679 | ea2384d3 | bellard | img_commit(argc, argv); |
680 | ea2384d3 | bellard | } else if (!strcmp(cmd, "convert")) { |
681 | ea2384d3 | bellard | img_convert(argc, argv); |
682 | ea2384d3 | bellard | } else if (!strcmp(cmd, "info")) { |
683 | ea2384d3 | bellard | img_info(argc, argv); |
684 | ea2384d3 | bellard | } else {
|
685 | ea2384d3 | bellard | help(); |
686 | ea2384d3 | bellard | } |
687 | ea2384d3 | bellard | return 0; |
688 | ea2384d3 | bellard | } |