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 int main(int argc, char *argv[])
116 static const struct option options[] = {
117 { "label", 0, NULL, 'l' },
118 { "label-raw", 0, NULL, 'L' },
119 { "uuid", 0, NULL, 'u' },
120 { "type", 0, NULL, 't' },
121 { "export", 0, NULL, 'x' },
122 { "skip-raid", 0, NULL, 's' },
123 { "probe-all", 0, NULL, 'a' },
124 { "help", 0, NULL, 'h' },
134 } print = PRINT_EXPORT;
136 struct volume_id *vid = NULL;
137 char label_safe[128];
145 const char *label, *uuid, *type, *type_version, *usage;
149 logging_init("vol_id");
151 /* hook in our debug into libvolume_id */
152 volume_id_log_fn = vid_log;
157 option = getopt_long(argc, argv, "lLutxsah", options, NULL);
166 print = PRINT_LABEL_RAW;
175 print = PRINT_EXPORT;
184 printf("Usage: vol_id [options] <device>\n"
185 " --export export key/value pairs\n"
186 " --type filesystem type\n"
187 " --label filesystem label\n"
188 " --label-raw raw label\n"
189 " --uuid filesystem uuid\n"
190 " --skip-raid don't probe for raid\n"
191 " --probe-all find possibly conflicting signatures\n"
203 fprintf(stderr, "no device\n");
208 fd = open(node, O_RDONLY);
210 fprintf(stderr, "%s: error opening volume\n", node);
215 vid = volume_id_open_fd(fd);
221 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
223 dbg("BLKGETSIZE64=%llu", (unsigned long long)size);
225 /* 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 dbg("dropping privileges to %u:%u",
229 (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid);
230 if (setgroups(0, NULL) != 0 ||
231 setgid(pw->pw_gid) != 0 ||
232 setuid(pw->pw_uid) != 0) {
233 fprintf(stderr, "error dropping privileges: %s\n", strerror(errno));
240 if (volume_id_probe_linux_raid(vid, 0, size) == 0)
241 if (volume_id_get_type(vid, &type))
242 printf("%s\n", type);
243 if (volume_id_probe_ddf_raid(vid, 0, 0) == 0)
244 if (volume_id_get_type(vid, &type))
245 printf("%s\n", type);
246 if (volume_id_probe_intel_software_raid(vid, 0, size) == 0)
247 if (volume_id_get_type(vid, &type))
248 printf("%s\n", type);
249 if (volume_id_probe_lsi_mega_raid(vid, 0, size) == 0)
250 if (volume_id_get_type(vid, &type))
251 printf("%s\n", type);
252 if (volume_id_probe_via_raid(vid, 0, size) == 0)
253 if (volume_id_get_type(vid, &type))
254 printf("%s\n", type);
255 if (volume_id_probe_silicon_medley_raid(vid, 0, size) == 0)
256 if (volume_id_get_type(vid, &type))
257 printf("%s\n", type);
258 if (volume_id_probe_nvidia_raid(vid, 0, size) == 0)
259 if (volume_id_get_type(vid, &type))
260 printf("%s\n", type);
261 if (volume_id_probe_promise_fasttrack_raid(vid, 0, size) == 0)
262 if (volume_id_get_type(vid, &type))
263 printf("%s\n", type);
264 if (volume_id_probe_highpoint_45x_raid(vid, 0, size) == 0)
265 if (volume_id_get_type(vid, &type))
266 printf("%s\n", type);
267 if (volume_id_probe_adaptec_raid(vid, 0, size) == 0)
268 if (volume_id_get_type(vid, &type))
269 printf("%s\n", type);
270 if (volume_id_probe_jmicron_raid(vid, 0, size) == 0)
271 if (volume_id_get_type(vid, &type))
272 printf("%s\n", type);
273 if (volume_id_probe_vfat(vid, 0, 0) == 0)
274 if (volume_id_get_type(vid, &type))
275 printf("%s\n", type);
276 if (volume_id_probe_linux_swap(vid, 0, 0) == 0)
277 if (volume_id_get_type(vid, &type))
278 printf("%s\n", type);
279 if (volume_id_probe_luks(vid, 0, 0) == 0)
280 if (volume_id_get_type(vid, &type))
281 printf("%s\n", type);
282 if (volume_id_probe_xfs(vid, 0, 0) == 0)
283 if (volume_id_get_type(vid, &type))
284 printf("%s\n", type);
285 if (volume_id_probe_ext(vid, 0, 0) == 0)
286 if (volume_id_get_type(vid, &type))
287 printf("%s\n", type);
288 if (volume_id_probe_reiserfs(vid, 0, 0) == 0)
289 if (volume_id_get_type(vid, &type))
290 printf("%s\n", type);
291 if (volume_id_probe_jfs(vid, 0, 0) == 0)
292 if (volume_id_get_type(vid, &type))
293 printf("%s\n", type);
294 if (volume_id_probe_udf(vid, 0, 0) == 0)
295 if (volume_id_get_type(vid, &type))
296 printf("%s\n", type);
297 if (volume_id_probe_iso9660(vid, 0, 0) == 0)
298 if (volume_id_get_type(vid, &type))
299 printf("%s\n", type);
300 if (volume_id_probe_hfs_hfsplus(vid, 0, 0) == 0)
301 if (volume_id_get_type(vid, &type))
302 printf("%s\n", type);
303 if (volume_id_probe_ufs(vid, 0, 0) == 0)
304 if (volume_id_get_type(vid, &type))
305 printf("%s\n", type);
306 if (volume_id_probe_ntfs(vid, 0, 0) == 0)
307 if (volume_id_get_type(vid, &type))
308 printf("%s\n", type);
309 if (volume_id_probe_cramfs(vid, 0, 0) == 0)
310 if (volume_id_get_type(vid, &type))
311 printf("%s\n", type);
312 if (volume_id_probe_romfs(vid, 0, 0) == 0)
313 if (volume_id_get_type(vid, &type))
314 printf("%s\n", type);
315 if (volume_id_probe_hpfs(vid, 0, 0) == 0)
316 if (volume_id_get_type(vid, &type))
317 printf("%s\n", type);
318 if (volume_id_probe_sysv(vid, 0, 0) == 0)
319 if (volume_id_get_type(vid, &type))
320 printf("%s\n", type);
321 if (volume_id_probe_minix(vid, 0, 0) == 0)
322 if (volume_id_get_type(vid, &type))
323 printf("%s\n", type);
324 if (volume_id_probe_ocfs1(vid, 0, 0) == 0)
325 if (volume_id_get_type(vid, &type))
326 printf("%s\n", type);
327 if (volume_id_probe_ocfs2(vid, 0, 0) == 0)
328 if (volume_id_get_type(vid, &type))
329 printf("%s\n", type);
330 if (volume_id_probe_vxfs(vid, 0, 0) == 0)
331 if (volume_id_get_type(vid, &type))
332 printf("%s\n", type);
333 if (volume_id_probe_squashfs(vid, 0, 0) == 0)
334 if (volume_id_get_type(vid, &type))
335 printf("%s\n", type);
336 if (volume_id_probe_netware(vid, 0, 0) == 0)
337 if (volume_id_get_type(vid, &type))
338 printf("%s\n", type);
339 if (volume_id_probe_gfs(vid, 0, 0) == 0)
340 if (volume_id_get_type(vid, &type))
341 printf("%s\n", type);
342 if (volume_id_probe_gfs2(vid, 0, 0) == 0)
343 if (volume_id_get_type(vid, &type))
344 printf("%s\n", type);
349 retval = volume_id_probe_filesystem(vid, 0, size);
351 retval = volume_id_probe_all(vid, 0, size);
353 fprintf(stderr, "%s: unknown volume type\n", node);
358 if (!volume_id_get_label(vid, &label) ||
359 !volume_id_get_usage(vid, &usage) ||
360 !volume_id_get_type(vid, &type) ||
361 !volume_id_get_type_version(vid, &type_version) ||
362 !volume_id_get_uuid(vid, &uuid)) {
367 set_str(label_safe, label, sizeof(label_safe));
368 replace_untrusted_chars(label_safe);
369 set_str(uuid_safe, uuid, sizeof(uuid_safe));
370 replace_untrusted_chars(uuid_safe);
374 printf("ID_FS_USAGE=%s\n", usage);
375 printf("ID_FS_TYPE=%s\n", type);
376 printf("ID_FS_VERSION=%s\n", type_version);
377 printf("ID_FS_UUID=%s\n", uuid);
378 printf("ID_FS_UUID_SAFE=%s\n", uuid_safe);
379 printf("ID_FS_LABEL=%s\n", label);
380 printf("ID_FS_LABEL_SAFE=%s\n", label_safe);
383 printf("%s\n", type);
386 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
390 printf("%s\n", label_safe);
393 if (uuid_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
397 printf("%s\n", uuid_safe);
399 case PRINT_LABEL_RAW:
400 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
404 printf("%s\n", label);
410 volume_id_close(vid);