Questo contenuto non è disponibile nella lingua selezionata.
Chapter 9. Using the Ceph block device Python module
The rbd
python module provides file-like access to Ceph block device images. In order to use this built-in tool, import the rbd
and rados
Python modules.
Prerequisites
- A running Red Hat Ceph Storage cluster.
- Root-level access to the node.
Procedure
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')
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To perform I/O on the image, 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)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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
.Close the image, the IO context and the connection to RADOS:
image.close() ioctx.close() cluster.shutdown()
image.close() ioctx.close() cluster.shutdown()
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To be safe, each of these calls must to be in a separate
:finally
block:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This can be cumbersome, so the Rados, Ioctx, and Image classes can be used as context managers that close or shut down automatically. Using them as context managers, the above example becomes:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow