<s> """<STR_LIT>""" <EOL> import os <EOL> import random <EOL> import re <EOL> import time <EOL> from oslo_log import log as logging <EOL> import nova . conf <EOL> from nova . i18n import _ , _LE , _LI , _LW <EOL> from nova import utils <EOL> from nova . virt . disk . mount import api <EOL> LOG = logging . getLogger ( __name__ ) <EOL> CONF = nova . conf . CONF <EOL> NBD_DEVICE_RE = re . compile ( '<STR_LIT>' ) <EOL> class NbdMount ( api . Mount ) : <EOL> """<STR_LIT>""" <EOL> mode = '<STR_LIT>' <EOL> def _detect_nbd_devices ( self ) : <EOL> """<STR_LIT>""" <EOL> return filter ( NBD_DEVICE_RE . match , os . listdir ( '<STR_LIT>' ) ) <EOL> def _find_unused ( self , devices ) : <EOL> for device in devices : <EOL> if not os . path . exists ( os . path . join ( '<STR_LIT>' , device , '<STR_LIT>' ) ) : <EOL> if not os . path . exists ( '<STR_LIT>' % device ) : <EOL> return device <EOL> else : <EOL> LOG . error ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , device ) <EOL> LOG . warning ( _LW ( '<STR_LIT>' ) ) <EOL> return None <EOL> def _allocate_nbd ( self ) : <EOL> if not os . path . exists ( '<STR_LIT>' ) : <EOL> LOG . error ( _LE ( '<STR_LIT>' ) ) <EOL> self . error = _ ( '<STR_LIT>' ) <EOL> return None <EOL> devices = self . _detect_nbd_devices ( ) <EOL> random . shuffle ( devices ) <EOL> device = self . _find_unused ( devices ) <EOL> if not device : <EOL> self . error = _ ( '<STR_LIT>' ) <EOL> return None <EOL> return os . path . join ( '<STR_LIT>' , device ) <EOL> @ utils . synchronized ( '<STR_LIT>' ) <EOL> def _inner_get_dev ( self ) : <EOL> device = self . _allocate_nbd ( ) <EOL> if not device : <EOL> return False <EOL> LOG . debug ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : device , '<STR_LIT>' : self . image . path } ) <EOL> _out , err = utils . trycmd ( '<STR_LIT>' , '<STR_LIT:-c>' , device , <EOL> self . image . path , <EOL> run_as_root = True ) <EOL> if err : <EOL> self . error = _ ( '<STR_LIT>' ) % err <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , self . error ) <EOL> return False <EOL> pidfile = "<STR_LIT>" % os . path . basename ( device ) <EOL> for _i in range ( CONF . timeout_nbd ) : <EOL> if os . path . exists ( pidfile ) : <EOL> self . device = device <EOL> break <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> else : <EOL> self . error = _ ( '<STR_LIT>' ) % device <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , self . error ) <EOL> _out , err = utils . trycmd ( '<STR_LIT>' , '<STR_LIT>' , device , <EOL> run_as_root = True ) <EOL> if err : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , err ) <EOL> return False <EOL> self . error = '<STR_LIT>' <EOL> self . linked = True <EOL> return True <EOL> def get_dev ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_dev_retry_helper ( ) <EOL> def unget_dev ( self ) : <EOL> if not self . linked : <EOL> return <EOL> LOG . debug ( '<STR_LIT>' , self . device ) <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , self . device , run_as_root = True ) <EOL> self . linked = False <EOL> self . device = None <EOL> def flush_dev ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . device : <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , <EOL> self . device , run_as_root = True ) </s>
