2
0
mirror of https://github.com/xcat2/confluent.git synced 2026-11-21 18:02:27 +00:00

Give every deployed node its own machine-id

Captured images carried the machine-id of the source system, and images
built with imgutil carry the one generated in the build root, so every
node deployed from a profile shared it. That breaks anything keyed on it,
such as DHCP DUIDs and journal identities.

Capture now stores an empty /etc/machine-id. Diskless boot on EL and SUSE
writes a stable per-node one derived from the node name and the DMI
product UUID, as Ubuntu already does, using openssl since the initramfs
has no sha512sum. Install to disk copies that machine-id into the target
and renames the boot loader entries from the image to it. Otherwise
kernel-install in post.sh adds a second set of entries under another ID.
This commit is contained in:
Markus Hilger
2026-11-03 16:25:51 +01:00
parent 830b6892d6
commit fc63269dc7
11 changed files with 170 additions and 0 deletions
@@ -151,6 +151,14 @@ set_selinux_context system_u:object_r:bin_t:s0 /sysroot/opt/confluent/bin/onboot
set_selinux_context system_u:object_r:bin_t:s0 /sysroot/opt/confluent/bin/apiclient
ln -s /etc/systemd/system/onboot.service /sysroot/etc/systemd/system/multi-user.target.wants/onboot.service
cp /etc/confluent/functions /sysroot/etc/confluent/functions
# give every node its own stable machine-id instead of the one baked into the
# image, which every node booting it would otherwise share (the initramfs has
# openssl but no sha512sum)
machineid=$( (echo $nodename; cat /sys/devices/virtual/dmi/id/product_uuid 2> /dev/null) | openssl dgst -sha512 -r | head -c 32)
if [ ${#machineid} -eq 32 ]; then
echo $machineid > /sysroot/etc/machine-id
chmod 444 /sysroot/etc/machine-id
fi
if [ -x /sysroot/usr/sbin/matchpathcon ] && grep -Eq '^SELINUX=(enforcing|permissive)' /sysroot/etc/selinux/config 2> /dev/null; then
# Files written to /sysroot before policy load are unlabeled.
selinuxpolicy=$(sed -n 's/^SELINUXTYPE=//p' /sysroot/etc/selinux/config)
@@ -71,6 +71,34 @@ class PartedRunner():
else:
return subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf8')
def set_machine_id(rootdir):
# the image carries the machine-id of the system it was captured from, or
# none at all, use the one this node got for its diskless boot instead
if not os.path.exists('/etc/machine-id'):
return
with open('/etc/machine-id') as midin:
machineid = midin.read().strip()
if not re.match('^[0-9a-f]{32}$', machineid):
return
midfile = os.path.join(rootdir, 'etc/machine-id')
if os.path.islink(midfile):
os.unlink(midfile)
with open(midfile, 'w') as midout:
midout.write(machineid + '\n')
os.chmod(midfile, 0o444)
# boot loader entries are named after the machine-id, rename them so
# kernel-install replaces them rather than adding duplicates
entdir = os.path.join(rootdir, 'boot/loader/entries')
for loadent in glob.glob(os.path.join(entdir, '*.conf')):
oldid, _, entname = os.path.basename(loadent).partition('-')
if oldid == machineid or not re.match('^[0-9a-f]{32}$', oldid):
continue
newent = os.path.join(entdir, '{}-{}'.format(machineid, entname))
if os.path.exists(newent):
os.unlink(loadent)
else:
os.rename(loadent, newent)
swapdev = '/dev/mapper/localstorage-swap'
def fixup(rootdir, vols):
@@ -103,6 +131,7 @@ def fixup(rootdir, vols):
tfile.write(tab + '\n')
with open(os.path.join(rootdir, 'etc/hostname'), 'w') as nameout:
nameout.write(socket.gethostname() + '\n')
set_machine_id(rootdir)
selinuxconfig = os.path.join(rootdir, 'etc/selinux/config')
policy = None
if os.path.exists(selinuxconfig):
@@ -125,6 +125,14 @@ set_selinux_context system_u:object_r:bin_t:s0 /sysroot/opt/confluent/bin/onboot
set_selinux_context system_u:object_r:bin_t:s0 /sysroot/opt/confluent/bin/apiclient
ln -s /etc/systemd/system/onboot.service /sysroot/etc/systemd/system/multi-user.target.wants/onboot.service
cp /etc/confluent/functions /sysroot/etc/confluent/functions
# give every node its own stable machine-id instead of the one baked into the
# image, which every node booting it would otherwise share (the initramfs has
# openssl but no sha512sum)
machineid=$( (echo $nodename; cat /sys/devices/virtual/dmi/id/product_uuid 2> /dev/null) | openssl dgst -sha512 -r | head -c 32)
if [ ${#machineid} -eq 32 ]; then
echo $machineid > /sysroot/etc/machine-id
chmod 444 /sysroot/etc/machine-id
fi
if grep installtodisk /proc/cmdline > /dev/null; then
. /etc/confluent/functions
run_remote installimage
@@ -77,6 +77,34 @@ class PartedRunner():
else:
return subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf8')
def set_machine_id(rootdir):
# the image carries the machine-id of the system it was captured from, or
# none at all, use the one this node got for its diskless boot instead
if not os.path.exists('/etc/machine-id'):
return
with open('/etc/machine-id') as midin:
machineid = midin.read().strip()
if not re.match('^[0-9a-f]{32}$', machineid):
return
midfile = os.path.join(rootdir, 'etc/machine-id')
if os.path.islink(midfile):
os.unlink(midfile)
with open(midfile, 'w') as midout:
midout.write(machineid + '\n')
os.chmod(midfile, 0o444)
# boot loader entries are named after the machine-id, rename them so
# kernel-install replaces them rather than adding duplicates
entdir = os.path.join(rootdir, 'boot/loader/entries')
for loadent in glob.glob(os.path.join(entdir, '*.conf')):
oldid, _, entname = os.path.basename(loadent).partition('-')
if oldid == machineid or not re.match('^[0-9a-f]{32}$', oldid):
continue
newent = os.path.join(entdir, '{}-{}'.format(machineid, entname))
if os.path.exists(newent):
os.unlink(loadent)
else:
os.rename(loadent, newent)
swapdev = '/dev/mapper/localstorage-swap'
def fixup(rootdir, vols):
@@ -118,6 +146,7 @@ def fixup(rootdir, vols):
tfile.write(tab + '\n')
with open(os.path.join(rootdir, 'etc/hostname'), 'w') as nameout:
nameout.write(socket.gethostname() + '\n')
set_machine_id(rootdir)
selinuxconfig = os.path.join(rootdir, 'etc/selinux/config')
policy = None
if os.path.exists(selinuxconfig):
@@ -129,6 +129,14 @@ set_selinux_context system_u:object_r:bin_t:s0 /sysroot/opt/confluent/bin/onboot
set_selinux_context system_u:object_r:bin_t:s0 /sysroot/opt/confluent/bin/apiclient
ln -s /etc/systemd/system/onboot.service /sysroot/etc/systemd/system/multi-user.target.wants/onboot.service
cp /etc/confluent/functions /sysroot/etc/confluent/functions
# give every node its own stable machine-id instead of the one baked into the
# image, which every node booting it would otherwise share (the initramfs has
# openssl but no sha512sum)
machineid=$( (echo $nodename; cat /sys/devices/virtual/dmi/id/product_uuid 2> /dev/null) | openssl dgst -sha512 -r | head -c 32)
if [ ${#machineid} -eq 32 ]; then
echo $machineid > /sysroot/etc/machine-id
chmod 444 /sysroot/etc/machine-id
fi
if [ -x /sysroot/usr/sbin/matchpathcon ] && grep -Eq '^SELINUX=(enforcing|permissive)' /sysroot/etc/selinux/config 2> /dev/null; then
# Files written to the upper layer before policy load are unlabeled.
# Label only those, using globstar since the initramfs has no find.
@@ -96,6 +96,34 @@ class PartedRunner():
else:
return subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf8')
def set_machine_id(rootdir):
# the image carries the machine-id of the system it was captured from, or
# none at all, use the one this node got for its diskless boot instead
if not os.path.exists('/etc/machine-id'):
return
with open('/etc/machine-id') as midin:
machineid = midin.read().strip()
if not re.match('^[0-9a-f]{32}$', machineid):
return
midfile = os.path.join(rootdir, 'etc/machine-id')
if os.path.islink(midfile):
os.unlink(midfile)
with open(midfile, 'w') as midout:
midout.write(machineid + '\n')
os.chmod(midfile, 0o444)
# boot loader entries are named after the machine-id, rename them so
# kernel-install replaces them rather than adding duplicates
entdir = os.path.join(rootdir, 'boot/loader/entries')
for loadent in glob.glob(os.path.join(entdir, '*.conf')):
oldid, _, entname = os.path.basename(loadent).partition('-')
if oldid == machineid or not re.match('^[0-9a-f]{32}$', oldid):
continue
newent = os.path.join(entdir, '{}-{}'.format(machineid, entname))
if os.path.exists(newent):
os.unlink(loadent)
else:
os.rename(loadent, newent)
def fixup(rootdir, vols):
devbymount = {}
for vol in vols:
@@ -138,6 +166,7 @@ def fixup(rootdir, vols):
tfile.write(tab + '\n')
with open(os.path.join(rootdir, 'etc/hostname'), 'w') as nameout:
nameout.write(socket.gethostname() + '\n')
set_machine_id(rootdir)
selinuxconfig = os.path.join(rootdir, 'etc/selinux/config')
policy = None
if os.path.exists(selinuxconfig):
@@ -153,6 +153,14 @@ set_selinux_context system_u:object_r:bin_t:s0 /sysroot/opt/confluent/bin/onboot
set_selinux_context system_u:object_r:bin_t:s0 /sysroot/opt/confluent/bin/apiclient
ln -s /etc/systemd/system/onboot.service /sysroot/etc/systemd/system/multi-user.target.wants/onboot.service
cp /etc/confluent/functions /sysroot/etc/confluent/functions
# give every node its own stable machine-id instead of the one baked into the
# image, which every node booting it would otherwise share (the initramfs has
# openssl but no sha512sum)
machineid=$( (echo $nodename; cat /sys/devices/virtual/dmi/id/product_uuid 2> /dev/null) | openssl dgst -sha512 -r | head -c 32)
if [ ${#machineid} -eq 32 ]; then
echo $machineid > /sysroot/etc/machine-id
chmod 444 /sysroot/etc/machine-id
fi
rm -f /sysroot/etc/dracut.conf.d/diskless.conf # remove diskless dracut from runtime, to make kdump happier
if [ -x /sysroot/usr/sbin/matchpathcon ] && grep -Eq '^SELINUX=(enforcing|permissive)' /sysroot/etc/selinux/config 2> /dev/null; then
# Files written to /sysroot before policy load are unlabeled.
@@ -113,6 +113,14 @@ chmod +x /sysroot/opt/confluent/bin/onboot.sh
cp /opt/confluent/bin/apiclient /sysroot/opt/confluent/bin
ln -s /etc/systemd/system/onboot.service /sysroot/etc/systemd/system/multi-user.target.wants/onboot.service
cp /etc/confluent/functions /sysroot/etc/confluent/functions
# give every node its own stable machine-id instead of the one baked into the
# image, which every node booting it would otherwise share (the initramfs has
# openssl but no sha512sum)
machineid=$( (echo $nodename; cat /sys/devices/virtual/dmi/id/product_uuid 2> /dev/null) | openssl dgst -sha512 -r | head -c 32)
if [ ${#machineid} -eq 32 ]; then
echo $machineid > /sysroot/etc/machine-id
chmod 444 /sysroot/etc/machine-id
fi
nameserversec=0
nameservers=""
@@ -113,6 +113,14 @@ chmod +x /sysroot/opt/confluent/bin/onboot.sh
cp /opt/confluent/bin/apiclient /sysroot/opt/confluent/bin
ln -s /etc/systemd/system/onboot.service /sysroot/etc/systemd/system/multi-user.target.wants/onboot.service
cp /etc/confluent/functions /sysroot/etc/confluent/functions
# give every node its own stable machine-id instead of the one baked into the
# image, which every node booting it would otherwise share (the initramfs has
# openssl but no sha512sum)
machineid=$( (echo $nodename; cat /sys/devices/virtual/dmi/id/product_uuid 2> /dev/null) | openssl dgst -sha512 -r | head -c 32)
if [ ${#machineid} -eq 32 ]; then
echo $machineid > /sysroot/etc/machine-id
chmod 444 /sysroot/etc/machine-id
fi
# Hand NetworkManager the connection the initramfs is already using. Without
# this it claims the interface on its own terms and the tethered root
@@ -74,6 +74,34 @@ class PartedRunner():
else:
return subprocess.run(command, stdout=subprocess.PIPE).stdout.decode('utf8')
def set_machine_id(rootdir):
# the image carries the machine-id of the system it was captured from, or
# none at all, use the one this node got for its diskless boot instead
if not os.path.exists('/etc/machine-id'):
return
with open('/etc/machine-id') as midin:
machineid = midin.read().strip()
if not re.match('^[0-9a-f]{32}$', machineid):
return
midfile = os.path.join(rootdir, 'etc/machine-id')
if os.path.islink(midfile):
os.unlink(midfile)
with open(midfile, 'w') as midout:
midout.write(machineid + '\n')
os.chmod(midfile, 0o444)
# boot loader entries are named after the machine-id, rename them so
# kernel-install replaces them rather than adding duplicates
entdir = os.path.join(rootdir, 'boot/loader/entries')
for loadent in glob.glob(os.path.join(entdir, '*.conf')):
oldid, _, entname = os.path.basename(loadent).partition('-')
if oldid == machineid or not re.match('^[0-9a-f]{32}$', oldid):
continue
newent = os.path.join(entdir, '{}-{}'.format(machineid, entname))
if os.path.exists(newent):
os.unlink(loadent)
else:
os.rename(loadent, newent)
swapdev = '/dev/mapper/localstorage-swap'
def fixup(rootdir, vols):
@@ -106,6 +134,7 @@ def fixup(rootdir, vols):
tfile.write(tab + '\n')
with open(os.path.join(rootdir, 'etc/hostname'), 'w') as nameout:
nameout.write(socket.gethostname() + '\n')
set_machine_id(rootdir)
selinuxconfig = os.path.join(rootdir, 'etc/selinux/config')
policy = None
if os.path.exists(selinuxconfig):
+6
View File
@@ -268,6 +268,7 @@ def capture_fs(args):
masker.mask('/etc/netplan/*.yaml')
# an empty hostname keeps the node name the deployment initramfs set
masker.mask('/etc/hostname', '/run/imgutil/captmp/empty')
masker.mask('/etc/machine-id', '/run/imgutil/captmp/machine-id')
mksquashargs = ['mksquashfs', '/run/imgutil/capin', fname + '.sfs', '-comp', 'xz']
excludes = get_capture_excludes(mdir)
exfile = None
@@ -491,6 +492,11 @@ def capture_system_back(args):
shadowout.write(newgshadow)
with open('/run/imgutil/captmp/empty', 'w') as shadowout:
pass
# an empty machine-id makes systemd generate one at boot without first
# boot semantics, the deployment sets a stable one per node
with open('/run/imgutil/captmp/machine-id', 'w') as midout:
pass
os.chmod('/run/imgutil/captmp/machine-id', 0o444)
i = 0
todelete = []
with open('/run/imgutil/capout/rootimg.sfs.plain', 'wb') as outimg: