Skip to content

About

OpenWrt package for Headscale (self-hosted Tailscale control server) with automatic storage media detection and aggressive RAM flash protection.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

openwrt-headscale

OpenWrt package for Headscale (an open-source, self-hosted implementation of the Tailscale control server).

Companion Web UI: For full LuCI web management (Overview, Settings, Nodes, Users, Pre-Auth Keys, ACL Policy, Live Logs, Client Onboarding Guide, and Flash Protection status), install luci-app-headscale alongside this package.


Features

  • Native OpenWrt Lifecycle: Managed by OpenWrt procd (START=90, STOP=10) and standard UCI configuration (/etc/config/headscale).
  • Dynamic Runtime Configuration: Generates /var/etc/headscale/config.yaml and captures daemon logs in /var/log/headscale/headscale.log (both in tmpfs RAM) with an /etc/headscale/config.yaml symlink for seamless headscale CLI usage.
  • Smart Storage Media Auto-Detection & Flash Protection: Automatically probes the hardware storage backing database.sqlite_path and switches between Aggressive RAM Mode (for raw flash / SD cards) and Direct Disk I/O Mode (for eMMC / USB / SSD / HDD).
  • Sysupgrade Persistence: Preserves /etc/config/headscale, ACL policies, SQLite database, and private keys (noise_private.key, derp_server_private.key) via conffiles.

Smart Storage Detection & Flash Protection (storage_mode)

Headscale continuously updates its SQLite database (last_seen node heartbeats, ephemeral timeouts, WAL frames). On routers without hardware wear-leveling, frequent synchronous writes can accelerate flash degradation.

/usr/share/headscale/headscale.sh automatically resolves the real physical storage device behind headscale.database.sqlite_path (traversing OpenWrt overlayfs upperdir mounts and /dev/loop* backing files via /proc/mounts and /sys/block) and applies the appropriate strategy when storage_mode is set to auto (default):

Detected Storage Media Detection Signals Default (auto) Behavior
Embedded Raw Flash ubifs, jffs2, /dev/ubi*, /dev/mtdblock*, sysfs /mtd/ or /ubi/ Aggressive RAM Mode
TF / SD Card /dev/mmcblk* with device/type == SD Aggressive RAM Mode
RAM Disk tmpfs, ramfs Aggressive RAM Mode
Built-in eMMC /dev/mmcblk* with device/type == MMC Direct Disk I/O Mode
USB Storage /dev/sd* backed by a /usb sysfs path Direct Disk I/O Mode
HDD / SSD / NVMe / VM Disk nvme*, non-USB sd*, vd*, hd*, xvd* Direct Disk I/O Mode

How Aggressive RAM Mode Works

  1. Zero Everyday Flash Writes: On startup, the persistent database (sqlite_path, default /etc/headscale/db.sqlite) is staged into /tmp/headscale/db.sqlite (tmpfs), and Headscale runs strictly against the in-memory database.
  2. Atomic Persistence with .bak Snapshot:
    • On clean stop, restart, or reboot (service_stopped() waits until procd_kill finishes shutting down Headscale and checkpointing SQLite WAL), /tmp/headscale/db.sqlite is hashed/compared (cmp -s), written to ${sqlite_path}.tmp, synced, rotated over ${sqlite_path}.bak, and atomically moved into sqlite_path. Unchanged databases perform zero flash writes.
    • Manual or admin-triggered snapshots via /etc/init.d/headscale sync_db use sqlite3 .backup when sqlite3 is installed, or perform a fast clean WAL-checkpoint quiesce cycle when sqlite3 is absent.
    • When paired with luci-app-headscale, administrative mutations (creating/deleting users, pre-auth keys, or modifying/deleting nodes) automatically trigger sync_db so structural changes survive sudden power loss.

Companion LuCI Package

luci-app-headscale (+headscale) builds directly on top of this package:

  • Calls /usr/share/headscale/headscale.sh probe to display live detected storage media, active mode, and sync state in the Overview and Settings → Database & Storage views.
  • Calls /etc/init.d/headscale sync_db for one-click manual sync and automatic post-mutation persistence.

Supported Architectures

  • Supported: x86_64, aarch64_generic, aarch64_cortex-a53 (OpenWrt 24.10 .ipk & OpenWrt 25.12+ .apk prebuilt in Releases).
  • Not Supported (mips / mipsel, e.g., mipsel_24kc / MT7621): Upstream Headscale 0.29.x relies on modernc.org/libc (pure-Go SQLite), which does not support 32-bit linux/mipsle. Devices based on mips / mipsel cannot compile or run this package.

Building

  1. Add both this package and luci-app-headscale (optional Web UI) into your OpenWrt buildroot:

    cd /path/to/openwrt
    git clone https://github.com/permails/openwrt-headscale.git package/headscale
    git clone https://github.com/permails/luci-app-headscale.git package/luci-app-headscale
  2. Update feeds and install dependencies:

    ./scripts/feeds update -a
    ./scripts/feeds install -a
  3. Select the package(s) in menuconfig:

    • Network -> VPN -> headscale (<*>)
    • LuCI -> 3. Applications -> luci-app-headscale (<*>, if using the Web UI)
  4. Compile:

    make package/headscale/compile V=s

Configuration & CLI Usage

Configuration lives in /etc/config/headscale. Key database options:

config headscale 'database'
	option type 'sqlite'
	option storage_mode 'auto'
	option sqlite_path '/etc/headscale/db.sqlite'
  • storage_mode: 'auto' (hardware auto-detection), 'ram' (force Aggressive RAM Mode), or 'direct' (force Direct Disk I/O).
  • sqlite_path: Persistent SQLite file path (e.g., /etc/headscale/db.sqlite, or an external mount such as /mnt/sda1/headscale/db.sqlite).

Enable and start the service:

uci set headscale.server.enabled='1'
uci commit headscale
/etc/init.d/headscale enable
/etc/init.d/headscale start

Storage helper & Headscale CLI commands:

# Inspect detected storage media and sync state (JSON)
/usr/share/headscale/headscale.sh probe

# Force-sync RAM database to persistent storage
/etc/init.d/headscale sync_db

# Standard Headscale CLI operations
headscale users create myuser
headscale preauthkeys create -u myuser --reusable --expiration 24h
headscale nodes list

Firewall

When exposing Headscale to WAN/external networks, ensure the HTTP listen port (default TCP 8080, or 8188 when configured via luci-app-headscale) and embedded DERP STUN port (default UDP 3478) are allowed in /etc/config/firewall.


Maintainer & License

About

OpenWrt package for Headscale (self-hosted Tailscale control server) with automatic storage media detection and aggressive RAM flash protection.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages