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[256];
159 const char *label, *uuid, *type, *type_version, *usage;
163 logging_init("vol_id");
165 /* hook in our debug into libvolume_id */
166 volume_id_log_fn = vid_log;
171 option = getopt_long(argc, argv, "lLutxsah", options, NULL);
180 print = PRINT_LABEL_RAW;
189 print = PRINT_EXPORT;
198 printf("Usage: vol_id [options] <device>\n"
199 " --export export key/value pairs\n"
200 " --type filesystem type\n"
201 " --label filesystem label\n"
202 " --label-raw raw label\n"
203 " --uuid filesystem uuid\n"
204 " --skip-raid don't probe for raid\n"
205 " --probe-all find possibly conflicting signatures\n"
217 fprintf(stderr, "no device\n");
222 fd = open(node, O_RDONLY);
224 fprintf(stderr, "%s: error opening volume\n", node);
229 vid = volume_id_open_fd(fd);
235 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
237 dbg("BLKGETSIZE64=%llu", (unsigned long long)size);
239 /* try to drop all privileges before reading disk content */
240 pw = getpwnam ("nobody");
241 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
242 dbg("dropping privileges to %u:%u",
243 (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid);
244 if (setgroups(0, NULL) != 0 ||
245 setgid(pw->pw_gid) != 0 ||
246 setuid(pw->pw_uid) != 0) {
247 fprintf(stderr, "error dropping privileges: %s\n", strerror(errno));
254 volume_id_all_probers(all_probers, vid, 0, size, NULL);
259 retval = volume_id_probe_filesystem(vid, 0, size);
261 retval = volume_id_probe_all(vid, 0, size);
263 fprintf(stderr, "%s: unknown volume type\n", node);
268 if (!volume_id_get_label(vid, &label) ||
269 !volume_id_get_usage(vid, &usage) ||
270 !volume_id_get_type(vid, &type) ||
271 !volume_id_get_type_version(vid, &type_version) ||
272 !volume_id_get_uuid(vid, &uuid)) {
277 set_str(label_safe, label, sizeof(label_safe));
278 replace_chars(label_safe, ALLOWED_CHARS_INPUT);
280 volume_id_encode_string(label, label_enc, sizeof(label_enc));
281 volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
285 printf("ID_FS_USAGE=%s\n", usage);
286 printf("ID_FS_TYPE=%s\n", type);
287 printf("ID_FS_VERSION=%s\n", type_version);
288 printf("ID_FS_UUID=%s\n", uuid);
289 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
290 printf("ID_FS_LABEL=%s\n", label);
291 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
292 printf("ID_FS_LABEL_SAFE=%s\n", label_safe);
295 printf("%s\n", type);
298 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
302 printf("%s\n", label_safe);
305 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
309 printf("%s\n", uuid_enc);
311 case PRINT_LABEL_RAW:
312 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
316 printf("%s\n", label);
322 volume_id_close(vid);