이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 5. Librbd (Python)
The rbd python module provides file-like access to RBD images.
5.1. Example: Creating and writing to an image 링크 복사링크가 클립보드에 복사되었습니다!
To use rbd, you must first connect to RADOS and open an IO context:
cluster = rados.Rados(conffile='my_ceph.conf') cluster.connect() ioctx = cluster.open_ioctx('mypool')
cluster = rados.Rados(conffile='my_ceph.conf')
cluster.connect()
ioctx = cluster.open_ioctx('mypool')
Then you instantiate an :class:rbd.RBD object, which you use to create the image:
rbd_inst = rbd.RBD() size = 4 * 1024**3 # 4 GiB rbd_inst.create(ioctx, 'myimage', size)
rbd_inst = rbd.RBD()
size = 4 * 1024**3 # 4 GiB
rbd_inst.create(ioctx, 'myimage', size)
To perform I/O on the image, you instantiate an :class:rbd.Image object:
image = rbd.Image(ioctx, 'myimage') data = 'foo' * 200 image.write(data, 0)
image = rbd.Image(ioctx, 'myimage')
data = 'foo' * 200
image.write(data, 0)
This writes 'foo' to the first 600 bytes of the image. Note that data cannot be :type:unicode - Librbd does not know how to deal with characters wider than a :c:type:char.
In the end, you’ll close the image, the IO context and the connection to RADOS:
image.close() ioctx.close() cluster.shutdown()
image.close()
ioctx.close()
cluster.shutdown()
To be safe, each of these calls would need to be in a separate :finally block:
This can be cumbersome, so the Rados, Ioctx, and Image classes can be used as context managers that close/shutdown automatically. Using them as context managers, the above example becomes: