mkswap: generate UUID if CONFIG_DESKTOP=y. +110 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2009-06-18 21:55:47 +02:00
parent 2207cd6e14
commit c9f280cc46

View File

@ -5,7 +5,6 @@
*
* Licensed under GPL version 2, see file LICENSE in this tarball for details.
*/
#include "libbb.h"
#if ENABLE_SELINUX
@ -51,6 +50,32 @@ static void mkswap_selinux_setcontext(int fd, const char *path)
# define mkswap_selinux_setcontext(fd, path) ((void)0)
#endif
#if ENABLE_DESKTOP
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
);
}
#else
# define mkswap_generate_uuid(buf) ((void)0)
#endif
#if 0 /* from Linux 2.6.23 */
/*
* Magic header for a swap area. The first part of the union is
@ -113,10 +138,10 @@ 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.
xlseek(fd, 1024, SEEK_SET);
xwrite(fd, hdr, NWORDS * 4);
xlseek(fd, pagesize - 10, SEEK_SET);