用于管理kvm主机的api。

kvm的Python项目详细描述


此模块旨在管理kvm管理程序。为此,请使用 unix module允许管理 本地和远程的类Unix系统,通过重载类 实例。这个模块只是virsh命令的包装器。它解析 virsh命令的输出(XML和文本)。命令分组在 可通过属性访问的子对象。

安装

此模块与python2.7和python 3.*兼容。模块是 在pypi上,以便可以使用pip命令安装它。

例如,在virtualenv中使用kvm

$ virtualenv env/ --prompt "(myprog)"
$ . ./env/bin/activate
(myprog) $ pip install kvm

否则,源位于github上:https://github.com/fmenabe/python-kvm

用法

您需要从unix模块导入必要的类。管理程序是 由hypervisor对象表示,并且必须包装类型为的对象 unix.Localunix.Remote。它理论上支持任何unix系统,但是 磁盘操作需要加载nbd模块,因此最好使用 unix.linux.Linux主机。

>>>fromuniximportLocal,Remote,UnixError>>>fromunix.linuximportLinux>>>importkvm>>>importjson>>>localhost=kvm.Hypervisor(Linux(Local()))>>>localhost.hypervisor.nodeinfo(){'nb_cpu':1,'nb_threads_per_core':2,'memory':16331936,'numa_cells':1,'cpu_model':'x86_64','nb_cores_per_cpu':4,'nb_cores':8,'cpu_freq':1340}>>>localhost.list_domains(all=True){'guest1':{'id':-1,'state':'shut off'}}{'guest2':{'id':1,'state':'running'}}>>>localhost.domain.start('guest1')# Wait a few seconds for the domain to start.>>>localhost.domain.state('guest1')'running'>>>localhost.domain.id('guest1')2>>>print(json.dumps(localhost.domain.conf('guest1'),indent=2))# json is use for pretty printing the dictionnary containing the# configuration.{"@type":"kvm","name":"guest1","uuid":"ed68d942-5d4b-7bba-4d74-7d44d73779d3","memory":{"@unit":"KiB","#text":"2097152"},...}>>>localhost.list_networks(){'default':{'autostart':True,'persistent':True,'state':'active'}}>>>host=Remote()>>>host.connect('hypervisor1')>>>host=kvm.Hypervisor(Linux(host))>>>host.hypervisor.nodeinfo(){'cores_per_socket':12,'cpu_frequency':'2200 MHz','cpu_model':'x86_64','cpu_sockets':2,'cpus':24,'memory_size':'98974432 kB','numa_cells':1,'threads_per_core':1}>>>host.list_domains(all=True){'guest1':{'id':1,'state':'running'}}{'guest2':{'id':2,'state':'running'}}>>>host.domain.shutdown('guest2')# Wait for the domain to stop.>>>host.domain.state('guest1')'shut off'# Using the context manager for the connecion.>>>fromunix.linuxaslinux,kvm>>>withlinux.connect('hypervisor1')ashost:...host=kvm.Hypervisor(host)...host.hypervisor.node_info()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java无法调用扩展超级非抽象类的抽象类方法   java Hibernate 5关联[]引用了一个未映射的实体[]:origin(User.hbm.xml)   java禁用JSpinner中的数字分组   java Spring的环境抽象是否使用PropertyEditor?   java是否创建自定义注释作为FrameworkAnnotation的别名?   java如何将Selenium安装为Unix守护程序?   java三值if运算符在正则if-one中   有人能帮我在一个程序上分析java代码吗?   java Spring集成5.0反应堆类型支持   java Websphere 7线程转储分析   java Akka期货和监管策略如何应对失败   使用Clojure中的可选参数调用java函数的构造函数   java如何消除安卓按钮的延迟?   Android上使用X.509证书的java认证应用程序   java如何获取列表中特定类型的所有对象?   java如何将json数组字符串转换为json对象   安卓 Java从长时间返回的日期不正确   java Android Studio在尝试从TextEdit搜索单词时崩溃   ViewPager的java子项