-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-vm
More file actions
executable file
·64 lines (56 loc) · 1.44 KB
/
test-vm
File metadata and controls
executable file
·64 lines (56 loc) · 1.44 KB
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
#!/bin/sh
SSH_IN=false
RUN=true
ARGS=""
while (( "$#" )); do
case "$1" in
-b|--build)
RUN=false
shift
;;
-t|--trace)
ARGS="$ARGS --show-trace"
shift
;;
-u|--update) # Update dependencies
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
nix flake lock --update-input $2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-s|--ssh) # SSH into the machine
SSH_IN=true
export QEMU_NET_OPTS=hostfwd=tcp::2221-:22
shift
;;
--) # Forward all further arguments to nixos-rebuild
shift
ARGS="$ARGS $@"
break
;;
-*|--*=|*) # unsupported flags
echo "Error: Unsupported flag or argument $1" >&2
exit 1
;;
esac
done
alias build-vm="sudo nixos-rebuild --flake '.#test-vm' build-vm $ARGS"
if build-vm 2>&1 | tee /tmp/test-vm-output && $RUN
then
# Remove all files to remove state, asks for confirmation
rm -i ./*.qcow2
# Parse path to start VM
VM_CMD=$(cat /tmp/test-vm-output | tail -n 1 | awk 'END { print $NF}')
# Output diagnostic info
echo
echo "Running virtual machine: $VM_CMD"
$SSH_IN && echo "To connect via SSH use: ssh USER@localhost -p 2221"
echo
# Run the VM
$VM_CMD
# Remove all files to remove state, asks for confirmation above 3 files
rm -I ./*.qcow2
fi