]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mon: make ceph-mon --mkfs idempotent
authorLoic Dachary <loic@dachary.org>
Sun, 29 Dec 2013 12:14:14 +0000 (13:14 +0100)
committerLoic Dachary <loic@dachary.org>
Tue, 31 Dec 2013 17:58:25 +0000 (18:58 +0100)
A mon is considered to exist if the mon-data directory exists and is not
empty. If ceph-mon --mkfs is run twice, it will display succeed the
second time around and display an informative message.

Signed-off-by: Loic Dachary <loic@dachary.org>
src/ceph_mon.cc

index 517013bbb08ff94ced94132fd3a663482c45166e..dad7c433845b70cffab4e0a833e3ee7c0db9a478 100644 (file)
@@ -117,6 +117,49 @@ int mon_data_exists(bool *r)
   return 0;
 }
 
+int mon_data_empty(bool *r)
+{
+  string mon_data = g_conf->mon_data;
+
+  DIR *dir = ::opendir(mon_data.c_str());
+  if (!dir) {
+    cerr << "opendir(" << mon_data << ") " << strerror(errno) << std::endl;
+    return -errno;
+  }
+  char buf[offsetof(struct dirent, d_name) + PATH_MAX + 1];
+
+  *r = false;
+  int code = 0;
+  struct dirent *de;
+  errno = 0;
+  while (!::readdir_r(dir, reinterpret_cast<struct dirent*>(buf), &de)) {
+    if (!de) {
+      if (errno) {
+       cerr << "readdir(" << mon_data << ") " << strerror(errno) << std::endl;
+       code = -errno;
+      }
+      break;
+    }
+    if (string(".") != de->d_name &&
+       string("..") != de->d_name) {
+      *r = true;
+      break;
+    }
+  }
+
+  ::closedir(dir);
+
+  return code;
+}
+
+int mon_exists(bool *r)
+{
+  int code = mon_data_exists(r);
+  if (code || *r == false)
+    return code;
+  return mon_data_empty(r);
+}
+
 void usage()
 {
   cerr << "usage: ceph-mon -i monid [flags]" << std::endl;
@@ -205,6 +248,14 @@ int main(int argc, const char **argv)
   }
 
   bool exists;
+  if (mon_exists(&exists))
+    exit(1);
+
+  if (mkfs && exists) {
+    cerr << g_conf->mon_data << " already exists" << std::endl;
+    exit(0);
+  }
+
   // -- mkfs --
   if (mkfs) {