环境
版本: rdo OpenStack Liberty
qemu: 2.3.0
libvirt: 1.2.17-13
思路
需要qemu-guest-agent支持,在镜像中安装qemu-guest-agent包,并设置开机自启动,然后镜像设置元数据:
这两种方式设置都可以:
glance image-update <image-id> --property hw_qemu_guest_agent=yes nova image-meta {image_id} set hw_qemu_guest_agent=yes
然后生成的虚拟机libvirt xml文件会如下信息:
<channel type=‘unix‘> <source mode=‘bind‘ path=‘/var/lib/libvirt/qemu/org.qemu.guest_agent.0.instance-00000011.sock‘/> <target type=‘virtio‘ name=‘org.qemu.guest_agent.0‘/> <address type=‘virtio-serial‘ controller=‘0‘ bus=‘0‘ port=‘1‘/> </channel>
1、热添加cpu功能:libvirt接口支持,通过配置nova flavor的元数据cpu:max来预分配vcpu;最终调用libvirt的setVcpusFlags函数来实现(需要注意的是:硬重启虚拟机,libvirt xml文件会根据数据库表的信息重新生成,这块已处理好)
2、热移除cpu功能:移除vcpu后,虚拟机需要重启生效(这里可以在api那端设置限制,关机状态下才可以进行移除vcpu操作)
3、热添加、热减少内存: 这个好像没什么问题, 设置最大内存的时候,libvirt那边不能是active的domain
这是本人的简单实现:https://github.com/yaoice/nova/commit/26e9e3840abc0f8c81342b58c14c5af0e397c063
测试
TOKEN=`openstack token issue | awk ‘/id/ {print $4}‘ | head -1` export TOKEN=${TOKEN} export SERVER=`nova list | awk ‘/ACTIVE/ {print $2}‘`
显示虚拟机vcpu个数
curl -i "http://node_172_16_214_226:8774/v2.1/7d58dea58dd448d4b095da27986176f2/servers/${SERVER}/os-server-cpu-hotplug" -X GET -H "User-Agent: python-novaclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: ${TOKEN}"
设置虚拟机vcpu
curl -g -i -X POST http://node_172_16_214_226:8774/v2.1/7d58dea58dd448d4b095da27986176f2/servers/${SERVER}/action -H "User-Agent: python-novaclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-OpenStack-Nova-API-Version: 2.5" -H "X-Auth-Token: ${TOKEN}" -d ‘{"setVcpus": {"vcpus": 6}}‘
设置成功的虚拟机的libvirt xml有如下信息:
libvirt vcpu xml: <vcpu placement=‘static‘ current=‘4‘>16</vcpu>
显示虚拟机的内存
curl -i "http://node_172_16_214_226:8774/v2.1/7d58dea58dd448d4b095da27986176f2/servers/${SERVER}/os-server-mem-hottune" -X GET -H "User-Agent: python-novaclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: ${TOKEN}"
设置虚拟机的内存
curl -g -i -X POST http://node_172_16_214_226:8774/v2.1/7d58dea58dd448d4b095da27986176f2/servers/${SERVER}/action -H "User-Agent: python-novaclient" -H "Content-Type: application/json" -H "Accept: application/json" -H "X-OpenStack-Nova-API-Version: 2.5" -H "X-Auth-Token: ${TOKEN}" -d ‘{"setMem": {"mem": 4096}}‘
常见报错
virsh # setvcpus --live --guest instance-00000040 3 error: invalid argument: requested vcpu count is greater than the count of enabled vcpus in the domain: 3 > 2 virsh # setmaxmem --live 11 7292608 error: Unable to change MaxMemorySize error: Requested operation is not valid: cannot resize the maximum memory on an active domain
参考链接
(how to add nova api method) https://github.com/accelazh/example-nova-exensions/blob/master/nova/nova/api/openstack/compute/contrib/example_resource.py
(how to configure qemu-guest-agent) https://www.sebastien-han.fr/blog/2015/02/09/openstack-perform-consistent-snapshots-with-qemu-guest-agent/
(flavor extra_specs usage) http://docs.openstack.org/admin-guide/compute-flavors.html
(KVM主机动态更改cpu数量和内存大小) http://bbotte.blog.51cto.com/6205307/1635733
(libvirt cpu hot remove 需要重启cpu数量才会真正减少) http://www.unixarena.com/2015/12/linux-kvm-how-to-add-remove-vcpu-to-guest-on-fly.html