openstack快速更改虚拟机配置
在使用nova resize去修改虚拟机的配置的时候,实际调用的是migrate的代码。 在默认情况下,执行resize的时候,首先会进行磁盘disk镜像和后端镜像进行一个合并成raw,然后转换成qcow2的过程。而且会进行迁移的动作。整个过程会相当的消耗时间,而且我们一般只需要修改cpu和mem的大小,并不需要去合并镜像,更不需要去做迁移。 总之,在单台宿主机上去进行nova resize是一件很让人蛋碎的事情。 事实上,在reboot一个instance的时候,这个instance的libvirt.xml(只考虑kvm环境)会重新从数据库中生成。所以只需要修改数据库就行了。 附上pyhon修改数据库脚本: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 import MySQLdb,sys ip = sys.argv[ 1 ] size = sys.argv[ 2 ] size_info = { '1' : '55800df7-5579-45c3-b4b3-584454aa2b77' , '2' : '6fe54364-e066-460b-89a2-ffe19cb26ae7' , '4' : '7a457be6-b1a0-48cd-88e1-c4e21d0518c1' , '6' : '7105ad91-3e49-43ed-9575-1a0535694578' , '8' : '61fc938c-234a-406b-b102-d535264e914d' , '12' : 'dc5ffd92-eedf-4851-a40a-90e657f799a2' , '16' : '5c7af872-2089-4ece-bba7-4099d2a3ef24' , } to_size = size_info[size] get_flavor_info_sql = "select id ,memory_mb,vcpus from instance_types \ where flavorid = '%s' ;" % \ to_size class mysqlEng(): def __init__( self ,host = '127.0.0.1' ,user = 'root' ,passwd = ' ',db=' None ',port=' 3306 '): self .conn = MySQLdb.connect(host = host,user = user,passwd = passwd,db = db,port = 3306 ) self .cur = self .conn.cursor() def select( self ,sql = 'show tables;' ): self .cur.execute(sql) ret = self .cur.fetchall() return ret def update( self ,sql = ''): self .cur.execute(sql) self .conn.commit() def __del__( self ): self .cur.close() self .conn.close() mysql_object = mysqlEng(host = '127.0.0.1' ,user = 'root' ,passwd = '123456' ,db = 'nova' ) flavor_info = mysql_object.select(get_flavor_info_sql) flavor_id = flavor_info[ 0 ][ 0 ] mem = flavor_info[ 0 ][ 1 ] vcpus = flavor_info[ 0 ][ 2 ] update_flavor_info_sql = "update instances set instance_type_id = '%d' ,vcpus = '%d' ,memory_mb = '%d' \ where hostname = '%s' AND vm_state ! = 'deleted' AND vm_state ! = 'error' ;" % \ (flavor_id,vcpus,mem,ip) mysql_object.update(update_flavor_info_sql) after_update_info_sql = "select vcpus,memory_mb from instances \ where hostname = '%s' AND vm_state ! = 'deleted' AND vm_state ! = 'error' ;" % \ ip after_update_info = mysql_object.select(after_update_info_sql) print '+' * 80 + '\n' print "This server %s now flavor : cpu=%s , mem=%s\n" % \ (ip,after_update_info[ 0 ][ 0 ],after_update_info[ 0 ][ 1 ]) print '+' * 80 + '\n' 修改脚本中size_info里的对应openstack中flavor的id号,还有mysql_obejcet对象中的mysql相关信息 使用方式:python update_size.py instance_name flavor 修改完成后必须使用nova reboot --hard instance_id 去重启,必须带上--hard去强制重启实例,不然不会生效。 本文转自lustlost 51CTO博客,原文链接:http://blog.51cto.com/lustlost/1367575,如需转载请自行联系原作者