From 7e9790107563dfa88ccd89aa0d0fa33266848400 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Fri, 8 Oct 2010 07:04:49 -0700 Subject: [PATCH] UUIDs --- 61-bcache.rules | 3 +++ Makefile | 13 +++++++--- bcache-test.c | 4 +-- bcache.h | 29 +++++++++++++++++++++ initramfs | 11 ++++++++ make-bcache.c | 40 +++++++++++++---------------- probe-bcache.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 138 insertions(+), 29 deletions(-) create mode 100644 61-bcache.rules create mode 100644 bcache.h create mode 100644 initramfs create mode 100644 probe-bcache.c diff --git a/61-bcache.rules b/61-bcache.rules new file mode 100644 index 0000000..acedefe --- /dev/null +++ b/61-bcache.rules @@ -0,0 +1,3 @@ +KERNEL=="sd*", ENV{DEVTYPE}=="disk", IMPORT{program}="/sbin/probe-bcache -o udev $tempnode" + +ENV{ID_FS_UUID_ENC}=="?*", SYMLINK+="disk/by-uuid/$env{ID_FS_UUID_ENC}" diff --git a/Makefile b/Makefile index a47719d..62e0fb9 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,20 @@ -PREFIX=/usr/local +#PREFIX=/usr/local CFLAGS=-O2 -Wall -g -all: make-bcache +all: make-bcache probe-bcache clean: rm -f make-bcache bcache-test *.o -install: make-bcache +install: make-bcache probe-bcache install -m0755 make-bcache ${PREFIX}/sbin/ - install -m0755 bcache-test ${PREFIX}/sbin/ + install -m0755 probe-bcache ${PREFIX}/sbin/ + install -m0644 61-bcache.rules /lib/udev/rules.d/ + install -m0755 initramfs /usr/share/initramfs-tools/hooks/bcache +# install -m0755 bcache-test ${PREFIX}/sbin/ bcache-test: LDFLAGS += -lm -lssl -lcrypto +make-bcache: LDFLAGS += -luuid +probe-bcache: LDFLAGS += -luuid diff --git a/bcache-test.c b/bcache-test.c index 5858cab..0f8ad37 100644 --- a/bcache-test.c +++ b/bcache-test.c @@ -62,7 +62,7 @@ double normal() n = 0 / (double) 0; return x; } - + do { x = random() / (double) (RAND_MAX / 2) - 1; y = random() / (double) (RAND_MAX / 2) - 1; @@ -233,7 +233,7 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } //setvbuf(stdout, NULL, _IONBF, 0); - + for (i = 0; !benchmark || i < benchmark; i++) { bool writing = (wtest && (i & 1)) || !rtest; nbytes = randsize ? drand48() * 16 + 1 : 1; diff --git a/bcache.h b/bcache.h new file mode 100644 index 0000000..448b6a5 --- /dev/null +++ b/bcache.h @@ -0,0 +1,29 @@ +#ifndef _BCACHE_H +#define _BCACHE_H + +static const char bcache_magic[] = { + 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca, + 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 }; + +struct cache_sb { + uint8_t magic[16]; +#define CACHE_CLEAN 1 +#define CACHE_SYNC 2 + uint32_t version; + uint16_t block_size; /* sectors */ + uint16_t bucket_size; /* sectors */ + uint32_t journal_start; /* buckets */ + uint32_t first_bucket; /* start of data */ + uint64_t nbuckets; /* device size */ + uint64_t btree_root; + uint16_t btree_level; + uint16_t _pad[3]; + uint8_t uuid[16]; +}; + +struct bucket_disk { + uint16_t priority; + uint8_t generation; +} __attribute((packed)); + +#endif diff --git a/initramfs b/initramfs new file mode 100644 index 0000000..3815dc9 --- /dev/null +++ b/initramfs @@ -0,0 +1,11 @@ +#!/bin/sh -e + +case "$1" in +prereqs) + echo "udev" + exit 0 + ;; +esac + +cp -p /lib/udev/rules.d/61-bcache.rules $DESTDIR/lib/udev/rules.d/ +cp -p /sbin/probe-bcache $DESTDIR/sbin diff --git a/make-bcache.c b/make-bcache.c index 04861f9..a7381d0 100644 --- a/make-bcache.c +++ b/make-bcache.c @@ -12,27 +12,9 @@ #include #include #include +#include -static const char bcache_magic[] = { - 0xc6, 0x85, 0x73, 0xf6, 0x4e, 0x1a, 0x45, 0xca, - 0x82, 0x65, 0xf5, 0x7f, 0x48, 0xba, 0x6d, 0x81 }; - -struct cache_sb { - uint8_t magic[16]; - uint32_t version; - uint16_t block_size; /* sectors */ - uint16_t bucket_size; /* sectors */ - uint32_t journal_start; /* buckets */ - uint32_t first_bucket; /* start of data */ - uint64_t nbuckets; /* device size */ - uint64_t btree_root; - uint16_t btree_level; -}; - -struct bucket_disk { - uint16_t priority; - uint8_t generation; -} __attribute((packed)); +#include "bcache.h" char zero[4096]; @@ -85,12 +67,21 @@ int main(int argc, char **argv) int64_t nblocks, bucketsize = 32, blocksize = 8; int fd, i, c; struct cache_sb sb; + char uuid[40]; - while ((c = getopt(argc, argv, "b:")) != -1) + uuid_generate(sb.uuid); + + while ((c = getopt(argc, argv, "U:b:")) != -1) switch (c) { case 'b': bucketsize = hatoi(optarg) / 512; break; + case 'U': + if (uuid_parse(optarg, sb.uuid)) { + printf("Bad uuid\n"); + exit(EXIT_FAILURE); + } + break; } if (argc <= optind) { @@ -117,6 +108,7 @@ int main(int argc, char **argv) sb.block_size = blocksize; sb.bucket_size = bucketsize; sb.nbuckets = nblocks / sb.bucket_size; + uuid_unparse(sb.uuid, uuid); do sb.first_bucket = ((--sb.nbuckets * sizeof(struct bucket_disk)) + (24 << 9)) / (sb.bucket_size << 9) + 1; @@ -131,12 +123,14 @@ int main(int argc, char **argv) "bucket_size: %u\n" "journal_start: %u\n" "first_bucket: %u\n" - "nbuckets: %ju\n", + "nbuckets: %ju\n" + "UUID: %s\n", sb.block_size, sb.bucket_size, sb.journal_start, sb.first_bucket, - sb.nbuckets); + sb.nbuckets, + uuid); /* Zero out priorities */ lseek(fd, 4096, SEEK_SET); diff --git a/probe-bcache.c b/probe-bcache.c new file mode 100644 index 0000000..ec81ea7 --- /dev/null +++ b/probe-bcache.c @@ -0,0 +1,67 @@ +#define _FILE_OFFSET_BITS 64 +#define __USE_FILE_OFFSET64 +#define _XOPEN_SOURCE 500 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bcache.h" + +int main(int argc, char **argv) +{ + bool udev = false; + int i, o; + extern char *optarg; + struct cache_sb sb; + char uuid[40]; + + while ((o = getopt(argc, argv, "o:")) != EOF) + switch (o) { + case 'o': + if (strcmp("udev", optarg)) { + printf("Invalid output format %s\n", optarg); + exit(EXIT_FAILURE); + } + udev = true; + break; + } + + + argv += optind; + argc -= optind; + + for (i = 0; i < argc; i++) { + int fd = open(argv[i], O_RDONLY); + if (fd == -1) + continue; + + + if (pread(fd, &sb, sizeof(sb), 4096) != sizeof(sb)) + continue; + + if (memcmp(sb.magic, bcache_magic, 16)) + continue; + + uuid_unparse(sb.uuid, uuid); + + if (udev) + printf("ID_FS_UUID=%s\n" + "ID_FS_UUID_ENC=%s\n" + "ID_FS_TYPE=bcache\n", + uuid, uuid); + else + printf("%s: UUID=\"\" TYPE=\"bcache\"\n", uuid); + } + + return 0; +} -- 2.39.2