From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MxKJB-0005YP-6b for qemu-devel@nongnu.org; Mon, 12 Oct 2009 08:47:25 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MxKJ6-0005WP-6x for qemu-devel@nongnu.org; Mon, 12 Oct 2009 08:47:24 -0400 Received: from [199.232.76.173] (port=45288 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MxKJ5-0005W9-U5 for qemu-devel@nongnu.org; Mon, 12 Oct 2009 08:47:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49807) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MxKJ5-0006d6-Bk for qemu-devel@nongnu.org; Mon, 12 Oct 2009 08:47:19 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n9CClHhI025578 for ; Mon, 12 Oct 2009 08:47:17 -0400 Received: from [10.35.0.60] (dhcp-0-60.tlv.redhat.com [10.35.0.60]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n9CClDL8026504 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 12 Oct 2009 08:47:15 -0400 Message-ID: <4AD32550.8040901@redhat.com> Date: Mon, 12 Oct 2009 14:47:12 +0200 From: Naphtali Sprei MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] [PATCH] Add readonly flag to -drive command List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org In order to safely share an image between guests (as read only drive), add a 'readonly' flag to the -drive command (qemu command line and monitor). Still missing passing the read only attribute to the guest, where possible. I don't know which device types supports read only, and don't know how to pass this information to guests. Also not sure what to do when qemu cannot open the file as writeable. Currently it opens it as read only. We might change it to give a warning or even an error. >>From 6e297da79a4c015555e3927e6d28744933a31ebe Mon Sep 17 00:00:00 2001 From: Naphtali Sprei Date: Mon, 12 Oct 2009 14:25:25 +0200 Subject: [PATCH] Added readonly flag to -drive command. This enables sharing same image between guests, with readonly access. Implementaion mark the drive as read_only and changes the flags when actually opening the file. TODO: 1. Pass the readonly attribute to the guest (write-protected drive ??) 2. Re-visit the scheme where qemu open a file (silently) in read only mode when it can't open for write. Now that user can specify read only (and didn't), might give a warning when not writeable, or even give an error. Signed-off-by: Naphtali Sprei --- block.c | 14 ++++++++++++-- block.h | 1 + qemu-config.c | 3 +++ vl.c | 6 ++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/block.c b/block.c index 33f3d65..01fd289 100644 --- a/block.c +++ b/block.c @@ -335,7 +335,8 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, char tmp_filename[PATH_MAX]; char backing_filename[PATH_MAX]; - bs->read_only = 0; + /* don't mess with it, should have been zeros, anyway */ + /* bs->read_only = 0; */ bs->is_temporary = 0; bs->encrypted = 0; bs->valid_key = 0; @@ -423,7 +424,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, /* Note: for compatibility, we open disk image files as RDWR, and RDONLY as fallback */ if (!(flags & BDRV_O_FILE)) - open_flags = BDRV_O_RDWR | + open_flags = (bs->read_only ? 0 : BDRV_O_RDWR) | (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); else open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); @@ -453,6 +454,8 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, /* if there is a backing file, use it */ BlockDriver *back_drv = NULL; bs->backing_hd = bdrv_new(""); + /* pass on read_only property to the backing_hd */ + bs->backing_hd->read_only = bs->read_only; path_combine(backing_filename, sizeof(backing_filename), filename, bs->backing_file); if (bs->backing_format[0] != '\0') @@ -925,6 +928,13 @@ int bdrv_is_read_only(BlockDriverState *bs) return bs->read_only; } +int bdrv_set_read_only(BlockDriverState *bs, int read_only) +{ + int ret = bs->read_only; + bs->read_only = read_only; + return ret; +} + int bdrv_is_sg(BlockDriverState *bs) { return bs->sg; diff --git a/block.h b/block.h index a966afb..302010d 100644 --- a/block.h +++ b/block.h @@ -136,6 +136,7 @@ int bdrv_get_type_hint(BlockDriverState *bs); int bdrv_get_translation_hint(BlockDriverState *bs); int bdrv_is_removable(BlockDriverState *bs); int bdrv_is_read_only(BlockDriverState *bs); +int bdrv_set_read_only(BlockDriverState *bs, int read_only); int bdrv_is_sg(BlockDriverState *bs); int bdrv_enable_write_cache(BlockDriverState *bs); int bdrv_is_inserted(BlockDriverState *bs); diff --git a/qemu-config.c b/qemu-config.c index bafaea2..5eb8838 100644 --- a/qemu-config.c +++ b/qemu-config.c @@ -71,6 +71,9 @@ QemuOptsList qemu_drive_opts = { .name = "addr", .type = QEMU_OPT_STRING, .help = "pci address (virtio only)", + },{ + .name = "readonly", + .type = QEMU_OPT_BOOL, }, { /* end if list */ } }, diff --git a/vl.c b/vl.c index 374f85b..3763911 100644 --- a/vl.c +++ b/vl.c @@ -2007,6 +2007,7 @@ DriveInfo *drive_init(QemuOpts *opts, void *opaque, int index; int cache; int aio = 0; + int ro = 0; int bdrv_flags, onerror; const char *devaddr; DriveInfo *dinfo; @@ -2038,6 +2039,7 @@ DriveInfo *drive_init(QemuOpts *opts, void *opaque, secs = qemu_opt_get_number(opts, "secs", 0); snapshot = qemu_opt_get_bool(opts, "snapshot", 0); + ro = qemu_opt_get_bool(opts, "readonly", 0); file = qemu_opt_get(opts, "file"); serial = qemu_opt_get(opts, "serial"); @@ -2329,6 +2331,10 @@ DriveInfo *drive_init(QemuOpts *opts, void *opaque, bdrv_flags &= ~BDRV_O_NATIVE_AIO; } + if (ro == 1) { + (void)bdrv_set_read_only(dinfo->bdrv, 1); + } + if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { fprintf(stderr, "qemu: could not open disk image %s: %s\n", file, strerror(errno)); -- 1.6.3.3