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/>.
35 #include <sys/ioctl.h>
37 #include "../../udev/udev.h"
38 #include "lib/libvolume_id.h"
40 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
43 struct udev *udev_ctx;
45 static void log_fn(struct udev *udev, int priority,
46 const char *file, int line, const char *fn,
47 const char *format, va_list args)
50 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
51 vfprintf(stderr, format, args);
53 vsyslog(priority, format, args);
57 static void vid_log(int priority, const char *file, int line, const char *format, ...)
61 if (priority > udev_get_log_priority(udev_ctx))
63 va_start(args, format);
64 log_fn(udev_ctx, priority, file, line, NULL, format, args);
69 static int all_probers(volume_id_probe_fn_t probe_fn,
70 struct volume_id *id, uint64_t off, uint64_t size,
75 if (probe_fn(id, off, size) == 0)
76 if (volume_id_get_type(id, &type))
82 int main(int argc, char *argv[])
84 static const struct option options[] = {
85 { "label", no_argument, NULL, 'l' },
86 { "label-raw", no_argument, NULL, 'L' },
87 { "uuid", no_argument, NULL, 'u' },
88 { "type", no_argument, NULL, 't' },
89 { "export", no_argument, NULL, 'x' },
90 { "skip-raid", no_argument, NULL, 's' },
91 { "size", required_argument, NULL, 'S' },
92 { "probe-all", no_argument, NULL, 'a' },
93 { "offset", optional_argument, NULL, 'o' },
94 { "debug", no_argument, NULL, 'd' },
95 { "help", no_argument, NULL, 'h' },
105 } print = PRINT_EXPORT;
107 struct volume_id *vid = NULL;
108 char label_safe[256];
113 char type_version_enc[256];
120 const char *label, *uuid, *type, *type_version, *usage;
124 udev_ctx = udev_new();
125 if (udev_ctx == NULL)
127 logging_init("vol_id");
128 udev_set_log_fn(udev_ctx, log_fn);
130 /* hook in our debug into libvolume_id */
131 volume_id_log_fn = vid_log;
136 option = getopt_long(argc, argv, "lLutxsS:aodh", options, NULL);
143 if (udev_get_log_priority(udev_ctx) < LOG_INFO)
144 udev_set_log_priority(udev_ctx, LOG_INFO);
150 print = PRINT_LABEL_RAW;
159 print = PRINT_EXPORT;
168 if (optarg[0] != '\0')
169 size = strtoull(optarg, NULL, 0);
172 if (optarg[0] != '\0')
173 offset = strtoull(optarg, NULL, 0);
176 printf("Usage: vol_id [options] <device>\n"
177 " --export export key/value pairs\n"
178 " --type filesystem type\n"
179 " --label filesystem label\n"
180 " --label-raw raw label\n"
181 " --uuid filesystem uuid\n"
182 " --skip-raid don't probe for raid\n"
183 " --probe-all find possibly conflicting signatures\n"
184 " --offset=<bytes> probe at the given offset\n"
185 " --size=<bytes> overwrite device size\n"
186 " --debug print debug output to stderr\n"
197 err(udev_ctx, "no device\n");
198 fprintf(stderr, "no device\n");
203 fd = open(node, O_RDONLY);
205 fprintf(stderr, "%s: error opening volume\n", node);
210 vid = volume_id_open_fd(fd);
217 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
219 info(udev_ctx, "BLKGETSIZE64=%" PRIu64 " (%" PRIu64 "GB)\n", size, size >> 30);
222 /* try to drop all privileges before reading disk content */
226 pw = getpwnam("nobody");
227 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
228 if (setgroups(0, NULL) != 0 ||
229 setgid(pw->pw_gid) != 0 ||
230 setuid(pw->pw_uid) != 0)
231 info(udev_ctx, "unable to drop privileges: %s\n\n", strerror(errno));
236 volume_id_all_probers(all_probers, vid, offset, size, NULL);
241 retval = volume_id_probe_filesystem(vid, offset, size);
243 retval = volume_id_probe_all(vid, offset, size);
245 fprintf(stderr, "unknown or non-unique volume type "
246 "(--probe-all lists possibly conflicting types)\n");
251 if (!volume_id_get_label(vid, &label) ||
252 !volume_id_get_usage(vid, &usage) ||
253 !volume_id_get_type(vid, &type) ||
254 !volume_id_get_type_version(vid, &type_version) ||
255 !volume_id_get_uuid(vid, &uuid)) {
260 udev_util_replace_whitespace(label, label_safe, sizeof(label_safe));
261 udev_util_replace_chars(label_safe, UDEV_ALLOWED_CHARS_INPUT);
262 volume_id_encode_string(label, label_enc, sizeof(label_enc));
264 udev_util_replace_whitespace(uuid, uuid_safe, sizeof(uuid_safe));
265 udev_util_replace_chars(uuid_safe, UDEV_ALLOWED_CHARS_INPUT);
266 volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
268 volume_id_encode_string(type, type_enc, sizeof(type_enc));
269 volume_id_encode_string(type_version, type_version_enc, sizeof(type_version_enc));
273 printf("ID_FS_USAGE=%s\n", usage);
274 printf("ID_FS_TYPE=%s\n", type_enc);
275 printf("ID_FS_VERSION=%s\n", type_version_enc);
276 printf("ID_FS_UUID=%s\n", uuid_safe);
277 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
278 printf("ID_FS_LABEL=%s\n", label_safe);
279 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
282 printf("%s\n", type);
285 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
289 printf("%s\n", label_safe);
292 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
296 printf("%s\n", uuid_enc);
298 case PRINT_LABEL_RAW:
299 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
303 printf("%s\n", label);
309 volume_id_close(vid);
310 udev_unref(udev_ctx);