chiark / gitweb /
[PATCH] udev callout for reading filesystem labels
[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 -M%M -m%m -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
28 #include "volume_id.h"
29
30 int main(int argc, char *argv[])
31 {
32         struct volume_id *vid;
33         const char help[] = "usage: udev_volume_id -m<minor> -M<major> [-t|-l|-u]\n";
34         int major = -1;
35         int minor = -1;
36         char *tail;
37         static const char short_options[] = "M:m:htlu";
38         int option;
39         char print = '\0';
40         int rc;
41
42
43         while (1) {
44                 option = getopt(argc, argv, short_options);
45                 if (option == -1)
46                         break;
47
48                 switch (option) {
49                 case 'M':
50                         major = (int) strtoul(optarg, &tail, 10);
51                         if (tail[0] != '\0') {
52                                 printf("invalid major\n");
53                                 exit(1);
54                         }
55                         break;
56                 case 'm':
57                         minor = (int) strtoul(optarg, &tail, 10);
58                         if (tail[0] != '\0') {
59                                 printf("invalid minor\n");
60                                 exit(1);
61                         }
62                         break;
63                 case 't':
64                         print = 't';
65                         break;
66                 case 'l':
67                         print = 'l';
68                         break;
69                 case 'u':
70                         print = 'u';
71                         break;
72                 case 'h':
73                 case '?':
74                 default:
75                         printf(help);
76                         exit(1);
77                 }
78         }
79
80         if (major == -1 || minor == -1) {
81                 printf(help);
82                 exit(1);
83         }
84
85         vid = volume_id_open_dev_t(makedev(major, minor));
86         if (vid == NULL) {
87                 printf("error open volume\n");
88                 exit(1);
89         }
90
91         rc = volume_id_probe(vid, ALL);
92         if (rc != 0) {
93                 printf("error probing volume\n");
94                 exit(1);
95         }
96
97         switch (print) {
98         case 't':
99                 printf("%s\n", vid->fs_name);
100                 break;
101         case 'l':
102                 if (vid->label_string[0] == '\0')
103                         exit(2);
104                 printf("%s\n", vid->label_string);
105                 break;
106         case 'u':
107                 if (vid->uuid_string[0] == '\0')
108                         exit(2);
109                 printf("%s\n", vid->uuid_string);
110                 break;
111         default:
112                 printf("T:%s\n", vid->fs_name);
113                 printf("L:%s\n", vid->label_string);
114                 printf("U:%s\n", vid->uuid_string);
115         }
116
117         volume_id_close(vid);
118
119         exit(0);
120 }