xref: /freebsd/tests/sys/cddl/zfs/bin/devname2devid.c (revision 2fae26bd8b752cfae083962a152f4b1ee54ada17)
1*2fae26bdSAlan Somers /*
2*2fae26bdSAlan Somers  * CDDL HEADER START
3*2fae26bdSAlan Somers  *
4*2fae26bdSAlan Somers  * The contents of this file are subject to the terms of the
5*2fae26bdSAlan Somers  * Common Development and Distribution License (the "License").
6*2fae26bdSAlan Somers  * You may not use this file except in compliance with the License.
7*2fae26bdSAlan Somers  *
8*2fae26bdSAlan Somers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2fae26bdSAlan Somers  * or http://www.opensolaris.org/os/licensing.
10*2fae26bdSAlan Somers  * See the License for the specific language governing permissions
11*2fae26bdSAlan Somers  * and limitations under the License.
12*2fae26bdSAlan Somers  *
13*2fae26bdSAlan Somers  * When distributing Covered Code, include this CDDL HEADER in each
14*2fae26bdSAlan Somers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2fae26bdSAlan Somers  * If applicable, add the following below this CDDL HEADER, with the
16*2fae26bdSAlan Somers  * fields enclosed by brackets "[]" replaced with your own identifying
17*2fae26bdSAlan Somers  * information: Portions Copyright [yyyy] [name of copyright owner]
18*2fae26bdSAlan Somers  *
19*2fae26bdSAlan Somers  * CDDL HEADER END
20*2fae26bdSAlan Somers  * $FreeBSD$
21*2fae26bdSAlan Somers  */
22*2fae26bdSAlan Somers 
23*2fae26bdSAlan Somers /*
24*2fae26bdSAlan Somers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25*2fae26bdSAlan Somers  * Use is subject to license terms.
26*2fae26bdSAlan Somers  */
27*2fae26bdSAlan Somers 
28*2fae26bdSAlan Somers #pragma ident	"@(#)devname2devid.c	1.3	07/05/25 SMI"
29*2fae26bdSAlan Somers 
30*2fae26bdSAlan Somers #include <sys/types.h>
31*2fae26bdSAlan Somers #include <sys/stat.h>
32*2fae26bdSAlan Somers #include <devid.h>
33*2fae26bdSAlan Somers #include <errno.h>
34*2fae26bdSAlan Somers #include <stdio.h>
35*2fae26bdSAlan Somers #include <stdlib.h>
36*2fae26bdSAlan Somers #include <fcntl.h>
37*2fae26bdSAlan Somers 
38*2fae26bdSAlan Somers /*
39*2fae26bdSAlan Somers  * Usage: devname2devid <devicepath>
40*2fae26bdSAlan Somers  *
41*2fae26bdSAlan Somers  * Examples:
42*2fae26bdSAlan Somers  *	# ./devname2devid /dev/dsk/c1t4d0s0
43*2fae26bdSAlan Somers  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/a
44*2fae26bdSAlan Somers  *	# ./devname2devid /dev/dsk/c1t4d0
45*2fae26bdSAlan Somers  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/wd
46*2fae26bdSAlan Somers  *	# ./devname2devid /dev/dsk/c1t4d0s1
47*2fae26bdSAlan Somers  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/b
48*2fae26bdSAlan Somers  *	#
49*2fae26bdSAlan Somers  *
50*2fae26bdSAlan Somers  * This program accepts a disk or disk slice path and prints a
51*2fae26bdSAlan Somers  * device id.
52*2fae26bdSAlan Somers  *
53*2fae26bdSAlan Somers  * Exit values:
54*2fae26bdSAlan Somers  *	0 - means success
55*2fae26bdSAlan Somers  *	1 - means failure
56*2fae26bdSAlan Somers  *
57*2fae26bdSAlan Somers  */
58*2fae26bdSAlan Somers int
59*2fae26bdSAlan Somers main(int argc, char *argv[])
60*2fae26bdSAlan Somers {
61*2fae26bdSAlan Somers 	int		fd;
62*2fae26bdSAlan Somers 	ddi_devid_t	devid;
63*2fae26bdSAlan Somers 	char		*minor_name, *devidstr, *device;
64*2fae26bdSAlan Somers #ifdef DEBUG
65*2fae26bdSAlan Somers 	devid_nmlist_t  *list = NULL;
66*2fae26bdSAlan Somers 	char		*search_path;
67*2fae26bdSAlan Somers 	int		i;
68*2fae26bdSAlan Somers #endif
69*2fae26bdSAlan Somers 
70*2fae26bdSAlan Somers 	if (argc == 1) {
71*2fae26bdSAlan Somers 		(void) printf("%s <devicepath> [search path]\n",
72*2fae26bdSAlan Somers 		    argv[0]);
73*2fae26bdSAlan Somers 		exit(1);
74*2fae26bdSAlan Somers 	}
75*2fae26bdSAlan Somers 	device = argv[1];
76*2fae26bdSAlan Somers 
77*2fae26bdSAlan Somers 	if ((fd = open(device, O_RDONLY|O_NDELAY)) < 0) {
78*2fae26bdSAlan Somers 		perror(device);
79*2fae26bdSAlan Somers 		exit(1);
80*2fae26bdSAlan Somers 	}
81*2fae26bdSAlan Somers 	if (devid_get(fd, &devid) != 0) {
82*2fae26bdSAlan Somers 		perror("devid_get");
83*2fae26bdSAlan Somers 		exit(1);
84*2fae26bdSAlan Somers 	}
85*2fae26bdSAlan Somers 	if (devid_get_minor_name(fd, &minor_name) != 0) {
86*2fae26bdSAlan Somers 		perror("devid_get_minor_name");
87*2fae26bdSAlan Somers 		exit(1);
88*2fae26bdSAlan Somers 	}
89*2fae26bdSAlan Somers 	if ((devidstr = devid_str_encode(devid, minor_name)) == 0) {
90*2fae26bdSAlan Somers 		perror("devid_str_encode");
91*2fae26bdSAlan Somers 		exit(1);
92*2fae26bdSAlan Somers 	}
93*2fae26bdSAlan Somers 
94*2fae26bdSAlan Somers 	(void) printf("devid %s\n", devidstr);
95*2fae26bdSAlan Somers 
96*2fae26bdSAlan Somers 	devid_str_free(devidstr);
97*2fae26bdSAlan Somers 
98*2fae26bdSAlan Somers #ifdef DEBUG
99*2fae26bdSAlan Somers 	if (argc == 3) {
100*2fae26bdSAlan Somers 		search_path = argv[2];
101*2fae26bdSAlan Somers 	} else {
102*2fae26bdSAlan Somers 		search_path = "/dev/rdsk";
103*2fae26bdSAlan Somers 	}
104*2fae26bdSAlan Somers 
105*2fae26bdSAlan Somers 	if (devid_deviceid_to_nmlist(search_path, devid, DEVID_MINOR_NAME_ALL,
106*2fae26bdSAlan Somers 	    &list)) {
107*2fae26bdSAlan Somers 		perror("devid_deviceid_to_nmlist");
108*2fae26bdSAlan Somers 		exit(1);
109*2fae26bdSAlan Somers 	}
110*2fae26bdSAlan Somers 
111*2fae26bdSAlan Somers 	/* loop through list and process device names and numbers */
112*2fae26bdSAlan Somers 	for (i = 0; list[i].devname != NULL; i++) {
113*2fae26bdSAlan Somers 		(void) printf("devname: %s %p\n", list[i].devname, list[i].dev);
114*2fae26bdSAlan Somers 	}
115*2fae26bdSAlan Somers 	devid_free_nmlist(list);
116*2fae26bdSAlan Somers 
117*2fae26bdSAlan Somers #endif /* DEBUG */
118*2fae26bdSAlan Somers 
119*2fae26bdSAlan Somers 	devid_str_free(minor_name);
120*2fae26bdSAlan Somers 	devid_free(devid);
121*2fae26bdSAlan Somers 
122*2fae26bdSAlan Somers 	return (0);
123*2fae26bdSAlan Somers }
124