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