Skip to contentSkip to conversation

master - commands: new method for defining commands

PATCH David Teigland 1 participant 1 message Feb 13, 2017

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=1e2420bca85da9a37570871cd70192e9ae831786
Commit:        1e2420bca85da9a37570871cd70192e9ae831786
Parent:        19267fa6aa0709e3d0c962ad0904920f06102a5b
Author:        David Teigland <[email protected]>
AuthorDate:    Fri Aug 12 15:52:18 2016 -0500
Committer:     David Teigland <[email protected]>
CommitterDate: Mon Feb 13 08:20:10 2017 -0600

commands: new method for defining commands

. Define a prototype for every lvm command.
. Match every user command with one definition.
. 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, --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
ID: lvresize_by_pv
DESC: Resize an LV by specified PV extents.
FLAGS: SECONDARY_SYNTAX

lvresize --poolmetadatasize SizeMB LV_thinpool
OO: --alloc Alloc, --autobackup Bool, --force, --nofsck, --nosync, --noudevsync,
--reportformat String, --stripes Number, --stripesize SizeKB
OP: PV ...
ID: lvresize_pool_metadata_by_size
DESC: Resize a pool metadata SubLV by a specified size.

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/help output is also auto generated, so it is always in sync with the definitions.

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.

The prototype syntax used for help/man output includes required --option and positional args on the first line, and optional --option and positional args enclosed in [ ] on subsequent lines.

command_name <required_opt_args> <required_pos_args>

          [ <optional_opt_args> ]
          [ <optional_pos_args> ]

Command definitions that are not to be advertised/suggested have the flag SECONDARY_SYNTAX. These commands will not be printed in the normal help output.

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 per-command-name implementation functions.

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.

---
 lib/commands/toolcontext.h            |   10 +-
 lib/metadata/lv_manip.c               |    3 +-
 lib/metadata/metadata-exported.h      |    9 +-
 lib/metadata/thin_manip.c             |   13 +
 test/shell/lvconvert-thin-raid.sh     |    4 +-
 test/shell/lvconvert-thin.sh          |    4 +-
 test/shell/lvcreate-cache.sh          |    2 +-
 test/shell/vgextend-restoremissing.sh |    1 -
 tools/Makefile.in                     |   11 +-
 tools/args.h                          |  407 +++---
 tools/command-lines.in                | 1579 +++++++++++++++++
 tools/command.h                       |  207 +++
 tools/commands.h                      | 1428 +---------------
 tools/create-commands.c               | 2990 +++++++++++++++++++++++++++++++++
 tools/lv_props.h                      |   56 +
 tools/lv_types.h                      |   34 +
 tools/lvm.c                           |   32 +-
 tools/lvm2cmdline.h                   |    9 +-
 tools/lvmcmdlib.c                     |    4 +-
 tools/lvmcmdline.c                    | 1446 ++++++++++++++--
 tools/toollib.c                       |  641 +++++++-
 tools/toollib.h                       |    6 +
 tools/tools.h                         |   86 +-
 tools/vals.h                          |  136 ++
 tools/vgchange.c                      |    3 +
 tools/vgconvert.c                     |   14 +-
 tools/vgextend.c                      |    6 -
 27 files changed, 7305 insertions(+), 1836 deletions(-)
lib/commands/toolcontext.h +9 -1
index bb962dc..2dc1c28 100644
@@ -88,12 +88,20 @@ struct cmd_context {
 	 * Command line and arguments.
 	 */
 	const char *cmd_line;
+	const char *name; /* needed before cmd->command is set */
 	struct command *command;
 	char **argv;
-	struct arg_values *arg_values;
+	struct arg_values *opt_arg_values;
 	struct dm_list arg_value_groups;
 
 	/*
+	 * Position args remaining after command name
+	 * and --options are removed from original argc/argv.
+	 */
+	int position_argc;
+	char **position_argv;
+
+	/*
 	 * Format handlers.
 	 */
 	const struct format_type *fmt;		/* current format to use by default */
lib/metadata/lv_manip.c +1 -2
index 01ab96b..bc599de 100644
@@ -319,7 +319,6 @@ static int _lv_layout_and_role_thin(struct dm_pool *mem,
 {
 	int top_level = 0;
 	unsigned snap_count;
-	struct lv_segment *seg;
 
 	/* non-top-level LVs */
 	if (lv_is_thin_pool_metadata(lv)) {
@@ -353,7 +352,7 @@ static int _lv_layout_and_role_thin(struct dm_pool *mem,
 			    !str_list_add_no_dup_check(mem, role, _lv_type_names[LV_TYPE_MULTITHINORIGIN]))
 				goto_bad;
 		}
-		if ((seg = first_seg(lv)) && (seg->origin || seg->external_lv))
+		if (lv_is_thin_snapshot(lv))
 			if (!str_list_add(mem, role, _lv_type_names[LV_TYPE_SNAPSHOT]) ||
 			    !str_list_add_no_dup_check(mem, role, _lv_type_names[LV_TYPE_THINSNAPSHOT]))
 				goto_bad;
lib/metadata/metadata-exported.h +8 -1
index 5fe169c..420893c 100644
@@ -1067,9 +1067,16 @@ struct lv_segment *get_only_segment_using_this_lv(const struct logical_volume *l
 * Useful functions for managing snapshots.
 */
 int lv_is_origin(const struct logical_volume *lv);
+#define lv_is_thick_origin lv_is_origin
+
 int lv_is_thin_origin(const struct logical_volume *lv, unsigned *snap_count);
-int lv_is_cache_origin(const struct logical_volume *lv);
+int lv_is_thin_snapshot(const struct logical_volume *lv);
+
 int lv_is_cow(const struct logical_volume *lv);
+#define lv_is_thick_snapshot lv_is_cow
+
+int lv_is_cache_origin(const struct logical_volume *lv);
+
 int lv_is_merging_cow(const struct logical_volume *cow);
 uint32_t cow_max_extents(const struct logical_volume *origin, uint32_t chunk_size);
 int cow_has_min_chunks(const struct volume_group *vg, uint32_t cow_extents, uint32_t chunk_size);
lib/metadata/thin_manip.c +13 -0
index 42ec552..3e2a185 100644
@@ -752,6 +752,19 @@ int lv_is_thin_origin(const struct logical_volume *lv, unsigned int *snap_count)
 	return r;
 }
 
+int lv_is_thin_snapshot(const struct logical_volume *lv)
+{
+	struct lv_segment *seg;
+
+	if (!lv_is_thin_volume(lv))
+		return 0;
+
+	if ((seg = first_seg(lv)) && (seg->origin || seg->external_lv))
+		return 1;
+
+	return 0;
+}
+
 /*
  * Explict check of new thin pool for usability
  *
test/shell/lvconvert-thin-raid.sh +2 -2
index 7fa8733..33e5b6d 100644
@@ -30,8 +30,8 @@ aux wait_for_sync $vg $lv2
 lvchange -an $vg/$lv1
 
 # conversion fails for internal volumes
-invalid lvconvert --thinpool $vg/${lv1}_rimage_0
-invalid lvconvert --yes --thinpool $vg/$lv1 --poolmetadata $vg/${lv2}_rimage_0
+not lvconvert --thinpool $vg/${lv1}_rimage_0
+not lvconvert --yes --thinpool $vg/$lv1 --poolmetadata $vg/${lv2}_rimage_0
 
 lvconvert --yes --thinpool $vg/$lv1 --poolmetadata $vg/$lv2
 
test/shell/lvconvert-thin.sh +2 -2
index 4bd1acd..3878fcb 100644
@@ -58,13 +58,13 @@ lvchange -an $vg/$lv1
 # conversion fails for mirror segment type
 fail lvconvert --thinpool $vg/$lv1
 # cannot use same LV
-invalid lvconvert --yes --thinpool $vg/$lv2 --poolmetadata $vg/$lv2
+not lvconvert --yes --thinpool $vg/$lv2 --poolmetadata $vg/$lv2
 
 prepare_lvs
 
 # conversion fails for internal volumes
 # can't use --readahead with --poolmetadata
-invalid lvconvert --thinpool $vg/$lv1 --poolmetadata $vg/$lv2 --readahead 512
+not lvconvert --thinpool $vg/$lv1 --poolmetadata $vg/$lv2 --readahead 512
 lvconvert --yes --thinpool $vg/$lv1 --poolmetadata $vg/$lv2
 
 prepare_lvs
test/shell/lvcreate-cache.sh +1 -1
index c38549f..9222bea 100644
@@ -56,7 +56,7 @@ fail lvcreate -l 1 --cachepool pool8 $vg
 
 # no size specified
 invalid lvcreate --cachepool pool $vg 2>&1 | tee err
-grep "specify either size or extents" err
+# grep "specify either size or extents" err
 
 # Check nothing has been created yet
 check vg_field $vg lv_count 0
test/shell/vgextend-restoremissing.sh +0 -1
index 7346519..2a82115 100644
@@ -23,7 +23,6 @@ lvcreate -l 1 -n lv1 $vg "$dev1"
 invalid vgextend
 # --metadatacopies => use --pvmetadatacopies
 invalid vgextend --metadatacopies 3 $vg "$dev1" 2>&1 | tee out
-grep -- "use --pvmetadatacopies" out
 
 # VG name should exist
 fail vgextend --restoremissing $vg-invalid "$dev1"
tools/Makefile.in +10 -1
index d93c31b..887bc69 100644
@@ -76,6 +76,7 @@ SOURCES2 =\
 
 TARGETS =\
 	.commands \
+	command-lines.h \
 	liblvm2cmd.a \
 	lvm
 
@@ -99,7 +100,8 @@ LIB_VERSION = $(LIB_VERSION_LVM)
 CLEAN_TARGETS = liblvm2cmd.$(LIB_SUFFIX) $(TARGETS_DM) \
 	liblvm2cmd.$(LIB_SUFFIX).$(LIB_VERSION) lvm-static.o \
 	liblvm2cmd-static.a dmsetup.static lvm.static \
-	$(LDDEPS) .exported_symbols_generated
+	$(LDDEPS) .exported_symbols_generated \
+	ccmd command-lines.h command-lines-count.h
 
 ifeq ("@CMDLIB@", "yes")
 	TARGETS += liblvm2cmd.$(LIB_SUFFIX).$(LIB_VERSION)
@@ -171,6 +173,13 @@ liblvm2cmd.$(LIB_SUFFIX).$(LIB_VERSION): liblvm2cmd.$(LIB_SUFFIX)
 	$(CC) -E -P $(srcdir)/cmdnames.h 2> /dev/null | \
 		egrep -v '^ *(|#.*|config|devtypes|dumpconfig|formats|fullreport|help|lastlog|lvpoll|pvdata|segtypes|systemid|tags|version) *$$' > .commands
 
+ccmd: create-commands.c
+	$(CC) create-commands.c -o ccmd
+
+command-lines.h: ccmd
+	./ccmd --output struct command-lines.in > command-lines.h
+	./ccmd --output count command-lines.in > command-lines-count.h
+
 ifneq ("$(CFLOW_CMD)", "")
 CFLOW_SOURCES = $(addprefix $(srcdir)/, $(SOURCES))
 -include $(top_builddir)/libdm/libdevmapper.cflow
tools/args.h +204 -203

419 lines not on this page · raw message

tools/command-lines.in new file +1579 -0

1,579 lines not on this page · raw message

tools/command.h new file +207 -0

207 lines not on this page · raw message

tools/commands.h +57 -1371

654 lines not on this page · raw message

tools/create-commands.c new file +2990 -0

2,990 lines not on this page · raw message

tools/lv_props.h new file +56 -0

56 lines not on this page · raw message

tools/lv_types.h new file +34 -0
new file mode 100644
index 0000000..22d0522

Lines of this file are not shown. View the raw message.

tools/lvm.c +16 -16
index b3af020..aae6da0 100644

Lines of this file are not shown. View the raw message.

tools/lvm2cmdline.h +5 -4
index 80bd03e..9b75c36 100644

Lines of this file are not shown. View the raw message.

tools/lvmcmdlib.c +2 -2
index 64c0d8b..a1e938b 100644

Lines of this file are not shown. View the raw message.

tools/lvmcmdline.c +1267 -179

1,446 lines not on this page · raw message

tools/toollib.c +632 -9

641 lines not on this page · raw message

tools/toollib.h +6 -0
index 531f690..d44b825 100644

Lines of this file are not shown. View the raw message.

tools/tools.h +64 -22

86 lines not on this page · raw message

tools/vals.h new file +136 -0

136 lines not on this page · raw message

tools/vgchange.c +3 -0
index ca0f450..4cf8a3c 100644

Lines of this file are not shown. View the raw message.

tools/vgconvert.c +1 -13
index 2c0ac08..9f98048 100644

Lines of this file are not shown. View the raw message.

tools/vgextend.c +0 -6
index e16316f..35168e9 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.