虚拟机创建流程中neutron代码分析(三)

前言:

当neutron-server创建了port信息,将port信息写入数据库中。流程返回到nova服务端,接着nova创建的流程继续走。在计算节点中neutron-agent同样要完成很多的工作来支持主机的通信。

简要流程:

 

详细代码分析:

spawn()作用:

  1. 准备磁盘信息
  2. 获取镜像的获取路径
  3. 准备启动xml文件
  4. 创建主机和网络,调用create_domain_and_network()函数获取网络信息。

准备启动xml文件的过程稍后有详细分析。

create_domin_and_network()

作用:

创建虚拟机过程中neutron工作主要体现的地方,详细的说明在下面的截图中。主要的工作是创建调用底层驱动libvirt创建虚拟机。

 -------------------------------------------------------------------------------------------------nova创建xml文件-------------------------------------------------------------------------

xml文件如下所示,其中定义了主机必要信息如mac地址,内核,磁盘,分区信息,网卡等。

  1 <domain type='qemu' id='3'>  
  2       <name>instance-00000002</name>  
  3       <uuid>8d812f63-573c-4f2c-a991-dfa68b844d20</uuid>  
  4       <metadata>  
  5         <nova:instance xmlns:nova="http://openstack.org/xmlns/libvirt/nova/1.0">  
  6           <nova:package version="13.1.0-1.el7"/>  
  7           <nova:name>myInstanceWithVolume</nova:name>  
  8           <nova:creationTime>2016-08-19 03:17:58</nova:creationTime>  
  9           <nova:flavor name="m1.small">  
 10             <nova:memory>2048</nova:memory>  
 11             <nova:disk>20</nova:disk>  
 12             <nova:swap>0</nova:swap>  
 13             <nova:ephemeral>0</nova:ephemeral>  
 14             <nova:vcpus>1</nova:vcpus>  
 15           </nova:flavor>  
 16           <nova:owner>  
 17             <nova:user uuid="6383b14b190d422ab3079e4e63c62e16">demo</nova:user>  
 18             <nova:project uuid="478a18506673406db8abc360cdc2f202">demo</nova:project>  
 19           </nova:owner>  
 20           <nova:root type="image" uuid="9bf97139-7760-4ace-b3d1-5bcd0ff30f55"/>  
 21         </nova:instance>  
 22       </metadata>  
 23       <memory unit='KiB'>2097152</memory>  
 24       <currentMemory unit='KiB'>2097152</currentMemory>  
 25       <vcpu placement='static'>1</vcpu>  
 26       <cputune>  
 27         <shares>1024</shares>  
 28       </cputune>  
 29       <resource>  
 30         <partition>/machine</partition>  
 31       </resource>  
 32       <sysinfo type='smbios'>  
 33         <system>  
 34           <entry name='manufacturer'>Fedora Project</entry>  
 35           <entry name='product'>OpenStack Nova</entry>  
 36           <entry name='version'>13.1.0-1.el7</entry>  
 37           <entry name='serial'>acb2b380-0bb5-7101-f93d-107563ca227e</entry>  
 38           <entry name='uuid'>8d812f63-573c-4f2c-a991-dfa68b844d20</entry>  
 39           <entry name='family'>Virtual Machine</entry>  
 40         </system>  
 41       </sysinfo>  
 42       <os>  
 43         <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>  
 44         <boot dev='hd'/>  
 45         <smbios mode='sysinfo'/>  
 46       </os>  
 47       <features>  
 48         <acpi/>  
 49         <apic/>  
 50       </features>  
 51       <cpu mode='host-model'>  
 52         <model fallback='allow'/>  
 53         <topology sockets='1' cores='1' threads='1'/>  
 54       </cpu>  
 55       <clock offset='utc'/>  
 56       <on_poweroff>destroy</on_poweroff>  
 57       <on_reboot>restart</on_reboot>  
 58       <on_crash>destroy</on_crash>  
 59       <devices>  
 60         <emulator>/usr/libexec/qemu-kvm</emulator>  
 61         <disk type='file' device='disk'>  
 62           <driver name='qemu' type='qcow2' cache='none'/>  
 63           <source file='/var/lib/nova/instances/8d812f63-573c-4f2c-a991-dfa68b844d20/disk'/>  
 64           <backingStore type='file' index='1'>  
 65             <format type='raw'/>  
 66             <source file='/var/lib/nova/instances/_base/86f82a7c75893f6ce52678cc7f7991aecc4aa9aa'/>  
 67             <backingStore/>  
 68           </backingStore>  
 69           <target dev='vda' bus='virtio'/>  
 70           <alias name='virtio-disk0'/>  
 71           <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>  
 72         </disk>  
 73         <disk type='block' device='disk'>  
 74           <driver name='qemu' type='raw' cache='none' io='native'/>  
 75           <source dev='/dev/disk/by-path/ip-192.168.8.8:3260-iscsi-iqn.2010-10.org.openstack:volume-13db53ea-475b-471d-bbb1-a0daffbf25ef-lun-0'/>  
 76           <backingStore/>  
 77           <target dev='vdb' bus='virtio'/>  
 78           <serial>13db53ea-475b-471d-bbb1-a0daffbf25ef</serial>  
 79           <alias name='virtio-disk1'/>  
 80           <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>  
 81         </disk>  
 82         <controller type='usb' index='0'>  
 83           <alias name='usb'/>  
 84           <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>  
 85         </controller>  
 86         <controller type='pci' index='0' model='pci-root'>  
 87           <alias name='pci.0'/>  
 88         </controller>  
 89         <serial type='file'>  
 90           <source path='/var/lib/nova/instances/8d812f63-573c-4f2c-a991-dfa68b844d20/console.log'/>  
 91           <target port='0'/>  
 92           <alias name='serial0'/>  
 93         </serial>  
 94         <serial type='pty'>  
 95           <source path='/dev/pts/3'/>  
 96           <target port='1'/>  
 97           <alias name='serial1'/>  
 98         </serial>  
 99         <console type='file'>  
100           <source path='/var/lib/nova/instances/8d812f63-573c-4f2c-a991-dfa68b844d20/console.log'/>  
101           <target type='serial' port='0'/>  
102           <alias name='serial0'/>  
103         </console>  
104         <input type='tablet' bus='usb'>  
105           <alias name='input0'/>  
106         </input>  
107         <input type='mouse' bus='ps2'/>  
108         <input type='keyboard' bus='ps2'/>  
109         <graphics type='vnc' port='5901' autoport='yes' listen='0.0.0.0' keymap='en-us'>  
110           <listen type='address' address='0.0.0.0'/>  
111         </graphics>  
112         <video>  
113           <model type='cirrus' vram='16384' heads='1'/>  
114           <alias name='video0'/>  
115           <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>  
116         </video>  
117         <memballoon model='virtio'>  
118           <stats period='10'/>  
119           <alias name='balloon0'/>  
120           <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>  
121         </memballoon>  
122       </devices>  
123     </domain>  

spawn()

作用:

调用get_guest_xml()函数创建生成虚拟机的xml文件。

get_guest_xml()

作用:

  1. 获取xml文件需要的信息,有网络信息,磁盘信息,镜像元数据等。
  2. 调用get_guest_config()函数进一步生成xml

get_guest_config()

作用:

  1. 调用libvirt驱动层函数生成一个guest变量,或者说是对象。
  2. 向guest对象中填充各种信息
  3. 调用driver.get_config()函数生成tap设备

get_config()

作用:

生成tap设备。

 

 tap设备解释(经典):

tap是linux虚拟出来的设备,表现为一个字符设备,用户可以通过对该设备读写,实现数据交互。

tun/tap 驱动程序实现了虚拟网卡的功能,tun表示虚拟的是点对点设备,tap表示虚拟的是以太网设备,这两种设备针对网络包实施不同的封装。

利用tun/tap 驱动,可以将tcp/ip协议栈处理好的网络分包传给任何一个使用tun/tap驱动的进程,由进程重新处理后再发到物理链路中。做为虚拟

网卡驱动,Tun/Tap驱动程序的数据接收和发送并不直接和真实网卡打交道,他在Linux内核中添加了一个TUN/TAP虚拟网络设备的驱动程序和

一个与之相关连的字符设备 /dev/net/tun,字符设备tun作为用户空间和内核空间交换数据的接口。当内核将数据包发送到虚拟网络设备时,数据

包被保存在设备相关的一个队列中,直到用户空间程序通过打开的字符设备tun的描述符读取时,它才会被拷贝到用户空间的缓冲区中,其效果

就相当于,数据包直接发送到了用户空间。通过系统调用write发送数据包时其原理与此类似。

在linux下,要实现内核空间和用户空间数据的交互,有多种方式:
1 可以通用socket创建特殊套接字,利用套接字实现数据交互;
2 通过proc文件系统创建文件来进行数据交互;
3 还可以使用设备文件的方式。访问设备文件会调用设备驱动相应的例程,设备驱动本身就是内核空间和用户空间的一个接口,
Tun/tap驱动就是利用设备文件实现用户空间和内核空间的数据交互。

get_config_bridge()

作用:

生成网桥设备

 

原文地址:https://www.cnblogs.com/goldsunshine/p/7988354.html