qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

qemu-img: add --shrink flag for resize

The flag is additional precaution against data loss. Perhaps in the future the
operation shrink without this flag will be blocked for all formats, but for now
we need to maintain compatibility with raw.

Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
Message-id: 20170918124230.8152-2-pbutsykin@virtuozzo.com
[mreitz: Added a missing space to a warning]
Signed-off-by: Max Reitz <mreitz@redhat.com>

authored by

Pavel Butsykin and committed by
Max Reitz
4ffca890 69ff158b

+33 -6
+2 -2
qemu-img-cmds.hx
··· 89 89 ETEXI 90 90 91 91 DEF("resize", img_resize, 92 - "resize [--object objectdef] [--image-opts] [-q] filename [+ | -]size") 92 + "resize [--object objectdef] [--image-opts] [-q] [--shrink] filename [+ | -]size") 93 93 STEXI 94 - @item resize [--object @var{objectdef}] [--image-opts] [-q] @var{filename} [+ | -]@var{size} 94 + @item resize [--object @var{objectdef}] [--image-opts] [-q] [--shrink] @var{filename} [+ | -]@var{size} 95 95 ETEXI 96 96 97 97 STEXI
+23
qemu-img.c
··· 64 64 OPTION_TARGET_IMAGE_OPTS = 263, 65 65 OPTION_SIZE = 264, 66 66 OPTION_PREALLOCATION = 265, 67 + OPTION_SHRINK = 266, 67 68 }; 68 69 69 70 typedef enum OutputFormat { ··· 3436 3437 }, 3437 3438 }; 3438 3439 bool image_opts = false; 3440 + bool shrink = false; 3439 3441 3440 3442 /* Remove size from argv manually so that negative numbers are not treated 3441 3443 * as options by getopt. */ ··· 3454 3456 {"object", required_argument, 0, OPTION_OBJECT}, 3455 3457 {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, 3456 3458 {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, 3459 + {"shrink", no_argument, 0, OPTION_SHRINK}, 3457 3460 {0, 0, 0, 0} 3458 3461 }; 3459 3462 c = getopt_long(argc, argv, ":f:hq", ··· 3495 3498 error_report("Invalid preallocation mode '%s'", optarg); 3496 3499 return 1; 3497 3500 } 3501 + break; 3502 + case OPTION_SHRINK: 3503 + shrink = true; 3498 3504 break; 3499 3505 } 3500 3506 } ··· 3567 3573 error_report("Preallocation can only be used for growing images"); 3568 3574 ret = -1; 3569 3575 goto out; 3576 + } 3577 + 3578 + if (total_size < current_size && !shrink) { 3579 + warn_report("Shrinking an image will delete all data beyond the " 3580 + "shrunken image's end. Before performing such an " 3581 + "operation, make sure there is no important data there."); 3582 + 3583 + if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) { 3584 + error_report( 3585 + "Use the --shrink option to perform a shrink operation."); 3586 + ret = -1; 3587 + goto out; 3588 + } else { 3589 + warn_report("Using the --shrink option will suppress this message. " 3590 + "Note that future versions of qemu-img may refuse to " 3591 + "shrink images without this option."); 3592 + } 3570 3593 } 3571 3594 3572 3595 ret = blk_truncate(blk, total_size, prealloc, &err);
+5 -1
qemu-img.texi
··· 545 545 At this point, @code{modified.img} can be discarded, since 546 546 @code{base.img + diff.qcow2} contains the same information. 547 547 548 - @item resize [--preallocation=@var{prealloc}] @var{filename} [+ | -]@var{size} 548 + @item resize [--shrink] [--preallocation=@var{prealloc}] @var{filename} [+ | -]@var{size} 549 549 550 550 Change the disk image as if it had been created with @var{size}. 551 551 552 552 Before using this command to shrink a disk image, you MUST use file system and 553 553 partitioning tools inside the VM to reduce allocated file systems and partition 554 554 sizes accordingly. Failure to do so will result in data loss! 555 + 556 + When shrinking images, the @code{--shrink} option must be given. This informs 557 + qemu-img that the user acknowledges all loss of data beyond the truncated 558 + image's end. 555 559 556 560 After using this command to grow a disk image, you must use file system and 557 561 partitioning tools inside the VM to actually begin using the new space on the
+2 -2
tests/qemu-iotests/102
··· 54 54 $QEMU_IO -c 'write 0 64k' "$TEST_IMG" | _filter_qemu_io 55 55 # Remove data cluster from image (first cluster: image header, second: reftable, 56 56 # third: refblock, fourth: L1 table, fifth: L2 table) 57 - $QEMU_IMG resize -f raw "$TEST_IMG" $((5 * 64 * 1024)) 57 + $QEMU_IMG resize -f raw --shrink "$TEST_IMG" $((5 * 64 * 1024)) 58 58 59 59 $QEMU_IO -c map "$TEST_IMG" 60 60 $QEMU_IMG map "$TEST_IMG" ··· 69 69 70 70 qemu_comm_method=monitor _launch_qemu -drive if=none,file="$TEST_IMG",id=drv0 71 71 72 - $QEMU_IMG resize -f raw "$TEST_IMG" $((5 * 64 * 1024)) 72 + $QEMU_IMG resize -f raw --shrink "$TEST_IMG" $((5 * 64 * 1024)) 73 73 74 74 _send_qemu_cmd $QEMU_HANDLE 'qemu-io drv0 map' 'allocated' \ 75 75 | sed -e 's/^(qemu).*qemu-io drv0 map...$/(qemu) qemu-io drv0 map/'
+1 -1
tests/qemu-iotests/106
··· 83 83 for growth_mode in falloc full off; do 84 84 echo 85 85 echo "--- growth_mode=$growth_mode ---" 86 - $QEMU_IMG resize -f "$IMGFMT" --preallocation=$growth_mode "$TEST_IMG" -${GROWTH_SIZE}K 86 + $QEMU_IMG resize -f "$IMGFMT" --shrink --preallocation=$growth_mode "$TEST_IMG" -${GROWTH_SIZE}K 87 87 done 88 88 89 89 # success, all done