Module Name: src Committed By: christos Date: Fri Oct 3 20:30:06 UTC 2014
Modified Files: src/sbin/gpt: biosboot.c create.c gpt_uuid.c gpt_uuid.h migrate.c Log Message: provide a copy of the kernel uuid generator (with portable API calls) for tools. To generate a diff of this commit: cvs rdiff -u -r1.13 -r1.14 src/sbin/gpt/biosboot.c cvs rdiff -u -r1.11 -r1.12 src/sbin/gpt/create.c cvs rdiff -u -r1.6 -r1.7 src/sbin/gpt/gpt_uuid.c cvs rdiff -u -r1.2 -r1.3 src/sbin/gpt/gpt_uuid.h cvs rdiff -u -r1.20 -r1.21 src/sbin/gpt/migrate.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sbin/gpt/biosboot.c diff -u src/sbin/gpt/biosboot.c:1.13 src/sbin/gpt/biosboot.c:1.14 --- src/sbin/gpt/biosboot.c:1.13 Thu Oct 2 15:15:21 2014 +++ src/sbin/gpt/biosboot.c Fri Oct 3 16:30:06 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: biosboot.c,v 1.13 2014/10/02 19:15:21 joerg Exp $ */ +/* $NetBSD: biosboot.c,v 1.14 2014/10/03 20:30:06 christos Exp $ */ /* * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -37,13 +37,13 @@ #include <sys/cdefs.h> #ifdef __RCSID -__RCSID("$NetBSD: biosboot.c,v 1.13 2014/10/02 19:15:21 joerg Exp $"); +__RCSID("$NetBSD: biosboot.c,v 1.14 2014/10/03 20:30:06 christos Exp $"); #endif #include <sys/stat.h> #include <sys/types.h> #include <sys/ioctl.h> -#ifndef HAVE_NBTOOL_CONFIG_H +#ifdef DIOCGWEDGEINFO #include <sys/disk.h> #endif #include <sys/param.h> Index: src/sbin/gpt/create.c diff -u src/sbin/gpt/create.c:1.11 src/sbin/gpt/create.c:1.12 --- src/sbin/gpt/create.c:1.11 Thu Oct 2 20:51:31 2014 +++ src/sbin/gpt/create.c Fri Oct 3 16:30:06 2014 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: create.c,v 1.11 2014/10/03 00:51:31 jnemeth Exp $"); +__RCSID("$NetBSD: create.c,v 1.12 2014/10/03 20:30:06 christos Exp $"); #endif #include <sys/types.h> @@ -174,7 +174,7 @@ create(int fd) hdr->hdr_lba_alt = htole64(last); hdr->hdr_lba_start = htole64(tbl->map_start + blocks); hdr->hdr_lba_end = htole64(last - blocks - 1LL); - gpt_uuid_create_new(hdr->hdr_guid); + gpt_uuid_generate(hdr->hdr_guid); hdr->hdr_lba_table = htole64(tbl->map_start); hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent)); if (le32toh(hdr->hdr_entries) > parts) @@ -183,7 +183,7 @@ create(int fd) ent = tbl->map_data; for (i = 0; i < le32toh(hdr->hdr_entries); i++) { - gpt_uuid_create_new(ent[i].ent_guid); + gpt_uuid_generate(ent[i].ent_guid); } hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) * Index: src/sbin/gpt/gpt_uuid.c diff -u src/sbin/gpt/gpt_uuid.c:1.6 src/sbin/gpt/gpt_uuid.c:1.7 --- src/sbin/gpt/gpt_uuid.c:1.6 Thu Oct 2 20:51:31 2014 +++ src/sbin/gpt/gpt_uuid.c Fri Oct 3 16:30:06 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: gpt_uuid.c,v 1.6 2014/10/03 00:51:31 jnemeth Exp $ */ +/* $NetBSD: gpt_uuid.c,v 1.7 2014/10/03 20:30:06 christos Exp $ */ /*- * Copyright (c) 2014 The NetBSD Foundation, Inc. @@ -32,7 +32,7 @@ #include <sys/cdefs.h> #ifdef __RCSID -__RCSID("$NetBSD: gpt_uuid.c,v 1.6 2014/10/03 00:51:31 jnemeth Exp $"); +__RCSID("$NetBSD: gpt_uuid.c,v 1.7 2014/10/03 20:30:06 christos Exp $"); #endif #include <stdio.h> @@ -44,10 +44,6 @@ __RCSID("$NetBSD: gpt_uuid.c,v 1.6 2014/ #include <sys/endian.h> #endif -#if !defined(HAVE_NBTOOL_CONFIG_H) -#include <sys/types.h> -#include <sys/uuid.h> -#endif const gpt_uuid_t gpt_uuid_nil; @@ -60,6 +56,12 @@ struct dce_uuid { uint8_t node[6]; }; +struct kern_uuid { + uint64_t ll; + uint16_t seq; + uint8_t node[6]; +}; + static const struct { struct dce_uuid u; const char *n; @@ -237,12 +239,95 @@ gpt_uuid_create(gpt_type_t t, gpt_uuid_t } #if !defined(HAVE_NBTOOL_CONFIG_H) -void -gpt_uuid_create_new(gpt_uuid_t t) +#include <sys/types.h> +#include <sys/uuid.h> +#else +#include <time.h> +/* + * Get the current time as a 5x bit count of 100000-microsecond intervals + * since 00:00:00.00, October 15,1582. We apply a magic offset to convert + * the Unix time since 00:00:00.00, January 1, 1970 to the date of the + * Gregorian reform to the Christian calendar. + */ +static uint64_t +uuid_time(void) +{ + struct timeval tv; + uint64_t xtime = 0x01B21DD213814000LL; + + (void)gettimeofday(&tv, NULL); + xtime += (uint64_t)tv.tv_sec * 10000000LL; + xtime += (uint64_t)(tv.tv_usec / 100000); + return (xtime & ((1LL << 60) - 1LL)); +} + +/* + * No portable way to get ethernet, use hostid instead + */ +static void +uuid_node(uint8_t node[6]) +{ + long hid = gethostid(); + node[0] = 'N'; + node[1] = 'B'; + node[2] = (hid >> 24) & 0xff; + node[3] = (hid >> 16) & 0xff; + node[4] = (hid >> 8) & 0xff; + node[5] = (hid >> 0) & 0xff; +} + +static void +uuid_generate(void *u, uint64_t *timep, int count) { - struct uuid u; + static struct kern_uuid uuid_last; + uint64_t xtime, ltime; + uint16_t lseq; + struct kern_uuid *uuid = u; + + uuid_node(uuid->node); + xtime = uuid_time(); + *timep = xtime; + + if (uuid_last.ll == 0LL || uuid_last.node[0] != uuid->node[0] || + uuid_last.node[1] != uuid->node[1] || + uuid_last.node[2] != uuid->node[2]) { + srandom((unsigned int) xtime); + uuid->seq = (uint16_t)random() & 0x3fff; + } else if (uuid_last.ll >= xtime) + uuid->seq = (uuid_last.seq + 1) & 0x3fff; + + uuid_last = *uuid; + uuid_last.ll = (xtime + count - 1) & ((1LL << 60) - 1LL); +} - uuidgen(&u, 1); - gpt_dce_to_uuid((struct dce_uuid *)&u, t); +static void +uuidgen(struct dce_uuid *store, int count) +{ + uint64_t xtime; + struct kern_uuid uuid; + int i; + + /* Generate the base UUID. */ + uuid_generate(&uuid, &xtime, count); + + for (i = 0; i < count; xtime++, i++) { + /* Set time and version (=1) and deal with byte order. */ + store[i].time_low = (uint32_t)xtime; + store[i].time_mid = (uint16_t)(xtime >> 32); + store[i].time_hi_and_version = + ((uint16_t)(xtime >> 48) & 0xfff) | (1 << 12); + store[i].clock_seq_hi_and_reserved = (uuid.seq >> 16) | 0x80; + store[i].clock_seq_low = uuid.seq & 0xff; + memcpy(store[i].node, uuid.node, sizeof(uuid.node)); + } } #endif + +void +gpt_uuid_generate(gpt_uuid_t t) +{ + struct dce_uuid u; + + uuidgen((void *)&u, 1); + gpt_dce_to_uuid(&u, t); +} Index: src/sbin/gpt/gpt_uuid.h diff -u src/sbin/gpt/gpt_uuid.h:1.2 src/sbin/gpt/gpt_uuid.h:1.3 --- src/sbin/gpt/gpt_uuid.h:1.2 Thu Oct 2 20:51:31 2014 +++ src/sbin/gpt/gpt_uuid.h Fri Oct 3 16:30:06 2014 @@ -91,7 +91,7 @@ void gpt_uuid_create(gpt_type_t, gpt_uui int gpt_uuid_parse(const char *, gpt_uuid_t); -void gpt_uuid_create_new(gpt_uuid_t); +void gpt_uuid_generate(gpt_uuid_t); __END_DECLS Index: src/sbin/gpt/migrate.c diff -u src/sbin/gpt/migrate.c:1.20 src/sbin/gpt/migrate.c:1.21 --- src/sbin/gpt/migrate.c:1.20 Thu Oct 2 20:51:31 2014 +++ src/sbin/gpt/migrate.c Fri Oct 3 16:30:06 2014 @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD: src/sbin/gpt/migrate.c,v 1.16 2005/09/01 02:42:52 marcel Exp $"); #endif #ifdef __RCSID -__RCSID("$NetBSD: migrate.c,v 1.20 2014/10/03 00:51:31 jnemeth Exp $"); +__RCSID("$NetBSD: migrate.c,v 1.21 2014/10/03 20:30:06 christos Exp $"); #endif #include <sys/types.h> @@ -335,7 +335,7 @@ migrate(int fd) hdr->hdr_lba_alt = htole64(tpg->map_start); hdr->hdr_lba_start = htole64(tbl->map_start + blocks); hdr->hdr_lba_end = htole64(lbt->map_start - 1LL); - gpt_uuid_create_new(hdr->hdr_guid); + gpt_uuid_generate(hdr->hdr_guid); hdr->hdr_lba_table = htole64(tbl->map_start); hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent)); if (le32toh(hdr->hdr_entries) > parts) @@ -344,7 +344,7 @@ migrate(int fd) ent = tbl->map_data; for (i = 0; i < le32toh(hdr->hdr_entries); i++) { - gpt_uuid_create_new(ent[i].ent_guid); + gpt_uuid_generate(ent[i].ent_guid); } /* Mirror partitions. */