Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sbin/gpt provide a copy of the kernel uuid generator (with p...
details: https://anonhg.NetBSD.org/src/rev/86c9e4d1267b
branches: trunk
changeset: 332683:86c9e4d1267b
user: christos <christos%NetBSD.org@localhost>
date: Fri Oct 03 20:30:06 2014 +0000
description:
provide a copy of the kernel uuid generator (with portable API calls) for
tools.
diffstat:
sbin/gpt/biosboot.c | 6 +-
sbin/gpt/create.c | 6 +-
sbin/gpt/gpt_uuid.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++-----
sbin/gpt/gpt_uuid.h | 2 +-
sbin/gpt/migrate.c | 6 +-
5 files changed, 106 insertions(+), 21 deletions(-)
diffs (240 lines):
diff -r e848d22c04e5 -r 86c9e4d1267b sbin/gpt/biosboot.c
--- a/sbin/gpt/biosboot.c Fri Oct 03 17:57:48 2014 +0000
+++ b/sbin/gpt/biosboot.c Fri Oct 03 20:30:06 2014 +0000
@@ -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>
diff -r e848d22c04e5 -r 86c9e4d1267b sbin/gpt/create.c
--- a/sbin/gpt/create.c Fri Oct 03 17:57:48 2014 +0000
+++ b/sbin/gpt/create.c Fri Oct 03 20:30:06 2014 +0000
@@ -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 @@
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 @@
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) *
diff -r e848d22c04e5 -r 86c9e4d1267b sbin/gpt/gpt_uuid.c
--- a/sbin/gpt/gpt_uuid.c Fri Oct 03 17:57:48 2014 +0000
+++ b/sbin/gpt/gpt_uuid.c Fri Oct 03 20:30:06 2014 +0000
@@ -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 @@
#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 @@
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 @@
}
#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);
+}
diff -r e848d22c04e5 -r 86c9e4d1267b sbin/gpt/gpt_uuid.h
--- a/sbin/gpt/gpt_uuid.h Fri Oct 03 17:57:48 2014 +0000
+++ b/sbin/gpt/gpt_uuid.h Fri Oct 03 20:30:06 2014 +0000
@@ -91,7 +91,7 @@
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
diff -r e848d22c04e5 -r 86c9e4d1267b sbin/gpt/migrate.c
--- a/sbin/gpt/migrate.c Fri Oct 03 17:57:48 2014 +0000
+++ b/sbin/gpt/migrate.c Fri Oct 03 20:30:06 2014 +0000
@@ -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 @@
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 @@
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. */
Home |
Main Index |
Thread Index |
Old Index