blob: fafb403811930c281b8bf5677869372cdbda8985 [file] [log] [blame]
#
# /etc/bash_completion.d/drbdadm
#
# Bash completion for the DRBD top-level management application, drbdadm.
#
# If you have bash completion enabled, this module will
#
# - provide tab completion for drbdadm sub-commands (up, down, primary,
# secondary etc.);
#
# - try to detect your current resource state and provide appropriate
# command completion for the sub-command you provided. For example,
# when if you have entered the "primary" sub-command, it will list
# only those resources that are currently in the Secondary role;
#
# - differentiate between stacked and unstacked resources.
#
# This module does NOT guarantee that the DRBD state engine will in
# fact agree to do what you ask it to. For example, resources that are
# currently Primary and not connected are not excluded from the
# completion list for the "detach" sub-command.
#
# Finally, this module is only capable of parsing resources correctly
# if you are using the default location for your DRBD configuration
# file (/etc/drbd.conf).
__drbdadm_all_resources() {
# Detects all resources currently listed in drbd.conf
local resources="$(${DRBDADM} sh-resources) all"
COMPREPLY=( $(compgen -W "$resources" -- "$current") )
}
__drbdadm_resources_by_status() {
# Detects only those resources that match a particular status
local status_type="$1"
shift 1
local status_filter="$*"
local resources="$(${DRBDADM} sh-resources)"
local filtered_resources
local res
for res in $resources; do
local resource_status="$(${DRBDADM} $status_type $res 2>/dev/null)"
# In case of multiple volumes, consider only the first line
set -- $resource_status
resource_status=$1
local i
for i in $status_filter; do
if [ "${resource_status%%/*}" = $i ]; then
filtered_resources="$filtered_resources $res"
fi
done
done
COMPREPLY=( $(compgen -W "$filtered_resources" -- "$current") )
}
__drbdadm_commands() {
# Lists drbdadm sub-commands
COMPREPLY=( $(compgen -W "$drbdadm_command_list" -- "$current") )
}
__drbdadm_options() {
# Lists global drbdadm options
local options='-d --dry-run -v --verbose -S --stacked -t --config-to-test'
COMPREPLY=( $(compgen -W "$options" -- "$current") )
}
__drbdadm_subcmd_options() {
local subcmd="$1"
local options=($(drbdadm help $subcmd | sed -e '1,/OPTIONS FOR/ d;/^$/,$ d;s/ \(--[a-z-]*\).*/\1/'))
local filtered
local o have
for o in ${options[@]}; do
for have in ${COMP_WORDS[@]}; do
[[ $o = "$have" ]] && continue 2
done
filtered="$filtered $o"
done
COMPREPLY=( $(compgen -W "$filtered" -- "$current") )
}
_drbdadm() {
local DRBDADM="env DRBD_DONT_WARN_ON_VERSION_MISMATCH=1 ${COMP_WORDS[0]}"
local drbdadm_command_list=' attach disk-options detach connect net-options disconnect up resource-options down primary secondary invalidate invalidate-remote outdate verify pause-sync resume-sync resize adjust wait-connect role cstate dstate dump wait-connect wait-con-int create-md dump-md wipe-md get-gi show-gi help apply-al hidden-commands '
# Redefine the drbdadm we use in __drbdadm_all_resources and
# __drbdadm_resources_by_status, if running in stacked mode
case "$COMP_LINE " in
*" -S "*|*" --stacked "*)
DRBDADM="$DRBDADM --stacked"
;;
esac
local current previous
# The word currently being evaluated for completion
current=${COMP_WORDS[COMP_CWORD]}
# The word that precedes the currently-evaluated one
previous=${COMP_WORDS[COMP_CWORD-1]}
case "$previous" in
drbdadm)
case "$current" in
-*)
__drbdadm_options
;;
*)
__drbdadm_commands
;;
esac
;;
primary)
__drbdadm_resources_by_status "role" "Secondary"
;;
secondary)
__drbdadm_resources_by_status "role" "Primary"
;;
detach|disk-options)
__drbdadm_resources_by_status "dstate" "UpToDate" "Inconsistent" "Outdated"
;;
outdate)
__drbdadm_resources_by_status "dstate" "UpToDate"
;;
attach|apply-al)
__drbdadm_resources_by_status "dstate" "Diskless" "Unconfigured"
;;
connect)
__drbdadm_resources_by_status "cstate" "StandAlone" "Unconfigured"
;;
invalidate-remote)
__drbdadm_resources_by_status "cstate" "Connected"
;;
disconnect|net-options)
__drbdadm_resources_by_status "cstate" "Connected" "WFConnection" "VerifyT" "VerifyS"
;;
verify)
__drbdadm_resources_by_status "cstate" "Connected"
;;
pause-sync)
__drbdadm_resources_by_status "cstate" "SyncSource" "SyncTarget"
;;
resume-sync)
__drbdadm_resources_by_status "cstate" "PausedSyncS" "PausedSyncT"
;;
*)
if (( COMP_CWORD > 2 )); then
local subcmd
subcmd=${COMP_WORDS[1]}
case "$drbdadm_command_list" in
*" $subcmd "*)
__drbdadm_subcmd_options $subcmd
;;
esac
else
__drbdadm_all_resources
fi
;;
esac
}
complete -o default -F _drbdadm drbdadm