]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: helper to generate unique image id
authorVenky Shankar <vshankar@redhat.com>
Mon, 27 Jun 2016 06:41:45 +0000 (12:11 +0530)
committerVenky Shankar <vshankar@redhat.com>
Sun, 7 Aug 2016 13:23:59 +0000 (18:53 +0530)
Signed-off-by: Venky Shankar <vshankar@redhat.com>
src/librbd/Utils.cc
src/librbd/Utils.h

index 9cda834ea162d5f7ee677550d09c1b5338a4a086..da54bb286ff0651e85b9f632b24af4156d481003 100644 (file)
@@ -36,5 +36,24 @@ librados::AioCompletion *create_rados_ack_callback(Context *on_finish) {
   return create_rados_ack_callback<Context, &Context::complete>(on_finish);
 }
 
+std::string generate_image_id(librados::IoCtx &ioctx) {
+  librados::Rados rados(ioctx);
+
+  uint64_t bid = rados.get_instance_id();
+  uint32_t extra = rand() % 0xFFFFFFFF;
+
+  ostringstream bid_ss;
+  bid_ss << std::hex << bid << std::hex << extra;
+  std::string id = bid_ss.str();
+
+  // ensure the image id won't overflow the fixed block name size
+  const size_t max_id_length = RBD_MAX_BLOCK_NAME_SIZE - strlen(RBD_DATA_PREFIX) - 1;
+  if (id.length() > max_id_length) {
+    id = id.substr(id.length() - max_id_length);
+  }
+
+  return id;
+}
+
 } // namespace util
 } // namespace librbd
index 46b9401ecb07eba4369cb83a29045941fce87f06..6bd4320073b9f240d55966037d03525a080cf1ef 100644 (file)
@@ -92,6 +92,8 @@ struct C_AsyncCallback : public Context {
 
 } // namespace detail
 
+std::string generate_image_id(librados::IoCtx &ioctx);
+
 const std::string group_header_name(const std::string &group_id);
 const std::string id_obj_name(const std::string &name);
 const std::string header_name(const std::string &image_id);