[PATCH 1/1] Add shrink flag to blockresize command

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: patchon <martinsson.patrik@xxxxxxxxx>

These commits simply adds the '--shrink' flag to the blockresize
command to prevent accidental shrinking of a block device. This
behaviour is already present on the vol-resize command and it
makes sense to mimic that behaviour.

Only implemented in the qemu-driver atm.

Signed-off-by: Patrik Martinsson <martinsson.patrik@xxxxxxxxx>
---
 src/qemu/qemu_driver.c | 15 ++++++++++++++-
 tools/virsh-domain.c   |  7 +++++++
 tools/virsh.pod        |  9 ++++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 95fe844c34..4e34eec796 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -11265,8 +11265,10 @@ qemuDomainBlockResize(virDomainPtr dom,
     char *device = NULL;
     const char *nodename = NULL;
     virDomainDiskDefPtr disk = NULL;
+    virDomainBlockInfo info;
 
-    virCheckFlags(VIR_DOMAIN_BLOCK_RESIZE_BYTES, -1);
+    virCheckFlags(VIR_DOMAIN_BLOCK_RESIZE_BYTES |
+                  VIR_STORAGE_VOL_RESIZE_SHRINK, -1);
 
     /* We prefer operating on bytes.  */
     if ((flags & VIR_DOMAIN_BLOCK_RESIZE_BYTES) == 0) {
@@ -11279,6 +11281,9 @@ qemuDomainBlockResize(virDomainPtr dom,
         size *= 1024;
     }
 
+    if (virDomainGetBlockInfo(dom, path, &info, 0) < 0)
+        goto cleanup;
+
     if (!(vm = qemuDomainObjFromDomain(dom)))
         goto cleanup;
 
@@ -11299,6 +11304,14 @@ qemuDomainBlockResize(virDomainPtr dom,
         goto endjob;
     }
 
+    if (size < info.capacity &&
+        !(flags & VIR_STORAGE_VOL_RESIZE_SHRINK)) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Can't shrink capacity below current "
+                         "capacity unless shrink flag explicitly specified"));
+        goto endjob;
+    }
+
     /* qcow2 and qed must be sized on 512 byte blocks/sectors,
      * so adjust size if necessary to round up.
      */
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index fbfdc09c0d..a67ee35428 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -2920,6 +2920,10 @@ static const vshCmdOptDef opts_blockresize[] = {
      .flags = VSH_OFLAG_REQ,
      .help = N_("New size of the block device, as scaled integer (default KiB)")
     },
+    {.name = "shrink",
+     .type = VSH_OT_BOOL,
+     .help = N_("allow the resize to shrink the volume")
+    },
     {.name = NULL}
 };
 
@@ -2938,6 +2942,9 @@ cmdBlockresize(vshControl *ctl, const vshCmd *cmd)
     if (vshCommandOptScaledInt(ctl, cmd, "size", &size, 1024, ULLONG_MAX) < 0)
         return false;
 
+    if (vshCommandOptBool(cmd, "shrink"))
+        flags |= VIR_STORAGE_VOL_RESIZE_SHRINK;
+
     /* Prefer the older interface of KiB.  */
     if (size % 1024 == 0)
         size /= 1024;
diff --git a/tools/virsh.pod b/tools/virsh.pod
index cf2798e71a..96387127d6 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -1027,7 +1027,7 @@ I<bandwidth> specifies copying bandwidth limit in MiB/s. For further information
 on the I<bandwidth> argument see the corresponding section for the B<blockjob>
 command.
 
-=item B<blockresize> I<domain> I<path> I<size>
+=item B<blockresize> I<domain> I<path> I<--size> I<--shrink>
 
 Resize a block device of domain while the domain is running, I<path>
 specifies the absolute path of the block device; it corresponds
@@ -1040,6 +1040,13 @@ I<size> is a scaled integer (see B<NOTES> above) which defaults to KiB
 "B" to get bytes (note that for historical reasons, this differs from
 B<vol-resize> which defaults to bytes without a suffix).
 
+Attempts to shrink the block device will fail unless I<--shrink> is present.
+The I<capacity> cannot be negative since the given capacity is the absolute
+size of the block device. Always exercise caution when shrinking a block device.
+B<Also note> that it is up to the driver to implement the check if shrinking
+should be allowed or not depending on if I<--shrink> is set. So far only the
+qemu-driver does this.
+
 =item B<console> I<domain> [I<devname>] [I<--safe>] [I<--force>]
 
 Connect the virtual serial console for the guest. The optional
-- 
2.23.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux