2 * vol_id - read filesystem label and uuid
4 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <sys/ioctl.h>
37 #include "../../udev.h"
38 #include "lib/libvolume_id.h"
40 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
43 void log_message(int priority, const char *format, ...)
46 static int udev_log = -1;
51 value = getenv("UDEV_LOG");
53 udev_log = log_priority(value);
58 if (priority > udev_log)
61 va_start(args, format);
62 vsyslog(priority, format, args);
67 static void vid_log(int priority, const char *file, int line, const char *format, ...)
73 va_start(args, format);
74 vsnprintf(log_str, sizeof(log_str), format, args);
75 log_str[sizeof(log_str)-1] = '\0';
76 log_message(priority, "%s:%i %s", file, line, log_str);
82 static void set_str(char *to, const char *from, size_t count)
86 /* strip trailing whitespace */
87 len = strnlen(from, count);
88 while (len && isspace(from[len-1]))
91 /* strip leading whitespace */
93 while (isspace(from[i]) && (i < len))
98 /* substitute multiple whitespace */
99 if (isspace(from[i])) {
100 while (isspace(from[i]))
105 if (from[i] == '/') {
114 static int all_probers(volume_id_probe_fn_t probe_fn,
115 struct volume_id *id, uint64_t off, uint64_t size,
120 if (probe_fn(id, off, size) == 0)
121 if (volume_id_get_type(id, &type))
122 printf("%s\n", type);
127 int main(int argc, char *argv[])
129 static const struct option options[] = {
130 { "label", 0, NULL, 'l' },
131 { "label-raw", 0, NULL, 'L' },
132 { "uuid", 0, NULL, 'u' },
133 { "type", 0, NULL, 't' },
134 { "export", 0, NULL, 'x' },
135 { "skip-raid", 0, NULL, 's' },
136 { "probe-all", 0, NULL, 'a' },
137 { "help", 0, NULL, 'h' },
147 } print = PRINT_EXPORT;
149 struct volume_id *vid = NULL;
150 char label_safe[128];
158 const char *label, *uuid, *type, *type_version, *usage;
162 logging_init("vol_id");
164 /* hook in our debug into libvolume_id */
165 volume_id_log_fn = vid_log;
170 option = getopt_long(argc, argv, "lLutxsah", options, NULL);
179 print = PRINT_LABEL_RAW;
188 print = PRINT_EXPORT;
197 printf("Usage: vol_id [options] <device>\n"
198 " --export export key/value pairs\n"
199 " --type filesystem type\n"
200 " --label filesystem label\n"
201 " --label-raw raw label\n"
202 " --uuid filesystem uuid\n"
203 " --skip-raid don't probe for raid\n"
204 " --probe-all find possibly conflicting signatures\n"
216 fprintf(stderr, "no device\n");
221 fd = open(node, O_RDONLY);
223 fprintf(stderr, "%s: error opening volume\n", node);
228 vid = volume_id_open_fd(fd);
234 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
236 dbg("BLKGETSIZE64=%llu", (unsigned long long)size);
238 /* try to drop all privileges before reading disk content */
239 pw = getpwnam ("nobody");
240 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
241 dbg("dropping privileges to %u:%u",
242 (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid);
243 if (setgroups(0, NULL) != 0 ||
244 setgid(pw->pw_gid) != 0 ||
245 setuid(pw->pw_uid) != 0) {
246 fprintf(stderr, "error dropping privileges: %s\n", strerror(errno));
253 volume_id_all_probers(all_probers, vid, 0, size, NULL);
258 retval = volume_id_probe_filesystem(vid, 0, size);
260 retval = volume_id_probe_all(vid, 0, size);
262 fprintf(stderr, "%s: unknown volume type\n", node);
267 if (!volume_id_get_label(vid, &label) ||
268 !volume_id_get_usage(vid, &usage) ||
269 !volume_id_get_type(vid, &type) ||
270 !volume_id_get_type_version(vid, &type_version) ||
271 !volume_id_get_uuid(vid, &uuid)) {
276 set_str(label_safe, label, sizeof(label_safe));
277 replace_untrusted_chars(label_safe);
278 set_str(uuid_safe, uuid, sizeof(uuid_safe));
279 replace_untrusted_chars(uuid_safe);
283 printf("ID_FS_USAGE=%s\n", usage);
284 printf("ID_FS_TYPE=%s\n", type);
285 printf("ID_FS_VERSION=%s\n", type_version);
286 printf("ID_FS_UUID=%s\n", uuid);
287 printf("ID_FS_UUID_SAFE=%s\n", uuid_safe);
288 printf("ID_FS_LABEL=%s\n", label);
289 printf("ID_FS_LABEL_SAFE=%s\n", label_safe);
292 printf("%s\n", type);
295 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
299 printf("%s\n", label_safe);
302 if (uuid_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
306 printf("%s\n", uuid_safe);
308 case PRINT_LABEL_RAW:
309 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
313 printf("%s\n", label);
319 volume_id_close(vid);