Statistics
| Branch: | Revision:

root / qemu-img.c @ 0ba22212

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