Commit 75c238058423fba689599dda4bed2b5bb7634570

Authored by bellard
1 parent 7c08dbf3

fixed image creation with base filename


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1056 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 1 changed file with 41 additions and 35 deletions
qemu-img.c
@@ -273,6 +273,36 @@ int read_password(char *buf, int buf_size) @@ -273,6 +273,36 @@ int read_password(char *buf, int buf_size)
273 } 273 }
274 #endif 274 #endif
275 275
  276 +static BlockDriverState *bdrv_new_open(const char *filename,
  277 + const char *fmt)
  278 +{
  279 + BlockDriverState *bs;
  280 + BlockDriver *drv;
  281 + char password[256];
  282 +
  283 + bs = bdrv_new("");
  284 + if (!bs)
  285 + error("Not enough memory");
  286 + if (fmt) {
  287 + drv = bdrv_find_format(fmt);
  288 + if (!drv)
  289 + error("Unknown file format '%s'", fmt);
  290 + } else {
  291 + drv = NULL;
  292 + }
  293 + if (bdrv_open2(bs, filename, 0, drv) < 0) {
  294 + error("Could not open '%s'", filename);
  295 + }
  296 + if (bdrv_is_encrypted(bs)) {
  297 + printf("Disk image '%s' is encrypted.\n", filename);
  298 + if (read_password(password, sizeof(password)) < 0)
  299 + error("No password given");
  300 + if (bdrv_set_key(bs, password) < 0)
  301 + error("invalid password");
  302 + }
  303 + return bs;
  304 +}
  305 +
276 static int img_create(int argc, char **argv) 306 static int img_create(int argc, char **argv)
277 { 307 {
278 int c, ret, encrypted; 308 int c, ret, encrypted;
@@ -308,7 +338,13 @@ static int img_create(int argc, char **argv) @@ -308,7 +338,13 @@ static int img_create(int argc, char **argv)
308 help(); 338 help();
309 filename = argv[optind++]; 339 filename = argv[optind++];
310 size = 0; 340 size = 0;
311 - if (!base_filename) { 341 + if (base_filename) {
  342 + BlockDriverState *bs;
  343 + bs = bdrv_new_open(base_filename, NULL);
  344 + bdrv_get_geometry(bs, &size);
  345 + size *= 512;
  346 + bdrv_delete(bs);
  347 + } else {
312 if (optind >= argc) 348 if (optind >= argc)
313 help(); 349 help();
314 p = argv[optind]; 350 p = argv[optind];
@@ -330,11 +366,11 @@ static int img_create(int argc, char **argv) @@ -330,11 +366,11 @@ static int img_create(int argc, char **argv)
330 filename, fmt); 366 filename, fmt);
331 if (encrypted) 367 if (encrypted)
332 printf(", encrypted"); 368 printf(", encrypted");
333 - if (base_filename)  
334 - printf(", backing_file=%s\n", 369 + if (base_filename) {
  370 + printf(", backing_file=%s",
335 base_filename); 371 base_filename);
336 - else  
337 - printf(", size=%lld kB\n", size / 1024); 372 + }
  373 + printf(", size=%lld kB\n", size / 1024);
338 ret = bdrv_create(drv, filename, size / 512, base_filename, encrypted); 374 ret = bdrv_create(drv, filename, size / 512, base_filename, encrypted);
339 if (ret < 0) { 375 if (ret < 0) {
340 if (ret == -ENOTSUP) { 376 if (ret == -ENOTSUP) {
@@ -437,36 +473,6 @@ static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) @@ -437,36 +473,6 @@ static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
437 return v; 473 return v;
438 } 474 }
439 475
440 -static BlockDriverState *bdrv_new_open(const char *filename,  
441 - const char *fmt)  
442 -{  
443 - BlockDriverState *bs;  
444 - BlockDriver *drv;  
445 - char password[256];  
446 -  
447 - bs = bdrv_new("");  
448 - if (!bs)  
449 - error("Not enough memory");  
450 - if (fmt) {  
451 - drv = bdrv_find_format(fmt);  
452 - if (!drv)  
453 - error("Unknown file format '%s'", fmt);  
454 - } else {  
455 - drv = NULL;  
456 - }  
457 - if (bdrv_open2(bs, filename, 0, drv) < 0) {  
458 - error("Could not open '%s'", filename);  
459 - }  
460 - if (bdrv_is_encrypted(bs)) {  
461 - printf("Disk image '%s' is encrypted.\n", filename);  
462 - if (read_password(password, sizeof(password)) < 0)  
463 - error("No password given");  
464 - if (bdrv_set_key(bs, password) < 0)  
465 - error("invalid password");  
466 - }  
467 - return bs;  
468 -}  
469 -  
470 #define IO_BUF_SIZE 65536 476 #define IO_BUF_SIZE 65536
471 477
472 static int img_convert(int argc, char **argv) 478 static int img_convert(int argc, char **argv)