From e28f2406f59dd4fad0ae62d78627c17c5d0c2fe2 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Thu, 3 Oct 2024 15:42:43 +0200 Subject: [PATCH] clanModules: Fix disk-id and machine-id --- clanModules/disk-id/roles/default.nix | 2 +- clanModules/{ => disk-id/roles}/uuid4.sh | 0 clanModules/machine-id/roles/default.nix | 2 +- clanModules/machine-id/roles/uuid4.sh | 20 ++++++++++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) rename clanModules/{ => disk-id/roles}/uuid4.sh (100%) create mode 100644 clanModules/machine-id/roles/uuid4.sh diff --git a/clanModules/disk-id/roles/default.nix b/clanModules/disk-id/roles/default.nix index 36d21a065..330718aad 100644 --- a/clanModules/disk-id/roles/default.nix +++ b/clanModules/disk-id/roles/default.nix @@ -14,7 +14,7 @@ pkgs.bash ]; script = '' - uuid=$(bash ${../../uuid4.sh}) + uuid=$(bash ${./uuid4.sh}) # Remove the hyphens from the UUID uuid_no_hyphens=$(echo -n "$uuid" | tr -d '-') diff --git a/clanModules/uuid4.sh b/clanModules/disk-id/roles/uuid4.sh similarity index 100% rename from clanModules/uuid4.sh rename to clanModules/disk-id/roles/uuid4.sh diff --git a/clanModules/machine-id/roles/default.nix b/clanModules/machine-id/roles/default.nix index 062c0c6ca..a15b58196 100644 --- a/clanModules/machine-id/roles/default.nix +++ b/clanModules/machine-id/roles/default.nix @@ -32,7 +32,7 @@ in pkgs.bash ]; script = '' - uuid=$(bash ${../../uuid4.sh}) + uuid=$(bash ${./uuid4.sh}) # Remove the hyphens from the UUID uuid_no_hyphens=$(echo -n "$uuid" | tr -d '-') diff --git a/clanModules/machine-id/roles/uuid4.sh b/clanModules/machine-id/roles/uuid4.sh new file mode 100644 index 000000000..ca3a7c47e --- /dev/null +++ b/clanModules/machine-id/roles/uuid4.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +# Read 16 bytes from /dev/urandom +uuid=$(dd if=/dev/urandom bs=1 count=16 2>/dev/null | od -An -tx1 | tr -d ' \n') + +# Break the UUID into pieces and apply the required modifications +byte6=${uuid:12:2} +byte8=${uuid:16:2} + +# Construct the correct version and variant +hex_byte6=$(printf "%x" $((0x$byte6 & 0x0F | 0x40))) +hex_byte8=$(printf "%x" $((0x$byte8 & 0x3F | 0x80))) + +# Rebuild the UUID with the correct fields +uuid_v4="${uuid:0:12}${hex_byte6}${uuid:14:2}${hex_byte8}${uuid:18:14}" + +# Format the UUID correctly 8-4-4-4-12 +uuid_formatted="${uuid_v4:0:8}-${uuid_v4:8:4}-${uuid_v4:12:4}-${uuid_v4:16:4}-${uuid_v4:20:12}" + +echo -n "$uuid_formatted" \ No newline at end of file