Thursday, June 19, 2014

XBMC - auto login, auto start, auto restart - systemd edition

These instructions are stupid, you should totally use this now. :)


Right up front I want to admit that I am not well versed in the inner workings of systemd and there are probably ways to get this done without resorting to "hax" as well. But I like easy, easy is simple by definition and I also like simple. Fancy that.

This is basically a refresh of an old post from 2012 that covers auto login and auto (re)start of xbmc under ubuntu/upstart. This time it is Arch Linux and, more specifically, systemd that is the obstacle. An obstacle which is not insurmountable, or at least not unworkaroundable as it were.

Assuming you run xbmc as the user: xbmc

Create the following file (and leading directories):
/etc/systemd/system/getty@tty1.service.d/autologin.conf

Run this: systemctl edit getty@tty1
Paste this:
[Service]
Type=simple
ExecStart=
ExecStart=-/usr/bin/agetty --autologin xbmc --noclear %I $TERM
Details here.

This logs the user xbmc in on tty1 at boot.

Autostart (and restart) XBMC
Create the file xbmc_loop.sh in /home/xbmc with the following content:
#!/bin/bash
# loop forever and ever and ever and ever ...
while : ; do
 /usr/bin/xinit /usr/bin/xbmc-standalone -- vt01 :0 -nolisten tcp
 clear

 echo "We crashed, how 'bout that?!"
 echo "Restarting XBMC ..."

 sleep 5
done
The vt01-part is important as it prevents xinit from starting another vt for X and in doing so loosing contact with our current authenticated session. This would, among other things, prevent easy use of systemd's powermanagement commands (suspend, poweroff etc.) from within XBMC. Falconindy of archlinux fame has written more about this here. If you are using Arch this is already taken care of by default so there is no need for the "vt01".

Now edit/create .bash_profile (or .bash_login or .profile, it doesn't matter) in /home/xbmc and enter this:
# check that we are actually at tty1 and are not logging in via ssh or in another vt on the machine.
if tty | grep -Fq tty1; then
 # Logged in at tty1, starting XBMC loop
 ~/xbmc_loop.sh &
fi
That should do it... the user xbmc will get logged in at boot and XBMC will be started within an authenticated session giving it access to shutdown, suspend etc.

Yay! ^^