Statistics
| Branch: | Revision:

root / qemu-img.c @ 14ce26e7

History | View | Annotate | Download (17.4 kB)

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