#!/usr/bin/env python
#coding=utf8
import libvirt
import mysqldb
import libvirt,os,sys
from xml.etree import elementtree
openstack_db_host='10.0.0.51'
openstack_db_user='root'
openstack_db_pwd='mysql'
uri='qemu:///system'
conn=libvirt.open(none)
user_id='5f93f059173248f0b3058a906dc07a0a'
project_id='c5b0af91883d47298e20b421f353695d'
power_stata=0
vm_state='active'
task_state='null'
default_ephemeral_device='null'
eohemeral_gb='null'
def db_cmd(host=openstack_db_host,user=openstack_db_user,passwd=openstack_db_pwd,\
db="nova",sql="none",select=true):
conn=mysqldb.connect(host=host,user=user,passwd=passwd,db=db,\
use_unicode=true,charset="utf8")
cursor=conn.cursor()
line=cursor.execute(sql)
if not select:
conn.commit()
conn.close()
return line
datas=cursor.fetchall()
conn.commit()
conn.close()
return datas
def get_devices(dom,path,devs):
tree=elementtree.fromstring(dom.xmldesc(0))
devices=
for target in tree.findall(path):
dev=target.get(devs)
if not dev in devices:
return devices
def get_doms_id():
return conn.listdomainsid()
def get_doms_info():
for dom_id in get_doms_id():
dom=conn.lookupbyid(dom_id)
print dom.name(),
if dom.name().startswith('instance'):
print 'openstack instance, pass'
continue
if dom.isactive:
power_state = 1
vm_state='active'
current_instance_id_sql='select max(id) from instances;'
instance_id=hex(db_cmd(sql=current_instance_id_sql)[0][0]+1)[2:-1]
instance_name='instance-'+'0'*(8-len(instance_id))+instance_id
print 'change instance %s to %s' % (dom.name(),instance_name)
vcpus=dom.maxvcpus()
memory=dom.info()[2]/1024
hostname=display_name=dom.name()
launched_on = host = conn.gethostname()
instance_type_id=1
uuid=dom.uuidstring()
root_device_name='/dev/'+get_devices(dom,"devices/disk/target","dev")[0]
root_gb=dom.blockinfo(get_devices(dom,"devices/disk/target","dev")[0],0)[0]/1024/1024/1024
migrate_sql="insert into instances (created_at,updated_at,deleted,user_id,project_id,\
power_state,vm_state,memory_mb,vcpus,hostname,host,\
display_name,launched_on,instance_type_id,uuid,root_device_name,\
task_state,default_ephemeral_device,root_gb,ephemeral_gb) values \
(now(),now(),'%d','%s','%s',%d,'%s','%s','%s','%s','%s','%s',\
'%s','%d','%s','%s',%s,'%s','%s','%d')" % \
(0,user_id,project_id,1,vm_state,memory,vcpus,hostname,host,\
display_name,launched_on,6,uuid,root_device_name,'null','null',root_gb,0)
db_cmd(sql=migrate_sql)
tree=elementtree.fromstring(dom.xmldesc(0))
tree[0].text=instance_name
tree=elementtree.elementtree(tree)
tree.write('/tmp/%s.xml' % instance_name ,encoding='utf-8')
print "virsh undefine %s" % dom.name()
dom.undefine()
print "shut off the instance %s" % dom.name()
dom.destroy()
conn.definexml("".join(file('/tmp/%s.xml' % instance_name, 'r').readlines()))
print "restart the instance %s(%s)" % (instance_name,dom.name())
dom.create()
if __name__=='__main__':
get_doms_info()
openstack 虛擬機器 遷移
遷移。如果 你的 雲 系統 正在 使用 共享 儲存,使用 nova live migration 命令 就可以。首先,要 獲得 需要 被 遷移 的 例項 列表 nova list host c01.example.com all tenants 接下來,把 它們 乙個 乙個 移走 nova live...
openstack虛擬機器網路配置
登陸horizon介面 1.用admin使用者登陸,在管理員下把預設的網路刪除 先刪路由器 2.在管理員下用admin使用者建立新的網路 newnet 其中選擇外部,共享,管理員狀態選項,在 newnet 網路下建立新的子網 192.168.2.0 24,閘道器為 192.168.2.150,192...
openstack 虛擬機器映象製作
準備 伺服器作業系統 centos 7 準備一台安裝了vnc viewer客戶端的windows作業系統用於訪問伺服器裡啟動的虛擬機器介面 前提是windows系統能訪問伺服器 sle 12 sp2 sap x86 64 gm 1.iso複製到伺服器 home 下 qemu img create f...