]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
UUIDs
authorKent Overstreet <kent.overstreet@gmail.com>
Fri, 8 Oct 2010 14:04:49 +0000 (07:04 -0700)
committerKent Overstreet <kent.overstreet@gmail.com>
Fri, 8 Oct 2010 14:04:49 +0000 (07:04 -0700)
61-bcache.rules [new file with mode: 0644]
Makefile
bcache-test.c
bcache.h [new file with mode: 0644]
initramfs [new file with mode: 0644]
make-bcache.c
probe-bcache.c [new file with mode: 0644]

diff --git a/61-bcache.rules b/61-bcache.rules
new file mode 100644 (file)
index 0000000..acedefe
--- /dev/null
@@ -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}"
index a47719d75926e34dea4b5dc12d386efbd4c6005b..62e0fb9d84f324b47cc7bf9c41141f74f5ab7204 100644 (file)
--- 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
index 5858cab12e80c5f9da3c696cfa37d5b7726dfe77..0f8ad3761bdf8868266765e301c8faed5f670f66 100644 (file)
@@ -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 (file)
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 (file)
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
index 04861f91ae785cfe1bdbcb64fe6b96a2e078ec05..a7381d0ae1630d373be595aa08f2c8524aaed0e4 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <uuid/uuid.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];
-       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 (file)
index 0000000..ec81ea7
--- /dev/null
@@ -0,0 +1,67 @@
+#define _FILE_OFFSET_BITS      64
+#define __USE_FILE_OFFSET64
+#define _XOPEN_SOURCE 500
+
+#include <fcntl.h>
+#include <linux/fs.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <uuid/uuid.h>
+
+#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;
+}