zoukankan      html  css  js  c++  java
  • access remote libvirtd

    访问远程libvirtd服务
    因为是在一个可信环境中运行,所以可以忽略安全方面的操作,步骤如下:
    (1)更改libvirtd配置
        1.1 更改/ect/sysconfig/libvirtd文件,打开LIBVIRTD_ARGS="--listen"设置
        1.2 更改/etc/libvirt/libvirtd.conf
            listen_tls = 0 #关闭tls
            listen_tcp = 1 # 打开tcp
            tcp_port = "16509" # 侦听关口
            listen_addr="0.0.0.0" # 侦听地址
            auth_tcp = "none" # 允许匿名访问
    (2) 可以通过 virsh -c qemu+tcp://10.2.3.123:16509/system来访问10.2.3.123上面的libvirtd服务
    (3) python代码示例如下:
      

    import libvirt
    import sys
    import logging
    
    '''
    enum virDomainState {
    
    VIR_DOMAIN_NOSTATE    =     0    no state
    VIR_DOMAIN_RUNNING    =     1    the domain is running
    VIR_DOMAIN_BLOCKED    =     2    the domain is blocked on resource
    VIR_DOMAIN_PAUSED    =     3    the domain is paused by user
    VIR_DOMAIN_SHUTDOWN    =     4    the domain is being shut down
    VIR_DOMAIN_SHUTOFF    =     5    the domain is shut off
    VIR_DOMAIN_CRASHED    =     6    the domain is crashed
    VIR_DOMAIN_PMSUSPENDED    =     7    the domain is suspended by guest power management
    VIR_DOMAIN_LAST    =     8    NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API.
    
    }
    
    struct virDomainInfo {
    
    unsigned char    state    the running state, one of virDomainState
    unsigned long    maxMem    the maximum memory in KBytes allowed
    unsigned long    memory    the memory in KBytes used by the domain
    unsigned short    nrVirtCpu    the number of virtual CPUs for the domain
    unsigned long long    cpuTime    the CPU time used in nanoseconds
    
    }
    
    
    '''
    def get_VM_infos(host, port=16509):
        '''
        list domains of the specified host
        '''
        infos = []
        try:
            pass
        
            uri = 'qemu+tcp://%s:%s/system' % (host, port)
            conn = libvirt.openReadOnly(uri)
            # list the defined but inactive domains
            domains = [conn.lookupByName(name) for name in conn.listDefinedDomains()]
            # list active domains
            domains.extend([conn.lookupByID(i) for i in conn.listDomainsID()])
    
            # for dom in domains:
            #     print 'ID = %d' % dom.ID()
            #     print 'Name = %s' % dom.name()
            #     infos = dom.info()
            #     print 'State = %d' % infos[0]
            #     # print 'State = %s' %s dom.state(0)
            #     print 'Max Memory = %s' % infos[1]
            #     print 'Memory = %s' % info[2]
            #     # print 'Max Memory = %d' % dom.maxMemory
            #     print 'Number of virt CPUs = %d' % infos[3]
            #     # print 'Number of virt CPUs = %s' % dom.maxVcpus()
            #     print 'CPU Time (in ns) = %d' % infos[4]
            #     print ' '
            for dom in domains:
                infos.append(dom.info())
        except Exception, e:
            print e
        return infos
    
    if __name__ == '__main__':
        infos = get_VM_infos('10.2.3.250', 16509)
        print infos
  • 相关阅读:
    ES5和ES6中的静态方法、类、单例模式
    Koa 应用生成器以及 Koa 路由模块化
    封装 Koa操作Mongodb数据库的DB类库
    Koa Session 的使用
    Koa 中 Cookie 的使用
    koa art-template 模板引擎
    koa koa-static 静态资源中间件
    koa post 提交数据 koa-bodyparser 中间件的使用
    koa ejs 模板引擎
    详解express与koa中间件执行顺序模式分析
  • 原文地址:https://www.cnblogs.com/Jerryshome/p/3484204.html
Copyright © 2011-2022 走看看