-
Notifications
You must be signed in to change notification settings - Fork 379
/
x11docker
4830 lines (4466 loc) · 234 KB
/
x11docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/bash
# x11docker
# Run GUI applications and desktop environments in docker on a separate X server or Wayland compositor.
# Circumvents common X security leaks.
# Provides GPU acceleration and pulseaudio sound.
# Restrictes docker container privileges with 'docker run --cap-drop=ALL --security-opt=no-new-privileges'
# Container user is same as host user to avoid root in container.
# Type 'x11docker --help' or scroll down to read usage information.
# https://github.com/mviereck/x11docker
Version="3.9.7.4"
changelog() {
# 26.03.2018 V3.9.7.4 --sysvinit: new option for init system SysVinit in container. Tested with devuan.
# --pulseaudio: bugfix: need to set env PULSE_SERVER
# 23.03.2018 --runit: add softlink for X socket in x11docker.CMD.sh for compatibility with runit on debian
# 21.03.2018 V3.9.7.3 --pulseaudio: share XDG_RUNTIME_DIR/pulse instead of connection over tcp
# 20.03.2018 V3.9.7.2 #30 Fix writeaccess() for user/group names with spaces in it
# 19.03.2018 --wm: fall back to autodetection if specified window manager not found
# bugfix --env: regard whitespace. Still need to handle special chars like"\'$.
# --add: new option to add a host command in xinitrc
# 18.03.2018 consolekit: enable and use automatically for --dbus-system, --openrc, --runit
# --dbus: enable automatically for --runit, --openrc
# mywatch(): watch again, now without sh -c
# 16.03.2018 --debug: new option to set -x in all scripts showing code lines while executed.
# --sharewayland, --waylandenv: deprecated, not needed for anything anymore. --wayland does the job.
# --help: usage() cleanup
# 16.03.2018 V3.9.7.1 bugfix alpine images: /etc/shadow entry must be /bin/sh, --dbus-system -with su fails with /bin/bash
# 15.03.2018 bugfix openSUSE: finish(): replace bc with bash-only calculation, bc misses on openSUSE
# 15.03.2018 V3.9.7 bugfix openSUSE/fedora: ps check for container pid; fixed desktop logout issue, too.
# structure change: don't sleep 1 for setup; instead wait for it in CMD.sh resp. run su or init in setup
# 14.03.2018 SSH with --hostdisplay: set --hostipc, --hostnet and --trusted. Do not X-generate cookie, bake self.
# 13.03.2018 bugfix ---weston/-weston-xwayland: do not start drm backend if started within X without DISPLAY -> crashed host X
# bugfix: regard ssh session, assume tty if DISPLAY is empty
# bugfix: --hostdisplay: don't set keymap
# xinitrc: some cleanup
# 11.03.2018 --verbose: power of moo
# 10.03.2018 V3.9.6.1 --lang: replace locale-gen with more general available localedef
# --tini: check for docker-init in PATH, disable if missing (#23)
# 09.03.2018 V3.9.6 --lang: new option to set language locale in utf8, create it if missing.
# 06.03.2018 V3.9.5 --keymap: new option to set keyboard layout
# 06.03.2018 V3.9.4.2 store keyboard layout (xkb_keymap) in separate file, not in xinitrc. Set on all X servers. #25
# 06.03.2018 V3.9.4.1 --pulseaudio: bugfix/typo
# 05.03.2018 share /etc/localtime with container to have the same time
# 05.03.2018 V3.9.4.0 --pulseaudio --hostnet: no fallback to alsa, use localhost IP instead
# --pulseaudio --no-internet: fallback to --alsa
# 04.03.2018 clean up error message on docker startup failure, remove multiple error lines
# --systemd: bugfix: terminate x11docker if systemd startup fails
# stdout and stderr of image command outsourced of docker.log
# docker log -f > docker.log to get output in detached mode
# --sys-admin: no longer deprecated, needed for debian 9 images (but not debian 10).
# --net and --ipc changed to --hostnet and --hostipc
# --dbus-daemon changed to --dbus-system
# --auto --gpu: fallback to --hostdisplay for seamless mode if xpra and weston not found (#23)
# 02.03.2018 #24 mount /dev/dri and /dev/snd not only with --device, but also --volume to keep ownership+group
# --hostdisplay: minor bugfix: Use correct display number to share /tmp/.X0-lock, only share if it exists
# more verbose messages in waiting routines
# 01.03.2018 V3.9.3.2 --no-xtest: disable extension XTEST. Default for most options.
# openSUSE docker package misses init binary, show warnings for --tini, issue #23
# 01.03.2018 V3.9.3.1 fix XTEST warning messages
# 01.03.2018 V3.9.3 --tini: show warning for outdated docker versions without option --init and fall back to --no-init, issue #23
# 26.02.2018 --xtest: new option to enable X extension XTEST. Default for --xdummy, --xvfb, --xpra
# --pulseaudio with --net: fallback to --alsa, disabling --pulseaudio
# 25.02.2018 V3.9.2.3 set container GID of video and audio to same as on host
# cat docker daemon messages for startup error message
# 24.02.2018 bugfix --kwin: kwin_wayland seems to need dbus-launch now
# 15.02.2018 mywatch(): replaced watch with custom sleep loop, watch failed in --hostdisplay (xinitrc) setups
# 10.02.2018 --exe: only forward stdin if not empty
# finish(): use pkill in most cases instead of kill to avoid kill success messages
# bugfix --weston/--kwin: wait for file creation of wayland socket, checking logfile is not enough
# mywatch(): verbose output
# 09.02.2018 V3.9.2.2 minor bugfix --exe: avoid possible hostexe options with basename for $Hostexebasename
# minor bugfix: typo checking /tmp/.Xn-lock
# check free display and cache folder with find only
# bugfix checking free display number: race condition if starting two x11docker instances at same time, second one failed because display number already in use
# 04.02.2018 plasmashell added to possible window managers
# 29.01.2018 V3.9.2.1 correct date/year in changelog (issue #21)
# 27.01.2018 finish(): minor bugfix: wrong warning although terminating bgpid was successfull
# 23.01.2018 create /x11docker/environment to store and provide container environment variables
# 21.01.2018 V3.9.2 bugfix: add groups video and audio if su is not used in container. /etc/group changes by dockerrc seem to be not regarded in that case.
# finish(): more precise check with pid and name before killing background pids
# 17.01.2018 V3.9.1.9 --xpra: if server crashes, use xpra option --mmap=no on restart
# 16.01.2018 V3.9.1.8 --xpra: stop x11docker if xpra server crashes multiple times
# 15.01.2018 V3.9.1.7 --gpu: share /dev/vga_arbiter and /dev/nvidia*
# 15.01.2018 V3.9.1.6 restart xpra server if it crashes (can happen with xpra 2.2, reason unknown)
# 13.01.2018 V3.9.1.5 bugfix xpra: reconnect to server after timeout (60s) if switching to console
# 12.01.2018 V3.9.1.4 --help: some usage updates
# 10.01.2018 --xorg: create virtual framebuffer if no monitor is connected (headless server setup)
# --xpra: note that 2.1.x series is more stable than 2.2.x series
# 06.01.2018 create $Cacherootfolder/Xenv.latest with latest X environment variables for easier custom access
# --verbose --systemd: hide error messages: Failed to add fd to store | Failed to set invocation ID | Failed to reset devices.list
# --systemd: set global environment XAUTHORITY
# 04.01.2018 V3.9.1.3 --dbus-daemon: set xhost +SI:localuser:$USER, needed for deepin
# 03.01.2018 bugfix --systemd: global XAUTHORITY setting was wrong, removed at all
# faster startup of pulseaudio, no sleep 1
# bugfix: pull terminal did not appear if running from terminal
# create fake homedir and softlinks to sharedirs in CMD.sh, base is /fakehome now
# 29.12.2017 extension XTEST: more restrictive defaults
# 28.12.2017 V3.9.1.2 --sudouser: root gets password 'x11docker', too
# check environment variables in image and set them in x11docker.CMD.sh. Allows PATH of x11docker/trinity again.
# bugfix parsing host XAUTHORITY if running from gksu
# cut image command at '#'
# 28.12.2017 V3.9.1.1 bugfix --systemd: directly share X socket as systemd can have issues with soft links
# 25.12.2017 V3.9.1 run in detached mode, drop mess of nohup/setsid/script
# 24.12.2017 --dbusdaemon: dropped consolekit, not really useful
# --dbusdaemon: switch only for --tini/--none. Always run daemon for --systemd --openrc --runit
# 22.12.2017 --systemd: create /sys/fs/cgroup/systemd if missing on host
# --sys-admin: deprecated thanks to --tmpfs=/run/lock
# containersetup.sh collects most former 'docker exec' commands from dockerrc
# 21.12.2017 V3.9.0.5 add capability DAC_OVERRIDE if user switching is allowed -> needed to change /etc/sudoers if ro
# bugfix: only create XDG_RUNTIME_DIR if not already existing
# --systemd: adding --tmpfs=/run/lock allows to drop --sys-admin !
# 20.12.2017 V3.9.0.4 docker run --workdir=/tmp, avoids issues with WORKDIR in image (seen with lirios/unstable)
# 19.12.2017 bugfix --dbus: check for dbus-launch in x11docker.CMD.sh, not in dockerrc on host
# 18.12.2017 changes to satisfy lirios:
# add docker run -ti
# run docker command with script -c to provide fake tty
# change /tmp/fakehome to /home/fakehome
# 17.12.2017 V3.9.0.3 switched back to /tmp/fakehome to avoid CHOWN and issues with --sharedir
# drop --cap-add CHOWN
# bugfix --sudouser, failed to start
# --sharedir: without --home[dir], create softlinks to /tmp/fakehome
# --home: avoid conflict with --sharedir=$HOME, mount as $HOME/$(basename $HOME)
# 16.12.2017 only chown $Benutzerhome if --home[dir] is not used. Change non-writeable error in warning only
# --hostdisplay: warning if host has no own cookie
# avoid grey edge with Xwayland, Xaxis must be dividable by 8
# 16.12.2017 V3.9.0.2 /etc/sudoers[.d/]: replace completly to avoid possible evil image setups
# --cap-add CHOWN as default to allow /home/$Benutzer with --sharedir
# 16.12.2017 V3.9.0.1 bugfix: --systemd: do not set $HOME globally, root may write into it
# use /home/$Benutzer instead of /tmp/fakehome
# 15.12.2017 V3.9.0 /etc/shadow: disable possible root password
# --dbusdaemon: new option to run dbus system daemon and consolekit in container
# 14.12.2017 re-checked capabilities for init systems
# --systemd: set environment globally, especially DISPLAY for deepin is needed
# --systemd: set xhost+SI:localuser:$Benutzer as XAUTHORITY seems to be ignored
# 12.12.2017 /tmp/.ICE-unix created in dockerrc, root owned with 1777, needed for SESSION_MANAGER
# --rw: deprecated, root file system is always r/w now due to 'docker exec' in dockerrc
# 10.12.2017 (V3.8.1) bugfix Ubuntu: avoid Wayland backend for Weston due to MIR issue #19
# (V3.8.1) --xorg: change Xorg to X. X is setuid wrapper for Xorg on Ubuntu 14.04
# (V3.8.1) +iglx removed from X options, not present in older versions of X, and maybe security issue.
# 09.12.2017 create user in dockerrc with 'docker exec' instead of using createuser.sh
# --xorg: removed +iglx from options, not supported on older X versions
# 07.12.2017 --openrc: new option for init system OpenRC in container
# --sharecgroup: new option to share /sys/fs/cgroup. default for --systemd.
# 06.12.2017 create /var/lib/dbus in dockerrc to avoid dbus errors with init systems
# show image name and display in weston windows
# bugfix --runit: add SYS_BOOT even with --cap-default
# 04.12.2017 V3.8.0 --sudouser: create user with docker run options instead of createuser script
# --sudouser: create /etc/sudoers.d/$Benutzer with docker exec in dockerrc
# 03.12.2017 --sudouser: create /etc/sudoers.d/$Benutzer instead of adding groups wheel and sudo
# createuser.sh: check for useradd, if missing use adduser (fits fedora and alpine/busybox as well)
# 02.12.2017 --runit: new option for init system runit
# --init: new option for init system tini (default now, docker run option --init)
# --no-init: new option to run image command as PID 1 (has been default before x11docker 3.8)
# 01.12.2017 --sys-admin: new option for --cap-add=SYS_ADMIN. Needed for systemd in debian based images.
# 28.11.2017 --sudouser, --systemd: set needed capabilities only instead of --cap-default
# --xpra --hostuser: create /run/user/$Hostuseruid if missing
# $Sharefolder/stdout+sterr: chmod 666 to allow access with --user
# container user password: x11docker (creating volume /etc/shadow)
# 25.11.2017 init system tini as default with 'docker run --init'
# --systemd: unprivileged systemd in container
# 23.11.2017 --exe and --xonly: regard --home and --homedir, --user and --hostuser
# --wayland: new option to auto-setup Wayland environment
# -W is now --wayland instead of --weston, -T for --weston now
# check pids before calling mywatch()
# 22.11.2017 bugfix: --hostdisplay --gpu needs trusted cookies
# colored logfile output
# 19.11.2017 bugfix in createuser.sh: adduser failed with fedora based images, use useradd and usermod instead
# 18.11.2017 bugfix: --pw=gksu: avoid wrong docker startup error message, use nohup in dockerrc
# 17.11.2017 --verbose: green colored output for logfile titles and verbose() lines
# 16.11.2017 set env DISPLAY XAUTHORITY and WAYLAND_DISPLAY in x1docker.CMD.sh as systemd eats them otherwise
# --systemd: new option to run systemd as PID1 in container and image command as a service
# use docker run option --tmpfs for /tmp, /var/tmp and /run instead of --volume=/tmp
# --sudouser: instead of empty password, user name is password now
# changed container share folder /tmp/x11docker to /x11docker to avoid issues with --tmpfs /tmp
# 11.11.2017 V3.7.2 allow rw with --volume=/var/tmp, needed for trinity
# 09.11.2017 bugfix for su on console: exec </dev/tty
# 06.11.2017 --nxagent: removed xhost startup workaround
# $Hostxenv: removed custom environment
# 05.11.2017 --nxagent: shift+F11 toggles fullscreen
# --nxagent on Mageia: only show warning about seamless mode instead of disabling it
# 03.11.2017 V3.7.1 bugfix for gksudo and lxsudo
# read host cookie with xauth if XAUTHORITY is empty, can happen with xdm
# --nxagent on Mageia: no seamless mode
# 02.11.2017 Ubuntu 16.04: bugfix for --xpra (must not set --webcam=no)
# 01.11.2017 replaced while/sleep loops with watch
# 31.10.2017 bugfix for weston and kwin on konsole, terminal for password prompt failed
# alertbox(): regard $DISPLAY, use $Anyterminal otherwise to support Wayland
# weston.ini: keyboard config setting on console
# fedora: show alert for --ipc/--trusted due to missing extension security
# 30.10.2017 V3.7.0 new option --alsa; use -wm for --xephyr and the likes; support more terminals and message dialogs
# 30.10.2017 V3.7.0 auto-choose window manager in --xephyr/--xorg/--weston-xwayland/--kwin-xwayland/--xwayland except --desktop is set
# 29.10.2017 --alsa: new option for ALSA sound
# changed content of variable $Xserver to X server option names itself
# 28.10.2017 --kwin-xwayland: set keyboard layout
# --kwin-native: deprecated, too much trouble, but less use
# 27.10.2017 extended terminal list for password prompt/docker pull
# --xhost: always disabling with no_xhost(), afterwards setting --xhost
# bugfix --weston/--weston-xwayland: set backend in compositor command, weston's autodetection can fail
# bugfix --kwin/--kwin-xwayland: set backend in compositor command, weston's autodetection can fail
# 25.10.2017 new function alertbox, outsourced from error(). yad, kaptain, kdialog, gxmessage, xterm: additional messagebox tools
# 25.10.2017 V3.6.3.9 show error messages regardless of --silent
# change "sudo" to "sudo -E", needed for OpenSUSE
# code cleanup, some improved messages
# 25.10.2017 V3.6.3.8 fedora: set --ipc and --trusted for --hostdisplay only
# 25.10.2017 V3.6.3.7 bugfix --hostdisplay on fedora: use host cookie, custom cookie is rejected
# 24.10.2017 V3.6.3.6 --wmlist: new option to retrieve list of window managers, used by x11docker-gui
# --gpu: improved support in autochoosing mode
# disabled note of xpra keyboard shortcuts, takes too long
# hardcoded xpra environment variables, parsing 'xpra showconfig' takes too long
# bugfix for --pw=sudo, issue with setsid
# 24.10.2017 V3.6.3.5 bugfix xpra with host user root: set environment variables
# dbus-launch for konsole and terminator, needed in dockerrc
# 23.10.2017 V3.6.3.4 add /usr/sbin to PATH, needed on mageia for ip
# bugfix --pw=sudo: 'setsid sudo' fails, must use 'sudo setsid'
# 23.10.2017 V3.6.3.3 removed experimental Code
# bugfix for --wm as root in xinitrc
# 23.10.2017 V3.6.3.2 remove debugging 'set -x' in xinitrc
# 23.10.2017 V3.6.3.1 bugfix: don't use su $USER in xinitrc
# 20.10.2017 split X server command with \backslash in multiple lines
# 20.10.2017 V3.6.3 new option --no-internet; adjustments for CentOS/RHEL, Arch and Manjaro
# 10.10.2017 V3.6.2 new option --xfishtank; better SELinux support; --scale and --size for --xorg
# 15.08.2017 V3.6.1 new options --stdout and --stderr; support stdin
# 12.08.2017 V3.6 allow root to start x11docker, use $(logname) for X server and as container user
# 17.05.2017 V3.5 hardening container security (--cap-drop=ALL), improved user handling (--user)
:
}
todo() {
# BUG console-kit-daemon fails. fix it or drop it.
# check fgconsole
# --env SHELL=/bin/bash fails in deepin with --dbus-system
# --env: escape special chars
# setupscript: set environment variables?
# x11docker/fvwm: check quit option
# opensuse/arch: issues with x11docker/fluxbox, background missing, sometimes no context menu
# BUG --weston,--kwin: docker sometimes fails although socket exists: "stat /run/user/1000/wayland-607: no such file or directory"
# minor BUG: some xtermrc log output gets lost with GUI password prompts, not sure why; maybe fds get lost when gui stops. Use nohup?
# check echo especially in sh scripts, replace with printf if needed (#25)
# avoid possible confusions --home --sharedir. --sharedir mounts can overlap --home folder. What about softlinks with existing target in --home? Maybe avoid /home/$USER at all
# check out possibilities to allow 'sudo docker' directly again.
# xpra restart on tty switch -> bug report
# --xpra --desktop restarts client if closing desktop window
# --lang: find out locale package names for several distris for documentation
# --keymap does not work on tty with --kwin and --kwin-xwayland. No idea how to set it.
# further check of xpra server crashes with jess/atom and chromium. --mmap=no avoids the bug: xpra bug report?
# BUG check whether VT is not in use with --xorg/--xpra/--xdummy, bug if accidently using vt that is already in use
# check all FIXME
# note: SYS_PTRACE allows polkit in docker
# --systemd: try to avoid xhost +SI:localuser:$Benutzer
# --xpra-xwayland, xdummy-xwayland: use kwin-wayland as fallback for missing weston?
# --xorg: getty and autologin to avoid Xwrapper.config changes?
# gnome3 based desktop failing due to gnome bugs: pantheon budgie gnome3
# check Xorg version for +iglx, check security implications, maybe option --iglx?
# GTK3 in Wayland: --dbus once worked with $Dbusdaemon=yes
# --wayland --user/--hostuser: wayland socket access denied due to XDG_RUNTIME_DIR
# --nxagent 3.5.0: Mageia 6: seamless mode fails
# fedora: SElinux issue: '--security-opt label=type:container_runtime_t': need more restrictive setting
# https://unix.stackexchange.com/questions/386767/selinux-and-docker-allow-access-to-x-unix-socket-in-tmp-x11-unix
# multimonitor support for --scale and --size
# check current multimonitor behaviour for weston on tty
# --xdummy --gpu on tty allows real resolutions only
# --xorg: check custom systemd start of X #7
# check X in container #7
# some tests with Xephyrglamor=no
### BUG collection: non x11docker bugs
# BUG Xwayland does not always sit at 0:0 on multiple outputs.
# bugreport: https://bugzilla.redhat.com/show_bug.cgi?id=1498665
# BUG nxagent with x11docker/lxde: segmentation fault of lxpanel with --userns-remap. bug in nxagent, lxpanel or x11docker?
# BUG --kwin*: wrong fullscreen and crashes in gnome-wayland, strange in weston, WAYLAND_DISPLAY="" does not help, probably bug in kwin
# BUG scale>1 Xwayland in Weston is too large (Xwayland bug), rendering issues on tty (switching scaled/unscaled Xwayland on keyboard/mouse events)
# bugreport: https://bugzilla.redhat.com/show_bug.cgi?id=1498669
# BUG x11docker-gui in weston freezes weston in combo boxes. Weston bug ? QT3/4 bug?
# BUG debian bug report lightdm/sddm contra gdm, dm can crash on tty switch if multiple graphical sessions are running
:
}
usage() { # --help: show usage information
echo "
x11docker: Run GUI applications and desktop environments in docker.
Optional features:
* Hardware acceleration for OpenGL
* Pulseaudio and ALSA sound
* Clipboard sharing
* Persistent home folders
* Wayland support
* Language locale creation
* Init system support (systemd, openrc, runit, tini)
Focus on security:
* Avoids X security leaks using additional X servers.
* Container user is same as host user to avoid root in container.
* Default docker container capabilities are dropped.
Usage:
To run a docker image with new X server (auto-choosing X server):
x11docker [OPTIONS] IMAGE [COMMAND]
x11docker [OPTIONS] -- "'"[DOCKER_RUN_OPTIONS]"'" IMAGE [COMMAND [ARG1 ARG2 ...]]
To run a host application on a new X server:
x11docker [OPTIONS] --exe COMMAND
x11docker [OPTIONS] --exe -- COMMAND [ARG1 ARG2 ...]
To run only a new empty X server:
x11docker [OPTIONS]
Dependencies on host:
Depending on chosen options, x11docker needs some packages to be installed.
It will check for them on startup and show messages if some are missing.
List of possible needed packages:
* most recommended to allow security and convenience:
xpra Xephyr xauth xrandr
* advanced GPU support:
weston Xwayland xdotool
* less important:
xclip pulseaudio kwin_wayland nxagent xdpyinfo Xvfb
* least important:
xserver-xorg-legacy xserver-xorg-video-dummy xfishtank
xdg-desktop-icon xdg-icon-resource unzip wget
Dependencies in image:
Doesn't have dependencies inside of docker images, except for
options --gpu, --lang and --pulseaudio, see below at option descriptions.
Options:
--help display this message and exit.
-e, --exe execute host application on new X server (no docker).
--xonly only create empty X server.
Basic settings: (especially influencing auto choosing X server)
-d, --desktop Indicate desktop environment in image.
-g, --gpu Hardware accelerated OpenGL rendering. Shares files in
/dev/dri. Works best with open source drivers installed
on host and OpenGL/Mesa in image. Closed source drivers
need to be the very same on host and in image.
Degrades container isolation. Container access to GPU.
-W, --wayland Set up a wayland environment. (Some QT5 apps also need
option --dbus, some GTK3 apps must run without --dbus.)
-w, --wm COMMAND Host window manager to use for single applications in
nested X server options like --xephyr.
To autodetect a host wm, use --wm=auto or short: -wm
To set default autodetected window manager:
update-alternatives --config x-window-manager
Shared folders:
-m, --home Share a host folder ~/x11docker/imagename as home folder
in container (to store persistent data).
--homedir DIR Specify custom host folder DIR for option --home.
--sharedir DIR Share host folder (or file) DIR with r/w access.
(can be specified multiple times for multiple folders).
Clipboard, sound, language:
-c, --clipboard Share clipboard between X servers (works best with xpra.
Most other X servers need xclip to be installed).
-p, --pulseaudio Sound with pulseaudio. Degrades isolation.
Needs 'pulseaudio' on host and in image.
--alsa Sound with ALSA. Shares devices in /dev/snd. You can
define desired sound card with: --env ALSA_CARD=cardname
Degrades isolation. Container access to sound hardware.
--lang LOCALE Language setting: search for utf8 LOCALE in image and
create it if missing. Needs package 'locale' im image.
LOCALE can be e.g. ru, en, de, zh_CN, cz, fr, fr_BE.
Same as host: --lang=\$LANG.
Special options:
--no-internet Disable internet access for container.
--no-entrypoint Disable ENTRYPOINT in image to allow other commands, too
--env VAR=value Set custom environment variable VAR=value
Special usecase for user shell: '--env SHELL=/bin/sh'
--showenv Echo new \$DISPLAY, \$XAUTHORITY and \$WAYLAND_DISPLAY.
For custom access to new X server. Get environment with:
read xenv < <(x11docker --showenv [...])
--sudouser Allow sudo and su for container user. Use with care,
severe reduction of default x11docker security!
Password: x11docker
--add COMMAND Add custom host command to xinitrc (use sh syntax).
--pw FRONTEND Choose frontend for password prompt. Possible FRONTEND:
su sudo gksu gksudo lxsu lxsudo kdesu kdesudo
pkexec beesu none
X server options:
--auto Auto choose X server for docker applications (default).
(Regards options --desktop, --gpu, --wayland and --wm).
-a, --xpra Use xpra to show application windows on host display.
(Needs 'xpra' on host. Get it from www.xpra.org.
With option --desktop xpra runs in nested desktop mode.)
-y, --xephyr Use nested X server Xephyr to show container desktops
in a window on host display. (Needs 'Xephyr' or 'Xnest').
With option --wm=auto usefull for single apps, too.
-n, --nxagent Like --xpra for single applications, but faster startup.
With --desktop like --xephyr, but resizeable.
(Needs 'nxagent', best since nxagent version 3.5.99).
-A, --xpra-xwayland Like --xpra, but supports option --gpu.
(Needs 'xpra', 'Xwayland', 'weston' and 'xdotool').
-Y, --weston-xwayland Like --xephyr, but supports option --gpu.
Runs as nested server in X or on its own from console.
(Needs 'weston' and 'Xwayland'.)
-h, --hostdisplay Share host display :0, quite bad container isolation!
Least overhead of all X server options.
-x, --xorg Run new core Xorg server. Runs ootb from console.
Switch tty with <CTRL><ALT><F1>....<F12>.
To run from within X, edit '/etc/X11/Xwrapper.conf' and
replace line: allowed_users=console
with lines allowed_users=anybody
needs_root_rights=yes
Debian 9 and Ubuntu 16.04: Install xserver-xorg-legacy.
Special X servers:
--kwin-xwayland Like --weston-xwayland, but using kwin_wayland
(Needs 'kwin_wayland' and 'Xwayland').
-X, --xwayland Use Xwayland, needs a running Wayland compositor.
(Needs 'Xwayland' to be installed.)
--xdummy Invisible X server. (Needs Xorg's dummy video driver)
--xvfb Invisible X server. (Needs 'Xvfb')
--xdummy and --xvfb can be used for custom access,
for example with VNC or ssh.
Output of environment variables on stdout. (--showenv)
Along with option --gpu an invisible setup with Weston,
Xwayland and xdotool is used (instead of Xdummy or Xvfb)
--nothing Do not provide any X or Wayland server.
Wayland without X: (see also above: --wayland)
-T, --weston Weston without X for pure Wayland applications.
Runs in X or from console. (Needs package weston.)
-K, --kwin KWin without X for pure Wayland applications.
Runs in X or from console. (Needs kwin_wayland.)
-H, --hostwayland Share host Wayland without X for pure Wayland apps.
(Needs already running Wayland compositor like Gnome 3.)
(Can be combined with --hostdisplay.)
X and Wayland appearance options:
-f, --fullscreen Run Xephyr, nxagent or Weston in fullscreen mode.
--size XxY Screen size of new X server (e.g. 800x600).
-l, --scale N Scale/zoom factor N for xpra, Xorg or Weston.
Allowed for --xpra, --xorg --xpra-xwayland: 0.25...8.0.
Allowed for --weston and --weston-xwayland: 1...9.
(Mismatching font sizes can be adjusted with --dpi).
--rotate N Rotate display (--xorg, --weston and --weston-xwayland)
Allowed values: 0, 90, 180, 270, flipped, flipped-90,
flipped-180, flipped-270. (flipped = mirrored)
--dpi N dpi value (dots per inch) to submit to clients.
Influences font size of some applications.
--output-count N Multiple outputs for Weston, KWin or Xephyr.
--xfishtank Show fish tank on new X server (needs 'xfishtank').
X authentication:
--xhost STR Set \"xhost STR\" on new X server (see 'man xhost').
(Use with care. '--xhost +' allows access for everyone).
-o, --no-xhost Disable any access to host X server granted by xhost.
--no-auth Disable cookie authentication on new X server.
--trusted Use trusted cookies for --hostdisplay
--untrusted Create untrusted cookies. Restricts X access.
Default for --hostdisplay to avoid keylogging and
MIT-SHM errors. If --gpu is set, --trusted is used.
X and Wayland configuration:
--display N Use display number N for new X server.
--vt N Use vt / tty N (affects --xorg, --xdummy, --xpra).
--keymap LAYOUT Set keyboard layout for new X server, e.g. de, us, ru.
For possible LAYOUT look at /usr/share/X11/xkb/symbols.
--xtest Enable XTEST. Default for --xvfb, --xdummy, --xpra.
--no-xtest Disable XTEST. Default for most options.
--westonini FILE Custom weston.ini for --weston and --weston-xwayland.
User settings:
--user N Create container user N (N=name or N=uid). Default:
same as host user. N can also be an unknown user id.
You can specify a group id with N being 'user:gid'.
--hostuser USER Run X (and container user) as user USER. Default is
result of \$(logname). (x11docker must run as root).
Init system and dbus daemon:
--tini Default: init system tini (built-in of docker).
--no-init No init system in container. Image command is PID 1.
--runit Init system runit. Degrades container isolation.
Needs 'runit' installed in image. 'dbus' is recommended.
--openrc Init system OpenRC. Degrades container isolation a bit,
but needs less capabilities than --runit and --systemd.
Needs 'openrc' installed in image. 'dbus' recommended.
--sysvinit Init system SysVinit. Degrades container isolation a bit,
but needs less capabilities than others.
Needs 'sysvinit' installed in image. 'dbus' recommended.
--systemd Init system systemd. Degrades container isolation.
For faster startup mask services that fail in container.
Needs 'systemd' installed in image.
Old systemd versions in image need --sys-admin, too.
--sharecgroup Share /sys/fs/cgroup. Default for --systemd.
Can be used with --openrc.
-b, --dbus Run dbus user session daemon for image command.
--dbus-system Run dbus system daemon in container (includes --dbus).
Enables consolekit and ck-launch-session if available.
Default for --systemd, --sysvinit, --openrc and --runit.
Container capabilities:
--hostipc Sets docker option --ipc=host, disables IPC namespacing.
Severe reduction of container isolation! Shares
host interprocess communication and shared memory.
Allows MIT-SHM extension of X servers.
--hostnet Set docker run option --net=host, disables network
namespacing. Severe reduction of container isolation!
Shares host network stack, allows dbus communication.
--cap-default Allow default docker container capabilities and
disable container security hardening of x11docker.
--sys-admin Add capability SYS_ADMIN. Please avoid that.
Custom capabilities can be added with --cap-add=CAP after --
Miscellaneous:
--starter Create starter on desktop and exit. You can move the
created .desktop file to ~/.local/share/applications
to get a menu entry.
--cachedir DIR Custom cache folder. (Default: \$HOME/.cache/x11docker)
--license Show license of x11docker (MIT) and exit.
--ps Preserve container and cache files on exit.
--cleanup Clean up orphaned containers and cache files.
Verbose options:
-v, --verbose Be verbose. (Shows logfiles).
--silent Do not show terminal messages (except errors).
--debug Debug mode: show command lines while executed. (set -x)
--stdout Show stdout of container applications.
--stderr Show stderr of container applications.
Installation options (need root permissions):
--install Install x11docker and x11docker-gui from current folder.
--update Download and install latest version from github.
--remove Remove x11docker from your system.
x11docker version: $Version
Please report issues at https://github.com/mviereck/x11docker
"
}
license() { # --license: show license (MIT)
echo 'MIT License
Copyright (c) 2015, 2016, 2017, 2018 Martin Viereck
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'
}
alertbox() { # X alert box with title $1 and message $2
local Title Message
Title=${1:-}
Message=${2:-}
Message="$(echo "$Message" | LANG=C sed "s/[\x80-\xFF]//g" | fold -w120 )" # remove UTF-8 special chars; line folding at 120 chars
# try some tools to show alert message. If all tools fail, return 1
command -v xmessage >/dev/null && [ -n "$DISPLAY" ] && {
echo "$Title
$Message" | xmessage -file - -default okay ||:
} || {
command -v gxmessage >/dev/null && [ -n "$DISPLAY" ] && {
echo "$Title
$Message" | gxmessage -file - -default okay ||:
}
} || {
command -v zenity >/dev/null && [ -n "$DISPLAY" ] && {
zenity --error --no-markup --ellipsize --title="$Title" --text="$Message" 2>/dev/null ||:
}
} || {
command -v yad >/dev/null && [ -n "$DISPLAY" ] && {
yad --image "dialog-error" --title "$Title" --button=gtk-ok:0 --text "$(echo "$Message" | sed 's/\\/\\\\/g')" --fixed 2>/dev/null ||:
}
} || {
command -v kaptain >/dev/null && [ -n "$DISPLAY" ] && {
echo 'start "'$Title'" -> message @close=" cancel" ;
message "'$(echo "$Message" | sed 's/\\/\\\\\\/g' | sed 's/"/\\"/g' | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' )'" -> @fill ;' | kaptain ||:
}
} || {
command -v kdialog >/dev/null && [ -n "$DISPLAY" ] && {
kdialog --title "$Title" --error "$(echo "$Message" | sed 's/\\/\\\\/g' )" 2>/dev/null ||:
}
} || {
command -v xterm >/dev/null && [ -n "$DISPLAY" ] && {
xterm -title "$Title" -e "echo '$(echo "$Message" | sed "s/'/\"/g")' ; read -n1" ||:
}
} || {
[ -n "$Anyterminal" ] && [ -e "$Cachefolder" ] && {
mkfile $Cachefolder/message
echo "#! /bin/bash
echo '$Title
$Message
(Press any key to close window)'
read -n1
" >> $Cachefolder/message
$Anyterminal /bin/bash $Cachefolder/message ||:
}
} || {
notify-send "$Title:
$Message" 2>/dev/null
} || return 1
return 0
}
error() { # show error messages on stderr and exit
Message="$*
Type 'x11docker --help' for usage information
For debugging, run x11docker in terminal and/or enable option '--verbose'
and look afterwards at logfile $Logfile3
If you think this is a bug in x11docker,
please report at https://github.com/mviereck/x11docker"
# output to terminal
echo -e "
\033[41mx11docker ERROR:\033[49m $Message
" >&2
# output to X dialogbox
[ -n "$Hostxenv" ] && export $Hostxenv
[ -n "$Newxenv" ] && {
[ "$Tty" = "yes" ] && export $Newxenv
[ "$Xserver" = "--xorg" ] && export $Newxenv
}
alertbox "x11docker ERROR" "$Message"
# output to logfile
[ -e "$Logfile" ] && echo "x11docker ERROR: $Message
" >> "$Logfile"
saygoodbye
touch $Errorfile
exit 1 # trap to finish()
}
warning() { # show warning messages
echo "$(tput setaf 3)x11docker WARNING:$(tput sgr0) $*" >&3
echo "" >&3
[ -e "$Logfile" ] && echo "x11docker WARNING: $*
" >> "$Logfile"
return 0
}
note() { # show notice messages
echo "$(tput setaf 2)x11docker note:$(tput sgr0) $*" >&3
echo "" >&3
[ -e "$Logfile" ] && echo "x11docker note: $*
" >> "$Logfile"
return 0
}
verbose() { # show verbose messages
# only logfile notes here, terminal output is done with tail in part:verbose
[ -e "$Logfile" ] && echo "x11docker: $*
" >> "$Logfile"
return 0
}
installer() { # --install, --update, --remove: Installer for x11docker
# --install:
# - copies x11docker and x11docker-gui to /usr/bin
# - installs icon in /usr/share/icons
# - creates x11docker.desktop file in /usr/share/applications
# --update:
# - download and install latest version from github
# --remove
# - remove installed files
local X11dockericonfile
export PATH="$PATH:/usr/local/bin" # avoid bug on opensuse where root does not have this in $PATH. Will become obsolete as new default is /usr/bin
# Prepairing
case ${1:-} in
--install)
command -v x11docker > /dev/null && { error "x11docker seems to be installed already.
Try 'x11docker --update' instead." ; }
[ -f "./x11docker" ] || { error "File x11docker not found in current folder.
Try 'x11docker --update' instead." ; }
command -v kaptain > /dev/null || { warning "x11docker-gui needs package kaptain
to provide a GUI, but could not find kaptain on your system.
Please install package kaptain if you want to use x11docker-gui.
x11docker-gui tries to use image x11docker/kaptain if kaptain missig.
x11docker itself does not need it and works fine from cli.
Get kaptain on github: https://github.com/mviereck/kaptain" ; }
;;
--update)
mkdir -p /tmp/x11docker-install && cd /tmp/x11docker-install || error "Could not create or cd to /tmp/x11docker-install"
echo "Downloading latest x11docker version from github"
command -v wget >/dev/null || error "wget not found. Need 'wget' for download.
Please install wget."
wget https://github.com/mviereck/x11docker/archive/master.zip || error "Could not download x11docker-master from github"
echo "Unpacking archive"
command -v unzip >/dev/null || error "Can not unpack archive. Please install 'unzip'."
unzip master.zip || error "Could not unzip archive"
echo ""
cd /tmp/x11docker-install/x11docker-master || error "could not cd to /tmp/x11docker-install/x11docker-master"
;;
esac
# Doing
case ${1:-} in
--install|--update)
[ -x /usr/local/bin/x11docker ] && rm -v /usr/local/bin/x11docker
[ -x /usr/local/bin/x11docker-gui ] && rm -v /usr/local/bin/x11docker-gui
echo "Installing x11docker and x11docker-gui in /usr/bin"
cp x11docker /usr/bin/ || error "Could not copy x11docker to /usr/bin"
chmod 755 /usr/bin/x11docker || error "Could not set executeable bit on x11docker"
cp x11docker-gui /usr/bin/ && chmod 755 /usr/bin/x11docker-gui || warning "x11docker-gui not found"
echo "Creating icon and application entry for x11docker"
X11dockericonfile=$(x11docker-gui --icon)
[ -e "$X11dockericonfile" ] && {
xdg-icon-resource install --context apps --novendor --size 72 "$X11dockericonfile" x11docker
command -v xdg-icon-resource >/dev/null || warning "Could not install icon for x11docker.
Please install 'xdg-icon-resource' and try again."
rm $X11dockericonfile
} || note "Could not create icon for x11docker"
[ -e "/usr/bin/x11docker-gui" ] && {
echo "[Desktop Entry]
Version=1.0
Type=Application
Name=x11docker
Comment=Run GUI applications in docker images
Exec=x11docker-gui
Icon=x11docker
Categories=System
" > /usr/share/applications/x11docker.desktop
} || note "Did not create desktop entry for x11docker-gui"
command -v kaptain >/dev/null || warning "Could not find 'kaptain' for x11docker-gui.
Consider to install 'kaptain' (version 0.73 or higher).
It is needed for x11docker-gui.
If your distributions does not provide it, look at kaptain repository:
https://github.com/mviereck/kaptain
Fallback: x11docker-gui will try to use image x11docker/kaptain."
echo "Storing README.md and LICENSE.txt in /usr/share/doc/x11docker"
mkdir -p /usr/share/doc/x11docker && {
cp README.md /usr/share/doc/x11docker/
cp LICENSE.txt /usr/share/doc/x11docker/
} || note "Error while creating /usr/share/doc/x11docker"
echo "Installation ready: x11docker version $(x11docker --version)"
;;
--remove)
echo "removing x11docker from your system"
[ -x /usr/local/bin/x11docker ] && { # from older installations. /usr/bin is default now as /usr/local/bin can miss in $PATH for root
/usr/local/bin/x11docker --cleanup
rm -v /usr/local/bin/x11docker
rm -v /usr/local/bin/x11docker-gui
}
[ -x /usr/bin/x11docker ] && {
/usr/bin/x11docker --cleanup
rm -v /usr/bin/x11docker
rm -v /usr/bin/x11docker-gui
}
[ -e "/usr/share/applications/x11docker.desktop" ] && rm -v /usr/share/applications/x11docker.desktop
[ -e "/usr/share/doc/x11docker" ] && rm -R -v /usr/share/doc/x11docker
xdg-icon-resource uninstall --size 72 x11docker
note "Will not remove files in your home folder.
There may be files left in \$HOME/.local/share/x11docker
The symbolic link \$HOME/x11docker may exist, too.
The cache folder \$HOME/.cache/x11docker should be removed already."
;;
esac
# Cleanup
case ${1:-} in
--update)
echo "Removing downloaded files"
cd ~
rm -R /tmp/x11docker-install
;;
esac
echo "Ready."
}
checkorphaned() { # --cleanup : check for non-removed containers and left cache files
local Orphanedcontainers Orphanedfolders Line
note "x11docker will check for orphaned containers from earlier sessions.
This can happen if docker was not closed successfully.
x11docker will look for those containers and will clean up x11docker cache.
Caution: any currently running x11docker sessions will be terminated, too."
Orphanedcontainers=""
Orphanedfolders=""
cd $Cacherootfolder || error "Could not cd to cache folder '$Cacherootfolder'."
[ $? ] && [ -n "$(echo "$Cacherootfolder" | grep .cache/x11docker)" ] && Orphanedfolders=$(echo $(find "$Cacherootfolder" -mindepth 1 -maxdepth 1 -type d | sed s%$Cacherootfolder/%%))
Orphanedcontainers="$(docker ps -a --filter name=x11docker_X --format "{{.Names}}")"
Orphanedcontainers="$(env IFS='' echo $Orphanedcontainers)"
if [ -z "$Orphanedcontainers" ] && [ -z "$Orphanedfolders" ] ; then
note "No orphaned containers or cache files found. good luck!"
else
note "Found orphaned containers:
$Orphanedcontainers"
note "Found orphaned folders in $Cacherootfolder:
$Orphanedfolders"
for Line in $Orphanedfolders ; do
[ -d "$Cacherootfolder/$Line/share" ] && [ ! -e "$Cacherootfolder/$Line/share/timetosaygoodbye" ] && {
note "Found possibly active container $Line.
Will summon it to terminate itself."
touch "$Cacherootfolder/$Line/share/timetosaygoodbye" # terminating possibly running x11docker sessions
sleep 3
}
done
[ -n "$Orphanedcontainers" ] && {
note "Removing containers with: docker rm -f $Orphanedcontainers
$(bash -c "docker rm -f $Orphanedcontainers" 2>&1)"
}
[ -n "$Orphanedfolders" ] && {
note "Removing cache files with: rm -R -f $Orphanedfolders
$(rm -R -f $Orphanedfolders 2>&1)"
}
fi
note "Ready."
}
storepid () { # store pids and names of background processes in file $Bgpidfile
# store Pid and process name of background processes in file
# $1 should be Pid, $2 should be name of process
# for use on exit / with trap to clean up with background processes
# this subroutine has a twin in xinitrc
echo ${1:-} ${2:-} >> $Bgpidfile
verbose "stored background pid ${1:-} of ${2:-}"
}
saygoodbye() { # create file signaling watching processes to terminate
verbose "Creating $Timetosaygoodbye"
[ -e "$Sharefolder" ] && $Mksu "touch $Timetosaygoodbye"
}
finish() { # trap routine, clean up background processes and cache
local Pid Name Zeit
trap - EXIT
verbose "terminating x11docker ..."
saygoodbye
sleep 1 # a bit time for background processes like mywatch() to look for $Timetosaygoodbye (most look once a 1 second)
[ -s "$Bgpidfile" ] && {
# check for possible remaining background processes stored in $Bgpidfile
while read -r Line ; do
Pid=$(echo $Line | awk '{print $1}')
Name=$(echo $Line | awk '{print $2}')
ps -p $Pid --no-headers >/dev/null && {
verbose "terminating background pid $Pid of $Name"
case $Name in
windowmanager)
Windowmanager="$(basename "$Windowmanager" | cut -d' ' -f1)"
ps -p $Pid | grep $Windowmanager >/dev/null && pkill $Pid
;;
xpraserver|xpraclient)
ps -p $Pid | grep 'xpra' >/dev/null && pkill $Pid
;;
compositor)
ps -p $Pid | grep -E 'weston|kwin_wayland' >/dev/null && kill $Pid
;;
x11docker-run|mywatch|xpraloop)
ps -p $Pid | grep x11docker >/dev/null && pkill $Pid
;;
hostexe|shareclipboard)
ps -p $Pid | grep 'bash' >/dev/null && pkill $Pid
;;
xfishtank)
ps -p $Pid | grep 'xfishtank' >/dev/null && pkill $Pid
;;
container)
[ -n "$Sudo" ] && Sudo="sudo -n" # no password prompt here, rather fail
$Sudo docker stop $Containername >/dev/null 2>&1 || {
! $Sudo docker images >/dev/null 2>&1 || { [ -n "$($Sudo docker ps --filter name=$Containername --quiet 2>&1)" ] || ps -p $Pid >/dev/null ; } && {
note "Found remaining container process. Most probably the X session was
interrupted. Can not stop container because x11docker does not run as root.
Will wait up to 10 seconds for docker to finish."
Zeit=$(date +%s)
while ps -p $Pid >/dev/null ; do
note "waiting for container to terminate ..."
sleep 1
[ 10 -lt $(($(date +%s) - $Zeit)) ] && break ||:
done
if ps -p $Pid >/dev/null ; then
note "docker container didn't terminate as it should.
Will not clean cache to avoid file permission issues.
You can remove the new container with command:
docker rm -f $Containername
Afterwards, remove cache files with:
rm -R $Cachefolder
or let x11docker do the cleanup work for you:
x11docker --cleanup"
Preservecachefiles="yes"
else
note "docker container terminated successfully"
fi
}
}
;;
*) # should never happen (tm)
note "Found remaining background process.
Will send signal KILL to $Line
$(ps -p $Pid --no-headers)"
pkill -KILL $Pid
sleep 1
ps -p $Pid --no-headers && warning "error killing $Pid $Name"
;;
esac
ps -p $Pid --no-headers >/dev/null && sleep 1
ps -p $Pid --no-headers >/dev/null && warning "error terminating $Pid $Name"
}
done < <(tac $Bgpidfile)
}
sleep 0.5 # a bit time for all processes to finish log output
[ -n "$Logfile2$Logfile3" ] && $Mksu "cp '$Logfile2' '$Logfile3'"
rm "$Logfile"
[ "$Preservecontainer" = "yes" ] && Preservecachefiles="yes"
[ "$Preservecachefiles" = "no" ] && echo "$Cachefolder" | grep -q .cache && echo "$Cachefolder" | grep -q x11docker && [ "x11docker" != "$(basename "$Cachefolder")" ] && rm -f -R "$Cachefolder"
if [ -e "$Errorfile" ]; then rm "$Errorfile" ; exit 1; else exit 0; fi
}
verlte() { # version number check $1 less than or equal $2
[ "${1:-}" = "$(echo -e "${1:-}\n${2:-}" | sort -V | head -n1)" ] && return 0 || return 1
}
verlt() { # version number check $1 less than $2
[ "${1:-}" = "${2:-}" ] && return 1 || { verlte "${1:-}" "${2:-}" && return 0 || return 1 ; }
}
mywatch() { # repeat $1 untils its output changes
local Watchoutput
# --interval must be integer for centos and fedora depite contrary documentation in manpage
verbose "Watching: sh -c '${1:-}'
Current output:
$(sh -c "${1:-}")"
env TERM=linux watch --interval 1 --chgexit --no-title -- "${1:-}" >/dev/null 2>&1
#Watchoutput="$(sh -c "${1:-}" 2>&1)"
#while sleep 1 ; do
# [ "$Watchoutput" = "$(sh -c "${1:-}" 2>&1)" ] || break
#done
verbose "Stopped watching: sh -c '${1:-}'
Current output:
$(sh -c "${1:-}")"
}
isnum() { # check if $1 is a number
[ "1" = "$(awk -v a="${1:-}" 'BEGIN {print (a == a + 0)}')" ]
}
writeaccess() { # check if useruid $1 has write access to folder $2
local dirVals gMember IFS
if IFS=" " read -a dirVals < <(stat -Lc "%U %G %A" "${2:-}") && (
( [ "$(id -u $dirVals)" == "${1:-}" ] && [ "${dirVals[2]:2:1}" == "w" ] ) ||
( [ "${dirVals[2]:8:1}" == "w" ] ) ||
( [ "${dirVals[2]:5:1}" == "w" ] && (
gMember=($(groups ${1:-} 2>/dev/null)) &&
[[ "${gMember[*]:2}" =~ ^(.* |)${dirVals[1]}( .*|)$ ]]
) ) )
then
return 0
else
[ "w" = "$(getfacl -pn "${2:-}" | grep user:${1:-}: | rev | cut -c2)" ] && return 0 || return 1 # FIXME: could check write access for gid, if uid access fails.
fi
}
waitforfilecreation() { # similar to inotify-wait: wait up to 15s for file $1 to be created
# $1 file to wait for
# $2 time to wait. default: 15s. possible: infinity
local Zeit Warten
Zeit=$(date +%s)
verbose "Waiting for file creation of ${1:-}"
case $2 in
"") Warten=15 ;;
infinity|inf) Warten=32000 ;; # nearly infinity in fast-moving today ...
*) Warten=${2:-} ;;
esac
while [ ! "$(find "${1:-}" 2>/dev/null)" ] ; do
sleep 0.2
[ $Warten -lt $(expr $(date +%s) - $Zeit) ] && {
warning "Failed to wait for file creation of
${1:-}"
return 1
}
verbose "waiting since $(expr $(date +%s) - $Zeit)s for ${1:-} to be created, will wait up to $Warten seconds."
[ -e "$Timetosaygoodbye" ] && {
verbose "stopped waiting for ${1:-} due to terminating signal."
return 1
}
done
verbose "Found new created file $(ls ${1:-})"
sleep 0.1
return 0
}
waitforfilecontent() { # wait for file $1 to be not empty
local Zeit Warten
# $1 file to look at
Zeit=$(date +%s)
Warten=15
verbose "Waiting for file content in ${1:-}"
while [ ! -s "${1:-}" ] ; do
sleep 0.2
[ $Warten -lt $(expr $(date +%s) - $Zeit) ] && return 1
verbose "waiting since $(expr $(date +%s) - $Zeit)s for ${1:-} to have content, will wait up to $Warten seconds."
[ -e "$Timetosaygoodbye" ] && {
verbose "stopped waiting for file content of ${1:-} due to terminating signal."
return 1
} done
verbose "Found file content in ${1:-}"
return 0
}
waitforlogentry() { # wait for entry $3 in logfile $2 of application $1