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, ...)
60 err = vasprintf(&pre, fmt, va);
66 err = asprintf(path, "%s-%s", pre, old);
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 **suffix)
344 name = udev_device_get_sysname(dev);
345 if (strncmp(name, "nst", 3) == 0 && strchr("lma", name[3]) != NULL)
346 asprintf(suffix, "nst%c", name[3]);
347 else if (strncmp(name, "st", 2) == 0 && strchr("lma", name[2]) != NULL)
348 asprintf(suffix, "st%c", name[2]);
351 static struct udev_device *handle_usb(struct udev_device *parent, char **path)
357 devtype = udev_device_get_devtype(parent);
358 if (devtype == NULL || strcmp(devtype, "usb_interface") != 0)
361 str = udev_device_get_sysname(parent);
362 port = strchr(str, '-');
367 parent = skip_subsystem(parent, "usb");
368 path_prepend(path, "usb-0:%s", port);
372 static struct udev_device *handle_cciss(struct udev_device *parent, char **path)
377 static struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
379 struct udev_device *scsi_dev;
381 scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
382 if (scsi_dev != NULL) {
387 hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
388 wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
389 lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
390 if (hba_id != NULL && lun != NULL && wwpn != NULL) {
391 path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
396 path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
398 parent = skip_subsystem(parent, "ccw");
402 int main(int argc, char **argv)
404 static const struct option options[] = {
405 { "debug", no_argument, NULL, 'd' },
406 { "help", no_argument, NULL, 'h' },
410 struct udev_device *dev;
411 struct udev_device *parent;
412 char syspath[UTIL_PATH_SIZE];
422 udev_log_init("path_id");
423 udev_set_log_fn(udev, log_fn);
428 option = getopt_long(argc, argv, "dh", options, NULL);
435 if (udev_get_log_priority(udev) < LOG_INFO)
436 udev_set_log_priority(udev, LOG_INFO);
439 printf("Usage: path_id [--debug] [--help] <devpath>\n"
440 " --debug print debug information\n"
441 " --help print this help text\n\n");
446 devpath = argv[optind];
447 if (devpath == NULL) {
448 fprintf(stderr, "No device specified\n");
453 util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
454 dev = udev_device_new_from_syspath(udev, syspath);
456 fprintf(stderr, "unable to access '%s'\n", devpath);
465 parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
466 if (parent != NULL) {
467 handle_ccw(parent, dev, &path);
471 /* walk up the chain of devices and compose path */
473 while (parent != NULL) {
476 subsys = udev_device_get_subsystem(parent);
478 if (subsys == NULL) {
480 } else if (strcmp(subsys, "scsi_tape") == 0) {
481 handle_scsi_tape(parent, &path_suffix);
482 } else if (strcmp(subsys, "scsi") == 0) {
483 parent = handle_scsi(parent, &path);
484 } else if (strcmp(subsys, "cciss") == 0) {
485 handle_cciss(parent, &path);
486 } else if (strcmp(subsys, "usb") == 0) {
487 parent = handle_usb(parent, &path);
488 } else if (strcmp(subsys, "serio") == 0) {
489 path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
490 parent = skip_subsystem(parent, "serio");
491 } else if (strcmp(subsys, "pci") == 0) {
492 path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
493 parent = skip_subsystem(parent, "pci");
494 } else if (strcmp(subsys, "platform") == 0) {
495 path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
496 parent = skip_subsystem(parent, "platform");
497 } else if (strcmp(subsys, "xen") == 0) {
498 path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
499 parent = skip_subsystem(parent, "xen");
500 } else if (strcmp(subsys, "virtio") == 0) {
501 path_prepend(&path, "virtio-pci-%s", udev_device_get_sysname(parent));
502 parent = skip_subsystem(parent, "virtio");
505 parent = udev_device_get_parent(parent);
509 if (path_suffix != NULL) {
510 printf("ID_PATH=%s%s\n", path, path_suffix);
513 printf("ID_PATH=%s\n", path);
519 udev_device_unref(dev);