7 The `rbd` python module provides file-like access to RBD images.
10 Example: Creating and writing to an image
11 =========================================
13 To use `rbd`, you must first connect to RADOS and open an IO
16 cluster = rados.Rados(conffile='my_ceph.conf')
18 ioctx = cluster.open_ioctx('mypool')
20 Then you instantiate an :class:rbd.RBD object, which you use to create the
24 size = 4 * 1024**3 # 4 GiB
25 rbd_inst.create(ioctx, 'myimage', size)
27 To perform I/O on the image, you instantiate an :class:rbd.Image object::
29 image = rbd.Image(ioctx, 'myimage')
33 This writes 'foo' to the first 600 bytes of the image. Note that data
34 cannot be :type:unicode - `Librbd` does not know how to deal with
35 characters wider than a :c:type:char.
37 In the end, you'll want to close the image, the IO context and the connection to RADOS::
43 To be safe, each of these calls would need to be in a separate :finally
46 cluster = rados.Rados(conffile='my_ceph_conf')
48 ioctx = cluster.open_ioctx('my_pool')
51 size = 4 * 1024**3 # 4 GiB
52 rbd_inst.create(ioctx, 'myimage', size)
53 image = rbd.Image(ioctx, 'myimage')
64 This can be cumbersome, so the :class:`Rados`, :class:`Ioctx`, and
65 :class:`Image` classes can be used as context managers that close/shutdown
66 automatically (see :pep:`343`). Using them as context managers, the
67 above example becomes::
69 with rados.Rados(conffile='my_ceph.conf') as cluster:
70 with cluster.open_ioctx('mypool') as ioctx:
72 size = 4 * 1024**3 # 4 GiB
73 rbd_inst.create(ioctx, 'myimage', size)
74 with rbd.Image(ioctx, 'myimage') as image:
82 :members: RBD, Image, SnapIterator