ceph/src/cosd.cc

175 lines
4.6 KiB
C++
Raw Normal View History

// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <iostream>
#include <string>
using namespace std;
#include "config.h"
#include "mon/MonMap.h"
#include "mon/MonClient.h"
#include "osd/OSD.h"
#include "msg/SimpleMessenger.h"
#include "common/Timer.h"
2009-02-09 23:46:02 +00:00
#include "common/common_init.h"
#include "include/color.h"
2008-03-11 01:36:50 +00:00
void usage()
{
2009-03-11 21:58:58 +00:00
cerr << "usage: cosd -i osdid [--osd-data=path] [--osd-journal=path] [--mkfs]" << std::endl;
2008-03-11 01:36:50 +00:00
cerr << " --debug_osd N set debug level (e.g. 10)" << std::endl;
2009-03-10 22:22:52 +00:00
generic_server_usage();
2008-03-11 01:36:50 +00:00
}
2008-01-01 22:04:31 +00:00
int main(int argc, const char **argv)
{
DEFINE_CONF_VARS(usage);
2008-01-01 22:04:31 +00:00
vector<const char*> args;
argv_to_vec(argc, argv, args);
2008-11-05 22:54:13 +00:00
env_to_vec(args);
2009-03-20 19:12:33 +00:00
common_init(args, "osd", true);
if (g_conf.clock_tare) g_clock.tare();
// osd specific args
2008-03-12 13:31:36 +00:00
bool mkfs = 0;
FOR_EACH_ARG(args) {
if (CONF_ARG_EQ("mkfs", '\0')) {
2008-03-11 01:36:50 +00:00
mkfs = 1;
} else {
cerr << "unrecognized arg " << args[i] << std::endl;
ARGS_USAGE();
}
}
2009-03-11 21:58:58 +00:00
// whoami
char *end;
int whoami = strtol(g_conf.id, &end, 10);
if (*end || end == g_conf.id || whoami < 0) {
cerr << "must specify '-i #' where # is the osd number" << std::endl;
2008-03-11 01:36:50 +00:00
usage();
}
2009-03-11 21:58:58 +00:00
if (!g_conf.osd_data) {
cerr << "must specify '--osd-data=foo' data path" << std::endl;
2008-03-11 01:36:50 +00:00
usage();
}
2008-01-25 20:04:31 +00:00
_dout_create_courtesy_output_symlink("osd", whoami);
// get monmap
MonClient mc;
if (mc.build_initial_monmap() < 0)
return -1;
if (mc.get_monmap_privately() < 0)
2008-01-26 17:33:13 +00:00
return -1;
2008-03-11 01:36:50 +00:00
if (mkfs) {
int err = OSD::mkfs(g_conf.osd_data, g_conf.osd_journal, mc.monmap.fsid, whoami);
2008-03-11 01:36:50 +00:00
if (err < 0) {
2009-03-11 21:58:58 +00:00
cerr << "error creating empty object store in " << g_conf.osd_data << ": " << strerror(-err) << std::endl;
2008-03-11 01:36:50 +00:00
exit(1);
}
cout << "created object store for osd" << whoami << " fsid " << mc.monmap.fsid << " on " << g_conf.osd_data << std::endl;
2008-03-11 01:36:50 +00:00
exit(0);
}
2009-03-11 21:58:58 +00:00
nstring magic;
ceph_fsid_t fsid;
int w;
int r = OSD::peek_super(g_conf.osd_data, magic, fsid, w);
if (r < 0) {
cerr << TEXT_RED << " ** " << TEXT_HAZARD << "ERROR: " << TEXT_RED
<< "unable to open OSD superblock on " << g_conf.osd_data << ": " << strerror(-r) << TEXT_NORMAL << std::endl;
if (r == -ENOTSUP)
cerr << TEXT_RED << " ** please verify that underlying storage supports xattrs" << TEXT_NORMAL << std::endl;
derr(0) << "unable to open OSD superblock on " << g_conf.osd_data << ": " << strerror(-r) << dendl;
2009-03-11 21:58:58 +00:00
exit(1);
}
if (w != whoami) {
cerr << "OSD id " << w << " != my id " << whoami << std::endl;
exit(1);
}
if (strcmp(magic.c_str(), CEPH_OSD_ONDISK_MAGIC)) {
cerr << "OSD magic " << magic << " != my " << CEPH_OSD_ONDISK_MAGIC << std::endl;
exit(1);
}
if (ceph_fsid_compare(&fsid, &mc.monmap.fsid)) {
cerr << "OSD fsid " << fsid << " != monmap fsid " << mc.monmap.fsid << std::endl;
2009-03-11 21:58:58 +00:00
exit(1);
2008-03-11 01:36:50 +00:00
}
// start up network
SimpleMessenger rank;
2008-01-26 17:33:13 +00:00
rank.bind();
rank.set_addr(mc.get_my_addr());
2008-01-26 17:33:13 +00:00
cout << "starting osd" << whoami
<< " at " << rank.get_rank_addr()
2009-03-12 17:56:37 +00:00
<< " osd_data " << g_conf.osd_data
<< " " << ((g_conf.osd_journal && g_conf.osd_journal[0]) ? g_conf.osd_journal:"(no journal)")
<< " fsid " << mc.monmap.fsid
2008-01-26 17:33:13 +00:00
<< std::endl;
2008-07-22 17:54:13 +00:00
g_timer.shutdown();
Messenger *m = rank.register_entity(entity_name_t::OSD(whoami));
2009-02-13 00:47:56 +00:00
assert_warn(m);
if (!m)
return 1;
Messenger *hbm = rank.register_entity(entity_name_t::OSD(whoami));
2009-02-13 00:47:56 +00:00
assert_warn(hbm);
if (!hbm)
return 1;
rank.set_default_policy(SimpleMessenger::Policy::stateless_server());
rank.set_policy(entity_name_t::TYPE_MON, SimpleMessenger::Policy::lossy_fast_fail());
rank.set_policy(entity_name_t::TYPE_OSD, SimpleMessenger::Policy::lossless());
rank.start();
// start osd
OSD *osd = new OSD(whoami, m, hbm, &mc, g_conf.osd_data, g_conf.osd_journal);
if (osd->init() < 0) {
cout << "error initializing osd" << std::endl;
return 1;
}
rank.wait();
// done
delete osd;
// cd on exit, so that gmon.out (if any) goes into a separate directory for each node.
char s[20];
sprintf(s, "gmon/%d", getpid());
if (mkdir(s, 0755) == 0)
chdir(s);
return 0;
}