9 The `rbd` python module provides file-like access to RBD images.
12 Example: Creating and writing to an image
13 =========================================
15 To use `rbd`, you must first connect to RADOS and open an IO
18 cluster = rados.Rados(conffile='my_ceph.conf')
20 ioctx = cluster.open_ioctx('mypool')
22 Then you instantiate an :class:rbd.RBD object, which you use to create the
26 size = 4 * 1024**3 # 4 GiB
27 rbd_inst.create(ioctx, 'myimage', size)
29 To perform I/O on the image, you instantiate an :class:rbd.Image object::
31 image = rbd.Image(ioctx, 'myimage')
35 This writes 'foo' to the first 600 bytes of the image. Note that data
36 cannot be :type:unicode - `Librbd` does not know how to deal with
37 characters wider than a :c:type:char.
39 In the end, you will want to close the image, the IO context and the connection to RADOS::
45 To be safe, each of these calls would need to be in a separate :finally
48 cluster = rados.Rados(conffile='my_ceph_conf')
51 ioctx = cluster.open_ioctx('my_pool')
54 size = 4 * 1024**3 # 4 GiB
55 rbd_inst.create(ioctx, 'myimage', size)
56 image = rbd.Image(ioctx, 'myimage')
67 This can be cumbersome, so the :class:`Rados`, :class:`Ioctx`, and
68 :class:`Image` classes can be used as context managers that close/shutdown
69 automatically (see :pep:`343`). Using them as context managers, the
70 above example becomes::
72 with rados.Rados(conffile='my_ceph.conf') as cluster:
73 with cluster.open_ioctx('mypool') as ioctx:
75 size = 4 * 1024**3 # 4 GiB
76 rbd_inst.create(ioctx, 'myimage', size)
77 with rbd.Image(ioctx, 'myimage') as image:
85 :members: RBD, Image, SnapIterator