ceph image有两种格式,目前我的ceph版本默认的格式是format 2,如果要指定创建的格式需要增加参数–image-format=1,这两种格式的区别在于是否支持clone及其扩展等功能。 format 1 - Use the original format for a new rbd image. This format is understood by all versions of librbd and the kernel rbd module, but does not support newer features like cloning. format 2 - Use the second rbd format, which is supported by librbd (but not the kernel rbd module) at this time. This adds support for cloning and is more easily extensible to allow more features in the future.
format 1 - 此格式兼容所有版本的 librbd 和内核模块,但是不支持较新的功能,像克隆。 format 2 - librbd 和 3.11 版以上内核模块才支持。此格式增加了克隆支持,使得扩展更容易,还允许以后增加新功能。
首先来看下format 1 格式的image及其对应的对象信息。 1、创建一个format 1格式的image $rbd create rbd/format1_image_1G –image-format 1 -s 1G 此时会提示该版本deprecated(弃用),不过不用管继续往下走。
2、此时可以从pool rbd中看到,有了一个format1_image_1G.rbd和rbd_directory $rados -p rbd ls format1_image_1G.rbd rbd_directory
rbd_directory里面保存了这个red pool中image列表信息,但是format 1格式的image并没有把image name和ID存放在rbd_directory中,后续可以看看format 2是否包含了此信息。此时可以通过命令查看rbd_directory里面的内容。 $rados -p rbd listomapvals rbd_directory
什么都没有。
3、这里创建的image(format1_image_1G)的元数据信息可以通过命令查看,其元数据信息包括:size, order,block_name_prefix,format.
$rbd info rbd/format1_image_1G rbd image ‘format1_image_1G’: size 1024 MB in 256 objects order 22 (4096 kB objects) block_name_prefix: rb.0.1014.2ae8944a format: 1
4、还可以通过命令查看format1_image_1G.rbd的元数据信息:如名字,创建时间,大小,字节为单位。
$rados -p rbd stat format1_image_1G.rbd rbd/format1_image_1G.rbd mtime 2017-05-05 14:50:40.000000, size 112
还可以将里面的内容读取出来。第一步将数据读取到1.txt文件,再二进制显示该1.txt文件 $rados -p rbd get format1_image_1G.rbd 1.txt
$xxd 1.txt 0000000: 3c3c 3c20 5261 646f 7320 426c 6f63 6b20 <<< Rados Block 0000010: 4465 7669 6365 2049 6d61 6765 203e 3e3e Device Image >>> 0000020: 0a00 0000 0000 0000 7262 2e30 2e31 3031 ……..rb.0.101 0000030: 342e 3261 6538 3934 3461 0000 0000 0000 4.2ae8944a…… 0000040: 5242 4400 3030 312e 3030 3500 1600 0000 RBD.001.005….. 0000050: 0000 0040 0000 0000 0000 0000 0000 0000 …@………… 0000060: 0000 0000 0000 0000 0000 0000 0000 0000 …………….
5、此时image为空,往这个image写上段数据,这里我从一个文件dd数据到这个image上,因此需要先将该image map到块设备,这里image map到了/dev/rbd0上。再往这个image上dd 4k的数据,数据来源一个文件testfile。 $sudo rbd map rbd/format1_image_1G /dev/rbd0
$cat testfile this is test file context
$sudo dd if=./testfile of=/dev/rbd0 bs=4K count=1 0+1 records in 0+1 records out 26 bytes (26 B) copied, 0.0120041 s, 2.2 kB/s
6、再次查看image下的数据对象,多了一个rb.0.1014.2ae8944a.000000000000。
$rados -p rbd ls format1_image_1G.rbd rbd_directory rb.0.1014.2ae8944a.000000000000
7、查看这个对象的内容,将该对象的内容读取到 1.txt文件中。查看1.txt文件大小: $rados -p rbd get rb.0.1014.2ae8944a.000000000000 1.txt
$du -sh 1.txt 4.0K 1.txt
查看1.txt内容,存放的就是刚刚复制的testfile中的内容 $xxd 1.txt |head ———二进制打开文件, 0000000: 7468 6973 2069 7320 7465 7374 2066 696c this is test fil 0000010: 6520 636f 6e74 6578 740a 0000 0000 0000 e context……. 0000020: 0000 0000 0000 0000 0000 0000 0000 0000 ……………. 0000030: 0000 0000 0000 0000 0000 0000 0000 0000 ……………. 0000040: 0000 0000 0000 0000 0000 0000 0000 0000 ……………. 0000050: 0000 0000 0000 0000 0000 0000 0000 0000 ……………. 0000060: 0000 0000 0000 0000 0000 0000 0000 0000 ……………. 0000070: 0000 0000 0000 0000 0000 0000 0000 0000 ……………. 0000080: 0000 0000 0000 0000 0000 0000 0000 0000 ……………. 0000090: 0000 0000 0000 0000 0000 0000 0000 0000 …………….
8、查找该对象对应的文件在哪里: $ceph osd map rbd rb.0.1014.2ae8944a.000000000000 osdmap e12 pool ‘rbd’ (0) object ‘rb.0.1014.2ae8944a.000000000000’ -> pg 0.8e0976ae (0.6) -> up ([0,1,2], p0) acting ([0,1,2], p0) 说明该对象存在pg=0.6 osd:0 1 2三个上。
9、进入OSD0 上查看文件:我这里是vstart启动的ceph环境,如果真是集群环境请到/var/lib/ceph/osd/ceph-0/current/下面去查找
$ls -lh dev/osd0/current/0.6_head/ total 4.1M -rw-r–r– 1 zhangyi users 0 May 5 14:45 __head_00000006__0 -rw-r–r– 1 zhangyi users 4.0M May 5 15:21 rb.0.1014.2ae8944a.000000000000__head_8E0976AE__0
10、查看该文件的内容是否是之前写的数据 $cat rb.0.1014.2ae8944a.000000000000__head_8E0976AE__0 |head this is test file context 正好是之前写入的数据。同时在OSD1 OSD2上也存放了该对象的副本信息。 ls -lh dev/osd1/current/0.6_head/ total 4.1M -rw-r–r– 1 zhangyi users 0 May 5 14:45 __head_00000006__0 -rw-r–r– 1 zhangyi users 4.0M May 5 15:21 rb.0.1014.2ae8944a.000000000000__head_8E0976AE__0
$ls -lh dev/osd2/current/0.6_head/ total 4.1M -rw-r–r– 1 zhangyi users 0 May 5 14:45 __head_00000006__0 -rw-r–r– 1 zhangyi users 4.0M May 5 15:21 rb.0.1014.2ae8944a.000000000000__head_8E0976AE__0
11、对象的命令解释: rb.0.1014.2ae8944a:表示对象的前缀也就是前面rbd info查询到的信息 000000000000:表示该对象文件位于该image那个4M块上,这里0表示第一个4M块 head:表示的是image的对象,如果是数字则表示的是snapshot的序号 8E0976AE:表示的是根据对象前缀计算的hash值 0:表示这个对象属于那个Pool,这里属于0号pool也就是rbd pool。