]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - zfs/tests/zfs-tests/cmd/devname2devid/devname2devid.c
UBUNTU: SAUCE: Update zfs to e02aaf17f15ad274fa1f24c9c826f1477911ea3f
[mirror_ubuntu-zesty-kernel.git] / zfs / tests / zfs-tests / cmd / devname2devid / devname2devid.c
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);
87 return (0);
88 }
89 entry = udev_list_entry_get_next(entry);
90 }
91
92 return (ENODATA);
93 }
94
95 /*
96 * Usage: devname2devid <devicepath>
97 *
98 * Examples:
99 * # ./devname2devid /dev/sda1
100 * devid scsi-350000394a8caede4-part1
101 *
102 * # ./devname2devid /dev/dm-1
103 * devid: 'dm-uuid-mpath-35000c5006304de3f'
104 *
105 * This program accepts a disk or disk slice path and prints a
106 * device id.
107 *
108 * Exit values:
109 * 0 - means success
110 * 1 - means failure
111 *
112 */
113 int
114 main(int argc, char *argv[])
115 {
116 struct udev *udev;
117 struct udev_device *dev = NULL;
118 char devid[128], nodepath[MAXPATHLEN];
119 char *device, *sysname;
120 int ret;
121
122 if (argc == 1) {
123 (void) printf("%s <devicepath> [search path]\n", argv[0]);
124 exit(1);
125 }
126 device = argv[1];
127
128 if ((udev = udev_new()) == NULL) {
129 perror("udev_new");
130 exit(1);
131 }
132
133 /* resolve path to a runtime device node instance */
134 if (realpath(device, nodepath) == NULL) {
135 perror("realpath");
136 exit(1);
137 }
138 sysname = strrchr(nodepath, '/') + 1;
139
140 if ((dev = udev_device_new_from_subsystem_sysname(udev, "block",
141 sysname)) == NULL) {
142 perror(sysname);
143 exit(1);
144 }
145
146 if ((ret = udev_device_get_devid(dev, devid, sizeof (devid))) != 0) {
147 udev_device_unref(dev);
148 errno = ret;
149 perror(sysname);
150 exit(1);
151 }
152
153 (void) printf("devid %s\n", devid);
154
155 udev_device_unref(dev);
156 udev_unref(udev);
157
158 return (0);
159 }