xref: /titanic_54/usr/src/cmd/prtvtoc/prtvtoc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984 AT&T	*/
27*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
32*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * Print a disk partition map (volume table of contents, or VTOC).
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <errno.h>
42*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
43*7c478bd9Sstevel@tonic-gate #include <unistd.h>
44*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate #include <stdio.h>
47*7c478bd9Sstevel@tonic-gate #include <limits.h>
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
58*7c478bd9Sstevel@tonic-gate /*
59*7c478bd9Sstevel@tonic-gate  * Assumes V_NUMPAR must be a power of 2.
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  * for V_NUMPAR = 8, we have
62*7c478bd9Sstevel@tonic-gate  * 	parttn(x)=(x & 0x07)	noparttn(x)=(x & 0x3fff8)
63*7c478bd9Sstevel@tonic-gate  *
64*7c478bd9Sstevel@tonic-gate  * for V_NUMPAR = 16, we have
65*7c478bd9Sstevel@tonic-gate  * 	parttn(x)=(x & 0x0f)	noparttn(x)=(x & 0x3fff0)
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate #define	parttn(x)	(x % V_NUMPAR)
68*7c478bd9Sstevel@tonic-gate #define	noparttn(x)	(x & (MAXMIN & ~(V_NUMPAR-1)))
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate  * Disk freespace structure.
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate typedef struct {
74*7c478bd9Sstevel@tonic-gate 	u_longlong_t	fr_start;	/* Start of free space */
75*7c478bd9Sstevel@tonic-gate 	u_longlong_t	fr_size;	/* Length of free space */
76*7c478bd9Sstevel@tonic-gate } freemap_t;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate static	freemap_t	*findfree(struct dk_geom *, struct vtoc *);
79*7c478bd9Sstevel@tonic-gate static	int	partcmp(const void *, const void *);
80*7c478bd9Sstevel@tonic-gate static	int	partcmp64(const void *, const void *);
81*7c478bd9Sstevel@tonic-gate static	int	prtvtoc(char *);
82*7c478bd9Sstevel@tonic-gate static	void	putfree(struct vtoc *, freemap_t *);
83*7c478bd9Sstevel@tonic-gate static	void	putfree64(struct dk_gpt *, freemap_t *);
84*7c478bd9Sstevel@tonic-gate static	void	puttable(struct dk_geom *, struct vtoc *, freemap_t *,
85*7c478bd9Sstevel@tonic-gate 			char *, char **);
86*7c478bd9Sstevel@tonic-gate static	void	puttable64(struct dk_gpt *, freemap_t *,
87*7c478bd9Sstevel@tonic-gate 			char *, char **);
88*7c478bd9Sstevel@tonic-gate static	int	readgeom(int, char *, struct dk_geom *);
89*7c478bd9Sstevel@tonic-gate static	int	readvtoc(int, char *, struct vtoc *);
90*7c478bd9Sstevel@tonic-gate static	int	readefi(int, char *, struct dk_gpt **);
91*7c478bd9Sstevel@tonic-gate static	void	usage(void);
92*7c478bd9Sstevel@tonic-gate static	int	warn(char *, char *);
93*7c478bd9Sstevel@tonic-gate static	char	*safe_strdup(char *);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate /*
96*7c478bd9Sstevel@tonic-gate  * External variables.
97*7c478bd9Sstevel@tonic-gate  */
98*7c478bd9Sstevel@tonic-gate extern char	*getfullrawname();
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * Static variables.
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate static short	fflag;			/* Print freespace shell assignments */
103*7c478bd9Sstevel@tonic-gate static short	hflag;			/* Omit headers */
104*7c478bd9Sstevel@tonic-gate static short	sflag;			/* Omit all but the column header */
105*7c478bd9Sstevel@tonic-gate static char	*fstab = VFSTAB;	/* Fstab pathname */
106*7c478bd9Sstevel@tonic-gate static char	*mnttab = MNTTAB;	/* mnttab pathname */
107*7c478bd9Sstevel@tonic-gate static char	*progname;		/* Last qualifier of arg0 */
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate int
110*7c478bd9Sstevel@tonic-gate main(int ac, char **av)
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	int		idx;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (progname = strrchr(av[0], '/'))
115*7c478bd9Sstevel@tonic-gate 		++progname;
116*7c478bd9Sstevel@tonic-gate 	else
117*7c478bd9Sstevel@tonic-gate 		progname = av[0];
118*7c478bd9Sstevel@tonic-gate 	while ((idx = getopt(ac, av, "fhst:m:")) != -1)
119*7c478bd9Sstevel@tonic-gate 		switch (idx) {
120*7c478bd9Sstevel@tonic-gate 		case 'f':
121*7c478bd9Sstevel@tonic-gate 			++fflag;
122*7c478bd9Sstevel@tonic-gate 			break;
123*7c478bd9Sstevel@tonic-gate 		case 'h':
124*7c478bd9Sstevel@tonic-gate 			++hflag;
125*7c478bd9Sstevel@tonic-gate 			break;
126*7c478bd9Sstevel@tonic-gate 		case 's':
127*7c478bd9Sstevel@tonic-gate 			++sflag;
128*7c478bd9Sstevel@tonic-gate 			break;
129*7c478bd9Sstevel@tonic-gate 		case 't':
130*7c478bd9Sstevel@tonic-gate 			fstab = optarg;
131*7c478bd9Sstevel@tonic-gate 			break;
132*7c478bd9Sstevel@tonic-gate 		case 'm':
133*7c478bd9Sstevel@tonic-gate 			mnttab = optarg;
134*7c478bd9Sstevel@tonic-gate 			break;
135*7c478bd9Sstevel@tonic-gate 		default:
136*7c478bd9Sstevel@tonic-gate 			usage();
137*7c478bd9Sstevel@tonic-gate 		}
138*7c478bd9Sstevel@tonic-gate 	if (optind >= ac)
139*7c478bd9Sstevel@tonic-gate 		usage();
140*7c478bd9Sstevel@tonic-gate 	idx = 0;
141*7c478bd9Sstevel@tonic-gate 	while (optind < ac)
142*7c478bd9Sstevel@tonic-gate 		idx |= prtvtoc(av[optind++]);
143*7c478bd9Sstevel@tonic-gate 	return (idx == 0 ? 0 : 1);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate static freemap_t	*freemap;
147*7c478bd9Sstevel@tonic-gate /*
148*7c478bd9Sstevel@tonic-gate  * findfree(): Find free space on a disk.
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate static freemap_t *
151*7c478bd9Sstevel@tonic-gate findfree(struct dk_geom *geom, struct vtoc *vtoc)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	struct partition	*part;
154*7c478bd9Sstevel@tonic-gate 	struct partition	**list;
155*7c478bd9Sstevel@tonic-gate 	freemap_t		*freeidx;
156*7c478bd9Sstevel@tonic-gate 	ulong_t			fullsize;
157*7c478bd9Sstevel@tonic-gate 	ulong_t			cylsize;
158*7c478bd9Sstevel@tonic-gate 	struct partition	*sorted[V_NUMPAR + 1];
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	freemap = calloc(sizeof (freemap_t), V_NUMPAR + 1);
161*7c478bd9Sstevel@tonic-gate 	cylsize  = (geom->dkg_nsect) * (geom->dkg_nhead);
162*7c478bd9Sstevel@tonic-gate 	fullsize = (geom->dkg_ncyl) * cylsize;
163*7c478bd9Sstevel@tonic-gate 	if (vtoc->v_nparts > V_NUMPAR) {
164*7c478bd9Sstevel@tonic-gate 		(void) warn("putfree()", "Too many partitions on disk!");
165*7c478bd9Sstevel@tonic-gate 		exit(1);
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 	list = sorted;
168*7c478bd9Sstevel@tonic-gate 	for (part = vtoc->v_part; part < vtoc->v_part + vtoc->v_nparts; ++part)
169*7c478bd9Sstevel@tonic-gate 		if (part->p_size && part->p_tag != V_BACKUP)
170*7c478bd9Sstevel@tonic-gate 			*list++ = part;
171*7c478bd9Sstevel@tonic-gate 	*list = 0;
172*7c478bd9Sstevel@tonic-gate 	qsort((char *)sorted, (uint_t)(list - sorted),
173*7c478bd9Sstevel@tonic-gate 		sizeof (*sorted), partcmp);
174*7c478bd9Sstevel@tonic-gate 	freeidx = freemap;
175*7c478bd9Sstevel@tonic-gate 	freeidx->fr_start = 0;
176*7c478bd9Sstevel@tonic-gate 	for (list = sorted; (part = *list) != NULL; ++list)
177*7c478bd9Sstevel@tonic-gate 		if (part->p_start <= freeidx->fr_start)
178*7c478bd9Sstevel@tonic-gate 			freeidx->fr_start += part->p_size;
179*7c478bd9Sstevel@tonic-gate 		else {
180*7c478bd9Sstevel@tonic-gate 			freeidx->fr_size = part->p_start - freeidx->fr_start;
181*7c478bd9Sstevel@tonic-gate 			(++freeidx)->fr_start = part->p_start + part->p_size;
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 	if (freeidx->fr_start < fullsize) {
184*7c478bd9Sstevel@tonic-gate 		freeidx->fr_size = fullsize - freeidx->fr_start;
185*7c478bd9Sstevel@tonic-gate 		++freeidx;
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 	freeidx->fr_start = freeidx->fr_size = 0;
188*7c478bd9Sstevel@tonic-gate 	return (freemap);
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate  * findfree64(): Find free space on a disk.
193*7c478bd9Sstevel@tonic-gate  */
194*7c478bd9Sstevel@tonic-gate static freemap_t *
195*7c478bd9Sstevel@tonic-gate findfree64(struct dk_gpt *efi)
196*7c478bd9Sstevel@tonic-gate {
197*7c478bd9Sstevel@tonic-gate 	struct dk_part		*part;
198*7c478bd9Sstevel@tonic-gate 	struct dk_part		**list;
199*7c478bd9Sstevel@tonic-gate 	freemap_t		*freeidx;
200*7c478bd9Sstevel@tonic-gate 	diskaddr_t		fullsize;
201*7c478bd9Sstevel@tonic-gate 	struct dk_part		**sorted;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	freemap = calloc(sizeof (freemap_t), efi->efi_nparts + 1);
204*7c478bd9Sstevel@tonic-gate 	sorted = calloc(sizeof (struct dk_part), efi->efi_nparts + 1);
205*7c478bd9Sstevel@tonic-gate 	fullsize = efi->efi_last_u_lba;
206*7c478bd9Sstevel@tonic-gate 	list = sorted;
207*7c478bd9Sstevel@tonic-gate 	for (part = efi->efi_parts;
208*7c478bd9Sstevel@tonic-gate 	    part < efi->efi_parts + efi->efi_nparts;
209*7c478bd9Sstevel@tonic-gate 	    ++part)
210*7c478bd9Sstevel@tonic-gate 		if (part->p_size && part->p_tag != V_BACKUP)
211*7c478bd9Sstevel@tonic-gate 			*list++ = part;
212*7c478bd9Sstevel@tonic-gate 	*list = 0;
213*7c478bd9Sstevel@tonic-gate 	qsort((char *)sorted, (uint_t)(list - sorted),
214*7c478bd9Sstevel@tonic-gate 		sizeof (*sorted), partcmp64);
215*7c478bd9Sstevel@tonic-gate 	freeidx = freemap;
216*7c478bd9Sstevel@tonic-gate 	freeidx->fr_start = efi->efi_first_u_lba;
217*7c478bd9Sstevel@tonic-gate 	for (list = sorted; (part = *list) != NULL; ++list)
218*7c478bd9Sstevel@tonic-gate 		if (part->p_start == freeidx->fr_start)
219*7c478bd9Sstevel@tonic-gate 			freeidx->fr_start += part->p_size;
220*7c478bd9Sstevel@tonic-gate 		else {
221*7c478bd9Sstevel@tonic-gate 			freeidx->fr_size = part->p_start - freeidx->fr_start;
222*7c478bd9Sstevel@tonic-gate 			(++freeidx)->fr_start = part->p_start + part->p_size;
223*7c478bd9Sstevel@tonic-gate 		}
224*7c478bd9Sstevel@tonic-gate 	if (freeidx->fr_start < fullsize) {
225*7c478bd9Sstevel@tonic-gate 		freeidx->fr_size = fullsize - freeidx->fr_start;
226*7c478bd9Sstevel@tonic-gate 		++freeidx;
227*7c478bd9Sstevel@tonic-gate 	}
228*7c478bd9Sstevel@tonic-gate 	freeidx->fr_start = freeidx->fr_size = 0;
229*7c478bd9Sstevel@tonic-gate 	return (freemap);
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate /*
233*7c478bd9Sstevel@tonic-gate  * getmntpt()
234*7c478bd9Sstevel@tonic-gate  *
235*7c478bd9Sstevel@tonic-gate  * Get the filesystem mountpoint of each partition on the disk
236*7c478bd9Sstevel@tonic-gate  * from the fstab or mnttab. Returns a pointer to an array of pointers to
237*7c478bd9Sstevel@tonic-gate  * directory names (indexed by partition number).
238*7c478bd9Sstevel@tonic-gate  */
239*7c478bd9Sstevel@tonic-gate static char **
240*7c478bd9Sstevel@tonic-gate getmntpt(major_t slot, minor_t nopartminor)
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate 	int idx;
243*7c478bd9Sstevel@tonic-gate 	FILE *file;
244*7c478bd9Sstevel@tonic-gate 	char devbuf[PATH_MAX], *item;
245*7c478bd9Sstevel@tonic-gate 	static char *list[V_NUMPAR];
246*7c478bd9Sstevel@tonic-gate 	struct stat sb;
247*7c478bd9Sstevel@tonic-gate 	struct mnttab mtab;
248*7c478bd9Sstevel@tonic-gate 	struct vfstab vtab;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < V_NUMPAR; ++idx)
251*7c478bd9Sstevel@tonic-gate 		list[idx] = NULL;
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	/* read mnttab for partition mountpoints */
254*7c478bd9Sstevel@tonic-gate 	if ((file = fopen(mnttab, "r")) == NULL) {
255*7c478bd9Sstevel@tonic-gate 		(void) warn(mnttab, strerror(errno));
256*7c478bd9Sstevel@tonic-gate 	} else {
257*7c478bd9Sstevel@tonic-gate 		while (getmntent(file, &mtab) == 0) {
258*7c478bd9Sstevel@tonic-gate 			item = mtab.mnt_special;
259*7c478bd9Sstevel@tonic-gate 			if ((item == NULL) || (mtab.mnt_mountp == NULL))
260*7c478bd9Sstevel@tonic-gate 				continue;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 			/*
263*7c478bd9Sstevel@tonic-gate 			 * Is it from /dev?
264*7c478bd9Sstevel@tonic-gate 			 */
265*7c478bd9Sstevel@tonic-gate 			if (strncmp(item, "/dev/", strlen("/dev/") != 0))
266*7c478bd9Sstevel@tonic-gate 				continue;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 			/*
269*7c478bd9Sstevel@tonic-gate 			 * Is it a character device?
270*7c478bd9Sstevel@tonic-gate 			 */
271*7c478bd9Sstevel@tonic-gate 			(void) snprintf(devbuf, sizeof (devbuf), "/dev/r%s",
272*7c478bd9Sstevel@tonic-gate 			    item + strlen("/dev/"));
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 			if ((stat(devbuf, &sb) != 0) ||
275*7c478bd9Sstevel@tonic-gate 			    ((sb.st_mode & S_IFMT) != S_IFCHR))
276*7c478bd9Sstevel@tonic-gate 				continue;
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 			/*
279*7c478bd9Sstevel@tonic-gate 			 * device must match input slot and nopartminor
280*7c478bd9Sstevel@tonic-gate 			 */
281*7c478bd9Sstevel@tonic-gate 			if ((major(sb.st_rdev) != slot) ||
282*7c478bd9Sstevel@tonic-gate 			    (noparttn(minor(sb.st_rdev)) != nopartminor))
283*7c478bd9Sstevel@tonic-gate 				continue;
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 			list[parttn(minor(sb.st_rdev))] =
286*7c478bd9Sstevel@tonic-gate 			    safe_strdup(mtab.mnt_mountp);
287*7c478bd9Sstevel@tonic-gate 		}
288*7c478bd9Sstevel@tonic-gate 		(void) fclose(file);
289*7c478bd9Sstevel@tonic-gate 	}
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	if ((file = fopen(fstab, "r")) == NULL) {
292*7c478bd9Sstevel@tonic-gate 		(void) warn(fstab, strerror(errno));
293*7c478bd9Sstevel@tonic-gate 		return (list);
294*7c478bd9Sstevel@tonic-gate 	}
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	/*
297*7c478bd9Sstevel@tonic-gate 	 * Look for the disk in the vfstab so that we can report its mount
298*7c478bd9Sstevel@tonic-gate 	 * point even if it isn't currently mounted.
299*7c478bd9Sstevel@tonic-gate 	 */
300*7c478bd9Sstevel@tonic-gate 	while (getvfsent(file, &vtab) == 0) {
301*7c478bd9Sstevel@tonic-gate 		item = vtab.vfs_special;
302*7c478bd9Sstevel@tonic-gate 		if ((item == NULL) || (vtab.vfs_mountp == NULL))
303*7c478bd9Sstevel@tonic-gate 			continue;
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 		if (strncmp(item, "/dev/", strlen("/dev/")) != 0)
306*7c478bd9Sstevel@tonic-gate 			continue;
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 		/*
309*7c478bd9Sstevel@tonic-gate 		 * Is it a character device?
310*7c478bd9Sstevel@tonic-gate 		 */
311*7c478bd9Sstevel@tonic-gate 		(void) snprintf(devbuf, sizeof (devbuf), "/dev/r%s",
312*7c478bd9Sstevel@tonic-gate 		    item + strlen("/dev/"));
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 		if ((stat(devbuf, &sb) != 0) ||
315*7c478bd9Sstevel@tonic-gate 		    ((sb.st_mode & S_IFMT) != S_IFCHR))
316*7c478bd9Sstevel@tonic-gate 			continue;
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 		/*
319*7c478bd9Sstevel@tonic-gate 		 * device must match input slot and nopartminor
320*7c478bd9Sstevel@tonic-gate 		 */
321*7c478bd9Sstevel@tonic-gate 		if ((major(sb.st_rdev) != slot) ||
322*7c478bd9Sstevel@tonic-gate 		    (noparttn(minor(sb.st_rdev)) != nopartminor))
323*7c478bd9Sstevel@tonic-gate 			continue;
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 		/*
326*7c478bd9Sstevel@tonic-gate 		 * use mnttab entry if both tables have entries
327*7c478bd9Sstevel@tonic-gate 		 */
328*7c478bd9Sstevel@tonic-gate 		if (list[parttn(minor(sb.st_rdev))] != NULL)
329*7c478bd9Sstevel@tonic-gate 			continue;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 		list[parttn(minor(sb.st_rdev))] = safe_strdup(vtab.vfs_mountp);
332*7c478bd9Sstevel@tonic-gate 	}
333*7c478bd9Sstevel@tonic-gate 	(void) fclose(file);
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	return (list);
336*7c478bd9Sstevel@tonic-gate }
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate /*
339*7c478bd9Sstevel@tonic-gate  * partcmp(): Qsort() key comparison of partitions by starting sector numbers.
340*7c478bd9Sstevel@tonic-gate  */
341*7c478bd9Sstevel@tonic-gate static int
342*7c478bd9Sstevel@tonic-gate partcmp(const void *one, const void *two)
343*7c478bd9Sstevel@tonic-gate {
344*7c478bd9Sstevel@tonic-gate 	return ((*(struct partition **)one)->p_start -
345*7c478bd9Sstevel@tonic-gate 		(*(struct partition **)two)->p_start);
346*7c478bd9Sstevel@tonic-gate }
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate static int
349*7c478bd9Sstevel@tonic-gate partcmp64(const void *one, const void *two)
350*7c478bd9Sstevel@tonic-gate {
351*7c478bd9Sstevel@tonic-gate 	if ((*(struct dk_part **)one)->p_start >
352*7c478bd9Sstevel@tonic-gate 		(*(struct dk_part **)two)->p_start)
353*7c478bd9Sstevel@tonic-gate 	    return (1);
354*7c478bd9Sstevel@tonic-gate 	else if ((*(struct dk_part **)one)->p_start <
355*7c478bd9Sstevel@tonic-gate 		(*(struct dk_part **)two)->p_start)
356*7c478bd9Sstevel@tonic-gate 	    return (-1);
357*7c478bd9Sstevel@tonic-gate 	else
358*7c478bd9Sstevel@tonic-gate 	    return (0);
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate /*
363*7c478bd9Sstevel@tonic-gate  * prtvtoc(): Read and print a VTOC.
364*7c478bd9Sstevel@tonic-gate  */
365*7c478bd9Sstevel@tonic-gate static int
366*7c478bd9Sstevel@tonic-gate prtvtoc(char *devname)
367*7c478bd9Sstevel@tonic-gate {
368*7c478bd9Sstevel@tonic-gate 	int		fd;
369*7c478bd9Sstevel@tonic-gate 	int		idx;
370*7c478bd9Sstevel@tonic-gate 	freemap_t	*freemap;
371*7c478bd9Sstevel@tonic-gate 	struct stat	sb;
372*7c478bd9Sstevel@tonic-gate 	struct vtoc	vtoc;
373*7c478bd9Sstevel@tonic-gate 	int		geo;
374*7c478bd9Sstevel@tonic-gate 	struct dk_geom	geom;
375*7c478bd9Sstevel@tonic-gate 	char		*name;
376*7c478bd9Sstevel@tonic-gate 	int		newvtoc = 0;
377*7c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efi;
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	name = getfullrawname(devname);
380*7c478bd9Sstevel@tonic-gate 	if (name == NULL)
381*7c478bd9Sstevel@tonic-gate 		return (warn(devname,
382*7c478bd9Sstevel@tonic-gate 		    "internal administrative call (getfullrawname) failed"));
383*7c478bd9Sstevel@tonic-gate 	if (strcmp(name, "") == 0)
384*7c478bd9Sstevel@tonic-gate 		name = devname;
385*7c478bd9Sstevel@tonic-gate 	if ((fd = open(name, O_NONBLOCK|O_RDONLY)) < 0)
386*7c478bd9Sstevel@tonic-gate 		return (warn(name, strerror(errno)));
387*7c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sb) < 0)
388*7c478bd9Sstevel@tonic-gate 		return (warn(name, strerror(errno)));
389*7c478bd9Sstevel@tonic-gate 	if ((sb.st_mode & S_IFMT) != S_IFCHR)
390*7c478bd9Sstevel@tonic-gate 		return (warn(name, "Not a raw device"));
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	geo = (readgeom(fd, name, &geom) == 0);
393*7c478bd9Sstevel@tonic-gate 	if (geo) {
394*7c478bd9Sstevel@tonic-gate 		if ((idx = readvtoc(fd, name, &vtoc)) == VT_ENOTSUP) {
395*7c478bd9Sstevel@tonic-gate 			idx = (readefi(fd, name, &efi) == 0);
396*7c478bd9Sstevel@tonic-gate 			newvtoc = 1;
397*7c478bd9Sstevel@tonic-gate 		} else
398*7c478bd9Sstevel@tonic-gate 			idx = (idx == 0);
399*7c478bd9Sstevel@tonic-gate 	}
400*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
401*7c478bd9Sstevel@tonic-gate 	if ((!geo) || (!idx))
402*7c478bd9Sstevel@tonic-gate 		return (-1);
403*7c478bd9Sstevel@tonic-gate 	if (!newvtoc)
404*7c478bd9Sstevel@tonic-gate 		freemap = findfree(&geom, &vtoc);
405*7c478bd9Sstevel@tonic-gate 	else
406*7c478bd9Sstevel@tonic-gate 		freemap = findfree64(efi);
407*7c478bd9Sstevel@tonic-gate 	if (fflag) {
408*7c478bd9Sstevel@tonic-gate 		if (!newvtoc)
409*7c478bd9Sstevel@tonic-gate 			putfree(&vtoc, freemap);
410*7c478bd9Sstevel@tonic-gate 		else
411*7c478bd9Sstevel@tonic-gate 			putfree64(efi, freemap);
412*7c478bd9Sstevel@tonic-gate 	} else {
413*7c478bd9Sstevel@tonic-gate 		if (!newvtoc)
414*7c478bd9Sstevel@tonic-gate 			puttable(&geom, &vtoc, freemap, devname,
415*7c478bd9Sstevel@tonic-gate 			    getmntpt(major(sb.st_rdev),
416*7c478bd9Sstevel@tonic-gate 			    noparttn(minor(sb.st_rdev))));
417*7c478bd9Sstevel@tonic-gate 		else
418*7c478bd9Sstevel@tonic-gate 			puttable64(efi, freemap, devname,
419*7c478bd9Sstevel@tonic-gate 			    getmntpt(major(sb.st_rdev),
420*7c478bd9Sstevel@tonic-gate 			    noparttn(minor(sb.st_rdev))));
421*7c478bd9Sstevel@tonic-gate 	}
422*7c478bd9Sstevel@tonic-gate 	if (newvtoc)
423*7c478bd9Sstevel@tonic-gate 		efi_free(efi);
424*7c478bd9Sstevel@tonic-gate 	return (0);
425*7c478bd9Sstevel@tonic-gate }
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate /*
428*7c478bd9Sstevel@tonic-gate  * putfree():
429*7c478bd9Sstevel@tonic-gate  *
430*7c478bd9Sstevel@tonic-gate  * Print shell assignments for disk free space. FREE_START and FREE_SIZE
431*7c478bd9Sstevel@tonic-gate  * represent the starting block and number of blocks of the first chunk
432*7c478bd9Sstevel@tonic-gate  * of free space. FREE_PART lists the unassigned partitions.
433*7c478bd9Sstevel@tonic-gate  */
434*7c478bd9Sstevel@tonic-gate static void
435*7c478bd9Sstevel@tonic-gate putfree(struct vtoc *vtoc, freemap_t *freemap)
436*7c478bd9Sstevel@tonic-gate {
437*7c478bd9Sstevel@tonic-gate 	freemap_t *freeidx;
438*7c478bd9Sstevel@tonic-gate 	ushort_t idx;
439*7c478bd9Sstevel@tonic-gate 	int free_count = 0;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	for (freeidx = freemap; freeidx->fr_size; ++freeidx)
442*7c478bd9Sstevel@tonic-gate 		free_count++;
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 	(void) printf("FREE_START=%llu FREE_SIZE=%llu FREE_COUNT=%d FREE_PART=",
445*7c478bd9Sstevel@tonic-gate 	    freemap->fr_start, freemap->fr_size, free_count);
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < vtoc->v_nparts; ++idx) {
448*7c478bd9Sstevel@tonic-gate 		if (vtoc->v_part[idx].p_size == 0 && idx != 2)
449*7c478bd9Sstevel@tonic-gate 			(void) printf("%x", idx);
450*7c478bd9Sstevel@tonic-gate 	}
451*7c478bd9Sstevel@tonic-gate 	(void) printf("\n");
452*7c478bd9Sstevel@tonic-gate }
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate static void
455*7c478bd9Sstevel@tonic-gate putfree64(struct dk_gpt *efi, freemap_t *freemap)
456*7c478bd9Sstevel@tonic-gate {
457*7c478bd9Sstevel@tonic-gate 	freemap_t *freeidx;
458*7c478bd9Sstevel@tonic-gate 	ushort_t idx;
459*7c478bd9Sstevel@tonic-gate 	int free_count = 0;
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	for (freeidx = freemap; freeidx->fr_size; ++freeidx)
462*7c478bd9Sstevel@tonic-gate 		free_count++;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	(void) printf("FREE_START=%llu FREE_SIZE=%llu FREE_COUNT=%d FREE_PART=",
465*7c478bd9Sstevel@tonic-gate 	    freemap->fr_start, freemap->fr_size, free_count);
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < efi->efi_nparts; ++idx) {
468*7c478bd9Sstevel@tonic-gate 		if (efi->efi_parts[idx].p_size == 0 && idx != 2)
469*7c478bd9Sstevel@tonic-gate 			(void) printf("%x", idx);
470*7c478bd9Sstevel@tonic-gate 	}
471*7c478bd9Sstevel@tonic-gate 	(void) printf("\n");
472*7c478bd9Sstevel@tonic-gate }
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate /*
475*7c478bd9Sstevel@tonic-gate  * puttable(): Print a human-readable VTOC.
476*7c478bd9Sstevel@tonic-gate  */
477*7c478bd9Sstevel@tonic-gate static void
478*7c478bd9Sstevel@tonic-gate puttable(struct dk_geom *geom, struct vtoc *vtoc, freemap_t *freemap,
479*7c478bd9Sstevel@tonic-gate     char *name, char **mtab)
480*7c478bd9Sstevel@tonic-gate {
481*7c478bd9Sstevel@tonic-gate 	ushort_t	idx;
482*7c478bd9Sstevel@tonic-gate 	ulong_t	cylsize;
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 	cylsize = (geom->dkg_nsect) * (geom->dkg_nhead);
485*7c478bd9Sstevel@tonic-gate 	if (!hflag && !sflag) {
486*7c478bd9Sstevel@tonic-gate 		(void) printf("* %s", name);
487*7c478bd9Sstevel@tonic-gate 		if (*vtoc->v_volume)
488*7c478bd9Sstevel@tonic-gate 			(void) printf(" (volume \"%.8s\")", vtoc->v_volume);
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 		(void) printf(" partition map\n");
491*7c478bd9Sstevel@tonic-gate 		(void) printf("*\n* Dimensions:\n");
492*7c478bd9Sstevel@tonic-gate 		(void) printf("* %7u bytes/sector\n", vtoc->v_sectorsz);
493*7c478bd9Sstevel@tonic-gate 		(void) printf("* %7u sectors/track\n", geom->dkg_nsect);
494*7c478bd9Sstevel@tonic-gate 		(void) printf("* %7u tracks/cylinder\n", geom->dkg_nhead);
495*7c478bd9Sstevel@tonic-gate 		(void) printf("* %7lu sectors/cylinder\n", cylsize);
496*7c478bd9Sstevel@tonic-gate 		(void) printf("* %7u cylinders\n", geom->dkg_pcyl);
497*7c478bd9Sstevel@tonic-gate 		(void) printf("* %7u accessible cylinders\n", geom->dkg_ncyl);
498*7c478bd9Sstevel@tonic-gate 		(void) printf("*\n* Flags:\n");
499*7c478bd9Sstevel@tonic-gate 		(void) printf("*   1: unmountable\n");
500*7c478bd9Sstevel@tonic-gate 		(void) printf("*  10: read-only\n*\n");
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 		if (freemap->fr_size) {
503*7c478bd9Sstevel@tonic-gate 			(void) printf("* Unallocated space:\n");
504*7c478bd9Sstevel@tonic-gate 			(void) printf("*\tFirst     Sector    Last\n");
505*7c478bd9Sstevel@tonic-gate 			(void) printf("*\tSector     Count    Sector \n");
506*7c478bd9Sstevel@tonic-gate 			do {
507*7c478bd9Sstevel@tonic-gate 				(void) printf("*   %9llu %9llu %9llu\n",
508*7c478bd9Sstevel@tonic-gate 				    freemap->fr_start, freemap->fr_size,
509*7c478bd9Sstevel@tonic-gate 				    freemap->fr_size + freemap->fr_start - 1);
510*7c478bd9Sstevel@tonic-gate 			} while ((++freemap)->fr_size);
511*7c478bd9Sstevel@tonic-gate 			(void) printf("*\n");
512*7c478bd9Sstevel@tonic-gate 		}
513*7c478bd9Sstevel@tonic-gate 	}
514*7c478bd9Sstevel@tonic-gate 	if (!hflag)  {
515*7c478bd9Sstevel@tonic-gate 		(void) printf(\
516*7c478bd9Sstevel@tonic-gate "*                          First     Sector    Last\n"
517*7c478bd9Sstevel@tonic-gate "* Partition  Tag  Flags    Sector     Count    Sector  Mount Directory\n");
518*7c478bd9Sstevel@tonic-gate 	}
519*7c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < vtoc->v_nparts; ++idx) {
520*7c478bd9Sstevel@tonic-gate 		if (vtoc->v_part[idx].p_size == 0)
521*7c478bd9Sstevel@tonic-gate 			continue;
522*7c478bd9Sstevel@tonic-gate 		(void) printf("      %2u  %5u    %02x  %9lu %9lu %9lu",
523*7c478bd9Sstevel@tonic-gate 		    idx, vtoc->v_part[idx].p_tag, vtoc->v_part[idx].p_flag,
524*7c478bd9Sstevel@tonic-gate 		    vtoc->v_part[idx].p_start, vtoc->v_part[idx].p_size,
525*7c478bd9Sstevel@tonic-gate 		    vtoc->v_part[idx].p_start + vtoc->v_part[idx].p_size - 1);
526*7c478bd9Sstevel@tonic-gate 		if (mtab && mtab[idx])
527*7c478bd9Sstevel@tonic-gate 			(void) printf("   %s", mtab[idx]);
528*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
529*7c478bd9Sstevel@tonic-gate 	}
530*7c478bd9Sstevel@tonic-gate }
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate /*
533*7c478bd9Sstevel@tonic-gate  * puttable(): Print a human-readable VTOC.
534*7c478bd9Sstevel@tonic-gate  */
535*7c478bd9Sstevel@tonic-gate static void
536*7c478bd9Sstevel@tonic-gate puttable64(struct dk_gpt *efi, freemap_t *freemap, char *name,
537*7c478bd9Sstevel@tonic-gate 	char **mtab)
538*7c478bd9Sstevel@tonic-gate {
539*7c478bd9Sstevel@tonic-gate 	ushort_t	idx;
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate 	if (!hflag && !sflag) {
542*7c478bd9Sstevel@tonic-gate 		(void) printf("* %s", name);
543*7c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < efi->efi_nparts; idx++)
544*7c478bd9Sstevel@tonic-gate 		    if (efi->efi_parts[idx].p_tag == V_RESERVED &&
545*7c478bd9Sstevel@tonic-gate 			*efi->efi_parts[idx].p_name)
546*7c478bd9Sstevel@tonic-gate 			    (void) printf(" (volume \"%.8s\")",
547*7c478bd9Sstevel@tonic-gate 				    efi->efi_parts[idx].p_name);
548*7c478bd9Sstevel@tonic-gate 		(void) printf(" partition map\n");
549*7c478bd9Sstevel@tonic-gate 		(void) printf("*\n* Dimensions:\n");
550*7c478bd9Sstevel@tonic-gate 		(void) printf("* %7u bytes/sector\n", efi->efi_lbasize);
551*7c478bd9Sstevel@tonic-gate 		(void) printf("* %llu sectors\n", efi->efi_last_lba + 1);
552*7c478bd9Sstevel@tonic-gate 		(void) printf("* %llu accessible sectors\n",
553*7c478bd9Sstevel@tonic-gate 		    efi->efi_last_u_lba - efi->efi_first_u_lba + 1);
554*7c478bd9Sstevel@tonic-gate 		(void) printf("*\n* Flags:\n");
555*7c478bd9Sstevel@tonic-gate 		(void) printf("*   1: unmountable\n");
556*7c478bd9Sstevel@tonic-gate 		(void) printf("*  10: read-only\n*\n");
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 		if (freemap->fr_size) {
559*7c478bd9Sstevel@tonic-gate 			(void) printf("* Unallocated space:\n");
560*7c478bd9Sstevel@tonic-gate 			(void) printf("*\tFirst     Sector    Last\n");
561*7c478bd9Sstevel@tonic-gate 			(void) printf("*\tSector     Count    Sector \n");
562*7c478bd9Sstevel@tonic-gate 			do {
563*7c478bd9Sstevel@tonic-gate 				(void) printf("*   %9llu %9llu %9llu\n",
564*7c478bd9Sstevel@tonic-gate 				    freemap->fr_start, freemap->fr_size,
565*7c478bd9Sstevel@tonic-gate 				    freemap->fr_size + freemap->fr_start - 1);
566*7c478bd9Sstevel@tonic-gate 			} while ((++freemap)->fr_size);
567*7c478bd9Sstevel@tonic-gate 			(void) printf("*\n");
568*7c478bd9Sstevel@tonic-gate 		}
569*7c478bd9Sstevel@tonic-gate 	}
570*7c478bd9Sstevel@tonic-gate 	if (!hflag)  {
571*7c478bd9Sstevel@tonic-gate 		(void) printf(\
572*7c478bd9Sstevel@tonic-gate "*                          First     Sector    Last\n"
573*7c478bd9Sstevel@tonic-gate "* Partition  Tag  Flags    Sector     Count    Sector  Mount Directory\n");
574*7c478bd9Sstevel@tonic-gate 	}
575*7c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < efi->efi_nparts; ++idx) {
576*7c478bd9Sstevel@tonic-gate 	    if (efi->efi_parts[idx].p_size == 0)
577*7c478bd9Sstevel@tonic-gate 		    continue;
578*7c478bd9Sstevel@tonic-gate 	    (void) printf("      %2u  %5u    %02x  %9llu %9llu %9llu",
579*7c478bd9Sstevel@tonic-gate 		idx, efi->efi_parts[idx].p_tag, efi->efi_parts[idx].p_flag,
580*7c478bd9Sstevel@tonic-gate 		efi->efi_parts[idx].p_start, efi->efi_parts[idx].p_size,
581*7c478bd9Sstevel@tonic-gate 		efi->efi_parts[idx].p_start + efi->efi_parts[idx].p_size - 1);
582*7c478bd9Sstevel@tonic-gate 	    if ((idx < 7) && mtab && mtab[idx])
583*7c478bd9Sstevel@tonic-gate 		    (void) printf("   %s", mtab[idx]);
584*7c478bd9Sstevel@tonic-gate 	    (void) printf("\n");
585*7c478bd9Sstevel@tonic-gate 	}
586*7c478bd9Sstevel@tonic-gate }
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate /*
589*7c478bd9Sstevel@tonic-gate  * readgeom(): Read the disk geometry.
590*7c478bd9Sstevel@tonic-gate  */
591*7c478bd9Sstevel@tonic-gate static int
592*7c478bd9Sstevel@tonic-gate readgeom(int fd, char *name, struct dk_geom *geom)
593*7c478bd9Sstevel@tonic-gate {
594*7c478bd9Sstevel@tonic-gate 	char err_string[128];
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	if ((ioctl(fd, DKIOCGGEOM, geom) < 0) && (errno != ENOTSUP)) {
597*7c478bd9Sstevel@tonic-gate 		(void) sprintf(err_string,
598*7c478bd9Sstevel@tonic-gate 		    "Unable to read Disk geometry errno = 0x%x",
599*7c478bd9Sstevel@tonic-gate 		    errno);
600*7c478bd9Sstevel@tonic-gate 		return (warn(name, err_string));
601*7c478bd9Sstevel@tonic-gate 	} else if (errno == ENOTSUP) {
602*7c478bd9Sstevel@tonic-gate 		(void) memset(geom, 0, sizeof (struct dk_geom));
603*7c478bd9Sstevel@tonic-gate 	}
604*7c478bd9Sstevel@tonic-gate 	return (0);
605*7c478bd9Sstevel@tonic-gate }
606*7c478bd9Sstevel@tonic-gate 
607*7c478bd9Sstevel@tonic-gate /*
608*7c478bd9Sstevel@tonic-gate  * readvtoc(): Read a partition map.
609*7c478bd9Sstevel@tonic-gate  */
610*7c478bd9Sstevel@tonic-gate static int
611*7c478bd9Sstevel@tonic-gate readvtoc(int fd, char *name, struct vtoc *vtoc)
612*7c478bd9Sstevel@tonic-gate {
613*7c478bd9Sstevel@tonic-gate 	int	retval;
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 	if ((retval = read_vtoc(fd, vtoc)) >= 0)
616*7c478bd9Sstevel@tonic-gate 		return (0);
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 	switch (retval) {
619*7c478bd9Sstevel@tonic-gate 	case (VT_EIO):
620*7c478bd9Sstevel@tonic-gate 		return (warn(name, "Unable to read VTOC"));
621*7c478bd9Sstevel@tonic-gate 	case (VT_EINVAL):
622*7c478bd9Sstevel@tonic-gate 		return (warn(name, "Invalid VTOC"));
623*7c478bd9Sstevel@tonic-gate 	case (VT_ERROR):
624*7c478bd9Sstevel@tonic-gate 		return (warn(name, "Unknown problem reading VTOC"));
625*7c478bd9Sstevel@tonic-gate 	}
626*7c478bd9Sstevel@tonic-gate 	return (retval);
627*7c478bd9Sstevel@tonic-gate }
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate /*
630*7c478bd9Sstevel@tonic-gate  * readefi(): Read a partition map.
631*7c478bd9Sstevel@tonic-gate  */
632*7c478bd9Sstevel@tonic-gate static int
633*7c478bd9Sstevel@tonic-gate readefi(int fd, char *name, struct dk_gpt **efi)
634*7c478bd9Sstevel@tonic-gate {
635*7c478bd9Sstevel@tonic-gate 	int	retval;
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate 	if ((retval = efi_alloc_and_read(fd, efi)) >= 0)
638*7c478bd9Sstevel@tonic-gate 		return (0);
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 	switch (retval) {
641*7c478bd9Sstevel@tonic-gate 	case (VT_EIO):
642*7c478bd9Sstevel@tonic-gate 		return (warn(name, "Unable to read VTOC"));
643*7c478bd9Sstevel@tonic-gate 	case (VT_EINVAL):
644*7c478bd9Sstevel@tonic-gate 		return (warn(name, "Invalid VTOC"));
645*7c478bd9Sstevel@tonic-gate 	case (VT_ERROR):
646*7c478bd9Sstevel@tonic-gate 		return (warn(name, "Unknown problem reading VTOC"));
647*7c478bd9Sstevel@tonic-gate 	}
648*7c478bd9Sstevel@tonic-gate 	return (retval);
649*7c478bd9Sstevel@tonic-gate }
650*7c478bd9Sstevel@tonic-gate 
651*7c478bd9Sstevel@tonic-gate static char *
652*7c478bd9Sstevel@tonic-gate safe_strdup(char *str)
653*7c478bd9Sstevel@tonic-gate {
654*7c478bd9Sstevel@tonic-gate 	char *ret;
655*7c478bd9Sstevel@tonic-gate 	if ((ret = strdup(str)) == NULL) {
656*7c478bd9Sstevel@tonic-gate 		(void) warn("memory allocation", strerror(errno));
657*7c478bd9Sstevel@tonic-gate 		exit(1);
658*7c478bd9Sstevel@tonic-gate 	}
659*7c478bd9Sstevel@tonic-gate 	return (ret);
660*7c478bd9Sstevel@tonic-gate }
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate /*
663*7c478bd9Sstevel@tonic-gate  * usage(): Print a helpful message and exit.
664*7c478bd9Sstevel@tonic-gate  */
665*7c478bd9Sstevel@tonic-gate static void
666*7c478bd9Sstevel@tonic-gate usage()
667*7c478bd9Sstevel@tonic-gate {
668*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage:\t%s [ -fhs ] [ -t fstab ] [ -m mnttab ] "
669*7c478bd9Sstevel@tonic-gate 	    "rawdisk ...\n", progname);
670*7c478bd9Sstevel@tonic-gate 	exit(1);
671*7c478bd9Sstevel@tonic-gate }
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate /*
674*7c478bd9Sstevel@tonic-gate  * warn(): Print an error message. Always returns -1.
675*7c478bd9Sstevel@tonic-gate  */
676*7c478bd9Sstevel@tonic-gate static int
677*7c478bd9Sstevel@tonic-gate warn(char *what, char *why)
678*7c478bd9Sstevel@tonic-gate {
679*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", progname, what, why);
680*7c478bd9Sstevel@tonic-gate 	return (-1);
681*7c478bd9Sstevel@tonic-gate }
682