aboutsummaryrefslogtreecommitdiff
path: root/bin/vm
blob: 1b7c878f5cf650ae37401644954705e521631ca7 (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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/sh
set -eu


usage() {
	cat <<-'EOF'
		Usage:
		  vm [-G] [-i IMAGE ] [-n] [-p PORTMAP ] [-S] [-v] ACTION [OS [-- QEMU_OPTIONS...]]
		  vm -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -G              use graphics
		  -i IMAGE        provide a custom QCoW2 image file
		  -m MEMORY       memory (default: 1G)
		  -n              dry-run: don't execute any code, assumes -v
		  -S              disable snapshots
		  -p PORTMAP      map the host port $HOSTP to the guest port
		                  $GUESTP using the $HOSTP:$GUESTP syntax
		  -v              verbose mode
		  -h, --help      show this message

		  ACTION          one of:
		                  - up
		                  - down
		                  - status
		                  - ls-remote
		                  - download
		  OS              the name or prefix of the OS to be acted upon
		  QEMU_OPTIONS    command line options to be given verbatim to QEMU


		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`.

		If the given OS name is a unique prefix, than it is enough to
		guess the rest of the name, i.e. if there is only only type of
		"Fedora" VM, fedora-amd64-headless, than saying "fedora" is enough.
		Otherwise, a sufficiently uniqe prefix is required, like
		"fedora-amd64", when there are more than one architecture of Fedora
		VMs.


		Examples:

		  Start the VM for Alpine in verbose mode, with guest port 8000
		  becoming available to the host via port 5555:

		    $ vm -v -p 5555:8000 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-amd64-headless	up
		    slackware-amd64-plasma	down
		    freebsd-amd64-headless	up


		  Create a new VM, using graphics and verbose mode:

		    $ qemu-img create -f qcow2 $XDG_STATE_HOME/vm/qemu/debian-amd64-headless.qcow2 32G
		    $ vm -vSG up debian -- -cdrom ~/Downloads/ISO/debian-11.5.0-amd64-netinst.iso


		  List images available on the remote, and download one of them:

		    $ vm ls-remote
		    debian-i386-headless
		    fedora-aarch64-headless
		    $ vm download fedora-aarch64-headless
	EOF
}


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

IMAGE=''
DRY_RUN=false
GRAPHICS=false
MEMORY=1G
PORTMAPS=''
SNAPSHOT=true
VERBOSE=false
while getopts 'Gi:m:np:Svh' flag; do
	case "$flag" in
		G)
			GRAPHICS=true
			;;
		(i)
			IMAGE="$OPTARG"
			;;
		(m)
			MEMORY="$OPTARG"
			;;
		(n)
			DRY_RUN=true
			VERBOSE=true
			;;
		(p)
			PORTMAPS="$PORTMAPS $OPTARG"
			;;
		S)
			SNAPSHOT=false
			;;
		(v)
			VERBOSE=true
			;;
		(h)
			usage
			help
			exit
			;;
		(*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))



REMOTE='https://euandre.org/s/vm'
QCOW_DIR="${XDG_STATE_HOME:-$HOME/.local/state}"/vm/qemu
RUNDIR="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/vm"
LOGS="${XDG_LOG_HOME:-$HOME/.local/var/log}"/vm/vm.log
mkdir -p "$RUNDIR" "$QCOW_DIR" "$XDG_DATA_HOME"/vm "$(dirname "$LOGS")"

guess_name() {
	PREFIX="$1"
	IMAGES="$(find "$QCOW_DIR" '(' -type f -or -type l ')' -name "${PREFIX}*")"
	COUNT="$(printf '%s\n' "$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
	if [ -z "$IMAGES" ]; then
		printf 'Cannot guess name with the given prefix: "%s".\n' "$PREFIX" >&2
		printf '\nNo possibilities were found.\n' >&2
		exit 2
	fi
	printf '%s\n' "$(basename "$IMAGES" .qcow2)"
}

guess_arch() {
	NAME="$(printf '%s\n' "$1" | cut -d- -f2)"
	case "$NAME" in
		amd64)
			printf 'x86_64'
			;;
		(*)
			printf '%s' "$NAME"
			;;
	esac
}

hostfwd() {
	for m in "$@"; do
		HOST="$( printf '%s\n' "$m" | cut -d: -f1)"
		GUEST="$(printf '%s\n' "$m" | cut -d: -f2)"
		printf ',hostfwd=tcp::%s-:%s' "$HOST" "$GUEST"
	done
}

write_ssh_config() {
	for port in "$RUNDIR"/*.ssh; do
		if [ ! -e "$port" ]; then
			break
		fi
		NAME="$(basename "$port" .ssh)"
		PORT="$(cat "$port")"
		cat <<-EOF
			Host $NAME
			  User vm
			  HostName localhost
			  Port $PORT
			  UserKnownHostsFile   /dev/null
			  GlobalKnownHostsFile /dev/null
			  StrictHostKeyChecking no

		EOF
	done | sponge "$XDG_DATA_HOME"/vm/ssh.conf
}

write_ssh_config


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

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


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)"
			PORTS=''
			if [ -e "$RUNDIR/$NAME.pid" ]; then
				STATUS=up
				PORTS="$(sed 's/ /,/g' "$RUNDIR/$NAME.ports")"
			else
				STATUS=down
			fi

			SNAPSHOT="$(cat "$RUNDIR/$NAME.snapshot" 2>/dev/null ||:)"
			if [ "$SNAPSHOT" = true ] && [ "$STATUS" = 'up' ]; then
				SNAPSHOT='snapshot'
			else
				SNAPSHOT=''
			fi

			printf '%s\t%s\t%s\t%s\n' "$NAME" "$STATUS" "$PORTS" "$SNAPSHOT"
		done
		;;
	up)
		eval "$(assert-arg -- "$OS" 'OS')"
		shift
		if [ "${1:-}" = '--' ]; then
			shift
		fi

		OS="$(guess_name "$OS")"
		ARCH="$(guess_arch "$OS")"
		PID_F="$RUNDIR/$OS.pid"
		SSH_PORT_F="$RUNDIR/$OS.ssh"
		PORTMAPS_F="$RUNDIR/$OS.ports"
		SNAPSHOT_F="$RUNDIR/$OS.snapshot"

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

		if [ -z "$IMAGE" ]; then
			IMAGE="$QCOW_DIR"/"$OS".qcow2
		fi

		SSH_PORT="$(free-port)"
		PORTMAPS="$SSH_PORT:22${PORTMAPS}"
		# shellcheck disable=SC2086
		HOSTFWD="$(hostfwd $PORTMAPS)"

		# shellcheck disable=2086
		set -- qemu-system-"$ARCH" \
			-m "$MEMORY"                              \
			-nic user,model=virtio"$HOSTFWD"          \
			-drive file="$IMAGE",media=disk,if=virtio \
			-enable-kvm                               \
			$FLAGS "$@"

		if [ "$DRY_RUN" = true ]; then
			printf '%s\n' "$@"
			exit
		fi

		if [ "$VERBOSE" = true ]; then
			set -x
		fi

		"$@" 1>>"$LOGS" 2>&1 &
		PID=$!
		set +x
		printf '%s' "$PID"      > "$PID_F"
		printf '%s' "$SSH_PORT" > "$SSH_PORT_F"
		printf '%s' "$PORTMAPS" > "$PORTMAPS_F"
		printf '%s' "$SNAPSHOT" > "$SNAPSHOT_F"

		write_ssh_config
		;;
	down)
		eval "$(assert-arg -- "$OS" 'OS')"
		shift
		if [ "${1:-}" = '--' ]; then
			shift
		fi

		OS="$(guess_name "$OS")"
		PID_F="$RUNDIR/$OS.pid"
		SSH_PORT_F="$RUNDIR/$OS.ssh"
		PORTMAPS_F="$RUNDIR/$OS.port"

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

		PID="$(cat "$PID_F")"

		if [ "$DRY_RUN" = true ]; then
			echo rm -f "$PID_F" "$SSH_PORT_F" "$PORTMAPS_F"
			echo kill "$PID"
			exit
		fi


		if [ "$VERBOSE" = true ]; then
			set -x
		fi
		rm -f "$PID_F" "$SSH_PORT_F" "$PORTMAPS_F"
		kill "$PID"
		set +x

		write_ssh_config
		;;
	ls-remote)
		wget -qO- "$REMOTE"/vms.txt
		;;
	download)
		eval "$(assert-arg -- "$OS" 'OS')"
		wget -cO "$QCOW_DIR/$OS".qcow2 "$REMOTE"/qemu/"$OS".qcow2
		;;
	*)
		printf 'Unrecognized action: "%s".\n\n' "$ACTION" >&2
		usage >&2
		exit 2
esac