2
0
mirror of https://github.com/xcat2/confluent.git synced 2026-11-23 10:52:41 +00:00

Merge pull request #365 from Obihoernchen/fix/capture-machine-id

Give every diskless and installed node its own machine-id
This commit is contained in:
Jarrod Johnson
2026-11-04 10:05:50 -05:00
committed by GitHub
10 changed files with 164 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):