def unplug_vbd(session, vbd_ref):
 max_attempts = (CONF.xenapi_num_vbd_unplug_retries + 1)
 for num_attempt in xrange(1, (max_attempts + 1)):
  try:
   session.call_xenapi('VBD.unplug', vbd_ref)
   return
  except session.XenAPI.Failure as exc:
   err = ((len(exc.details) > 0) and exc.details[0])
   if (err == 'DEVICE_ALREADY_DETACHED'):
    LOG.info(_('VBD %s already detached'), vbd_ref)
    return
   elif (err == 'DEVICE_DETACH_REJECTED'):
    LOG.info(_('VBD %(vbd_ref)s detach rejected, attempt %(num_attempt)d/%(max_attempts)d'), locals())
   else:
    LOG.exception(exc)
    raise volume_utils.StorageError((_('Unable to unplug VBD %s') % vbd_ref))
  greenthread.sleep(1)
 raise volume_utils.StorageError((_('Reached maximum number of retries trying to unplug VBD %s') % vbd_ref))

def unplug_vbd ( session , vbd_ref ) : max_attempts = ( CONF . xenapi_num_vbd_unplug_retries + 1 ) for num_attempt in xrange ( 1 , ( max_attempts + 1 ) ) : try : session . call_xenapi ( 'VBD.unplug' , vbd_ref ) return except session . XenAPI . Failure as exc : err = ( ( len ( exc . details ) > 0 ) and exc . details [ 0 ] ) if ( err == 'DEVICE_ALREADY_DETACHED' ) : LOG . info ( _ ( 'VBD %s already detached' ) , vbd_ref ) return elif ( err == 'DEVICE_DETACH_REJECTED' ) : LOG . info ( _ ( 'VBD %(vbd_ref)s detach rejected, attempt %(num_attempt)d/%(max_attempts)d' ) , locals ( ) ) else : LOG . exception ( exc ) raise volume_utils . StorageError ( ( _ ( 'Unable to unplug VBD %s' ) % vbd_ref ) ) greenthread . sleep ( 1 ) raise volume_utils . StorageError ( ( _ ( 'Reached maximum number of retries trying to unplug VBD %s' ) % vbd_ref ) )
unplug vbd from vm .