Commit ca94dbc753ecc7318f329106f55712f1faaa7cf2

Authored by Kevin Wolf
Committed by Anthony Liguori
1 parent bf4e5d92

qemu-io: Implement bdrv_load_vmstate/bdrv_save_vmstate

The load_vmstate and save_vmstate functions are implemented as a
variation of the normal read/write operation, enabled by the -b option.
This is the same mechanism as is used to switch from read/write to
pread/pwrite.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing 1 changed file with 44 additions and 6 deletions
qemu-io.c
... ... @@ -187,6 +187,22 @@ static int do_pwrite(char *buf, int64_t offset, int count, int *total)
187 187 return 1;
188 188 }
189 189  
  190 +static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
  191 +{
  192 + *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
  193 + if (*total < 0)
  194 + return *total;
  195 + return 1;
  196 +}
  197 +
  198 +static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
  199 +{
  200 + *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
  201 + if (*total < 0)
  202 + return *total;
  203 + return 1;
  204 +}
  205 +
190 206 #define NOT_DONE 0x7fffffff
191 207 static void aio_rw_done(void *opaque, int ret)
192 208 {
... ... @@ -242,6 +258,7 @@ read_help(void)
242 258 "\n"
243 259 " Reads a segment of the currently open file, optionally dumping it to the\n"
244 260 " standard output stream (with -v option) for subsequent inspection.\n"
  261 +" -b, -- read from the VM state rather than the virtual disk\n"
245 262 " -C, -- report statistics in a machine parsable format\n"
246 263 " -l, -- length for pattern verification (only with -P)\n"
247 264 " -p, -- use bdrv_pread to read the file\n"
... ... @@ -257,7 +274,7 @@ read_f(int argc, char **argv)
257 274 {
258 275 struct timeval t1, t2;
259 276 int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
260   - int Pflag = 0, sflag = 0, lflag = 0;
  277 + int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
261 278 int c, cnt;
262 279 char *buf;
263 280 int64_t offset;
... ... @@ -266,8 +283,11 @@ read_f(int argc, char **argv)
266 283 int total = 0;
267 284 int pattern = 0, pattern_offset = 0, pattern_count = 0;
268 285  
269   - while ((c = getopt(argc, argv, "Cl:pP:qs:v")) != EOF) {
  286 + while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
270 287 switch (c) {
  288 + case 'b':
  289 + bflag = 1;
  290 + break;
271 291 case 'C':
272 292 Cflag = 1;
273 293 break;
... ... @@ -308,6 +328,11 @@ read_f(int argc, char **argv)
308 328 if (optind != argc - 2)
309 329 return command_usage(&read_cmd);
310 330  
  331 + if (bflag && pflag) {
  332 + printf("-b and -p cannot be specified at the same time\n");
  333 + return 0;
  334 + }
  335 +
311 336 offset = cvtnum(argv[optind]);
312 337 if (offset < 0) {
313 338 printf("non-numeric length argument -- %s\n", argv[optind]);
... ... @@ -352,6 +377,8 @@ read_f(int argc, char **argv)
352 377 gettimeofday(&t1, NULL);
353 378 if (pflag)
354 379 cnt = do_pread(buf, offset, count, &total);
  380 + else if (bflag)
  381 + cnt = do_load_vmstate(buf, offset, count, &total);
355 382 else
356 383 cnt = do_read(buf, offset, count, &total);
357 384 gettimeofday(&t2, NULL);
... ... @@ -394,7 +421,7 @@ static const cmdinfo_t read_cmd = {
394 421 .cfunc = read_f,
395 422 .argmin = 2,
396 423 .argmax = -1,
397   - .args = "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
  424 + .args = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
398 425 .oneline = "reads a number of bytes at a specified offset",
399 426 .help = read_help,
400 427 };
... ... @@ -534,6 +561,7 @@ write_help(void)
534 561 "\n"
535 562 " Writes into a segment of the currently open file, using a buffer\n"
536 563 " filled with a set pattern (0xcdcdcdcd).\n"
  564 +" -b, -- write to the VM state rather than the virtual disk\n"
537 565 " -p, -- use bdrv_pwrite to write the file\n"
538 566 " -P, -- use different pattern to fill file\n"
539 567 " -C, -- report statistics in a machine parsable format\n"
... ... @@ -545,7 +573,7 @@ static int
545 573 write_f(int argc, char **argv)
546 574 {
547 575 struct timeval t1, t2;
548   - int Cflag = 0, pflag = 0, qflag = 0;
  576 + int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
549 577 int c, cnt;
550 578 char *buf;
551 579 int64_t offset;
... ... @@ -554,8 +582,11 @@ write_f(int argc, char **argv)
554 582 int total = 0;
555 583 int pattern = 0xcd;
556 584  
557   - while ((c = getopt(argc, argv, "CpP:q")) != EOF) {
  585 + while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
558 586 switch (c) {
  587 + case 'b':
  588 + bflag = 1;
  589 + break;
559 590 case 'C':
560 591 Cflag = 1;
561 592 break;
... ... @@ -576,6 +607,11 @@ write_f(int argc, char **argv)
576 607 if (optind != argc - 2)
577 608 return command_usage(&write_cmd);
578 609  
  610 + if (bflag && pflag) {
  611 + printf("-b and -p cannot be specified at the same time\n");
  612 + return 0;
  613 + }
  614 +
579 615 offset = cvtnum(argv[optind]);
580 616 if (offset < 0) {
581 617 printf("non-numeric length argument -- %s\n", argv[optind]);
... ... @@ -608,6 +644,8 @@ write_f(int argc, char **argv)
608 644 gettimeofday(&t1, NULL);
609 645 if (pflag)
610 646 cnt = do_pwrite(buf, offset, count, &total);
  647 + else if (bflag)
  648 + cnt = do_save_vmstate(buf, offset, count, &total);
611 649 else
612 650 cnt = do_write(buf, offset, count, &total);
613 651 gettimeofday(&t2, NULL);
... ... @@ -636,7 +674,7 @@ static const cmdinfo_t write_cmd = {
636 674 .cfunc = write_f,
637 675 .argmin = 2,
638 676 .argmax = -1,
639   - .args = "[-aCpq] [-P pattern ] off len",
  677 + .args = "[-abCpq] [-P pattern ] off len",
640 678 .oneline = "writes a number of bytes at a specified offset",
641 679 .help = write_help,
642 680 };
... ...