chiark / gitweb /
6b62db0775e38c52bf0ea20fee07965732918d35
[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) 2004 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", 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 "../../libsysfs/sysfs/libsysfs.h"
32 #include "../../udev_lib.h"
33 #include "../../logging.h"
34 #include "volume_id.h"
35 #include "dasdlabel.h"
36
37 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
38
39 #ifdef LOG
40 unsigned char logname[LOGNAME_SIZE];
41 void log_message(int level, const char *format, ...)
42 {
43         va_list args;
44
45         va_start(args, format);
46         vsyslog(level, format, args);
47         va_end(args);
48 }
49 #endif
50
51 static struct volume_id *open_classdev(struct sysfs_class_device *class_dev)
52 {
53         struct volume_id *vid;
54         struct sysfs_attribute *attr;
55         int major, minor;
56
57         attr = sysfs_get_classdev_attr(class_dev, "dev");
58
59         if (attr == NULL) {
60                 printf("error reading 'dev' attribute\n");
61                 return NULL;
62         }
63
64         if (sscanf(attr->value, "%u:%u", &major, &minor) != 2) {
65                 printf("error getting major/minor number\n");
66                 return NULL;
67         }
68
69         vid = volume_id_open_dev_t(makedev(major, minor));
70         if (vid == NULL) {
71                 printf("error open volume\n");
72                 return NULL;
73         }
74
75         return vid;
76 }
77
78 static char *usage_id_name(enum volume_id_usage usage)
79 {
80         switch(usage) {
81         case VOLUME_ID_UNUSED:
82                 return "unused";
83         case VOLUME_ID_UNPROBED:
84                 return "unprobed";
85         case VOLUME_ID_OTHER:
86                 return "other";
87         case VOLUME_ID_PARTITIONTABLE:
88                 return "partitiontable";
89         case VOLUME_ID_FILESYSTEM:
90                 return "filesystem";
91         case VOLUME_ID_RAID:
92                 return "raid";
93         default:
94                 return "unknown type_id";
95         }
96 }
97
98 int main(int argc, char *argv[])
99 {
100         const char help[] = "usage: udev_volume_id [-t|-l|-u|-d]\n"
101                             "       -t filesystem type\n"
102                             "       -l filesystem label\n"
103                             "       -u filesystem uuid\n"
104                             "       -d disk label from main device\n"
105                             "\n";
106         static const char short_options[] = "htlud";
107         char sysfs_path[SYSFS_PATH_MAX];
108         char dev_path[SYSFS_PATH_MAX];
109         struct sysfs_class_device *class_dev = NULL;
110         struct sysfs_class_device *class_dev_parent = NULL;
111         struct volume_id *vid = NULL;
112         char *devpath;
113         char probe = 'p';
114         char print = 'a';
115         char dasd_label[7];
116         static char name[VOLUME_ID_LABEL_SIZE];
117         int len, i, j;
118         unsigned long long size;
119         int rc = 1;
120
121         while (1) {
122                 int option;
123
124                 option = getopt(argc, argv, short_options);
125                 if (option == -1)
126                         break;
127
128                 switch (option) {
129                 case 't':
130                         print = 't';
131                         continue;
132                 case 'l':
133                         print = 'l';
134                         continue;
135                 case 'u':
136                         print = 'u';
137                         continue;
138                 case 'd':
139                         probe = 'd';
140                         continue;
141                 case 'h':
142                 case '?':
143                 default:
144                         printf(help);
145                         exit(1);
146                 }
147         }
148
149         devpath = getenv("DEVPATH");
150         if (devpath == NULL) {
151                 printf("error DEVPATH empty\n");
152                 goto exit;
153         }
154
155         if (sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX) != 0) {
156                 printf("error getting sysfs mount path\n");
157                 goto exit;
158         }
159
160         strfieldcpy(dev_path, sysfs_path);
161         strfieldcat(dev_path, devpath);
162
163         class_dev = sysfs_open_class_device_path(dev_path);
164         if (class_dev == NULL) {
165                 printf("error getting class device\n");
166                 goto exit;
167         }
168
169         switch(probe) {
170         case 'p' :
171                 /* open block device */
172                 vid = open_classdev(class_dev);
173                 if (vid == NULL)
174                         goto exit;
175
176                 if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
177                         size = 0;
178
179                 if (volume_id_probe(vid, VOLUME_ID_ALL, 0, size) == 0)
180                         goto print;
181                 break;
182         case 'd' :
183                 /* if we are on a partition, open main block device instead */
184                 class_dev_parent = sysfs_get_classdev_parent(class_dev);
185                 if (class_dev_parent != NULL)
186                         vid = open_classdev(class_dev_parent);
187                 else
188                         vid = open_classdev(class_dev_parent);
189                 if (vid == NULL)
190                         goto exit;
191
192                 if (probe_ibm_partition(vid->fd, dasd_label) == 0) {
193                         vid->type = "dasd";
194                         strncpy(vid->label, dasd_label, 6);
195                         vid->label[6] = '\0';
196                         goto print;
197                 }
198                 break;
199         }
200
201         printf("unknown volume type\n");
202         goto exit;
203
204
205 print:
206         len = strnlen(vid->label, VOLUME_ID_LABEL_SIZE);
207
208         /* remove trailing spaces */
209         while (len > 0 && isspace(vid->label[len-1]))
210                 len--;
211         name[len] = '\0';
212
213         /* substitute chars */
214         i = 0;
215         j = 0;
216         while (j < len) {
217                 switch(vid->label[j]) {
218                 case '/' :
219                         break;
220                 case ' ' :
221                         name[i++] = '_';
222                         break;
223                 default :
224                         name[i++] = vid->label[j];
225                 }
226                 j++;
227         }
228         name[i] = '\0';
229
230         switch (print) {
231         case 't':
232                 printf("%s\n", vid->type);
233                 break;
234         case 'l':
235                 if (name[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
236                         rc = 2;
237                         goto exit;
238                 }
239                 printf("%s\n", name);
240                 break;
241         case 'u':
242                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
243                         rc = 2;
244                         goto exit;
245                 }
246                 printf("%s\n", vid->uuid);
247                 break;
248         case 'a':
249                 printf("F:%s\n", usage_id_name(vid->usage_id));
250                 printf("T:%s\n", vid->type);
251                 printf("V:%s\n", vid->type_version);
252                 printf("L:%s\n", vid->label);
253                 printf("N:%s\n", name);
254                 printf("U:%s\n", vid->uuid);
255         }
256         rc = 0;
257
258 exit:
259         if (class_dev != NULL)
260                 sysfs_close_class_device(class_dev);
261         if (vid != NULL)
262                 volume_id_close(vid);
263
264         exit(rc);
265 }