#NEWLINE# @ contextlib . contextmanager #NEWLINE# def vdi_attached_here( session , vdi_ref , read_only = False ) : #NEWLINE# #INDENT# this_vm_ref = _get_this_vm_ref ( session ) #NEWLINE# vbd_ref = create_vbd ( session , this_vm_ref , vdi_ref , 'autodetect' , read_only = read_only , bootable = False ) #NEWLINE# try : #NEWLINE# #INDENT# LOG . debug ( 'Plugging VBD %s ... ' , vbd_ref ) #NEWLINE# session . VBD . plug ( vbd_ref , session ) #NEWLINE# try : #NEWLINE# #INDENT# LOG . debug ( 'Plugging VBD %s done.' , vbd_ref ) #NEWLINE# orig_dev = session . call_xenapi ( 'VBD.get_device' , vbd_ref ) #NEWLINE# LOG . debug ( 'VBD %(vbd_ref)s plugged as %(orig_dev)s' , { #NEWLINE# #INDENT# 'vbd_ref' : vbd_ref , #NEWLINE# 'orig_dev' : orig_dev , #NEWLINE# #UNINDENT# } ) #NEWLINE# dev = _remap_vbd_dev ( orig_dev ) #NEWLINE# if ( dev != orig_dev ) : #NEWLINE# #INDENT# LOG . debug ( 'VBD %(vbd_ref)s plugged into wrong dev, remapping to %(dev)s' , { #NEWLINE# #INDENT# 'vbd_ref' : vbd_ref , #NEWLINE# 'dev' : dev , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# _wait_for_device ( dev ) #NEWLINE# ( yield dev ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# utils . execute ( 'sync' , run_as_root = True ) #NEWLINE# LOG . debug ( 'Destroying VBD for VDI %s ... ' , vdi_ref ) #NEWLINE# unplug_vbd ( session , vbd_ref , this_vm_ref ) #NEWLINE# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# destroy_vbd ( session , vbd_ref ) #NEWLINE# #UNINDENT# except exception . StorageError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# LOG . debug ( 'Destroying VBD for VDI %s done.' , vdi_ref )