From 7fde8e90aee4d36e3214cb7da9a1d0ae4789e24f Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Mon, 20 Aug 2012 10:56:14 -0700 Subject: [PATCH] mon: create, verify cluster_fsid file in mon_data dir on mkfs Having this present is convenient for external tools. Signed-off-by: Sage Weil --- src/mon/Monitor.cc | 52 +++++++++++++++++++++++++++++++++++++++++++++- src/mon/Monitor.h | 14 +++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index 902d6d508f78a..f6b3878dcf700 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -2015,6 +2015,45 @@ void Monitor::tick() new_tick(); } +int Monitor::check_fsid() +{ + ostringstream ss; + ss << monmap->get_fsid(); + string us = ss.str(); + bufferlist ebl; + int r = store->get_bl_ss(ebl, "cluster_fsid", 0); + if (r < 0) + return r; + + string es(ebl.c_str(), ebl.length()); + + // only keep the first line + size_t pos = es.find_first_of('\n'); + if (pos != string::npos) + es.resize(pos); + + dout(10) << "check_fsid cluster_fsid contains '" << es << "'" << dendl; + if (es.length() < us.length() || + strncmp(us.c_str(), es.c_str(), us.length()) != 0) { + derr << "error: cluster_fsid file exists with value '" << es + << "', != our uuid " << monmap->get_fsid() << dendl; + return -EEXIST; + } + + return 0; +} + +int Monitor::write_fsid() +{ + ostringstream ss; + ss << monmap->get_fsid() << "\n"; + string us = ss.str(); + + bufferlist b; + b.append(us); + return store->put_bl_ss(b, "cluster_fsid", 0); +} + /* * this is the closest thing to a traditional 'mkfs' for ceph. * initialize the monitor state machines to their initial values. @@ -2028,10 +2067,15 @@ int Monitor::mkfs(bufferlist& osdmapbl) return err; } + // verify cluster fsid + int r = check_fsid(); + if (r < 0 && r != -ENOENT) + return r; + bufferlist magicbl; magicbl.append(CEPH_MON_ONDISK_MAGIC); magicbl.append("\n"); - int r = store->put_bl_ss(magicbl, "magic", 0); + r = store->put_bl_ss(magicbl, "magic", 0); if (r < 0) return r; @@ -2073,6 +2117,12 @@ int Monitor::mkfs(bufferlist& osdmapbl) keyring.encode_plaintext(keyringbl); store->put_bl_ss(keyringbl, "mkfs", "keyring"); + // sync and write out fsid to indicate completion. + store->sync(); + r = write_fsid(); + if (r < 0) + return r; + return 0; } diff --git a/src/mon/Monitor.h b/src/mon/Monitor.h index 5aeb16698f3d7..c7cccf5c4c66a 100644 --- a/src/mon/Monitor.h +++ b/src/mon/Monitor.h @@ -419,6 +419,20 @@ public: int mkfs(bufferlist& osdmapbl); + /** + * check cluster_fsid file + * + * @return EEXIST if file exists and doesn't match, 0 on match, or negative error code + */ + int check_fsid(); + + /** + * write cluster_fsid file + * + * @return 0 on success, or negative error code + */ + int write_fsid(); + void do_admin_command(std::string command, std::string args, ostream& ss); private: -- 2.39.5