chiark / gitweb /
volume_id: old md metadata has only 32 bit for the uuid
[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 <sys/ioctl.h>
34
35 #include "../../udev.h"
36 #include "lib/libvolume_id.h"
37
38 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
39
40 #ifdef USE_LOG
41 void log_message(int priority, const char *format, ...)
42 {
43         va_list args;
44         static int udev_log = -1;
45
46         if (udev_log == -1) {
47                 const char *value;
48
49                 value = getenv("UDEV_LOG");
50                 if (value)
51                         udev_log = log_priority(value);
52                 else
53                         udev_log = LOG_ERR;
54         }
55
56         if (priority > udev_log)
57                 return;
58
59         va_start(args, format);
60         vsyslog(priority, format, args);
61         va_end(args);
62 }
63 #endif
64
65 static void vid_log(int priority, const char *file, int line, const char *format, ...)
66 {
67 #ifdef USE_LOG
68         char log_str[1024];
69         va_list args;
70
71         va_start(args, format);
72         vsnprintf(log_str, sizeof(log_str), format, args);
73         log_str[sizeof(log_str)-1] = '\0';
74         log_message(priority, "%s:%i %s", file, line, log_str);
75         va_end(args);
76 #endif
77         return;
78 }
79
80 static void set_str(char *to, const char *from, size_t count)
81 {
82         size_t i, j, len;
83
84         /* strip trailing whitespace */
85         len = strnlen(from, count);
86         while (len && isspace(from[len-1]))
87                 len--;
88
89         /* strip leading whitespace */
90         i = 0;
91         while (isspace(from[i]) && (i < len))
92                 i++;
93
94         j = 0;
95         while (i < len) {
96                 /* substitute multiple whitespace */
97                 if (isspace(from[i])) {
98                         while (isspace(from[i]))
99                                 i++;
100                         to[j++] = '_';
101                 }
102                 /* skip chars */
103                 if (from[i] == '/') {
104                         i++;
105                         continue;
106                 }
107                 to[j++] = from[i++];
108         }
109         to[j] = '\0';
110 }
111
112 int main(int argc, char *argv[])
113 {
114         const char help[] = "Usage: vol_id [options] <device>\n"
115                             " --export        export key/value pairs\n"
116                             "  -t             filesystem type\n"
117                             "  -l             filesystem label\n"
118                             "  -u             filesystem uuid\n"
119                             "  -L             raw label\n"
120                             " --skip-raid     don't probe for raid\n"
121                             " --probe-all     find possibly conflicting signatures\n"
122                             " --help\n"
123                             "\n";
124         enum print_type {
125                 PRINT_EXPORT,
126                 PRINT_TYPE,
127                 PRINT_LABEL,
128                 PRINT_UUID,
129                 PRINT_RAW_LABEL,
130         } print = PRINT_EXPORT;
131         struct volume_id *vid = NULL;
132         static char name[VOLUME_ID_LABEL_SIZE];
133         int i;
134         uint64_t size;
135         int skip_raid = 0;
136         int probe_all = 0;
137         const char *node = NULL;
138         struct passwd *pw;
139         int retval;
140         int rc = 0;
141
142         logging_init("vol_id");
143
144         /* hook in our debug into libvolume_id */
145         volume_id_log_fn = vid_log;
146
147         for (i = 1 ; i < argc; i++) {
148                 char *arg = argv[i];
149
150                 if (strcmp(arg, "--export") == 0) {
151                         print = PRINT_EXPORT;
152                 } else if (strcmp(arg, "-t") == 0) {
153                         print = PRINT_TYPE;
154                 } else if (strcmp(arg, "-l") == 0) {
155                         print = PRINT_LABEL;
156                 } else if (strcmp(arg, "-u") == 0) {
157                         print = PRINT_UUID;
158                 } else if (strcmp(arg, "-L") == 0) {
159                         print = PRINT_RAW_LABEL;
160                 } else if (strcmp(arg, "--skip-raid") == 0) {
161                         skip_raid = 1;
162                 } else if (strcmp(arg, "--probe-all") == 0) {
163                         probe_all = 1;
164                 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
165                         printf(help);
166                         goto exit;
167                 } else
168                         node = arg;
169         }
170         if (!node) {
171                 err("no node specified");
172                 fprintf(stderr, help);
173                 rc = 1;
174                 goto exit;
175         }
176
177         vid = volume_id_open_node(node);
178         if (vid == NULL) {
179                 fprintf(stderr, "%s: error open volume\n", node);
180                 rc = 2;
181                 goto exit;
182         }
183
184         if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
185                 size = 0;
186         dbg("BLKGETSIZE64=%llu", (unsigned long long)size);
187
188         /* try to drop all privileges before reading disk content */
189         pw = getpwnam ("nobody");
190         if (pw != NULL && pw->pw_uid > 0 && pw->pw_gid > 0) {
191                 dbg("dropping privileges to %u:%u",
192                     (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid);
193                 if (setgroups(0, NULL) != 0 ||
194                     setgid(pw->pw_gid) != 0 ||
195                     setuid(pw->pw_uid) != 0) {
196                         fprintf(stderr, "error dropping privileges: %s\n", strerror(errno));
197                         rc = 3;
198                         goto exit;
199                 }
200         }
201
202         if (probe_all) {
203                 if (volume_id_probe_linux_raid(vid, 0, size) == 0)
204                         printf("%s\n", vid->type);
205                 if (volume_id_probe_intel_software_raid(vid, 0, size) == 0)
206                         printf("%s\n", vid->type);
207                 if (volume_id_probe_lsi_mega_raid(vid, 0, size) == 0)
208                         printf("%s\n", vid->type);
209                 if (volume_id_probe_via_raid(vid, 0, size) == 0)
210                         printf("%s\n", vid->type);
211                 if (volume_id_probe_silicon_medley_raid(vid, 0, size) == 0)
212                         printf("%s\n", vid->type);
213                 if (volume_id_probe_nvidia_raid(vid, 0, size) == 0)
214                         printf("%s\n", vid->type);
215                 if (volume_id_probe_promise_fasttrack_raid(vid, 0, size) == 0)
216                         printf("%s\n", vid->type);
217                 if (volume_id_probe_highpoint_45x_raid(vid, 0, size) == 0)
218                         printf("%s\n", vid->type);
219                 if (volume_id_probe_adaptec_raid(vid, 0, size) == 0)
220                         printf("%s\n", vid->type);
221                 if (volume_id_probe_jmicron_raid(vid, 0, size) == 0)
222                         printf("%s\n", vid->type);
223                 if (volume_id_probe_vfat(vid, 0, 0) == 0)
224                         printf("%s\n", vid->type);
225                 if (volume_id_probe_linux_swap(vid, 0, 0) == 0)
226                         printf("%s\n", vid->type);
227                 if (volume_id_probe_luks(vid, 0, 0) == 0)
228                         printf("%s\n", vid->type);
229                 if (volume_id_probe_xfs(vid, 0, 0) == 0)
230                         printf("%s\n", vid->type);
231                 if (volume_id_probe_ext(vid, 0, 0) == 0)
232                         printf("%s\n", vid->type);
233                 if (volume_id_probe_reiserfs(vid, 0, 0) == 0)
234                         printf("%s\n", vid->type);
235                 if (volume_id_probe_jfs(vid, 0, 0) == 0)
236                         printf("%s\n", vid->type);
237                 if (volume_id_probe_udf(vid, 0, 0) == 0)
238                         printf("%s\n", vid->type);
239                 if (volume_id_probe_iso9660(vid, 0, 0) == 0)
240                         printf("%s\n", vid->type);
241                 if (volume_id_probe_hfs_hfsplus(vid, 0, 0) == 0)
242                         printf("%s\n", vid->type);
243                 if (volume_id_probe_ufs(vid, 0, 0) == 0)
244                         printf("%s\n", vid->type);
245                 if (volume_id_probe_ntfs(vid, 0, 0)  == 0)
246                         printf("%s\n", vid->type);
247                 if (volume_id_probe_cramfs(vid, 0, 0) == 0)
248                         printf("%s\n", vid->type);
249                 if (volume_id_probe_romfs(vid, 0, 0) == 0)
250                         printf("%s\n", vid->type);
251                 if (volume_id_probe_hpfs(vid, 0, 0) == 0)
252                         printf("%s\n", vid->type);
253                 if (volume_id_probe_sysv(vid, 0, 0) == 0)
254                         printf("%s\n", vid->type);
255                 if (volume_id_probe_minix(vid, 0, 0) == 0)
256                         printf("%s\n", vid->type);
257                 if (volume_id_probe_ocfs1(vid, 0, 0) == 0)
258                         printf("%s\n", vid->type);
259                 if (volume_id_probe_ocfs2(vid, 0, 0) == 0)
260                         printf("%s\n", vid->type);
261                 if (volume_id_probe_vxfs(vid, 0, 0) == 0)
262                         printf("%s\n", vid->type);
263                 if (volume_id_probe_squashfs(vid, 0, 0) == 0)
264                         printf("%s\n", vid->type);
265                 if (volume_id_probe_netware(vid, 0, 0) == 0)
266                         printf("%s\n", vid->type);
267                 if (volume_id_probe_gfs(vid, 0, 0) == 0)
268                         printf("%s\n", vid->type);
269                 if (volume_id_probe_gfs2(vid, 0, 0) == 0)
270                         printf("%s\n", vid->type);
271
272                 goto exit;
273         }
274
275         if (skip_raid)
276                 retval = volume_id_probe_filesystem(vid, 0, size);
277         else
278                 retval = volume_id_probe_all(vid, 0, size);
279         if (retval != 0) {
280                 fprintf(stderr, "%s: unknown volume type\n", node);
281                 rc = 4;
282                 goto exit;
283         }
284
285         set_str(name, vid->label, sizeof(vid->label));
286         replace_untrusted_chars(name);
287
288         switch (print) {
289         case PRINT_EXPORT:
290                 printf("ID_FS_USAGE=%s\n", vid->usage);
291                 printf("ID_FS_TYPE=%s\n", vid->type);
292                 printf("ID_FS_VERSION=%s\n", vid->type_version);
293                 printf("ID_FS_UUID=%s\n", vid->uuid);
294                 printf("ID_FS_LABEL=%s\n", vid->label);
295                 printf("ID_FS_LABEL_SAFE=%s\n", name);
296                 break;
297         case PRINT_TYPE:
298                 printf("%s\n", vid->type);
299                 break;
300         case PRINT_LABEL:
301                 if (name[0] == '\0' || vid->usage_id == VOLUME_ID_RAID) {
302                         rc = 3;
303                         goto exit;
304                 }
305                 printf("%s\n", name);
306                 break;
307         case PRINT_UUID:
308                 if (vid->uuid[0] == '\0' || vid->usage_id == VOLUME_ID_RAID) {
309                         rc = 4;
310                         goto exit;
311                 }
312                 printf("%s\n", vid->uuid);
313                 break;
314         case PRINT_RAW_LABEL:
315                 if (vid->label[0] == '\0' || vid->usage_id == VOLUME_ID_RAID) {
316                         rc = 3;
317                         goto exit;
318                 }
319                 printf("%s\n", vid->label);
320                 break;
321         }
322
323 exit:
324         if (vid != NULL)
325                 volume_id_close(vid);
326
327         logging_close();
328         return rc;
329 }