<s> from oslo_concurrency import processutils <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from oslo_utils import units <EOL> import six <EOL> import nova . conf <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova . i18n import _LW <EOL> from nova . virt . libvirt import utils <EOL> lvm_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> choices = ( '<STR_LIT:none>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:0> , <EOL> help = '<STR_LIT>' ) , <EOL> ] <EOL> CONF = nova . conf . CONF <EOL> CONF . register_opts ( lvm_opts , '<STR_LIT>' ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def create_volume ( vg , lv , size , sparse = False ) : <EOL> """<STR_LIT>""" <EOL> vg_info = get_volume_group_info ( vg ) <EOL> free_space = vg_info [ '<STR_LIT>' ] <EOL> def check_size ( vg , lv , size ) : <EOL> if size > free_space : <EOL> raise RuntimeError ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % <EOL> { '<STR_LIT>' : vg , <EOL> '<STR_LIT>' : free_space , <EOL> '<STR_LIT:size>' : size , <EOL> '<STR_LIT>' : lv } ) <EOL> if sparse : <EOL> preallocated_space = <NUM_LIT:64> * units . Mi <EOL> check_size ( vg , lv , preallocated_space ) <EOL> if free_space < size : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : vg , <EOL> '<STR_LIT>' : free_space , <EOL> '<STR_LIT:size>' : size , <EOL> '<STR_LIT>' : lv } ) <EOL> cmd = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' % preallocated_space , <EOL> '<STR_LIT>' , '<STR_LIT>' % size , '<STR_LIT>' , lv , vg ) <EOL> else : <EOL> check_size ( vg , lv , size ) <EOL> cmd = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' % size , '<STR_LIT>' , lv , vg ) <EOL> utils . execute ( * cmd , run_as_root = True , attempts = <NUM_LIT:3> ) <EOL> def get_volume_group_info ( vg ) : <EOL> """<STR_LIT>""" <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:|>' , <EOL> '<STR_LIT>' , '<STR_LIT:b>' , '<STR_LIT>' , '<STR_LIT>' , vg , <EOL> run_as_root = True ) <EOL> info = out . split ( '<STR_LIT:|>' ) <EOL> if len ( info ) != <NUM_LIT:2> : <EOL> raise RuntimeError ( _ ( "<STR_LIT>" ) % vg ) <EOL> return { '<STR_LIT>' : int ( info [ <NUM_LIT:0> ] ) , <EOL> '<STR_LIT>' : int ( info [ <NUM_LIT:1> ] ) , <EOL> '<STR_LIT>' : int ( info [ <NUM_LIT:0> ] ) - int ( info [ <NUM_LIT:1> ] ) } <EOL> def list_volumes ( vg ) : <EOL> """<STR_LIT>""" <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , vg , <EOL> run_as_root = True ) <EOL> return [ line . strip ( ) for line in out . splitlines ( ) ] <EOL> def volume_info ( path ) : <EOL> """<STR_LIT>""" <EOL> out , err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:|>' , path , run_as_root = True ) <EOL> info = [ line . split ( '<STR_LIT:|>' ) for line in out . splitlines ( ) ] <EOL> if len ( info ) != <NUM_LIT:2> : <EOL> raise RuntimeError ( _ ( "<STR_LIT>" ) % path ) <EOL> return dict ( zip ( * info ) ) <EOL> def get_volume_size ( path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> out , _err = utils . execute ( '<STR_LIT>' , '<STR_LIT>' , path , <EOL> run_as_root = True ) <EOL> except processutils . ProcessExecutionError : <EOL> if not utils . path_exists ( path ) : <EOL> raise exception . VolumeBDMPathNotFound ( path = path ) <EOL> else : <EOL> raise <EOL> return int ( out ) <EOL> def _zero_volume ( path , volume_size ) : <EOL> """<STR_LIT>""" <EOL> bs = units . Mi <EOL> direct_flags = ( '<STR_LIT>' , ) <EOL> sync_flags = ( ) <EOL> remaining_bytes = volume_size <EOL> while remaining_bytes : <EOL> zero_blocks = remaining_bytes / bs <EOL> seek_blocks = ( volume_size - remaining_bytes ) / bs <EOL> zero_cmd = ( '<STR_LIT>' , '<STR_LIT>' % bs , <EOL> '<STR_LIT>' , '<STR_LIT>' % path , <EOL> '<STR_LIT>' % seek_blocks , '<STR_LIT>' % zero_blocks ) <EOL> zero_cmd += direct_flags <EOL> zero_cmd += sync_flags <EOL> if zero_blocks : <EOL> utils . execute ( * zero_cmd , run_as_root = True ) <EOL> remaining_bytes %= bs <EOL> bs /= units . Ki <EOL> direct_flags = ( ) <EOL> sync_flags = ( '<STR_LIT>' , ) <EOL> def clear_volume ( path ) : <EOL> """<STR_LIT>""" <EOL> volume_clear = CONF . libvirt . volume_clear <EOL> if volume_clear == '<STR_LIT:none>' : <EOL> return <EOL> volume_clear_size = int ( CONF . libvirt . volume_clear_size ) * units . Mi <EOL> try : <EOL> volume_size = get_volume_size ( path ) <EOL> except exception . VolumeBDMPathNotFound : <EOL> LOG . warning ( _LW ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:path>' : path } ) <EOL> return <EOL> if volume_clear_size != <NUM_LIT:0> and volume_clear_size < volume_size : <EOL> volume_size = volume_clear_size <EOL> if volume_clear == '<STR_LIT>' : <EOL> _zero_volume ( path , volume_size ) <EOL> elif volume_clear == '<STR_LIT>' : <EOL> utils . execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' % volume_size , path , <EOL> run_as_root = True ) <EOL> def remove_volumes ( paths ) : <EOL> """<STR_LIT>""" <EOL> errors = [ ] <EOL> for path in paths : <EOL> clear_volume ( path ) <EOL> lvremove = ( '<STR_LIT>' , '<STR_LIT>' , path ) <EOL> try : <EOL> utils . execute ( * lvremove , attempts = <NUM_LIT:3> , run_as_root = True ) <EOL> except processutils . ProcessExecutionError as exp : <EOL> errors . append ( six . text_type ( exp ) ) <EOL> if errors : <EOL> raise exception . VolumesNotRemoved ( reason = ( '<STR_LIT:U+002CU+0020>' ) . join ( errors ) ) </s>
