Statistics
| Branch: | Revision:

root / qemu-img.c @ 8b7968f7

History | View | Annotate | Download (42.2 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 9ea2ea71 Kevin Wolf
#include "qemu-option.h"
26 f7b4a940 aliguori
#include "osdep.h"
27 ec36ba14 ths
#include "block_int.h"
28 9230eaf6 aliguori
#include <stdio.h>
29 ea2384d3 bellard
30 e8445331 bellard
#ifdef _WIN32
31 e8445331 bellard
#include <windows.h>
32 e8445331 bellard
#endif
33 e8445331 bellard
34 c227f099 Anthony Liguori
typedef struct img_cmd_t {
35 153859be Stuart Brady
    const char *name;
36 153859be Stuart Brady
    int (*handler)(int argc, char **argv);
37 c227f099 Anthony Liguori
} img_cmd_t;
38 153859be Stuart Brady
39 137519ce aurel32
/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
40 adfe078e Stefan Hajnoczi
#define BDRV_O_FLAGS BDRV_O_CACHE_WB
41 137519ce aurel32
42 8b7968f7 Stefan Weil
static void GCC_FMT_ATTR(1, 2) error(const char *fmt, ...)
43 ea2384d3 bellard
{
44 ea2384d3 bellard
    va_list ap;
45 ea2384d3 bellard
    va_start(ap, fmt);
46 57d1a2b6 bellard
    fprintf(stderr, "qemu-img: ");
47 ea2384d3 bellard
    vfprintf(stderr, fmt, ap);
48 ea2384d3 bellard
    fprintf(stderr, "\n");
49 ea2384d3 bellard
    va_end(ap);
50 ea2384d3 bellard
}
51 ea2384d3 bellard
52 ea2384d3 bellard
static void format_print(void *opaque, const char *name)
53 ea2384d3 bellard
{
54 ea2384d3 bellard
    printf(" %s", name);
55 ea2384d3 bellard
}
56 ea2384d3 bellard
57 d2c639d6 blueswir1
/* Please keep in synch with qemu-img.texi */
58 3f379ab1 pbrook
static void help(void)
59 ea2384d3 bellard
{
60 e00291c0 Paolo Bonzini
    const char *help_msg =
61 e00291c0 Paolo Bonzini
           "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
62 3f020d70 malc
           "usage: qemu-img command [command options]\n"
63 3f020d70 malc
           "QEMU disk image utility\n"
64 3f020d70 malc
           "\n"
65 3f020d70 malc
           "Command syntax:\n"
66 153859be Stuart Brady
#define DEF(option, callback, arg_string)        \
67 153859be Stuart Brady
           "  " arg_string "\n"
68 153859be Stuart Brady
#include "qemu-img-cmds.h"
69 153859be Stuart Brady
#undef DEF
70 153859be Stuart Brady
#undef GEN_DOCS
71 3f020d70 malc
           "\n"
72 3f020d70 malc
           "Command parameters:\n"
73 3f020d70 malc
           "  'filename' is a disk image filename\n"
74 3f020d70 malc
           "  'fmt' is the disk image format. It is guessed automatically in most cases\n"
75 3f020d70 malc
           "  'size' is the disk image size in bytes. Optional suffixes\n"
76 3f020d70 malc
           "    'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
77 3f020d70 malc
           "    and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
78 3f020d70 malc
           "  'output_filename' is the destination disk image filename\n"
79 3f020d70 malc
           "  'output_fmt' is the destination format\n"
80 3f020d70 malc
           "  'options' is a comma separated list of format specific options in a\n"
81 3f020d70 malc
           "    name=value format. Use -o ? for an overview of the options supported by the\n"
82 3f020d70 malc
           "    used format\n"
83 3f020d70 malc
           "  '-c' indicates that target image must be compressed (qcow format only)\n"
84 3f020d70 malc
           "  '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
85 3f020d70 malc
           "       match exactly. The image doesn't need a working backing file before\n"
86 3f020d70 malc
           "       rebasing in this case (useful for renaming the backing file)\n"
87 3f020d70 malc
           "  '-h' with or without a command shows this help and lists the supported formats\n"
88 3f020d70 malc
           "\n"
89 3f020d70 malc
           "Parameters to snapshot subcommand:\n"
90 3f020d70 malc
           "  'snapshot' is the name of the snapshot to create, apply or delete\n"
91 3f020d70 malc
           "  '-a' applies a snapshot (revert disk to saved state)\n"
92 3f020d70 malc
           "  '-c' creates a snapshot\n"
93 3f020d70 malc
           "  '-d' deletes a snapshot\n"
94 e00291c0 Paolo Bonzini
           "  '-l' lists all snapshots in the given image\n";
95 e00291c0 Paolo Bonzini
96 e00291c0 Paolo Bonzini
    printf("%s\nSupported formats:", help_msg);
97 ea2384d3 bellard
    bdrv_iterate_format(format_print, NULL);
98 ea2384d3 bellard
    printf("\n");
99 ea2384d3 bellard
    exit(1);
100 ea2384d3 bellard
}
101 ea2384d3 bellard
102 ea2384d3 bellard
#if defined(WIN32)
103 ea2384d3 bellard
/* XXX: put correct support for win32 */
104 ea2384d3 bellard
static int read_password(char *buf, int buf_size)
105 ea2384d3 bellard
{
106 ea2384d3 bellard
    int c, i;
107 ea2384d3 bellard
    printf("Password: ");
108 ea2384d3 bellard
    fflush(stdout);
109 ea2384d3 bellard
    i = 0;
110 ea2384d3 bellard
    for(;;) {
111 ea2384d3 bellard
        c = getchar();
112 ea2384d3 bellard
        if (c == '\n')
113 ea2384d3 bellard
            break;
114 ea2384d3 bellard
        if (i < (buf_size - 1))
115 ea2384d3 bellard
            buf[i++] = c;
116 ea2384d3 bellard
    }
117 ea2384d3 bellard
    buf[i] = '\0';
118 ea2384d3 bellard
    return 0;
119 ea2384d3 bellard
}
120 ea2384d3 bellard
121 ea2384d3 bellard
#else
122 ea2384d3 bellard
123 ea2384d3 bellard
#include <termios.h>
124 ea2384d3 bellard
125 ea2384d3 bellard
static struct termios oldtty;
126 ea2384d3 bellard
127 ea2384d3 bellard
static void term_exit(void)
128 ea2384d3 bellard
{
129 ea2384d3 bellard
    tcsetattr (0, TCSANOW, &oldtty);
130 ea2384d3 bellard
}
131 ea2384d3 bellard
132 ea2384d3 bellard
static void term_init(void)
133 ea2384d3 bellard
{
134 ea2384d3 bellard
    struct termios tty;
135 ea2384d3 bellard
136 ea2384d3 bellard
    tcgetattr (0, &tty);
137 ea2384d3 bellard
    oldtty = tty;
138 ea2384d3 bellard
139 ea2384d3 bellard
    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
140 ea2384d3 bellard
                          |INLCR|IGNCR|ICRNL|IXON);
141 ea2384d3 bellard
    tty.c_oflag |= OPOST;
142 ea2384d3 bellard
    tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
143 ea2384d3 bellard
    tty.c_cflag &= ~(CSIZE|PARENB);
144 ea2384d3 bellard
    tty.c_cflag |= CS8;
145 ea2384d3 bellard
    tty.c_cc[VMIN] = 1;
146 ea2384d3 bellard
    tty.c_cc[VTIME] = 0;
147 3b46e624 ths
148 ea2384d3 bellard
    tcsetattr (0, TCSANOW, &tty);
149 ea2384d3 bellard
150 ea2384d3 bellard
    atexit(term_exit);
151 ea2384d3 bellard
}
152 ea2384d3 bellard
153 3f379ab1 pbrook
static int read_password(char *buf, int buf_size)
154 ea2384d3 bellard
{
155 ea2384d3 bellard
    uint8_t ch;
156 ea2384d3 bellard
    int i, ret;
157 ea2384d3 bellard
158 ea2384d3 bellard
    printf("password: ");
159 ea2384d3 bellard
    fflush(stdout);
160 ea2384d3 bellard
    term_init();
161 ea2384d3 bellard
    i = 0;
162 ea2384d3 bellard
    for(;;) {
163 ea2384d3 bellard
        ret = read(0, &ch, 1);
164 ea2384d3 bellard
        if (ret == -1) {
165 ea2384d3 bellard
            if (errno == EAGAIN || errno == EINTR) {
166 ea2384d3 bellard
                continue;
167 ea2384d3 bellard
            } else {
168 ea2384d3 bellard
                ret = -1;
169 ea2384d3 bellard
                break;
170 ea2384d3 bellard
            }
171 ea2384d3 bellard
        } else if (ret == 0) {
172 ea2384d3 bellard
            ret = -1;
173 ea2384d3 bellard
            break;
174 ea2384d3 bellard
        } else {
175 ea2384d3 bellard
            if (ch == '\r') {
176 ea2384d3 bellard
                ret = 0;
177 ea2384d3 bellard
                break;
178 ea2384d3 bellard
            }
179 ea2384d3 bellard
            if (i < (buf_size - 1))
180 ea2384d3 bellard
                buf[i++] = ch;
181 ea2384d3 bellard
        }
182 ea2384d3 bellard
    }
183 ea2384d3 bellard
    term_exit();
184 ea2384d3 bellard
    buf[i] = '\0';
185 ea2384d3 bellard
    printf("\n");
186 ea2384d3 bellard
    return ret;
187 ea2384d3 bellard
}
188 ea2384d3 bellard
#endif
189 ea2384d3 bellard
190 75c23805 bellard
static BlockDriverState *bdrv_new_open(const char *filename,
191 9bc378c1 Sheng Yang
                                       const char *fmt,
192 f163d073 Stefan Hajnoczi
                                       int flags)
193 75c23805 bellard
{
194 75c23805 bellard
    BlockDriverState *bs;
195 75c23805 bellard
    BlockDriver *drv;
196 75c23805 bellard
    char password[256];
197 75c23805 bellard
198 75c23805 bellard
    bs = bdrv_new("");
199 c2abccec MORITA Kazutaka
    if (!bs) {
200 75c23805 bellard
        error("Not enough memory");
201 c2abccec MORITA Kazutaka
        goto fail;
202 c2abccec MORITA Kazutaka
    }
203 75c23805 bellard
    if (fmt) {
204 75c23805 bellard
        drv = bdrv_find_format(fmt);
205 c2abccec MORITA Kazutaka
        if (!drv) {
206 75c23805 bellard
            error("Unknown file format '%s'", fmt);
207 c2abccec MORITA Kazutaka
            goto fail;
208 c2abccec MORITA Kazutaka
        }
209 75c23805 bellard
    } else {
210 75c23805 bellard
        drv = NULL;
211 75c23805 bellard
    }
212 d6e9098e Kevin Wolf
    if (bdrv_open(bs, filename, flags, drv) < 0) {
213 75c23805 bellard
        error("Could not open '%s'", filename);
214 c2abccec MORITA Kazutaka
        goto fail;
215 75c23805 bellard
    }
216 75c23805 bellard
    if (bdrv_is_encrypted(bs)) {
217 75c23805 bellard
        printf("Disk image '%s' is encrypted.\n", filename);
218 c2abccec MORITA Kazutaka
        if (read_password(password, sizeof(password)) < 0) {
219 75c23805 bellard
            error("No password given");
220 c2abccec MORITA Kazutaka
            goto fail;
221 c2abccec MORITA Kazutaka
        }
222 c2abccec MORITA Kazutaka
        if (bdrv_set_key(bs, password) < 0) {
223 75c23805 bellard
            error("invalid password");
224 c2abccec MORITA Kazutaka
            goto fail;
225 c2abccec MORITA Kazutaka
        }
226 75c23805 bellard
    }
227 75c23805 bellard
    return bs;
228 c2abccec MORITA Kazutaka
fail:
229 c2abccec MORITA Kazutaka
    if (bs) {
230 c2abccec MORITA Kazutaka
        bdrv_delete(bs);
231 c2abccec MORITA Kazutaka
    }
232 c2abccec MORITA Kazutaka
    return NULL;
233 75c23805 bellard
}
234 75c23805 bellard
235 c2abccec MORITA Kazutaka
static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
236 efa84d43 Kevin Wolf
    int flags, const char *base_filename, const char *base_fmt)
237 efa84d43 Kevin Wolf
{
238 efa84d43 Kevin Wolf
    if (flags & BLOCK_FLAG_ENCRYPT) {
239 efa84d43 Kevin Wolf
        if (set_option_parameter(list, BLOCK_OPT_ENCRYPT, "on")) {
240 efa84d43 Kevin Wolf
            error("Encryption not supported for file format '%s'", fmt);
241 c2abccec MORITA Kazutaka
            return -1;
242 efa84d43 Kevin Wolf
        }
243 efa84d43 Kevin Wolf
    }
244 efa84d43 Kevin Wolf
    if (flags & BLOCK_FLAG_COMPAT6) {
245 efa84d43 Kevin Wolf
        if (set_option_parameter(list, BLOCK_OPT_COMPAT6, "on")) {
246 efa84d43 Kevin Wolf
            error("VMDK version 6 not supported for file format '%s'", fmt);
247 c2abccec MORITA Kazutaka
            return -1;
248 efa84d43 Kevin Wolf
        }
249 efa84d43 Kevin Wolf
    }
250 efa84d43 Kevin Wolf
251 efa84d43 Kevin Wolf
    if (base_filename) {
252 efa84d43 Kevin Wolf
        if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
253 efa84d43 Kevin Wolf
            error("Backing file not supported for file format '%s'", fmt);
254 c2abccec MORITA Kazutaka
            return -1;
255 efa84d43 Kevin Wolf
        }
256 efa84d43 Kevin Wolf
    }
257 efa84d43 Kevin Wolf
    if (base_fmt) {
258 efa84d43 Kevin Wolf
        if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
259 efa84d43 Kevin Wolf
            error("Backing file format not supported for file format '%s'", fmt);
260 c2abccec MORITA Kazutaka
            return -1;
261 efa84d43 Kevin Wolf
        }
262 efa84d43 Kevin Wolf
    }
263 c2abccec MORITA Kazutaka
    return 0;
264 efa84d43 Kevin Wolf
}
265 efa84d43 Kevin Wolf
266 ea2384d3 bellard
static int img_create(int argc, char **argv)
267 ea2384d3 bellard
{
268 c2abccec MORITA Kazutaka
    int c, ret = 0, flags;
269 ea2384d3 bellard
    const char *fmt = "raw";
270 9230eaf6 aliguori
    const char *base_fmt = NULL;
271 ea2384d3 bellard
    const char *filename;
272 ea2384d3 bellard
    const char *base_filename = NULL;
273 b50cbabc MORITA Kazutaka
    BlockDriver *drv, *proto_drv;
274 b50cbabc MORITA Kazutaka
    QEMUOptionParameter *param = NULL, *create_options = NULL;
275 9ea2ea71 Kevin Wolf
    char *options = NULL;
276 3b46e624 ths
277 ec36ba14 ths
    flags = 0;
278 ea2384d3 bellard
    for(;;) {
279 9ea2ea71 Kevin Wolf
        c = getopt(argc, argv, "F:b:f:he6o:");
280 ea2384d3 bellard
        if (c == -1)
281 ea2384d3 bellard
            break;
282 ea2384d3 bellard
        switch(c) {
283 ea2384d3 bellard
        case 'h':
284 ea2384d3 bellard
            help();
285 ea2384d3 bellard
            break;
286 9230eaf6 aliguori
        case 'F':
287 9230eaf6 aliguori
            base_fmt = optarg;
288 9230eaf6 aliguori
            break;
289 ea2384d3 bellard
        case 'b':
290 ea2384d3 bellard
            base_filename = optarg;
291 ea2384d3 bellard
            break;
292 ea2384d3 bellard
        case 'f':
293 ea2384d3 bellard
            fmt = optarg;
294 ea2384d3 bellard
            break;
295 ea2384d3 bellard
        case 'e':
296 ec36ba14 ths
            flags |= BLOCK_FLAG_ENCRYPT;
297 ea2384d3 bellard
            break;
298 d8871c5a ths
        case '6':
299 ec36ba14 ths
            flags |= BLOCK_FLAG_COMPAT6;
300 d8871c5a ths
            break;
301 9ea2ea71 Kevin Wolf
        case 'o':
302 9ea2ea71 Kevin Wolf
            options = optarg;
303 9ea2ea71 Kevin Wolf
            break;
304 ea2384d3 bellard
        }
305 ea2384d3 bellard
    }
306 9230eaf6 aliguori
307 b50cbabc MORITA Kazutaka
    /* Get the filename */
308 b50cbabc MORITA Kazutaka
    if (optind >= argc)
309 b50cbabc MORITA Kazutaka
        help();
310 b50cbabc MORITA Kazutaka
    filename = argv[optind++];
311 b50cbabc MORITA Kazutaka
312 9ea2ea71 Kevin Wolf
    /* Find driver and parse its options */
313 9ea2ea71 Kevin Wolf
    drv = bdrv_find_format(fmt);
314 c2abccec MORITA Kazutaka
    if (!drv) {
315 9ea2ea71 Kevin Wolf
        error("Unknown file format '%s'", fmt);
316 c2abccec MORITA Kazutaka
        return 1;
317 c2abccec MORITA Kazutaka
    }
318 9230eaf6 aliguori
319 b50cbabc MORITA Kazutaka
    proto_drv = bdrv_find_protocol(filename);
320 c2abccec MORITA Kazutaka
    if (!proto_drv) {
321 b50cbabc MORITA Kazutaka
        error("Unknown protocol '%s'", filename);
322 c2abccec MORITA Kazutaka
        return 1;
323 c2abccec MORITA Kazutaka
    }
324 b50cbabc MORITA Kazutaka
325 b50cbabc MORITA Kazutaka
    create_options = append_option_parameters(create_options,
326 b50cbabc MORITA Kazutaka
                                              drv->create_options);
327 b50cbabc MORITA Kazutaka
    create_options = append_option_parameters(create_options,
328 b50cbabc MORITA Kazutaka
                                              proto_drv->create_options);
329 b50cbabc MORITA Kazutaka
330 db08adf5 Kevin Wolf
    if (options && !strcmp(options, "?")) {
331 b50cbabc MORITA Kazutaka
        print_option_help(create_options);
332 c2abccec MORITA Kazutaka
        goto out;
333 db08adf5 Kevin Wolf
    }
334 db08adf5 Kevin Wolf
335 9f56640c Kevin Wolf
    /* Create parameter list with default values */
336 b50cbabc MORITA Kazutaka
    param = parse_option_parameters("", create_options, param);
337 9f56640c Kevin Wolf
    set_option_parameter_int(param, BLOCK_OPT_SIZE, -1);
338 9f56640c Kevin Wolf
339 9f56640c Kevin Wolf
    /* Parse -o options */
340 9ea2ea71 Kevin Wolf
    if (options) {
341 b50cbabc MORITA Kazutaka
        param = parse_option_parameters(options, create_options, param);
342 9ea2ea71 Kevin Wolf
        if (param == NULL) {
343 9ea2ea71 Kevin Wolf
            error("Invalid options for file format '%s'.", fmt);
344 c2abccec MORITA Kazutaka
            ret = -1;
345 c2abccec MORITA Kazutaka
            goto out;
346 9ea2ea71 Kevin Wolf
        }
347 9ea2ea71 Kevin Wolf
    }
348 9ea2ea71 Kevin Wolf
349 9ea2ea71 Kevin Wolf
    /* Add size to parameters */
350 9ea2ea71 Kevin Wolf
    if (optind < argc) {
351 9ea2ea71 Kevin Wolf
        set_option_parameter(param, BLOCK_OPT_SIZE, argv[optind++]);
352 9ea2ea71 Kevin Wolf
    }
353 9ea2ea71 Kevin Wolf
354 9ea2ea71 Kevin Wolf
    /* Add old-style options to parameters */
355 c2abccec MORITA Kazutaka
    ret = add_old_style_options(fmt, param, flags, base_filename, base_fmt);
356 c2abccec MORITA Kazutaka
    if (ret < 0) {
357 c2abccec MORITA Kazutaka
        goto out;
358 c2abccec MORITA Kazutaka
    }
359 9ea2ea71 Kevin Wolf
360 9ea2ea71 Kevin Wolf
    // The size for the image must always be specified, with one exception:
361 9ea2ea71 Kevin Wolf
    // If we are using a backing file, we can obtain the size from there
362 9f56640c Kevin Wolf
    if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
363 9ea2ea71 Kevin Wolf
364 9ea2ea71 Kevin Wolf
        QEMUOptionParameter *backing_file =
365 9ea2ea71 Kevin Wolf
            get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
366 9ea2ea71 Kevin Wolf
        QEMUOptionParameter *backing_fmt =
367 9ea2ea71 Kevin Wolf
            get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
368 9ea2ea71 Kevin Wolf
369 9ea2ea71 Kevin Wolf
        if (backing_file && backing_file->value.s) {
370 9ea2ea71 Kevin Wolf
            BlockDriverState *bs;
371 9ea2ea71 Kevin Wolf
            uint64_t size;
372 9ea2ea71 Kevin Wolf
            const char *fmt = NULL;
373 9ea2ea71 Kevin Wolf
            char buf[32];
374 9ea2ea71 Kevin Wolf
375 9ea2ea71 Kevin Wolf
            if (backing_fmt && backing_fmt->value.s) {
376 9ea2ea71 Kevin Wolf
                 if (bdrv_find_format(backing_fmt->value.s)) {
377 9ea2ea71 Kevin Wolf
                     fmt = backing_fmt->value.s;
378 9ea2ea71 Kevin Wolf
                } else {
379 9ea2ea71 Kevin Wolf
                     error("Unknown backing file format '%s'",
380 9ea2ea71 Kevin Wolf
                        backing_fmt->value.s);
381 c2abccec MORITA Kazutaka
                     ret = -1;
382 c2abccec MORITA Kazutaka
                     goto out;
383 9ea2ea71 Kevin Wolf
                }
384 9ea2ea71 Kevin Wolf
            }
385 9ea2ea71 Kevin Wolf
386 adfe078e Stefan Hajnoczi
            bs = bdrv_new_open(backing_file->value.s, fmt, BDRV_O_FLAGS);
387 c2abccec MORITA Kazutaka
            if (!bs) {
388 c2abccec MORITA Kazutaka
                ret = -1;
389 c2abccec MORITA Kazutaka
                goto out;
390 c2abccec MORITA Kazutaka
            }
391 9ea2ea71 Kevin Wolf
            bdrv_get_geometry(bs, &size);
392 9ea2ea71 Kevin Wolf
            size *= 512;
393 9ea2ea71 Kevin Wolf
            bdrv_delete(bs);
394 9ea2ea71 Kevin Wolf
395 9ea2ea71 Kevin Wolf
            snprintf(buf, sizeof(buf), "%" PRId64, size);
396 9ea2ea71 Kevin Wolf
            set_option_parameter(param, BLOCK_OPT_SIZE, buf);
397 9ea2ea71 Kevin Wolf
        } else {
398 9ea2ea71 Kevin Wolf
            error("Image creation needs a size parameter");
399 c2abccec MORITA Kazutaka
            ret = -1;
400 c2abccec MORITA Kazutaka
            goto out;
401 9ea2ea71 Kevin Wolf
        }
402 75c23805 bellard
    }
403 9ea2ea71 Kevin Wolf
404 9ea2ea71 Kevin Wolf
    printf("Formatting '%s', fmt=%s ", filename, fmt);
405 9ea2ea71 Kevin Wolf
    print_option_parameters(param);
406 9ea2ea71 Kevin Wolf
    puts("");
407 9ea2ea71 Kevin Wolf
408 9ea2ea71 Kevin Wolf
    ret = bdrv_create(drv, filename, param);
409 b50cbabc MORITA Kazutaka
    free_option_parameters(create_options);
410 9ea2ea71 Kevin Wolf
    free_option_parameters(param);
411 9ea2ea71 Kevin Wolf
412 ea2384d3 bellard
    if (ret < 0) {
413 ea2384d3 bellard
        if (ret == -ENOTSUP) {
414 3c56521b bellard
            error("Formatting or formatting option not supported for file format '%s'", fmt);
415 6e9ea0c0 aurel32
        } else if (ret == -EFBIG) {
416 6e9ea0c0 aurel32
            error("The image size is too large for file format '%s'", fmt);
417 ea2384d3 bellard
        } else {
418 3e7896de Juan Quintela
            error("%s: error while creating %s: %s", filename, fmt, strerror(-ret));
419 ea2384d3 bellard
        }
420 ea2384d3 bellard
    }
421 c2abccec MORITA Kazutaka
out:
422 c2abccec MORITA Kazutaka
    if (ret) {
423 c2abccec MORITA Kazutaka
        return 1;
424 c2abccec MORITA Kazutaka
    }
425 ea2384d3 bellard
    return 0;
426 ea2384d3 bellard
}
427 ea2384d3 bellard
428 e076f338 Kevin Wolf
/*
429 e076f338 Kevin Wolf
 * Checks an image for consistency. Exit codes:
430 e076f338 Kevin Wolf
 *
431 e076f338 Kevin Wolf
 * 0 - Check completed, image is good
432 e076f338 Kevin Wolf
 * 1 - Check not completed because of internal errors
433 e076f338 Kevin Wolf
 * 2 - Check completed, image is corrupted
434 e076f338 Kevin Wolf
 * 3 - Check completed, image has leaked clusters, but is good otherwise
435 e076f338 Kevin Wolf
 */
436 1585969c aliguori
static int img_check(int argc, char **argv)
437 1585969c aliguori
{
438 1585969c aliguori
    int c, ret;
439 1585969c aliguori
    const char *filename, *fmt;
440 1585969c aliguori
    BlockDriverState *bs;
441 e076f338 Kevin Wolf
    BdrvCheckResult result;
442 1585969c aliguori
443 1585969c aliguori
    fmt = NULL;
444 1585969c aliguori
    for(;;) {
445 1585969c aliguori
        c = getopt(argc, argv, "f:h");
446 1585969c aliguori
        if (c == -1)
447 1585969c aliguori
            break;
448 1585969c aliguori
        switch(c) {
449 1585969c aliguori
        case 'h':
450 1585969c aliguori
            help();
451 1585969c aliguori
            break;
452 1585969c aliguori
        case 'f':
453 1585969c aliguori
            fmt = optarg;
454 1585969c aliguori
            break;
455 1585969c aliguori
        }
456 1585969c aliguori
    }
457 1585969c aliguori
    if (optind >= argc)
458 1585969c aliguori
        help();
459 1585969c aliguori
    filename = argv[optind++];
460 1585969c aliguori
461 adfe078e Stefan Hajnoczi
    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS);
462 c2abccec MORITA Kazutaka
    if (!bs) {
463 c2abccec MORITA Kazutaka
        return 1;
464 c2abccec MORITA Kazutaka
    }
465 e076f338 Kevin Wolf
    ret = bdrv_check(bs, &result);
466 e076f338 Kevin Wolf
467 e076f338 Kevin Wolf
    if (ret == -ENOTSUP) {
468 1585969c aliguori
        error("This image format does not support checks");
469 e076f338 Kevin Wolf
        bdrv_delete(bs);
470 e076f338 Kevin Wolf
        return 1;
471 e076f338 Kevin Wolf
    }
472 e076f338 Kevin Wolf
473 e076f338 Kevin Wolf
    if (!(result.corruptions || result.leaks || result.check_errors)) {
474 e076f338 Kevin Wolf
        printf("No errors were found on the image.\n");
475 e076f338 Kevin Wolf
    } else {
476 e076f338 Kevin Wolf
        if (result.corruptions) {
477 e076f338 Kevin Wolf
            printf("\n%d errors were found on the image.\n"
478 e076f338 Kevin Wolf
                "Data may be corrupted, or further writes to the image "
479 e076f338 Kevin Wolf
                "may corrupt it.\n",
480 e076f338 Kevin Wolf
                result.corruptions);
481 e076f338 Kevin Wolf
        }
482 e076f338 Kevin Wolf
483 e076f338 Kevin Wolf
        if (result.leaks) {
484 e076f338 Kevin Wolf
            printf("\n%d leaked clusters were found on the image.\n"
485 e076f338 Kevin Wolf
                "This means waste of disk space, but no harm to data.\n",
486 e076f338 Kevin Wolf
                result.leaks);
487 e076f338 Kevin Wolf
        }
488 e076f338 Kevin Wolf
489 e076f338 Kevin Wolf
        if (result.check_errors) {
490 e076f338 Kevin Wolf
            printf("\n%d internal errors have occurred during the check.\n",
491 e076f338 Kevin Wolf
                result.check_errors);
492 1585969c aliguori
        }
493 1585969c aliguori
    }
494 1585969c aliguori
495 1585969c aliguori
    bdrv_delete(bs);
496 e076f338 Kevin Wolf
497 e076f338 Kevin Wolf
    if (ret < 0 || result.check_errors) {
498 e076f338 Kevin Wolf
        printf("\nAn error has occurred during the check: %s\n"
499 e076f338 Kevin Wolf
            "The check is not complete and may have missed error.\n",
500 e076f338 Kevin Wolf
            strerror(-ret));
501 c2abccec MORITA Kazutaka
        return 1;
502 c2abccec MORITA Kazutaka
    }
503 e076f338 Kevin Wolf
504 e076f338 Kevin Wolf
    if (result.corruptions) {
505 e076f338 Kevin Wolf
        return 2;
506 e076f338 Kevin Wolf
    } else if (result.leaks) {
507 e076f338 Kevin Wolf
        return 3;
508 e076f338 Kevin Wolf
    } else {
509 e076f338 Kevin Wolf
        return 0;
510 e076f338 Kevin Wolf
    }
511 1585969c aliguori
}
512 1585969c aliguori
513 ea2384d3 bellard
static int img_commit(int argc, char **argv)
514 ea2384d3 bellard
{
515 ea2384d3 bellard
    int c, ret;
516 ea2384d3 bellard
    const char *filename, *fmt;
517 ea2384d3 bellard
    BlockDriverState *bs;
518 ea2384d3 bellard
519 ea2384d3 bellard
    fmt = NULL;
520 ea2384d3 bellard
    for(;;) {
521 ea2384d3 bellard
        c = getopt(argc, argv, "f:h");
522 ea2384d3 bellard
        if (c == -1)
523 ea2384d3 bellard
            break;
524 ea2384d3 bellard
        switch(c) {
525 ea2384d3 bellard
        case 'h':
526 ea2384d3 bellard
            help();
527 ea2384d3 bellard
            break;
528 ea2384d3 bellard
        case 'f':
529 ea2384d3 bellard
            fmt = optarg;
530 ea2384d3 bellard
            break;
531 ea2384d3 bellard
        }
532 ea2384d3 bellard
    }
533 5fafdf24 ths
    if (optind >= argc)
534 ea2384d3 bellard
        help();
535 ea2384d3 bellard
    filename = argv[optind++];
536 ea2384d3 bellard
537 adfe078e Stefan Hajnoczi
    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR);
538 c2abccec MORITA Kazutaka
    if (!bs) {
539 c2abccec MORITA Kazutaka
        return 1;
540 c2abccec MORITA Kazutaka
    }
541 ea2384d3 bellard
    ret = bdrv_commit(bs);
542 ea2384d3 bellard
    switch(ret) {
543 ea2384d3 bellard
    case 0:
544 ea2384d3 bellard
        printf("Image committed.\n");
545 ea2384d3 bellard
        break;
546 ea2384d3 bellard
    case -ENOENT:
547 ea2384d3 bellard
        error("No disk inserted");
548 ea2384d3 bellard
        break;
549 ea2384d3 bellard
    case -EACCES:
550 ea2384d3 bellard
        error("Image is read-only");
551 ea2384d3 bellard
        break;
552 ea2384d3 bellard
    case -ENOTSUP:
553 ea2384d3 bellard
        error("Image is already committed");
554 ea2384d3 bellard
        break;
555 ea2384d3 bellard
    default:
556 ea2384d3 bellard
        error("Error while committing image");
557 ea2384d3 bellard
        break;
558 ea2384d3 bellard
    }
559 ea2384d3 bellard
560 ea2384d3 bellard
    bdrv_delete(bs);
561 c2abccec MORITA Kazutaka
    if (ret) {
562 c2abccec MORITA Kazutaka
        return 1;
563 c2abccec MORITA Kazutaka
    }
564 ea2384d3 bellard
    return 0;
565 ea2384d3 bellard
}
566 ea2384d3 bellard
567 ea2384d3 bellard
static int is_not_zero(const uint8_t *sector, int len)
568 ea2384d3 bellard
{
569 ea2384d3 bellard
    int i;
570 ea2384d3 bellard
    len >>= 2;
571 ea2384d3 bellard
    for(i = 0;i < len; i++) {
572 ea2384d3 bellard
        if (((uint32_t *)sector)[i] != 0)
573 ea2384d3 bellard
            return 1;
574 ea2384d3 bellard
    }
575 ea2384d3 bellard
    return 0;
576 ea2384d3 bellard
}
577 ea2384d3 bellard
578 f58c7b35 ths
/*
579 f58c7b35 ths
 * Returns true iff the first sector pointed to by 'buf' contains at least
580 f58c7b35 ths
 * a non-NUL byte.
581 f58c7b35 ths
 *
582 f58c7b35 ths
 * 'pnum' is set to the number of sectors (including and immediately following
583 f58c7b35 ths
 * the first one) that are known to be in the same allocated/unallocated state.
584 f58c7b35 ths
 */
585 ea2384d3 bellard
static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
586 ea2384d3 bellard
{
587 ea2384d3 bellard
    int v, i;
588 ea2384d3 bellard
589 ea2384d3 bellard
    if (n <= 0) {
590 ea2384d3 bellard
        *pnum = 0;
591 ea2384d3 bellard
        return 0;
592 ea2384d3 bellard
    }
593 ea2384d3 bellard
    v = is_not_zero(buf, 512);
594 ea2384d3 bellard
    for(i = 1; i < n; i++) {
595 ea2384d3 bellard
        buf += 512;
596 ea2384d3 bellard
        if (v != is_not_zero(buf, 512))
597 ea2384d3 bellard
            break;
598 ea2384d3 bellard
    }
599 ea2384d3 bellard
    *pnum = i;
600 ea2384d3 bellard
    return v;
601 ea2384d3 bellard
}
602 ea2384d3 bellard
603 3e85c6fd Kevin Wolf
/*
604 3e85c6fd Kevin Wolf
 * Compares two buffers sector by sector. Returns 0 if the first sector of both
605 3e85c6fd Kevin Wolf
 * buffers matches, non-zero otherwise.
606 3e85c6fd Kevin Wolf
 *
607 3e85c6fd Kevin Wolf
 * pnum is set to the number of sectors (including and immediately following
608 3e85c6fd Kevin Wolf
 * the first one) that are known to have the same comparison result
609 3e85c6fd Kevin Wolf
 */
610 3e85c6fd Kevin Wolf
static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
611 3e85c6fd Kevin Wolf
    int *pnum)
612 3e85c6fd Kevin Wolf
{
613 3e85c6fd Kevin Wolf
    int res, i;
614 3e85c6fd Kevin Wolf
615 3e85c6fd Kevin Wolf
    if (n <= 0) {
616 3e85c6fd Kevin Wolf
        *pnum = 0;
617 3e85c6fd Kevin Wolf
        return 0;
618 3e85c6fd Kevin Wolf
    }
619 3e85c6fd Kevin Wolf
620 3e85c6fd Kevin Wolf
    res = !!memcmp(buf1, buf2, 512);
621 3e85c6fd Kevin Wolf
    for(i = 1; i < n; i++) {
622 3e85c6fd Kevin Wolf
        buf1 += 512;
623 3e85c6fd Kevin Wolf
        buf2 += 512;
624 3e85c6fd Kevin Wolf
625 3e85c6fd Kevin Wolf
        if (!!memcmp(buf1, buf2, 512) != res) {
626 3e85c6fd Kevin Wolf
            break;
627 3e85c6fd Kevin Wolf
        }
628 3e85c6fd Kevin Wolf
    }
629 3e85c6fd Kevin Wolf
630 3e85c6fd Kevin Wolf
    *pnum = i;
631 3e85c6fd Kevin Wolf
    return res;
632 3e85c6fd Kevin Wolf
}
633 3e85c6fd Kevin Wolf
634 80ee15a6 Kevin Wolf
#define IO_BUF_SIZE (2 * 1024 * 1024)
635 ea2384d3 bellard
636 ea2384d3 bellard
static int img_convert(int argc, char **argv)
637 ea2384d3 bellard
{
638 c2abccec MORITA Kazutaka
    int c, ret = 0, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
639 f58c7b35 ths
    const char *fmt, *out_fmt, *out_baseimg, *out_filename;
640 b50cbabc MORITA Kazutaka
    BlockDriver *drv, *proto_drv;
641 c2abccec MORITA Kazutaka
    BlockDriverState **bs = NULL, *out_bs = NULL;
642 96b8f136 ths
    int64_t total_sectors, nb_sectors, sector_num, bs_offset;
643 96b8f136 ths
    uint64_t bs_sectors;
644 c2abccec MORITA Kazutaka
    uint8_t * buf = NULL;
645 ea2384d3 bellard
    const uint8_t *buf1;
646 faea38e7 bellard
    BlockDriverInfo bdi;
647 b50cbabc MORITA Kazutaka
    QEMUOptionParameter *param = NULL, *create_options = NULL;
648 efa84d43 Kevin Wolf
    char *options = NULL;
649 ea2384d3 bellard
650 ea2384d3 bellard
    fmt = NULL;
651 ea2384d3 bellard
    out_fmt = "raw";
652 f58c7b35 ths
    out_baseimg = NULL;
653 ec36ba14 ths
    flags = 0;
654 ea2384d3 bellard
    for(;;) {
655 efa84d43 Kevin Wolf
        c = getopt(argc, argv, "f:O:B:hce6o:");
656 ea2384d3 bellard
        if (c == -1)
657 ea2384d3 bellard
            break;
658 ea2384d3 bellard
        switch(c) {
659 ea2384d3 bellard
        case 'h':
660 ea2384d3 bellard
            help();
661 ea2384d3 bellard
            break;
662 ea2384d3 bellard
        case 'f':
663 ea2384d3 bellard
            fmt = optarg;
664 ea2384d3 bellard
            break;
665 ea2384d3 bellard
        case 'O':
666 ea2384d3 bellard
            out_fmt = optarg;
667 ea2384d3 bellard
            break;
668 f58c7b35 ths
        case 'B':
669 f58c7b35 ths
            out_baseimg = optarg;
670 f58c7b35 ths
            break;
671 ea2384d3 bellard
        case 'c':
672 ec36ba14 ths
            flags |= BLOCK_FLAG_COMPRESS;
673 ea2384d3 bellard
            break;
674 ea2384d3 bellard
        case 'e':
675 ec36ba14 ths
            flags |= BLOCK_FLAG_ENCRYPT;
676 ec36ba14 ths
            break;
677 ec36ba14 ths
        case '6':
678 ec36ba14 ths
            flags |= BLOCK_FLAG_COMPAT6;
679 ea2384d3 bellard
            break;
680 efa84d43 Kevin Wolf
        case 'o':
681 efa84d43 Kevin Wolf
            options = optarg;
682 efa84d43 Kevin Wolf
            break;
683 ea2384d3 bellard
        }
684 ea2384d3 bellard
    }
685 3b46e624 ths
686 926c2d23 balrog
    bs_n = argc - optind - 1;
687 926c2d23 balrog
    if (bs_n < 1) help();
688 926c2d23 balrog
689 926c2d23 balrog
    out_filename = argv[argc - 1];
690 f58c7b35 ths
691 c2abccec MORITA Kazutaka
    if (bs_n > 1 && out_baseimg) {
692 f58c7b35 ths
        error("-B makes no sense when concatenating multiple input images");
693 c2abccec MORITA Kazutaka
        return 1;
694 c2abccec MORITA Kazutaka
    }
695 926c2d23 balrog
        
696 926c2d23 balrog
    bs = calloc(bs_n, sizeof(BlockDriverState *));
697 c2abccec MORITA Kazutaka
    if (!bs) {
698 926c2d23 balrog
        error("Out of memory");
699 c2abccec MORITA Kazutaka
        return 1;
700 c2abccec MORITA Kazutaka
    }
701 926c2d23 balrog
702 926c2d23 balrog
    total_sectors = 0;
703 926c2d23 balrog
    for (bs_i = 0; bs_i < bs_n; bs_i++) {
704 adfe078e Stefan Hajnoczi
        bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS);
705 c2abccec MORITA Kazutaka
        if (!bs[bs_i]) {
706 926c2d23 balrog
            error("Could not open '%s'", argv[optind + bs_i]);
707 c2abccec MORITA Kazutaka
            ret = -1;
708 c2abccec MORITA Kazutaka
            goto out;
709 c2abccec MORITA Kazutaka
        }
710 926c2d23 balrog
        bdrv_get_geometry(bs[bs_i], &bs_sectors);
711 926c2d23 balrog
        total_sectors += bs_sectors;
712 926c2d23 balrog
    }
713 ea2384d3 bellard
714 efa84d43 Kevin Wolf
    /* Find driver and parse its options */
715 ea2384d3 bellard
    drv = bdrv_find_format(out_fmt);
716 c2abccec MORITA Kazutaka
    if (!drv) {
717 d34dda5e ths
        error("Unknown file format '%s'", out_fmt);
718 c2abccec MORITA Kazutaka
        ret = -1;
719 c2abccec MORITA Kazutaka
        goto out;
720 c2abccec MORITA Kazutaka
    }
721 efa84d43 Kevin Wolf
722 b50cbabc MORITA Kazutaka
    proto_drv = bdrv_find_protocol(out_filename);
723 c2abccec MORITA Kazutaka
    if (!proto_drv) {
724 b50cbabc MORITA Kazutaka
        error("Unknown protocol '%s'", out_filename);
725 c2abccec MORITA Kazutaka
        ret = -1;
726 c2abccec MORITA Kazutaka
        goto out;
727 c2abccec MORITA Kazutaka
    }
728 b50cbabc MORITA Kazutaka
729 b50cbabc MORITA Kazutaka
    create_options = append_option_parameters(create_options,
730 b50cbabc MORITA Kazutaka
                                              drv->create_options);
731 b50cbabc MORITA Kazutaka
    create_options = append_option_parameters(create_options,
732 b50cbabc MORITA Kazutaka
                                              proto_drv->create_options);
733 db08adf5 Kevin Wolf
    if (options && !strcmp(options, "?")) {
734 b50cbabc MORITA Kazutaka
        print_option_help(create_options);
735 c2abccec MORITA Kazutaka
        goto out;
736 db08adf5 Kevin Wolf
    }
737 db08adf5 Kevin Wolf
738 efa84d43 Kevin Wolf
    if (options) {
739 b50cbabc MORITA Kazutaka
        param = parse_option_parameters(options, create_options, param);
740 efa84d43 Kevin Wolf
        if (param == NULL) {
741 efa84d43 Kevin Wolf
            error("Invalid options for file format '%s'.", out_fmt);
742 c2abccec MORITA Kazutaka
            ret = -1;
743 c2abccec MORITA Kazutaka
            goto out;
744 efa84d43 Kevin Wolf
        }
745 efa84d43 Kevin Wolf
    } else {
746 b50cbabc MORITA Kazutaka
        param = parse_option_parameters("", create_options, param);
747 efa84d43 Kevin Wolf
    }
748 efa84d43 Kevin Wolf
749 efa84d43 Kevin Wolf
    set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
750 c2abccec MORITA Kazutaka
    ret = add_old_style_options(out_fmt, param, flags, out_baseimg, NULL);
751 c2abccec MORITA Kazutaka
    if (ret < 0) {
752 c2abccec MORITA Kazutaka
        goto out;
753 c2abccec MORITA Kazutaka
    }
754 efa84d43 Kevin Wolf
755 efa84d43 Kevin Wolf
    /* Check if compression is supported */
756 efa84d43 Kevin Wolf
    if (flags & BLOCK_FLAG_COMPRESS) {
757 efa84d43 Kevin Wolf
        QEMUOptionParameter *encryption =
758 efa84d43 Kevin Wolf
            get_option_parameter(param, BLOCK_OPT_ENCRYPT);
759 efa84d43 Kevin Wolf
760 efa84d43 Kevin Wolf
        if (!drv->bdrv_write_compressed) {
761 efa84d43 Kevin Wolf
            error("Compression not supported for this file format");
762 c2abccec MORITA Kazutaka
            ret = -1;
763 c2abccec MORITA Kazutaka
            goto out;
764 efa84d43 Kevin Wolf
        }
765 efa84d43 Kevin Wolf
766 efa84d43 Kevin Wolf
        if (encryption && encryption->value.n) {
767 efa84d43 Kevin Wolf
            error("Compression and encryption not supported at the same time");
768 c2abccec MORITA Kazutaka
            ret = -1;
769 c2abccec MORITA Kazutaka
            goto out;
770 efa84d43 Kevin Wolf
        }
771 efa84d43 Kevin Wolf
    }
772 efa84d43 Kevin Wolf
773 efa84d43 Kevin Wolf
    /* Create the new image */
774 efa84d43 Kevin Wolf
    ret = bdrv_create(drv, out_filename, param);
775 ea2384d3 bellard
    if (ret < 0) {
776 ea2384d3 bellard
        if (ret == -ENOTSUP) {
777 93c65b47 aliguori
            error("Formatting not supported for file format '%s'", out_fmt);
778 6e9ea0c0 aurel32
        } else if (ret == -EFBIG) {
779 6e9ea0c0 aurel32
            error("The image size is too large for file format '%s'", out_fmt);
780 ea2384d3 bellard
        } else {
781 3e7896de Juan Quintela
            error("%s: error while converting %s: %s", out_filename, out_fmt, strerror(-ret));
782 ea2384d3 bellard
        }
783 c2abccec MORITA Kazutaka
        goto out;
784 ea2384d3 bellard
    }
785 3b46e624 ths
786 1bd8e175 Kevin Wolf
    out_bs = bdrv_new_open(out_filename, out_fmt,
787 1bd8e175 Kevin Wolf
        BDRV_O_FLAGS | BDRV_O_RDWR | BDRV_O_NO_FLUSH);
788 c2abccec MORITA Kazutaka
    if (!out_bs) {
789 c2abccec MORITA Kazutaka
        ret = -1;
790 c2abccec MORITA Kazutaka
        goto out;
791 c2abccec MORITA Kazutaka
    }
792 ea2384d3 bellard
793 926c2d23 balrog
    bs_i = 0;
794 926c2d23 balrog
    bs_offset = 0;
795 926c2d23 balrog
    bdrv_get_geometry(bs[0], &bs_sectors);
796 d6771bfa TeLeMan
    buf = qemu_malloc(IO_BUF_SIZE);
797 926c2d23 balrog
798 926c2d23 balrog
    if (flags & BLOCK_FLAG_COMPRESS) {
799 c2abccec MORITA Kazutaka
        ret = bdrv_get_info(out_bs, &bdi);
800 c2abccec MORITA Kazutaka
        if (ret < 0) {
801 faea38e7 bellard
            error("could not get block driver info");
802 c2abccec MORITA Kazutaka
            goto out;
803 c2abccec MORITA Kazutaka
        }
804 faea38e7 bellard
        cluster_size = bdi.cluster_size;
805 c2abccec MORITA Kazutaka
        if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) {
806 ea2384d3 bellard
            error("invalid cluster size");
807 c2abccec MORITA Kazutaka
            ret = -1;
808 c2abccec MORITA Kazutaka
            goto out;
809 c2abccec MORITA Kazutaka
        }
810 ea2384d3 bellard
        cluster_sectors = cluster_size >> 9;
811 ea2384d3 bellard
        sector_num = 0;
812 ea2384d3 bellard
        for(;;) {
813 926c2d23 balrog
            int64_t bs_num;
814 926c2d23 balrog
            int remainder;
815 926c2d23 balrog
            uint8_t *buf2;
816 926c2d23 balrog
817 ea2384d3 bellard
            nb_sectors = total_sectors - sector_num;
818 ea2384d3 bellard
            if (nb_sectors <= 0)
819 ea2384d3 bellard
                break;
820 ea2384d3 bellard
            if (nb_sectors >= cluster_sectors)
821 ea2384d3 bellard
                n = cluster_sectors;
822 ea2384d3 bellard
            else
823 ea2384d3 bellard
                n = nb_sectors;
824 926c2d23 balrog
825 926c2d23 balrog
            bs_num = sector_num - bs_offset;
826 926c2d23 balrog
            assert (bs_num >= 0);
827 926c2d23 balrog
            remainder = n;
828 926c2d23 balrog
            buf2 = buf;
829 926c2d23 balrog
            while (remainder > 0) {
830 926c2d23 balrog
                int nlow;
831 926c2d23 balrog
                while (bs_num == bs_sectors) {
832 926c2d23 balrog
                    bs_i++;
833 926c2d23 balrog
                    assert (bs_i < bs_n);
834 926c2d23 balrog
                    bs_offset += bs_sectors;
835 926c2d23 balrog
                    bdrv_get_geometry(bs[bs_i], &bs_sectors);
836 926c2d23 balrog
                    bs_num = 0;
837 0bfcd599 Blue Swirl
                    /* printf("changing part: sector_num=%" PRId64 ", "
838 0bfcd599 Blue Swirl
                       "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
839 0bfcd599 Blue Swirl
                       "\n", sector_num, bs_i, bs_offset, bs_sectors); */
840 926c2d23 balrog
                }
841 926c2d23 balrog
                assert (bs_num < bs_sectors);
842 926c2d23 balrog
843 926c2d23 balrog
                nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
844 926c2d23 balrog
845 c2abccec MORITA Kazutaka
                ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
846 c2abccec MORITA Kazutaka
                if (ret < 0) {
847 926c2d23 balrog
                    error("error while reading");
848 c2abccec MORITA Kazutaka
                    goto out;
849 c2abccec MORITA Kazutaka
                }
850 926c2d23 balrog
851 926c2d23 balrog
                buf2 += nlow * 512;
852 926c2d23 balrog
                bs_num += nlow;
853 926c2d23 balrog
854 926c2d23 balrog
                remainder -= nlow;
855 926c2d23 balrog
            }
856 926c2d23 balrog
            assert (remainder == 0);
857 926c2d23 balrog
858 ea2384d3 bellard
            if (n < cluster_sectors)
859 ea2384d3 bellard
                memset(buf + n * 512, 0, cluster_size - n * 512);
860 ea2384d3 bellard
            if (is_not_zero(buf, cluster_size)) {
861 c2abccec MORITA Kazutaka
                ret = bdrv_write_compressed(out_bs, sector_num, buf,
862 c2abccec MORITA Kazutaka
                                            cluster_sectors);
863 c2abccec MORITA Kazutaka
                if (ret != 0) {
864 ec3757de bellard
                    error("error while compressing sector %" PRId64,
865 ec3757de bellard
                          sector_num);
866 c2abccec MORITA Kazutaka
                    goto out;
867 c2abccec MORITA Kazutaka
                }
868 ea2384d3 bellard
            }
869 ea2384d3 bellard
            sector_num += n;
870 ea2384d3 bellard
        }
871 faea38e7 bellard
        /* signal EOF to align */
872 faea38e7 bellard
        bdrv_write_compressed(out_bs, 0, NULL, 0);
873 ea2384d3 bellard
    } else {
874 f2feebbd Kevin Wolf
        int has_zero_init = bdrv_has_zero_init(out_bs);
875 f2feebbd Kevin Wolf
876 f58c7b35 ths
        sector_num = 0; // total number of sectors converted so far
877 ea2384d3 bellard
        for(;;) {
878 ea2384d3 bellard
            nb_sectors = total_sectors - sector_num;
879 ea2384d3 bellard
            if (nb_sectors <= 0)
880 ea2384d3 bellard
                break;
881 ea2384d3 bellard
            if (nb_sectors >= (IO_BUF_SIZE / 512))
882 ea2384d3 bellard
                n = (IO_BUF_SIZE / 512);
883 ea2384d3 bellard
            else
884 ea2384d3 bellard
                n = nb_sectors;
885 926c2d23 balrog
886 926c2d23 balrog
            while (sector_num - bs_offset >= bs_sectors) {
887 926c2d23 balrog
                bs_i ++;
888 926c2d23 balrog
                assert (bs_i < bs_n);
889 926c2d23 balrog
                bs_offset += bs_sectors;
890 926c2d23 balrog
                bdrv_get_geometry(bs[bs_i], &bs_sectors);
891 0bfcd599 Blue Swirl
                /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
892 0bfcd599 Blue Swirl
                  "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
893 926c2d23 balrog
                   sector_num, bs_i, bs_offset, bs_sectors); */
894 926c2d23 balrog
            }
895 926c2d23 balrog
896 926c2d23 balrog
            if (n > bs_offset + bs_sectors - sector_num)
897 926c2d23 balrog
                n = bs_offset + bs_sectors - sector_num;
898 926c2d23 balrog
899 f2feebbd Kevin Wolf
            if (has_zero_init) {
900 d032044f Akkarit Sangpetch
                /* If the output image is being created as a copy on write image,
901 d032044f Akkarit Sangpetch
                   assume that sectors which are unallocated in the input image
902 d032044f Akkarit Sangpetch
                   are present in both the output's and input's base images (no
903 d032044f Akkarit Sangpetch
                   need to copy them). */
904 d032044f Akkarit Sangpetch
                if (out_baseimg) {
905 d032044f Akkarit Sangpetch
                    if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
906 d032044f Akkarit Sangpetch
                                           n, &n1)) {
907 d032044f Akkarit Sangpetch
                        sector_num += n1;
908 d032044f Akkarit Sangpetch
                        continue;
909 d032044f Akkarit Sangpetch
                    }
910 d032044f Akkarit Sangpetch
                    /* The next 'n1' sectors are allocated in the input image. Copy
911 d032044f Akkarit Sangpetch
                       only those as they may be followed by unallocated sectors. */
912 d032044f Akkarit Sangpetch
                    n = n1;
913 93c65b47 aliguori
                }
914 93c65b47 aliguori
            } else {
915 93c65b47 aliguori
                n1 = n;
916 f58c7b35 ths
            }
917 f58c7b35 ths
918 c2abccec MORITA Kazutaka
            ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
919 c2abccec MORITA Kazutaka
            if (ret < 0) {
920 ea2384d3 bellard
                error("error while reading");
921 c2abccec MORITA Kazutaka
                goto out;
922 c2abccec MORITA Kazutaka
            }
923 ea2384d3 bellard
            /* NOTE: at the same time we convert, we do not write zero
924 ea2384d3 bellard
               sectors to have a chance to compress the image. Ideally, we
925 ea2384d3 bellard
               should add a specific call to have the info to go faster */
926 ea2384d3 bellard
            buf1 = buf;
927 ea2384d3 bellard
            while (n > 0) {
928 f58c7b35 ths
                /* If the output image is being created as a copy on write image,
929 f58c7b35 ths
                   copy all sectors even the ones containing only NUL bytes,
930 93c65b47 aliguori
                   because they may differ from the sectors in the base image.
931 93c65b47 aliguori

932 93c65b47 aliguori
                   If the output is to a host device, we also write out
933 93c65b47 aliguori
                   sectors that are entirely 0, since whatever data was
934 93c65b47 aliguori
                   already there is garbage, not 0s. */
935 f2feebbd Kevin Wolf
                if (!has_zero_init || out_baseimg ||
936 93c65b47 aliguori
                    is_allocated_sectors(buf1, n, &n1)) {
937 c2abccec MORITA Kazutaka
                    ret = bdrv_write(out_bs, sector_num, buf1, n1);
938 c2abccec MORITA Kazutaka
                    if (ret < 0) {
939 ea2384d3 bellard
                        error("error while writing");
940 c2abccec MORITA Kazutaka
                        goto out;
941 c2abccec MORITA Kazutaka
                    }
942 ea2384d3 bellard
                }
943 ea2384d3 bellard
                sector_num += n1;
944 ea2384d3 bellard
                n -= n1;
945 ea2384d3 bellard
                buf1 += n1 * 512;
946 ea2384d3 bellard
            }
947 ea2384d3 bellard
        }
948 ea2384d3 bellard
    }
949 c2abccec MORITA Kazutaka
out:
950 c2abccec MORITA Kazutaka
    free_option_parameters(create_options);
951 c2abccec MORITA Kazutaka
    free_option_parameters(param);
952 d6771bfa TeLeMan
    qemu_free(buf);
953 c2abccec MORITA Kazutaka
    if (out_bs) {
954 c2abccec MORITA Kazutaka
        bdrv_delete(out_bs);
955 c2abccec MORITA Kazutaka
    }
956 c2abccec MORITA Kazutaka
    for (bs_i = 0; bs_i < bs_n; bs_i++) {
957 c2abccec MORITA Kazutaka
        if (bs[bs_i]) {
958 c2abccec MORITA Kazutaka
            bdrv_delete(bs[bs_i]);
959 c2abccec MORITA Kazutaka
        }
960 c2abccec MORITA Kazutaka
    }
961 926c2d23 balrog
    free(bs);
962 c2abccec MORITA Kazutaka
    if (ret) {
963 c2abccec MORITA Kazutaka
        return 1;
964 c2abccec MORITA Kazutaka
    }
965 ea2384d3 bellard
    return 0;
966 ea2384d3 bellard
}
967 ea2384d3 bellard
968 57d1a2b6 bellard
#ifdef _WIN32
969 57d1a2b6 bellard
static int64_t get_allocated_file_size(const char *filename)
970 57d1a2b6 bellard
{
971 e8445331 bellard
    typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
972 e8445331 bellard
    get_compressed_t get_compressed;
973 57d1a2b6 bellard
    struct _stati64 st;
974 e8445331 bellard
975 e8445331 bellard
    /* WinNT support GetCompressedFileSize to determine allocate size */
976 e8445331 bellard
    get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
977 e8445331 bellard
    if (get_compressed) {
978 e8445331 bellard
            DWORD high, low;
979 e8445331 bellard
            low = get_compressed(filename, &high);
980 e8445331 bellard
            if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
981 e8445331 bellard
            return (((int64_t) high) << 32) + low;
982 e8445331 bellard
    }
983 e8445331 bellard
984 5fafdf24 ths
    if (_stati64(filename, &st) < 0)
985 57d1a2b6 bellard
        return -1;
986 57d1a2b6 bellard
    return st.st_size;
987 57d1a2b6 bellard
}
988 57d1a2b6 bellard
#else
989 57d1a2b6 bellard
static int64_t get_allocated_file_size(const char *filename)
990 57d1a2b6 bellard
{
991 57d1a2b6 bellard
    struct stat st;
992 5fafdf24 ths
    if (stat(filename, &st) < 0)
993 57d1a2b6 bellard
        return -1;
994 57d1a2b6 bellard
    return (int64_t)st.st_blocks * 512;
995 57d1a2b6 bellard
}
996 57d1a2b6 bellard
#endif
997 57d1a2b6 bellard
998 faea38e7 bellard
static void dump_snapshots(BlockDriverState *bs)
999 faea38e7 bellard
{
1000 faea38e7 bellard
    QEMUSnapshotInfo *sn_tab, *sn;
1001 faea38e7 bellard
    int nb_sns, i;
1002 faea38e7 bellard
    char buf[256];
1003 faea38e7 bellard
1004 faea38e7 bellard
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1005 faea38e7 bellard
    if (nb_sns <= 0)
1006 faea38e7 bellard
        return;
1007 faea38e7 bellard
    printf("Snapshot list:\n");
1008 faea38e7 bellard
    printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1009 faea38e7 bellard
    for(i = 0; i < nb_sns; i++) {
1010 faea38e7 bellard
        sn = &sn_tab[i];
1011 faea38e7 bellard
        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1012 faea38e7 bellard
    }
1013 faea38e7 bellard
    qemu_free(sn_tab);
1014 faea38e7 bellard
}
1015 faea38e7 bellard
1016 ea2384d3 bellard
static int img_info(int argc, char **argv)
1017 ea2384d3 bellard
{
1018 ea2384d3 bellard
    int c;
1019 ea2384d3 bellard
    const char *filename, *fmt;
1020 ea2384d3 bellard
    BlockDriverState *bs;
1021 ea2384d3 bellard
    char fmt_name[128], size_buf[128], dsize_buf[128];
1022 96b8f136 ths
    uint64_t total_sectors;
1023 96b8f136 ths
    int64_t allocated_size;
1024 93b6b2a3 bellard
    char backing_filename[1024];
1025 93b6b2a3 bellard
    char backing_filename2[1024];
1026 faea38e7 bellard
    BlockDriverInfo bdi;
1027 ea2384d3 bellard
1028 ea2384d3 bellard
    fmt = NULL;
1029 ea2384d3 bellard
    for(;;) {
1030 ea2384d3 bellard
        c = getopt(argc, argv, "f:h");
1031 ea2384d3 bellard
        if (c == -1)
1032 ea2384d3 bellard
            break;
1033 ea2384d3 bellard
        switch(c) {
1034 ea2384d3 bellard
        case 'h':
1035 ea2384d3 bellard
            help();
1036 ea2384d3 bellard
            break;
1037 ea2384d3 bellard
        case 'f':
1038 ea2384d3 bellard
            fmt = optarg;
1039 ea2384d3 bellard
            break;
1040 ea2384d3 bellard
        }
1041 ea2384d3 bellard
    }
1042 5fafdf24 ths
    if (optind >= argc)
1043 ea2384d3 bellard
        help();
1044 ea2384d3 bellard
    filename = argv[optind++];
1045 ea2384d3 bellard
1046 adfe078e Stefan Hajnoczi
    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING);
1047 c2abccec MORITA Kazutaka
    if (!bs) {
1048 c2abccec MORITA Kazutaka
        return 1;
1049 c2abccec MORITA Kazutaka
    }
1050 ea2384d3 bellard
    bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
1051 ea2384d3 bellard
    bdrv_get_geometry(bs, &total_sectors);
1052 ea2384d3 bellard
    get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
1053 57d1a2b6 bellard
    allocated_size = get_allocated_file_size(filename);
1054 57d1a2b6 bellard
    if (allocated_size < 0)
1055 a10ea30b blueswir1
        snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
1056 de167e41 bellard
    else
1057 5fafdf24 ths
        get_human_readable_size(dsize_buf, sizeof(dsize_buf),
1058 de167e41 bellard
                                allocated_size);
1059 ea2384d3 bellard
    printf("image: %s\n"
1060 ea2384d3 bellard
           "file format: %s\n"
1061 ec3757de bellard
           "virtual size: %s (%" PRId64 " bytes)\n"
1062 ea2384d3 bellard
           "disk size: %s\n",
1063 5fafdf24 ths
           filename, fmt_name, size_buf,
1064 ec3757de bellard
           (total_sectors * 512),
1065 ea2384d3 bellard
           dsize_buf);
1066 ea2384d3 bellard
    if (bdrv_is_encrypted(bs))
1067 ea2384d3 bellard
        printf("encrypted: yes\n");
1068 faea38e7 bellard
    if (bdrv_get_info(bs, &bdi) >= 0) {
1069 5fafdf24 ths
        if (bdi.cluster_size != 0)
1070 faea38e7 bellard
            printf("cluster_size: %d\n", bdi.cluster_size);
1071 faea38e7 bellard
    }
1072 93b6b2a3 bellard
    bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
1073 faea38e7 bellard
    if (backing_filename[0] != '\0') {
1074 93b6b2a3 bellard
        path_combine(backing_filename2, sizeof(backing_filename2),
1075 93b6b2a3 bellard
                     filename, backing_filename);
1076 5fafdf24 ths
        printf("backing file: %s (actual path: %s)\n",
1077 93b6b2a3 bellard
               backing_filename,
1078 93b6b2a3 bellard
               backing_filename2);
1079 faea38e7 bellard
    }
1080 faea38e7 bellard
    dump_snapshots(bs);
1081 ea2384d3 bellard
    bdrv_delete(bs);
1082 ea2384d3 bellard
    return 0;
1083 ea2384d3 bellard
}
1084 ea2384d3 bellard
1085 f7b4a940 aliguori
#define SNAPSHOT_LIST   1
1086 f7b4a940 aliguori
#define SNAPSHOT_CREATE 2
1087 f7b4a940 aliguori
#define SNAPSHOT_APPLY  3
1088 f7b4a940 aliguori
#define SNAPSHOT_DELETE 4
1089 f7b4a940 aliguori
1090 153859be Stuart Brady
static int img_snapshot(int argc, char **argv)
1091 f7b4a940 aliguori
{
1092 f7b4a940 aliguori
    BlockDriverState *bs;
1093 f7b4a940 aliguori
    QEMUSnapshotInfo sn;
1094 f7b4a940 aliguori
    char *filename, *snapshot_name = NULL;
1095 c2abccec MORITA Kazutaka
    int c, ret = 0, bdrv_oflags;
1096 f7b4a940 aliguori
    int action = 0;
1097 f7b4a940 aliguori
    qemu_timeval tv;
1098 f7b4a940 aliguori
1099 f5edb014 Naphtali Sprei
    bdrv_oflags = BDRV_O_RDWR;
1100 f7b4a940 aliguori
    /* Parse commandline parameters */
1101 f7b4a940 aliguori
    for(;;) {
1102 f7b4a940 aliguori
        c = getopt(argc, argv, "la:c:d:h");
1103 f7b4a940 aliguori
        if (c == -1)
1104 f7b4a940 aliguori
            break;
1105 f7b4a940 aliguori
        switch(c) {
1106 f7b4a940 aliguori
        case 'h':
1107 f7b4a940 aliguori
            help();
1108 153859be Stuart Brady
            return 0;
1109 f7b4a940 aliguori
        case 'l':
1110 f7b4a940 aliguori
            if (action) {
1111 f7b4a940 aliguori
                help();
1112 153859be Stuart Brady
                return 0;
1113 f7b4a940 aliguori
            }
1114 f7b4a940 aliguori
            action = SNAPSHOT_LIST;
1115 f5edb014 Naphtali Sprei
            bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
1116 f7b4a940 aliguori
            break;
1117 f7b4a940 aliguori
        case 'a':
1118 f7b4a940 aliguori
            if (action) {
1119 f7b4a940 aliguori
                help();
1120 153859be Stuart Brady
                return 0;
1121 f7b4a940 aliguori
            }
1122 f7b4a940 aliguori
            action = SNAPSHOT_APPLY;
1123 f7b4a940 aliguori
            snapshot_name = optarg;
1124 f7b4a940 aliguori
            break;
1125 f7b4a940 aliguori
        case 'c':
1126 f7b4a940 aliguori
            if (action) {
1127 f7b4a940 aliguori
                help();
1128 153859be Stuart Brady
                return 0;
1129 f7b4a940 aliguori
            }
1130 f7b4a940 aliguori
            action = SNAPSHOT_CREATE;
1131 f7b4a940 aliguori
            snapshot_name = optarg;
1132 f7b4a940 aliguori
            break;
1133 f7b4a940 aliguori
        case 'd':
1134 f7b4a940 aliguori
            if (action) {
1135 f7b4a940 aliguori
                help();
1136 153859be Stuart Brady
                return 0;
1137 f7b4a940 aliguori
            }
1138 f7b4a940 aliguori
            action = SNAPSHOT_DELETE;
1139 f7b4a940 aliguori
            snapshot_name = optarg;
1140 f7b4a940 aliguori
            break;
1141 f7b4a940 aliguori
        }
1142 f7b4a940 aliguori
    }
1143 f7b4a940 aliguori
1144 f7b4a940 aliguori
    if (optind >= argc)
1145 f7b4a940 aliguori
        help();
1146 f7b4a940 aliguori
    filename = argv[optind++];
1147 f7b4a940 aliguori
1148 f7b4a940 aliguori
    /* Open the image */
1149 f163d073 Stefan Hajnoczi
    bs = bdrv_new_open(filename, NULL, bdrv_oflags);
1150 c2abccec MORITA Kazutaka
    if (!bs) {
1151 c2abccec MORITA Kazutaka
        return 1;
1152 c2abccec MORITA Kazutaka
    }
1153 f7b4a940 aliguori
1154 f7b4a940 aliguori
    /* Perform the requested action */
1155 f7b4a940 aliguori
    switch(action) {
1156 f7b4a940 aliguori
    case SNAPSHOT_LIST:
1157 f7b4a940 aliguori
        dump_snapshots(bs);
1158 f7b4a940 aliguori
        break;
1159 f7b4a940 aliguori
1160 f7b4a940 aliguori
    case SNAPSHOT_CREATE:
1161 f7b4a940 aliguori
        memset(&sn, 0, sizeof(sn));
1162 f7b4a940 aliguori
        pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
1163 f7b4a940 aliguori
1164 f7b4a940 aliguori
        qemu_gettimeofday(&tv);
1165 f7b4a940 aliguori
        sn.date_sec = tv.tv_sec;
1166 f7b4a940 aliguori
        sn.date_nsec = tv.tv_usec * 1000;
1167 f7b4a940 aliguori
1168 f7b4a940 aliguori
        ret = bdrv_snapshot_create(bs, &sn);
1169 f7b4a940 aliguori
        if (ret)
1170 f7b4a940 aliguori
            error("Could not create snapshot '%s': %d (%s)",
1171 f7b4a940 aliguori
                snapshot_name, ret, strerror(-ret));
1172 f7b4a940 aliguori
        break;
1173 f7b4a940 aliguori
1174 f7b4a940 aliguori
    case SNAPSHOT_APPLY:
1175 f7b4a940 aliguori
        ret = bdrv_snapshot_goto(bs, snapshot_name);
1176 f7b4a940 aliguori
        if (ret)
1177 f7b4a940 aliguori
            error("Could not apply snapshot '%s': %d (%s)",
1178 f7b4a940 aliguori
                snapshot_name, ret, strerror(-ret));
1179 f7b4a940 aliguori
        break;
1180 f7b4a940 aliguori
1181 f7b4a940 aliguori
    case SNAPSHOT_DELETE:
1182 f7b4a940 aliguori
        ret = bdrv_snapshot_delete(bs, snapshot_name);
1183 f7b4a940 aliguori
        if (ret)
1184 f7b4a940 aliguori
            error("Could not delete snapshot '%s': %d (%s)",
1185 f7b4a940 aliguori
                snapshot_name, ret, strerror(-ret));
1186 f7b4a940 aliguori
        break;
1187 f7b4a940 aliguori
    }
1188 f7b4a940 aliguori
1189 f7b4a940 aliguori
    /* Cleanup */
1190 f7b4a940 aliguori
    bdrv_delete(bs);
1191 c2abccec MORITA Kazutaka
    if (ret) {
1192 c2abccec MORITA Kazutaka
        return 1;
1193 c2abccec MORITA Kazutaka
    }
1194 153859be Stuart Brady
    return 0;
1195 f7b4a940 aliguori
}
1196 f7b4a940 aliguori
1197 3e85c6fd Kevin Wolf
static int img_rebase(int argc, char **argv)
1198 3e85c6fd Kevin Wolf
{
1199 c2abccec MORITA Kazutaka
    BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
1200 f163d073 Stefan Hajnoczi
    BlockDriver *old_backing_drv, *new_backing_drv;
1201 3e85c6fd Kevin Wolf
    char *filename;
1202 e53dbee0 Kevin Wolf
    const char *fmt, *out_basefmt, *out_baseimg;
1203 3e85c6fd Kevin Wolf
    int c, flags, ret;
1204 3e85c6fd Kevin Wolf
    int unsafe = 0;
1205 3e85c6fd Kevin Wolf
1206 3e85c6fd Kevin Wolf
    /* Parse commandline parameters */
1207 e53dbee0 Kevin Wolf
    fmt = NULL;
1208 3e85c6fd Kevin Wolf
    out_baseimg = NULL;
1209 3e85c6fd Kevin Wolf
    out_basefmt = NULL;
1210 3e85c6fd Kevin Wolf
1211 3e85c6fd Kevin Wolf
    for(;;) {
1212 e53dbee0 Kevin Wolf
        c = getopt(argc, argv, "uhf:F:b:");
1213 3e85c6fd Kevin Wolf
        if (c == -1)
1214 3e85c6fd Kevin Wolf
            break;
1215 3e85c6fd Kevin Wolf
        switch(c) {
1216 3e85c6fd Kevin Wolf
        case 'h':
1217 3e85c6fd Kevin Wolf
            help();
1218 3e85c6fd Kevin Wolf
            return 0;
1219 e53dbee0 Kevin Wolf
        case 'f':
1220 e53dbee0 Kevin Wolf
            fmt = optarg;
1221 e53dbee0 Kevin Wolf
            break;
1222 3e85c6fd Kevin Wolf
        case 'F':
1223 3e85c6fd Kevin Wolf
            out_basefmt = optarg;
1224 3e85c6fd Kevin Wolf
            break;
1225 3e85c6fd Kevin Wolf
        case 'b':
1226 3e85c6fd Kevin Wolf
            out_baseimg = optarg;
1227 3e85c6fd Kevin Wolf
            break;
1228 3e85c6fd Kevin Wolf
        case 'u':
1229 3e85c6fd Kevin Wolf
            unsafe = 1;
1230 3e85c6fd Kevin Wolf
            break;
1231 3e85c6fd Kevin Wolf
        }
1232 3e85c6fd Kevin Wolf
    }
1233 3e85c6fd Kevin Wolf
1234 3e85c6fd Kevin Wolf
    if ((optind >= argc) || !out_baseimg)
1235 3e85c6fd Kevin Wolf
        help();
1236 3e85c6fd Kevin Wolf
    filename = argv[optind++];
1237 3e85c6fd Kevin Wolf
1238 3e85c6fd Kevin Wolf
    /*
1239 3e85c6fd Kevin Wolf
     * Open the images.
1240 3e85c6fd Kevin Wolf
     *
1241 3e85c6fd Kevin Wolf
     * Ignore the old backing file for unsafe rebase in case we want to correct
1242 3e85c6fd Kevin Wolf
     * the reference to a renamed or moved backing file.
1243 3e85c6fd Kevin Wolf
     */
1244 adfe078e Stefan Hajnoczi
    flags = BDRV_O_FLAGS | BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
1245 f163d073 Stefan Hajnoczi
    bs = bdrv_new_open(filename, fmt, flags);
1246 c2abccec MORITA Kazutaka
    if (!bs) {
1247 c2abccec MORITA Kazutaka
        return 1;
1248 c2abccec MORITA Kazutaka
    }
1249 3e85c6fd Kevin Wolf
1250 3e85c6fd Kevin Wolf
    /* Find the right drivers for the backing files */
1251 3e85c6fd Kevin Wolf
    old_backing_drv = NULL;
1252 3e85c6fd Kevin Wolf
    new_backing_drv = NULL;
1253 3e85c6fd Kevin Wolf
1254 3e85c6fd Kevin Wolf
    if (!unsafe && bs->backing_format[0] != '\0') {
1255 3e85c6fd Kevin Wolf
        old_backing_drv = bdrv_find_format(bs->backing_format);
1256 3e85c6fd Kevin Wolf
        if (old_backing_drv == NULL) {
1257 3e85c6fd Kevin Wolf
            error("Invalid format name: '%s'", bs->backing_format);
1258 c2abccec MORITA Kazutaka
            ret = -1;
1259 c2abccec MORITA Kazutaka
            goto out;
1260 3e85c6fd Kevin Wolf
        }
1261 3e85c6fd Kevin Wolf
    }
1262 3e85c6fd Kevin Wolf
1263 3e85c6fd Kevin Wolf
    if (out_basefmt != NULL) {
1264 3e85c6fd Kevin Wolf
        new_backing_drv = bdrv_find_format(out_basefmt);
1265 3e85c6fd Kevin Wolf
        if (new_backing_drv == NULL) {
1266 3e85c6fd Kevin Wolf
            error("Invalid format name: '%s'", out_basefmt);
1267 c2abccec MORITA Kazutaka
            ret = -1;
1268 c2abccec MORITA Kazutaka
            goto out;
1269 3e85c6fd Kevin Wolf
        }
1270 3e85c6fd Kevin Wolf
    }
1271 3e85c6fd Kevin Wolf
1272 3e85c6fd Kevin Wolf
    /* For safe rebasing we need to compare old and new backing file */
1273 3e85c6fd Kevin Wolf
    if (unsafe) {
1274 3e85c6fd Kevin Wolf
        /* Make the compiler happy */
1275 3e85c6fd Kevin Wolf
        bs_old_backing = NULL;
1276 3e85c6fd Kevin Wolf
        bs_new_backing = NULL;
1277 3e85c6fd Kevin Wolf
    } else {
1278 3e85c6fd Kevin Wolf
        char backing_name[1024];
1279 3e85c6fd Kevin Wolf
1280 3e85c6fd Kevin Wolf
        bs_old_backing = bdrv_new("old_backing");
1281 3e85c6fd Kevin Wolf
        bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
1282 c2abccec MORITA Kazutaka
        ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS,
1283 c2abccec MORITA Kazutaka
                        old_backing_drv);
1284 c2abccec MORITA Kazutaka
        if (ret) {
1285 3e85c6fd Kevin Wolf
            error("Could not open old backing file '%s'", backing_name);
1286 c2abccec MORITA Kazutaka
            goto out;
1287 3e85c6fd Kevin Wolf
        }
1288 3e85c6fd Kevin Wolf
1289 3e85c6fd Kevin Wolf
        bs_new_backing = bdrv_new("new_backing");
1290 cdbae851 Kevin Wolf
        ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS,
1291 c2abccec MORITA Kazutaka
                        new_backing_drv);
1292 c2abccec MORITA Kazutaka
        if (ret) {
1293 584771e6 Kevin Wolf
            error("Could not open new backing file '%s'", out_baseimg);
1294 c2abccec MORITA Kazutaka
            goto out;
1295 3e85c6fd Kevin Wolf
        }
1296 3e85c6fd Kevin Wolf
    }
1297 3e85c6fd Kevin Wolf
1298 3e85c6fd Kevin Wolf
    /*
1299 3e85c6fd Kevin Wolf
     * Check each unallocated cluster in the COW file. If it is unallocated,
1300 3e85c6fd Kevin Wolf
     * accesses go to the backing file. We must therefore compare this cluster
1301 3e85c6fd Kevin Wolf
     * in the old and new backing file, and if they differ we need to copy it
1302 3e85c6fd Kevin Wolf
     * from the old backing file into the COW file.
1303 3e85c6fd Kevin Wolf
     *
1304 3e85c6fd Kevin Wolf
     * If qemu-img crashes during this step, no harm is done. The content of
1305 3e85c6fd Kevin Wolf
     * the image is the same as the original one at any time.
1306 3e85c6fd Kevin Wolf
     */
1307 3e85c6fd Kevin Wolf
    if (!unsafe) {
1308 3e85c6fd Kevin Wolf
        uint64_t num_sectors;
1309 3e85c6fd Kevin Wolf
        uint64_t sector;
1310 cc60e327 Kevin Wolf
        int n;
1311 d6771bfa TeLeMan
        uint8_t * buf_old;
1312 d6771bfa TeLeMan
        uint8_t * buf_new;
1313 d6771bfa TeLeMan
1314 d6771bfa TeLeMan
        buf_old = qemu_malloc(IO_BUF_SIZE);
1315 d6771bfa TeLeMan
        buf_new = qemu_malloc(IO_BUF_SIZE);
1316 3e85c6fd Kevin Wolf
1317 3e85c6fd Kevin Wolf
        bdrv_get_geometry(bs, &num_sectors);
1318 3e85c6fd Kevin Wolf
1319 3e85c6fd Kevin Wolf
        for (sector = 0; sector < num_sectors; sector += n) {
1320 3e85c6fd Kevin Wolf
1321 3e85c6fd Kevin Wolf
            /* How many sectors can we handle with the next read? */
1322 3e85c6fd Kevin Wolf
            if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
1323 3e85c6fd Kevin Wolf
                n = (IO_BUF_SIZE / 512);
1324 3e85c6fd Kevin Wolf
            } else {
1325 3e85c6fd Kevin Wolf
                n = num_sectors - sector;
1326 3e85c6fd Kevin Wolf
            }
1327 3e85c6fd Kevin Wolf
1328 3e85c6fd Kevin Wolf
            /* If the cluster is allocated, we don't need to take action */
1329 cc60e327 Kevin Wolf
            ret = bdrv_is_allocated(bs, sector, n, &n);
1330 cc60e327 Kevin Wolf
            if (ret) {
1331 3e85c6fd Kevin Wolf
                continue;
1332 3e85c6fd Kevin Wolf
            }
1333 3e85c6fd Kevin Wolf
1334 3e85c6fd Kevin Wolf
            /* Read old and new backing file */
1335 c2abccec MORITA Kazutaka
            ret = bdrv_read(bs_old_backing, sector, buf_old, n);
1336 c2abccec MORITA Kazutaka
            if (ret < 0) {
1337 3e85c6fd Kevin Wolf
                error("error while reading from old backing file");
1338 c2abccec MORITA Kazutaka
                goto out;
1339 3e85c6fd Kevin Wolf
            }
1340 c2abccec MORITA Kazutaka
            ret = bdrv_read(bs_new_backing, sector, buf_new, n);
1341 c2abccec MORITA Kazutaka
            if (ret < 0) {
1342 3e85c6fd Kevin Wolf
                error("error while reading from new backing file");
1343 c2abccec MORITA Kazutaka
                goto out;
1344 3e85c6fd Kevin Wolf
            }
1345 3e85c6fd Kevin Wolf
1346 3e85c6fd Kevin Wolf
            /* If they differ, we need to write to the COW file */
1347 3e85c6fd Kevin Wolf
            uint64_t written = 0;
1348 3e85c6fd Kevin Wolf
1349 3e85c6fd Kevin Wolf
            while (written < n) {
1350 3e85c6fd Kevin Wolf
                int pnum;
1351 3e85c6fd Kevin Wolf
1352 3e85c6fd Kevin Wolf
                if (compare_sectors(buf_old + written * 512,
1353 60b1bd4f Kevin Wolf
                    buf_new + written * 512, n - written, &pnum))
1354 3e85c6fd Kevin Wolf
                {
1355 3e85c6fd Kevin Wolf
                    ret = bdrv_write(bs, sector + written,
1356 3e85c6fd Kevin Wolf
                        buf_old + written * 512, pnum);
1357 3e85c6fd Kevin Wolf
                    if (ret < 0) {
1358 3e85c6fd Kevin Wolf
                        error("Error while writing to COW image: %s",
1359 3e85c6fd Kevin Wolf
                            strerror(-ret));
1360 c2abccec MORITA Kazutaka
                        goto out;
1361 3e85c6fd Kevin Wolf
                    }
1362 3e85c6fd Kevin Wolf
                }
1363 3e85c6fd Kevin Wolf
1364 3e85c6fd Kevin Wolf
                written += pnum;
1365 3e85c6fd Kevin Wolf
            }
1366 3e85c6fd Kevin Wolf
        }
1367 d6771bfa TeLeMan
1368 d6771bfa TeLeMan
        qemu_free(buf_old);
1369 d6771bfa TeLeMan
        qemu_free(buf_new);
1370 3e85c6fd Kevin Wolf
    }
1371 3e85c6fd Kevin Wolf
1372 3e85c6fd Kevin Wolf
    /*
1373 3e85c6fd Kevin Wolf
     * Change the backing file. All clusters that are different from the old
1374 3e85c6fd Kevin Wolf
     * backing file are overwritten in the COW file now, so the visible content
1375 3e85c6fd Kevin Wolf
     * doesn't change when we switch the backing file.
1376 3e85c6fd Kevin Wolf
     */
1377 3e85c6fd Kevin Wolf
    ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
1378 3e85c6fd Kevin Wolf
    if (ret == -ENOSPC) {
1379 3e85c6fd Kevin Wolf
        error("Could not change the backing file to '%s': No space left in "
1380 3e85c6fd Kevin Wolf
            "the file header", out_baseimg);
1381 3e85c6fd Kevin Wolf
    } else if (ret < 0) {
1382 3e85c6fd Kevin Wolf
        error("Could not change the backing file to '%s': %s",
1383 3e85c6fd Kevin Wolf
            out_baseimg, strerror(-ret));
1384 3e85c6fd Kevin Wolf
    }
1385 3e85c6fd Kevin Wolf
1386 3e85c6fd Kevin Wolf
    /*
1387 3e85c6fd Kevin Wolf
     * TODO At this point it is possible to check if any clusters that are
1388 3e85c6fd Kevin Wolf
     * allocated in the COW file are the same in the backing file. If so, they
1389 3e85c6fd Kevin Wolf
     * could be dropped from the COW file. Don't do this before switching the
1390 3e85c6fd Kevin Wolf
     * backing file, in case of a crash this would lead to corruption.
1391 3e85c6fd Kevin Wolf
     */
1392 c2abccec MORITA Kazutaka
out:
1393 3e85c6fd Kevin Wolf
    /* Cleanup */
1394 3e85c6fd Kevin Wolf
    if (!unsafe) {
1395 3e85c6fd Kevin Wolf
        bdrv_delete(bs_old_backing);
1396 3e85c6fd Kevin Wolf
        bdrv_delete(bs_new_backing);
1397 3e85c6fd Kevin Wolf
    }
1398 3e85c6fd Kevin Wolf
1399 3e85c6fd Kevin Wolf
    bdrv_delete(bs);
1400 c2abccec MORITA Kazutaka
    if (ret) {
1401 c2abccec MORITA Kazutaka
        return 1;
1402 c2abccec MORITA Kazutaka
    }
1403 3e85c6fd Kevin Wolf
    return 0;
1404 3e85c6fd Kevin Wolf
}
1405 3e85c6fd Kevin Wolf
1406 ae6b0ed6 Stefan Hajnoczi
static int img_resize(int argc, char **argv)
1407 ae6b0ed6 Stefan Hajnoczi
{
1408 ae6b0ed6 Stefan Hajnoczi
    int c, ret, relative;
1409 ae6b0ed6 Stefan Hajnoczi
    const char *filename, *fmt, *size;
1410 ae6b0ed6 Stefan Hajnoczi
    int64_t n, total_size;
1411 ae6b0ed6 Stefan Hajnoczi
    BlockDriverState *bs;
1412 ae6b0ed6 Stefan Hajnoczi
    QEMUOptionParameter *param;
1413 ae6b0ed6 Stefan Hajnoczi
    QEMUOptionParameter resize_options[] = {
1414 ae6b0ed6 Stefan Hajnoczi
        {
1415 ae6b0ed6 Stefan Hajnoczi
            .name = BLOCK_OPT_SIZE,
1416 ae6b0ed6 Stefan Hajnoczi
            .type = OPT_SIZE,
1417 ae6b0ed6 Stefan Hajnoczi
            .help = "Virtual disk size"
1418 ae6b0ed6 Stefan Hajnoczi
        },
1419 ae6b0ed6 Stefan Hajnoczi
        { NULL }
1420 ae6b0ed6 Stefan Hajnoczi
    };
1421 ae6b0ed6 Stefan Hajnoczi
1422 ae6b0ed6 Stefan Hajnoczi
    fmt = NULL;
1423 ae6b0ed6 Stefan Hajnoczi
    for(;;) {
1424 ae6b0ed6 Stefan Hajnoczi
        c = getopt(argc, argv, "f:h");
1425 ae6b0ed6 Stefan Hajnoczi
        if (c == -1) {
1426 ae6b0ed6 Stefan Hajnoczi
            break;
1427 ae6b0ed6 Stefan Hajnoczi
        }
1428 ae6b0ed6 Stefan Hajnoczi
        switch(c) {
1429 ae6b0ed6 Stefan Hajnoczi
        case 'h':
1430 ae6b0ed6 Stefan Hajnoczi
            help();
1431 ae6b0ed6 Stefan Hajnoczi
            break;
1432 ae6b0ed6 Stefan Hajnoczi
        case 'f':
1433 ae6b0ed6 Stefan Hajnoczi
            fmt = optarg;
1434 ae6b0ed6 Stefan Hajnoczi
            break;
1435 ae6b0ed6 Stefan Hajnoczi
        }
1436 ae6b0ed6 Stefan Hajnoczi
    }
1437 ae6b0ed6 Stefan Hajnoczi
    if (optind + 1 >= argc) {
1438 ae6b0ed6 Stefan Hajnoczi
        help();
1439 ae6b0ed6 Stefan Hajnoczi
    }
1440 ae6b0ed6 Stefan Hajnoczi
    filename = argv[optind++];
1441 ae6b0ed6 Stefan Hajnoczi
    size = argv[optind++];
1442 ae6b0ed6 Stefan Hajnoczi
1443 ae6b0ed6 Stefan Hajnoczi
    /* Choose grow, shrink, or absolute resize mode */
1444 ae6b0ed6 Stefan Hajnoczi
    switch (size[0]) {
1445 ae6b0ed6 Stefan Hajnoczi
    case '+':
1446 ae6b0ed6 Stefan Hajnoczi
        relative = 1;
1447 ae6b0ed6 Stefan Hajnoczi
        size++;
1448 ae6b0ed6 Stefan Hajnoczi
        break;
1449 ae6b0ed6 Stefan Hajnoczi
    case '-':
1450 ae6b0ed6 Stefan Hajnoczi
        relative = -1;
1451 ae6b0ed6 Stefan Hajnoczi
        size++;
1452 ae6b0ed6 Stefan Hajnoczi
        break;
1453 ae6b0ed6 Stefan Hajnoczi
    default:
1454 ae6b0ed6 Stefan Hajnoczi
        relative = 0;
1455 ae6b0ed6 Stefan Hajnoczi
        break;
1456 ae6b0ed6 Stefan Hajnoczi
    }
1457 ae6b0ed6 Stefan Hajnoczi
1458 ae6b0ed6 Stefan Hajnoczi
    /* Parse size */
1459 ae6b0ed6 Stefan Hajnoczi
    param = parse_option_parameters("", resize_options, NULL);
1460 ae6b0ed6 Stefan Hajnoczi
    if (set_option_parameter(param, BLOCK_OPT_SIZE, size)) {
1461 ae6b0ed6 Stefan Hajnoczi
        /* Error message already printed when size parsing fails */
1462 ae6b0ed6 Stefan Hajnoczi
        exit(1);
1463 ae6b0ed6 Stefan Hajnoczi
    }
1464 ae6b0ed6 Stefan Hajnoczi
    n = get_option_parameter(param, BLOCK_OPT_SIZE)->value.n;
1465 ae6b0ed6 Stefan Hajnoczi
    free_option_parameters(param);
1466 ae6b0ed6 Stefan Hajnoczi
1467 ae6b0ed6 Stefan Hajnoczi
    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR);
1468 c2abccec MORITA Kazutaka
    if (!bs) {
1469 c2abccec MORITA Kazutaka
        return 1;
1470 c2abccec MORITA Kazutaka
    }
1471 ae6b0ed6 Stefan Hajnoczi
1472 ae6b0ed6 Stefan Hajnoczi
    if (relative) {
1473 ae6b0ed6 Stefan Hajnoczi
        total_size = bdrv_getlength(bs) + n * relative;
1474 ae6b0ed6 Stefan Hajnoczi
    } else {
1475 ae6b0ed6 Stefan Hajnoczi
        total_size = n;
1476 ae6b0ed6 Stefan Hajnoczi
    }
1477 ae6b0ed6 Stefan Hajnoczi
    if (total_size <= 0) {
1478 ae6b0ed6 Stefan Hajnoczi
        error("New image size must be positive");
1479 c2abccec MORITA Kazutaka
        ret = -1;
1480 c2abccec MORITA Kazutaka
        goto out;
1481 ae6b0ed6 Stefan Hajnoczi
    }
1482 ae6b0ed6 Stefan Hajnoczi
1483 ae6b0ed6 Stefan Hajnoczi
    ret = bdrv_truncate(bs, total_size);
1484 ae6b0ed6 Stefan Hajnoczi
    switch (ret) {
1485 ae6b0ed6 Stefan Hajnoczi
    case 0:
1486 ae6b0ed6 Stefan Hajnoczi
        printf("Image resized.\n");
1487 ae6b0ed6 Stefan Hajnoczi
        break;
1488 ae6b0ed6 Stefan Hajnoczi
    case -ENOTSUP:
1489 ae6b0ed6 Stefan Hajnoczi
        error("This image format does not support resize");
1490 ae6b0ed6 Stefan Hajnoczi
        break;
1491 ae6b0ed6 Stefan Hajnoczi
    case -EACCES:
1492 ae6b0ed6 Stefan Hajnoczi
        error("Image is read-only");
1493 ae6b0ed6 Stefan Hajnoczi
        break;
1494 ae6b0ed6 Stefan Hajnoczi
    default:
1495 ae6b0ed6 Stefan Hajnoczi
        error("Error resizing image (%d)", -ret);
1496 ae6b0ed6 Stefan Hajnoczi
        break;
1497 ae6b0ed6 Stefan Hajnoczi
    }
1498 c2abccec MORITA Kazutaka
out:
1499 ae6b0ed6 Stefan Hajnoczi
    bdrv_delete(bs);
1500 c2abccec MORITA Kazutaka
    if (ret) {
1501 c2abccec MORITA Kazutaka
        return 1;
1502 c2abccec MORITA Kazutaka
    }
1503 ae6b0ed6 Stefan Hajnoczi
    return 0;
1504 ae6b0ed6 Stefan Hajnoczi
}
1505 ae6b0ed6 Stefan Hajnoczi
1506 c227f099 Anthony Liguori
static const img_cmd_t img_cmds[] = {
1507 153859be Stuart Brady
#define DEF(option, callback, arg_string)        \
1508 153859be Stuart Brady
    { option, callback },
1509 153859be Stuart Brady
#include "qemu-img-cmds.h"
1510 153859be Stuart Brady
#undef DEF
1511 153859be Stuart Brady
#undef GEN_DOCS
1512 153859be Stuart Brady
    { NULL, NULL, },
1513 153859be Stuart Brady
};
1514 153859be Stuart Brady
1515 ea2384d3 bellard
int main(int argc, char **argv)
1516 ea2384d3 bellard
{
1517 c227f099 Anthony Liguori
    const img_cmd_t *cmd;
1518 153859be Stuart Brady
    const char *cmdname;
1519 ea2384d3 bellard
1520 ea2384d3 bellard
    bdrv_init();
1521 ea2384d3 bellard
    if (argc < 2)
1522 ea2384d3 bellard
        help();
1523 153859be Stuart Brady
    cmdname = argv[1];
1524 8f9b157e aurel32
    argc--; argv++;
1525 153859be Stuart Brady
1526 153859be Stuart Brady
    /* find the command */
1527 153859be Stuart Brady
    for(cmd = img_cmds; cmd->name != NULL; cmd++) {
1528 153859be Stuart Brady
        if (!strcmp(cmdname, cmd->name)) {
1529 153859be Stuart Brady
            return cmd->handler(argc, argv);
1530 153859be Stuart Brady
        }
1531 ea2384d3 bellard
    }
1532 153859be Stuart Brady
1533 153859be Stuart Brady
    /* not found */
1534 153859be Stuart Brady
    help();
1535 ea2384d3 bellard
    return 0;
1536 ea2384d3 bellard
}