mirror of
git://sourceware.org/git/lvm2.git
synced 2024-12-22 17:35:59 +03:00
d1a149b51d
. 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.
137 lines
6.1 KiB
C
137 lines
6.1 KiB
C
|
|
/*
|
|
* Define value types which describe values accepted
|
|
* by the --option's in args.h, and can also describe
|
|
* the values accepted as positional args.
|
|
*
|
|
* Previously, accepted values were only "described"
|
|
* by identifying the parsing function to use.
|
|
*
|
|
* Some standard val types are used by many options,
|
|
* e.g. many options (aa_ARG, bb_ARG, cc_ARG) all
|
|
* accept a number_VAL.
|
|
*
|
|
* Other special val types are used by only one option,
|
|
* e.g. only mirrorlog_ARG accepts a mirrorlog_VAL.
|
|
* This typically means that there are some specific
|
|
* words that are recognized after the option.
|
|
*
|
|
* Some options currently take a standard val type,
|
|
* (esp string_VAL), but they could be given their
|
|
* own custom val type. The advantage of using a
|
|
* custom val type is the possibility of validating
|
|
* the value when parsing it with a custom parsing
|
|
* function, and the possibility of displaying the
|
|
* actual accepted values in the command usage.
|
|
* Without a custom val type, the code must do ad hoc
|
|
* validation of the string values, and the usage
|
|
* output for the option will only say "String"
|
|
* rather than giving the accepted string values.
|
|
* Even without a custom parsing function, there is
|
|
* reason to define a custom x_VAL enum so that a
|
|
* more descriptive usage string can be specified
|
|
* as opposed to just "String".
|
|
*
|
|
* Most of the val types defined here are used after
|
|
* --option's, and are referenced in foo_ARG entries
|
|
* in args.h. But, some val types are only used to
|
|
* represent positional values in command definitions,
|
|
* e.g. vg_VAL.
|
|
*
|
|
* val(a, b, c, d)
|
|
*
|
|
* a: foo_VAL enums
|
|
* b: the function to parse and set the value
|
|
* c: the name used to reference this value in command defs
|
|
* d: what to display in usage output for this value
|
|
*
|
|
* command defintions will use --option NAME, where NAME
|
|
* is shown in val() field c. NAME will be translated to
|
|
* foo_VAL enum in field a, which is used in commands[]
|
|
* structs.
|
|
*
|
|
* option definitions (arg.h) will reference foo_VAL enum
|
|
* in field a.
|
|
*
|
|
* FIXME: for specialized val types, the set of recognized
|
|
* words is not defined or stored in a consistent way,
|
|
* but is just whatever the parsing function happens to look
|
|
* for, so adding a new accepted value for the val type is
|
|
* often just making the parsing function recognize a new
|
|
* word. This new word should then also be added to the
|
|
* usage string for the val type here. It would be nice
|
|
* if the accepted values could be defined in a more
|
|
* consistent way, perhaps in struct val_props.
|
|
*
|
|
* The usage text for an option is not always the full
|
|
* set of words accepted for an option, but may be a
|
|
* subset. i.e. an outdated word that no longer does
|
|
* anything may not be shown, but may still be recognized
|
|
* and ignored, or an option that shouldn't be used in
|
|
* general isn't shown to avoid suggesting it.
|
|
* e.g. for --activate we show the most common "y|n|ay"
|
|
* without showing the lvmlockd variations "ey|sy" which
|
|
* are not applicable in general.
|
|
*
|
|
* FIXME: are there some specialized or irrelevant
|
|
* options included in the usage text below that should
|
|
* be removed? Should "lvm1" be removed?
|
|
*
|
|
* For Number args that take optional units, a full usage
|
|
* could be "Number[bBsSkKmMgGtTpPeE]" (with implied |),
|
|
* but repeating this full specification produces cluttered
|
|
* output, and doesn't indicate which unit is the default.
|
|
* "Number[units]" would be cleaner, as would a subset of
|
|
* common units, e.g. "Number[kmg...]", but neither helps
|
|
* with default. "Number[k|unit]" and "Number[m|unit]" show
|
|
* the default, and "unit" indicates that other units
|
|
* are possible without listing them all. This also
|
|
* suggests using the preferred lower case letters, because
|
|
* --size and other option args treat upper/lower letters
|
|
* the same, all as 1024 SI base. For this reason, we
|
|
* should avoid suggesting the upper case letters.
|
|
*/
|
|
|
|
val(none_VAL, NULL, "None", "") /* unused, for enum value 0 */
|
|
val(conststr_VAL, NULL, "ConstString", "") /* used only for command defs */
|
|
val(constnum_VAL, NULL, "ConstNumber", "") /* used only for command defs */
|
|
val(bool_VAL, yes_no_arg, "Bool", "y|n")
|
|
val(number_VAL, int_arg, "Number", NULL)
|
|
val(string_VAL, string_arg, "String", NULL)
|
|
val(vg_VAL, string_arg, "VG", NULL)
|
|
val(lv_VAL, string_arg, "LV", NULL)
|
|
val(pv_VAL, string_arg, "PV", NULL)
|
|
val(tag_VAL, tag_arg, "Tag", NULL)
|
|
val(select_VAL, NULL, "Select", NULL) /* used only for command defs */
|
|
val(activationmode_VAL, string_arg, "ActivationMode", "partial|degraded|complete")
|
|
val(activation_VAL, activation_arg, "Active", "y|n|ay")
|
|
val(cachemode_VAL, cachemode_arg, "CacheMode", "writethrough|writeback")
|
|
val(discards_VAL, discards_arg, "Discards", "passdown|nopassdown|ignore")
|
|
val(mirrorlog_VAL, mirrorlog_arg, "MirrorLog", "core|disk")
|
|
val(sizekb_VAL, size_kb_arg, "SizeKB", "Number[k|unit]")
|
|
val(sizemb_VAL, size_mb_arg, "SizeMB", "Number[m|unit]")
|
|
val(numsigned_VAL, int_arg_with_sign, "SNumber", "[+|-]Number")
|
|
val(numsignedper_VAL, int_arg_with_sign_and_percent, "SNumberP", "[+|-]Number[%{VG|PVS|FREE}]")
|
|
val(permission_VAL, permission_arg, "Permission", "rw|r")
|
|
val(metadatatype_VAL, metadatatype_arg, "MetadataType", "lvm2|lvm1")
|
|
val(units_VAL, string_arg, "Units", "hHbBsSkKmMgGtTpPeE")
|
|
val(segtype_VAL, segtype_arg, "SegType", "linear|striped|snapshot|mirror|raid*|thin|cache|thin-pool|cache-pool")
|
|
/* FIXME: cling_by_tags is left out of help text because it makes the line wrap */
|
|
val(alloc_VAL, alloc_arg, "Alloc", "contiguous|cling|normal|anywhere|inherit")
|
|
val(locktype_VAL, locktype_arg, "LockType", "sanlock|dlm|none")
|
|
val(readahead_VAL, readahead_arg, "Readahead", "auto|none|NumberSectors")
|
|
val(metadatacopies_VAL, metadatacopies_arg, "MetadataCopies", "all|unmanaged|Number")
|
|
|
|
/* this should always be last */
|
|
val(VAL_COUNT, NULL, NULL, NULL)
|
|
|
|
/*
|
|
* I suspect many of the following are good candidates for a custom VAL enum
|
|
* for the benefit of custom parsing, or custom usage, or both:
|
|
*
|
|
* configreport_ARG, configtype_ARG, polloperation_ARG, raidrebuild_ARG,
|
|
* raidsyncaction_ARG, raidwritemostly_ARG, reportformat_ARG, syncaction_ARG,
|
|
* cachepolicy_ARG, cachesettings_ARG, writemostly_ARG
|
|
*/
|
|
|