diff --git a/docs/README.md b/docs/README.md index 266fad593..af5f9cc31 100644 --- a/docs/README.md +++ b/docs/README.md @@ -76,6 +76,8 @@ If your server and services experience issues, feel free to come to [our support - [Alternative architectures](alternative-architectures.md) +- [Configuring Synology DSM](configuring-playbook-synology.md) + - [Container images used by the playbook](container-images.md) - [Obtaining an Access Token](obtaining-access-tokens.md) diff --git a/docs/configuring-playbook-synology.md b/docs/configuring-playbook-synology.md new file mode 100644 index 000000000..7a90bd1c0 --- /dev/null +++ b/docs/configuring-playbook-synology.md @@ -0,0 +1,162 @@ + + +# Configuring Synology DSM + +This document is a guide for preparing Synology DSM for the installation of the [Matrix Docker Ansible Deploy](https://github.com/spantaleev/matrix-docker-ansible-deploy) project. + +> **Note:** Synology DSM is a community-supported platform. It is not officially tested or maintained by the project maintainers. Use at your own discretion. + +**Intended audience:** Users already familiar with DSM, SSH, and this Ansible project. + +## Assumptions + +- DSM version 7 or higher +- `Volume1` is used as the default Docker storage location +- You are using DSM's built-in reverse proxy for handling HTTPS + +## How Synology Support Works + +The playbook automatically detects Synology DSM by checking for `/etc/synoinfo.conf`. When detected, it: + +- Uses `synouser` and `synogroup` (DSM-native tools) instead of standard Linux user management +- Pins the Python `requests` package to a version compatible with the Docker SDK +- Deploys a `matrix-synology-boot-fix` service that runs on every boot after Docker is ready + +### Boot-fix Service + +Synology DSM has two boot-time quirks that the boot-fix service addresses automatically: + +**1. `/volume1` shared mount propagation** + +Docker requires `/volume1` to be mounted as shared (`mount --make-shared /volume1`) for container bind mounts with `bind-propagation=slave` to work correctly (used by matrix-synapse for its media store). On Synology, this cannot be inserted into the systemd chain before Container Manager starts — doing so causes Container Manager to detect a broken dependency and prompt for repair on every boot. The boot-fix service runs this command after Docker is already up, safely outside Container Manager's dependency chain. + +**2. Skipped services at boot** + +Synology's systemd drops services with multi-level dependency chains from the boot activation queue (e.g. `matrix-traefik → matrix-container-socket-proxy → docker`). These services show as `inactive (dead)` after reboot even though they are enabled. The boot-fix service scans for any enabled `matrix-*.service` that is still inactive after boot and starts them automatically. + +> **If you previously configured a Task Scheduler entry** (`Control Panel > Task Scheduler`) to run `mount --make-shared /volume1` at boot-up, you can remove it — the boot-fix service now handles this. + +## Synology GUI Preparation + +1. **Enable SSH** + - `Control Panel` > `Terminal & SNMP` > `Enable SSH service` + +2. **Enable SFTP** + - `Control Panel` > `File Service` > `FTP` > `Enable SFTP service` with default port + +3. **Enable User Home Directory** + - `Control Panel` > `User & Group` > `Advanced` > `Enable user home service` + +4. **Install Container Manager** + - Install from `Package Center` + +5. **Configure Reverse Proxy** + - `Control Panel` > `Login Portal` > `Advanced` > `Reverse Proxy` + - Create entries for each service you enable (e.g. Matrix, Element, admin page) + - Example entry: + - Source: `HTTPS` / `matrix.example.com` / port `443` + - Destination: `HTTP` / `localhost` / port `81` + +## SSH Preparation + +### (Optional but Recommended) Enable SSH Key Authentication + +Configure key-based SSH login to avoid password prompts during Ansible runs. + +### Set Up the Ansible Environment + +Create a project folder and Python virtual environment on the DSM host: + +```shell +mkdir ~/path/to/your/project/folder +cd ~/path/to/your/project/folder + +python3 -m venv ./myenv +source ./myenv/bin/activate +``` + +## Inventory Configuration + +In your `inventory/hosts` file, set the Python interpreter to your virtual environment: + +```ini +# SSH key authentication example +matrix.example.com ansible_host= ansible_ssh_user= become=true become_user=root ansible_python_interpreter=/absolute/path/to/myenv/bin/python ansible_sudo_pass='your-password' +``` + +## vars.yml Configuration + +Add the following Synology-specific variables to your `vars.yml`: + +```yaml +# Synology-specific settings + +# User and group that will be created automatically by the playbook +matrix_user_name: "matrix" +matrix_group_name: "matrix" + +# Data path on your Synology volume +matrix_base_data_path: "/volume1/docker/matrix" + +# Use Synology Container Manager's Docker daemon instead of installing Docker +matrix_playbook_docker_installation_enabled: false +devture_systemd_docker_base_host_command_docker: "/usr/local/bin/docker" +devture_systemd_docker_base_docker_service_name: "pkg-ContainerManager-dockerd.service" + +# Use Synology's NTP service +devture_timesync_ntpd_service: "chronyd" + +# Reverse proxy settings — use HTTPS at the DSM reverse proxy level +matrix_playbook_ssl_enabled: true +traefik_config_entrypoint_web_secure_enabled: false + +# Bind to localhost only — DSM reverse proxy handles public traffic +traefik_container_web_host_bind_port: '127.0.0.1:81' +matrix_playbook_public_matrix_federation_api_traefik_entrypoint_host_bind_port: '127.0.0.1:8449' + +# Trust X-Forwarded-* headers from the local reverse proxy +traefik_config_entrypoint_web_forwardedHeaders_insecure: true + +matrix_playbook_public_matrix_federation_api_traefik_entrypoint_config_custom: + forwardedHeaders: + insecure: true +``` + +## Running the Playbook + +Before running the playbook for the first time, run this once manually to ensure `/volume1` has shared mount propagation for the initial setup: + +```shell +sudo mount --make-shared /volume1 +``` + +After the playbook runs, this is handled automatically on every subsequent boot by the `matrix-synology-boot-fix` service. + +```shell +# Full setup and start +ansible-playbook -i inventory/hosts setup.yml --tags=setup-all,start + +# Stop all services +ansible-playbook -i inventory/hosts setup.yml --tags=stop + +# Apply config changes (always include start to restart running containers) +ansible-playbook -i inventory/hosts setup.yml --tags=stop,setup-all,start +``` + +> **Important:** Always include `stop` before `setup-all,start` when changing configuration. Running `setup-all` alone does not restart already-running containers. + +## Creating Matrix Users + +After the services are running, create your first Matrix user: + +```shell +# option 1: +sudo docker exec -it matrix-synapse register_new_matrix_user http://localhost:8008 -c /data/homeserver.yaml -u your_username -p your_password + +# option 2: +ansible-playbook -i inventory/hosts setup.yml --extra-vars='username=your_username password=your_password admin=yes|no' --tags=register-user +``` diff --git a/roles/custom/matrix-base/tasks/main.yml b/roles/custom/matrix-base/tasks/main.yml index d6d4d8f26..86f8d9299 100644 --- a/roles/custom/matrix-base/tasks/main.yml +++ b/roles/custom/matrix-base/tasks/main.yml @@ -4,6 +4,7 @@ # SPDX-FileCopyrightText: 2020 Marcel Partap # SPDX-FileCopyrightText: 2022 Marko Weltzer # SPDX-FileCopyrightText: 2022 Warren Bailey +# SPDX-FileCopyrightText: 2026 Chiu Ki Sit # # SPDX-License-Identifier: AGPL-3.0-or-later @@ -24,6 +25,13 @@ block: - ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_matrix_user.yml" +- tags: + - setup-all + - install-all + block: + - ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_synology_prerequisites.yml" + when: matrix_synoinfo_conf_stat.stat.exists | bool + - tags: - setup-all - install-all diff --git a/roles/custom/matrix-base/tasks/setup_matrix_base.yml b/roles/custom/matrix-base/tasks/setup_matrix_base.yml index 94750383a..017f98e5c 100644 --- a/roles/custom/matrix-base/tasks/setup_matrix_base.yml +++ b/roles/custom/matrix-base/tasks/setup_matrix_base.yml @@ -7,6 +7,7 @@ # SPDX-FileCopyrightText: 2022 Sebastian Gumprich # SPDX-FileCopyrightText: 2024 - 2025 Suguru Hirahara # SPDX-FileCopyrightText: 2024 László Várady +# SPDX-FileCopyrightText: 2026 Chiu Ki Sit # # SPDX-License-Identifier: AGPL-3.0-or-later @@ -28,3 +29,12 @@ src: "{{ role_path }}/templates/bin/remove-all.j2" dest: "{{ matrix_bin_path }}/remove-all" mode: '0750' + +- name: Ensure Matrix base path ownership is correct using numeric UID/GID (Synology) + ansible.builtin.file: + path: "{{ matrix_base_data_path }}" + owner: "{{ matrix_user_uid }}" + group: "{{ matrix_user_gid }}" + recurse: true + state: directory + when: matrix_synoinfo_conf_stat.stat.exists | bool diff --git a/roles/custom/matrix-base/tasks/setup_matrix_user.yml b/roles/custom/matrix-base/tasks/setup_matrix_user.yml index b2512a437..219e3c1f3 100644 --- a/roles/custom/matrix-base/tasks/setup_matrix_user.yml +++ b/roles/custom/matrix-base/tasks/setup_matrix_user.yml @@ -1,31 +1,18 @@ # SPDX-FileCopyrightText: 2020 - 2022 Slavi Pantaleev # SPDX-FileCopyrightText: 2022 Marko Weltzer +# SPDX-FileCopyrightText: 2026 Chiu Ki Sit # # SPDX-License-Identifier: AGPL-3.0-or-later --- -- name: Ensure Matrix group is created - ansible.builtin.group: - name: "{{ matrix_group_name }}" - gid: "{{ omit if matrix_user_gid is none else matrix_user_gid }}" - state: present - system: "{{ matrix_group_system }}" - register: matrix_group +- name: Check for Synology DSM + ansible.builtin.stat: + path: /etc/synoinfo.conf + register: matrix_synoinfo_conf_stat -- name: Ensure Matrix user is created - ansible.builtin.user: - name: "{{ matrix_user_name }}" - uid: "{{ omit if matrix_user_uid is none else matrix_user_uid }}" - state: present - group: "{{ matrix_group_name }}" - home: "{{ matrix_base_data_path }}" - create_home: false - system: "{{ matrix_user_system }}" - shell: "{{ matrix_user_shell }}" - register: matrix_user +- ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_matrix_user_synology.yml" + when: matrix_synoinfo_conf_stat.stat.exists | bool -- name: Initialize matrix_user_uid and matrix_user_gid - ansible.builtin.set_fact: - matrix_user_uid: "{{ matrix_user.uid }}" - matrix_user_gid: "{{ matrix_group.gid }}" +- ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_matrix_user_linux.yml" + when: not (matrix_synoinfo_conf_stat.stat.exists | bool) diff --git a/roles/custom/matrix-base/tasks/setup_matrix_user_linux.yml b/roles/custom/matrix-base/tasks/setup_matrix_user_linux.yml new file mode 100644 index 000000000..b2512a437 --- /dev/null +++ b/roles/custom/matrix-base/tasks/setup_matrix_user_linux.yml @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: 2020 - 2022 Slavi Pantaleev +# SPDX-FileCopyrightText: 2022 Marko Weltzer +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +--- + +- name: Ensure Matrix group is created + ansible.builtin.group: + name: "{{ matrix_group_name }}" + gid: "{{ omit if matrix_user_gid is none else matrix_user_gid }}" + state: present + system: "{{ matrix_group_system }}" + register: matrix_group + +- name: Ensure Matrix user is created + ansible.builtin.user: + name: "{{ matrix_user_name }}" + uid: "{{ omit if matrix_user_uid is none else matrix_user_uid }}" + state: present + group: "{{ matrix_group_name }}" + home: "{{ matrix_base_data_path }}" + create_home: false + system: "{{ matrix_user_system }}" + shell: "{{ matrix_user_shell }}" + register: matrix_user + +- name: Initialize matrix_user_uid and matrix_user_gid + ansible.builtin.set_fact: + matrix_user_uid: "{{ matrix_user.uid }}" + matrix_user_gid: "{{ matrix_group.gid }}" diff --git a/roles/custom/matrix-base/tasks/setup_matrix_user_synology.yml b/roles/custom/matrix-base/tasks/setup_matrix_user_synology.yml new file mode 100644 index 000000000..079d67be6 --- /dev/null +++ b/roles/custom/matrix-base/tasks/setup_matrix_user_synology.yml @@ -0,0 +1,46 @@ +# SPDX-FileCopyrightText: 2026 Chiu Ki Sit +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +--- + +- name: Check if Matrix user exists (Synology) + ansible.builtin.command: id {{ matrix_user_name }} + register: matrix_user_check + changed_when: false + failed_when: false + +- name: Ensure Matrix user is created (Synology) + ansible.builtin.command: /usr/syno/sbin/synouser --add {{ matrix_user_name }} "" "{{ matrix_user_name }}" 0 "" "" + when: matrix_user_check.rc != 0 + changed_when: true + +- name: Check if Matrix group exists (Synology) + ansible.builtin.command: /usr/syno/sbin/synogroup --get {{ matrix_group_name }} + register: matrix_group_check + changed_when: false + failed_when: false + +- name: Ensure Matrix group is created (Synology) + ansible.builtin.command: /usr/syno/sbin/synogroup --add {{ matrix_group_name }} {{ matrix_user_name }} + when: matrix_group_check.rc != 0 + changed_when: true + +- name: Get Matrix user UID (Synology) + ansible.builtin.command: id -u {{ matrix_user_name }} + register: matrix_user_uid_result + changed_when: false + +- name: Get Matrix group GID (Synology) + ansible.builtin.command: + argv: + - python3 + - -c + - "import grp; print(grp.getgrnam('{{ matrix_group_name }}').gr_gid)" + register: matrix_user_gid_result + changed_when: false + +- name: Initialize matrix_user_uid and matrix_user_gid + ansible.builtin.set_fact: + matrix_user_uid: "{{ matrix_user_uid_result.stdout }}" + matrix_user_gid: "{{ matrix_user_gid_result.stdout }}" diff --git a/roles/custom/matrix-base/tasks/setup_synology_boot_fix.yml b/roles/custom/matrix-base/tasks/setup_synology_boot_fix.yml new file mode 100644 index 000000000..66498c677 --- /dev/null +++ b/roles/custom/matrix-base/tasks/setup_synology_boot_fix.yml @@ -0,0 +1,27 @@ +# SPDX-FileCopyrightText: 2026 Chiu Ki Sit +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +--- + +- name: Deploy Matrix boot recovery script (Synology) + ansible.builtin.template: + src: "{{ role_path }}/templates/bin/matrix-synology-boot-fix.j2" + dest: "{{ matrix_bin_path }}/matrix-synology-boot-fix" + mode: "0750" + owner: root + group: root + +- name: Deploy Matrix boot recovery service (Synology) + ansible.builtin.template: + src: "{{ role_path }}/templates/systemd/matrix-synology-boot-fix.service.j2" + dest: /etc/systemd/system/matrix-synology-boot-fix.service + mode: "0644" + register: matrix_synology_boot_fix_service + +- name: Reload systemd and enable Matrix boot recovery service (Synology) + ansible.builtin.systemd: + name: matrix-synology-boot-fix.service + daemon_reload: true + enabled: true + when: matrix_synology_boot_fix_service.changed diff --git a/roles/custom/matrix-base/tasks/setup_synology_prerequisites.yml b/roles/custom/matrix-base/tasks/setup_synology_prerequisites.yml new file mode 100644 index 000000000..1589d639b --- /dev/null +++ b/roles/custom/matrix-base/tasks/setup_synology_prerequisites.yml @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2026 Chiu Ki Sit +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +--- + +- name: Ensure requests Python package is pinned for Docker SDK compatibility (Synology) + ansible.builtin.pip: + name: requests==2.31.0 + state: present + +- ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_synology_boot_fix.yml" diff --git a/roles/custom/matrix-base/templates/bin/matrix-synology-boot-fix.j2 b/roles/custom/matrix-base/templates/bin/matrix-synology-boot-fix.j2 new file mode 100755 index 000000000..e852e8622 --- /dev/null +++ b/roles/custom/matrix-base/templates/bin/matrix-synology-boot-fix.j2 @@ -0,0 +1,50 @@ +#!/bin/sh +# SPDX-FileCopyrightText: 2026 Chiu Ki Sit +# +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# Boot recovery for Matrix services on Synology DSM. +# +# This script runs after multi-user.target (outside Container Manager's dependency +# chain) and does two things: +# +# 1. Makes /volume1 mount-shared so Docker bind-propagation=slave mounts work. +# Inserting this into the systemd chain Before=pkg-ContainerManager-dockerd.service +# causes Container Manager to detect a broken dependency and prompt for repair, +# so it must run here instead, after Docker is already up. +# +# 2. Starts any enabled matrix-*.service that systemd skipped at boot. +# Synology's systemd drops services with multi-level dependency chains +# (e.g. traefik -> socket-proxy -> docker) from the boot activation queue. +# Services that need bind-propagation=slave (e.g. matrix-synapse) are +# created after step 1, so the propagation is already in effect. + +# Wait up to 120s for Docker to be ready +i=0 +while [ "$i" -lt 60 ]; do + /usr/local/bin/docker info >/dev/null 2>&1 && break + i=$((i + 1)) + sleep 2 +done + +if ! /usr/local/bin/docker info >/dev/null 2>&1; then + echo "matrix-synology-boot-fix: Docker not ready after 120s, aborting" >&2 + exit 1 +fi + +# Make /volume1 shared so Docker bind-propagation=slave mounts work correctly. +# Must run after Docker is up to avoid interfering with Container Manager's +# integrity checks, but before matrix-synapse (and any other service using +# bind-propagation=slave) creates its containers. +/bin/mount --make-shared /volume1 +echo "matrix-synology-boot-fix: /volume1 set to shared mount propagation" + +# Start any enabled matrix-*.service that is inactive (skipped at boot, not intentionally stopped) +/bin/systemctl list-unit-files 'matrix-*.service' --state=enabled --no-legend 2>/dev/null | \ + while read -r unit _state; do + [ "$unit" = "matrix-synology-boot-fix.service" ] && continue + if [ "$(/bin/systemctl is-active "$unit" 2>/dev/null)" = "inactive" ]; then + echo "matrix-synology-boot-fix: starting $unit" + /bin/systemctl start "$unit" + fi + done diff --git a/roles/custom/matrix-base/templates/systemd/matrix-synology-boot-fix.service.j2 b/roles/custom/matrix-base/templates/systemd/matrix-synology-boot-fix.service.j2 new file mode 100644 index 000000000..f6db14fcd --- /dev/null +++ b/roles/custom/matrix-base/templates/systemd/matrix-synology-boot-fix.service.j2 @@ -0,0 +1,16 @@ +# SPDX-FileCopyrightText: 2026 Chiu Ki Sit +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +[Unit] +Description=Matrix Services Boot Recovery (Synology) +# Run after multi-user.target so all matrix services have been attempted first. +After=multi-user.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart={{ matrix_bin_path }}/matrix-synology-boot-fix + +[Install] +WantedBy=multi-user.target