Statistics
| Branch: | Revision:

root / qemu-img.c @ 63e886eb

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

1014 93c65b47 aliguori
                   If the output is to a host device, we also write out
1015 93c65b47 aliguori
                   sectors that are entirely 0, since whatever data was
1016 93c65b47 aliguori
                   already there is garbage, not 0s. */
1017 f2feebbd Kevin Wolf
                if (!has_zero_init || out_baseimg ||
1018 a22f123c Kevin Wolf
                    is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
1019 c2abccec MORITA Kazutaka
                    ret = bdrv_write(out_bs, sector_num, buf1, n1);
1020 c2abccec MORITA Kazutaka
                    if (ret < 0) {
1021 3fba9d81 Stefan Hajnoczi
                        error_report("error while writing sector %" PRId64
1022 3fba9d81 Stefan Hajnoczi
                                     ": %s", sector_num, strerror(-ret));
1023 c2abccec MORITA Kazutaka
                        goto out;
1024 c2abccec MORITA Kazutaka
                    }
1025 ea2384d3 bellard
                }
1026 ea2384d3 bellard
                sector_num += n1;
1027 ea2384d3 bellard
                n -= n1;
1028 ea2384d3 bellard
                buf1 += n1 * 512;
1029 ea2384d3 bellard
            }
1030 6b837bc4 Jes Sorensen
            qemu_progress_print(local_progress, 100);
1031 ea2384d3 bellard
        }
1032 ea2384d3 bellard
    }
1033 c2abccec MORITA Kazutaka
out:
1034 6b837bc4 Jes Sorensen
    qemu_progress_end();
1035 c2abccec MORITA Kazutaka
    free_option_parameters(create_options);
1036 c2abccec MORITA Kazutaka
    free_option_parameters(param);
1037 bb1c0597 Kevin Wolf
    qemu_vfree(buf);
1038 c2abccec MORITA Kazutaka
    if (out_bs) {
1039 c2abccec MORITA Kazutaka
        bdrv_delete(out_bs);
1040 c2abccec MORITA Kazutaka
    }
1041 31ca34b8 Jes Sorensen
    if (bs) {
1042 31ca34b8 Jes Sorensen
        for (bs_i = 0; bs_i < bs_n; bs_i++) {
1043 31ca34b8 Jes Sorensen
            if (bs[bs_i]) {
1044 31ca34b8 Jes Sorensen
                bdrv_delete(bs[bs_i]);
1045 31ca34b8 Jes Sorensen
            }
1046 c2abccec MORITA Kazutaka
        }
1047 7267c094 Anthony Liguori
        g_free(bs);
1048 c2abccec MORITA Kazutaka
    }
1049 c2abccec MORITA Kazutaka
    if (ret) {
1050 c2abccec MORITA Kazutaka
        return 1;
1051 c2abccec MORITA Kazutaka
    }
1052 ea2384d3 bellard
    return 0;
1053 ea2384d3 bellard
}
1054 ea2384d3 bellard
1055 57d1a2b6 bellard
1056 faea38e7 bellard
static void dump_snapshots(BlockDriverState *bs)
1057 faea38e7 bellard
{
1058 faea38e7 bellard
    QEMUSnapshotInfo *sn_tab, *sn;
1059 faea38e7 bellard
    int nb_sns, i;
1060 faea38e7 bellard
    char buf[256];
1061 faea38e7 bellard
1062 faea38e7 bellard
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1063 faea38e7 bellard
    if (nb_sns <= 0)
1064 faea38e7 bellard
        return;
1065 faea38e7 bellard
    printf("Snapshot list:\n");
1066 faea38e7 bellard
    printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1067 faea38e7 bellard
    for(i = 0; i < nb_sns; i++) {
1068 faea38e7 bellard
        sn = &sn_tab[i];
1069 faea38e7 bellard
        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1070 faea38e7 bellard
    }
1071 7267c094 Anthony Liguori
    g_free(sn_tab);
1072 faea38e7 bellard
}
1073 faea38e7 bellard
1074 ea2384d3 bellard
static int img_info(int argc, char **argv)
1075 ea2384d3 bellard
{
1076 ea2384d3 bellard
    int c;
1077 ea2384d3 bellard
    const char *filename, *fmt;
1078 ea2384d3 bellard
    BlockDriverState *bs;
1079 ea2384d3 bellard
    char fmt_name[128], size_buf[128], dsize_buf[128];
1080 96b8f136 ths
    uint64_t total_sectors;
1081 96b8f136 ths
    int64_t allocated_size;
1082 93b6b2a3 bellard
    char backing_filename[1024];
1083 93b6b2a3 bellard
    char backing_filename2[1024];
1084 faea38e7 bellard
    BlockDriverInfo bdi;
1085 ea2384d3 bellard
1086 ea2384d3 bellard
    fmt = NULL;
1087 ea2384d3 bellard
    for(;;) {
1088 ea2384d3 bellard
        c = getopt(argc, argv, "f:h");
1089 b8fb60da Jes Sorensen
        if (c == -1) {
1090 ea2384d3 bellard
            break;
1091 b8fb60da Jes Sorensen
        }
1092 ea2384d3 bellard
        switch(c) {
1093 ef87394c Jes Sorensen
        case '?':
1094 ea2384d3 bellard
        case 'h':
1095 ea2384d3 bellard
            help();
1096 ea2384d3 bellard
            break;
1097 ea2384d3 bellard
        case 'f':
1098 ea2384d3 bellard
            fmt = optarg;
1099 ea2384d3 bellard
            break;
1100 ea2384d3 bellard
        }
1101 ea2384d3 bellard
    }
1102 b8fb60da Jes Sorensen
    if (optind >= argc) {
1103 ea2384d3 bellard
        help();
1104 b8fb60da Jes Sorensen
    }
1105 ea2384d3 bellard
    filename = argv[optind++];
1106 ea2384d3 bellard
1107 adfe078e Stefan Hajnoczi
    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING);
1108 c2abccec MORITA Kazutaka
    if (!bs) {
1109 c2abccec MORITA Kazutaka
        return 1;
1110 c2abccec MORITA Kazutaka
    }
1111 ea2384d3 bellard
    bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
1112 ea2384d3 bellard
    bdrv_get_geometry(bs, &total_sectors);
1113 ea2384d3 bellard
    get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
1114 4a1d5e1f Fam Zheng
    allocated_size = bdrv_get_allocated_file_size(bs);
1115 b8fb60da Jes Sorensen
    if (allocated_size < 0) {
1116 a10ea30b blueswir1
        snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
1117 b8fb60da Jes Sorensen
    } else {
1118 5fafdf24 ths
        get_human_readable_size(dsize_buf, sizeof(dsize_buf),
1119 de167e41 bellard
                                allocated_size);
1120 b8fb60da Jes Sorensen
    }
1121 ea2384d3 bellard
    printf("image: %s\n"
1122 ea2384d3 bellard
           "file format: %s\n"
1123 ec3757de bellard
           "virtual size: %s (%" PRId64 " bytes)\n"
1124 ea2384d3 bellard
           "disk size: %s\n",
1125 5fafdf24 ths
           filename, fmt_name, size_buf,
1126 ec3757de bellard
           (total_sectors * 512),
1127 ea2384d3 bellard
           dsize_buf);
1128 b8fb60da Jes Sorensen
    if (bdrv_is_encrypted(bs)) {
1129 ea2384d3 bellard
        printf("encrypted: yes\n");
1130 b8fb60da Jes Sorensen
    }
1131 faea38e7 bellard
    if (bdrv_get_info(bs, &bdi) >= 0) {
1132 b8fb60da Jes Sorensen
        if (bdi.cluster_size != 0) {
1133 faea38e7 bellard
            printf("cluster_size: %d\n", bdi.cluster_size);
1134 b8fb60da Jes Sorensen
        }
1135 64c79160 Dong Xu Wang
        if (bdi.is_dirty) {
1136 64c79160 Dong Xu Wang
            printf("cleanly shut down: no\n");
1137 64c79160 Dong Xu Wang
        }
1138 faea38e7 bellard
    }
1139 93b6b2a3 bellard
    bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
1140 faea38e7 bellard
    if (backing_filename[0] != '\0') {
1141 93b6b2a3 bellard
        path_combine(backing_filename2, sizeof(backing_filename2),
1142 93b6b2a3 bellard
                     filename, backing_filename);
1143 5fafdf24 ths
        printf("backing file: %s (actual path: %s)\n",
1144 93b6b2a3 bellard
               backing_filename,
1145 93b6b2a3 bellard
               backing_filename2);
1146 faea38e7 bellard
    }
1147 faea38e7 bellard
    dump_snapshots(bs);
1148 ea2384d3 bellard
    bdrv_delete(bs);
1149 ea2384d3 bellard
    return 0;
1150 ea2384d3 bellard
}
1151 ea2384d3 bellard
1152 f7b4a940 aliguori
#define SNAPSHOT_LIST   1
1153 f7b4a940 aliguori
#define SNAPSHOT_CREATE 2
1154 f7b4a940 aliguori
#define SNAPSHOT_APPLY  3
1155 f7b4a940 aliguori
#define SNAPSHOT_DELETE 4
1156 f7b4a940 aliguori
1157 153859be Stuart Brady
static int img_snapshot(int argc, char **argv)
1158 f7b4a940 aliguori
{
1159 f7b4a940 aliguori
    BlockDriverState *bs;
1160 f7b4a940 aliguori
    QEMUSnapshotInfo sn;
1161 f7b4a940 aliguori
    char *filename, *snapshot_name = NULL;
1162 c2abccec MORITA Kazutaka
    int c, ret = 0, bdrv_oflags;
1163 f7b4a940 aliguori
    int action = 0;
1164 f7b4a940 aliguori
    qemu_timeval tv;
1165 f7b4a940 aliguori
1166 710da702 Kevin Wolf
    bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
1167 f7b4a940 aliguori
    /* Parse commandline parameters */
1168 f7b4a940 aliguori
    for(;;) {
1169 f7b4a940 aliguori
        c = getopt(argc, argv, "la:c:d:h");
1170 b8fb60da Jes Sorensen
        if (c == -1) {
1171 f7b4a940 aliguori
            break;
1172 b8fb60da Jes Sorensen
        }
1173 f7b4a940 aliguori
        switch(c) {
1174 ef87394c Jes Sorensen
        case '?':
1175 f7b4a940 aliguori
        case 'h':
1176 f7b4a940 aliguori
            help();
1177 153859be Stuart Brady
            return 0;
1178 f7b4a940 aliguori
        case 'l':
1179 f7b4a940 aliguori
            if (action) {
1180 f7b4a940 aliguori
                help();
1181 153859be Stuart Brady
                return 0;
1182 f7b4a940 aliguori
            }
1183 f7b4a940 aliguori
            action = SNAPSHOT_LIST;
1184 f5edb014 Naphtali Sprei
            bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
1185 f7b4a940 aliguori
            break;
1186 f7b4a940 aliguori
        case 'a':
1187 f7b4a940 aliguori
            if (action) {
1188 f7b4a940 aliguori
                help();
1189 153859be Stuart Brady
                return 0;
1190 f7b4a940 aliguori
            }
1191 f7b4a940 aliguori
            action = SNAPSHOT_APPLY;
1192 f7b4a940 aliguori
            snapshot_name = optarg;
1193 f7b4a940 aliguori
            break;
1194 f7b4a940 aliguori
        case 'c':
1195 f7b4a940 aliguori
            if (action) {
1196 f7b4a940 aliguori
                help();
1197 153859be Stuart Brady
                return 0;
1198 f7b4a940 aliguori
            }
1199 f7b4a940 aliguori
            action = SNAPSHOT_CREATE;
1200 f7b4a940 aliguori
            snapshot_name = optarg;
1201 f7b4a940 aliguori
            break;
1202 f7b4a940 aliguori
        case 'd':
1203 f7b4a940 aliguori
            if (action) {
1204 f7b4a940 aliguori
                help();
1205 153859be Stuart Brady
                return 0;
1206 f7b4a940 aliguori
            }
1207 f7b4a940 aliguori
            action = SNAPSHOT_DELETE;
1208 f7b4a940 aliguori
            snapshot_name = optarg;
1209 f7b4a940 aliguori
            break;
1210 f7b4a940 aliguori
        }
1211 f7b4a940 aliguori
    }
1212 f7b4a940 aliguori
1213 b8fb60da Jes Sorensen
    if (optind >= argc) {
1214 f7b4a940 aliguori
        help();
1215 b8fb60da Jes Sorensen
    }
1216 f7b4a940 aliguori
    filename = argv[optind++];
1217 f7b4a940 aliguori
1218 f7b4a940 aliguori
    /* Open the image */
1219 f163d073 Stefan Hajnoczi
    bs = bdrv_new_open(filename, NULL, bdrv_oflags);
1220 c2abccec MORITA Kazutaka
    if (!bs) {
1221 c2abccec MORITA Kazutaka
        return 1;
1222 c2abccec MORITA Kazutaka
    }
1223 f7b4a940 aliguori
1224 f7b4a940 aliguori
    /* Perform the requested action */
1225 f7b4a940 aliguori
    switch(action) {
1226 f7b4a940 aliguori
    case SNAPSHOT_LIST:
1227 f7b4a940 aliguori
        dump_snapshots(bs);
1228 f7b4a940 aliguori
        break;
1229 f7b4a940 aliguori
1230 f7b4a940 aliguori
    case SNAPSHOT_CREATE:
1231 f7b4a940 aliguori
        memset(&sn, 0, sizeof(sn));
1232 f7b4a940 aliguori
        pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
1233 f7b4a940 aliguori
1234 f7b4a940 aliguori
        qemu_gettimeofday(&tv);
1235 f7b4a940 aliguori
        sn.date_sec = tv.tv_sec;
1236 f7b4a940 aliguori
        sn.date_nsec = tv.tv_usec * 1000;
1237 f7b4a940 aliguori
1238 f7b4a940 aliguori
        ret = bdrv_snapshot_create(bs, &sn);
1239 b8fb60da Jes Sorensen
        if (ret) {
1240 15654a6d Jes Sorensen
            error_report("Could not create snapshot '%s': %d (%s)",
1241 f7b4a940 aliguori
                snapshot_name, ret, strerror(-ret));
1242 b8fb60da Jes Sorensen
        }
1243 f7b4a940 aliguori
        break;
1244 f7b4a940 aliguori
1245 f7b4a940 aliguori
    case SNAPSHOT_APPLY:
1246 f7b4a940 aliguori
        ret = bdrv_snapshot_goto(bs, snapshot_name);
1247 b8fb60da Jes Sorensen
        if (ret) {
1248 15654a6d Jes Sorensen
            error_report("Could not apply snapshot '%s': %d (%s)",
1249 f7b4a940 aliguori
                snapshot_name, ret, strerror(-ret));
1250 b8fb60da Jes Sorensen
        }
1251 f7b4a940 aliguori
        break;
1252 f7b4a940 aliguori
1253 f7b4a940 aliguori
    case SNAPSHOT_DELETE:
1254 f7b4a940 aliguori
        ret = bdrv_snapshot_delete(bs, snapshot_name);
1255 b8fb60da Jes Sorensen
        if (ret) {
1256 15654a6d Jes Sorensen
            error_report("Could not delete snapshot '%s': %d (%s)",
1257 f7b4a940 aliguori
                snapshot_name, ret, strerror(-ret));
1258 b8fb60da Jes Sorensen
        }
1259 f7b4a940 aliguori
        break;
1260 f7b4a940 aliguori
    }
1261 f7b4a940 aliguori
1262 f7b4a940 aliguori
    /* Cleanup */
1263 f7b4a940 aliguori
    bdrv_delete(bs);
1264 c2abccec MORITA Kazutaka
    if (ret) {
1265 c2abccec MORITA Kazutaka
        return 1;
1266 c2abccec MORITA Kazutaka
    }
1267 153859be Stuart Brady
    return 0;
1268 f7b4a940 aliguori
}
1269 f7b4a940 aliguori
1270 3e85c6fd Kevin Wolf
static int img_rebase(int argc, char **argv)
1271 3e85c6fd Kevin Wolf
{
1272 c2abccec MORITA Kazutaka
    BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
1273 f163d073 Stefan Hajnoczi
    BlockDriver *old_backing_drv, *new_backing_drv;
1274 3e85c6fd Kevin Wolf
    char *filename;
1275 661a0f71 Federico Simoncelli
    const char *fmt, *cache, *out_basefmt, *out_baseimg;
1276 3e85c6fd Kevin Wolf
    int c, flags, ret;
1277 3e85c6fd Kevin Wolf
    int unsafe = 0;
1278 6b837bc4 Jes Sorensen
    int progress = 0;
1279 3e85c6fd Kevin Wolf
1280 3e85c6fd Kevin Wolf
    /* Parse commandline parameters */
1281 e53dbee0 Kevin Wolf
    fmt = NULL;
1282 661a0f71 Federico Simoncelli
    cache = BDRV_DEFAULT_CACHE;
1283 3e85c6fd Kevin Wolf
    out_baseimg = NULL;
1284 3e85c6fd Kevin Wolf
    out_basefmt = NULL;
1285 3e85c6fd Kevin Wolf
    for(;;) {
1286 661a0f71 Federico Simoncelli
        c = getopt(argc, argv, "uhf:F:b:pt:");
1287 b8fb60da Jes Sorensen
        if (c == -1) {
1288 3e85c6fd Kevin Wolf
            break;
1289 b8fb60da Jes Sorensen
        }
1290 3e85c6fd Kevin Wolf
        switch(c) {
1291 ef87394c Jes Sorensen
        case '?':
1292 3e85c6fd Kevin Wolf
        case 'h':
1293 3e85c6fd Kevin Wolf
            help();
1294 3e85c6fd Kevin Wolf
            return 0;
1295 e53dbee0 Kevin Wolf
        case 'f':
1296 e53dbee0 Kevin Wolf
            fmt = optarg;
1297 e53dbee0 Kevin Wolf
            break;
1298 3e85c6fd Kevin Wolf
        case 'F':
1299 3e85c6fd Kevin Wolf
            out_basefmt = optarg;
1300 3e85c6fd Kevin Wolf
            break;
1301 3e85c6fd Kevin Wolf
        case 'b':
1302 3e85c6fd Kevin Wolf
            out_baseimg = optarg;
1303 3e85c6fd Kevin Wolf
            break;
1304 3e85c6fd Kevin Wolf
        case 'u':
1305 3e85c6fd Kevin Wolf
            unsafe = 1;
1306 3e85c6fd Kevin Wolf
            break;
1307 6b837bc4 Jes Sorensen
        case 'p':
1308 6b837bc4 Jes Sorensen
            progress = 1;
1309 6b837bc4 Jes Sorensen
            break;
1310 661a0f71 Federico Simoncelli
        case 't':
1311 661a0f71 Federico Simoncelli
            cache = optarg;
1312 661a0f71 Federico Simoncelli
            break;
1313 3e85c6fd Kevin Wolf
        }
1314 3e85c6fd Kevin Wolf
    }
1315 3e85c6fd Kevin Wolf
1316 9a9d9dba Anthony Liguori
    if ((optind >= argc) || (!unsafe && !out_baseimg)) {
1317 3e85c6fd Kevin Wolf
        help();
1318 b8fb60da Jes Sorensen
    }
1319 3e85c6fd Kevin Wolf
    filename = argv[optind++];
1320 3e85c6fd Kevin Wolf
1321 6b837bc4 Jes Sorensen
    qemu_progress_init(progress, 2.0);
1322 6b837bc4 Jes Sorensen
    qemu_progress_print(0, 100);
1323 6b837bc4 Jes Sorensen
1324 661a0f71 Federico Simoncelli
    flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
1325 c3993cdc Stefan Hajnoczi
    ret = bdrv_parse_cache_flags(cache, &flags);
1326 661a0f71 Federico Simoncelli
    if (ret < 0) {
1327 661a0f71 Federico Simoncelli
        error_report("Invalid cache option: %s", cache);
1328 661a0f71 Federico Simoncelli
        return -1;
1329 661a0f71 Federico Simoncelli
    }
1330 661a0f71 Federico Simoncelli
1331 3e85c6fd Kevin Wolf
    /*
1332 3e85c6fd Kevin Wolf
     * Open the images.
1333 3e85c6fd Kevin Wolf
     *
1334 3e85c6fd Kevin Wolf
     * Ignore the old backing file for unsafe rebase in case we want to correct
1335 3e85c6fd Kevin Wolf
     * the reference to a renamed or moved backing file.
1336 3e85c6fd Kevin Wolf
     */
1337 f163d073 Stefan Hajnoczi
    bs = bdrv_new_open(filename, fmt, flags);
1338 c2abccec MORITA Kazutaka
    if (!bs) {
1339 c2abccec MORITA Kazutaka
        return 1;
1340 c2abccec MORITA Kazutaka
    }
1341 3e85c6fd Kevin Wolf
1342 3e85c6fd Kevin Wolf
    /* Find the right drivers for the backing files */
1343 3e85c6fd Kevin Wolf
    old_backing_drv = NULL;
1344 3e85c6fd Kevin Wolf
    new_backing_drv = NULL;
1345 3e85c6fd Kevin Wolf
1346 3e85c6fd Kevin Wolf
    if (!unsafe && bs->backing_format[0] != '\0') {
1347 3e85c6fd Kevin Wolf
        old_backing_drv = bdrv_find_format(bs->backing_format);
1348 3e85c6fd Kevin Wolf
        if (old_backing_drv == NULL) {
1349 15654a6d Jes Sorensen
            error_report("Invalid format name: '%s'", bs->backing_format);
1350 c2abccec MORITA Kazutaka
            ret = -1;
1351 c2abccec MORITA Kazutaka
            goto out;
1352 3e85c6fd Kevin Wolf
        }
1353 3e85c6fd Kevin Wolf
    }
1354 3e85c6fd Kevin Wolf
1355 3e85c6fd Kevin Wolf
    if (out_basefmt != NULL) {
1356 3e85c6fd Kevin Wolf
        new_backing_drv = bdrv_find_format(out_basefmt);
1357 3e85c6fd Kevin Wolf
        if (new_backing_drv == NULL) {
1358 15654a6d Jes Sorensen
            error_report("Invalid format name: '%s'", out_basefmt);
1359 c2abccec MORITA Kazutaka
            ret = -1;
1360 c2abccec MORITA Kazutaka
            goto out;
1361 3e85c6fd Kevin Wolf
        }
1362 3e85c6fd Kevin Wolf
    }
1363 3e85c6fd Kevin Wolf
1364 3e85c6fd Kevin Wolf
    /* For safe rebasing we need to compare old and new backing file */
1365 3e85c6fd Kevin Wolf
    if (unsafe) {
1366 3e85c6fd Kevin Wolf
        /* Make the compiler happy */
1367 3e85c6fd Kevin Wolf
        bs_old_backing = NULL;
1368 3e85c6fd Kevin Wolf
        bs_new_backing = NULL;
1369 3e85c6fd Kevin Wolf
    } else {
1370 3e85c6fd Kevin Wolf
        char backing_name[1024];
1371 3e85c6fd Kevin Wolf
1372 3e85c6fd Kevin Wolf
        bs_old_backing = bdrv_new("old_backing");
1373 3e85c6fd Kevin Wolf
        bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
1374 c2abccec MORITA Kazutaka
        ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS,
1375 c2abccec MORITA Kazutaka
                        old_backing_drv);
1376 c2abccec MORITA Kazutaka
        if (ret) {
1377 15654a6d Jes Sorensen
            error_report("Could not open old backing file '%s'", backing_name);
1378 c2abccec MORITA Kazutaka
            goto out;
1379 3e85c6fd Kevin Wolf
        }
1380 3e85c6fd Kevin Wolf
1381 3e85c6fd Kevin Wolf
        bs_new_backing = bdrv_new("new_backing");
1382 cdbae851 Kevin Wolf
        ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS,
1383 c2abccec MORITA Kazutaka
                        new_backing_drv);
1384 c2abccec MORITA Kazutaka
        if (ret) {
1385 15654a6d Jes Sorensen
            error_report("Could not open new backing file '%s'", out_baseimg);
1386 c2abccec MORITA Kazutaka
            goto out;
1387 3e85c6fd Kevin Wolf
        }
1388 3e85c6fd Kevin Wolf
    }
1389 3e85c6fd Kevin Wolf
1390 3e85c6fd Kevin Wolf
    /*
1391 3e85c6fd Kevin Wolf
     * Check each unallocated cluster in the COW file. If it is unallocated,
1392 3e85c6fd Kevin Wolf
     * accesses go to the backing file. We must therefore compare this cluster
1393 3e85c6fd Kevin Wolf
     * in the old and new backing file, and if they differ we need to copy it
1394 3e85c6fd Kevin Wolf
     * from the old backing file into the COW file.
1395 3e85c6fd Kevin Wolf
     *
1396 3e85c6fd Kevin Wolf
     * If qemu-img crashes during this step, no harm is done. The content of
1397 3e85c6fd Kevin Wolf
     * the image is the same as the original one at any time.
1398 3e85c6fd Kevin Wolf
     */
1399 3e85c6fd Kevin Wolf
    if (!unsafe) {
1400 3e85c6fd Kevin Wolf
        uint64_t num_sectors;
1401 87a1b3e3 Kevin Wolf
        uint64_t old_backing_num_sectors;
1402 87a1b3e3 Kevin Wolf
        uint64_t new_backing_num_sectors;
1403 3e85c6fd Kevin Wolf
        uint64_t sector;
1404 cc60e327 Kevin Wolf
        int n;
1405 d6771bfa TeLeMan
        uint8_t * buf_old;
1406 d6771bfa TeLeMan
        uint8_t * buf_new;
1407 6b837bc4 Jes Sorensen
        float local_progress;
1408 d6771bfa TeLeMan
1409 bb1c0597 Kevin Wolf
        buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
1410 bb1c0597 Kevin Wolf
        buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
1411 3e85c6fd Kevin Wolf
1412 3e85c6fd Kevin Wolf
        bdrv_get_geometry(bs, &num_sectors);
1413 87a1b3e3 Kevin Wolf
        bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
1414 87a1b3e3 Kevin Wolf
        bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
1415 3e85c6fd Kevin Wolf
1416 6b837bc4 Jes Sorensen
        local_progress = (float)100 /
1417 4ee96418 Jes Sorensen
            (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
1418 3e85c6fd Kevin Wolf
        for (sector = 0; sector < num_sectors; sector += n) {
1419 3e85c6fd Kevin Wolf
1420 3e85c6fd Kevin Wolf
            /* How many sectors can we handle with the next read? */
1421 3e85c6fd Kevin Wolf
            if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
1422 3e85c6fd Kevin Wolf
                n = (IO_BUF_SIZE / 512);
1423 3e85c6fd Kevin Wolf
            } else {
1424 3e85c6fd Kevin Wolf
                n = num_sectors - sector;
1425 3e85c6fd Kevin Wolf
            }
1426 3e85c6fd Kevin Wolf
1427 3e85c6fd Kevin Wolf
            /* If the cluster is allocated, we don't need to take action */
1428 cc60e327 Kevin Wolf
            ret = bdrv_is_allocated(bs, sector, n, &n);
1429 cc60e327 Kevin Wolf
            if (ret) {
1430 3e85c6fd Kevin Wolf
                continue;
1431 3e85c6fd Kevin Wolf
            }
1432 3e85c6fd Kevin Wolf
1433 87a1b3e3 Kevin Wolf
            /*
1434 87a1b3e3 Kevin Wolf
             * Read old and new backing file and take into consideration that
1435 87a1b3e3 Kevin Wolf
             * backing files may be smaller than the COW image.
1436 87a1b3e3 Kevin Wolf
             */
1437 87a1b3e3 Kevin Wolf
            if (sector >= old_backing_num_sectors) {
1438 87a1b3e3 Kevin Wolf
                memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
1439 87a1b3e3 Kevin Wolf
            } else {
1440 87a1b3e3 Kevin Wolf
                if (sector + n > old_backing_num_sectors) {
1441 87a1b3e3 Kevin Wolf
                    n = old_backing_num_sectors - sector;
1442 87a1b3e3 Kevin Wolf
                }
1443 87a1b3e3 Kevin Wolf
1444 87a1b3e3 Kevin Wolf
                ret = bdrv_read(bs_old_backing, sector, buf_old, n);
1445 87a1b3e3 Kevin Wolf
                if (ret < 0) {
1446 87a1b3e3 Kevin Wolf
                    error_report("error while reading from old backing file");
1447 87a1b3e3 Kevin Wolf
                    goto out;
1448 87a1b3e3 Kevin Wolf
                }
1449 3e85c6fd Kevin Wolf
            }
1450 87a1b3e3 Kevin Wolf
1451 87a1b3e3 Kevin Wolf
            if (sector >= new_backing_num_sectors) {
1452 87a1b3e3 Kevin Wolf
                memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
1453 87a1b3e3 Kevin Wolf
            } else {
1454 87a1b3e3 Kevin Wolf
                if (sector + n > new_backing_num_sectors) {
1455 87a1b3e3 Kevin Wolf
                    n = new_backing_num_sectors - sector;
1456 87a1b3e3 Kevin Wolf
                }
1457 87a1b3e3 Kevin Wolf
1458 87a1b3e3 Kevin Wolf
                ret = bdrv_read(bs_new_backing, sector, buf_new, n);
1459 87a1b3e3 Kevin Wolf
                if (ret < 0) {
1460 87a1b3e3 Kevin Wolf
                    error_report("error while reading from new backing file");
1461 87a1b3e3 Kevin Wolf
                    goto out;
1462 87a1b3e3 Kevin Wolf
                }
1463 3e85c6fd Kevin Wolf
            }
1464 3e85c6fd Kevin Wolf
1465 3e85c6fd Kevin Wolf
            /* If they differ, we need to write to the COW file */
1466 3e85c6fd Kevin Wolf
            uint64_t written = 0;
1467 3e85c6fd Kevin Wolf
1468 3e85c6fd Kevin Wolf
            while (written < n) {
1469 3e85c6fd Kevin Wolf
                int pnum;
1470 3e85c6fd Kevin Wolf
1471 3e85c6fd Kevin Wolf
                if (compare_sectors(buf_old + written * 512,
1472 60b1bd4f Kevin Wolf
                    buf_new + written * 512, n - written, &pnum))
1473 3e85c6fd Kevin Wolf
                {
1474 3e85c6fd Kevin Wolf
                    ret = bdrv_write(bs, sector + written,
1475 3e85c6fd Kevin Wolf
                        buf_old + written * 512, pnum);
1476 3e85c6fd Kevin Wolf
                    if (ret < 0) {
1477 15654a6d Jes Sorensen
                        error_report("Error while writing to COW image: %s",
1478 3e85c6fd Kevin Wolf
                            strerror(-ret));
1479 c2abccec MORITA Kazutaka
                        goto out;
1480 3e85c6fd Kevin Wolf
                    }
1481 3e85c6fd Kevin Wolf
                }
1482 3e85c6fd Kevin Wolf
1483 3e85c6fd Kevin Wolf
                written += pnum;
1484 3e85c6fd Kevin Wolf
            }
1485 6b837bc4 Jes Sorensen
            qemu_progress_print(local_progress, 100);
1486 3e85c6fd Kevin Wolf
        }
1487 d6771bfa TeLeMan
1488 bb1c0597 Kevin Wolf
        qemu_vfree(buf_old);
1489 bb1c0597 Kevin Wolf
        qemu_vfree(buf_new);
1490 3e85c6fd Kevin Wolf
    }
1491 3e85c6fd Kevin Wolf
1492 3e85c6fd Kevin Wolf
    /*
1493 3e85c6fd Kevin Wolf
     * Change the backing file. All clusters that are different from the old
1494 3e85c6fd Kevin Wolf
     * backing file are overwritten in the COW file now, so the visible content
1495 3e85c6fd Kevin Wolf
     * doesn't change when we switch the backing file.
1496 3e85c6fd Kevin Wolf
     */
1497 3e85c6fd Kevin Wolf
    ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
1498 3e85c6fd Kevin Wolf
    if (ret == -ENOSPC) {
1499 15654a6d Jes Sorensen
        error_report("Could not change the backing file to '%s': No "
1500 15654a6d Jes Sorensen
                     "space left in the file header", out_baseimg);
1501 3e85c6fd Kevin Wolf
    } else if (ret < 0) {
1502 15654a6d Jes Sorensen
        error_report("Could not change the backing file to '%s': %s",
1503 3e85c6fd Kevin Wolf
            out_baseimg, strerror(-ret));
1504 3e85c6fd Kevin Wolf
    }
1505 3e85c6fd Kevin Wolf
1506 6b837bc4 Jes Sorensen
    qemu_progress_print(100, 0);
1507 3e85c6fd Kevin Wolf
    /*
1508 3e85c6fd Kevin Wolf
     * TODO At this point it is possible to check if any clusters that are
1509 3e85c6fd Kevin Wolf
     * allocated in the COW file are the same in the backing file. If so, they
1510 3e85c6fd Kevin Wolf
     * could be dropped from the COW file. Don't do this before switching the
1511 3e85c6fd Kevin Wolf
     * backing file, in case of a crash this would lead to corruption.
1512 3e85c6fd Kevin Wolf
     */
1513 c2abccec MORITA Kazutaka
out:
1514 6b837bc4 Jes Sorensen
    qemu_progress_end();
1515 3e85c6fd Kevin Wolf
    /* Cleanup */
1516 3e85c6fd Kevin Wolf
    if (!unsafe) {
1517 eb863add Kevin Wolf
        if (bs_old_backing != NULL) {
1518 eb863add Kevin Wolf
            bdrv_delete(bs_old_backing);
1519 eb863add Kevin Wolf
        }
1520 eb863add Kevin Wolf
        if (bs_new_backing != NULL) {
1521 eb863add Kevin Wolf
            bdrv_delete(bs_new_backing);
1522 eb863add Kevin Wolf
        }
1523 3e85c6fd Kevin Wolf
    }
1524 3e85c6fd Kevin Wolf
1525 3e85c6fd Kevin Wolf
    bdrv_delete(bs);
1526 c2abccec MORITA Kazutaka
    if (ret) {
1527 c2abccec MORITA Kazutaka
        return 1;
1528 c2abccec MORITA Kazutaka
    }
1529 3e85c6fd Kevin Wolf
    return 0;
1530 3e85c6fd Kevin Wolf
}
1531 3e85c6fd Kevin Wolf
1532 ae6b0ed6 Stefan Hajnoczi
static int img_resize(int argc, char **argv)
1533 ae6b0ed6 Stefan Hajnoczi
{
1534 ae6b0ed6 Stefan Hajnoczi
    int c, ret, relative;
1535 ae6b0ed6 Stefan Hajnoczi
    const char *filename, *fmt, *size;
1536 ae6b0ed6 Stefan Hajnoczi
    int64_t n, total_size;
1537 2a81998a Jes Sorensen
    BlockDriverState *bs = NULL;
1538 ae6b0ed6 Stefan Hajnoczi
    QEMUOptionParameter *param;
1539 ae6b0ed6 Stefan Hajnoczi
    QEMUOptionParameter resize_options[] = {
1540 ae6b0ed6 Stefan Hajnoczi
        {
1541 ae6b0ed6 Stefan Hajnoczi
            .name = BLOCK_OPT_SIZE,
1542 ae6b0ed6 Stefan Hajnoczi
            .type = OPT_SIZE,
1543 ae6b0ed6 Stefan Hajnoczi
            .help = "Virtual disk size"
1544 ae6b0ed6 Stefan Hajnoczi
        },
1545 ae6b0ed6 Stefan Hajnoczi
        { NULL }
1546 ae6b0ed6 Stefan Hajnoczi
    };
1547 ae6b0ed6 Stefan Hajnoczi
1548 e80fec7f Kevin Wolf
    /* Remove size from argv manually so that negative numbers are not treated
1549 e80fec7f Kevin Wolf
     * as options by getopt. */
1550 e80fec7f Kevin Wolf
    if (argc < 3) {
1551 e80fec7f Kevin Wolf
        help();
1552 e80fec7f Kevin Wolf
        return 1;
1553 e80fec7f Kevin Wolf
    }
1554 e80fec7f Kevin Wolf
1555 e80fec7f Kevin Wolf
    size = argv[--argc];
1556 e80fec7f Kevin Wolf
1557 e80fec7f Kevin Wolf
    /* Parse getopt arguments */
1558 ae6b0ed6 Stefan Hajnoczi
    fmt = NULL;
1559 ae6b0ed6 Stefan Hajnoczi
    for(;;) {
1560 ae6b0ed6 Stefan Hajnoczi
        c = getopt(argc, argv, "f:h");
1561 ae6b0ed6 Stefan Hajnoczi
        if (c == -1) {
1562 ae6b0ed6 Stefan Hajnoczi
            break;
1563 ae6b0ed6 Stefan Hajnoczi
        }
1564 ae6b0ed6 Stefan Hajnoczi
        switch(c) {
1565 ef87394c Jes Sorensen
        case '?':
1566 ae6b0ed6 Stefan Hajnoczi
        case 'h':
1567 ae6b0ed6 Stefan Hajnoczi
            help();
1568 ae6b0ed6 Stefan Hajnoczi
            break;
1569 ae6b0ed6 Stefan Hajnoczi
        case 'f':
1570 ae6b0ed6 Stefan Hajnoczi
            fmt = optarg;
1571 ae6b0ed6 Stefan Hajnoczi
            break;
1572 ae6b0ed6 Stefan Hajnoczi
        }
1573 ae6b0ed6 Stefan Hajnoczi
    }
1574 e80fec7f Kevin Wolf
    if (optind >= argc) {
1575 ae6b0ed6 Stefan Hajnoczi
        help();
1576 ae6b0ed6 Stefan Hajnoczi
    }
1577 ae6b0ed6 Stefan Hajnoczi
    filename = argv[optind++];
1578 ae6b0ed6 Stefan Hajnoczi
1579 ae6b0ed6 Stefan Hajnoczi
    /* Choose grow, shrink, or absolute resize mode */
1580 ae6b0ed6 Stefan Hajnoczi
    switch (size[0]) {
1581 ae6b0ed6 Stefan Hajnoczi
    case '+':
1582 ae6b0ed6 Stefan Hajnoczi
        relative = 1;
1583 ae6b0ed6 Stefan Hajnoczi
        size++;
1584 ae6b0ed6 Stefan Hajnoczi
        break;
1585 ae6b0ed6 Stefan Hajnoczi
    case '-':
1586 ae6b0ed6 Stefan Hajnoczi
        relative = -1;
1587 ae6b0ed6 Stefan Hajnoczi
        size++;
1588 ae6b0ed6 Stefan Hajnoczi
        break;
1589 ae6b0ed6 Stefan Hajnoczi
    default:
1590 ae6b0ed6 Stefan Hajnoczi
        relative = 0;
1591 ae6b0ed6 Stefan Hajnoczi
        break;
1592 ae6b0ed6 Stefan Hajnoczi
    }
1593 ae6b0ed6 Stefan Hajnoczi
1594 ae6b0ed6 Stefan Hajnoczi
    /* Parse size */
1595 ae6b0ed6 Stefan Hajnoczi
    param = parse_option_parameters("", resize_options, NULL);
1596 ae6b0ed6 Stefan Hajnoczi
    if (set_option_parameter(param, BLOCK_OPT_SIZE, size)) {
1597 ae6b0ed6 Stefan Hajnoczi
        /* Error message already printed when size parsing fails */
1598 2a81998a Jes Sorensen
        ret = -1;
1599 2a81998a Jes Sorensen
        goto out;
1600 ae6b0ed6 Stefan Hajnoczi
    }
1601 ae6b0ed6 Stefan Hajnoczi
    n = get_option_parameter(param, BLOCK_OPT_SIZE)->value.n;
1602 ae6b0ed6 Stefan Hajnoczi
    free_option_parameters(param);
1603 ae6b0ed6 Stefan Hajnoczi
1604 ae6b0ed6 Stefan Hajnoczi
    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR);
1605 c2abccec MORITA Kazutaka
    if (!bs) {
1606 2a81998a Jes Sorensen
        ret = -1;
1607 2a81998a Jes Sorensen
        goto out;
1608 c2abccec MORITA Kazutaka
    }
1609 ae6b0ed6 Stefan Hajnoczi
1610 ae6b0ed6 Stefan Hajnoczi
    if (relative) {
1611 ae6b0ed6 Stefan Hajnoczi
        total_size = bdrv_getlength(bs) + n * relative;
1612 ae6b0ed6 Stefan Hajnoczi
    } else {
1613 ae6b0ed6 Stefan Hajnoczi
        total_size = n;
1614 ae6b0ed6 Stefan Hajnoczi
    }
1615 ae6b0ed6 Stefan Hajnoczi
    if (total_size <= 0) {
1616 15654a6d Jes Sorensen
        error_report("New image size must be positive");
1617 c2abccec MORITA Kazutaka
        ret = -1;
1618 c2abccec MORITA Kazutaka
        goto out;
1619 ae6b0ed6 Stefan Hajnoczi
    }
1620 ae6b0ed6 Stefan Hajnoczi
1621 ae6b0ed6 Stefan Hajnoczi
    ret = bdrv_truncate(bs, total_size);
1622 ae6b0ed6 Stefan Hajnoczi
    switch (ret) {
1623 ae6b0ed6 Stefan Hajnoczi
    case 0:
1624 ae6b0ed6 Stefan Hajnoczi
        printf("Image resized.\n");
1625 ae6b0ed6 Stefan Hajnoczi
        break;
1626 ae6b0ed6 Stefan Hajnoczi
    case -ENOTSUP:
1627 259b2173 Kevin Wolf
        error_report("This image does not support resize");
1628 ae6b0ed6 Stefan Hajnoczi
        break;
1629 ae6b0ed6 Stefan Hajnoczi
    case -EACCES:
1630 15654a6d Jes Sorensen
        error_report("Image is read-only");
1631 ae6b0ed6 Stefan Hajnoczi
        break;
1632 ae6b0ed6 Stefan Hajnoczi
    default:
1633 15654a6d Jes Sorensen
        error_report("Error resizing image (%d)", -ret);
1634 ae6b0ed6 Stefan Hajnoczi
        break;
1635 ae6b0ed6 Stefan Hajnoczi
    }
1636 c2abccec MORITA Kazutaka
out:
1637 2a81998a Jes Sorensen
    if (bs) {
1638 2a81998a Jes Sorensen
        bdrv_delete(bs);
1639 2a81998a Jes Sorensen
    }
1640 c2abccec MORITA Kazutaka
    if (ret) {
1641 c2abccec MORITA Kazutaka
        return 1;
1642 c2abccec MORITA Kazutaka
    }
1643 ae6b0ed6 Stefan Hajnoczi
    return 0;
1644 ae6b0ed6 Stefan Hajnoczi
}
1645 ae6b0ed6 Stefan Hajnoczi
1646 c227f099 Anthony Liguori
static const img_cmd_t img_cmds[] = {
1647 153859be Stuart Brady
#define DEF(option, callback, arg_string)        \
1648 153859be Stuart Brady
    { option, callback },
1649 153859be Stuart Brady
#include "qemu-img-cmds.h"
1650 153859be Stuart Brady
#undef DEF
1651 153859be Stuart Brady
#undef GEN_DOCS
1652 153859be Stuart Brady
    { NULL, NULL, },
1653 153859be Stuart Brady
};
1654 153859be Stuart Brady
1655 ea2384d3 bellard
int main(int argc, char **argv)
1656 ea2384d3 bellard
{
1657 c227f099 Anthony Liguori
    const img_cmd_t *cmd;
1658 153859be Stuart Brady
    const char *cmdname;
1659 ea2384d3 bellard
1660 53f76e58 Kevin Wolf
    error_set_progname(argv[0]);
1661 53f76e58 Kevin Wolf
1662 ea2384d3 bellard
    bdrv_init();
1663 ea2384d3 bellard
    if (argc < 2)
1664 ea2384d3 bellard
        help();
1665 153859be Stuart Brady
    cmdname = argv[1];
1666 8f9b157e aurel32
    argc--; argv++;
1667 153859be Stuart Brady
1668 67d384e8 Zhi Yong Wu
    qemu_init_main_loop();
1669 67d384e8 Zhi Yong Wu
1670 153859be Stuart Brady
    /* find the command */
1671 153859be Stuart Brady
    for(cmd = img_cmds; cmd->name != NULL; cmd++) {
1672 153859be Stuart Brady
        if (!strcmp(cmdname, cmd->name)) {
1673 153859be Stuart Brady
            return cmd->handler(argc, argv);
1674 153859be Stuart Brady
        }
1675 ea2384d3 bellard
    }
1676 153859be Stuart Brady
1677 153859be Stuart Brady
    /* not found */
1678 153859be Stuart Brady
    help();
1679 ea2384d3 bellard
    return 0;
1680 ea2384d3 bellard
}