From a2b7d470204898b8297469f9e9b6a3bb26bbc8cd Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Sat, 16 Jan 2021 11:43:05 +0200
Subject: [PATCH] Updates to the Kodi role and a little refactoring.

- Using the GBM backend for Kodi failed (maybe it's because I'm using
Flatpak, maybe not). Weston and Wayland failed (maybe the Weston version
in Buster is too old). Go back to good old Xorg.
- Configure Plymouth boot spalsh.
- Refactor GRUB and initramfs to the common debian_server role.
- Disable cgroup2 (unified hierarchy) because Docker in Buster doesn't
support it (Bullseye does) and with buster-backports it's now enabled by
default.
---
 kodi.yaml                                     |  1 -
 roles/debian_server/handlers/main.yml         |  8 +++
 roles/debian_server/tasks/main.yml            |  9 +++
 .../files/{kodi@.service => kodi.service}     | 20 ++----
 roles/kodi/files/xorg.service                 | 41 ++++++++++++
 roles/kodi/handlers/main.yml                  | 12 +++-
 roles/kodi/meta/main.yml                      | 13 ++++
 roles/kodi/tasks/main.yml                     | 67 +++++++++++++++++--
 8 files changed, 147 insertions(+), 24 deletions(-)
 create mode 100644 roles/debian_server/handlers/main.yml
 rename roles/kodi/files/{kodi@.service => kodi.service} (62%)
 create mode 100644 roles/kodi/files/xorg.service
 create mode 100644 roles/kodi/meta/main.yml

diff --git a/kodi.yaml b/kodi.yaml
index a6a5977..c8c0511 100644
--- a/kodi.yaml
+++ b/kodi.yaml
@@ -2,7 +2,6 @@
 - hosts:
     - kodi
   roles:
-    - debian_server
     - kodi
   become: true
   become_user: root
diff --git a/roles/debian_server/handlers/main.yml b/roles/debian_server/handlers/main.yml
new file mode 100644
index 0000000..84d9730
--- /dev/null
+++ b/roles/debian_server/handlers/main.yml
@@ -0,0 +1,8 @@
+---
+- name: Rebuild initramfs
+  command:
+    cmd: update-initramfs -u
+
+- name: Update GRUB
+  command:
+    cmd: update-grub
diff --git a/roles/debian_server/tasks/main.yml b/roles/debian_server/tasks/main.yml
index 4328752..af3cb9f 100644
--- a/roles/debian_server/tasks/main.yml
+++ b/roles/debian_server/tasks/main.yml
@@ -5,6 +5,15 @@
       - ansible_distribution == "Debian"
       - ansible_distribution_major_version|int >= 10
 
+- name: Disable cgroup2 for Docker
+  lineinfile:
+    backup: true
+    line: |-
+      GRUB_CMDLINE_LINUX_DEFAULT="quiet systemd.unified_cgroup_hierarchy=0"
+    path: /etc/default/grub
+  notify:
+    - Update GRUB
+
 - name: Enable the backports repo
   loop:
     - deb
diff --git a/roles/kodi/files/kodi@.service b/roles/kodi/files/kodi.service
similarity index 62%
rename from roles/kodi/files/kodi@.service
rename to roles/kodi/files/kodi.service
index 751fea9..7ca8002 100644
--- a/roles/kodi/files/kodi@.service
+++ b/roles/kodi/files/kodi.service
@@ -1,6 +1,6 @@
 [Unit]
 Description=Kodi Media Center on %i
-After=systemd-user-sessions.service sound.target network-online.target
+After=systemd-user-sessions.service sound.target network-online.target xorg.service
 
 # Require the library to be mounted.
 After=srv-library.mount
@@ -12,27 +12,17 @@ After=dbus.socket
 Conflicts=getty@%i.service
 Before=graphical.target
 
-# On systems without virtual consoles, don't start
-ConditionPathExists=/dev/tty0
-
 [Service]
 User=kodi
 PAMName=login
-ExecStart=flatpak run --device=all --filesystem=/srv/library tv.kodi.Kodi --standalone --windowing=gbm -fs
+Environment="DISPLAY=:0"
+ExecStart=flatpak run --device=all --filesystem=/etc/group --filesystem=/srv/library tv.kodi.Kodi --standalone --windowing=x11 -fs
 Type=simple
-Restart=on-abort
+Restart=always
 RestartSec=5
 KillMode=control-group
 
-# A virtual terminal is needed.
-TTYPath=/dev/%i
-TTYReset=yes
-TTYVHangup=yes
-TTYVTDisallocate=yes
-
 # Fail to start if not controlling the tty.
-StandardOutput=tty
-StandardInput=tty
 StandardError=journal
 
 # Log this user with utmp, letting it show up with commands 'w' and 'who'.
@@ -41,4 +31,4 @@ UtmpMode=user
 
 [Install]
 WantedBy=graphical.target
-DefaultInstance=tty7
+DefaultInstance=tty1
diff --git a/roles/kodi/files/xorg.service b/roles/kodi/files/xorg.service
new file mode 100644
index 0000000..cc0ae53
--- /dev/null
+++ b/roles/kodi/files/xorg.service
@@ -0,0 +1,41 @@
+[Unit]
+Description=X Windows display server
+After=systemd-user-sessions.service
+
+# D-Bus is necessary for contacting logind. Logind is required.
+Wants=dbus.socket
+After=dbus.socket
+
+Conflicts=getty@tty7.service
+Before=graphical.target
+
+# On systems without virtual consoles, don't start
+ConditionPathExists=/dev/tty7
+
+[Service]
+User=kodi
+SupplementaryGroups=tty
+PAMName=login
+ExecStart=startx
+Type=simple
+Restart=on-abort
+RestartSec=5
+KillMode=control-group
+
+# A virtual terminal is needed.
+TTYPath=/dev/tty7
+TTYReset=yes
+TTYVHangup=yes
+TTYVTDisallocate=yes
+
+# Fail to start if not controlling the tty.
+StandardOutput=tty
+StandardInput=tty
+StandardError=journal
+
+# Log this user with utmp, letting it show up with commands 'w' and 'who'.
+UtmpIdentifier=tty7
+UtmpMode=user
+
+[Install]
+WantedBy=graphical.target
diff --git a/roles/kodi/handlers/main.yml b/roles/kodi/handlers/main.yml
index d041eb1..63b8267 100644
--- a/roles/kodi/handlers/main.yml
+++ b/roles/kodi/handlers/main.yml
@@ -1,4 +1,10 @@
 ---
-- name: Rebuild initramfs
-  command:
-    cmd: update-initramfs -u
+- name: Restart Xorg
+  service:
+    name: xorg.service
+    state: restarted
+
+- name: Restart Kodi
+  service:
+    name: kodi.service
+    state: restarted
diff --git a/roles/kodi/meta/main.yml b/roles/kodi/meta/main.yml
new file mode 100644
index 0000000..312c74a
--- /dev/null
+++ b/roles/kodi/meta/main.yml
@@ -0,0 +1,13 @@
+---
+galaxy_info:
+  author: Nimrod Adar
+  company: Shore
+  description: Standalone Kodi
+  license: MIT
+  min_ansible_version: 2.10
+  platforms:
+    - name: Debian
+      versions:
+        - Buster
+dependencies:
+  - role: debian_server
diff --git a/roles/kodi/tasks/main.yml b/roles/kodi/tasks/main.yml
index 2c050d5..1c858da 100644
--- a/roles/kodi/tasks/main.yml
+++ b/roles/kodi/tasks/main.yml
@@ -1,16 +1,22 @@
 ---
 - name: APT install
   apt:
+    cache_valid_time: 3600
+    install_recommends: false
     name:
       - alsa-utils
       - avahi-daemon
       - desktop-base
       - flatpak
-      # - plymouth-themes
+      - plymouth-label
+      - plymouth-themes
       - firmware-linux
       - udisks2
       - unison
       - upower
+      - xorg
+    state: present
+    update_cache: true
 
 - name: Configure boot splash
   copy:
@@ -23,6 +29,31 @@
   notify:
     - Rebuild initramfs
 
+- name: Enable boot splash
+  lineinfile:
+    backup: true
+    # yamllint disable-line rule:line-length
+    line: GRUB_CMDLINE_LINUX_DEFAULT="quiet splash systemd.unified_cgroup_hierarchy=0"
+    path: /etc/default/grub
+  notify:
+    - Update GRUB
+
+- name: Create library mountpoint
+  file:
+    mode: 0o0755
+    path: /srv/library
+    state: directory
+
+- name: Mount library
+  ansible.posix.mount:
+    backup: true
+    fstype: auto
+    opts: defaults
+    path: /srv/library
+    src: UUID=cbc01549-0acf-472f-8964-2701e7fb4927
+    state: mounted
+  notify: Restart Kodi
+
 - name: Add Flatpak remotes
   with_dict:
     flathub: https://flathub.org/repo/flathub.flatpakrepo
@@ -43,6 +74,12 @@
 - name: Create user
   user:
     create_home: true
+    groups:
+      - audio
+      - cdrom
+      - input
+      - plugdev
+      - video
     home: /var/lib/kodi
     name: kodi
     password: '!'  # pragma: allowlist secret
@@ -50,16 +87,36 @@
     state: present
     system: true
     uid: 900
+  notify:
+    - Restart Xorg
+    - Restart Kodi
 
-- name: Copy service
+- name: Configure rootless Xorg
+  copy:
+    backup: true
+    content: |
+      allowed_users=anybody
+    dest: /etc/X11/Xwrapper.config
+    mode: 0o0644
+  notify:
+    - Restart Xorg
+
+- name: Copy services
+  with_items: &services
+    - xorg.service
+    - kodi.service
   copy:
     dest: /etc/systemd/system/
     mode: preserve
-    src: kodi@.service
+    src: '{{ item }}'
+  notify:
+    - Restart Xorg
+    - Restart Kodi
 
-- name: Enable service
+- name: Enable services
+  with_items: *services
   service:
     daemon-reload: true
     enabled: true
-    name: kodi@tty7.service
+    name: '{{ item }}'
     state: started
-- 
GitLab