Statistics
| Branch: | Revision:

root / qemu-img.c @ 8c5e95d8

History | View | Annotate | Download (21.6 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 3f379ab1 pbrook
static void __attribute__((noreturn)) error(const char *fmt, ...)
34 ea2384d3 bellard
{
35 ea2384d3 bellard
    va_list ap;
36 ea2384d3 bellard
    va_start(ap, fmt);
37 57d1a2b6 bellard
    fprintf(stderr, "qemu-img: ");
38 ea2384d3 bellard
    vfprintf(stderr, fmt, ap);
39 ea2384d3 bellard
    fprintf(stderr, "\n");
40 ea2384d3 bellard
    exit(1);
41 ea2384d3 bellard
    va_end(ap);
42 ea2384d3 bellard
}
43 ea2384d3 bellard
44 ea2384d3 bellard
static void format_print(void *opaque, const char *name)
45 ea2384d3 bellard
{
46 ea2384d3 bellard
    printf(" %s", name);
47 ea2384d3 bellard
}
48 ea2384d3 bellard
49 3f379ab1 pbrook
static void help(void)
50 ea2384d3 bellard
{
51 68d0f70e bellard
    printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
52 57d1a2b6 bellard
           "usage: qemu-img command [command options]\n"
53 ea2384d3 bellard
           "QEMU disk image utility\n"
54 ea2384d3 bellard
           "\n"
55 ea2384d3 bellard
           "Command syntax:\n"
56 ec36ba14 ths
           "  create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
57 ea2384d3 bellard
           "  commit [-f fmt] filename\n"
58 f58c7b35 ths
           "  convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
59 ea2384d3 bellard
           "  info [-f fmt] filename\n"
60 ea2384d3 bellard
           "\n"
61 ea2384d3 bellard
           "Command parameters:\n"
62 ea2384d3 bellard
           "  'filename' is a disk image filename\n"
63 ea2384d3 bellard
           "  'base_image' is the read-only disk image which is used as base for a copy on\n"
64 ea2384d3 bellard
           "    write image; the copy on write image only stores the modified data\n"
65 f58c7b35 ths
           "  'output_base_image' forces the output image to be created as a copy on write\n"
66 f58c7b35 ths
           "    image of the specified base image; 'output_base_image' should have the same\n"
67 f58c7b35 ths
           "    content as the input's base image, however the path, image format, etc may\n"
68 f58c7b35 ths
           "    differ\n"
69 ea2384d3 bellard
           "  'fmt' is the disk image format. It is guessed automatically in most cases\n"
70 ea2384d3 bellard
           "  'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
71 ea2384d3 bellard
           "    and 'G' (gigabyte) are supported\n"
72 ea2384d3 bellard
           "  'output_filename' is the destination disk image filename\n"
73 ea2384d3 bellard
           "  'output_fmt' is the destination format\n"
74 ea2384d3 bellard
           "  '-c' indicates that target image must be compressed (qcow format only)\n"
75 ea2384d3 bellard
           "  '-e' indicates that the target image must be encrypted (qcow format only)\n"
76 ec36ba14 ths
           "  '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
77 ea2384d3 bellard
           );
78 ea2384d3 bellard
    printf("\nSupported format:");
79 ea2384d3 bellard
    bdrv_iterate_format(format_print, NULL);
80 ea2384d3 bellard
    printf("\n");
81 ea2384d3 bellard
    exit(1);
82 ea2384d3 bellard
}
83 ea2384d3 bellard
84 ea2384d3 bellard
#if defined(WIN32)
85 ea2384d3 bellard
/* XXX: put correct support for win32 */
86 ea2384d3 bellard
static int read_password(char *buf, int buf_size)
87 ea2384d3 bellard
{
88 ea2384d3 bellard
    int c, i;
89 ea2384d3 bellard
    printf("Password: ");
90 ea2384d3 bellard
    fflush(stdout);
91 ea2384d3 bellard
    i = 0;
92 ea2384d3 bellard
    for(;;) {
93 ea2384d3 bellard
        c = getchar();
94 ea2384d3 bellard
        if (c == '\n')
95 ea2384d3 bellard
            break;
96 ea2384d3 bellard
        if (i < (buf_size - 1))
97 ea2384d3 bellard
            buf[i++] = c;
98 ea2384d3 bellard
    }
99 ea2384d3 bellard
    buf[i] = '\0';
100 ea2384d3 bellard
    return 0;
101 ea2384d3 bellard
}
102 ea2384d3 bellard
103 ea2384d3 bellard
#else
104 ea2384d3 bellard
105 ea2384d3 bellard
#include <termios.h>
106 ea2384d3 bellard
107 ea2384d3 bellard
static struct termios oldtty;
108 ea2384d3 bellard
109 ea2384d3 bellard
static void term_exit(void)
110 ea2384d3 bellard
{
111 ea2384d3 bellard
    tcsetattr (0, TCSANOW, &oldtty);
112 ea2384d3 bellard
}
113 ea2384d3 bellard
114 ea2384d3 bellard
static void term_init(void)
115 ea2384d3 bellard
{
116 ea2384d3 bellard
    struct termios tty;
117 ea2384d3 bellard
118 ea2384d3 bellard
    tcgetattr (0, &tty);
119 ea2384d3 bellard
    oldtty = tty;
120 ea2384d3 bellard
121 ea2384d3 bellard
    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
122 ea2384d3 bellard
                          |INLCR|IGNCR|ICRNL|IXON);
123 ea2384d3 bellard
    tty.c_oflag |= OPOST;
124 ea2384d3 bellard
    tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
125 ea2384d3 bellard
    tty.c_cflag &= ~(CSIZE|PARENB);
126 ea2384d3 bellard
    tty.c_cflag |= CS8;
127 ea2384d3 bellard
    tty.c_cc[VMIN] = 1;
128 ea2384d3 bellard
    tty.c_cc[VTIME] = 0;
129 3b46e624 ths
130 ea2384d3 bellard
    tcsetattr (0, TCSANOW, &tty);
131 ea2384d3 bellard
132 ea2384d3 bellard
    atexit(term_exit);
133 ea2384d3 bellard
}
134 ea2384d3 bellard
135 3f379ab1 pbrook
static int read_password(char *buf, int buf_size)
136 ea2384d3 bellard
{
137 ea2384d3 bellard
    uint8_t ch;
138 ea2384d3 bellard
    int i, ret;
139 ea2384d3 bellard
140 ea2384d3 bellard
    printf("password: ");
141 ea2384d3 bellard
    fflush(stdout);
142 ea2384d3 bellard
    term_init();
143 ea2384d3 bellard
    i = 0;
144 ea2384d3 bellard
    for(;;) {
145 ea2384d3 bellard
        ret = read(0, &ch, 1);
146 ea2384d3 bellard
        if (ret == -1) {
147 ea2384d3 bellard
            if (errno == EAGAIN || errno == EINTR) {
148 ea2384d3 bellard
                continue;
149 ea2384d3 bellard
            } else {
150 ea2384d3 bellard
                ret = -1;
151 ea2384d3 bellard
                break;
152 ea2384d3 bellard
            }
153 ea2384d3 bellard
        } else if (ret == 0) {
154 ea2384d3 bellard
            ret = -1;
155 ea2384d3 bellard
            break;
156 ea2384d3 bellard
        } else {
157 ea2384d3 bellard
            if (ch == '\r') {
158 ea2384d3 bellard
                ret = 0;
159 ea2384d3 bellard
                break;
160 ea2384d3 bellard
            }
161 ea2384d3 bellard
            if (i < (buf_size - 1))
162 ea2384d3 bellard
                buf[i++] = ch;
163 ea2384d3 bellard
        }
164 ea2384d3 bellard
    }
165 ea2384d3 bellard
    term_exit();
166 ea2384d3 bellard
    buf[i] = '\0';
167 ea2384d3 bellard
    printf("\n");
168 ea2384d3 bellard
    return ret;
169 ea2384d3 bellard
}
170 ea2384d3 bellard
#endif
171 ea2384d3 bellard
172 75c23805 bellard
static BlockDriverState *bdrv_new_open(const char *filename,
173 75c23805 bellard
                                       const char *fmt)
174 75c23805 bellard
{
175 75c23805 bellard
    BlockDriverState *bs;
176 75c23805 bellard
    BlockDriver *drv;
177 75c23805 bellard
    char password[256];
178 75c23805 bellard
179 75c23805 bellard
    bs = bdrv_new("");
180 75c23805 bellard
    if (!bs)
181 75c23805 bellard
        error("Not enough memory");
182 75c23805 bellard
    if (fmt) {
183 75c23805 bellard
        drv = bdrv_find_format(fmt);
184 75c23805 bellard
        if (!drv)
185 75c23805 bellard
            error("Unknown file format '%s'", fmt);
186 75c23805 bellard
    } else {
187 75c23805 bellard
        drv = NULL;
188 75c23805 bellard
    }
189 75c23805 bellard
    if (bdrv_open2(bs, filename, 0, drv) < 0) {
190 75c23805 bellard
        error("Could not open '%s'", filename);
191 75c23805 bellard
    }
192 75c23805 bellard
    if (bdrv_is_encrypted(bs)) {
193 75c23805 bellard
        printf("Disk image '%s' is encrypted.\n", filename);
194 75c23805 bellard
        if (read_password(password, sizeof(password)) < 0)
195 75c23805 bellard
            error("No password given");
196 75c23805 bellard
        if (bdrv_set_key(bs, password) < 0)
197 75c23805 bellard
            error("invalid password");
198 75c23805 bellard
    }
199 75c23805 bellard
    return bs;
200 75c23805 bellard
}
201 75c23805 bellard
202 ea2384d3 bellard
static int img_create(int argc, char **argv)
203 ea2384d3 bellard
{
204 ec36ba14 ths
    int c, ret, flags;
205 ea2384d3 bellard
    const char *fmt = "raw";
206 ea2384d3 bellard
    const char *filename;
207 ea2384d3 bellard
    const char *base_filename = NULL;
208 96b8f136 ths
    uint64_t size;
209 ea2384d3 bellard
    const char *p;
210 ea2384d3 bellard
    BlockDriver *drv;
211 3b46e624 ths
212 ec36ba14 ths
    flags = 0;
213 ea2384d3 bellard
    for(;;) {
214 ec36ba14 ths
        c = getopt(argc, argv, "b:f:he6");
215 ea2384d3 bellard
        if (c == -1)
216 ea2384d3 bellard
            break;
217 ea2384d3 bellard
        switch(c) {
218 ea2384d3 bellard
        case 'h':
219 ea2384d3 bellard
            help();
220 ea2384d3 bellard
            break;
221 ea2384d3 bellard
        case 'b':
222 ea2384d3 bellard
            base_filename = optarg;
223 ea2384d3 bellard
            break;
224 ea2384d3 bellard
        case 'f':
225 ea2384d3 bellard
            fmt = optarg;
226 ea2384d3 bellard
            break;
227 ea2384d3 bellard
        case 'e':
228 ec36ba14 ths
            flags |= BLOCK_FLAG_ENCRYPT;
229 ea2384d3 bellard
            break;
230 d8871c5a ths
        case '6':
231 ec36ba14 ths
            flags |= BLOCK_FLAG_COMPAT6;
232 d8871c5a ths
            break;
233 ea2384d3 bellard
        }
234 ea2384d3 bellard
    }
235 5fafdf24 ths
    if (optind >= argc)
236 ea2384d3 bellard
        help();
237 ea2384d3 bellard
    filename = argv[optind++];
238 ea2384d3 bellard
    size = 0;
239 75c23805 bellard
    if (base_filename) {
240 75c23805 bellard
        BlockDriverState *bs;
241 75c23805 bellard
        bs = bdrv_new_open(base_filename, NULL);
242 75c23805 bellard
        bdrv_get_geometry(bs, &size);
243 75c23805 bellard
        size *= 512;
244 75c23805 bellard
        bdrv_delete(bs);
245 75c23805 bellard
    } else {
246 ea2384d3 bellard
        if (optind >= argc)
247 ea2384d3 bellard
            help();
248 ea2384d3 bellard
        p = argv[optind];
249 ea2384d3 bellard
        size = strtoul(p, (char **)&p, 0);
250 ea2384d3 bellard
        if (*p == 'M') {
251 ea2384d3 bellard
            size *= 1024 * 1024;
252 ea2384d3 bellard
        } else if (*p == 'G') {
253 ea2384d3 bellard
            size *= 1024 * 1024 * 1024;
254 ea2384d3 bellard
        } else if (*p == 'k' || *p == 'K' || *p == '\0') {
255 ea2384d3 bellard
            size *= 1024;
256 ea2384d3 bellard
        } else {
257 ea2384d3 bellard
            help();
258 ea2384d3 bellard
        }
259 ea2384d3 bellard
    }
260 ea2384d3 bellard
    drv = bdrv_find_format(fmt);
261 ea2384d3 bellard
    if (!drv)
262 ea2384d3 bellard
        error("Unknown file format '%s'", fmt);
263 0cfec834 ths
    printf("Formatting '%s', fmt=%s",
264 ea2384d3 bellard
           filename, fmt);
265 ec36ba14 ths
    if (flags & BLOCK_FLAG_ENCRYPT)
266 ea2384d3 bellard
        printf(", encrypted");
267 ec36ba14 ths
    if (flags & BLOCK_FLAG_COMPAT6)
268 ec36ba14 ths
        printf(", compatibility level=6");
269 75c23805 bellard
    if (base_filename) {
270 75c23805 bellard
        printf(", backing_file=%s",
271 ea2384d3 bellard
               base_filename);
272 75c23805 bellard
    }
273 96b8f136 ths
    printf(", size=%" PRIu64 " kB\n", size / 1024);
274 ec36ba14 ths
    ret = bdrv_create(drv, filename, size / 512, base_filename, flags);
275 ea2384d3 bellard
    if (ret < 0) {
276 ea2384d3 bellard
        if (ret == -ENOTSUP) {
277 3c56521b bellard
            error("Formatting or formatting option not supported for file format '%s'", fmt);
278 ea2384d3 bellard
        } else {
279 ea2384d3 bellard
            error("Error while formatting");
280 ea2384d3 bellard
        }
281 ea2384d3 bellard
    }
282 ea2384d3 bellard
    return 0;
283 ea2384d3 bellard
}
284 ea2384d3 bellard
285 ea2384d3 bellard
static int img_commit(int argc, char **argv)
286 ea2384d3 bellard
{
287 ea2384d3 bellard
    int c, ret;
288 ea2384d3 bellard
    const char *filename, *fmt;
289 ea2384d3 bellard
    BlockDriver *drv;
290 ea2384d3 bellard
    BlockDriverState *bs;
291 ea2384d3 bellard
292 ea2384d3 bellard
    fmt = NULL;
293 ea2384d3 bellard
    for(;;) {
294 ea2384d3 bellard
        c = getopt(argc, argv, "f:h");
295 ea2384d3 bellard
        if (c == -1)
296 ea2384d3 bellard
            break;
297 ea2384d3 bellard
        switch(c) {
298 ea2384d3 bellard
        case 'h':
299 ea2384d3 bellard
            help();
300 ea2384d3 bellard
            break;
301 ea2384d3 bellard
        case 'f':
302 ea2384d3 bellard
            fmt = optarg;
303 ea2384d3 bellard
            break;
304 ea2384d3 bellard
        }
305 ea2384d3 bellard
    }
306 5fafdf24 ths
    if (optind >= argc)
307 ea2384d3 bellard
        help();
308 ea2384d3 bellard
    filename = argv[optind++];
309 ea2384d3 bellard
310 ea2384d3 bellard
    bs = bdrv_new("");
311 ea2384d3 bellard
    if (!bs)
312 ea2384d3 bellard
        error("Not enough memory");
313 ea2384d3 bellard
    if (fmt) {
314 ea2384d3 bellard
        drv = bdrv_find_format(fmt);
315 ea2384d3 bellard
        if (!drv)
316 ea2384d3 bellard
            error("Unknown file format '%s'", fmt);
317 ea2384d3 bellard
    } else {
318 ea2384d3 bellard
        drv = NULL;
319 ea2384d3 bellard
    }
320 ea2384d3 bellard
    if (bdrv_open2(bs, filename, 0, drv) < 0) {
321 ea2384d3 bellard
        error("Could not open '%s'", filename);
322 ea2384d3 bellard
    }
323 ea2384d3 bellard
    ret = bdrv_commit(bs);
324 ea2384d3 bellard
    switch(ret) {
325 ea2384d3 bellard
    case 0:
326 ea2384d3 bellard
        printf("Image committed.\n");
327 ea2384d3 bellard
        break;
328 ea2384d3 bellard
    case -ENOENT:
329 ea2384d3 bellard
        error("No disk inserted");
330 ea2384d3 bellard
        break;
331 ea2384d3 bellard
    case -EACCES:
332 ea2384d3 bellard
        error("Image is read-only");
333 ea2384d3 bellard
        break;
334 ea2384d3 bellard
    case -ENOTSUP:
335 ea2384d3 bellard
        error("Image is already committed");
336 ea2384d3 bellard
        break;
337 ea2384d3 bellard
    default:
338 ea2384d3 bellard
        error("Error while committing image");
339 ea2384d3 bellard
        break;
340 ea2384d3 bellard
    }
341 ea2384d3 bellard
342 ea2384d3 bellard
    bdrv_delete(bs);
343 ea2384d3 bellard
    return 0;
344 ea2384d3 bellard
}
345 ea2384d3 bellard
346 ea2384d3 bellard
static int is_not_zero(const uint8_t *sector, int len)
347 ea2384d3 bellard
{
348 ea2384d3 bellard
    int i;
349 ea2384d3 bellard
    len >>= 2;
350 ea2384d3 bellard
    for(i = 0;i < len; i++) {
351 ea2384d3 bellard
        if (((uint32_t *)sector)[i] != 0)
352 ea2384d3 bellard
            return 1;
353 ea2384d3 bellard
    }
354 ea2384d3 bellard
    return 0;
355 ea2384d3 bellard
}
356 ea2384d3 bellard
357 f58c7b35 ths
/*
358 f58c7b35 ths
 * Returns true iff the first sector pointed to by 'buf' contains at least
359 f58c7b35 ths
 * a non-NUL byte.
360 f58c7b35 ths
 *
361 f58c7b35 ths
 * 'pnum' is set to the number of sectors (including and immediately following
362 f58c7b35 ths
 * the first one) that are known to be in the same allocated/unallocated state.
363 f58c7b35 ths
 */
364 ea2384d3 bellard
static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
365 ea2384d3 bellard
{
366 ea2384d3 bellard
    int v, i;
367 ea2384d3 bellard
368 ea2384d3 bellard
    if (n <= 0) {
369 ea2384d3 bellard
        *pnum = 0;
370 ea2384d3 bellard
        return 0;
371 ea2384d3 bellard
    }
372 ea2384d3 bellard
    v = is_not_zero(buf, 512);
373 ea2384d3 bellard
    for(i = 1; i < n; i++) {
374 ea2384d3 bellard
        buf += 512;
375 ea2384d3 bellard
        if (v != is_not_zero(buf, 512))
376 ea2384d3 bellard
            break;
377 ea2384d3 bellard
    }
378 ea2384d3 bellard
    *pnum = i;
379 ea2384d3 bellard
    return v;
380 ea2384d3 bellard
}
381 ea2384d3 bellard
382 ea2384d3 bellard
#define IO_BUF_SIZE 65536
383 ea2384d3 bellard
384 ea2384d3 bellard
static int img_convert(int argc, char **argv)
385 ea2384d3 bellard
{
386 926c2d23 balrog
    int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
387 f58c7b35 ths
    const char *fmt, *out_fmt, *out_baseimg, *out_filename;
388 ea2384d3 bellard
    BlockDriver *drv;
389 926c2d23 balrog
    BlockDriverState **bs, *out_bs;
390 96b8f136 ths
    int64_t total_sectors, nb_sectors, sector_num, bs_offset;
391 96b8f136 ths
    uint64_t bs_sectors;
392 ea2384d3 bellard
    uint8_t buf[IO_BUF_SIZE];
393 ea2384d3 bellard
    const uint8_t *buf1;
394 faea38e7 bellard
    BlockDriverInfo bdi;
395 ea2384d3 bellard
396 ea2384d3 bellard
    fmt = NULL;
397 ea2384d3 bellard
    out_fmt = "raw";
398 f58c7b35 ths
    out_baseimg = NULL;
399 ec36ba14 ths
    flags = 0;
400 ea2384d3 bellard
    for(;;) {
401 f58c7b35 ths
        c = getopt(argc, argv, "f:O:B:hce6");
402 ea2384d3 bellard
        if (c == -1)
403 ea2384d3 bellard
            break;
404 ea2384d3 bellard
        switch(c) {
405 ea2384d3 bellard
        case 'h':
406 ea2384d3 bellard
            help();
407 ea2384d3 bellard
            break;
408 ea2384d3 bellard
        case 'f':
409 ea2384d3 bellard
            fmt = optarg;
410 ea2384d3 bellard
            break;
411 ea2384d3 bellard
        case 'O':
412 ea2384d3 bellard
            out_fmt = optarg;
413 ea2384d3 bellard
            break;
414 f58c7b35 ths
        case 'B':
415 f58c7b35 ths
            out_baseimg = optarg;
416 f58c7b35 ths
            break;
417 ea2384d3 bellard
        case 'c':
418 ec36ba14 ths
            flags |= BLOCK_FLAG_COMPRESS;
419 ea2384d3 bellard
            break;
420 ea2384d3 bellard
        case 'e':
421 ec36ba14 ths
            flags |= BLOCK_FLAG_ENCRYPT;
422 ec36ba14 ths
            break;
423 ec36ba14 ths
        case '6':
424 ec36ba14 ths
            flags |= BLOCK_FLAG_COMPAT6;
425 ea2384d3 bellard
            break;
426 ea2384d3 bellard
        }
427 ea2384d3 bellard
    }
428 3b46e624 ths
429 926c2d23 balrog
    bs_n = argc - optind - 1;
430 926c2d23 balrog
    if (bs_n < 1) help();
431 926c2d23 balrog
432 926c2d23 balrog
    out_filename = argv[argc - 1];
433 f58c7b35 ths
434 f58c7b35 ths
    if (bs_n > 1 && out_baseimg)
435 f58c7b35 ths
        error("-B makes no sense when concatenating multiple input images");
436 926c2d23 balrog
        
437 926c2d23 balrog
    bs = calloc(bs_n, sizeof(BlockDriverState *));
438 926c2d23 balrog
    if (!bs)
439 926c2d23 balrog
        error("Out of memory");
440 926c2d23 balrog
441 926c2d23 balrog
    total_sectors = 0;
442 926c2d23 balrog
    for (bs_i = 0; bs_i < bs_n; bs_i++) {
443 926c2d23 balrog
        bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt);
444 926c2d23 balrog
        if (!bs[bs_i])
445 926c2d23 balrog
            error("Could not open '%s'", argv[optind + bs_i]);
446 926c2d23 balrog
        bdrv_get_geometry(bs[bs_i], &bs_sectors);
447 926c2d23 balrog
        total_sectors += bs_sectors;
448 926c2d23 balrog
    }
449 ea2384d3 bellard
450 ea2384d3 bellard
    drv = bdrv_find_format(out_fmt);
451 ea2384d3 bellard
    if (!drv)
452 d34dda5e ths
        error("Unknown file format '%s'", out_fmt);
453 ec36ba14 ths
    if (flags & BLOCK_FLAG_COMPRESS && drv != &bdrv_qcow && drv != &bdrv_qcow2)
454 ea2384d3 bellard
        error("Compression not supported for this file format");
455 ec36ba14 ths
    if (flags & BLOCK_FLAG_ENCRYPT && drv != &bdrv_qcow && drv != &bdrv_qcow2)
456 ea2384d3 bellard
        error("Encryption not supported for this file format");
457 d8871c5a ths
    if (flags & BLOCK_FLAG_COMPAT6 && drv != &bdrv_vmdk)
458 ec36ba14 ths
        error("Alternative compatibility level not supported for this file format");
459 ec36ba14 ths
    if (flags & BLOCK_FLAG_ENCRYPT && flags & BLOCK_FLAG_COMPRESS)
460 ea2384d3 bellard
        error("Compression and encryption not supported at the same time");
461 926c2d23 balrog
462 f58c7b35 ths
    ret = bdrv_create(drv, out_filename, total_sectors, out_baseimg, flags);
463 ea2384d3 bellard
    if (ret < 0) {
464 ea2384d3 bellard
        if (ret == -ENOTSUP) {
465 3c56521b bellard
            error("Formatting not supported for file format '%s'", fmt);
466 ea2384d3 bellard
        } else {
467 ea2384d3 bellard
            error("Error while formatting '%s'", out_filename);
468 ea2384d3 bellard
        }
469 ea2384d3 bellard
    }
470 3b46e624 ths
471 ea2384d3 bellard
    out_bs = bdrv_new_open(out_filename, out_fmt);
472 ea2384d3 bellard
473 926c2d23 balrog
    bs_i = 0;
474 926c2d23 balrog
    bs_offset = 0;
475 926c2d23 balrog
    bdrv_get_geometry(bs[0], &bs_sectors);
476 926c2d23 balrog
477 926c2d23 balrog
    if (flags & BLOCK_FLAG_COMPRESS) {
478 faea38e7 bellard
        if (bdrv_get_info(out_bs, &bdi) < 0)
479 faea38e7 bellard
            error("could not get block driver info");
480 faea38e7 bellard
        cluster_size = bdi.cluster_size;
481 ea2384d3 bellard
        if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
482 ea2384d3 bellard
            error("invalid cluster size");
483 ea2384d3 bellard
        cluster_sectors = cluster_size >> 9;
484 ea2384d3 bellard
        sector_num = 0;
485 ea2384d3 bellard
        for(;;) {
486 926c2d23 balrog
            int64_t bs_num;
487 926c2d23 balrog
            int remainder;
488 926c2d23 balrog
            uint8_t *buf2;
489 926c2d23 balrog
490 ea2384d3 bellard
            nb_sectors = total_sectors - sector_num;
491 ea2384d3 bellard
            if (nb_sectors <= 0)
492 ea2384d3 bellard
                break;
493 ea2384d3 bellard
            if (nb_sectors >= cluster_sectors)
494 ea2384d3 bellard
                n = cluster_sectors;
495 ea2384d3 bellard
            else
496 ea2384d3 bellard
                n = nb_sectors;
497 926c2d23 balrog
498 926c2d23 balrog
            bs_num = sector_num - bs_offset;
499 926c2d23 balrog
            assert (bs_num >= 0);
500 926c2d23 balrog
            remainder = n;
501 926c2d23 balrog
            buf2 = buf;
502 926c2d23 balrog
            while (remainder > 0) {
503 926c2d23 balrog
                int nlow;
504 926c2d23 balrog
                while (bs_num == bs_sectors) {
505 926c2d23 balrog
                    bs_i++;
506 926c2d23 balrog
                    assert (bs_i < bs_n);
507 926c2d23 balrog
                    bs_offset += bs_sectors;
508 926c2d23 balrog
                    bdrv_get_geometry(bs[bs_i], &bs_sectors);
509 926c2d23 balrog
                    bs_num = 0;
510 926c2d23 balrog
                    /* printf("changing part: sector_num=%lld, "
511 926c2d23 balrog
                       "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
512 926c2d23 balrog
                       sector_num, bs_i, bs_offset, bs_sectors); */
513 926c2d23 balrog
                }
514 926c2d23 balrog
                assert (bs_num < bs_sectors);
515 926c2d23 balrog
516 926c2d23 balrog
                nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
517 926c2d23 balrog
518 926c2d23 balrog
                if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0) 
519 926c2d23 balrog
                    error("error while reading");
520 926c2d23 balrog
521 926c2d23 balrog
                buf2 += nlow * 512;
522 926c2d23 balrog
                bs_num += nlow;
523 926c2d23 balrog
524 926c2d23 balrog
                remainder -= nlow;
525 926c2d23 balrog
            }
526 926c2d23 balrog
            assert (remainder == 0);
527 926c2d23 balrog
528 ea2384d3 bellard
            if (n < cluster_sectors)
529 ea2384d3 bellard
                memset(buf + n * 512, 0, cluster_size - n * 512);
530 ea2384d3 bellard
            if (is_not_zero(buf, cluster_size)) {
531 5fafdf24 ths
                if (bdrv_write_compressed(out_bs, sector_num, buf,
532 faea38e7 bellard
                                          cluster_sectors) != 0)
533 ec3757de bellard
                    error("error while compressing sector %" PRId64,
534 ec3757de bellard
                          sector_num);
535 ea2384d3 bellard
            }
536 ea2384d3 bellard
            sector_num += n;
537 ea2384d3 bellard
        }
538 faea38e7 bellard
        /* signal EOF to align */
539 faea38e7 bellard
        bdrv_write_compressed(out_bs, 0, NULL, 0);
540 ea2384d3 bellard
    } else {
541 f58c7b35 ths
        sector_num = 0; // total number of sectors converted so far
542 ea2384d3 bellard
        for(;;) {
543 ea2384d3 bellard
            nb_sectors = total_sectors - sector_num;
544 ea2384d3 bellard
            if (nb_sectors <= 0)
545 ea2384d3 bellard
                break;
546 ea2384d3 bellard
            if (nb_sectors >= (IO_BUF_SIZE / 512))
547 ea2384d3 bellard
                n = (IO_BUF_SIZE / 512);
548 ea2384d3 bellard
            else
549 ea2384d3 bellard
                n = nb_sectors;
550 926c2d23 balrog
551 926c2d23 balrog
            while (sector_num - bs_offset >= bs_sectors) {
552 926c2d23 balrog
                bs_i ++;
553 926c2d23 balrog
                assert (bs_i < bs_n);
554 926c2d23 balrog
                bs_offset += bs_sectors;
555 926c2d23 balrog
                bdrv_get_geometry(bs[bs_i], &bs_sectors);
556 926c2d23 balrog
                /* printf("changing part: sector_num=%lld, bs_i=%d, "
557 926c2d23 balrog
                  "bs_offset=%lld, bs_sectors=%lld\n",
558 926c2d23 balrog
                   sector_num, bs_i, bs_offset, bs_sectors); */
559 926c2d23 balrog
            }
560 926c2d23 balrog
561 926c2d23 balrog
            if (n > bs_offset + bs_sectors - sector_num)
562 926c2d23 balrog
                n = bs_offset + bs_sectors - sector_num;
563 926c2d23 balrog
564 f58c7b35 ths
            /* If the output image is being created as a copy on write image,
565 f58c7b35 ths
               assume that sectors which are unallocated in the input image
566 f58c7b35 ths
               are present in both the output's and input's base images (no
567 f58c7b35 ths
               need to copy them). */
568 f58c7b35 ths
            if (out_baseimg) {
569 f58c7b35 ths
               if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset, n, &n1)) {
570 f58c7b35 ths
                  sector_num += n1;
571 f58c7b35 ths
                  continue;
572 f58c7b35 ths
               }
573 f58c7b35 ths
               /* The next 'n1' sectors are allocated in the input image. Copy
574 f58c7b35 ths
                  only those as they may be followed by unallocated sectors. */
575 f58c7b35 ths
               n = n1;
576 f58c7b35 ths
            }
577 f58c7b35 ths
578 926c2d23 balrog
            if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0) 
579 ea2384d3 bellard
                error("error while reading");
580 ea2384d3 bellard
            /* NOTE: at the same time we convert, we do not write zero
581 ea2384d3 bellard
               sectors to have a chance to compress the image. Ideally, we
582 ea2384d3 bellard
               should add a specific call to have the info to go faster */
583 ea2384d3 bellard
            buf1 = buf;
584 ea2384d3 bellard
            while (n > 0) {
585 f58c7b35 ths
                /* If the output image is being created as a copy on write image,
586 f58c7b35 ths
                   copy all sectors even the ones containing only NUL bytes,
587 f58c7b35 ths
                   because they may differ from the sectors in the base image. */
588 f58c7b35 ths
                if (out_baseimg || 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
}