Linux: Fix detection of register_sysctl_sz
[zfs.git] / tests / zfs-tests / cmd / devname2devid.c
blob0f3655be71438dd4d17ad9386ea242a02e766309
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
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>
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/"
47 static int
48 udev_device_get_devid(struct udev_device *dev, char *bufptr, size_t buflen)
50 struct udev_list_entry *entry;
51 const char *bus;
52 char devbyid[MAXPATHLEN];
54 /* The bus based by-id path is preferred */
55 bus = udev_device_get_property_value(dev, "ID_BUS");
57 if (bus == NULL) {
58 const char *dm_uuid;
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);
68 return (0);
70 return (ENODATA);
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) {
81 const char *name;
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);
90 entry = udev_list_entry_get_next(entry);
93 return (ENODATA);
97 * Usage: devname2devid <devicepath>
99 * Examples:
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
107 * device id.
109 * Exit values:
110 * 0 - means success
111 * 1 - means failure
115 main(int argc, char *argv[])
117 struct udev *udev;
118 struct udev_device *dev = NULL;
119 char devid[128], nodepath[MAXPATHLEN];
120 char *device, *sysname;
121 int ret;
123 if (argc == 1) {
124 (void) printf("%s <devicepath> [search path]\n", argv[0]);
125 exit(1);
127 device = argv[1];
129 if ((udev = udev_new()) == NULL) {
130 perror("udev_new");
131 exit(1);
134 /* resolve path to a runtime device node instance */
135 if (realpath(device, nodepath) == NULL) {
136 perror("realpath");
137 exit(1);
139 sysname = strrchr(nodepath, '/') + 1;
141 if ((dev = udev_device_new_from_subsystem_sysname(udev, "block",
142 sysname)) == NULL) {
143 perror(sysname);
144 exit(1);
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);
154 (void) printf("devid %s\n", devid);
156 udev_device_unref(dev);
157 udev_unref(udev);
159 return (0);