Creating a media control server. Solution for running headless!
#1
*EDIT* Re-wrote to clean it up and make it both clearer, and simpler. I hope it helps someone.

Running Kodi Headless Solution

My server is running 24 / 7. My Kodi machines on the lan are all pxe booted. Therefore they shouldn't have any services running on them other than Kodi / Desktop as needed, nothing relying on them anyway. Up till today my media center in my bedroom was on full time as it was also my Kodi library controller. All of the sources are defined here, get scanned into the sql server here first. The other machines all pull from the sql server. My goal was to be able to turn that machine off, and leave it off unless it was actually needed. Move library control to the server? Server is headless. How would this work?

First the why. I did this because the way things are these days conservation is key. If you want to save the planet, or money... either way cutting your electric use helps so it's a good thing regardless. No sense letting anything run when it is doing nothing 99.9% of the time.

I use Ubuntu 18.04 at home on my server. I have leveraged LXD containers to keep my services network capable if I need to change them to a new machine for whatever reason. I decided to put Kodi into an LXD container that would not have a screen attached. Here is the solution.

This guide requires a modest level of linux knowledge and command line ability.

Create your LXD container as needed. This will also work in a virtual machine, LXC container, or on the bare metal itself.

Your machine will need to have a static ip. Ubuntu 18.04 utilizes netplan. My netplan config from my container for example
Code:
network:
 version: 2
 renderer: networkd
 ethernets:
  eth0:
   dhcp4: false
   dhcp6: false
   addresses: [192.168.1.242/24]
   gateway4: 192.168.1.1
   nameservers:
    addresses: [192.168.1.253]

You will need a virtual display for this container. This tricks it into thinking that it has a monitor when it does not. Follow the instructions on this link.

https://askubuntu.com/questions/453109/a...plugged-in

Add the Kodi ppa and install the following software.
Code:
sudo add-apt-repository ppa:team-xbmc/ppa
sudo apt install lightdm kodi kodi-eventclients-kodi-send openbox x11vnc ssh

*NOTE* I start Kodi with LightDM + Openbox. Just a habit I've gotten into. I have no doubt this will work with a systemd service file as well.

Create a dedicated non privileged user to run Kodi.
Code:
sudo adduser kodi

Adjust lightdm to autologin to this user on boot.
Code:
echo "[Seat:*]
autologin-session=openbox
autologin-user=kodi
autologin-user-timeout=0" | sudo tee -a /usr/share/lightdm/lightdm.conf.d/60-kodilogin.conf

Create Openbox autostart file.
Code:
su kodi
mkdir -p /home/kodi/.config/openbox
echo "kodi &" > /home/kodi/.config/openbox/autostart

Create this systemd service file to run the vnc server.
Code:
echo "[Unit]
Description = Start and keep X11VNC server running

[Service]
User = kodi
Group = kodi
Type = simple
ExecStart = /usr/bin/x11vnc -display :0
Restart = always

[Install]
WantedBy = multi-user.target" | sudo tee -a /etc/systemd/system/vncstart.service

Enable service file.
Code:
sudo systemctl enable vncstart

Using rsync copy your existing main library source manager .kodi folder to the kodi user on this machine.
Code:
su kodi
rsync -auv --progress [email protected]:/home/kodi/.kodi ~/

At this point restart your container. When it comes up, Kodi should be running with all the settings and configuration of the former location you controlled sources from. I am currently using TightVNC Viewer to access this machine as needed. The port will be 5900.

This allows me to keep an instance of Kodi running at all times as long as the server is on. When the server notices a new file in the media directories it sends a library update command to this "machine" via ssh using kodi-send. One minute later the same command runs on all other Kodi locations on the lan, assuming they are active. This keeps the library consistent across the lan, without having to keep anything running that isn't absolutely necessary.

*EDIT 2*

This also allows one to use Kodi as a headless server to spread audio across your home. Just set the PULSE_SERVER variable in the containers /etc/environment to a remote pulseaudio server that is physically connected to your sound system or whatever. Good use for a RPi or something. Still working out a few kinks on this, but I have gotten the container to play audio on the living room computer / tv.

*EDIT 3*

Last bug worked out. Script to reacquire remote pulse server is here. This is little more than a hack, but since it should rarely need to run no worries. Disable above vncstart service file if using this script and replace with this service file.
Code:
[Unit]
Description = Reacquire remote pulse audio server when becomes available

[Service]
User = root
Group = root
EnvironmentFile = /etc/environment
Type = simple
ExecStart = /usr/local/bin/libserverload

[Install]
WantedBy = multi-user.target

Runs this script.
 
Code:
#!/bin/bash
LOCKFILE=/root/kodilock

while true ; do
        IFS=':' read -r type addr port <<< "$PULSE_SERVER"
        if [[ -e "$LOCKFILE" ]] ; then
                if ! ping -c 1 "$addr" ; then
                        [[ -e "$LOCKFILE" ]] && rm "$LOCKFILE"
                fi
        else
                if ping -c 1 "$addr" ; then
                        touch "$LOCKFILE"
                        sleep 30
                        systemctl restart lightdm
                else
                        [[ -e "$LOCKFILE" ]] && rm "$LOCKFILE"
                fi
        fi
        if ! pgrep x11vnc ; then
                su -c "x11vnc -display :0" kodi &
        fi
        sleep 5
done
Reply
#2
looks like a good guide and one I'd be interested in playing with.

One thing, in edit three you've written "The $REMOTE_PULSE_KODI variable should == the ip or fqdn of your pulse server target."

But I don't see a variable of this name in the scripts I've probably missed something...but hey figured worth mentioning
Reply
#3
(2020-05-22, 13:57)littlejeem Wrote: looks like a good guide and one I'd be interested in playing with.

One thing, in edit three you've written "The $REMOTE_PULSE_KODI variable should == the ip or fqdn of your pulse server target."

But I don't see a variable of this name in the scripts I've probably missed something...but hey figured worth mentioning

You are correct. I've edited the post a few times as I've been refining things a bit and missed removing some things. My most recent adjustment was to pull the pulse server ip directly from the $PULSE_SERVER variable which eliminated the need for the extra config file & variable. I've fixed that part in the above post. Thank you for catching that.
Reply
 
Thread Rating:
  • 0 Vote(s) - 0 Average



Logout Mark Read Team Forum Stats Members Help
Creating a media control server. Solution for running headless!00
This forum uses Lukasz Tkacz MyBB addons.