1716fd348SMartin Matuska /*
2716fd348SMartin Matuska * CDDL HEADER START
3716fd348SMartin Matuska *
4716fd348SMartin Matuska * The contents of this file are subject to the terms of the
5716fd348SMartin Matuska * Common Development and Distribution License (the "License").
6716fd348SMartin Matuska * You may not use this file except in compliance with the License.
7716fd348SMartin Matuska *
8716fd348SMartin Matuska * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10716fd348SMartin Matuska * See the License for the specific language governing permissions
11716fd348SMartin Matuska * and limitations under the License.
12716fd348SMartin Matuska *
13716fd348SMartin Matuska * When distributing Covered Code, include this CDDL HEADER in each
14716fd348SMartin Matuska * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15716fd348SMartin Matuska * If applicable, add the following below this CDDL HEADER, with the
16716fd348SMartin Matuska * fields enclosed by brackets "[]" replaced with your own identifying
17716fd348SMartin Matuska * information: Portions Copyright [yyyy] [name of copyright owner]
18716fd348SMartin Matuska *
19716fd348SMartin Matuska * CDDL HEADER END
20716fd348SMartin Matuska */
21716fd348SMartin Matuska
22716fd348SMartin Matuska /*
23716fd348SMartin Matuska * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24716fd348SMartin Matuska * Use is subject to license terms.
25716fd348SMartin Matuska *
26716fd348SMartin Matuska * Copyright (c) 2016, Intel Corporation.
27716fd348SMartin Matuska */
28716fd348SMartin Matuska
29716fd348SMartin Matuska #include <sys/types.h>
30716fd348SMartin Matuska #include <sys/param.h>
31716fd348SMartin Matuska #include <sys/stat.h>
32716fd348SMartin Matuska #include <libudev.h>
33716fd348SMartin Matuska #include <errno.h>
34716fd348SMartin Matuska #include <stdio.h>
35716fd348SMartin Matuska #include <stdlib.h>
36716fd348SMartin Matuska #include <string.h>
37716fd348SMartin Matuska #include <fcntl.h>
38716fd348SMartin Matuska
39716fd348SMartin Matuska /*
40716fd348SMartin Matuska * Linux persistent device strings for vdev labels
41716fd348SMartin Matuska *
42716fd348SMartin Matuska * based on udev_device_get_devid() at zfs/lib/libzfs/libzfs_import.c
43716fd348SMartin Matuska */
44716fd348SMartin Matuska
45716fd348SMartin Matuska #define DEV_BYID_PATH "/dev/disk/by-id/"
46716fd348SMartin Matuska
47716fd348SMartin Matuska static int
udev_device_get_devid(struct udev_device * dev,char * bufptr,size_t buflen)48716fd348SMartin Matuska udev_device_get_devid(struct udev_device *dev, char *bufptr, size_t buflen)
49716fd348SMartin Matuska {
50716fd348SMartin Matuska struct udev_list_entry *entry;
51716fd348SMartin Matuska const char *bus;
52716fd348SMartin Matuska char devbyid[MAXPATHLEN];
53716fd348SMartin Matuska
54716fd348SMartin Matuska /* The bus based by-id path is preferred */
55716fd348SMartin Matuska bus = udev_device_get_property_value(dev, "ID_BUS");
56716fd348SMartin Matuska
57716fd348SMartin Matuska if (bus == NULL) {
58716fd348SMartin Matuska const char *dm_uuid;
59716fd348SMartin Matuska
60716fd348SMartin Matuska /*
61716fd348SMartin Matuska * For multipath nodes use the persistent uuid based identifier
62716fd348SMartin Matuska *
63716fd348SMartin Matuska * Example: 'dm-uuid-mpath-35000c5006304de3f'
64716fd348SMartin Matuska */
65716fd348SMartin Matuska dm_uuid = udev_device_get_property_value(dev, "DM_UUID");
66716fd348SMartin Matuska if (dm_uuid != NULL) {
67716fd348SMartin Matuska (void) snprintf(bufptr, buflen, "dm-uuid-%s", dm_uuid);
68716fd348SMartin Matuska return (0);
69716fd348SMartin Matuska }
70716fd348SMartin Matuska return (ENODATA);
71716fd348SMartin Matuska }
72716fd348SMartin Matuska
73716fd348SMartin Matuska /*
74716fd348SMartin Matuska * locate the bus specific by-id link
75716fd348SMartin Matuska *
76716fd348SMartin Matuska * Example: 'scsi-MG03SCA300_350000494a8cb3d67-part1'
77716fd348SMartin Matuska */
78716fd348SMartin Matuska (void) snprintf(devbyid, sizeof (devbyid), "%s%s-", DEV_BYID_PATH, bus);
79716fd348SMartin Matuska entry = udev_device_get_devlinks_list_entry(dev);
80716fd348SMartin Matuska while (entry != NULL) {
81716fd348SMartin Matuska const char *name;
82716fd348SMartin Matuska
83716fd348SMartin Matuska name = udev_list_entry_get_name(entry);
84716fd348SMartin Matuska if (strncmp(name, devbyid, strlen(devbyid)) == 0) {
85716fd348SMartin Matuska name += strlen(DEV_BYID_PATH);
86716fd348SMartin Matuska (void) stpncpy(bufptr, name, buflen - 1);
87716fd348SMartin Matuska bufptr[buflen - 1] = '\0';
88716fd348SMartin Matuska return (0);
89716fd348SMartin Matuska }
90716fd348SMartin Matuska entry = udev_list_entry_get_next(entry);
91716fd348SMartin Matuska }
92716fd348SMartin Matuska
93716fd348SMartin Matuska return (ENODATA);
94716fd348SMartin Matuska }
95716fd348SMartin Matuska
96716fd348SMartin Matuska /*
97716fd348SMartin Matuska * Usage: devname2devid <devicepath>
98716fd348SMartin Matuska *
99716fd348SMartin Matuska * Examples:
100716fd348SMartin Matuska * # ./devname2devid /dev/sda1
101716fd348SMartin Matuska * devid scsi-350000394a8caede4-part1
102716fd348SMartin Matuska *
103716fd348SMartin Matuska * # ./devname2devid /dev/dm-1
104716fd348SMartin Matuska * devid: 'dm-uuid-mpath-35000c5006304de3f'
105716fd348SMartin Matuska *
106716fd348SMartin Matuska * This program accepts a disk or disk slice path and prints a
107716fd348SMartin Matuska * device id.
108716fd348SMartin Matuska *
109716fd348SMartin Matuska * Exit values:
110716fd348SMartin Matuska * 0 - means success
111716fd348SMartin Matuska * 1 - means failure
112716fd348SMartin Matuska *
113716fd348SMartin Matuska */
114716fd348SMartin Matuska int
main(int argc,char * argv[])115716fd348SMartin Matuska main(int argc, char *argv[])
116716fd348SMartin Matuska {
117716fd348SMartin Matuska struct udev *udev;
118716fd348SMartin Matuska struct udev_device *dev = NULL;
119716fd348SMartin Matuska char devid[128], nodepath[MAXPATHLEN];
120716fd348SMartin Matuska char *device, *sysname;
121716fd348SMartin Matuska int ret;
122716fd348SMartin Matuska
123716fd348SMartin Matuska if (argc == 1) {
124716fd348SMartin Matuska (void) printf("%s <devicepath> [search path]\n", argv[0]);
125716fd348SMartin Matuska exit(1);
126716fd348SMartin Matuska }
127716fd348SMartin Matuska device = argv[1];
128716fd348SMartin Matuska
129716fd348SMartin Matuska if ((udev = udev_new()) == NULL) {
130716fd348SMartin Matuska perror("udev_new");
131716fd348SMartin Matuska exit(1);
132716fd348SMartin Matuska }
133716fd348SMartin Matuska
134716fd348SMartin Matuska /* resolve path to a runtime device node instance */
135716fd348SMartin Matuska if (realpath(device, nodepath) == NULL) {
136716fd348SMartin Matuska perror("realpath");
137716fd348SMartin Matuska exit(1);
138716fd348SMartin Matuska }
139716fd348SMartin Matuska sysname = strrchr(nodepath, '/') + 1;
140716fd348SMartin Matuska
141716fd348SMartin Matuska if ((dev = udev_device_new_from_subsystem_sysname(udev, "block",
142716fd348SMartin Matuska sysname)) == NULL) {
143716fd348SMartin Matuska perror(sysname);
144716fd348SMartin Matuska exit(1);
145716fd348SMartin Matuska }
146716fd348SMartin Matuska
147716fd348SMartin Matuska if ((ret = udev_device_get_devid(dev, devid, sizeof (devid))) != 0) {
148716fd348SMartin Matuska udev_device_unref(dev);
149716fd348SMartin Matuska errno = ret;
150716fd348SMartin Matuska perror(sysname);
151716fd348SMartin Matuska exit(1);
152716fd348SMartin Matuska }
153716fd348SMartin Matuska
154716fd348SMartin Matuska (void) printf("devid %s\n", devid);
155716fd348SMartin Matuska
156716fd348SMartin Matuska udev_device_unref(dev);
157716fd348SMartin Matuska udev_unref(udev);
158716fd348SMartin Matuska
159716fd348SMartin Matuska return (0);
160716fd348SMartin Matuska }
161