Skip to content

Instantly share code, notes, and snippets.

@SCP002
Last active November 25, 2024 21:36
Show Gist options
  • Save SCP002/b8b23bc1bf32b2dab9569d70996c0553 to your computer and use it in GitHub Desktop.
Save SCP002/b8b23bc1bf32b2dab9569d70996c0553 to your computer and use it in GitHub Desktop.
Linux: Create a container with GUI, graphics card and audio support using LXC

Configuring LXC (LXD / Incus) container with GUI, graphics card and audio support

Install LXD

sudo snap install lxd

Create network

sudo lxc network create lxcnet --type=bridge ipv4.address=10.0.0.1/24 ipv4.nat=true

Create storage pool

sudo lxc storage create storagepool zfs

Create profile

sudo lxc profile create desktop
sudo lxc profile edit desktop

Profile file contents:

name: desktop
description: Container with GUI, graphics card and audio support
devices:
  PASocket0:
    bind: container
    connect: unix:/run/myusername/1000/pulse/native
    gid: '1000'
    listen: unix:/home/ubuntu/pulse-socket
    mode: '0777'
    security.gid: '1000'
    security.uid: '1000'
    type: proxy
    uid: '1000'
  X0:
    bind: container
    connect: unix:/tmp/.X11-unix/X1
    listen: unix:/tmp/.X11-unix/X0
    security.gid: '1000'
    security.uid: '1000'
    type: proxy
  gpu0:
    type: gpu
config:
  boot.autostart: 'false'
  environment.DISPLAY: ':0'
  environment.PULSE_SERVER: unix:/home/ubuntu/pulse-socket
  nvidia.driver.capabilities: graphics, compute, display, utility, video
  nvidia.runtime: 'true'

This profile makes the following assumptions (you need to adapt it to fit your setup):

  • Host and container are Ubuntu 24.04.
  • Host window system is X11.
  • Host X11 socket file name is X1 (check ls /tmp/.X11-unix/ and replace X1 in devices.X0.connect with what command gave to you).
  • Host graphics card is nvidia.
  • Host user name is myusername (replace it with yours in devices.PASocket0.connect line).
  • Host User Identifier (UID) is 1000.
  • Host Group Identifier (GID) is 1000.

Create and start the instance

sudo lxc launch ubuntu:24.04 mycontainer --profile=desktop --storage=storagepool --network=lxcnet

Attach to the container

sudo lxc exec mycontainer -- bash

Install required packages

apt update
apt install libgl1 pulseaudio --yes

Integration (optional)

In this exaple we are trying to create a desktop shortcut for the game Warzone 2100, from container warzone2100.

Create .desktop file:

tee /tmp/warzone2100.desktop > /dev/null << ENDSEQ
#!/usr/bin/env xdg-open

[Desktop Entry]
Type=Application
Exec=pkexec sh -c "lxc start warzone2100; lxc exec warzone2100 -- /usr/games/warzone2100.real && lxc stop warzone2100"
Terminal=false
Name=Warzone 2100
ENDSEQ

Add .desktop file to applications menu:

sudo desktop-file-install /tmp/warzone2100.desktop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment