Friday 14 June 2019

Containing your Gaming



Ever felt like playing a quick game of CS:GO while that compilation is finishing without having to install software that could impact how your current development environment is setup? The simple answer is to game inside a container.

The following are my configuration notes based upon the excellent articles by Stéphane Graber and Simos Xenitellis which allow you to firstly install (and subsequently remove) the containerisation software and then within that how to install and configure Steam to support local and streaming gaming.

# change the following as required
  # Ubuntu user: linuxium
  # Container: steam
  # Container user: linuxiumcomau
# update the current environment to the latest software
sudo apt update
sudo apt upgrade
# add LXD group required for containers and make yourself (linuxium) a member of this group
sudo groupadd --system lxd
sudo usermod -aG lxd linuxium
# reboot to ensure the group membership has updated
sudo reboot
# check you are a member of LXD group
id
# install the LXD container software
sudo snap install lxd
# initializse LXD making sure the 'size in GB' is adequate for gaming e.g. 15 is required for a local CS:GO
lxd init
# check with an LXD command just to make sure everything is working
lxc list
# create login alias for user linuxiumcomau (alternative to lxc exec steam -- /bin/login -p -f linuxiumcomau)
lxc alias add login 'exec @ARGS@ --mode interactive -- /bin/sh -ac $@linuxiumcomau - exec /bin/login -p -f '
# create a LXD gui profile
cat > lxdguiprofile.txt <<+
config:
  environment.DISPLAY: :0
  raw.idmap: both 1000 1000
  user.user-data: |
    #cloud-config
    runcmd:
      - 'sed -i "s/; enable-shm = yes/enable-shm = no/g" /etc/pulse/client.conf'
      - 'echo export PULSE_SERVER=unix:/tmp/.pulse-native | tee --append /home/linuxiumcomau/.profile'
    packages:
      - x11-apps
      - mesa-utils
      - pulseaudio
description: GUI LXD profile
devices:
  PASocket:
    path: /tmp/.pulse-native
    source: /run/user/1000/pulse/native
    type: disk
  X0:
    path: /tmp/.X11-unix/X0
    source: /tmp/.X11-unix/X0
    type: disk
  hostgpu:
    type: gpu
name: gui
used_by:
+
lxc profile create gui
cat lxdguiprofile.txt | lxc profile edit gui
# setup a bridge if using Steam streaming to get the container on the same network as Ubuntu
  nm-connection-editor
    Click on "+" button at the bottom.
    Choose "Bridge" and click "Create".
      The default bridge interface will be named bridge0.
    Click on "Add" button.
    Choose "Ethernet" and click "Create".
    In "Device" field, choose the interface to enslave to the bridge, e.g. eth0.
    Click on "General" tab, and check both "Automatically connect to this network when it is available" and "All users may connect to this network".
    Save the change.
    Click on "General" tab of the bridge, make sure two check boxes are enabled ("Automatically connect to this network when it is available" and "All users may connect to this network").
    Go to "IPv4 Settings" tab, and configure either DHCP or static IP address for the bridge.
      Note that you should use the same IPv4 settings as the enslaved Ethernet interface eth0.
    Finally, save the bridge settings.
    As you have the additional bridge connection you no longer need the previously configured wired connection so delete the original wired connection e.g. Ethernet connection 1.
      You will momentarily lose a connection since the IP address assigned to eth0 is taken by bridge0.
  # update the default profile to use this newly created bridge
  lxc profile edit default # replace lxdbr0 by bridge0
  # delete the old bridge
  lxc network delete lxdbr0
# create an Ubuntu container called steam and install Steam
lxc launch ubuntu:18.04 steam
lxc exec steam -- bash
  apt update
  apt upgrade
  # create user linuxiumcomau
  killall -u ubuntu
  groupmod -n linuxiumcomau ubuntu
  usermod -md /home/linuxiumcomau -l linuxiumcomau ubuntu
  usermod -aG users linuxiumcomau
  loginctl enable-linger linuxiumcomau
  sed -i 's/ubuntu/linuxiumcomau/' /etc/sudoers.d/90-cloud-init-users
  # install dekstop packages
  apt install adwaita-icon-theme-full ubuntu-desktop^
  # exit
lxc login steam
  sudo sed -i "s/; enable-shm = yes/enable-shm = no/g" /etc/pulse/client.conf
  echo export PULSE_SERVER=unix:/tmp/.pulse-native | tee --append /home/linuxiumcomau/.profile
  # exit
# assign the gui profile
lxc profile assign steam default,gui
lxc restart steam
lxc login steam
  firefox # go to Steam website (https://store.steampowered.com) and
    # click Install Steam (top right and then centre screen and save the .deb file)
  cd Downloads/
  sudo apt install ./steam_latest.deb
  steam # use ctrl and \ to terminate
  # exit
# create HUD entry
lxc file pull steam/home/linuxiumcomau/.local/share/Steam/tenfoot/resource/images/steam_tray_48.tga ~/.local/share/icons/
cat > ~/.local/share/applications/steam.desktop <<+
[Desktop Entry]
 Name=Steam
 Comment=Play games on Steam
 Exec=lxc exec steam -- sudo --user linuxiumcomau --login steam
 Icon=/home/linuxium/.local/share/icons/steam_tray_48.tga
 Terminal=false
 Type=Application
 Categories=Game;
+
# use HUD to search for Steam and then add to favourites
# launch Steam

# and to remove everything
# remove HUD entry from favourites
rm ~/.local/share/applications/steam.desktop ~/.local/share/icons/steam_tray_48.tga
# remove LXD
lxc stop steam --force
lxc delete steam
lxc  list
lxc delete <any other containers in list - should be empty in this example>
lxc image list
lxc image delete <any images in list - should be empty in this example>
lxc network list
lxc network delete <e.g. LXD bridge i.e. lxdbr0 if present>
echo '{"config": {}}' | lxc profile edit default
lxc storage volume list default
lxc storage volume delete default <whatever is in list - should be empty in this example>
lxc storage delete default
sudo snap remove lxd
sudo deluser lxd
sudo groupdel lxd

Please donate if you find this guide useful using the following link http://goo.gl/nXWSGf.