udev-builtin-path_id.c revision 89f17d4f807f2fbd9918b513296e25ee2be108c3
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * compose persistent device path
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Copyright (C) 2009-2011 Kay Sievers <kay@vrfy.org>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Logic based on Hannes Reinecke's shell script.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * This program is free software: you can redistribute it and/or modify
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * it under the terms of the GNU General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * This program is distributed in the hope that it will be useful,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * GNU General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * You should have received a copy of the GNU General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic int path_prepend(char **path, const char *fmt, ...)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering err = asprintf(&new, "%s-%s", pre, *path);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen** Linux only supports 32 bit luns.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering** See drivers/scsi/scsi_scan.c::scsilun_to_int() for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int format_lun_number(struct udev_device *dev, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unsigned long lun = strtoul(udev_device_get_sysnum(dev), NULL, 10);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* address method 0, peripheral device addressing with bus id of zero */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* handle all other lun addressing methods by using a variant of the original lun format */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return path_prepend(path, "lun-0x%04x%04x00000000", (lun & 0xffff), (lun >> 16) & 0xffff);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic struct udev_device *skip_subsystem(struct udev_device *dev, const char *subsys)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering subsystem = udev_device_get_subsystem(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (subsystem == NULL || !streq(subsystem, subsys))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_scsi_fibre_channel(struct udev_device *parent, char **path)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen struct udev *udev = udev_device_get_udev(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering fcdev = udev_device_new_from_subsystem_sysname(udev, "fc_transport", udev_device_get_sysname(targetdev));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen port = udev_device_get_sysattr_value(fcdev, "port_name");
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering path_prepend(path, "fc-%s-%s", port, lun);
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poetteringstatic struct udev_device *handle_scsi_sas(struct udev_device *parent, char **path)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev *udev = udev_device_get_udev(parent);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering targetdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_target");
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering target_parent = udev_device_get_parent(targetdev);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sasdev = udev_device_new_from_subsystem_sysname(udev, "sas_device",
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen sas_address = udev_device_get_sysattr_value(sasdev, "sas_address");
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering path_prepend(path, "sas-%s-%s", sas_address, lun);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringstatic struct udev_device *handle_scsi_iscsi(struct udev_device *parent, char **path)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering struct udev *udev = udev_device_get_udev(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* find iscsi session */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering transportdev = udev_device_get_parent(transportdev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (startswith(udev_device_get_sysname(transportdev), "session"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* find iscsi session device */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sessiondev = udev_device_new_from_subsystem_sysname(udev, "iscsi_session", udev_device_get_sysname(transportdev));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering target = udev_device_get_sysattr_value(sessiondev, "targetname");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (asprintf(&connname, "connection%s:0", udev_device_get_sysnum(transportdev)) < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering conndev = udev_device_new_from_subsystem_sysname(udev, "iscsi_connection", connname);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering addr = udev_device_get_sysattr_value(conndev, "persistent_address");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering port = udev_device_get_sysattr_value(conndev, "persistent_port");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "ip-%s:%s-iscsi-%s-%s", addr, port, target, lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_scsi_default(struct udev_device *parent, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (sscanf(name, "%d:%d:%d:%d", &host, &bus, &target, &lun) != 4)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Rebase host offset to get the local relative number
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * Note: This is by definition racy, unreliable and too simple.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * Please do not copy this model anywhere. It's just a left-over
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * from the time we had no idea how things should look like in
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * Making assumptions about a global in-kernel counter and use
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * that to calculate a local offset is a very broken concept. It
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * can only work as long as things are in strict order.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * The kernel needs to export the instance/port number of a
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * controller directly, without the need for rebase magic like
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * this. Manual driver unbind/bind, parallel hotplug/unplug will
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * get into the way of this "I hope it works" logic.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering base = strdup(udev_device_get_syspath(hostdev));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (dent->d_type != DT_DIR && dent->d_type != DT_LNK)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * find the smallest number; the host really needs to export its
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * own instance number per parent device; relying on the global host
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * enumeration and plainly rebasing the numbers sounds unreliable
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "scsi-%u:%u:%u:%u", host, bus, target, lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_scsi_hyperv(struct udev_device *parent, char **path) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen hostdev = udev_device_get_parent_with_subsystem_devtype(parent, "scsi", "scsi_host");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering vmbusdev = udev_device_get_parent(hostdev);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering guid_str = udev_device_get_sysattr_value(vmbusdev, "device_id");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (strlen(guid_str) < 37 || guid_str[0] != '{' || guid_str[36] != '}')
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_scsi(struct udev_device *parent, char **path)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *id;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (devtype == NULL || !streq(devtype, "scsi_device"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* firewire */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering id = udev_device_get_sysattr_value(parent, "ieee1394_id");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* scsi sysfs does not have a "subsystem" for the transport */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_scsi_fibre_channel(parent, path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (strstr(name, "/end_device-") != NULL) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * We do not support the ATA transport class, it uses global counters
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * to name the ata devices which numbers spread across multiple
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * controllers.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * The real link numbers are not exported. Also, possible chains of ports
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * behind port multipliers cannot be composed that way.
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * Until all that is solved at the kernel level, there are no by-path/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * links for ATA devices.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = handle_scsi_default(parent, path);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic struct udev_device *handle_cciss(struct udev_device *parent, char **path)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (sscanf(str, "c%ud%u%*s", &controller, &disk) != 2)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "cciss-disk%u", disk);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "cciss");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void handle_scsi_tape(struct udev_device *dev, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* must be the last device in the syspath */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (startswith(name, "nst") && strchr("lma", name[3]) != NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else if (startswith(name, "st") && strchr("lma", name[2]) != NULL)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_usb(struct udev_device *parent, char **path)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *str;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (!streq(devtype, "usb_interface") && !streq(devtype, "usb_device"))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_bcma(struct udev_device *parent, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sysname = udev_device_get_sysname(parent);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (sscanf(sysname, "bcma%*u:%u", &core) != 1)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic struct udev_device *handle_ccw(struct udev_device *parent, struct udev_device *dev, char **path)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering scsi_dev = udev_device_get_parent_with_subsystem_devtype(dev, "scsi", "scsi_device");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *lun;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen hba_id = udev_device_get_sysattr_value(scsi_dev, "hba_id");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen wwpn = udev_device_get_sysattr_value(scsi_dev, "wwpn");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen lun = udev_device_get_sysattr_value(scsi_dev, "fcp_lun");
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (hba_id != NULL && lun != NULL && wwpn != NULL) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen path_prepend(path, "ccw-%s-zfcp-%s:%s", hba_id, wwpn, lun);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(path, "ccw-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int builtin_path_id(struct udev_device *dev, int argc, char *argv[], bool test)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* S390 ccw bus */
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen parent = udev_device_get_parent_with_subsystem_devtype(dev, "ccw", NULL);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* walk up the chain of devices and compose path */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering subsys = udev_device_get_subsystem(parent);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "serio-%s", udev_device_get_sysnum(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "serio");
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering path_prepend(&path, "pci-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "platform-%s", udev_device_get_sysname(parent));
19887cd06a3af2f045e763986eda19e208bd3f85Zbigniew Jędrzejewski-Szmek parent = skip_subsystem(parent, "platform");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "acpi-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "xen-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "virtio-pci-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering parent = skip_subsystem(parent, "virtio");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering path_prepend(&path, "scm-%s", udev_device_get_sysname(parent));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * Do not return a single-parent-device-only for block
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * devices, they might have entire buses behind it which
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * do not get unique IDs only by using the parent device.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!some_transport && streq(udev_device_get_subsystem(dev), "block")) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering const char *p;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* compose valid udev tag name */
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering for (p = path, i = 0; *p; p++) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* skip all leading '_' */
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering /* avoid second '_' */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* strip trailing '_' */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_builtin_add_property(dev, test, "ID_PATH", path);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringconst struct udev_builtin udev_builtin_path_id = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering .help = "compose persistent device path",