|
#include <iostream>
|
|
#include <string>
|
|
#include <rados/librados.hpp>
|
|
#include <rbd/librbd.hpp>
|
|
|
|
int main(int argc, const char **argv)
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
/* Declare the cluster handle and required variables. */
|
|
librados::Rados cluster;
|
|
librados::IoCtx ioctx;
|
|
librbd::RBD rbd;
|
|
librbd::Image img;
|
|
char cluster_name[] = "ceph";
|
|
char user_name[] = "client.admin";
|
|
uint64_t flags = 0;
|
|
|
|
/* Initialize the cluster handle with the "ceph" cluster name and "client.admin" user */
|
|
{
|
|
ret = cluster.init2(user_name, cluster_name, flags);
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't initialize the cluster handle! error " << strerror(ret) << std::endl;
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Created a cluster handle." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Read a Ceph configuration file to configure the cluster handle. */
|
|
{
|
|
ret = cluster.conf_read_file(NULL);
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't read the Ceph configuration file! error " << strerror(ret) << std::endl;
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Read the Ceph configuration file." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Read command line arguments */
|
|
{
|
|
ret = cluster.conf_parse_argv(argc, argv);
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't parse command line options! error " << strerror(ret) << std::endl;
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Parsed command line options." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Connect to the cluster */
|
|
{
|
|
ret = cluster.connect();
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't connect to cluster! error " << strerror(ret) << std::endl;
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Connected to the cluster." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Create pool */
|
|
{
|
|
ret = cluster.pool_create("test_pool");
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't create pool! error " << strerror(ret) << std::endl;
|
|
//return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Created pool." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Create IOContext */
|
|
{
|
|
ret = cluster.ioctx_create("test_pool", ioctx);
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't create IOContext! error " << strerror(ret) << std::endl;
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Created IOContext." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Create rbd */
|
|
{
|
|
int order = 22;
|
|
ret = rbd.create(ioctx, "test_image", 1<<22, &order);
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't create RBD! error " << strerror(ret) << std::endl;
|
|
//return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Created RBD." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Open image */
|
|
{
|
|
ret = rbd.open_read_only(ioctx, img, "test_image", nullptr);
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't open image! error " << strerror(ret) << std::endl;
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Opened image." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Close image */
|
|
// {
|
|
// ret = img.close();
|
|
// if (ret < 0) {
|
|
// std::cerr << "Couldn't close image! error " << strerror(ret) << std::endl;
|
|
// return EXIT_FAILURE;
|
|
// } else {
|
|
// std::cout << "Closed image." << std::endl;
|
|
// }
|
|
// }
|
|
|
|
/* Remove rbd */
|
|
{
|
|
ret = rbd.remove(ioctx, "test_image");
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't remove RBD! error " << strerror(-ret) << std::endl;
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Created RBD." << std::endl;
|
|
}
|
|
}
|
|
|
|
/* Close IOContext */
|
|
std::cout << "Closing IOContext." << std::endl;
|
|
ioctx.close();
|
|
std::cout << "Closed IOContext." << std::endl;
|
|
|
|
/* Delete pool */
|
|
{
|
|
ret = cluster.pool_delete("test_pool");
|
|
if (ret < 0) {
|
|
std::cerr << "Couldn't delete pool! error " << strerror(ret) << std::endl;
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
std::cout << "Deleted pool." << std::endl;
|
|
}
|
|
}
|
|
|
|
std::cout << "Shutting down cluster handle." << std::endl;
|
|
cluster.shutdown();
|
|
std::cout << "Shut down cluster handle." << std::endl;
|
|
|
|
return 0;
|
|
}
|