From patchwork Fri May 30 17:35:30 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Cody X-Patchwork-Id: 354307 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A8CEF1400D8 for ; Sat, 31 May 2014 04:48:42 +1000 (EST) Received: from localhost ([::1]:55796 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WqQmN-0001ZL-O1 for incoming@patchwork.ozlabs.org; Fri, 30 May 2014 13:39:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46386) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WqQj7-0005NT-CL for qemu-devel@nongnu.org; Fri, 30 May 2014 13:36:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WqQiz-0002kN-MS for qemu-devel@nongnu.org; Fri, 30 May 2014 13:36:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:63094) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WqQiz-0002kA-Ek for qemu-devel@nongnu.org; Fri, 30 May 2014 13:36:13 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s4UHaBL1022068 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 30 May 2014 13:36:11 -0400 Received: from localhost (ovpn-112-54.phx2.redhat.com [10.3.112.54]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s4UHa8OQ014213 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NO); Fri, 30 May 2014 13:36:10 -0400 From: Jeff Cody To: qemu-devel@nongnu.org Date: Fri, 30 May 2014 13:35:30 -0400 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, benoit.canet@irqsave.net, pkrempa@redhat.com, famz@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH v3 12/12] block: add QAPI command to allow live backing file change X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This allows a user to make a live change to the backing file recorded in an open image. The image file to modify can be specified 2 ways: 1) 'device' string, and image filename 2) image node-name Note: this does not cause the backing file itself to be reopened; it merely changes the backing filename in the image file structure, and in internal BDS structures. It is the responsibility of the user to pass a filename string that can be resolved when the image chain is reopened, and the filename string is not validated. A good analogy for this command is that it is a live version of 'qemu-img rebase -u', with respect to changing the backing file string. Signed-off-by: Jeff Cody Reviewed-by: Eric Blake --- blockdev.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ qapi-schema.json | 65 +++++++++++++++++++++++++++++ qmp-commands.hx | 78 +++++++++++++++++++++++++++++++++++ 3 files changed, 265 insertions(+) diff --git a/blockdev.c b/blockdev.c index b867314..e3c9ccf 100644 --- a/blockdev.c +++ b/blockdev.c @@ -2440,6 +2440,128 @@ void qmp_block_job_complete(const char *device, Error **errp) block_job_complete(job, errp); } +void qmp_change_backing_file(bool has_device, const char *device, + bool has_image, const char *image, + bool has_image_node_name, + const char *image_node_name, + const char *backing_file, + Error **errp) +{ + BlockDriverState *bs = NULL; + BlockDriverState *image_bs = NULL; + Error *local_err = NULL; + bool ro; + int open_flags; + int ret; + + /* validate argument combinations */ + if (has_image && has_image_node_name) { + error_setg(errp, "'image' and 'image-node-name' " + "are mutually exclusive"); + return; + } + + if (has_image && !has_device) { + error_setg(errp, "'image' specified, but not 'device'"); + return; + } + + if (!has_image && !has_image_node_name && !has_device) { + error_setg(errp, "'image', 'image_node_name', and 'device' omitted - " + "cannot determine which image to modify"); + return; + } + + /* find the BDS to operate on */ + if (has_device) { + bs = bdrv_find(device); + if (!bs) { + error_set(errp, QERR_DEVICE_NOT_FOUND, device); + return; + } + } + + if (has_image_node_name) { + image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + bs = bs ?: bdrv_find_active(image_bs); + } + + if (bs && has_image) { + if (!strcmp(bs->filename, image)) { + image_bs = bs; + } else { + image_bs = bdrv_find_backing_image(bs, image); + } + } + + if (!has_image && !has_image_node_name) { + image_bs = bs; + } + + if (!image_bs) { + error_setg(errp, "image file not found"); + return; + } + + if (!bs) { + error_setg(errp, "could not find active layer for '%s'", + image_bs->filename); + return; + } + + if (bdrv_find_base(image_bs) == image_bs) { + error_setg(errp, "not allowing backing file change on an image " + "without a backing file"); + return; + } + + /* even though we are not necessarily operating on bs, we need it to + * determine if block ops are currently prohibited on the chain */ + if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { + return; + } + + /* final sanity check */ + if (!bdrv_chain_contains(bs, image_bs)) { + error_setg(errp, "'%s' and image file are not in the same chain", + device); + return; + } + + /* if not r/w, reopen to make r/w */ + open_flags = image_bs->open_flags; + ro = bdrv_is_read_only(image_bs); + + if (ro) { + bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); + if (local_err) { + error_propagate(errp, local_err); + return; + } + } + + ret = bdrv_change_backing_file(image_bs, backing_file, + image_bs->drv ? image_bs->drv->format_name : ""); + + if (ret < 0) { + error_setg_errno(errp, -ret, "Could not change backing file to '%s'", + backing_file); + /* don't exit here, so we can try to restore open flags if + * appropriate */ + } + + if (ro) { + bdrv_reopen(image_bs, open_flags, &local_err); + if (local_err) { + error_propagate(errp, local_err); /* will preserve prior errp */ + } + } +} + void qmp_blockdev_add(BlockdevOptions *options, Error **errp) { QmpOutputVisitor *ov = qmp_output_visitor_new(); diff --git a/qapi-schema.json b/qapi-schema.json index 2cf5907..859d44d 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -2092,6 +2092,71 @@ 'returns': 'str' } ## +# @change-backing-file +# +# Change the backing file in the image file metadata. This does not cause QEMU +# to reopen the image file to reparse the backing filename (it may, however, +# perform a reopen to change permissions from r/o -> r/w -> r/o, if needed). +# The new backing file string is written into the image file metadata, and the +# QEMU internal strings are updated. +# +# The image file to perform the operation on can be specified by two different +# methods: +# +# Method 1: Supply the device name (e.g. 'virtio0'), and optionally the image +# filename. This would use arguments @device and @image. +# +# Method 2: Supply the node-name of the image to modify, via @image-node-name. +# +# Arguments @image and @image-node-name are mutually exclusive. +# +# If @image is specified, @device must be specified as well. +# +# Method 1 interface +#--------------------- +# @device: #optional The name of the device. If @image is specified, +# then @device is required. If @image-node-name is +# specified instead, @device is optional and used to +# validate @image-node-name. +# +# @image: #optional The file name of the image to modify. If omitted, +# and @image-node-name is not supplied, then the +# default is the active layer of the chain described +# by @device. +# +# Method 2 interface +#--------------------- +# @image-node-name #optional The name of the block driver state node of the +# image to modify. If @device is specified along +# with @image-node-name, then it is used to verify +# @image-node-name is in the chain described by +# @device. +# +# Common arguments +#--------------------- +# @backing-file: The string to write as the backing file. This string is +# not validated, so care should be taken when specifying +# the string or the image chain may not be able to be +# reopened again. +# +# If a pathname string is such that it cannot be +# resolved by QEMU, that means that subsequent QMP or +# HMP commands must use node-names for the image in +# question, as filename lookup methods will fail. +# +# +# Returns: Nothing on success +# If @device does not exist or cannot be determined, DeviceNotFound +# If @image is specified, but not @device, GenericError +# If both @image and @image-node-name are specified, GenericError +# +# Since: 2.1 +## +{ 'command': 'change-backing-file', + 'data': { '*device': 'str', '*image': 'str', '*image-node-name': 'str', + 'backing-file': 'str' } } + +## # @block-commit # # Live commit of data from overlay image nodes into backing nodes - i.e., diff --git a/qmp-commands.hx b/qmp-commands.hx index f4a2a0a..3f76b5e 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -1440,6 +1440,84 @@ Example: EQMP { + .name = "change-backing-file", + .args_type = "device:s?,image:s?,image-node-name:s?,backing-file:s", + .mhandler.cmd_new = qmp_marshal_input_change_backing_file, + }, + +SQMP +change-backing-file +------------------- +Since: 2.1 + +Change the backing file in the image file metadata. This does not cause QEMU +to reopen the image file to reparse the backing filename (it may, however, +perform a reopen to change permissions from r/o -> r/w -> r/o, if needed). +The new backing file string is written into the image file metadata, and the +QEMU internal strings are updated. + +The image file to perform the operation on can be specified by two different +methods: + + Method 1: Supply the device name (e.g. 'virtio0'), and optionally the image + filename. This would use arguments "device" and "image". + + Method 2: Supply the node-name of the image to modify, via "image-node-name". + +Arguments: + +Arguments "image" or "image-node-name" are mutually exclusive. + +If "image" is specified, "device" must be specified as well. + +Method 1 interface +-------------------- +- "device": The name of the device. If "image" is specified, + then "device" is required. If "image-node-name" is + specified instead, "device" is optional and used to + validate "image-node-name". + (json-string, optional) + +- "image": The file name of the image to modify. If omitted, + and "image-node-name" is not supplied, then the + default is the active layer of the chain described + by device. + (json-string, optional) + + +Method 2 interface +-------------------- +- "image-node-name": The name of the block driver state node of the + image to modify. If "device" is specified along + with "image-node-name", then it is used to verify + "image-node-name" is in the chain described by + "device". + (json-string, optional) + + +Common arguments +-------------------- +- "backing-file": The string to write as the backing file. This string is + not validated, so care should be taken when specifying + the string or the image chain may not be able to be + reopened again. + (json-string) + + If a pathname string is such that it cannot be + resolved by QEMU, that means that subsequent QMP or + HMP commands must use node-names for the image in + question, as filename lookup methods will fail. + + +Returns: Nothing on success + If "device" does not exist or cannot be determined, DeviceNotFound + If "image" is specified, but not "device, GenericError + If both "image" and "image-node-name" are specified, GenericError + + +EQMP + + { .name = "balloon", .args_type = "value:M", .mhandler.cmd_new = qmp_marshal_input_balloon,