#encoding=utf-8
import libvirt
import tkinter as tk
def close_connection():#关闭连接
try:
conn.close()
except:
print("--关闭连接失败--")
return 1
print('--成功关闭连接--')
def list_connection():#主机信息
print("---------主机信息---------")
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("-------------------------")
lableInit.config(text="--------主机信息--------\n"
"模型:{}\n"
"内存大小:{}MB\n"
"CPU数量:{}个\n"
"虚拟类型:{}\n"
"连接URI:{}\n"
"内存空间:{}bytes\n"
.format(str(nodeinfo[0]),str(nodeinfo[1]),str(nodeinfo[2]),conn.getType(),uri,str(mem)))
def show_machine():#显示虚拟机状态
#开启的域
vms_dict={}
domain_list=conn.listDomainsID()
for vm in domain_list:
vms_dict[str(vm)]=conn.lookupByID(vm).name()
print("""
处于开启状态的虚拟机
----------------
ID\tName
----------------""")
if vms_dict:
for id_part,name_part in vms_dict.items():
print("{}\t{}".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))
lableInit.config(text="开启状态:{}\n"
"关机状态:{}\n".format(vms_dict,vmd_list))
else:
print("None")
print("----------------")
#lableInit.config(text="虚拟机信息已在终端显示")
#--------对虚拟机域进行操作---------
def temp():
name = entryName.get()
f = open('/etc/libvirt/qemu/{}.xml'.format(name)) # xml文件需要事先准备好
xml = f.read()
conn.createXML(xml)
f.close()
lableInit.config(text="临时虚拟机 {} 创建完成".format(name))
def define():
name=entryName.get()
f=open('/etc/libvirt/qemu/{}.xml'.format(name))#xml文件需要事先准备好
xml=f.read()
dom = conn.defineXML(xml)
f.close()
lableInit.config(text="虚拟机 {} 创建成功".format(name))
def undefine():
dom = conn.lookupByName(entryName.get())
dom.undefine()
print("虚拟机 {} 已取消定义".format(entryName.get()))
lableInit.config(text="虚拟机 {} 已取消定义".format(entryName.get()))
def suspand():
dom = conn.lookupByName(entryName.get())
dom.suspend()
print("虚拟机 {} 暂停成功".format(entryName.get()))
lableInit.config(text="虚拟机 {} 已暂停".format(entryName.get()))
def resume():
dom = conn.lookupByName(entryName.get())
dom.resume()
print("虚拟机 {} 运行成功".format(entryName.get()))
lableInit.config(text="虚拟机 {} 继续运行".format(entryName.get()))
def destroy():
dom = conn.lookupByName(entryName.get())
dom.destroy()
print("虚拟机 {} 销毁成功".format(entryName.get()))
lableInit.config(text="虚拟机 {} 已销毁".format(entryName.get()))
def start():
dom = conn.lookupByName(entryName.get())
dom.create()
print("虚拟机 {} 启动成功".format(entryName.get()))
lableInit.config(text="虚拟机 {} 已启动".format(entryName.get()))
def shutdown():
dom = conn.lookupByName(entryName.get())
dom.shutdown()
print("虚拟机 {} 关闭成功".format(entryName.get()))
lableInit.config(text="虚拟机 {} 已关闭".format(entryName.get()))
#------------菜单控制-------------
def test():
name=str(entryName.get())
print(name)
#--------------------------------
conn = libvirt.open("qemu:///system")#链接
top=tk.Tk()
top.title("虚拟机管理平台")
#top.geometry('602x')#窗体大小
lableTitle = tk.Label(top,bg='#00BFFF',fg="#EEE8AA",font = 'Helvetica -24 bold',text="虚 拟 机 管 理 平 台",width=46)
lableTitle.grid(row=0,column=0,columnspan=2,ipady=10)
#-------------------------------
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')
#-------操作------
showHost =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold', text="显示主机信息",command=list_connection)
showHost.grid(row=2,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=2,column=1,padx=4,pady=4,sticky='W')
tempVM =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold',text="创建虚拟机",command=temp)
tempVM.grid(row=3,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=3,column=1,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=4,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=4,column=1,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=5,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=5,column=1,padx=4,pady=4,sticky='W')
defineVM =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold',text="创建虚拟机"
"(永久创建)",command=define)
defineVM.grid(row=6,column=0,padx=4,pady=4,sticky='E')
undefineVM =tk.Button(top,bg='#00BFFF',fg='white',width=18,height=2,font = 'Helvetica -15 bold',text="删除虚拟机"
"(取消定义)",command=undefine)
undefineVM.grid(row=6,column=1,padx=4,pady=4,sticky='W')
lableInitTitle = tk.Label(top,bg='black',font = 'Helvetica -13 bold',text="* * * 系 统 反 馈 信 息 * * *",width=85,fg="blue")
lableInitTitle.grid(row=7,column=0,columnspan=2,ipady=10)
lableInit = tk.Label(top,bg='black',font = 'Helvetica -13 bold',text="NULL",width=85,fg="white")
lableInit.grid(row=8,column=0,columnspan=2,ipady=10)
photo=tk.PhotoImage(file="/home/ldw/kvmtest/copyright.png")
labelphoto=tk.Label(image=photo)
labelphoto.image=photo
labelphoto.grid(row=9,column=0,columnspan=2)
#quitVM = tk.Button(top,text="退出管理系统",command=close_connection())
#quitVM.pack()
top.mainloop()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.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.156.157.158.159.160.161.162.163.164.165.166.167.168.169.170.171.172.173.174.175.176.177.178.179.180.181.182.183.184.185.186.187.188.189.190.191.192.193.194.195.196.197.198.199.200.201.202.203.204.205.206.207.208.209.210.211.212.