def create_image(ioctx, imagename, features): print("beging---create_image imagename:%s", imagename) if features is not None: print("beging ---RBD().create imagename:%s", imagename) ret = RBD().create(ioctx, imagename, IMG_SIZE, IMG_ORDER, old_format=False, features=int(features)) print("end ---RBD().create imagename:%s", imagename) record_rbdapi_test('RBD.create', ret, Ioctx_obj_to_dict(ioctx), imagename, IMG_SIZE, IMG_ORDER, False, features) print("end ---record_rbdapi_test RBD().create imagename:%s", imagename) else: ret = RBD().create(ioctx, imagename, IMG_SIZE, IMG_ORDER, old_format=True) record_rbdapi_test('RBD.create', ret, Ioctx_obj_to_dict(ioctx), imagename, IMG_SIZE, IMG_ORDER, True) print('create image ' + imagename) print("end---create_image imagename:%s", imagename) return imagename
def check_default_params(format, order=None, features=None, stripe_count=None, stripe_unit=None, exception=None): global rados global ioctx orig_vals = {} for k in [ 'rbd_default_format', 'rbd_default_order', 'rbd_default_features', 'rbd_default_stripe_count', 'rbd_default_stripe_unit' ]: orig_vals[k] = rados.conf_get(k) try: rados.conf_set('rbd_default_format', str(format)) if order is not None: rados.conf_set('rbd_default_order', str(order or 0)) if features is not None: rados.conf_set('rbd_default_features', str(features or 0)) if stripe_count is not None: rados.conf_set('rbd_default_stripe_count', str(stripe_count or 0)) if stripe_unit is not None: rados.conf_set('rbd_default_stripe_unit', str(stripe_unit or 0)) image_name = get_temp_image_name() if exception is None: RBD().create(ioctx, image_name, IMG_SIZE) try: with Image(ioctx, image_name) as image: eq(format == 1, image.old_format()) expected_order = order if not order: expected_order = 22 actual_order = image.stat()['order'] eq(expected_order, actual_order) expected_features = features if expected_features is None or format == 1: expected_features = 0 if format == 1 else 7 eq(expected_features, image.features()) expected_stripe_count = stripe_count if not expected_stripe_count or format == 1 or \ features & RBD_FEATURE_STRIPINGV2 == 0: expected_stripe_count = 1 eq(expected_stripe_count, image.stripe_count()) expected_stripe_unit = stripe_unit if not expected_stripe_unit or format == 1 or \ features & RBD_FEATURE_STRIPINGV2 == 0: expected_stripe_unit = 1 << actual_order eq(expected_stripe_unit, image.stripe_unit()) finally: RBD().remove(ioctx, image_name) else: assert_raises(exception, RBD().create, ioctx, image_name, IMG_SIZE) finally: for k, v in orig_vals.iteritems(): rados.conf_set(k, v)
def master(ioctx): print("starting master") safe_delete_image(ioctx, CLONE_IMG_RENAME) safe_delete_image(ioctx, CLONE_IMG_NAME) safe_delete_image(ioctx, PARENT_IMG_NAME) features = get_features() RBD().create(ioctx, PARENT_IMG_NAME, IMG_SIZE, IMG_ORDER, old_format=False, features=features) with Image(ioctx, PARENT_IMG_NAME) as image: image.create_snap('snap1') image.protect_snap('snap1') RBD().clone(ioctx, PARENT_IMG_NAME, 'snap1', ioctx, CLONE_IMG_NAME, features=features) with Image(ioctx, CLONE_IMG_NAME) as image: print("acquiring exclusive lock") offset = 0 data = os.urandom(512) while offset < IMG_SIZE: image.write(data, offset) offset += (1 << IMG_ORDER) image.write(b'1', IMG_SIZE - 1) assert(image.is_exclusive_lock_owner()) print("waiting for slave to complete") while image.is_exclusive_lock_owner(): time.sleep(5) safe_delete_image(ioctx, CLONE_IMG_RENAME) safe_delete_image(ioctx, CLONE_IMG_NAME) delete_image(ioctx, PARENT_IMG_NAME) print("finished")
def test_follower_flatten(self): with Image(ioctx, image_name) as image: image.create_snap('snap') image.protect_snap('snap') try: RBD().clone(ioctx, image_name, 'snap', ioctx, 'clone', features) with Image(ioctx, 'clone') as image1, Image(ioctx2, 'clone') as image2: data = rand_data(256) image1.write(data, 0) image2.flatten() assert_raises(ImageNotFound, image1.parent_info) parent = True for x in range(30): try: image2.parent_info() except ImageNotFound: parent = False break eq(False, parent) finally: RBD().remove(ioctx, 'clone') with Image(ioctx, image_name) as image: image.unprotect_snap('snap') image.remove_snap('snap')
def create_image(): global image_name image_name = get_temp_image_name() if features is not None: RBD().create(ioctx, image_name, IMG_SIZE, IMG_ORDER, old_format=False, features=int(features)) else: RBD().create(ioctx, image_name, IMG_SIZE, IMG_ORDER, old_format=True)
def create_image(): if features is not None: RBD().create(ioctx, IMG_NAME, IMG_SIZE, IMG_ORDER, old_format=False, features=int(features)) else: RBD().create(ioctx, IMG_NAME, IMG_SIZE, IMG_ORDER, old_format=True)
def test_context_manager(): with Rados(conffile='') as cluster: with cluster.open_ioctx('rbd') as ioctx: RBD().create(ioctx, IMG_NAME, IMG_SIZE) with Image(ioctx, IMG_NAME) as image: data = rand_data(256) image.write(data, 0) read = image.read(0, 256) RBD().remove(ioctx, IMG_NAME) eq(data, read)
def test_context_manager(): with Rados(conffile='') as cluster: with cluster.open_ioctx(pool_name) as ioctx: image_name = get_temp_image_name() RBD().create(ioctx, image_name, IMG_SIZE) with Image(ioctx, image_name) as image: data = rand_data(256) image.write(data, 0) read = image.read(0, 256) RBD().remove(ioctx, image_name) eq(data, read)
def create_image(): features = os.getenv("RBD_FEATURES") if features is not None: RBD().create(ioctx, IMG_NAME, IMG_SIZE, IMG_ORDER, old_format=False, features=int(features)) else: RBD().create(ioctx, IMG_NAME, IMG_SIZE, IMG_ORDER, old_format=True)
def refresh_rbd_stats_pools(self, pools): self.log.debug('refreshing rbd pools %s' % (pools)) counters_info = self.rbd_stats['counters_info'] for pool_name in pools: try: pool_id = self.rados.pool_lookup(pool_name) with self.rados.open_ioctx(pool_name) as ioctx: if pool_id not in self.rbd_stats['pools']: self.rbd_stats['pools'][pool_id] = {'images' : {}} pool = self.rbd_stats['pools'][pool_id] pool['name'] = pool_name images = {} for image_meta in RBD().list2(ioctx): image = {'n' : image_meta['name']} image_id = image_meta['id'] if image_id in pool['images']: image['c'] = pool['images'][image_id]['c'] else: image['c'] = [[0, 0] for x in counters_info] images[image_id] = image pool['images'] = images except Exception as e: self.log.error('failed listing pool %s: %s' % (pool_name, e)) self.rbd_stats['pools_refresh_time'] = time.time()
def __init__(self, log, cluster_name, pool_name, conffile=''): self.log = log self.cluster_name = cluster_name self.pool_name = pool_name self.conffile = conffile self.rbd_name = [] # [rbd_name, ...] self.snap_name = {} # {rbd_name: [snap_name, ...], ...} self.rbd_info = {} # {rbd_name: {}, ...} self.snap_info = {} # {rbd_name: {}, ...} self.snap_id_list = {} # {rbd_name, [snap_id, ...], ...} self.rbd_snap_id = {} # {rbd_snap_name: id, ... } try: self.cluster = rados.Rados(conffile=self.conffile) self.cluster.connect() self.ioctx = self.cluster.open_ioctx(pool_name) self.rbd = RBD() self.connected = True except Exception as e: self.log.error("unable to open ioctx of pool %s, conffile=%s, %s" % (pool_name, conffile, e)) exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stdout) self.connected = False
def test_rename(): rbd = RBD() image_name2 = get_temp_image_name() rbd.rename(ioctx, image_name, image_name2) eq([image_name2], rbd.list(ioctx)) rbd.rename(ioctx, image_name2, image_name) eq([image_name], rbd.list(ioctx))
def refresh_rbd_stats_pools(self, pools): self.log.debug('refreshing rbd pools %s' % (pools)) rbd = RBD() counters_info = self.rbd_stats['counters_info'] for pool_name, cfg_ns_names in pools.items(): try: pool_id = self.rados.pool_lookup(pool_name) with self.rados.open_ioctx(pool_name) as ioctx: if pool_id not in self.rbd_stats['pools']: self.rbd_stats['pools'][pool_id] = {'images': {}} pool = self.rbd_stats['pools'][pool_id] pool['name'] = pool_name pool['ns_names'] = cfg_ns_names if cfg_ns_names: nspace_names = list(cfg_ns_names) else: nspace_names = [''] + rbd.namespace_list(ioctx) for nspace_name in pool['images']: if nspace_name not in nspace_names: del pool['images'][nspace_name] for nspace_name in nspace_names: if (nspace_name and not rbd.namespace_exists(ioctx, nspace_name)): self.log.debug('unknown namespace %s for pool %s' % (nspace_name, pool_name)) continue ioctx.set_namespace(nspace_name) if nspace_name not in pool['images']: pool['images'][nspace_name] = {} namespace = pool['images'][nspace_name] images = {} for image_meta in RBD().list2(ioctx): image = {'n': image_meta['name']} image_id = image_meta['id'] if image_id in namespace: image['c'] = namespace[image_id]['c'] else: image['c'] = [[0, 0] for x in counters_info] images[image_id] = image pool['images'][nspace_name] = images except Exception as e: self.log.error('failed listing pool %s: %s' % (pool_name, e)) self.rbd_stats['pools_refresh_time'] = time.time()
def get_rbd_list(self): try: self.log.debug("Get RBD name list from ceph cluster.") rbd = RBD() rbd_list = rbd.list(self.ioctx) self.log.debug("RBD name list:", rbd_list) return rbd_list except Exception as e: self.log.error("Fail to get RBD name list. %s" % e) return False
def refresh_image_names(self, resolve_image_names): rbd = RBD() for pool_id, namespace in resolve_image_names: image_key = (pool_id, namespace) images = self.image_name_cache.setdefault(image_key, {}) with self.rados.open_ioctx2(int(pool_id)) as ioctx: ioctx.set_namespace(namespace) for image_meta in rbd.list2(ioctx): images[image_meta['id']] = image_meta['name'] self.log.debug("resolve_image_names: {}={}".format( image_key, images))
def delete_image(ioctx, img_name): image = Image(ioctx, img_name) for snap in image.list_snaps(): snap_name = snap['name'] print("removing snapshot: %s@%s" % (img_name, snap_name)) if image.is_protected_snap(snap_name): image.unprotect_snap(snap_name) image.remove_snap(snap_name) image.close() print("removing image: %s" % img_name) RBD().remove(ioctx, img_name)
def slave(ioctx): print("starting slave") while True: try: with Image(ioctx, CLONE_IMG_NAME) as image: if image.list_lockers() != []: break except Exception: pass with Image(ioctx, CLONE_IMG_NAME) as image: print("detected master") print("flatten") image.flatten() assert (not image.is_exclusive_lock_owner()) print("resize") image.resize(IMG_SIZE / 2) assert (not image.is_exclusive_lock_owner()) assert (image.stat()['size'] == IMG_SIZE / 2) print("create_snap") image.create_snap('snap1') assert (not image.is_exclusive_lock_owner()) assert ('snap1' in map(lambda snap: snap['name'], image.list_snaps())) print("protect_snap") image.protect_snap('snap1') assert (not image.is_exclusive_lock_owner()) assert (image.is_protected_snap()) print("unprotect_snap") image.unprotect_snap('snap1') assert (not image.is_exclusive_lock_owner()) assert (not image.is_protected_snap()) print("remove_snap") image.remove_snap('snap1') assert (not image.is_exclusive_lock_owner()) assert (list(image.list_snaps()) == []) print("write") data = os.urandom(512) image.write(data, 0) assert (image.is_exclusive_lock_owner()) print("rename") RBD().rename(ioctx, CLONE_IMG_NAME, CLONE_IMG_RENAME) print("finished")
def setUp(self): global ioctx global features self.rbd = RBD() create_image() self.image = Image(ioctx, IMG_NAME) data = rand_data(256) self.image.write(data, IMG_SIZE / 2) self.image.create_snap('snap1') global features self.image.protect_snap('snap1') self.rbd.clone(ioctx, IMG_NAME, 'snap1', ioctx, 'clone', features) self.clone = Image(ioctx, 'clone')
def rbd_list(dbg, cluster, pool): log.debug("%s: xcpng.librbd.rbd_utils.get_rbd_list: %s" % (dbg, pool)) ioctx = cluster.open_ioctx(pool) rbd_inst = RBD() try: rbds = rbd_inst.list(ioctx) return rbds except Exception as e: log.error( "%s: xcpng.librbd.rbd_utils.get_rbd_list: Failed to get list of rbds for pool: %s " % (dbg, pool)) raise Exception(e) finally: ioctx.close()
def test_open_read_only(): with Rados(conffile='') as cluster: with cluster.open_ioctx('rbd') as ioctx: RBD().create(ioctx, IMG_NAME, IMG_SIZE) data = rand_data(256) with Image(ioctx, IMG_NAME) as image: image.write(data, 0) image.create_snap('snap') with Image(ioctx, IMG_NAME, read_only=True) as image: read = image.read(0, 256) eq(data, read) assert_raises(ReadOnlyImage, image.write, data, 0) assert_raises(ReadOnlyImage, image.create_snap, 'test') assert_raises(ReadOnlyImage, image.remove_snap, 'snap') assert_raises(ReadOnlyImage, image.rollback_to_snap, 'snap') assert_raises(ReadOnlyImage, image.protect_snap, 'snap') assert_raises(ReadOnlyImage, image.unprotect_snap, 'snap') assert_raises(ReadOnlyImage, image.unprotect_snap, 'snap') assert_raises(ReadOnlyImage, image.flatten) with Image(ioctx, IMG_NAME) as image: image.remove_snap('snap') RBD().remove(ioctx, IMG_NAME) eq(data, read)
def setUp(self): global ioctx global features self.rbd = RBD() create_image() self.image = Image(ioctx, image_name) data = rand_data(256) self.image.write(data, IMG_SIZE // 2) self.image.create_snap('snap1') global features self.image.protect_snap('snap1') self.clone_name = get_temp_image_name() self.rbd.clone(ioctx, image_name, 'snap1', ioctx, self.clone_name, features) self.clone = Image(ioctx, self.clone_name)
def rbd_create(dbg, cluster, pool, name, size): log.debug( "%s: xcpng.librbd.rbd_utils.rbd_create: Cluster ID: %s Pool: %s Name: %s Size: %s" % (dbg, cluster.get_fsid(), pool, name, size)) ioctx = cluster.open_ioctx(pool) rbd_inst = RBD() try: rbd_inst.create(ioctx, name, size, order=RBD_IMAGE_ORDER) except Exception as e: log.error( "%s: xcpng.librbd.rbd_utils.rbd_create: Failed to create an image: Cluster ID: %s Pool %s Name: %s Size: %s" % (dbg, cluster.get_fsid(), pool, name, size)) raise Exception(e) finally: ioctx.close()
def rbd_remove(dbg, cluster, pool, name): log.debug( "%s: xcpng.librbd.rbd_utils.rbd_remove: Cluster ID: %s Pool %s Name: %s" % (dbg, cluster.get_fsid(), pool, name)) ioctx = cluster.open_ioctx(pool) rbd_inst = RBD() try: rbd_inst.remove(ioctx, name) except Exception as e: log.error( "%s: xcpng.librbd.rbd_utils.rbd_remove: Failed to remove an image: Cluster ID: %s Pool %s Name: %s" % (dbg, cluster.get_fsid(), pool, name)) raise Exception(e) finally: ioctx.close()
def test_create_with_params(self): global features image_name = get_temp_image_name() order = 20 stripe_unit = 1 << 20 stripe_count = 10 self.rbd.create(ioctx, image_name, IMG_SIZE, order, False, features, stripe_unit, stripe_count) image = Image(ioctx, image_name) info = image.stat() check_stat(info, IMG_SIZE, order) eq(image.features(), features) eq(image.stripe_unit(), stripe_unit) eq(image.stripe_count(), stripe_count) image.close() RBD().remove(ioctx, image_name)
def rbd_rename(dbg, cluster, pool, old_name, new_name): log.debug( "%s: xcpng.librbd.rbd_utils.rbd_rename: Cluster ID: %s Pool: %s Old name: %s New name: %s" % (dbg, cluster.get_fsid(), pool, old_name, new_name)) ioctx = cluster.open_ioctx(pool) rbd_inst = RBD() try: rbd_inst.rename(ioctx, old_name, new_name) except Exception as e: log.error( "%s: xcpng.librbd.rbd_utils.rbd_utilisation: Failed to get an image utilization: Cluster ID: %s Pool %s Old Name: %s New Name: %s" % (dbg, cluster.get_fsid(), pool, old_name, new_name)) raise Exception(e) finally: ioctx.close()
def rbd_clone(dbg, cluster, parent_pool, parent, snapshot, clone_pool, clone): log.debug( "%s: xcpng.librbd.rbd_utils.rbd_clone: Cluster ID: %s Parent Pool: %s Parent: %s Snapshot: %s Clone Pool: %s Clone: %s" % (dbg, cluster.get_fsid(), parent_pool, parent, snapshot, clone_pool, clone)) p_ioctx = cluster.open_ioctx(parent_pool) p_image = Image(p_ioctx, parent) c_ioctx = cluster.open_ioctx(clone_pool) rbd_inst = RBD() try: if not p_image.is_protected_snap(snapshot): p_image.protect_snap(snapshot) rbd_inst.clone(p_ioctx, parent, snapshot, c_ioctx, clone) except Exception as e: log.error( "%s: xcpng.librbd.rbd_utils.rbd_clone: Failed to make a clone: Cluster ID: %s Parent Pool: %s Parent: %s Snapshot: %s Clone Pool: %s Clone: %s" % (dbg, cluster.get_fsid(), parent_pool, parent, snapshot, clone_pool, clone)) raise Exception(e) finally: p_ioctx.close() c_ioctx.close()
def remove_image(): if image_name is not None: RBD().remove(ioctx, image_name)
def setUp(self): self.rbd = RBD() create_image() self.image = Image(ioctx, image_name)
def test_list(): eq([image_name], RBD().list(ioctx))
def test_list_empty(): eq([], RBD().list(ioctx))