Statistics
| Branch: | Revision:

root / qemu-img.c @ fad6cb1a

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