mirror of
git://sourceware.org/git/lvm2.git
synced 2025-03-11 20:58:50 +03:00
. Define a prototype for every lvm command. . Verify every user command matches one. . Generate help text and man pages from them. The new file command-lines.in defines a prototype for every unique lvm command. A unique lvm command is a unique combination of: command name + required option args + required positional args. Each of these prototypes also includes the optional option args and optional positional args that the command will accept, a description, and a unique string ID for the definition. Any valid command will match one of the prototypes. Here's an example of the lvresize command definitions from command-lines.in, there are three unique lvresize commands: lvresize --size SizeMB LV OO: --alloc Alloc, --autobackup Bool, --force, --nofsck, --nosync, --noudevsync, --reportformat String, --resizefs, --stripes Number, --stripesize SizeKB, --test, --poolmetadatasize SizeMB OP: PV ... ID: lvresize_by_size DESC: Resize an LV by a specified size. lvresize LV PV ... OO: --alloc Alloc, --autobackup Bool, --force, --nofsck, --nosync, --noudevsync, --reportformat String, --resizefs, --stripes Number, --stripesize SizeKB, --test ID: lvresize_by_pv DESC: Resize an LV by a specified PV. lvresize --poolmetadatasize SizeMB LV_thinpool OO: --alloc Alloc, --autobackup Bool, --force, --nofsck, --nosync, --noudevsync, --reportformat String, --stripes Number, --stripesize SizeKB, --test OP: PV ... ID: lvresize_pool_metadata_by_size DESC: Resize the metadata SubLV of a pool LV. The three commands have separate definitions because they have different required parameters. Required parameters are specified on the first line of the definition. Optional options are listed after OO, and optional positional args are listed after OP. This data is used to generate corresponding command definition structures for lvm in command-lines.h. "usage" text is also generated, so it is always in sync with the definitions. Example of the corresponding generated structure in command-lines.h for the first lvresize prototype (these structures are never edited directly): commands[78].name = "lvresize"; commands[78].command_line_id = "lvresize_by_size"; commands[78].command_line_enum = lvresize_by_size_CMD; commands[78].fn = lvresize; commands[78].ro_count = 1; commands[78].rp_count = 1; commands[78].oo_count = 22; commands[78].op_count = 1; commands[78].desc = "DESC: Resize an LV by a specified size."; commands[78].usage = "lvresize --size Number[m|unit] LV" " [ --alloc contiguous|cling|normal|anywhere|inherit, --autobackup y|n, --nofsck, --nosync, --reportformat String, --resizefs, --stripes Number, --stripesize Number[k|unit], --poolmetadatasize Number[m|unit] ]" " [ PV ... ]"; commands[78].usage_common = " [ --commandprofile String, --config String, --debug, --driverloaded y|n, --help, --profile String, --quiet, --verbose, --version, --yes, --force, --test, --noudevsync ]"; commands[78].required_opt_args[0].opt = size_ARG; commands[78].required_opt_args[0].def.val_bits = val_enum_to_bit(sizemb_VAL); commands[78].required_pos_args[0].pos = 1; commands[78].required_pos_args[0].def.val_bits = val_enum_to_bit(lv_VAL); commands[78].optional_opt_args[0].opt = commandprofile_ARG; commands[78].optional_opt_args[0].def.val_bits = val_enum_to_bit(string_VAL); commands[78].optional_opt_args[1].opt = config_ARG; commands[78].optional_opt_args[1].def.val_bits = val_enum_to_bit(string_VAL); commands[78].optional_opt_args[2].opt = debug_ARG; commands[78].optional_opt_args[3].opt = driverloaded_ARG; commands[78].optional_opt_args[3].def.val_bits = val_enum_to_bit(bool_VAL); commands[78].optional_opt_args[4].opt = help_ARG; commands[78].optional_opt_args[5].opt = profile_ARG; commands[78].optional_opt_args[5].def.val_bits = val_enum_to_bit(string_VAL); commands[78].optional_opt_args[6].opt = quiet_ARG; commands[78].optional_opt_args[7].opt = verbose_ARG; commands[78].optional_opt_args[8].opt = version_ARG; commands[78].optional_opt_args[9].opt = yes_ARG; commands[78].optional_opt_args[10].opt = alloc_ARG; commands[78].optional_opt_args[10].def.val_bits = val_enum_to_bit(alloc_VAL); commands[78].optional_opt_args[11].opt = autobackup_ARG; commands[78].optional_opt_args[11].def.val_bits = val_enum_to_bit(bool_VAL); commands[78].optional_opt_args[12].opt = force_ARG; commands[78].optional_opt_args[13].opt = nofsck_ARG; commands[78].optional_opt_args[14].opt = nosync_ARG; commands[78].optional_opt_args[15].opt = noudevsync_ARG; commands[78].optional_opt_args[16].opt = reportformat_ARG; commands[78].optional_opt_args[16].def.val_bits = val_enum_to_bit(string_VAL); commands[78].optional_opt_args[17].opt = resizefs_ARG; commands[78].optional_opt_args[18].opt = stripes_ARG; commands[78].optional_opt_args[18].def.val_bits = val_enum_to_bit(number_VAL); commands[78].optional_opt_args[19].opt = stripesize_ARG; commands[78].optional_opt_args[19].def.val_bits = val_enum_to_bit(sizekb_VAL); commands[78].optional_opt_args[20].opt = test_ARG; commands[78].optional_opt_args[21].opt = poolmetadatasize_ARG; commands[78].optional_opt_args[21].def.val_bits = val_enum_to_bit(sizemb_VAL); commands[78].optional_pos_args[0].pos = 2; commands[78].optional_pos_args[0].def.val_bits = val_enum_to_bit(pv_VAL); commands[78].optional_pos_args[0].def.flags = ARG_DEF_FLAG_MAY_REPEAT; Every user-entered command is compared against the set of command structures, and matched with one. An error is reported if an entered command does not have the required parameters for any definition. The closest match is printed as a suggestion, and running lvresize --help will display the usage for each possible lvresize command, e.g.: $ lvresize --help lvresize - Resize a logical volume Resize an LV by a specified size. lvresize --size Number[m|unit] LV [ --alloc contiguous|cling|normal|anywhere|inherit, --autobackup y|n, --nofsck, --nosync, --reportformat String, --resizefs, --stripes Number, --stripesize Number[k|unit], --poolmetadatasize Number[m|unit] ] [ PV ... ] Resize an LV by a specified PV. lvresize LV PV ... [ --alloc contiguous|cling|normal|anywhere|inherit, --autobackup y|n, --nofsck, --nosync, --reportformat String, --resizefs, --stripes Number, --stripesize Number[k|unit] ] Resize the metadata SubLV of a pool LV. lvresize --poolmetadatasize Number[m|unit] LV_thinpool [ --alloc contiguous|cling|normal|anywhere|inherit, --autobackup y|n, --nofsck, --nosync, --reportformat String, --stripes Number, --stripesize Number[k|unit] ] [ PV ... ] Common options: [ --commandprofile String, --config String, --debug, --driverloaded y|n, --help, --profile String, --quiet, --verbose, --version, --yes, --force, --test, --noudevsync ] (Use --help --help for usage notes.) $ lvresize --poolmetadatasize 4 Failed to find a matching command definition. Closest command usage is: lvresize --poolmetadatasize Number[m|unit] LV_thinpool Man page prototypes are also generated from the same original command definitions, and are always in sync with the code and help text. Very early in command execution, a matching command definition is found. lvm then knows the operation being done, and that the provided args conform to the definition. This will allow lots of ad hoc checking/validation to be removed throughout the code. Each command definition can also be routed to a specific function to implement it. The function is associated with an enum value for the command definition (generated from the ID string.) These per-command-definition implementation functions have not yet been created, so all commands currently fall back to the existing implementation. Using per-command-definition functions will allow lots of code to be removed which tries to figure out what the command is meant to do. This is currently based on ad hoc and complicated option analysis. When using the new functions, what the command is doing is already known from the associated command definition. So, this first phase validates every user-entered command against the set of command prototypes, then calls the existing implementation. The second phase can associate an implementation function with each definition, and take further advantage of the known operation to avoid the complicated option analysis.
170 lines
4.7 KiB
C
170 lines
4.7 KiB
C
/*
|
|
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
|
|
* Copyright (C) 2004-2015 Red Hat, Inc. All rights reserved.
|
|
*
|
|
* This file is part of LVM2.
|
|
*
|
|
* This copyrighted material is made available to anyone wishing to use,
|
|
* modify, copy, or redistribute it subject to the terms and conditions
|
|
* of the GNU Lesser General Public License v.2.1.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
#ifndef _LVM_COMMAND_H
|
|
#define _LVM_COMMAND_H
|
|
|
|
struct cmd_context;
|
|
|
|
/* old per-command-name function */
|
|
typedef int (*command_fn) (struct cmd_context *cmd, int argc, char **argv);
|
|
|
|
/* new per-command-line-id functions */
|
|
typedef int (*command_line_fn) (struct cmd_context *cmd, int argc, char **argv);
|
|
|
|
struct command_function {
|
|
int command_line_enum;
|
|
command_line_fn fn;
|
|
};
|
|
|
|
struct command_name {
|
|
const char *name;
|
|
const char *desc; /* general command description from commands.h */
|
|
unsigned int flags;
|
|
|
|
/* union of {required,optional}_opt_args for all commands with this name */
|
|
int valid_args[ARG_COUNT];
|
|
int num_args;
|
|
};
|
|
|
|
/*
|
|
* Command defintion
|
|
*
|
|
* A command is defined in terms of a command name,
|
|
* required options (+args), optional options (+args),
|
|
* required positional args, optional positional args.
|
|
*
|
|
* A positional arg always has non-zero pos_arg.def.types.
|
|
* The first positional arg has pos_arg.pos of 1.
|
|
*/
|
|
|
|
/* arg_def flags */
|
|
#define ARG_DEF_FLAG_NEW 1 << 0
|
|
#define ARG_DEF_FLAG_MAY_REPEAT 1 << 1
|
|
|
|
/* arg_def lv_types */
|
|
enum {
|
|
ARG_DEF_LV_ANY = 0,
|
|
ARG_DEF_LV_LINEAR = 1 << 0,
|
|
ARG_DEF_LV_STRIPED = 1 << 1,
|
|
ARG_DEF_LV_SNAPSHOT = 1 << 2,
|
|
ARG_DEF_LV_MIRROR = 1 << 3,
|
|
ARG_DEF_LV_RAID = 1 << 4,
|
|
ARG_DEF_LV_RAID0 = 1 << 5,
|
|
ARG_DEF_LV_RAID1 = 1 << 6,
|
|
ARG_DEF_LV_RAID4 = 1 << 7,
|
|
ARG_DEF_LV_RAID5 = 1 << 8,
|
|
ARG_DEF_LV_RAID6 = 1 << 9,
|
|
ARG_DEF_LV_RAID10 = 1 << 10,
|
|
ARG_DEF_LV_THIN = 1 << 11,
|
|
ARG_DEF_LV_THINPOOL = 1 << 12,
|
|
ARG_DEF_LV_CACHE = 1 << 13,
|
|
ARG_DEF_LV_CACHEPOOL = 1 << 14,
|
|
ARG_DEF_LV_LAST = 1 << 15,
|
|
};
|
|
|
|
static inline int val_bit_is_set(uint64_t val_bits, int val_enum)
|
|
{
|
|
return (val_bits & (1 << val_enum)) ? 1 : 0;
|
|
}
|
|
|
|
static inline uint64_t val_enum_to_bit(int val_enum)
|
|
{
|
|
return 1 << val_enum;
|
|
}
|
|
|
|
/* Description a value that follows an option or exists in a position. */
|
|
|
|
struct arg_def {
|
|
uint64_t val_bits; /* bits of x_VAL, can be multiple for pos_arg */
|
|
uint64_t num; /* a literal number for conststr_VAL */
|
|
const char *str; /* a literal string for constnum_VAL */
|
|
uint32_t lv_types; /* ARG_DEF_LV_, for lv_VAL, can be multiple */
|
|
uint32_t flags; /* ARG_DEF_FLAG_ */
|
|
};
|
|
|
|
/* Description of an option and the value that follows it. */
|
|
|
|
struct opt_arg {
|
|
int opt; /* option, e.g. foo_ARG */
|
|
struct arg_def def; /* defines accepted values */
|
|
};
|
|
|
|
/* Description of a position and the value that exists there. */
|
|
|
|
struct pos_arg {
|
|
int pos; /* position, e.g. first is 1 */
|
|
struct arg_def def; /* defines accepted values */
|
|
};
|
|
|
|
/*
|
|
* CMD_RO_ARGS needs to accomodate a list of options,
|
|
* of which one is required after which the rest are
|
|
* optional.
|
|
*/
|
|
#define CMD_RO_ARGS 64 /* required opt args */
|
|
#define CMD_OO_ARGS 150 /* optional opt args */
|
|
#define CMD_RP_ARGS 8 /* required positional args */
|
|
#define CMD_OP_ARGS 8 /* optional positional args */
|
|
|
|
/*
|
|
* one or more from required_opt_args is required,
|
|
* then the rest are optional.
|
|
*/
|
|
#define CMD_FLAG_ONE_REQUIRED_OPT 1
|
|
|
|
/* a register of the lvm commands */
|
|
struct command {
|
|
const char *name;
|
|
const char *desc; /* specific command description from command-lines.h */
|
|
const char *usage; /* excludes common options like --help, --debug */
|
|
const char *usage_common; /* includes commmon options like --help, --debug */
|
|
const char *command_line_id;
|
|
int command_line_enum; /* <command_line_id>_CMD */
|
|
|
|
struct command_name *cname;
|
|
|
|
command_fn fn; /* old style */
|
|
struct command_function *functions; /* new style */
|
|
|
|
unsigned int flags; /* copied from command_name.flags from commands.h */
|
|
|
|
unsigned int cmd_flags; /* CMD_FLAG_ */
|
|
|
|
/* definitions of opt/pos args */
|
|
|
|
/* required args following an --opt */
|
|
struct opt_arg required_opt_args[CMD_RO_ARGS];
|
|
|
|
/* optional args following an --opt */
|
|
struct opt_arg optional_opt_args[CMD_OO_ARGS];
|
|
|
|
/* required positional args */
|
|
struct pos_arg required_pos_args[CMD_RP_ARGS];
|
|
|
|
/* optional positional args */
|
|
struct pos_arg optional_pos_args[CMD_OP_ARGS];
|
|
|
|
int ro_count;
|
|
int oo_count;
|
|
int rp_count;
|
|
int op_count;
|
|
|
|
/* used for processing current position */
|
|
int pos_count;
|
|
};
|
|
|
|
#endif
|