4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2016, Intel Corporation.
29 #include <sys/types.h>
30 #include <sys/param.h>
40 * Linux persistent device strings for vdev labels
42 * based on udev_device_get_devid() at zfs/lib/libzfs/libzfs_import.c
45 #define DEV_BYID_PATH "/dev/disk/by-id/"
48 udev_device_get_devid(struct udev_device
*dev
, char *bufptr
, size_t buflen
)
50 struct udev_list_entry
*entry
;
52 char devbyid
[MAXPATHLEN
];
54 /* The bus based by-id path is preferred */
55 bus
= udev_device_get_property_value(dev
, "ID_BUS");
61 * For multipath nodes use the persistent uuid based identifier
63 * Example: 'dm-uuid-mpath-35000c5006304de3f'
65 dm_uuid
= udev_device_get_property_value(dev
, "DM_UUID");
66 if (dm_uuid
!= NULL
) {
67 (void) snprintf(bufptr
, buflen
, "dm-uuid-%s", dm_uuid
);
74 * locate the bus specific by-id link
76 * Example: 'scsi-MG03SCA300_350000494a8cb3d67-part1'
78 (void) snprintf(devbyid
, sizeof (devbyid
), "%s%s-", DEV_BYID_PATH
, bus
);
79 entry
= udev_device_get_devlinks_list_entry(dev
);
80 while (entry
!= NULL
) {
83 name
= udev_list_entry_get_name(entry
);
84 if (strncmp(name
, devbyid
, strlen(devbyid
)) == 0) {
85 name
+= strlen(DEV_BYID_PATH
);
86 (void) stpncpy(bufptr
, name
, buflen
- 1);
87 bufptr
[buflen
- 1] = '\0';
90 entry
= udev_list_entry_get_next(entry
);
97 * Usage: devname2devid <devicepath>
100 * # ./devname2devid /dev/sda1
101 * devid scsi-350000394a8caede4-part1
103 * # ./devname2devid /dev/dm-1
104 * devid: 'dm-uuid-mpath-35000c5006304de3f'
106 * This program accepts a disk or disk slice path and prints a
115 main(int argc
, char *argv
[])
118 struct udev_device
*dev
= NULL
;
119 char devid
[128], nodepath
[MAXPATHLEN
];
120 char *device
, *sysname
;
124 (void) printf("%s <devicepath> [search path]\n", argv
[0]);
129 if ((udev
= udev_new()) == NULL
) {
134 /* resolve path to a runtime device node instance */
135 if (realpath(device
, nodepath
) == NULL
) {
139 sysname
= strrchr(nodepath
, '/') + 1;
141 if ((dev
= udev_device_new_from_subsystem_sysname(udev
, "block",
147 if ((ret
= udev_device_get_devid(dev
, devid
, sizeof (devid
))) != 0) {
148 udev_device_unref(dev
);
154 (void) printf("devid %s\n", devid
);
156 udev_device_unref(dev
);