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/udev.h"
38 #include "lib/libvolume_id.h"
40 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
42 static void log_fn(struct udev *udev, int priority,
43 const char *file, int line, const char *fn,
44 const char *format, va_list args)
46 vsyslog(priority, format, args);
49 static void vid_log(int priority, const char *file, int line, const char *format, ...)
53 va_start(args, format);
54 log_fn(NULL, priority, file, line, NULL, format, args);
59 static void set_str(char *to, const char *from, size_t count)
63 /* strip trailing whitespace */
64 len = strnlen(from, count);
65 while (len && isspace(from[len-1]))
68 /* strip leading whitespace */
70 while (isspace(from[i]) && (i < len))
75 /* substitute multiple whitespace */
76 if (isspace(from[i])) {
77 while (isspace(from[i]))
91 static int all_probers(volume_id_probe_fn_t probe_fn,
92 struct volume_id *id, uint64_t off, uint64_t size,
97 if (probe_fn(id, off, size) == 0)
98 if (volume_id_get_type(id, &type))
104 int main(int argc, char *argv[])
107 static const struct option options[] = {
108 { "label", 0, NULL, 'l' },
109 { "label-raw", 0, NULL, 'L' },
110 { "uuid", 0, NULL, 'u' },
111 { "type", 0, NULL, 't' },
112 { "export", 0, NULL, 'x' },
113 { "skip-raid", 0, NULL, 's' },
114 { "probe-all", 0, NULL, 'a' },
115 { "offset", 2, NULL, 'o' },
116 { "help", 0, NULL, 'h' },
126 } print = PRINT_EXPORT;
128 struct volume_id *vid = NULL;
129 char label_safe[256];
138 const char *label, *uuid, *type, *type_version, *usage;
145 logging_init("vol_id");
146 udev_set_log_fn(udev, log_fn);
148 /* hook in our debug into libvolume_id */
149 volume_id_log_fn = vid_log;
154 option = getopt_long(argc, argv, "lLutxsaoh", options, NULL);
163 print = PRINT_LABEL_RAW;
172 print = PRINT_EXPORT;
181 if (optarg[0] != '\0')
182 offset = strtoull(optarg, NULL, 0);
185 printf("Usage: vol_id [options] <device>\n"
186 " --export export key/value pairs\n"
187 " --type filesystem type\n"
188 " --label filesystem label\n"
189 " --label-raw raw label\n"
190 " --uuid filesystem uuid\n"
191 " --skip-raid don't probe for raid\n"
192 " --probe-all find possibly conflicting signatures\n"
193 " --offset skip given number of bytes of input\n"
204 err(udev, "no device\n");
205 fprintf(stderr, "no device\n");
210 fd = open(node, O_RDONLY);
212 fprintf(stderr, "%s: error opening volume\n", node);
217 vid = volume_id_open_fd(fd);
223 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
225 dbg(udev, "BLKGETSIZE64=%llu\n", (unsigned long long)size);
227 /* try to drop all privileges before reading disk content */
231 pw = getpwnam("nobody");
232 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
233 if (setgroups(0, NULL) != 0 ||
234 setgid(pw->pw_gid) != 0 ||
235 setuid(pw->pw_uid) != 0)
236 info(udev, "unable to drop privileges: %s\n\n", strerror(errno));
241 volume_id_all_probers(all_probers, vid, offset, size, NULL);
246 retval = volume_id_probe_filesystem(vid, offset, size);
248 retval = volume_id_probe_all(vid, offset, size);
250 fprintf(stderr, "%s: unknown volume type\n", node);
255 if (!volume_id_get_label(vid, &label) ||
256 !volume_id_get_usage(vid, &usage) ||
257 !volume_id_get_type(vid, &type) ||
258 !volume_id_get_type_version(vid, &type_version) ||
259 !volume_id_get_uuid(vid, &uuid)) {
264 set_str(label_safe, label, sizeof(label_safe));
265 replace_chars(label_safe, ALLOWED_CHARS_INPUT);
267 volume_id_encode_string(label, label_enc, sizeof(label_enc));
268 volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
272 printf("ID_FS_USAGE=%s\n", usage);
273 printf("ID_FS_TYPE=%s\n", type);
274 printf("ID_FS_VERSION=%s\n", type_version);
275 printf("ID_FS_UUID=%s\n", uuid);
276 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
277 printf("ID_FS_LABEL=%s\n", label);
278 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
279 printf("ID_FS_LABEL_SAFE=%s\n", label_safe);
282 printf("%s\n", type);
285 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
289 printf("%s\n", label_safe);
292 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
296 printf("%s\n", uuid_enc);
298 case PRINT_LABEL_RAW:
299 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
303 printf("%s\n", label);
309 volume_id_close(vid);