xref: /illumos-gate/usr/src/cmd/prtvtoc/prtvtoc.c (revision 342440ec94087b8c751c580ab9ed6c693d31d418)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*342440ecSPrasad Singamsetty  * Common Development and Distribution License (the "License").
6*342440ecSPrasad Singamsetty  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984 AT&T	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
30*342440ecSPrasad Singamsetty  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
317c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * Print a disk partition map (volume table of contents, or VTOC).
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <fcntl.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <limits.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
497c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
507c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
517c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
527c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Assumes V_NUMPAR must be a power of 2.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * for V_NUMPAR = 8, we have
597c478bd9Sstevel@tonic-gate  * 	parttn(x)=(x & 0x07)	noparttn(x)=(x & 0x3fff8)
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * for V_NUMPAR = 16, we have
627c478bd9Sstevel@tonic-gate  * 	parttn(x)=(x & 0x0f)	noparttn(x)=(x & 0x3fff0)
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate #define	parttn(x)	(x % V_NUMPAR)
657c478bd9Sstevel@tonic-gate #define	noparttn(x)	(x & (MAXMIN & ~(V_NUMPAR-1)))
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Disk freespace structure.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate typedef struct {
717c478bd9Sstevel@tonic-gate 	u_longlong_t	fr_start;	/* Start of free space */
727c478bd9Sstevel@tonic-gate 	u_longlong_t	fr_size;	/* Length of free space */
737c478bd9Sstevel@tonic-gate } freemap_t;
747c478bd9Sstevel@tonic-gate 
75*342440ecSPrasad Singamsetty static	freemap_t	*findfree(struct dk_geom *, struct extvtoc *);
767c478bd9Sstevel@tonic-gate static	int	partcmp(const void *, const void *);
777c478bd9Sstevel@tonic-gate static	int	partcmp64(const void *, const void *);
787c478bd9Sstevel@tonic-gate static	int	prtvtoc(char *);
79*342440ecSPrasad Singamsetty static	void	putfree(struct extvtoc *, freemap_t *);
807c478bd9Sstevel@tonic-gate static	void	putfree64(struct dk_gpt *, freemap_t *);
81*342440ecSPrasad Singamsetty static	void	puttable(struct dk_geom *, struct extvtoc *, freemap_t *,
827c478bd9Sstevel@tonic-gate 			char *, char **);
837c478bd9Sstevel@tonic-gate static	void	puttable64(struct dk_gpt *, freemap_t *,
847c478bd9Sstevel@tonic-gate 			char *, char **);
857c478bd9Sstevel@tonic-gate static	int	readgeom(int, char *, struct dk_geom *);
86*342440ecSPrasad Singamsetty static	int	readvtoc(int, char *, struct extvtoc *);
877c478bd9Sstevel@tonic-gate static	int	readefi(int, char *, struct dk_gpt **);
887c478bd9Sstevel@tonic-gate static	void	usage(void);
897c478bd9Sstevel@tonic-gate static	int	warn(char *, char *);
907c478bd9Sstevel@tonic-gate static	char	*safe_strdup(char *);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * External variables.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate extern char	*getfullrawname();
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Static variables.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate static short	fflag;			/* Print freespace shell assignments */
1007c478bd9Sstevel@tonic-gate static short	hflag;			/* Omit headers */
1017c478bd9Sstevel@tonic-gate static short	sflag;			/* Omit all but the column header */
1027c478bd9Sstevel@tonic-gate static char	*fstab = VFSTAB;	/* Fstab pathname */
1037c478bd9Sstevel@tonic-gate static char	*mnttab = MNTTAB;	/* mnttab pathname */
1047c478bd9Sstevel@tonic-gate static char	*progname;		/* Last qualifier of arg0 */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate int
1077c478bd9Sstevel@tonic-gate main(int ac, char **av)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	int		idx;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (progname = strrchr(av[0], '/'))
1127c478bd9Sstevel@tonic-gate 		++progname;
1137c478bd9Sstevel@tonic-gate 	else
1147c478bd9Sstevel@tonic-gate 		progname = av[0];
1157c478bd9Sstevel@tonic-gate 	while ((idx = getopt(ac, av, "fhst:m:")) != -1)
1167c478bd9Sstevel@tonic-gate 		switch (idx) {
1177c478bd9Sstevel@tonic-gate 		case 'f':
1187c478bd9Sstevel@tonic-gate 			++fflag;
1197c478bd9Sstevel@tonic-gate 			break;
1207c478bd9Sstevel@tonic-gate 		case 'h':
1217c478bd9Sstevel@tonic-gate 			++hflag;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 		case 's':
1247c478bd9Sstevel@tonic-gate 			++sflag;
1257c478bd9Sstevel@tonic-gate 			break;
1267c478bd9Sstevel@tonic-gate 		case 't':
1277c478bd9Sstevel@tonic-gate 			fstab = optarg;
1287c478bd9Sstevel@tonic-gate 			break;
1297c478bd9Sstevel@tonic-gate 		case 'm':
1307c478bd9Sstevel@tonic-gate 			mnttab = optarg;
1317c478bd9Sstevel@tonic-gate 			break;
1327c478bd9Sstevel@tonic-gate 		default:
1337c478bd9Sstevel@tonic-gate 			usage();
1347c478bd9Sstevel@tonic-gate 		}
1357c478bd9Sstevel@tonic-gate 	if (optind >= ac)
1367c478bd9Sstevel@tonic-gate 		usage();
1377c478bd9Sstevel@tonic-gate 	idx = 0;
1387c478bd9Sstevel@tonic-gate 	while (optind < ac)
1397c478bd9Sstevel@tonic-gate 		idx |= prtvtoc(av[optind++]);
1407c478bd9Sstevel@tonic-gate 	return (idx == 0 ? 0 : 1);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static freemap_t	*freemap;
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * findfree(): Find free space on a disk.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate static freemap_t *
148*342440ecSPrasad Singamsetty findfree(struct dk_geom *geom, struct extvtoc *vtoc)
1497c478bd9Sstevel@tonic-gate {
150*342440ecSPrasad Singamsetty 	struct extpartition	*part;
151*342440ecSPrasad Singamsetty 	struct extpartition	**list;
1527c478bd9Sstevel@tonic-gate 	freemap_t		*freeidx;
153*342440ecSPrasad Singamsetty 	diskaddr_t		fullsize;
1547c478bd9Sstevel@tonic-gate 	ulong_t			cylsize;
155*342440ecSPrasad Singamsetty 	struct extpartition	*sorted[V_NUMPAR + 1];
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	freemap = calloc(sizeof (freemap_t), V_NUMPAR + 1);
1587c478bd9Sstevel@tonic-gate 	cylsize  = (geom->dkg_nsect) * (geom->dkg_nhead);
159*342440ecSPrasad Singamsetty 	fullsize = (diskaddr_t)(geom->dkg_ncyl) * cylsize;
1607c478bd9Sstevel@tonic-gate 	if (vtoc->v_nparts > V_NUMPAR) {
1617c478bd9Sstevel@tonic-gate 		(void) warn("putfree()", "Too many partitions on disk!");
1627c478bd9Sstevel@tonic-gate 		exit(1);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	list = sorted;
1657c478bd9Sstevel@tonic-gate 	for (part = vtoc->v_part; part < vtoc->v_part + vtoc->v_nparts; ++part)
1667c478bd9Sstevel@tonic-gate 		if (part->p_size && part->p_tag != V_BACKUP)
1677c478bd9Sstevel@tonic-gate 			*list++ = part;
1687c478bd9Sstevel@tonic-gate 	*list = 0;
1697c478bd9Sstevel@tonic-gate 	qsort((char *)sorted, (uint_t)(list - sorted),
1707c478bd9Sstevel@tonic-gate 		sizeof (*sorted), partcmp);
1717c478bd9Sstevel@tonic-gate 	freeidx = freemap;
1727c478bd9Sstevel@tonic-gate 	freeidx->fr_start = 0;
1737c478bd9Sstevel@tonic-gate 	for (list = sorted; (part = *list) != NULL; ++list)
1747c478bd9Sstevel@tonic-gate 		if (part->p_start <= freeidx->fr_start)
1757c478bd9Sstevel@tonic-gate 			freeidx->fr_start += part->p_size;
1767c478bd9Sstevel@tonic-gate 		else {
1777c478bd9Sstevel@tonic-gate 			freeidx->fr_size = part->p_start - freeidx->fr_start;
1787c478bd9Sstevel@tonic-gate 			(++freeidx)->fr_start = part->p_start + part->p_size;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 	if (freeidx->fr_start < fullsize) {
1817c478bd9Sstevel@tonic-gate 		freeidx->fr_size = fullsize - freeidx->fr_start;
1827c478bd9Sstevel@tonic-gate 		++freeidx;
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	freeidx->fr_start = freeidx->fr_size = 0;
1857c478bd9Sstevel@tonic-gate 	return (freemap);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * findfree64(): Find free space on a disk.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate static freemap_t *
1927c478bd9Sstevel@tonic-gate findfree64(struct dk_gpt *efi)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	struct dk_part		*part;
1957c478bd9Sstevel@tonic-gate 	struct dk_part		**list;
1967c478bd9Sstevel@tonic-gate 	freemap_t		*freeidx;
1977c478bd9Sstevel@tonic-gate 	diskaddr_t		fullsize;
1987c478bd9Sstevel@tonic-gate 	struct dk_part		**sorted;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	freemap = calloc(sizeof (freemap_t), efi->efi_nparts + 1);
2017c478bd9Sstevel@tonic-gate 	sorted = calloc(sizeof (struct dk_part), efi->efi_nparts + 1);
2027c478bd9Sstevel@tonic-gate 	fullsize = efi->efi_last_u_lba;
2037c478bd9Sstevel@tonic-gate 	list = sorted;
2047c478bd9Sstevel@tonic-gate 	for (part = efi->efi_parts;
2057c478bd9Sstevel@tonic-gate 	    part < efi->efi_parts + efi->efi_nparts;
2067c478bd9Sstevel@tonic-gate 	    ++part)
2077c478bd9Sstevel@tonic-gate 		if (part->p_size && part->p_tag != V_BACKUP)
2087c478bd9Sstevel@tonic-gate 			*list++ = part;
2097c478bd9Sstevel@tonic-gate 	*list = 0;
2107c478bd9Sstevel@tonic-gate 	qsort((char *)sorted, (uint_t)(list - sorted),
2117c478bd9Sstevel@tonic-gate 		sizeof (*sorted), partcmp64);
2127c478bd9Sstevel@tonic-gate 	freeidx = freemap;
2137c478bd9Sstevel@tonic-gate 	freeidx->fr_start = efi->efi_first_u_lba;
2147c478bd9Sstevel@tonic-gate 	for (list = sorted; (part = *list) != NULL; ++list)
2157c478bd9Sstevel@tonic-gate 		if (part->p_start == freeidx->fr_start)
2167c478bd9Sstevel@tonic-gate 			freeidx->fr_start += part->p_size;
2177c478bd9Sstevel@tonic-gate 		else {
2187c478bd9Sstevel@tonic-gate 			freeidx->fr_size = part->p_start - freeidx->fr_start;
2197c478bd9Sstevel@tonic-gate 			(++freeidx)->fr_start = part->p_start + part->p_size;
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 	if (freeidx->fr_start < fullsize) {
2227c478bd9Sstevel@tonic-gate 		freeidx->fr_size = fullsize - freeidx->fr_start;
2237c478bd9Sstevel@tonic-gate 		++freeidx;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 	freeidx->fr_start = freeidx->fr_size = 0;
2267c478bd9Sstevel@tonic-gate 	return (freemap);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * getmntpt()
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  * Get the filesystem mountpoint of each partition on the disk
2337c478bd9Sstevel@tonic-gate  * from the fstab or mnttab. Returns a pointer to an array of pointers to
2347c478bd9Sstevel@tonic-gate  * directory names (indexed by partition number).
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate static char **
2377c478bd9Sstevel@tonic-gate getmntpt(major_t slot, minor_t nopartminor)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	int idx;
2407c478bd9Sstevel@tonic-gate 	FILE *file;
2417c478bd9Sstevel@tonic-gate 	char devbuf[PATH_MAX], *item;
2427c478bd9Sstevel@tonic-gate 	static char *list[V_NUMPAR];
2437c478bd9Sstevel@tonic-gate 	struct stat sb;
2447c478bd9Sstevel@tonic-gate 	struct mnttab mtab;
2457c478bd9Sstevel@tonic-gate 	struct vfstab vtab;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < V_NUMPAR; ++idx)
2487c478bd9Sstevel@tonic-gate 		list[idx] = NULL;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* read mnttab for partition mountpoints */
2517c478bd9Sstevel@tonic-gate 	if ((file = fopen(mnttab, "r")) == NULL) {
2527c478bd9Sstevel@tonic-gate 		(void) warn(mnttab, strerror(errno));
2537c478bd9Sstevel@tonic-gate 	} else {
2547c478bd9Sstevel@tonic-gate 		while (getmntent(file, &mtab) == 0) {
2557c478bd9Sstevel@tonic-gate 			item = mtab.mnt_special;
2567c478bd9Sstevel@tonic-gate 			if ((item == NULL) || (mtab.mnt_mountp == NULL))
2577c478bd9Sstevel@tonic-gate 				continue;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 			/*
2607c478bd9Sstevel@tonic-gate 			 * Is it from /dev?
2617c478bd9Sstevel@tonic-gate 			 */
2627c478bd9Sstevel@tonic-gate 			if (strncmp(item, "/dev/", strlen("/dev/") != 0))
2637c478bd9Sstevel@tonic-gate 				continue;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 			/*
2667c478bd9Sstevel@tonic-gate 			 * Is it a character device?
2677c478bd9Sstevel@tonic-gate 			 */
2687c478bd9Sstevel@tonic-gate 			(void) snprintf(devbuf, sizeof (devbuf), "/dev/r%s",
2697c478bd9Sstevel@tonic-gate 			    item + strlen("/dev/"));
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 			if ((stat(devbuf, &sb) != 0) ||
2727c478bd9Sstevel@tonic-gate 			    ((sb.st_mode & S_IFMT) != S_IFCHR))
2737c478bd9Sstevel@tonic-gate 				continue;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 			/*
2767c478bd9Sstevel@tonic-gate 			 * device must match input slot and nopartminor
2777c478bd9Sstevel@tonic-gate 			 */
2787c478bd9Sstevel@tonic-gate 			if ((major(sb.st_rdev) != slot) ||
2797c478bd9Sstevel@tonic-gate 			    (noparttn(minor(sb.st_rdev)) != nopartminor))
2807c478bd9Sstevel@tonic-gate 				continue;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 			list[parttn(minor(sb.st_rdev))] =
2837c478bd9Sstevel@tonic-gate 			    safe_strdup(mtab.mnt_mountp);
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 		(void) fclose(file);
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if ((file = fopen(fstab, "r")) == NULL) {
2897c478bd9Sstevel@tonic-gate 		(void) warn(fstab, strerror(errno));
2907c478bd9Sstevel@tonic-gate 		return (list);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * Look for the disk in the vfstab so that we can report its mount
2957c478bd9Sstevel@tonic-gate 	 * point even if it isn't currently mounted.
2967c478bd9Sstevel@tonic-gate 	 */
2977c478bd9Sstevel@tonic-gate 	while (getvfsent(file, &vtab) == 0) {
2987c478bd9Sstevel@tonic-gate 		item = vtab.vfs_special;
2997c478bd9Sstevel@tonic-gate 		if ((item == NULL) || (vtab.vfs_mountp == NULL))
3007c478bd9Sstevel@tonic-gate 			continue;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		if (strncmp(item, "/dev/", strlen("/dev/")) != 0)
3037c478bd9Sstevel@tonic-gate 			continue;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		/*
3067c478bd9Sstevel@tonic-gate 		 * Is it a character device?
3077c478bd9Sstevel@tonic-gate 		 */
3087c478bd9Sstevel@tonic-gate 		(void) snprintf(devbuf, sizeof (devbuf), "/dev/r%s",
3097c478bd9Sstevel@tonic-gate 		    item + strlen("/dev/"));
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		if ((stat(devbuf, &sb) != 0) ||
3127c478bd9Sstevel@tonic-gate 		    ((sb.st_mode & S_IFMT) != S_IFCHR))
3137c478bd9Sstevel@tonic-gate 			continue;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		/*
3167c478bd9Sstevel@tonic-gate 		 * device must match input slot and nopartminor
3177c478bd9Sstevel@tonic-gate 		 */
3187c478bd9Sstevel@tonic-gate 		if ((major(sb.st_rdev) != slot) ||
3197c478bd9Sstevel@tonic-gate 		    (noparttn(minor(sb.st_rdev)) != nopartminor))
3207c478bd9Sstevel@tonic-gate 			continue;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		/*
3237c478bd9Sstevel@tonic-gate 		 * use mnttab entry if both tables have entries
3247c478bd9Sstevel@tonic-gate 		 */
3257c478bd9Sstevel@tonic-gate 		if (list[parttn(minor(sb.st_rdev))] != NULL)
3267c478bd9Sstevel@tonic-gate 			continue;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		list[parttn(minor(sb.st_rdev))] = safe_strdup(vtab.vfs_mountp);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	(void) fclose(file);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	return (list);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * partcmp(): Qsort() key comparison of partitions by starting sector numbers.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate static int
3397c478bd9Sstevel@tonic-gate partcmp(const void *one, const void *two)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	return ((*(struct partition **)one)->p_start -
3427c478bd9Sstevel@tonic-gate 		(*(struct partition **)two)->p_start);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate static int
3467c478bd9Sstevel@tonic-gate partcmp64(const void *one, const void *two)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	if ((*(struct dk_part **)one)->p_start >
3497c478bd9Sstevel@tonic-gate 		(*(struct dk_part **)two)->p_start)
3507c478bd9Sstevel@tonic-gate 	    return (1);
3517c478bd9Sstevel@tonic-gate 	else if ((*(struct dk_part **)one)->p_start <
3527c478bd9Sstevel@tonic-gate 		(*(struct dk_part **)two)->p_start)
3537c478bd9Sstevel@tonic-gate 	    return (-1);
3547c478bd9Sstevel@tonic-gate 	else
3557c478bd9Sstevel@tonic-gate 	    return (0);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * prtvtoc(): Read and print a VTOC.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate static int
3637c478bd9Sstevel@tonic-gate prtvtoc(char *devname)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	int		fd;
3667c478bd9Sstevel@tonic-gate 	int		idx;
3677c478bd9Sstevel@tonic-gate 	freemap_t	*freemap;
3687c478bd9Sstevel@tonic-gate 	struct stat	sb;
369*342440ecSPrasad Singamsetty 	struct extvtoc	vtoc;
3707c478bd9Sstevel@tonic-gate 	int		geo;
3717c478bd9Sstevel@tonic-gate 	struct dk_geom	geom;
3727c478bd9Sstevel@tonic-gate 	char		*name;
3737c478bd9Sstevel@tonic-gate 	int		newvtoc = 0;
3747c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efi;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	name = getfullrawname(devname);
3777c478bd9Sstevel@tonic-gate 	if (name == NULL)
3787c478bd9Sstevel@tonic-gate 		return (warn(devname,
3797c478bd9Sstevel@tonic-gate 		    "internal administrative call (getfullrawname) failed"));
3807c478bd9Sstevel@tonic-gate 	if (strcmp(name, "") == 0)
3817c478bd9Sstevel@tonic-gate 		name = devname;
3827c478bd9Sstevel@tonic-gate 	if ((fd = open(name, O_NONBLOCK|O_RDONLY)) < 0)
3837c478bd9Sstevel@tonic-gate 		return (warn(name, strerror(errno)));
3847c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) < 0)
3857c478bd9Sstevel@tonic-gate 		return (warn(name, strerror(errno)));
3867c478bd9Sstevel@tonic-gate 	if ((sb.st_mode & S_IFMT) != S_IFCHR)
3877c478bd9Sstevel@tonic-gate 		return (warn(name, "Not a raw device"));
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	geo = (readgeom(fd, name, &geom) == 0);
3907c478bd9Sstevel@tonic-gate 	if (geo) {
3917c478bd9Sstevel@tonic-gate 		if ((idx = readvtoc(fd, name, &vtoc)) == VT_ENOTSUP) {
3927c478bd9Sstevel@tonic-gate 			idx = (readefi(fd, name, &efi) == 0);
3937c478bd9Sstevel@tonic-gate 			newvtoc = 1;
3947c478bd9Sstevel@tonic-gate 		} else
3957c478bd9Sstevel@tonic-gate 			idx = (idx == 0);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 	(void) close(fd);
3987c478bd9Sstevel@tonic-gate 	if ((!geo) || (!idx))
3997c478bd9Sstevel@tonic-gate 		return (-1);
4007c478bd9Sstevel@tonic-gate 	if (!newvtoc)
4017c478bd9Sstevel@tonic-gate 		freemap = findfree(&geom, &vtoc);
4027c478bd9Sstevel@tonic-gate 	else
4037c478bd9Sstevel@tonic-gate 		freemap = findfree64(efi);
4047c478bd9Sstevel@tonic-gate 	if (fflag) {
4057c478bd9Sstevel@tonic-gate 		if (!newvtoc)
4067c478bd9Sstevel@tonic-gate 			putfree(&vtoc, freemap);
4077c478bd9Sstevel@tonic-gate 		else
4087c478bd9Sstevel@tonic-gate 			putfree64(efi, freemap);
4097c478bd9Sstevel@tonic-gate 	} else {
4107c478bd9Sstevel@tonic-gate 		if (!newvtoc)
4117c478bd9Sstevel@tonic-gate 			puttable(&geom, &vtoc, freemap, devname,
4127c478bd9Sstevel@tonic-gate 			    getmntpt(major(sb.st_rdev),
4137c478bd9Sstevel@tonic-gate 			    noparttn(minor(sb.st_rdev))));
4147c478bd9Sstevel@tonic-gate 		else
4157c478bd9Sstevel@tonic-gate 			puttable64(efi, freemap, devname,
4167c478bd9Sstevel@tonic-gate 			    getmntpt(major(sb.st_rdev),
4177c478bd9Sstevel@tonic-gate 			    noparttn(minor(sb.st_rdev))));
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 	if (newvtoc)
4207c478bd9Sstevel@tonic-gate 		efi_free(efi);
4217c478bd9Sstevel@tonic-gate 	return (0);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate  * putfree():
4267c478bd9Sstevel@tonic-gate  *
4277c478bd9Sstevel@tonic-gate  * Print shell assignments for disk free space. FREE_START and FREE_SIZE
4287c478bd9Sstevel@tonic-gate  * represent the starting block and number of blocks of the first chunk
4297c478bd9Sstevel@tonic-gate  * of free space. FREE_PART lists the unassigned partitions.
4307c478bd9Sstevel@tonic-gate  */
4317c478bd9Sstevel@tonic-gate static void
432*342440ecSPrasad Singamsetty putfree(struct extvtoc *vtoc, freemap_t *freemap)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	freemap_t *freeidx;
4357c478bd9Sstevel@tonic-gate 	ushort_t idx;
4367c478bd9Sstevel@tonic-gate 	int free_count = 0;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	for (freeidx = freemap; freeidx->fr_size; ++freeidx)
4397c478bd9Sstevel@tonic-gate 		free_count++;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	(void) printf("FREE_START=%llu FREE_SIZE=%llu FREE_COUNT=%d FREE_PART=",
4427c478bd9Sstevel@tonic-gate 	    freemap->fr_start, freemap->fr_size, free_count);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < vtoc->v_nparts; ++idx) {
4457c478bd9Sstevel@tonic-gate 		if (vtoc->v_part[idx].p_size == 0 && idx != 2)
4467c478bd9Sstevel@tonic-gate 			(void) printf("%x", idx);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	(void) printf("\n");
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate static void
4527c478bd9Sstevel@tonic-gate putfree64(struct dk_gpt *efi, freemap_t *freemap)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	freemap_t *freeidx;
4557c478bd9Sstevel@tonic-gate 	ushort_t idx;
4567c478bd9Sstevel@tonic-gate 	int free_count = 0;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	for (freeidx = freemap; freeidx->fr_size; ++freeidx)
4597c478bd9Sstevel@tonic-gate 		free_count++;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	(void) printf("FREE_START=%llu FREE_SIZE=%llu FREE_COUNT=%d FREE_PART=",
4627c478bd9Sstevel@tonic-gate 	    freemap->fr_start, freemap->fr_size, free_count);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < efi->efi_nparts; ++idx) {
4657c478bd9Sstevel@tonic-gate 		if (efi->efi_parts[idx].p_size == 0 && idx != 2)
4667c478bd9Sstevel@tonic-gate 			(void) printf("%x", idx);
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	(void) printf("\n");
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * puttable(): Print a human-readable VTOC.
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate static void
475*342440ecSPrasad Singamsetty puttable(struct dk_geom *geom, struct extvtoc *vtoc, freemap_t *freemap,
4767c478bd9Sstevel@tonic-gate     char *name, char **mtab)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	ushort_t	idx;
4797c478bd9Sstevel@tonic-gate 	ulong_t	cylsize;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	cylsize = (geom->dkg_nsect) * (geom->dkg_nhead);
4827c478bd9Sstevel@tonic-gate 	if (!hflag && !sflag) {
4837c478bd9Sstevel@tonic-gate 		(void) printf("* %s", name);
4847c478bd9Sstevel@tonic-gate 		if (*vtoc->v_volume)
4857c478bd9Sstevel@tonic-gate 			(void) printf(" (volume \"%.8s\")", vtoc->v_volume);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		(void) printf(" partition map\n");
4887c478bd9Sstevel@tonic-gate 		(void) printf("*\n* Dimensions:\n");
4897c478bd9Sstevel@tonic-gate 		(void) printf("* %7u bytes/sector\n", vtoc->v_sectorsz);
4907c478bd9Sstevel@tonic-gate 		(void) printf("* %7u sectors/track\n", geom->dkg_nsect);
4917c478bd9Sstevel@tonic-gate 		(void) printf("* %7u tracks/cylinder\n", geom->dkg_nhead);
4927c478bd9Sstevel@tonic-gate 		(void) printf("* %7lu sectors/cylinder\n", cylsize);
4937c478bd9Sstevel@tonic-gate 		(void) printf("* %7u cylinders\n", geom->dkg_pcyl);
4947c478bd9Sstevel@tonic-gate 		(void) printf("* %7u accessible cylinders\n", geom->dkg_ncyl);
4957c478bd9Sstevel@tonic-gate 		(void) printf("*\n* Flags:\n");
4967c478bd9Sstevel@tonic-gate 		(void) printf("*   1: unmountable\n");
4977c478bd9Sstevel@tonic-gate 		(void) printf("*  10: read-only\n*\n");
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		if (freemap->fr_size) {
5007c478bd9Sstevel@tonic-gate 			(void) printf("* Unallocated space:\n");
5017c478bd9Sstevel@tonic-gate 			(void) printf("*\tFirst     Sector    Last\n");
5027c478bd9Sstevel@tonic-gate 			(void) printf("*\tSector     Count    Sector \n");
5037c478bd9Sstevel@tonic-gate 			do {
5047c478bd9Sstevel@tonic-gate 				(void) printf("*   %9llu %9llu %9llu\n",
5057c478bd9Sstevel@tonic-gate 				    freemap->fr_start, freemap->fr_size,
5067c478bd9Sstevel@tonic-gate 				    freemap->fr_size + freemap->fr_start - 1);
5077c478bd9Sstevel@tonic-gate 			} while ((++freemap)->fr_size);
5087c478bd9Sstevel@tonic-gate 			(void) printf("*\n");
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 	if (!hflag)  {
5127c478bd9Sstevel@tonic-gate 		(void) printf(\
5137c478bd9Sstevel@tonic-gate "*                          First     Sector    Last\n"
5147c478bd9Sstevel@tonic-gate "* Partition  Tag  Flags    Sector     Count    Sector  Mount Directory\n");
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < vtoc->v_nparts; ++idx) {
5177c478bd9Sstevel@tonic-gate 		if (vtoc->v_part[idx].p_size == 0)
5187c478bd9Sstevel@tonic-gate 			continue;
519*342440ecSPrasad Singamsetty 		(void) printf("      %2u  %5u    %02x  %9llu %9llu %9llu",
5207c478bd9Sstevel@tonic-gate 		    idx, vtoc->v_part[idx].p_tag, vtoc->v_part[idx].p_flag,
5217c478bd9Sstevel@tonic-gate 		    vtoc->v_part[idx].p_start, vtoc->v_part[idx].p_size,
5227c478bd9Sstevel@tonic-gate 		    vtoc->v_part[idx].p_start + vtoc->v_part[idx].p_size - 1);
5237c478bd9Sstevel@tonic-gate 		if (mtab && mtab[idx])
5247c478bd9Sstevel@tonic-gate 			(void) printf("   %s", mtab[idx]);
5257c478bd9Sstevel@tonic-gate 		(void) printf("\n");
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate  * puttable(): Print a human-readable VTOC.
5317c478bd9Sstevel@tonic-gate  */
5327c478bd9Sstevel@tonic-gate static void
5337c478bd9Sstevel@tonic-gate puttable64(struct dk_gpt *efi, freemap_t *freemap, char *name,
5347c478bd9Sstevel@tonic-gate 	char **mtab)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	ushort_t	idx;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (!hflag && !sflag) {
5397c478bd9Sstevel@tonic-gate 		(void) printf("* %s", name);
5407c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < efi->efi_nparts; idx++)
5417c478bd9Sstevel@tonic-gate 		    if (efi->efi_parts[idx].p_tag == V_RESERVED &&
5427c478bd9Sstevel@tonic-gate 			*efi->efi_parts[idx].p_name)
5437c478bd9Sstevel@tonic-gate 			    (void) printf(" (volume \"%.8s\")",
5447c478bd9Sstevel@tonic-gate 				    efi->efi_parts[idx].p_name);
5457c478bd9Sstevel@tonic-gate 		(void) printf(" partition map\n");
5467c478bd9Sstevel@tonic-gate 		(void) printf("*\n* Dimensions:\n");
5477c478bd9Sstevel@tonic-gate 		(void) printf("* %7u bytes/sector\n", efi->efi_lbasize);
5487c478bd9Sstevel@tonic-gate 		(void) printf("* %llu sectors\n", efi->efi_last_lba + 1);
5497c478bd9Sstevel@tonic-gate 		(void) printf("* %llu accessible sectors\n",
5507c478bd9Sstevel@tonic-gate 		    efi->efi_last_u_lba - efi->efi_first_u_lba + 1);
5517c478bd9Sstevel@tonic-gate 		(void) printf("*\n* Flags:\n");
5527c478bd9Sstevel@tonic-gate 		(void) printf("*   1: unmountable\n");
5537c478bd9Sstevel@tonic-gate 		(void) printf("*  10: read-only\n*\n");
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		if (freemap->fr_size) {
5567c478bd9Sstevel@tonic-gate 			(void) printf("* Unallocated space:\n");
5577c478bd9Sstevel@tonic-gate 			(void) printf("*\tFirst     Sector    Last\n");
5587c478bd9Sstevel@tonic-gate 			(void) printf("*\tSector     Count    Sector \n");
5597c478bd9Sstevel@tonic-gate 			do {
5607c478bd9Sstevel@tonic-gate 				(void) printf("*   %9llu %9llu %9llu\n",
5617c478bd9Sstevel@tonic-gate 				    freemap->fr_start, freemap->fr_size,
5627c478bd9Sstevel@tonic-gate 				    freemap->fr_size + freemap->fr_start - 1);
5637c478bd9Sstevel@tonic-gate 			} while ((++freemap)->fr_size);
5647c478bd9Sstevel@tonic-gate 			(void) printf("*\n");
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	if (!hflag)  {
5687c478bd9Sstevel@tonic-gate 		(void) printf(\
5697c478bd9Sstevel@tonic-gate "*                          First     Sector    Last\n"
5707c478bd9Sstevel@tonic-gate "* Partition  Tag  Flags    Sector     Count    Sector  Mount Directory\n");
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < efi->efi_nparts; ++idx) {
5737c478bd9Sstevel@tonic-gate 	    if (efi->efi_parts[idx].p_size == 0)
5747c478bd9Sstevel@tonic-gate 		    continue;
5757c478bd9Sstevel@tonic-gate 	    (void) printf("      %2u  %5u    %02x  %9llu %9llu %9llu",
5767c478bd9Sstevel@tonic-gate 		idx, efi->efi_parts[idx].p_tag, efi->efi_parts[idx].p_flag,
5777c478bd9Sstevel@tonic-gate 		efi->efi_parts[idx].p_start, efi->efi_parts[idx].p_size,
5787c478bd9Sstevel@tonic-gate 		efi->efi_parts[idx].p_start + efi->efi_parts[idx].p_size - 1);
5797c478bd9Sstevel@tonic-gate 	    if ((idx < 7) && mtab && mtab[idx])
5807c478bd9Sstevel@tonic-gate 		    (void) printf("   %s", mtab[idx]);
5817c478bd9Sstevel@tonic-gate 	    (void) printf("\n");
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * readgeom(): Read the disk geometry.
5877c478bd9Sstevel@tonic-gate  */
5887c478bd9Sstevel@tonic-gate static int
5897c478bd9Sstevel@tonic-gate readgeom(int fd, char *name, struct dk_geom *geom)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	char err_string[128];
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if ((ioctl(fd, DKIOCGGEOM, geom) < 0) && (errno != ENOTSUP)) {
5947c478bd9Sstevel@tonic-gate 		(void) sprintf(err_string,
5957c478bd9Sstevel@tonic-gate 		    "Unable to read Disk geometry errno = 0x%x",
5967c478bd9Sstevel@tonic-gate 		    errno);
5977c478bd9Sstevel@tonic-gate 		return (warn(name, err_string));
5987c478bd9Sstevel@tonic-gate 	} else if (errno == ENOTSUP) {
5997c478bd9Sstevel@tonic-gate 		(void) memset(geom, 0, sizeof (struct dk_geom));
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	return (0);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate /*
6057c478bd9Sstevel@tonic-gate  * readvtoc(): Read a partition map.
6067c478bd9Sstevel@tonic-gate  */
6077c478bd9Sstevel@tonic-gate static int
608*342440ecSPrasad Singamsetty readvtoc(int fd, char *name, struct extvtoc *vtoc)
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	int	retval;
6117c478bd9Sstevel@tonic-gate 
612*342440ecSPrasad Singamsetty 	if ((retval = read_extvtoc(fd, vtoc)) >= 0)
6137c478bd9Sstevel@tonic-gate 		return (0);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	switch (retval) {
6167c478bd9Sstevel@tonic-gate 	case (VT_EIO):
6177c478bd9Sstevel@tonic-gate 		return (warn(name, "Unable to read VTOC"));
6187c478bd9Sstevel@tonic-gate 	case (VT_EINVAL):
6197c478bd9Sstevel@tonic-gate 		return (warn(name, "Invalid VTOC"));
6207c478bd9Sstevel@tonic-gate 	case (VT_ERROR):
6217c478bd9Sstevel@tonic-gate 		return (warn(name, "Unknown problem reading VTOC"));
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 	return (retval);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate  * readefi(): Read a partition map.
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate static int
6307c478bd9Sstevel@tonic-gate readefi(int fd, char *name, struct dk_gpt **efi)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	int	retval;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if ((retval = efi_alloc_and_read(fd, efi)) >= 0)
6357c478bd9Sstevel@tonic-gate 		return (0);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	switch (retval) {
6387c478bd9Sstevel@tonic-gate 	case (VT_EIO):
6397c478bd9Sstevel@tonic-gate 		return (warn(name, "Unable to read VTOC"));
6407c478bd9Sstevel@tonic-gate 	case (VT_EINVAL):
6417c478bd9Sstevel@tonic-gate 		return (warn(name, "Invalid VTOC"));
6427c478bd9Sstevel@tonic-gate 	case (VT_ERROR):
6437c478bd9Sstevel@tonic-gate 		return (warn(name, "Unknown problem reading VTOC"));
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	return (retval);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate static char *
6497c478bd9Sstevel@tonic-gate safe_strdup(char *str)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	char *ret;
6527c478bd9Sstevel@tonic-gate 	if ((ret = strdup(str)) == NULL) {
6537c478bd9Sstevel@tonic-gate 		(void) warn("memory allocation", strerror(errno));
6547c478bd9Sstevel@tonic-gate 		exit(1);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 	return (ret);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * usage(): Print a helpful message and exit.
6617c478bd9Sstevel@tonic-gate  */
6627c478bd9Sstevel@tonic-gate static void
6637c478bd9Sstevel@tonic-gate usage()
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage:\t%s [ -fhs ] [ -t fstab ] [ -m mnttab ] "
6667c478bd9Sstevel@tonic-gate 	    "rawdisk ...\n", progname);
6677c478bd9Sstevel@tonic-gate 	exit(1);
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate /*
6717c478bd9Sstevel@tonic-gate  * warn(): Print an error message. Always returns -1.
6727c478bd9Sstevel@tonic-gate  */
6737c478bd9Sstevel@tonic-gate static int
6747c478bd9Sstevel@tonic-gate warn(char *what, char *why)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", progname, what, why);
6777c478bd9Sstevel@tonic-gate 	return (-1);
6787c478bd9Sstevel@tonic-gate }
679