<s> import errno <EOL> import os <EOL> import re <EOL> from lxml import etree <EOL> from oslo_concurrency import processutils <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> from nova . compute import arch <EOL> from nova . compute import vm_mode <EOL> import nova . conf <EOL> from nova . i18n import _ <EOL> from nova . i18n import _LI <EOL> from nova import utils <EOL> from nova . virt import images <EOL> from nova . virt . libvirt import config as vconfig <EOL> from nova . virt . libvirt . volume import remotefs <EOL> from nova . virt import volumeutils <EOL> libvirt_opts = [ <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> CONF = nova . conf . CONF <EOL> CONF . register_opts ( libvirt_opts , '<STR_LIT>' ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> RESIZE_SNAPSHOT_NAME = '<STR_LIT>' <EOL> def execute ( * args , ** kwargs ) : <EOL> return utils . execute ( * args , ** kwargs ) <EOL> def get_iscsi_initiator ( ) : <EOL> return volumeutils . get_iscsi_initiator ( ) <EOL> def create_image ( disk_format , path , size ) : <EOL> """<STR_LIT>""" <EOL> execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , disk_format , path , size ) <EOL> def create_cow_image ( backing_file , path , size = None ) : <EOL> """<STR_LIT>""" <EOL> base_cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> cow_opts = [ ] <EOL> if backing_file : <EOL> cow_opts += [ '<STR_LIT>' % backing_file ] <EOL> base_details = images . qemu_img_info ( backing_file ) <EOL> else : <EOL> base_details = None <EOL> if base_details and base_details . cluster_size is not None : <EOL> cow_opts += [ '<STR_LIT>' % base_details . cluster_size ] <EOL> if size is not None : <EOL> cow_opts += [ '<STR_LIT>' % size ] <EOL> if cow_opts : <EOL> csv_opts = "<STR_LIT:U+002C>" . join ( cow_opts ) <EOL> cow_opts = [ '<STR_LIT>' , csv_opts ] <EOL> cmd = base_cmd + cow_opts + [ path ] <EOL> execute ( * cmd ) <EOL> def pick_disk_driver_name ( hypervisor_version , is_block_dev = False ) : <EOL> """<STR_LIT>""" <EOL> if CONF . libvirt . virt_type == "<STR_LIT>" : <EOL> if is_block_dev : <EOL> return "<STR_LIT>" <EOL> else : <EOL> if hypervisor_version >= <NUM_LIT> : <EOL> try : <EOL> execute ( '<STR_LIT>' , '<STR_LIT:status>' , <EOL> run_as_root = True , check_exit_code = True ) <EOL> except OSError as exc : <EOL> if exc . errno == errno . ENOENT : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> return '<STR_LIT>' <EOL> else : <EOL> raise <EOL> except processutils . ProcessExecutionError : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> return '<STR_LIT>' <EOL> try : <EOL> out , err = execute ( '<STR_LIT>' , '<STR_LIT>' , check_exit_code = False ) <EOL> if out == '<STR_LIT>' : <EOL> if hypervisor_version > <NUM_LIT> : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> else : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , out ) <EOL> except OSError as exc : <EOL> if exc . errno == errno . ENOENT : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> else : <EOL> raise <EOL> return "<STR_LIT:file>" <EOL> elif CONF . libvirt . virt_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return None <EOL> def get_disk_size ( path , format = None ) : <EOL> """<STR_LIT>""" <EOL> size = images . qemu_img_info ( path , format ) . virtual_size <EOL> return int ( size ) <EOL> def get_disk_backing_file ( path , basename = True , format = None ) : <EOL> """<STR_LIT>""" <EOL> backing_file = images . qemu_img_info ( path , format ) . backing_file <EOL> if backing_file and basename : <EOL> backing_file = os . path . basename ( backing_file ) <EOL> return backing_file <EOL> def copy_image ( src , dest , host = None , receive = False , <EOL> on_execute = None , on_completion = None , <EOL> compression = True ) : <EOL> """<STR_LIT>""" <EOL> if not host : <EOL> execute ( '<STR_LIT>' , src , dest ) <EOL> else : <EOL> if receive : <EOL> src = "<STR_LIT>" % ( utils . safe_ip_format ( host ) , src ) <EOL> else : <EOL> dest = "<STR_LIT>" % ( utils . safe_ip_format ( host ) , dest ) <EOL> remote_filesystem_driver = remotefs . RemoteFilesystem ( ) <EOL> remote_filesystem_driver . copy_file ( src , dest , <EOL> on_execute = on_execute , on_completion = on_completion , <EOL> compression = compression ) <EOL> def write_to_file ( path , contents , umask = None ) : <EOL> """<STR_LIT>""" <EOL> if umask : <EOL> saved_umask = os . umask ( umask ) <EOL> try : <EOL> with open ( path , '<STR_LIT:w>' ) as f : <EOL> f . write ( contents ) <EOL> finally : <EOL> if umask : <EOL> os . umask ( saved_umask ) <EOL> def chown ( path , owner ) : <EOL> """<STR_LIT>""" <EOL> execute ( '<STR_LIT>' , owner , path , run_as_root = True ) <EOL> def update_mtime ( path ) : <EOL> """<STR_LIT>""" <EOL> execute ( '<STR_LIT>' , '<STR_LIT:-c>' , path , run_as_root = True ) <EOL> def _id_map_to_config ( id_map ) : <EOL> return "<STR_LIT>" % ( id_map . start , id_map . target , id_map . count ) <EOL> def chown_for_id_maps ( path , id_maps ) : <EOL> """<STR_LIT>""" <EOL> uid_maps_str = '<STR_LIT:U+002C>' . join ( [ _id_map_to_config ( id_map ) for id_map in id_maps if <EOL> isinstance ( id_map , <EOL> vconfig . LibvirtConfigGuestUIDMap ) ] ) <EOL> gid_maps_str = '<STR_LIT:U+002C>' . join ( [ _id_map_to_config ( id_map ) for id_map in id_maps if <EOL> isinstance ( id_map , <EOL> vconfig . LibvirtConfigGuestGIDMap ) ] ) <EOL> execute ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , uid_maps_str , <EOL> '<STR_LIT>' , gid_maps_str , path , run_as_root = True ) <EOL> def extract_snapshot ( disk_path , source_fmt , out_path , dest_fmt ) : <EOL> """<STR_LIT>""" <EOL> if dest_fmt == '<STR_LIT>' : <EOL> dest_fmt = '<STR_LIT>' <EOL> if dest_fmt == '<STR_LIT>' : <EOL> dest_fmt = '<STR_LIT>' <EOL> qemu_img_cmd = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , source_fmt , '<STR_LIT>' , dest_fmt ) <EOL> if CONF . libvirt . snapshot_compression and dest_fmt == "<STR_LIT>" : <EOL> qemu_img_cmd += ( '<STR_LIT:-c>' , ) <EOL> qemu_img_cmd += ( disk_path , out_path ) <EOL> execute ( * qemu_img_cmd ) <EOL> def load_file ( path ) : <EOL> """<STR_LIT>""" <EOL> with open ( path , '<STR_LIT:r>' ) as fp : <EOL> return fp . read ( ) <EOL> def file_open ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return open ( * args , ** kwargs ) <EOL> def file_delete ( path ) : <EOL> """<STR_LIT>""" <EOL> return os . unlink ( path ) <EOL> def path_exists ( path ) : <EOL> """<STR_LIT>""" <EOL> return os . path . exists ( path ) <EOL> def find_disk ( virt_dom ) : <EOL> """<STR_LIT>""" <EOL> xml_desc = virt_dom . XMLDesc ( <NUM_LIT:0> ) <EOL> domain = etree . fromstring ( xml_desc ) <EOL> os_type = domain . find ( '<STR_LIT>' ) . text <EOL> driver = None <EOL> if CONF . libvirt . virt_type == '<STR_LIT>' : <EOL> filesystem = domain . find ( '<STR_LIT>' ) <EOL> driver = filesystem . find ( '<STR_LIT>' ) <EOL> source = filesystem . find ( '<STR_LIT:source>' ) <EOL> disk_path = source . get ( '<STR_LIT>' ) <EOL> disk_path = disk_path [ <NUM_LIT:0> : disk_path . rfind ( '<STR_LIT>' ) ] <EOL> disk_path = os . path . join ( disk_path , '<STR_LIT>' ) <EOL> elif CONF . libvirt . virt_type == '<STR_LIT>' and os_type == vm_mode . EXE : <EOL> filesystem = domain . find ( '<STR_LIT>' ) <EOL> driver = filesystem . find ( '<STR_LIT>' ) <EOL> source = filesystem . find ( '<STR_LIT:source>' ) <EOL> disk_path = source . get ( '<STR_LIT:file>' ) <EOL> else : <EOL> disk = domain . find ( '<STR_LIT>' ) <EOL> driver = disk . find ( '<STR_LIT>' ) <EOL> source = disk . find ( '<STR_LIT:source>' ) <EOL> disk_path = source . get ( '<STR_LIT:file>' ) or source . get ( '<STR_LIT>' ) <EOL> if not disk_path and CONF . libvirt . images_type == '<STR_LIT>' : <EOL> disk_path = source . get ( '<STR_LIT:name>' ) <EOL> if disk_path : <EOL> disk_path = '<STR_LIT>' + disk_path <EOL> if not disk_path : <EOL> raise RuntimeError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if driver is not None : <EOL> format = driver . get ( '<STR_LIT:type>' ) <EOL> if format == '<STR_LIT>' : <EOL> format = '<STR_LIT>' <EOL> else : <EOL> format = None <EOL> return ( disk_path , format ) <EOL> def get_disk_type_from_path ( path ) : <EOL> """<STR_LIT>""" <EOL> if path . startswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> elif path . startswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' <EOL> elif ( os . path . isdir ( path ) and <EOL> os . path . exists ( os . path . join ( path , "<STR_LIT>" ) ) ) : <EOL> return '<STR_LIT>' <EOL> return None <EOL> def get_fs_info ( path ) : <EOL> """<STR_LIT>""" <EOL> hddinfo = os . statvfs ( path ) <EOL> total = hddinfo . f_frsize * hddinfo . f_blocks <EOL> free = hddinfo . f_frsize * hddinfo . f_bavail <EOL> used = hddinfo . f_frsize * ( hddinfo . f_blocks - hddinfo . f_bfree ) <EOL> return { '<STR_LIT>' : total , <EOL> '<STR_LIT>' : free , <EOL> '<STR_LIT>' : used } <EOL> def fetch_image ( context , target , image_id , user_id , project_id , max_size = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> images . fetch_to_raw ( context , image_id , target , user_id , project_id , <EOL> max_size = max_size ) <EOL> def fetch_raw_image ( context , target , image_id , user_id , project_id , <EOL> max_size = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> images . fetch ( context , image_id , target , user_id , project_id , <EOL> max_size = max_size ) <EOL> def get_instance_path ( instance , forceold = False , relative = False ) : <EOL> """<STR_LIT>""" <EOL> pre_grizzly_name = os . path . join ( CONF . instances_path , instance . name ) <EOL> if forceold or os . path . exists ( pre_grizzly_name ) : <EOL> if relative : <EOL> return instance . name <EOL> return pre_grizzly_name <EOL> if relative : <EOL> return instance . uuid <EOL> return os . path . join ( CONF . instances_path , instance . uuid ) <EOL> def get_instance_path_at_destination ( instance , migrate_data = None ) : <EOL> """<STR_LIT>""" <EOL> instance_relative_path = None <EOL> if migrate_data : <EOL> instance_relative_path = migrate_data . instance_relative_path <EOL> if instance_relative_path : <EOL> instance_dir = os . path . join ( CONF . instances_path , <EOL> instance_relative_path ) <EOL> else : <EOL> instance_dir = get_instance_path ( instance ) <EOL> return instance_dir <EOL> def get_arch ( image_meta ) : <EOL> """<STR_LIT>""" <EOL> if image_meta : <EOL> image_arch = image_meta . properties . get ( '<STR_LIT>' ) <EOL> if image_arch is not None : <EOL> return image_arch <EOL> return arch . from_host ( ) <EOL> def is_mounted ( mount_path , source = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> check_cmd = [ '<STR_LIT>' , '<STR_LIT>' , mount_path ] <EOL> if source : <EOL> check_cmd . extend ( [ '<STR_LIT>' , source ] ) <EOL> utils . execute ( * check_cmd ) <EOL> return True <EOL> except processutils . ProcessExecutionError : <EOL> return False <EOL> except OSError as exc : <EOL> if exc . errno == errno . ENOENT : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) ) <EOL> return False <EOL> def is_valid_hostname ( hostname ) : <EOL> return re . match ( r"<STR_LIT>" , hostname ) </s>
