nova-compute源码分析

时间:2023-03-10 08:37:42
nova-compute源码分析

源码版本:H版

首先看启动脚本如下:

/usr/bin/nova-compute

import sys
from nova.cmd.compute import main
if __name__ == "__main__":
  sys.exit(main())

nova/cmd/compute.py

def main():
objects.register_all()
config.parse_args(sys.argv)
logging.setup('nova')
utils.monkey_patch() """强制使用nova-conductor访问数据库"""
if not CONF.conductor.use_local:
block_db_access()
objects_base.NovaObject.indirection_api = \
conductor_rpcapi.ConductorAPI()
"""下面的服务启动过程和nova-conductor类似,可以参见:http://www.cnblogs.com/littlebugfish/p/4058054.html"""
server = service.Service.create(binary='nova-compute',
topic=CONF.compute_topic,
db_allowed=False)
service.serve(server)
service.wait()

  由于nova-compute是一个rpc service,所以具体的请求最终将由Manager类来处理。(具体原由可以参考:http://www.cnblogs.com/littlebugfish/p/4058054.html)所以接下来具体查看ComputeManager,代码如下:

nova/compute/manager.py

ComputeManager类:

def __init__(self, compute_driver=None, *args, **kwargs):
...
self.driver = driver.load_compute_driver(self.virtapi, compute_driver)
self.use_legacy_block_device_info = \
self.driver.need_legacy_block_device_info

  其中最为关键的是加载ComputeDriver对象用于操作管理虚拟机,接着分析如下:

nova/virt/driver.py

LibvirtDriver类:

def load_compute_driver(virtapi, compute_driver=None):
if not compute_driver:
compute_driver = CONF.compute_driver if not compute_driver:
LOG.error(_("Compute driver option required, but not specified"))
sys.exit(1) LOG.info(_("Loading compute driver '%s'") % compute_driver)
try:
driver = importutils.import_object_ns('nova.virt',
compute_driver,
virtapi)
return utils.check_isinstance(driver, ComputeDriver)
except ImportError:
LOG.exception(_("Unable to load the virtualization driver"))
sys.exit(1)

  这里在配置文件中已经指明如下信息:

#compute_driver=libvirt.LibvirtDriver
compute_driver=libvirt.LibvirtDriver

  所以会创建底层虚拟化管理器LibvirtDriver对象,实际上ComputeManager最终都会将处理请求转交给LibvirtDriver去执行!

参考文档:

http://bingotree.cn/?p=289#more-289