From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55545) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WxFht-0007ai-MG for qemu-devel@nongnu.org; Wed, 18 Jun 2014 09:15:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WxFhl-0006FM-Oc for qemu-devel@nongnu.org; Wed, 18 Jun 2014 09:15:17 -0400 Received: from lputeaux-656-01-25-125.w80-12.abo.wanadoo.fr ([80.12.84.125]:51535 helo=paradis.irqsave.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WxFhl-0006F2-FX for qemu-devel@nongnu.org; Wed, 18 Jun 2014 09:15:09 -0400 Date: Wed, 18 Jun 2014 15:15:07 +0200 From: =?iso-8859-1?Q?Beno=EEt?= Canet Message-ID: <20140618131507.GD4107@irqsave.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v6 for 2.1 10/10] block: add QAPI command to allow live backing file change List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jeff Cody Cc: kwolf@redhat.com, benoit.canet@irqsave.net, pkrempa@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com The Tuesday 17 Jun 2014 =E0 17:53:58 (-0400), Jeff Cody wrote : > This allows a user to make a live change to the backing file recorded i= n > an open image. >=20 > The image file to modify can be specified 2 ways: >=20 > 1) image filename > 2) image node-name >=20 > 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. >=20 > 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. >=20 > 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. >=20 > Reviewed-by: Eric Blake > Signed-off-by: Jeff Cody > --- > blockdev.c | 102 +++++++++++++++++++++++++++++++++++++++++++= ++++++++ > qapi/block-core.json | 60 ++++++++++++++++++++++++++++++ > qmp-commands.hx | 74 +++++++++++++++++++++++++++++++++++++ > 3 files changed, 236 insertions(+) >=20 > diff --git a/blockdev.c b/blockdev.c > index 42c7b3f..273e52a 100644 > --- a/blockdev.c > +++ b/blockdev.c > @@ -2414,6 +2414,108 @@ void qmp_block_job_complete(const char *device,= Error **errp) > block_job_complete(job, errp); > } > =20 > +void qmp_change_backing_file(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 =3D NULL; > + BlockDriverState *image_bs =3D NULL; > + Error *local_err =3D 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; > + } > + > + /* find the top layer BDS of the chain */ > + bs =3D bdrv_find(device); > + if (!bs) { > + error_set(errp, QERR_DEVICE_NOT_FOUND, device); > + return; > + } > + > + if (has_image_node_name) { > + image_bs =3D bdrv_lookup_bs(NULL, image_node_name, &local_err)= ; > + if (local_err) { > + error_propagate(errp, local_err); > + return; > + } > + } > + > + if (has_image) { > + if (!strcmp(bs->filename, image)) { > + image_bs =3D bs; > + } else { > + image_bs =3D bdrv_find_backing_image(bs, image); > + } > + } > + > + if (!has_image && !has_image_node_name) { > + image_bs =3D bs; > + } > + > + if (!image_bs) { > + error_setg(errp, "image file not found"); > + return; > + } > + > + if (bdrv_find_base(image_bs) =3D=3D 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 chai= n", > + device); > + return; > + } > + > + /* if not r/w, reopen to make r/w */ > + open_flags =3D image_bs->open_flags; > + ro =3D 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 =3D bdrv_change_backing_file(image_bs, backing_file, > + image_bs->drv ? image_bs->drv->format_n= ame : ""); > + > + 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 e= rrp */ > + } > + } > +} > + > void qmp_blockdev_add(BlockdevOptions *options, Error **errp) > { > QmpOutputVisitor *ov =3D qmp_output_visitor_new(); > diff --git a/qapi/block-core.json b/qapi/block-core.json > index c76d45d..0939143 100644 > --- a/qapi/block-core.json > +++ b/qapi/block-core.json > @@ -680,6 +680,66 @@ > 'data': 'BlockdevSnapshot' } > =20 > ## > +# @change-backing-file > +# > +# Change the backing file in the image file metadata. This does not c= ause QEMU > +# to reopen the image file to reparse the backing filename (it may, ho= wever, > +# perform a reopen to change permissions from r/o -> r/w -> r/o, if ne= eded). > +# 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 d= ifferent > +# methods: > +# > +# Method 1: Supply the device name (e.g. 'virtio0'), and optionally t= he image > +# filename. This would use arguments @device and @image. > +# > +# Method 2: Supply the device name, and the node-name of the image to= modify, > +# via @image-node-name. > +# > +# Arguments @image and @image-node-name are mutually exclusive. > +# > +# Method 1 interface > +#--------------------- > +# @image: #optional The file name of the image to modify. If= omitted, > +# and @image-node-name is not supplied, the= n 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 o= f the > +# image to modify. The @device argument is= used to > +# verify @image-node-name is in the chain d= escribed > +# by @device. > +# > +# Common arguments > +#--------------------- > +# @device: The name of the device. > +# > +# @backing-file: The string to write as the backing file. This str= ing is > +# not validated, so care should be taken when specif= ying > +# the string or the image chain may not be able to b= e > +# reopened again. > +# > +# If a pathname string is such that it cannot be > +# resolved by QEMU, that means that subsequent QMP o= r > +# 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, DeviceNo= tFound > +# If @image is specified, but not @device, GenericError > +# If both @image and @image-node-name are specified, GenericE= rror > +# > +# 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 314cbba..c1cbfd2 100644 > --- a/qmp-commands.hx > +++ b/qmp-commands.hx > @@ -1438,6 +1438,80 @@ Example: > EQMP > =20 > { > + .name =3D "change-backing-file", > + .args_type =3D "device:s,image:s?,image-node-name:s?,backing-= file:s", > + .mhandler.cmd_new =3D 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 cau= se QEMU > +to reopen the image file to reparse the backing filename (it may, howe= ver, > +perform a reopen to change permissions from r/o -> r/w -> r/o, if need= ed). > +The new backing file string is written into the image file metadata, a= nd the > +QEMU internal strings are updated. > + > +The image file to perform the operation on can be specified by two dif= ferent > +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 device name, and the node-name of the image to m= odify, > + via "image-node-name". > + > +Arguments: > + > +Arguments "image" or "image-node-name" are mutually exclusive. > + > + > +Method 1 interface > +-------------------- > +- "image": The file name of the image to modify. If omit= ted, > + and "image-node-name" is not supplied, then th= e > + default is the active layer of the chain descr= ibed > + by device. > + (json-string, optional) > + > + > +Method 2 interface > +-------------------- > +- "image-node-name": The name of the block driver state node of the > + image to modify. The "device" is argument is = used to > + verify "image-node-name" is in the chain descr= ibed by > + "device". > + (json-string, optional) > + > + > +Common arguments > +-------------------- > +- "device": The name of the device. > + (json-string) > + > +- "backing-file": The string to write as the backing file. This= string is > + not validated, so care should be taken when sp= ecifying > + 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 Q= MP 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, DeviceNot= Found > + If "image" is specified, but not "device, GenericError > + If both "image" and "image-node-name" are specified, GenericE= rror > + > + > +EQMP > + > + { > .name =3D "balloon", > .args_type =3D "value:M", > .mhandler.cmd_new =3D qmp_marshal_input_balloon, > --=20 > 1.9.3 >=20 Could you provide an example script of how these backing file manipulatio= n are suposed to be done ? Best regards Beno=EEt