Example #1
0
def test():

    # Create a Lock
    lock = Lock("test")

    # Should not be yet held.
    assert lock.held() == False

    # Go get it
    lock.acquire()

    # Second lock shall throw in debug mode.
    try:
        lock.acquire()
    except AssertionError as e:
        if str(e) != flock.WriteLock.ERROR_ISLOCKED:
            raise
    else:
        raise AssertionError("Reaquired a locked lock")

    lock.release()

    Lock.cleanup()
Example #2
0
File: VDI.py Project: chandrikas/sm
    def configure_blocktracking(self, sr_uuid, vdi_uuid, enable):
        """Function for configuring blocktracking"""
        import blktap2
        vdi_ref = self.sr.srcmd.params['vdi_ref']

        # Check if raw VDI or snapshot
        if self.vdi_type == vhdutil.VDI_TYPE_RAW or \
            self.session.xenapi.VDI.get_is_a_snapshot(vdi_ref):
            raise xs_errors.XenError('VDIType',
                                     opterr='Raw VDI or snapshot not permitted')

        # Check if already enabled
        if self._get_blocktracking_status() == enable:
            return

        # Save disk state before pause
        disk_state = blktap2.VDI.tap_status(self.session, vdi_uuid)

        if not blktap2.VDI.tap_pause(self.session, sr_uuid, vdi_uuid):
            error = "Failed to pause VDI %s" % vdi_uuid
            raise xs_errors.XenError('CBTActivateFailed', opterr=error)
        logfile = None

        try:
            if enable:
                try:
                    # Check available space
                    self._ensure_cbt_space()
                    logfile = self._create_cbt_log()
                    # Set consistency
                    if disk_state:
                        util.SMlog("Setting consistency of cbtlog file to False for VDI: %s"
                                   % self.uuid)
                        logpath = self._get_cbt_logpath(self.uuid)
                        self._cbt_op(self.uuid, cbtutil.set_cbt_consistency,
                                     logpath, False)
                except Exception as error:
                    self._delete_cbt_log()
                    raise xs_errors.XenError('CBTActivateFailed',
                                             opterr=str(error))
            else:
                from lock import Lock
                lock = Lock("cbtlog", str(vdi_uuid))
                lock.acquire()
                try:
                    # Find parent of leaf metadata file, if any,
                    # and nullify its successor
                    logpath = self._get_cbt_logpath(self.uuid)
                    parent = self._cbt_op(self.uuid,
                                          cbtutil.get_cbt_parent, logpath)
                    self._delete_cbt_log()
                    parent_path = self._get_cbt_logpath(parent)
                    if self._cbt_log_exists(parent_path):
                        self._cbt_op(parent, cbtutil.set_cbt_child,
                                     parent_path, uuid.UUID(int=0))
                except Exception as error:
                    raise xs_errors.XenError('CBTDeactivateFailed', str(error))
                finally:
                    lock.release()
                    lock.cleanup("cbtlog", str(vdi_uuid))
        finally:
            blktap2.VDI.tap_unpause(self.session, sr_uuid, vdi_uuid)
Example #3
0
File: VDI.py Project: chandrikas/sm
    def delete(self, sr_uuid, vdi_uuid, data_only=False):
        """Delete this VDI.

        This operation IS idempotent and should succeed if the VDI
        exists and can be deleted or if the VDI does not exist. It is
        the responsibility of the higher-level management tool to
        ensure that the detach() operation has been explicitly called
        prior to deletion, otherwise the delete() will fail if the
        disk is still attached.
        """
        import blktap2
        from lock import Lock

        if data_only == False and self._get_blocktracking_status():
            logpath = self._get_cbt_logpath(vdi_uuid)
            parent_uuid = self._cbt_op(vdi_uuid, cbtutil.get_cbt_parent,
                                       logpath)
            parent_path = self._get_cbt_logpath(parent_uuid)
            child_uuid = self._cbt_op(vdi_uuid, cbtutil.get_cbt_child, logpath)
            child_path = self._get_cbt_logpath(child_uuid)

            lock = Lock("cbtlog", str(vdi_uuid))

            if self._cbt_log_exists(parent_path):
                self._cbt_op(parent_uuid, cbtutil.set_cbt_child,
                             parent_path, child_uuid)

            if self._cbt_log_exists(child_path):
                self._cbt_op(child_uuid, cbtutil.set_cbt_parent,
                             child_path, parent_uuid)
                lock.acquire()
                try:
                    # Coalesce contents of bitmap with child's bitmap
                    # Check if child bitmap is currently attached
                    paused_for_coalesce = False
                    consistent = self._cbt_op(child_uuid,
                                              cbtutil.get_cbt_consistency,
                                              child_path)
                    if not consistent:
                        if not blktap2.VDI.tap_pause(self.session,
                                                     sr_uuid, child_uuid):
                            raise util.SMException("failed to pause VDI %s")
                        paused_for_coalesce = True
                    self._activate_cbt_log(self._get_cbt_logname(vdi_uuid))
                    self._cbt_op(child_uuid, cbtutil.coalesce_bitmap,
                                 logpath, child_path)
                    lock.release()
                except util.CommandException:
                    # If there is an exception in coalescing,
                    # CBT log file is not deleted and pointers are reset
                    # to what they were
                    util.SMlog("Exception in coalescing bitmaps on VDI delete,"
                               " restoring to previous state")
                    try:
                        if self._cbt_log_exists(parent_path):
                            self._cbt_op(parent_uuid, cbtutil.set_cbt_child,
                                         parent_path, vdi_uuid)
                        if self._cbt_log_exists(child_path):
                            self._cbt_op(child_uuid, cbtutil.set_cbt_parent,
                                         child_path, vdi_uuid)
                    finally:
                        lock.release()
                        lock.cleanup("cbtlog", str(vdi_uuid))
                    return
                finally:
                    # Unpause tapdisk if it wasn't originally paused
                    if paused_for_coalesce:
                        blktap2.VDI.tap_unpause(self.session, sr_uuid,
                                                child_uuid)
            lock.acquire()
            try:
                self._delete_cbt_log()
            finally:
                lock.release()
                lock.cleanup("cbtlog", str(vdi_uuid))
Example #4
0
    def configure_blocktracking(self, sr_uuid, vdi_uuid, enable):
        """Function for configuring blocktracking"""
        import blktap2
        vdi_ref = self.sr.srcmd.params['vdi_ref']

        # Check if raw VDI or snapshot
        if self.vdi_type == vhdutil.VDI_TYPE_RAW or \
            self.session.xenapi.VDI.get_is_a_snapshot(vdi_ref):
            raise xs_errors.XenError(
                'VDIType', opterr='Raw VDI or snapshot not permitted')

        # Check if already enabled
        if self._get_blocktracking_status() == enable:
            return

        # Save disk state before pause
        disk_state = blktap2.VDI.tap_status(self.session, vdi_uuid)

        if not blktap2.VDI.tap_pause(self.session, sr_uuid, vdi_uuid):
            error = "Failed to pause VDI %s" % vdi_uuid
            raise xs_errors.XenError('CBTActivateFailed', opterr=error)
        logfile = None

        try:
            if enable:
                try:
                    # Check available space
                    self._ensure_cbt_space()
                    logfile = self._create_cbt_log()
                    # Set consistency
                    if disk_state:
                        util.SMlog(
                            "Setting consistency of cbtlog file to False for VDI: %s"
                            % self.uuid)
                        logpath = self._get_cbt_logpath(self.uuid)
                        self._cbt_op(self.uuid, cbtutil.set_cbt_consistency,
                                     logpath, False)
                except Exception as error:
                    self._delete_cbt_log()
                    raise xs_errors.XenError('CBTActivateFailed',
                                             opterr=str(error))
            else:
                from lock import Lock
                lock = Lock("cbtlog", str(vdi_uuid))
                lock.acquire()
                try:
                    # Find parent of leaf metadata file, if any,
                    # and nullify its successor
                    logpath = self._get_cbt_logpath(self.uuid)
                    parent = self._cbt_op(self.uuid, cbtutil.get_cbt_parent,
                                          logpath)
                    self._delete_cbt_log()
                    parent_path = self._get_cbt_logpath(parent)
                    if self._cbt_log_exists(parent_path):
                        self._cbt_op(parent, cbtutil.set_cbt_child,
                                     parent_path, uuid.UUID(int=0))
                except Exception as error:
                    raise xs_errors.XenError('CBTDeactivateFailed', str(error))
                finally:
                    lock.release()
                    lock.cleanup("cbtlog", str(vdi_uuid))
        finally:
            blktap2.VDI.tap_unpause(self.session, sr_uuid, vdi_uuid)
Example #5
0
    def delete(self, sr_uuid, vdi_uuid, data_only=False):
        """Delete this VDI.

        This operation IS idempotent and should succeed if the VDI
        exists and can be deleted or if the VDI does not exist. It is
        the responsibility of the higher-level management tool to
        ensure that the detach() operation has been explicitly called
        prior to deletion, otherwise the delete() will fail if the
        disk is still attached.
        """
        import blktap2
        from lock import Lock

        if data_only == False and self._get_blocktracking_status():
            logpath = self._get_cbt_logpath(vdi_uuid)
            parent_uuid = self._cbt_op(vdi_uuid, cbtutil.get_cbt_parent,
                                       logpath)
            parent_path = self._get_cbt_logpath(parent_uuid)
            child_uuid = self._cbt_op(vdi_uuid, cbtutil.get_cbt_child, logpath)
            child_path = self._get_cbt_logpath(child_uuid)

            lock = Lock("cbtlog", str(vdi_uuid))

            if self._cbt_log_exists(parent_path):
                self._cbt_op(parent_uuid, cbtutil.set_cbt_child, parent_path,
                             child_uuid)

            if self._cbt_log_exists(child_path):
                self._cbt_op(child_uuid, cbtutil.set_cbt_parent, child_path,
                             parent_uuid)
                lock.acquire()
                try:
                    # Coalesce contents of bitmap with child's bitmap
                    # Check if child bitmap is currently attached
                    paused_for_coalesce = False
                    consistent = self._cbt_op(child_uuid,
                                              cbtutil.get_cbt_consistency,
                                              child_path)
                    if not consistent:
                        if not blktap2.VDI.tap_pause(self.session, sr_uuid,
                                                     child_uuid):
                            raise util.SMException("failed to pause VDI %s")
                        paused_for_coalesce = True
                    self._activate_cbt_log(self._get_cbt_logname(vdi_uuid))
                    self._cbt_op(child_uuid, cbtutil.coalesce_bitmap, logpath,
                                 child_path)
                    lock.release()
                except util.CommandException:
                    # If there is an exception in coalescing,
                    # CBT log file is not deleted and pointers are reset
                    # to what they were
                    util.SMlog("Exception in coalescing bitmaps on VDI delete,"
                               " restoring to previous state")
                    try:
                        if self._cbt_log_exists(parent_path):
                            self._cbt_op(parent_uuid, cbtutil.set_cbt_child,
                                         parent_path, vdi_uuid)
                        if self._cbt_log_exists(child_path):
                            self._cbt_op(child_uuid, cbtutil.set_cbt_parent,
                                         child_path, vdi_uuid)
                    finally:
                        lock.release()
                        lock.cleanup("cbtlog", str(vdi_uuid))
                    return
                finally:
                    # Unpause tapdisk if it wasn't originally paused
                    if paused_for_coalesce:
                        blktap2.VDI.tap_unpause(self.session, sr_uuid,
                                                child_uuid)
            lock.acquire()
            try:
                self._delete_cbt_log()
            finally:
                lock.release()
                lock.cleanup("cbtlog", str(vdi_uuid))
Example #6
0
def test():

    # Create a Lock
    lock = Lock("test")

    # Should not be yet held.
    assert lock.held() == False

    # Go get it
    lock.acquire()

    # Second lock shall throw in debug mode.
    try:
        lock.acquire()
    except AssertionError, e:
        if str(e) != flock.WriteLock.ERROR_ISLOCKED:
            raise
    else:
        raise AssertionError("Reaquired a locked lock")

    lock.release()

    Lock.cleanup()


if __name__ == '__main__':
    print >> sys.stderr, "Running self tests..."
    test()
    print >> sys.stderr, "OK."