Whamcloud - gitweb
LU-16945 tests: skip sanity test_27Cg in interop
[fs/lustre-release.git] / lustre / scripts / bash-completion / lustre
1 _lustre_cmds()
2 {
3         local cmd="$1"
4         local sub="$2"
5
6         $cmd $sub --list-commands |
7                 sed -e '/=/d' -e s/exit// -e s/quit//
8 }
9
10 _lustre_long_opts()
11 {
12         local cmd="$1"
13         local sub="$2"
14         local subsub="$3"
15
16         # strip off usage message decoration and leave long opts
17         if [[ -n "$subsub" ]]; then
18                 "$cmd" "$sub" help "$subsub" |& grep -owE -- '--[-a-zA-Z0]*'
19         else
20                 "$cmd" help "$sub" |& sed -e 1d | grep -owE -- '--[-a-zA-Z0]*'
21         fi
22         # several commands take the same options as setstripe, except --delete
23         case "$sub$subsub" in
24         migrate|mirrorcreate|mirrorextend)
25                 _lustre_long_opts "$cmd" setstripe | grep -v -- --delete
26         esac
27 }
28
29 _lustre_short_opts()
30 {
31         local cmd="$1"
32         local sub="$2"
33         local subsub="$3"
34
35         # strip off usage message decoration and leave short opts
36         if [[ -n "$subsub" ]]; then
37                 "$cmd" "$sub" help "$subsub" |& grep -owE -- '-[-a-zA-Z0]'
38         else
39                 "$cmd" help "$sub" |& grep -owE -- '-[-a-zA-Z0]'
40         fi
41         # several commands take the same options as setstripe, except -d
42         case "$sub$subsub" in
43         migrate|mirrorextend|mirrorextend)
44                 _lustre_short_opts "$cmd" setstripe | grep -v -- -d
45         esac
46 }
47
48 _lustre_comp_flags()
49 {
50         local cmd=$1
51         local flags
52
53         flags=$("$cmd" help find |& tr "<>[],}" " " |
54                 grep -- '--component-flags {' | cut -d\{ -f2)
55         if [[ -z "$flags" ]]; then
56                 local version=($("$cmd" --version))
57
58                 case "${version[1]}" in
59                 2.13*) flags="init stale prefer offline nosync extension";;
60                 2.12*|2.11*) flags="init stale prefer offline nosync";;
61                 *) flags="init";;
62                 esac
63         fi
64         echo $flags
65 }
66
67 _lustre_mountpoints()
68 {
69         findmnt --list -t lustre -n -o TARGET
70 }
71
72 _lustre_mount_fsnames()
73 {
74         local mountpoint
75
76         # FIXME: will fail if newlines in $mountpoint, why would anyone do that?
77         _lustre_mountpoints | while read mountpoint; do
78                 lfs getname -n "$mountpoint" 2> /dev/null
79         done
80 }
81
82 _lustre_devices()
83 {
84         lctl device_list | awk '{ print $4 }'
85 }
86
87 _lustre_fsnames()
88 {
89         local mountpoint="${1:-'.'}"
90
91         local fsname=$(lfs getname -n "$mountpoint" 2>/dev/null)
92
93         [[ -n "$fsname" ]] && echo "$fsname" || _lustre_mount_fsnames
94 }
95
96 _lustre_layouts()
97 {
98         "$cmd" help find |& tr "[]," " " | grep -- --layout | sed "s/.*-L //"
99 }
100
101 _lustre_mdts()
102 {
103         lfs mdts $1 | grep _UUID | sed -e "s/[0-9]*: //" -e "s/_UUID.*//"
104 }
105
106 _lustre_osts()
107 {
108         lfs osts $1 | grep _UUID | sed -e "s/[0-9]*: //" -e "s/_UUID.*//"
109 }
110
111 _lustre_pools()
112 {
113         if [[ -d "$1" ]]; then
114                 "$cmd" pool_list $1 2> /dev/null | grep -v "[Pp]ools from" |
115                         cut -d. -f2
116                 return 0
117         fi
118
119         for fsname in $(_lustre_fsnames $1); do
120                 "$cmd" pool_list $fsname 2> /dev/null | grep -v "[Pp]ools from"
121         done
122 }
123
124 _lfs()
125 {
126         local cur prev words cword
127         local mountpoint cmd sub find_opts
128
129         COMPREPLY=()
130         # allow different versions of bash_completion to work
131         if declare -F _init_completion > /dev/null; then
132                 # this provides more functionality, but is only in v2.x
133                 _init_completion || return
134         else
135                 # this is compatible with both v1.3 and v2.x
136                 _get_comp_words_by_ref cur prev words cword
137         fi
138
139         cmd="${words[0]}"
140         sub="${words[1]}"
141         [[ "$sub" == "mirror" || "$sub" == "pcc" ]] && subsub="${words[2]}"
142         if [[ "$cword" == "1" || "$prev" == "help" ]]; then
143                 COMPREPLY+=($(compgen -W '$(_lustre_cmds "$cmd")' -- "$cur"))
144                 return 0
145         fi
146
147         case "$cur" in
148         --*)
149                 COMPREPLY+=($(compgen -W '$(_lustre_long_opts "$cmd" "$sub" "$subsub")' -- "$cur"))
150                 return 0
151                 ;;
152         -*)
153                 # lfs find allows "-longopt" for compatibility with find(1)
154                 [[ "$sub" == "find" ]] && find_opts=$(_lustre_long_opts "$cmd" find)
155                 COMPREPLY+=($(compgen -W '$(_lustre_short_opts "$cmd" "$sub" "$subsub") ${find_opts//--/-}' -- "$cur"))
156                 return 0
157                 ;;
158         esac
159
160         case "$sub" in
161         check)
162                 [[ -n "$cur" ]] && return 0
163                 COMPREPLY+=($(compgen -W '$("$cmd" help check |& grep usage |
164                                             sed -e "s/[<>|]/ /g" \
165                                                 -e "s/.*check //")' -- "$cur"))
166                 return 0
167                 ;;
168         df)
169                 mapfile -t COMPREPLY < <(
170                         _lustre_mountpoints | grep -- "^$cur" | sed 's/ /\\ /g'
171                 )
172                 return 0
173                 ;;
174         find)
175                 [[ -d "${words[2]}" ]] && mountpoint="${words[2]}"
176                 case "${prev/--/-}" in
177                 -component-flags|-comp-flags)
178                         # FIXME: this should allow a comma-separated list
179                         COMPREPLY+=($(compgen -W '$(_lustre_comp_flags)' -- "$cur"))
180                         return 0
181                         ;;
182                 -g|-group)
183                         COMPREPLY+=($(compgen -g -- "$cur"))
184                         return 0
185                         ;;
186                 -L|-layout)
187                         COMPREPLY+=($(compgen -W '$(_lustre_layouts)' -- "$cur"))
188                         return 0
189                         ;;
190                 -m|-mdt)
191                         # FIXME: this should allow a comma-separated list
192                         COMPREPLY+=($(compgen -W '$(_lustre_mdts "$mountpoint")' -- "$cur"))
193                         return 0
194                         ;;
195                 -O|-ost)
196                         # FIXME: this should allow a comma-separated list
197                         COMPREPLY+=($(compgen -W '$(_lustre_osts "$mountpoint")' -- "$cur"))
198                         return 0
199                         ;;
200                 -pool)
201                         COMPREPLY+=($(compgen -W '$(_lustre_pools "$mountpoint")' -- "$cur"))
202                         return 0
203                         ;;
204                 -t|-type)
205                         COMPREPLY+=($(compgen -W 'b c d f l p s' -- "$cur"))
206                         return 0
207                         ;;
208                 -u|-user)
209                         COMPREPLY+=($(compgen -u -- "$cur"))
210                         return 0
211                         ;;
212                 esac
213                 if [ -z "$mountpoint" ]; then
214                         mapfile -t COMPREPLY < <(
215                                 _lustre_mountpoints | grep -- "^$cur" |
216                                         sed -e 's/ /\\ /g'
217                         )
218                         return 0
219                 fi
220                 ;;
221         mirror)
222                 if [[ "$prev" == "$sub" ]]; then
223                         COMPREPLY+=($(compgen -W '$(_lustre_cmds "$cmd" "$sub")' -- "$cur"))
224                         return 0
225                 fi
226                 ;;
227         pcc)
228                 if [[ "$prev" == "$sub" ]]; then
229                         COMPREPLY+=($(compgen -W '$(_lustre_cmds "$cmd" "$sub")' -- "$cur"))
230                         return 0
231                 fi
232                 ;;
233         pool_list)
234                 COMPREPLY+=($(compgen -W '$(_lustre_fsnames
235                                             _lustre_pools)' -- "$cur"))
236                 return 0
237                 ;;
238         setstripe)
239                 case "$prev" in
240                 --component-flags|--comp-flags)
241                         # only subset allowed, easier to list than exclude for now
242                         # COMPREPLY+=($(compgen -W '$(_lustre_comp_flags)' -- "$cur"))
243                         # FIXME: this should allow a comma-separated list
244                         COMPREPLY+=($(compgen -W 'nosync prefer' -- "$cur"))
245                         return 0
246                         ;;
247                 -p|--pool)
248                         COMPREPLY+=($(compgen -W '$(_lustre_pools)' -- "$cur"))
249                         return 0
250                         ;;
251                 esac
252                 ;;
253         esac
254
255         _filedir
256         return 0
257 } &&
258 complete -F _lfs lfs
259
260 _lctl()
261 {
262         local cur prev words cword
263
264         COMPREPLY=()
265         # allow different versions of bash_completion to work
266         if declare -F _init_completion > /dev/null; then
267                 # this provides more functionality, but is only in v2.x
268                 _init_completion || return
269         else
270                 # this is compatible with both v1.3 and v2.x
271                 _get_comp_words_by_ref cur prev words cword
272         fi
273
274         cmd="${words[0]}"
275         sub="${words[1]}"
276         [[ "$sub" == "--device" && $cword -ge 4 ]] && sub="${words[3]}"
277
278         if [[ "$cword" == "1" || "$prev" == "help" ]]; then
279                 COMPREPLY+=($(compgen -W '$(_lustre_cmds "$cmd")' -- "$cur"))
280                 return 0
281         fi
282
283         case "$cur" in
284         --*)
285                 COMPREPLY+=($(compgen -W '$(_lustre_long_opts "$cmd" "$sub")' -- "$cur"))
286                 return 0
287                 ;;
288         -*)
289                 COMPREPLY+=($(compgen -W '$(_lustre_short_opts "$cmd" "$sub")' -- "$cur"))
290                 return 0
291                 ;;
292         esac
293
294         case "$sub" in
295         --device)
296                 if [[ "$cword" == "2" ]]; then
297                         COMPREPLY+=($(compgen -W '$(_lustre_devices)' -- "$cur"))
298                 elif [[ "$cword" == "3" ]]; then
299                         COMPREPLY+=($(compgen -W '$(_lustre_cmds "$cmd")' -- "$cur"))
300                 fi
301                 return 0
302                 ;;
303         get_param|list_param|set_param)
304                 local filter="s/=$//"
305                 [[ "$sub" == "set_param" ]] && filter="/[^=/]$/d"
306                 mapfile -t COMPREPLY < <(
307                     "$cmd" list_param -F "${cur#[\"\']}*" 2>/dev/null |
308                     sed -e "$filter" -e 's#/$#.#' \
309                         -e "s#^${cur//\*/[^.]*}#$cur#"
310                 )
311                 compopt -o nospace
312
313                 return 0
314                 ;;
315         pcc)
316                 if [[ "$prev" == "$sub" ]]; then
317                         COMPREPLY+=($(compgen -W '$(_lustre_cmds "$cmd" "$sub")' -- "$cur"))
318                         return 0
319                 fi
320                 ;;
321         pool_list)
322                 if [[ "$cword" == "2" ]]; then
323                         COMPREPLY+=($(compgen -W '$(_lustre_mountpoints
324                                                     _lustre_fsnames
325                                                     _lustre_pools)' -- "$cur"))
326                         return 0
327                 fi
328                 ;;
329         pool_destroy)
330                 if [[ "$cword" == "2" ]]; then
331                         COMPREPLY+=($(compgen -W '$(_lustre_pools)' -- "$cur"))
332                         return 0
333                 fi
334                 return 0
335                 ;;
336         pool_add|pool_remove)
337                 if [[ "$cword" == "2" ]]; then
338                         COMPREPLY+=($(compgen -W '$(_lustre_pools)' -- "$cur"))
339                         return 0
340                 elif [[ "$cword" == "3" ]]; then
341                         COMPREPLY+=($(compgen -W '$(_lustre_osts)' -- "$cur"))
342                         return 0
343                 fi
344                 return 0
345                 ;;
346         esac
347 } &&
348 complete -F _lctl lctl