2 * udev_volume_id - udev callout to read filesystem label and uuid
4 * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
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"
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.
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.
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.
33 #include <sys/ioctl.h>
35 #include "../../udev_utils.h"
36 #include "../../logging.h"
37 #include "volume_id/volume_id.h"
38 #include "volume_id/dasd.h"
40 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
43 void log_message(int level, const char *format, ...)
47 va_start(args, format);
48 vsyslog(level, format, args);
55 int main(int argc, char *argv[])
57 const char help[] = "usage: udev_volume_id [-t|-l|-u] <device>\n"
58 " -t filesystem type\n"
59 " -l filesystem label\n"
60 " -u filesystem uuid\n"
62 static const char short_options[] = "htlu";
63 struct volume_id *vid = NULL;
66 static char name[VOLUME_ID_LABEL_SIZE];
68 unsigned long long size;
71 logging_init("udev_volume_id");
76 option = getopt(argc, argv, short_options);
98 device = argv[optind];
104 vid = volume_id_open_node(device);
106 printf("error open volume\n");
110 if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
113 if (volume_id_probe_all(vid, 0, size) == 0)
116 if (volume_id_probe_dasd(vid) == 0)
119 printf("unknown volume type\n");
124 len = strnlen(vid->label, VOLUME_ID_LABEL_SIZE);
126 /* remove trailing spaces */
127 while (len > 0 && isspace(vid->label[len-1]))
131 /* substitute chars */
135 switch(vid->label[j]) {
142 name[i++] = vid->label[j];
150 printf("%s\n", vid->type);
153 if (name[0] == '\0' ||
154 (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
158 printf("%s\n", name);
161 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
165 printf("%s\n", vid->uuid);
168 printf("F:%s\n", vid->usage);
169 printf("T:%s\n", vid->type);
170 printf("V:%s\n", vid->type_version);
171 printf("L:%s\n", vid->label);
172 printf("N:%s\n", name);
173 printf("U:%s\n", vid->uuid);
179 volume_id_close(vid);