diff options
author | Kent Overstreet <kent.overstreet@linux.dev> | 2024-03-14 23:03:19 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2024-03-14 23:03:19 -0400 |
commit | baa981737d65a23b1c0c55d004d4f2301a212a4b (patch) | |
tree | 06bac24697ab6b304f4aecde84410ddf698aabc5 |
query-uuid: initial commit
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | query-uuid.c | 52 |
3 files changed, 64 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4c364c5 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ + +PKG_CONFIG?=pkg-config + +PKGCONFIG_LIBS=uuid + +CFLAGS+=-std=gnu11 -O2 -g -Wall + +CFLAGS+=$(shell $(PKG_CONFIG) --cflags $(PKGCONFIG_LIBS)) +LDLIBS+=$(shell $(PKG_CONFIG) --libs $(PKGCONFIG_LIBS)) + +query-uuid: query-uuid.o @@ -0,0 +1 @@ +Simple commandline tool for FS_IOC_GETFSUUID diff --git a/query-uuid.c b/query-uuid.c new file mode 100644 index 0000000..6cba1b6 --- /dev/null +++ b/query-uuid.c @@ -0,0 +1,52 @@ +#include <fcntl.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ioctl.h> +#include <unistd.h> +#include <linux/types.h> + +#include <uuid/uuid.h> + +struct fsuuid2 { + __u8 len; + __u8 uuid[16]; +}; +#define FS_IOC_GETFSUUID _IOR(0x15, 0, struct fsuuid2) + +void die(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fputc('\n', stderr); + + _exit(EXIT_FAILURE); +} + +int main(int argc, char *argv[]) +{ + const char *path = argv[1] ?: "."; + int fd = open(path, O_RDONLY); + if (fd < 0) + die("Error opening %s: %m\n", path); + + struct fsuuid2 uuid; + if (ioctl(fd, FS_IOC_GETFSUUID, (unsigned long) &uuid)) + die("FS_IOC_GETFSUUID error: %m\n"); + + if (uuid.len == 16) { + uuid_t u; + memcpy(&u, uuid.uuid, 16); + + char str[37]; + uuid_unparse(u, str); + puts(str); + } else { + for (unsigned i = 0; i < uuid.len; i++) + printf("%x", uuid.uuid[i]); + } +} |