chiark / gitweb /
volume_id: better DDF raid detection
[elogind.git] / extras / volume_id / vol_id.c
1 /*
2  * vol_id - read filesystem label and uuid
3  *
4  * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <getopt.h>
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35
36 #include "../../udev/udev.h"
37 #include "lib/libvolume_id.h"
38
39 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
40
41 static int debug;
42 struct udev *udev_ctx;
43
44 static void log_fn(struct udev *udev, int priority,
45                    const char *file, int line, const char *fn,
46                    const char *format, va_list args)
47 {
48         if (debug) {
49                 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
50                 vfprintf(stderr, format, args);
51         } else {
52                 vsyslog(priority, format, args);
53         }
54 }
55
56 static void vid_log(int priority, const char *file, int line, const char *format, ...)
57 {
58         va_list args;
59
60         if (priority > udev_get_log_priority(udev_ctx))
61                 return;
62         va_start(args, format);
63         log_fn(udev_ctx, priority, file, line, NULL, format, args);
64         va_end(args);
65         return;
66 }
67
68 static void set_str(char *to, const char *from, size_t count)
69 {
70         size_t i, j, len;
71
72         /* strip trailing whitespace */
73         len = strnlen(from, count);
74         while (len && isspace(from[len-1]))
75                 len--;
76
77         /* strip leading whitespace */
78         i = 0;
79         while (isspace(from[i]) && (i < len))
80                 i++;
81
82         j = 0;
83         while (i < len) {
84                 /* substitute multiple whitespace */
85                 if (isspace(from[i])) {
86                         while (isspace(from[i]))
87                                 i++;
88                         to[j++] = '_';
89                 }
90                 /* skip chars */
91                 if (from[i] == '/') {
92                         i++;
93                         continue;
94                 }
95                 to[j++] = from[i++];
96         }
97         to[j] = '\0';
98 }
99
100 static int all_probers(volume_id_probe_fn_t probe_fn,
101                        struct volume_id *id, uint64_t off, uint64_t size,
102                        void *data)
103 {
104         const char *type;
105
106         if (probe_fn(id, off, size) == 0)
107                 if (volume_id_get_type(id, &type))
108                         printf("%s\n", type);
109
110         return 0;
111 }
112
113 int main(int argc, char *argv[])
114 {
115         static const struct option options[] = {
116                 { "label", no_argument, NULL, 'l' },
117                 { "label-raw", no_argument, NULL, 'L' },
118                 { "uuid", no_argument, NULL, 'u' },
119                 { "type", no_argument, NULL, 't' },
120                 { "export", no_argument, NULL, 'x' },
121                 { "skip-raid", no_argument, NULL, 's' },
122                 { "size", required_argument, NULL, 'S' },
123                 { "probe-all", no_argument, NULL, 'a' },
124                 { "offset", optional_argument, NULL, 'o' },
125                 { "debug", no_argument, NULL, 'd' },
126                 { "help", no_argument, NULL, 'h' },
127                 {}
128         };
129
130         enum print_type {
131                 PRINT_EXPORT,
132                 PRINT_TYPE,
133                 PRINT_LABEL,
134                 PRINT_UUID,
135                 PRINT_LABEL_RAW,
136         } print = PRINT_EXPORT;
137
138         struct volume_id *vid = NULL;
139         char label_safe[256];
140         char label_enc[256];
141         char uuid_safe[256];
142         char uuid_enc[256];
143         char type_enc[256];
144         char type_version_enc[256];
145         uint64_t size = 0;
146         int skip_raid = 0;
147         int probe_all = 0;
148         uint64_t offset = 0;
149         const char *node;
150         int fd;
151         const char *label, *uuid, *type, *type_version, *usage;
152         int retval;
153         int rc = 0;
154
155         udev_ctx = udev_new();
156         if (udev_ctx == NULL)
157                 goto exit;
158         logging_init("vol_id");
159         udev_set_log_fn(udev_ctx, log_fn);
160
161         /* hook in our debug into libvolume_id */
162         volume_id_log_fn = vid_log;
163
164         while (1) {
165                 int option;
166
167                 option = getopt_long(argc, argv, "lLutxsS:aodh", options, NULL);
168                 if (option == -1)
169                         break;
170
171                 switch (option) {
172                 case 'd':
173                         debug = 1;
174                         if (udev_get_log_priority(udev_ctx) < LOG_INFO)
175                                 udev_set_log_priority(udev_ctx, LOG_INFO);
176                         break;
177                 case 'l':
178                         print = PRINT_LABEL;
179                         break;
180                 case 'L':
181                         print = PRINT_LABEL_RAW;
182                         break;
183                 case 'u':
184                         print = PRINT_UUID;
185                         break;
186                 case 't':
187                         print = PRINT_TYPE;
188                         break;
189                 case 'x':
190                         print = PRINT_EXPORT;
191                         break;
192                 case 's':
193                         skip_raid = 1;
194                         break;
195                 case 'a':
196                         probe_all = 1;
197                         break;
198                 case 'S':
199                         if (optarg[0] != '\0')
200                                 size = strtoull(optarg, NULL, 0);
201                         break;
202                 case 'o':
203                         if (optarg[0] != '\0')
204                                 offset = strtoull(optarg, NULL, 0);
205                         break;
206                 case 'h':
207                         printf("Usage: vol_id [options] <device>\n"
208                             " --export         export key/value pairs\n"
209                             " --type           filesystem type\n"
210                             " --label          filesystem label\n"
211                             " --label-raw      raw label\n"
212                             " --uuid           filesystem uuid\n"
213                             " --skip-raid      don't probe for raid\n"
214                             " --probe-all      find possibly conflicting signatures\n"
215                             " --offset=<bytes> probe at the given offset\n"
216                             " --size=<bytes>   overwrite device size\n"
217                             " --debug          print debug output to stderr\n"
218                             " --help\n\n");
219                         goto exit;
220                 default:
221                         retval = 1;
222                         goto exit;
223                 }
224         }
225
226         node = argv[optind];
227         if (!node) {
228                 err(udev_ctx, "no device\n");
229                 fprintf(stderr, "no device\n");
230                 rc = 1;
231                 goto exit;
232         }
233
234         fd = open(node, O_RDONLY);
235         if (fd < 0) {
236                 fprintf(stderr, "%s: error opening volume\n", node);
237                 rc = 2;
238                 goto exit;
239         }
240
241         vid = volume_id_open_fd(fd);
242         if (vid == NULL) {
243                 rc = 2;
244                 goto exit;
245         }
246
247         if (size == 0) {
248                 if (ioctl(fd, BLKGETSIZE64, &size) != 0)
249                         size = 0;
250                 info(udev_ctx, "BLKGETSIZE64=%llu (%lluGB)\n", (unsigned long long)size, (unsigned long long)size >> 30);
251         }
252
253         /* try to drop all privileges before reading disk content */
254         if (getuid() == 0) {
255                 struct passwd *pw;
256
257                 pw = getpwnam("nobody");
258                 if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
259                         if (setgroups(0, NULL) != 0 ||
260                             setgid(pw->pw_gid) != 0 ||
261                             setuid(pw->pw_uid) != 0)
262                                 info(udev_ctx, "unable to drop privileges: %s\n\n", strerror(errno));
263                 }
264         }
265
266         if (probe_all) {
267                 volume_id_all_probers(all_probers, vid, offset, size, NULL);
268                 goto exit;
269         }
270
271         if (skip_raid)
272                 retval = volume_id_probe_filesystem(vid, offset, size);
273         else
274                 retval = volume_id_probe_all(vid, offset, size);
275         if (retval != 0) {
276                 fprintf(stderr, "%s: unknown volume type\n", node);
277                 rc = 4;
278                 goto exit;
279         }
280
281         if (!volume_id_get_label(vid, &label) ||
282             !volume_id_get_usage(vid, &usage) ||
283             !volume_id_get_type(vid, &type) ||
284             !volume_id_get_type_version(vid, &type_version) ||
285             !volume_id_get_uuid(vid, &uuid)) {
286                 rc = 4;
287                 goto exit;
288         }
289
290         set_str(label_safe, label, sizeof(label_safe));
291         util_replace_chars(label_safe, ALLOWED_CHARS_INPUT);
292         volume_id_encode_string(label, label_enc, sizeof(label_enc));
293
294         set_str(uuid_safe, uuid, sizeof(uuid_safe));
295         util_replace_chars(uuid_safe, ALLOWED_CHARS_INPUT);
296         volume_id_encode_string(uuid, uuid_enc, sizeof(uuid_enc));
297
298         volume_id_encode_string(type, type_enc, sizeof(type_enc));
299         volume_id_encode_string(type_version, type_version_enc, sizeof(type_version_enc));
300
301         switch (print) {
302         case PRINT_EXPORT:
303                 printf("ID_FS_USAGE=%s\n", usage);
304                 printf("ID_FS_TYPE=%s\n", type_enc);
305                 printf("ID_FS_VERSION=%s\n", type_version_enc);
306                 printf("ID_FS_UUID=%s\n", uuid_safe);
307                 printf("ID_FS_UUID_ENC=%s\n", uuid_enc);
308                 printf("ID_FS_LABEL=%s\n", label_safe);
309                 printf("ID_FS_LABEL_ENC=%s\n", label_enc);
310                 break;
311         case PRINT_TYPE:
312                 printf("%s\n", type);
313                 break;
314         case PRINT_LABEL:
315                 if (label_safe[0] == '\0' || strcmp(usage, "raid") == 0) {
316                         rc = 3;
317                         goto exit;
318                 }
319                 printf("%s\n", label_safe);
320                 break;
321         case PRINT_UUID:
322                 if (uuid_enc[0] == '\0' || strcmp(usage, "raid") == 0) {
323                         rc = 4;
324                         goto exit;
325                 }
326                 printf("%s\n", uuid_enc);
327                 break;
328         case PRINT_LABEL_RAW:
329                 if (label[0] == '\0' || strcmp(usage, "raid") == 0) {
330                         rc = 3;
331                         goto exit;
332                 }
333                 printf("%s\n", label);
334                 break;
335         }
336
337 exit:
338         if (vid != NULL)
339                 volume_id_close(vid);
340         udev_unref(udev_ctx);
341         logging_close();
342         return rc;
343 }