chiark / gitweb /
c36b89f8bd4d8d443cdde11ff6400226e22fbf0e
[elogind.git] / extras / volume_id / udev_volume_id.c
1 /*
2  * udev_volume_id - udev callout to read filesystem label and uuid
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      sample udev rule for creation of a symlink with the filsystem uuid:
7  *      KERNEL="sd*", PROGRAM="/sbin/udev_volume_id -u %N", SYMLINK="%c"
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <sys/ioctl.h>
30
31 #include "../../udev_utils.h"
32 #include "../../logging.h"
33 #include "volume_id/volume_id.h"
34 #include "volume_id/dasd.h"
35
36 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
37
38 #ifdef LOG
39 void log_message(int level, const char *format, ...)
40 {
41         va_list args;
42
43         va_start(args, format);
44         vsyslog(level, format, args);
45         va_end(args);
46 }
47 #endif
48
49 extern int optind;
50
51 int main(int argc, char *argv[])
52 {
53         const char help[] = "usage: udev_volume_id [-t|-l|-u] <device>\n"
54                             "       -t filesystem type\n"
55                             "       -l filesystem label\n"
56                             "       -u filesystem uuid\n"
57                             "\n";
58         static const char short_options[] = "htlu";
59         struct volume_id *vid = NULL;
60         const char *device;
61         char print = 'a';
62         static char name[VOLUME_ID_LABEL_SIZE];
63         int len, i, j;
64         unsigned long long size;
65         int rc = 1;
66
67         logging_init("udev_volume_id");
68
69         while (1) {
70                 int option;
71
72                 option = getopt(argc, argv, short_options);
73                 if (option == -1)
74                         break;
75
76                 switch (option) {
77                 case 't':
78                         print = 't';
79                         continue;
80                 case 'l':
81                         print = 'l';
82                         continue;
83                 case 'u':
84                         print = 'u';
85                         continue;
86                 case 'h':
87                 case '?':
88                 default:
89                         printf(help);
90                         exit(1);
91                 }
92         }
93
94         device = argv[optind];
95         if (device == NULL) {
96                 printf(help);
97                 exit(1);
98         }
99
100         vid = volume_id_open_node(device);
101         if (vid == NULL) {
102                 printf("error open volume\n");
103                 goto exit;
104         }
105
106         if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
107                 size = 0;
108
109         if (volume_id_probe_all(vid, 0, size) == 0)
110                 goto print;
111
112         if (volume_id_probe_dasd(vid) == 0)
113                 goto print;
114
115         printf("unknown volume type\n");
116         goto exit;
117
118
119 print:
120         len = strnlen(vid->label, VOLUME_ID_LABEL_SIZE);
121
122         /* remove trailing spaces */
123         while (len > 0 && isspace(vid->label[len-1]))
124                 len--;
125         name[len] = '\0';
126
127         /* substitute chars */
128         i = 0;
129         j = 0;
130         while (j < len) {
131                 switch(vid->label[j]) {
132                 case '/' :
133                         break;
134                 case ' ' :
135                         name[i++] = '_';
136                         break;
137                 default :
138                         name[i++] = vid->label[j];
139                 }
140                 j++;
141         }
142         name[i] = '\0';
143
144         switch (print) {
145         case 't':
146                 printf("%s\n", vid->type);
147                 break;
148         case 'l':
149                 if (name[0] == '\0' ||
150                     (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
151                         rc = 2;
152                         goto exit;
153                 }
154                 printf("%s\n", name);
155                 break;
156         case 'u':
157                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
158                         rc = 2;
159                         goto exit;
160                 }
161                 printf("%s\n", vid->uuid);
162                 break;
163         case 'a':
164                 printf("F:%s\n", vid->usage);
165                 printf("T:%s\n", vid->type);
166                 printf("V:%s\n", vid->type_version);
167                 printf("L:%s\n", vid->label);
168                 printf("N:%s\n", name);
169                 printf("U:%s\n", vid->uuid);
170         }
171         rc = 0;
172
173 exit:
174         if (vid != NULL)
175                 volume_id_close(vid);
176
177         logging_close();
178
179         exit(rc);
180 }