[git commit] util-linux: add minimal uuidgen implementation

Denys Vlasenko vda.linux at googlemail.com
Sun Feb 1 10:41:08 UTC 2026


commit: https://git.busybox.net/busybox/commit/?id=405afbd5518910149496ec97257b8402e7e647f6
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Add a simple uuidgen utility that generates RFC 4122 compliant
UUIDs (version 4, random). Uses the existing generate_uuid()
function from libbb and volume_id_set_uuid() for formatting.

Implementation suggested by Ulli.

Features:
- Generates standard format UUIDs: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
- RFC 4122 version 4 compliant
- Minimal implementation (~1.1 kb)
- NOFORK applet for efficiency
- Uses existing volume_id infrastructure for UUID formatting

function                                             old     new   delta
uuidgen_main                                           -      71     +71
applet_names                                        2862    2870      +8
.rodata                                           107010  107016      +6
applet_main                                         1648    1652      +4
applet_suid                                          103     104      +1
applet_install_loc                                   206     207      +1
applet_flags                                         103     104      +1
packed_usage                                       35952   35941     -11
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 6/1 up/down: 92/-11)             Total: 81 bytes

Signed-off-by: Osama Abdelkader <osama.abdelkader at gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 libbb/xfuncs_printf.c |  6 +++---
 util-linux/uuidgen.c  | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index d413c81e8..63054edc4 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -642,14 +642,14 @@ void FAST_FUNC generate_uuid(uint8_t *buf)
 	 * uint32_t time_low (big endian)
 	 * uint16_t time_mid (big endian)
 	 * uint16_t time_hi_and_version (big endian)
-	 *  version is a 4-bit field:
+	 *  version is a 4-bit field (most significant 4 bits):
 	 *   1 Time-based
 	 *   2 DCE Security, with embedded POSIX UIDs
 	 *   3 Name-based (MD5)
 	 *   4 Randomly generated
 	 *   5 Name-based (SHA-1)
 	 * uint16_t clk_seq_and_variant (big endian)
-	 *  variant is a 3-bit field:
+	 *  variant is a 3-bit field (most significant 3 bits):
 	 *   0xx Reserved, NCS backward compatibility
 	 *   10x The variant specified in rfc4122
 	 *   110 Reserved, Microsoft backward compatibility
@@ -681,7 +681,7 @@ void FAST_FUNC generate_uuid(uint8_t *buf)
 
 	/* version = 4 */
 	buf[4 + 2    ] = (buf[4 + 2    ] & 0x0f) | 0x40;
-	/* variant = 10x */
+	/* variant = 10x binary (uppermost 3 bits) */
 	buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
 }
 
diff --git a/util-linux/uuidgen.c b/util-linux/uuidgen.c
new file mode 100644
index 000000000..c2e1dd4da
--- /dev/null
+++ b/util-linux/uuidgen.c
@@ -0,0 +1,57 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini uuidgen implementation for busybox
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+//config:config UUIDGEN
+//config:	bool "uuidgen (1.1 kb)"
+//config:	default y
+//config:	help
+//config:	Generate a UUID (Universally Unique Identifier) in RFC 4122 format.
+
+//applet:IF_UUIDGEN(APPLET_NOFORK(uuidgen, uuidgen, BB_DIR_USR_BIN, BB_SUID_DROP, uuidgen))
+
+//kbuild:lib-$(CONFIG_UUIDGEN) += uuidgen.o
+
+//usage:#define uuidgen_trivial_usage
+//usage:       ""
+//usage:#define uuidgen_full_usage "\n\n"
+//usage:       "Generate a random UUID"
+
+/* util-linux 2.41.1:
+ * -r, --random          generate random-based uuid
+ * -t, --time            generate time-based uuid
+ * -n, --namespace <ns>  generate hash-based uuid in this namespace
+ *                       available namespaces: @dns @url @oid @x500
+ * -N, --name <name>     generate hash-based uuid from this name
+ * -m, --md5             generate md5 hash
+ * -C, --count <num>     generate more uuids in loop
+ * -s, --sha1            generate sha1 hash
+ * -6, --time-v6         generate time-based v6 uuid
+ * -7, --time-v7         generate time-based v7 uuid
+ * -x, --hex             interpret name as hex string
+ * manpage:
+ * "By default uuidgen will generate a random-based UUID if a high-quality random number generator is present.
+ * Otherwise, it will choose a time-based UUID."
+ */
+#include "libbb.h"
+#include "volume_id/volume_id_internal.h"
+
+/* This is a NOFORK applet. Be very careful! */
+
+int uuidgen_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int uuidgen_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
+{
+	struct volume_id id;
+	uint8_t uuid[16];
+
+	/* support/ignore -r (--random) */
+	getopt32(argv, "^" "r" "\0" "=0"/* no args!*/);
+
+	generate_uuid(uuid);
+	volume_id_set_uuid(&id, uuid, UUID_DCE);
+	puts(id.uuid);
+
+	return 0;
+}


More information about the busybox-cvs mailing list