Patchwork [1/3] block: add resize monitor command

login
register
mail settings
Submitter Christoph Hellwig
Date Jan. 14, 2011, 4:20 p.m.
Message ID <20110114162057.GA19184@lst.de>
Download mbox | patch
Permalink /patch/78953/
State New
Headers show

Comments

Christoph Hellwig - Jan. 14, 2011, 4:20 p.m.
Add a monitor command that allows resizing of block devices while
qemu is running.  It uses the existing bdrv_truncate method already
used by qemu-img to do it's work.  Compared to qemu-img the size
parsing is very simplicistic, but I think having a properly numering
object is more useful for non-humand monitor users than having
the units and relative resize parsing.

For SCSI devices the new size can be updated in Linux guests by
doing the following shell command:

	echo > /sys/class/scsi_device/0:0:0:0/device/rescan

For ATA devices I don't know of a way to update the block device
size in Linux system, and for virtio-blk the next two patches
will provide an automatic update of the size when this command
is issued on the host.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Stefan Hajnoczi - Jan. 17, 2011, 11:28 a.m.
On Fri, Jan 14, 2011 at 4:20 PM, Christoph Hellwig <hch@lst.de> wrote:
> +STEXI
> +@item resize
> +@findex resize
> +Resize a block image while a guest is running.  Usuaully requires guest

s/Usuaully/Usually/

> +action to see the updated size.  Resize to a lower size is supported,
> +but should be used with extreme caution.

This resizes the image files.  Resizing an LVM volume is a useful case
too.  One way to integrate that feature is to implement a host_device
.bdrv_truncate() that checks the underlying device size and returns
success if it matches the new value and failure otherwise.  Or perhaps
allow the QEMU resize command without an argument to fetch the size
from the underlying device (aka refresh the size).

> +    if (bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM) {
> +        error_report("Can not resize CDROM devices\n");
> +        return -1;
> +    }

Hrm...BDRV_TYPE_FLOPPY probably too?

Stefan
Christoph Hellwig - Jan. 17, 2011, 3:59 p.m.
On Mon, Jan 17, 2011 at 11:28:47AM +0000, Stefan Hajnoczi wrote:
> > + ? ?if (bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM) {
> > + ? ? ? ?error_report("Can not resize CDROM devices\n");
> > + ? ? ? ?return -1;
> > + ? ?}
> 
> Hrm...BDRV_TYPE_FLOPPY probably too?

If we want to be consistent, yes.  Or we could remove the CDROM check
and let people shoot themselves in the foot as much as they want.
Kevin Wolf - Jan. 17, 2011, 5:36 p.m.
Am 14.01.2011 17:20, schrieb Christoph Hellwig:
> Add a monitor command that allows resizing of block devices while
> qemu is running.  It uses the existing bdrv_truncate method already
> used by qemu-img to do it's work.  Compared to qemu-img the size
> parsing is very simplicistic, but I think having a properly numering
> object is more useful for non-humand monitor users than having
> the units and relative resize parsing.
> 
> For SCSI devices the new size can be updated in Linux guests by
> doing the following shell command:
> 
> 	echo > /sys/class/scsi_device/0:0:0:0/device/rescan
> 
> For ATA devices I don't know of a way to update the block device
> size in Linux system, and for virtio-blk the next two patches
> will provide an automatic update of the size when this command
> is issued on the host.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> 
> Index: qemu/hmp-commands.hx
> ===================================================================
> --- qemu.orig/hmp-commands.hx	2011-01-14 15:11:48.527004132 +0100
> +++ qemu/hmp-commands.hx	2011-01-14 15:40:14.407006506 +0100
> @@ -53,6 +53,24 @@ Quit the emulator.
>  ETEXI
>  
>      {
> +        .name       = "resize",
> +        .args_type  = "id:s,size:l",

size should be 'o' instead of 'l'. The latter may be too small on 32 bit
hosts and doesn't support convenient suffixes:

 * 'l'          target long (32 or 64 bit)

 * 'o'          octets (aka bytes)
 *              user mode accepts an optional T, t, G, g, M, m, K, k
 *              suffix, which multiplies the value by 2^40 for
 *              suffixes T and t, 2^30 for suffixes G and g, 2^20 for
 *              M and m, 2^10 for K and k

> +        .params     = "device size",
> +        .help       = "resize a block image",
> +        .user_print = monitor_user_noop,
> +        .mhandler.cmd_new = do_resize,
> +    },
> +
> +STEXI
> +@item resize
> +@findex resize
> +Resize a block image while a guest is running.  Usuaully requires guest
> +action to see the updated size.  Resize to a lower size is supported,
> +but should be used with extreme caution.
> +ETEXI
> +
> +
> +    {
>          .name       = "eject",
>          .args_type  = "force:-f,device:B",
>          .params     = "[-f] device",
> Index: qemu/blockdev.c
> ===================================================================
> --- qemu.orig/blockdev.c	2011-01-14 15:11:48.539261151 +0100
> +++ qemu/blockdev.c	2011-01-14 15:50:35.604293558 +0100
> @@ -700,3 +700,41 @@ int do_drive_del(Monitor *mon, const QDi
>  
>      return 0;
>  }
> +
> +int do_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
> +{
> +    const char *id = qdict_get_str(qdict, "id");
> +    int64_t size = qdict_get_int(qdict, "size");
> +    BlockDriverState *bs;
> +
> +    bs = bdrv_find(id);
> +    if (!bs) {
> +        qerror_report(QERR_DEVICE_NOT_FOUND, id);
> +        return -1;
> +    }
> +
> +    if (bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM) {
> +        error_report("Can not resize CDROM devices\n");
> +        return -1;
> +    }

Hm, is there a real reason except that CD-ROMs are read-only? The code
below seems to take read-only devices into account.

Kevin
Christoph Hellwig - Jan. 18, 2011, 12:28 p.m.
On Mon, Jan 17, 2011 at 06:36:15PM +0100, Kevin Wolf wrote:
> size should be 'o' instead of 'l'. The latter may be too small on 32 bit
> hosts and doesn't support convenient suffixes:

Fixed.

> Hm, is there a real reason except that CD-ROMs are read-only? The code
> below seems to take read-only devices into account.

I've removed the CDROM check for the next version.
Christoph Hellwig - Jan. 19, 2011, 3:35 p.m.
On Mon, Jan 17, 2011 at 06:36:15PM +0100, Kevin Wolf wrote:
> > +        .args_type  = "id:s,size:l",
> 
> size should be 'o' instead of 'l'. The latter may be too small on 32 bit
> hosts and doesn't support convenient suffixes:

o actually fails for 2GB or more for me:

(qemu) resize scratch 2047 
resize scratch 2047 
(qemu) 
(qemu) resize scrarch 2048
resize scrarch 2048
invalid size

for l these worked fine.
Kevin Wolf - Jan. 19, 2011, 3:49 p.m.
Am 19.01.2011 16:35, schrieb Christoph Hellwig:
> On Mon, Jan 17, 2011 at 06:36:15PM +0100, Kevin Wolf wrote:
>>> +        .args_type  = "id:s,size:l",
>>
>> size should be 'o' instead of 'l'. The latter may be too small on 32 bit
>> hosts and doesn't support convenient suffixes:
> 
> o actually fails for 2GB or more for me:
> 
> (qemu) resize scratch 2047 
> resize scratch 2047 
> (qemu) 
> (qemu) resize scrarch 2048
> resize scrarch 2048
> invalid size
> 
> for l these worked fine.

Hm, yeah, 'o' uses ssize_t instead of int64_t, so it's broken on a 32
bit host as well. Though I assume that you use a 64 bit host, and I
can't really see what's the problem there...

Kevin
Christoph Hellwig - Jan. 19, 2011, 3:52 p.m.
On Wed, Jan 19, 2011 at 04:49:04PM +0100, Kevin Wolf wrote:
> > (qemu) resize scratch 2047 
> > resize scratch 2047 
> > (qemu) 
> > (qemu) resize scrarch 2048
> > resize scrarch 2048
> > invalid size
> > 
> > for l these worked fine.
> 
> Hm, yeah, 'o' uses ssize_t instead of int64_t, so it's broken on a 32
> bit host as well. Though I assume that you use a 64 bit host, and I
> can't really see what's the problem there...

This is a test on a 32-bit host.  The target_long of "l" worked fine
because a kvm enabled qemu always builds for an x86-64 target, even
with a 32-bit host.

Patch

Index: qemu/hmp-commands.hx
===================================================================
--- qemu.orig/hmp-commands.hx	2011-01-14 15:11:48.527004132 +0100
+++ qemu/hmp-commands.hx	2011-01-14 15:40:14.407006506 +0100
@@ -53,6 +53,24 @@  Quit the emulator.
 ETEXI
 
     {
+        .name       = "resize",
+        .args_type  = "id:s,size:l",
+        .params     = "device size",
+        .help       = "resize a block image",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_resize,
+    },
+
+STEXI
+@item resize
+@findex resize
+Resize a block image while a guest is running.  Usuaully requires guest
+action to see the updated size.  Resize to a lower size is supported,
+but should be used with extreme caution.
+ETEXI
+
+
+    {
         .name       = "eject",
         .args_type  = "force:-f,device:B",
         .params     = "[-f] device",
Index: qemu/blockdev.c
===================================================================
--- qemu.orig/blockdev.c	2011-01-14 15:11:48.539261151 +0100
+++ qemu/blockdev.c	2011-01-14 15:50:35.604293558 +0100
@@ -700,3 +700,41 @@  int do_drive_del(Monitor *mon, const QDi
 
     return 0;
 }
+
+int do_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    const char *id = qdict_get_str(qdict, "id");
+    int64_t size = qdict_get_int(qdict, "size");
+    BlockDriverState *bs;
+
+    bs = bdrv_find(id);
+    if (!bs) {
+        qerror_report(QERR_DEVICE_NOT_FOUND, id);
+        return -1;
+    }
+
+    if (bdrv_get_type_hint(bs) == BDRV_TYPE_CDROM) {
+        error_report("Can not resize CDROM devices\n");
+        return -1;
+    }
+
+    if (size < 0) {
+        error_report("Can only resize to positive sizes");
+        return -1;
+    }
+
+    switch (bdrv_truncate(bs, size)) {
+    case 0:
+        return 0;
+    case -ENOTSUP:
+        error_report("This image format does not support resize");
+        return -1;
+    case -EACCES:
+        error_report("Image is read-only");
+        return -1;
+    default:
+        error_report("Error resizing image");
+        return -1;
+    }
+}
+
Index: qemu/blockdev.h
===================================================================
--- qemu.orig/blockdev.h	2011-01-14 15:11:48.548263456 +0100
+++ qemu/blockdev.h	2011-01-14 15:12:00.965047363 +0100
@@ -53,5 +53,6 @@  int do_change_block(Monitor *mon, const
                     const char *filename, const char *fmt);
 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data);
+int do_resize(Monitor *mon, const QDict *qdict, QObject **ret_data);
 
 #endif