Statistics
| Branch: | Revision:

root / qemu-img.c @ 8984bd2e

History | View | Annotate | Download (20.4 kB)

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