summaryrefslogtreecommitdiff
path: root/tools/lvchange.c
diff options
context:
space:
mode:
authorDavid Teigland <teigland@redhat.com>2016-08-12 15:52:18 -0500
committerDavid Teigland <teigland@redhat.com>2016-10-13 16:59:53 -0500
commit761e4ef9ab90a1303602db8aabae6b86366c3e2f (patch)
tree6729385e69bedfb5f6c496027c29d3ef69d09694 /tools/lvchange.c
parent5c55c4ac18f60822da6bf61dde9dec3467a9e79a (diff)
downloadlvm2-dev-dct-cmd-defs20.tar.gz
commands: new method for defining commandsdev-dct-cmd-defs20
. 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.
Diffstat (limited to 'tools/lvchange.c')
-rw-r--r--tools/lvchange.c283
1 files changed, 283 insertions, 0 deletions
diff --git a/tools/lvchange.c b/tools/lvchange.c
index 250d7209b..462e0a71f 100644
--- a/tools/lvchange.c
+++ b/tools/lvchange.c
@@ -1436,3 +1436,286 @@ int lvchange(struct cmd_context *cmd, int argc, char **argv)
update ? READ_FOR_UPDATE : 0, NULL,
&_lvchange_single);
}
+
+#if 0
+/*
+ * Check if the status of the LV allows running lvchange.
+ *
+ * FIXME: check for invalid VG/LV properties in a way that is not prone
+ * to missing some. Currently, there are some checks here, some in the
+ * functions above, some in process_each, and some may be missing.
+ */
+static int _lvchange_status_is_valid(struct cmd_context *cmd, struct logical_volume *lv)
+{
+ if (!(lv->vg->status & LVM_WRITE)) {
+ log_error("Operation not permitted on LV %s: writable VG required.",
+ display_lvname(lv));
+ return 0;
+ }
+
+ if (lv_is_pvmove(lv)) {
+ log_error("Operation not permitted on LV %s: used for pvmove.",
+ display_lvname(lv));
+ if (arg_is_set(cmd, activate_ARG))
+ log_error("Use 'pvmove --abort' to abandon a pvmove");
+ return 0;
+ }
+
+ if (lv_is_mirror_log(lv)) {
+ log_error("Operation not permitted on LV %s: is mirror log.",
+ display_lvname(lv));
+ return 0;
+ }
+
+ if (lv_is_mirror_image(lv)) {
+ log_error("Operation not permitted on LV %s: is mirror image.",
+ display_lvname(lv));
+ return 0;
+ }
+
+ if (lv_is_origin(lv) && !lv_is_thin_volume(lv)) {
+ log_error("Operation not permitted on LV %s: is under snapshot.",
+ display_lvname(lv));
+ return 0;
+ }
+
+ return 1;
+}
+
+static int _lvchange_properties_single(struct cmd_context *cmd,
+ struct logical_volume *lv,
+ struct processing_handle *handle)
+{
+ int doit = 0, docmds = 0;
+
+ /* FIXME: sort out hidden/internal LVs, e.g. _lvchange_hidden_is_valid() */
+
+ if (!_lvchange_status_is_valid(cmd, lv))
+ return_ECMD_FAILED;
+
+ if (arg_is_set(cmd, persistent_ARG) && lv_is_pool(lv)) {
+ log_error("Operation not permitted on LV %s: persistent device numbers are not supported with pools.",
+ display_lvname(lv));
+ return ECMD_FAILED;
+ }
+
+ /*
+ * If a persistent lv lock already exists from activation
+ * (with the needed mode or higher), this will be a no-op.
+ * Otherwise, the lv lock will be taken as non-persistent
+ * and released when this command exits.
+ */
+ if (!lockd_lv(cmd, lv, "ex", 0)) {
+ stack;
+ return ECMD_FAILED;
+ }
+
+ for (i = 0; i < cmd->command->ro_count; i++) {
+ opt_enum = cmd->command->required_opt_args[i].opt;
+
+ if (!arg_is_set(cmd, opt_enum))
+ continue;
+
+ if (!archive(lv->vg))
+ return_ECMD_FAILED;
+
+ docmds++;
+
+ switch (opt_enum) {
+ case permission_ARG:
+ doit += _lvchange_permission(cmd, lv);
+ break;
+
+ case alloc_ARG:
+ case contiguous_ARG:
+ doit += _lvchange_alloc(cmd, lv);
+ break;
+
+ case errorwhenfull_ARG:
+ doit += _lvchange_errorwhenfull(cmd, lv);
+ break;
+
+ case readahead_ARG:
+ doit += _lvchange_readahead(cmd, lv);
+ break;
+
+ case persistent_ARG:
+ doit += _lvchange_persistent(cmd, lv);
+ break;
+
+ case discards_ARG:
+ case zero_ARG:
+ doit += _lvchange_pool_update(cmd, lv);
+ break;
+
+ case addtag_ARG:
+ case deltag_ARG:
+ doit += _lvchange_tag(cmd, lv, opt_enum);
+ break;
+
+ case writemostly_ARG:
+ case writebehind_ARG:
+ doit += _lvchange_writemostly(lv);
+ break;
+
+ case minrecoveryrate_ARG:
+ case maxrecoveryrate_ARG:
+ doit += _lvchange_recovery_rate(lv);
+ break;
+
+ case profile_ARG:
+ case metadataprofile_ARG:
+ case detachprofile_ARG:
+ doit += _lvchange_profile(lv);
+ break;
+
+ case setactivationskip_ARG:
+ doit += _lvchange_activation_skip(lv);
+ break;
+
+ case cachemode_ARG:
+ case cachepolicy_ARG:
+ case cachesettings_ARG:
+ doit += _lvchange_cache(cmd, lv);
+ break;
+
+ default:
+ log_error(INTERNAL_ERROR "Failed to check for option %s",
+ arg_long_option_name(i));
+ }
+
+ if (doit)
+ log_print_unless_silent("Logical volume %s changed.", display_lvname(lv));
+
+ if (doit != docmds)
+ return_ECMD_FAILED;
+
+ return ECMD_PROCESSED;
+}
+
+int lvchange_properties_cmd(struct cmd_context *cmd, int argc, char **argv)
+{
+ struct processing_handle *handle = init_processing_handle(cmd, NULL);
+ int ret;
+
+ ret = process_each_lv(cmd, argc, argv, NULL, NULL, READ_FOR_UPDATE, handle, _lvchange_properties_single);
+
+ destroy_processing_handle(cmd, handle);
+ return ret;
+}
+
+static int _lvchange_activate_single(struct cmd_context *cmd,
+ struct logical_volume *lv,
+ struct processing_handle *handle)
+{
+ struct logical_volume *origin;
+ char snaps_msg[128];
+
+ /* FIXME: sort out hidden/internal LVs, e.g. _lvchange_hidden_is_valid() */
+
+ if (!_lvchange_status_is_valid(cmd, lv))
+ return_ECMD_FAILED;
+
+ /* FIXME: untangle the proper logic for cow / sparse / virtual origin */
+
+ /* If LV is sparse, activate origin instead */
+ if (lv_is_cow(lv) && lv_is_virtual_origin(origin = origin_from_cow(lv)))
+ lv = origin;
+
+ if (lv_is_cow(lv)) {
+ origin = origin_from_cow(lv);
+ if (origin->origin_count < 2)
+ snaps_msg[0] = '\0';
+ else if (dm_snprintf(snaps_msg, sizeof(snaps_msg),
+ " and %u other snapshot(s)",
+ origin->origin_count - 1) < 0) {
+ log_error("Failed to prepare message.");
+ return ECMD_FAILED;
+ }
+
+ if (!arg_is_set(cmd, yes_ARG) &&
+ (yes_no_prompt("Change of snapshot %s will also change its "
+ "origin %s%s. Proceed? [y/n]: ",
+ display_lvname(lv), display_lvname(origin),
+ snaps_msg) == 'n')) {
+ log_error("Logical volume %s not changed.", display_lvname(lv));
+ return ECMD_FAILED;
+ }
+ }
+
+ /*
+ * If --sysinit -aay is used and at the same time lvmetad is used,
+ * we want to rely on autoactivation to take place. Also, we
+ * need to take special care here as lvmetad service does
+ * not neet to be running at this moment yet - it could be
+ * just too early during system initialization time.
+ */
+ if (arg_is_set(cmd, sysinit_ARG) && (arg_uint_value(cmd, activate_ARG, 0) == CHANGE_AAY)) {
+ if (lvmetad_used()) {
+ log_warn("WARNING: lvmetad is active, skipping direct activation during sysinit.");
+ return ECMD_PROCESSED;
+ }
+ }
+
+ if (!_lvchange_activate(cmd, lv))
+ return_ECMD_FAILED;
+
+ return ECMD_PROCESSED;
+}
+
+int lvchange_activate_cmd(struct cmd_context *cmd, int argc, char **argv)
+{
+ struct processing_handle *handle = init_processing_handle(cmd, NULL);
+
+ cmd->handles_missing_pvs = 1;
+ cmd->lockd_vg_default_sh = 1;
+
+ /*
+ * Include foreign VGs that contain active LVs.
+ * That shouldn't happen in general, but if it does by some
+ * mistake, then we want to allow those LVs to be deactivated.
+ */
+ cmd->include_active_foreign_vgs = 1;
+
+ /* Allow deactivating if locks fail. */
+ if (is_change_activating((activation_change_t)arg_uint_value(cmd, activate_ARG, CHANGE_AY)))
+ cmd->lockd_vg_enforce_sh = 1;
+
+ ret = process_each_lv(cmd, argc, argv, NULL, NULL, 0, handle, _lvchange_activate_single);
+
+ destroy_processing_handle(cmd, handle);
+ return ret;
+}
+
+static int _lvchange_refresh_single(struct cmd_context *cmd,
+ struct logical_volume *lv,
+ struct processing_handle *handle)
+{
+ /* FIXME: sort out hidden/internal LVs, e.g. _lvchange_hidden_is_valid() */
+
+ if (!_lvchange_status_is_valid(cmd, lv))
+ return_ECMD_FAILED;
+
+ log_verbose("Refreshing logical volume %s (if active).", display_lvname(lv));
+
+ if (!_lv_refresh(cmd, lv))
+ return_ECMD_FAILED;
+
+ return ECMD_PROCESSED;
+}
+
+int lvchange_refresh_cmd(struct cmd_context *cmd, int argc, char **argv)
+{
+ struct processing_handle *handle = init_processing_handle(cmd, NULL);
+ int ret;
+
+ cmd->handles_missing_pvs = 1;
+ cmd->lockd_vg_default_sh = 1;
+
+ ret = process_each_lv(cmd, argc, argv, NULL, NULL, 0, handle, _lvchange_refresh_single);
+
+ destroy_processing_handle(cmd, handle);
+ return ret;
+}
+#endif
+