環境html
版本: rdo OpenStack Liberty
node
qemu: 2.3.0python
libvirt: 1.2.17-13linux
思路git
須要qemu-guest-agent支持,在鏡像中安裝qemu-guest-agent包,並設置開機自啓動,而後鏡像設置元數據:github
這兩種方式設置均可以:數據庫
glance p_w_picpath-update <p_w_picpath-id> --property hw_qemu_guest_agent=yes nova p_w_picpath-meta {p_w_picpath_id} set hw_qemu_guest_agent=yes
而後生成的虛擬機libvirt xml文件會以下信息:json
<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>
一、熱添加cpu功能:libvirt接口支持,經過配置nova flavor的元數據cpu:max來預分配vcpu;最終調用libvirt的setVcpusFlags函數來實現(須要注意的是:硬重啓虛擬機,libvirt xml文件會根據數據庫表的信息從新生成,這塊已處理好)
api
二、熱移除cpu功能:移除vcpu後,虛擬機須要重啓生效(這裏能夠在api那端設置限制,關機狀態下才能夠進行移除vcpu操做)bash
三、熱添加、熱減小內存: 這個好像沒什麼問題, 設置最大內存的時候,libvirt那邊不能是active的domain
這是本人的簡單實現:https://github.com/yaoice/nova/commit/26e9e3840abc0f8c81342b58c14c5af0e397c063
Hot Plug操做系統版本支持列表
測試
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