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
  • 相关阅读:
    (转)原始图像数据和PDF中的图像数据
    itextSharp 附pdf文件解析
    (转)pdf文件结构
    【iCore1S 双核心板_ARM】例程九:DAC实验——输出直流电压
    【iCore4 双核心板_FPGA】例程七:状态机实验——状态机使用
    【iCore4 双核心板_FPGA】例程六:触发器实验——触发器的使用
    【iCore4 双核心板_ARM】例程八:定时器PWM实验——呼吸灯
    【iCore4 双核心板_ARM】例程七:WWDG看门狗实验——复位ARM
    【iCore1S 双核心板_FPGA】例程七:基础逻辑门实验——逻辑门使用
    【iCore1S 双核心板_FPGA】例程六:状态机实验——状态机使用
  • 原文地址:https://www.cnblogs.com/Jerryshome/p/3484204.html
Copyright © 2011-2022 走看看