日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Nova 启动虚拟机流程解析

發布時間:2025/3/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nova 启动虚拟机流程解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

文章目錄

  • 目錄
  • 前言
  • 從請求說起
    • nova-api service 階段

前言

Nova 啟動虛擬機的東西太多,持續更新…

從請求說起

無論是通過 Dashboard 還是 CLI 啟動一個虛擬機,發送的是 POST /servers請求,改與該請求的 Body 詳情,可以瀏覽官方文檔 Create server。

nova-api service 階段

Nova API Service 本質是一個 WSGI Application,采用了 Paste + PasteDeploy + Routes + WebOb 框架,簡稱 PPRW。關于架構的實現不屬于本文范疇,所以直接看接收到 POST /servers 請求之后的處理函數(View Method)create。

NOTE:首先需要說明的是,下文中所有的代碼解析均直接通過注釋的方式呈現完成。

# File: /opt/stack/nova/nova/api/openstack/compute/servers.py@wsgi.response(202)@wsgi.expected_errors((400, 403, 409))@validation.schema(schema_servers.base_create_v20, '2.0', '2.0')@validation.schema(schema_servers.base_create, '2.1', '2.18')@validation.schema(schema_servers.base_create_v219, '2.19', '2.31')@validation.schema(schema_servers.base_create_v232, '2.32', '2.32')@validation.schema(schema_servers.base_create_v233, '2.33', '2.36')@validation.schema(schema_servers.base_create_v237, '2.37', '2.41')@validation.schema(schema_servers.base_create_v242, '2.42', '2.51')@validation.schema(schema_servers.base_create_v252, '2.52', '2.56')@validation.schema(schema_servers.base_create_v257, '2.57', '2.62')@validation.schema(schema_servers.base_create_v263, '2.63', '2.66')@validation.schema(schema_servers.base_create_v267, '2.67')def create(self, req, body):"""Creates a new server for a given user."""context = req.environ['nova.context']server_dict = body['server']# 用戶設置的虛擬機密碼password = self._get_server_admin_password(server_dict)name = common.normalize_name(server_dict['name'])description = nameif api_version_request.is_supported(req, min_version='2.19'):description = server_dict.get('description')# Arguments to be passed to instance create functioncreate_kwargs = {}# 需要注入到虛擬機的 user_data 數據(Configuration information or scripts to use upon launch.)create_kwargs['user_data'] = server_dict.get('user_data')# NOTE(alex_xu): The v2.1 API compat mode, we strip the spaces for# keypair create. But we didn't strip spaces at here for# backward-compatible some users already created keypair and name with# leading/trailing spaces by legacy v2 API.# keypair 名稱create_kwargs['key_name'] = server_dict.get('key_name')# 是否啟用 config_drive(布爾值)create_kwargs['config_drive'] = server_dict.get('config_drive')# 虛擬機安全組security_groups = server_dict.get('security_groups')if security_groups is not None:create_kwargs['security_groups'] = [sg['name'] for sg in security_groups if sg.get('name')]create_kwargs['security_groups'] = list(set(create_kwargs['security_groups']))# 虛擬機調度提示信息,是一種高級的調度因子scheduler_hints = {}if 'os:scheduler_hints' in body:scheduler_hints = body['os:scheduler_hints']elif 'OS-SCH-HNT:scheduler_hints' in body:scheduler_hints = body['OS-SCH-HNT:scheduler_hints']create_kwargs['scheduler_hints'] = scheduler_hints# min_count and max_count are optional. If they exist, they may come# in as strings. Verify that they are valid integers and > 0.# Also, we want to default 'min_count' to 1, and default# 'max_count' to be 'min_count'.min_count = int(server_dict.get('min_count', 1))max_count = int(server_dict.get('max_count', min_count))return_id = server_dict.get('return_reservation_id', False)if min_count > max_count:msg = _('min_count must be <= max_count')raise exc.HTTPBadRequest(explanation=msg)create_kwargs['min_count'] = min_countcreate_kwargs['max_count'] = max_countcreate_kwargs['return_reservation_id'] = return_id# 指定可用域availability_zone = server_dict.pop("availability_zone", None)# 虛擬機 tags 信息if api_version_request.is_supported(req, min_version='2.52'):create_kwargs['tags'] = server_dict.get('tags')helpers.translate_attributes(helpers.CREATE,server_dict, create_kwargs)target = {'project_id': context.project_id,'user_id': context.user_id,'availability_zone': availability_zone}# 驗證 target 是否支持 create 操作context.can(server_policies.SERVERS % 'create', target)# Skip policy check for 'create:trusted_certs' if no trusted# certificate IDs were provided.# 鏡像證書trusted_certs = server_dict.get('trusted_image_certificates', None)if trusted_certs:create_kwargs['trusted_certs'] = trusted_certscontext.can(server_policies.SERVERS % 'create:trusted_certs',target=target)# TODO(Shao He, Feng) move this policy check to os-availability-zone# extension after refactor it.parse_az = self.compute_api.parse_availability_zonetry:# 解析 --availability-zone AZ1:Compute1:Hypervisor1 參數availability_zone, host, node = parse_az(context,availability_zone)except exception.InvalidInput as err:raise exc.HTTPBadRequest(explanation=six.text_type(err))if host or node:context.can(server_policies.SERVERS % 'create:forced_host', {})# NOTE(danms): Don't require an answer from all cells here, as# we assume that if a cell isn't reporting we won't schedule into# it anyway. A bit of a gamble, but a reasonable one.min_compute_version = service_obj.get_minimum_version_all_cells(nova_context.get_admin_context(), ['nova-compute'])# 是否支持 device tagging 功能supports_device_tagging = (min_compute_version >=DEVICE_TAGGING_MIN_COMPUTE_VERSION)# 兩個 Boot from volume 的 block device mapping 版本block_device_mapping_legacy = server_dict.get('block_device_mapping',[])block_device_mapping_v2 = server_dict.get('block_device_mapping_v2',[])if block_device_mapping_legacy and block_device_mapping_v2:expl = _('Using different block_device_mapping syntaxes ''is not allowed in the same request.')raise exc.HTTPBadRequest(explanation=expl)if block_device_mapping_legacy:for bdm in block_device_mapping_legacy:if 'delete_on_termination' in bdm:bdm['delete_on_termination'] = strutils.bool_from_string(bdm['delete_on_termination'])create_kwargs['block_device_mapping'] = block_device_mapping_legacy# Sets the legacy_bdm flag if we got a legacy block device mapping.create_kwargs['legacy_bdm'] = Trueelif block_device_mapping_v2:# Have to check whether --image is given, see bug 1433609image_href = server_dict.get('imageRef')image_uuid_specified = image_href is not Nonetry:# Transform the API format of data to the internally used one.# block_device_mapping_v2 的數據結構:# "block_device_mapping_v2": [{# "boot_index": "0",# "uuid": "ac408821-c95a-448f-9292-73986c790911",# "source_type": "image",# "volume_size": "25",# "destination_type": "volume",# "delete_on_termination": true,# "tag": "disk1",# "disk_bus": "scsi"}]block_device_mapping = [block_device.BlockDeviceDict.from_api(bdm_dict,image_uuid_specified)for bdm_dict in block_device_mapping_v2]except exception.InvalidBDMFormat as e:raise exc.HTTPBadRequest(explanation=e.format_message())create_kwargs['block_device_mapping'] = block_device_mapping# Unset the legacy_bdm flag if we got a block device mapping.create_kwargs['legacy_bdm'] = Falseblock_device_mapping = create_kwargs.get("block_device_mapping")if block_device_mapping:# 檢查 target 是否支持 create:attach_volume 操作context.can(server_policies.SERVERS % 'create:attach_volume',target)for bdm in block_device_mapping:if bdm.get('tag', None) and not supports_device_tagging:msg = _('Block device tags are not yet supported.')raise exc.HTTPBadRequest(explanation=msg)# 獲取指定的 image uuid# 如果沒有 image_href 并且存在 block_device_mapping 則返回 '' 空字符串,Boot from volume 就不需要指定 Image 了# 如果有 image_href,則 image_uuid = image_hrefimage_uuid = self._image_from_req_data(server_dict, create_kwargs)# NOTE(cyeoh): Although upper layer can set the value of# return_reservation_id in order to request that a reservation# id be returned to the client instead of the newly created# instance information we do not want to pass this parameter# to the compute create call which always returns both. We use# this flag after the instance create call to determine what# to return to the clientreturn_reservation_id = create_kwargs.pop('return_reservation_id',False)# 通過 networks attribute 創建一個 list of requested networksrequested_networks = server_dict.get('networks', None)if requested_networks is not None:requested_networks = self._get_requested_networks(requested_networks, supports_device_tagging)# Skip policy check for 'create:attach_network' if there is no# network allocation request.if requested_networks and len(requested_networks) and \not requested_networks.no_allocate:context.can(server_policies.SERVERS % 'create:attach_network',target)# 獲取 flavor object(DB)flavor_id = self._flavor_id_from_req_data(body)try:inst_type = flavors.get_flavor_by_flavor_id(flavor_id, ctxt=context, read_deleted="no")# 是否支持 Cinder Mulit-Attachsupports_multiattach = common.supports_multiattach_volume(req)# 跳轉到 Compute API 處理,實際上后來是繼續跳轉到 Conductor 了。(instances, resv_id) = self.compute_api.create(context,inst_type,image_uuid,display_name=name,display_description=description,availability_zone=availability_zone,forced_host=host, forced_node=node,metadata=server_dict.get('metadata', {}),admin_password=password,requested_networks=requested_networks,check_server_group_quota=True,supports_multiattach=supports_multiattach,**create_kwargs)except (exception.QuotaError,exception.PortLimitExceeded) as error:raise exc.HTTPForbidden(explanation=error.format_message())except exception.ImageNotFound:msg = _("Can not find requested image")raise exc.HTTPBadRequest(explanation=msg)except exception.KeypairNotFound:msg = _("Invalid key_name provided.")raise exc.HTTPBadRequest(explanation=msg)except exception.ConfigDriveInvalidValue:msg = _("Invalid config_drive provided.")raise exc.HTTPBadRequest(explanation=msg)except (exception.BootFromVolumeRequiredForZeroDiskFlavor,exception.ExternalNetworkAttachForbidden) as error:raise exc.HTTPForbidden(explanation=error.format_message())except messaging.RemoteError as err:msg = "%(err_type)s: %(err_msg)s" % {'err_type': err.exc_type,'err_msg': err.value}raise exc.HTTPBadRequest(explanation=msg)except UnicodeDecodeError as error:msg = "UnicodeError: %s" % errorraise exc.HTTPBadRequest(explanation=msg)except (exception.CPUThreadPolicyConfigurationInvalid,exception.ImageNotActive,exception.ImageBadRequest,exception.ImageNotAuthorized,exception.FixedIpNotFoundForAddress,exception.FlavorNotFound,exception.FlavorDiskTooSmall,exception.FlavorMemoryTooSmall,exception.InvalidMetadata,exception.InvalidRequest,exception.InvalidVolume,exception.MultiplePortsNotApplicable,exception.InvalidFixedIpAndMaxCountRequest,exception.InstanceUserDataMalformed,exception.PortNotFound,exception.FixedIpAlreadyInUse,exception.SecurityGroupNotFound,exception.PortRequiresFixedIP,exception.NetworkRequiresSubnet,exception.NetworkNotFound,exception.InvalidBDM,exception.InvalidBDMSnapshot,exception.InvalidBDMVolume,exception.InvalidBDMImage,exception.InvalidBDMBootSequence,exception.InvalidBDMLocalsLimit,exception.InvalidBDMVolumeNotBootable,exception.InvalidBDMEphemeralSize,exception.InvalidBDMFormat,exception.InvalidBDMSwapSize,exception.VolumeTypeNotFound,exception.AutoDiskConfigDisabledByImage,exception.ImageCPUPinningForbidden,exception.ImageCPUThreadPolicyForbidden,exception.ImageNUMATopologyIncomplete,exception.ImageNUMATopologyForbidden,exception.ImageNUMATopologyAsymmetric,exception.ImageNUMATopologyCPUOutOfRange,exception.ImageNUMATopologyCPUDuplicates,exception.ImageNUMATopologyCPUsUnassigned,exception.ImageNUMATopologyMemoryOutOfRange,exception.InvalidNUMANodesNumber,exception.InstanceGroupNotFound,exception.MemoryPageSizeInvalid,exception.MemoryPageSizeForbidden,exception.PciRequestAliasNotDefined,exception.RealtimeConfigurationInvalid,exception.RealtimeMaskNotFoundOrInvalid,exception.SnapshotNotFound,exception.UnableToAutoAllocateNetwork,exception.MultiattachNotSupportedOldMicroversion,exception.CertificateValidationFailed) as error:raise exc.HTTPBadRequest(explanation=error.format_message())except (exception.PortInUse,exception.InstanceExists,exception.NetworkAmbiguous,exception.NoUniqueMatch,exception.MultiattachSupportNotYetAvailable,exception.VolumeTypeSupportNotYetAvailable,exception.CertificateValidationNotYetAvailable) as error:raise exc.HTTPConflict(explanation=error.format_message())# If the caller wanted a reservation_id, return itif return_reservation_id:return wsgi.ResponseObject({'reservation_id': resv_id})server = self._view_builder.create(req, instances[0])# Enables returning of the instance password by the relevant server API calls# such as create, rebuild, evacuate, or rescue. if CONF.api.enable_instance_password:server['server']['adminPass'] = passwordrobj = wsgi.ResponseObject(server)return self._add_location(robj)

上述的參數基本上可以與 Dashboard 上的 Form 表單一一對應:

# File: /opt/stack/nova/nova/compute/api.py@hooks.add_hook("create_instance")def create(self, context, instance_type,image_href, kernel_id=None, ramdisk_id=None,min_count=None, max_count=None,display_name=None, display_description=None,key_name=None, key_data=None, security_groups=None,availability_zone=None, forced_host=None, forced_node=None,user_data=None, metadata=None, injected_files=None,admin_password=None, block_device_mapping=None,access_ip_v4=None, access_ip_v6=None, requested_networks=None,config_drive=None, auto_disk_config=None, scheduler_hints=None,legacy_bdm=True, shutdown_terminate=False,check_server_group_quota=False, tags=None,supports_multiattach=False, trusted_certs=None):"""Provision instances, sending instance information to thescheduler. The scheduler will determine where the instance(s)go and will handle creating the DB entries.Returns a tuple of (instances, reservation_id)"""if requested_networks and max_count is not None and max_count > 1:# 驗證不能指定一個 IP 地址來創建多個虛擬機self._check_multiple_instances_with_specified_ip(requested_networks)if utils.is_neutron():# # 驗證不能指定一個 Port 來創建多個虛擬機self._check_multiple_instances_with_neutron_ports(requested_networks)# 驗證指定的 AZ 是否為可用域if availability_zone:available_zones = availability_zones.\get_availability_zones(context.elevated(), True)if forced_host is None and availability_zone not in \available_zones:msg = _('The requested availability zone is not available')raise exception.InvalidRequest(msg)# filter_properties 就是一個 Dict 類型對象,現在包含了實參的列表的內容。filter_properties = scheduler_utils.build_filter_properties(scheduler_hints, forced_host, forced_node, instance_type)return self._create_instance(context, instance_type,image_href, kernel_id, ramdisk_id,min_count, max_count,display_name, display_description,key_name, key_data, security_groups,availability_zone, user_data, metadata,injected_files, admin_password,access_ip_v4, access_ip_v6,requested_networks, config_drive,block_device_mapping, auto_disk_config,filter_properties=filter_properties,legacy_bdm=legacy_bdm,shutdown_terminate=shutdown_terminate,check_server_group_quota=check_server_group_quota,tags=tags, supports_multiattach=supports_multiattach,trusted_certs=trusted_certs)def _create_instance(self, context, instance_type,image_href, kernel_id, ramdisk_id,min_count, max_count,display_name, display_description,key_name, key_data, security_groups,availability_zone, user_data, metadata, injected_files,admin_password, access_ip_v4, access_ip_v6,requested_networks, config_drive,block_device_mapping, auto_disk_config, filter_properties,reservation_id=None, legacy_bdm=True, shutdown_terminate=False,check_server_group_quota=False, tags=None,supports_multiattach=False, trusted_certs=None):"""Verify all the input parameters regardless of the provisioningstrategy being performed and schedule the instance(s) forcreation."""# Normalize and setup some parametersif reservation_id is None:reservation_id = utils.generate_uid('r')security_groups = security_groups or ['default']min_count = min_count or 1max_count = max_count or min_countblock_device_mapping = block_device_mapping or []tags = tags or []if image_href:# 獲取 Image Id 和 Image Metadataimage_id, boot_meta = self._get_image(context, image_href)else:# This is similar to the logic in _retrieve_trusted_certs_object.if (trusted_certs or(CONF.glance.verify_glance_signatures andCONF.glance.enable_certificate_validation andCONF.glance.default_trusted_certificate_ids)):msg = _("Image certificate validation is not supported ""when booting from volume")raise exception.CertificateValidationFailed(message=msg)image_id = None# 如果沒有指定 Image 的話就獲取 block device 的 Metadataboot_meta = self._get_bdm_image_metadata(context, block_device_mapping, legacy_bdm)# ???self._check_auto_disk_config(image=boot_meta,auto_disk_config=auto_disk_config)# 進一步驗證和轉換虛擬機創建參數base_options, max_net_count, key_pair, security_groups, \network_metadata = self._validate_and_build_base_options(context, instance_type, boot_meta, image_href, image_id,kernel_id, ramdisk_id, display_name, display_description,key_name, key_data, security_groups, availability_zone,user_data, metadata, access_ip_v4, access_ip_v6,requested_networks, config_drive, auto_disk_config,reservation_id, max_count)# max_net_count is the maximum number of instances requested by the# user adjusted for any network quota constraints, including# consideration of connections to each requested network# 如果最大的 network 數量小于最小的虛擬機數量,則觸發異常if max_net_count < min_count:raise exception.PortLimitExceeded()elif max_net_count < max_count:LOG.info("max count reduced from %(max_count)d to ""%(max_net_count)d due to network port quota",{'max_count': max_count,'max_net_count': max_net_count})max_count = max_net_count# 進一步檢查 Boot from volume 的 block device 的可用性block_device_mapping = self._check_and_transform_bdm(context,base_options, instance_type, boot_meta, min_count, max_count,block_device_mapping, legacy_bdm)# We can't do this check earlier because we need bdms from all sources# to have been merged in order to get the root bdm.# 進一步檢查 quota 和 image 的可用性self._checks_for_create_and_rebuild(context, image_id, boot_meta,instance_type, metadata, injected_files,block_device_mapping.root_bdm())# 轉換為多個虛擬機為 InstanceGroup 數據結構instance_group = self._get_requested_instance_group(context,filter_properties)# 轉換為 TagList 數據結構tags = self._create_tag_list_obj(context, tags)# 將諸多創建參數封裝到 instances_to_build 列表類型對象中instances_to_build = self._provision_instances(context, instance_type, min_count, max_count, base_options,boot_meta, security_groups, block_device_mapping,shutdown_terminate, instance_group, check_server_group_quota,filter_properties, key_pair, tags, trusted_certs,supports_multiattach, network_metadata)instances = []request_specs = []build_requests = []for rs, build_request, im in instances_to_build:build_requests.append(build_request)instance = build_request.get_new_instance(context)instances.append(instance)request_specs.append(rs)if CONF.cells.enable:# NOTE(danms): CellsV1 can't do the new thing, so we# do the old thing here. We can remove this path once# we stop supporting v1.for instance in instances:# 創建 Instance 數據庫對象instance.create()# NOTE(melwitt): We recheck the quota after creating the objects# to prevent users from allocating more resources than their# allowed quota in the event of a race. This is configurable# because it can be expensive if strict quota limits are not# required in a deployment.# 再一次檢查資源配額if CONF.quota.recheck_quota:try:compute_utils.check_num_instances_quota(context, instance_type, 0, 0,orig_num_req=len(instances))except exception.TooManyInstances:with excutils.save_and_reraise_exception():# Need to clean up all the instances we created# along with the build requests, request specs,# and instance mappings.self._cleanup_build_artifacts(instances,instances_to_build)self.compute_task_api.build_instances(context,instances=instances, image=boot_meta,filter_properties=filter_properties,admin_password=admin_password,injected_files=injected_files,requested_networks=requested_networks,security_groups=security_groups,block_device_mapping=block_device_mapping,legacy_bdm=False)else:self.compute_task_api.schedule_and_build_instances(context,build_requests=build_requests,request_spec=request_specs,image=boot_meta,admin_password=admin_password,injected_files=injected_files,requested_networks=requested_networks,block_device_mapping=block_device_mapping,tags=tags)return instances, reservation_iddef _validate_and_build_base_options(self, context, instance_type,boot_meta, image_href, image_id,kernel_id, ramdisk_id, display_name,display_description, key_name,key_data, security_groups,availability_zone, user_data,metadata, access_ip_v4, access_ip_v6,requested_networks, config_drive,auto_disk_config, reservation_id,max_count):"""Verify all the input parameters regardless of the provisioningstrategy being performed."""# 確定 Flavor 可用if instance_type['disabled']:raise exception.FlavorNotFound(flavor_id=instance_type['id'])# 確定 user_data 數據編碼格式為 Base64if user_data:try:base64utils.decode_as_bytes(user_data)except TypeError:raise exception.InstanceUserDataMalformed()# When using Neutron, _check_requested_secgroups will translate and# return any requested security group names to uuids.security_groups = (self._check_requested_secgroups(context, security_groups))# Note: max_count is the number of instances requested by the user,# max_network_count is the maximum number of instances taking into# account any network quotasmax_network_count = self._check_requested_networks(context,requested_networks, max_count)# Choose kernel and ramdisk appropriate for the instance.# ramdisk(虛擬內存盤)是一種將內存模擬成硬盤來使用的技術,可以在內存啟動虛擬機。kernel_id, ramdisk_id = self._handle_kernel_and_ramdisk(context, kernel_id, ramdisk_id, boot_meta)# 依舊返回是布爾值類型config_drive = self._check_config_drive(config_drive)# 通過 keypair name 獲取 keypair dataif key_data is None and key_name is not None:key_pair = objects.KeyPair.get_by_name(context,context.user_id,key_name)key_data = key_pair.public_keyelse:key_pair = None# 獲取系統盤設備的名稱(可能從 Image 獲取或者從 Volume 獲取)root_device_name = block_device.prepend_dev(block_device.properties_root_device_name(boot_meta.get('properties', {})))try:# 此處的 image 是一個抽象概念,表示啟動操作系統的系統盤。將 Image File 和 Volume 統一為 ImageMeta 對象。image_meta = objects.ImageMeta.from_dict(boot_meta)except ValueError as e:# there must be invalid values in the image meta properties so# consider this an invalid requestmsg = _('Invalid image metadata. Error: %s') % six.text_type(e)raise exception.InvalidRequest(msg)# 從 Flavor properties 和 Image properties 獲取 NUMA 屬性numa_topology = hardware.numa_get_constraints(instance_type, image_meta)system_metadata = {}# PCI requests come from two sources: instance flavor and# requested_networks. The first call in below returns an# InstancePCIRequests object which is a list of InstancePCIRequest# objects. The second call in below creates an InstancePCIRequest# object for each SR-IOV port, and append it to the list in the# InstancePCIRequests object# PCI requests come from two sources: instance flavor and requested_networks. # 從 Flavor 獲取 PCI 設備pci_request_info = pci_request.get_pci_requests_from_flavor(instance_type)# ???network_metadata = self.network_api.create_resource_requests(context, requested_networks, pci_request_info)# base_options 的數據基本就是 instances 數據庫表的屬性。base_options = {'reservation_id': reservation_id,'image_ref': image_href,'kernel_id': kernel_id or '','ramdisk_id': ramdisk_id or '','power_state': power_state.NOSTATE,'vm_state': vm_states.BUILDING,'config_drive': config_drive,'user_id': context.user_id,'project_id': context.project_id,'instance_type_id': instance_type['id'],'memory_mb': instance_type['memory_mb'],'vcpus': instance_type['vcpus'],'root_gb': instance_type['root_gb'],'ephemeral_gb': instance_type['ephemeral_gb'],'display_name': display_name,'display_description': display_description,'user_data': user_data,'key_name': key_name,'key_data': key_data,'locked': False,'metadata': metadata or {},'access_ip_v4': access_ip_v4,'access_ip_v6': access_ip_v6,'availability_zone': availability_zone,'root_device_name': root_device_name,'progress': 0,'pci_requests': pci_request_info,'numa_topology': numa_topology,'system_metadata': system_metadata}options_from_image = self._inherit_properties_from_image(boot_meta, auto_disk_config)base_options.update(options_from_image)# return the validated options and maximum number of instances allowed# by the network quotasreturn (base_options, max_network_count, key_pair, security_groups,network_metadata)def _provision_instances(self, context, instance_type, min_count,max_count, base_options, boot_meta, security_groups,block_device_mapping, shutdown_terminate,instance_group, check_server_group_quota, filter_properties,key_pair, tags, trusted_certs, supports_multiattach,network_metadata=None):# Check quotasnum_instances = compute_utils.check_num_instances_quota(context, instance_type, min_count, max_count)security_groups = self.security_group_api.populate_security_groups(security_groups)self.security_group_api.ensure_default(context)LOG.debug("Going to run %s instances...", num_instances)instances_to_build = []try:for i in range(num_instances):# Create a uuid for the instance so we can store the# RequestSpec before the instance is created.instance_uuid = uuidutils.generate_uuid()# Store the RequestSpec that will be used for scheduling.# 將于 scheduling 相關的參數都封裝到 RequestSpec 對象,便于在 Nova Scheduler 中應用。req_spec = objects.RequestSpec.from_components(context,instance_uuid, boot_meta, instance_type,base_options['numa_topology'],base_options['pci_requests'], filter_properties,instance_group, base_options['availability_zone'],security_groups=security_groups)# 如果是 Boot from Volume,則使用系統盤作為操作系統的根盤if block_device_mapping:# Record whether or not we are a BFV instanceroot = block_device_mapping.root_bdm()# 標記為 Boot from volumereq_spec.is_bfv = bool(root and root.is_volume)else:# If we have no BDMs, we're clearly not BFVreq_spec.is_bfv = False# NOTE(danms): We need to record num_instances on the request# spec as this is how the conductor knows how many were in this# batch.req_spec.num_instances = num_instances# 創建 RequestSpec 數據庫記錄req_spec.create()# NOTE(stephenfin): The network_metadata field is not persisted# and is therefore set after 'create' is called.if network_metadata:req_spec.network_metadata = network_metadata# Create an instance object, but do not store in db yet.instance = objects.Instance(context=context)instance.uuid = instance_uuidinstance.update(base_options)instance.keypairs = objects.KeyPairList(objects=[])if key_pair:instance.keypairs.objects.append(key_pair)instance.trusted_certs = self._retrieve_trusted_certs_object(context, trusted_certs)instance = self.create_db_entry_for_new_instance(context,instance_type, boot_meta, instance, security_groups,block_device_mapping, num_instances, i,shutdown_terminate, create_instance=False)# 確定 Block Device 時可用的,并設定其 Size 且與 Instance 關聯起來block_device_mapping = (self._bdm_validate_set_size_and_instance(context,instance, instance_type, block_device_mapping,supports_multiattach))instance_tags = self._transform_tags(tags, instance.uuid)# 將于虛擬機創建(啟動)相關的參數封裝到 BuildRequest 對象build_request = objects.BuildRequest(context,instance=instance, instance_uuid=instance.uuid,project_id=instance.project_id,block_device_mappings=block_device_mapping,tags=instance_tags)build_request.create()# Create an instance_mapping. The null cell_mapping indicates# that the instance doesn't yet exist in a cell, and lookups# for it need to instead look for the RequestSpec.# cell_mapping will be populated after scheduling, with a# scheduling failure using the cell_mapping for the special# cell0.# 將于虛擬機定位(cells)相關的參數封裝到 InstanceMapping 對象inst_mapping = objects.InstanceMapping(context=context)inst_mapping.instance_uuid = instance_uuidinst_mapping.project_id = context.project_idinst_mapping.cell_mapping = Noneinst_mapping.create()instances_to_build.append((req_spec, build_request, inst_mapping))if instance_group:if check_server_group_quota:try:objects.Quotas.check_deltas(context, {'server_group_members': 1},instance_group, context.user_id)except exception.OverQuota:msg = _("Quota exceeded, too many servers in ""group")raise exception.QuotaError(msg)members = objects.InstanceGroup.add_members(context, instance_group.uuid, [instance.uuid])# NOTE(melwitt): We recheck the quota after creating the# object to prevent users from allocating more resources# than their allowed quota in the event of a race. This is# configurable because it can be expensive if strict quota# limits are not required in a deployment.if CONF.quota.recheck_quota and check_server_group_quota:try:objects.Quotas.check_deltas(context, {'server_group_members': 0},instance_group, context.user_id)except exception.OverQuota:objects.InstanceGroup._remove_members_in_db(context, instance_group.id, [instance.uuid])msg = _("Quota exceeded, too many servers in ""group")raise exception.QuotaError(msg)# list of members added to servers group in this iteration# is needed to check quota of server group during add next# instanceinstance_group.members.extend(members)# In the case of any exceptions, attempt DB cleanupexcept Exception:with excutils.save_and_reraise_exception():self._cleanup_build_artifacts(None, instances_to_build)return instances_to_build

至此,POST /servers 在 nova-api service 的工作流程基本就完成了,總的來說主要做了兩件事情:校驗轉換

  • API 接口的版本控制、數據類型校驗(e.g. validation.schema)
  • 用戶操作意圖許可校驗(e.g. context.can、supports_device_tagging、supports_multiattach)
  • 虛擬機關聯資源的可用性校驗
  • 用戶輸入數據整理、歸納、分類并轉化為程序內部使用的數據結構(對象)

后面通過 self.compute_task_api 調用進入到 nova-conductor service 的工作流程。

轉載于:https://www.cnblogs.com/jmilkfan-fanguiju/p/10589721.html

總結

以上是生活随笔為你收集整理的Nova 启动虚拟机流程解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。