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;
144 const char *label, *uuid, *type, *type_version, *usage;
148 logging_init("vol_id");
150 /* hook in our debug into libvolume_id */
151 volume_id_log_fn = vid_log;
156 option = getopt_long(argc, argv, "lLutxsah", options, NULL);
165 print = PRINT_LABEL_RAW;
174 print = PRINT_EXPORT;
183 printf("Usage: vol_id [options] <device>\n"
184 " --export export key/value pairs\n"
185 " --type filesystem type\n"
186 " --label filesystem label\n"
187 " --label-raw raw label\n"
188 " --uuid filesystem uuid\n"
189 " --skip-raid don't probe for raid\n"
190 " --probe-all find possibly conflicting signatures\n"
202 fprintf(stderr, "no device\n");
207 fd = open(node, O_RDONLY);
209 fprintf(stderr, "%s: error opening volume\n", node);
214 vid = volume_id_open_fd(fd);
220 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
222 dbg("BLKGETSIZE64=%llu", (unsigned long long)size);
224 /* try to drop all privileges before reading disk content */
225 pw = getpwnam ("nobody");
226 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
227 dbg("dropping privileges to %u:%u",
228 (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid);
229 if (setgroups(0, NULL) != 0 ||
230 setgid(pw->pw_gid) != 0 ||
231 setuid(pw->pw_uid) != 0) {
232 fprintf(stderr, "error dropping privileges: %s\n", strerror(errno));
239 if (volume_id_probe_linux_raid(vid, 0, size) == 0)
240 if (volume_id_get_type(vid, &type))
241 printf("%s\n", type);
242 if (volume_id_probe_intel_software_raid(vid, 0, size) == 0)
243 if (volume_id_get_type(vid, &type))
244 printf("%s\n", type);
245 if (volume_id_probe_lsi_mega_raid(vid, 0, size) == 0)
246 if (volume_id_get_type(vid, &type))
247 printf("%s\n", type);
248 if (volume_id_probe_via_raid(vid, 0, size) == 0)
249 if (volume_id_get_type(vid, &type))
250 printf("%s\n", type);
251 if (volume_id_probe_silicon_medley_raid(vid, 0, size) == 0)
252 if (volume_id_get_type(vid, &type))
253 printf("%s\n", type);
254 if (volume_id_probe_nvidia_raid(vid, 0, size) == 0)
255 if (volume_id_get_type(vid, &type))
256 printf("%s\n", type);
257 if (volume_id_probe_promise_fasttrack_raid(vid, 0, size) == 0)
258 if (volume_id_get_type(vid, &type))
259 printf("%s\n", type);
260 if (volume_id_probe_highpoint_45x_raid(vid, 0, size) == 0)
261 if (volume_id_get_type(vid, &type))
262 printf("%s\n", type);
263 if (volume_id_probe_adaptec_raid(vid, 0, size) == 0)
264 if (volume_id_get_type(vid, &type))
265 printf("%s\n", type);
266 if (volume_id_probe_jmicron_raid(vid, 0, size) == 0)
267 if (volume_id_get_type(vid, &type))
268 printf("%s\n", type);
269 if (volume_id_probe_vfat(vid, 0, 0) == 0)
270 if (volume_id_get_type(vid, &type))
271 printf("%s\n", type);
272 if (volume_id_probe_linux_swap(vid, 0, 0) == 0)
273 if (volume_id_get_type(vid, &type))
274 printf("%s\n", type);
275 if (volume_id_probe_luks(vid, 0, 0) == 0)
276 if (volume_id_get_type(vid, &type))
277 printf("%s\n", type);
278 if (volume_id_probe_xfs(vid, 0, 0) == 0)
279 if (volume_id_get_type(vid, &type))
280 printf("%s\n", type);
281 if (volume_id_probe_ext(vid, 0, 0) == 0)
282 if (volume_id_get_type(vid, &type))
283 printf("%s\n", type);
284 if (volume_id_probe_reiserfs(vid, 0, 0) == 0)
285 if (volume_id_get_type(vid, &type))
286 printf("%s\n", type);
287 if (volume_id_probe_jfs(vid, 0, 0) == 0)
288 if (volume_id_get_type(vid, &type))
289 printf("%s\n", type);
290 if (volume_id_probe_udf(vid, 0, 0) == 0)
291 if (volume_id_get_type(vid, &type))
292 printf("%s\n", type);
293 if (volume_id_probe_iso9660(vid, 0, 0) == 0)
294 if (volume_id_get_type(vid, &type))
295 printf("%s\n", type);
296 if (volume_id_probe_hfs_hfsplus(vid, 0, 0) == 0)
297 if (volume_id_get_type(vid, &type))
298 printf("%s\n", type);
299 if (volume_id_probe_ufs(vid, 0, 0) == 0)
300 if (volume_id_get_type(vid, &type))
301 printf("%s\n", type);
302 if (volume_id_probe_ntfs(vid, 0, 0) == 0)
303 if (volume_id_get_type(vid, &type))
304 printf("%s\n", type);
305 if (volume_id_probe_cramfs(vid, 0, 0) == 0)
306 if (volume_id_get_type(vid, &type))
307 printf("%s\n", type);
308 if (volume_id_probe_romfs(vid, 0, 0) == 0)
309 if (volume_id_get_type(vid, &type))
310 printf("%s\n", type);
311 if (volume_id_probe_hpfs(vid, 0, 0) == 0)
312 if (volume_id_get_type(vid, &type))
313 printf("%s\n", type);
314 if (volume_id_probe_sysv(vid, 0, 0) == 0)
315 if (volume_id_get_type(vid, &type))
316 printf("%s\n", type);
317 if (volume_id_probe_minix(vid, 0, 0) == 0)
318 if (volume_id_get_type(vid, &type))
319 printf("%s\n", type);
320 if (volume_id_probe_ocfs1(vid, 0, 0) == 0)
321 if (volume_id_get_type(vid, &type))
322 printf("%s\n", type);
323 if (volume_id_probe_ocfs2(vid, 0, 0) == 0)
324 if (volume_id_get_type(vid, &type))
325 printf("%s\n", type);
326 if (volume_id_probe_vxfs(vid, 0, 0) == 0)
327 if (volume_id_get_type(vid, &type))
328 printf("%s\n", type);
329 if (volume_id_probe_squashfs(vid, 0, 0) == 0)
330 if (volume_id_get_type(vid, &type))
331 printf("%s\n", type);
332 if (volume_id_probe_netware(vid, 0, 0) == 0)
333 if (volume_id_get_type(vid, &type))
334 printf("%s\n", type);
335 if (volume_id_probe_gfs(vid, 0, 0) == 0)
336 if (volume_id_get_type(vid, &type))
337 printf("%s\n", type);
338 if (volume_id_probe_gfs2(vid, 0, 0) == 0)
339 if (volume_id_get_type(vid, &type))
340 printf("%s\n", type);
345 retval = volume_id_probe_filesystem(vid, 0, size);
347 retval = volume_id_probe_all(vid, 0, size);
349 fprintf(stderr, "%s: unknown volume type\n", node);
354 if (!volume_id_get_label(vid, &label) ||
355 !volume_id_get_usage(vid, &usage) ||
356 !volume_id_get_type(vid, &type) ||
357 !volume_id_get_type_version(vid, &type_version) ||
358 !volume_id_get_uuid(vid, &uuid)) {
362 set_str(name, label, sizeof(name));
363 replace_untrusted_chars(name);
367 printf("ID_FS_USAGE=%s\n", usage);
368 printf("ID_FS_TYPE=%s\n", type);
369 printf("ID_FS_VERSION=%s\n", type_version);
370 printf("ID_FS_UUID=%s\n", uuid);
371 printf("ID_FS_LABEL=%s\n", label);
372 printf("ID_FS_LABEL_SAFE=%s\n", name);
375 printf("%s\n", type);
378 if (name[0] == '\0' || strcmp(usage, "raid") == 0) {
382 printf("%s\n", name);
385 if (uuid[0] == '\0' || strcmp(usage, "raid") == 0) {
389 printf("%s\n", uuid);
391 case PRINT_LABEL_RAW:
392 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
396 printf("%s\n", label);
402 volume_id_close(vid);