enforce read only on disks
PATCH 1/1 enforce read only on disks
Hi all,
currently even if you specify 'r' for a disk on your VM config file, qemu tries to open the file O_RDWR first.
This patch enforces that a disk with mode = 'r' on xenstore is actually opened O_RDONLY.
Regards,
Stefano Stabellini
Signed-off-by Stefano Stabellini <[email protected]>
block-raw-posix.c +0 -1
@@ -133,7 +133,6 @@ open_flags |=O_RDWR ; }else { open_flags |=O_RDONLY ; - bs->read_only =1 ; }if (flags &BDRV_O_CREAT ) open_flags |=O_CREAT |O_TRUNC ;
block.c +10 -4
@@ -376,7 +376,6 @@char tmp_filename[PATH_MAX ];char backing_filename[PATH_MAX ]; - bs->read_only =0 ; bs->is_temporary =0 ; bs->encrypted =0 ;
@@ -444,12 +443,14 @@ bs->total_sectors =0 ;/* driver will set if it does not do getlength */ if (bs->opaque ==NULL && drv->instance_size >0 )return -1 ; -/* Note: for compatibility, we open disk image files as RDWR, and -RDONLY as fallback */ if (!(flags &BDRV_O_FILE )) - open_flags =BDRV_O_RDWR | (flags &BDRV_O_CACHE_MASK ); + open_flags = flags &BDRV_O_CACHE_MASK ;else open_flags = flags & ~(BDRV_O_FILE |BDRV_O_SNAPSHOT ); +if (bs->read_only) + open_flags &= ~BDRV_O_RDWR ; +else + open_flags |=BDRV_O_RDWR ; ret = drv->bdrv_open (bs, filename, open_flags);if ((ret == -EACCES || ret == -EPERM ) && !(flags &BDRV_O_FILE )) { ret = drv->bdrv_open (bs, filename, open_flags & ~BDRV_O_RDWR );
@@ -867,6 +868,11 @@int bdrv_is_read_only (BlockDriverState *bs) {return bs->read_only; +} + +void bdrv_set_read_only (BlockDriverState *bs) +{ + bs->read_only =1 ; }int bdrv_is_sg (BlockDriverState *bs)
block.h +1 -0
@@ -125,6 +125,7 @@int bdrv_get_translation_hint (BlockDriverState *bs);int bdrv_is_removable (BlockDriverState *bs);int bdrv_is_read_only (BlockDriverState *bs); +void bdrv_set_read_only (BlockDriverState *bs);int bdrv_is_sg (BlockDriverState *bs);int bdrv_is_inserted (BlockDriverState *bs);int bdrv_media_changed (BlockDriverState *bs);
xenstore.c +10 -1
@@ -297,7 +297,7 @@char **e_danger =NULL ;char *buf =NULL ;char *fpath =NULL , *bpath =NULL , - *dev =NULL , *params =NULL , *drv =NULL ; + *dev =NULL , *params =NULL , *mode =NULL , *drv =NULL ;int i, any_hdN =0 , ret;unsigned int len, num, hd_index, pci_devid =0 ;BlockDriverState *bs;
@@ -413,6 +413,11 @@ params = newparams; format = &bdrv_raw; } +free (mode); +if (pasprintf (&buf,"%s/mode" , bpath) ==-1 ) + mode =NULL ; +else + mode =xs_read (xsh,XBT_NULL , buf, &len);#if 0 /* Phantom VBDs are disabled because the use of paths
@@ -454,6 +459,9 @@if (pasprintf (&buf,"%s/params" , bpath) !=-1 )xs_watch (xsh, buf, dev); } + +if (mode &&strchr (mode,'w' ) ==NULL ) +bdrv_set_read_only (bs);/* open device now if media present */ #ifdef CONFIG_STUBDOM
@@ -571,6 +579,7 @@ } out: +free (mode);free (danger_type);free (params);free (dev);