python控制kvm
import libvirt
import tkinter as tk
import os
# 关闭连接
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("-------------------------")
# 显示虚拟机状态
def show_machine():
# 开启的域
vms_dict = {}
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 suspand():
dom = conn.lookupByName(entryName.get())
dom.suspend()
print("虚拟机 {} 暂停成功".format(entryName.get()))
def resume():
dom = conn.lookupByName(entryName.get())
dom.resume()
print("虚拟机 {} 运行成功".format(entryName.get()))
def start():
dom = conn.lookupByName(entryName.get())
dom.create()
print("虚拟机 {} 启动成功".format(entryName.get()))
def shutdown():
dom = conn.lookupByName(entryName.get())
dom.shutdown()
print("虚拟机 {} 关闭成功".format(entryName.get()))
def define():
name=entryName.get()
# os.system('sudo cd /var/lib/libvirt/images')
# os.system('sudo qemu-img create -f qcow2 %s.qcow2 0.5G'%(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=1,format=qcow2 --cdrom /home/user/下载/CentOS-7-x86_64-DVD-2009.iso --noautoconsole'%(name,name))
def undefine():
dom = conn.lookupByName(entryName.get())
dom.undefine()
dom.destroy()
print("虚拟机 {} 已取消定义".format(entryName.get()))
# ------------菜单控制-------------
def test():
name = str(entryName.get())
print(name)
# --------------------------------
conn = libvirt.open("qemu:///system") # 链接
window = tk.Tk(screenName=':0')
top = tk.Tk()
top.title("KVM Interface")
top.configure(background='Ivory')
# top.geometry('602x')#窗体大小
lableTitle = tk.Label(top, bg='Ivory', fg="#636F64", font='黑体 -24 bold', text="KVM Interface", width=46)
lableTitle.grid(row=0, column=0, columnspan=3, ipady=10)
#-------------------------------
labelHello = tk.Label(top,bg='Ivory',fg='saddlebrown',font = '黑体 -15 bold', text = "pelase choose a kvm:")
labelHello.grid(row=1,column=0,columnspan=2,ipady=10)
entryName=tk.Entry(top,width=24)
entryName.grid(row=1,column=1,padx=6,sticky='E')
#------- *** 作------
lableInitTitle = tk.Label(top,bg='Ivory',font = '黑体 -13 bold',text="enquiry:",width=85,fg="#636F64",justify="right")
lableInitTitle.grid(row=3,column=0,columnspan=2)
showHost = tk.Button(top, bg='moccasin', fg='saddlebrown', width=18, height=2, font='黑体 -15 bold', text="information",
command=list_connection)
showHost.grid(row=4, column=0, padx=40, pady=20, sticky='W')
showVM = tk.Button(top, bg='moccasin', fg='saddlebrown', width=18, height=2, font='黑体 -15 bold', text="state",
command=show_machine)
showVM.grid(row=4, column=1, padx=40, pady=20, sticky='W')
lableInitTitle = tk.Label(top, bg='Ivory', font='黑体 -13 bold', text="operation:", width=85, fg="#636F64",
justify="right")
lableInitTitle.grid(row=5, column=0, columnspan=2, ipady=20)
suspandVM = tk.Button(top, bg='moccasin', fg='saddlebrown', width=18, height=2, font='黑体 -15 bold', text="suspand",
command=suspand)
suspandVM.grid(row=6, column=0, padx=40, pady=20, sticky='W')
resumeVM = tk.Button(top, bg='moccasin', fg='saddlebrown', width=18, height=2, font='黑体 -15 bold', text="resume",
command=resume)
resumeVM.grid(row=7, column=0, padx=40, pady=20, sticky='W')
startVM = tk.Button(top, bg='moccasin', fg='saddlebrown', width=18, height=2, font='黑体 -15 bold', text="start",
command=start)
startVM.grid(row=6, column=1, padx=40, pady=20, sticky='W')
shutdownVM = tk.Button(top, bg='moccasin', fg='saddlebrown', width=18, height=2, font='黑体 -15 bold', text="shutdown",
command=shutdown)
shutdownVM.grid(row=7, column=1, padx=40, pady=20, sticky='W')
defineVM =tk.Button(top,bg='moccasin',fg='saddlebrown',width=18,height=2,font = '黑体 -15 bold',text="define",command=define)
defineVM.grid(row=6,column=2,padx=80,pady=40,sticky='E')
undefineVM =tk.Button(top,bg='moccasin',fg='saddlebrown',width=18,height=2,font = '黑体 -15 bold',text="undefine",command=undefine)
undefineVM.grid(row=7,column=2,padx=80,pady=40,sticky='E')
top.mainloop()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)