-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.in
executable file
·209 lines (164 loc) · 3.76 KB
/
vm.in
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
#!/bin/sh -efu
[ -z "@MKLOCAL@" ] ||
export PATH="@PREFIX@/libshell:${PATH#@PREFIX@/libshell:}"
. shell-args
. shell-getopt
. shell-signal
. shell-temp
show_help() {
local n d i m x s
set +f --
for cmd in "@PREFIX@/vm-command-"*; do
[ -x "$cmd" ] ||
break
[ -n "${cmd##*.in}" ] ||
continue
set -- "$@" "$cmd"
done
printf 'Usage: %s [options] <command> [<profile>] [command-options]\n' "$PROG"
for cmd; do
VM_HELP=USAGE "$cmd" |
while read -r m; do
printf ' or: %s %s\n' "$PROG" "$m"
done
done
cat <<-EOF
The program to run virtual machines and run programs in their
virtual environment.
Commands:
EOF
for cmd; do
n="${cmd##*/vm-command-}"
m="$(VM_HELP=SUMMARY "$cmd")"
printf ' %10s - %s;\n' "$n" "$m"
done
for cmd; do
n="${cmd##*/vm-command-}"
m="$(VM_HELP=OPTIONS "$cmd")"
[ -n "${m:+1}" ] ||
continue
printf '\n'
printf 'Command options (%s):\n%s\n' "$n" "$m"
done
printf '\n'
printf 'Options:\n'
s=' '
printf '%s\n' $vm_params |
sort |
while read n; do
eval "m=\"\$${n}_description\""
eval "c=\"\${${n}_cmdline-}\""
eval "d=\"\${${n}_default-}\""
[ -n "$c" ] ||
continue
i="${#c}"
x="$s"
while [ $i -gt 0 ]; do
x="${x#?}"
i=$(($i-1))
done
printf ' --%s%s%s.\n' "$c" "$x" "$m"
[ -z "$d" ] ||
printf ' %s%s;\n' "$s" "(default: '$d')"
printf '\n'
done
cat <<EOF
--dry-run Show final qemu command;
-v, --verbose Print a message for each action;
-V, --version Print program version and exit;
-h, --help Show this text and exit.
Report bugs to author.
EOF
exit
}
print_version() {
cat <<-EOF
$PROG version @VERSION@
Written by Alexey Gladkov <gladkov.alexey@gmail.com>
Copyright (C) 2015 Alexey Gladkov <gladkov.alexey@gmail.com>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
exit
}
getopt_args=
generate_getopt_args() {
local n c
for n in $vm_params; do
eval "c=\"\${${n}_cmdline-}\""
[ -z "$c" ] ||
getopt_args="$getopt_args$c:,"
done
}
vm_params=' '
parameter() {
local vartype varname cfgname cmdline default description
varname="$1"; shift
vartype="$1"; shift
cfgname="$1"; shift
cmdline="$1"; shift
default="$1"; shift
description="$1"; shift
vm_params="$vm_params$varname "
[ -z "$cmdline" ] ||
eval "export ${varname}_cmdline=\"\$cmdline\""
[ -z "$cfgname" ] ||
eval "export ${varname}_cfgname=\"\$cfgname\""
eval "export ${varname}_default=\"$default\""
eval "export ${varname}_type=\"\$vartype\""
eval "export ${varname}_description=\"\$description\""
}
set_cmdline_variable() {
local c n
for n in $vm_params; do
eval "c=\"\${${n}_cmdline-}\""
if [ "$c" = "$1" ]; then
eval "export ${n}=\"\$2\""
break
fi
done
}
. "@PREFIX@/vm-options"
GETOPT_ALLOW_UNKNOWN=1
TEMP=`generate_getopt_args; getopt -n $PROG -o 'h,v,V' -l "${getopt_args}dry-run,no-check-kernel-config,help,verbose,version" -- "$@"` ||
show_usage
eval set -- "$TEMP"
vm_dryrun=
while :; do
case "$1" in
--dry-run)
vm_dryrun=echo
;;
--no-check-kernel-config)
no_check_kernel_config=1
;;
-v|--verbose) verbose=-v
;;
-h|--help) show_help
;;
-V|--version) print_version
;;
--[a-z]*)
set_cmdline_variable "${1#--}" "$2"
shift
;;
--) shift; break
;;
esac
shift
done
[ "$#" -ge 1 ] ||
show_usage "More arguments required."
command="$1"; shift
[ -x "@PREFIX@/vm-command-$command" ] ||
show_usage "Command unknown: $command"
vm_config_file="${VM_CONFIG:-$HOME/.vmconfig}"
verbose "Config file: $vm_config_file"
cwddir="$(readlink -ev .)"
workdir=
create_temporary workdir
export vm_params vm_dryrun
export vm_config_file
export verbose cwddir
unset VM_HELP
. "@PREFIX@/vm-command-$command"