diff options
Diffstat (limited to 'pdkcommon-server')
-rwxr-xr-x | pdkcommon-server | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/pdkcommon-server b/pdkcommon-server new file mode 100755 index 0000000..1b16e3b --- /dev/null +++ b/pdkcommon-server @@ -0,0 +1,89 @@ +#!/bin/bash + +declare container_name="pdkcontainer-server" +declare IMAGE="tizenpdk:v0.2" +declare VOLUMES="-v /dev/bus/usb:/dev/bus/usb" +#declare DEVICES="--devices=/dev/bus/usb" +declare PRIVILEGE="--privileged" +declare hostname=$(hostname) +declare container_id +VOLUMES=$(echo $VOLUMES "-v /etc/localtime:/etc/localtime:ro -v /etc/mic/:/etc/mic/") +HOSTNAME=" --hostname $(hostname) " + +DirCheck() +{ + remain_array=(home share var srv root tmp) + for i in ${remain_array[@]} + do + re=$(ls / | grep -w $i) + if [ $re ] + then + VOLUMES="$VOLUMES --volume=/$re:/$re" + fi + done +# echo $VOLUMES +} + +PermissonCheck(){ + docker ps >/dev/null + if [ $? -ne 0 ] + then + echo -e "\033[31m[Error]\033[0m:\033[33mYou haven't permission to run docker without 'sudo'.We suggest you to use docker without sudo. \033[0m" + exit + fi +} + +# clear the $IMAGE container which is Exited status +Autoclean(){ + ids=$(docker ps -a | grep -w $IMAGE | grep -w 'Exited' | awk '{print $1}') + for id in $ids + do + docker rm $id 1>/dev/null + done +} + +# delte a container by id or container name +DeleteContainer() +{ + docker stop $1 >/dev/null 2>&1 + if [ $?==0 ] + then + docker rm $1 >/dev/null 2>&1 + fi +} + +# create a container +CreateContainer() +{ + #Check mounting +# if [ ! -f /proc/sys/fs/binfmt_misc/register ]; then +# mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc +# fi + container_id=$(docker run -itd --network=pdknet --name=$1 ${PRIVILEGE} ${VOLUMES} ${IMAGE} /bin/bash) +} + +# check whether the container is exists +CheckContainer() +{ + container_id=$(docker ps | grep -w $1 | awk '{print $1}') + #echo $container_id "test" + # after try 10 times, if container_id always is 0, then exit + i=0 + while [ -z $container_id ] + do + CreateContainer $1 + i=1 + done + if [ $i -ne 1 ] + then + echo "Can't create container, please check" + exit 0 + else + echo "create container" $container_id "successful" + fi +} + +DirCheck +PermissonCheck +Autoclean +CheckContainer $container_name |