forked from iic-jku/IIC-OSIC-TOOLS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_x.sh
executable file
·154 lines (140 loc) · 4.73 KB
/
start_x.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
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
#!/bin/bash
# Harald Pretl & Georg Zachl, IIC, JKU, 2022
if [ -n "${DRY_RUN}" ]; then
echo "This is a dry run, all commands will be printed to the shell (Commands printed but not executed are marked with $)!"
ECHO_IF_DRY_RUN="echo $"
fi
if [ -z ${CONTAINER_NAME+z} ]; then
CONTAINER_NAME="iic-osic-tools_xserver_uid_"$(id -u)
fi
# Check if the container exists and if it is running.
if [ "$(docker ps -q -f name="${CONTAINER_NAME}")" ]; then
echo "Container is running! (Hint: It can also be stopped with \"docker stop ${CONTAINER_NAME}\" and removed with \"docker rm ${CONTAINER_NAME}\" if required.)"
echo -n "Press \"s\" to stop, and \"r\" to stop & remove: "
read -n 1 k <&1
echo ""
if [[ $k = s ]] ; then
${ECHO_IF_DRY_RUN} docker stop "${CONTAINER_NAME}"
elif [[ $k = r ]] ; then
${ECHO_IF_DRY_RUN} docker stop "${CONTAINER_NAME}"
${ECHO_IF_DRY_RUN} docker rm "${CONTAINER_NAME}"
fi
fi
# SET YOU DESIGN PATH RIGHT!
if [ -z ${DESIGNS+z} ]; then
DESIGNS=$HOME/eda/designs
if [ ! -d "$DESIGNS" ]; then
${ECHO_IF_DRY_RUN} mkdir -p "$DESIGNS"
fi
echo "Design directory auto-set to $DESIGNS"
fi
if [ -z ${DOCKER_USER+z} ]; then
DOCKER_USER="hpretl"
fi
if [ -z ${DOCKER_IMAGE+z} ]; then
DOCKER_IMAGE="iic-osic-tools"
fi
if [ -z ${DOCKER_TAG+z} ]; then
DOCKER_TAG="latest"
fi
if [ -z ${CONTAINER_USER+z} ]; then
CONTAINER_USER=$(id -u)
fi
if [ -z ${CONTAINER_GROUP+z} ]; then
CONTAINER_GROUP=$(id -g)
fi
PARAMS=""
if [[ "$OSTYPE" == "linux"* ]]; then
echo "Auto detected Linux"
# Should also be a senseful default
if [ -z ${XSOCK+z} ]; then
if [ -d "/tmp/.X11-unix" ]; then
XSOCK="/tmp/.X11-unix"
else
echo "X socket could not be found. Please set manually!"
exit 1
fi
fi
PARAMS="$PARAMS -v $XSOCK:/tmp/.X11-unix:rw"
if [ -z ${DISP+z} ]; then
if [ -z ${DISPLAY+z} ]; then
echo "No DISPLAY set"
exit 1
else
DISP=$DISPLAY
fi
fi
if [ -z ${XAUTH+z} ]; then
# Senseful defaults (uses XAUTHORITY Shell variable if set, or the default .Xauthority -file in the caller home directory)
if [ -z ${XAUTHORITY+z} ]; then
if [ -f "$HOME/.Xauthority" ]; then
XAUTH="$HOME/.Xauthority"
else
echo "Xauthority could not be found. Please set manually!"
exit 1
fi
else
XAUTH=$XAUTHORITY
fi
# Thanks to https://stackoverflow.com/a/25280523
XAUTH_TMP="/tmp/.${CONTAINER_NAME}_xauthority"
#create empty file
${ECHO_IF_DRY_RUN} echo -n > "${XAUTH_TMP}"
if [ -z "${ECHO_IF_DRY_RUN}" ]; then
xauth -f "${XAUTH}" nlist "${DISP}" | sed -e 's/^..../ffff/' | xauth -f "${XAUTH_TMP}" nmerge -
else
${ECHO_IF_DRY_RUN} "xauth -f ${XAUTH} nlist ${DISP} | sed -e 's/^..../ffff/' | xauth -f ${XAUTH_TMP} nmerge -"
fi
XAUTH=${XAUTH_TMP}
if [ -d "/dev/dri" ]; then
echo "/dev/dri detected, forwarding GPU for graphics acceleration."
PARAMS="${PARAMS} --device=/dev/dri:/dev/dri"
else
echo "No /dev/dri detected!"
FORCE_LIBGL_INDIRECT=1
fi
fi
PARAMS="$PARAMS -v $XAUTH:/headless/.xauthority:rw -e XAUTHORITY=/headless/.xauthority"
elif [[ "$OSTYPE" == "darwin"* ]]; then
if [ -z ${DISP+z} ]; then
DISP="host.docker.internal:0"
if [[ $(type -P "xhost") ]]; then
${ECHO_IF_DRY_RUN} xhost +localhost
else
echo "WARNING: xhost could not be found, access control to the X server must be managed manually!"
fi
fi
if [ "$(defaults read org.xquartz.x11 enable_iglx)" = 0 ]; then
${ECHO_IF_DRY_RUN} defaults write org.xquartz.x11 enable_iglx 1
echo "Enabled XQuartz OpenGL for indirect rendering."
echo "WARNING: Please restart XQuartz!"
exit 1
fi
FORCE_LIBGL_INDIRECT=1
else
echo "Not setup for ${OSTYPE}"
exit 1
fi
if [ -n "${FORCE_LIBGL_INDIRECT}" ]; then
echo "Using indirect rendering."
PARAMS="${PARAMS} -e LIBGL_ALWAYS_INDIRECT=1"
fi
# If the container exists but is exited, it can restarted.
if [ "$(docker ps -aq -f name="${CONTAINER_NAME}")" ]; then
echo "Container ${CONTAINER_NAME} exists. (Hint: It can also be restarted with \"docker start ${CONTAINER_NAME}\" or removed with \"docker rm ${CONTAINER_NAME}\" if required.)"
echo -n "Press \"s\" to start, and \"r\" to remove: "
read -n 1 k <&1
echo ""
if [[ $k = s ]] ; then
${ECHO_IF_DRY_RUN} docker start "${CONTAINER_NAME}"
elif [[ $k = r ]] ; then
${ECHO_IF_DRY_RUN} docker rm "${CONTAINER_NAME}"
fi
else
echo "Container does not exist, creating ${CONTAINER_NAME} ..."
# Finally, run the container, sets DISPLAY to the local display number
${ECHO_IF_DRY_RUN} docker pull "${DOCKER_USER}/${DOCKER_IMAGE}:${DOCKER_TAG}"
# Disable SC2086, $PARAMS must be globbed and splitted.
# shellcheck disable=SC2086
${ECHO_IF_DRY_RUN} docker run -d --user "${CONTAINER_USER}:${CONTAINER_GROUP}" -e "DISPLAY=${DISP}" -v "${DESIGNS}:/foss/designs:rw" ${PARAMS} --name "${CONTAINER_NAME}" "${DOCKER_USER}/${DOCKER_IMAGE}:${DOCKER_TAG}"
fi