]>
git.proxmox.com Git - ovs.git/blob - ovsdb/ovsdb-tool.c
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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"
29 #include "dynamic-string.h"
35 #include "ovsdb-data.h"
36 #include "ovsdb-error.h"
37 #include "socket-util.h"
43 VLOG_DEFINE_THIS_MODULE(ovsdb_tool
);
45 /* -m, --more: Verbosity level for "show-log" command output. */
46 static int show_log_verbosity
;
48 static const struct command all_commands
[];
50 static void usage(void) NO_RETURN
;
51 static void parse_options(int argc
, char *argv
[]);
53 static const char *default_db(void);
54 static const char *default_schema(void);
57 main(int argc
, char *argv
[])
59 set_program_name(argv
[0]);
60 parse_options(argc
, argv
);
61 signal(SIGPIPE
, SIG_IGN
);
62 run_command(argc
- optind
, argv
+ optind
, all_commands
);
67 parse_options(int argc
, char *argv
[])
69 static struct option long_options
[] = {
70 {"more", no_argument
, NULL
, 'm'},
71 {"verbose", optional_argument
, NULL
, 'v'},
72 {"help", no_argument
, NULL
, 'h'},
73 {"version", no_argument
, NULL
, 'V'},
76 char *short_options
= long_options_to_short_options(long_options
);
81 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
95 ovs_print_version(0, 0);
99 vlog_set_verbosity(optarg
);
115 printf("%s: Open vSwitch database management utility\n"
116 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
117 " create [DB [SCHEMA]] create DB with the given SCHEMA\n"
118 " compact [DB [DST]] compact DB in-place (or to DST)\n"
119 " convert [DB [SCHEMA [DST]]] convert DB to SCHEMA (to DST)\n"
120 " db-version [DB] report version of schema used by DB\n"
121 " db-cksum [DB] report checksum of schema used by DB\n"
122 " schema-version [SCHEMA] report SCHEMA's schema version\n"
123 " schema-cksum [SCHEMA] report SCHEMA's checksum\n"
124 " query [DB] TRNS execute read-only transaction on DB\n"
125 " transact [DB] TRNS execute read/write transaction on DB\n"
126 " [-m]... show-log [DB] print DB's log entries\n"
127 "The default DB is %s.\n"
128 "The default SCHEMA is %s.\n",
129 program_name
, program_name
, default_db(), default_schema());
131 printf("\nOther options:\n"
132 " -m, --more increase show-log verbosity\n"
133 " -h, --help display this help message\n"
134 " -V, --version display version information\n");
143 db
= xasprintf("%s/conf.db", ovs_dbdir());
153 schema
= xasprintf("%s/vswitch.ovsschema", ovs_pkgdatadir());
159 parse_json(const char *s
)
161 struct json
*json
= json_from_string(s
);
162 if (json
->type
== JSON_STRING
) {
163 ovs_fatal(0, "\"%s\": %s", s
, json
->u
.string
);
169 print_and_free_json(struct json
*json
)
171 char *string
= json_to_string(json
, JSSF_SORT
);
178 check_ovsdb_error(struct ovsdb_error
*error
)
181 ovs_fatal(0, "%s", ovsdb_error_to_string(error
));
186 do_create(int argc
, char *argv
[])
188 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
189 const char *schema_file_name
= argc
>= 3 ? argv
[2] : default_schema();
190 struct ovsdb_schema
*schema
;
191 struct ovsdb_log
*log
;
194 /* Read schema from file and convert to JSON. */
195 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema
));
196 json
= ovsdb_schema_to_json(schema
);
197 ovsdb_schema_destroy(schema
);
199 /* Create database file. */
200 check_ovsdb_error(ovsdb_log_open(db_file_name
, OVSDB_LOG_CREATE
,
202 check_ovsdb_error(ovsdb_log_write(log
, json
));
203 check_ovsdb_error(ovsdb_log_commit(log
));
204 ovsdb_log_close(log
);
210 compact_or_convert(const char *src_name_
, const char *dst_name_
,
211 const struct ovsdb_schema
*new_schema
,
214 char *src_name
, *dst_name
;
215 struct lockfile
*src_lock
;
216 struct lockfile
*dst_lock
;
217 bool in_place
= dst_name_
== NULL
;
221 /* Dereference symlinks for source and destination names. In the in-place
222 * case this ensures that, if the source name is a symlink, we replace its
223 * target instead of replacing the symlink by a regular file. In the
224 * non-in-place, this has the same effect for the destination name. */
225 src_name
= follow_symlinks(src_name_
);
227 ? xasprintf("%s.tmp", src_name
)
228 : follow_symlinks(dst_name_
));
230 /* Lock the source, if we will be replacing it. */
232 retval
= lockfile_lock(src_name
, &src_lock
);
234 ovs_fatal(retval
, "%s: failed to lock lockfile", src_name
);
238 /* Get (temporary) destination and lock it. */
239 retval
= lockfile_lock(dst_name
, &dst_lock
);
241 ovs_fatal(retval
, "%s: failed to lock lockfile", dst_name
);
245 check_ovsdb_error(new_schema
246 ? ovsdb_file_open_as_schema(src_name
, new_schema
, &db
)
247 : ovsdb_file_open(src_name
, true, &db
, NULL
));
248 check_ovsdb_error(ovsdb_file_save_copy(dst_name
, false, comment
, db
));
251 /* Replace source. */
253 if (rename(dst_name
, src_name
)) {
254 ovs_fatal(errno
, "failed to rename \"%s\" to \"%s\"",
257 fsync_parent_dir(dst_name
);
258 lockfile_unlock(src_lock
);
261 lockfile_unlock(dst_lock
);
268 do_compact(int argc
, char *argv
[])
270 const char *db
= argc
>= 2 ? argv
[1] : default_db();
271 const char *target
= argc
>= 3 ? argv
[2] : NULL
;
273 compact_or_convert(db
, target
, NULL
, "compacted by ovsdb-tool "VERSION
);
277 do_convert(int argc
, char *argv
[])
279 const char *db
= argc
>= 2 ? argv
[1] : default_db();
280 const char *schema
= argc
>= 3 ? argv
[2] : default_schema();
281 const char *target
= argc
>= 4 ? argv
[3] : NULL
;
282 struct ovsdb_schema
*new_schema
;
284 check_ovsdb_error(ovsdb_schema_from_file(schema
, &new_schema
));
285 compact_or_convert(db
, target
, new_schema
,
286 "converted by ovsdb-tool "VERSION
);
287 ovsdb_schema_destroy(new_schema
);
291 do_needs_conversion(int argc
, char *argv
[])
293 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
294 const char *schema_file_name
= argc
>= 3 ? argv
[2] : default_schema();
295 struct ovsdb_schema
*schema1
, *schema2
;
297 check_ovsdb_error(ovsdb_file_read_schema(db_file_name
, &schema1
));
298 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema2
));
299 puts(ovsdb_schema_equal(schema1
, schema2
) ? "no" : "yes");
300 ovsdb_schema_destroy(schema1
);
301 ovsdb_schema_destroy(schema2
);
305 do_db_version(int argc
, char *argv
[])
307 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
308 struct ovsdb_schema
*schema
;
310 check_ovsdb_error(ovsdb_file_read_schema(db_file_name
, &schema
));
311 puts(schema
->version
);
312 ovsdb_schema_destroy(schema
);
316 do_db_cksum(int argc OVS_UNUSED
, char *argv
[])
318 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
319 struct ovsdb_schema
*schema
;
321 check_ovsdb_error(ovsdb_file_read_schema(db_file_name
, &schema
));
323 ovsdb_schema_destroy(schema
);
327 do_schema_version(int argc
, char *argv
[])
329 const char *schema_file_name
= argc
>= 2 ? argv
[1] : default_schema();
330 struct ovsdb_schema
*schema
;
332 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema
));
333 puts(schema
->version
);
334 ovsdb_schema_destroy(schema
);
338 do_schema_cksum(int argc
, char *argv
[])
340 const char *schema_file_name
= argc
>= 2 ? argv
[1] : default_schema();
341 struct ovsdb_schema
*schema
;
343 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name
, &schema
));
345 ovsdb_schema_destroy(schema
);
349 transact(bool read_only
, int argc
, char *argv
[])
351 const char *db_file_name
= argc
>= 3 ? argv
[1] : default_db();
352 const char *transaction
= argv
[argc
- 1];
353 struct json
*request
, *result
;
356 check_ovsdb_error(ovsdb_file_open(db_file_name
, read_only
, &db
, NULL
));
358 request
= parse_json(transaction
);
359 result
= ovsdb_execute(db
, NULL
, request
, 0, NULL
);
360 json_destroy(request
);
362 print_and_free_json(result
);
367 do_query(int argc
, char *argv
[])
369 transact(true, argc
, argv
);
373 do_transact(int argc
, char *argv
[])
375 transact(false, argc
, argv
);
379 print_db_changes(struct shash
*tables
, struct shash
*names
,
380 const struct ovsdb_schema
*schema
)
382 struct shash_node
*n1
;
384 SHASH_FOR_EACH (n1
, tables
) {
385 const char *table
= n1
->name
;
386 struct ovsdb_table_schema
*table_schema
;
387 struct json
*rows
= n1
->data
;
388 struct shash_node
*n2
;
390 if (n1
->name
[0] == '_' || rows
->type
!= JSON_OBJECT
) {
394 table_schema
= shash_find_data(&schema
->tables
, table
);
395 SHASH_FOR_EACH (n2
, json_object(rows
)) {
396 const char *row_uuid
= n2
->name
;
397 struct json
*columns
= n2
->data
;
398 struct shash_node
*n3
;
399 char *old_name
, *new_name
;
400 bool free_new_name
= false;
402 old_name
= new_name
= shash_find_data(names
, row_uuid
);
403 if (columns
->type
== JSON_OBJECT
) {
404 struct json
*new_name_json
;
406 new_name_json
= shash_find_data(json_object(columns
), "name");
408 new_name
= json_to_string(new_name_json
, JSSF_SORT
);
409 free_new_name
= true;
413 printf("\ttable %s", table
);
417 printf(" insert row %s (%.8s):\n", new_name
, row_uuid
);
419 printf(" insert row %.8s:\n", row_uuid
);
422 printf(" row %s (%.8s):\n", old_name
, row_uuid
);
425 if (columns
->type
== JSON_OBJECT
) {
426 if (show_log_verbosity
> 1) {
427 SHASH_FOR_EACH (n3
, json_object(columns
)) {
428 const char *column
= n3
->name
;
429 const struct ovsdb_column
*column_schema
;
430 struct json
*value
= n3
->data
;
431 char *value_string
= NULL
;
435 ? shash_find_data(&table_schema
->columns
, column
)
438 const struct ovsdb_type
*type
;
439 struct ovsdb_error
*error
;
440 struct ovsdb_datum datum
;
442 type
= &column_schema
->type
;
443 error
= ovsdb_datum_from_json(&datum
, type
,
449 ovsdb_datum_to_string(&datum
, type
, &s
);
450 value_string
= ds_steal_cstr(&s
);
452 ovsdb_error_destroy(error
);
456 value_string
= json_to_string(value
, JSSF_SORT
);
458 printf("\t\t%s=%s\n", column
, value_string
);
463 || (new_name
!= old_name
&& strcmp(old_name
, new_name
))) {
465 shash_delete(names
, shash_find(names
, row_uuid
));
468 shash_add(names
, row_uuid
, (new_name
470 : xmemdup0(row_uuid
, 8)));
472 } else if (columns
->type
== JSON_NULL
) {
473 struct shash_node
*node
;
475 printf("\t\tdelete row\n");
476 node
= shash_find(names
, row_uuid
);
478 shash_delete(names
, node
);
491 do_show_log(int argc
, char *argv
[])
493 const char *db_file_name
= argc
>= 2 ? argv
[1] : default_db();
495 struct ovsdb_log
*log
;
496 struct ovsdb_schema
*schema
;
499 check_ovsdb_error(ovsdb_log_open(db_file_name
, OVSDB_LOG_READ_ONLY
,
506 check_ovsdb_error(ovsdb_log_read(log
, &json
));
511 printf("record %u:", i
);
513 check_ovsdb_error(ovsdb_schema_from_json(json
, &schema
));
514 printf(" \"%s\" schema, version=\"%s\", cksum=\"%s\"\n",
515 schema
->name
, schema
->version
, schema
->cksum
);
516 } else if (json
->type
== JSON_OBJECT
) {
517 struct json
*date
, *comment
;
519 date
= shash_find_data(json_object(json
), "_date");
520 if (date
&& date
->type
== JSON_INTEGER
) {
521 time_t t
= json_integer(date
);
525 strftime(s
, sizeof s
, "%Y-%m-%d %H:%M:%S", gmtime_r(&t
, &tm
));
529 comment
= shash_find_data(json_object(json
), "_comment");
530 if (comment
&& comment
->type
== JSON_STRING
) {
531 printf(" \"%s\"", json_string(comment
));
534 if (i
> 0 && show_log_verbosity
> 0) {
536 print_db_changes(json_object(json
), &names
, schema
);
543 ovsdb_log_close(log
);
544 ovsdb_schema_destroy(schema
);
545 /* XXX free 'names'. */
549 do_help(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
554 static const struct command all_commands
[] = {
555 { "create", 0, 2, do_create
},
556 { "compact", 0, 2, do_compact
},
557 { "convert", 0, 3, do_convert
},
558 { "needs-conversion", 0, 2, do_needs_conversion
},
559 { "db-version", 0, 1, do_db_version
},
560 { "db-cksum", 0, 1, do_db_cksum
},
561 { "schema-version", 0, 1, do_schema_version
},
562 { "schema-cksum", 0, 1, do_schema_cksum
},
563 { "query", 1, 2, do_query
},
564 { "transact", 1, 2, do_transact
},
565 { "show-log", 0, 1, do_show_log
},
566 { "help", 0, INT_MAX
, do_help
},
567 { NULL
, 0, 0, NULL
},