From dafc37df96a9d6ac04309c57318d2e355fc4c134 Mon Sep 17 00:00:00 2001 From: Joakim Date: Sun, 1 Nov 2026 17:33:21 +0100 Subject: [PATCH] N/A --- setup-fikakassa-kioskmode.sh | 370 ++++++++++++++++------------------- 1 file changed, 171 insertions(+), 199 deletions(-) diff --git a/setup-fikakassa-kioskmode.sh b/setup-fikakassa-kioskmode.sh index e40e420..8976c1f 100755 --- a/setup-fikakassa-kioskmode.sh +++ b/setup-fikakassa-kioskmode.sh @@ -1,47 +1,28 @@ #!/usr/bin/env bash set -Eeuo pipefail -# Fikakassan kiosk setup +# Fikakassan kiosk setup - V5 # Debian 13 / X11 / Openbox / Chromium / NetworkManager # -# Run as root: -# sudo ./setup-kiosk.sh -# -# The script: -# - installs the kiosk stack and Python runtime -# - uses the existing Debian user as the kiosk user -# - installs the Fikakassan app from the directory containing this script -# - starts FastAPI automatically at boot -# - configures Wi-Fi through NetworkManager -# - autologins the kiosk user -# - starts Chromium in true kiosk mode -# - disables suspend/blanking and common escape routes -# - blocks ordinary Internet traffic over the Wi-Fi hotspot while allowing -# full Internet access over wired Ethernet when a cable is connected -# - keeps Wi-Fi and Ethernet connected simultaneously -# -# Before running, put this script in the root of the Fikakassan project. +# V5 adds automatic display + touchscreen rotation using iio-sensor-proxy. +# It is designed to be rerunnable on an already partially configured V4 host. KIOSK_USER="${KIOSK_USER:-joakim}" APP_DIR="${APP_DIR:-/opt/fikakassa}" KIOSK_URL="${KIOSK_URL:-http://127.0.0.1:8000/}" - -WIFI_CONNECTION="${WIFI_CONNECTION:-Fikakassan-WiFi}" +WIFI_CONNECTION="${WIFI_CONNECTION:-Fikakassa-WiFi}" WIFI_SSID="${WIFI_SSID:-}" WIFI_PASSWORD="${WIFI_PASSWORD:-}" - -# Set to 0 if you explicitly want ordinary Internet access from the kiosk. LOCK_DOWN_INTERNET="${LOCK_DOWN_INTERNET:-1}" log() { printf '\n\033[1;32m==> %s\033[0m\n' "$*"; } warn() { printf '\n\033[1;33mWARNING: %s\033[0m\n' "$*" >&2; } die() { printf '\n\033[1;31mERROR: %s\033[0m\n' "$*" >&2; exit 1; } -[[ $EUID -eq 0 ]] || die "Kör skriptet som root eller med sudo." -command -v apt-get >/dev/null || die "Det här skriptet är avsett för Debian/derivat med apt." +[[ $EUID -eq 0 ]] || die "Kör skriptet med sudo." +command -v apt-get >/dev/null || die "Det här skriptet kräver Debian/apt." SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" - [[ -f "$SCRIPT_DIR/app/main.py" ]] || die "app/main.py saknas. Kör skriptet från Fikakassans projektkatalog." [[ -f "$SCRIPT_DIR/requirements.txt" ]] || die "requirements.txt saknas." @@ -54,7 +35,6 @@ if [[ -z "$WIFI_PASSWORD" ]]; then fi log "Installerar systempaket" - export DEBIAN_FRONTEND=noninteractive apt-get update apt-get install -y \ @@ -69,55 +49,43 @@ apt-get install -y \ chromium \ unclutter \ x11-xserver-utils \ + xinput \ + iio-sensor-proxy \ fonts-dejavu \ fonts-liberation \ nftables \ - rsync + rsync \ + systemd-timesyncd systemctl enable NetworkManager.service systemctl enable ssh.service systemctl enable nftables.service +systemctl enable systemd-timesyncd.service || true log "Kontrollerar kiosk-användare" - -if ! id "$KIOSK_USER" >/dev/null 2>&1; then - die "Användaren '$KIOSK_USER' finns inte. Skapa användaren först eller ange KIOSK_USER=..." -fi - -# Use the existing Debian user for the kiosk session. Do not alter its -# existing group memberships or sudo permissions. +id "$KIOSK_USER" >/dev/null 2>&1 || die "Användaren '$KIOSK_USER' finns inte." usermod -s /bin/bash "$KIOSK_USER" +KIOSK_HOME="$(getent passwd "$KIOSK_USER" | cut -d: -f6)" +[[ -d "$KIOSK_HOME" ]] || die "Hittade inte hemkatalogen för $KIOSK_USER." log "Installerar Fikakassan i $APP_DIR" - install -d -o root -g root "$APP_DIR" rsync -a --delete \ --exclude='.venv/' \ --exclude='__pycache__/' \ --exclude='*.pyc' \ "$SCRIPT_DIR/" "$APP_DIR/" - -# The kiosk user needs to read the application, but not modify it. chown -R root:root "$APP_DIR" find "$APP_DIR" -type d -exec chmod 755 {} + find "$APP_DIR" -type f -exec chmod 644 {} + log "Skapar Python virtualenv" - rm -rf "$APP_DIR/.venv" python3 -m venv "$APP_DIR/.venv" - -# Do not execute the pip wrapper directly. Some Debian filesystems/policies -# can leave the wrapper without execute permission. Running pip through the -# virtualenv Python avoids that failure. -"$APP_DIR/.venv/bin/python" -m pip install \ - --disable-pip-version-check \ - --no-cache-dir \ - -r "$APP_DIR/requirements.txt" +"$APP_DIR/.venv/bin/python" -m pip install --disable-pip-version-check --no-cache-dir -r "$APP_DIR/requirements.txt" log "Skapar systemd-tjänst för Fikakassan" - -cat > /etc/systemd/system/fikakassa.service < /etc/systemd/system/fikakassa.service </dev/null 2>&1 || true nmcli connection add type wifi \ ifname "$WIFI_IF" \ @@ -168,8 +128,7 @@ nmcli connection add type wifi \ ipv4.method auto \ ipv4.never-default yes \ ipv4.route-metric 600 \ - ipv6.method disabled - + ipv6.method disabled >/dev/null nmcli connection modify "$WIFI_CONNECTION" \ connection.autoconnect yes \ connection.autoconnect-retries 0 \ @@ -179,17 +138,13 @@ nmcli connection modify "$WIFI_CONNECTION" \ ipv4.route-metric 600 \ ipv6.method disabled \ ipv6.never-default yes - -# Try immediately, but do not abort the installation if the A50 hotspot is -# currently off or temporarily not visible. if nmcli connection up "$WIFI_CONNECTION"; then log "Wi-Fi anslutet till $WIFI_SSID" else - warn "Wi-Fi-hotspotten '$WIFI_SSID' är inte tillgänglig just nu. Profilen är skapad och kommer att återansluta automatiskt när hotspotten startas." + warn "Wi-Fi-hotspotten '$WIFI_SSID' är inte tillgänglig just nu. Profilen är skapad och återansluter automatiskt senare." fi -# Wired Ethernet is the maintenance/Internet path. Ensure every currently -# known Ethernet profile prefers a normal DHCP default route with a low metric. +log "Konfigurerar Ethernet" while IFS=: read -r CON_NAME CON_TYPE; do [[ "$CON_TYPE" == "802-3-ethernet" ]] || continue nmcli connection modify "$CON_NAME" \ @@ -198,10 +153,6 @@ while IFS=: read -r CON_NAME CON_TYPE; do ipv6.never-default no \ connection.autoconnect yes || true done < <(nmcli -t -f NAME,TYPE connection show) - -# Create a DHCP Ethernet profile for each physical Ethernet interface that -# currently has no suitable profile. This makes plugging in a cable later -# reliably provide the maintenance/Internet path. while read -r ETH_IF; do [[ -n "$ETH_IF" ]] || continue PROFILE="Fikakassa-Ethernet-$ETH_IF" @@ -219,97 +170,54 @@ while read -r ETH_IF; do done < <(nmcli -t -f DEVICE,TYPE device status | awk -F: '$2 == "ethernet" {print $1}') log "Konfigurerar lokal brandvägg" - if [[ "$LOCK_DOWN_INTERNET" == "1" ]]; then - # The kiosk needs: - # - local LAN traffic to the A50/kiosk - # - DNS for NTP hostname resolution - # - UDP/123 for NTP - # - DHCP - # - established traffic - # - # It must NOT be able to browse the Internet through the phone. - # A wired Ethernet connection (interface names en*) is intentionally - # exempted from the outbound lockdown, so plugging in a cable gives - # normal Internet access for updates and development while Wi-Fi remains - # available for the A50/local kiosk network. - # - # All RFC1918 traffic is permitted so the Android bridge can talk to the - # kiosk even if Samsung changes the hotspot subnet. - - cat > /etc/nftables.conf <<'EOF' +cat > /etc/nftables.conf <<'EOF2' #!/usr/sbin/nft -f - flush ruleset table inet kiosk_filter { chain input { type filter hook input priority filter; policy drop; - iifname "lo" accept ct state established,related accept - - # ICMP is useful for diagnostics and does not provide Internet browsing. ip protocol icmp accept - - # DHCP client traffic. udp sport 67 udp dport 68 accept - - # SSH and Fikakassan are available from the local network only. ip saddr 10.0.0.0/8 tcp dport 22 accept ip saddr 172.16.0.0/12 tcp dport 22 accept ip saddr 192.168.0.0/16 tcp dport 22 accept - ip saddr 10.0.0.0/8 tcp dport 8000 accept ip saddr 172.16.0.0/12 tcp dport 8000 accept ip saddr 192.168.0.0/16 tcp dport 8000 accept } - chain forward { type filter hook forward priority filter; policy drop; } - chain output { type filter hook output priority filter; policy drop; - oifname "lo" accept ct state established,related accept - - # DHCP client. udp sport 68 udp dport 67 accept - - # A wired Ethernet connection is the maintenance/Internet path. - # Allow normal outbound traffic there; Wi-Fi remains locked to local - # networks plus DNS/NTP. oifname "en*" accept - - # DNS and NTP are explicitly allowed. udp dport 53 accept tcp dport 53 accept udp dport 123 accept - - # Permit local/private networks only. ip daddr 10.0.0.0/8 accept ip daddr 172.16.0.0/12 accept ip daddr 192.168.0.0/16 accept - - # ICMP for local diagnostics. ip daddr 10.0.0.0/8 ip protocol icmp accept ip daddr 172.16.0.0/12 ip protocol icmp accept ip daddr 192.168.0.0/16 ip protocol icmp accept } } -EOF - - nft -f /etc/nftables.conf +EOF2 +nft -f /etc/nftables.conf else warn "Internet-lockdown är avstängt (LOCK_DOWN_INTERNET=0)." fi -log "Stänger av automatisk sleep/blanking" - +log "Stänger av sleep/blanking" mkdir -p /etc/systemd/logind.conf.d -cat > /etc/systemd/logind.conf.d/90-fikakassa.conf <<'EOF' +cat > /etc/systemd/logind.conf.d/90-fikakassa.conf <<'EOF2' [Login] HandlePowerKey=ignore HandleSuspendKey=ignore @@ -317,114 +225,175 @@ HandleHibernateKey=ignore HandleLidSwitch=ignore HandleLidSwitchExternalPower=ignore IdleAction=ignore -EOF - +EOF2 systemctl restart systemd-logind.service || true log "Konfigurerar X11/Openbox" - -KIOSK_HOME="/home/$KIOSK_USER" -install -d -o "$KIOSK_USER" -g "$KIOSK_USER" \ - "$KIOSK_HOME/.config/openbox" \ - "$KIOSK_HOME/.config" - -cat > "$KIOSK_HOME/.xinitrc" <<'EOF' +install -d -o "$KIOSK_USER" -g "$KIOSK_USER" "$KIOSK_HOME/.config/openbox" "$KIOSK_HOME/.config" +cat > "$KIOSK_HOME/.xinitrc" <<'EOF2' #!/bin/sh exec openbox-session -EOF +EOF2 chown "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.xinitrc" chmod 755 "$KIOSK_HOME/.xinitrc" -# A small explicit launcher makes the kiosk session independent of shell -# startup quirks. -cat > /usr/local/bin/fikakassa-startx < /usr/local/bin/fikakassa-startx <<'EOF2' #!/bin/sh exec /usr/bin/startx -EOF +EOF2 chmod 755 /usr/local/bin/fikakassa-startx -cat > "$KIOSK_HOME/.config/openbox/autostart" < /usr/local/bin/fikakassa-orientation <<'EOF2' #!/bin/sh +set -u -# Disable screen blanking and DPMS. +DISPLAY=:0 +export DISPLAY + +USER_NAME="${SUDO_USER:-joakim}" +USER_HOME="$(getent passwd "$USER_NAME" | cut -d: -f6 2>/dev/null || echo /home/joakim)" +export XAUTHORITY="$USER_HOME/.Xauthority" + +get_output() { + xrandr --query 2>/dev/null | awk '/ connected/{print $1; exit}' +} + +set_touch_matrix() { + orientation="$1" + # Find likely touchscreen devices. Some X1 Yoga revisions expose names + # containing ELAN, Wacom or Touchscreen. + for id in $(xinput --list --id-only 2>/dev/null); do + name="$(xinput --list --name-only 2>/dev/null | sed -n "${id}p" 2>/dev/null || true)" + props="$(xinput list-props "$id" 2>/dev/null || true)" + if printf '%s\n' "$props" | grep -qi 'Coordinate Transformation Matrix'; then + label="$(xinput list --name-only 2>/dev/null | head -n "$((id + 1))" | tail -n 1 2>/dev/null || true)" + if printf '%s\n%s\n' "$name" "$label" | grep -Eqi 'touchscreen|touch screen|ELAN|Wacom|Touch'; then + case "$orientation" in + normal|undefined) + matrix='1 0 0 0 1 0 0 0 1' ;; + bottom-up) + matrix='-1 0 1 0 -1 1 0 0 1' ;; + left-up) + matrix='0 -1 1 1 0 0 0 0 1' ;; + right-up) + matrix='0 1 0 -1 0 1 0 0 1' ;; + *) + matrix='1 0 0 0 1 0 0 0 1' ;; + esac + xinput set-prop "$id" 'Coordinate Transformation Matrix' $matrix 2>/dev/null || true + fi + fi + done +} + +apply_orientation() { + orientation="$1" + output="$(get_output)" + [ -n "$output" ] || return 0 + + case "$orientation" in + normal|undefined) + rotation=normal ;; + bottom-up) + rotation=inverted ;; + left-up) + rotation=left ;; + right-up) + rotation=right ;; + *) + rotation=normal ;; + esac + + xrandr --output "$output" --rotate "$rotation" 2>/dev/null || true + set_touch_matrix "$orientation" +} + +# Wait for X to be ready. +until xrandr --query >/dev/null 2>&1; do sleep 1; done + +# monitor-sensor prints orientation changes. The first orientation line is +# normally emitted immediately after connecting to the sensor service. +monitor-sensor 2>/dev/null | while IFS= read -r line; do + case "$line" in + *"orientation changed:"*) + orientation="${line##*: }" + apply_orientation "$orientation" + ;; + *"orientation:"*) + orientation="${line##*: }" + apply_orientation "$orientation" + ;; + esac +done +EOF2 +chmod 755 /usr/local/bin/fikakassa-orientation + +cat > "$KIOSK_HOME/.config/openbox/autostart" </tmp/fikakassa-orientation.log 2>&1 & + +exec chromium \ + --kiosk \ + --start-fullscreen \ + --no-first-run \ + --no-default-browser-check \ + --disable-session-crashed-bubble \ + --disable-infobars \ + --disable-translate \ + --disable-features=TranslateUI \ + --password-store=basic \ + --disable-pinch \ + --overscroll-history-navigation=0 \ + --disable-background-networking \ + --disable-component-update \ + --disable-domain-reliability \ + --disable-sync \ + --metrics-recording-only \ + --noerrdialogs \ "$KIOSK_URL" -EOF +EOF2 chown -R "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.config" chmod 755 "$KIOSK_HOME/.config/openbox/autostart" log "Konfigurerar automatisk inloggning på tty1" - mkdir -p /etc/systemd/system/getty@tty1.service.d -cat > /etc/systemd/system/getty@tty1.service.d/override.conf < /etc/systemd/system/getty@tty1.service.d/override.conf < "$KIOSK_HOME/.bash_profile" <<'EOF' -# Start graphical kiosk session automatically on tty1. +EOF2 +cat > "$KIOSK_HOME/.bash_profile" <<'EOF2' if [ -z "$DISPLAY" ] && [ "$(tty 2>/dev/null)" = "/dev/tty1" ]; then exec /usr/local/bin/fikakassa-startx fi -EOF +EOF2 chown "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.bash_profile" chmod 644 "$KIOSK_HOME/.bash_profile" systemctl daemon-reload systemctl enable getty@tty1.service +systemctl enable --now systemd-timesyncd.service || true -# Validate the files before restarting tty1. This is intentionally fatal: a -# kiosk without its graphical startup should not silently fall back to tty. +log "Kontrollerar kioskfiler" test -x "$KIOSK_HOME/.xinitrc" test -x "$KIOSK_HOME/.config/openbox/autostart" test -f "$KIOSK_HOME/.bash_profile" test -x /usr/local/bin/fikakassa-startx +test -x /usr/local/bin/fikakassa-orientation -# Restarting getty will log the current terminal user out. This is expected -# when the script is run interactively on tty1. -log "Sätter systemtid/NTP" - -# systemd-timesyncd is enough; no Internet browsing is required. -apt-get install -y systemd-timesyncd -systemctl enable --now systemd-timesyncd.service || true - -log "Aktiverar automatisk omstart av Chromium om X-sessionen kraschar" - -# Xorg/startx returns to tty1 if Chromium/Openbox dies. systemd getty will -# respawn the login shell, which starts X again through .bash_profile. - -cat < Xorg -> Openbox -> Chromium -Internet-lockdown: - $([[ "$LOCK_DOWN_INTERNET" == "1" ]] && echo "AKTIV: endast lokalt nät + DNS/NTP" || echo "AVSTÄNGD") +Rotation: + iio-sensor-proxy + monitor-sensor + display + touchscreen roteras automatiskt -Starta om maskinen nu: - reboot +FastAPI: + systemctl status fikakassa + journalctl -u fikakassa -f + +Orientation-logg: + /tmp/fikakassa-orientation.log + +Starta om när du är klar: + sudo reboot OBS: - "Power on efter strömavbrott" är en BIOS/UEFI-inställning och kan inte - garanteras av Linux. På X1 Yoga behöver "Power on with AC attach" / - motsvarande BIOS-inställning aktiveras om maskinen ska starta automatiskt - efter ett strömavbrott. + Om skärmen roteras men touch inte gör det korrekt, kontrollera: + xinput list + och: + cat /tmp/fikakassa-orientation.log -EOF + Automatisk start efter strömavbrott kräver rätt BIOS/UEFI-inställning. + +EOF2