aboutsummaryrefslogtreecommitdiffstats
path: root/probe-bcache.c
blob: a6400468347e5f30ae0cc971a63747a71c5d67c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Author: Kent Overstreet <kmo@daterainc.com>
 *
 * GPLv2
 */

#define _FILE_OFFSET_BITS	64
#define __USE_FILE_OFFSET64
#define _XOPEN_SOURCE 500

#include <blkid.h>
#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_disk sb_disk;
	char uuid[40];
	blkid_probe pr;

	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 (!(pr = blkid_new_probe()))
			continue;
		if (blkid_probe_set_device(pr, fd, 0, 0))
			continue;
		/* probe partitions too */
		if (blkid_probe_enable_partitions(pr, true))
			continue;
		/* bail if anything was found
		 * probe-bcache isn't needed once blkid recognizes bcache */
		if (!blkid_do_probe(pr)) {
			continue;
		}

		if (pread(fd, &sb_disk, sizeof(sb_disk), SB_START) != sizeof(sb_disk))
			continue;

		if (memcmp(sb_disk.magic, bcache_magic, 16))
			continue;

		uuid_unparse(sb_disk.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;
}