[Bash-completion-devel] [PATCH] Fix lvm completion related with physical volumes

Liuhua Wang lwang at suse.com
Wed Dec 16 03:21:18 UTC 2015


There are the following problems with lvm completions:
1)_lvm_physicalvolumes() only gets PVs that belong to a VG. In some
  cases like pvremove we can use all PVs including those not included
  in any VGs.
  solution: Add _lvm_physicalvolumes_all to get all PVs and correct
            all the commands.

2)pvcreate should be able to use all block devcices.
  solution: Add _lvm_filedir() to use _filedir except set $cur to /dev
            when $cur is empty.

3)when /etc/lvm/lvm.conf silent is 1 there is no output for vg/lv/pvscan,
  bash-completion will not work.
  solution: Check the value of silent option. If it is 1 then temporarily
            set silent 0 and recover back to 1 after the command executed.

Signed-off-by: Liuhua Wang <lwang at suse.com>
Reviewed-by: Lidong Zhong <lzhong at suse.com>
---
 completions/lvm | 54 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 47 insertions(+), 7 deletions(-)

diff --git a/completions/lvm b/completions/lvm
index d04a549..e0c83bd 100644
--- a/completions/lvm
+++ b/completions/lvm
@@ -1,19 +1,57 @@
 # bash completion for lvm                                  -*- shell-script -*-
 
+_lvm_filedir()
+{
+    cur=${cur:-/dev/}
+    _filedir
+}
+
 _lvm_volumegroups()
 {
+    local silent
+    silent=$(sed -n -e "s|^[ \t]*silent[ \t]*=[ \t]*\([01]\)|\1|p" /etc/lvm/lvm.conf)
+    silent=${silent:-0}
+    [ $silent -eq 1 ] && sed -i "s|\(^[ \t]*silent[ \t]*=[ \t]*\)[01].*$|\10|" /etc/lvm/lvm.conf
+
     COMPREPLY=( $(compgen -W "$( vgscan 2>/dev/null | \
         sed -n -e 's|.*Found.*"\(.*\)".*$|\1|p' )" -- "$cur" ) )
+
+    [ $silent -eq 1 ] && sed -i "s|\(^[ \t]*silent[ \t]*=[ \t]*\)[01].*$|\11|" /etc/lvm/lvm.conf
+}
+
+_lvm_physicalvolumes_all()
+{
+    local silent
+    silent=$(sed -n -e "s|^[ \t]*silent[ \t]*=[ \t]*\([01]\)|\1|p" /etc/lvm/lvm.conf)
+    silent=${silent:-0}
+    [ $silent -eq 1 ] && sed -i "s|\(^[ \t]*silent[ \t]*=[ \t]*\)[01].*$|\10|" /etc/lvm/lvm.conf
+
+    COMPREPLY=( $(compgen -W "$( pvscan 2>/dev/null | \
+        sed -n -e 's|^.*PV \([^ ]*\) .*|\1|p' )" -- "$cur" ) )
+    
+    [ $silent -eq 1 ] && sed -i "s|\(^[ \t]*silent[ \t]*=[ \t]*\)[01].*$|\11|" /etc/lvm/lvm.conf
 }
 
 _lvm_physicalvolumes()
 {
+    local silent
+    silent=$(sed -n -e "s|^[ \t]*silent[ \t]*=[ \t]*\([01]\)|\1|p" /etc/lvm/lvm.conf)
+    silent=${silent:-0}
+    [ $silent -eq 1 ] && sed -i "s|\(^[ \t]*silent[ \t]*=[ \t]*\)[01].*$|\10|" /etc/lvm/lvm.conf
+  
     COMPREPLY=( $(compgen -W "$( pvscan 2>/dev/null | \
         sed -n -e 's|^.*PV \(.*\) VG.*$|\1|p' )" -- "$cur" ) )
+
+    [ $silent -eq 1 ] && sed -i "s|\(^[ \t]*silent[ \t]*=[ \t]*\)[01].*$|\11|" /etc/lvm/lvm.conf
 }
 
 _lvm_logicalvolumes()
 {
+    local silent
+    silent=$(sed -n -e "s|^[ \t]*silent[ \t]*=[ \t]*\([01]\)|\1|p" /etc/lvm/lvm.conf)
+    silent=${silent:-0}
+    [ $silent -eq 1 ] && sed -i "s|\(^[ \t]*silent[ \t]*=[ \t]*\)[01].*$|\10|" /etc/lvm/lvm.conf
+
     COMPREPLY=( $(compgen -W "$( lvscan 2>/dev/null | \
         sed -n -e "s|^.*'\(.*\)'.*$|\1|p" )" -- "$cur" ) )
     if [[ $cur == /dev/mapper/* ]]; then
@@ -23,6 +61,8 @@ _lvm_logicalvolumes()
             [[ ${COMPREPLY[i]} == */control ]] && unset 'COMPREPLY[i]'
         done
     fi
+
+    [ $silent -eq 1 ] && sed -i "s|\(^[ \t]*silent[ \t]*=[ \t]*\)[01].*$|\11|" /etc/lvm/lvm.conf
 }
 
 _lvm_units()
@@ -96,7 +136,7 @@ _pvs()
     if [[ "$cur" == -* ]]; then
         COMPREPLY=( $( compgen -W '$( _parse_usage "$1" --help )' -- "$cur" ) )
     else
-        _lvm_physicalvolumes
+        _lvm_physicalvolumes_all
     fi
 } &&
 complete -F _pvs pvs
@@ -116,7 +156,7 @@ _pvdisplay()
     if [[ "$cur" == -* ]]; then
         COMPREPLY=( $( compgen -W '$( _parse_usage "$1" --help )' -- "$cur" ) )
     else
-        _lvm_physicalvolumes
+        _lvm_physicalvolumes_all
     fi
 } &&
 complete -F _pvdisplay pvdisplay
@@ -136,7 +176,7 @@ _pvchange()
     if [[ "$cur" == -* ]]; then
         COMPREPLY=( $( compgen -W '$( _parse_usage "$1" --help )' -- "$cur" ) )
     else
-        _lvm_physicalvolumes
+        _lvm_physicalvolumes_all
     fi
 } &&
 complete -F _pvchange pvchange
@@ -168,7 +208,7 @@ _pvcreate()
     if [[ "$cur" == -* ]]; then
         COMPREPLY=( $( compgen -W '$( _parse_usage "$1" --help )' -- "$cur" ) )
     else
-        _lvm_physicalvolumes
+        _lvm_filedir
     fi
 } &&
 complete -F _pvcreate pvcreate
@@ -206,7 +246,7 @@ _pvremove()
     if [[ "$cur" == -* ]]; then
         COMPREPLY=( $( compgen -W '$( _parse_usage "$1" --help )' -- "$cur" ) )
     else
-        _lvm_physicalvolumes
+        _lvm_physicalvolumes_all
     fi
 } &&
 complete -F _pvremove pvremove
@@ -322,7 +362,7 @@ _vgcreate()
         if [[ $args -eq 0 ]]; then
             _lvm_volumegroups
         else
-            _lvm_physicalvolumes
+            _lvm_physicalvolumes_all
         fi
     fi
 } &&
@@ -412,7 +452,7 @@ _vgextend()
         if [[ $args -eq 0 ]]; then
             _lvm_volumegroups
         else
-            _lvm_physicalvolumes
+            _lvm_physicalvolumes_all
         fi
     fi
 } &&
-- 
1.8.4.5




More information about the Bash-completion-devel mailing list