Statistics
| Branch: | Revision:

root / qemu-img.c @ 926c2d23

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