chiark / gitweb /
[PATCH] Add support for highpoint ataraid to volume_id to suppress
[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_mnt_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         logging_init("udev_volume_id");
122
123         while (1) {
124                 int option;
125
126                 option = getopt(argc, argv, short_options);
127                 if (option == -1)
128                         break;
129
130                 switch (option) {
131                 case 't':
132                         print = 't';
133                         continue;
134                 case 'l':
135                         print = 'l';
136                         continue;
137                 case 'u':
138                         print = 'u';
139                         continue;
140                 case 'd':
141                         probe = 'd';
142                         continue;
143                 case 'h':
144                 case '?':
145                 default:
146                         printf(help);
147                         exit(1);
148                 }
149         }
150
151         devpath = getenv("DEVPATH");
152         if (devpath == NULL) {
153                 printf("error DEVPATH empty\n");
154                 goto exit;
155         }
156
157         if (sysfs_get_mnt_path(sysfs_mnt_path, SYSFS_PATH_MAX) != 0) {
158                 printf("error getting sysfs mount path\n");
159                 goto exit;
160         }
161
162         strfieldcpy(dev_path, sysfs_mnt_path);
163         strfieldcat(dev_path, devpath);
164
165         class_dev = sysfs_open_class_device_path(dev_path);
166         if (class_dev == NULL) {
167                 printf("error getting class device\n");
168                 goto exit;
169         }
170
171         switch(probe) {
172         case 'p' :
173                 /* open block device */
174                 vid = open_classdev(class_dev);
175                 if (vid == NULL)
176                         goto exit;
177
178                 if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
179                         size = 0;
180
181                 if (volume_id_probe(vid, VOLUME_ID_ALL, 0, size) == 0)
182                         goto print;
183                 break;
184         case 'd' :
185                 /* if we are on a partition, open main block device instead */
186                 class_dev_parent = sysfs_get_classdev_parent(class_dev);
187                 if (class_dev_parent != NULL)
188                         vid = open_classdev(class_dev_parent);
189                 else
190                         vid = open_classdev(class_dev_parent);
191                 if (vid == NULL)
192                         goto exit;
193
194                 if (probe_ibm_partition(vid->fd, dasd_label) == 0) {
195                         vid->type = "dasd";
196                         strncpy(vid->label, dasd_label, 6);
197                         vid->label[6] = '\0';
198                         goto print;
199                 }
200                 break;
201         }
202
203         printf("unknown volume type\n");
204         goto exit;
205
206
207 print:
208         len = strnlen(vid->label, VOLUME_ID_LABEL_SIZE);
209
210         /* remove trailing spaces */
211         while (len > 0 && isspace(vid->label[len-1]))
212                 len--;
213         name[len] = '\0';
214
215         /* substitute chars */
216         i = 0;
217         j = 0;
218         while (j < len) {
219                 switch(vid->label[j]) {
220                 case '/' :
221                         break;
222                 case ' ' :
223                         name[i++] = '_';
224                         break;
225                 default :
226                         name[i++] = vid->label[j];
227                 }
228                 j++;
229         }
230         name[i] = '\0';
231
232         switch (print) {
233         case 't':
234                 printf("%s\n", vid->type);
235                 break;
236         case 'l':
237                 if (name[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
238                         rc = 2;
239                         goto exit;
240                 }
241                 printf("%s\n", name);
242                 break;
243         case 'u':
244                 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
245                         rc = 2;
246                         goto exit;
247                 }
248                 printf("%s\n", vid->uuid);
249                 break;
250         case 'a':
251                 printf("F:%s\n", usage_id_name(vid->usage_id));
252                 printf("T:%s\n", vid->type);
253                 printf("V:%s\n", vid->type_version);
254                 printf("L:%s\n", vid->label);
255                 printf("N:%s\n", name);
256                 printf("U:%s\n", vid->uuid);
257         }
258         rc = 0;
259
260 exit:
261         if (class_dev != NULL)
262                 sysfs_close_class_device(class_dev);
263         if (vid != NULL)
264                 volume_id_close(vid);
265
266         logging_close();
267
268         exit(rc);
269 }