blockdev: BlockBackend and media
v8 cover blockdev: BlockBackend and media
Now that the main rework part of this series is merged, these remaining patches here implement atomic tray/medium operations and add the read-only-mode parameter to change and blockdev-change-medium (which was the original purpose of this series!).
Once again, I'd like to thank all the reviewers for sticking to this long series over many iterations. Thanks a lot! :-)
v8:
- Dropped first 26 patches (merged to master now)
- Patch 1 (orig. 27): Fixed use-after-free [Kevin]
- Patch 2 (added): It's ugly, but it will only be a temporary hack, and we need it since we have to update bdrv_states in blockdev.c whenever a BDS is inserted or removed from a BB
- Patch 3 (added): Proposed by Kevin
- Patch 4 (orig. 28): [Kevin]
- Drop BLOCK_OP_TYPE_EJECT check (moved to blockdev-remove-medium)
- Call blk_dev_change_media_cb() if @force is true, even if the tray is locked
- In the documentation, clarify that you'll have to wait for a DEVICE_TRAY_MOVED event after issuing this command, and that under certain circumstances, this event may never occur at all
- Patch 6 (orig. 30):
- Add BLOCK_OP_TYPE_EJCT check [Kevin]
- Remove BDS from bdrv_states list [Berto]
- Note that the tray does not have to be open for device-less BBs
- Patch 7 (orig. 31):
- Add BDS to bdrv_states list [Berto]
- Note that the tray does not have to be open for device-less BBs
- Patch 9 (orig. 33): Use functions provided by block/block-backend.c (patch 3) to inherit the root state from the BB [Kevin]
- Patch 13 (orig. 37): Rebase conflicts due to the changes to patch 9
- Patch 14 (orig. 38): Add missing \n [Kevin]
- Patch 15 (orig. 39):
- Add a test which runs a block job on a device after the medium has been removed and a new one has been inserted [Berto]
- Do not run the test on another platform than PC
git-backport-diff against v7:
Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively
001/15:[0002] [FC] 'block: Add blk_remove_bs()' 002/15:[down] 'block: Make bdrv_states public'
003/15:[down] 'block: Add functions for inheriting a BBRS' 004/15:[0038] [FC] 'blockdev: Add blockdev-open-tray' 005/15:[----] [-C] 'blockdev: Add blockdev-close-tray' 006/15:[0030] [FC] 'blockdev: Add blockdev-remove-medium' 007/15:[0010] [FC] 'blockdev: Add blockdev-insert-medium' 008/15:[----] [--] 'blockdev: Implement eject with basic operations' 009/15:[0010] [FC] 'blockdev: Implement change with basic operations' 010/15:[----] [--] 'block: Inquire tray state before tray-moved events' 011/15:[----] [-C] 'qmp: Introduce blockdev-change-medium' 012/15:[----] [--] 'hmp: Use blockdev-change-medium for change command' 013/15:[0007] [FC] 'blockdev: read-only-mode for blockdev-change-medium' 014/15:[0002] [FC] 'hmp: Add read-only-mode option to change command' 015/15:[0086] [FC] 'iotests: Add test for change-related QMP commands'
Max Reitz (15):
block: Add blk_remove_bs()
block: Make bdrv_states public
block: Add functions for inheriting a BBRS
blockdev: Add blockdev-open-tray
blockdev: Add blockdev-close-tray
blockdev: Add blockdev-remove-medium
blockdev: Add blockdev-insert-medium
blockdev: Implement eject with basic operations
blockdev: Implement change with basic operations
block: Inquire tray state before tray-moved events
qmp: Introduce blockdev-change-medium
hmp: Use blockdev-change-medium for change command
blockdev: read-only-mode for blockdev-change-medium
hmp: Add read-only-mode option to change command
iotests: Add test for change-related QMP commands
block.c | 3 +- block/block-backend.c | 56 +++- blockdev.c | 286 ++++++++++++---- hmp-commands.hx | 20 +- hmp.c | 47 ++- include/block/block_int.h | 2 + include/sysemu/block-backend.h | 3 + include/sysemu/blockdev.h | 2 - qapi-schema.json | 6 +- qapi/block-core.json | 126 ++++++++ qmp-commands.hx | 218 +++++++++++++ qmp.c | 3 +- tests/qemu-iotests/118 | 720 +++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/118.out | 5 + tests/qemu-iotests/group | 1 + 15 files changed, 1396 insertions(+), 102 deletions(-) create mode 100755 tests/qemu-iotests/118 create mode 100644 tests/qemu-iotests/118.out
Signature · 1 line
2.6.2
PATCH v8 1/15 block: Add blk_remove_bs()
This function removes the BlockDriverState associated with the given BlockBackend from that BB and sets the BDS pointer in the BB to NULL.
--- block/block-backend.c | 12 ++++++++++++ include/sysemu/block-backend.h | 1 + 2 files changed, 13 insertions(+)
block/block-backend.c +12 -0
@@ -334,6 +334,18 @@ void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk) }/* +* Disassociates the currently associated BlockDriverState from @blk. +*/ +void blk_remove_bs (BlockBackend *blk) +{ +blk_update_root_state (blk); + + blk->bs->blk =NULL ; +bdrv_unref (blk->bs); + blk->bs =NULL ; +} + +/* * Associates a new BlockDriverState with @blk. */ void blk_insert_bs (BlockBackend *blk,BlockDriverState *bs)
include/sysemu/block-backend.h +1 -0
@@ -72,6 +72,7 @@ BlockBackend *blk_by_name(const char *name);BlockBackend *blk_next (BlockBackend *blk);BlockDriverState *blk_bs (BlockBackend *blk); +void blk_remove_bs (BlockBackend *blk);void blk_insert_bs (BlockBackend *blk,BlockDriverState *bs);void blk_hide_on_behalf_of_hmp_drive_del (BlockBackend *blk);
Signature · 1 line
2.6.2
Re: [Qemu-devel] [PATCH v8 01/15] block: Add blk_remove_bs()
block/block-backend.c void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk) · line 346
Quoted 8 lines · Max Reitz
block/block-backend.c +8
-0+void blk_remove_bs (BlockBackend *blk) +{ +blk_update_root_state (blk); + + blk->bs->blk =NULL ; +bdrv_unref (blk->bs); + blk->bs =NULL ; +}
I cannot think of any example out of the blue but I wonder if removing the reference to the BDS (and possibly destroying it) while blk->bs is still pointing to it could be a source of problems.
Berto
PATCH v8 2/15 block: Make bdrv_states public
In reply to Max ReitzWhen inserting a BDS tree into a BB, we will need to add the root BDS to this list. Since we will want to do that in the blockdev-insert-medium implementation in blockdev.c, we will need access to it there.
This patch is not exactly elegant, but bdrv_states will be removed in the future anyway because we no longer need it since we have BBs.
--- block.c | 3 +-- include/block/block_int.h | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-)
block.c +1 -2
@@ -73,8 +73,7 @@ struct BdrvDirtyBitmap {#define NOT_DONE 0x7fffffff/* used while emulated sync operation in progress */ -static QTAILQ_HEAD(, BlockDriverState) bdrv_states = -QTAILQ_HEAD_INITIALIZER (bdrv_states); +struct BdrvStates bdrv_states =QTAILQ_HEAD_INITIALIZER (bdrv_states); static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =QTAILQ_HEAD_INITIALIZER (graph_bdrv_states);
include/block/block_int.h +2 -0
@@ -473,6 +473,8 @@ extern BlockDriver bdrv_file;extern BlockDriver bdrv_raw;extern BlockDriver bdrv_qcow2; +extern QTAILQ_HEAD(BdrvStates, BlockDriverState) bdrv_states; +/** * bdrv_setup_io_funcs: *
Signature · 1 line
2.6.2
Re: [Qemu-devel] [PATCH v8 02/15] block: Make bdrv_states public
Quoted 8 lines · Max Reitz
When inserting a BDS tree into a BB, we will need to add the root BDS to this list. Since we will want to do that in the blockdev-insert-medium implementation in
blockdev.c, we will need access to it there.This patch is not exactly elegant, but
bdrv_stateswill be removed in the future anyway because we no longer need it since we have BBs.Signed-off-by Max Reitz <[email protected]>
Berto
PATCH v8 3/15 block: Add functions for inheriting a BBRS
In reply to Max ReitzIn order to open a BDS which inherits a BB's root state, blk_get_open_flags_from_root_state() is used to inquire the flags to be passed to bdrv_open(), and blk_apply_root_state() is used to apply the remaining state after the BDS has been opened.
--- block/block-backend.c | 27 +++++++++++++++++++++++++++ include/sysemu/block-backend.h | 2 ++ 2 files changed, 29 insertions(+)
block/block-backend.c +27 -0
@@ -1239,6 +1239,33 @@ void blk_update_root_state(BlockBackend *blk) } } +/* +* Applies the information in the root state to the given BlockDriverState. This +* does not include the flags which have to be specified for bdrv_open(), use +* blk_get_open_flags_from_root_state() to inquire them. +*/ +void blk_apply_root_state (BlockBackend *blk,BlockDriverState *bs) +{ + bs->detect_zeroes = blk->root_state.detect_zeroes; +if (blk->root_state.throttle_group) { +bdrv_io_limits_enable (bs, blk->root_state.throttle_group); + } +} + +/* +* Returns the flags to be used for bdrv_open() of a BlockDriverState which is +* supposed to inherit the root state. +*/ +int blk_get_open_flags_from_root_state (BlockBackend *blk) +{ +int bs_flags; + + bs_flags = blk->root_state.read_only ?0 :BDRV_O_RDWR ; + bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR ; + +return bs_flags; +} +BlockBackendRootState *blk_get_root_state (BlockBackend *blk) {return &blk->root_state;
include/sysemu/block-backend.h +2 -0
@@ -167,6 +167,8 @@ void blk_io_unplug(BlockBackend *blk);BlockAcctStats *blk_get_stats (BlockBackend *blk);BlockBackendRootState *blk_get_root_state (BlockBackend *blk);void blk_update_root_state (BlockBackend *blk); +void blk_apply_root_state (BlockBackend *blk,BlockDriverState *bs); +int blk_get_open_flags_from_root_state (BlockBackend *blk);void *blk_aio_get (const AIOCBInfo *aiocb_info,BlockBackend *blk,BlockCompletionFunc *cb,void *opaque);
Signature · 1 line
2.6.2
PATCH v8 4/15 blockdev: Add blockdev-open-tray
In reply to Max Reitz--- blockdev.c | 36 ++++++++++++++++++++++++++++++++++++ qapi/block-core.json | 32 ++++++++++++++++++++++++++++++++ qmp-commands.hx | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+)
blockdev.c +36 -0
@@ -2077,6 +2077,42 @@ out:aio_context_release (aio_context); } +void qmp_blockdev_open_tray (const char *device,bool has_force,bool force, +Error **errp) +{ +BlockBackend *blk; +bool locked; + +if (!has_force) { + force = false; + } + + blk =blk_by_name (device); +if (!blk) { +error_set (errp,ERROR_CLASS_DEVICE_NOT_FOUND , +"Device '%s' not found" , device); +return ; + } + +if (!blk_dev_has_removable_media (blk)) { +error_setg (errp,"Device '%s' is not removable" , device); +return ; + } + +if (blk_dev_is_tray_open (blk)) { +return ; + } + + locked =blk_dev_is_medium_locked (blk); +if (locked) { +blk_dev_eject_request (blk, force); + } + +if (!locked || force) { +blk_dev_change_media_cb (blk, false); + } +} +/* throttling disk I/O limits */ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, int64_t bps_wr,
qapi/block-core.json +32 -0
@@ -1876,6 +1876,38 @@ ## { 'command': 'blockdev-add', 'data': { 'options': 'BlockdevOptions' } } +## +# @blockdev-open-tray: +# +# Opens a block device's tray. If there is a block driver state tree inserted as +# a medium, it will become inaccessible to the guest (but it will remain +# associated to the block device, so closing the tray will make it accessible +# again). +# +# If the tray was already open before, this will be a no-op. +# +# Once the tray opens, a DEVICE_TRAY_MOVED event is emitted. There are cases in +# which no such event will be generated, these include: +# - if the guest has locked the tray, @force is false and the guest does not +# respond to the eject request +# - if the BlockBackend denoted by @device does not have a guest device attached +# to it +# - if the guest device does not have an actual tray and is empty, for instance +# for floppy disk drives +# +# @device: block device name +# +# @force: #optional if false (the default), an eject request will be sent to +# the guest if it has locked the tray (and the tray will not be opened +# immediately); if true, the tray will be opened regardless of whether +# it is locked +# +# Since: 2.5 +## +{ 'command': 'blockdev-open-tray', + 'data': { 'device': 'str', + '*force': 'bool' } } + ## # @BlockErrorAction
qmp-commands.hx +48 -0
54 lines not on this page · raw message
Signature · 1 line
2.6.2
PATCH v8 5/15 blockdev: Add blockdev-close-tray
In reply to Max Reitz--- blockdev.c | 23 +++++++++++++++++++++++ qapi/block-core.json | 16 ++++++++++++++++ qmp-commands.hx | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+)
blockdev.c +23 -0
@@ -2113,6 +2113,29 @@ void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, } } +void qmp_blockdev_close_tray (const char *device,Error **errp) +{ +BlockBackend *blk; + + blk =blk_by_name (device); +if (!blk) { +error_set (errp,ERROR_CLASS_DEVICE_NOT_FOUND , +"Device '%s' not found" , device); +return ; + } + +if (!blk_dev_has_removable_media (blk)) { +error_setg (errp,"Device '%s' is not removable" , device); +return ; + } + +if (!blk_dev_is_tray_open (blk)) { +return ; + } + +blk_dev_change_media_cb (blk, true); +} +/* throttling disk I/O limits */ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, int64_t bps_wr,
qapi/block-core.json +16 -0
@@ -1908,6 +1908,22 @@ 'data': { 'device': 'str', '*force': 'bool' } } +## +# @blockdev-close-tray: +# +# Closes a block device's tray. If there is a block driver state tree associated +# with the block device (which is currently ejected), that tree will be loaded +# as the medium. +# +# If the tray was already closed before, this will be a no-op. +# +# @device: block device name +# +# Since: 2.5 +## +{ 'command': 'blockdev-close-tray', + 'data': { 'device': 'str' } } + ## # @BlockErrorAction
qmp-commands.hx +35 -0
@@ -3984,6 +3984,41 @@ Example: EQMP { + .name = "blockdev-close-tray", + .args_type = "device:s", + .mhandler.cmd_new = qmp_marshal_blockdev_close_tray, + }, + +SQMP +blockdev-close-tray +------------------- + +Closes a block device's tray. If there is a block driver state tree associated +with the block device (which is currently ejected), that tree will be loaded as +the medium. + +If the tray was already closed before, this will be a no-op. + +Arguments: + +- "device": block device name (json-string) + +Example: + +-> { "execute": "blockdev-close-tray", + "arguments": { "device": "ide1-cd0" } } + +<- { "timestamp": { "seconds": 1418751345, + "microseconds": 272147 }, + "event": "DEVICE_TRAY_MOVED", + "data": { "device": "ide1-cd0", + "tray-open": false } } + +<- { "return": {} } + +EQMP + + { .name = "query-named-block-nodes", .args_type = "", .mhandler.cmd_new = qmp_marshal_query_named_block_nodes,
Signature · 1 line
2.6.2
PATCH v8 6/15 blockdev: Add blockdev-remove-medium
In reply to Max Reitz--- blockdev.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ qapi/block-core.json | 16 ++++++++++++++++ qmp-commands.hx | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+)
blockdev.c +51 -0
@@ -2136,6 +2136,57 @@ void qmp_blockdev_close_tray(const char *device, Error **errp)blk_dev_change_media_cb (blk, true); } +void qmp_blockdev_remove_medium (const char *device,Error **errp) +{ +BlockBackend *blk; +BlockDriverState *bs; +AioContext *aio_context; +bool has_device; + + blk =blk_by_name (device); +if (!blk) { +error_set (errp,ERROR_CLASS_DEVICE_NOT_FOUND , +"Device '%s' not found" , device); +return ; + } + +/* For BBs without a device, we can exchange the BDS tree at will */ + has_device =blk_get_attached_dev (blk); + +if (has_device && !blk_dev_has_removable_media (blk)) { +error_setg (errp,"Device '%s' is not removable" , device); +return ; + } + +if (has_device && !blk_dev_is_tray_open (blk)) { +error_setg (errp,"Tray of device '%s' is not open" , device); +return ; + } + + bs =blk_bs (blk); +if (!bs) { +return ; + } + + aio_context =bdrv_get_aio_context (bs); +aio_context_acquire (aio_context); + +if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_EJECT , errp)) { + goto out; + } + +/* This follows the convention established by bdrv_make_anon() */ +if (bs->device_list.tqe_prev) { +QTAILQ_REMOVE (&bdrv_states, bs, device_list); + bs->device_list.tqe_prev =NULL ; + } + +blk_remove_bs (blk); + +out: +aio_context_release (aio_context); +} +/* throttling disk I/O limits */ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, int64_t bps_wr,
qapi/block-core.json +16 -0
@@ -1924,6 +1924,22 @@ { 'command': 'blockdev-close-tray', 'data': { 'device': 'str' } } +## +# @blockdev-remove-medium: +# +# Removes a medium (a block driver state tree) from a block device. That block +# device's tray must currently be open (unless there is no attached guest +# device). +# +# If the tray is open and there is no medium inserted, this will be a no-op. +# +# @device: block device name +# +# Since: 2.5 +## +{ 'command': 'blockdev-remove-medium', + 'data': { 'device': 'str' } } + ## # @BlockErrorAction
qmp-commands.hx +45 -0
51 lines not on this page · raw message
Signature · 1 line
2.6.2
Re: [Qemu-devel] [PATCH v8 06/15] blockdev: Add blockdev-remove-medium
Am 26.10.2015 um 21:39 hat Max Reitz geschrieben:
blockdev.c void qmp_blockdev_close_tray(const char *device, Error **errp) · line 2184
Quoted 13 lines
Signed-off-by Max Reitz <[email protected]>blockdev.c +11
-0+if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_EJECT , errp)) { + goto out; + } + +/* This follows the convention established by bdrv_make_anon() */ +if (bs->device_list.tqe_prev) { +QTAILQ_REMOVE (&bdrv_states, bs, device_list); + bs->device_list.tqe_prev =NULL ; + } + +blk_remove_bs (blk);
Wouldn't it be nicer to move the bdrv_states update into blk_remove_bs() and blk_insert_bs()? Can be done on top of this series, though, if you don't need to respin for another reason.
Kevin
PATCH v8 7/15 blockdev: Add blockdev-insert-medium
In reply to Max ReitzAnd a helper function for that, which directly takes a pointer to the BDS to be inserted instead of its node-name (which will be used for implementing 'change' using blockdev-insert-medium).
--- blockdev.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ qapi/block-core.json | 17 ++++++++++++++++ qmp-commands.hx | 37 ++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+)
blockdev.c +56 -0
@@ -2187,6 +2187,62 @@ out:aio_context_release (aio_context); } +static void qmp_blockdev_insert_anon_medium (const char *device, +BlockDriverState *bs,Error **errp) +{ +BlockBackend *blk; +bool has_device; + + blk =blk_by_name (device); +if (!blk) { +error_set (errp,ERROR_CLASS_DEVICE_NOT_FOUND , +"Device '%s' not found" , device); +return ; + } + +/* For BBs without a device, we can exchange the BDS tree at will */ + has_device =blk_get_attached_dev (blk); + +if (has_device && !blk_dev_has_removable_media (blk)) { +error_setg (errp,"Device '%s' is not removable" , device); +return ; + } + +if (has_device && !blk_dev_is_tray_open (blk)) { +error_setg (errp,"Tray of device '%s' is not open" , device); +return ; + } + +if (blk_bs (blk)) { +error_setg (errp,"There already is a medium in device '%s'" , device); +return ; + } + +blk_insert_bs (blk, bs); + +QTAILQ_INSERT_TAIL (&bdrv_states, bs, device_list); +} + +void qmp_blockdev_insert_medium (const char *device,const char *node_name, +Error **errp) +{ +BlockDriverState *bs; + + bs =bdrv_find_node (node_name); +if (!bs) { +error_setg (errp,"Node '%s' not found" , node_name); +return ; + } + +if (bs->blk) { +error_setg (errp,"Node '%s' is already in use by '%s'" , node_name, +blk_name (bs->blk)); +return ; + } + +qmp_blockdev_insert_anon_medium (device, bs, errp); +} +/* throttling disk I/O limits */ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, int64_t bps_wr,
qapi/block-core.json +17 -0
@@ -1940,6 +1940,23 @@ { 'command': 'blockdev-remove-medium', 'data': { 'device': 'str' } } +## +# @blockdev-insert-medium: +# +# Inserts a medium (a block driver state tree) into a block device. That block +# device's tray must currently be open (unless there is no attached guest +# device) and there must be no medium inserted already. +# +# @device: block device name +# +# @node-name: name of a node in the block driver state graph +# +# Since: 2.5 +## +{ 'command': 'blockdev-insert-medium', + 'data': { 'device': 'str', + 'node-name': 'str'} } + ## # @BlockErrorAction
qmp-commands.hx +37 -0
@@ -4064,6 +4064,43 @@ Example: EQMP { + .name = "blockdev-insert-medium", + .args_type = "device:s,node-name:s", + .mhandler.cmd_new = qmp_marshal_blockdev_insert_medium, + }, + +SQMP +blockdev-insert-medium +---------------------- + +Inserts a medium (a block driver state tree) into a block device. That block +device's tray must currently be open (unless there is no attached guest device) +and there must be no medium inserted already. + +Arguments: + +- "device": block device name (json-string) +- "node-name": root node of the BDS tree to insert into the block device + +Example: + +-> { "execute": "blockdev-add", + "arguments": { "options": { "node-name": "node0", + "driver": "raw", + "file": { "driver": "file", + "filename": "fedora.iso" } } } } + +<- { "return": {} } + +-> { "execute": "blockdev-insert-medium", + "arguments": { "device": "ide1-cd0", + "node-name": "node0" } } + +<- { "return": {} } + +EQMP + + { .name = "query-named-block-nodes", .args_type = "", .mhandler.cmd_new = qmp_marshal_query_named_block_nodes,
Signature · 1 line
2.6.2
PATCH v8 8/15 blockdev: Implement eject with basic operations
In reply to Max ReitzImplement 'eject' by calling blockdev-open-tray and blockdev-remove-medium.
--- blockdev.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
blockdev.c +5 -6
@@ -1969,16 +1969,15 @@ out:void qmp_eject (const char *device,bool has_force,bool force,Error **errp) { -BlockBackend *blk; +Error *local_err =NULL ; - blk =blk_by_name (device); -if (!blk) { -error_set (errp,ERROR_CLASS_DEVICE_NOT_FOUND , -"Device '%s' not found" , device); +qmp_blockdev_open_tray (device, has_force, force, &local_err); +if (local_err) { +error_propagate (errp, local_err);return ; } -eject_device (blk, force, errp); +qmp_blockdev_remove_medium (device, errp); } void qmp_block_passwd(bool has_device, const char *device,
Signature · 1 line
2.6.2
PATCH v8 9/15 blockdev: Implement change with basic operations
In reply to Max ReitzImplement 'change' on block devices by calling blockdev-open-tray, blockdev-remove-medium, blockdev-insert-medium (a variation of that which does not need a node-name) and blockdev-close-tray.
--- blockdev.c | 178 +++++++++++++++++++++++-------------------------------------- 1 file changed, 68 insertions(+), 110 deletions(-)
blockdev.c +68 -110
@@ -1929,44 +1929,6 @@ exit: } } - -static void eject_device (BlockBackend *blk,int force,Error **errp) -{ -BlockDriverState *bs =blk_bs (blk); -AioContext *aio_context; - -if (!bs) { -/* No medium inserted, so there is nothing to do */ -return ; - } - - aio_context =bdrv_get_aio_context (bs); -aio_context_acquire (aio_context); - -if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_EJECT , errp)) { - goto out; - } -if (!blk_dev_has_removable_media (blk)) { -error_setg (errp,"Device '%s' is not removable" , -bdrv_get_device_name (bs)); - goto out; - } - -if (blk_dev_is_medium_locked (blk) && !blk_dev_is_tray_open (blk)) { -blk_dev_eject_request (blk, force); -if (!force) { -error_setg (errp,"Device '%s' is locked" , -bdrv_get_device_name (bs)); - goto out; - } - } - -bdrv_close (bs); - -out: -aio_context_release (aio_context); -} -void qmp_eject (const char *device,bool has_force,bool force,Error **errp) {Error *local_err =NULL ;
@@ -2004,78 +1966,6 @@ void qmp_block_passwd(bool has_device, const char *device,aio_context_release (aio_context); } -/* Assumes AioContext is held */ -static void qmp_bdrv_open_encrypted (BlockDriverState **pbs, -const char *filename, -int bdrv_flags,const char *format, -const char *password,Error **errp) -{ -Error *local_err =NULL ; -QDict *options =NULL ; -int ret; - -if (format) { - options =qdict_new (); -qdict_put (options,"driver" ,qstring_from_str (format)); - } - - ret =bdrv_open (pbs, filename,NULL , options, bdrv_flags, &local_err); -if (ret <0 ) { -error_propagate (errp, local_err); -return ; - } - -bdrv_add_key (*pbs, password, errp); -} - -void qmp_change_blockdev (const char *device,const char *filename, -const char *format,Error **errp) -{ -BlockBackend *blk; -BlockDriverState *bs; -AioContext *aio_context; -int bdrv_flags; -bool new_bs; -Error *err =NULL ; - - blk =blk_by_name (device); -if (!blk) { -error_set (errp,ERROR_CLASS_DEVICE_NOT_FOUND , -"Device '%s' not found" , device); -return ; - } - bs =blk_bs (blk); - new_bs = !bs; - - aio_context =blk_get_aio_context (blk); -aio_context_acquire (aio_context); - -eject_device (blk,0 , &err); -if (err) { -error_propagate (errp, err); - goto out; - } - - bdrv_flags =blk_is_read_only (blk) ?0 :BDRV_O_RDWR ; - bdrv_flags |=blk_get_root_state (blk)->open_flags & ~BDRV_O_RDWR ; - -qmp_bdrv_open_encrypted (&bs, filename, bdrv_flags, format,NULL , &err); -if (err) { -error_propagate (errp, err); - goto out; - } - -if (new_bs) { -blk_insert_bs (blk, bs); -/* Has been sent automatically by bdrv_open() if blk_bs(blk) was not -* NULL */ -blk_dev_change_media_cb (blk, true); - } - -out: -aio_context_release (aio_context); -} -void qmp_blockdev_open_tray (const char *device,bool has_force,bool force,Error **errp) {
@@ -2242,6 +2132,74 @@ void qmp_blockdev_insert_medium(const char *device, const char *node_name,qmp_blockdev_insert_anon_medium (device, bs, errp); } +void qmp_change_blockdev (const char *device,const char *filename, +const char *format,Error **errp) +{ +BlockBackend *blk; +BlockDriverState *medium_bs =NULL ; +int bdrv_flags, ret; +QDict *options =NULL ; +Error *err =NULL ; + + blk =blk_by_name (device); +if (!blk) { +error_set (errp,ERROR_CLASS_DEVICE_NOT_FOUND , +"Device '%s' not found" , device); + goto fail; + } + +if (blk_bs (blk)) { +blk_update_root_state (blk); + } + + bdrv_flags =blk_get_open_flags_from_root_state (blk); + +if (format) { + options =qdict_new (); +qdict_put (options,"driver" ,qstring_from_str (format)); + } + +assert (!medium_bs); + ret =bdrv_open (&medium_bs, filename,NULL , options, bdrv_flags, errp); +if (ret <0 ) { + goto fail; + } + +blk_apply_root_state (blk, medium_bs); + +bdrv_add_key (medium_bs,NULL , &err); +if (err) { +error_propagate (errp, err); + goto fail; + } + +qmp_blockdev_open_tray (device, false, false, &err); +if (err) { +error_propagate (errp, err); + goto fail; + } + +qmp_blockdev_remove_medium (device, &err); +if (err) { +error_propagate (errp, err); + goto fail; + } + +qmp_blockdev_insert_anon_medium (device, medium_bs, &err); +if (err) { +error_propagate (errp, err); + goto fail; + } + +qmp_blockdev_close_tray (device, errp); + +fail: +/* If the medium has been inserted, the device has its own reference, so +* ours must be relinquished; and if it has not been inserted successfully, +* the reference must be relinquished anyway */ +bdrv_unref (medium_bs); +} +/* throttling disk I/O limits */ void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, int64_t bps_wr,
Signature · 1 line
2.6.2
PATCH v8 10/15 block: Inquire tray state before tray-moved events
In reply to Max Reitzblk_dev_change_media_cb() is called for all potential tray movements; however, it is possible to request closing the tray but nothing actually happening (on a floppy disk drive without a medium).
Thus, the actual tray status should be inquired before sending a tray-moved event (and an event should be sent whenever the status changed).
Checking @load is now superfluous; it was necessary because it was possible to change a medium without having explicitly opened the tray and closed it again (or it might have been possible, at least). This is no longer possible, though.
--- block/block-backend.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
block/block-backend.c +7 -10
@@ -429,18 +429,15 @@ void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,void blk_dev_change_media_cb (BlockBackend *blk,bool load) {if (blk->dev_ops && blk->dev_ops->change_media_cb) { -bool tray_was_closed = !blk_dev_is_tray_open (blk); +bool tray_was_open, tray_is_open; + tray_was_open =blk_dev_is_tray_open (blk); blk->dev_ops->change_media_cb (blk->dev_opaque, load); -if (tray_was_closed) { -/* tray open */ -qapi_event_send_device_tray_moved (blk_name (blk), - true, &error_abort); - } -if (load) { -/* tray close */ -qapi_event_send_device_tray_moved (blk_name (blk), - false, &error_abort); + tray_is_open =blk_dev_is_tray_open (blk); + +if (tray_was_open != tray_is_open) { +qapi_event_send_device_tray_moved (blk_name (blk), tray_is_open, + &error_abort); } } }
Signature · 1 line
2.6.2
PATCH v8 11/15 qmp: Introduce blockdev-change-medium
In reply to Max ReitzIntroduce a new QMP command 'blockdev-change-medium' which is intended to replace the 'change' command for block devices. The existing function qmp_change_blockdev() is accordingly renamed to qmp_blockdev_change_medium().
--- blockdev.c | 7 ++++--- include/sysemu/blockdev.h | 2 -- qapi-schema.json | 6 ++++-- qapi/block-core.json | 23 +++++++++++++++++++++++ qmp-commands.hx | 31 +++++++++++++++++++++++++++++++ qmp.c | 2 +- 6 files changed, 63 insertions(+), 8 deletions(-)
blockdev.c +4 -3
@@ -2132,8 +2132,9 @@ void qmp_blockdev_insert_medium(const char *device, const char *node_name,qmp_blockdev_insert_anon_medium (device, bs, errp); } -void qmp_change_blockdev (const char *device,const char *filename, -const char *format,Error **errp) +void qmp_blockdev_change_medium (const char *device,const char *filename, +bool has_format,const char *format, +Error **errp) {BlockBackend *blk;BlockDriverState *medium_bs =NULL ;
@@ -2154,7 +2155,7 @@ void qmp_change_blockdev(const char *device, const char *filename, bdrv_flags =blk_get_open_flags_from_root_state (blk); -if (format) { +if (has_format) { options =qdict_new ();qdict_put (options,"driver" ,qstring_from_str (format)); }
include/sysemu/blockdev.h +0 -2
@@ -63,8 +63,6 @@ DriveInfo *drive_new(QemuOpts *arg, BlockInterfaceType block_default_type);/* device-hotplug */ -void qmp_change_blockdev (const char *device,const char *filename, -const char *format,Error **errp);void hmp_commit (Monitor *mon,const QDict *qdict);void hmp_drive_del (Monitor *mon,const QDict *qdict);#endif
qapi-schema.json +4 -2
@@ -1842,8 +1842,10 @@ # device's password. The behavior of reads and writes to the block # device between when these calls are executed is undefined. # -# Notes: It is strongly recommended that this interface is not used especially -# for changing block devices. +# Notes: This interface is deprecated, and it is strongly recommended that you +# avoid using it. For changing block devices, use +# blockdev-change-medium; for changing VNC parameters, use +# change-vnc-password. # # Since: 0.14.0 ##
qapi/block-core.json +23 -0
@@ -1959,6 +1959,29 @@ ## +# @blockdev-change-medium: +# +# Changes the medium inserted into a block device by ejecting the current medium +# and loading a new image file which is inserted as the new medium (this command +# combines blockdev-open-tray, blockdev-remove-medium, blockdev-insert-medium +# and blockdev-close-tray). +# +# @device: block device name +# +# @filename: filename of the new image to be loaded +# +# @format: #optional, format to open the new image with (defaults to +# the probed format) +# +# Since: 2.5 +## +{ 'command': 'blockdev-change-medium', + 'data': { 'device': 'str', + 'filename': 'str', + '*format': 'str' } } + + +## # @BlockErrorAction # # An enumeration of action that has been taken when a DISK I/O occurs
qmp-commands.hx +31 -0
@@ -4163,6 +4163,37 @@ Example: EQMP { + .name = "blockdev-change-medium", + .args_type = "device:B,filename:F,format:s?", + .mhandler.cmd_new = qmp_marshal_blockdev_change_medium, + }, + +SQMP +blockdev-change-medium +---------------------- + +Changes the medium inserted into a block device by ejecting the current medium +and loading a new image file which is inserted as the new medium. + +Arguments: + +- "device": device name (json-string) +- "filename": filename of the new image (json-string) +- "format": format of the new image (json-string, optional) + +Examples: + +1. Change a removable medium + +-> { "execute": "blockdev-change-medium", + "arguments": { "device": "ide1-cd0", + "filename": "/srv/images/Fedora-12-x86_64-DVD.iso", + "format": "raw" } } +<- { "return": {} } + +EQMP + + { .name = "query-memdev", .args_type = "", .mhandler.cmd_new = qmp_marshal_query_memdev,
qmp.c +1 -1
@@ -414,7 +414,7 @@ void qmp_change(const char *device, const char *target,if (strcmp (device,"vnc" ) ==0 ) {qmp_change_vnc (target, has_arg, arg, errp); }else { -qmp_change_blockdev (device, target, arg, errp); +qmp_blockdev_change_medium (device, target, has_arg, arg, errp); } }
Signature · 1 line
2.6.2
PATCH v8 12/15 hmp: Use blockdev-change-medium for change command
In reply to Max ReitzUse separate code paths for the two overloaded functions of the 'change' HMP command, and invoke the 'blockdev-change-medium' QMP command if used on a block device (by calling qmp_blockdev_change_medium()).
--- hmp.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-)
hmp.c +15 -12
@@ -1338,22 +1338,25 @@ void hmp_change(Monitor *mon, const QDict *qdict)const char *arg =qdict_get_try_str (qdict,"arg" );Error *err =NULL ; -if (strcmp (device,"vnc" ) ==0 && - (strcmp (target,"passwd" ) ==0 || -strcmp (target,"password" ) ==0 )) { -if (!arg) { -monitor_read_password (mon, hmp_change_read_arg,NULL ); +if (strcmp (device,"vnc" ) ==0 ) { +if (strcmp (target,"passwd" ) ==0 || +strcmp (target,"password" ) ==0 ) { +if (!arg) { +monitor_read_password (mon, hmp_change_read_arg,NULL ); +return ; + } + } +qmp_change ("vnc" , target, !!arg, arg, &err); + }else { +qmp_blockdev_change_medium (device, target, !!arg, arg, &err); +if (err && +error_get_class (err) ==ERROR_CLASS_DEVICE_ENCRYPTED ) { +error_free (err); +monitor_read_block_device_key (mon, device,NULL ,NULL );return ; } } -qmp_change (device, target, !!arg, arg, &err); -if (err && -error_get_class (err) ==ERROR_CLASS_DEVICE_ENCRYPTED ) { -error_free (err); -monitor_read_block_device_key (mon, device,NULL ,NULL ); -return ; - }hmp_handle_error (mon, &err); }
Signature · 1 line
2.6.2
PATCH v8 13/15 blockdev: read-only-mode for blockdev-change-medium
In reply to Max ReitzAdd an option to qmp_blockdev_change_medium() which allows changing the read-only status of the block device whose medium is changed.
Some drives do not have a inherently fixed read-only status; for instance, floppy disks can be set read-only or writable independently of the drive. Some users may find it useful to be able to therefore change the read-only status of a block device when changing the medium.
--- blockdev.c | 22 ++++++++++++++++++++++ hmp.c | 2 +- qapi/block-core.json | 24 +++++++++++++++++++++++- qmp-commands.hx | 24 +++++++++++++++++++++++- qmp.c | 3 ++- 5 files changed, 71 insertions(+), 4 deletions(-)
blockdev.c +22 -0
@@ -2134,6 +2134,8 @@ void qmp_blockdev_insert_medium(const char *device, const char *node_name,void qmp_blockdev_change_medium (const char *device,const char *filename,bool has_format,const char *format, +bool has_read_only, +BlockdevChangeReadOnlyMode read_only,Error **errp) {BlockBackend *blk;
@@ -2155,6 +2157,26 @@ void qmp_blockdev_change_medium(const char *device, const char *filename, bdrv_flags =blk_get_open_flags_from_root_state (blk); +if (!has_read_only) { + read_only =BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN ; + } + +switch (read_only) { +case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN : +break ; + +case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY : + bdrv_flags &= ~BDRV_O_RDWR ; +break ; + +case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE : + bdrv_flags |=BDRV_O_RDWR ; +break ; + +default : +abort (); + } +if (has_format) { options =qdict_new ();qdict_put (options,"driver" ,qstring_from_str (format));
hmp.c +1 -1
@@ -1348,7 +1348,7 @@ void hmp_change(Monitor *mon, const QDict *qdict) }qmp_change ("vnc" , target, !!arg, arg, &err); }else { -qmp_blockdev_change_medium (device, target, !!arg, arg, &err); +qmp_blockdev_change_medium (device, target, !!arg, arg, false,0 , &err);if (err &&error_get_class (err) ==ERROR_CLASS_DEVICE_ENCRYPTED ) {error_free (err);
qapi/block-core.json +23 -1
@@ -1959,6 +1959,24 @@ ## +# @BlockdevChangeReadOnlyMode: +# +# Specifies the new read-only mode of a block device subject to the +# @blockdev-change-medium command. +# +# @retain: Retains the current read-only mode +# +# @read-only: Makes the device read-only +# +# @read-write: Makes the device writable +# +# Since: 2.3 +## +{ 'enum': 'BlockdevChangeReadOnlyMode', + 'data': ['retain', 'read-only', 'read-write'] } + + +## # @blockdev-change-medium: # # Changes the medium inserted into a block device by ejecting the current medium
@@ -1973,12 +1991,16 @@ # @format: #optional, format to open the new image with (defaults to # the probed format) # +# @read-only-mode: #optional, change the read-only mode of the device; defaults +# to 'retain' +# # Since: 2.5 ## { 'command': 'blockdev-change-medium', 'data': { 'device': 'str', 'filename': 'str', - '*format': 'str' } } + '*format': 'str', + '*read-only-mode': 'BlockdevChangeReadOnlyMode' } } ##
qmp-commands.hx +23 -1
@@ -4164,7 +4164,7 @@ EQMP { .name = "blockdev-change-medium", - .args_type = "device:B,filename:F,format:s?", + .args_type = "device:B,filename:F,format:s?,read-only-mode:s?", .mhandler.cmd_new = qmp_marshal_blockdev_change_medium, },
@@ -4180,6 +4180,8 @@ Arguments: - "device": device name (json-string) - "filename": filename of the new image (json-string) - "format": format of the new image (json-string, optional) +- "read-only-mode": new read-only mode (json-string, optional) + - Possible values: "retain" (default), "read-only", "read-write" Examples:
@@ -4191,6 +4193,26 @@ Examples: "format": "raw" } } <- { "return": {} } +2. Load a read-only medium into a writable drive + +-> { "execute": "blockdev-change-medium", + "arguments": { "device": "isa-fd0", + "filename": "/srv/images/ro.img", + "format": "raw", + "read-only-mode": "retain" } } + +<- { "error": + { "class": "GenericError", + "desc": "Could not open '/srv/images/ro.img': Permission denied" } } + +-> { "execute": "blockdev-change-medium", + "arguments": { "device": "isa-fd0", + "filename": "/srv/images/ro.img", + "format": "raw", + "read-only-mode": "read-only" } } + +<- { "return": {} } + EQMP {
qmp.c +2 -1
@@ -414,7 +414,8 @@ void qmp_change(const char *device, const char *target,if (strcmp (device,"vnc" ) ==0 ) {qmp_change_vnc (target, has_arg, arg, errp); }else { -qmp_blockdev_change_medium (device, target, has_arg, arg, errp); +qmp_blockdev_change_medium (device, target, has_arg, arg, false,0 , + errp); } }
Signature · 1 line
2.6.2
PATCH v8 14/15 hmp: Add read-only-mode option to change command
In reply to Max ReitzExpose the new read-only-mode option of 'blockdev-change-medium' for the 'change' HMP command.
--- hmp-commands.hx | 20 +++++++++++++++++--- hmp.c | 22 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-)
hmp-commands.hx +17 -3
@@ -194,8 +194,8 @@ ETEXI { .name = "change", - .args_type = "device:B,target:F,arg:s?", - .params = "device filename [format]", + .args_type = "device:B,target:F,arg:s?,read-only-mode:s?", + .params = "device filename [format [read-only-mode]]", .help = "change a removable medium, optional format", .mhandler.cmd = hmp_change, },
@@ -206,7 +206,7 @@ STEXI Change the configuration of a device. @table @option -@item change @var{diskdevice} @var{filename} [@var{format}] +@item change @var{diskdevice} @var{filename} [@var{format} [@var{read-only-mode}]] Change the medium for a removable disk device to point to @var{filename}. eg @example
@@ -215,6 +215,20 @@ Change the medium for a removable disk device to point to @var{filename}. eg @var{format} is optional. +@var{read-only-mode} may be used to change the read-only status of the device. +It accepts the following values: + +@table @var +@item retain +Retains the current status; this is the default. + +@item read-only +Makes the device read-only. + +@item read-write +Makes the device writable. +@end table + @item change vnc @var{display},@var{options} Change the configuration of the VNC server. The valid syntax for @var{display} and @var{options} are described at @ref{sec_invocation}. eg
hmp.c +21 -1
@@ -27,6 +27,7 @@#include "qapi/opts-visitor.h" #include "qapi/qmp/qerror.h" #include "qapi/string-output-visitor.h" +#include "qapi/util.h" #include "qapi-visit.h" #include "ui/console.h" #include "block/qapi.h"
@@ -1336,9 +1337,16 @@ void hmp_change(Monitor *mon, const QDict *qdict)const char *device =qdict_get_str (qdict,"device" );const char *target =qdict_get_str (qdict,"target" );const char *arg =qdict_get_try_str (qdict,"arg" ); +const char *read_only =qdict_get_try_str (qdict,"read-only-mode" ); +BlockdevChangeReadOnlyMode read_only_mode =0 ;Error *err =NULL ;if (strcmp (device,"vnc" ) ==0 ) { +if (read_only) { +monitor_printf (mon, +"Parameter 'read-only-mode' is invalid for VNC\n" ); +return ; + }if (strcmp (target,"passwd" ) ==0 ||strcmp (target,"password" ) ==0 ) {if (!arg) {
@@ -1348,7 +1356,19 @@ void hmp_change(Monitor *mon, const QDict *qdict) }qmp_change ("vnc" , target, !!arg, arg, &err); }else { -qmp_blockdev_change_medium (device, target, !!arg, arg, false,0 , &err); +if (read_only) { + read_only_mode = +qapi_enum_parse (BlockdevChangeReadOnlyMode_lookup, + read_only,BLOCKDEV_CHANGE_READ_ONLY_MODE_MAX , +BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN , &err); +if (err) { +hmp_handle_error (mon, &err); +return ; + } + } + +qmp_blockdev_change_medium (device, target, !!arg, arg, + !!read_only, read_only_mode, &err);if (err &&error_get_class (err) ==ERROR_CLASS_DEVICE_ENCRYPTED ) {error_free (err);
Signature · 1 line
2.6.2
PATCH v8 15/15 iotests: Add test for change-related QMP commands
In reply to Max Reitz--- tests/qemu-iotests/118 | 720 +++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/118.out | 5 + tests/qemu-iotests/group | 1 + 3 files changed, 726 insertions(+) create mode 100755 tests/qemu-iotests/118 create mode 100644 tests/qemu-iotests/118.out
tests/qemu-iotests/118 new file +720 -0
@@ -0,0 +1,720 @@ +#!/usr/bin/env python +# +# Test case for the QMP 'change' command and all other associated +# commands +# +# Copyright (C) 2015 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +import os +import stat +import time +import iotests +from iotests import qemu_img + +old_img = os.path.join(iotests.test_dir, 'test0.img') +new_img = os.path.join(iotests.test_dir, 'test1.img') + +class ChangeBaseClass(iotests.QMPTestCase): + has_opened = False + has_closed = False + + def process_events(self): + for event in self.vm.get_qmp_events(wait=False): + if (event['event'] == 'DEVICE_TRAY_MOVED' and + event['data']['device'] == 'drive0'): + if event['data']['tray-open'] == False: + self.has_closed = True + else: + self.has_opened = True + + def wait_for_open(self): + timeout = time.clock() + 3 + while not self.has_opened and time.clock() < timeout: + self.process_events() + if not self.has_opened: + self.fail('Timeout while waiting for the tray to open') + + def wait_for_close(self): + timeout = time.clock() + 3 + while not self.has_closed and time.clock() < timeout: + self.process_events() + if not self.has_opened: + self.fail('Timeout while waiting for the tray to close') + +class GeneralChangeTestsBaseClass(ChangeBaseClass): + def test_change(self): + result = self.vm.qmp('change', device='drive0', target=new_img, + arg=iotests.imgfmt) + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_blockdev_change_medium(self): + result = self.vm.qmp('blockdev-change-medium', device='drive0', + filename=new_img, + format=iotests.imgfmt) + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_eject(self): + result = self.vm.qmp('eject', device='drive0', force=True) + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + self.assert_qmp_absent(result, 'return[0]/inserted') + + def test_tray_eject_change(self): + result = self.vm.qmp('eject', device='drive0', force=True) + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + self.assert_qmp_absent(result, 'return[0]/inserted') + + result = self.vm.qmp('blockdev-change-medium', device='drive0', + filename=new_img, + format=iotests.imgfmt) + self.assert_qmp(result, 'return', {}) + + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_tray_open_close(self): + result = self.vm.qmp('blockdev-open-tray', device='drive0', force=True) + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + if self.was_empty == True: + self.assert_qmp_absent(result, 'return[0]/inserted') + else: + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-close-tray', device='drive0') + self.assert_qmp(result, 'return', {}) + + if self.has_real_tray or not self.was_empty: + self.wait_for_close() + + result = self.vm.qmp('query-block') + if self.has_real_tray or not self.was_empty: + self.assert_qmp(result, 'return[0]/tray_open', False) + else: + self.assert_qmp(result, 'return[0]/tray_open', True) + if self.was_empty == True: + self.assert_qmp_absent(result, 'return[0]/inserted') + else: + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + def test_tray_eject_close(self): + result = self.vm.qmp('eject', device='drive0', force=True) + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + self.assert_qmp_absent(result, 'return[0]/inserted') + + result = self.vm.qmp('blockdev-close-tray', device='drive0') + self.assert_qmp(result, 'return', {}) + + if self.has_real_tray: + self.wait_for_close() + + result = self.vm.qmp('query-block') + if self.has_real_tray: + self.assert_qmp(result, 'return[0]/tray_open', False) + else: + self.assert_qmp(result, 'return[0]/tray_open', True) + self.assert_qmp_absent(result, 'return[0]/inserted') + + def test_tray_open_change(self): + result = self.vm.qmp('blockdev-open-tray', device='drive0', force=True) + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + if self.was_empty == True: + self.assert_qmp_absent(result, 'return[0]/inserted') + else: + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', device='drive0', + filename=new_img, + format=iotests.imgfmt) + self.assert_qmp(result, 'return', {}) + + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_cycle(self): + result = self.vm.qmp('blockdev-add', + options={'node-name': 'new', + 'driver': iotests.imgfmt, + 'file': {'filename': new_img, + 'driver': 'file'}}) + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('blockdev-open-tray', device='drive0', force=True) + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + if self.was_empty == True: + self.assert_qmp_absent(result, 'return[0]/inserted') + else: + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-remove-medium', device='drive0') + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + self.assert_qmp_absent(result, 'return[0]/inserted') + + result = self.vm.qmp('blockdev-insert-medium', device='drive0', + node_name='new') + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + result = self.vm.qmp('blockdev-close-tray', device='drive0') + self.assert_qmp(result, 'return', {}) + + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_close_on_closed(self): + result = self.vm.qmp('blockdev-close-tray', device='drive0') + # Should be a no-op + self.assert_qmp(result, 'return', {}) + self.assertEquals(self.vm.get_qmp_events(wait=False), []) + + def test_remove_on_closed(self): + if self.has_opened: + # Empty floppy drive + return + + result = self.vm.qmp('blockdev-remove-medium', device='drive0') + self.assert_qmp(result, 'error/class', 'GenericError') + + def test_insert_on_closed(self): + if self.has_opened: + # Empty floppy drive + return + + result = self.vm.qmp('blockdev-add', + options={'node-name': 'new', + 'driver': iotests.imgfmt, + 'file': {'filename': new_img, + 'driver': 'file'}}) + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('blockdev-insert-medium', device='drive0', + node_name='new') + self.assert_qmp(result, 'error/class', 'GenericError') + +class TestInitiallyFilled(GeneralChangeTestsBaseClass): + was_empty = False + + def setUp(self, media, interface): + qemu_img('create', '-f', iotests.imgfmt, old_img, '1440k') + qemu_img('create', '-f', iotests.imgfmt, new_img, '1440k') + self.vm = iotests.VM().add_drive(old_img, 'media=%s' % media, interface) + self.vm.launch() + + def tearDown(self): + self.vm.shutdown() + os.remove(old_img) + os.remove(new_img) + + def test_insert_on_filled(self): + result = self.vm.qmp('blockdev-add', + options={'node-name': 'new', + 'driver': iotests.imgfmt, + 'file': {'filename': new_img, + 'driver': 'file'}}) + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('blockdev-open-tray', device='drive0') + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + + result = self.vm.qmp('blockdev-insert-medium', device='drive0', + node_name='new') + self.assert_qmp(result, 'error/class', 'GenericError') + +class TestInitiallyEmpty(GeneralChangeTestsBaseClass): + was_empty = True + + def setUp(self, media, interface): + qemu_img('create', '-f', iotests.imgfmt, new_img, '1440k') + self.vm = iotests.VM().add_drive(None, 'media=%s' % media, interface) + self.vm.launch() + + def tearDown(self): + self.vm.shutdown() + os.remove(new_img) + + def test_remove_on_empty(self): + result = self.vm.qmp('blockdev-open-tray', device='drive0') + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + + result = self.vm.qmp('blockdev-remove-medium', device='drive0') + # Should be a no-op + self.assert_qmp(result, 'return', {}) + +class TestCDInitiallyFilled(TestInitiallyFilled): + TestInitiallyFilled = TestInitiallyFilled + has_real_tray = True + + def setUp(self): + self.TestInitiallyFilled.setUp(self, 'cdrom', 'ide') + +class TestCDInitiallyEmpty(TestInitiallyEmpty): + TestInitiallyEmpty = TestInitiallyEmpty + has_real_tray = True + + def setUp(self): + self.TestInitiallyEmpty.setUp(self, 'cdrom', 'ide') + +class TestFloppyInitiallyFilled(TestInitiallyFilled): + TestInitiallyFilled = TestInitiallyFilled + has_real_tray = False + + def setUp(self): + self.TestInitiallyFilled.setUp(self, 'disk', 'floppy') + +class TestFloppyInitiallyEmpty(TestInitiallyEmpty): + TestInitiallyEmpty = TestInitiallyEmpty + has_real_tray = False + + def setUp(self): + self.TestInitiallyEmpty.setUp(self, 'disk', 'floppy') + # FDDs not having a real tray and there not being a medium inside the + # tray at startup means the tray will be considered open + self.has_opened = True + +class TestChangeReadOnly(ChangeBaseClass): + def setUp(self): + qemu_img('create', '-f', iotests.imgfmt, old_img, '1440k') + qemu_img('create', '-f', iotests.imgfmt, new_img, '1440k') + self.vm = iotests.VM() + + def tearDown(self): + self.vm.shutdown() + os.chmod(old_img, 0666) + os.chmod(new_img, 0666) + os.remove(old_img) + os.remove(new_img) + + def test_ro_ro_retain(self): + os.chmod(old_img, 0444) + os.chmod(new_img, 0444) + self.vm.add_drive(old_img, 'media=disk,read-only=on', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', device='drive0', + filename=new_img, + format=iotests.imgfmt, + read_only_mode='retain') + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_ro_rw_retain(self): + os.chmod(old_img, 0444) + self.vm.add_drive(old_img, 'media=disk,read-only=on', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', device='drive0', + filename=new_img, + format=iotests.imgfmt, + read_only_mode='retain') + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_rw_ro_retain(self): + os.chmod(new_img, 0444) + self.vm.add_drive(old_img, 'media=disk', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', device='drive0', + filename=new_img, + format=iotests.imgfmt, + read_only_mode='retain') + self.assert_qmp(result, 'error/class', 'GenericError') + + self.assertEquals(self.vm.get_qmp_events(wait=False), []) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + def test_ro_rw(self): + os.chmod(old_img, 0444) + self.vm.add_drive(old_img, 'media=disk,read-only=on', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', + device='drive0', + filename=new_img, + format=iotests.imgfmt, + read_only_mode='read-write') + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_rw_ro(self): + os.chmod(new_img, 0444) + self.vm.add_drive(old_img, 'media=disk', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', + device='drive0', + filename=new_img, + format=iotests.imgfmt, + read_only_mode='read-only') + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_make_rw_ro(self): + self.vm.add_drive(old_img, 'media=disk', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', + device='drive0', + filename=new_img, + format=iotests.imgfmt, + read_only_mode='read-only') + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_make_ro_rw(self): + os.chmod(new_img, 0444) + self.vm.add_drive(old_img, 'media=disk', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', + device='drive0', + filename=new_img, + format=iotests.imgfmt, + read_only_mode='read-write') + self.assert_qmp(result, 'error/class', 'GenericError') + + self.assertEquals(self.vm.get_qmp_events(wait=False), []) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + def test_make_rw_ro_by_retain(self): + os.chmod(old_img, 0444) + self.vm.add_drive(old_img, 'media=disk,read-only=on', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', device='drive0', + filename=new_img, + format=iotests.imgfmt, + read_only_mode='retain') + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + def test_make_ro_rw_by_retain(self): + os.chmod(new_img, 0444) + self.vm.add_drive(old_img, 'media=disk', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-change-medium', device='drive0', + filename=new_img, + format=iotests.imgfmt, + read_only_mode='retain') + self.assert_qmp(result, 'error/class', 'GenericError') + + self.assertEquals(self.vm.get_qmp_events(wait=False), []) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + def test_rw_ro_cycle(self): + os.chmod(new_img, 0444) + self.vm.add_drive(old_img, 'media=disk', 'floppy') + self.vm.launch() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-add', + options={'node-name': 'new', + 'driver': iotests.imgfmt, + 'read-only': True, + 'file': {'filename': new_img, + 'driver': 'file'}}) + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('blockdev-open-tray', device='drive0', force=True) + self.assert_qmp(result, 'return', {}) + + self.wait_for_open() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + self.assert_qmp(result, 'return[0]/inserted/ro', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + result = self.vm.qmp('blockdev-remove-medium', device='drive0') + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + self.assert_qmp_absent(result, 'return[0]/inserted') + + result = self.vm.qmp('blockdev-insert-medium', device='drive0', + node_name='new') + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', True) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + + result = self.vm.qmp('blockdev-close-tray', device='drive0') + self.assert_qmp(result, 'return', {}) + + self.wait_for_close() + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/ro', True) + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + +GeneralChangeTestsBaseClass = None +TestInitiallyFilled = None +TestInitiallyEmpty = None + + +class TestBlockJobsAfterCycle(ChangeBaseClass): + def setUp(self): + qemu_img('create', '-f', iotests.imgfmt, old_img, '1M') + + self.vm = iotests.VM() + self.vm.launch() + + result = self.vm.qmp('blockdev-add', + options={'id': 'drive0', + 'driver': 'null-co'}) + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/image/format', 'null-co') + + # For device-less BBs, calling blockdev-open-tray or blockdev-close-tray + # is not necessary + result = self.vm.qmp('blockdev-remove-medium', device='drive0') + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('query-block') + self.assert_qmp_absent(result, 'return[0]/inserted') + + result = self.vm.qmp('blockdev-add', + options={'node-name': 'node0', + 'driver': iotests.imgfmt, + 'file': {'filename': old_img, + 'driver': 'file'}}) + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('blockdev-insert-medium', device='drive0', + node_name='node0') + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/tray_open', False) + self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img) + + def tearDown(self): + self.vm.shutdown() + os.remove(old_img) + try: + os.remove(new_img) + except OSError: + pass + + def test_snapshot_and_commit(self): + # We need backing file support + if iotests.imgfmt != 'qcow2' and iotests.imgfmt != 'qed': + return + + result = self.vm.qmp('blockdev-snapshot-sync', device='drive0', + snapshot_file=new_img, + format=iotests.imgfmt) + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('query-block') + self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img) + self.assert_qmp(result, + 'return[0]/inserted/image/backing-image/filename', + old_img) + + result = self.vm.qmp('block-commit', device='drive0') + self.assert_qmp(result, 'return', {}) + + self.vm.event_wait(name='BLOCK_JOB_READY') + + result = self.vm.qmp('query-block-jobs') + self.assert_qmp(result, 'return[0]/device', 'drive0') + + result = self.vm.qmp('block-job-complete', device='drive0') + self.assert_qmp(result, 'return', {}) + + self.vm.event_wait(name='BLOCK_JOB_COMPLETED') + + +if __name__ == '__main__': + if iotests.qemu_default_machine != 'pc': + # We need floppy and IDE CD-ROM + iotests.notrun('not suitable for this machine type: %s' % + iotests.qemu_default_machine) + iotests.main()
tests/qemu-iotests/118.out new file +5 -0
@@ -0,0 +1,5 @@ +........................................................... +---------------------------------------------------------------------- +Ran 59 tests + +OK
tests/qemu-iotests/group +1 -0
@@ -122,6 +122,7 @@ 114 rw auto quick 115 rw auto 116 rw auto quick +118 rw auto 119 rw auto quick 120 rw auto quick 121 rw auto
Signature · 1 line
2.6.2
Kevin Wolf
AppliedAm 26.10.2015 um 21:39 hat Max Reitz geschrieben:
Quoted 7 lines
Now that the main rework part of this series is merged, these remaining patches here implement atomic tray/medium operations and add the read-only-mode parameter to change and blockdev-change-medium (which was the original purpose of this series!).
Once again, I'd like to thank all the reviewers for sticking to this long series over many iterations. Thanks a lot! :-)
Thanks, applied to the block branch.
Kevin