1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 Google, Inc. All Rights Reserved.
5 * Test program which uses the raw ext4 set_fsuuid ioctl directly.
7 * $0 COMMAND MOUNT_POINT [UUID]
9 * COMMAND must be either "get" or "set".
10 * The UUID must be a 16 octet sequence represented as 32 hexadecimal digits in
11 * canonical textual representation, e.g. output from `uuidgen`.
22 #include <sys/ioctl.h>
23 #include <uuid/uuid.h>
32 #ifndef EXT4_IOC_GETFSUUID
33 #define EXT4_IOC_GETFSUUID _IOR('f', 44, struct fsuuid)
36 #ifndef EXT4_IOC_SETFSUUID
37 #define EXT4_IOC_SETFSUUID _IOW('f', 44, struct fsuuid)
40 int main(int argc, char **argv)
43 struct fsuuid *fsuuid = NULL;
46 fprintf(stderr, "Invalid arguments\n");
50 fd = open(argv[2], O_RDONLY);
56 fsuuid = malloc(sizeof(*fsuuid) + sizeof(uuid_t));
61 fsuuid->fsu_len = sizeof(uuid_t);
62 fsuuid->fsu_flags = 0;
64 if (strcmp(argv[1], "get") == 0) {
68 if (ioctl(fd, EXT4_IOC_GETFSUUID, fsuuid)) {
69 fprintf(stderr, "%s while trying to get fs uuid\n",
74 memcpy(&uuid, fsuuid->fsu_uuid, sizeof(uuid));
75 uuid_unparse(uuid, uuid_str);
76 printf("%s\n", uuid_str);
77 } else if (strcmp(argv[1], "set") == 0) {
81 fprintf(stderr, "UUID argument missing.\n");
85 error = uuid_parse(argv[3], uuid);
87 fprintf(stderr, "Invalid UUID. The UUID should be in "
88 "canonical format. Example: "
89 "8c628557-6987-42b2-ba16-b7cc79ddfb43\n");
93 memcpy(&fsuuid->fsu_uuid, uuid, sizeof(uuid));
94 if (ioctl(fd, EXT4_IOC_SETFSUUID, fsuuid)) {
95 fprintf(stderr, "%s while trying to set fs uuid\n",
100 fprintf(stderr, "Invalid command\n");