2 * vol_id - read filesystem label and uuid
4 * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 #include <sys/ioctl.h>
38 #include "../../udev/udev.h"
39 #include "lib/libvolume_id.h"
41 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
44 struct udev *udev_ctx;
46 static void log_fn(struct udev *udev, int priority,
47 const char *file, int line, const char *fn,
48 const char *format, va_list args)
51 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
52 vfprintf(stderr, format, args);
54 vsyslog(priority, format, args);
58 static void vid_log(int priority, const char *file, int line, const char *format, ...)
62 if (priority > udev_get_log_priority(udev_ctx))
64 va_start(args, format);
65 log_fn(udev_ctx, priority, file, line, NULL, format, args);
70 static int all_probers(volume_id_probe_fn_t probe_fn,
71 struct volume_id *id, uint64_t off, uint64_t size,
76 if (probe_fn(id, off, size) == 0)
77 if (volume_id_get_type(id, &type))
83 int main(int argc, char *argv[])
85 static const struct option options[] = {
86 { "label", no_argument, NULL, 'l' },
87 { "label-raw", no_argument, NULL, 'L' },
88 { "uuid", no_argument, NULL, 'u' },
89 { "type", no_argument, NULL, 't' },
90 { "export", no_argument, NULL, 'x' },
91 { "skip-raid", no_argument, NULL, 's' },
92 { "size", required_argument, NULL, 'S' },
93 { "probe-all", no_argument, NULL, 'a' },
94 { "offset", optional_argument, NULL, 'o' },
95 { "debug", no_argument, NULL, 'd' },
96 { "help", no_argument, NULL, 'h' },
106 } print = PRINT_EXPORT;
108 struct volume_id *vid = NULL;
109 char label_safe[256];
113 char uuid_sub_enc[256];
115 char type_version_enc[256];
122 const char *label, *uuid, *uuid_sub, *type, *type_version, *usage;
126 udev_ctx = udev_new();
127 if (udev_ctx == NULL)
129 logging_init("vol_id");
130 udev_set_log_fn(udev_ctx, log_fn);
132 /* hook in our debug into libvolume_id */
133 volume_id_log_fn = vid_log;
138 option = getopt_long(argc, argv, "lLutxsS:aodh", options, NULL);
145 if (udev_get_log_priority(udev_ctx) < LOG_INFO)
146 udev_set_log_priority(udev_ctx, LOG_INFO);
152 print = PRINT_LABEL_RAW;
161 print = PRINT_EXPORT;
170 if (optarg[0] != '\0')
171 size = strtoull(optarg, NULL, 0);
174 if (optarg[0] != '\0')
175 offset = strtoull(optarg, NULL, 0);
178 printf("Usage: vol_id [options] <device>\n"
179 " --export export key/value pairs\n"
180 " --type filesystem type\n"
181 " --label filesystem label\n"
182 " --label-raw raw label\n"
183 " --uuid filesystem uuid\n"
184 " --skip-raid don't probe for raid\n"
185 " --probe-all find possibly conflicting signatures\n"
186 " --offset=<bytes> probe at the given offset\n"
187 " --size=<bytes> overwrite device size\n"
188 " --debug print debug output to stderr\n"
199 err(udev_ctx, "no device\n");
200 fprintf(stderr, "no device\n");
205 fd = open(node, O_RDONLY);
207 fprintf(stderr, "%s: error opening volume\n", node);
212 vid = volume_id_open_fd(fd);
219 if (ioctl(fd, BLKGETSIZE64, &size) == 0) {
220 info(udev_ctx, "BLKGETSIZE64=%" PRIu64 " (%" PRIu64 "GB)\n", size, size >> 30);
224 if (fstat(fd, &statbuf) == 0 && S_ISREG(statbuf.st_mode))
225 size = statbuf.st_size;
228 info(udev_ctx, "stat=%" PRIu64 " (%" PRIu64 "GB)\n", size, size >> 30);
232 /* try to drop all privileges before reading disk content */
236 pw = getpwnam("nobody");
237 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
238 if (setgroups(0, NULL) != 0 ||
239 setgid(pw->pw_gid) != 0 ||
240 setuid(pw->pw_uid) != 0)
241 info(udev_ctx, "unable to drop privileges: %s\n\n", strerror(errno));
246 volume_id_all_probers(all_probers, vid, offset, size, NULL);
251 retval = volume_id_probe_filesystem(vid, offset, size);
253 retval = volume_id_probe_all(vid, offset, size);
255 fprintf(stderr, "unknown or non-unique volume type "
256 "(--probe-all lists possibly conflicting types)\n");
261 if (!volume_id_get_label(vid, &label) ||
262 !volume_id_get_usage(vid, &usage) ||
263 !volume_id_get_type(vid, &type) ||
264 !volume_id_get_type_version(vid, &type_version) ||
265 !volume_id_get_uuid(vid, &uuid) ||
266 !volume_id_get_uuid_sub(vid, &uuid_sub)) {
271 udev_util_replace_whitespace(label, label_safe, sizeof(label_safe));
272 udev_util_replace_chars(label_safe, UDEV_ALLOWED_CHARS_INPUT);
273 volume_id_encode_string(label, label_enc, sizeof(label_enc));
275 udev_util_replace_whitespace(uuid, uuid_safe, sizeof(uuid_safe));
276 udev_util_replace_chars(uuid_safe, UDEV_ALLOWED_CHARS_INPUT);
277 volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
279 volume_id_encode_string(uuid_sub, uuid_sub_enc, sizeof(uuid_sub_enc));
281 volume_id_encode_string(type, type_enc, sizeof(type_enc));
282 volume_id_encode_string(type_version, type_version_enc, sizeof(type_version_enc));
286 printf("ID_FS_USAGE=%s\n", usage);
287 printf("ID_FS_TYPE=%s\n", type_enc);
288 printf("ID_FS_VERSION=%s\n", type_version_enc);
289 printf("ID_FS_UUID=%s\n", uuid_safe);
290 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
291 if (uuid_sub_enc[0] != '\0')
292 printf("ID_FS_UUID_SUB_ENC=%s\n", uuid_sub_enc);
293 printf("ID_FS_LABEL=%s\n", label_safe);
294 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
297 printf("%s\n", type);
300 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
304 printf("%s\n", label_safe);
307 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
311 printf("%s\n", uuid_enc);
313 case PRINT_LABEL_RAW:
314 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
318 printf("%s\n", label);
324 volume_id_close(vid);
325 udev_unref(udev_ctx);