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/>.
34 #include <sys/ioctl.h>
36 #include "../../udev/udev.h"
37 #include "lib/libvolume_id.h"
39 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
42 struct udev *udev_ctx;
44 static void log_fn(struct udev *udev, int priority,
45 const char *file, int line, const char *fn,
46 const char *format, va_list args)
49 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
50 vfprintf(stderr, format, args);
52 vsyslog(priority, format, args);
56 static void vid_log(int priority, const char *file, int line, const char *format, ...)
60 va_start(args, format);
61 udev_log(udev_ctx, priority, file, line, NULL, format, args);
66 static void set_str(char *to, const char *from, size_t count)
70 /* strip trailing whitespace */
71 len = strnlen(from, count);
72 while (len && isspace(from[len-1]))
75 /* strip leading whitespace */
77 while (isspace(from[i]) && (i < len))
82 /* substitute multiple whitespace */
83 if (isspace(from[i])) {
84 while (isspace(from[i]))
98 static int all_probers(volume_id_probe_fn_t probe_fn,
99 struct volume_id *id, uint64_t off, uint64_t size,
104 if (probe_fn(id, off, size) == 0)
105 if (volume_id_get_type(id, &type))
106 printf("%s\n", type);
111 int main(int argc, char *argv[])
113 static const struct option options[] = {
114 { "label", no_argument, NULL, 'l' },
115 { "label-raw", no_argument, NULL, 'L' },
116 { "uuid", no_argument, NULL, 'u' },
117 { "type", no_argument, NULL, 't' },
118 { "export", no_argument, NULL, 'x' },
119 { "skip-raid", no_argument, NULL, 's' },
120 { "size", required_argument, NULL, 'S' },
121 { "probe-all", no_argument, NULL, 'a' },
122 { "offset", optional_argument, NULL, 'o' },
123 { "debug", no_argument, NULL, 'd' },
124 { "help", no_argument, NULL, 'h' },
134 } print = PRINT_EXPORT;
136 struct volume_id *vid = NULL;
137 char label_safe[256];
146 const char *label, *uuid, *type, *type_version, *usage;
150 udev_ctx = udev_new();
151 if (udev_ctx == NULL)
153 logging_init("vol_id");
154 udev_set_log_fn(udev_ctx, log_fn);
156 /* hook in our debug into libvolume_id */
157 volume_id_log_fn = vid_log;
162 option = getopt_long(argc, argv, "lLutxsS:aodh", options, NULL);
169 if (udev_get_log_priority(udev_ctx) < LOG_INFO)
170 udev_set_log_priority(udev_ctx, LOG_INFO);
176 print = PRINT_LABEL_RAW;
185 print = PRINT_EXPORT;
194 if (optarg[0] != '\0')
195 size = strtoull(optarg, NULL, 0);
198 if (optarg[0] != '\0')
199 offset = strtoull(optarg, NULL, 0);
202 printf("Usage: vol_id [options] <device>\n"
203 " --export export key/value pairs\n"
204 " --type filesystem type\n"
205 " --label filesystem label\n"
206 " --label-raw raw label\n"
207 " --uuid filesystem uuid\n"
208 " --skip-raid don't probe for raid\n"
209 " --probe-all find possibly conflicting signatures\n"
210 " --offset=<bytes> probe at the given offset\n"
211 " --size=<bytes> overwrite device size\n"
212 " --debug print debug output to stderr\n"
223 err(udev_ctx, "no device\n");
224 fprintf(stderr, "no device\n");
229 fd = open(node, O_RDONLY);
231 fprintf(stderr, "%s: error opening volume\n", node);
236 vid = volume_id_open_fd(fd);
243 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
245 info(udev_ctx, "BLKGETSIZE64=%llu (%lluGB)\n", (unsigned long long)size, (unsigned long long)size >> 30);
248 /* try to drop all privileges before reading disk content */
252 pw = getpwnam("nobody");
253 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
254 if (setgroups(0, NULL) != 0 ||
255 setgid(pw->pw_gid) != 0 ||
256 setuid(pw->pw_uid) != 0)
257 info(udev_ctx, "unable to drop privileges: %s\n\n", strerror(errno));
262 volume_id_all_probers(all_probers, vid, offset, size, NULL);
267 retval = volume_id_probe_filesystem(vid, offset, size);
269 retval = volume_id_probe_all(vid, offset, size);
271 fprintf(stderr, "%s: unknown volume type\n", node);
276 if (!volume_id_get_label(vid, &label) ||
277 !volume_id_get_usage(vid, &usage) ||
278 !volume_id_get_type(vid, &type) ||
279 !volume_id_get_type_version(vid, &type_version) ||
280 !volume_id_get_uuid(vid, &uuid)) {
285 set_str(label_safe, label, sizeof(label_safe));
286 util_replace_chars(label_safe, ALLOWED_CHARS_INPUT);
288 volume_id_encode_string(label, label_enc, sizeof(label_enc));
289 volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
293 printf("ID_FS_USAGE=%s\n", usage);
294 printf("ID_FS_TYPE=%s\n", type);
295 printf("ID_FS_VERSION=%s\n", type_version);
296 printf("ID_FS_UUID=%s\n", uuid);
297 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
298 printf("ID_FS_LABEL=%s\n", label);
299 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
300 printf("ID_FS_LABEL_SAFE=%s\n", label_safe);
303 printf("%s\n", type);
306 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
310 printf("%s\n", label_safe);
313 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
317 printf("%s\n", uuid_enc);
319 case PRINT_LABEL_RAW:
320 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
324 printf("%s\n", label);
330 volume_id_close(vid);
331 udev_unref(udev_ctx);