2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include "lvm2cmdline.h"
19 #include "lvm-version.h"
23 #include "last-path-component.h"
30 #include <sys/resource.h>
32 #ifdef HAVE_GETOPTLONG
34 # define GETOPTLONG_FN(a, b, c, d, e) getopt_long((a), (b), (c), (d), (e))
35 # define OPTIND_INIT 0
41 # define GETOPTLONG_FN(a, b, c, d, e) getopt((a), (b), (c))
42 # define OPTIND_INIT 1
45 #ifdef UDEV_SYNC_SUPPORT
46 # define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
51 * Table of valid switches
53 static struct arg_props _arg_props[ARG_COUNT + 1] = {
54 #define arg(a, b, c, d, e) {b, "", "--" c, d, e},
59 static struct cmdline_context _cmdline;
61 /* Command line args */
62 unsigned arg_count(const struct cmd_context *cmd, int a)
64 return cmd->arg_values[a].count;
67 unsigned grouped_arg_count(const struct arg_values *av, int a)
72 unsigned arg_is_set(const struct cmd_context *cmd, int a)
74 return arg_count(cmd, a) ? 1 : 0;
77 unsigned grouped_arg_is_set(const struct arg_values *av, int a)
79 return grouped_arg_count(av, a) ? 1 : 0;
82 const char *arg_value(struct cmd_context *cmd, int a)
84 return cmd->arg_values[a].value;
87 const char *arg_str_value(struct cmd_context *cmd, int a, const char *def)
89 return arg_count(cmd, a) ? cmd->arg_values[a].value : def;
92 const char *grouped_arg_str_value(const struct arg_values *av, int a, const char *def)
94 return grouped_arg_count(av, a) ? av[a].value : def;
97 int32_t arg_int_value(struct cmd_context *cmd, int a, const int32_t def)
99 return arg_count(cmd, a) ? cmd->arg_values[a].i_value : def;
102 uint32_t arg_uint_value(struct cmd_context *cmd, int a, const uint32_t def)
104 return arg_count(cmd, a) ? cmd->arg_values[a].ui_value : def;
107 int64_t arg_int64_value(struct cmd_context *cmd, int a, const int64_t def)
109 return arg_count(cmd, a) ? cmd->arg_values[a].i64_value : def;
112 uint64_t arg_uint64_value(struct cmd_context *cmd, int a, const uint64_t def)
114 return arg_count(cmd, a) ? cmd->arg_values[a].ui64_value : def;
118 const void *arg_ptr_value(struct cmd_context *cmd, int a, const void *def)
120 return arg_count(cmd, a) ? cmd->arg_values[a].ptr : def;
124 sign_t arg_sign_value(struct cmd_context *cmd, int a, const sign_t def)
126 return arg_count(cmd, a) ? cmd->arg_values[a].sign : def;
129 percent_type_t arg_percent_value(struct cmd_context *cmd, int a, const percent_type_t def)
131 return arg_count(cmd, a) ? cmd->arg_values[a].percent : def;
134 int arg_count_increment(struct cmd_context *cmd, int a)
136 return cmd->arg_values[a].count++;
139 int yes_no_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
141 av->sign = SIGN_NONE;
142 av->percent = PERCENT_NONE;
144 if (!strcmp(av->value, "y")) {
149 else if (!strcmp(av->value, "n")) {
160 int yes_no_excl_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
162 av->sign = SIGN_NONE;
163 av->percent = PERCENT_NONE;
165 if (!strcmp(av->value, "e") || !strcmp(av->value, "ey") ||
166 !strcmp(av->value, "ye")) {
167 av->i_value = CHANGE_AE;
168 av->ui_value = CHANGE_AE;
171 else if (!strcmp(av->value, "y")) {
172 av->i_value = CHANGE_AY;
173 av->ui_value = CHANGE_AY;
176 else if (!strcmp(av->value, "n") || !strcmp(av->value, "en") ||
177 !strcmp(av->value, "ne")) {
178 av->i_value = CHANGE_AN;
179 av->ui_value = CHANGE_AN;
182 else if (!strcmp(av->value, "ln") || !strcmp(av->value, "nl")) {
183 av->i_value = CHANGE_ALN;
184 av->ui_value = CHANGE_ALN;
187 else if (!strcmp(av->value, "ly") || !strcmp(av->value, "yl")) {
188 av->i_value = CHANGE_ALY;
189 av->ui_value = CHANGE_ALY;
198 int metadatatype_arg(struct cmd_context *cmd, struct arg_values *av)
200 return get_format_by_name(cmd, av->value) ? 1 : 0;
203 static int _get_int_arg(struct arg_values *av, char **ptr)
208 av->percent = PERCENT_NONE;
213 av->sign = SIGN_PLUS;
217 av->sign = SIGN_MINUS;
221 av->sign = SIGN_NONE;
227 v = strtol(val, ptr, 10);
232 av->i_value = (int32_t) v;
233 av->ui_value = (uint32_t) v;
234 av->i64_value = (int64_t) v;
235 av->ui64_value = (uint64_t) v;
240 /* Size stored in sectors */
241 static int _size_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av, int factor)
245 static const char *suffixes = "kmgtpebs";
248 uint64_t v_tmp, adjustment;
250 av->percent = PERCENT_NONE;
255 av->sign = SIGN_PLUS;
259 av->sign = SIGN_MINUS;
263 av->sign = SIGN_NONE;
269 v = strtod(val, &ptr);
275 for (i = strlen(suffixes) - 1; i >= 0; i--)
276 if (suffixes[i] == tolower((int) *ptr))
286 v_tmp = (uint64_t) v;
287 adjustment = v_tmp % 512;
289 v_tmp += (512 - adjustment);
290 log_error("Size is not a multiple of 512. "
291 "Try using %"PRIu64" or %"PRIu64".",
297 /* all other units: kmgtpe */
305 av->i_value = (int32_t) v;
306 av->ui_value = (uint32_t) v;
307 av->i64_value = (int64_t) v;
308 av->ui64_value = (uint64_t) v;
313 int size_kb_arg(struct cmd_context *cmd, struct arg_values *av)
315 return _size_arg(cmd, av, 2);
318 int size_mb_arg(struct cmd_context *cmd, struct arg_values *av)
320 return _size_arg(cmd, av, 2048);
323 int int_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
327 if (!_get_int_arg(av, &ptr) || (*ptr) || (av->sign == SIGN_MINUS))
333 int int_arg_with_sign(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
337 if (!_get_int_arg(av, &ptr) || (*ptr))
343 int int_arg_with_sign_and_percent(struct cmd_context *cmd __attribute__((unused)),
344 struct arg_values *av)
348 if (!_get_int_arg(av, &ptr))
357 if (!strcasecmp(ptr, "V") || !strcasecmp(ptr, "VG"))
358 av->percent = PERCENT_VG;
359 else if (!strcasecmp(ptr, "L") || !strcasecmp(ptr, "LV"))
360 av->percent = PERCENT_LV;
361 else if (!strcasecmp(ptr, "P") || !strcasecmp(ptr, "PV") ||
362 !strcasecmp(ptr, "PVS"))
363 av->percent = PERCENT_PVS;
364 else if (!strcasecmp(ptr, "F") || !strcasecmp(ptr, "FR") ||
365 !strcasecmp(ptr, "FREE"))
366 av->percent = PERCENT_FREE;
367 else if (!strcasecmp(ptr, "O") || !strcasecmp(ptr, "OR") ||
368 !strcasecmp(ptr, "ORIGIN"))
369 av->percent = PERCENT_ORIGIN;
376 int minor_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
380 if (!_get_int_arg(av, &ptr) || (*ptr) || (av->sign == SIGN_MINUS))
383 if (av->i_value > 255) {
384 log_error("Minor number outside range 0-255");
391 int major_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
395 if (!_get_int_arg(av, &ptr) || (*ptr) || (av->sign == SIGN_MINUS))
398 if (av->i_value > 255) {
399 log_error("Major number outside range 0-255");
403 /* FIXME Also Check against /proc/devices */
408 int string_arg(struct cmd_context *cmd __attribute__((unused)),
409 struct arg_values *av __attribute__((unused)))
414 int tag_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
416 char *pos = av->value;
421 if (!validate_tag(pos))
429 int permission_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
431 av->sign = SIGN_NONE;
433 if ((!strcmp(av->value, "rw")) || (!strcmp(av->value, "wr")))
434 av->ui_value = LVM_READ | LVM_WRITE;
436 else if (!strcmp(av->value, "r"))
437 av->ui_value = LVM_READ;
445 int alloc_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
447 alloc_policy_t alloc;
449 av->sign = SIGN_NONE;
451 alloc = get_alloc_from_string(av->value);
452 if (alloc == ALLOC_INVALID)
455 av->ui_value = (uint32_t) alloc;
460 int segtype_arg(struct cmd_context *cmd, struct arg_values *av)
462 return get_segtype_from_string(cmd, av->value) ? 1 : 0;
466 * Positive integer, zero or "auto".
468 int readahead_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av)
470 if (!strcasecmp(av->value, "auto")) {
471 av->ui_value = DM_READ_AHEAD_AUTO;
475 if (!strcasecmp(av->value, "none")) {
476 av->ui_value = DM_READ_AHEAD_NONE;
480 if (!_size_arg(cmd, av, 1))
483 if (av->sign == SIGN_MINUS)
490 * Non-zero, positive integer, "all", or "unmanaged"
492 int metadatacopies_arg(struct cmd_context *cmd, struct arg_values *av)
494 if (!strncmp(cmd->command->name, "vg", 2)) {
495 if (!strcasecmp(av->value, "all")) {
496 av->ui_value = VGMETADATACOPIES_ALL;
500 if (!strcasecmp(av->value, "unmanaged")) {
501 av->ui_value = VGMETADATACOPIES_UNMANAGED;
506 return int_arg(cmd, av);
509 static void __alloc(int size)
511 if (!(_cmdline.commands = dm_realloc(_cmdline.commands, sizeof(*_cmdline.commands) * size))) {
512 log_fatal("Couldn't allocate memory.");
516 _cmdline.commands_size = size;
519 static void _alloc_command(void)
521 if (!_cmdline.commands_size)
524 if (_cmdline.commands_size <= _cmdline.num_commands)
525 __alloc(2 * _cmdline.commands_size);
528 static void _create_new_command(const char *name, command_fn command,
530 const char *desc, const char *usagestr,
531 int nargs, int *args)
537 nc = _cmdline.commands + _cmdline.num_commands++;
541 nc->usage = usagestr;
544 nc->num_args = nargs;
545 nc->valid_args = args;
548 static void _register_command(const char *name, command_fn fn, const char *desc,
549 unsigned flags, const char *usagestr, ...)
555 /* count how many arguments we have */
556 va_start(ap, usagestr);
557 while (va_arg(ap, int) >= 0)
561 /* allocate space for them */
562 if (!(args = dm_malloc(sizeof(*args) * nargs))) {
563 log_fatal("Out of memory.");
568 va_start(ap, usagestr);
569 for (i = 0; i < nargs; i++)
570 args[i] = va_arg(ap, int);
573 /* enter the command in the register */
574 _create_new_command(name, fn, flags, desc, usagestr, nargs, args);
577 void lvm_register_commands(void)
579 #define xx(a, b, c, d...) _register_command(# a, a, b, c, ## d, \
581 debug_ARG, help_ARG, help2_ARG, \
582 version_ARG, verbose_ARG, \
583 quiet_ARG, config_ARG, -1);
584 #include "commands.h"
588 static struct command *_find_command(const char *name)
593 base = last_path_component(name);
595 for (i = 0; i < _cmdline.num_commands; i++) {
596 if (!strcmp(base, _cmdline.commands[i].name))
600 if (i >= _cmdline.num_commands)
603 return _cmdline.commands + i;
606 static void _short_usage(const char *name)
608 log_error("Run `%s --help' for more information.", name);
611 static int _usage(const char *name)
613 struct command *com = _find_command(name);
616 log_print("%s: no such command.", name);
620 log_print("%s: %s\n\n%s", com->name, com->desc, com->usage);
625 * Sets up the short and long argument. If there
626 * is no short argument then the index of the
627 * argument in the the_args array is set as the
628 * long opt value. Yuck. Of course this means we
629 * can't have more than 'a' long arguments.
631 static void _add_getopt_arg(int arg, char **ptr, struct option **o)
633 struct arg_props *a = _cmdline.arg_props + arg;
636 *(*ptr)++ = a->short_arg;
641 #ifdef HAVE_GETOPTLONG
642 if (*(a->long_arg + 2)) {
643 (*o)->name = a->long_arg + 2;
644 (*o)->has_arg = a->fn ? 1 : 0;
647 (*o)->val = a->short_arg;
655 static int _find_arg(struct command *com, int opt)
660 for (i = 0; i < com->num_args; i++) {
661 arg = com->valid_args[i];
662 a = _cmdline.arg_props + arg;
665 * opt should equal either the
666 * short arg, or the index into
669 if ((a->short_arg && (opt == a->short_arg)) ||
670 (!a->short_arg && (opt == arg)))
677 static int _process_command_line(struct cmd_context *cmd, int *argc,
681 char str[((ARG_COUNT + 1) * 2) + 1], *ptr = str;
682 struct option opts[ARG_COUNT + 1], *o = opts;
684 struct arg_values *av;
685 struct arg_value_group_list *current_group = NULL;
687 if (!(cmd->arg_values = dm_pool_zalloc(cmd->mem, sizeof(*cmd->arg_values) * ARG_COUNT))) {
688 log_fatal("Unable to allocate memory for command line arguments.");
692 /* fill in the short and long opts */
693 for (i = 0; i < cmd->command->num_args; i++)
694 _add_getopt_arg(cmd->command->valid_args[i], &ptr, &o);
697 memset(o, 0, sizeof(*o));
699 /* initialise getopt_long & scan for command line switches */
701 optind = OPTIND_INIT;
702 while ((opt = GETOPTLONG_FN(*argc, *argv, str, opts, NULL)) >= 0) {
707 if ((arg = _find_arg(cmd->command, opt)) < 0) {
708 log_fatal("Unrecognised option.");
712 a = _cmdline.arg_props + arg;
714 av = &cmd->arg_values[arg];
716 if (a->flags & ARG_GROUPABLE) {
717 /* Start a new group of arguments the first time or if a non-countable argument is repeated. */
718 if (!current_group || (current_group->arg_values[arg].count && !(a->flags & ARG_COUNTABLE))) {
719 /* FIXME Reduce size including only groupable args */
720 if (!(current_group = dm_pool_zalloc(cmd->mem, sizeof(struct arg_value_group_list) + sizeof(*cmd->arg_values) * ARG_COUNT))) {
721 log_fatal("Unable to allocate memory for command line arguments.");
725 dm_list_add(&cmd->arg_value_groups, ¤t_group->list);
727 /* Maintain total argument count as well as count within each group */
729 av = ¤t_group->arg_values[arg];
732 if (av->count && !(a->flags & ARG_COUNTABLE)) {
733 log_error("Option%s%c%s%s may not be repeated.",
734 a->short_arg ? " -" : "",
735 a->short_arg ? : ' ',
736 (a->short_arg && a->long_arg) ?
737 "/" : "", a->long_arg ? : "");
743 log_error("Option requires argument.");
749 if (!a->fn(cmd, av)) {
750 log_error("Invalid argument for %s: %s", a->long_arg, optarg);
763 static int _merge_synonym(struct cmd_context *cmd, int oldarg, int newarg)
765 const struct arg_values *old;
766 struct arg_values *new;
768 if (arg_count(cmd, oldarg) && arg_count(cmd, newarg)) {
769 log_error("%s and %s are synonyms. Please only supply one.",
770 _cmdline.arg_props[oldarg].long_arg, _cmdline.arg_props[newarg].long_arg);
774 if (!arg_count(cmd, oldarg))
777 old = cmd->arg_values + oldarg;
778 new = cmd->arg_values + newarg;
780 new->count = old->count;
781 new->value = old->value;
782 new->i_value = old->i_value;
783 new->ui_value = old->ui_value;
784 new->i64_value = old->i64_value;
785 new->ui64_value = old->ui64_value;
786 new->sign = old->sign;
791 int version(struct cmd_context *cmd __attribute__((unused)),
792 int argc __attribute__((unused)),
793 char **argv __attribute__((unused)))
797 log_print("LVM version: %s", LVM_VERSION);
798 if (library_version(vsn, sizeof(vsn)))
799 log_print("Library version: %s", vsn);
800 if (driver_version(vsn, sizeof(vsn)))
801 log_print("Driver version: %s", vsn);
803 return ECMD_PROCESSED;
806 static int _get_settings(struct cmd_context *cmd)
808 cmd->current_settings = cmd->default_settings;
810 if (arg_count(cmd, debug_ARG))
811 cmd->current_settings.debug = _LOG_FATAL +
812 (arg_count(cmd, debug_ARG) - 1);
814 if (arg_count(cmd, verbose_ARG))
815 cmd->current_settings.verbose = arg_count(cmd, verbose_ARG);
817 if (arg_count(cmd, quiet_ARG)) {
818 cmd->current_settings.debug = 0;
819 cmd->current_settings.verbose = 0;
822 if (arg_count(cmd, test_ARG))
823 cmd->current_settings.test = arg_count(cmd, test_ARG);
825 if (arg_count(cmd, driverloaded_ARG)) {
826 cmd->current_settings.activation =
827 arg_int_value(cmd, driverloaded_ARG,
828 cmd->default_settings.activation);
831 cmd->current_settings.archive = arg_int_value(cmd, autobackup_ARG, cmd->current_settings.archive);
832 cmd->current_settings.backup = arg_int_value(cmd, autobackup_ARG, cmd->current_settings.backup);
833 cmd->current_settings.cache_vgmetadata = cmd->command->flags & CACHE_VGMETADATA ? 1 : 0;
834 cmd->partial_activation = 0;
836 if (arg_count(cmd, partial_ARG)) {
837 cmd->partial_activation = 1;
838 log_print("Partial mode. Incomplete logical volumes will be processed.");
841 if (arg_count(cmd, ignorelockingfailure_ARG) || arg_count(cmd, sysinit_ARG))
842 init_ignorelockingfailure(1);
844 init_ignorelockingfailure(0);
846 if (arg_count(cmd, nosuffix_ARG))
847 cmd->current_settings.suffix = 0;
849 if (arg_count(cmd, units_ARG))
850 if (!(cmd->current_settings.unit_factor =
851 units_to_bytes(arg_str_value(cmd, units_ARG, ""),
852 &cmd->current_settings.unit_type))) {
853 log_error("Invalid units specification");
854 return EINVALID_CMD_LINE;
857 if (arg_count(cmd, trustcache_ARG)) {
858 if (arg_count(cmd, all_ARG)) {
859 log_error("--trustcache is incompatible with --all");
860 return EINVALID_CMD_LINE;
863 log_warn("WARNING: Cache file of PVs will be trusted. "
864 "New devices holding PVs may get ignored.");
868 if (arg_count(cmd, noudevsync_ARG))
869 cmd->current_settings.udev_sync = 0;
871 /* Handle synonyms */
872 if (!_merge_synonym(cmd, resizable_ARG, resizeable_ARG) ||
873 !_merge_synonym(cmd, allocation_ARG, allocatable_ARG) ||
874 !_merge_synonym(cmd, allocation_ARG, resizeable_ARG) ||
875 !_merge_synonym(cmd, virtualoriginsize_ARG, virtualsize_ARG))
876 return EINVALID_CMD_LINE;
878 if ((!strncmp(cmd->command->name, "pv", 2) &&
879 !_merge_synonym(cmd, metadatacopies_ARG, pvmetadatacopies_ARG)) ||
880 (!strncmp(cmd->command->name, "vg", 2) &&
881 !_merge_synonym(cmd, metadatacopies_ARG, vgmetadatacopies_ARG)))
882 return EINVALID_CMD_LINE;
884 /* Zero indicates success */
888 static int _process_common_commands(struct cmd_context *cmd)
890 if (arg_count(cmd, help_ARG) || arg_count(cmd, help2_ARG)) {
891 _usage(cmd->command->name);
892 return ECMD_PROCESSED;
895 if (arg_count(cmd, version_ARG)) {
896 return version(cmd, 0, (char **) NULL);
899 /* Zero indicates it's OK to continue processing this command */
903 static void _display_help(void)
907 log_error("Available lvm commands:");
908 log_error("Use 'lvm help <command>' for more information");
911 for (i = 0; i < _cmdline.num_commands; i++) {
912 struct command *com = _cmdline.commands + i;
914 log_error("%-16.16s%s", com->name, com->desc);
918 int help(struct cmd_context *cmd __attribute__((unused)), int argc, char **argv)
920 int ret = ECMD_PROCESSED;
926 for (i = 0; i < argc; i++)
927 if (!_usage(argv[i]))
928 ret = EINVALID_CMD_LINE;
934 static void _apply_settings(struct cmd_context *cmd)
936 init_debug(cmd->current_settings.debug);
937 init_verbose(cmd->current_settings.verbose + VERBOSE_BASE_LEVEL);
938 init_test(cmd->current_settings.test);
939 init_full_scan_done(0);
940 init_mirror_in_sync(0);
942 init_msg_prefix(cmd->default_settings.msg_prefix);
943 init_cmd_name(cmd->default_settings.cmd_name);
945 archive_enable(cmd, cmd->current_settings.archive);
946 backup_enable(cmd, cmd->current_settings.backup);
948 set_activation(cmd->current_settings.activation);
950 cmd->fmt = get_format_by_name(cmd, arg_str_value(cmd, metadatatype_ARG,
951 cmd->current_settings.fmt_name));
953 cmd->handles_missing_pvs = 0;
956 static int _set_udev_checking(struct cmd_context *cmd)
958 #ifdef UDEV_SYNC_SUPPORT
960 const char *udev_dev_dir;
961 size_t udev_dev_dir_len;
964 if (!(udev = udev_new()) ||
965 !(udev_dev_dir = udev_get_dev_path(udev)) ||
967 log_error("Could not get udev dev path.");
970 udev_dev_dir_len = strlen(udev_dev_dir);
972 /* There's always a slash at the end of dev_dir. But check udev_dev_dir! */
973 if (udev_dev_dir[udev_dev_dir_len - 1] != '/')
974 dirs_diff = strncmp(cmd->dev_dir, udev_dev_dir,
977 dirs_diff = strcmp(cmd->dev_dir, udev_dev_dir);
980 log_debug("The path %s used for creating device nodes and "
981 "symlinks that is set in the configuration differs "
982 "from the path %s that is used by udev. All warnings "
983 "about udev not working correctly while processing "
984 "particular nodes and symlinks will be suppressed. "
985 "These nodes and symlinks will be managed in each "
986 "directory separately.",
987 cmd->dev_dir, udev_dev_dir);
988 dm_udev_set_checking(0);
989 init_udev_checking(0);
997 static const char *_copy_command_line(struct cmd_context *cmd, int argc, char **argv)
1002 * Build up the complete command line, used as a
1003 * description for backups.
1005 if (!dm_pool_begin_object(cmd->mem, 128))
1008 for (i = 0; i < argc; i++) {
1009 space = strchr(argv[i], ' ') ? 1 : 0;
1011 if (space && !dm_pool_grow_object(cmd->mem, "'", 1))
1014 if (!dm_pool_grow_object(cmd->mem, argv[i], strlen(argv[i])))
1017 if (space && !dm_pool_grow_object(cmd->mem, "'", 1))
1021 if (!dm_pool_grow_object(cmd->mem, " ", 1))
1028 if (!dm_pool_grow_object(cmd->mem, "\0", 1))
1031 return dm_pool_end_object(cmd->mem);
1034 log_error("Couldn't copy command line.");
1035 dm_pool_abandon_object(cmd->mem);
1039 int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
1044 init_error_message_produced(0);
1046 /* each command should start out with sigint flag cleared */
1049 if (!(cmd->cmd_line = _copy_command_line(cmd, argc, argv))) {
1054 log_debug("Parsing: %s", cmd->cmd_line);
1056 if (!(cmd->command = _find_command(argv[0])))
1057 return ENO_SUCH_CMD;
1059 if (!_process_command_line(cmd, &argc, &argv)) {
1060 log_error("Error during parsing of command line.");
1061 return EINVALID_CMD_LINE;
1064 set_cmd_name(cmd->command->name);
1066 if (arg_count(cmd, config_ARG))
1067 if (override_config_tree_from_string(cmd,
1068 arg_str_value(cmd, config_ARG, ""))) {
1069 ret = EINVALID_CMD_LINE;
1073 if (arg_count(cmd, config_ARG) || !cmd->config_valid || config_files_changed(cmd)) {
1074 /* Reinitialise various settings inc. logging, filters */
1075 if (!refresh_toolcontext(cmd)) {
1076 if (cmd->cft_override) {
1077 destroy_config_tree(cmd->cft_override);
1078 cmd->cft_override = NULL;
1080 log_error("Updated config file invalid. Aborting.");
1085 if ((ret = _get_settings(cmd)))
1087 _apply_settings(cmd);
1089 log_debug("Processing: %s", cmd->cmd_line);
1091 #ifdef O_DIRECT_SUPPORT
1092 log_debug("O_DIRECT will be used");
1095 if (!_set_udev_checking(cmd))
1098 if ((ret = _process_common_commands(cmd)))
1101 if (cmd->metadata_read_only &&
1102 !(cmd->command->flags & PERMITTED_READ_ONLY)) {
1103 log_error("%s: Command not permitted while global/metadata_read_only "
1104 "is set.", cmd->cmd_line);
1108 if (arg_count(cmd, nolocking_ARG))
1113 if (!init_locking(locking_type, cmd, arg_count(cmd, sysinit_ARG))) {
1118 ret = cmd->command->fn(cmd, argc, argv);
1124 log_verbose("Test mode: Wiping internal cache");
1125 lvmcache_destroy(cmd, 1);
1128 if (cmd->cft_override) {
1129 destroy_config_tree(cmd->cft_override);
1130 cmd->cft_override = NULL;
1132 if (!refresh_toolcontext(cmd))
1136 /* FIXME Move this? */
1137 cmd->current_settings = cmd->default_settings;
1138 _apply_settings(cmd);
1140 if (ret == EINVALID_CMD_LINE && !_cmdline.interactive)
1141 _short_usage(cmd->command->name);
1143 log_debug("Completed: %s", cmd->cmd_line);
1146 * free off any memory the command used.
1148 dm_list_init(&cmd->arg_value_groups);
1149 dm_pool_empty(cmd->mem);
1152 reset_log_duplicated();
1157 int lvm_return_code(int ret)
1159 return (ret == ECMD_PROCESSED ? 0 : ret);
1162 int lvm_split(char *str, int *argc, char **argv, int max)
1168 while (*b && isspace(*b))
1171 if ((!*b) || (*b == '#'))
1175 while (*e && !isspace(*e))
1178 argv[(*argc)++] = b;
1190 static const char *_get_cmdline(pid_t pid)
1192 static char _proc_cmdline[32];
1196 snprintf(buf, sizeof(buf), DEFAULT_PROC_DIR "/%u/cmdline", pid);
1197 /* FIXME Use generic read code. */
1198 if ((fd = open(buf, O_RDONLY)) > 0) {
1199 if ((n = read(fd, _proc_cmdline, sizeof(_proc_cmdline) - 1)) < 0) {
1200 log_sys_error("read", buf);
1204 log_sys_error("close", buf);
1206 _proc_cmdline[n] = '\0';
1208 return _proc_cmdline;
1211 static const char *_get_filename(int fd)
1213 static char filename[PATH_MAX];
1214 char buf[32]; /* Assumes short DEFAULT_PROC_DIR */
1217 snprintf(buf, sizeof(buf), DEFAULT_PROC_DIR "/self/fd/%u", fd);
1219 if ((size = readlink(buf, filename, sizeof(filename) - 1)) == -1)
1222 filename[size] = '\0';
1227 static void _close_descriptor(int fd, unsigned suppress_warnings,
1228 const char *command, pid_t ppid,
1229 const char *parent_cmdline)
1232 const char *filename;
1234 /* Ignore bad file descriptors */
1235 if (fcntl(fd, F_GETFD) == -1 && errno == EBADF)
1238 if (!suppress_warnings)
1239 filename = _get_filename(fd);
1242 if (suppress_warnings)
1246 fprintf(stderr, "File descriptor %d (%s) leaked on "
1247 "%s invocation.", fd, filename, command);
1248 else if (errno == EBADF)
1251 fprintf(stderr, "Close failed on stray file descriptor "
1252 "%d (%s): %s", fd, filename, strerror(errno));
1254 fprintf(stderr, " Parent PID %" PRIpid_t ": %s\n", ppid, parent_cmdline);
1257 static void _close_stray_fds(const char *command)
1261 unsigned suppress_warnings = 0;
1262 pid_t ppid = getppid();
1263 const char *parent_cmdline = _get_cmdline(ppid);
1265 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
1266 fprintf(stderr, "getrlimit(RLIMIT_NOFILE) failed: %s\n",
1271 if (getenv("LVM_SUPPRESS_FD_WARNINGS"))
1272 suppress_warnings = 1;
1274 for (fd = 3; fd < rlim.rlim_cur; fd++)
1275 _close_descriptor(fd, suppress_warnings, command, ppid,
1279 struct cmd_context *init_lvm(void)
1281 struct cmd_context *cmd;
1283 if (!(cmd = create_toolcontext(0, NULL)))
1286 _cmdline.arg_props = &_arg_props[0];
1288 if (stored_errno()) {
1289 destroy_toolcontext(cmd);
1296 static void _fin_commands(void)
1300 for (i = 0; i < _cmdline.num_commands; i++)
1301 dm_free(_cmdline.commands[i].valid_args);
1303 dm_free(_cmdline.commands);
1305 _cmdline.commands = NULL;
1306 _cmdline.num_commands = 0;
1307 _cmdline.commands_size = 0;
1310 void lvm_fin(struct cmd_context *cmd)
1313 destroy_toolcontext(cmd);
1316 static int _run_script(struct cmd_context *cmd, int argc, char **argv)
1320 char buffer[CMD_LEN];
1322 int magic_number = 0;
1323 char *script_file = argv[0];
1325 if ((script = fopen(script_file, "r")) == NULL)
1326 return ENO_SUCH_CMD;
1328 while (fgets(buffer, sizeof(buffer), script) != NULL) {
1329 if (!magic_number) {
1330 if (buffer[0] == '#' && buffer[1] == '!')
1337 if ((strlen(buffer) == sizeof(buffer) - 1)
1338 && (buffer[sizeof(buffer) - 1] - 2 != '\n')) {
1340 log_error("Line too long (max 255) beginning: %s",
1342 ret = EINVALID_CMD_LINE;
1345 if (lvm_split(buffer, &argc, argv, MAX_ARGS) == MAX_ARGS) {
1347 log_error("Too many arguments: %s", buffer);
1348 ret = EINVALID_CMD_LINE;
1353 if (!strcmp(argv[0], "quit") || !strcmp(argv[0], "exit"))
1355 ret = lvm_run_command(cmd, argc, argv);
1356 if (ret != ECMD_PROCESSED) {
1357 if (!error_message_produced()) {
1358 log_debug(INTERNAL_ERROR "Failed command did not use log_error");
1359 log_error("Command failed with status code %d.", ret);
1366 log_sys_error("fclose", script_file);
1372 * Determine whether we should fall back and exec the equivalent LVM1 tool
1374 static int _lvm1_fallback(struct cmd_context *cmd)
1379 if (!find_config_tree_int(cmd, "global/fallback_to_lvm1",
1380 DEFAULT_FALLBACK_TO_LVM1) ||
1381 strncmp(cmd->kernel_vsn, "2.4.", 4))
1385 dm_present = driver_version(vsn, sizeof(vsn));
1388 if (dm_present || !lvm1_present(cmd))
1394 static void _exec_lvm1_command(char **argv)
1396 char path[PATH_MAX];
1398 if (dm_snprintf(path, sizeof(path), "%s.lvm1", argv[0]) < 0) {
1399 log_error("Failed to create LVM1 tool pathname");
1404 log_sys_error("execvp", path);
1407 static void _nonroot_warning(void)
1409 if (getuid() || geteuid())
1410 log_warn("WARNING: Running as a non-root user. Functionality may be unavailable.");
1413 int lvm2_main(int argc, char **argv)
1417 struct cmd_context *cmd;
1419 base = last_path_component(argv[0]);
1420 if (strcmp(base, "lvm") && strcmp(base, "lvm.static") &&
1421 strcmp(base, "initrd-lvm"))
1424 _close_stray_fds(base);
1426 if (is_static() && strcmp(base, "lvm.static") &&
1427 path_exists(LVM_SHARED_PATH) &&
1428 !getenv("LVM_DID_EXEC")) {
1429 setenv("LVM_DID_EXEC", base, 1);
1430 execvp(LVM_SHARED_PATH, argv);
1431 unsetenv("LVM_DID_EXEC");
1434 /* "version" command is simple enough so it doesn't need any complex init */
1435 if (!alias && argc > 1 && !strcmp(argv[1], "version"))
1436 return lvm_return_code(version(NULL, argc, argv));
1438 if (!(cmd = init_lvm()))
1442 lvm_register_commands();
1444 if (_lvm1_fallback(cmd)) {
1445 /* Attempt to run equivalent LVM1 tool instead */
1451 log_error("Falling back to LVM1 tools, but no "
1452 "command specified.");
1456 _exec_lvm1_command(argv);
1460 #ifdef READLINE_SUPPORT
1461 if (!alias && argc == 1) {
1463 ret = lvm_shell(cmd, &_cmdline);
1470 log_fatal("Please supply an LVM command.");
1472 ret = EINVALID_CMD_LINE;
1481 ret = lvm_run_command(cmd, argc, argv);
1482 if ((ret == ENO_SUCH_CMD) && (!alias))
1483 ret = _run_script(cmd, argc, argv);
1484 if (ret == ENO_SUCH_CMD)
1485 log_error("No such command. Try 'help'.");
1487 if ((ret != ECMD_PROCESSED) && !error_message_produced()) {
1488 log_debug(INTERNAL_ERROR "Failed command did not use log_error");
1489 log_error("Command failed with status code %d.", ret);
1494 return lvm_return_code(ret);