10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7563SPrasad.Singamsetty@Sun.COM * Common Development and Distribution License (the "License").
6*7563SPrasad.Singamsetty@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
220Sstevel@tonic-gate /* All Rights Reserved */
230Sstevel@tonic-gate
240Sstevel@tonic-gate
250Sstevel@tonic-gate /* Copyright (c) 1984 AT&T */
260Sstevel@tonic-gate /* All Rights Reserved */
270Sstevel@tonic-gate
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
30*7563SPrasad.Singamsetty@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
310Sstevel@tonic-gate * Use is subject to license terms.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate * Print a disk partition map (volume table of contents, or VTOC).
360Sstevel@tonic-gate */
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <fcntl.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <limits.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate #include <sys/dkio.h>
490Sstevel@tonic-gate #include <sys/vtoc.h>
500Sstevel@tonic-gate #include <sys/mnttab.h>
510Sstevel@tonic-gate #include <sys/vfstab.h>
520Sstevel@tonic-gate #include <sys/mkdev.h>
530Sstevel@tonic-gate
540Sstevel@tonic-gate #include <sys/efi_partition.h>
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate * Assumes V_NUMPAR must be a power of 2.
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * for V_NUMPAR = 8, we have
590Sstevel@tonic-gate * parttn(x)=(x & 0x07) noparttn(x)=(x & 0x3fff8)
600Sstevel@tonic-gate *
610Sstevel@tonic-gate * for V_NUMPAR = 16, we have
620Sstevel@tonic-gate * parttn(x)=(x & 0x0f) noparttn(x)=(x & 0x3fff0)
630Sstevel@tonic-gate */
640Sstevel@tonic-gate #define parttn(x) (x % V_NUMPAR)
650Sstevel@tonic-gate #define noparttn(x) (x & (MAXMIN & ~(V_NUMPAR-1)))
660Sstevel@tonic-gate
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * Disk freespace structure.
690Sstevel@tonic-gate */
700Sstevel@tonic-gate typedef struct {
710Sstevel@tonic-gate u_longlong_t fr_start; /* Start of free space */
720Sstevel@tonic-gate u_longlong_t fr_size; /* Length of free space */
730Sstevel@tonic-gate } freemap_t;
740Sstevel@tonic-gate
75*7563SPrasad.Singamsetty@Sun.COM static freemap_t *findfree(struct dk_geom *, struct extvtoc *);
760Sstevel@tonic-gate static int partcmp(const void *, const void *);
770Sstevel@tonic-gate static int partcmp64(const void *, const void *);
780Sstevel@tonic-gate static int prtvtoc(char *);
79*7563SPrasad.Singamsetty@Sun.COM static void putfree(struct extvtoc *, freemap_t *);
800Sstevel@tonic-gate static void putfree64(struct dk_gpt *, freemap_t *);
81*7563SPrasad.Singamsetty@Sun.COM static void puttable(struct dk_geom *, struct extvtoc *, freemap_t *,
820Sstevel@tonic-gate char *, char **);
830Sstevel@tonic-gate static void puttable64(struct dk_gpt *, freemap_t *,
840Sstevel@tonic-gate char *, char **);
850Sstevel@tonic-gate static int readgeom(int, char *, struct dk_geom *);
86*7563SPrasad.Singamsetty@Sun.COM static int readvtoc(int, char *, struct extvtoc *);
870Sstevel@tonic-gate static int readefi(int, char *, struct dk_gpt **);
880Sstevel@tonic-gate static void usage(void);
890Sstevel@tonic-gate static int warn(char *, char *);
900Sstevel@tonic-gate static char *safe_strdup(char *);
910Sstevel@tonic-gate
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate * External variables.
940Sstevel@tonic-gate */
950Sstevel@tonic-gate extern char *getfullrawname();
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate * Static variables.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate static short fflag; /* Print freespace shell assignments */
1000Sstevel@tonic-gate static short hflag; /* Omit headers */
1010Sstevel@tonic-gate static short sflag; /* Omit all but the column header */
1020Sstevel@tonic-gate static char *fstab = VFSTAB; /* Fstab pathname */
1030Sstevel@tonic-gate static char *mnttab = MNTTAB; /* mnttab pathname */
1040Sstevel@tonic-gate static char *progname; /* Last qualifier of arg0 */
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate int
main(int ac,char ** av)1070Sstevel@tonic-gate main(int ac, char **av)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate int idx;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if (progname = strrchr(av[0], '/'))
1120Sstevel@tonic-gate ++progname;
1130Sstevel@tonic-gate else
1140Sstevel@tonic-gate progname = av[0];
1150Sstevel@tonic-gate while ((idx = getopt(ac, av, "fhst:m:")) != -1)
1160Sstevel@tonic-gate switch (idx) {
1170Sstevel@tonic-gate case 'f':
1180Sstevel@tonic-gate ++fflag;
1190Sstevel@tonic-gate break;
1200Sstevel@tonic-gate case 'h':
1210Sstevel@tonic-gate ++hflag;
1220Sstevel@tonic-gate break;
1230Sstevel@tonic-gate case 's':
1240Sstevel@tonic-gate ++sflag;
1250Sstevel@tonic-gate break;
1260Sstevel@tonic-gate case 't':
1270Sstevel@tonic-gate fstab = optarg;
1280Sstevel@tonic-gate break;
1290Sstevel@tonic-gate case 'm':
1300Sstevel@tonic-gate mnttab = optarg;
1310Sstevel@tonic-gate break;
1320Sstevel@tonic-gate default:
1330Sstevel@tonic-gate usage();
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate if (optind >= ac)
1360Sstevel@tonic-gate usage();
1370Sstevel@tonic-gate idx = 0;
1380Sstevel@tonic-gate while (optind < ac)
1390Sstevel@tonic-gate idx |= prtvtoc(av[optind++]);
1400Sstevel@tonic-gate return (idx == 0 ? 0 : 1);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate static freemap_t *freemap;
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * findfree(): Find free space on a disk.
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate static freemap_t *
findfree(struct dk_geom * geom,struct extvtoc * vtoc)148*7563SPrasad.Singamsetty@Sun.COM findfree(struct dk_geom *geom, struct extvtoc *vtoc)
1490Sstevel@tonic-gate {
150*7563SPrasad.Singamsetty@Sun.COM struct extpartition *part;
151*7563SPrasad.Singamsetty@Sun.COM struct extpartition **list;
1520Sstevel@tonic-gate freemap_t *freeidx;
153*7563SPrasad.Singamsetty@Sun.COM diskaddr_t fullsize;
1540Sstevel@tonic-gate ulong_t cylsize;
155*7563SPrasad.Singamsetty@Sun.COM struct extpartition *sorted[V_NUMPAR + 1];
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate freemap = calloc(sizeof (freemap_t), V_NUMPAR + 1);
1580Sstevel@tonic-gate cylsize = (geom->dkg_nsect) * (geom->dkg_nhead);
159*7563SPrasad.Singamsetty@Sun.COM fullsize = (diskaddr_t)(geom->dkg_ncyl) * cylsize;
1600Sstevel@tonic-gate if (vtoc->v_nparts > V_NUMPAR) {
1610Sstevel@tonic-gate (void) warn("putfree()", "Too many partitions on disk!");
1620Sstevel@tonic-gate exit(1);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate list = sorted;
1650Sstevel@tonic-gate for (part = vtoc->v_part; part < vtoc->v_part + vtoc->v_nparts; ++part)
1660Sstevel@tonic-gate if (part->p_size && part->p_tag != V_BACKUP)
1670Sstevel@tonic-gate *list++ = part;
1680Sstevel@tonic-gate *list = 0;
1690Sstevel@tonic-gate qsort((char *)sorted, (uint_t)(list - sorted),
1700Sstevel@tonic-gate sizeof (*sorted), partcmp);
1710Sstevel@tonic-gate freeidx = freemap;
1720Sstevel@tonic-gate freeidx->fr_start = 0;
1730Sstevel@tonic-gate for (list = sorted; (part = *list) != NULL; ++list)
1740Sstevel@tonic-gate if (part->p_start <= freeidx->fr_start)
1750Sstevel@tonic-gate freeidx->fr_start += part->p_size;
1760Sstevel@tonic-gate else {
1770Sstevel@tonic-gate freeidx->fr_size = part->p_start - freeidx->fr_start;
1780Sstevel@tonic-gate (++freeidx)->fr_start = part->p_start + part->p_size;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate if (freeidx->fr_start < fullsize) {
1810Sstevel@tonic-gate freeidx->fr_size = fullsize - freeidx->fr_start;
1820Sstevel@tonic-gate ++freeidx;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate freeidx->fr_start = freeidx->fr_size = 0;
1850Sstevel@tonic-gate return (freemap);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate * findfree64(): Find free space on a disk.
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate static freemap_t *
findfree64(struct dk_gpt * efi)1920Sstevel@tonic-gate findfree64(struct dk_gpt *efi)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate struct dk_part *part;
1950Sstevel@tonic-gate struct dk_part **list;
1960Sstevel@tonic-gate freemap_t *freeidx;
1970Sstevel@tonic-gate diskaddr_t fullsize;
1980Sstevel@tonic-gate struct dk_part **sorted;
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate freemap = calloc(sizeof (freemap_t), efi->efi_nparts + 1);
2010Sstevel@tonic-gate sorted = calloc(sizeof (struct dk_part), efi->efi_nparts + 1);
2020Sstevel@tonic-gate fullsize = efi->efi_last_u_lba;
2030Sstevel@tonic-gate list = sorted;
2040Sstevel@tonic-gate for (part = efi->efi_parts;
2050Sstevel@tonic-gate part < efi->efi_parts + efi->efi_nparts;
2060Sstevel@tonic-gate ++part)
2070Sstevel@tonic-gate if (part->p_size && part->p_tag != V_BACKUP)
2080Sstevel@tonic-gate *list++ = part;
2090Sstevel@tonic-gate *list = 0;
2100Sstevel@tonic-gate qsort((char *)sorted, (uint_t)(list - sorted),
2110Sstevel@tonic-gate sizeof (*sorted), partcmp64);
2120Sstevel@tonic-gate freeidx = freemap;
2130Sstevel@tonic-gate freeidx->fr_start = efi->efi_first_u_lba;
2140Sstevel@tonic-gate for (list = sorted; (part = *list) != NULL; ++list)
2150Sstevel@tonic-gate if (part->p_start == freeidx->fr_start)
2160Sstevel@tonic-gate freeidx->fr_start += part->p_size;
2170Sstevel@tonic-gate else {
2180Sstevel@tonic-gate freeidx->fr_size = part->p_start - freeidx->fr_start;
2190Sstevel@tonic-gate (++freeidx)->fr_start = part->p_start + part->p_size;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate if (freeidx->fr_start < fullsize) {
2220Sstevel@tonic-gate freeidx->fr_size = fullsize - freeidx->fr_start;
2230Sstevel@tonic-gate ++freeidx;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate freeidx->fr_start = freeidx->fr_size = 0;
2260Sstevel@tonic-gate return (freemap);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate * getmntpt()
2310Sstevel@tonic-gate *
2320Sstevel@tonic-gate * Get the filesystem mountpoint of each partition on the disk
2330Sstevel@tonic-gate * from the fstab or mnttab. Returns a pointer to an array of pointers to
2340Sstevel@tonic-gate * directory names (indexed by partition number).
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate static char **
getmntpt(major_t slot,minor_t nopartminor)2370Sstevel@tonic-gate getmntpt(major_t slot, minor_t nopartminor)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate int idx;
2400Sstevel@tonic-gate FILE *file;
2410Sstevel@tonic-gate char devbuf[PATH_MAX], *item;
2420Sstevel@tonic-gate static char *list[V_NUMPAR];
2430Sstevel@tonic-gate struct stat sb;
2440Sstevel@tonic-gate struct mnttab mtab;
2450Sstevel@tonic-gate struct vfstab vtab;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate for (idx = 0; idx < V_NUMPAR; ++idx)
2480Sstevel@tonic-gate list[idx] = NULL;
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate /* read mnttab for partition mountpoints */
2510Sstevel@tonic-gate if ((file = fopen(mnttab, "r")) == NULL) {
2520Sstevel@tonic-gate (void) warn(mnttab, strerror(errno));
2530Sstevel@tonic-gate } else {
2540Sstevel@tonic-gate while (getmntent(file, &mtab) == 0) {
2550Sstevel@tonic-gate item = mtab.mnt_special;
2560Sstevel@tonic-gate if ((item == NULL) || (mtab.mnt_mountp == NULL))
2570Sstevel@tonic-gate continue;
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * Is it from /dev?
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate if (strncmp(item, "/dev/", strlen("/dev/") != 0))
2630Sstevel@tonic-gate continue;
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate * Is it a character device?
2670Sstevel@tonic-gate */
2680Sstevel@tonic-gate (void) snprintf(devbuf, sizeof (devbuf), "/dev/r%s",
2690Sstevel@tonic-gate item + strlen("/dev/"));
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate if ((stat(devbuf, &sb) != 0) ||
2720Sstevel@tonic-gate ((sb.st_mode & S_IFMT) != S_IFCHR))
2730Sstevel@tonic-gate continue;
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate * device must match input slot and nopartminor
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate if ((major(sb.st_rdev) != slot) ||
2790Sstevel@tonic-gate (noparttn(minor(sb.st_rdev)) != nopartminor))
2800Sstevel@tonic-gate continue;
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate list[parttn(minor(sb.st_rdev))] =
2830Sstevel@tonic-gate safe_strdup(mtab.mnt_mountp);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate (void) fclose(file);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate if ((file = fopen(fstab, "r")) == NULL) {
2890Sstevel@tonic-gate (void) warn(fstab, strerror(errno));
2900Sstevel@tonic-gate return (list);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate * Look for the disk in the vfstab so that we can report its mount
2950Sstevel@tonic-gate * point even if it isn't currently mounted.
2960Sstevel@tonic-gate */
2970Sstevel@tonic-gate while (getvfsent(file, &vtab) == 0) {
2980Sstevel@tonic-gate item = vtab.vfs_special;
2990Sstevel@tonic-gate if ((item == NULL) || (vtab.vfs_mountp == NULL))
3000Sstevel@tonic-gate continue;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate if (strncmp(item, "/dev/", strlen("/dev/")) != 0)
3030Sstevel@tonic-gate continue;
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate * Is it a character device?
3070Sstevel@tonic-gate */
3080Sstevel@tonic-gate (void) snprintf(devbuf, sizeof (devbuf), "/dev/r%s",
3090Sstevel@tonic-gate item + strlen("/dev/"));
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate if ((stat(devbuf, &sb) != 0) ||
3120Sstevel@tonic-gate ((sb.st_mode & S_IFMT) != S_IFCHR))
3130Sstevel@tonic-gate continue;
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate * device must match input slot and nopartminor
3170Sstevel@tonic-gate */
3180Sstevel@tonic-gate if ((major(sb.st_rdev) != slot) ||
3190Sstevel@tonic-gate (noparttn(minor(sb.st_rdev)) != nopartminor))
3200Sstevel@tonic-gate continue;
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate /*
3230Sstevel@tonic-gate * use mnttab entry if both tables have entries
3240Sstevel@tonic-gate */
3250Sstevel@tonic-gate if (list[parttn(minor(sb.st_rdev))] != NULL)
3260Sstevel@tonic-gate continue;
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate list[parttn(minor(sb.st_rdev))] = safe_strdup(vtab.vfs_mountp);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate (void) fclose(file);
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate return (list);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate /*
3360Sstevel@tonic-gate * partcmp(): Qsort() key comparison of partitions by starting sector numbers.
3370Sstevel@tonic-gate */
3380Sstevel@tonic-gate static int
partcmp(const void * one,const void * two)3390Sstevel@tonic-gate partcmp(const void *one, const void *two)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate return ((*(struct partition **)one)->p_start -
3420Sstevel@tonic-gate (*(struct partition **)two)->p_start);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate static int
partcmp64(const void * one,const void * two)3460Sstevel@tonic-gate partcmp64(const void *one, const void *two)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate if ((*(struct dk_part **)one)->p_start >
3490Sstevel@tonic-gate (*(struct dk_part **)two)->p_start)
3500Sstevel@tonic-gate return (1);
3510Sstevel@tonic-gate else if ((*(struct dk_part **)one)->p_start <
3520Sstevel@tonic-gate (*(struct dk_part **)two)->p_start)
3530Sstevel@tonic-gate return (-1);
3540Sstevel@tonic-gate else
3550Sstevel@tonic-gate return (0);
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate /*
3600Sstevel@tonic-gate * prtvtoc(): Read and print a VTOC.
3610Sstevel@tonic-gate */
3620Sstevel@tonic-gate static int
prtvtoc(char * devname)3630Sstevel@tonic-gate prtvtoc(char *devname)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate int fd;
3660Sstevel@tonic-gate int idx;
3670Sstevel@tonic-gate freemap_t *freemap;
3680Sstevel@tonic-gate struct stat sb;
369*7563SPrasad.Singamsetty@Sun.COM struct extvtoc vtoc;
3700Sstevel@tonic-gate int geo;
3710Sstevel@tonic-gate struct dk_geom geom;
3720Sstevel@tonic-gate char *name;
3730Sstevel@tonic-gate int newvtoc = 0;
3740Sstevel@tonic-gate struct dk_gpt *efi;
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate name = getfullrawname(devname);
3770Sstevel@tonic-gate if (name == NULL)
3780Sstevel@tonic-gate return (warn(devname,
3790Sstevel@tonic-gate "internal administrative call (getfullrawname) failed"));
3800Sstevel@tonic-gate if (strcmp(name, "") == 0)
3810Sstevel@tonic-gate name = devname;
3820Sstevel@tonic-gate if ((fd = open(name, O_NONBLOCK|O_RDONLY)) < 0)
3830Sstevel@tonic-gate return (warn(name, strerror(errno)));
3840Sstevel@tonic-gate if (fstat(fd, &sb) < 0)
3850Sstevel@tonic-gate return (warn(name, strerror(errno)));
3860Sstevel@tonic-gate if ((sb.st_mode & S_IFMT) != S_IFCHR)
3870Sstevel@tonic-gate return (warn(name, "Not a raw device"));
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate geo = (readgeom(fd, name, &geom) == 0);
3900Sstevel@tonic-gate if (geo) {
3910Sstevel@tonic-gate if ((idx = readvtoc(fd, name, &vtoc)) == VT_ENOTSUP) {
3920Sstevel@tonic-gate idx = (readefi(fd, name, &efi) == 0);
3930Sstevel@tonic-gate newvtoc = 1;
3940Sstevel@tonic-gate } else
3950Sstevel@tonic-gate idx = (idx == 0);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate (void) close(fd);
3980Sstevel@tonic-gate if ((!geo) || (!idx))
3990Sstevel@tonic-gate return (-1);
4000Sstevel@tonic-gate if (!newvtoc)
4010Sstevel@tonic-gate freemap = findfree(&geom, &vtoc);
4020Sstevel@tonic-gate else
4030Sstevel@tonic-gate freemap = findfree64(efi);
4040Sstevel@tonic-gate if (fflag) {
4050Sstevel@tonic-gate if (!newvtoc)
4060Sstevel@tonic-gate putfree(&vtoc, freemap);
4070Sstevel@tonic-gate else
4080Sstevel@tonic-gate putfree64(efi, freemap);
4090Sstevel@tonic-gate } else {
4100Sstevel@tonic-gate if (!newvtoc)
4110Sstevel@tonic-gate puttable(&geom, &vtoc, freemap, devname,
4120Sstevel@tonic-gate getmntpt(major(sb.st_rdev),
4130Sstevel@tonic-gate noparttn(minor(sb.st_rdev))));
4140Sstevel@tonic-gate else
4150Sstevel@tonic-gate puttable64(efi, freemap, devname,
4160Sstevel@tonic-gate getmntpt(major(sb.st_rdev),
4170Sstevel@tonic-gate noparttn(minor(sb.st_rdev))));
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate if (newvtoc)
4200Sstevel@tonic-gate efi_free(efi);
4210Sstevel@tonic-gate return (0);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate * putfree():
4260Sstevel@tonic-gate *
4270Sstevel@tonic-gate * Print shell assignments for disk free space. FREE_START and FREE_SIZE
4280Sstevel@tonic-gate * represent the starting block and number of blocks of the first chunk
4290Sstevel@tonic-gate * of free space. FREE_PART lists the unassigned partitions.
4300Sstevel@tonic-gate */
4310Sstevel@tonic-gate static void
putfree(struct extvtoc * vtoc,freemap_t * freemap)432*7563SPrasad.Singamsetty@Sun.COM putfree(struct extvtoc *vtoc, freemap_t *freemap)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate freemap_t *freeidx;
4350Sstevel@tonic-gate ushort_t idx;
4360Sstevel@tonic-gate int free_count = 0;
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate for (freeidx = freemap; freeidx->fr_size; ++freeidx)
4390Sstevel@tonic-gate free_count++;
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate (void) printf("FREE_START=%llu FREE_SIZE=%llu FREE_COUNT=%d FREE_PART=",
4420Sstevel@tonic-gate freemap->fr_start, freemap->fr_size, free_count);
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate for (idx = 0; idx < vtoc->v_nparts; ++idx) {
4450Sstevel@tonic-gate if (vtoc->v_part[idx].p_size == 0 && idx != 2)
4460Sstevel@tonic-gate (void) printf("%x", idx);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate (void) printf("\n");
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate static void
putfree64(struct dk_gpt * efi,freemap_t * freemap)4520Sstevel@tonic-gate putfree64(struct dk_gpt *efi, freemap_t *freemap)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate freemap_t *freeidx;
4550Sstevel@tonic-gate ushort_t idx;
4560Sstevel@tonic-gate int free_count = 0;
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate for (freeidx = freemap; freeidx->fr_size; ++freeidx)
4590Sstevel@tonic-gate free_count++;
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate (void) printf("FREE_START=%llu FREE_SIZE=%llu FREE_COUNT=%d FREE_PART=",
4620Sstevel@tonic-gate freemap->fr_start, freemap->fr_size, free_count);
4630Sstevel@tonic-gate
4640Sstevel@tonic-gate for (idx = 0; idx < efi->efi_nparts; ++idx) {
4650Sstevel@tonic-gate if (efi->efi_parts[idx].p_size == 0 && idx != 2)
4660Sstevel@tonic-gate (void) printf("%x", idx);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate (void) printf("\n");
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate * puttable(): Print a human-readable VTOC.
4730Sstevel@tonic-gate */
4740Sstevel@tonic-gate static void
puttable(struct dk_geom * geom,struct extvtoc * vtoc,freemap_t * freemap,char * name,char ** mtab)475*7563SPrasad.Singamsetty@Sun.COM puttable(struct dk_geom *geom, struct extvtoc *vtoc, freemap_t *freemap,
4760Sstevel@tonic-gate char *name, char **mtab)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate ushort_t idx;
4790Sstevel@tonic-gate ulong_t cylsize;
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate cylsize = (geom->dkg_nsect) * (geom->dkg_nhead);
4820Sstevel@tonic-gate if (!hflag && !sflag) {
4830Sstevel@tonic-gate (void) printf("* %s", name);
4840Sstevel@tonic-gate if (*vtoc->v_volume)
4850Sstevel@tonic-gate (void) printf(" (volume \"%.8s\")", vtoc->v_volume);
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate (void) printf(" partition map\n");
4880Sstevel@tonic-gate (void) printf("*\n* Dimensions:\n");
4890Sstevel@tonic-gate (void) printf("* %7u bytes/sector\n", vtoc->v_sectorsz);
4900Sstevel@tonic-gate (void) printf("* %7u sectors/track\n", geom->dkg_nsect);
4910Sstevel@tonic-gate (void) printf("* %7u tracks/cylinder\n", geom->dkg_nhead);
4920Sstevel@tonic-gate (void) printf("* %7lu sectors/cylinder\n", cylsize);
4930Sstevel@tonic-gate (void) printf("* %7u cylinders\n", geom->dkg_pcyl);
4940Sstevel@tonic-gate (void) printf("* %7u accessible cylinders\n", geom->dkg_ncyl);
4950Sstevel@tonic-gate (void) printf("*\n* Flags:\n");
4960Sstevel@tonic-gate (void) printf("* 1: unmountable\n");
4970Sstevel@tonic-gate (void) printf("* 10: read-only\n*\n");
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate if (freemap->fr_size) {
5000Sstevel@tonic-gate (void) printf("* Unallocated space:\n");
5010Sstevel@tonic-gate (void) printf("*\tFirst Sector Last\n");
5020Sstevel@tonic-gate (void) printf("*\tSector Count Sector \n");
5030Sstevel@tonic-gate do {
5040Sstevel@tonic-gate (void) printf("* %9llu %9llu %9llu\n",
5050Sstevel@tonic-gate freemap->fr_start, freemap->fr_size,
5060Sstevel@tonic-gate freemap->fr_size + freemap->fr_start - 1);
5070Sstevel@tonic-gate } while ((++freemap)->fr_size);
5080Sstevel@tonic-gate (void) printf("*\n");
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate if (!hflag) {
5120Sstevel@tonic-gate (void) printf(\
5130Sstevel@tonic-gate "* First Sector Last\n"
5140Sstevel@tonic-gate "* Partition Tag Flags Sector Count Sector Mount Directory\n");
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate for (idx = 0; idx < vtoc->v_nparts; ++idx) {
5170Sstevel@tonic-gate if (vtoc->v_part[idx].p_size == 0)
5180Sstevel@tonic-gate continue;
519*7563SPrasad.Singamsetty@Sun.COM (void) printf(" %2u %5u %02x %9llu %9llu %9llu",
5200Sstevel@tonic-gate idx, vtoc->v_part[idx].p_tag, vtoc->v_part[idx].p_flag,
5210Sstevel@tonic-gate vtoc->v_part[idx].p_start, vtoc->v_part[idx].p_size,
5220Sstevel@tonic-gate vtoc->v_part[idx].p_start + vtoc->v_part[idx].p_size - 1);
5230Sstevel@tonic-gate if (mtab && mtab[idx])
5240Sstevel@tonic-gate (void) printf(" %s", mtab[idx]);
5250Sstevel@tonic-gate (void) printf("\n");
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate /*
5300Sstevel@tonic-gate * puttable(): Print a human-readable VTOC.
5310Sstevel@tonic-gate */
5320Sstevel@tonic-gate static void
puttable64(struct dk_gpt * efi,freemap_t * freemap,char * name,char ** mtab)5330Sstevel@tonic-gate puttable64(struct dk_gpt *efi, freemap_t *freemap, char *name,
5340Sstevel@tonic-gate char **mtab)
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate ushort_t idx;
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate if (!hflag && !sflag) {
5390Sstevel@tonic-gate (void) printf("* %s", name);
5400Sstevel@tonic-gate for (idx = 0; idx < efi->efi_nparts; idx++)
5410Sstevel@tonic-gate if (efi->efi_parts[idx].p_tag == V_RESERVED &&
5420Sstevel@tonic-gate *efi->efi_parts[idx].p_name)
5430Sstevel@tonic-gate (void) printf(" (volume \"%.8s\")",
5440Sstevel@tonic-gate efi->efi_parts[idx].p_name);
5450Sstevel@tonic-gate (void) printf(" partition map\n");
5460Sstevel@tonic-gate (void) printf("*\n* Dimensions:\n");
5470Sstevel@tonic-gate (void) printf("* %7u bytes/sector\n", efi->efi_lbasize);
5480Sstevel@tonic-gate (void) printf("* %llu sectors\n", efi->efi_last_lba + 1);
5490Sstevel@tonic-gate (void) printf("* %llu accessible sectors\n",
5500Sstevel@tonic-gate efi->efi_last_u_lba - efi->efi_first_u_lba + 1);
5510Sstevel@tonic-gate (void) printf("*\n* Flags:\n");
5520Sstevel@tonic-gate (void) printf("* 1: unmountable\n");
5530Sstevel@tonic-gate (void) printf("* 10: read-only\n*\n");
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate if (freemap->fr_size) {
5560Sstevel@tonic-gate (void) printf("* Unallocated space:\n");
5570Sstevel@tonic-gate (void) printf("*\tFirst Sector Last\n");
5580Sstevel@tonic-gate (void) printf("*\tSector Count Sector \n");
5590Sstevel@tonic-gate do {
5600Sstevel@tonic-gate (void) printf("* %9llu %9llu %9llu\n",
5610Sstevel@tonic-gate freemap->fr_start, freemap->fr_size,
5620Sstevel@tonic-gate freemap->fr_size + freemap->fr_start - 1);
5630Sstevel@tonic-gate } while ((++freemap)->fr_size);
5640Sstevel@tonic-gate (void) printf("*\n");
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate if (!hflag) {
5680Sstevel@tonic-gate (void) printf(\
5690Sstevel@tonic-gate "* First Sector Last\n"
5700Sstevel@tonic-gate "* Partition Tag Flags Sector Count Sector Mount Directory\n");
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate for (idx = 0; idx < efi->efi_nparts; ++idx) {
5730Sstevel@tonic-gate if (efi->efi_parts[idx].p_size == 0)
5740Sstevel@tonic-gate continue;
5750Sstevel@tonic-gate (void) printf(" %2u %5u %02x %9llu %9llu %9llu",
5760Sstevel@tonic-gate idx, efi->efi_parts[idx].p_tag, efi->efi_parts[idx].p_flag,
5770Sstevel@tonic-gate efi->efi_parts[idx].p_start, efi->efi_parts[idx].p_size,
5780Sstevel@tonic-gate efi->efi_parts[idx].p_start + efi->efi_parts[idx].p_size - 1);
5790Sstevel@tonic-gate if ((idx < 7) && mtab && mtab[idx])
5800Sstevel@tonic-gate (void) printf(" %s", mtab[idx]);
5810Sstevel@tonic-gate (void) printf("\n");
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate * readgeom(): Read the disk geometry.
5870Sstevel@tonic-gate */
5880Sstevel@tonic-gate static int
readgeom(int fd,char * name,struct dk_geom * geom)5890Sstevel@tonic-gate readgeom(int fd, char *name, struct dk_geom *geom)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate char err_string[128];
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate if ((ioctl(fd, DKIOCGGEOM, geom) < 0) && (errno != ENOTSUP)) {
5940Sstevel@tonic-gate (void) sprintf(err_string,
5950Sstevel@tonic-gate "Unable to read Disk geometry errno = 0x%x",
5960Sstevel@tonic-gate errno);
5970Sstevel@tonic-gate return (warn(name, err_string));
5980Sstevel@tonic-gate } else if (errno == ENOTSUP) {
5990Sstevel@tonic-gate (void) memset(geom, 0, sizeof (struct dk_geom));
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate return (0);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate * readvtoc(): Read a partition map.
6060Sstevel@tonic-gate */
6070Sstevel@tonic-gate static int
readvtoc(int fd,char * name,struct extvtoc * vtoc)608*7563SPrasad.Singamsetty@Sun.COM readvtoc(int fd, char *name, struct extvtoc *vtoc)
6090Sstevel@tonic-gate {
6100Sstevel@tonic-gate int retval;
6110Sstevel@tonic-gate
612*7563SPrasad.Singamsetty@Sun.COM if ((retval = read_extvtoc(fd, vtoc)) >= 0)
6130Sstevel@tonic-gate return (0);
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate switch (retval) {
6160Sstevel@tonic-gate case (VT_EIO):
6170Sstevel@tonic-gate return (warn(name, "Unable to read VTOC"));
6180Sstevel@tonic-gate case (VT_EINVAL):
6190Sstevel@tonic-gate return (warn(name, "Invalid VTOC"));
6200Sstevel@tonic-gate case (VT_ERROR):
6210Sstevel@tonic-gate return (warn(name, "Unknown problem reading VTOC"));
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate return (retval);
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate
6260Sstevel@tonic-gate /*
6270Sstevel@tonic-gate * readefi(): Read a partition map.
6280Sstevel@tonic-gate */
6290Sstevel@tonic-gate static int
readefi(int fd,char * name,struct dk_gpt ** efi)6300Sstevel@tonic-gate readefi(int fd, char *name, struct dk_gpt **efi)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate int retval;
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate if ((retval = efi_alloc_and_read(fd, efi)) >= 0)
6350Sstevel@tonic-gate return (0);
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate switch (retval) {
6380Sstevel@tonic-gate case (VT_EIO):
6390Sstevel@tonic-gate return (warn(name, "Unable to read VTOC"));
6400Sstevel@tonic-gate case (VT_EINVAL):
6410Sstevel@tonic-gate return (warn(name, "Invalid VTOC"));
6420Sstevel@tonic-gate case (VT_ERROR):
6430Sstevel@tonic-gate return (warn(name, "Unknown problem reading VTOC"));
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate return (retval);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate static char *
safe_strdup(char * str)6490Sstevel@tonic-gate safe_strdup(char *str)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate char *ret;
6520Sstevel@tonic-gate if ((ret = strdup(str)) == NULL) {
6530Sstevel@tonic-gate (void) warn("memory allocation", strerror(errno));
6540Sstevel@tonic-gate exit(1);
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate return (ret);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate * usage(): Print a helpful message and exit.
6610Sstevel@tonic-gate */
6620Sstevel@tonic-gate static void
usage()6630Sstevel@tonic-gate usage()
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate (void) fprintf(stderr, "Usage:\t%s [ -fhs ] [ -t fstab ] [ -m mnttab ] "
6660Sstevel@tonic-gate "rawdisk ...\n", progname);
6670Sstevel@tonic-gate exit(1);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate
6700Sstevel@tonic-gate /*
6710Sstevel@tonic-gate * warn(): Print an error message. Always returns -1.
6720Sstevel@tonic-gate */
6730Sstevel@tonic-gate static int
warn(char * what,char * why)6740Sstevel@tonic-gate warn(char *what, char *why)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", progname, what, why);
6770Sstevel@tonic-gate return (-1);
6780Sstevel@tonic-gate }
679