Skip to content

Commit

Permalink
kiss-vm add three new sub-cmds: iflist, if.unplugin, if.plugin
Browse files Browse the repository at this point in the history
iflist is the wrapper of: virsh domiflist <vmname>
if.unplugin,if.plugin could be used to do unplug/plug test

Signed-off-by: Jianhong Yin <[email protected]>
  • Loading branch information
tcler committed May 2, 2024
1 parent 0ba087e commit d6cdda4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
15 changes: 13 additions & 2 deletions bash-completion/vm
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ _vm()
local netparams= netlist=
_init_completion || return

subcommandList=(prepare enable-nested-vm help h create ls list clone port-available login console qmp monitor delete ifaddr viewer vncproc xml edit exec restart reboot stop start autostart stat blklist netls netcreate netinfo netdel netstart cpfrom cpto rootdir homedir add.iso add.disk add.if add.if.bridge add.if.direct add.if.passthru)
subcommandList=(prepare enable-nested-vm help h create ls list clone port-available login console qmp monitor delete ifaddr iflist if.unplugin if.plugin viewer vncproc xml edit exec restart reboot stop start autostart stat blklist netls netcreate netinfo netdel netstart cpfrom cpto rootdir homedir add.iso add.disk add.if add.if.bridge add.if.direct add.if.passthru)

for ((i=0; i<${#words[@]}; i++)); do
w=${words[$i]}
Expand Down Expand Up @@ -138,7 +138,7 @@ _vm()
fi
fi
;;
del|delete|stop|autos*|start|stat|blk*|srcfiles|homedir)
del|delete|stop|autos*|start|stat|blk*|srcfiles|homedir|iflist|ifl*)
vmlist=$(virsh list --all --name 2>/dev/null)
COMPREPLY=( $( compgen -W "$vmlist" -- "$cur" ) )
;;
Expand All @@ -156,6 +156,17 @@ _vm()
COMPREPLY=( $( compgen -W "#<Enter>" -- "$cur" ) )
fi
;;
if.unplug*|if.u*|if.plug*|if.p*)
if [[ $narg -le 3 ]]; then
vmlist=$(virsh list --all --name 2>/dev/null)
COMPREPLY=( $( compgen -W "$vmlist" -- "$cur" ) )
elif [[ $narg -le 4 ]]; then
_iflist="$(seq $(vm iflist ${words[2]} 2>/dev/null|wc -l))"
COMPREPLY=( $( compgen -W "$_iflist" -- "${cur}" ) )
else
COMPREPLY=( $( compgen -W "#<Enter>" -- "$cur" ) )
fi
;;
add.iso|add.disk)
if [[ $narg -le 3 ]]; then
vmlist=$(virsh list --all --name 2>/dev/null)
Expand Down
36 changes: 34 additions & 2 deletions kiss-vm
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,32 @@ vmdelete() {
for vm; do _vmdelete $vm; done
}

vmiflist() {
local _vmname=$(vmname_extract -all "$1")
[[ -z "$_vmname" ]] && {
resf=$RuntimeTmp/vmlist
vmdialogradiolist vm-iflist $resf all && _vmname=$(sed 's/"//g' $resf)
[[ -z "$_vmname" ]] && { return; }
}
virsh domiflist "$_vmname" | awk 'NR>2&&$NF'
}

vmifunplugin() {
local _vmname=$(vmname_extract -all "$1")
local _ifid=$2
[[ $_ifid = [0-9] ]] && _ifid=$(vmiflist $_vmname|awk "NR==${_ifid}{print \$NF}")
[[ -z "$_ifid" ]] && { echo "{WARN} Usage: $0 <vmname> <if-mac|if-srcname|if-number>" >&2; return 1; }
run -debug virsh domif-setlink "$_vmname" "$_ifid" down
}

vmifplugin() {
local _vmname=$(vmname_extract -all "$1")
local _ifid=$2
[[ $_ifid = [0-9] ]] && _ifid=$(vmiflist $_vmname|awk "NR==${_ifid}{print \$NF}")
[[ -z "$_ifid" ]] && { echo "{WARN} Usage: $0 <vmname> <if-mac|if-srcname|if-number>" >&2; return 1; }
run -debug virsh domif-setlink "$_vmname" "$_ifid" up
}

vmifaddr() {
local GETENT=
local VERBOSEIF=
Expand Down Expand Up @@ -2361,7 +2387,10 @@ Usage() {
$P login [VM [U [P]]]#login VM via ssh //you can use l,lo,log* instead login
$P console [VM] #log VM via console //you can use co,con,cons* instead console
$P delete [VM list] #delete VMs //you can use d,de,del*,r,rm instead delete
$P ifaddr [VM] #show ip address //you can use i,if,if* instead ifaddr
$P ifaddr [VM] #show ip address //you can use if,ifa* instead ifaddr
$P iflist [VM] #show domif list //you can use ifl,ifl* instead iflist
$P if.unplugin <vmname> <if-num> #unplug //you can use if.u* instead full spell
$P if.plugin <vmname> <if-num> #plugin //you can use if.p* instead full spell
$P viewer [VM] #call virt-viewer //you can use vi,vie,view instead
$P vnc [VM] #show vnc host:port //you can use vn instead
$P vnc [-get|-put|-putln] [VM] #read screen text or send string thru vnc
Expand Down Expand Up @@ -3020,7 +3049,10 @@ case "$subcmd" in
cpto|cpt*) vmcopyto "$@"; exit $?;;
e|ex|ex*) vmexec "$@"; exit $?;;
ls|li|lis|list) vmlist "$@"; exit $?;;
i|if|if*) vmifaddr "$@"; exit $?;;
if|ifa*|ifaddr) vmifaddr "$@"; exit $?;;
ifl*|iflist) vmiflist "$@"; exit $?;;
if.un*|if.unplug) vmifunplugin "$@"; exit $?;;
if.pl*|if.plug) vmifplugin "$@"; exit $?;;
vi|vie|view*) vmviewer "$@"; exit $?;;
vn|vnc|vncp*) vmvncproc "$@"; exit $?;;
x|xm*) vmxml "$@"; exit $?;;
Expand Down

0 comments on commit d6cdda4

Please sign in to comment.