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
46 * Table of valid switches
48 static struct arg_props _arg_props
[ARG_COUNT
+ 1] = {
49 #define arg(a, b, c, d, e) {b, "", "--" c, d, e},
54 static struct cmdline_context _cmdline
;
56 /* Command line args */
57 unsigned arg_count(const struct cmd_context
*cmd
, int a
)
59 return cmd
->arg_values
[a
].count
;
62 unsigned grouped_arg_count(const struct arg_values
*av
, int a
)
67 unsigned arg_is_set(const struct cmd_context
*cmd
, int a
)
69 return arg_count(cmd
, a
) ? 1 : 0;
72 unsigned grouped_arg_is_set(const struct arg_values
*av
, int a
)
74 return grouped_arg_count(av
, a
) ? 1 : 0;
77 const char *arg_value(struct cmd_context
*cmd
, int a
)
79 return cmd
->arg_values
[a
].value
;
82 const char *arg_str_value(struct cmd_context
*cmd
, int a
, const char *def
)
84 return arg_count(cmd
, a
) ? cmd
->arg_values
[a
].value
: def
;
87 const char *grouped_arg_str_value(const struct arg_values
*av
, int a
, const char *def
)
89 return grouped_arg_count(av
, a
) ? av
[a
].value
: def
;
92 int32_t grouped_arg_int_value(const struct arg_values
*av
, int a
, const int32_t def
)
94 return grouped_arg_count(av
, a
) ? av
[a
].i_value
: def
;
97 int32_t first_grouped_arg_int_value(struct cmd_context
*cmd
, int a
, const int32_t def
)
99 struct arg_value_group_list
*current_group
;
100 struct arg_values
*av
;
102 dm_list_iterate_items(current_group
, &cmd
->arg_value_groups
) {
103 av
= current_group
->arg_values
;
104 if (grouped_arg_count(av
, a
))
105 return grouped_arg_int_value(av
, a
, def
);
111 int32_t arg_int_value(struct cmd_context
*cmd
, int a
, const int32_t def
)
113 return (_cmdline
.arg_props
[a
].flags
& ARG_GROUPABLE
) ?
114 first_grouped_arg_int_value(cmd
, a
, def
) : (arg_count(cmd
, a
) ? cmd
->arg_values
[a
].i_value
: def
);
117 uint32_t arg_uint_value(struct cmd_context
*cmd
, int a
, const uint32_t def
)
119 return arg_count(cmd
, a
) ? cmd
->arg_values
[a
].ui_value
: def
;
122 int64_t arg_int64_value(struct cmd_context
*cmd
, int a
, const int64_t def
)
124 return arg_count(cmd
, a
) ? cmd
->arg_values
[a
].i64_value
: def
;
127 uint64_t arg_uint64_value(struct cmd_context
*cmd
, int a
, const uint64_t def
)
129 return arg_count(cmd
, a
) ? cmd
->arg_values
[a
].ui64_value
: def
;
133 const void *arg_ptr_value(struct cmd_context *cmd, int a, const void *def)
135 return arg_count(cmd, a) ? cmd->arg_values[a].ptr : def;
139 sign_t
arg_sign_value(struct cmd_context
*cmd
, int a
, const sign_t def
)
141 return arg_count(cmd
, a
) ? cmd
->arg_values
[a
].sign
: def
;
144 percent_type_t
arg_percent_value(struct cmd_context
*cmd
, int a
, const percent_type_t def
)
146 return arg_count(cmd
, a
) ? cmd
->arg_values
[a
].percent
: def
;
149 int arg_count_increment(struct cmd_context
*cmd
, int a
)
151 return cmd
->arg_values
[a
].count
++;
154 int yes_no_arg(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
)
156 av
->sign
= SIGN_NONE
;
157 av
->percent
= PERCENT_NONE
;
159 if (!strcmp(av
->value
, "y")) {
164 else if (!strcmp(av
->value
, "n")) {
175 int yes_no_excl_arg(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
)
177 av
->sign
= SIGN_NONE
;
178 av
->percent
= PERCENT_NONE
;
180 if (!strcmp(av
->value
, "e") || !strcmp(av
->value
, "ey") ||
181 !strcmp(av
->value
, "ye")) {
182 av
->i_value
= CHANGE_AE
;
183 av
->ui_value
= CHANGE_AE
;
186 else if (!strcmp(av
->value
, "y")) {
187 av
->i_value
= CHANGE_AY
;
188 av
->ui_value
= CHANGE_AY
;
191 else if (!strcmp(av
->value
, "n") || !strcmp(av
->value
, "en") ||
192 !strcmp(av
->value
, "ne")) {
193 av
->i_value
= CHANGE_AN
;
194 av
->ui_value
= CHANGE_AN
;
197 else if (!strcmp(av
->value
, "ln") || !strcmp(av
->value
, "nl")) {
198 av
->i_value
= CHANGE_ALN
;
199 av
->ui_value
= CHANGE_ALN
;
202 else if (!strcmp(av
->value
, "ly") || !strcmp(av
->value
, "yl")) {
203 av
->i_value
= CHANGE_ALY
;
204 av
->ui_value
= CHANGE_ALY
;
213 int metadatatype_arg(struct cmd_context
*cmd
, struct arg_values
*av
)
215 return get_format_by_name(cmd
, av
->value
) ? 1 : 0;
218 static int _get_int_arg(struct arg_values
*av
, char **ptr
)
223 av
->percent
= PERCENT_NONE
;
228 av
->sign
= SIGN_PLUS
;
232 av
->sign
= SIGN_MINUS
;
236 av
->sign
= SIGN_NONE
;
242 v
= strtol(val
, ptr
, 10);
247 av
->i_value
= (int32_t) v
;
248 av
->ui_value
= (uint32_t) v
;
249 av
->i64_value
= (int64_t) v
;
250 av
->ui64_value
= (uint64_t) v
;
255 /* Size stored in sectors */
256 static int _size_arg(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
, int factor
)
260 static const char *suffixes
= "kmgtpebs";
263 uint64_t v_tmp
, adjustment
;
265 av
->percent
= PERCENT_NONE
;
270 av
->sign
= SIGN_PLUS
;
274 av
->sign
= SIGN_MINUS
;
278 av
->sign
= SIGN_NONE
;
284 v
= strtod(val
, &ptr
);
290 for (i
= strlen(suffixes
) - 1; i
>= 0; i
--)
291 if (suffixes
[i
] == tolower((int) *ptr
))
297 /* v is already in sectors */
301 v_tmp
= (uint64_t) v
;
302 adjustment
= v_tmp
% 512;
304 v_tmp
+= (512 - adjustment
);
305 log_error("Size is not a multiple of 512. "
306 "Try using %"PRIu64
" or %"PRIu64
".",
312 /* all other units: kmgtpe */
320 av
->i_value
= (int32_t) v
;
321 av
->ui_value
= (uint32_t) v
;
322 av
->i64_value
= (int64_t) v
;
323 av
->ui64_value
= (uint64_t) v
;
328 int size_kb_arg(struct cmd_context
*cmd
, struct arg_values
*av
)
330 return _size_arg(cmd
, av
, 2);
333 int size_mb_arg(struct cmd_context
*cmd
, struct arg_values
*av
)
335 return _size_arg(cmd
, av
, 2048);
338 int int_arg(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
)
342 if (!_get_int_arg(av
, &ptr
) || (*ptr
) || (av
->sign
== SIGN_MINUS
))
348 int int_arg_with_sign(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
)
352 if (!_get_int_arg(av
, &ptr
) || (*ptr
))
358 int int_arg_with_sign_and_percent(struct cmd_context
*cmd
__attribute__((unused
)),
359 struct arg_values
*av
)
363 if (!_get_int_arg(av
, &ptr
))
372 if (!strcasecmp(ptr
, "V") || !strcasecmp(ptr
, "VG"))
373 av
->percent
= PERCENT_VG
;
374 else if (!strcasecmp(ptr
, "L") || !strcasecmp(ptr
, "LV"))
375 av
->percent
= PERCENT_LV
;
376 else if (!strcasecmp(ptr
, "P") || !strcasecmp(ptr
, "PV") ||
377 !strcasecmp(ptr
, "PVS"))
378 av
->percent
= PERCENT_PVS
;
379 else if (!strcasecmp(ptr
, "F") || !strcasecmp(ptr
, "FR") ||
380 !strcasecmp(ptr
, "FREE"))
381 av
->percent
= PERCENT_FREE
;
382 else if (!strcasecmp(ptr
, "O") || !strcasecmp(ptr
, "OR") ||
383 !strcasecmp(ptr
, "ORIGIN"))
384 av
->percent
= PERCENT_ORIGIN
;
391 int minor_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("Minor number outside range 0-255");
406 int major_arg(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
)
410 if (!_get_int_arg(av
, &ptr
) || (*ptr
) || (av
->sign
== SIGN_MINUS
))
413 if (av
->i_value
> 255) {
414 log_error("Major number outside range 0-255");
418 /* FIXME Also Check against /proc/devices */
423 int string_arg(struct cmd_context
*cmd
__attribute__((unused
)),
424 struct arg_values
*av
__attribute__((unused
)))
429 int tag_arg(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
)
431 char *pos
= av
->value
;
436 if (!validate_tag(pos
))
444 int permission_arg(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
)
446 av
->sign
= SIGN_NONE
;
448 if ((!strcmp(av
->value
, "rw")) || (!strcmp(av
->value
, "wr")))
449 av
->ui_value
= LVM_READ
| LVM_WRITE
;
451 else if (!strcmp(av
->value
, "r"))
452 av
->ui_value
= LVM_READ
;
460 int alloc_arg(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
)
462 alloc_policy_t alloc
;
464 av
->sign
= SIGN_NONE
;
466 alloc
= get_alloc_from_string(av
->value
);
467 if (alloc
== ALLOC_INVALID
)
470 av
->ui_value
= (uint32_t) alloc
;
475 int segtype_arg(struct cmd_context
*cmd
, struct arg_values
*av
)
477 return get_segtype_from_string(cmd
, av
->value
) ? 1 : 0;
481 * Positive integer, zero or "auto".
483 int readahead_arg(struct cmd_context
*cmd
__attribute__((unused
)), struct arg_values
*av
)
485 if (!strcasecmp(av
->value
, "auto")) {
486 av
->ui_value
= DM_READ_AHEAD_AUTO
;
490 if (!strcasecmp(av
->value
, "none")) {
491 av
->ui_value
= DM_READ_AHEAD_NONE
;
495 if (!_size_arg(cmd
, av
, 1))
498 if (av
->sign
== SIGN_MINUS
)
505 * Non-zero, positive integer, "all", or "unmanaged"
507 int metadatacopies_arg(struct cmd_context
*cmd
, struct arg_values
*av
)
509 if (!strncmp(cmd
->command
->name
, "vg", 2)) {
510 if (!strcasecmp(av
->value
, "all")) {
511 av
->ui_value
= VGMETADATACOPIES_ALL
;
515 if (!strcasecmp(av
->value
, "unmanaged")) {
516 av
->ui_value
= VGMETADATACOPIES_UNMANAGED
;
521 return int_arg(cmd
, av
);
524 static void __alloc(int size
)
526 if (!(_cmdline
.commands
= dm_realloc(_cmdline
.commands
, sizeof(*_cmdline
.commands
) * size
))) {
527 log_fatal("Couldn't allocate memory.");
531 _cmdline
.commands_size
= size
;
534 static void _alloc_command(void)
536 if (!_cmdline
.commands_size
)
539 if (_cmdline
.commands_size
<= _cmdline
.num_commands
)
540 __alloc(2 * _cmdline
.commands_size
);
543 static void _create_new_command(const char *name
, command_fn command
,
545 const char *desc
, const char *usagestr
,
546 int nargs
, int *args
)
552 nc
= _cmdline
.commands
+ _cmdline
.num_commands
++;
556 nc
->usage
= usagestr
;
559 nc
->num_args
= nargs
;
560 nc
->valid_args
= args
;
563 static void _register_command(const char *name
, command_fn fn
, const char *desc
,
564 unsigned flags
, const char *usagestr
, ...)
570 /* count how many arguments we have */
571 va_start(ap
, usagestr
);
572 while (va_arg(ap
, int) >= 0)
576 /* allocate space for them */
577 if (!(args
= dm_malloc(sizeof(*args
) * nargs
))) {
578 log_fatal("Out of memory.");
583 va_start(ap
, usagestr
);
584 for (i
= 0; i
< nargs
; i
++)
585 args
[i
] = va_arg(ap
, int);
588 /* enter the command in the register */
589 _create_new_command(name
, fn
, flags
, desc
, usagestr
, nargs
, args
);
592 void lvm_register_commands(void)
594 #define xx(a, b, c, d...) _register_command(# a, a, b, c, ## d, \
596 debug_ARG, help_ARG, help2_ARG, \
597 version_ARG, verbose_ARG, \
598 quiet_ARG, config_ARG, -1);
599 #include "commands.h"
603 static struct command
*_find_command(const char *name
)
608 base
= last_path_component(name
);
610 for (i
= 0; i
< _cmdline
.num_commands
; i
++) {
611 if (!strcmp(base
, _cmdline
.commands
[i
].name
))
615 if (i
>= _cmdline
.num_commands
)
618 return _cmdline
.commands
+ i
;
621 static void _short_usage(const char *name
)
623 log_error("Run `%s --help' for more information.", name
);
626 static int _usage(const char *name
)
628 struct command
*com
= _find_command(name
);
631 log_print("%s: no such command.", name
);
635 log_print("%s: %s\n\n%s", com
->name
, com
->desc
, com
->usage
);
640 * Sets up the short and long argument. If there
641 * is no short argument then the index of the
642 * argument in the the_args array is set as the
643 * long opt value. Yuck. Of course this means we
644 * can't have more than 'a' long arguments.
646 static void _add_getopt_arg(int arg
, char **ptr
, struct option
**o
)
648 struct arg_props
*a
= _cmdline
.arg_props
+ arg
;
651 *(*ptr
)++ = a
->short_arg
;
656 #ifdef HAVE_GETOPTLONG
657 if (*(a
->long_arg
+ 2)) {
658 (*o
)->name
= a
->long_arg
+ 2;
659 (*o
)->has_arg
= a
->fn
? 1 : 0;
662 (*o
)->val
= a
->short_arg
;
664 (*o
)->val
= arg
+ 128;
670 static int _find_arg(struct command
*com
, int opt
)
675 for (i
= 0; i
< com
->num_args
; i
++) {
676 arg
= com
->valid_args
[i
];
677 a
= _cmdline
.arg_props
+ arg
;
680 * opt should equal either the
681 * short arg, or the index into
684 if ((a
->short_arg
&& (opt
== a
->short_arg
)) ||
685 (!a
->short_arg
&& (opt
== (arg
+ 128))))
692 static int _process_command_line(struct cmd_context
*cmd
, int *argc
,
696 char str
[((ARG_COUNT
+ 1) * 2) + 1], *ptr
= str
;
697 struct option opts
[ARG_COUNT
+ 1], *o
= opts
;
699 struct arg_values
*av
;
700 struct arg_value_group_list
*current_group
= NULL
;
702 if (!(cmd
->arg_values
= dm_pool_zalloc(cmd
->mem
, sizeof(*cmd
->arg_values
) * ARG_COUNT
))) {
703 log_fatal("Unable to allocate memory for command line arguments.");
707 /* fill in the short and long opts */
708 for (i
= 0; i
< cmd
->command
->num_args
; i
++)
709 _add_getopt_arg(cmd
->command
->valid_args
[i
], &ptr
, &o
);
712 memset(o
, 0, sizeof(*o
));
714 /* initialise getopt_long & scan for command line switches */
716 optind
= OPTIND_INIT
;
717 while ((opt
= GETOPTLONG_FN(*argc
, *argv
, str
, opts
, NULL
)) >= 0) {
722 if ((arg
= _find_arg(cmd
->command
, opt
)) < 0) {
723 log_fatal("Unrecognised option.");
727 a
= _cmdline
.arg_props
+ arg
;
729 av
= &cmd
->arg_values
[arg
];
731 if (a
->flags
& ARG_GROUPABLE
) {
732 /* Start a new group of arguments the first time or if a non-countable argument is repeated. */
733 if (!current_group
|| (current_group
->arg_values
[arg
].count
&& !(a
->flags
& ARG_COUNTABLE
))) {
734 /* FIXME Reduce size including only groupable args */
735 if (!(current_group
= dm_pool_zalloc(cmd
->mem
, sizeof(struct arg_value_group_list
) + sizeof(*cmd
->arg_values
) * ARG_COUNT
))) {
736 log_fatal("Unable to allocate memory for command line arguments.");
740 dm_list_add(&cmd
->arg_value_groups
, ¤t_group
->list
);
742 /* Maintain total argument count as well as count within each group */
744 av
= ¤t_group
->arg_values
[arg
];
747 if (av
->count
&& !(a
->flags
& ARG_COUNTABLE
)) {
748 log_error("Option%s%c%s%s may not be repeated.",
749 a
->short_arg
? " -" : "",
750 a
->short_arg
? : ' ',
751 (a
->short_arg
&& a
->long_arg
) ?
752 "/" : "", a
->long_arg
? : "");
758 log_error("Option requires argument.");
764 if (!a
->fn(cmd
, av
)) {
765 log_error("Invalid argument for %s: %s", a
->long_arg
, optarg
);
778 static int _merge_synonym(struct cmd_context
*cmd
, int oldarg
, int newarg
)
780 const struct arg_values
*old
;
781 struct arg_values
*new;
783 if (arg_count(cmd
, oldarg
) && arg_count(cmd
, newarg
)) {
784 log_error("%s and %s are synonyms. Please only supply one.",
785 _cmdline
.arg_props
[oldarg
].long_arg
, _cmdline
.arg_props
[newarg
].long_arg
);
789 if (!arg_count(cmd
, oldarg
))
792 old
= cmd
->arg_values
+ oldarg
;
793 new = cmd
->arg_values
+ newarg
;
795 new->count
= old
->count
;
796 new->value
= old
->value
;
797 new->i_value
= old
->i_value
;
798 new->ui_value
= old
->ui_value
;
799 new->i64_value
= old
->i64_value
;
800 new->ui64_value
= old
->ui64_value
;
801 new->sign
= old
->sign
;
806 int version(struct cmd_context
*cmd
__attribute__((unused
)),
807 int argc
__attribute__((unused
)),
808 char **argv
__attribute__((unused
)))
812 log_print("LVM version: %s", LVM_VERSION
);
813 if (library_version(vsn
, sizeof(vsn
)))
814 log_print("Library version: %s", vsn
);
815 if (driver_version(vsn
, sizeof(vsn
)))
816 log_print("Driver version: %s", vsn
);
818 return ECMD_PROCESSED
;
821 static int _get_settings(struct cmd_context
*cmd
)
823 cmd
->current_settings
= cmd
->default_settings
;
825 if (arg_count(cmd
, debug_ARG
))
826 cmd
->current_settings
.debug
= _LOG_FATAL
+
827 (arg_count(cmd
, debug_ARG
) - 1);
829 if (arg_count(cmd
, verbose_ARG
))
830 cmd
->current_settings
.verbose
= arg_count(cmd
, verbose_ARG
);
832 if (arg_count(cmd
, quiet_ARG
)) {
833 cmd
->current_settings
.debug
= 0;
834 cmd
->current_settings
.verbose
= 0;
837 if (arg_count(cmd
, test_ARG
))
838 cmd
->current_settings
.test
= arg_count(cmd
, test_ARG
);
840 if (arg_count(cmd
, driverloaded_ARG
)) {
841 cmd
->current_settings
.activation
=
842 arg_int_value(cmd
, driverloaded_ARG
,
843 cmd
->default_settings
.activation
);
846 cmd
->current_settings
.archive
= arg_int_value(cmd
, autobackup_ARG
, cmd
->current_settings
.archive
);
847 cmd
->current_settings
.backup
= arg_int_value(cmd
, autobackup_ARG
, cmd
->current_settings
.backup
);
848 cmd
->current_settings
.cache_vgmetadata
= cmd
->command
->flags
& CACHE_VGMETADATA
? 1 : 0;
849 cmd
->partial_activation
= 0;
851 if (arg_count(cmd
, partial_ARG
)) {
852 cmd
->partial_activation
= 1;
853 log_print("Partial mode. Incomplete logical volumes will be processed.");
856 if (arg_count(cmd
, ignorelockingfailure_ARG
) || arg_count(cmd
, sysinit_ARG
))
857 init_ignorelockingfailure(1);
859 init_ignorelockingfailure(0);
861 if (arg_count(cmd
, nosuffix_ARG
))
862 cmd
->current_settings
.suffix
= 0;
864 if (arg_count(cmd
, units_ARG
))
865 if (!(cmd
->current_settings
.unit_factor
=
866 units_to_bytes(arg_str_value(cmd
, units_ARG
, ""),
867 &cmd
->current_settings
.unit_type
))) {
868 log_error("Invalid units specification");
869 return EINVALID_CMD_LINE
;
872 if (arg_count(cmd
, trustcache_ARG
)) {
873 if (arg_count(cmd
, all_ARG
)) {
874 log_error("--trustcache is incompatible with --all");
875 return EINVALID_CMD_LINE
;
878 log_warn("WARNING: Cache file of PVs will be trusted. "
879 "New devices holding PVs may get ignored.");
883 if (arg_count(cmd
, noudevsync_ARG
)) {
884 cmd
->current_settings
.udev_sync
= 0;
885 cmd
->current_settings
.udev_fallback
= 1;
888 /* Handle synonyms */
889 if (!_merge_synonym(cmd
, resizable_ARG
, resizeable_ARG
) ||
890 !_merge_synonym(cmd
, allocation_ARG
, allocatable_ARG
) ||
891 !_merge_synonym(cmd
, allocation_ARG
, resizeable_ARG
) ||
892 !_merge_synonym(cmd
, virtualoriginsize_ARG
, virtualsize_ARG
))
893 return EINVALID_CMD_LINE
;
895 if ((!strncmp(cmd
->command
->name
, "pv", 2) &&
896 !_merge_synonym(cmd
, metadatacopies_ARG
, pvmetadatacopies_ARG
)) ||
897 (!strncmp(cmd
->command
->name
, "vg", 2) &&
898 !_merge_synonym(cmd
, metadatacopies_ARG
, vgmetadatacopies_ARG
)))
899 return EINVALID_CMD_LINE
;
901 /* Zero indicates success */
905 static int _process_common_commands(struct cmd_context
*cmd
)
907 if (arg_count(cmd
, help_ARG
) || arg_count(cmd
, help2_ARG
)) {
908 _usage(cmd
->command
->name
);
909 return ECMD_PROCESSED
;
912 if (arg_count(cmd
, version_ARG
)) {
913 return version(cmd
, 0, (char **) NULL
);
916 /* Zero indicates it's OK to continue processing this command */
920 static void _display_help(void)
924 log_error("Available lvm commands:");
925 log_error("Use 'lvm help <command>' for more information");
928 for (i
= 0; i
< _cmdline
.num_commands
; i
++) {
929 struct command
*com
= _cmdline
.commands
+ i
;
931 log_error("%-16.16s%s", com
->name
, com
->desc
);
935 int help(struct cmd_context
*cmd
__attribute__((unused
)), int argc
, char **argv
)
937 int ret
= ECMD_PROCESSED
;
943 for (i
= 0; i
< argc
; i
++)
944 if (!_usage(argv
[i
]))
945 ret
= EINVALID_CMD_LINE
;
951 static void _apply_settings(struct cmd_context
*cmd
)
953 init_debug(cmd
->current_settings
.debug
);
954 init_verbose(cmd
->current_settings
.verbose
+ VERBOSE_BASE_LEVEL
);
955 init_test(cmd
->current_settings
.test
);
956 init_full_scan_done(0);
957 init_mirror_in_sync(0);
958 init_dmeventd_monitor(DEFAULT_DMEVENTD_MONITOR
);
960 init_msg_prefix(cmd
->default_settings
.msg_prefix
);
961 init_cmd_name(cmd
->default_settings
.cmd_name
);
963 archive_enable(cmd
, cmd
->current_settings
.archive
);
964 backup_enable(cmd
, cmd
->current_settings
.backup
);
966 set_activation(cmd
->current_settings
.activation
);
968 cmd
->fmt
= get_format_by_name(cmd
, arg_str_value(cmd
, metadatatype_ARG
,
969 cmd
->current_settings
.fmt_name
));
971 cmd
->handles_missing_pvs
= 0;
974 static const char *_copy_command_line(struct cmd_context
*cmd
, int argc
, char **argv
)
979 * Build up the complete command line, used as a
980 * description for backups.
982 if (!dm_pool_begin_object(cmd
->mem
, 128))
985 for (i
= 0; i
< argc
; i
++) {
986 space
= strchr(argv
[i
], ' ') ? 1 : 0;
988 if (space
&& !dm_pool_grow_object(cmd
->mem
, "'", 1))
991 if (!dm_pool_grow_object(cmd
->mem
, argv
[i
], strlen(argv
[i
])))
994 if (space
&& !dm_pool_grow_object(cmd
->mem
, "'", 1))
998 if (!dm_pool_grow_object(cmd
->mem
, " ", 1))
1005 if (!dm_pool_grow_object(cmd
->mem
, "\0", 1))
1008 return dm_pool_end_object(cmd
->mem
);
1011 log_error("Couldn't copy command line.");
1012 dm_pool_abandon_object(cmd
->mem
);
1016 int lvm_run_command(struct cmd_context
*cmd
, int argc
, char **argv
)
1021 struct dm_config_tree
*old_cft
;
1023 init_error_message_produced(0);
1025 /* each command should start out with sigint flag cleared */
1028 if (!(cmd
->cmd_line
= _copy_command_line(cmd
, argc
, argv
))) {
1033 log_debug("Parsing: %s", cmd
->cmd_line
);
1035 if (!(cmd
->command
= _find_command(argv
[0])))
1036 return ENO_SUCH_CMD
;
1038 if (!_process_command_line(cmd
, &argc
, &argv
)) {
1039 log_error("Error during parsing of command line.");
1040 return EINVALID_CMD_LINE
;
1043 set_cmd_name(cmd
->command
->name
);
1045 if (arg_count(cmd
, config_ARG
))
1046 if (override_config_tree_from_string(cmd
, arg_str_value(cmd
, config_ARG
, ""))) {
1047 ret
= EINVALID_CMD_LINE
;
1051 if (arg_count(cmd
, config_ARG
) || !cmd
->config_valid
|| config_files_changed(cmd
)) {
1052 /* Reinitialise various settings inc. logging, filters */
1053 if (!refresh_toolcontext(cmd
)) {
1054 old_cft
= remove_overridden_config_tree(cmd
);
1056 dm_config_destroy(old_cft
);
1057 log_error("Updated config file invalid. Aborting.");
1062 if ((ret
= _get_settings(cmd
)))
1064 _apply_settings(cmd
);
1066 if (!get_activation_monitoring_mode(cmd
, &monitoring
))
1068 init_dmeventd_monitor(monitoring
);
1070 log_debug("Processing: %s", cmd
->cmd_line
);
1072 #ifdef O_DIRECT_SUPPORT
1073 log_debug("O_DIRECT will be used");
1076 if ((ret
= _process_common_commands(cmd
))) {
1077 if (ret
!= ECMD_PROCESSED
)
1082 if (cmd
->metadata_read_only
&&
1083 !(cmd
->command
->flags
& PERMITTED_READ_ONLY
)) {
1084 log_error("%s: Command not permitted while global/metadata_read_only "
1085 "is set.", cmd
->cmd_line
);
1089 if (arg_count(cmd
, nolocking_ARG
))
1094 if (!init_locking(locking_type
, cmd
, arg_count(cmd
, sysinit_ARG
))) {
1099 ret
= cmd
->command
->fn(cmd
, argc
, argv
);
1105 log_verbose("Test mode: Wiping internal cache");
1106 lvmcache_destroy(cmd
, 1);
1109 if ((old_cft
= remove_overridden_config_tree(cmd
))) {
1110 dm_config_destroy(old_cft
);
1112 if (!refresh_toolcontext(cmd
))
1116 /* FIXME Move this? */
1117 cmd
->current_settings
= cmd
->default_settings
;
1118 _apply_settings(cmd
);
1120 if (ret
== EINVALID_CMD_LINE
&& !_cmdline
.interactive
)
1121 _short_usage(cmd
->command
->name
);
1123 log_debug("Completed: %s", cmd
->cmd_line
);
1126 * free off any memory the command used.
1128 dm_list_init(&cmd
->arg_value_groups
);
1129 dm_pool_empty(cmd
->mem
);
1132 reset_log_duplicated();
1137 int lvm_return_code(int ret
)
1139 return (ret
== ECMD_PROCESSED
? 0 : ret
);
1142 int lvm_split(char *str
, int *argc
, char **argv
, int max
)
1148 while (*b
&& isspace(*b
))
1151 if ((!*b
) || (*b
== '#'))
1155 while (*e
&& !isspace(*e
))
1158 argv
[(*argc
)++] = b
;
1170 static const char *_get_cmdline(pid_t pid
)
1172 static char _proc_cmdline
[32];
1176 snprintf(buf
, sizeof(buf
), DEFAULT_PROC_DIR
"/%u/cmdline", pid
);
1177 /* FIXME Use generic read code. */
1178 if ((fd
= open(buf
, O_RDONLY
)) >= 0) {
1179 if ((n
= read(fd
, _proc_cmdline
, sizeof(_proc_cmdline
) - 1)) < 0) {
1180 log_sys_error("read", buf
);
1184 log_sys_error("close", buf
);
1186 _proc_cmdline
[n
] = '\0';
1188 return _proc_cmdline
;
1191 static const char *_get_filename(int fd
)
1193 static char filename
[PATH_MAX
];
1194 char buf
[32]; /* Assumes short DEFAULT_PROC_DIR */
1197 snprintf(buf
, sizeof(buf
), DEFAULT_PROC_DIR
"/self/fd/%u", fd
);
1199 if ((size
= readlink(buf
, filename
, sizeof(filename
) - 1)) == -1)
1202 filename
[size
] = '\0';
1207 static void _close_descriptor(int fd
, unsigned suppress_warnings
,
1208 const char *command
, pid_t ppid
,
1209 const char *parent_cmdline
)
1212 const char *filename
;
1214 /* Ignore bad file descriptors */
1215 if (fcntl(fd
, F_GETFD
) == -1 && errno
== EBADF
)
1218 if (!suppress_warnings
)
1219 filename
= _get_filename(fd
);
1222 if (suppress_warnings
)
1226 fprintf(stderr
, "File descriptor %d (%s) leaked on "
1227 "%s invocation.", fd
, filename
, command
);
1228 else if (errno
== EBADF
)
1231 fprintf(stderr
, "Close failed on stray file descriptor "
1232 "%d (%s): %s", fd
, filename
, strerror(errno
));
1234 fprintf(stderr
, " Parent PID %" PRIpid_t
": %s\n", ppid
, parent_cmdline
);
1237 static void _close_stray_fds(const char *command
)
1241 unsigned suppress_warnings
= 0;
1242 pid_t ppid
= getppid();
1243 const char *parent_cmdline
= _get_cmdline(ppid
);
1245 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
1246 fprintf(stderr
, "getrlimit(RLIMIT_NOFILE) failed: %s\n",
1251 if (getenv("LVM_SUPPRESS_FD_WARNINGS"))
1252 suppress_warnings
= 1;
1254 for (fd
= 3; fd
< (int)rlim
.rlim_cur
; fd
++)
1255 _close_descriptor(fd
, suppress_warnings
, command
, ppid
,
1259 struct cmd_context
*init_lvm(void)
1261 struct cmd_context
*cmd
;
1263 if (!udev_init_library_context())
1266 if (!(cmd
= create_toolcontext(0, NULL
, 1, 0))) {
1267 udev_fin_library_context();
1271 _cmdline
.arg_props
= &_arg_props
[0];
1273 if (stored_errno()) {
1274 destroy_toolcontext(cmd
);
1275 udev_fin_library_context();
1282 static void _fin_commands(void)
1286 for (i
= 0; i
< _cmdline
.num_commands
; i
++)
1287 dm_free(_cmdline
.commands
[i
].valid_args
);
1289 dm_free(_cmdline
.commands
);
1291 _cmdline
.commands
= NULL
;
1292 _cmdline
.num_commands
= 0;
1293 _cmdline
.commands_size
= 0;
1296 void lvm_fin(struct cmd_context
*cmd
)
1299 destroy_toolcontext(cmd
);
1300 udev_fin_library_context();
1303 static int _run_script(struct cmd_context
*cmd
, int argc
, char **argv
)
1307 char buffer
[CMD_LEN
];
1309 int magic_number
= 0;
1310 char *script_file
= argv
[0];
1312 if ((script
= fopen(script_file
, "r")) == NULL
)
1313 return ENO_SUCH_CMD
;
1315 while (fgets(buffer
, sizeof(buffer
), script
) != NULL
) {
1316 if (!magic_number
) {
1317 if (buffer
[0] == '#' && buffer
[1] == '!')
1324 if ((strlen(buffer
) == sizeof(buffer
) - 1)
1325 && (buffer
[sizeof(buffer
) - 1] - 2 != '\n')) {
1327 log_error("Line too long (max 255) beginning: %s",
1329 ret
= EINVALID_CMD_LINE
;
1332 if (lvm_split(buffer
, &argc
, argv
, MAX_ARGS
) == MAX_ARGS
) {
1334 log_error("Too many arguments: %s", buffer
);
1335 ret
= EINVALID_CMD_LINE
;
1340 if (!strcmp(argv
[0], "quit") || !strcmp(argv
[0], "exit"))
1342 ret
= lvm_run_command(cmd
, argc
, argv
);
1343 if (ret
!= ECMD_PROCESSED
) {
1344 if (!error_message_produced()) {
1345 log_debug(INTERNAL_ERROR
"Failed command did not use log_error");
1346 log_error("Command failed with status code %d.", ret
);
1353 log_sys_error("fclose", script_file
);
1359 * Determine whether we should fall back and exec the equivalent LVM1 tool
1361 static int _lvm1_fallback(struct cmd_context
*cmd
)
1366 if (!find_config_tree_int(cmd
, "global/fallback_to_lvm1",
1367 DEFAULT_FALLBACK_TO_LVM1
) ||
1368 strncmp(cmd
->kernel_vsn
, "2.4.", 4))
1372 dm_present
= driver_version(vsn
, sizeof(vsn
));
1375 if (dm_present
|| !lvm1_present(cmd
))
1381 static void _exec_lvm1_command(char **argv
)
1383 char path
[PATH_MAX
];
1385 if (dm_snprintf(path
, sizeof(path
), "%s.lvm1", argv
[0]) < 0) {
1386 log_error("Failed to create LVM1 tool pathname");
1391 log_sys_error("execvp", path
);
1394 static void _nonroot_warning(void)
1396 if (getuid() || geteuid())
1397 log_warn("WARNING: Running as a non-root user. Functionality may be unavailable.");
1400 int lvm2_main(int argc
, char **argv
)
1404 struct cmd_context
*cmd
;
1406 base
= last_path_component(argv
[0]);
1407 if (strcmp(base
, "lvm") && strcmp(base
, "lvm.static") &&
1408 strcmp(base
, "initrd-lvm"))
1411 _close_stray_fds(base
);
1413 if (is_static() && strcmp(base
, "lvm.static") &&
1414 path_exists(LVM_SHARED_PATH
) &&
1415 !getenv("LVM_DID_EXEC")) {
1416 if (setenv("LVM_DID_EXEC", base
, 1))
1417 log_sys_error("setenv", "LVM_DID_EXEC");
1418 if (execvp(LVM_SHARED_PATH
, argv
) == -1)
1419 log_sys_error("execvp", "LVM_SHARED_PATH");
1420 if (unsetenv("LVM_DID_EXEC"))
1421 log_sys_error("unsetenv", "LVM_DID_EXEC");
1424 /* "version" command is simple enough so it doesn't need any complex init */
1425 if (!alias
&& argc
> 1 && !strcmp(argv
[1], "version"))
1426 return lvm_return_code(version(NULL
, argc
, argv
));
1428 if (!(cmd
= init_lvm()))
1432 lvm_register_commands();
1434 if (_lvm1_fallback(cmd
)) {
1435 /* Attempt to run equivalent LVM1 tool instead */
1441 log_error("Falling back to LVM1 tools, but no "
1442 "command specified.");
1446 _exec_lvm1_command(argv
);
1450 #ifdef READLINE_SUPPORT
1451 if (!alias
&& argc
== 1) {
1453 ret
= lvm_shell(cmd
, &_cmdline
);
1460 log_fatal("Please supply an LVM command.");
1462 ret
= EINVALID_CMD_LINE
;
1471 ret
= lvm_run_command(cmd
, argc
, argv
);
1472 if ((ret
== ENO_SUCH_CMD
) && (!alias
))
1473 ret
= _run_script(cmd
, argc
, argv
);
1474 if (ret
== ENO_SUCH_CMD
)
1475 log_error("No such command. Try 'help'.");
1477 if ((ret
!= ECMD_PROCESSED
) && !error_message_produced()) {
1478 log_debug(INTERNAL_ERROR
"Failed command did not use log_error");
1479 log_error("Command failed with status code %d.", ret
);
1484 return lvm_return_code(ret
);