]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/diskpart/diskpart.c
This commit was generated by cvs2svn to compensate for changes in r73393,
[FreeBSD/FreeBSD.git] / usr.sbin / diskpart / diskpart.c
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1988, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)diskpart.c  8.3 (Berkeley) 11/30/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47
48 /*
49  * Program to calculate standard disk partition sizes.
50  */
51 #include <sys/param.h>
52 #define DKTYPENAMES
53 #include <sys/disklabel.h>
54
55 #include <ctype.h>
56 #include <err.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 #define for_now                 /* show all of `c' partition for disklabel */
63 #define NPARTITIONS     8
64 #define PART(x)         (x - 'a')
65
66 /*
67  * Default partition sizes, where they exist.
68  */
69 #define NDEFAULTS       4
70 int     defpart[NDEFAULTS][NPARTITIONS] = {
71    { 15884, 66880, 0, 15884, 307200, 0, 0, 291346 },    /* ~ 356+ Mbytes */
72    { 15884, 33440, 0, 15884, 55936, 0, 0, 291346 },     /* ~ 206-355 Mbytes */
73    { 15884, 33440, 0, 15884, 55936, 0, 0, 0 },          /* ~ 61-205 Mbytes */
74    { 15884, 10032, 0, 15884, 0, 0, 0, 0 },              /* ~ 20-60 Mbytes */
75 };
76
77 /*
78  * Each array defines a layout for a disk;
79  * that is, the collection of partitions totally
80  * covers the physical space on a disk.
81  */
82 #define NLAYOUTS        3
83 char    layouts[NLAYOUTS][NPARTITIONS] = {
84    { 'a', 'b', 'h', 'g' },
85    { 'a', 'b', 'h', 'd', 'e', 'f' },
86    { 'c' },
87 };
88
89 /*
90  * Default disk block and disk block fragment
91  * sizes for each file system.  Those file systems
92  * with zero block and frag sizes are special cases
93  * (e.g. swap areas or for access to the entire device).
94  */
95 struct  partition defparam[NPARTITIONS] = {
96         { 0, 0, 1024, FS_UNUSED, 8, 0 },                /* a */
97         { 0, 0, 1024, FS_SWAP,   8, 0 },                /* b */
98         { 0, 0, 1024, FS_UNUSED, 8, 0 },                /* c */
99         { 0, 0,  512, FS_UNUSED, 8, 0 },                /* d */
100         { 0, 0, 1024, FS_UNUSED, 8, 0 },                /* e */
101         { 0, 0, 1024, FS_UNUSED, 8, 0 },                /* f */
102         { 0, 0, 1024, FS_UNUSED, 8, 0 },                /* g */
103         { 0, 0, 1024, FS_UNUSED, 8, 0 }                 /* h */
104 };
105
106 /*
107  * Each disk has some space reserved for a bad sector
108  * forwarding table.  DEC standard 144 uses the first
109  * 5 even numbered sectors in the last track of the
110  * last cylinder for replicated storage of the bad sector
111  * table; another 126 sectors past this is needed as a
112  * pool of replacement sectors.
113  */
114 int     badsecttable = 126;     /* # sectors */
115
116 int     pflag;                  /* print device driver partition tables */
117 int     dflag;                  /* print disktab entry */
118
119 struct  disklabel *promptfordisk();
120 int     gettype __P((char *, char **));
121 static void usage __P((void));
122 int     ustrcmp __P((char *, char *));
123
124 int
125 main(argc, argv)
126         int argc;
127         char *argv[];
128 {
129         struct disklabel *dp;
130         register int curcyl, spc, def, part, layout, j;
131         int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
132         int totsize = 0;
133         char *lp, *tyname = NULL;
134         int ch;
135
136         while ((ch = getopt(argc, argv, "dps:")) != -1)
137                 switch(ch) {
138                 case 'd':
139                         dflag++;
140                         break;
141                 case 'p':
142                         pflag++;
143                         break;
144                 case 's':
145                         totsize = atoi(optarg);
146                         break;
147                 default:
148                         usage();
149                 }
150         argv += optind;
151         argc -= optind;
152
153         if (argc != 1)
154                 usage();
155
156         dp = getdiskbyname(*argv);
157         if (dp == NULL) {
158                 if (isatty(0))
159                         dp = promptfordisk(*argv);
160                 if (dp == NULL)
161                         errx(2, "%s: unknown disk type", *argv);
162         } else {
163                 if (dp->d_flags & D_REMOVABLE)
164                         tyname = "removable";
165                 else if (dp->d_flags & D_RAMDISK)
166                         tyname = "simulated";
167                 else
168                         tyname = "winchester";
169         }
170         spc = dp->d_secpercyl;
171         /*
172          * Bad sector table contains one track for the replicated
173          * copies of the table and enough full tracks preceding
174          * the last track to hold the pool of free blocks to which
175          * bad sectors are mapped.
176          * If disk size was specified explicitly, use specified size.
177          */
178         if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT &&
179             totsize == 0) {
180                 badsecttable = dp->d_nsectors +
181                     roundup(badsecttable, dp->d_nsectors);
182                 threshhold = howmany(spc, badsecttable);
183         } else {
184                 badsecttable = 0;
185                 threshhold = 0;
186         }
187         /*
188          * If disk size was specified, recompute number of cylinders
189          * that may be used, and set badsecttable to any remaining
190          * fraction of the last cylinder.
191          */
192         if (totsize != 0) {
193                 dp->d_ncylinders = howmany(totsize, spc);
194                 badsecttable = spc * dp->d_ncylinders - totsize;
195         }
196
197         /*
198          * Figure out if disk is large enough for
199          * expanded swap area and 'd', 'e', and 'f'
200          * partitions.  Otherwise, use smaller defaults
201          * based on RK07.
202          */
203         for (def = 0; def < NDEFAULTS; def++) {
204                 curcyl = 0;
205                 for (part = PART('a'); part < NPARTITIONS; part++)
206                         curcyl += howmany(defpart[def][part], spc);
207                 if (curcyl < dp->d_ncylinders - threshhold)
208                         break;
209         }
210         if (def >= NDEFAULTS)
211                 errx(3, "%s: disk too small, calculate by hand", *argv);
212
213         /*
214          * Calculate number of cylinders allocated to each disk
215          * partition.  We may waste a bit of space here, but it's
216          * in the interest of (very backward) compatibility
217          * (for mixed disk systems).
218          */
219         for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
220                 numcyls[part] = 0;
221                 if (defpart[def][part] != 0) {
222                         numcyls[part] = howmany(defpart[def][part], spc);
223                         curcyl += numcyls[part];
224                 }
225         }
226         numcyls[PART('f')] = dp->d_ncylinders - curcyl;
227         numcyls[PART('g')] =
228                 numcyls[PART('d')] + numcyls[PART('e')] + numcyls[PART('f')];
229         numcyls[PART('c')] = dp->d_ncylinders;
230         defpart[def][PART('f')] = numcyls[PART('f')] * spc - badsecttable;
231         defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
232         defpart[def][PART('c')] = numcyls[PART('c')] * spc;
233 #ifndef for_now
234         if (totsize || !pflag)
235 #else
236         if (totsize)
237 #endif
238                 defpart[def][PART('c')] -= badsecttable;
239
240         /*
241          * Calculate starting cylinder number for each partition.
242          * Note the 'h' partition is physically located before the
243          * 'g' or 'd' partition.  This is reflected in the layout
244          * arrays defined above.
245          */
246         for (layout = 0; layout < NLAYOUTS; layout++) {
247                 curcyl = 0;
248                 for (lp = layouts[layout]; *lp != 0; lp++) {
249                         startcyl[PART(*lp)] = curcyl;
250                         curcyl += numcyls[PART(*lp)];
251                 }
252         }
253
254         if (pflag) {
255                 printf("}, %s_sizes[%d] = {\n", dp->d_typename, NPARTITIONS);
256                 for (part = PART('a'); part < NPARTITIONS; part++) {
257                         if (numcyls[part] == 0) {
258                                 printf("\t0,\t0,\n");
259                                 continue;
260                         }
261                         if (dp->d_type != DTYPE_MSCP) {
262                                printf("\t%d,\t%d,\t\t/* %c=cyl %d thru %d */\n",
263                                         defpart[def][part], startcyl[part],
264                                         'A' + part, startcyl[part],
265                                         startcyl[part] + numcyls[part] - 1);
266                                 continue;
267                         }
268                         printf("\t%d,\t%d,\t\t/* %c=sectors %d thru %d */\n",
269                                 defpart[def][part], spc * startcyl[part],
270                                 'A' + part, spc * startcyl[part],
271                                 spc * startcyl[part] + defpart[def][part] - 1);
272                 }
273                 exit(0);
274         }
275         if (dflag) {
276                 int nparts;
277
278                 /*
279                  * In case the disk is in the ``in-between'' range
280                  * where the 'g' partition is smaller than the 'h'
281                  * partition, reverse the frag sizes so the /usr partition
282                  * is always set up with a frag size larger than the
283                  * user's partition.
284                  */
285                 if (defpart[def][PART('g')] < defpart[def][PART('h')]) {
286                         int temp;
287
288                         temp = defparam[PART('h')].p_fsize;
289                         defparam[PART('h')].p_fsize =
290                                 defparam[PART('g')].p_fsize;
291                         defparam[PART('g')].p_fsize = temp;
292                 }
293                 printf("%s:\\\n", dp->d_typename);
294                 printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname,
295                         dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
296                 if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
297                         printf("sc#%d:", dp->d_secpercyl);
298                 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT)
299                         printf("sf:");
300                 printf("\\\n\t:dt=%s:", dktypenames[dp->d_type]);
301                 for (part = NDDATA - 1; part >= 0; part--)
302                         if (dp->d_drivedata[part])
303                                 break;
304                 for (j = 0; j <= part; j++)
305                         printf("d%d#%d:", j, dp->d_drivedata[j]);
306                 printf("\\\n");
307                 for (nparts = 0, part = PART('a'); part < NPARTITIONS; part++)
308                         if (defpart[def][part] != 0)
309                                 nparts++;
310                 for (part = PART('a'); part < NPARTITIONS; part++) {
311                         if (defpart[def][part] == 0)
312                                 continue;
313                         printf("\t:p%c#%d:", 'a' + part, defpart[def][part]);
314                         printf("o%c#%d:b%c#%d:f%c#%d:",
315                             'a' + part, spc * startcyl[part],
316                             'a' + part,
317                             defparam[part].p_frag * defparam[part].p_fsize,
318                             'a' + part, defparam[part].p_fsize);
319                         if (defparam[part].p_fstype == FS_SWAP)
320                                 printf("t%c=swap:", 'a' + part);
321                         nparts--;
322                         printf("%s\n", nparts > 0 ? "\\" : "");
323                 }
324 #ifdef for_now
325                 defpart[def][PART('c')] -= badsecttable;
326                 part = PART('c');
327                 printf("#\t:p%c#%d:", 'a' + part, defpart[def][part]);
328                 printf("o%c#%d:b%c#%d:f%c#%d:\n",
329                     'a' + part, spc * startcyl[part],
330                     'a' + part,
331                     defparam[part].p_frag * defparam[part].p_fsize,
332                     'a' + part, defparam[part].p_fsize);
333 #endif
334                 exit(0);
335         }
336         printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
337                 dp->d_typename, dp->d_nsectors, dp->d_ntracks,
338                 dp->d_ncylinders);
339         printf("\n    Partition\t   Size\t Offset\t   Range\n");
340         for (part = PART('a'); part < NPARTITIONS; part++) {
341                 printf("\t%c\t", 'a' + part);
342                 if (numcyls[part] == 0) {
343                         printf(" unused\n");
344                         continue;
345                 }
346                 printf("%7d\t%7d\t%4d - %d%s\n",
347                         defpart[def][part], startcyl[part] * spc,
348                         startcyl[part], startcyl[part] + numcyls[part] - 1,
349                         defpart[def][part] % spc ? "*" : "");
350         }
351         return(0);
352 }
353
354 static void
355 usage()
356 {
357         fprintf(stderr, "usage: disktab [-p] [-d] [-s size] disk-type\n");
358         exit(1);
359 }
360
361 struct disklabel disk;
362
363 struct  field {
364         char            *f_name;
365         char            *f_defaults;
366         u_int32_t       *f_location;
367 } fields[] = {
368         { "sector size",                "512",  &disk.d_secsize },
369         { "#sectors/track",             0,      &disk.d_nsectors },
370         { "#tracks/cylinder",           0,      &disk.d_ntracks },
371         { "#cylinders",                 0,      &disk.d_ncylinders },
372         { 0, 0, 0 },
373 };
374
375 char *
376 mygets(buf)
377         char *buf;
378 {
379         size_t len;
380
381         if (fgets(buf, BUFSIZ, stdin) == NULL)
382                 buf[0] = '\0';
383         len = strlen(buf);
384         if (len != 0 && buf[len - 1] == '\n')
385                 buf[len - 1] = '\0';
386         return (buf);
387 }
388
389 struct disklabel *
390 promptfordisk(name)
391         char *name;
392 {
393         struct disklabel *dp = &disk;
394         struct field *fp;
395         int i;
396         char buf[BUFSIZ], **tp, *cp;
397
398         strncpy(dp->d_typename, name, sizeof(dp->d_typename));
399         fprintf(stderr,
400                 "%s: unknown disk type, want to supply parameters (y/n)? ",
401                 name);
402         (void) mygets(buf);
403         if (*buf != 'y')
404                 return ((struct disklabel *)0);
405         for (;;) {
406                 fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]);
407                 (void) mygets(buf);
408                 if (buf[0] == 0) {
409                         dp->d_type = 1;
410                         break;
411                 }
412                 if ((i = gettype(buf, dktypenames)) >= 0) {
413                         dp->d_type = i;
414                         break;
415                 }
416                 fprintf(stderr, "%s: unrecognized controller type\n", buf);
417                 fprintf(stderr, "use one of:\n");
418                 for (tp = dktypenames; *tp; tp++)
419                         if (index(*tp, ' ') == 0)
420                                 fprintf(stderr, "\t%s\n", *tp);
421         }
422 gettype:
423         dp->d_flags = 0;
424         fprintf(stderr, "type (winchester|removable|simulated)? ");
425         (void) mygets(buf);
426         if (strcmp(buf, "removable") == 0)
427                 dp->d_flags = D_REMOVABLE;
428         else if (strcmp(buf, "simulated") == 0)
429                 dp->d_flags = D_RAMDISK;
430         else if (strcmp(buf, "winchester")) {
431                 fprintf(stderr, "%s: bad disk type\n", buf);
432                 goto gettype;
433         }
434         strncpy(dp->d_typename, buf, sizeof(dp->d_typename));
435         fprintf(stderr, "(type <cr> to get default value, if only one)\n");
436         if (dp->d_type == DTYPE_SMD)
437            fprintf(stderr, "Do %ss support bad144 bad block forwarding (yes)? ",
438                 dp->d_typename);
439         (void) mygets(buf);
440         if (*buf != 'n')
441                 dp->d_flags |= D_BADSECT;
442         for (fp = fields; fp->f_name != NULL; fp++) {
443 again:
444                 fprintf(stderr, "%s ", fp->f_name);
445                 if (fp->f_defaults != NULL)
446                         fprintf(stderr, "(%s)", fp->f_defaults);
447                 fprintf(stderr, "? ");
448                 cp = mygets(buf);
449                 if (*cp == '\0') {
450                         if (fp->f_defaults == NULL) {
451                                 fprintf(stderr, "no default value\n");
452                                 goto again;
453                         }
454                         cp = fp->f_defaults;
455                 }
456                 *fp->f_location = atol(cp);
457                 if (*fp->f_location == 0) {
458                         fprintf(stderr, "%s: bad value\n", cp);
459                         goto again;
460                 }
461         }
462         fprintf(stderr, "sectors/cylinder (%d)? ",
463             dp->d_nsectors * dp->d_ntracks);
464         (void) mygets(buf);
465         if (buf[0] == 0)
466                 dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks;
467         else
468                 dp->d_secpercyl = atol(buf);
469         fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n");
470         for (i = 0; i < NDDATA; i++) {
471                 fprintf(stderr, "d%d? ", i);
472                 (void) mygets(buf);
473                 if (buf[0] == 0)
474                         break;
475                 dp->d_drivedata[i] = atol(buf);
476         }
477         return (dp);
478 }
479
480 int
481 gettype(t, names)
482         char *t;
483         char **names;
484 {
485         register char **nm;
486
487         for (nm = names; *nm; nm++)
488                 if (ustrcmp(t, *nm) == 0)
489                         return (nm - names);
490         if (isdigit(*t))
491                 return (atoi(t));
492         return (-1);
493 }
494
495 int
496 ustrcmp(s1, s2)
497         register char *s1, *s2;
498 {
499 #define lower(c)        (islower(c) ? (c) : tolower(c))
500
501         for (; *s1; s1++, s2++) {
502                 if (*s1 == *s2)
503                         continue;
504                 if (isalpha(*s1) && isalpha(*s2) &&
505                     lower(*s1) == lower(*s2))
506                         continue;
507                 return (*s2 - *s1);
508         }
509         return (0);
510 }