-
Notifications
You must be signed in to change notification settings - Fork 17
/
example_wrapper.sh
executable file
·211 lines (163 loc) · 5.35 KB
/
example_wrapper.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
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
#!/bin/bash
set -e
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
trap 'echo "$0: \"${last_command}\" command failed with exit code $?"' ERR
## | ------------------- configure the paths ------------------ |
# Change the following paths when moving the script and the folders around
# get the path to the current directory
MRS_APPTAINER_PATH=`dirname "$0"`
MRS_APPTAINER_PATH=`( cd "$MRS_APPTAINER_PATH" && pwd )`
# alternatively, set it directly
# MRS_APPTAINER_PATH=$HOME/git/mrs_apptainer
# define paths to the subfolders
IMAGES_PATH="$MRS_APPTAINER_PATH/images"
OVERLAYS_PATH="$MRS_APPTAINER_PATH/overlays"
MOUNT_PATH="$MRS_APPTAINER_PATH/mount"
## | ----------------------- user config ---------------------- |
# use <file>.sif for normal container
# use <folder>/ for sandbox container
CONTAINER_NAME="mrs_uav_system.sif"
OVERLAY_NAME="mrs_uav_system.img"
CONTAINED=true # true: will isolate from the HOST's home
CLEAN_ENV=true # true: will clean the shell environment before runnning container
USE_NVIDIA=false # true: will tell Apptainer that it should use nvidia graphics. Does not work every time.
# the following are mutually exclusive
OVERLAY=false # true: will load persistant overlay (overlay can be created with scripts/create_overlay.sh)
WRITABLE=false # true: will run it as --writable (works with --sandbox containers, image can be converted with scripts/convert_sandbox.sh)
FAKEROOT=false # true: emulate root inside the container
# defines what should be mounted from the host to the container
# [TYPE], [SOURCE (host)], [DESTINATION (container)]
# - !!! the folders are not being mounted when running with sudo
MOUNTS=(
# mount the custom user workspace into the container
# HOST PATH CONTAINER PATH
"type=bind" "$MRS_APPTAINER_PATH/user_ros_workspace" "/home/$USER/user_ros_workspace"
# mount the MRS shell additions into the container, DO NOT MODIFY
"type=bind" "$MOUNT_PATH" "/opt/mrs/host"
# mount folders to facilitate Xserver piping
"type=bind" "/tmp/.X11-unix" "/tmp/.X11-unix"
"type=bind" "/dev/dri" "/dev/dri"
"type=bind" "$HOME/.Xauthority" "/home/$USER/.Xauthority"
)
## | ------------------ advanced user config ------------------ |
# not supposed to be changed by a normal user
DEBUG=false # true: print the apptainer command instead of running it
KEEP_ROOT_PRIVS=false # true: let root keep privileges in the container
DETACH_TMP=true # true: do NOT mount host's /tmp
## | --------------------- user config end -------------------- |
if [ -z "$1" ]; then
ACTION="run"
else
ACTION=${1}
fi
CONTAINER_PATH=$IMAGES_PATH/$CONTAINER_NAME
if $OVERLAY; then
if [ ! -e $OVERLAYS_PATH/$OVERLAY_NAME ]; then
echo "Overlay file does not exist, initialize it with the 'create_fs_overlay.sh' script"
exit 1
fi
OVERLAY_ARG="-o $OVERLAYS_PATH/$OVERLAY_NAME"
$DEBUG && echo "Debug: using overlay"
else
OVERLAY_ARG=""
fi
if $CONTAINED; then
CONTAINED_ARG="--home /tmp/apptainer/home:/home/$USER"
$DEBUG && echo "Debug: running as contained"
else
CONTAINED_ARG=""
fi
if $WRITABLE; then
WRITABLE_ARG="--writable"
DETACH_TMP=false
FAKEROOT=true
$DEBUG && echo "Debug: running as writable"
else
WRITABLE_ARG=""
fi
if $KEEP_ROOT_PRIVS; then
KEEP_ROOT_PRIVS_ARG="--keep-privs"
$DEBUG && echo "Debug: keep root privs"
else
KEEP_ROOT_PRIVS_ARG=""
fi
if $FAKEROOT; then
FAKE_ROOT_ARG="--fakeroot"
$DEBUG && echo "Debug: fake root"
else
FAKE_ROOT_ARG=""
fi
if $CLEAN_ENV; then
CLEAN_ENV_ARG="-e"
$DEBUG && echo "Debug: clean env"
else
CLEAN_ENV_ARG=""
fi
# if we want nvidia, add the "--nv" arg
if $USE_NVIDIA; then
NVIDIA_ARG="--nv"
else
NVIDIA_ARG=""
fi
if $DETACH_TMP; then
TMP_PATH="/tmp/apptainer/tmp"
DETACH_TMP_ARG="--bind $TMP_PATH:/tmp"
$DEBUG && echo "Debug: detaching tmp from the host"
else
DETACH_TMP_ARG=""
fi
if $DEBUG; then
EXEC_CMD="echo"
else
EXEC_CMD="eval"
fi
MOUNT_ARG=""
if ! $WRITABLE; then
# prepare the mounting points, resolve the full paths
for ((i=0; i < ${#MOUNTS[*]}; i++));
do
((i%3==0)) && TYPE[$i/3]="${MOUNTS[$i]}"
((i%3==1)) && SOURCE[$i/3]="${MOUNTS[$i]}"
((i%3==2)) && DESTINATION[$i/3]="${MOUNTS[$i]}"
done
for ((i=0; i < ${#TYPE[*]}; i++)); do
if test -e ${SOURCE[$i]}; then
FULL_SOURCE=$( realpath -e ${SOURCE[$i]} )
FULL_DESTINATION=$( realpath -ms ${DESTINATION[$i]} )
MOUNT_ARG="$MOUNT_ARG --mount ${TYPE[$i]},source=$FULL_SOURCE,destination=$FULL_DESTINATION"
else
echo "Error while mounting '${SOURCE[$i]}', the path does not exist".
fi
done
fi
if [[ "$ACTION" == "run" ]]; then
[ ! -z "$@" ] && shift
CMD="$@"
elif [[ $ACTION == "exec" ]]; then
shift
CMD="/bin/bash -c '${@}'"
elif [[ $ACTION == "shell" ]]; then
CMD=""
else
echo "Action is missing"
exit 1
fi
# create tmp folder for apptainer in host's tmp
[ ! -e /tmp/apptainer/tmp ] && mkdir -p /tmp/apptainer/tmp
[ ! -e /tmp/apptainer/home ] && mkdir -p /tmp/apptainer/home
# this will set $DISPLAY in the container to the same value as on your host machine
export APPTAINERENV_DISPLAY=$DISPLAY
xhost + > /dev/null 2>&1
$EXEC_CMD apptainer $ACTION \
$NVIDIA_ARG \
$OVERLAY_ARG \
$CONTAINED_ARG \
$WRITABLE_ARG \
$CLEAN_ENV_ARG \
$FAKE_ROOT_ARG \
$KEEP_ROOT_PRIVS_ARG \
$MOUNT_ARG \
$DETACH_TMP_ARG \
$CONTAINER_PATH \
$CMD
xhost - > /dev/null 2>&1