Statistics
| Branch: | Revision:

root / qemu-img.c @ 4cdbc094

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

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