]> git.sesse.net Git - bcachefs-tools-debian/blob - probe-bcache.c
bcache-tools are GPL
[bcachefs-tools-debian] / probe-bcache.c
1 /*
2  * Author: Kent Overstreet <kmo@daterainc.com>
3  *
4  * GPLv2
5  */
6
7 #define _FILE_OFFSET_BITS       64
8 #define __USE_FILE_OFFSET64
9 #define _XOPEN_SOURCE 500
10
11 #include <fcntl.h>
12 #include <linux/fs.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <uuid/uuid.h>
23
24 #include "bcache.h"
25
26 int main(int argc, char **argv)
27 {
28         bool udev = false;
29         int i, o;
30         extern char *optarg;
31         struct cache_sb sb;
32         char uuid[40];
33
34         while ((o = getopt(argc, argv, "o:")) != EOF)
35                 switch (o) {
36                 case 'o':
37                         if (strcmp("udev", optarg)) {
38                                 printf("Invalid output format %s\n", optarg);
39                                 exit(EXIT_FAILURE);
40                         }
41                         udev = true;
42                         break;
43                 }
44
45
46         argv += optind;
47         argc -= optind;
48
49         for (i = 0; i < argc; i++) {
50                 int fd = open(argv[i], O_RDONLY);
51                 if (fd == -1)
52                         continue;
53
54
55                 if (pread(fd, &sb, sizeof(sb), 4096) != sizeof(sb))
56                         continue;
57
58                 if (memcmp(sb.magic, bcache_magic, 16))
59                         continue;
60
61                 uuid_unparse(sb.uuid, uuid);
62
63                 if (udev)
64                         printf("ID_FS_UUID=%s\n"
65                                "ID_FS_UUID_ENC=%s\n"
66                                "ID_FS_TYPE=bcache\n",
67                                uuid, uuid);
68                 else
69                         printf("%s: UUID=\"\" TYPE=\"bcache\"\n", uuid);
70         }
71
72         return 0;
73 }