1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * compose persistent device path
5 * Logic based on Hannes Reinecke's shell script.
18 #include "alloc-util.h"
19 #include "dirent-util.h"
21 #include "string-util.h"
24 #include "udev-util.h"
27 static void path_prepend(char **path
, const char *fmt
, ...) {
29 _cleanup_free_
char *pre
= NULL
;
33 r
= vasprintf(&pre
, fmt
, va
);
43 new = strjoin(pre
, "-", *path
);
49 free_and_replace(*path
, new);
51 *path
= TAKE_PTR(pre
);
55 ** Linux only supports 32 bit luns.
56 ** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
58 static void format_lun_number(struct udev_device
*dev
, char **path
) {
59 unsigned long lun
= strtoul(udev_device_get_sysnum(dev
), NULL
, 10);
62 /* address method 0, peripheral device addressing with bus id of zero */
63 path_prepend(path
, "lun-%lu", lun
);
65 /* handle all other lun addressing methods by using a variant of the original lun format */
66 path_prepend(path
, "lun-0x%04lx%04lx00000000", lun
& 0xffff, (lun
>> 16) & 0xffff);
69 static struct udev_device
*skip_subsystem(struct udev_device
*dev
, const char *subsys
) {
70 struct udev_device
*parent
= dev
;
76 const char *subsystem
;
78 subsystem
= udev_device_get_subsystem(parent
);
79 if (!streq_ptr(subsystem
, subsys
))
83 parent
= udev_device_get_parent(parent
);
89 static struct udev_device
*handle_scsi_fibre_channel(struct udev_device
*parent
, char **path
) {
91 struct udev_device
*targetdev
;
92 _cleanup_(udev_device_unrefp
) struct udev_device
*fcdev
= NULL
;
94 _cleanup_free_
char *lun
= NULL
;
99 udev
= udev_device_get_udev(parent
);
101 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
105 fcdev
= udev_device_new_from_subsystem_sysname(udev
, "fc_transport", udev_device_get_sysname(targetdev
));
109 port
= udev_device_get_sysattr_value(fcdev
, "port_name");
113 format_lun_number(parent
, &lun
);
114 path_prepend(path
, "fc-%s-%s", port
, lun
);
118 static struct udev_device
*handle_scsi_sas_wide_port(struct udev_device
*parent
, char **path
) {
120 struct udev_device
*targetdev
, *target_parent
;
121 _cleanup_(udev_device_unrefp
) struct udev_device
*sasdev
= NULL
;
122 const char *sas_address
;
123 _cleanup_free_
char *lun
= NULL
;
128 udev
= udev_device_get_udev(parent
);
130 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
134 target_parent
= udev_device_get_parent(targetdev
);
138 sasdev
= udev_device_new_from_subsystem_sysname(udev
, "sas_device",
139 udev_device_get_sysname(target_parent
));
143 sas_address
= udev_device_get_sysattr_value(sasdev
, "sas_address");
147 format_lun_number(parent
, &lun
);
148 path_prepend(path
, "sas-%s-%s", sas_address
, lun
);
152 static struct udev_device
*handle_scsi_sas(struct udev_device
*parent
, char **path
)
155 struct udev_device
*targetdev
, *target_parent
, *port
, *expander
;
156 _cleanup_(udev_device_unrefp
) struct udev_device
157 *target_sasdev
= NULL
, *expander_sasdev
= NULL
, *port_sasdev
= NULL
;
158 const char *sas_address
= NULL
;
160 const char *phy_count
;
161 _cleanup_free_
char *lun
= NULL
;
166 udev
= udev_device_get_udev(parent
);
168 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_target");
172 target_parent
= udev_device_get_parent(targetdev
);
177 target_sasdev
= udev_device_new_from_subsystem_sysname(
178 udev
, "sas_device", udev_device_get_sysname(target_parent
));
182 /* The next parent is sas port */
183 port
= udev_device_get_parent(target_parent
);
187 /* Get port device */
188 port_sasdev
= udev_device_new_from_subsystem_sysname(
189 udev
, "sas_port", udev_device_get_sysname(port
));
191 phy_count
= udev_device_get_sysattr_value(port_sasdev
, "num_phys");
195 /* Check if we are simple disk */
196 if (strncmp(phy_count
, "1", 2) != 0)
197 return handle_scsi_sas_wide_port(parent
, path
);
199 /* Get connected phy */
200 phy_id
= udev_device_get_sysattr_value(target_sasdev
, "phy_identifier");
204 /* The port's parent is either hba or expander */
205 expander
= udev_device_get_parent(port
);
209 /* Get expander device */
210 expander_sasdev
= udev_device_new_from_subsystem_sysname(
211 udev
, "sas_device", udev_device_get_sysname(expander
));
212 if (expander_sasdev
) {
213 /* Get expander's address */
214 sas_address
= udev_device_get_sysattr_value(expander_sasdev
,
220 format_lun_number(parent
, &lun
);
222 path_prepend(path
, "sas-exp%s-phy%s-%s", sas_address
, phy_id
, lun
);
224 path_prepend(path
, "sas-phy%s-%s", phy_id
, lun
);
229 static struct udev_device
*handle_scsi_iscsi(struct udev_device
*parent
, char **path
) {
231 struct udev_device
*transportdev
;
232 _cleanup_(udev_device_unrefp
) struct udev_device
233 *sessiondev
= NULL
, *conndev
= NULL
;
234 const char *target
, *connname
, *addr
, *port
;
235 _cleanup_free_
char *lun
= NULL
;
240 udev
= udev_device_get_udev(parent
);
242 /* find iscsi session */
243 transportdev
= parent
;
245 transportdev
= udev_device_get_parent(transportdev
);
248 if (startswith(udev_device_get_sysname(transportdev
), "session"))
252 /* find iscsi session device */
253 sessiondev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_session", udev_device_get_sysname(transportdev
));
257 target
= udev_device_get_sysattr_value(sessiondev
, "targetname");
261 connname
= strjoina("connection", udev_device_get_sysnum(transportdev
), ":0");
262 conndev
= udev_device_new_from_subsystem_sysname(udev
, "iscsi_connection", connname
);
266 addr
= udev_device_get_sysattr_value(conndev
, "persistent_address");
267 port
= udev_device_get_sysattr_value(conndev
, "persistent_port");
271 format_lun_number(parent
, &lun
);
272 path_prepend(path
, "ip-%s:%s-iscsi-%s-%s", addr
, port
, target
, lun
);
276 static struct udev_device
*handle_scsi_ata(struct udev_device
*parent
, char **path
) {
278 struct udev_device
*targetdev
, *target_parent
;
279 _cleanup_(udev_device_unrefp
) struct udev_device
*atadev
= NULL
;
285 udev
= udev_device_get_udev(parent
);
287 targetdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
291 target_parent
= udev_device_get_parent(targetdev
);
295 atadev
= udev_device_new_from_subsystem_sysname(udev
, "ata_port", udev_device_get_sysname(target_parent
));
299 port_no
= udev_device_get_sysattr_value(atadev
, "port_no");
303 path_prepend(path
, "ata-%s", port_no
);
307 static struct udev_device
*handle_scsi_default(struct udev_device
*parent
, char **path
) {
308 struct udev_device
*hostdev
;
309 int host
, bus
, target
, lun
;
310 const char *name
, *base
, *pos
;
311 _cleanup_closedir_
DIR *dir
= NULL
;
318 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
322 name
= udev_device_get_sysname(parent
);
323 if (sscanf(name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
) != 4)
327 * Rebase host offset to get the local relative number
329 * Note: This is by definition racy, unreliable and too simple.
330 * Please do not copy this model anywhere. It's just a left-over
331 * from the time we had no idea how things should look like in
334 * Making assumptions about a global in-kernel counter and use
335 * that to calculate a local offset is a very broken concept. It
336 * can only work as long as things are in strict order.
338 * The kernel needs to export the instance/port number of a
339 * controller directly, without the need for rebase magic like
340 * this. Manual driver unbind/bind, parallel hotplug/unplug will
341 * get into the way of this "I hope it works" logic.
344 base
= udev_device_get_syspath(hostdev
);
345 pos
= strrchr(base
, '/');
349 base
= strndupa(base
, pos
- base
);
354 FOREACH_DIRENT_ALL(dent
, dir
, break) {
358 if (dent
->d_name
[0] == '.')
360 if (!IN_SET(dent
->d_type
, DT_DIR
, DT_LNK
))
362 if (!startswith(dent
->d_name
, "host"))
364 i
= strtoul(&dent
->d_name
[4], &rest
, 10);
368 * find the smallest number; the host really needs to export its
369 * own instance number per parent device; relying on the global host
370 * enumeration and plainly rebasing the numbers sounds unreliable
372 if (basenum
== -1 || i
< basenum
)
379 path_prepend(path
, "scsi-%u:%u:%u:%u", host
, bus
, target
, lun
);
383 static struct udev_device
*handle_scsi_hyperv(struct udev_device
*parent
, char **path
, size_t guid_str_len
) {
384 struct udev_device
*hostdev
;
385 struct udev_device
*vmbusdev
;
386 const char *guid_str
;
387 _cleanup_free_
char *lun
= NULL
;
393 assert(guid_str_len
< sizeof(guid
));
395 hostdev
= udev_device_get_parent_with_subsystem_devtype(parent
, "scsi", "scsi_host");
399 vmbusdev
= udev_device_get_parent(hostdev
);
403 guid_str
= udev_device_get_sysattr_value(vmbusdev
, "device_id");
407 if (strlen(guid_str
) < guid_str_len
|| guid_str
[0] != '{' || guid_str
[guid_str_len
-1] != '}')
410 for (i
= 1, k
= 0; i
< guid_str_len
-1; i
++) {
411 if (guid_str
[i
] == '-')
413 guid
[k
++] = guid_str
[i
];
417 format_lun_number(parent
, &lun
);
418 path_prepend(path
, "vmbus-%s-%s", guid
, lun
);
422 static struct udev_device
*handle_scsi(struct udev_device
*parent
, char **path
, bool *supported_parent
) {
423 const char *devtype
, *id
, *name
;
425 devtype
= udev_device_get_devtype(parent
);
426 if (!streq_ptr(devtype
, "scsi_device"))
430 id
= udev_device_get_sysattr_value(parent
, "ieee1394_id");
432 path_prepend(path
, "ieee1394-0x%s", id
);
433 *supported_parent
= true;
434 return skip_subsystem(parent
, "scsi");
437 /* scsi sysfs does not have a "subsystem" for the transport */
438 name
= udev_device_get_syspath(parent
);
440 if (strstr(name
, "/rport-")) {
441 *supported_parent
= true;
442 return handle_scsi_fibre_channel(parent
, path
);
445 if (strstr(name
, "/end_device-")) {
446 *supported_parent
= true;
447 return handle_scsi_sas(parent
, path
);
450 if (strstr(name
, "/session")) {
451 *supported_parent
= true;
452 return handle_scsi_iscsi(parent
, path
);
455 if (strstr(name
, "/ata"))
456 return handle_scsi_ata(parent
, path
);
458 if (strstr(name
, "/vmbus_"))
459 return handle_scsi_hyperv(parent
, path
, 37);
460 else if (strstr(name
, "/VMBUS"))
461 return handle_scsi_hyperv(parent
, path
, 38);
463 return handle_scsi_default(parent
, path
);
466 static struct udev_device
*handle_cciss(struct udev_device
*parent
, char **path
) {
468 unsigned int controller
, disk
;
470 str
= udev_device_get_sysname(parent
);
471 if (sscanf(str
, "c%ud%u%*s", &controller
, &disk
) != 2)
474 path_prepend(path
, "cciss-disk%u", disk
);
475 return skip_subsystem(parent
, "cciss");
478 static void handle_scsi_tape(struct udev_device
*dev
, char **path
) {
481 /* must be the last device in the syspath */
485 name
= udev_device_get_sysname(dev
);
486 if (startswith(name
, "nst") && strchr("lma", name
[3]))
487 path_prepend(path
, "nst%c", name
[3]);
488 else if (startswith(name
, "st") && strchr("lma", name
[2]))
489 path_prepend(path
, "st%c", name
[2]);
492 static struct udev_device
*handle_usb(struct udev_device
*parent
, char **path
) {
493 const char *devtype
, *str
, *port
;
495 devtype
= udev_device_get_devtype(parent
);
498 if (!STR_IN_SET(devtype
, "usb_interface", "usb_device"))
501 str
= udev_device_get_sysname(parent
);
502 port
= strchr(str
, '-');
507 path_prepend(path
, "usb-0:%s", port
);
508 return skip_subsystem(parent
, "usb");
511 static struct udev_device
*handle_bcma(struct udev_device
*parent
, char **path
) {
515 sysname
= udev_device_get_sysname(parent
);
516 if (sscanf(sysname
, "bcma%*u:%u", &core
) != 1)
519 path_prepend(path
, "bcma-%u", core
);
523 /* Handle devices of AP bus in System z platform. */
524 static struct udev_device
*handle_ap(struct udev_device
*parent
, char **path
) {
525 const char *type
, *func
;
530 type
= udev_device_get_sysattr_value(parent
, "type");
531 func
= udev_device_get_sysattr_value(parent
, "ap_functions");
534 path_prepend(path
, "ap-%s-%s", type
, func
);
536 path_prepend(path
, "ap-%s", udev_device_get_sysname(parent
));
538 return skip_subsystem(parent
, "ap");
541 static int builtin_path_id(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
542 struct udev_device
*parent
;
543 _cleanup_free_
char *path
= NULL
;
544 bool supported_transport
= false;
545 bool supported_parent
= false;
549 /* walk up the chain of devices and compose path */
554 subsys
= udev_device_get_subsystem(parent
);
557 } else if (streq(subsys
, "scsi_tape")) {
558 handle_scsi_tape(parent
, &path
);
559 } else if (streq(subsys
, "scsi")) {
560 parent
= handle_scsi(parent
, &path
, &supported_parent
);
561 supported_transport
= true;
562 } else if (streq(subsys
, "cciss")) {
563 parent
= handle_cciss(parent
, &path
);
564 supported_transport
= true;
565 } else if (streq(subsys
, "usb")) {
566 parent
= handle_usb(parent
, &path
);
567 supported_transport
= true;
568 } else if (streq(subsys
, "bcma")) {
569 parent
= handle_bcma(parent
, &path
);
570 supported_transport
= true;
571 } else if (streq(subsys
, "serio")) {
572 path_prepend(&path
, "serio-%s", udev_device_get_sysnum(parent
));
573 parent
= skip_subsystem(parent
, "serio");
574 } else if (streq(subsys
, "pci")) {
575 path_prepend(&path
, "pci-%s", udev_device_get_sysname(parent
));
576 parent
= skip_subsystem(parent
, "pci");
577 supported_parent
= true;
578 } else if (streq(subsys
, "platform")) {
579 path_prepend(&path
, "platform-%s", udev_device_get_sysname(parent
));
580 parent
= skip_subsystem(parent
, "platform");
581 supported_transport
= true;
582 supported_parent
= true;
583 } else if (streq(subsys
, "acpi")) {
584 path_prepend(&path
, "acpi-%s", udev_device_get_sysname(parent
));
585 parent
= skip_subsystem(parent
, "acpi");
586 supported_parent
= true;
587 } else if (streq(subsys
, "xen")) {
588 path_prepend(&path
, "xen-%s", udev_device_get_sysname(parent
));
589 parent
= skip_subsystem(parent
, "xen");
590 supported_parent
= true;
591 } else if (streq(subsys
, "virtio")) {
592 parent
= skip_subsystem(parent
, "virtio");
593 supported_transport
= true;
594 } else if (streq(subsys
, "scm")) {
595 path_prepend(&path
, "scm-%s", udev_device_get_sysname(parent
));
596 parent
= skip_subsystem(parent
, "scm");
597 supported_transport
= true;
598 supported_parent
= true;
599 } else if (streq(subsys
, "ccw")) {
600 path_prepend(&path
, "ccw-%s", udev_device_get_sysname(parent
));
601 parent
= skip_subsystem(parent
, "ccw");
602 supported_transport
= true;
603 supported_parent
= true;
604 } else if (streq(subsys
, "ccwgroup")) {
605 path_prepend(&path
, "ccwgroup-%s", udev_device_get_sysname(parent
));
606 parent
= skip_subsystem(parent
, "ccwgroup");
607 supported_transport
= true;
608 supported_parent
= true;
609 } else if (streq(subsys
, "ap")) {
610 parent
= handle_ap(parent
, &path
);
611 supported_transport
= true;
612 supported_parent
= true;
613 } else if (streq(subsys
, "iucv")) {
614 path_prepend(&path
, "iucv-%s", udev_device_get_sysname(parent
));
615 parent
= skip_subsystem(parent
, "iucv");
616 supported_transport
= true;
617 supported_parent
= true;
618 } else if (streq(subsys
, "nvme")) {
619 const char *nsid
= udev_device_get_sysattr_value(dev
, "nsid");
622 path_prepend(&path
, "nvme-%s", nsid
);
623 parent
= skip_subsystem(parent
, "nvme");
624 supported_parent
= true;
625 supported_transport
= true;
630 parent
= udev_device_get_parent(parent
);
637 * Do not return devices with an unknown parent device type. They
638 * might produce conflicting IDs if the parent does not provide a
639 * unique and predictable name.
641 if (!supported_parent
)
645 * Do not return block devices without a well-known transport. Some
646 * devices do not expose their buses and do not provide a unique
647 * and predictable name that way.
649 if (streq_ptr(udev_device_get_subsystem(dev
), "block") && !supported_transport
)
653 char tag
[UTIL_NAME_SIZE
];
657 /* compose valid udev tag name */
658 for (p
= path
, i
= 0; *p
; p
++) {
659 if ((*p
>= '0' && *p
<= '9') ||
660 (*p
>= 'A' && *p
<= 'Z') ||
661 (*p
>= 'a' && *p
<= 'z') ||
667 /* skip all leading '_' */
671 /* avoid second '_' */
677 /* strip trailing '_' */
678 while (i
> 0 && tag
[i
-1] == '_')
682 udev_builtin_add_property(dev
, test
, "ID_PATH", path
);
683 udev_builtin_add_property(dev
, test
, "ID_PATH_TAG", tag
);
689 const struct udev_builtin udev_builtin_path_id
= {
691 .cmd
= builtin_path_id
,
692 .help
= "Compose persistent device path",