From: Venky Shankar Date: Mon, 27 Jun 2016 06:41:45 +0000 (+0530) Subject: librbd: helper to generate unique image id X-Git-Tag: ses5-milestone5~161^2~5 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=1c82132321b906259a395ed687a6a4dc357e62f4;p=ceph.git librbd: helper to generate unique image id Signed-off-by: Venky Shankar --- diff --git a/src/librbd/Utils.cc b/src/librbd/Utils.cc index 9cda834ea162..da54bb286ff0 100644 --- a/src/librbd/Utils.cc +++ b/src/librbd/Utils.cc @@ -36,5 +36,24 @@ librados::AioCompletion *create_rados_ack_callback(Context *on_finish) { return create_rados_ack_callback(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 diff --git a/src/librbd/Utils.h b/src/librbd/Utils.h index 46b9401ecb07..6bd4320073b9 100644 --- a/src/librbd/Utils.h +++ b/src/librbd/Utils.h @@ -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);