2008-02-01 21:45:55 +00:00
|
|
|
// -*- 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 <errno.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
|
2011-02-22 16:38:45 +00:00
|
|
|
#include "common/config.h"
|
2011-02-20 17:18:03 +00:00
|
|
|
#include "common/ceph_argparse.h"
|
2011-02-16 16:04:29 +00:00
|
|
|
#include "common/common_init.h"
|
2008-02-01 21:45:55 +00:00
|
|
|
#include "mon/MonMap.h"
|
|
|
|
|
2009-03-12 23:08:13 +00:00
|
|
|
void usage()
|
2008-02-01 21:45:55 +00:00
|
|
|
{
|
2010-06-11 16:57:13 +00:00
|
|
|
cout << " usage: [--print] [--create [--clobber]] [--add name 1.2.3.4:567] [--rm name] <mapfilename>" << std::endl;
|
2008-02-01 21:45:55 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
vector<const char*> args;
|
|
|
|
argv_to_vec(argc, argv, args);
|
2009-03-12 23:08:13 +00:00
|
|
|
DEFINE_CONF_VARS(usage);
|
2008-02-01 21:45:55 +00:00
|
|
|
|
|
|
|
const char *me = argv[0];
|
|
|
|
|
2008-03-10 23:23:41 +00:00
|
|
|
const char *fn = 0;
|
2008-02-01 21:45:55 +00:00
|
|
|
bool print = false;
|
|
|
|
bool create = false;
|
|
|
|
bool clobber = false;
|
|
|
|
bool modified = false;
|
2010-06-08 22:17:08 +00:00
|
|
|
map<string,entity_addr_t> add;
|
|
|
|
list<string> rm;
|
2008-02-01 21:45:55 +00:00
|
|
|
|
2011-03-25 00:01:51 +00:00
|
|
|
common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY,
|
|
|
|
CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
|
2011-05-23 17:11:15 +00:00
|
|
|
common_init_finish(&g_ceph_context);
|
2009-03-12 23:08:13 +00:00
|
|
|
FOR_EACH_ARG(args) {
|
2011-03-28 23:10:33 +00:00
|
|
|
if (CEPH_ARGPARSE_EQ("print", '\0')) {
|
|
|
|
CEPH_ARGPARSE_SET_ARG_VAL(&print, OPT_BOOL);
|
|
|
|
} else if (CEPH_ARGPARSE_EQ("create", '\0')) {
|
|
|
|
CEPH_ARGPARSE_SET_ARG_VAL(&create, OPT_BOOL);
|
|
|
|
} else if (CEPH_ARGPARSE_EQ("clobber", '\0')) {
|
|
|
|
CEPH_ARGPARSE_SET_ARG_VAL(&clobber, OPT_BOOL);
|
|
|
|
} else if (CEPH_ARGPARSE_EQ("add", '\0')) {
|
2010-06-08 22:17:08 +00:00
|
|
|
if (++i >= args.size())
|
|
|
|
usage();
|
|
|
|
string name = args[i];
|
2008-11-05 22:54:13 +00:00
|
|
|
if (++i >= args.size())
|
2009-03-12 23:08:13 +00:00
|
|
|
usage();
|
2008-02-01 21:45:55 +00:00
|
|
|
entity_addr_t addr;
|
2010-06-02 05:07:15 +00:00
|
|
|
if (!addr.parse(args[i])) {
|
2008-02-01 21:45:55 +00:00
|
|
|
cerr << me << ": invalid ip:port '" << args[i] << "'" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-06-06 16:25:00 +00:00
|
|
|
if (addr.get_port() == 0)
|
|
|
|
addr.set_port(CEPH_MON_PORT);
|
2010-06-08 22:17:08 +00:00
|
|
|
add[name] = addr;
|
|
|
|
modified = true;
|
2011-03-28 23:10:33 +00:00
|
|
|
} else if (CEPH_ARGPARSE_EQ("rm", '\0')) {
|
2010-06-08 22:17:08 +00:00
|
|
|
if (++i >= args.size())
|
|
|
|
usage();
|
|
|
|
string name = args[i];
|
|
|
|
rm.push_back(name);
|
2008-02-01 21:45:55 +00:00
|
|
|
modified = true;
|
2008-03-10 23:23:41 +00:00
|
|
|
} else if (!fn)
|
|
|
|
fn = args[i];
|
2008-11-05 22:54:13 +00:00
|
|
|
else {
|
2009-03-12 23:08:13 +00:00
|
|
|
cout << "invalid argument: '" << args[i] << "'" << std::endl;
|
|
|
|
usage();
|
2008-11-05 22:54:13 +00:00
|
|
|
}
|
2008-02-01 21:45:55 +00:00
|
|
|
}
|
2008-03-10 23:23:41 +00:00
|
|
|
if (!fn)
|
2009-03-12 23:08:13 +00:00
|
|
|
usage();
|
2008-02-01 21:45:55 +00:00
|
|
|
|
|
|
|
MonMap monmap;
|
|
|
|
|
|
|
|
cout << me << ": monmap file " << fn << std::endl;
|
|
|
|
|
|
|
|
int r = 0;
|
2010-11-23 20:25:11 +00:00
|
|
|
if (!(create && clobber)) {
|
|
|
|
try {
|
|
|
|
r = monmap.read(fn);
|
|
|
|
} catch (...) {
|
|
|
|
cerr << me << ": unable to read monmap file" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2008-02-01 21:45:55 +00:00
|
|
|
|
2009-09-18 23:17:04 +00:00
|
|
|
char buf[80];
|
2008-02-01 21:45:55 +00:00
|
|
|
if (!create && r < 0) {
|
2011-02-16 18:28:48 +00:00
|
|
|
cerr << me << ": couldn't open " << fn << ": " << strerror_r(-r, buf, sizeof(buf)) << std::endl;
|
2008-02-01 21:45:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (create && !clobber && r == 0) {
|
|
|
|
cerr << me << ": " << fn << " exists, --clobber to overwrite" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-04-08 21:30:14 +00:00
|
|
|
if (create) {
|
2008-04-09 18:09:49 +00:00
|
|
|
srand(getpid() + time(0));
|
2008-04-08 21:30:14 +00:00
|
|
|
monmap.generate_fsid();
|
|
|
|
cout << me << ": generated fsid " << monmap.fsid << std::endl;
|
|
|
|
modified++;
|
|
|
|
}
|
|
|
|
|
2010-06-11 17:15:29 +00:00
|
|
|
for (map<string,entity_addr_t>::iterator p = add.begin(); p != add.end(); p++) {
|
|
|
|
if (monmap.contains(p->first)) {
|
|
|
|
cerr << me << ": map already contains mon." << p->first << std::endl;
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (monmap.contains(p->second)) {
|
|
|
|
cerr << me << ": map already contains " << p->second << std::endl;
|
|
|
|
usage();
|
|
|
|
}
|
2010-06-08 22:17:08 +00:00
|
|
|
monmap.add(p->first, p->second);
|
2010-06-11 17:15:29 +00:00
|
|
|
}
|
2010-06-08 22:17:08 +00:00
|
|
|
for (list<string>::iterator p = rm.begin(); p != rm.end(); p++) {
|
2008-02-01 21:45:55 +00:00
|
|
|
cout << me << ": removing " << *p << std::endl;
|
2010-06-08 22:17:08 +00:00
|
|
|
if (!monmap.contains(*p)) {
|
2008-02-01 21:45:55 +00:00
|
|
|
cerr << me << ": map does not contain " << *p << std::endl;
|
2009-03-12 23:08:13 +00:00
|
|
|
usage();
|
2008-02-01 21:45:55 +00:00
|
|
|
}
|
2010-06-08 22:17:08 +00:00
|
|
|
monmap.remove(*p);
|
2008-02-01 21:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!print && !modified)
|
2009-03-12 23:08:13 +00:00
|
|
|
usage();
|
2008-02-01 21:45:55 +00:00
|
|
|
|
2008-03-10 23:23:41 +00:00
|
|
|
if (modified)
|
|
|
|
monmap.epoch++;
|
|
|
|
|
2008-02-01 21:45:55 +00:00
|
|
|
if (print)
|
2009-10-11 04:34:40 +00:00
|
|
|
monmap.print(cout);
|
2008-02-01 21:45:55 +00:00
|
|
|
|
|
|
|
if (modified) {
|
|
|
|
// write it out
|
|
|
|
cout << me << ": writing epoch " << monmap.epoch
|
|
|
|
<< " to " << fn
|
|
|
|
<< " (" << monmap.size() << " monitors)"
|
|
|
|
<< std::endl;
|
|
|
|
int r = monmap.write(fn);
|
2009-03-10 16:49:06 +00:00
|
|
|
if (r < 0) {
|
2009-09-18 23:17:04 +00:00
|
|
|
cerr << "monmaptool: error writing to '" << fn << "': " << strerror_r(-r, buf, sizeof(buf)) << std::endl;
|
2009-03-10 16:49:06 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2008-02-01 21:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|