-
Notifications
You must be signed in to change notification settings - Fork 3
/
docker-entrypoint.sh
executable file
·100 lines (81 loc) · 1.64 KB
/
docker-entrypoint.sh
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
#!/usr/bin/env bash
set -eo pipefail
[[ -z "$TRACE" ]] || set -x
vars() {
export SSH_AUTH_SOCK=/tmp/ssh-auth-sock
}
_start() {
exec cat<<'_start'
#!/usr/bin/env bash
set -eo pipefail
[[ -z "$TRACE" ]] || set -x
vars() {
true
}
fail() {
echo "$@"
exit 1
}
check_tool() {
tool=$1; shift
command -v "$tool" >/dev/null || fail "Please install $tool first!"
}
available_interface() {
netstat -in | grep -E "^e" | grep -v "Link#" | awk '{print $1}' | tail -1
}
set_local_ip() {
XIP=$(ifconfig $(available_interface) | grep "inet " | awk '{print $2}')
}
forward_x_over_ip() {
XDISPLAY=$(( RANDOM % 512 ))
XPORT=$(( 6000 + XDISPLAY ))
socat TCP-LISTEN:$XPORT,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\" &
XDISPLAY_PID=$!
}
forward_sshagent_over_ip() {
XSSH=$(( 7000 + XDISPLAY ))
socat TCP-LISTEN:$XSSH,reuseaddr,fork UNIX-CLIENT:\"$SSH_AUTH_SOCK\" &
XSSH_PID=$!
}
install_trap() {
trap "kill $XSSH_PID $XDISPLAY_PID" EXIT
}
run_virtmanager() {
docker run \
--rm \
--volume=$HOME/.ssh:/root/.ssh \
--volume=$HOME/.config:/root/.config \
--env=TRACE \
--env=DISPLAY=$XIP:$XDISPLAY \
--env=SSH_AGENT_SOCKET=$XIP:$XSSH \
steigr/virt-manager run
}
main() {
check_tool ifconfig
check_tool socat
check_tool grep
check_tool awk
set_local_ip
forward_x_over_ip
forward_sshagent_over_ip
install_trap
run_virtmanager
}
vars
main "$@"
_start
}
_run() {
rm "$0"
start-stop-daemon -S -x /usr/bin/socat -b -- UNIX-LISTEN:$SSH_AUTH_SOCK,fork TCP:$SSH_AGENT_SOCKET
exec virt-manager --no-fork
}
main() {
cmd=$1
[[ "$#" -le 0 ]] || shift
[[ "$cmd" != "run" ]] || _run "$@"
[[ "$cmd" != "start" ]] || _start "$@"
exec "$cmd" "$@"
}
vars
main "$@"