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.
29 #include <sys/ioctl.h>
31 #include "../../udev_utils.h"
32 #include "../../logging.h"
33 #include "volume_id/volume_id.h"
34 #include "volume_id/dasd.h"
36 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
39 void log_message(int level, const char *format, ...)
43 va_start(args, format);
44 vsyslog(level, format, args);
51 int main(int argc, char *argv[])
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"
58 static const char short_options[] = "htlu";
59 struct volume_id *vid = NULL;
62 static char name[VOLUME_ID_LABEL_SIZE];
64 unsigned long long size;
67 logging_init("udev_volume_id");
72 option = getopt(argc, argv, short_options);
94 device = argv[optind];
100 vid = volume_id_open_node(device);
102 printf("error open volume\n");
106 if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
109 if (volume_id_probe_all(vid, 0, size) == 0)
112 if (volume_id_probe_dasd(vid) == 0)
115 printf("unknown volume type\n");
120 len = strnlen(vid->label, VOLUME_ID_LABEL_SIZE);
122 /* remove trailing spaces */
123 while (len > 0 && isspace(vid->label[len-1]))
127 /* substitute chars */
131 switch(vid->label[j]) {
138 name[i++] = vid->label[j];
146 printf("%s\n", vid->type);
149 if (name[0] == '\0' ||
150 (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
154 printf("%s\n", name);
157 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
161 printf("%s\n", vid->uuid);
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);
175 volume_id_close(vid);