ceph之rbd读写API 测试

rados、rbd读写API测试:

  1 //compile: 
  2 //gcc ceph_test_v2.c  -lrbd -lrados  -g -Wall
  3 
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 #include <string.h>
  7 #include <rados/librados.h>
  8 #include <rbd/librbd.h>
  9 
 10 static int print_progress_percent(uint64_t offset, uint64_t src_size,void *data)
 11 {
 12     float percent = ((float)offset*100)/src_size;
 13     printf("%3.2f%% done
", percent);
 14     return 0;
 15 }
 16 
 17 int main(int argc, char** argv)
 18 {
 19     rados_t cluster;
 20     char cluster_name[] = "ceph";
 21     char user_name[] = "client.admin";
 22     uint64_t flags = 0;
 23     int i=0;
 24 
 25     rados_ioctx_t io; 
 26     char *poolname = "kvm";
 27 
 28     //Initialize the cluster handle with the "ceph" cluster name 
 29     //and the "client.admin" user name
 30     int rt_num;
 31     rt_num = rados_create2(&cluster, cluster_name, user_name, flags);
 32     if(rt_num<0){
 33         printf("%s: Couldn't create the cluster handle! %s
", argv[0], strerror(-rt_num));
 34         return -1; 
 35     }else{
 36         printf("
Created a cluster handle success. 
");
 37     }   
 38     
 39     //Read a ceph configuration file to configure the cluster handle
 40     rt_num = rados_conf_read_file(cluster, "/etc/ceph/ceph.conf");
 41     if(rt_num<0){
 42         printf("%s:can't read config file: %s
", argv[0], strerror(-rt_num));
 43         return -1;
 44     }else{
 45         printf("
Read the config file. 
");
 46     }
 47 
 48     //Read command line arguments
 49     /*
 50     rt_num = rados_conf_parse_argv(cluster, argc, argv);
 51     if(rt_num<0){
 52         printf("%s:cannot parse command line arguments:%s
", argv[0], strerror(-rt_num));
 53         return -1;
 54     }else{
 55         printf("
Read the command line arguments.
");
 56     }*/
 57 
 58     //Connect to the cluster
 59     rt_num = rados_connect(cluster);
 60     if(rt_num<0){
 61         printf("%s:cannot connect to cluster:%s
", argv[0], strerror(-rt_num));
 62         return -1;
 63     }else{
 64         printf("
Connected to the cluster.
");
 65     }
 66 
 67     //create io handle
 68     rt_num = rados_ioctx_create(cluster, poolname, &io);
 69     if(rt_num<0){
 70         printf("%s:cannot open rados pool %s:%s", argv[0], poolname, strerror(-rt_num));
 71         rados_shutdown(cluster);
 72         return -1;
 73     }else{
 74         printf("
Created I/O context. 
");
 75     }
 76 
 77     const char* name = "3400.img";
 78     const char* name_01 = "3000.img";
 79     /*const char* name_02 = "rbd_test_02.img";
 80     uint64_t size = 1073741824;
 81     uint64_t features = 1073741824;
 82     int order = 22;     //warning
 83     //int rbd_create2(rados_ioctx_t io, const char *name, uint64_t size, uint64_t features, int *order);
 84     rt_num = rbd_create( io, name, size, &order);
 85     if( rt_num ==0 ){
 86         printf("
rbd_create success !!
");
 87     }else{
 88         printf("
rbd_create failed !!%s
", strerror(-rt_num));
 89     }*/
 90 
 91 
 92     rbd_image_t image;
 93     //rbd_image_info_t info;
 94     //size_t  infosize;
 95 
 96     //rt_num = rbd_stat(image, &info, sizeof(info));
 97     //if(rt_num < 0){
 98     //  printf("rbd_stat error !!
");
 99     //}
100     //printf("rbd_stat rt_num:%d, size:%d", rt_num, (int)info.size);
101 
102     //int rbd_remove_with_progress(rados_ioctx_t io, const char *name,librbd_progress_fn_t cb, void *cbdata);
103     //librbd_progress_fn_t cb;
104     //void* cbdata = NULL;
105     printf("
remove img name:%s
", name_01);
106     rt_num = rbd_remove_with_progress(io, name_01, print_progress_percent, NULL);
107     if(rt_num ==0){
108         printf("
rbd_remove success !!
");
109     }
110 
111     //int rbd_open(rados_ioctx_t io, const char *name, rbd_image_t *image, const char *snap_name);
112     rt_num = rbd_open( io, name, &image, NULL);
113                                                      if( rt_num ==0 ){
114         printf("
rbd_open success !!
");
115     }else{
116         printf("
rbd_open failed !!%s
", strerror(-rt_num));
117     }
118 
119     size_t num = 156384;
120     char* buf=(char*)malloc(num);
121     char buf1[30]="";
122     uint64_t ofs = 0;
123     ssize_t  ok_size = 0;
124     i = 10;
125     //ssize_t rbd_write(rbd_image_t image, uint64_t ofs, size_t len, const char *buf);
126     for(;i>0;i--){
127         ok_size = rbd_write(image, ofs, num , buf);
128         if(ok_size == num){
129             ofs = ok_size + ofs;
130             printf("rbd_write ok!! ofs: %lu
", ofs);
131         }else{
132             printf("rbd_write fail!!
");
133         }
134     }
135 
136     rbd_image_info_t info_tmp;
137     rt_num = rbd_stat(image, &info_tmp, sizeof(info_tmp));
138     if(rt_num < 0){
139         printf("
rbd_stat failed %s
", name);
140     }else{
141         printf("
rbd_stat size :%d
", (int)info_tmp.size);
142     }
143 
144 
145     ofs = 0;
146     rt_num = rbd_get_size(image, &ofs);
147     if(rt_num >= 0){
148         printf("
rbd_get_size %d
", (int)ofs);
149     }
150     printf("
rbd_get_size return_num: %d
", rt_num);
151     ofs = 0;
152     //ssize_t rbd_read(rbd_image_t image, uint64_t ofs, size_t len, char *buf);
153     for(i=0;i<10;i++){
154         ok_size = rbd_read(image, ofs, 10, buf1);
155         if(ok_size >0){
156             ofs = 10+ofs;
157             printf("
the content is:%s", buf1);
158         }else{
159             printf("rbd_read fail!!
");
160         }
161     }
162 
163     //int rbd_aio_write(rbd_image_t image, uint64_t off, size_t len, const char *buf, rbd_completion_t c);
164     //int rbd_aio_read(rbd_image_t image, uint64_t off, size_t len, char *buf, rbd_completion_t c);
165 
166     /*
167      rt_num = rbd_open_read_only( io, name_01, image, NULL);
168      if(rt_num == 0){
169         printf("
rbd_open_read_only success !!
");
170      }else{
171         printf("
rbd_open_read_only fail !!
");
172      }
173     */
174 
175 
176 
177     rt_num = rbd_close(image);
178     if(rt_num == 0){
179         printf("
rbd_close success !!
");
180     }else{
181         printf("
rbd_close failed !!%s
", strerror(-rt_num));
182     }
183 
184     printf("
Closing the connection
");
185     rados_ioctx_destroy(io);
186     printf("
Shut down the handle
");
187     rados_shutdown(cluster);
188     return 0;
189 }

原文地址:https://www.cnblogs.com/chris-cp/p/4589118.html