Refactor lvm:filter logic

  * Apply filter, only if something has been passed
  * lvm support lvm.conf check - lets use it!
  * Changes in lvm.conf must be
    reflected in initramfs => add corresponding trigger

Prod-related: PROD-26396 (PROD:26396)

Change-Id: I7055f78db52773cbf7d4f11e255110ca9053f4be
diff --git a/linux/files/lvm.conf b/linux/files/lvm.conf
index e6b8a58..a72870e 100644
--- a/linux/files/lvm.conf
+++ b/linux/files/lvm.conf
@@ -79,14 +79,14 @@
         # routines to acquire this information. For example, this information
         # is used to drive LVM filtering like MD component detection, multipath
         # component detection, partition detection and others.
-        # 
+        #
         # Accepted values:
         #   none
         #     No external device information source is used.
         #   udev
         #     Reuse existing udev database records. Applicable only if LVM is
         #     compiled with udev support.
-        # 
+        #
         external_device_info_source = "none"
 
         # Configuration option devices/preferred_names.
@@ -103,10 +103,10 @@
         # Prefer the name with the least number of slashes.
         # Prefer a name that is a symlink.
         # Prefer the path with least value in lexicographical order.
-        # 
+        #
         # Example
         # preferred_names = [ "^/dev/mpath/", "^/dev/mapper/mpath", "^/dev/[hs]d" ]
-        # 
+        #
         # This configuration option does not have a default value defined.
 
         # Configuration option devices/filter.
@@ -125,12 +125,9 @@
         # as the combination might produce unexpected results (test changes.)
         # Run vgscan after changing the filter to regenerate the cache.
         # See the use_lvmetad comment for a special case regarding filters.
-        # 
+        #
         # Example
         # Accept every block device:
-
-        filter = [ {%- for vgname, vg in storage.lvm.items() %}{%- if vg.get('enabled', True) %}{%- for dev in vg.devices %}"a|{{ dev }}*|"{%- if not loop.last %},{%- endif %}{%- endfor %}{%- endif %}{%- endfor %}, "r|.*|" ]
-
         # filter = [ "a|.*/|" ]
         # Reject the cdrom drive:
         # filter = [ "r|/dev/cdrom|" ]
@@ -140,9 +137,21 @@
         # filter = [ "a|loop|", "r|/dev/hdc|", "a|/dev/ide|", "r|.*|" ]
         # Use anchors to be very specific:
         # filter = [ "a|^/dev/hda8$|", "r|.*/|" ]
-        # 
+        #
         # This configuration option has an automatic default value.
         # filter = [ "a|.*/|" ]
+        {% set filter_list=[] %}
+        {%- for vgname, vg in storage.lvm.items() %}
+          {%- if vg.get('enabled', True) %}
+            {%- for dev in vg.devices %}
+              {%- do filter_list.append("a|" + dev + "*|") %}
+            {%- endfor %}
+          {%- endif %}
+        {%- endfor %}
+        {%- if filter_list|length > 0 %}
+        {%- do filter_list.append('r|.*|') %}
+        filter = {{ filter_list }}
+        {%- endif %}
 
         # Configuration option devices/global_filter.
         # Limit the block devices that are used by LVM system components.
@@ -176,10 +185,10 @@
         # List of additional acceptable block device types.
         # These are of device type names from /proc/devices, followed by the
         # maximum number of partitions.
-        # 
+        #
         # Example
         # types = [ "fd", 16 ]
-        # 
+        #
         # This configuration option is advanced.
         # This configuration option does not have a default value defined.
 
@@ -317,7 +326,7 @@
         # defined here, it will check whether any of them are attached to the
         # PVs concerned and then seek to match those PV tags between existing
         # extents and new extents.
-        # 
+        #
         # Example
         # Use the special tag "@*" as a wildcard to match any PV tag:
         # cling_tag_list = [ "@*" ]
@@ -325,7 +334,7 @@
         # PVs are tagged with either @site1 or @site2 to indicate where
         # they are situated:
         # cling_tag_list = [ "@site1", "@site2" ]
-        # 
+        #
         # This configuration option does not have a default value defined.
 
         # Configuration option allocation/maximise_cling.
@@ -374,14 +383,14 @@
 
         # Configuration option allocation/cache_mode.
         # The default cache mode used for new cache.
-        # 
+        #
         # Accepted values:
         #   writethrough
         #     Data blocks are immediately written from the cache to disk.
         #   writeback
         #     Data blocks are written from the cache back to disk after some
         #     delay to improve performance.
-        # 
+        #
         # This setting replaces allocation/cache_pool_cachemode.
         # This configuration option has an automatic default value.
         # cache_mode = "writethrough"
@@ -423,18 +432,18 @@
 
         # Configuration option allocation/thin_pool_discards.
         # The discards behaviour of thin pool volumes.
-        # 
+        #
         # Accepted values:
         #   ignore
         #   nopassdown
         #   passdown
-        # 
+        #
         # This configuration option has an automatic default value.
         # thin_pool_discards = "passdown"
 
         # Configuration option allocation/thin_pool_chunk_size_policy.
         # The chunk size calculation policy for thin pool volumes.
-        # 
+        #
         # Accepted values:
         #   generic
         #     If thin_pool_chunk_size is defined, use it. Otherwise, calculate
@@ -446,7 +455,7 @@
         #     the chunk size for performance based on device hints exposed in
         #     sysfs - the optimal_io_size. The chunk size is always at least
         #     512KiB.
-        # 
+        #
         # This configuration option has an automatic default value.
         # thin_pool_chunk_size_policy = "generic"
 
@@ -629,11 +638,11 @@
         # Configuration option global/format.
         # The default metadata format that commands should use.
         # The -M 1|2 option overrides this setting.
-        # 
+        #
         # Accepted values:
         #   lvm1
         #   lvm2
-        # 
+        #
         # This configuration option has an automatic default value.
         # format = "lvm2"
 
@@ -657,7 +666,7 @@
 
         # Configuration option global/locking_type.
         # Type of locking to use.
-        # 
+        #
         # Accepted values:
         #   0
         #     Turns off locking. Warning: this risks metadata corruption if
@@ -683,7 +692,7 @@
         #     safely because it belongs to an inaccessible domain and might be
         #     in use, for example a virtual machine image or a disk that is
         #     shared by a clustered machine.
-        # 
+        #
         locking_type = 1
 
         # Configuration option global/wait_for_locks.
@@ -755,7 +764,7 @@
         # Configuration option global/mirror_segtype_default.
         # The segment type used by the short mirroring option -m.
         # The --type mirror|raid1 option overrides this setting.
-        # 
+        #
         # Accepted values:
         #   mirror
         #     The original RAID1 implementation from LVM/DM. It is
@@ -775,7 +784,7 @@
         #     handling a failure. This mirror implementation is not
         #     cluster-aware and cannot be used in a shared (active/active)
         #     fashion in a cluster.
-        # 
+        #
         mirror_segtype_default = "raid1"
 
         # Configuration option global/raid10_segtype_default.
@@ -784,7 +793,7 @@
         # The --stripes/-i and --mirrors/-m options can both be specified
         # during the creation of a logical volume to use both striping and
         # mirroring for the LV. There are two different implementations.
-        # 
+        #
         # Accepted values:
         #   raid10
         #     LVM uses MD's RAID10 personality through DM. This is the
@@ -794,7 +803,7 @@
         #     is done by creating a mirror LV on top of striped sub-LVs,
         #     effectively creating a RAID 0+1 array. The layering is suboptimal
         #     in terms of providing redundancy and performance.
-        # 
+        #
         raid10_segtype_default = "raid10"
 
         # Configuration option global/sparse_segtype_default.
@@ -802,7 +811,7 @@
         # The --type snapshot|thin option overrides this setting.
         # The combination of -V and -L options creates a sparse LV. There are
         # two different implementations.
-        # 
+        #
         # Accepted values:
         #   snapshot
         #     The original snapshot implementation from LVM/DM. It uses an old
@@ -814,7 +823,7 @@
         #     bigger minimal chunk size (64KiB) and uses a separate volume for
         #     metadata. It has better performance, especially when more data
         #     is used. It also supports full snapshots.
-        # 
+        #
         sparse_segtype_default = "thin"
 
         # Configuration option global/lvdisplay_shows_full_device_path.
@@ -921,20 +930,20 @@
         # causing problems. Features include: block_size, discards,
         # discards_non_power_2, external_origin, metadata_resize,
         # external_origin_extend, error_if_no_space.
-        # 
+        #
         # Example
         # thin_disabled_features = [ "discards", "block_size" ]
-        # 
+        #
         # This configuration option does not have a default value defined.
 
         # Configuration option global/cache_disabled_features.
         # Features to not use in the cache driver.
         # This can be helpful for testing, or to avoid using a feature that is
         # causing problems. Features include: policy_mq, policy_smq.
-        # 
+        #
         # Example
         # cache_disabled_features = [ "policy_smq" ]
-        # 
+        #
         # This configuration option does not have a default value defined.
 
         # Configuration option global/cache_check_executable.
@@ -981,7 +990,7 @@
         # or vgimport.) A VG on shared storage devices is accessible only to
         # the host with a matching system ID. See 'man lvmsystemid' for
         # information on limitations and correct usage.
-        # 
+        #
         # Accepted values:
         #   none
         #     The host has no system ID.
@@ -998,7 +1007,7 @@
         #   file
         #     Use the contents of another file (system_id_file) to set the
         #     system ID.
-        # 
+        #
         system_id_source = "none"
 
         # Configuration option global/system_id_file.
@@ -1101,7 +1110,7 @@
         # If this list is defined, an LV is only activated if it matches an
         # entry in this list. If this list is undefined, it imposes no limits
         # on LV activation (all are allowed).
-        # 
+        #
         # Accepted values:
         #   vgname
         #     The VG name is matched exactly and selects all LVs in the VG.
@@ -1115,10 +1124,10 @@
         #     or VG. See tags/hosttags. If any host tags exist but volume_list
         #     is not defined, a default single-entry list containing '@*' is
         #     assumed.
-        # 
+        #
         # Example
         # volume_list = [ "vg1", "vg2/lvol1", "@tag1", "@*" ]
-        # 
+        #
         # This configuration option does not have a default value defined.
 
         # Configuration option activation/auto_activation_volume_list.
@@ -1138,7 +1147,7 @@
         # commands run directly by a user. A user may also use the 'a' flag
         # directly to perform auto-activation. Also see pvscan(8) for more
         # information about auto-activation.
-        # 
+        #
         # Accepted values:
         #   vgname
         #     The VG name is matched exactly and selects all LVs in the VG.
@@ -1152,10 +1161,10 @@
         #     or VG. See tags/hosttags. If any host tags exist but volume_list
         #     is not defined, a default single-entry list containing '@*' is
         #     assumed.
-        # 
+        #
         # Example
         # volume_list = [ "vg1", "vg2/lvol1", "@tag1", "@*" ]
-        # 
+        #
         # This configuration option does not have a default value defined.
 
         # Configuration option activation/read_only_volume_list.
@@ -1164,7 +1173,7 @@
         # against this list, and if it matches, it is activated in read-only
         # mode. This overrides the permission setting stored in the metadata,
         # e.g. from --permission rw.
-        # 
+        #
         # Accepted values:
         #   vgname
         #     The VG name is matched exactly and selects all LVs in the VG.
@@ -1178,10 +1187,10 @@
         #     or VG. See tags/hosttags. If any host tags exist but volume_list
         #     is not defined, a default single-entry list containing '@*' is
         #     assumed.
-        # 
+        #
         # Example
         # volume_list = [ "vg1", "vg2/lvol1", "@tag1", "@*" ]
-        # 
+        #
         # This configuration option does not have a default value defined.
 
         # Configuration option activation/raid_region_size.
@@ -1203,13 +1212,13 @@
 
         # Configuration option activation/readahead.
         # Setting to use when there is no readahead setting in metadata.
-        # 
+        #
         # Accepted values:
         #   none
         #     Disable readahead.
         #   auto
         #     Use default value chosen by kernel.
-        # 
+        #
         readahead = "auto"
 
         # Configuration option activation/raid_fault_policy.
@@ -1220,7 +1229,7 @@
         # performed by dmeventd automatically, and the steps perfomed by the
         # manual command lvconvert --repair --use-policies.
         # Automatic handling requires dmeventd to be monitoring the LV.
-        # 
+        #
         # Accepted values:
         #   warn
         #     Use the system log to warn the user that a device in the RAID LV
@@ -1231,7 +1240,7 @@
         #   allocate
         #     Attempt to use any extra physical volumes in the VG as spares and
         #     replace faulty devices.
-        # 
+        #
         raid_fault_policy = "warn"
 
         # Configuration option activation/mirror_image_fault_policy.
@@ -1243,7 +1252,7 @@
         # determines the steps perfomed by dmeventd automatically, and the steps
         # performed by the manual command lvconvert --repair --use-policies.
         # Automatic handling requires dmeventd to be monitoring the LV.
-        # 
+        #
         # Accepted values:
         #   remove
         #     Simply remove the faulty device and run without it. If the log
@@ -1268,7 +1277,7 @@
         #     the redundant nature of the mirror. This policy acts like
         #     'remove' if no suitable device and space can be allocated for the
         #     replacement.
-        # 
+        #
         mirror_image_fault_policy = "remove"
 
         # Configuration option activation/mirror_log_fault_policy.
@@ -1283,26 +1292,26 @@
         # The minimum value is 50 (a smaller value is treated as 50.)
         # Also see snapshot_autoextend_percent.
         # Automatic extension requires dmeventd to be monitoring the LV.
-        # 
+        #
         # Example
         # Using 70% autoextend threshold and 20% autoextend size, when a 1G
         # snapshot exceeds 700M, it is extended to 1.2G, and when it exceeds
         # 840M, it is extended to 1.44G:
         # snapshot_autoextend_threshold = 70
-        # 
+        #
         snapshot_autoextend_threshold = 100
 
         # Configuration option activation/snapshot_autoextend_percent.
         # Auto-extending a snapshot adds this percent extra space.
         # The amount of additional space added to a snapshot is this
         # percent of its current size.
-        # 
+        #
         # Example
         # Using 70% autoextend threshold and 20% autoextend size, when a 1G
         # snapshot exceeds 700M, it is extended to 1.2G, and when it exceeds
         # 840M, it is extended to 1.44G:
         # snapshot_autoextend_percent = 20
-        # 
+        #
         snapshot_autoextend_percent = 20
 
         # Configuration option activation/thin_pool_autoextend_threshold.
@@ -1311,26 +1320,26 @@
         # The minimum value is 50 (a smaller value is treated as 50.)
         # Also see thin_pool_autoextend_percent.
         # Automatic extension requires dmeventd to be monitoring the LV.
-        # 
+        #
         # Example
         # Using 70% autoextend threshold and 20% autoextend size, when a 1G
         # thin pool exceeds 700M, it is extended to 1.2G, and when it exceeds
         # 840M, it is extended to 1.44G:
         # thin_pool_autoextend_threshold = 70
-        # 
+        #
         thin_pool_autoextend_threshold = 100
 
         # Configuration option activation/thin_pool_autoextend_percent.
         # Auto-extending a thin pool adds this percent extra space.
         # The amount of additional space added to a thin pool is this
         # percent of its current size.
-        # 
+        #
         # Example
         # Using 70% autoextend threshold and 20% autoextend size, when a 1G
         # thin pool exceeds 700M, it is extended to 1.2G, and when it exceeds
         # 840M, it is extended to 1.44G:
         # thin_pool_autoextend_percent = 20
-        # 
+        #
         thin_pool_autoextend_percent = 20
 
         # Configuration option activation/mlock_filter.
@@ -1344,10 +1353,10 @@
         # pages corresponding to lines that match are not pinned. On some
         # systems, locale-archive was found to make up over 80% of the memory
         # used by the process.
-        # 
+        #
         # Example
         # mlock_filter = [ "locale/locale-archive", "gconv/gconv-modules.cache" ]
-        # 
+        #
         # This configuration option is advanced.
         # This configuration option does not have a default value defined.
 
@@ -1386,7 +1395,7 @@
         # Configuration option activation/activation_mode.
         # How LVs with missing devices are activated.
         # The --activationmode option overrides this setting.
-        # 
+        #
         # Accepted values:
         #   complete
         #     Only allow activation of an LV if all of the Physical Volumes it
@@ -1401,7 +1410,7 @@
         #     could cause data loss with a portion of the LV inaccessible.
         #     This setting should not normally be used, but may sometimes
         #     assist with data recovery.
-        # 
+        #
         activation_mode = "degraded"
 
         # Configuration option activation/lock_start_list.
@@ -1422,7 +1431,7 @@
         # Configuration option metadata/pvmetadatacopies.
         # Number of copies of metadata to store on each PV.
         # The --pvmetadatacopies option overrides this setting.
-        # 
+        #
         # Accepted values:
         #   2
         #     Two copies of the VG metadata are stored on the PV, one at the
@@ -1432,7 +1441,7 @@
         #   0
         #     No copies of VG metadata are stored on the PV. This may be
         #     useful for VGs containing large numbers of PVs.
-        # 
+        #
         # This configuration option is advanced.
         # This configuration option has an automatic default value.
         # pvmetadatacopies = 1
@@ -1484,10 +1493,10 @@
         # the machine could lock up. Never edit any files in these directories
         # by hand unless you are absolutely sure you know what you are doing!
         # Use the supplied toolset to make changes (e.g. vgcfgrestore).
-        # 
+        #
         # Example
         # dirs = [ "/etc/lvm/metadata", "/mnt/disk2/lvm/metadata2" ]
-        # 
+        #
         # This configuration option is advanced.
         # This configuration option does not have a default value defined.
 # }
@@ -1578,7 +1587,7 @@
         # sequences are copied verbatim. Each special character sequence is
         # introduced by the '%' character and such sequence is then
         # substituted with a value as described below.
-        # 
+        #
         # Accepted values:
         #   %a
         #     The abbreviated name of the day of the week according to the
@@ -1701,7 +1710,7 @@
         #     The timezone name or abbreviation.
         #   %%
         #     A literal '%' character.
-        # 
+        #
         # This configuration option has an automatic default value.
         # time_format = "%Y-%m-%d %T %z"
 
@@ -1870,12 +1879,12 @@
         # applied to the local machine as a 'host tag'. If this subsection is
         # empty (has no host_list), then the subsection name is always applied
         # as a 'host tag'.
-        # 
+        #
         # Example
         # The host tag foo is given to all hosts, and the host tag
         # bar is given to the hosts named machine1 and machine2.
         # tags { foo { } bar { host_list = [ "machine1", "machine2" ] } }
-        # 
+        #
         # This configuration section has variable name.
         # This configuration section has an automatic default value.
         # tag {
diff --git a/linux/storage/lvm.sls b/linux/storage/lvm.sls
index 6d1797d..d53ad13 100644
--- a/linux/storage/lvm.sls
+++ b/linux/storage/lvm.sls
@@ -13,6 +13,18 @@
   - require:
     - pkg: linux_lvm_pkgs
 
+check_/etc/lvm/lvm.conf:
+  cmd.wait:
+  - name: lvm dumpconfig
+  - watch:
+    - file: /etc/lvm/lvm.conf
+
+update-initramfs_/etc/lvm/lvm.conf:
+  cmd.wait:
+  - name: update-initramfs -k all -u
+  - watch:
+    - file: /etc/lvm/lvm.conf
+
 lvm_services:
   service.running:
   - enable: true
@@ -24,9 +36,9 @@
 
 {%- for vgname, vg in storage.lvm.items() %}
 
-{%- if vg.get('enabled', True) %}
+  {%- if vg.get('enabled', True) %}
 
-{%- for dev in vg.devices %}
+  {%- for dev in vg.devices %}
 lvm_{{ vg.get('name', vgname) }}_pv_{{ dev }}:
   lvm.pv_present:
     - name: {{ dev }}
@@ -36,14 +48,14 @@
       - service: lvm_services
     - require_in:
       - lvm: lvm_vg_{{ vg.get('name', vgname) }}
-{%- endfor %}
+  {%- endfor %}
 
 lvm_vg_{{ vg.get('name', vgname) }}:
   lvm.vg_present:
     - name: {{ vg.get('name', vgname) }}
     - devices: {{ vg.devices|join(',') }}
 
-{%- for lvname, volume in vg.get('volume', {}).items() %}
+  {%- for lvname, volume in vg.get('volume', {}).items() %}
 
 lvm_{{ vg.get('name', vgname) }}_lv_{{ volume.get('name', lvname) }}:
   lvm.lv_present:
@@ -60,9 +72,9 @@
     {%- endif %}
     {%- endif %}
 
-{%- endfor %}
+  {%- endfor %}
 
-{%- endif %}
+  {%- endif %}
 
 {%- endfor %}