Running everything required for kodi in a LXC WITH NO SYSTEMD BULLSHIT WHATSOEVER

But…. why?

Because:

  • why not
  • it’s kinda fun
  • you get to learn things
  • I can try bleeding edge stuff in my Debian Stable host
  • some kind of containment, but as we’ll see this implementation is FAR FAR AWAY from secure things

Do the things

Setup the Host

So I’m running this on a almost-all-you-need-in-a-motherboard ASrock J3160DC. You need a new-ish kernel so all the good stuff in the Intel chipset is loaded. Also don’t disable stuff in your BIOS.

I’ll output video & sound through the HDMI cable.

Check that the following devices exist. If you don’t, try a more recent kernel (in this example I have linux-image-4.7.0-0.bpo.1-amd64 from jessie-backports).

# ls /dev/dri/card0 
/dev/dri/card0
# ls /dev/snd/
by-path  controlC0  hwC0D0  hwC0D2  pcmC0D0c  pcmC0D0p  pcmC0D1p  pcmC0D2c  pcmC0D3p  pcmC0D7p  pcmC0D8p  seq  timer

You’ll of course need some packages for your container.

apt-get install lxc 

While you’re here, remove some crap

apt-get remove --purge  systemd systemd-shim cgmanager

Not sure about this one:

apt-get install i965-va-driver

Create the Debian Sid Guest

This is valid only if you use a LV for your rootfs. Modify accordingly.

 lxc-create -n kodi-lxc -t debian -B lvm --vgname VG00 --fssize 5G -- -r sid

Update your LXC config. This is very dirty, as it gives your guest access to your host’s hardware which basically defeats the purpose of container. Oh well.

# Common configuration
lxc.include = /usr/share/lxc/config/debian.common.conf

# HERE DO YOUR NETWORK CONFIG
#lxc.network.type = veth
lxc.network.flags = up
# that's the interface defined above in host's interfaces file
lxc.network.link = ....
lxc.network.hwaddr = ....
lxc.network.ipv4 = ....
lxc.network.ipv4.gateway = ....
lxc.rootfs = /dev/VG00/kodi-lxc
lxc.mount.entry = /dev/snd dev/snd none bind,optional,create=dir
lxc.mount.entry = /dev/dri dev/dri none bind,optional,create=dir
lxc.mount.entry = /dev/input dev/input none bind,optional,create=dir
lxc.mount.entry = /dev/tty7 dev/tty7 none bind,optional,create=file
lxc.mount = /var/lib/lxc/kodi-lxc/fstab
lxc.utsname = kodi-lxc
lxc.arch = amd64
lxc.autodev = 1
lxc.kmsg = 0

lxc.cgroup.devices.allow = c 226:0 rwm # /dev/dri/card0
lxc.cgroup.devices.allow = c 136:6 rwm # /dev/console
lxc.cgroup.devices.allow = c 116:* rwm # /dev/snd/*
lxc.cgroup.devices.allow = c 13:* rwm  # /dev/input/* input devices
lxc.cgroup.devices.allow = c 4:7 rwm   # /dev/tty7	

Do what you need to connect to your guest, then it’s time for some the usual hygiene procedure.

echo -e "Package: systemd-sysv\nPin: release o=Debian\nPin-Priority: -1" > /etc/apt/preferences.d/no-systemd
echo -n "deb http://http.debian.net/debian sid main contrib non-free" > /etc/apt/sources.list
apt-get install sysvinit-core sysvinit-utils
apt-get remove --purge  systemd systemd-shim cgmanager
apt update; apt upgrade

Not sure all these are needed, but I did the following, and it works.

apt install alsa-utils i965-va-driver kodi mesa-utils xserver-xorg xserver-xorg-input-kbd xserver-xorg-video-all 

Kodi runs nicely as a normal user (stolen from Kodi’s wiki)

adduser --disabled-password --disabled-login --gecos "" kodi
usermod -a -G cdrom,audio,video,plugdev,users,dialout,dip,input kodi

To have all the things starting up when you boot your guest, put this in ̀/root/xinit.sh:

#!/bin/bash
/bin/bash --login -c "/usr/bin/X vt7"

And in your crontab

@reboot cd /root; bash xinit.sh

Your Xorg will be sad to not have any udev/evdev to help him figure out stuff, so disable auto-device-discovery-magic in a custom /etc/X11/xorg.conf:

Section "ServerLayout"
	Identifier  "Configured"
	Option "AutoAddDevices" "false"
EndSection
																														
Section "InputDevice"
	Identifier "Keyboard0"
	Driver "kbd"
	Option "XkbLayout" "fr"
EndSection
																														
Section "Screen"
	Identifier "Default Screen"
	Device "i915"
EndSection

Then for the user kodi, I made a silly script /home/kodi/kodi.sh

logger "Trying to start kodi"
while true ; do
    if [[ `pidof kodi.bin` == "" ]]; then
        if [ -f /tmp/.X0-lock ] ; then
            logger "X is here! starting kodi"
            DISPLAY=:0 kodi-standalone
            logger "Kodi over and out"
        fi
    else
        logger "kodi is around already"
        exit
    fi 
    sleep 2
done

that is started ̀@reboot in the user’s crontab.

Try everything ! oh oh oh oh ohhhh

WARNING WARNING WARNING WARNING

If you’re like me and like umask 0077, remember to umask 0022 before starting your LXC, when ̀lxc.autodev is set to 1:

lxc-stop --kill -n kodi-lxc ; umask 0022 ; lxc-start -n kodi-lxc -d

And after a minute you should see kodi coming around on your screen/TV.