#NEWLINE# @ contextlib . contextmanager #NEWLINE# def _dummy_vm( session , instance , vdi_ref ) : #NEWLINE# #INDENT# "This creates a temporary VM so that we can snapshot a VDI.\n\n    VDI's can't be snapshotted directly since the API expects a `vm_ref`. To\n    work around this, we need to create a temporary VM and then map the VDI to\n    the VM using a temporary VBD.\n    " #NEWLINE# name_label = 'dummy' #NEWLINE# vm_ref = create_vm ( session , instance , name_label , None , None ) #NEWLINE# try : #NEWLINE# #INDENT# vbd_ref = create_vbd ( session , vdi_ref , vdi_ref , 'autodetect' , read_only = True ) #NEWLINE# try : #NEWLINE# #INDENT# ( yield vm_ref ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# destroy_vbd ( session , vbd_ref ) #NEWLINE# #UNINDENT# except exception . StorageError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# destroy_vm ( session , instance , vm_ref )