Add 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. Signed-off-by: Max Reitz <mre...@redhat.com> Reviewed-by: Eric Blake <ebl...@redhat.com> --- blockdev.c | 25 ++++++++++++++++++++++++- hmp.c | 2 +- qapi/block-core.json | 24 +++++++++++++++++++++++- qmp-commands.hx | 24 +++++++++++++++++++++++- qmp.c | 3 ++- 5 files changed, 73 insertions(+), 5 deletions(-) diff --git a/blockdev.c b/blockdev.c index 0f18c92..77e47da 100644 --- a/blockdev.c +++ b/blockdev.c @@ -2104,6 +2104,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; @@ -2125,7 +2127,28 @@ void qmp_blockdev_change_medium(const char *device, const char *filename, } blk_rs = blk_get_root_state(blk); - bdrv_flags = blk_rs->read_only ? 0 : BDRV_O_RDWR; + + if (!has_read_only) { + read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; + } + + switch (read_only) { + case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: + bdrv_flags = blk_rs->read_only ? 0 : BDRV_O_RDWR; + break; + + case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: + bdrv_flags = 0; + break; + + case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: + bdrv_flags = BDRV_O_RDWR; + break; + + default: + abort(); + } + bdrv_flags |= blk_rs->open_flags & ~BDRV_O_RDWR; if (has_format) { diff --git a/hmp.c b/hmp.c index 1a201a9..a4f3634 100644 --- a/hmp.c +++ b/hmp.c @@ -1327,7 +1327,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); diff --git a/qapi/block-core.json b/qapi/block-core.json index bc56625..cb99cad 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -1944,6 +1944,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 @@ -1958,12 +1976,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' } } ## diff --git a/qmp-commands.hx b/qmp-commands.hx index ce8516b..5936b24 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -4091,7 +4091,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_input_blockdev_change_medium, }, @@ -4107,6 +4107,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: @@ -4118,6 +4120,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 { diff --git a/qmp.c b/qmp.c index 55f006c..3493b3b 100644 --- a/qmp.c +++ b/qmp.c @@ -413,7 +413,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); } } -- 2.4.6