systemd-machine-tags.service: apply machine-tags.d/ configuration at boot

The service runs "hostnamectl tags --apply" before sysinit.target, so that
the tags declared in the machine-tags.d/ directories are in place before
most units are started and may be matched by ConditionMachineTag=. It is
skipped if no configuration exists.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek committed 2026-10-30 16:56:37 +01:00
1 parent 4350c46b02
commit 0a43fdc495
7 files changed
+108 -1

No files matched your search

+4
View File
@@ -46,6 +46,9 @@
stored in <filename>/etc/machine-info</filename>, thus applying the same configuration repeatedly has no
further effect.</para>
<para>During boot, the configuration is applied automatically by
<citerefentry><refentrytitle>systemd-machine-tags.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.</para>
<para>This allows OS vendors, image builders and system extensions (see
<citerefentry><refentrytitle>systemd-sysext</refentrytitle><manvolnum>8</manvolnum></citerefentry>) to
declare tags in a drop-in directory hierarchy, instead of setting them imperatively.</para>
@@ -132,6 +135,7 @@ local.rack=7</programlisting>
<member><citerefentry><refentrytitle>hostnamectl</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>machine-info</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>systemd-hostnamed.service</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>systemd-machine-tags.service</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
</simplelist></para>
</refsect1>
+1
View File
@@ -1158,6 +1158,7 @@ manpages = [
['systemd-machine-id-commit.service', '8', [], ''],
['systemd-machine-id-setup', '1', [], ''],
['systemd-machine-tag@.service', '8', [], 'ENABLE_HOSTNAMED'],
['systemd-machine-tags.service', '8', [], 'ENABLE_HOSTNAMED'],
['systemd-machined.service', '8', ['systemd-machined'], 'ENABLE_MACHINED'],
['systemd-makefs@.service',
'8',
+60
View File
@@ -0,0 +1,60 @@
<?xml version="1.0"?>
<!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
<refentry id="systemd-machine-tags.service" conditional='ENABLE_HOSTNAMED'
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>systemd-machine-tags.service</title>
<productname>systemd</productname>
</refentryinfo>
<refmeta>
<refentrytitle>systemd-machine-tags.service</refentrytitle>
<manvolnum>8</manvolnum>
</refmeta>
<refnamediv>
<refname>systemd-machine-tags.service</refname>
<refpurpose>Apply the machine tags configuration at boot</refpurpose>
</refnamediv>
<refsynopsisdiv>
<para><filename>systemd-machine-tags.service</filename></para>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para><filename>systemd-machine-tags.service</filename> is an early boot service that applies the machine
tag configuration declared in the <filename>machine-tags.d/</filename> directories, see
<citerefentry><refentrytitle>machine-tags.d</refentrytitle><manvolnum>5</manvolnum></citerefentry>, by
invoking <command>hostnamectl tags --apply</command>, see
<citerefentry><refentrytitle>hostnamectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>. The
additions and removals declared there are applied on top of the tags already configured in the
<varname>TAGS=</varname> field of
<citerefentry><refentrytitle>machine-info</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
<para>The service is ordered before <filename>sysinit.target</filename>, so that the configured tags are
in place before most other units are started and may be used in their
<varname>ConditionMachineTag=</varname> settings, see
<citerefentry><refentrytitle>systemd.unit</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
<xi:include href="version-info.xml" xpointer="v263"/>
</refsect1>
<refsect1>
<title>See Also</title>
<para><simplelist type="inline">
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>machine-tags.d</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>hostnamectl</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>systemd-hostnamed.service</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>systemd-machine-tag@.service</refentrytitle><manvolnum>8</manvolnum></citerefentry></member>
<member><citerefentry><refentrytitle>machine-info</refentrytitle><manvolnum>5</manvolnum></citerefentry></member>
</simplelist></para>
</refsect1>
</refentry>
+5
View File
@@ -609,6 +609,11 @@ EOF
hostnamectl tags --apply
assert_eq "$(hostnamectl tags)" "local.rack=7:manual:role=b"
# The boot-time service applies the configuration as well.
echo "from-service" >/etc/machine-tags.d/86-service-test71.tags
systemctl restart systemd-machine-tags.service
assert_eq "$(hostnamectl tags)" "from-service:local.rack=7:manual:role=b"
# "-*" resets the list.
echo "-* fresh" >/etc/machine-tags.d/90-reset-test71.tags
varlinkctl call /run/systemd/io.systemd.Hostname io.systemd.Hostname.ApplyTags '{}'
+5
View File
@@ -579,6 +579,11 @@ units = [
'file' : 'systemd-machine-tag@.service',
'conditions' : ['ENABLE_HOSTNAMED'],
},
{
'file' : 'systemd-machine-tags.service',
'conditions' : ['ENABLE_HOSTNAMED'],
'symlinks' : ['sysinit.target.wants/'],
},
{
'file' : 'systemd-machined.service.in',
'conditions' : ['ENABLE_MACHINED'],
+1 -1
View File
@@ -12,7 +12,7 @@ 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
After=local-fs.target systemd-hostnamed.socket systemd-machine-tags.service
Requisite=local-fs.target systemd-hostnamed.socket
Before=shutdown.target
Conflicts=shutdown.target
+32
View File
@@ -0,0 +1,32 @@
# 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=Apply Machine Tags Configuration
Documentation=man:systemd-machine-tags.service(8) man:machine-tags.d(5)
DefaultDependencies=no
ConditionPathExists=!/etc/initrd-release
ConditionPathIsReadWrite=/etc
ConditionDirectoryNotEmpty=|/etc/machine-tags.d
ConditionDirectoryNotEmpty=|/run/machine-tags.d
ConditionDirectoryNotEmpty=|/usr/local/lib/machine-tags.d
ConditionDirectoryNotEmpty=|/usr/lib/machine-tags.d
Requires=systemd-hostnamed.socket
After=systemd-hostnamed.socket
After=local-fs.target
After=systemd-firstboot.service
After=systemd-sysext.service systemd-confext.service
Before=sysinit.target shutdown.target
Conflicts=shutdown.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=hostnamectl tags --apply