2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
26 #include "command-line.h"
28 #include "dynamic-string.h"
34 #include "ovsdb-data.h"
35 #include "ovsdb-error.h"
36 #include "socket-util.h"
42 VLOG_DEFINE_THIS_MODULE(ovsdb_tool);
44 /* -m, --more: Verbosity level for "show-log" command output. */
45 static int show_log_verbosity;
47 static const struct command all_commands[];
49 static void usage(void) NO_RETURN;
50 static void parse_options(int argc, char *argv[]);
53 main(int argc, char *argv[])
55 set_program_name(argv[0]);
56 parse_options(argc, argv);
57 signal(SIGPIPE, SIG_IGN);
58 run_command(argc - optind, argv + optind, all_commands);
63 parse_options(int argc, char *argv[])
65 static struct option long_options[] = {
66 {"more", no_argument, NULL, 'm'},
67 {"verbose", optional_argument, NULL, 'v'},
68 {"help", no_argument, NULL, 'h'},
69 {"version", no_argument, NULL, 'V'},
72 char *short_options = long_options_to_short_options(long_options);
77 c = getopt_long(argc, argv, short_options, long_options, NULL);
91 ovs_print_version(0, 0);
95 vlog_set_verbosity(optarg);
111 printf("%s: Open vSwitch database management utility\n"
112 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
113 " create DB SCHEMA create DB with the given SCHEMA\n"
114 " compact DB [DST] compact DB in-place (or to DST)\n"
115 " convert DB SCHEMA [DST] convert DB to SCHEMA (to DST)\n"
116 " db-version DB report version of schema used by DB\n"
117 " db-cksum DB report checksum of schema used by DB\n"
118 " schema-version SCHEMA report SCHEMA's schema version\n"
119 " schema-cksum SCHEMA report SCHEMA's checksum\n"
120 " query DB TRNS execute read-only transaction on DB\n"
121 " transact DB TRNS execute read/write transaction on DB\n"
122 " show-log DB prints information about DB's log entries\n",
123 program_name, program_name);
125 printf("\nOther options:\n"
126 " -m, --more increase show-log verbosity\n"
127 " -h, --help display this help message\n"
128 " -V, --version display version information\n");
133 parse_json(const char *s)
135 struct json *json = json_from_string(s);
136 if (json->type == JSON_STRING) {
137 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
143 print_and_free_json(struct json *json)
145 char *string = json_to_string(json, JSSF_SORT);
152 check_ovsdb_error(struct ovsdb_error *error)
155 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
160 do_create(int argc OVS_UNUSED, char *argv[])
162 const char *db_file_name = argv[1];
163 const char *schema_file_name = argv[2];
164 struct ovsdb_schema *schema;
165 struct ovsdb_log *log;
168 /* Read schema from file and convert to JSON. */
169 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
170 json = ovsdb_schema_to_json(schema);
171 ovsdb_schema_destroy(schema);
173 /* Create database file. */
174 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
176 check_ovsdb_error(ovsdb_log_write(log, json));
177 check_ovsdb_error(ovsdb_log_commit(log));
178 ovsdb_log_close(log);
184 compact_or_convert(const char *src_name, const char *dst_name,
185 const struct ovsdb_schema *new_schema,
188 struct lockfile *src_lock;
189 struct lockfile *dst_lock;
190 bool in_place = dst_name == NULL;
194 /* Lock the source, if we will be replacing it. */
196 retval = lockfile_lock(src_name, 0, &src_lock);
198 ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
202 /* Get (temporary) destination and lock it. */
204 dst_name = xasprintf("%s.tmp", src_name);
206 retval = lockfile_lock(dst_name, 0, &dst_lock);
208 ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
212 check_ovsdb_error(new_schema
213 ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
214 : ovsdb_file_open(src_name, true, &db, NULL));
215 check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
218 /* Replace source. */
220 if (rename(dst_name, src_name)) {
221 ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
224 fsync_parent_dir(dst_name);
225 lockfile_unlock(src_lock);
228 lockfile_unlock(dst_lock);
232 do_compact(int argc OVS_UNUSED, char *argv[])
234 compact_or_convert(argv[1], argv[2], NULL,
235 "compacted by ovsdb-tool "VERSION BUILDNR);
239 do_convert(int argc OVS_UNUSED, char *argv[])
241 const char *schema_file_name = argv[2];
242 struct ovsdb_schema *new_schema;
244 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &new_schema));
245 compact_or_convert(argv[1], argv[3], new_schema,
246 "converted by ovsdb-tool "VERSION BUILDNR);
247 ovsdb_schema_destroy(new_schema);
251 do_needs_conversion(int argc OVS_UNUSED, char *argv[])
253 const char *db_file_name = argv[1];
254 const char *schema_file_name = argv[2];
255 struct ovsdb_schema *schema1, *schema2;
257 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema1));
258 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema2));
259 puts(ovsdb_schema_equal(schema1, schema2) ? "no" : "yes");
260 ovsdb_schema_destroy(schema1);
261 ovsdb_schema_destroy(schema2);
265 do_db_version(int argc OVS_UNUSED, char *argv[])
267 const char *db_file_name = argv[1];
268 struct ovsdb_schema *schema;
270 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
271 puts(schema->version);
272 ovsdb_schema_destroy(schema);
276 do_db_cksum(int argc OVS_UNUSED, char *argv[])
278 const char *db_file_name = argv[1];
279 struct ovsdb_schema *schema;
281 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
283 ovsdb_schema_destroy(schema);
287 do_schema_version(int argc OVS_UNUSED, char *argv[])
289 const char *schema_file_name = argv[1];
290 struct ovsdb_schema *schema;
292 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
293 puts(schema->version);
294 ovsdb_schema_destroy(schema);
298 do_schema_cksum(int argc OVS_UNUSED, char *argv[])
300 const char *schema_file_name = argv[1];
301 struct ovsdb_schema *schema;
303 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
305 ovsdb_schema_destroy(schema);
309 transact(bool read_only, const char *db_file_name, const char *transaction)
311 struct json *request, *result;
314 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
316 request = parse_json(transaction);
317 result = ovsdb_execute(db, NULL, request, 0, NULL);
318 json_destroy(request);
320 print_and_free_json(result);
325 do_query(int argc OVS_UNUSED, char *argv[])
327 transact(true, argv[1], argv[2]);
331 do_transact(int argc OVS_UNUSED, char *argv[])
333 transact(false, argv[1], argv[2]);
337 print_db_changes(struct shash *tables, struct shash *names,
338 const struct ovsdb_schema *schema)
340 struct shash_node *n1;
342 SHASH_FOR_EACH (n1, tables) {
343 const char *table = n1->name;
344 struct ovsdb_table_schema *table_schema;
345 struct json *rows = n1->data;
346 struct shash_node *n2;
348 if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
352 table_schema = shash_find_data(&schema->tables, table);
353 SHASH_FOR_EACH (n2, json_object(rows)) {
354 const char *row_uuid = n2->name;
355 struct json *columns = n2->data;
356 struct shash_node *n3;
357 char *old_name, *new_name;
358 bool free_new_name = false;
360 old_name = new_name = shash_find_data(names, row_uuid);
361 if (columns->type == JSON_OBJECT) {
362 struct json *new_name_json;
364 new_name_json = shash_find_data(json_object(columns), "name");
366 new_name = json_to_string(new_name_json, JSSF_SORT);
367 free_new_name = true;
371 printf("\ttable %s", table);
375 printf(" insert row %s (%.8s):\n", new_name, row_uuid);
377 printf(" insert row %.8s:\n", row_uuid);
380 printf(" row %s (%.8s):\n", old_name, row_uuid);
383 if (columns->type == JSON_OBJECT) {
384 if (show_log_verbosity > 1) {
385 SHASH_FOR_EACH (n3, json_object(columns)) {
386 const char *column = n3->name;
387 const struct ovsdb_column *column_schema;
388 struct json *value = n3->data;
389 char *value_string = NULL;
393 ? shash_find_data(&table_schema->columns, column)
396 const struct ovsdb_error *error;
397 const struct ovsdb_type *type;
398 struct ovsdb_datum datum;
400 type = &column_schema->type;
401 error = ovsdb_datum_from_json(&datum, type,
407 ovsdb_datum_to_string(&datum, type, &s);
408 value_string = ds_steal_cstr(&s);
412 value_string = json_to_string(value, JSSF_SORT);
414 printf("\t\t%s=%s\n", column, value_string);
419 || (new_name != old_name && strcmp(old_name, new_name))) {
421 shash_delete(names, shash_find(names, row_uuid));
424 shash_add(names, row_uuid, (new_name
426 : xmemdup0(row_uuid, 8)));
428 } else if (columns->type == JSON_NULL) {
429 struct shash_node *node;
431 printf("\t\tdelete row\n");
432 node = shash_find(names, row_uuid);
434 shash_delete(names, node);
447 do_show_log(int argc OVS_UNUSED, char *argv[])
449 const char *db_file_name = argv[1];
451 struct ovsdb_log *log;
452 struct ovsdb_schema *schema;
455 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
462 check_ovsdb_error(ovsdb_log_read(log, &json));
467 printf("record %u:", i);
469 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
470 printf(" \"%s\" schema, version=\"%s\", cksum=\"%s\"\n",
471 schema->name, schema->version, schema->cksum);
472 } else if (json->type == JSON_OBJECT) {
473 struct json *date, *comment;
475 date = shash_find_data(json_object(json), "_date");
476 if (date && date->type == JSON_INTEGER) {
477 time_t t = json_integer(date);
480 strftime(s, sizeof s, "%Y-%m-%d %H:%M:%S", localtime(&t));
484 comment = shash_find_data(json_object(json), "_comment");
485 if (comment && comment->type == JSON_STRING) {
486 printf(" \"%s\"", json_string(comment));
489 if (i > 0 && show_log_verbosity > 0) {
491 print_db_changes(json_object(json), &names, schema);
498 ovsdb_log_close(log);
499 ovsdb_schema_destroy(schema);
500 /* XXX free 'names'. */
504 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
509 static const struct command all_commands[] = {
510 { "create", 2, 2, do_create },
511 { "compact", 1, 2, do_compact },
512 { "convert", 2, 3, do_convert },
513 { "needs-conversion", 2, 2, do_needs_conversion },
514 { "db-version", 1, 1, do_db_version },
515 { "db-cksum", 1, 1, do_db_cksum },
516 { "schema-version", 1, 1, do_schema_version },
517 { "schema-cksum", 1, 1, do_schema_cksum },
518 { "query", 2, 2, do_query },
519 { "transact", 2, 2, do_transact },
520 { "show-log", 1, 1, do_show_log },
521 { "help", 0, INT_MAX, do_help },
522 { NULL, 0, 0, NULL },