Snapshots

A snapshot is a read-only copy of the state of an image at a particular point intime. One of the advanced features of Ceph block devices is that you can createsnapshots of the images to retain a history of an image’s state. Ceph alsosupports snapshot layering, which allows you to clone images (e.g., a VM image)quickly and easily. Ceph supports block device snapshots using the rbdcommand and many higher level interfaces, including QEMU, libvirt,OpenStack and CloudStack.

Important

To use use RBD snapshots, you must have a running Ceph cluster.

Note

Because RBD does not know about the file system, snapshots arecrash-consistent if they are not coordinated with the mountingcomputer. So, we recommend you stop I/O before taking a snapshot ofan image. If the image contains a file system, the file system must bein a consistent state before taking a snapshot or you may have to runfsck. To stop I/O you can use fsfreeze command. Seefsfreeze(8) man page for more details.For virtual machines, qemu-guest-agent can be used to automaticallyfreeze file systems when creating a snapshot.

Snapshots - 图1

Cephx Notes

When cephx is enabled (it is by default), you must specify a user name or IDand a path to the keyring containing the corresponding key for the user. SeeUser Management for details. You may also add the CEPH_ARGS environmentvariable to avoid re-entry of the following parameters.

  1. rbd --id {user-ID} --keyring=/path/to/secret [commands]
  2. rbd --name {username} --keyring=/path/to/secret [commands]

For example:

  1. rbd --id admin --keyring=/etc/ceph/ceph.keyring [commands]
  2. rbd --name client.admin --keyring=/etc/ceph/ceph.keyring [commands]

Tip

Add the user and secret to the CEPH_ARGS environmentvariable so that you don’t need to enter them each time.

Snapshot Basics

The following procedures demonstrate how to create, list, and removesnapshots using the rbd command on the command line.

Create Snapshot

To create a snapshot with rbd, specify the snap create option, the poolname and the image name.

  1. rbd snap create {pool-name}/{image-name}@{snap-name}

For example:

  1. rbd snap create rbd/foo@snapname

List Snapshots

To list snapshots of an image, specify the pool name and the image name.

  1. rbd snap ls {pool-name}/{image-name}

For example:

  1. rbd snap ls rbd/foo

Rollback Snapshot

To rollback to a snapshot with rbd, specify the snap rollback option, thepool name, the image name and the snap name.

  1. rbd snap rollback {pool-name}/{image-name}@{snap-name}

For example:

  1. rbd snap rollback rbd/foo@snapname

Note

Rolling back an image to a snapshot means overwritingthe current version of the image with data from a snapshot. Thetime it takes to execute a rollback increases with the size of theimage. It is faster to clone from a snapshot than to rollbackan image to a snapshot, and it is the preferred method of returningto a pre-existing state.

Delete a Snapshot

To delete a snapshot with rbd, specify the snap rm option, the poolname, the image name and the snap name.

  1. rbd snap rm {pool-name}/{image-name}@{snap-name}

For example:

  1. rbd snap rm rbd/foo@snapname

Note

Ceph OSDs delete data asynchronously, so deleting a snapshotdoesn’t free up the disk space immediately.

Purge Snapshots

To delete all snapshots for an image with rbd, specify the snap purgeoption and the image name.

  1. rbd snap purge {pool-name}/{image-name}

For example:

  1. rbd snap purge rbd/foo

Layering

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

Snapshots - 图2

Note

The terms “parent” and “child” mean a Ceph block device snapshot (parent),and the corresponding image cloned from the snapshot (child). These terms areimportant for the command line usage below.

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

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

Note

Ceph only supports cloning for format 2 images (i.e., created withrbd create —image-format 2). The kernel client supports cloned imagessince kernel 3.10.

Getting Started with Layering

Ceph block device layering is a simple process. You must have an image. You mustcreate a snapshot of the image. You must protect the snapshot. Once you haveperformed these steps, you can begin cloning the snapshot.

Snapshots - 图3

The cloned image has a reference to the parent snapshot, and includes the poolID, image ID and snapshot ID. The inclusion of the pool ID means that you mayclone snapshots from one pool to images in another pool.

  • Image Template: A common use case for block device layering is to create amaster image and a snapshot that serves as a template for clones. For example,a user may create an image for a Linux distribution (e.g., Ubuntu 12.04), andcreate a snapshot for it. Periodically, the user may update the image and createa new snapshot (e.g., sudo apt-get update, sudo apt-get upgrade,sudo apt-get dist-upgrade followed by rbd snap create). As the imagematures, the user can clone any one of the snapshots.

  • Extended Template: A more advanced use case includes extending a templateimage that provides more information than a base image. For example, a user mayclone an image (e.g., a VM template) and install other software (e.g., a database,a content management system, an analytics system, etc.) and then snapshot theextended image, which itself may be updated just like the base image.

  • Template Pool: One way to use block device layering is to create apool that contains master images that act as templates, and snapshots of thosetemplates. You may then extend read-only privileges to users so that theymay clone the snapshots without the ability to write or execute within the pool.

  • Image Migration/Recovery: One way to use block device layering is to migrateor recover data from one pool into another pool.

Protecting a Snapshot

Clones access the parent snapshots. All clones would break if a user inadvertentlydeleted the parent snapshot. To prevent data loss, you MUST protect thesnapshot before you can clone it.

  1. rbd snap protect {pool-name}/{image-name}@{snapshot-name}

For example:

  1. rbd snap protect rbd/my-image@my-snapshot

Note

You cannot delete a protected snapshot.

Cloning a Snapshot

To clone a snapshot, specify you need to specify the parent pool, image andsnapshot; and, the child pool and image name. You must protect the snapshotbefore you can clone it.

  1. rbd clone {pool-name}/{parent-image}@{snap-name} {pool-name}/{child-image-name}

For example:

  1. rbd clone rbd/my-image@my-snapshot rbd/new-image

Note

You may clone a snapshot from one pool to an image in another pool. For example,you may maintain read-only images and snapshots as templates in one pool, and writeableclones in another pool.

Unprotecting a Snapshot

Before you can delete a snapshot, you must unprotect it first. Additionally,you may NOT delete snapshots that have references from clones. You mustflatten each clone of a snapshot, before you can delete the snapshot.

  1. rbd snap unprotect {pool-name}/{image-name}@{snapshot-name}

For example:

  1. rbd snap unprotect rbd/my-image@my-snapshot

Listing Children of a Snapshot

To list the children of a snapshot, execute the following:

  1. rbd children {pool-name}/{image-name}@{snapshot-name}

For example:

  1. rbd children rbd/my-image@my-snapshot

Flattening a Cloned Image

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

  1. rbd flatten {pool-name}/{image-name}

For example:

  1. rbd flatten rbd/new-image

Note

Since a flattened image contains all the information from the snapshot,a flattened image will take up more storage space than a layered clone.