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

login
register
mail settings
Submitter Christoph Hellwig
Date Jan. 24, 2011, 12:32 p.m.
Message ID <20110124123233.GA16613@lst.de>
Download mbox | patch
Permalink /patch/80158/
State New
Headers show

Comments

Christoph Hellwig - Jan. 24, 2011, 12:32 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>
Marcelo Tosatti - Jan. 24, 2011, 5:44 p.m.
On Mon, Jan 24, 2011 at 01:32:33PM +0100, Christoph Hellwig wrote:
> 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-24 11:55:36.744254374 +0100
> +++ qemu/hmp-commands.hx	2011-01-24 11:56:23.619254094 +0100
> @@ -53,6 +53,25 @@ Quit the emulator.
>  ETEXI
>  
>      {
> +        .name       = "block_resize",
> +        .args_type  = "device:B,size:o",
> +        .params     = "device size",
> +        .help       = "resize a block image",
> +        .user_print = monitor_user_noop,
> +        .mhandler.cmd_new = do_block_resize,
> +    },
> +
> +STEXI
> +@item block_resize
> +@findex block_resize
> +Resize a block image while a guest is running.  Usually requires guest
> +action to see the updated size.  Resize to a lower size is supported,
> +but should be used with extreme caution.  Note that this command only
> +resizes image files, it can not resize block devices like LVM volumes.
> +ETEXI
> +
> +
> +    {
>          .name       = "eject",
>          .args_type  = "force:-f,device:B",
>          .params     = "[-f] device",
> Index: qemu/blockdev.c
> ===================================================================
> --- qemu.orig/blockdev.c	2011-01-24 11:56:20.903004129 +0100
> +++ qemu/blockdev.c	2011-01-24 11:56:38.391254165 +0100
> @@ -705,3 +705,33 @@ int do_drive_del(Monitor *mon, const QDi
>  
>      return 0;
>  }
> +
> +/*
> + * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
> + * existing QERR_ macro mess is cleaned up.  A good example for better
> + * error reports can be found in the qemu-img resize code.
> + */
> +int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
> +{
> +    const char *device = qdict_get_str(qdict, "device");
> +    int64_t size = qdict_get_int(qdict, "size");
> +    BlockDriverState *bs;
> +
> +    bs = bdrv_find(device);
> +    if (!bs) {
> +        qerror_report(QERR_DEVICE_NOT_FOUND, device);
> +        return -1;
> +    }
> +
> +    if (size < 0) {
> +        qerror_report(QERR_UNDEFINED_ERROR);
> +        return -1;
> +    }
> +
> +    if (bdrv_truncate(bs, size)) {
> +        qerror_report(QERR_UNDEFINED_ERROR);
> +        return -1;
> +    }

Can't resize if block migration is in progress. Don't see a problem 
with simply disallowing resize in that case.
Kevin Wolf - Jan. 25, 2011, 12:01 p.m.
Am 24.01.2011 18:44, schrieb Marcelo Tosatti:
> On Mon, Jan 24, 2011 at 01:32:33PM +0100, Christoph Hellwig wrote:
>> 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-24 11:55:36.744254374 +0100
>> +++ qemu/hmp-commands.hx	2011-01-24 11:56:23.619254094 +0100
>> @@ -53,6 +53,25 @@ Quit the emulator.
>>  ETEXI
>>  
>>      {
>> +        .name       = "block_resize",
>> +        .args_type  = "device:B,size:o",
>> +        .params     = "device size",
>> +        .help       = "resize a block image",
>> +        .user_print = monitor_user_noop,
>> +        .mhandler.cmd_new = do_block_resize,
>> +    },
>> +
>> +STEXI
>> +@item block_resize
>> +@findex block_resize
>> +Resize a block image while a guest is running.  Usually requires guest
>> +action to see the updated size.  Resize to a lower size is supported,
>> +but should be used with extreme caution.  Note that this command only
>> +resizes image files, it can not resize block devices like LVM volumes.
>> +ETEXI
>> +
>> +
>> +    {
>>          .name       = "eject",
>>          .args_type  = "force:-f,device:B",
>>          .params     = "[-f] device",
>> Index: qemu/blockdev.c
>> ===================================================================
>> --- qemu.orig/blockdev.c	2011-01-24 11:56:20.903004129 +0100
>> +++ qemu/blockdev.c	2011-01-24 11:56:38.391254165 +0100
>> @@ -705,3 +705,33 @@ int do_drive_del(Monitor *mon, const QDi
>>  
>>      return 0;
>>  }
>> +
>> +/*
>> + * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
>> + * existing QERR_ macro mess is cleaned up.  A good example for better
>> + * error reports can be found in the qemu-img resize code.
>> + */
>> +int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
>> +{
>> +    const char *device = qdict_get_str(qdict, "device");
>> +    int64_t size = qdict_get_int(qdict, "size");
>> +    BlockDriverState *bs;
>> +
>> +    bs = bdrv_find(device);
>> +    if (!bs) {
>> +        qerror_report(QERR_DEVICE_NOT_FOUND, device);
>> +        return -1;
>> +    }
>> +
>> +    if (size < 0) {
>> +        qerror_report(QERR_UNDEFINED_ERROR);
>> +        return -1;
>> +    }
>> +
>> +    if (bdrv_truncate(bs, size)) {
>> +        qerror_report(QERR_UNDEFINED_ERROR);
>> +        return -1;
>> +    }
> 
> Can't resize if block migration is in progress. Don't see a problem 
> with simply disallowing resize in that case.

Then we should add a check to bdrv_truncate.

Kevin
Christoph Hellwig - Jan. 25, 2011, 6:05 p.m.
On Tue, Jan 25, 2011 at 01:01:04PM +0100, Kevin Wolf wrote:
> > Can't resize if block migration is in progress. Don't see a problem 
> > with simply disallowing resize in that case.
> 
> Then we should add a check to bdrv_truncate.

What check exactly would that be anyway?
Marcelo Tosatti - Jan. 26, 2011, 11:56 a.m.
On Tue, Jan 25, 2011 at 07:05:37PM +0100, Christoph Hellwig wrote:
> On Tue, Jan 25, 2011 at 01:01:04PM +0100, Kevin Wolf wrote:
> > > Can't resize if block migration is in progress. Don't see a problem 
> > > with simply disallowing resize in that case.
> > 
> > Then we should add a check to bdrv_truncate.
> 
> What check exactly would that be anyway?

Need to add the information to BlockDriver state. drive_del is also
broken, it can be fixed later. Patch looks good to me.

Patch

Index: qemu/hmp-commands.hx
===================================================================
--- qemu.orig/hmp-commands.hx	2011-01-24 11:55:36.744254374 +0100
+++ qemu/hmp-commands.hx	2011-01-24 11:56:23.619254094 +0100
@@ -53,6 +53,25 @@  Quit the emulator.
 ETEXI
 
     {
+        .name       = "block_resize",
+        .args_type  = "device:B,size:o",
+        .params     = "device size",
+        .help       = "resize a block image",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_block_resize,
+    },
+
+STEXI
+@item block_resize
+@findex block_resize
+Resize a block image while a guest is running.  Usually requires guest
+action to see the updated size.  Resize to a lower size is supported,
+but should be used with extreme caution.  Note that this command only
+resizes image files, it can not resize block devices like LVM volumes.
+ETEXI
+
+
+    {
         .name       = "eject",
         .args_type  = "force:-f,device:B",
         .params     = "[-f] device",
Index: qemu/blockdev.c
===================================================================
--- qemu.orig/blockdev.c	2011-01-24 11:56:20.903004129 +0100
+++ qemu/blockdev.c	2011-01-24 11:56:38.391254165 +0100
@@ -705,3 +705,33 @@  int do_drive_del(Monitor *mon, const QDi
 
     return 0;
 }
+
+/*
+ * XXX: replace the QERR_UNDEFINED_ERROR errors with real values once the
+ * existing QERR_ macro mess is cleaned up.  A good example for better
+ * error reports can be found in the qemu-img resize code.
+ */
+int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    const char *device = qdict_get_str(qdict, "device");
+    int64_t size = qdict_get_int(qdict, "size");
+    BlockDriverState *bs;
+
+    bs = bdrv_find(device);
+    if (!bs) {
+        qerror_report(QERR_DEVICE_NOT_FOUND, device);
+        return -1;
+    }
+
+    if (size < 0) {
+        qerror_report(QERR_UNDEFINED_ERROR);
+        return -1;
+    }
+
+    if (bdrv_truncate(bs, size)) {
+        qerror_report(QERR_UNDEFINED_ERROR);
+        return -1;
+    }
+
+    return 0;
+}
Index: qemu/blockdev.h
===================================================================
--- qemu.orig/blockdev.h	2011-01-24 11:55:36.764254165 +0100
+++ qemu/blockdev.h	2011-01-24 11:56:23.627253465 +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_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data);
 
 #endif
Index: qemu/qmp-commands.hx
===================================================================
--- qemu.orig/qmp-commands.hx	2011-01-24 11:55:36.771253955 +0100
+++ qemu/qmp-commands.hx	2011-01-24 11:56:23.632253884 +0100
@@ -601,6 +601,34 @@  Example:
 -> { "execute": "netdev_del", "arguments": { "id": "netdev1" } }
 <- { "return": {} }
 
+
+EQMP
+
+    {
+        .name       = "block_resize",
+        .args_type  = "device:B,size:o",
+        .params     = "device size",
+        .help       = "resize a block image",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_block_resize,
+    },
+
+SQMP
+block_resize
+------------
+
+Resize a block image while a guest is running.
+
+Arguments:
+
+- "device": the device's ID, must be unique (json-string)
+- "size": new size
+
+Example:
+
+-> { "execute": "block_resize", "arguments": { "device": "scratch", "size": 1073741824 } }
+<- { "return": {} }
+
 EQMP
 
     {