Statistics
| Branch: | Revision:

root / qemu-img.c @ d8871c5a

History | View | Annotate | Download (18.5 kB)

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