xref: /freebsd/tests/sys/cddl/zfs/bin/devname2devid.c (revision ac00d4d59b18a76c6148ca5d7439bb446d38da5c)
12fae26bdSAlan Somers /*
22fae26bdSAlan Somers  * CDDL HEADER START
32fae26bdSAlan Somers  *
42fae26bdSAlan Somers  * The contents of this file are subject to the terms of the
52fae26bdSAlan Somers  * Common Development and Distribution License (the "License").
62fae26bdSAlan Somers  * You may not use this file except in compliance with the License.
72fae26bdSAlan Somers  *
82fae26bdSAlan Somers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92fae26bdSAlan Somers  * or http://www.opensolaris.org/os/licensing.
102fae26bdSAlan Somers  * See the License for the specific language governing permissions
112fae26bdSAlan Somers  * and limitations under the License.
122fae26bdSAlan Somers  *
132fae26bdSAlan Somers  * When distributing Covered Code, include this CDDL HEADER in each
142fae26bdSAlan Somers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152fae26bdSAlan Somers  * If applicable, add the following below this CDDL HEADER, with the
162fae26bdSAlan Somers  * fields enclosed by brackets "[]" replaced with your own identifying
172fae26bdSAlan Somers  * information: Portions Copyright [yyyy] [name of copyright owner]
182fae26bdSAlan Somers  *
192fae26bdSAlan Somers  * CDDL HEADER END
202fae26bdSAlan Somers  */
212fae26bdSAlan Somers 
222fae26bdSAlan Somers /*
232fae26bdSAlan Somers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
242fae26bdSAlan Somers  * Use is subject to license terms.
252fae26bdSAlan Somers  */
262fae26bdSAlan Somers 
272fae26bdSAlan Somers 
282fae26bdSAlan Somers #include <sys/types.h>
292fae26bdSAlan Somers #include <sys/stat.h>
302fae26bdSAlan Somers #include <devid.h>
312fae26bdSAlan Somers #include <errno.h>
322fae26bdSAlan Somers #include <stdio.h>
332fae26bdSAlan Somers #include <stdlib.h>
342fae26bdSAlan Somers #include <fcntl.h>
352fae26bdSAlan Somers 
362fae26bdSAlan Somers /*
372fae26bdSAlan Somers  * Usage: devname2devid <devicepath>
382fae26bdSAlan Somers  *
392fae26bdSAlan Somers  * Examples:
40*5a2fc464SAndriy Gapon  *	# ./devname2devid /dev/c1t4d0s0
412fae26bdSAlan Somers  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/a
42*5a2fc464SAndriy Gapon  *	# ./devname2devid /dev/c1t4d0
432fae26bdSAlan Somers  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/wd
44*5a2fc464SAndriy Gapon  *	# ./devname2devid /dev/c1t4d0s1
452fae26bdSAlan Somers  *	devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/b
462fae26bdSAlan Somers  *	#
472fae26bdSAlan Somers  *
482fae26bdSAlan Somers  * This program accepts a disk or disk slice path and prints a
492fae26bdSAlan Somers  * device id.
502fae26bdSAlan Somers  *
512fae26bdSAlan Somers  * Exit values:
522fae26bdSAlan Somers  *	0 - means success
532fae26bdSAlan Somers  *	1 - means failure
542fae26bdSAlan Somers  *
552fae26bdSAlan Somers  */
562fae26bdSAlan Somers int
main(int argc,char * argv[])572fae26bdSAlan Somers main(int argc, char *argv[])
582fae26bdSAlan Somers {
592fae26bdSAlan Somers 	int		fd;
602fae26bdSAlan Somers 	ddi_devid_t	devid;
612fae26bdSAlan Somers 	char		*minor_name, *devidstr, *device;
622fae26bdSAlan Somers #ifdef DEBUG
632fae26bdSAlan Somers 	devid_nmlist_t  *list = NULL;
642fae26bdSAlan Somers 	char		*search_path;
652fae26bdSAlan Somers 	int		i;
662fae26bdSAlan Somers #endif
672fae26bdSAlan Somers 
682fae26bdSAlan Somers 	if (argc == 1) {
692fae26bdSAlan Somers 		(void) printf("%s <devicepath> [search path]\n",
702fae26bdSAlan Somers 		    argv[0]);
712fae26bdSAlan Somers 		exit(1);
722fae26bdSAlan Somers 	}
732fae26bdSAlan Somers 	device = argv[1];
742fae26bdSAlan Somers 
752fae26bdSAlan Somers 	if ((fd = open(device, O_RDONLY|O_NDELAY)) < 0) {
762fae26bdSAlan Somers 		perror(device);
772fae26bdSAlan Somers 		exit(1);
782fae26bdSAlan Somers 	}
792fae26bdSAlan Somers 	if (devid_get(fd, &devid) != 0) {
802fae26bdSAlan Somers 		perror("devid_get");
812fae26bdSAlan Somers 		exit(1);
822fae26bdSAlan Somers 	}
832fae26bdSAlan Somers 	if (devid_get_minor_name(fd, &minor_name) != 0) {
842fae26bdSAlan Somers 		perror("devid_get_minor_name");
852fae26bdSAlan Somers 		exit(1);
862fae26bdSAlan Somers 	}
872fae26bdSAlan Somers 	if ((devidstr = devid_str_encode(devid, minor_name)) == 0) {
882fae26bdSAlan Somers 		perror("devid_str_encode");
892fae26bdSAlan Somers 		exit(1);
902fae26bdSAlan Somers 	}
912fae26bdSAlan Somers 
922fae26bdSAlan Somers 	(void) printf("devid %s\n", devidstr);
932fae26bdSAlan Somers 
942fae26bdSAlan Somers 	devid_str_free(devidstr);
952fae26bdSAlan Somers 
962fae26bdSAlan Somers #ifdef DEBUG
972fae26bdSAlan Somers 	if (argc == 3) {
982fae26bdSAlan Somers 		search_path = argv[2];
992fae26bdSAlan Somers 	} else {
100*5a2fc464SAndriy Gapon 		search_path = "/dev/";
1012fae26bdSAlan Somers 	}
1022fae26bdSAlan Somers 
1032fae26bdSAlan Somers 	if (devid_deviceid_to_nmlist(search_path, devid, DEVID_MINOR_NAME_ALL,
1042fae26bdSAlan Somers 	    &list)) {
1052fae26bdSAlan Somers 		perror("devid_deviceid_to_nmlist");
1062fae26bdSAlan Somers 		exit(1);
1072fae26bdSAlan Somers 	}
1082fae26bdSAlan Somers 
1092fae26bdSAlan Somers 	/* loop through list and process device names and numbers */
1102fae26bdSAlan Somers 	for (i = 0; list[i].devname != NULL; i++) {
1112fae26bdSAlan Somers 		(void) printf("devname: %s %p\n", list[i].devname, list[i].dev);
1122fae26bdSAlan Somers 	}
1132fae26bdSAlan Somers 	devid_free_nmlist(list);
1142fae26bdSAlan Somers 
1152fae26bdSAlan Somers #endif /* DEBUG */
1162fae26bdSAlan Somers 
1172fae26bdSAlan Somers 	devid_str_free(minor_name);
1182fae26bdSAlan Somers 	devid_free(devid);
1192fae26bdSAlan Somers 
1202fae26bdSAlan Somers 	return (0);
1212fae26bdSAlan Somers }
122