diff --git a/confluent_osdeploy/el10-diskless/profiles/default/scripts/imageboot.sh b/confluent_osdeploy/el10-diskless/profiles/default/scripts/imageboot.sh index cb11505f..d1acfe83 100644 --- a/confluent_osdeploy/el10-diskless/profiles/default/scripts/imageboot.sh +++ b/confluent_osdeploy/el10-diskless/profiles/default/scripts/imageboot.sh @@ -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) diff --git a/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py index 7ee34db1..18cb99f1 100644 --- a/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el7-diskless/profiles/default/scripts/image2disk.py @@ -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): diff --git a/confluent_osdeploy/el7-diskless/profiles/default/scripts/imageboot.sh b/confluent_osdeploy/el7-diskless/profiles/default/scripts/imageboot.sh index b387aad5..1e82e84d 100644 --- a/confluent_osdeploy/el7-diskless/profiles/default/scripts/imageboot.sh +++ b/confluent_osdeploy/el7-diskless/profiles/default/scripts/imageboot.sh @@ -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 diff --git a/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py index 0230a580..66b3d38c 100644 --- a/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py @@ -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): diff --git a/confluent_osdeploy/el8-diskless/profiles/default/scripts/imageboot.sh b/confluent_osdeploy/el8-diskless/profiles/default/scripts/imageboot.sh index d9898e80..747b28b8 100644 --- a/confluent_osdeploy/el8-diskless/profiles/default/scripts/imageboot.sh +++ b/confluent_osdeploy/el8-diskless/profiles/default/scripts/imageboot.sh @@ -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. diff --git a/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py index f26babcb..ce03f84b 100644 --- a/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py @@ -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): diff --git a/confluent_osdeploy/el9-diskless/profiles/default/scripts/imageboot.sh b/confluent_osdeploy/el9-diskless/profiles/default/scripts/imageboot.sh index a374958c..c94b25f6 100644 --- a/confluent_osdeploy/el9-diskless/profiles/default/scripts/imageboot.sh +++ b/confluent_osdeploy/el9-diskless/profiles/default/scripts/imageboot.sh @@ -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. diff --git a/confluent_osdeploy/suse15-diskless/profiles/default/scripts/imageboot.sh b/confluent_osdeploy/suse15-diskless/profiles/default/scripts/imageboot.sh index 7be5223b..5e077464 100644 --- a/confluent_osdeploy/suse15-diskless/profiles/default/scripts/imageboot.sh +++ b/confluent_osdeploy/suse15-diskless/profiles/default/scripts/imageboot.sh @@ -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="" diff --git a/confluent_osdeploy/suse16-diskless/profiles/default/scripts/imageboot.sh b/confluent_osdeploy/suse16-diskless/profiles/default/scripts/imageboot.sh index d49813cb..65b5dd87 100644 --- a/confluent_osdeploy/suse16-diskless/profiles/default/scripts/imageboot.sh +++ b/confluent_osdeploy/suse16-diskless/profiles/default/scripts/imageboot.sh @@ -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 diff --git a/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py index 176d80c5..151e60db 100644 --- a/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py @@ -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): diff --git a/imgutil/imgutil b/imgutil/imgutil index b62be75b..91943eed 100644 --- a/imgutil/imgutil +++ b/imgutil/imgutil @@ -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: