]>
git.proxmox.com Git - ceph.git/blob - ceph/examples/librbd/hello_world.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * This is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software
9 * Foundation. See file COPYING.
12 // install the librados-dev and librbd package to get this
13 #include <rados/librados.hpp>
14 #include <rbd/librbd.hpp>
19 int main(int argc
, const char **argv
)
23 // we will use all of these below
24 const char *pool_name
= "hello_world_pool";
25 std::string
hello("hello world!");
26 std::string
object_name("hello_object");
27 librados::IoCtx io_ctx
;
29 // first, we create a Rados object and initialize it
30 librados::Rados rados
;
32 ret
= rados
.init("admin"); // just use the client.admin keyring
33 if (ret
< 0) { // let's handle any error that might have come back
34 std::cerr
<< "couldn't initialize rados! error " << ret
<< std::endl
;
38 std::cout
<< "we just set up a rados cluster object" << std::endl
;
43 * Now we need to get the rados object its config info. It can
44 * parse argv for us to find the id, monitors, etc, so let's just
48 ret
= rados
.conf_parse_argv(argc
, argv
);
50 // This really can't happen, but we need to check to be a good citizen.
51 std::cerr
<< "failed to parse config options! error " << ret
<< std::endl
;
55 std::cout
<< "we just parsed our config options" << std::endl
;
56 // We also want to apply the config file if the user specified
57 // one, and conf_parse_argv won't do that for us.
58 for (int i
= 0; i
< argc
; ++i
) {
59 if ((strcmp(argv
[i
], "-c") == 0) || (strcmp(argv
[i
], "--conf") == 0)) {
60 ret
= rados
.conf_read_file(argv
[i
+1]);
62 // This could fail if the config file is malformed, but it'd be hard.
63 std::cerr
<< "failed to parse config file " << argv
[i
+1]
64 << "! error" << ret
<< std::endl
;
75 * next, we actually connect to the cluster
78 ret
= rados
.connect();
80 std::cerr
<< "couldn't connect to cluster! error " << ret
<< std::endl
;
84 std::cout
<< "we just connected to the rados cluster" << std::endl
;
89 * let's create our own pool instead of scribbling over real data.
90 * Note that this command creates pools with default PG counts specified
91 * by the monitors, which may not be appropriate for real use -- it's fine
92 * for testing, though.
95 ret
= rados
.pool_create(pool_name
);
97 std::cerr
<< "couldn't create pool! error " << ret
<< std::endl
;
100 std::cout
<< "we just created a new pool named " << pool_name
<< std::endl
;
105 * create an "IoCtx" which is used to do IO to a pool
108 ret
= rados
.ioctx_create(pool_name
, io_ctx
);
110 std::cerr
<< "couldn't set up ioctx! error " << ret
<< std::endl
;
114 std::cout
<< "we just created an ioctx for our pool" << std::endl
;
119 * create an rbd image and write data to it
122 std::string name
= "librbd_test";
123 uint64_t size
= 2 << 20;
128 ret
= rbd
.create(io_ctx
, name
.c_str(), size
, &order
);
130 std::cerr
<< "couldn't create an rbd image! error " << ret
<< std::endl
;
134 std::cout
<< "we just created an rbd image" << std::endl
;
137 ret
= rbd
.open(io_ctx
, image
, name
.c_str(), NULL
);
139 std::cerr
<< "couldn't open the rbd image! error " << ret
<< std::endl
;
143 std::cout
<< "we just opened the rbd image" << std::endl
;
146 int TEST_IO_SIZE
= 512;
147 char test_data
[TEST_IO_SIZE
+ 1];
150 for (i
= 0; i
< TEST_IO_SIZE
; ++i
) {
151 test_data
[i
] = (char) (rand() % (126 - 33) + 33);
153 test_data
[TEST_IO_SIZE
] = '\0';
155 size_t len
= strlen(test_data
);
157 bl
.append(test_data
, len
);
159 ret
= image
.write(0, len
, bl
);
161 std::cerr
<< "couldn't write to the rbd image! error " << ret
<< std::endl
;
165 std::cout
<< "we just wrote data to our rbd image " << std::endl
;
169 * let's read the image and compare it to the data we wrote
171 ceph::bufferlist bl_r
;
173 read
= image
.read(0, TEST_IO_SIZE
, bl_r
);
175 std::cerr
<< "we couldn't read data from the image! error" << std::endl
;
180 std::string
bl_res(bl_r
.c_str(), read
);
182 int res
= memcmp(bl_res
.c_str(), test_data
, TEST_IO_SIZE
);
184 std::cerr
<< "what we read didn't match expected! error" << std::endl
;
186 std::cout
<< "we read our data on the image successfully" << std::endl
;
192 *let's now delete the image
194 ret
= rbd
.remove(io_ctx
, name
.c_str());
196 std::cerr
<< "failed to delete rbd image! error " << ret
<< std::endl
;
200 std::cout
<< "we just deleted our rbd image " << std::endl
;
207 * And now we're done, so let's remove our pool and then
208 * shut down the connection gracefully.
210 int delete_ret
= rados
.pool_delete(pool_name
);
211 if (delete_ret
< 0) {
213 std::cerr
<< "We failed to delete our test pool!" << std::endl
;