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