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\n", 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\n", argv[0], strerror(-rt_num));
 34         return -1; 
 35     }else{
 36         printf("\nCreated a cluster handle success. \n");
 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\n", argv[0], strerror(-rt_num));
 43         return -1;
 44     }else{
 45         printf("\nRead the config file. \n");
 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\n", argv[0], strerror(-rt_num));
 53         return -1;
 54     }else{
 55         printf("\nRead the command line arguments.\n");
 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\n", argv[0], strerror(-rt_num));
 62         return -1;
 63     }else{
 64         printf("\nConnected to the cluster.\n");
 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("\nCreated I/O context. \n");
 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("\nrbd_create success !!\n");
 87     }else{
 88         printf("\nrbd_create failed !!%s\n", 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 !!\n");
 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("\nremove img name:%s\n", name_01);
106     rt_num = rbd_remove_with_progress(io, name_01, print_progress_percent, NULL);
107     if(rt_num ==0){
108         printf("\nrbd_remove success !!\n");
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("\nrbd_open success !!\n");
115     }else{
116         printf("\nrbd_open failed !!%s\n", 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\n", ofs);
131         }else{
132             printf("rbd_write fail!!\n");
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("\nrbd_stat failed %s\n", name);
140     }else{
141         printf("\nrbd_stat size :%d\n", (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("\nrbd_get_size %d\n", (int)ofs);
149     }
150     printf("\nrbd_get_size return_num: %d\n", 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("\nthe content is:%s", buf1);
158         }else{
159             printf("rbd_read fail!!\n");
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("\nrbd_open_read_only success !!\n");
170      }else{
171         printf("\nrbd_open_read_only fail !!\n");
172      }
173     */
174 
175 
176 
177     rt_num = rbd_close(image);
178     if(rt_num == 0){
179         printf("\nrbd_close success !!\n");
180     }else{
181         printf("\nrbd_close failed !!%s\n", strerror(-rt_num));
182     }
183 
184     printf("\nClosing the connection\n");
185     rados_ioctx_destroy(io);
186     printf("\nShut down the handle\n");
187     rados_shutdown(cluster);
188     return 0;
189 }

 

posted on 2015-06-19 16:30  阳台  阅读(1068)  评论(0编辑  收藏  举报

导航