aboutsummaryrefslogtreecommitdiff
path: root/bin/vm
blob: 52bcce551fad388c37b55a7a2e8eff767cc4dec0 (plain) (blame)
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
#!/bin/sh
set -eu


usage() {
	cat <<-'EOF'
		Usage:
		  vm [-G] [-S] [-v] ACTION [OS]
		  vm -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -G            use graphics
		  -S            don't write to VM image (snapshot on)
		  -v            verbose mode
		  -h, --help    show this message

		  ACTION        one of:
		                - up
		                - down
		                - status
		  OS            the name of the OS to be acted upon


		Manage the state of known virtual machines.

		The VM QCOW2 images are stored under
		$XDG_STATE_HOME/vm/qemu/, as "$OS.qcow2" files.  The PIDs
		of the running images are stored in individual files under
		$XDG_RUNTIME_DIR/vm/pids/, as "$OS.pid" files.

		It also generates an SSH configuration file under
                $XDG_DATA_HOME/vm/ssh.conf to be `Included` by the main
                ~/.ssh/config file, which contains one alias entry for each VM,
                so that one can do a combination of `vm up alpine && ssh alpine`.


		Examples:

		  Start the VM for Alpine:

		    $ vm up alpine


		  Stop the VM for Slackware, which was already down

		    $ vm down slackware
		    The VM for "slackware" is not running, already.


		  List the available VMs, and their current state:

		    $ vm status
		    alpine up
		    slackware down
		    freebsd:up
	EOF
}


for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

GRAPHICS=false
SNAPSHOT=false
VERBOSE=false
while getopts 'GSvh' flag; do
	case "$flag" in
		G)
			GRAPHICS=true
			;;
		S)
			SNAPSHOT=true
			;;
		v)
			VERBOSE=true
			;;
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))



QCOW_DIR="${XDG_STATE_HOME:-$HOME/.local/state}"/vm/qemu
RUNDIR="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/vm"
LOGS="$RUNDIR"/logs
mkdir -p "$RUNDIR" "$QCOW_DIR" "$XDG_DATA_HOME"/vm

guess_name() {
	PREFIX="$1"
	IMAGES="$(find "$QCOW_DIR" -type f -name "${PREFIX}*")"
	COUNT="$(echo "$IMAGES" | wc -l)"
	if [ "$COUNT" != 1 ]; then
		printf 'Cannot guess name with the given prefix: "%s".\n' "$PREFIX" >&2
		printf '\nThe possibilities are:\n' >&2
		printf '%s\n' "$IMAGES" |
			xargs -I% basename % .qcow2 |
			sed 's/^/- /' >&2
		exit 2
	fi
	printf '%s\n' "$(basename "$IMAGES" .qcow2)"
}

write_ssh_config() {
	for port in "$RUNDIR"/*.port; do
		if [ ! -e "$port" ]; then
			break
		fi
		NAME="$(basename "$port" .port)"
		PORT="$(cat "$port")"
		cat <<-EOF
			Host $NAME
			  HostName localhost
			  Port $PORT

		EOF
	done > "$XDG_DATA_HOME"/vm/ssh.conf
}

write_ssh_config


ACTION="${1:-}"
OS="${2:-}"

eval "$(assert-arg "$ACTION" 'ACTION')"


FLAGS=''
if [ "$GRAPHICS" = false ]; then
	FLAGS="$FLAGS -nographic"
else
	FLAGS="$FLAGS -display sdl"
fi
if [ "$SNAPSHOT" = true ]; then
	FLAGS="$FLAGS -snapshot"
fi


case "$ACTION" in
	status)
		for img in "$QCOW_DIR"/*.qcow2; do
			if [ ! -e "$img" ]; then
				break
			fi
			NAME="$(basename "$img" .qcow2)"
			if [ -e "$RUNDIR/$NAME.pid" ]; then
				STATUS=up
			else
				STATUS=down
			fi
			printf '%s\t%s\n' "$NAME" "$STATUS"
		done
		;;
	up)
		eval "$(assert-arg "$OS" 'OS')"
		OS="$(guess_name "$OS")"
		PID_F="$RUNDIR/$OS.pid"
		PORT_F="$RUNDIR/$OS.port"

		if [ -e "$PID_F" ]; then
			printf 'The VM for "%s" is already running with PID %s.\n' \
				"$OS" "$(cat "$PID_F")" >&2
			exit 1
		fi

		PORT="$(free-port)"
		QCOW="$QCOW_DIR"/"$OS".qcow2
		if [ "$VERBOSE" = true ]; then
			set -x
		fi
		# shellcheck disable=2086
		qemu-system-x86_64 \
			-m 1G                                           \
			-nic user,model=virtio,hostfwd=tcp::"$PORT"-:22 \
			-drive file="$QCOW",media=disk,if=virtio        \
			-enable-kvm                                     \
			$FLAGS 1>>"$LOGS" 2>&1 &
		PID=$!
		set +x
		printf '%s' "$PID"  > "$PID_F"
		printf '%s' "$PORT" > "$PORT_F"

		write_ssh_config
		;;
	down)
		eval "$(assert-arg "$OS" 'OS')"
		OS="$(guess_name "$OS")"
		PID_F="$RUNDIR/$OS.pid"
		PORT_F="$RUNDIR/$OS.port"

		if [ ! -e "$PID_F" ]; then
			printf 'The VM for "%s" is not running, already.\n' "$OS" >&2
			exit 1
		fi

		PID="$(cat "$PID_F")"
		rm -f "$PID_F" "$PORT_F"
		kill "$PID"

		write_ssh_config
		;;
	*)
		printf 'Unrecognized action: "%s".\n\n' "$ACTION" >&2
		usage >&2
		exit 2
esac