Example #1
0
    def deleteSnapshot(self, id, name):
        if self.isCurrentStorageAction(id):
            raise Exception("Can't delete a snapshot from a locked machine")
        newname = '%s_%s' % (name, 'DELETING')
        self._renameSnapshot(id, name, newname)
        name = newname
        domain = self._get_domain(id)
        snapshot = domain.snapshotLookupByName(name, 0)
        snapshotfiles = self._getSnapshotDisks(id, name)
        volumes = []
        todelete = []
        for snapshotfile in snapshotfiles:
            is_root_volume = self._isRootVolume(domain,
                                                snapshotfile['file'].path)
            if not is_root_volume:
                print('Blockcommit from %s to %s' %
                      (snapshotfile['file'].path,
                       snapshotfile['file'].backing_file_path))
                result = domain.blockCommit(
                    snapshotfile['name'],
                    snapshotfile['file'].backing_file_path,
                    snapshotfile['file'].path)
                todelete.append(snapshotfile['file'].path)
                volumes.append(snapshotfile['name'])
            else:
                # we can't use blockcommit on topsnapshots
                new_base = Qcow2(
                    snapshotfile['file'].backing_file_path).backing_file_path
                todelete.append(snapshotfile['file'].backing_file_path)
                if not new_base:
                    continue
                print('Blockrebase from %s' % new_base)
                flags = libvirt.VIR_DOMAIN_BLOCK_REBASE_COPY | libvirt.VIR_DOMAIN_BLOCK_REBASE_REUSE_EXT | libvirt.VIR_DOMAIN_BLOCK_REBASE_SHALLOW
                result = domain.blockRebase(snapshotfile['name'], new_base,
                                            flags)
                volumes.append(snapshotfile['name'])

        while not self._block_job_domain_info(domain, volumes):
            time.sleep(0.5)

        # we can undefine the snapshot
        snapshot.delete(flags=libvirt.VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA)
        for disk in todelete:
            if os.path.exists(disk):
                os.remove(disk)
        return True
Example #2
0
 def _clone(self, id, name, clonefrom):
     domain = self.connection.lookupByUUIDString(id)
     domainconfig = domain.XMLDesc()
     name = "%s_%s.qcow2" % (name, time.time())
     destination_path = os.path.join(self.templatepath, name)
     if domain.state()[0] in [
             libvirt.VIR_DOMAIN_SHUTDOWN,
             libvirt.VIR_DOMAIN_SHUTOFF,
             libvirt.VIR_DOMAIN_CRASHED,
             libvirt.VIR_DOMAIN_PAUSED,
     ] or not self._isRootVolume(domain, clonefrom):
         if not self.isLocked(id):
             lock = self._lockDomain(id)
             if lock != LOCKCREATED:
                 raise Exception("Failed to create lock: %s" % str(lock))
         else:
             raise Exception("Can't perform this action on a locked domain")
         q2 = Qcow2(clonefrom)
         try:
             q2.export(destination_path)
         finally:
             if self.isLocked(id):
                 self._unlockDomain(id)
     else:
         domain.undefine()
         try:
             domain.blockRebase(clonefrom, destination_path, 0,
                                libvirt.VIR_DOMAIN_BLOCK_REBASE_COPY)
             rebasedone = False
             while not rebasedone:
                 rebasedone = self._block_job_info(domain, clonefrom)
             domain.blockJobAbort(clonefrom, 0)
         except BaseException:
             self.connection.defineXML(domainconfig)
             raise
         self.connection.defineXML(domainconfig)
     return destination_path