设为首页 收藏本站
查看: 1370|回复: 0

[经验分享] ceph之rbd读写API 测试

[复制链接]

尚未签到

发表于 2015-9-10 07:30:33 | 显示全部楼层 |阅读模式
  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 }
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-111639-1-1.html 上篇帖子: ceph之Placement Group 下篇帖子: centos6.4 ceph安装部署之ceph object storage
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表