Statistics
| Branch: | Revision:

root / qemu-img.c @ 20caf0f7

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

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