在使用nova resize去修改虛擬機的配置的時候,實際調用的是migrate的代碼。python
在默認狀況下,執行resize的時候,首先會進行磁盤disk鏡像和後端鏡像進行一個合併成raw,而後轉換成qcow2的過程。並且會進行遷移的動做。整個過程會至關的消耗時間,並且咱們通常只須要修改cpu和mem的大小,並不須要去合併鏡像,更不須要去作遷移。 |
總之,在單臺宿主機上去進行nova resize是一件很讓人蛋碎的事情。mysql
事實上,在reboot一個instance的時候,這個instance的libvirt.xml(只考慮kvm環境)會從新從數據庫中生成。因此只須要修改數據庫就好了。sql
附上pyhon修改數據庫腳本:數據庫
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 flavoride
修改完成後必須使用nova reboot --hard instance_id 去重啓,必須帶上--hard去強制重啓實例,否則不會生效。fetch