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 priority, const char *format, ...)
46 static int udev_log = -1;
51 value = getenv("UDEV_LOG");
53 udev_log = log_priority(value);
58 if (priority > udev_log)
61 va_start(args, format);
62 vsyslog(priority, format, args);
67 static void set_str(char *to, const unsigned char *from, int count)
72 len = strnlen(from, count);
73 while (isspace(from[len-1]))
77 while (isspace(from[i]) && (i < len))
96 int main(int argc, char *argv[])
98 const char help[] = "usage: udev_volume_id [--export|-t|-l|-u] <device>\n"
100 " -t filesystem type\n"
101 " -l filesystem label\n"
102 " -u filesystem uuid\n"
109 } print = PRINT_EXPORT;
110 struct volume_id *vid = NULL;
111 static char name[VOLUME_ID_LABEL_SIZE];
113 unsigned long long size;
114 const char *node = NULL;
117 logging_init("udev_volume_id");
119 for (i = 1 ; i < argc; i++) {
122 if (strcmp(arg, "--export") == 0) {
123 print = PRINT_EXPORT;
124 } else if (strcmp(arg, "-t") == 0) {
126 } else if (strcmp(arg, "-l") == 0) {
128 } else if (strcmp(arg, "-u") == 0) {
134 err("no node specified");
135 fprintf(stderr, help);
140 vid = volume_id_open_node(node);
142 fprintf(stderr, "error open volume\n");
147 if (ioctl(vid->fd, BLKGETSIZE64, &size) != 0)
150 if (volume_id_probe_all(vid, 0, size) == 0)
153 if (volume_id_probe_dasd(vid) == 0)
156 fprintf(stderr, "unknown volume type\n");
161 set_str(name, vid->label, sizeof(vid->label));
165 printf("ID_FS_USAGE=%s\n", vid->usage);
166 printf("ID_FS_TYPE=%s\n", vid->type);
167 printf("ID_FS_VERSION=%s\n", vid->type_version);
168 printf("ID_FS_UUID=%s\n", vid->uuid);
169 printf("ID_FS_LABEL=%s\n", vid->label);
170 printf("ID_FS_LABEL_SAFE=%s\n", name);
173 printf("%s\n", vid->type);
176 if (name[0] == '\0' ||
177 (vid->usage_id != VOLUME_ID_FILESYSTEM && vid->usage_id != VOLUME_ID_DISKLABEL)) {
181 printf("%s\n", name);
184 if (vid->uuid[0] == '\0' || vid->usage_id != VOLUME_ID_FILESYSTEM) {
188 printf("%s\n", vid->uuid);
194 volume_id_close(vid);