From 432f54118d2f3e48ab34b8008259193a03369e91 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 29 May 2026 18:27:09 +0200 Subject: [PATCH] units: add systemd-machine-tag@.service template Add a service template that adds the machine tag given as its instance name to the host's machine tags. Whenever an instance is pulled in, the tag is added; the operation is idempotent. It talks to hostnamed over Varlink (which is socket-activated), so it works very early at boot, before the system bus is up. It is ordered after local-fs.target so that /etc/machine-info can be written, requires hostnamed's socket and the local filesystems to be available, and does not run in the initrd. --- man/rules/meson.build | 1 + man/systemd-machine-tag@.service.xml | 65 ++++++++++++++++++++++++++++ units/meson.build | 4 ++ units/systemd-machine-tag@.service | 23 ++++++++++ 4 files changed, 93 insertions(+) create mode 100644 man/systemd-machine-tag@.service.xml create mode 100644 units/systemd-machine-tag@.service diff --git a/man/rules/meson.build b/man/rules/meson.build index d3df447352..f3db05fc78 100644 --- a/man/rules/meson.build +++ b/man/rules/meson.build @@ -1107,6 +1107,7 @@ manpages = [ ['systemd-loop@.service', '8', [], ''], ['systemd-machine-id-commit.service', '8', [], ''], ['systemd-machine-id-setup', '1', [], ''], + ['systemd-machine-tag@.service', '8', [], 'ENABLE_HOSTNAMED'], ['systemd-machined.service', '8', ['systemd-machined'], 'ENABLE_MACHINED'], ['systemd-makefs@.service', '8', diff --git a/man/systemd-machine-tag@.service.xml b/man/systemd-machine-tag@.service.xml new file mode 100644 index 0000000000..b5bc1d81c7 --- /dev/null +++ b/man/systemd-machine-tag@.service.xml @@ -0,0 +1,65 @@ + + + + + + + + + systemd-machine-tag@.service + systemd + + + + systemd-machine-tag@.service + 8 + + + + systemd-machine-tag@.service + Add a machine tag to the host + + + + systemd-machine-tag@.service + + + + Description + + systemd-machine-tag@TAG.service is a service + template that adds the machine tag TAG (taken from the instance name) to the + host's list of machine tags. Whenever an instance of this template is pulled in, the corresponding tag is + added; the operation is idempotent, so pulling it in repeatedly has no further effect. + + The primary use of this template is automatic, hardware-driven tagging: the + SYSTEMD_MACHINE_TAG udev property, when set on a device, causes the matching instance + of this template to be pulled in (via SYSTEMD_WANTS=), so that plugging in or + detecting a specific device automatically tags the host. See + systemd.device5 for + details on the SYSTEMD_WANTS= mechanism. + + Machine tags may be inspected and edited with the tags verb of + hostnamectl1, and are + stored in the TAGS= field of + machine-info5. Each + tag must be 1…255 characters long and consist only of ASCII alphanumeric characters, + - and .. + + + + + + See Also + + systemd1 + hostnamectl1 + systemd-hostnamed.service8 + machine-info5 + udev7 + + + + diff --git a/units/meson.build b/units/meson.build index de7a796a1e..db5edb01e6 100644 --- a/units/meson.build +++ b/units/meson.build @@ -503,6 +503,10 @@ units = [ 'file' : 'systemd-machine-id-commit.service', 'symlinks' : ['sysinit.target.wants/'], }, + { + 'file' : 'systemd-machine-tag@.service', + 'conditions' : ['ENABLE_HOSTNAMED'], + }, { 'file' : 'systemd-machined.service.in', 'conditions' : ['ENABLE_MACHINED'], diff --git a/units/systemd-machine-tag@.service b/units/systemd-machine-tag@.service new file mode 100644 index 0000000000..7fc578d7e8 --- /dev/null +++ b/units/systemd-machine-tag@.service @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# This file is part of systemd. +# +# systemd is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 2.1 of the License, or +# (at your option) any later version. + +[Unit] +Description=Add Machine Tag %i +Documentation=man:systemd-machine-tag@.service(8) +DefaultDependencies=no +ConditionPathExists=!/etc/initrd-release +After=local-fs.target systemd-hostnamed.socket +Requisite=local-fs.target systemd-hostnamed.socket +Before=shutdown.target +Conflicts=shutdown.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=varlinkctl call /run/systemd/io.systemd.Hostname io.systemd.Hostname.SetTags '{"add":["%i"]}'