xref: /onnv-gate/usr/src/cmd/devmgmt/mkdtab/mkdtab.c (revision 7563:84ec90ffc3f7)
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 /*
22*7563SPrasad.Singamsetty@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
260Sstevel@tonic-gate /*	  All Rights Reserved  	*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include	<stdio.h>
290Sstevel@tonic-gate #include	<stdlib.h>
300Sstevel@tonic-gate #include	<string.h>
310Sstevel@tonic-gate #include	<fcntl.h>
320Sstevel@tonic-gate #include	<sys/types.h>
330Sstevel@tonic-gate #include	<unistd.h>
340Sstevel@tonic-gate #include	<devmgmt.h>
350Sstevel@tonic-gate #include	<devtab.h>
360Sstevel@tonic-gate #include	<dirent.h>
370Sstevel@tonic-gate #include	<libgen.h>
380Sstevel@tonic-gate #include	<sys/stat.h>
390Sstevel@tonic-gate #include	<sys/vtoc.h>
400Sstevel@tonic-gate #include	<sys/vfstab.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Update device.tab and dgroup.tab to reflect current configuration.
440Sstevel@tonic-gate  * Designed so it can be run either once at installation time or after
450Sstevel@tonic-gate  * every reboot.  The alias naming scheme used is non-intuitive but
460Sstevel@tonic-gate  * is consistent with existing conventions and documentation and with
470Sstevel@tonic-gate  * the device numbering scheme used by the disks command.
480Sstevel@tonic-gate  * Code borrowed liberally from prtconf, disks and prtvtoc commands.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * make this long enough to start out with.
530Sstevel@tonic-gate  * there are place we write into putdevcmd
540Sstevel@tonic-gate  * where we are not testing for overrun.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate #define	ORIGLEN	1024
570Sstevel@tonic-gate 
58644Sakaplan struct dpart {
59*7563SPrasad.Singamsetty@Sun.COM 	char		alias[20];
60*7563SPrasad.Singamsetty@Sun.COM 	char		*cdevice;
61*7563SPrasad.Singamsetty@Sun.COM 	char		*bdevice;
62*7563SPrasad.Singamsetty@Sun.COM 	diskaddr_t	capacity;
630Sstevel@tonic-gate };
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static int		vfsnum;
660Sstevel@tonic-gate static char		*putdevcmd;
670Sstevel@tonic-gate static char		cmd[80];
680Sstevel@tonic-gate static int		lastlen = ORIGLEN;
690Sstevel@tonic-gate #ifdef att3b2
700Sstevel@tonic-gate static struct mainedt	*edtp;
710Sstevel@tonic-gate #endif
720Sstevel@tonic-gate static struct vfstab	*vfstab;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static void checkandresize(int);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static char *
memstr(const char * str)770Sstevel@tonic-gate memstr(const char *str)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	char	*mem;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if ((mem = (char *)malloc((uint_t)strlen(str) + 1)) == NULL) {
820Sstevel@tonic-gate 		(void) fprintf(stderr,
830Sstevel@tonic-gate 		    "%s: can't update device tables:Out of memory\n", cmd);
840Sstevel@tonic-gate 		exit(1);
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 	return (strcpy(mem, str));
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * Add device table entry for the floppy drive.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate static void
fdisk(const int diskno,const char * disknm)950Sstevel@tonic-gate fdisk(const int diskno, const char *disknm)
960Sstevel@tonic-gate {
97*7563SPrasad.Singamsetty@Sun.COM 	if (snprintf(putdevcmd, lastlen, "/usr/bin/putdev -a diskette%d "
98*7563SPrasad.Singamsetty@Sun.COM 	    "cdevice=/dev/r%s bdevice=/dev/%s desc=\"Floppy Drive\" "
99*7563SPrasad.Singamsetty@Sun.COM 	    "mountpt=/mnt volume=diskette "
100*7563SPrasad.Singamsetty@Sun.COM 	    "type=diskette removable=true capacity=2880 "
101*7563SPrasad.Singamsetty@Sun.COM 	    "fmtcmd=\"/usr/bin/fdformat -f -v /dev/r%s\" "
102*7563SPrasad.Singamsetty@Sun.COM 	    "erasecmd=\"/usr/sbin/fdformat -f -v /dev/r%s\" "
103*7563SPrasad.Singamsetty@Sun.COM 	    "removecmd=\"/usr/bin/eject\" copy=true "
104*7563SPrasad.Singamsetty@Sun.COM 	    "mkfscmd=\"/usr/sbin/mkfs -F ufs /dev/r%s 2880 18 "
105*7563SPrasad.Singamsetty@Sun.COM 	    "2 4096 512 80 2 5 3072 t\"",
106*7563SPrasad.Singamsetty@Sun.COM 	    diskno, disknm, disknm, disknm, disknm, disknm) >= lastlen) {
1070Sstevel@tonic-gate 		(void) fprintf(stderr,
1080Sstevel@tonic-gate 		    "%s: Command too long: %s\n", cmd, putdevcmd);
1090Sstevel@tonic-gate 		exit(1);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 	(void) system(putdevcmd);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static void
do_fdisks(void)1150Sstevel@tonic-gate do_fdisks(void)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	DIR *dp;
1180Sstevel@tonic-gate 	struct dirent *dirp;
1190Sstevel@tonic-gate 	int drive = 1;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if ((dp = opendir("/dev")) == NULL) {
1220Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: can't open /dev\n", cmd);
1230Sstevel@tonic-gate 		return;
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	while ((dirp = readdir(dp)) != NULL) {
1270Sstevel@tonic-gate 		if (gmatch(dirp->d_name, "diskette*")) {
1280Sstevel@tonic-gate 			fdisk(drive++, dirp->d_name);
1290Sstevel@tonic-gate 		}
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	(void) closedir(dp);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * hdisk() gets information about the specified hard drive from the vtoc
1370Sstevel@tonic-gate  * and vfstab and adds the disk and partition entries to device.tab. If
1380Sstevel@tonic-gate  * we can't access the raw disk we simply assume it isn't properly configured
1390Sstevel@tonic-gate  * and we add no entries to device.tab.
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate static void
hdisk(const int drive,const char * drivepfx)1420Sstevel@tonic-gate hdisk(const int drive, const char *drivepfx)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	char		*cdskpath;
1450Sstevel@tonic-gate 	char		*bdskpath;
1460Sstevel@tonic-gate 	char		*mountpoint;
1470Sstevel@tonic-gate 	int		i, j, dpartcnt, fd;
148*7563SPrasad.Singamsetty@Sun.COM 	struct extvtoc	vtoc;
1490Sstevel@tonic-gate 	static struct dpart    *dparttab;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if ((cdskpath = (char *)malloc(strlen(drivepfx) + 13)) == NULL) {
1520Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: Memory request failed\n", cmd);
1530Sstevel@tonic-gate 		exit(1);
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	(void) snprintf(cdskpath, strlen(drivepfx) + 13, "/dev/rdsk/%ss2",
1570Sstevel@tonic-gate 	    drivepfx);
1580Sstevel@tonic-gate 	if ((fd = open(cdskpath, O_RDONLY)) == -1) {
1590Sstevel@tonic-gate 		free(cdskpath);
1600Sstevel@tonic-gate 		return;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	/*
1650Sstevel@tonic-gate 	 * Read volume table of contents.
1660Sstevel@tonic-gate 	 */
167*7563SPrasad.Singamsetty@Sun.COM 	if (read_extvtoc(fd, &vtoc) < 0) {
1680Sstevel@tonic-gate 		(void) close(fd);
1690Sstevel@tonic-gate 		free(cdskpath);
1700Sstevel@tonic-gate 		return;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	(void) close(fd);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * Begin building the putdev command string that will be
1770Sstevel@tonic-gate 	 * used to make the entry for this disk.
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if ((bdskpath = (char *)malloc(strlen(drivepfx) + 13)) == NULL) {
1810Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: Memory request failed\n", cmd);
1820Sstevel@tonic-gate 		exit(1);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	(void) snprintf(bdskpath, strlen(drivepfx) + 13, "/dev/dsk/%ss2",
1860Sstevel@tonic-gate 	    drivepfx);
187*7563SPrasad.Singamsetty@Sun.COM 	if (snprintf(putdevcmd, lastlen, "/usr/bin/putdev -a disk%d "
188*7563SPrasad.Singamsetty@Sun.COM 	    "cdevice=%s bdevice=%s "
189*7563SPrasad.Singamsetty@Sun.COM 	    "desc=\"Disk Drive\" type=disk "
190*7563SPrasad.Singamsetty@Sun.COM 	    "part=true removable=false capacity=%llu dpartlist=",
191*7563SPrasad.Singamsetty@Sun.COM 	    drive, cdskpath, bdskpath, vtoc.v_part[2].p_size) >= lastlen) {
1920Sstevel@tonic-gate 		(void) fprintf(stderr,
1930Sstevel@tonic-gate 		    "%s: Command too long: %s\n", cmd, putdevcmd);
1940Sstevel@tonic-gate 		exit(1);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	free(cdskpath);
1980Sstevel@tonic-gate 	free(bdskpath);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/*
2010Sstevel@tonic-gate 	 * Build a table of disk partitions we are interested in and finish
2020Sstevel@tonic-gate 	 * the putdev command string for the disk by adding the dpartlist.
2030Sstevel@tonic-gate 	 */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if ((dparttab =
206*7563SPrasad.Singamsetty@Sun.COM 	    (struct dpart *)malloc((int)vtoc.v_nparts *
207*7563SPrasad.Singamsetty@Sun.COM 	    sizeof (struct dpart))) == NULL) {
2080Sstevel@tonic-gate 		(void) fprintf(stderr,
209*7563SPrasad.Singamsetty@Sun.COM 		    "%s: can't disk partitions table: Out of memory\n", cmd);
2100Sstevel@tonic-gate 		exit(1);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	dpartcnt = 0;
2140Sstevel@tonic-gate 	for (i = 0; i < (int)vtoc.v_nparts; ++i) {
2150Sstevel@tonic-gate 		if (vtoc.v_part[i].p_size == 0 || vtoc.v_part[i].p_flag != 0)
2160Sstevel@tonic-gate 			continue;
2170Sstevel@tonic-gate 		(void) sprintf(dparttab[dpartcnt].alias, "dpart%d%02d", drive,
2180Sstevel@tonic-gate 		    i);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 		if ((dparttab[dpartcnt].cdevice =
221*7563SPrasad.Singamsetty@Sun.COM 		    (char *)malloc(strlen(drivepfx) + 14)) == NULL) {
2220Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: Out of memory\n", cmd);
2230Sstevel@tonic-gate 			exit(1);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		(void) snprintf(dparttab[dpartcnt].cdevice,
2270Sstevel@tonic-gate 		    strlen(drivepfx) + 14, "/dev/rdsk/%ss%x", drivepfx, i);
2280Sstevel@tonic-gate 		if ((dparttab[dpartcnt].bdevice =
229*7563SPrasad.Singamsetty@Sun.COM 		    (char *)malloc(strlen(drivepfx) + 14)) == NULL) {
2300Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: Out of memory\n", cmd);
2310Sstevel@tonic-gate 			exit(1);
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 		(void) snprintf(dparttab[dpartcnt].bdevice,
2340Sstevel@tonic-gate 		    strlen(drivepfx) + 14, "/dev/dsk/%ss%x", drivepfx, i);
2350Sstevel@tonic-gate 		dparttab[dpartcnt].capacity = vtoc.v_part[i].p_size;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		if (dpartcnt != 0)
2380Sstevel@tonic-gate 			(void) strcat(putdevcmd, ",");
2390Sstevel@tonic-gate 		(void) strcat(putdevcmd, dparttab[dpartcnt].alias);
2400Sstevel@tonic-gate 		dpartcnt++;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	(void) system(putdevcmd);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * We assemble the rest of the information about the partitions by
2470Sstevel@tonic-gate 	 * looking in the vfstab.
2480Sstevel@tonic-gate 	 */
2490Sstevel@tonic-gate 	for (i = 0; i < dpartcnt; i++) {
2500Sstevel@tonic-gate 		for (j = 0; j < vfsnum; j++) {
2510Sstevel@tonic-gate 			if (vfstab[j].vfs_special != NULL &&
2520Sstevel@tonic-gate 			    strcmp(dparttab[i].bdevice,
253*7563SPrasad.Singamsetty@Sun.COM 			    vfstab[j].vfs_special) == 0)
2540Sstevel@tonic-gate 				break;
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 		if (j < vfsnum) {
2570Sstevel@tonic-gate 			/*
2580Sstevel@tonic-gate 			 * Partition found in vfstab.
2590Sstevel@tonic-gate 			 */
2600Sstevel@tonic-gate 			if (vfstab[j].vfs_mountp == NULL ||
2610Sstevel@tonic-gate 			    strcmp(vfstab[j].vfs_mountp, "-") == 0)
262*7563SPrasad.Singamsetty@Sun.COM 				mountpoint = "/mnt";
2630Sstevel@tonic-gate 			else
264*7563SPrasad.Singamsetty@Sun.COM 				mountpoint = vfstab[j].vfs_mountp;
265*7563SPrasad.Singamsetty@Sun.COM 			if (snprintf(putdevcmd, lastlen, "/usr/bin/putdev "
266*7563SPrasad.Singamsetty@Sun.COM 			    "-a %s cdevice=%s bdevice=%s "
267*7563SPrasad.Singamsetty@Sun.COM 			    "desc=\"Disk Partition\" type=dpart "
268*7563SPrasad.Singamsetty@Sun.COM 			    "removable=false capacity=%llu dparttype=fs "
269*7563SPrasad.Singamsetty@Sun.COM 			    "fstype=%s mountpt=%s", dparttab[i].alias,
270*7563SPrasad.Singamsetty@Sun.COM 			    dparttab[i].cdevice, dparttab[i].bdevice,
271*7563SPrasad.Singamsetty@Sun.COM 			    dparttab[i].capacity, vfstab[j].vfs_fstype,
272*7563SPrasad.Singamsetty@Sun.COM 			    mountpoint) >= lastlen) {
2730Sstevel@tonic-gate 					(void) fprintf(stderr,
2740Sstevel@tonic-gate 					    "%s: Command too long: %s\n",
2750Sstevel@tonic-gate 					    cmd, putdevcmd);
2760Sstevel@tonic-gate 					exit(1);
2770Sstevel@tonic-gate 				}
2780Sstevel@tonic-gate 				(void) system(putdevcmd);
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 		free(dparttab[i].cdevice);
2810Sstevel@tonic-gate 		free(dparttab[i].bdevice);
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 	free(dparttab);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static void
do_hdisks(void)2870Sstevel@tonic-gate do_hdisks(void)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	DIR *dp;
2900Sstevel@tonic-gate 	struct dirent *dirp;
2910Sstevel@tonic-gate 	int drive = 1;	char disknm[MAXNAMLEN+1];
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	if ((dp = opendir("/dev/rdsk")) == NULL) {
2940Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: can't open /dev/rdsk\n", cmd);
2950Sstevel@tonic-gate 		return;
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	while ((dirp = readdir(dp)) != NULL) {
2990Sstevel@tonic-gate 		if (gmatch(dirp->d_name, "c[0-9]*s2")) {
3000Sstevel@tonic-gate 			(void) strcpy(disknm, dirp->d_name);
3010Sstevel@tonic-gate 			/*
3020Sstevel@tonic-gate 			 * now know off the 's2'
3030Sstevel@tonic-gate 			 */
3040Sstevel@tonic-gate 			disknm[strlen(disknm)-2] = '\0';
3050Sstevel@tonic-gate 			/*
3060Sstevel@tonic-gate 			 * And do it!
3070Sstevel@tonic-gate 			 */
3080Sstevel@tonic-gate 			hdisk(drive++, disknm);
3090Sstevel@tonic-gate 		}
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	(void) closedir(dp);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate  * Add device table entry for the cartridge tape drive.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate static void
tape(const int driveno,const char * drivenm)3200Sstevel@tonic-gate tape(const int driveno, const char *drivenm)
3210Sstevel@tonic-gate {
322*7563SPrasad.Singamsetty@Sun.COM 	if (snprintf(putdevcmd, lastlen, "/usr/bin/putdev -a ctape%d "
323*7563SPrasad.Singamsetty@Sun.COM 	    "cdevice=/dev/rmt/%s "
324*7563SPrasad.Singamsetty@Sun.COM 	    "desc=\"Tape Drive\" volume=\"tape\" "
325*7563SPrasad.Singamsetty@Sun.COM 	    "type=ctape removable=true capacity=45539 bufsize=15872 "
326*7563SPrasad.Singamsetty@Sun.COM 	    "erasecmd=\"/usr/bin/mt -f /dev/rmt/%s erase\" "
327*7563SPrasad.Singamsetty@Sun.COM 	    "removecmd=\"/usr/bin/mt -f /dev/rmt/%s offline\"",
3280Sstevel@tonic-gate 	    driveno, drivenm, drivenm, drivenm) >= lastlen) {
3290Sstevel@tonic-gate 		(void) fprintf(stderr,
3300Sstevel@tonic-gate 		    "%s: Command too long: %s\n", cmd, putdevcmd);
3310Sstevel@tonic-gate 		exit(1);
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	(void) system(putdevcmd);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate static void
do_tapes(void)3370Sstevel@tonic-gate do_tapes(void)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	DIR *dp;
3400Sstevel@tonic-gate 	struct dirent *dirp;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	if ((dp = opendir("/dev/rmt")) == NULL) {
3430Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: can't open /dev/rmt\n", cmd);
3440Sstevel@tonic-gate 		return;
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	while ((dirp = readdir(dp)) != NULL) {
3480Sstevel@tonic-gate 		if (gmatch(dirp->d_name, "[0-9]") ||
3490Sstevel@tonic-gate 		    gmatch(dirp->d_name, "[1-9][0-9]")) {
3500Sstevel@tonic-gate 			tape(atoi(dirp->d_name), dirp->d_name);
3510Sstevel@tonic-gate 		}
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	(void) closedir(dp);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate static void
initialize(void)3580Sstevel@tonic-gate initialize(void)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	FILE		*fp;
3610Sstevel@tonic-gate 	int		i;
3620Sstevel@tonic-gate 	struct vfstab	vfsent;
3630Sstevel@tonic-gate 	char		*criteria[5];
3640Sstevel@tonic-gate 	char		**olddevlist;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * Build a copy of vfstab in memory for later use.
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	if ((fp = fopen("/etc/vfstab", "r")) == NULL) {
3700Sstevel@tonic-gate 		(void) fprintf(stderr,
3710Sstevel@tonic-gate 		    "%s: can't update device tables:Can't open /etc/vfstab\n",
372*7563SPrasad.Singamsetty@Sun.COM 		    cmd);
3730Sstevel@tonic-gate 		exit(1);
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	/*
3770Sstevel@tonic-gate 	 * Go through the vfstab file once to get the number of entries so
3780Sstevel@tonic-gate 	 * we can allocate the right amount of contiguous memory.
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	vfsnum = 0;
3810Sstevel@tonic-gate 	while (getvfsent(fp, &vfsent) == 0)
3820Sstevel@tonic-gate 		vfsnum++;
3830Sstevel@tonic-gate 	rewind(fp);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	if ((vfstab = (struct vfstab *)malloc(vfsnum * sizeof (struct vfstab)))
3860Sstevel@tonic-gate 	    == NULL) {
3870Sstevel@tonic-gate 		(void) fprintf(stderr,
388*7563SPrasad.Singamsetty@Sun.COM 		    "%s: can't update device tables:Out of memory\n", cmd);
3890Sstevel@tonic-gate 		exit(1);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/*
3930Sstevel@tonic-gate 	 * Go through the vfstab file one more time to populate our copy in
3940Sstevel@tonic-gate 	 * memory.  We only populate the fields we'll need.
3950Sstevel@tonic-gate 	 */
3960Sstevel@tonic-gate 	i = 0;
3970Sstevel@tonic-gate 	while (getvfsent(fp, &vfsent) == 0 && i < vfsnum) {
3980Sstevel@tonic-gate 		if (vfsent.vfs_special == NULL)
3990Sstevel@tonic-gate 			vfstab[i].vfs_special = NULL;
4000Sstevel@tonic-gate 		else
4010Sstevel@tonic-gate 			vfstab[i].vfs_special = memstr(vfsent.vfs_special);
4020Sstevel@tonic-gate 		if (vfsent.vfs_mountp == NULL)
4030Sstevel@tonic-gate 			vfstab[i].vfs_mountp = NULL;
4040Sstevel@tonic-gate 		else
4050Sstevel@tonic-gate 			vfstab[i].vfs_mountp = memstr(vfsent.vfs_mountp);
4060Sstevel@tonic-gate 		if (vfsent.vfs_fstype == NULL)
4070Sstevel@tonic-gate 			vfstab[i].vfs_fstype = NULL;
4080Sstevel@tonic-gate 		else
4090Sstevel@tonic-gate 			vfstab[i].vfs_fstype = memstr(vfsent.vfs_fstype);
4100Sstevel@tonic-gate 		i++;
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 	(void) fclose(fp);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	/*
4150Sstevel@tonic-gate 	 * Now remove all current entries of type disk, dpart, ctape
4160Sstevel@tonic-gate 	 * and diskette from the device and device group tables.
4170Sstevel@tonic-gate 	 * Any changes made manually since the last time this command
4180Sstevel@tonic-gate 	 * was run will be lost.  Note that after this we are committed
4190Sstevel@tonic-gate 	 * to try our best to rebuild the tables (i.e. the command
4200Sstevel@tonic-gate 	 * should try not to fail completely after this point).
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	criteria[0] = "type=disk";
4230Sstevel@tonic-gate 	criteria[1] = "type=dpart";
4240Sstevel@tonic-gate 	criteria[2] = "type=ctape";
4250Sstevel@tonic-gate 	criteria[3] = "type=diskette";
4260Sstevel@tonic-gate 	criteria[4] = (char *)NULL;
4270Sstevel@tonic-gate 	olddevlist = getdev((char **)NULL, criteria, 0);
4280Sstevel@tonic-gate 	_enddevtab();	/* getdev() should do this but doesn't */
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	putdevcmd = malloc(ORIGLEN);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if (putdevcmd == NULL) {
4330Sstevel@tonic-gate 		perror("malloc");
434*7563SPrasad.Singamsetty@Sun.COM 		exit(-1);
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	(void) memset(putdevcmd, 0, ORIGLEN);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	for (i = 0; olddevlist[i] != (char *)NULL; i++) {
4400Sstevel@tonic-gate 		if (snprintf(putdevcmd, lastlen,
4410Sstevel@tonic-gate 		    "/usr/bin/putdev -d %s", olddevlist[i]) >= lastlen) {
4420Sstevel@tonic-gate 			(void) fprintf(stderr,
4430Sstevel@tonic-gate 			    "%s: Command too long: %s\n", cmd, putdevcmd);
4440Sstevel@tonic-gate 			exit(1);
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 		(void) system(putdevcmd);
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	(void) sprintf(putdevcmd, "/usr/bin/putdgrp -d disk 2>/dev/null");
4500Sstevel@tonic-gate 	(void) system(putdevcmd);
4510Sstevel@tonic-gate 	(void) sprintf(putdevcmd, "/usr/bin/putdgrp -d dpart 2>/dev/null");
4520Sstevel@tonic-gate 	(void) system(putdevcmd);
4530Sstevel@tonic-gate 	(void) sprintf(putdevcmd, "/usr/bin/putdgrp -d ctape 2>/dev/null");
4540Sstevel@tonic-gate 	(void) system(putdevcmd);
4550Sstevel@tonic-gate 	(void) sprintf(putdevcmd, "/usr/bin/putdgrp -d diskette 2>/dev/null");
4560Sstevel@tonic-gate 	(void) system(putdevcmd);
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * Update the dgroup.tab file with information from the updated device.tab.
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate static void
mkdgroups(void)4640Sstevel@tonic-gate mkdgroups(void)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	int	i;
4670Sstevel@tonic-gate 	char	*criteria[2];
4680Sstevel@tonic-gate 	char	**devlist;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	criteria[1] = (char *)NULL;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	criteria[0] = "type=disk";
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	devlist = getdev((char **)NULL, criteria, DTAB_ANDCRITERIA);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	(void) sprintf(putdevcmd, "/usr/bin/putdgrp disk");
4770Sstevel@tonic-gate 	for (i = 0; devlist[i] != (char *)NULL; i++) {
4780Sstevel@tonic-gate 		checkandresize((strlen(putdevcmd) + strlen(devlist[i]) + 2));
4790Sstevel@tonic-gate 		(void) strcat(putdevcmd, " ");
4800Sstevel@tonic-gate 		(void) strcat(putdevcmd, devlist[i]);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 	if (i != 0)
4830Sstevel@tonic-gate 		(void) system(putdevcmd);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	criteria[0] = "type=dpart";
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	devlist = getdev((char **)NULL, criteria, DTAB_ANDCRITERIA);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	(void) sprintf(putdevcmd, "/usr/bin/putdgrp dpart");
4900Sstevel@tonic-gate 	for (i = 0; devlist[i] != (char *)NULL; i++) {
4910Sstevel@tonic-gate 		checkandresize((strlen(putdevcmd) + strlen(devlist[i]) + 2));
4920Sstevel@tonic-gate 		(void) strcat(putdevcmd, " ");
4930Sstevel@tonic-gate 		(void) strcat(putdevcmd, devlist[i]);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 	if (i != 0)
4960Sstevel@tonic-gate 		(void) system(putdevcmd);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	criteria[0] = "type=ctape";
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	devlist = getdev((char **)NULL, criteria, DTAB_ANDCRITERIA);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	(void) sprintf(putdevcmd, "/usr/bin/putdgrp ctape");
5030Sstevel@tonic-gate 	for (i = 0; devlist[i] != (char *)NULL; i++) {
5040Sstevel@tonic-gate 		checkandresize((strlen(putdevcmd) + strlen(devlist[i]) + 2));
5050Sstevel@tonic-gate 		(void) strcat(putdevcmd, " ");
5060Sstevel@tonic-gate 		(void) strcat(putdevcmd, devlist[i]);
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 	if (i != 0)
5090Sstevel@tonic-gate 		(void) system(putdevcmd);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	criteria[0] = "type=diskette";
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	devlist = getdev((char **)NULL, criteria, DTAB_ANDCRITERIA);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	(void) sprintf(putdevcmd, "/usr/bin/putdgrp diskette");
5160Sstevel@tonic-gate 	for (i = 0; devlist[i] != (char *)NULL; i++) {
5170Sstevel@tonic-gate 		checkandresize((strlen(putdevcmd) + strlen(devlist[i]) + 2));
5180Sstevel@tonic-gate 		(void) strcat(putdevcmd, " ");
5190Sstevel@tonic-gate 		(void) strcat(putdevcmd, devlist[i]);
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	if (i != 0)
5220Sstevel@tonic-gate 		(void) system(putdevcmd);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate static void
checkandresize(int size)5260Sstevel@tonic-gate checkandresize(int size)
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	if (size >= lastlen) {
5290Sstevel@tonic-gate 		putdevcmd = realloc(putdevcmd, lastlen * 2);
5300Sstevel@tonic-gate 		lastlen = lastlen * 2;
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate /*ARGSUSED*/
535644Sakaplan int
main(int argc,char ** argv)5360Sstevel@tonic-gate main(int argc, char **argv)
5370Sstevel@tonic-gate {
5380Sstevel@tonic-gate 	(void) strncpy(cmd, argv[0], 80);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	initialize();
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/*
5430Sstevel@tonic-gate 	 * AT&T code looked at the 3B2 EDT here.  Since we have a known-good
5440Sstevel@tonic-gate 	 * /dev directory ( presuming 'disks' has already been run), we simply
5450Sstevel@tonic-gate 	 * look in the /dev subdirectories.
5460Sstevel@tonic-gate 	 */
5470Sstevel@tonic-gate 	do_hdisks();
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	do_fdisks();
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	do_tapes();
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/*
5540Sstevel@tonic-gate 	 * Update the dgroup.tab file.
5550Sstevel@tonic-gate 	 */
5560Sstevel@tonic-gate 	mkdgroups();
5570Sstevel@tonic-gate 
558644Sakaplan 	return (0);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate }
561