From 2b04c53bfeead4ce0f921563fb575facecac29a1 Mon Sep 17 00:00:00 2001 From: Sandro Keil Date: Tue, 17 Jul 2018 22:18:27 +0200 Subject: [PATCH] Add support for own Docker container name (close #61) --- x11docker | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/x11docker b/x11docker index 5cbde63f..6f8d0b6a 100755 --- a/x11docker +++ b/x11docker @@ -468,7 +468,7 @@ $(cat $Bgpidfile 2>/dev/null)" Pid=$(echo $Line | awk '{print $1}') Name=$(echo $Line | awk '{print $2}') debugnote "Checking: $Pid ($Name): $(pspid $Pid ||:)" - { checkpid $Pid || [ "$Name" = "vcxsrv.exe" ] ; } && { + { checkpid $Pid || [ "$Name" = "vcxsrv.exe" ] ; } && { case $Name in xinit) killpid $Pid $Name xinit ;; Xserver) killpid $Pid $Name 'Xorg|Xephyr|Xvfb|Xwayland|nxagent' ;; @@ -935,6 +935,7 @@ cleanup() { # --cleanup : check for non-removed containers and left grep -q .cache/x11docker <<<$Cachebasefolder && Orphanedfolders=$(find "$Cachebasefolder" -mindepth 1 -maxdepth 1 -type d | sed s%$Cachebasefolder/%% | grep -v x11docker-gui) [ "${X11DOCKER_LASTCLEANFOLDER:-}" ] && Orphanedfolders="$(echo "$Orphanedfolders" | grep -v $X11DOCKER_LASTCLEANFOLDER)" Orphanedcontainers="$(docker ps -a --filter name=x11docker_X --format "{{.Names}}")" + Orphanedcontainers="$Orphanedcontainers $(find "$Cachebasefolder" -mindepth 1 -maxdepth 1 -type f -name 'container.*' | sed 's/^.*\.//')" Orphanedcontainers="$(env IFS='' echo $Orphanedcontainers)" [ -z "$Orphanedcontainers$Orphanedfolders" ] && { @@ -1443,6 +1444,7 @@ Runfromhost="" # option --runfromhost: add host Runasroot="" # option --runasroot: add command running as root to container setup script Workdir="/tmp" # option --workdir: set working directory Sharessh="" # option --sharessh: share ssh agent +Owncontainername="no" # option --name: Own Docker container name # init and dbus Initsystem="tini" # options --systemd --openrc --runit --sysvinit --tini --no-init:Init system in container @@ -1527,7 +1529,7 @@ stdout() { Longoptions="$Longoptions,starter,cleanup,license,licence,help,version,install,update,update-master,remove,wmlist" # special options without starting X server Longoptions="$Longoptions,xhost:,no-auth,vt:,display:,xtest,no-xtest,env:,showenv,showid,showpid1" # X and environment options Longoptions="$Longoptions,verbose,silent,debug,stdout,stderr,stdin" # verbose options - Longoptions="$Longoptions,hostipc,hostnet,ps,cache,cap-default,no-entrypoint,sudouser,hostuser:,group-add:" # capabilities, users + Longoptions="$Longoptions,hostipc,hostnet,ps,cache,cap-default,no-entrypoint,sudouser,hostuser:,group-add:,name:" # capabilities, users Longoptions="$Longoptions,cachedir:,homedir:,westonini:,sharedir:,keymap:,runfromhost:,runasroot:,sharessh" # developer options Parsedoptions="$(getopt --options $Shortoptions --longoptions $Longoptions --name "$0" -- "$@" 2>/tmp/x11docker_parsererror)" @@ -1674,6 +1676,7 @@ $Customenvironment" ; shift ;; # set custom environm --hostipc) Sharehostipc="yes" ;; # docker run option --ipc=host --hostnet) Sharehostnet="yes" ;; # docker run option --net=host --cap-default) Capdropall="no" ;; # don't use --cap-drop=ALL + --name) Containername="${2:-}" ; Owncontainername="yes" ; shift ;; # set container name ## miscellaneous --ps) Preservecachefiles="yes" ;; # preserve container instead of removing it with 'docker run --rm' @@ -1788,6 +1791,9 @@ $Customenvironment" ; shift ;; # set custom environm cache folder $Cachebasefolder" # can happen with --cachedir # Logfiles. $Logfile will be moved to $Sharefolder later + [ "$Owncontainername" = "yes" ] && { + touch "$Cachebasefolder/container.$Containername" + } Mycookie=$(mcookie | cut -b1-6) [ -z "$Mycookie" ] && Mycookie=$RANDOM Logfile="$Cachebasefolder/x11docker.log.$Mycookie" @@ -3913,7 +3919,7 @@ done [ "$Pullterminal" = "NOLUCK" ] && warning "Can not find a terminal emulator that works for option $Xserver. Please install one of: ${Terminallist%"NOLUCK"}" - + [ -z "$Hostdisplay" ] && [ -n "$Newwaylandsocket" ] && { case $Pullterminal in qterminal) Pullterminal="env QT_QPA_PLATFORM=wayland $Pullterminal" ;; @@ -3956,7 +3962,7 @@ done [ "$Forwardstdin" = "yes" ] && { cat <&0 >$Cmdstdinfile & storepid $! catstdin } - + case $Cygwin in no) Watchpidfifo="$Cachefolder/$Watchpidfifo" && $Mksu "mkfifo $Watchpidfifo" @@ -3969,15 +3975,15 @@ done Timetosaygoodbyefifo="$Sharefolder/$Timetosaygoodbyefifo" && mkfile $Timetosaygoodbyefifo && chmod 666 $Timetosaygoodbyefifo ;; esac - + # used by waitfortheend() exec 8<>$Timetosaygoodbyefifo - + # start watching important pids, e.g. xinit, container. exec 9<>$Watchpidfifo setonwatchpidlist $$ watchpidlist & storepid $! watchpidlist - + # start watching for messages out of container or dockerrc exec 6<>$Messagefifo watchmessagefifo & storepid $! watchmessagefifo @@ -4187,7 +4193,7 @@ DefaultEnvironment=DISPLAY=$Newdisplay XAUTHORITY=$Cshare/Xclientcookie $(IFS=$' run) Dockercommand="docker run -d --tty" [ "$Preservecachefiles" = "no" ] && Dockercommand="$Dockercommand --rm" - Containername="x11docker_X${Newdisplaynumber}_${Mycookie}_$Imagenametr" + [ -z "$Containername" ] && Containername="x11docker_X${Newdisplaynumber}_${Mycookie}_$Imagenametr" Dockercommand="$Dockercommand --name=$Containername" verbose "Container name: $Containername" @@ -4297,7 +4303,7 @@ DefaultEnvironment=DISPLAY=$Newdisplay XAUTHORITY=$Cshare/Xclientcookie $(IFS=$' # option --sharecgroup [ "$Sharecgroup" = "yes" ] && Dockercommand="$Dockercommand \\ -v /sys/fs/cgroup:/sys/fs/cgroup:ro" - + # Add video group for GPU access, needed by some systems # Add audio group for ALSA # Does only have real effect if /dev/dri or /dev/snd is shared @@ -4317,7 +4323,7 @@ DefaultEnvironment=DISPLAY=$Newdisplay XAUTHORITY=$Cshare/Xclientcookie $(IFS=$' # Needed especially for --systemd and --dbus-daemon Dockercommand="$Dockercommand \\ --tmpfs /run --tmpfs /run/lock" - + # entrypoint is checked in dockerrc Dockercommand="$Dockercommand \\ --entrypoint=env" @@ -4334,10 +4340,10 @@ DefaultEnvironment=DISPLAY=$Newdisplay XAUTHORITY=$Cshare/Xclientcookie $(IFS=$' for ((Count=1 ; Count<=$Sharevolumescount ; Count++)) ; do case "${Sharevolumes[$Count]}" in "$Benutzerhome") Dockercommand="$Dockercommand \\ - -v '${Sharevolumes[$Count]}':'${Sharevolumes[$Count]}/$(basename $Benutzerhome)':rw" + -v '${Sharevolumes[$Count]}':'${Sharevolumes[$Count]}/$(basename $Benutzerhome)':rw" ;; *) Dockercommand="$Dockercommand \\ - -v '${Sharevolumes[$Count]}':'${Sharevolumes[$Count]}':rw" + -v '${Sharevolumes[$Count]}':'${Sharevolumes[$Count]}':rw" ;; esac done @@ -4398,7 +4404,7 @@ DefaultEnvironment=DISPLAY=$Newdisplay XAUTHORITY=$Cshare/Xclientcookie $(IFS=$' ## option --sharessh [ -n "$Sharessh" ] && Dockercommand="$Dockercommand \\ --env SSH_AUTH_SOCK=$Sharessh" - + # option --env: add custom environment. Only needed here for possible 'docker exec'. Otherwise set in container.CMD.sh IFS=$'\n' for Line in $Customenvironment ; do @@ -4414,7 +4420,7 @@ DefaultEnvironment=DISPLAY=$Newdisplay XAUTHORITY=$Cshare/Xclientcookie $(IFS=$' --" case $Switchcontaineruser in no) Dockercommand="$Dockercommand $Imagename /bin/sh - $Cshare/container.CMD.sh" ;; # dockerrc runs container.rootsetup.sh with 'docker exec' - yes) + yes) case $Initsystem in tini) Dockercommand="$Dockercommand $Imagename $Cshare/tini -- /bin/sh - $Cshare/container.rootsetup.sh" ;; # container.rootsetup.sh runs container.CMD.sh *) Dockercommand="$Dockercommand $Imagename /bin/sh - $Cshare/container.rootsetup.sh" ;; # container.rootsetup.sh runs container.CMD.sh @@ -4639,7 +4645,7 @@ DefaultEnvironment=DISPLAY=$Newdisplay XAUTHORITY=$Cshare/Xclientcookie $(IFS=$' *) echo " echo 'export XDG_SESSION_TYPE=x11'" ;; esac echo " echo ''" - + [ "$Xserver" = "--vcxsrv" ] && [ "$Sharegpu" = "yes" ] && echo " echo 'export LIBGL_ALWAYS_INDIRECT=1'" [ "$Switchcontaineruser" = "yes" ] && echo " echo 'export TINI_SUBREAPER=1'" @@ -4705,14 +4711,14 @@ DefaultEnvironment=DISPLAY=$Newdisplay XAUTHORITY=$Cshare/Xclientcookie $(IFS=$' echo " note 'Want to create and mount a cgroup for $Wantcgroup. As x11docker currently does not run as root, this will probably fail. Please either run x11docker as root, or run with option --pw=su or --pw=sudo. - + Alternatively, create cgroup mountpoint yourself with: mkdir -p /sys/fs/cgroup/$Wantcgroup mount -t cgroup cgroup /sys/fs/cgroup/$Wantcgroup -o none,name=$Wantcgroup - + If you get a read-only error message, remove write protection with: mount -o remount,rw cgroup /sys/fs/cgroup - + You can restore write protection after cgroup creation with: mount -o remount,ro cgroup /sys/fs/cgroup'" [ "$Wantcgroup" = "elogind" ] && echo "note 'If you do not want or need elogind in container, @@ -4782,7 +4788,7 @@ DefaultEnvironment=DISPLAY=$Newdisplay XAUTHORITY=$Cshare/Xclientcookie $(IFS=$' echo "" #[ "$Debugmode" = "yes" ] && echo "set -x" - + echo "$Messagefifofuncs" echo "Messagefile=$Cshare/message.fifo" echo ""