Kodi Community Forum

Full Version: [HOW TO] Multi separate sessions while keeping KODI running (aka Terminal Services)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A warning in advance : I'm not english, so bear with any mistakes that may be rampant in this WOT.

AFAIK, nothing like this has been posted around. It's also very simple to do and straightforward

Objective :
Run KODI fullscreen on the main monitor
while having a secondary session, completly independant, which allows remote connections through vnc
Both on the same computer, without any additionnal hardware, or virtual machine required
and not taking a lot of time to understand and running it, preferably out-of-the-box

Took me a while and tons of reading, but I finally got something that should be able to work for a long time.
Best of all, in fact it offer much more possibilities than it seems.



Links to the story and how it works, links (patch, module), and other stuff
see post #2



Requirements :
system : Ubuntu Desktop (with lightdm), and systemd.
Should be doable without one or the other, if not both, but you're good for some tinkering.

software : advamation virtual Framebuffer module.
This module, fully open source, is able to create a virtual video framebuffer device, allowing systemd to start a new lightdm instance on it, fully independant.
Available on their site, Download: advaboard_rpi1-20140626.tgz
The update is not needed, it's for other components we won't use
Source is available in the archive, MIT-/X11-license-like

The documentation is available here for the whole project,
and the one we need for the framebuffer is here



Limitations :
I prefer to start with the ugly part, to know into what we've moving.
  • command line is required
  • the max possible sessions is 32, including the initial one. It's just an arbitrary limit from the kernel, you can't create more than 32 framebuffer.
    By changing the value in Linux/include/uapi/linux/fb.h and recompiling the kernel, or asking nicely to kernel.org, it can be increased
  • as the driver is purely software, you'll need to install gnome-session-flashback. No discussion about it, Unity 3D is not usable, as it expect hardware acceleration the virtual FB can't provide, and will try to compensate by eating a whole CPU to do the rendering. And Unity 2D has been killed some times ago by Canonical. With gnome flashback, it'll drop to an expected 3%
  • not a limitation, but to get the hang of this, it's required to delve into systemd, lightdm, and xorg. But not too much, it's still at a sane level.
  • each seat/session will requires around 200Mb, for lightdm/dbus/vnc server/... That's the drawback, we asked for an independant screen, so we have it ... fully.
  • also, don't expect sound to works on the "virtual" sessions (in fact it's doable, but only if you can muster your courage to hack into pulseaudio in daemon mode)
  • lightdm doesn't easily handle change on the lightdm.conf. And systemd doesn't give a way to manually start a seat, so a lightdm stop/start might be required. Thus killing the kodi session
    The good new is this config file just need a one-time update, so no much trouble here.

  • don't get a look to your syslog the first time. You might not like it, especially because systemd force a udev scan each time you attach a new device. But with some preparation, just one time is required, even if you reboot after.
  • speaking about syslog, if you see a message about "kernel tainting", it's just because we've loaded a non signed module (the advamation kernel module). It's the Linux way to politely says you have a custom driver.




How to :


1 : preparations
Compilation requirements :

=> for advamation FB :
Code:
sudo apt-get update
sudo apt-get install make linux-headers-$(uname -r)
It's just to be sure, but everything should already be available in ubuntu desktop

=> for XOrg dummy drivers (video and input)
Code:
sudo apt-get install xserver-xorg-input-void xserver-xorg-video-fbdev

=> for gnome-flashback (you can do it much later if you want to see Unity cpu usage)
Code:
sudo apt-get install gnome-session-flashback



2 : Framebuffer compilation
get the advamation archive, unpack it, and copy the kernel/advamationfb directory somewhere natural for you : you'll need to load the module after each reboot, and might compile it again after a kernel update.
I personnally use a /server/<app> tree to separate from the distro, but whatever boat floats for you.

If you want, apply the patch I made. (see post #2

Nothing fancy, but I removed the 1024*1024 size limit, corrected the compilation warnings, added the full BPP range (8, 16, 24 and 32 bpp), and some minor cleaning.
It still works out of the box as-is, but with a 1024*1024 max size.


Go into the copied directory (cd ...)
then, run the command :
Code:
make

After the compilation is done, the driver is loader with :
Code:
sudo modprobe -a fb_sys_fops sysfillrect syscopyarea sysimgblt
sudo insmod ./advamationfb.ko

Should be good now, check this sys entry :
Code:
ls /sys/bus/platform/drivers/advamationfb/list_devices
If available, the module is loaded and running, it not, check the system and compilation logs.



3: virtual Framebuffer usage
To create a new framebuffer (FB) :
Code:
echo "800,600,16" > /sys/bus/platform/drivers/advamationfb/new_device
800,600 is the scren size, 16 is the bit per pixels (bpp). Possible values are 16, 24, 32 (and 8 with the patch)
you can create a maximum of 32 FB, with the same command.
Remember, the max res is 1024*1024 for the unpatched version

To list the existing FB :
Code:
cat /sys/bus/platform/drivers/advamationfb/list_devices
It'll return something around this :
0: fb1, 800x600x16
1: fb2, ...

To delete an existing FB :
Code:
echo 0 > /sys/bus/platform/drivers/advamationfb/delete_device
the 0 come from the first number when you list the existing devices.

For now, if you have deleted all of them, recreate a single FB



4: lightdm configuration
Before playing with it, we need multiples things properly set, starting with lightdm.

Drop this into your /etc/lightdm/lightdm.conf (create it if missing, root 644)
Code:
[lightdm]
logind-check-graphical=false

# settings for all seats
[Seat:*]
allow-guest=false
# greeter-setup-script=/usr/bin/numlockx on

# first and default seat
[Seat:seat0]
autologin-user=kodi

# second seat
[Seat:seat1-virtual]
xserver-config=/etc/X11/xorg.conf.advamation.0
allow-user-switching=false

If you want more explanation, go at the source, it's well documented

I've kept commented the numlockx line, uncomment it if you have problem with the numlock not activated (don't forget "apt-get intall numlockx" first )
User switching is deactivated, it messes with vnc. And with multiples virtual devices, there's no real need.
The xorg.conf.advamation.0 file is a config file for X.Org, to make it understand we're using virtual/dummy devices. It'll be defined after

One thing to know : lightdm loads the seat alphabetically, so even if seat0 is the first one in the list, you might got a bad surprise if the virtual seats come first when you sort them. So, while the name can be anything, taking a little care to ensure it'll be loaded after seat0 will help

When done, do a full lightdm restart :
Code:
sudo service lightdm stop && sudo service lightdm start

Note : using "sudo killall -SIGHUP lightdm" should do the trick, but I didn't have the expected result. Didn't investigate, so it could be a mistake on my end



5: Xorg configuration
Xorg will complain a lot if we don't set some things first for him.

So drop this file here : /etc/X11/xorg.conf.advamation.0 (root, 644)
Code:
# help source : http://xpra.org/xorg.conf

Section "ServerFlags"
  Option "DontVTSwitch" "true"
  Option "AllowMouseOpenFail" "true"
  Option "PciForceNone" "true"
  Option "AutoEnableDevices" "false"
  Option "AutoAddDevices" "false"
  Option "AutoAddGPU" "false"
EndSection


Section "Device"
    Identifier  "Advamation Framebuffer"
    Driver      "fbdev"
    Option      "fbdev" "/dev/fb1"
    Option      "IgnoreEDID" "true"
EndSection

Section "Monitor"
  Identifier "dummy_monitor"
  HorizSync   5.0 - 1000.0
  VertRefresh 5.0 - 200.0
EndSection

Section "Screen"
  Identifier  "Default Screen"
  Device      "Advamation Framebuffer"
  Monitor     "dummy_monitor"
EndSection

Section "ServerLayout"
    Identifier   "Advamation_Layout"
    Screen       "Default Screen"
    InputDevice  "dummy_mouse"
    InputDevice  "dummy_keyboard"
EndSection


Section "InputDevice"
  Identifier "dummy_mouse"
  Option "CorePointer" "true"
  Driver "void"
EndSection

Section "InputDevice"
  Identifier "dummy_keyboard"
  Option "CoreKeyboard" "true"
  Driver "void"
EndSection

One setting to be carefull with is this line : Option "fbdev" "/dev/fb1"
The /dev/fb1 parameted must be adjusted to the framebuffer created with the advamation kernel module. If it reports a /dev/fb2, this line must be changed accordingly.
Also, if you have multiples virtuals framebuffers, you must duplicate this file as many time (hence the .0 at the end), one for each framebuffer.



6: systemd configuration
This will need to be done only one time, but required for each framebuffer created.

First, we need to check if systemd is playing along by listing all the visible devices :
Code:
loginctl --no-pager seat-status
Lot of stuff here. Add a " | grep /fb " to the command

We should see something like this :
+-/sys/devices/pci0000:00/0000:00:02.0/graphics/fb0
+-/sys/devices/platform/advamationfb.0/graphics/fb1

First one is the physical card, second one is the virtual framebuffer

Now we can attach the FB to the seat we want to use :
Code:
loginctl attach seat1-virtual /sys/devices/platform/advamationfb.0/graphics/fb1
The udev rule has been created, you can check it here : /etc/udev/rules.d/

In case you need to delete and recreate the seat with a different name or FB, you will have to use those commands :
Code:
loginctl terminate-seat seat1-virtual
loginctl flush-devices
If you have multiples seats to create, flush-device is greatly overkill, so you need'll to edit the files in /etc/udev/rules.d/



7: cleanup and vnc
right after systemd has been set, you should have your X session already running, started by systemd
Check with a " ps -ef |grep Xorg ", if you see seat0 and seat1-virtual as 2 differents process, you're good.

Now, only vnc to launch, and it's done.

I'm used to x11vnc, so here's the command :
Code:
sudo apt-get install x11vnc
sudo x11vnc -display :1 -auth /var/run/lightdm/root/:1 -loop -forever -bg -o /var/log/x11vnc.1.log -rfbport 5901 -norepeat -quiet &
sudo is used to ensure any user can connect to vnc and use greeter
The directory /var/run/lightdm/root/ contains the lightdm cookies to allow vnc to plug itself into it.

You can connect with your vnc client now, using :1 as port (ie : kodibox:1 or kodibox:5901, it's the same as long as the base port starts at 5900)

If you did install gnome-fallback, on greeter, when you select any user, there's a little bolt in the upper right of the box. Use it to select gnome Metacity.
If you didn't, check with the top command the cpu usage while you use the UI, you'll see the compiz process (used by Unity) in the first line.
Lightdm is able to refresh itself for this, so you can install gnome-fallback, and it should be available right after a logoff.



8: daily/monthly use after a reboot
This is nice, but how does it work after a reboot ?

You'll need to load the module, create the virtual FB, and start vnc. As long as the settings don't change, there's no need to go back to lightdm and systemd configurations.

Code:
(as root)
modprobe -a fb_sys_fops sysfillrect syscopyarea sysimgblt
insmod ./advamationfb.ko

echo "1680,1050,24" > /sys/bus/platform/drivers/advamationfb/new_device

nohup x11vnc -display :1 ... &

done, nothing more.

If you want more virtual session, you'll need to create them with the module, add other seats in lightdm.conf, and assign the framebuffers to the seats.

The hardest part came from putting all this together, and the initial configuration. The rest is really a breeze, it takes only seconds to create other seats, and having a real TSE server with 10+ more users on a linux box.
Now I just need a Citrix-like framework over it, but I digress Big Grin
Links
Advamation module : ]website , direct link
advamation module patch : pastebin



About GIT
I'm not a GIT user (yeah, a shame). If someone want to take this and make a project, I would be gratefull.
I think there's something to do with this, I never seen something as easy as this to create a whole TSE server on Linux



Principle, and the story behind :
Quote:I found a lot of guides and how-to, either with vncserver or doing some multiseat configuration using xorg-dummy-video, but nothing I really got working or easy to install and manage.
Others was only working with a secondary video card, far from being a solution for my goals.

And systemd come, and broke a lot of stuff. But it has also some interesting idea behind.
The first one being that systemd is responsible of starting the lightdm manager. So I went back instead to what systemd look for to do this job.
The single element required is a video device, starting with a framebuffer in the /dev space. With this, systemd is able to create a new physical seat, and the dedicated session manager with it.

So I looked for a virtual framebuffer. But it has to be usable like any physical device, systemd won't see the xorg-video-dummy module (or I fumbled here and missed something)
Anyway, I started looking for a kernel virtual framebuffer, and finally found a little gem in the advamation toolbox for a rasp kit.

Compiled it, started it, created a new device, and systemd happily complied after attaching the new framebuffer, and I suddenly had a new seat on the fly.
Some tweaks to lightdm config file (and a restart), launched vnc on the new session display, and finally got what I was looking for since some years

But, as this configuration is also my line of work, I kept tinkering with this, and found I had much more than that : a fully terminal server running on linux.
There are some limitations, but I could start 6 independant framebuffer, with their own session and vnc server, and could have all of them working without interference.
With adequate hardware, more is possible, and very easily to create/configure.


About advamation
I don't even know them, I just found the driver and their site while searching for something that was loosely looking like this. A big thank for making this little gem.
I like the idea of the rasp combo they made, anyway. The possibilities with a rasp never stop to astonish.
and reserved