-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm-ls.sh
executable file
·96 lines (83 loc) · 2.84 KB
/
vm-ls.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash -ue
#
# 'vmscripts' low-level VM management scripts - active VMs listing
#
# Copyright © 2015, 2016, 2017 Thilo Fromm. Released under the terms of the GNU
# GLP v3.
#
# vmscripts is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# vmscripts is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with vmscripts. If not, see <http://www.gnu.org/licenses/>.#
#
vmscripts_prereq="none"
ls_shortopts="-a -t"
ls_longopts="--active --tap"
ls_usage() {
echo " Usage:"
echo " vm ls [-a|--active] [-t|--tap] - List VMs."
echo
echo " Optional arguments include:"
echo
echo " -a|--active List started / running VMs only."
echo " -t|--tap List VMs using TAP networking only."
}
# ----
vm_ls_active() {
local line=""
for line in $(screen -ls | grep '\-vmscripts' | awk '{print $1}'); do
echo "${line/-vmscripts/}" | sed 's/^[0-9]\+\.//g'
done
}
# ----
vm_ls() {
local opts ls_act=false ls_tap=false
opts=$(getopt -o at -l "active,tap" -n "vm ls" -- "$@")
for o in $opts; do
case $o in
-a|--active) ls_act=true;;
-t|--tap) ls_tap=true;;
esac
done
local active="$(vm_ls_active | sed 's/^\(.*\)$/ \1 /')"
for name in $(ls -1 "$VM_CONFIG_PATH/") ; do
[ ! -d "$VM_CONFIG_PATH/$name" ] && continue
local aflag="" lflag=""
# check for soft-linked VM; display link source
if [ -L "$VM_CONFIG_PATH/$name/${name}.img" ] ; then
local src=$(basename \
$(readlink "$VM_CONFIG_PATH/$name/${name}.img" \
| sed 's/\.img//'))
lflag="(->$src)"
fi
# check whether it's currently active
if echo "$active" | grep -qw " $name "; then
aflag="(active)"
elif $ls_act ; then
continue
fi
local net="" netmode="" cpu="" mem="" forward_ports=""
source "$VM_CONFIG_PATH/${name}/${name}.cfg"
[ -z "$netmode" ] && netmode="hidden"
$ls_tap && [ "$netmode" != "tap" ] && continue
printf "%20s %10.10s %20.20s %16.16s %s\n" \
"$name" "$aflag" "$lflag" "$net" "$netmode"
done
}
# ----
if [ `basename "$0"` = "vm-ls.sh" ] ; then
[ "${vm_tools_initialized-NO}" != "YES" ] && {
exec $(which vm) "ls" $@
exit 1; }
vm_ls $@
else
true
fi