-
Notifications
You must be signed in to change notification settings - Fork 4
/
vmctl
executable file
·119 lines (91 loc) · 2.59 KB
/
vmctl
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
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
#
# Written by Klaus Jensen <[email protected]>
if [[ ${BASH_VERSINFO[0]} -lt 4 ]]; then
echo "error: vmctl requires bash version 4 or later" >&2
exit 1
fi
set -euo pipefail
case "$(uname)" in
Darwin )
PATH="${HOMEBREW_PREFIX}/opt/gnu-getopt/bin:${PATH}"
;;
esac
# script basename and basedir
BASENAME="$(basename "${BASH_SOURCE[0]}")"
BASEDIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
#shellcheck source=common/rc
source "${BASEDIR}/common/rc"
#shellcheck source=common/defaults
source "${BASEDIR}/common/defaults"
USAGE="usage: ${BASENAME} -c CONFIG COMMAND [OPTION...]
QEMU Testing Galore!
See \`vmctl -c CONFIG <command> --help\` for usage on individual commands.
Options:
-c, --config virtual machine configuration to use; required for most
commands
-b, --basedir set the base machine directory; defaults to \$VMCTL_VMROOT
(or the current directory if unset)
Commands:
run launch a vm configuration
ssh ssh into a running VM
log show logs from a VM
console attach to the serial console of a running VM
monitor attach to the QEMU monitor of a running VM
poweroff gracefully shutdown a running VM (if supported)
kill kill a VM (monitor quit)
vars return output suitable for \`eval\`, specifying various
variables about the setup"
_main() {
local short="b:c:h"
local long="basedir:,config:,help"
if ! tmp=$(getopt -o "+${short}" --long "$long" -n "${BASH_SOURCE[0]}" -- "$@"); then
exit 1
fi
eval set -- "$tmp"
unset tmp
while true; do
case "$1" in
'-c' | '--config' )
if [[ ! -r "$2" ]]; then
_fatal 1 "could not read vm configuration '$2'"
fi
VMCONFIG="$2"; shift 2
;;
'-b' | '--basedir' )
if [[ ! -d "$2" ]]; then
_fatal 1 "invalid base directory '$2'"
fi
VMCTL_VMROOT="$2"; shift 2
;;
'-h' | '--help' )
_usage "$USAGE" 0
;;
'--' )
shift; break
;;
* )
exit 1
;;
esac
done
if [[ $# -lt 1 ]]; then
_usage "$USAGE" 1
fi
subcmd="$1"; shift
# handle aliases
case "$subcmd" in
"stop" )
subcmd="poweroff"
;;
esac
if [[ ! -f "${BASEDIR}/cmd/${subcmd}" ]]; then
_usage "$USAGE" 1
fi
#shellcheck disable=SC1090
source "${BASEDIR}/cmd/${subcmd}"
"_${subcmd}" "$@"
}
_main "$@"