Files
vboxmanage-bash-completion/VBoxManage

342 lines
12 KiB
Bash

# bash command-line completion for virtualbox
# development version, or specially used for my opts
# Author: Sebastian T. Hafner <sonix@own-hero.net>
#
# This version of bash completion was born due to the need of fast and easy
# access to the maze of commands and parameters VBoxManage provides. Based on
# Sebastian script I've managed to improve it and adapt to newest stable version
# available in Gentoo Portage.
#
# [x] adoptstate
# [ ] bandwidthctl
# [x] clonehd
# [x] clonevm
# [ ] closemedium
# [x] controlvm
# [ ] convertfromraw
# [ ] createhd
# [ ] createvm
# [ ] debugvm
# [ ] dhcpserver
# [ ] discardstate
# [ ] export
# [ ] extpack
# [ ] getextradata
# [ ] guestcontrol
# [ ] guestproperty
# [ ] hostonlyif
# [ ] import
# [x] list
# [ ] metrics
# [ ] modifyhd
# [.] modifyvm - a LOT options missing
# [ ] natnetwork
# [ ] registervm
# [ ] setextradata
# [ ] setproperty
# [ ] sharedfolder
# [x] showhdinfo
# [x] showvminfo
# [x] snapshot
# [x] startvm
# [o] storageattach - no all options yet
# [ ] storagectl
# [x] unregistervm
# [ ] usbfilter
_VBoxManage() {
local cur prev opts vms vms cmd count item
# Generate registered hard disk files.
# NOTE: This function may introduce some quirks, if there is a space or
# other characters which usually are treated as IFS - like space. Pipe
# character used in disk filename will ruin this completions.
_hdcomp() {
local cur=$1
local hdds
hdds=$(VBoxManage list hdds | \
grep -A 1 'normal (base)' | \
grep "Location:" | \
sed 's/Location:\s\+//' | \
sed 's/\s/\\ /g' | \
tr '\n' '|' | \
sed 's/|$//')
IFS='|' read -ra hdds <<< "$hdds"
for item in "${hdds[@]}"
do
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item")
done
}
# Complete registered VM names.
# Issues are the same as in above function.
_vmscomp() {
local cur=$1
local vms
compopt -o filenames
vms=$(VBoxManage list vms | \
awk -F ' {' '{ print $1 }' | \
tr '\n' '|' | \
sed 's/|$//' | \
sed 's/"//g')
IFS='|' read -ra vms <<< "$vms"
for item in "${vms[@]}"
do
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item")
done
}
COMP_WORDBREAKS=${COMP_WORDBREAKS//|/} # remove pipe from comp word breaks
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ COMP_CWORD -ge 2 ]]; then
cmd="${COMP_WORDS[1]}"
if [[ $cmd == "-q" ]]; then
cmd="${COMP_WORDS[2]}"
fi
fi
# all possible commands for the VBoxManage
opts=$(VBoxManage -q help | \
egrep -o "^\s\s[a-z]+ " | \
grep -v VBoxManage | \
awk '{print $1}'| \
sort | \
uniq)
if [[ ${cur} == "-q" || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
case "${prev}" in
showhdinfo)
_hdcomp ${cur}
;;
clonevm)
_vmscomp ${cur}
;;
clonehd)
case "${cur}" in
-*)
COMPREPLY=( $(compgen -W "--format --variant
--existing" -- ${cur}) )
;;
*)
_hdcomp ${cur}
;;
esac
;;
--format)
COMPREPLY=( $(compgen -W "VDI VMDK VHD RAW" -- ${cur}) )
;;
adoptstate|storageattach|startvm|modifyvm|controlvm|showvminfo|\
unregistervm|snapshot)
_vmscomp ${cur}
;;
--type) #startvm --type
[[ ${cmd} == "startvm" ]] && \
COMPREPLY=( $(compgen -W "headless vrdp sdl gui" -- ${cur}) )
[[ ${cmd} == "createhd" ]] && \
COMPREPLY=( $(compgen -W "normal writethrough" -- ${cur}) )
;;
--ostype)
OS=$(VBoxManage list ostypes | egrep "^ID" | sed "s/ID:\s\+//"|sort)
COMPREPLY=( $(compgen -W "$OS" -- ${cur}) )
;;
--variant)
COMPREPLY=( $(compgen -W "Standard Fixed Split2G Stream ESX" \
-- ${cur}) )
;;
--vrdpauthtype)
COMPREPLY=( $(compgen -W "null external guest" -- ${cur}) )
;;
--nictype[1-6])
COMPREPLY=( $(compgen -W "Am79C970A Am79C973 82540EM 82543GC
82545EM" -- ${cur}) )
;;
--nic[1-6])
COMPREPLY=( $(compgen -W "bridged none null nat hostonly intnet" \
-- ${cur}) )
;;
--boot[1-4])
COMPREPLY=( $(compgen -W "none floppy dvd disk net" -- ${cur}) )
;;
list)
t=$(VBoxManage list | \
grep '|' | \
sed 's/\[.*\]//g'| \
sed 's/VBoxManage list//' | \
tr "\\n" " " | \
sed 's/$/\n/' | \
sed 's/\s\+//g' | \
sed 's/|/ /g')
COMPREPLY=( $(compgen -W "$t" -- ${cur}) )
;;
--device)
COMPREPLY=( $(compgen -W "0" -- ${cur}) )
;;
--port)
COMPREPLY=( $(compgen -W "1 2 3 4" -- ${cur}) )
;;
--type)
COMPREPLY=( $(compgen -W "dvddrive hdd fdd" -- ${cur}) )
;;
--medium)
for item in "none" "emptydrive" "additions"
do
[[ ${item^^} == ${cur^^}* ]] && COMPREPLY+=("$item")
done
_hdcomp ${cur}
;;
--snapshot)
;;
--*) #any options ..
COMPREPLY=()
;;
*)
case "${cmd}" in
clonevm)
case "${prev}" in
--mode)
COMPREPLY=( $(compgen -W "machine machineandchildren all" -- ${cur}) )
;;
--options)
COMPREPLY=( $(compgen -W "link keepallmacs
keepnatmacs keepdisknames" -- ${cur}) )
;;
esac
COMPREPLY=( $(compgen -W "--snapshot --mode --options
--name --groups --basefolder --uuid --register" -- ${cur}) )
;;
adoptstate)
COMPREPLY=()
;;
createhd)
COMPREPLY=( $(compgen -W "--filename --size --type
--format --comment --remember --variant" -- ${cur}) )
;;
createvm)
COMPREPLY=( $(compgen -W "--name --ostype --register
--basefolder --settingsfile --uid" -- ${cur}) )
;;
controlvm)
case "${prev}" in
nictracefile[0-9]*)
COMPREPLY=( $(compgen -f -- ${cur}) )
;;
nictrace[0-9]*)
COMPREPLY=( $(compgen -W "on off" -- ${cur}) )
;;
nicpromisc[0-9]*)
COMPREPLY=( $(compgen -W "deny allow-vms
allow-all" -- ${cur}) )
;;
nic[0-9]*)
COMPREPLY=( $(compgen -W "null nat bridged intnet
hostonly generic natnetwork" -- ${cur}) )
;;
natpf[0-9]*)
COMPREPLY=( $(compgen -W "delete tcp
udp" -- ${cur}) )
;;
clipboard)
COMPREPLY=( $(compgen -W "disabled hosttoguest
guesttohost bidirectional" -- ${cur}) )
;;
draganddrop)
COMPREPLY=( $(compgen -W "disabled
hosttoguest" -- ${cur}) )
;;
vrde)
COMPREPLY=( $(compgen -W "on off" -- ${cur}) )
;;
vcpenabled)
COMPREPLY=( $(compgen -W "on off" -- ${cur}) )
;;
vcpscreens)
COMPREPLY=( $(compgen -W "all none" -- ${cur}) )
;;
setcredentials)
COMPREPLY=( $(compgen -W "--passwordfile
--allowlocallogon" -- ${cur}) )
;;
teleport)
COMPREPLY=( $(compgen -W "--host --port --maxdowntime
--passwordfile --password" -- ${cur}) )
;;
webcam)
COMPREPLY=( $(compgen -W "attach detach
list" -- ${cur}) )
;;
acpipowerbutton|acpisleepbutton|cpuexecutioncap|\
guestmemoryballoon|keyboardputscancode|\
nicproperty[0-9]*|pause|plugcpu|poweroff|reset|\
resume|savestate|screenshotpng|setlinkstate1|\
setvideomodehint|unplugcpu|usbattach|usbdetach|\
vrdeport|vrdeproperty|vrdevideochannelquality)
COMPREPLY=()
;;
*)
COMPREPLY=( $(compgen -W "acpipowerbutton
acpisleepbutton clipboard cpuexecutioncap
draganddrop guestmemoryballoon keyboardputscancode
natpf1 nic1 nicpromisc1 nicproperty1 nictrace1
nictracefile1 pause plugcpu poweroff reset resume
savestate screenshotpng setcredentials setlinkstate1
setvideomodehint teleport unplugcpu usbattach
usbdetach vcpenabled vcpscreens vrde vrdeport
vrdeproperty vrdevideochannelquality
webcam" -- ${cur}) )
;;
esac
;;
unregistervm)
COMPREPLY=( $(compgen -W "--delete" -- ${cur}) )
;;
startvm)
COMPREPLY=( $(compgen -W "--type" -- ${cur}) )
;;
snapshot)
COMPREPLY=( $(compgen -W "take delete restore restorecurrent
edit list showvminfo --description --current --name
--details --machinereadable" -- ${cur}) )
;;
modifyvm)
COMPREPLY=( $(compgen -W "--name --memory --ostype --usb
--vram --acpi --ioapic --hwvirtex --accelerate3d --cpus
--sataportcount --sataport1 --sataport2 --sataport3
--sataport4 --nic1 --nictype1 --nic2 --nictype2 --nic3
--nictype3 --nic4 --nictype4 --nic5 --nictype5 --nic6
--nictype6 --bridgeadapter1 --bridgeadapter2
--bridgeadapter3 --bridgeadapter4 --bridgeadapter5
--bridgeadapter6 --vrdp --vrdpport --vrdpauthtype" \
-- ${cur}) )
;;
closemedium)
COMPREPLY=( $(compgen -W "disk dvd floppy" -- ${cur}) )
;;
storageattach)
COMPREPLY=( $(compgen -W "--storagectl --device --port
--medium --type" -- ${cur}) )
;;
showvminfo)
COMPREPLY=( $(compgen -W "--details --machinereadable
--log" -- ${cur}) )
;;
*)
COMPREPLY=()
;;
esac
;;
esac
}
complete -o default -F _VBoxManage VBoxManage
# vim: set ft=sh tw=80 sw=4 et :