int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
BlockDriver *drv)
{
- int ret, open_flags;
+ int ret, open_flags, try_rw;
char tmp_filename[PATH_MAX];
char backing_filename[PATH_MAX];
- bs->read_only = 0;
bs->is_temporary = 0;
bs->encrypted = 0;
bs->valid_key = 0;
/* Note: for compatibility, we open disk image files as RDWR, and
RDONLY as fallback */
+ try_rw = !bs->read_only || bs->is_temporary;
if (!(flags & BDRV_O_FILE))
- open_flags = BDRV_O_RDWR |
- (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
+ open_flags = (try_rw ? BDRV_O_RDWR : 0) |
+ (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
else
open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
ret = drv->bdrv_open(bs, filename, open_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')
return -ENOMEDIUM;
if (!drv->bdrv_truncate)
return -ENOTSUP;
+ if (bs->read_only)
+ return -EACCES;
return drv->bdrv_truncate(bs, offset);
}
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;
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);
int index;
int cache;
int aio = 0;
+ int ro = 0;
int bdrv_flags, onerror;
const char *devaddr;
DriveInfo *dinfo;
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");
bdrv_flags &= ~BDRV_O_NATIVE_AIO;
}
+ if (ro == 1) {
+ if (type == IF_IDE) {
+ fprintf(stderr, "qemu: readonly flag not supported for drive with ide interface\n");
+ return NULL;
+ }
+ (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));