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 IMAGES_LIST=$(docker images $REGISTRY/$REPO/$NAME-$FLAVOUR:* --format '{{.Tag}}')
97 VER_NUM=$(echo "$IMAGES_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 ! List of found images:"
102 elif [ $VER_NUM -lt 1 ]; then
103 echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
106 if [ "$IMAGES_LIST" = "" ]; then
107 echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
112 IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
116 echo "Using instance ID #$ID (user $(id -un))"
118 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
120 docker ps -a |grep "$NAME" > /dev/null
121 [ "$?" = "0" ] && { echo "Image name already exist ! (use -h option to read help)"; exit 1; }
123 XDS_WKS=$HOME/xds-workspace
124 XDTDIR=$XDS_WKS/.xdt_$ID
126 SSH_PORT=$((2222 + ID))
127 WWW_PORT=$((8000 + ID))
128 BOOT_PORT=$((69 + ID))
129 NBD_PORT=$((10809 + ID))
131 # Delete container on error
133 trap "cleanExit" 0 1 2 15
136 if [ "$creation_done" != "true" -a "$NO_CLEANUP" != "true" ]; then
137 docker rm -f "${NAME}" > /dev/null 2>&1
141 ### Create the new container
142 mkdir -p $XDS_WKS $XDTDIR || exit 1
144 --publish=${SSH_PORT}:22 \
145 --publish=${WWW_PORT}:8000 \
146 --publish=${BOOT_PORT}:69/udp \
147 --publish=${NBD_PORT}:10809 \
149 --hostname="$NAME" --name="$NAME" \
150 --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
151 -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \
153 $USER_VOLUME_OPTION \
155 if [ "$?" != "0" ]; then
156 echo "An error was encountered while creating docker container."
161 echo "Copying your identity to container $NAME"
162 echo -n wait ssh service .
166 while [ $res -ne 0 ] && [ $count -le $max ]; do
168 docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2
171 count=$(expr $count + 1);
175 ssh-keygen -R [localhost]:$SSH_PORT -f ~/.ssh/known_hosts
176 docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh"
177 docker cp ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys
178 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/*"
179 ssh -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit
181 echo "You can now login using:"
182 echo " ssh -p $SSH_PORT $DOCKER_USER@localhost"
186 if ($UPDATE_UID); then
187 echo -n "Setup docker user and group id to match yours "
189 docker exec -t ${NAME} bash -c "/bin/loginctl kill-user devel"
193 while [ $res -ne 1 ] && [ $count -le $max ]; do
195 docker exec ${NAME} bash -c "loginctl user-status devel |grep sd-pam" 2>/dev/null 1>&2
198 count=$(expr $count + 1);
204 docker exec -t ${NAME} bash -c "id $(id -u)" > /dev/null 2>&1
205 if [ "$?" = "0" -a "$(id -u)" != "1664" ]; then
206 echo "Cannot set docker devel user id to your id: conflict id $(id -u) !"
209 docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER" || exit 1
213 docker exec -t ${NAME} bash -c "grep $(id -g) /etc/group" > /dev/null 2>&1
214 if [ "$?" = "0" ]; then
215 docker exec -t ${NAME} bash -c "usermod -g $(id -g) $DOCKER_USER" || exit 1
217 docker exec -t ${NAME} bash -c "groupmod -g $(id -g) $DOCKER_USER" || exit 1
221 docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER" || exit 1
223 docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /tmp/xds*"
225 docker exec -t ${NAME} bash -c "systemctl start autologin"
227 ssh -p $SSH_PORT $DOCKER_USER@localhost -- "systemctl --user start xds-server" || exit 1
229 docker restart ${NAME}
234 ### Force xds-server restart
235 if ($FORCE_RESTART); then
236 echo "Restart xds-server..."
237 ssh -p $SSH_PORT $DOCKER_USER@localhost -- "systemctl --user restart xds-server" || exit 1
240 echo "Done, docker container $NAME is ready to be used."