3 ##########################################
4 # WARNING WARNING WARNING WARNING
6 # This script is an example to start a new AGL XDS container
8 # You should customize it to fit your environment and in particular
9 # adjust the paths and permissions where needed.
11 # Note that sharing volumes with host system is not mandatory: it
12 # was just added for performances reasons: building from a SSD is
13 # just faster than using the container filesystem: that's why /xdt is
14 # mounted from there. Same applies to ~/mirror and ~/share, which are
15 # just 2 convenient folders to store reference build caches (used in prepare_meta script)
17 ##########################################
19 REGISTRY=docker.automotivelinux.org
25 # ---------------------------------------------------
26 # --- computed - don't touch !
27 # ---------------------------------------------------
30 DEFIMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
33 echo "Usage: $(basename $0) [-h|--help] [-fr] [-id <instance container ID>] "
34 echo " [-nc] [-nuu] [-v|--volume <inpath:outpath>] [image name]"
35 echo "Image name is optional; 'docker images' is used by default to get image"
40 echo " -fr | --force-restart Force restart of xds-server service"
41 echo " -id Instance ID used to build container name, a positive integer (0,1,2,...)"
42 echo " -nuu | --no-uid-update Don't update user/group id within docker"
43 echo " -v | --volume Additional docker volume to bind, syntax is -v /InDockerPath:/HostPath "
53 while [ $# -ne 0 ]; do
69 if [[ "$1" =~ .*:.* ]]; then
70 USER_VOLUME_OPTION="-v $1"
72 echo "Invalid volume option, format must be /InDockerPath:/hostPath"
81 if [[ "$1" =~ ^[\.0-9]+$ ]]; then
82 IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$1
91 [ "$ID" = "" ] && ID=0
93 # Dynamically retrieve image name
94 if [ "$IMAGE" = "" ]; then
96 VERSION_LIST=$(docker images $REGISTRY/$REPO/$NAME-$FLAVOUR --format '{{.Tag}}')
97 VER_NUM=$(echo "$VERSION_LIST" | wc -l)
98 if [ $VER_NUM -gt 1 ]; then
99 echo "ERROR: more than one xds image found, please set explicitly the image to use !"
100 echo "List of found images:"
103 elif [ $VER_NUM -lt 1 ]; then
104 echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
107 if [ "$VERSION_LIST" = "" ]; then
108 echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
113 IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION_LIST
117 echo "Using instance ID #$ID (user $(id -un))"
119 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
121 docker ps -a |grep "$NAME" > /dev/null
122 [ "$?" = "0" ] && { echo "Image name already exist ! (use -h option to read help)"; exit 1; }
124 XDS_WKS=$HOME/xds-workspace
125 XDTDIR=$XDS_WKS/.xdt_$ID
127 SSH_PORT=$((2222 + ID))
128 WWW_PORT=$((8000 + ID))
129 BOOT_PORT=$((69 + ID))
130 NBD_PORT=$((10809 + ID))
132 # Delete container on error
134 trap "cleanExit" 0 1 2 15
137 if [ "$creation_done" != "true" -a "$NO_CLEANUP" != "true" ]; then
138 docker rm -f "${NAME}" > /dev/null 2>&1
142 ### Create the new container
143 mkdir -p $XDS_WKS $XDTDIR || exit 1
145 --publish=${SSH_PORT}:22 \
146 --publish=${WWW_PORT}:8000 \
147 --publish=${BOOT_PORT}:69/udp \
148 --publish=${NBD_PORT}:10809 \
150 --hostname="$NAME" --name="$NAME" \
151 --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
152 -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \
154 $USER_VOLUME_OPTION \
156 if [ "$?" != "0" ]; then
157 echo "An error was encountered while creating docker container."
162 echo "Copying your identity to container $NAME"
163 echo -n wait ssh service .
167 while [ $res -ne 0 ] && [ $count -le $max ]; do
169 docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2
172 count=$(expr $count + 1);
176 ssh-keygen -R [localhost]:$SSH_PORT -f ~/.ssh/known_hosts
177 docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh"
178 docker cp ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys
179 docker exec ${NAME} bash -c "chown $DOCKER_USER:$DOCKER_USER -R /home/$DOCKER_USER/.ssh ;chmod 0700 /home/$DOCKER_USER/.ssh; chmod 0600 /home/$DOCKER_USER/.ssh/*"
180 ssh -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit
182 echo "You can now login using:"
183 echo " ssh -p $SSH_PORT $DOCKER_USER@localhost"
187 if ($UPDATE_UID); then
188 echo -n "Setup docker user and group id to match yours "
190 docker exec -t ${NAME} bash -c "/bin/loginctl kill-user devel"
194 while [ $res -ne 1 ] && [ $count -le $max ]; do
196 docker exec ${NAME} bash -c "loginctl user-status devel |grep sd-pam" 2>/dev/null 1>&2
199 count=$(expr $count + 1);
205 docker exec -t ${NAME} bash -c "id $(id -u)" > /dev/null 2>&1
206 if [ "$?" = "0" -a "$(id -u)" != "1664" ]; then
207 echo "Cannot set docker devel user id to your id: conflict id $(id -u) !"
210 docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER" || exit 1
214 docker exec -t ${NAME} bash -c "grep $(id -g) /etc/group" > /dev/null 2>&1
215 if [ "$?" = "0" ]; then
216 docker exec -t ${NAME} bash -c "usermod -g $(id -g) $DOCKER_USER" || exit 1
218 docker exec -t ${NAME} bash -c "groupmod -g $(id -g) $DOCKER_USER" || exit 1
222 docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER" || exit 1
224 docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /tmp/xds*"
226 docker exec -t ${NAME} bash -c "systemctl start autologin"
228 ssh -p $SSH_PORT $DOCKER_USER@localhost "systemctl --user start xds-server" || exit 1
230 docker restart ${NAME}
235 ### Force xds-server restart
236 if ($FORCE_RESTART); then
237 echo "Restart xds-server..."
238 ssh -p $SSH_PORT $DOCKER_USER@localhost -- "systemctl --user restart xds-server" || exit 1
241 echo "Done, docker container $NAME is ready to be used."