How to use librados?

To get started with librados, a development environment is needed. For the examples in this chapter, one of the monitor nodes can be used to act as both the development environment and the client to run the developed application. The examples in this book assume you are using a Debian based distribution:

  1. Firstly, install the base build tools for the operating system:
       $ sudo apt-get install build-essential

The preceding command gives the following output:

  1. Install the librados development library:
       $ sudo apt-get install librados-dev

The preceding command gives the following output:

  1. Now that your environment is complete, let's create a quick application written in C to establish a connection to the test Ceph cluster:
       $ mkdir test_app
$ cd test_app
  1. Create a file called test_app.c with your favorite text editor and place the following in it:
       #include <rados/librados.h> 
#include <stdio.h>
#include <stdlib.h>

rados_t rados = NULL;

int exit_func();


int main(int argc, const char **argv)
{
int ret = 0;
ret = rados_create(&rados, "admin"); // Use the
client.admin keyring
if (ret < 0) { // Check that the rados object was created
printf("couldn't initialize rados! error %d ", ret);
ret = EXIT_FAILURE;
exit_func;
}
else
printf("RADOS initialized ");

ret = rados_conf_read_file(rados, "/etc/ceph/ceph.conf");
if (ret < 0) { //Parse the ceph.conf to obtain cluster details
printf("failed to parse config options! error %d ", ret);
ret = EXIT_FAILURE;
exit_func();
}
else
printf("Ceph config parsed ");

ret = rados_connect(rados); //Initiate connection to the
Ceph cluster
if (ret < 0) {
printf("couldn't connect to cluster! error %d ", ret);
ret = EXIT_FAILURE;
exit_func;
} else {
printf("Connected to the rados cluster ");
}

exit_func(); //End of example, call exit_func to clean
up and finish

}


int exit_func ()
{
rados_shutdown(rados); //Destroy connection to the
Ceph cluster
printf("RADOS connection destroyed ");
printf("The END ");
exit(0);
}
  1. Compile the test application, by running the following command:
       $ gcc test_app.c -o test_app -lrados
It's important to note that you need to tell gcc to link to the librados library to make use of its functions.
  1. Then, test that the app works by running it. Don't forget to run it as root or use sudo, otherwise you won't have access to the Ceph keyring:
       sudo ./test_app

The preceding command gives the following output:

The test application simply reads your ceph.conf configuration, uses it to establish a connection to your Ceph cluster, and then disconnects. It's hardly the most exciting of applications but it tests that the basic infrastructure is in place and is working and establishes a foundation for the rest of the examples in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.75.165