2 * compose persistent device path
4 * Copyright (C) 2009 Kay Sievers <kay.sievers@vrfy.org>
6 * Logic based on Hannes Reinecke's shell script.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include "libudev-private.h"
38 static void log_fn(struct udev *udev, int priority,
39 const char *file, int line, const char *fn,
40 const char *format, va_list args)
43 fprintf(stderr, "%s: ", fn != NULL ? fn : file);
44 vfprintf(stderr, format, args);
46 vsyslog(priority, format, args);
50 static int path_prepend(char **path, const char *fmt, ...)
57 err = vasprintf(&pre, fmt, va);
65 err = asprintf(&new, "%s-%s", pre, *path);
79 ** Linux only supports 32 bit luns.
80 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
82 static int format_lun_number(struct udev_device *dev, char **path)
84 unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
86 /* address method 0, peripheral device addressing with bus id of zero */
88 return path_prepend(path, "lun-%d", lun);
89 /* handle all other lun addressing methods by using a variant of the original lun format */
90 return path_prepend(path, "lun-0x%04x%04x00000000", (lun & 0xffff), (lun >> 16) & 0xffff);
93 static struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys)
95 struct udev_device *parent = dev;
97 while (parent != NULL) {
98 const char *subsystem;
100 subsystem = udev_device_get_subsystem(parent);
101 if (subsystem == NULL || strcmp(subsystem, subsys) != 0)
104 parent = udev_device_get_parent(parent);
109 static struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path)
111 struct udev *udev = udev_device_get_udev(parent);
112 struct udev_device *targetdev;
113 struct udev_device *fcdev = NULL;
117 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
118 if (targetdev == NULL)
121 fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
124 port = udev_device_get_sysattr_value(fcdev, "port_name");
130 format_lun_number(parent, &lun);
131 path_prepend(path, "fc-%s-%s", port, lun);
135 udev_device_unref(fcdev);
139 static struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
141 struct udev *udev = udev_device_get_udev(parent);
142 struct udev_device *targetdev;
143 struct udev_device *target_parent;
144 struct udev_device *sasdev;
145 const char *sas_address;
148 targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
149 if (targetdev == NULL)
152 target_parent = udev_device_get_parent(targetdev);
153 if (target_parent == NULL)
156 sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
157 udev_device_get_sysname(target_parent));
161 sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
162 if (sas_address == NULL) {
167 format_lun_number(parent, &lun);
168 path_prepend(path, "sas-%s-%s", sas_address, lun);
172 udev_device_unref(sasdev);
176 static struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path)
178 struct udev *udev = udev_device_get_udev(parent);
179 struct udev_device *transportdev;
180 struct udev_device *sessiondev = NULL;
183 struct udev_device *conndev = NULL;
188 /* find iscsi session */
189 transportdev = parent;
191 transportdev = udev_device_get_parent(transportdev);
192 if (transportdev == NULL)
194 if (strncmp(udev_device_get_sysname(transportdev), "session", 7) == 0)
198 /* find iscsi session device */
199 sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
200 if (sessiondev == NULL)
202 target = udev_device_get_sysattr_value(sessiondev, "targetname");
203 if (target == NULL) {
208 if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
212 conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
214 if (conndev == NULL) {
218 addr = udev_device_get_sysattr_value(conndev, "persistent_address");
219 port = udev_device_get_sysattr_value(conndev, "persistent_port");
220 if (addr == NULL || port == NULL) {
225 format_lun_number(parent, &lun);
226 path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
230 udev_device_unref(sessiondev);
231 udev_device_unref(conndev);
235 static struct udev_device *handle_scsi_default(struct udev_device *parent, char **path)
237 struct udev_device *hostdev;
238 int host, bus, target, lun;
246 hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
250 name = udev_device_get_sysname(parent);
251 if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
254 /* rebase host offset to get the local relative number */
256 base = strdup(udev_device_get_syspath(hostdev));
259 pos = strrchr(base, '/');
270 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
274 if (dent->d_name[0] == '.')
276 if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
278 if (strncmp(dent->d_name, "host", 4) != 0)
280 i = strtoul(&dent->d_name[4], &rest, 10);
283 if (basenum == -1 || i < basenum)
293 path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
299 static struct udev_device *handle_scsi(struct udev_device *parent, char **path)
305 devtype = udev_device_get_devtype(parent);
306 if (devtype == NULL || strcmp(devtype, "scsi_device") != 0)
310 id = udev_device_get_sysattr_value(parent, "ieee1394_id");
312 parent = skip_subsystem(parent, "scsi");
313 path_prepend(path, "ieee1394-0x%s", id);
317 /* lousy scsi sysfs does not have a "subsystem" for the transport */
318 name = udev_device_get_syspath(parent);
320 if (strstr(name, "/rport-") != NULL) {
321 parent = handle_scsi_fibre_channel(parent, path);
325 if (strstr(name, "/end_device-") != NULL) {
326 parent = handle_scsi_sas(parent, path);
330 if (strstr(name, "/session") != NULL) {
331 parent = handle_scsi_iscsi(parent, path);
335 parent = handle_scsi_default(parent, path);
340 static void handle_scsi_tape(struct udev_device *dev, char **path)
344 /* must be the last device in the syspath */
348 name = udev_device_get_sysname(dev);
349 if (strncmp(name, "nst", 3) == 0 && strchr("lma", name[3]) != NULL)
350 path_prepend(path, "nst%c", name[3]);
351 else if (strncmp(name, "st", 2) == 0 && strchr("lma", name[2]) != NULL)
352 path_prepend(path, "st%c", name[2]);
355 static struct udev_device *handle_usb(struct udev_device *parent, char **path)
361 devtype = udev_device_get_devtype(parent);
364 if (strcmp(devtype, "usb_interface") != 0 && strcmp(devtype, "usb_device") != 0)
367 str = udev_device_get_sysname(parent);
368 port = strchr(str, '-');
373 parent = skip_subsystem(parent, "usb");
374 path_prepend(path, "usb-0:%s", port);
378 static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
383 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
385 struct udev_device *scsi_dev;
387 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
388 if (scsi_dev != NULL) {
393 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
394 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
395 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
396 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
397 path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
402 path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
404 parent = skip_subsystem(parent, "ccw");
408 int main(int argc, char **argv)
410 static const struct option options[] = {
411 { "debug", no_argument, NULL, 'd' },
412 { "help", no_argument, NULL, 'h' },
416 struct udev_device *dev;
417 struct udev_device *parent;
418 char syspath[UTIL_PATH_SIZE];
421 int rc = EXIT_FAILURE;
427 udev_log_init("path_id");
428 udev_set_log_fn(udev, log_fn);
433 option = getopt_long(argc, argv, "dh", options, NULL);
440 if (udev_get_log_priority(udev) < LOG_INFO)
441 udev_set_log_priority(udev, LOG_INFO);
444 printf("Usage: path_id [--debug] [--help] <devpath>\n"
445 " --debug print debug information\n"
446 " --help print this help text\n\n");
451 devpath = argv[optind];
452 if (devpath == NULL) {
453 fprintf(stderr, "No device specified\n");
458 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
459 dev = udev_device_new_from_syspath(udev, syspath);
461 fprintf(stderr, "unable to access '%s'\n", devpath);
467 parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
468 if (parent != NULL) {
469 handle_ccw(parent, dev, &path);
473 /* walk up the chain of devices and compose path */
475 while (parent != NULL) {
478 subsys = udev_device_get_subsystem(parent);
480 if (subsys == NULL) {
482 } else if (strcmp(subsys, "scsi_tape") == 0) {
483 handle_scsi_tape(parent, &path);
484 } else if (strcmp(subsys, "scsi") == 0) {
485 parent = handle_scsi(parent, &path);
486 } else if (strcmp(subsys, "cciss") == 0) {
487 handle_cciss(parent, &path);
488 } else if (strcmp(subsys, "usb") == 0) {
489 parent = handle_usb(parent, &path);
490 } else if (strcmp(subsys, "serio") == 0) {
491 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
492 parent = skip_subsystem(parent, "serio");
493 } else if (strcmp(subsys, "pci") == 0) {
494 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
495 parent = skip_subsystem(parent, "pci");
496 } else if (strcmp(subsys, "platform") == 0) {
497 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
498 parent = skip_subsystem(parent, "platform");
499 } else if (strcmp(subsys, "acpi") == 0) {
500 path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
501 parent = skip_subsystem(parent, "acpi");
502 } else if (strcmp(subsys, "xen") == 0) {
503 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
504 parent = skip_subsystem(parent, "xen");
505 } else if (strcmp(subsys, "virtio") == 0) {
506 path_prepend(&path, "virtio-pci-%s", udev_device_get_sysname(parent));
507 parent = skip_subsystem(parent, "virtio");
510 parent = udev_device_get_parent(parent);
514 char tag[UTIL_NAME_SIZE];
518 /* compose valid udev tag name */
519 for (p = path, i = 0; *p; p++) {
520 if ((*p >= '0' && *p <= '9') ||
521 (*p >= 'A' && *p <= 'Z') ||
522 (*p >= 'a' && *p <= 'z') ||
528 /* skip all leading '_' */
532 /* avoid second '_' */
538 /* strip trailing '_' */
539 while (i > 0 && tag[i-1] == '_')
543 printf("ID_PATH=%s\n", path);
544 printf("ID_PATH_TAG=%s\n", tag);
549 udev_device_unref(dev);