Skip to content

Instantly share code, notes, and snippets.

@yushijinhun
Last active March 11, 2024 12:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yushijinhun/ab68582a9a7adebd070502748da5afc2 to your computer and use it in GitHub Desktop.
Save yushijinhun/ab68582a9a7adebd070502748da5afc2 to your computer and use it in GitHub Desktop.
[LXD] Forward X11 & Wayland to container

Steps

First, create a LXD profile named forward-desktop:

lxc profile create forward-desktop

Edit the profile:

lxc profile forward-desktop

Replace config: {} and devices: {} with:

config:
  user.user-data: |
    #cloud-config
    packages:
      - va-driver-all
      - vdpau-driver-all
      - qtwayland5
      - at-spi2-core
    write_files:
      - path: /etc/systemd/system/setup-forward-symlinks.service
        content: |
          [Unit]
          After=sysinit.target

          [Service]
          Type=oneshot
          ExecStart=/opt/setup-forward-symlinks.sh

          [Install]
          WantedBy=multi-user.target
      - path: /opt/setup-forward-symlinks.sh
        permissions: '0755'
        content: |
          #!/bin/bash
          USER_UID=1000 # Replace this with your container user UID
          RUNTIME_DIR=/run/user/$USER_UID
          mkdir -p $RUNTIME_DIR
          chmod 700 $RUNTIME_DIR
          chown $USER_UID:$USER_UID $RUNTIME_DIR
          ln -sf /mnt/wayland-socket $RUNTIME_DIR/wayland-0
          chown $USER_UID:$USER_UID $RUNTIME_DIR
          mkdir -p /tmp/.X11-unix
          ln -sf /mnt/x11-socket /tmp/.X11-unix/X0
          chmod 776 /tmp/.X11-unix/X0
          chown $USER_UID:$USER_UID /tmp/.X11-unix/X0
    runcmd:
      - systemctl daemon-reload
      - systemctl enable setup-forward-symlinks.service
      - systemctl --no-block start setup-forward-symlinks.service
      # Replace '/home/user' with your home directory
      - 'echo "export DISPLAY=:0 WAYLAND_DISPLAY=wayland-0 XDG_SESSION_TYPE=wayland" >> /home/user/.bashrc'
devices:
  gpu:
    gid: "44" # GID of 'video' group
    type: gpu
  wayland-socket:
    bind: container
    connect: unix:/run/user/1000/wayland-0 # Replace this with `$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY`
    listen: unix:/mnt/wayland-socket
    type: proxy
    security.uid: "1000" # Replace this with your host user UID
    security.gid: "1000" # Replace this with your host user GID
    uid: "1000" # Replace this with your container user UID
    gid: "1000" # Replace this with your container user GID
  x11-socket:
    bind: container
    connect: unix:/tmp/.X11-unix/X0 # Replace 'X0' with your X display ($DISPLAY)
    listen: unix:/mnt/x11-socket
    type: proxy
    security.uid: "1000" # Replace this with your host user UID
    security.gid: "1000" # Replace this with your host user GID
    uid: "1000" # Replace this with your container user UID
    gid: "1000" # Replace this with your container user GID

Create a container with forward-desktop profile:

lxc launch ubuntu:20.04 test -p default -p forward-desktop

In container: Install and run gedit:

sudo apt install gedit --no-install-recommends --no-install-suggests
gedit

Test environment

  • Host: Ubuntu 21.04 (LXD 4.13)
  • Container: Ubuntu 20.04
  • GPU: Intel Graphics
    • Hardware acceleration with Intel GPU works fine
    • NVIDIA hasn't been tested

References

@KosmX
Copy link

KosmX commented Mar 11, 2024

Using it with the open source nVidia driver (NVK) should work fine
proprietary driver: make sure to match the kernel module version with the driver version exactly. (nvidia container toolkit can help with this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment