diff --git a/src/cosd.cc b/src/cosd.cc index fc455f16ca1..9c01046fac3 100644 --- a/src/cosd.cc +++ b/src/cosd.cc @@ -145,11 +145,9 @@ int main(int argc, const char **argv) g_timer.shutdown(); messenger->register_entity(entity_name_t::OSD(whoami)); - assert_warn(messenger); if (!messenger) return 1; messenger_hb->register_entity(entity_name_t::OSD(whoami)); - assert_warn(messenger_hb); if (!messenger_hb) return 1; @@ -157,13 +155,22 @@ int main(int argc, const char **argv) messenger->set_policy(entity_name_t::TYPE_MON, SimpleMessenger::Policy::client()); messenger->set_policy(entity_name_t::TYPE_OSD, SimpleMessenger::Policy::lossless_peer()); + + OSD *osd = new OSD(whoami, messenger, messenger_hb, &mc, g_conf.osd_data, g_conf.osd_journal, mkjournal); + + int err = osd->pre_init(); + if (err < 0) { + char buf[80]; + cerr << "error initializing osd: " << strerror_r(-err, buf, sizeof(buf)) << std::endl; + return 1; + } + messenger->start(); messenger_hb->start(true); // only need to daemon() once // start osd - OSD *osd = new OSD(whoami, messenger, messenger_hb, &mc, g_conf.osd_data, g_conf.osd_journal, mkjournal); if (osd->init() < 0) { - cout << "error initializing osd" << std::endl; + cout << "error starting osd" << std::endl; return 1; } diff --git a/src/os/FileStore.cc b/src/os/FileStore.cc index 7ca992c596f..1c60267273b 100644 --- a/src/os/FileStore.cc +++ b/src/os/FileStore.cc @@ -351,12 +351,29 @@ int FileStore::lock_fsid() int r = ::fcntl(fsid_fd, F_SETLK, &l); if (r < 0) { char buf[80]; - derr(0) << "mount failed to lock " << basedir << "/fsid, is another cosd still running? " << strerror_r(errno, buf, sizeof(buf)) << dendl; + derr(0) << "lock_fsid failed to lock " << basedir << "/fsid, is another cosd still running? " << strerror_r(errno, buf, sizeof(buf)) << dendl; return -errno; } return 0; } +bool FileStore::test_mount_in_use() +{ + dout(5) << "test_mount basedir " << basedir << " journal " << journalpath << dendl; + char fn[PATH_MAX]; + snprintf(fn, sizeof(fn), "%s/fsid", basedir.c_str()); + + // verify fs isn't in use + + fsid_fd = ::open(fn, O_RDWR, 0644); + if (fsid_fd < 0) + return 0; // no fsid, ok. + bool inuse = lock_fsid() < 0; + ::close(fsid_fd); + fsid_fd = -1; + return inuse; +} + int FileStore::mount() { char buf[80]; diff --git a/src/os/FileStore.h b/src/os/FileStore.h index fc1d43e6642..6cceed96b9a 100644 --- a/src/os/FileStore.h +++ b/src/os/FileStore.h @@ -106,6 +106,7 @@ class FileStore : public JournalingObjectStore { lock("FileStore::lock"), sync_epoch(0), stop(false), sync_thread(this), flusher_queue_len(0), flusher_thread(this) { } + bool test_mount_in_use(); int mount(); int umount(); int mkfs(); diff --git a/src/os/ObjectStore.h b/src/os/ObjectStore.h index 8423457dd59..527313d631d 100644 --- a/src/os/ObjectStore.h +++ b/src/os/ObjectStore.h @@ -430,6 +430,7 @@ public: virtual ~ObjectStore() {} // mgmt + virtual bool test_mount_in_use() = 0; virtual int mount() = 0; virtual int umount() = 0; virtual int mkfs() = 0; // wipe diff --git a/src/osd/OSD.cc b/src/osd/OSD.cc index ff7f4a86176..18164a6635c 100644 --- a/src/osd/OSD.cc +++ b/src/osd/OSD.cc @@ -389,17 +389,33 @@ void handle_signal(int signal) void cls_initialize(OSD *_osd); + +int OSD::pre_init() +{ + Mutex::Locker lock(osd_lock); + + assert(!store); + store = create_object_store(dev_path, journal_path); + if (!store) { + dout(0) << " unable to create object store" << dendl; + return -ENODEV; + } + + if (store->test_mount_in_use()) { + dout(0) << "object store " << dev_path << " is currently in use" << dendl; + cout << "object store " << dev_path << " is currently in use (cosd already running?)" << std::endl; + return -EBUSY; + } + return 0; +} + int OSD::init() { Mutex::Locker lock(osd_lock); // mount. dout(2) << "mounting " << dev_path << " " << (journal_path ? journal_path : "(no journal)") << dendl; - store = create_object_store(dev_path, journal_path); - if (!store) { - dout(0) << " unable to create object store" << dendl; - return -ENODEV; - } + assert(store); // call pre_init() first! if (mknewjournal) { int r = store->mkjournal(); diff --git a/src/osd/OSD.h b/src/osd/OSD.h index ece5afcdc23..e378fd6e69e 100644 --- a/src/osd/OSD.h +++ b/src/osd/OSD.h @@ -865,6 +865,7 @@ public: // startup/shutdown + int pre_init(); int init(); int shutdown();