chiark / gitweb /
volume_id: add DDF support
[elogind.git] / extras / volume_id / vol_id.c
1 /*
2  * vol_id - read filesystem label and uuid
3  *
4  * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
5  *
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.
9  * 
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.
14  * 
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.
18  *
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <getopt.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36
37 #include "../../udev.h"
38 #include "lib/libvolume_id.h"
39
40 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
41
42 #ifdef USE_LOG
43 void log_message(int priority, const char *format, ...)
44 {
45         va_list args;
46         static int udev_log = -1;
47
48         if (udev_log == -1) {
49                 const char *value;
50
51                 value = getenv("UDEV_LOG");
52                 if (value)
53                         udev_log = log_priority(value);
54                 else
55                         udev_log = LOG_ERR;
56         }
57
58         if (priority > udev_log)
59                 return;
60
61         va_start(args, format);
62         vsyslog(priority, format, args);
63         va_end(args);
64 }
65 #endif
66
67 static void vid_log(int priority, const char *file, int line, const char *format, ...)
68 {
69 #ifdef USE_LOG
70         char log_str[1024];
71         va_list args;
72
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);
77         va_end(args);
78 #endif
79         return;
80 }
81
82 static void set_str(char *to, const char *from, size_t count)
83 {
84         size_t i, j, len;
85
86         /* strip trailing whitespace */
87         len = strnlen(from, count);
88         while (len && isspace(from[len-1]))
89                 len--;
90
91         /* strip leading whitespace */
92         i = 0;
93         while (isspace(from[i]) && (i < len))
94                 i++;
95
96         j = 0;
97         while (i < len) {
98                 /* substitute multiple whitespace */
99                 if (isspace(from[i])) {
100                         while (isspace(from[i]))
101                                 i++;
102                         to[j++] = '_';
103                 }
104                 /* skip chars */
105                 if (from[i] == '/') {
106                         i++;
107                         continue;
108                 }
109                 to[j++] = from[i++];
110         }
111         to[j] = '\0';
112 }
113
114 int main(int argc, char *argv[])
115 {
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' },
125                 {}
126         };
127
128         enum print_type {
129                 PRINT_EXPORT,
130                 PRINT_TYPE,
131                 PRINT_LABEL,
132                 PRINT_UUID,
133                 PRINT_LABEL_RAW,
134         } print = PRINT_EXPORT;
135
136         struct volume_id *vid = NULL;
137         char name[128];
138         uint64_t size;
139         int skip_raid = 0;
140         int probe_all = 0;
141         const char *node;
142         struct passwd *pw;
143         int fd;
144         const char *label, *uuid, *type, *type_version, *usage;
145         int retval;
146         int rc = 0;
147
148         logging_init("vol_id");
149
150         /* hook in our debug into libvolume_id */
151         volume_id_log_fn = vid_log;
152
153         while (1) {
154                 int option;
155
156                 option = getopt_long(argc, argv, "lLutxsah", options, NULL);
157                 if (option == -1)
158                         break;
159
160                 switch (option) {
161                 case 'l':
162                         print = PRINT_LABEL;
163                         break;
164                 case 'L':
165                         print = PRINT_LABEL_RAW;
166                         break;
167                 case 'u':
168                         print = PRINT_UUID;
169                         break;
170                 case 't':
171                         print = PRINT_TYPE;
172                         break;
173                 case 'x':
174                         print = PRINT_EXPORT;
175                         break;
176                 case 's':
177                         skip_raid = 1;
178                         break;
179                 case 'a':
180                         probe_all = 1;
181                         break;
182                 case 'h':
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"
191                             " --help\n\n");
192                         goto exit;
193                 default:
194                         retval = 1;
195                         goto exit;
196                 }
197         }
198
199         node = argv[optind];
200         if (!node) {
201                 err("no device");
202                 fprintf(stderr, "no device\n");
203                 rc = 1;
204                 goto exit;
205         }
206
207         fd = open(node, O_RDONLY);
208         if (fd < 0) {
209                 fprintf(stderr, "%s: error opening volume\n", node);
210                 rc = 2;
211                 goto exit;
212         }
213
214         vid = volume_id_open_fd(fd);
215         if (vid == NULL) {
216                 rc = 2;
217                 goto exit;
218         }
219
220         if (ioctl(fd, BLKGETSIZE64, &size) != 0)
221                 size = 0;
222         dbg("BLKGETSIZE64=%llu", (unsigned long long)size);
223
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));
233                         rc = 3;
234                         goto exit;
235                 }
236         }
237
238         if (probe_all) {
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);
341                 goto exit;
342         }
343
344         if (skip_raid)
345                 retval = volume_id_probe_filesystem(vid, 0, size);
346         else
347                 retval = volume_id_probe_all(vid, 0, size);
348         if (retval != 0) {
349                 fprintf(stderr, "%s: unknown volume type\n", node);
350                 rc = 4;
351                 goto exit;
352         }
353
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)) {
359                 rc = 4;
360                 goto exit;
361         }
362         set_str(name, label, sizeof(name));
363         replace_untrusted_chars(name);
364
365         switch (print) {
366         case PRINT_EXPORT:
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);
373                 break;
374         case PRINT_TYPE:
375                 printf("%s\n", type);
376                 break;
377         case PRINT_LABEL:
378                 if (name[0] == '\0' || strcmp(usage, "raid") == 0) {
379                         rc = 3;
380                         goto exit;
381                 }
382                 printf("%s\n", name);
383                 break;
384         case PRINT_UUID:
385                 if (uuid[0] == '\0' || strcmp(usage, "raid") == 0) {
386                         rc = 4;
387                         goto exit;
388                 }
389                 printf("%s\n", uuid);
390                 break;
391         case PRINT_LABEL_RAW:
392                 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
393                         rc = 3;
394                         goto exit;
395                 }
396                 printf("%s\n", label);
397                 break;
398         }
399
400 exit:
401         if (vid != NULL)
402                 volume_id_close(vid);
403
404         logging_close();
405         return rc;
406 }