libvirt热迁移源码和命令以及相关参数调研

调研版本:基于libvirt4.5.0
之前在文章对openstack调用libvirt研究中
https://blog.csdn.net/jmz19910110/article/details/115304610
找到了最后封装的调用virt api的函数
对应找到libvirt源码中的迁移函数定义

在src/libvirt-domain.c中

int
virDomainMigrateToURI3(virDomainPtr domain,
                       const char *dconnuri,
                       virTypedParameterPtr params,
                       unsigned int nparams,
                       unsigned int flags)
{
    VIR_DOMAIN_DEBUG(domain, "dconnuri=%s, params=%p, nparms=%u flags=0x%x",
                     NULLSTR(dconnuri), params, nparams, flags);
    VIR_TYPED_PARAMS_DEBUG(params, nparams);

    virResetLastError();

    /* First checkout the source */
    virCheckDomainReturn(domain, -1);
    virCheckReadOnlyGoto(domain->conn->flags, error);

    if (virDomainMigrateUnmanagedCheckCompat(domain, flags) < 0)
        goto error;

    if (flags & VIR_MIGRATE_PEER2PEER)
        virCheckNonNullArgGoto(dconnuri, error);
    else
        dconnuri = NULL;

    if (virDomainMigrateUnmanagedParams(domain, dconnuri,
                                        params, nparams, flags) < 0)
        goto error;

    return 0;

 error:
    virDispatchError(domain->conn);
    return -1;
}

但是这里是libvirt的C源码,我们在使用其他工具或者服务调用时都是使用对应语言的bind api,
下面给出一个用python调用libvirt 以及热迁移的demo小例子,目前迁移部分因为使用的是nfs的共享存储,所以在我们的ceph环境上跑不起来还有待改进

#!/usr/bin/python
#-*-coding:utf-8-*- 
import libvirt
import sys
 
def createConnection():
    conn = libvirt.openReadOnly(None)
    if conn == None:
        print '连接KVM失败'
        sys.exit(1)
    else:
        print '连接KVM成功'
        return conn
 
def closeConnection(conn):
    print ''
    try:
        conn.close()
    except:
        print '关闭KVM连接失败'
        return 1
    print 'KVM连接关闭'
 
def getDomInfoByName(conn, name):
    print ''
    print '通过KVM虚拟机名字获得虚拟机信息'
    try:
        domain = conn.lookupByName(name)
    except:
        print ("获取KVM域失败{}".format(name))
        return 1
         
    print "虚拟机ID: {}虚拟机名字: {}".format ( domain.ID(), domain.name())
    print "虚拟机状态: {}".format(domain.state(0))
    print "虚拟机信息: {}".format(domain.info())
    print "虚拟机最大内存:{} MB".format((domain.maxMemory()/1024))
    print "虚拟机内存状态:{}".format(domain.memoryStats())
    print "虚拟机CPU核数:{}".format(domain.maxVcpus())
def getDomInfoByID(conn, id):
    print ''
    print '通过KVM虚拟机ID获得虚拟机信息'
    try:
        domain = conn.lookupByID(id)
    except:
        print 'Failed to find the Domain with ID "%"' % id
        return 1
         
    print "虚拟机ID: {}虚拟机名字: {}".format ( domain.ID(), domain.name())
    print "虚拟机状态: {}".format(domain.state(0))
    print "虚拟机信息: {}".format(domain.info())
    print "虚拟机最大内存:{} MB".format((domain.maxMemory()/1024))
    print "虚拟机内存状态:{}".format(domain.memoryStats())
    print "虚拟机CPU核数:{}".format(domain.maxVcpus())
if __name__ == '__main__':
    name1 = sys.argv[2]

    id1 = int(sys.argv[1])

    print "获得域信息通过libvirt python API"
    conn = createConnection()
    #getDomInfoByName(conn, name1)
    #getDomInfoByID(conn, id1)
    conn_004 = libvirt.open('qemu+tcp://172.27.132.201/system')
    conn_005 = libvirt.open('qemu+tcp://172.27.132.202/system')

    vm_domain = conn_004.lookupByName('idv-85c5e0f9-1ac5-4163-bd29-3ce4738bd3f8')
    print vm_domain.migrate(conn_005,True,'idv-85c5e0f9-1ac5-4163-bd29-3ce4738bd3f8',None,0)
    print "end..."

    closeConnection(conn)

如果直接使用virsh的命令行进行热迁移的话,命令如下:

virsh migrate --live [vm_name] qemu+tcp://controller2/system --verbose

vm_name可以用virsh list查看 目前这个命令也是遇到同样问题就是和ceph的环境不匹配,需要进一步调研如何在ceph的环境下使用
libvirt提供的热迁移接口包括以下参数,可以从openstack的 nova/virt/libvirt/guest.py的migrate函数里的注释部分查看到一部分参数定义和解释
也可以直接virsh migrate --help查看

           VIR_MIGRATE_LIVE Do not pause the VM during migration
           VIR_MIGRATE_PEER2PEER Direct connection between source &
                                 destination hosts
           VIR_MIGRATE_TUNNELLED Tunnel migration data over the
                                 libvirt RPC channel
           VIR_MIGRATE_PERSIST_DEST If the migration is successful,
                                    persist the domain on the
                                    destination host.
           VIR_MIGRATE_UNDEFINE_SOURCE If the migration is successful,
                                       undefine the domain on the
                                       source host.
           VIR_MIGRATE_PAUSED Leave the domain suspended on the remote
                              side.
           VIR_MIGRATE_NON_SHARED_DISK Migration with non-shared
                                       storage with full disk copy
           VIR_MIGRATE_NON_SHARED_INC Migration with non-shared
                                      storage with incremental disk
                                      copy
           VIR_MIGRATE_CHANGE_PROTECTION Protect against domain
                                         configuration changes during
                                         the migration process (set
                                         automatically when
                                         supported).
           VIR_MIGRATE_UNSAFE Force migration even if it is considered
                              unsafe.
           VIR_MIGRATE_OFFLINE Migrate offline

–tunnelled 是否开启隧道通信,如果不开启,则是在两台hyperV之间建立直接通讯传递数据来进行迁移

如果加上这个命令,那么就是使用livirtd的 RPC隧道进行通信

–direct 猜测为是否直接管理迁移,对应文档中说的直连管理控制

–p2p 是否开启点对点迁移,对应文档中说的托管管理控制

你可能感兴趣的:(libvirt热迁移源码和命令以及相关参数调研)