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 CURDIR=$(cd $(dirname $0) && pwd -P)
21 REGISTRY=docker.automotivelinux.org
27 # ---------------------------------------------------
28 # --- computed - don't touch !
29 # ---------------------------------------------------
32 DEFIMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
35 echo "Usage: $(basename $0) [-h] [-fr] [-v] <instance ID> [image name]" >&2
36 echo "Instance ID must be 0 or a positive integer (1,2,...)" >&2
37 echo "Image name is optional: 'make show-image' is used by default to get image" >&2
38 echo "Default image: $DEFIMAGE" >&2
41 echo " -fr | --force-restart Force restart of xds-server service"
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 "
52 while [ $# -ne 0 ]; do
65 if [[ "$1" =~ .*:.* ]]; then
66 USER_VOLUME_OPTION="-v $1"
68 echo "Invalid volume option, format must be /InDockerPath:/hostPath"
73 if [[ "$1" =~ ^[0-9]+$ ]]; then
83 [ "$ID" = "" ] && ID=0
85 # Dynamically retrieve image name
86 if [ "$IMAGE" = "" ]; then
88 VER_NUM=`docker images $REGISTRY/$REPO/$NAME-$FLAVOUR:* --format {{.Tag}} | wc -l`
89 if [ $VER_NUM -gt 1 ]; then
90 echo "ERROR: more than one xds image found, please set explicitly the image to use !"
92 elif [ $VER_NUM -lt 1 ]; then
93 echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
97 VERSION=`docker images $REGISTRY/$REPO/$NAME-$FLAVOUR:* --format {{.Tag}}`
98 if [ "$VERSION" = "" ]; then
99 echo "ERROR: cannot automatically retrieve image tag for $REGISTRY/$REPO/$NAME-$FLAVOUR"
104 IMAGE=$REGISTRY/$REPO/$NAME-$FLAVOUR:$VERSION
108 echo "Using instance ID #$ID (user $(id -un))"
110 NAME=agl-xds-$(hostname|cut -f1 -d'.')-$ID-$USER
112 docker ps -a |grep "$NAME" > /dev/null
113 [ "$?" = "0" ] && { echo "Image name already exist ! (use -h option to read help)"; exit 1; }
115 XDS_WKS=$HOME/xds-workspace
116 XDTDIR=$XDS_WKS/.xdt_$ID
118 SSH_PORT=$((2222 + ID))
119 WWW_PORT=$((8000 + ID))
120 BOOT_PORT=$((69 + ID))
121 NBD_PORT=$((10809 + ID))
123 ### Create the new container
124 mkdir -p $XDS_WKS $XDTDIR || exit 1
126 --publish=${SSH_PORT}:22 \
127 --publish=${WWW_PORT}:8000 \
128 --publish=${BOOT_PORT}:69/udp \
129 --publish=${NBD_PORT}:10809 \
131 --hostname=$NAME --name=$NAME \
132 --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
133 -v $XDS_WKS:/home/$DOCKER_USER/xds-workspace \
135 $USER_VOLUME_OPTION \
137 if [ "$?" != "0" ]; then
138 echo "An error was encountered while creating docker container."
143 echo "Copying your identity to container $NAME"
144 echo -n wait ssh service .
148 while [ $res -ne 0 ] && [ $count -le $max ]; do
150 docker exec ${NAME} bash -c "systemctl status ssh" 2>/dev/null 1>&2
153 count=$(expr $count + 1);
157 ssh-keygen -R [localhost]:$SSH_PORT -f ~/.ssh/known_hosts
158 docker exec ${NAME} bash -c "mkdir -p /home/$DOCKER_USER/.ssh"
159 docker cp ~/.ssh/id_rsa.pub ${NAME}:/home/$DOCKER_USER/.ssh/authorized_keys
160 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/*"
161 ssh -o StrictHostKeyChecking=no -p $SSH_PORT $DOCKER_USER@localhost exit
163 echo "You can now login using:"
164 echo " ssh -p $SSH_PORT $DOCKER_USER@localhost"
167 if ($UPDATE_UID); then
168 echo "Setup docker user and group id to match yours"
169 docker exec -t ${NAME} bash -c "systemctl stop xds-server" || exit 1
170 docker exec -t ${NAME} bash -c "usermod -u $(id -u) $DOCKER_USER && groupmod -g $(id -g) $DOCKER_USER" || exit 1
171 docker exec -t ${NAME} bash -c "chown -R $DOCKER_USER:$DOCKER_USER /home/$DOCKER_USER /tmp/xds*" || exit 1
172 docker exec -t ${NAME} bash -c "systemctl start xds-server" || exit 1
173 docker exec -t ${NAME} bash -c "systemctl start xds-server" || exit 1
176 ### Force xds-server restart
177 if ($FORCE_RESTART); then
178 echo "Stopping xds-server..."
179 docker exec -t ${NAME} bash -c "systemctl stop xds-server" || exit 1
181 echo "Starting xds-server..."
182 docker exec -t ${NAME} bash -c "systemctl start xds-server" || exit 1
185 echo "Done, docker container $NAME is ready to be used."