def _swap_volume(self, domain, disk_path, new_path, resize_to):
    'Swap existing disk with a new block device.'
    xml = domain.XMLDesc((libvirt.VIR_DOMAIN_XML_INACTIVE | libvirt.VIR_DOMAIN_XML_SECURE))
    try:
        domain.blockJobAbort(disk_path, 0)
    except Exception:
        pass
    try:
        if domain.isPersistent():
            domain.undefine()
        domain.blockRebase(disk_path, new_path, 0, (libvirt.VIR_DOMAIN_BLOCK_REBASE_COPY | libvirt.VIR_DOMAIN_BLOCK_REBASE_REUSE_EXT))
        while self._wait_for_block_job(domain, disk_path):
            time.sleep(0.5)
        domain.blockJobAbort(disk_path, libvirt.VIR_DOMAIN_BLOCK_JOB_ABORT_PIVOT)
        if resize_to:
            while self._wait_for_block_job(domain, disk_path, wait_for_job_clean=True):
                time.sleep(0.5)
            domain.blockResize(disk_path, ((resize_to * units.Gi) / units.Ki))
    finally:
        self._conn.defineXML(xml)
Swap existing disk with a new block device.
def _swap_volume(self, domain, disk_path, new_path, resize_to):
    
    xml = domain.XMLDesc((libvirt.VIR_DOMAIN_XML_INACTIVE | libvirt.VIR_DOMAIN_XML_SECURE))
    try:
        domain.blockJobAbort(disk_path, 0)
    except Exception:
        pass
    try:
        if domain.isPersistent():
            domain.undefine()
        domain.blockRebase(disk_path, new_path, 0, (libvirt.VIR_DOMAIN_BLOCK_REBASE_COPY | libvirt.VIR_DOMAIN_BLOCK_REBASE_REUSE_EXT))
        while self._wait_for_block_job(domain, disk_path):
            time.sleep(0.5)
        domain.blockJobAbort(disk_path, libvirt.VIR_DOMAIN_BLOCK_JOB_ABORT_PIVOT)
        if resize_to:
            while self._wait_for_block_job(domain, disk_path, wait_for_job_clean=True):
                time.sleep(0.5)
            domain.blockResize(disk_path, ((resize_to * units.Gi) / units.Ki))
    finally:
        self._conn.defineXML(xml)