对于云计算的实践,在虚拟机上面布置kvm虚拟机后使用python调用libvirt库进行远程可视化管理,实现输出虚拟机信息、新建虚拟机、删除虚拟机等功能,并在虚拟机集群上面运行mpi代码。
用pycharm专业版连接kvm的步骤见本文章。
#encoding=utf-8
import libvirt
import tkinter as tk
import time
import os
import subprocess
def list_connection():#主机信息
text=entryName1.get()
print("---------主机信息---------")
if (text=='master1'):
conn=conn0
if(text=='master0'):
conn=conn1
nodeinfo = conn.getInfo() # 获取虚拟化主机信息
print('模型: ' + str(nodeinfo[0])) # cpu模型
print('内存大小: ' + str(nodeinfo[1]) + 'MB') # 内存
print('CPU数量: ' + str(nodeinfo[2])) # cpu数量
print('虚拟类型: ' + conn.getType()) # 虚拟化类型
uri = conn.getURI() # 目前链接的uri
print('连接URI: : ' + uri)
mem = conn.getFreeMemory()
print("空闲内存: " + str(mem) + " bytes.")
print("-------------------------")
def show_machine():#显示虚拟机状态
#开启的域
text=entryName1.get()
vms_dict={}
if(text=="master1"):
conn=conn0
if(text=="master0"):
conn=conn1
domain_list = conn.listDomainsID()
for vm in domain_list:
vms_dict[str(vm)]=conn.lookupByID(vm).name()
print("""
处于开启状态的虚拟机
----------------
ID Name
----------------""")
if vms_dict:
for id_part,name_part in vms_dict.items():
print("{} {}".format(id_part,name_part))
#lableInit.config(text="开启状态:".format(vms_dict))
else:
print("None")
print("----------------")
#关闭的域
global vmd_list
vmd_list=[]
for i in conn.listDefinedDomains():
vmd_list.append(i)
print("""
处于关闭状态的虚拟机
----------------
Name
----------------""")
if vmd_list:
for name_part in vmd_list:
print("{}".format(name_part))
else:
print("None")
print("----------------")
#lableInit.config(text="虚拟机信息已在终端显示")
def show_information():
text = entryName1.get()
if (text == 'master1'):
conn = conn0
if (text == 'master0'):
conn = conn1
for id in conn.listDomainsID():
domain = conn.lookupByID(id)
nodeinfo=domain.info()
print('模型: ' + str(nodeinfo[0])) # cpu模型
print('内存大小: ' + str(nodeinfo[1]) + 'MB') # 内存
print('CPU数量: ' + str(nodeinfo[3])) # cpu数量
t1 = time.time()
c1 = int(nodeinfo[4])
time.sleep(1)
t2 = time.time()
c2 = int(nodeinfo[4])
c_nums = int(nodeinfo[3])
usage = (c2 - c1) * 100 / ((t2 - t1) * c_nums * 1e9)
print('CPU使用率'+ str(usage))
#--------对虚拟机域进行 *** 作---------
def temp():
text = entryName1.get()
if (text == 'master1'):
conn = conn0
if (text == 'master0'):
conn = conn1
name = entryName.get()
os.system('cd /var/lib/libvirt/images')
os.system('sudo qemu-img create -f qcow2 %s.qcow2 10G'%(name))
os.system('sudo virt-install --connect qemu:///system --virt-type kvm --name %s --ram 1024 --vcpus 1 --network network=br0 --disk path=/var/lib/libvirt/images/%s.qcow2,size=15,format=qcow2 --cdrom /home/user/下载/CentOS-7-x86_64-DVD-2009.iso --noautoconsole'%(name,name))
def undefine():
text = entryName1.get()
if (text == 'master1'):
conn = conn0
if (text == 'master0'):
conn = conn1
dom = conn.lookupByName(entryName.get())
dom.undefine()
print("虚拟机 {} 已取消定义".format(entryName.get()))
def suspand():
text = entryName1.get()
if (text == 'master1'):
conn = conn0
if (text == 'master0'):
conn = conn1
dom = conn.lookupByName(entryName.get())
dom.suspend()
print("虚拟机 {} 暂停成功".format(entryName.get()))
def resume():
text = entryName1.get()
if (text == 'master1'):
conn = conn0
if (text == 'master0'):
conn = conn1
dom = conn.lookupByName(entryName.get())
dom.resume()
print("虚拟机 {} 运行成功".format(entryName.get()))
def destroy():
text = entryName1.get()
if (text == 'master1'):
conn = conn0
if (text == 'master0'):
conn = conn1
dom = conn.lookupByName(entryName.get())
dom.destroy()
dom.undefine()
print("虚拟机 {} 销毁成功".format(entryName.get()))
def start():
text = entryName1.get()
if (text == 'master1'):
conn = conn0
if (text == 'master0'):
conn = conn1
dom = conn.lookupByName(entryName.get())
dom.create()
print("虚拟机 {} 启动成功".format(entryName.get()))
def shutdown():
text = entryName1.get()
if (text == 'master1'):
conn = conn0
if (text == 'master0'):
conn = conn1
dom = conn.lookupByName(entryName.get())
dom.shutdown()
print("虚拟机 {} 关闭成功".format(entryName.get()))
#------------菜单控制-------------
def test():
name=str(entryName.get())
print(name)
#--------------------------------
conn0 = libvirt.open("qemu:///system")#链接
conn1=libvirt.open('qemu+ssh://user@master0:22/system')
top=tk.Tk(screenName=':0')
top.title("虚拟机管理平台")
labelHello = tk.Label(top,fg='#c71585',font = 'Helvetica -18 bold', text = "请输入虚拟机的名称:")
labelHello.grid(row=1,column=0,padx=2,pady=6,sticky='E')
entryName=tk.Entry(top,width=24)
entryName.grid(row=1,column=1,padx=2,sticky='W')
labelHello1 = tk.Label(top,fg='#c71585',font = 'Helvetica -18 bold', text = "请输入主节点的名称:")
labelHello1.grid(row=0,column=0,padx=2,pady=6,sticky='E')
entryName1=tk.Entry(top,width=24)
entryName1.grid(row=0,column=1,padx=2,sticky='W')
#------- *** 作------
showHost =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="显示主机信息",command=list_connection)
showHost.grid(row=3,column=0,padx=4,pady=4,sticky='E')
showVM =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="显示虚拟机状态",command=show_machine)
showVM.grid(row=3,column=2,padx=4,pady=4,sticky='W')
showVM1 =tk.Button(top,bg='#00BFFF',fg='white',width=36,height=2,font = 'Helvetica -15 bold', text="显示虚拟机信息",command=show_information)
showVM1.grid(row=2,column=1,padx=4,pady=4)
tempVM =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold',text="创建虚拟机",command=temp)
tempVM.grid(row=4,column=0,padx=4,pady=4,sticky='E')
destroyVM = tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="销毁虚拟机", command=destroy)
destroyVM.grid(row=4,column=2,padx=4,pady=4,sticky='W')
suspandVM =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold',text="暂停虚拟机",command=suspand)
suspandVM.grid(row=5,column=0,padx=4,pady=4,sticky='E')
resumeVM = tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="运行虚拟机", command=resume)
resumeVM.grid(row=5,column=2,padx=4,pady=4,sticky='W')
startVM = tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="开启虚拟机", command=start)
startVM.grid(row=6,column=0,padx=4,pady=4,sticky='E')
shutdownVM = tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="关闭虚拟机", command=shutdown)
shutdownVM.grid(row=6,column=2,padx=4,pady=4,sticky='W')
top.mainloop()
mpi代码见本文章。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)