AdGuardHome icon indicating copy to clipboard operation
AdGuardHome copied to clipboard

Fail to install "cannot install AdGuardHome as a service"

Open guldhammer72 opened this issue 3 weeks ago • 2 comments

Prerequisites

Platform (OS and CPU architecture)

Linux, ARMv7

Installation

GitHub releases or script from README

Setup

On one machine

AdGuard Home version

Edge

Action

Trying to install Adgyrardhome on a fresh Raspbian on a Raspberry Pi 4B

curl -s -S -L https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/master/scripts/install.sh | sh -s -- -c edge starting AdGuard Home installation script [sudo] adgangskode for guld: starting AdGuard Home installation script 2026/11/12 23:00:19.272274 [info] starting adguard home version="AdGuard Home, version v1.0.0-a.116+df45d7c0" 2026/11/12 23:00:19.272935 [info] service: AdGuard Home, version v1.0.0-a.116+df45d7c0 2026/11/12 23:00:19.272976 [info] service: control action=install 2026/11/12 23:00:19.273751 [info] service_manager: installing service name=AdGuardHome 2026/11/12 23:00:19.274345 [error] service: action failed err="action "install": installing service: installing service: Init already exists: /etc/systemd/system/AdGuardHome.service" cannot install AdGuardHome as a service

Expected result

A fresh install og adguardhome edge

Actual result

cannot install AdGuardHome as a service

Additional information and/or screenshots

No response

guldhammer72 avatar Nov 12 '26 22:11 guldhammer72

I can reproduce this. I’ll forward it to the dev team.

GulnazSalakhova avatar Nov 13 '26 11:11 GulnazSalakhova

I traced this one down to its root cause and opened a fix: #8721.

Problem

There are three layers. The first is the actual bug, and the other two turn it into the confusing "Init already exists" message.

  1. The systemd unit template no longer renders. go.mod is on kardianos/service v1.3.0, whose built-in template engine no longer supports Go text/template syntax. internal/ossvc/config_linux.go still uses that syntax ({{.Description}}, {{if and …}}, {{if gt …}}). Rendering it fails with service: unknown template key ".Description". The TODO in go.mod and the v1.3 upgrade notes both say the custom templates still needed rewriting, and that wasn't done yet. So on current edge (v1.0.0-a.116), every systemd install fails on its first attempt.
  2. The library leaves a partial unit behind. In kardianos/service service_systemd_linux.go, Install creates /etc/systemd/system/AdGuardHome.service with os.OpenFile (line 157) before it parses and renders the template (line 203). When rendering fails, it returns with the file already on disk and doesn't roll back. Any later Install hits the existence check at lines 152-154 and returns Init already exists: /etc/systemd/system/AdGuardHome.service.
  3. install.sh retries without cleaning up. In scripts/install.sh, install_service() treats a failed -s install as fatal and runs rm -r "$agh_dir" without uninstalling the service. On armv7 it then sets reinstall=1 and reruns itself with armv5 (the #2542 fallback). In that rerun, handle_existing() returns early because the directory is gone, so -s uninstall never runs, and the retry hits the leftover unit. Wrapped by handleServiceInstallCmd → manager.install, that's exactly the message in this report: action "install": installing service: installing service: Init already exists: ….

Confirmed vs. inferred: layers 1-3 are confirmed by reading the code and by unit tests. I haven't re-run it on a real Pi 4B. The log pasted above shows only the final error. It matches either the armv7 → armv5 retry or a second manual attempt after the first one left the unit behind. Both lead to the same leftover file. On other CPUs the script stops after the first failure, so users there would see the template error instead, and the stale unit would still be left behind.

Reproduce

On a systemd Linux host with current edge:

  1. curl -s -S -L https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/master/scripts/install.sh | sh -s -- -c edge -v
  2. The first AdGuardHome -s install fails while rendering the unit (on armv7 the script then retries with armv5 and fails with Init already exists).
  3. ls -l /etc/systemd/system/AdGuardHome.service shows a leftover (empty or partial) unit.
  4. Every later install attempt fails with Init already exists until that file is removed by hand.

Workaround for affected users until a fix ships: sudo rm /etc/systemd/system/AdGuardHome.service && sudo systemctl daemon-reload. A plain re-run will still fail on step 1 until the template is fixed.

Fix

PR: #8721 (branch cursor/fix-install-service-exists-8714, commit 521e979)

  • internal/ossvc/config_linux.go:43-67: the systemd unit template is rewritten for the kardianos/service v1.3 engine. The output is the same unit as before (RestartSec=10, stdout/stderr to the journal).
  • internal/ossvc/defaultmanager.go:153-210: if Install fails, AdGuard Home uninstalls to remove any partial unit. If the error is Init already exists, it uninstalls and installs once more, so a stale unit from an earlier failed attempt is replaced.
  • internal/home/service.go:337-373: if the unit installs but the service fails to start, the unit is uninstalled again, so nothing half-installed is left behind.
  • scripts/install.sh:547-553: before rm -r "$agh_dir", the script now runs a best-effort ./AdGuardHome -s uninstall (through the existing maybe_sudo), so the armv7 → armv5 fallback starts clean.
  • CHANGELOG.md: Unreleased → Fixed entry for #8714.

Tests added:

  • TestSystemdScript_syntax: the new unit template renders with the v1.3 engine and produces a valid unit.
  • TestManager_Install, TestManager_Install_alreadyExists: the first call returns Init already exists, uninstall runs, and the second install succeeds.
  • TestHandleServiceInstallCmd_rollback: an install error doesn't start the service, and a start error uninstalls the unit.

go test -race -count=1 passes for ./internal/ossvc/ and ./internal/home/. go vet, staticcheck, gosec, gocyclo/gocognit, shadow, nilness, ineffassign, unparam, fieldalignment and misspell pass on the touched packages. shellcheck and shfmt pass for scripts/install.sh.

Caveats

  • Other init systems: the SysV, OpenWrt, FreeBSD, macOS (launchd) and OpenBSD custom templates still use the old text/template syntax, so installs there probably fail the same way on edge. This PR only covers systemd, which is what this report is about. I can extend it to the other templates if you'd like that in the same PR.
  • On Init already exists, the existing AdGuardHome unit is replaced, including one that was edited by hand. If you'd rather keep an existing unit and only remove empty or partial ones, I can narrow that.
  • If systemctl disable itself fails before the library deletes the file, the retry can still see the old unit.

Miixms99 avatar Nov 15 '26 16:11 Miixms99