Ceph rbd cmd练习

目的:阅读官方文档,熟悉rbd命令。

rbd命令行参数:
http://docs.ceph.com/docs/wip-5900/man/8/rbd/

image相关

http://docs.ceph.com/docs/wip-5900/rbd/rados-rbd-cmds/

列出pools
http://docs.ceph.com/docs/wip-5900/rados/operations/pools/

root@maqi-openstack:~$ ceph osd lspools
0 rbd,1 cephfs_data,2 cephfs_metadata,3 .rgw.root,4 .rgw.control,5 .rgw,6 .rgw.gc,7 .users.uid,

这里显示了0 ~ 7共8个pool。

The default pools include:

  • data
  • metadata
  • rbd

创建image

root@maqi-openstack:~$ rbd create foo --size 1024

列出image

root@maqi-openstack:~$ rbd ls
foo
root@maqi-openstack:~$ rbd ls rbd        #rbd是默认pool
foo

查看image详细信息

root@maqi-openstack:~$ rbd --image foo info
rbd image 'foo':
    size 1024 MB in 256 objects
    order 22 (4096 kB objects)
    block_name_prefix: rb.0.102b.74b0dc51
    format: 1
root@maqi-openstack:~$ rbd --image foo -p rbd info
rbd image 'foo':
    size 1024 MB in 256 objects
    order 22 (4096 kB objects)
    block_name_prefix: rb.0.102b.74b0dc51
    format: 1

Resize image

root@maqi-openstack:~$ rbd resize --image foo --size 2048
Resizing image: 100% complete...done.
root@maqi-openstack:~$ rbd --image foo info
rbd image 'foo':
    size 2048 MB in 512 objects
    order 22 (4096 kB objects)
    block_name_prefix: rb.0.102b.74b0dc51
    format: 1

删除image

root@maqi-openstack:~$ rbd rm foo -p rbd
Removing image: 100% complete...done.
root@maqi-openstack:~$ rbd ls
root@maqi-openstack:~$ rbd --image foo info
2015-10-12 15:56:48.457090 7f81b9afc840 -1 librbd::ImageCtx: error finding header: (2) No such file or directory
rbd: error opening image foo: (2) No such file or directory

snapshot相关

http://docs.ceph.com/docs/wip-5900/rbd/rbd-snapshot/

snapshot可以这样表示:

{pool-name}/{image-name}@{snap-name}

创建snapshot

root@maqi-openstack:~$ rbd --pool rbd snap create --snap snapname foo
root@maqi-openstack:~$ rbd --pool rbd snap ls foo
SNAPID NAME        SIZE
     2 snapname 1024 MB

或者

root@maqi-openstack:~$ rbd snap create rbd/foo@snapshot
root@maqi-openstack:~$ rbd snap ls rbd/foo
SNAPID NAME        SIZE
     2 snapname 1024 MB
     3 snapshot 1024 MB

Rollback snapshot

Rolling back an image to a snapshot means overwriting the current version of the image with data from a snapshot. The time it takes to execute a rollback increases with the size of the image. It is faster to clone from a snapshot than to rollback an image to a snapshot, and it is the preferred method of returning to a pre-existing state.

root@maqi-openstack:~$ rbd --pool rbd snap rollback --snap snapname foo
Rolling back to snapshot: 100% complete...done.

或者

root@maqi-openstack:~$ rbd snap rollback rbd/foo@snapshot
Rolling back to snapshot: 100% complete...done.

删除snapshot

root@maqi-openstack:~$ rbd --pool rbd snap rm --snap snapname foo
root@maqi-openstack:~$ rbd snap rm rbd/foo@snapshot

snapshot layering

Ceph supports the ability to create many copy-on-write (COW) clones of a block device shapshot. Snapshot layering enables Ceph block device clients to create images very quickly. For example, you might create a block device image with a Linux VM written to it; then, snapshot the image, protect the snapshot, and create as many copy-on-write clones as you like. A snapshot is read-only, so cloning a snapshot simplifies semantics–making it possible to create clones rapidly.

Ceph rbd cmd练习_第1张图片

Each cloned image (child) stores a reference to its parent image, which enables the cloned image to open the parent snapshot and read it.

A COW clone of a snapshot behaves exactly like any other Ceph block device image. You can read to, write from, clone, and resize cloned images. There are no special restrictions with cloned images. However, the copy-on-write clone of a snapshot refers to the snapshot, so you MUST protect the snapshot before you clone it. The following diagram depicts the process.

Ceph rbd cmd练习_第2张图片

  • snapshot都是read-only的
  • 对snapshot做clone,利用了COW方式,生成新的image

使用snapshot layering时(也就是多次snapshot):

  1. 对image创建snapshot
  2. protect这个snapshot,使其成为parent snapshot
  3. clone这个parent snapshot。clone出来的image就像一个普通的image。

Protect a snapshot

root@maqi-openstack:~$ rbd snap create --pool rbd --snap parent_snap foo
root@maqi-openstack:~$ rbd snap protect --pool rbd --snap parent_snap foo
rbd: protecting snap failed: (38) Function not implemented
2015-10-13 06:31:42.966603 7fc2ecf95840 -1 librbd: snap_protect: image must support layering

只有format 2的image支持layering:http://docs.ceph.com/docs/wip-5900/rbd/rbd-snapshot/#layering

op@ubuntu-op:~$ sudo rbd create bar --image-format 2 --size 1024
op@ubuntu-op:~$ sudo rbd snap create rbd/bar@parent_snap
op@ubuntu-op:~$ sudo rbd snap protect rbd/bar@parent_snap

op@ubuntu-op:~$ sudo rbd ls      # 只列出image
bar
op@ubuntu-op:~$ sudo rbd ls -l   # 列出imagesnapshot
NAME             SIZE PARENT              FMT PROT LOCK
bar             1024M                       2
bar@parent_snap 1024M                       2 yes

protect之后不能删除:

op@ubuntu-op:~$ sudo rbd snap rm rbd/bar@parent_snap
rbd: snapshot 'parent_snap' is protected from removal.
2015-10-13 15:18:57.462634 7fe483d067c0 -1 librbd: removing snapshot from header failed: (16) Device or resource busy

Clone a snapshot

op@ubuntu-op:~$ sudo rbd  clone rbd/bar@parent_snap rbd/bar_child_1

op@ubuntu-op:~$ sudo rbd ls      
bar
bar_child_1
op@ubuntu-op:~$ sudo rbd ls -l
NAME             SIZE PARENT              FMT PROT LOCK
bar             1024M                       2
bar@parent_snap 1024M                       2 yes
bar_child_1     1024M rbd/bar@parent_snap   2

注意:

  1. clone出来的东东是image,而不是snapshot
  2. clone命令只能针对snap,不能直接clone image:
op@ubuntu-op:~$ sudo rbd clone bar rbd/bar_child_2
rbd: snap name was not specified

Unprotect a snapshot

op@ubuntu-op:~$ sudo rbd snap unprotect rbd/bar@parent_snap
2015-10-13 15:25:01.553498 7faa3eac97c0 -1 librbd: snap_unprotect: can't unprotect; at least 1 child(ren) in pool rbd
rbd: unprotecting snap failed: (16) Device or resource busy
op@ubuntu-op:~$ sudo rbd rm rbd/bar_child_1
Removing image: 100% complete...done.
op@ubuntu-op:~$ sudo rbd snap unprotect rbd/bar@parent_snap

查看某个snapshot有没有被clone过(使用children命令)

op@ubuntu-op:~$ sudo rbd children rbd/bar@parent_snap
rbd/bar_child_1

Flatten a child image

Cloned images retain a reference to the parent snapshot. When you remove the reference from the child clone to the parent snapshot, you effectively “flatten” the image by copying the information from the snapshot to the clone. The time it takes to flatten a clone increases with the size of the snapshot. To delete a snapshot, you must flatten the child images first.

op@ubuntu-op:~$ sudo rbd flatten bar_child_1
Image flatten: 100% complete...done.

op@ubuntu-op:~$ sudo rbd ls -l
NAME             SIZE PARENT FMT PROT LOCK
bar             1024M          2
bar@parent_snap 1024M          2 yes
bar_child_1     1024M          2

RBD layering

http://docs.ceph.com/docs/wip-5900/dev/rbd-layering/
简单来说,就是使用rbd clone命令,从image的某个snapshot克隆出一个新的image。

Incremental snapshot

https://ceph.com/dev-notes/incremental-snapshots-with-rbd/
针对image,rbd有3个diff命令,可以比较image的变化:diff, export-diff, import-diff

  • export-diff [image-name] [dest-path] [–from-snap snapname]
    Exports an incremental diff for an image to dest path (use - for stdout). If an initial snapshot is specified, only changes since that snapshot are included; otherwise, any regions of the image that contain data are included. The end snapshot is specified using the standard –snap option or @snap syntax (see below). The image diff format includes metadata about image size changes, and the start and end snapshots. It efficiently represents discarded or ‘zero’ regions of the image.

  • import-diff [src-path] [image-name]
    Imports an incremental diff of an image and applies it to the current image. If the diff was generated relative to a start snapshot, we verify that snapshot already exists before continuing. If there was an end snapshot we verify it does not already exist before applying the changes, and create the snapshot when we are done.

  • diff [image-name] [–from-snap snapname]
    Dump a list of byte extents in the image that have changed since the specified start snapshot, or since the image was created. Each output line includes the starting offset (in bytes), the length of the region (in bytes), and either ‘zero’ or ‘data’ to indicate whether the region is known to be zeros or may contain other data.

image bar有两个snapshot:

op@ubuntu-op:~$ sudo rbd snap create rbd/bar --snap bar_snap_2
op@ubuntu-op:~$ sudo rbd snap list bar
SNAPID NAME           SIZE
     3 parent_snap 1024 MB
     5 bar_snap_2  1024 MB

查看bar从创建以来的改动,输出到标准输出(因为没有改动,所以为空):

op@ubuntu-op:~$ sudo rbd export-diff bar -
rbd diff v1
Exporting image: 100% complete...done.

查看bar从snapshot parent_snap以后的改动,输入到文件中:

op@ubuntu-op:~$ sudo rbd export-diff bar diff_1 --snap parent_snap
Exporting image: 100% complete...done.
op@ubuntu-op:~$ cat diff_1
rbd diff v1
t
 parent_snaps@e

op@ubuntu-op:~$ file diff_1
diff_1: data

用export-diff取得diff文件后,可以用import-diff把这个diff文件import到某个image中

op@ubuntu-op:~$ sudo rbd import-diff diff_1 bar
end snapshot 'parent_snap' already exists, aborting
Importing image diff: 0% complete...failed.

op@ubuntu-op:~$ sudo rbd import-diff diff_1 bar_child_1
Importing image diff: 100% complete...done.
rbd import-diff /path/to/diff backup_image

This will write the contents of the differential to the backup_image and create a snapshot with the same name as the original ending snapshot. It will fail and do nothing if a snapshot with this name already exists. Since overwriting the same data is idempotent, it’s safe to have an import-diff interrupted in the middle.

转载于:https://www.cnblogs.com/qeelee/p/5159136.html

你可能感兴趣的:(Ceph rbd cmd练习)