[PATCH] mkswap: generate UUID

Denys Vlasenko vda.linux at googlemail.com
Thu Jun 18 19:34:56 UTC 2009


On Thursday 18 June 2009 17:47, Colin Watson wrote:
> Many modern Linux installers use UUIDs rather than device names to
> identify devices. This works better if mkswap sets a UUID.
> 
> Signed-off-by: Colin Watson <cjwatson at ubuntu.com>

  CC      e2fsprogs/old_e2fsprogs/uuid/gen_uuid.o
cc1: warnings being treated as errors
e2fsprogs/old_e2fsprogs/uuid/gen_uuid.c: In function 'get_node_id':
e2fsprogs/old_e2fsprogs/uuid/gen_uuid.c:125: error: unused parameter 'node_id'
make[1]: *** [e2fsprogs/old_e2fsprogs/uuid/gen_uuid.o] Error 1
make: *** [e2fsprogs/old_e2fsprogs/uuid] Error 2

With it fixed:

# make bloatcheck
function                                             old     new   delta
uuid_generate_time                                     -     344    +344
get_random_fd                                          -     149    +149
uuid_unpack                                            -     108    +108
get_random_bytes                                       -     103    +103
uuid_unparse_x                                         -      95     +95
uuid_pack                                              -      84     +84
uuid_generate_random                                   -      80     +80
mkswap_main                                          210     270     +60
uuid_generate                                          -      34     +34
uuid_unparse                                           -      19     +19
static.last                                            -       8      +8
static.node_id                                         -       6      +6
static.has_init                                        -       4      +4
static.fd                                              -       4      +4
static.adjustment                                      -       4      +4
fmt_lower                                              -       4      +4
static.clock_seq                                       -       2      +2
------------------------------------------------------------------------------
(add/remove: 16/0 grow/shrink: 1/0 up/down: 1108/0)          Total: 1108 bytes


One *kilobyte* to generate 16 byte random number?

I propose this instead:

diff -d -urpN busybox.5/util-linux/mkswap.c busybox.6/util-linux/mkswap.c
--- busybox.5/util-linux/mkswap.c	2009-06-17 23:00:05.000000000 +0200
+++ busybox.6/util-linux/mkswap.c	2009-06-18 21:31:51.000000000 +0200
@@ -51,6 +51,28 @@ static void mkswap_selinux_setcontext(in
 #define mkswap_selinux_setcontext(fd, path) ((void)0)
 #endif
 
+static void mkswap_generate_uuid(uint8_t *buf)
+{
+	unsigned i;
+	char uuid_string[32];
+
+	/* rand() is guaranteed to generate at least [0, 2^15) range,
+	 * but lowest bits in some libc are not so "random".  */
+	srand((unsigned)monotonic_us() + getpid());
+	for (i = 0; i < 16; i++)
+		buf[i] = rand() >> 5;
+
+	bin2hex(uuid_string, (void*) buf, 16);
+	/* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
+	printf("UUID=%.8s"  "-%.4s-%.4s-%.4s-%.12s\n",
+		uuid_string,
+		uuid_string+8,
+		uuid_string+8+4,
+		uuid_string+8+4+4,
+		uuid_string+8+4+4+4
+	);
+}
+
 #if 0 /* from Linux 2.6.23 */
 /*
  * Magic header for a swap area. The first part of the union is
@@ -113,6 +135,7 @@ int mkswap_main(int argc, char **argv)
 	// Make a header. hdr is zero-filled so far...
 	hdr[0] = 1;
 	hdr[1] = (len / pagesize) - 1;
+	mkswap_generate_uuid((void*) &hdr[3]);
 
 	// Write the header.  Sync to disk because some kernel versions check
 	// signature on disk (not in cache) during swapon.


Please test.
--
vda


More information about the busybox mailing list