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