def copy(self, dest_ioctx, dest_name, features=0, order=None, stripe_unit=0, stripe_count=0): """ Copy the image to another location. :param dest_ioctx: determines which pool to copy into :type dest_ioctx: :class:`rados.Ioctx` :param dest_name: the name of the copy :type dest_name: str :param features: bitmask of features to enable; if set, must include layering :type features: int :param order: the image is split into (2**order) byte objects :type order: int :param stripe_unit: stripe unit in bytes (default 0 for object size) :type stripe_unit: int :param stripe_count: objects to stripe over before looping :type stripe_count: int :raises: :class:`TypeError` :raises: :class:`InvalidArgument` :raises: :class:`ImageExists` :raises: :class:`FunctionNotSupported` :raises: :class:`ArgumentOutOfRange` """ if order is None: order = 0 if not isinstance(dest_name, str_type): raise TypeError('dest_name must be a string') has_copy3 = hasattr(self.librbd, 'rbd_copy3') if (stripe_unit != 0 or stripe_count != 0) and not has_copy3: raise FunctionNotSupported('installed version of librbd does' ' not support stripe unit or count') if has_copy3: opts = c_void_p() self.librbd.rbd_image_options_create(byref(opts)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FEATURES, c_uint64(features)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_ORDER, c_uint64(order)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_UNIT, c_uint64(stripe_unit)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_COUNT, c_uint64(stripe_count)) ret = self.librbd.rbd_copy3(self.image, dest_ioctx.io, cstr(dest_name), opts) self.librbd.rbd_image_options_get_uint64(opts, RBD_IMAGE_OPTION_ORDER, byref(c_uint64(order))) self.librbd.rbd_image_options_destroy(opts) else: ret = self.librbd.rbd_copy(self.image, dest_ioctx.io, cstr(dest_name)) if ret < 0: raise make_ex(ret, 'error copying image %s to %s' % (self.name, dest_name))
def break_lock(self, client, cookie): """ Release a lock held by another rados client. """ if not isinstance(client, str_type): raise TypeError('client must be a string') if not isinstance(cookie, str_type): raise TypeError('cookie must be a string') ret = self.librbd.rbd_break_lock(self.image, cstr(client), cstr(cookie)) if ret < 0: raise make_ex(ret, 'error unlocking image')
def lock_shared(self, cookie, tag): """ Take a shared lock on the image. The tag must match that of the existing lockers, if any. :raises: :class:`ImageBusy` if a different client or cookie locked it :class:`ImageExists` if the same client and cookie locked it """ if not isinstance(cookie, str_type): raise TypeError('cookie must be a string') if not isinstance(tag, str_type): raise TypeError('tag must be a string') ret = self.librbd.rbd_lock_shared(self.image, cstr(cookie), cstr(tag)) if ret < 0: raise make_ex(ret, 'error acquiring shared lock on image')
def rename_snap(self, srcname, dstname): """ rename a snapshot of the image. :param srcname: the src name of the snapshot :type srcname: str :param dstname: the dst name of the snapshot :type dstname: str :raises: :class:`ImageExists` """ if not isinstance(srcname, str_type): raise TypeError('src name must be a string') if not isinstance(dstname, str_type): raise TypeError('dst name must be a string') ret = self.librbd.rbd_snap_rename(self.image, cstr(srcname), cstr(dstname)) if ret != 0: raise make_ex(ret, 'error renaming snapshot of %s from %s to %s' % (self.name, srcname, dstname))
def rename(self, ioctx, src, dest): """ Rename an RBD image. :param ioctx: determines which RADOS pool the image is in :type ioctx: :class:`rados.Ioctx` :param src: the current name of the image :type src: str :param dest: the new name of the image :type dest: str :raises: :class:`ImageNotFound`, :class:`ImageExists` """ if not isinstance(src, str_type) or not isinstance(dest, str_type): raise TypeError('src and dest must be strings') ret = self.librbd.rbd_rename(ioctx.io, cstr(src), cstr(dest)) if ret != 0: raise make_ex(ret, 'error renaming image')
def diff_iterate(self, offset, length, from_snapshot, iterate_cb, include_parent = True, whole_object = False): """ Iterate over the changed extents of an image. This will call iterate_cb with three arguments: (offset, length, exists) where the changed extent starts at offset bytes, continues for length bytes, and is full of data (if exists is True) or zeroes (if exists is False). If from_snapshot is None, it is interpreted as the beginning of time and this generates all allocated extents. The end version is whatever is currently selected (via set_snap) for the image. Raises :class:`InvalidArgument` if from_snapshot is after the currently set snapshot. Raises :class:`ImageNotFound` if from_snapshot is not the name of a snapshot of the image. :param offset: start offset in bytes :type offset: int :param length: size of region to report on, in bytes :type length: int :param from_snapshot: starting snapshot name, or None :type from_snapshot: str or None :param iterate_cb: function to call for each extent :type iterate_cb: function acception arguments for offset, length, and exists :param include_parent: True if full history diff should include parent :type include_parent: bool :param whole_object: True if diff extents should cover whole object :type whole_object: bool :raises: :class:`InvalidArgument`, :class:`IOError`, :class:`ImageNotFound` """ if from_snapshot is not None and not isinstance(from_snapshot, str_type): raise TypeError('client must be a string') RBD_DIFF_CB = CFUNCTYPE(c_int, c_uint64, c_size_t, c_int, c_void_p) cb_holder = DiffIterateCB(iterate_cb) cb = RBD_DIFF_CB(cb_holder.callback) ret = self.librbd.rbd_diff_iterate2(self.image, cstr(from_snapshot), c_uint64(offset), c_uint64(length), c_uint8(include_parent), c_uint8(whole_object), cb, c_void_p(None)) if ret < 0: msg = 'error generating diff from snapshot %s' % from_snapshot raise make_ex(ret, msg)
def unlock(self, cookie): """ Release a lock on the image that was locked by this rados client. """ if not isinstance(cookie, str_type): raise TypeError('cookie must be a string') ret = self.librbd.rbd_unlock(self.image, cstr(cookie)) if ret < 0: raise make_ex(ret, 'error unlocking image')
def __init__(self, ioctx, name, snapshot=None, read_only=False): """ Open the image at the given snapshot. If a snapshot is specified, the image will be read-only, unless :func:`Image.set_snap` is called later. If read-only mode is used, metadata for the :class:`Image` object (such as which snapshots exist) may become obsolete. See the C api for more details. To clean up from opening the image, :func:`Image.close` should be called. For ease of use, this is done automatically when an :class:`Image` is used as a context manager (see :pep:`343`). :param ioctx: determines which RADOS pool the image is in :type ioctx: :class:`rados.Ioctx` :param name: the name of the image :type name: str :param snapshot: which snapshot to read from :type snaphshot: str :param read_only: whether to open the image in read-only mode :type read_only: bool """ self.closed = True self.librbd = load_librbd() self.image = c_void_p() self.name = name if not isinstance(name, str_type): raise TypeError('name must be a string') if snapshot is not None and not isinstance(snapshot, str_type): raise TypeError('snapshot must be a string or None') if read_only: if not hasattr(self.librbd, 'rbd_open_read_only'): raise FunctionNotSupported('installed version of librbd does ' 'not support open in read-only mode') ret = self.librbd.rbd_open_read_only(ioctx.io, cstr(name), byref(self.image), cstr(snapshot)) else: ret = self.librbd.rbd_open(ioctx.io, cstr(name), byref(self.image), cstr(snapshot)) if ret != 0: raise make_ex(ret, 'error opening image %s at snapshot %s' % (name, snapshot)) self.closed = False
def lock_exclusive(self, cookie): """ Take an exclusive lock on the image. :raises: :class:`ImageBusy` if a different client or cookie locked it :class:`ImageExists` if the same client and cookie locked it """ if not isinstance(cookie, str_type): raise TypeError('cookie must be a string') ret = self.librbd.rbd_lock_exclusive(self.image, cstr(cookie)) if ret < 0: raise make_ex(ret, 'error acquiring exclusive lock on image')
def create_snap(self, name): """ Create a snapshot of the image. :param name: the name of the snapshot :type name: str :raises: :class:`ImageExists` """ if not isinstance(name, str_type): raise TypeError('name must be a string') ret = self.librbd.rbd_snap_create(self.image, cstr(name)) if ret != 0: raise make_ex(ret, 'error creating snapshot %s from %s' % (name, self.name))
def remove_snap(self, name): """ Delete a snapshot of the image. :param name: the name of the snapshot :type name: str :raises: :class:`IOError`, :class:`ImageBusy` """ if not isinstance(name, str_type): raise TypeError('name must be a string') ret = self.librbd.rbd_snap_remove(self.image, cstr(name)) if ret != 0: raise make_ex(ret, 'error removing snapshot %s from %s' % (name, self.name))
def unprotect_snap(self, name): """ Mark a snapshot unprotected. This allows it to be deleted if it was protected. :param name: the snapshot to unprotect :type name: str :raises: :class:`IOError`, :class:`ImageNotFound` """ if not isinstance(name, str_type): raise TypeError('name must be a string') ret = self.librbd.rbd_snap_unprotect(self.image, cstr(name)) if ret != 0: raise make_ex(ret, 'error unprotecting snapshot %s@%s' % (self.name, name))
def set_snap(self, name): """ Set the snapshot to read from. Writes will raise ReadOnlyImage while a snapshot is set. Pass None to unset the snapshot (reads come from the current image) , and allow writing again. :param name: the snapshot to read from, or None to unset the snapshot :type name: str or None """ if name is not None and not isinstance(name, str_type): raise TypeError('name must be a string') ret = self.librbd.rbd_snap_set(self.image, cstr(name)) if ret != 0: raise make_ex(ret, 'error setting image %s to snapshot %s' % (self.name, name))
def rollback_to_snap(self, name): """ Revert the image to its contents at a snapshot. This is a potentially expensive operation, since it rolls back each object individually. :param name: the snapshot to rollback to :type name: str :raises: :class:`IOError` """ if not isinstance(name, str_type): raise TypeError('name must be a string') ret = self.librbd.rbd_snap_rollback(self.image, cstr(name)) if ret != 0: raise make_ex(ret, 'error rolling back image %s to snapshot %s' % (self.name, name))
def is_protected_snap(self, name): """ Find out whether a snapshot is protected from deletion. :param name: the snapshot to check :type name: str :returns: bool - whether the snapshot is protected :raises: :class:`IOError`, :class:`ImageNotFound` """ if not isinstance(name, str_type): raise TypeError('name must be a string') is_protected = c_int() ret = self.librbd.rbd_snap_is_protected(self.image, cstr(name), byref(is_protected)) if ret != 0: raise make_ex(ret, 'error checking if snapshot %s@%s is protected' % (self.name, name)) return is_protected.value == 1
def remove(self, ioctx, name): """ Delete an RBD image. This may take a long time, since it does not return until every object that comprises the image has been deleted. Note that all snapshots must be deleted before the image can be removed. If there are snapshots left, :class:`ImageHasSnapshots` is raised. If the image is still open, or the watch from a crashed client has not expired, :class:`ImageBusy` is raised. :param ioctx: determines which RADOS pool the image is in :type ioctx: :class:`rados.Ioctx` :param name: the name of the image to remove :type name: str :raises: :class:`ImageNotFound`, :class:`ImageBusy`, :class:`ImageHasSnapshots` """ if not isinstance(name, str_type): raise TypeError('name must be a string') ret = self.librbd.rbd_remove(ioctx.io, cstr(name)) if ret != 0: raise make_ex(ret, 'error removing image')
def create(self, ioctx, name, size, order=None, old_format=True, features=0, stripe_unit=0, stripe_count=0): """ Create an rbd image. :param ioctx: the context in which to create the image :type ioctx: :class:`rados.Ioctx` :param name: what the image is called :type name: str :param size: how big the image is in bytes :type size: int :param order: the image is split into (2**order) byte objects :type order: int :param old_format: whether to create an old-style image that is accessible by old clients, but can't use more advanced features like layering. :type old_format: bool :param features: bitmask of features to enable :type features: int :param stripe_unit: stripe unit in bytes (default 0 for object size) :type stripe_unit: int :param stripe_count: objects to stripe over before looping :type stripe_count: int :raises: :class:`ImageExists` :raises: :class:`TypeError` :raises: :class:`InvalidArgument` :raises: :class:`FunctionNotSupported` """ if order is None: order = 0 if not isinstance(name, str_type): raise TypeError('name must be a string') if old_format: if features != 0 or stripe_unit != 0 or stripe_count != 0: raise InvalidArgument('format 1 images do not support feature' ' masks or non-default striping') ret = self.librbd.rbd_create(ioctx.io, cstr(name), c_uint64(size), byref(c_int(order))) else: if not hasattr(self.librbd, 'rbd_create2'): raise FunctionNotSupported('installed version of librbd does' ' not support format 2 images') has_create4 = hasattr(self.librbd, 'rbd_create4') has_create3 = hasattr(self.librbd, 'rbd_create3') if (stripe_unit != 0 or stripe_count != 0) and not has_create3: raise FunctionNotSupported('installed version of librbd does' ' not support stripe unit or count') if has_create4: format = old_format and 1 or 2 opts = c_void_p() self.librbd.rbd_image_options_create(byref(opts)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FORMAT, c_uint64(format)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FEATURES, c_uint64(features)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_ORDER, c_uint64(order)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_UNIT, c_uint64(stripe_unit)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_COUNT, c_uint64(stripe_count)) ret = self.librbd.rbd_create4(ioctx.io, cstr(name), c_uint64(size), opts) self.librbd.rbd_image_options_get_uint64(opts, RBD_IMAGE_OPTION_ORDER, byref(c_uint64(order))) self.librbd.rbd_image_options_destroy(opts) elif has_create3: ret = self.librbd.rbd_create3(ioctx.io, cstr(name), c_uint64(size), c_uint64(features), byref(c_int(order)), c_uint64(stripe_unit), c_uint64(stripe_count)) else: ret = self.librbd.rbd_create2(ioctx.io, cstr(name), c_uint64(size), c_uint64(features), byref(c_int(order))) if ret < 0: raise make_ex(ret, 'error creating image')
def clone(self, p_ioctx, p_name, p_snapname, c_ioctx, c_name, features=0, order=None, stripe_unit=0, stripe_count=0): """ Clone a parent rbd snapshot into a COW sparse child. :param p_ioctx: the parent context that represents the parent snap :type ioctx: :class:`rados.Ioctx` :param p_name: the parent image name :type name: str :param p_snapname: the parent image snapshot name :type name: str :param c_ioctx: the child context that represents the new clone :type ioctx: :class:`rados.Ioctx` :param c_name: the clone (child) name :type name: str :param features: bitmask of features to enable; if set, must include layering :type features: int :param order: the image is split into (2**order) byte objects :type order: int :param stripe_unit: stripe unit in bytes (default 0 for object size) :type stripe_unit: int :param stripe_count: objects to stripe over before looping :type stripe_count: int :raises: :class:`TypeError` :raises: :class:`InvalidArgument` :raises: :class:`ImageExists` :raises: :class:`FunctionNotSupported` :raises: :class:`ArgumentOutOfRange` """ if order is None: order = 0 if not isinstance(p_snapname, str_type) or not isinstance(p_name, str_type): raise TypeError('parent name and snapname must be strings') if not isinstance(c_name, str_type): raise TypeError('child name must be a string') has_clone3 = hasattr(self.librbd, 'rbd_clone3') if (stripe_unit != 0 or stripe_count != 0) and not has_clone3: raise FunctionNotSupported('installed version of librbd does' ' not support stripe unit or count') if has_clone3: opts = c_void_p() self.librbd.rbd_image_options_create(byref(opts)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_FEATURES, c_uint64(features)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_ORDER, c_uint64(order)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_UNIT, c_uint64(stripe_unit)) self.librbd.rbd_image_options_set_uint64(opts, RBD_IMAGE_OPTION_STRIPE_COUNT, c_uint64(stripe_count)) ret = self.librbd.rbd_clone3(p_ioctx.io, cstr(p_name), cstr(p_snapname), c_ioctx.io, cstr(c_name), opts) self.librbd.rbd_image_options_get_uint64(opts, RBD_IMAGE_OPTION_ORDER, byref(c_uint64(order))) self.librbd.rbd_image_options_destroy(opts) else: ret = self.librbd.rbd_clone(p_ioctx.io, cstr(p_name), cstr(p_snapname), c_ioctx.io, cstr(c_name), c_uint64(features), byref(c_int(order))) if ret < 0: raise make_ex(ret, 'error creating clone')