From: Loic Dachary Date: Sun, 29 Dec 2013 12:14:14 +0000 (+0100) Subject: mon: make ceph-mon --mkfs idempotent X-Git-Tag: v0.75~11^2~1 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=1eafe8dc45419a6b7d319345cac7fbc0d684d1b1;p=ceph.git mon: make ceph-mon --mkfs idempotent 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 --- diff --git a/src/ceph_mon.cc b/src/ceph_mon.cc index 517013bbb08ff..dad7c433845b7 100644 --- a/src/ceph_mon.cc +++ b/src/ceph_mon.cc @@ -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(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) {