Path: blob/main/tests/sys/cddl/zfs/bin/devname2devid.c
39536 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2007 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/252627#include <sys/types.h>28#include <sys/stat.h>29#include <devid.h>30#include <errno.h>31#include <stdio.h>32#include <stdlib.h>33#include <fcntl.h>3435/*36* Usage: devname2devid <devicepath>37*38* Examples:39* # ./devname2devid /dev/c1t4d0s040* devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/a41* # ./devname2devid /dev/c1t4d042* devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/wd43* # ./devname2devid /dev/c1t4d0s144* devid id1,sd@SSEAGATE_ST318404LSUN18G_3BT2G0Z300002146G4CR/b45* #46*47* This program accepts a disk or disk slice path and prints a48* device id.49*50* Exit values:51* 0 - means success52* 1 - means failure53*54*/55int56main(int argc, char *argv[])57{58int fd;59ddi_devid_t devid;60char *minor_name, *devidstr, *device;61#ifdef DEBUG62devid_nmlist_t *list = NULL;63char *search_path;64int i;65#endif6667if (argc == 1) {68(void) printf("%s <devicepath> [search path]\n",69argv[0]);70exit(1);71}72device = argv[1];7374if ((fd = open(device, O_RDONLY|O_NDELAY)) < 0) {75perror(device);76exit(1);77}78if (devid_get(fd, &devid) != 0) {79perror("devid_get");80exit(1);81}82if (devid_get_minor_name(fd, &minor_name) != 0) {83perror("devid_get_minor_name");84exit(1);85}86if ((devidstr = devid_str_encode(devid, minor_name)) == 0) {87perror("devid_str_encode");88exit(1);89}9091(void) printf("devid %s\n", devidstr);9293devid_str_free(devidstr);9495#ifdef DEBUG96if (argc == 3) {97search_path = argv[2];98} else {99search_path = "/dev/";100}101102if (devid_deviceid_to_nmlist(search_path, devid, DEVID_MINOR_NAME_ALL,103&list)) {104perror("devid_deviceid_to_nmlist");105exit(1);106}107108/* loop through list and process device names and numbers */109for (i = 0; list[i].devname != NULL; i++) {110(void) printf("devname: %s %p\n", list[i].devname, list[i].dev);111}112devid_free_nmlist(list);113114#endif /* DEBUG */115116devid_str_free(minor_name);117devid_free(devid);118119return (0);120}121122123