In tradition, people pass-through GPU to VM, so after VM started, images from VM will show in screen. That’s what usually people use server as htpc do.
But we can’t pass-through GPU to container. In my previous post, I only use GPU to hardware accelerate. Container displays images by talking to host compositor socket. And after container started, only root user exists. I don’t recommend root user using desktop environment.
So, in this post, I will show you how to autostart container DE after host DE started.
Because container DE depends on host DE, we need to check host DE first. If host DE is X11, then everything is fine, display manager will do everything for us, including starting graphical render, autologin, autostart. But if is wayland, we have to so everything ourselves. Don’t argue with me about Gnome or KDE, they can use display manager, so use X11 way. A lot of wayland compositor don’t support display manager, so we have to start wayland compositor ourselves by typing in terminal or using .profile or systemd. After DE started, there will be a socket, X11 has /tmp/.X11-unix/X0, wayland has /run/user/1000/wayland-1. As long as the socket exists, we are good to go.
Because we use container, we have a container manager. I only know how to use lxd and incus, if you use anything else, you need to do research yourself. We need to use api to query container status, because you know to start a container DE you need to start the container first.
In lxd:
lxc query --request GET /1.0/instances/<instance_name>/state
In incus:
incus query --request GET /1.0/instances/<instance_name>/state
We only need to know status, as long as it’s running, we are good to go.
I will use incus, but don’t worry lxd is very similar to incus, I think only difference is lxd use lxc, incus use incus.
Before autostart CT DE, we need a usable CT DE, you can read this post:
If you want X11: https://discuss.linuxcontainers.org/t/incus-lxd-profile-for-gui-apps-wayland-x11-and-pulseaudio/
If you want wayland: https://discuss.linuxcontainers.org/t/a-detail-step-to-run-desktop-environment-in-container/
If you followed X11 post, you will have DISPLAY and XAUTHORITY ready. You only need to add start cmd in ~/.profile to start your DE, for example: startxfce4. But if you try to ssh into this CT after xfce4 already started, you will fail because xfce4 is already started. Unfortunately, unlike wayland, X11 doesn’t create another socket file after started, so there is no way to tell whether X11 started by file. But after hours testing, I found X11 DE always starts file manager, because file manager is the one managing background picture. So you can add this in ~/.profile if you use xfce in CT, or if you using another DE change Thunar to your file manager name and startxfce4 to your start cmd:
cmd="pidof Thunar"
$cmd
status=$?
if ! [ $status -eq 0 ]; then
nohup startxfce4 > /dev/null 2>&1 &
fi
If you followed wayland post, you will have XDG_RUNTIME_DIR and WAYLAND_DISPLAY ready. Only need to add this in CT ~/.profile, change compositor to your compositor name:
if ! [ -S "$XDG_RUNTIME_DIR/wayland-0" ] ; then
nohup compositor > /dev/null 2>&1 &
fi
I tried I can’t start compositor by using incus exec <instance_name> <compositor>
.And incus query --request POST /1.0/instances/<instance_name>/exec –data {"command": [ "compositor" ],"cwd": "/home/foo/","environment": {"FOO": "BAR"},"group": 1000,"interactive": true,"user": 1000,"width": 80}
started compositor, but the compositor is not usable.
I guess to start a compositor we really need a shell environment.
There are two option I know how to start compositor with shell environment, incus exec <instance_name> -- su -l <user>
and ssh <user>@<ip>
.
Incus exec faied because it need a shell too so either it won’t run or run with a terminal emulator.
Only ssh left.
We want to use ssh key to autologin, and auto kill ssh connection. I trust you know how to set static ip for CT.
In CT:
sed -i 's/#PubkeyAuthentication/PubkeyAuthentication/g' /etc/ssh/sshd_config
echo -e "TCPKeepAlive yes\nClientAliveInterval 300\nClientAliveCountMax 2" > /etc/ssh/sshd_config
In host:
ssh-keygen
ssh-copy-id -i <private key> <container user>@<container ip>
We can add this in ~/.ssh/config to login CT without password:
Host <ip>
IdentityFile /home/userName/.ssh/<private key>
It’s time to tell X11 window manager/wayland compositor to auto ssh to CTDE. But there are too many variations, some DE use ~/.autostart/some.desktop, some use ~/.config/DE/autostart, some use gui application. You need to find out yourself. Download script in GitHub - basicallynewbie/ctde: script for start incus/lxd container desktop environment using ssh. The ctde.py is the one starting CT DE, change it to fit you need. If your host DE uses desktop file to autostart just edit the path will be ok. You need to chmod +x to active them. I tested wayland nested in wayland, x11 nested in x11, and they both worked.
That’s it.