I showed you how to nest container’s X11 in host’s X11 window manager in: https://discuss.linuxcontainers.org/t/a-detail-step-to-run-desktop-environment-in-container/ .
Well, I cannot dodge X11 window manager, because there is no X11 compositor available. So, this time I’m going to show you how to use xinit as X11 window manager. It’s the minimum way to start X11 in host as I know. But it’s not perfect, will be retaining a running terminal emulator which will kill host X11 when killed.
requirements in host:
- incus/lxd
- a terminal emulator(debian came with xterm)
- xinit (you don’t really need it, see my next post)
- xorg
Let’s start X11 in host. It’s very simple: startx
. And you will see a black screen with a terminal emulator, it’s xterm by default, but can be another if you have ever run sudo update-alternatives --config x-terminal-emulator
.
That’s all we need to do in host.
profile
config:
raw.idmap: |-
uid 1000 1000
gid 1000 1000
environment.DISPLAY: ':0'
environment.XAUTHORITY: '/mnt/.Xauthority'
devices:
mygpu:
gid: "1000"
gputype: physical
mode: "0770"
pci: 0000:00:02.0
type: gpu
uid: "1000"
x11:
bind: container
connect: unix:@/tmp/.X11-unix/X0
listen: unix:@/tmp/.X11-unix/X0
security.gid: "1000"
security.uid: "1000"
type: proxy
xauth:
path: /mnt/.Xauthority
propagation: shared
source: /home/UsernameInHost/.Xauthority
type: disk
Let’s create a container: incus launch images:debian/12 ct -p default -p profile
Use any distribution you like, then create a user: useradd -m -G audio,video,sudo -s /bin/bash <user>
Install any X11 desktop environment you like: apt install <desktop environment> --no-install-recommends
Switch to the user just created: su -l <user>
Start the desktop environment, I’m using mate-desktop-environment-core, so: mate-session
As you see, the terminal emulator still exists. Don’t kill it otherwise host X11 goes down with it, just move it to another workspace, or just use another workspace.
The interesting part is the background still exists after container desktop environment shutdown, unlike nest xfce4 in Fluxbox which background will be destroyed.
After testing api, I succeeded open container desktop environment with one line of code, although I don’t know what environment values are necessary:
incus query --request POST --wait /1.0/instances/ct/exec --data '{"command":["sh","-c", "mate-session"],"environment":{"DISPLAY":":0","XAUTHORITY": "/mnt/.Xauthority","SHELL":"/bin/bash","CWD":"/home/<user>","HOME":"/home/<user>","LANG":"en_US.UTF-8","XDG_SESSION_CLASS":"user","TERM":"xterm-256color","USER":"<user>","SHLVL":"1","XDG_RUNTIME_DIR":"/run/user/1000","PATH":"/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"},"group": 1000,"user": 1000}'
I tried a day to test xinit and startx to start without a terminal emulator, but no luck. They will not let container using X11 socket even with a slightly change. If anyone knows how, please share your comment.