-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm
executable file
·267 lines (226 loc) · 6.61 KB
/
vm
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash -ue
#
# 'vmscripts' low-level VM management scripts main call entry point
#
# Copyright © 2015 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/>.#
#
# MODULES API
# ===============
#
# - modules must be in the current PATH
# - modules are named 'vm-<modname>.sh'
# - modules *MUST* immplement at least a main function like
#
# vm_<modname>() {
# # main entry function of module
# }
#
# -----------------
#
# - modules *MAY* implement these utility functions:
#
# <modname>_usage() {
# echo "Usage: vm <modname> ...."
# }
#
# -----
#
# <modname>_complete() {
# # auto-complete helper for the module
# }
#
# -----
#
# - modules *MAY* define command line arguments for use with auto completion,
# e.g.:
#
# <modname>_shortopts="-i -d -s -M -m"
# <modname>_longopts="--iso --disk --disk-size --move --mem"
#
# -----
#
# - modules *MAY* define one global variable determining the importance of the
# <vm-name> argument:
#
# vmscripts_prereq=""
#
# Values:
# 'none' - VM name must neither be provided nor needs the VM to exist
# 'name' - A VM name must be provided
# 'exist' - A name must be provided and the corresponding VM must exist
# 'inactive' - The VM specified by the <name> argument must be powered off
# 'active' - The VM specified by the <name> argument must be up + running
#
# -----------------
#
# Variables defined and exported by "vm" main entry script
# - basically, look at setup_env() below
# - also, all values from the config and the run time config
#
# Variables and Functions to be used by modules
#
VM_CONFIG_PATH="$HOME/.vmscripts"
[ ! -d "$VM_CONFIG_PATH" ] && mkdir -p "$VM_CONFIG_PATH"
export VM_CONFIG_PATH
# ----
usage() {
local ret=0
echo
if [ $# -ge 1 ] ; then
echo " vm $vm_cmd: $@"
echo
ret=1
fi
${vm_cmd}_usage
echo
exit $ret
}
# ----
die() {
echo >&2
echo "ERROR: $@" >&2
echo >&2
exit 1
}
# ----
write_rtconf() {
echo "$1=\"$2\"" >> "$vm_rtconf"
}
# ----
#
# 'vm' script internal Variables and Functions
#
function source_with_defaults() {
local cmd="$1"
local script=$(which "vm-${cmd}.sh" 2>/dev/null)
[ -z "$script" ] && return 1
vmscripts_prereq="none"
eval "${cmd}_usage() { return ; }"
eval "${cmd}_complete() { return 1; }"
eval "${cmd}_longopts=''"
eval "${cmd}_shortopts=''"
source "$script"
}
# ----
vm_usage() {
local ret=0
echo
if [ $# -ge 1 ] ; then
echo " $@"
ret=1
fi
echo " Usage:"
echo " vm <creat|prepare|ls|slink|start|ports|iodir|attach|ssh|off|purge>"
echo
exit $ret
}
# ----
setup_env() {
vm_name="$1"
vm_path="$VM_CONFIG_PATH/$vm_name"
local vm_prefix="$vm_path/$vm_name"
vm_disk_image="${vm_prefix}.img"
vm_iso_image="${vm_prefix}.iso"
vm_iodir="${vm_prefix}-io"
vm_pidfile="${vm_prefix}.pid"
vm_logfile="${vm_prefix}.log"
vm_rtconf="${vm_prefix}.run"
vm_config="${vm_prefix}.cfg"
# source static and run time config if present
[ -f "$vm_config" ] && source "$vm_config"
[ -f "$vm_rtconf" ] && source "$vm_rtconf"
[ -z "${netmode-}" ] && netmode="hidden"
vm_tools_initialized="YES"
export vm_name vm_path vm_disk_image vm_iso_image vm_pidfile \
vm_logfile vm_rtconf vm_config vm_tools_initialized write_rtconf \
vm_iodir
}
# ----
initialize_command() {
local cmd="$1"
source_with_defaults "$cmd" || \
vm_usage "Unknown command '$vm_cmd'."
# check VM name command line option
[ "$vmscripts_prereq" = "none" ] && return
[ -z "$vm_name" ] && \
usage "This command takes a mandatory <name> argument"
[ "$vmscripts_prereq" = "name" ] && return
vm ls | grep -qw "$vm_name" || \
usage "Unknown VM ${vm_name}. Try 'vm ls' to get a list."
[ "$vmscripts_prereq" = "exist" ] && return
case "$vmscripts_prereq" in
active)
vm ls -a | grep -qw "$vm_name" || \
usage "VM ${vm_name} is currently inactive. Try starting it with 'vm start $vm_name'."
;;
inactive)
vm ls | grep '(active)' | grep -qw "$vm_name" && \
usage "VM ${vm_name} is currently active. Try shutting it down with 'vm off $vm_name'."
;;
esac
return 0
}
# ----
vm_complete() {
local cword="$1"; shift
local words=( $@ )
local cur="${words[$cword]-}"
local opts="creat prepare ls slink start ports iodir attach ssh off purge"
[ $cword -eq 1 ] && { compgen -W "${opts}" -- ${cur}; return; }
local cmd="${words[1]}"
source_with_defaults "$cmd" || return
while true; do
[ $cword -eq 2 ] && case $vmscripts_prereq in
exist)
compgen -W "$(vm ls \
| awk '{print $1}')" -- ${cur}; break ;;
inactive)
compgen -W "$(vm ls \
| grep -v '(active)' \
| awk '{print $1}')" -- ${cur}; break ;;
active)
compgen -W "$(vm ls -a \
| awk '{print $1}')" -- ${cur}; break ;;
esac
break
done
[ $cword -le 2 -a "$vmscripts_prereq" != "none" -o $cword -le 1 ] && return
opts="$(eval echo \$${cmd}_shortopts) $(eval echo \$${cmd}_longopts)"
if [ -n "$opts" ] && ! ${cmd}_complete "$cword" $@ ; then
compgen -W "${opts}" -- $cur
fi
}
# ----
vm_main() {
vm_name=""
vm_cmd=""
[ $# -lt 1 ] && vm_usage "No command provided."
[ "$1" = "-h" ] && vm_usage
[ "$1" = "complete" ] && { shift; vm_complete $@; exit; }
vm_cmd="$1"; shift
export vm_cmd
setup_env "${1-}"
local script=$(which "vm-${vm_cmd}.sh" 2>/dev/null)
initialize_command "$vm_cmd"
[ "${1-}" = "-h" ] && usage
vm_${vm_cmd} "$@"
}
# ----
if [ `basename "$0"` = "vm" ] ; then
vm_main "$@"
else
true
fi