centos安装:
yum install ceph-common
ubuntu安装:
apt-get install ceph-common
安装后,能在python模块中导入rbd与rados模块 似乎不使用与在windows或者Macos上
shell 命令查看:
rbd diff pool_name/image_name | awk ‘{ SUM += $2 } END { print SUM/1024/1024 ” MB” }’
rbd diff pool_name/image_name | awk '{ SUM += $2 } END { print SUM/1024/1024 " MB" }'
# coding=utf-8
import rados
import rbd
cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')
print cluster
cluster.connect()
def getUsedSize(pool_name, image_name):
# 内部变量调用外部变量,必须使用一个class
class ImageStorage: pass
storage = ImageStorage()
storage.used = 0
def iterate_cb(offset, length, exists):
print 'iterate_cb'
print 'offset: ', offset, ' length: ', length, ' exists: ', exists
storage.used += length
with cluster.open_ioctx(pool_name) as ioctx:
print 'pool state', ioctx.get_stats()
rbd_inst = rbd.RBD()
with rbd.Image(ioctx, image_name) as image:
print 'image size: ', image.size()
print 'image stat: ', image.stat()
print 'iamge stripe_unit: ', image.stripe_unit()
print 'image stripe_count: ', image.stripe_count()
print image.diff_iterate(0, 10485760, None, iterate_cb)
return storage.used
size = getUsedSize('rbd', 'test')
print size,'byte ', size/1024**2,'MB'