Files
fikakassa/setup-fikakassa-kioskmode.sh
Joakim f247cd69bf N/A
2026-11-01 17:41:22 +01:00

468 lines
14 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
# Fikakassan kiosk setup - V5
# Debian 13 / X11 / Openbox / Chromium / NetworkManager
#
# V6 fixes touchscreen device detection and reuses saved Wi-Fi profiles 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:-Fikakassa-WiFi}"
WIFI_SSID="${WIFI_SSID:-}"
WIFI_PASSWORD="${WIFI_PASSWORD:-}"
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 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."
log "Installerar systempaket"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y \
network-manager \
openssh-server \
python3 \
python3-venv \
python3-pip \
xorg \
xinit \
openbox \
chromium \
unclutter \
x11-xserver-utils \
xinput \
iio-sensor-proxy \
fonts-dejavu \
fonts-liberation \
nftables \
rsync \
systemd-timesyncd
systemctl enable NetworkManager.service
systemctl enable ssh.service
systemctl enable nftables.service
systemctl enable systemd-timesyncd.service || true
# Reuse an existing NetworkManager Wi-Fi profile whenever possible.
# Only ask for SSID/password if no suitable saved profile exists.
WIFI_PROFILE_EXISTS=0
if nmcli -t -f NAME,TYPE connection show 2>/dev/null | awk -F: -v n="$WIFI_CONNECTION" '$1 == n && $2 == "802-11-wireless" {found=1} END{exit !found}'; then
WIFI_PROFILE_EXISTS=1
fi
if [[ -z "$WIFI_SSID" && "$WIFI_PROFILE_EXISTS" == "1" ]]; then
WIFI_SSID="$(nmcli -g 802-11-wireless.ssid connection show "$WIFI_CONNECTION" 2>/dev/null || true)"
fi
# If no named profile exists, reuse the currently active Wi-Fi profile.
if [[ -z "$WIFI_SSID" ]]; then
ACTIVE_WIFI_PROFILE="$(nmcli -t -f NAME,TYPE connection show --active 2>/dev/null | awk -F: '$2 == "802-11-wireless" {print $1; exit}')"
if [[ -n "$ACTIVE_WIFI_PROFILE" ]]; then
WIFI_CONNECTION="$ACTIVE_WIFI_PROFILE"
WIFI_PROFILE_EXISTS=1
WIFI_SSID="$(nmcli -g 802-11-wireless.ssid connection show "$WIFI_CONNECTION" 2>/dev/null || true)"
fi
fi
if [[ -z "$WIFI_SSID" ]]; then
read -r -p "Wi-Fi SSID för Samsung A50-hotspot: " WIFI_SSID
fi
if [[ "$WIFI_PROFILE_EXISTS" == "0" && -z "$WIFI_PASSWORD" ]]; then
read -r -s -p "Wi-Fi-lösenord: " WIFI_PASSWORD
echo
fi
log "Kontrollerar kiosk-användare"
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/"
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"
"$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 <<EOF2
[Unit]
Description=Fikakassan FastAPI
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=$APP_DIR
Environment=PYTHONUNBUFFERED=1
ExecStart=$APP_DIR/.venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 8000
Restart=always
RestartSec=2
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ProtectHome=true
ReadWritePaths=$APP_DIR/data
[Install]
WantedBy=multi-user.target
EOF2
install -d -m 755 "$APP_DIR/data"
systemctl daemon-reload
systemctl enable --now fikakassa.service
log "Konfigurerar Wi-Fi"
systemctl enable --now NetworkManager.service
nmcli radio wifi on
WIFI_IF="$(nmcli -t -f DEVICE,TYPE device status | awk -F: '$2 == "wifi" {print $1; exit}')"
[[ -n "$WIFI_IF" ]] || die "Hittade inget Wi-Fi-interface."
if [[ "$WIFI_PROFILE_EXISTS" == "1" ]]; then
log "Återanvänder sparad Wi-Fi-profil '$WIFI_CONNECTION'"
else
nmcli connection add type wifi \
ifname "$WIFI_IF" \
con-name "$WIFI_CONNECTION" \
ssid "$WIFI_SSID" \
wifi-sec.key-mgmt wpa-psk \
wifi-sec.psk "$WIFI_PASSWORD" \
ipv4.method auto \
ipv4.never-default yes \
ipv4.route-metric 600 \
ipv6.method disabled >/dev/null
fi
nmcli connection modify "$WIFI_CONNECTION" \
connection.autoconnect yes \
connection.autoconnect-retries 0 \
connection.autoconnect-priority 100 \
connection.mdns 0 \
ipv4.never-default yes \
ipv4.route-metric 600 \
ipv6.method disabled \
ipv6.never-default yes
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 återansluter automatiskt senare."
fi
log "Konfigurerar Ethernet"
while IFS=: read -r CON_NAME CON_TYPE; do
[[ "$CON_TYPE" == "802-3-ethernet" ]] || continue
nmcli connection modify "$CON_NAME" \
ipv4.never-default no \
ipv4.route-metric 100 \
ipv6.never-default no \
connection.autoconnect yes || true
done < <(nmcli -t -f NAME,TYPE connection show)
while read -r ETH_IF; do
[[ -n "$ETH_IF" ]] || continue
PROFILE="Fikakassa-Ethernet-$ETH_IF"
if ! nmcli -t -f NAME connection show | grep -Fxq "$PROFILE"; then
nmcli connection add type ethernet \
ifname "$ETH_IF" \
con-name "$PROFILE" \
ipv4.method auto \
ipv4.never-default no \
ipv4.route-metric 100 \
ipv6.method auto \
ipv6.never-default no \
connection.autoconnect yes >/dev/null
fi
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
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
ip protocol icmp accept
udp sport 67 udp dport 68 accept
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
udp sport 68 udp dport 67 accept
oifname "en*" accept
udp dport 53 accept
tcp dport 53 accept
udp dport 123 accept
ip daddr 10.0.0.0/8 accept
ip daddr 172.16.0.0/12 accept
ip daddr 192.168.0.0/16 accept
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
}
}
EOF2
nft -f /etc/nftables.conf
else
warn "Internet-lockdown är avstängt (LOCK_DOWN_INTERNET=0)."
fi
log "Stänger av sleep/blanking"
mkdir -p /etc/systemd/logind.conf.d
cat > /etc/systemd/logind.conf.d/90-fikakassa.conf <<'EOF2'
[Login]
HandlePowerKey=ignore
HandleSuspendKey=ignore
HandleHibernateKey=ignore
HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
IdleAction=ignore
EOF2
systemctl restart systemd-logind.service || true
log "Konfigurerar X11/Openbox"
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
EOF2
chown "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.xinitrc"
chmod 755 "$KIOSK_HOME/.xinitrc"
cat > /usr/local/bin/fikakassa-startx <<'EOF2'
#!/bin/sh
exec /usr/bin/startx
EOF2
chmod 755 /usr/local/bin/fikakassa-startx
# Orientation helper. It uses the accelerometer exposed through
# iio-sensor-proxy and rotates both the internal display and touchscreen.
cat > /usr/local/bin/fikakassa-orientation <<'EOF2'
#!/bin/sh
set -u
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"
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
# V5 tried to map device IDs to --name-only output by line number.
# That is not valid: xinput IDs are not array indexes. V6 instead gets
# the actual device name for every ID and only changes real touch devices.
xinput --list --id-only 2>/dev/null | while IFS= read -r id; do
[ -n "$id" ] || continue
info="$(xinput list "$id" 2>/dev/null || true)"
props="$(xinput list-props "$id" 2>/dev/null || true)"
printf '%s\n' "$props" | grep -q 'Coordinate Transformation Matrix' || continue
if printf '%s\n' "$info" | grep -Eqi 'touchscreen|touch screen|touchpad|ELAN|Wacom'; then
echo "Touch: id=$id $info -> $matrix" >> /tmp/fikakassa-orientation.log
xinput set-prop "$id" 'Coordinate Transformation Matrix' $matrix 2>/dev/null || true
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" <<EOF2
#!/bin/sh
xset s off
xset -dpms
xset s noblank
unclutter --timeout 2 --jitter 5 --ignore-scrolling &
# Start orientation watcher for Lenovo X1 Yoga.
/usr/local/bin/fikakassa-orientation >/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"
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 <<EOF2
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin $KIOSK_USER --noclear %I \\$TERM
Type=idle
EOF2
cat > "$KIOSK_HOME/.bash_profile" <<'EOF2'
if [ -z "$DISPLAY" ] && [ "$(tty 2>/dev/null)" = "/dev/tty1" ]; then
exec /usr/local/bin/fikakassa-startx
fi
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
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
cat <<EOF2
========================================
FIKAKASSAN KIOSK V6 ÄR KONFIGURERAD
========================================
Kiosk-användare: $KIOSK_USER
Applikation: $APP_DIR
Webbapp: $KIOSK_URL
Wi-Fi: $WIFI_SSID
Wi-Fi-profil: $WIFI_CONNECTION
Nätverk:
Wi-Fi = lokalt A50-nät, ingen default Internet-route
Ethernet = normal Internet-route, metric 100
Kiosk:
tty1 autologin -> Xorg -> Openbox -> Chromium
Rotation:
iio-sensor-proxy + monitor-sensor
display + touchscreen roteras automatiskt
touchscreen-device identifieras korrekt via xinput
FastAPI:
systemctl status fikakassa
journalctl -u fikakassa -f
Orientation-logg:
/tmp/fikakassa-orientation.log
Starta om när du är klar:
sudo reboot
OBS:
Om touch fortfarande inte följer skärmen, kontrollera:
xinput list --short
xinput list-props <ID>
och:
cat /tmp/fikakassa-orientation.log
Automatisk start efter strömavbrott kräver rätt BIOS/UEFI-inställning.
EOF2