Libvirt

libvirt提供了虚拟化的一些操作和管理方式,有libvirt API,virsh,libvirtd等,而virsh是libvirt API的CLI封装;libvirtd顾名思义,应该就是一个daemon进程

对于接口,可以简单通过openstack里nova测试一下

先随便找一台虚拟机,通过nova接口查询一下instanceId

$ nova show e608ab49-4ad3-43f1-9b06-13704a803790
+--------------------------------------------------+-------------------------------------------------------------------+
| Property                                         | Value                                                             |
+--------------------------------------------------+-------------------------------------------------------------------+
| OS-DCF:diskConfig                                | MANUAL                                                            |
| OS-EXT-SRV-ATTR:instance_name                    | instance-00092797                                                 |
| OS-EXT-STS:power_state                           | 1                                                                 |
| OS-EXT-STS:task_state                            | -                                                                 |
| OS-EXT-STS:vm_state                              | active                                                            |
| OS-SRV-USG:launched_at                           | 2016-07-18T05:13:42.000000                                        |
| OS-SRV-USG:terminated_at                         | -                                                                 |
| accessIPv4                                       |                                                                   |
| accessIPv6                                       |                                                                   |
| config_drive                                     | 1                                                                 |
| created                                          | 2016-07-18T05:12:30Z                                              |
| flavor                                           | flavor_1 (1)                                                      |
| hostId                                           | a79519ce4511dccf36123fb973347e9b5f19865ec150433acb41c445          |
| hypervisor_type                                  | qemu                                                              |
| id                                               | e608ab49-4ad3-43f1-9b06-13704a803790                              |
| idc_58cda77a95d04eff99d22d32b23bde1c network     | 10.180.177.87                                                     |
| image                                            | Private_28196_Set_Password (9f014e8b-0daf-4d9c-b2a6-06bd3d19d454) |
| key_name                                         | dev                                                               |
| metadata                                         | {}                                                                |
| name                                             | lihui-testing-libvirtd-1                                          |
| os-extended-volumes:volumes_attached             | []                                                                |
| os-netease-extended-volumes:volumes_attached     | []                                                                |
| os-server-status                                 | up                                                                |
| os_type                                          | linux                                                             |
| private_58cda77a95d04eff99d22d32b23bde1c network | 10.180.64.11                                                      |
| progress                                         | 0                                                                 |
| security_groups                                  | default                                                           |
| status                                           | ACTIVE                                                            |
| tenant_id                                        | 58cda77a95d04eff99d22d32b23bde1c                                  |
| updated                                          | 2016-07-18T09:55:35Z                                              |
| use_ceph                                         | yes                                                               |
| user_id                                          | 18b34b038ddd47dcba37939c8712ba0e                                  |
+--------------------------------------------------+-------------------------------------------------------------------+

此时虚拟机是启动状态,通过virsh来执行shutdown

$ sudo virsh shutdown instance-00092797
Domain instance-00092797 is being shutdown

此时再来观察虚拟机状态,可以发现虚拟机的确被关闭了

$ nova show e608ab49-4ad3-43f1-9b06-13704a803790 | grep vm_state
| OS-EXT-STS:vm_state                              | stopped                                                           |
$ nova show e608ab49-4ad3-43f1-9b06-13704a803790 | grep os-server-status
| os-server-status                                 | down                                                              |

从这里可以看到,和nova stop的效果是一致的,这是因为virsh是libvirt API的命令行封装形式,而nova是openstack里主机API的封装形式,而主机API调用最终的执行也是会通过调用libvirt API来执进行虚拟机的操作

一图解千愁,来自IBM文档库,搜索libvirt即有

QQ20160726 0 2x

从下往上,通过virsh或者其他调用libvirt API,daemon响应后,通过对不同驱动的支持(比如QEMU驱动,XEN驱动等)来调不同的Hypervisor最终实现各种虚拟化的操作

但对于libvirt API的调用和libvirtd的响应,以及执行是否需要通过虚拟机所在的节点上的libvirtd daemon来进行操作,还没完全弄清,待继续研究~!

发表回复