xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/devname2devid.c (revision 2a66634d1bc6d7401adafad4a3be7b9ac6bab8b3)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2016, Intel Corporation.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <libudev.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <fcntl.h>
38 
39 /*
40  * Linux persistent device strings for vdev labels
41  *
42  * based on udev_device_get_devid() at zfs/lib/libzfs/libzfs_import.c
43  */
44 
45 #define	DEV_BYID_PATH	"/dev/disk/by-id/"
46 
47 static int
48 udev_device_get_devid(struct udev_device *dev, char *bufptr, size_t buflen)
49 {
50 	struct udev_list_entry *entry;
51 	const char *bus;
52 	char devbyid[MAXPATHLEN];
53 
54 	/* The bus based by-id path is preferred */
55 	bus = udev_device_get_property_value(dev, "ID_BUS");
56 
57 	if (bus == NULL) {
58 		const char *dm_uuid;
59 
60 		/*
61 		 * For multipath nodes use the persistent uuid based identifier
62 		 *
63 		 * Example: 'dm-uuid-mpath-35000c5006304de3f'
64 		 */
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);
68 			return (0);
69 		}
70 		return (ENODATA);
71 	}
72 
73 	/*
74 	 * locate the bus specific by-id link
75 	 *
76 	 * Example: 'scsi-MG03SCA300_350000494a8cb3d67-part1'
77 	 */
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) {
81 		const char *name;
82 
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';
88 			return (0);
89 		}
90 		entry = udev_list_entry_get_next(entry);
91 	}
92 
93 	return (ENODATA);
94 }
95 
96 /*
97  * Usage: devname2devid <devicepath>
98  *
99  * Examples:
100  * # ./devname2devid /dev/sda1
101  * devid scsi-350000394a8caede4-part1
102  *
103  * # ./devname2devid /dev/dm-1
104  * devid: 'dm-uuid-mpath-35000c5006304de3f'
105  *
106  * This program accepts a disk or disk slice path and prints a
107  * device id.
108  *
109  * Exit values:
110  *	0 - means success
111  *	1 - means failure
112  *
113  */
114 int
115 main(int argc, char *argv[])
116 {
117 	struct udev *udev;
118 	struct udev_device *dev = NULL;
119 	char devid[128], nodepath[MAXPATHLEN];
120 	char *device, *sysname;
121 	int ret;
122 
123 	if (argc == 1) {
124 		(void) printf("%s <devicepath> [search path]\n", argv[0]);
125 		exit(1);
126 	}
127 	device = argv[1];
128 
129 	if ((udev = udev_new()) == NULL) {
130 		perror("udev_new");
131 		exit(1);
132 	}
133 
134 	/* resolve path to a runtime device node instance */
135 	if (realpath(device, nodepath) == NULL) {
136 		perror("realpath");
137 		exit(1);
138 	}
139 	sysname = strrchr(nodepath, '/') + 1;
140 
141 	if ((dev = udev_device_new_from_subsystem_sysname(udev, "block",
142 	    sysname)) == NULL) {
143 		perror(sysname);
144 		exit(1);
145 	}
146 
147 	if ((ret = udev_device_get_devid(dev, devid, sizeof (devid))) != 0) {
148 		udev_device_unref(dev);
149 		errno = ret;
150 		perror(sysname);
151 		exit(1);
152 	}
153 
154 	(void) printf("devid %s\n", devid);
155 
156 	udev_device_unref(dev);
157 	udev_unref(udev);
158 
159 	return (0);
160 }
161