diskpart.c revision 1.8 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 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 /* static char sccsid[] = "from: @(#)diskpart.c 8.3 (Berkeley) 11/30/94"; */
42 static char rcsid[] = "$NetBSD: diskpart.c,v 1.8 1997/06/21 09:07:09 lukem Exp $";
43 #endif /* not lint */
44
45 /*
46 * Program to calculate standard disk partition sizes.
47 */
48 #include <sys/param.h>
49 #define DKTYPENAMES
50 #include <sys/disklabel.h>
51
52 #include <ctype.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #define for_now /* show all of `c' partition for disklabel */
60 #define NPARTITIONS 8
61 #define PART(x) (x - 'a')
62
63 /*
64 * Default partition sizes, where they exist.
65 */
66 #define NDEFAULTS 4
67 int defpart[NDEFAULTS][NPARTITIONS] = {
68 { 15884, 66880, 0, 15884, 307200, 0, 0, 291346 }, /* ~ 356+ Mbytes */
69 { 15884, 33440, 0, 15884, 55936, 0, 0, 291346 }, /* ~ 206-355 Mbytes */
70 { 15884, 33440, 0, 15884, 55936, 0, 0, 0 }, /* ~ 61-205 Mbytes */
71 { 15884, 10032, 0, 15884, 0, 0, 0, 0 }, /* ~ 20-60 Mbytes */
72 };
73
74 /*
75 * Each array defines a layout for a disk;
76 * that is, the collection of partitions totally
77 * covers the physical space on a disk.
78 */
79 #define NLAYOUTS 3
80 char layouts[NLAYOUTS][NPARTITIONS] = {
81 { 'a', 'b', 'h', 'g' },
82 { 'a', 'b', 'h', 'd', 'e', 'f' },
83 { 'c' },
84 };
85
86 /*
87 * Default disk block and disk block fragment
88 * sizes for each file system. Those file systems
89 * with zero block and frag sizes are special cases
90 * (e.g. swap areas or for access to the entire device).
91 */
92 struct partition defparam[NPARTITIONS] = {
93 { 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* a */
94 { 0, 0, 1024, FS_SWAP, 8, { 0 }, }, /* b */
95 { 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* c */
96 { 0, 0, 512, FS_UNUSED, 8, { 0 }, }, /* d */
97 { 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* e */
98 { 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* f */
99 { 0, 0, 1024, FS_UNUSED, 8, { 0 }, }, /* g */
100 { 0, 0, 1024, FS_UNUSED, 8, { 0 }, } /* h */
101 };
102
103 /*
104 * Each disk has some space reserved for a bad sector
105 * forwarding table. DEC standard 144 uses the first
106 * 5 even numbered sectors in the last track of the
107 * last cylinder for replicated storage of the bad sector
108 * table; another 126 sectors past this is needed as a
109 * pool of replacement sectors.
110 */
111 int badsecttable = 126; /* # sectors */
112
113 int pflag; /* print device driver partition tables */
114 int dflag; /* print disktab entry */
115
116 struct disklabel *promptfordisk __P((const char *));
117 void usage __P((void));
118 int gettype __P((const char *, char **));
119
120 int
121 main(argc, argv)
122 int argc;
123 char *argv[];
124 {
125
126 struct disklabel *dp;
127 int curcyl, spc, def, part, layout, j, ch;
128 int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
129 off_t totsize = 0;
130 char *lp, *tyname;
131
132 while ((ch = getopt(argc, argv, "pds:")) != -1) {
133 switch (ch) {
134 case 'd':
135 dflag++;
136 break;
137
138 case 'p':
139 pflag++;
140 break;
141
142 case 's':
143 totsize = strtoul(optarg, &lp, 10);
144 if (*lp != '\0')
145 usage();
146 break;
147
148 case '?':
149 default:
150 usage();
151 /* NOTREACHED */
152 }
153 }
154 argc -= optind;
155 argv += optind;
156
157 if (argc != 1) {
158 usage();
159 /* NOTREACHED */
160 }
161
162 dp = getdiskbyname(*argv);
163 if (dp == NULL) {
164 if (isatty(0))
165 dp = promptfordisk(*argv);
166 if (dp == NULL) {
167 fprintf(stderr, "%s: unknown disk type\n", *argv);
168 exit(1);
169 }
170 }
171 if (dp->d_flags & D_REMOVABLE)
172 tyname = "removable";
173 else if (dp->d_flags & D_RAMDISK)
174 tyname = "simulated";
175 else
176 tyname = "winchester";
177 spc = dp->d_secpercyl;
178 /*
179 * Bad sector table contains one track for the replicated
180 * copies of the table and enough full tracks preceding
181 * the last track to hold the pool of free blocks to which
182 * bad sectors are mapped.
183 * If disk size was specified explicitly, use specified size.
184 */
185 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT &&
186 totsize == 0) {
187 badsecttable = dp->d_nsectors +
188 roundup(badsecttable, dp->d_nsectors);
189 threshhold = howmany(spc, badsecttable);
190 } else {
191 badsecttable = 0;
192 threshhold = 0;
193 }
194 /*
195 * If disk size was specified, recompute number of cylinders
196 * that may be used, and set badsecttable to any remaining
197 * fraction of the last cylinder.
198 */
199 if (totsize != 0) {
200 dp->d_ncylinders = howmany(totsize, spc);
201 badsecttable = spc * dp->d_ncylinders - totsize;
202 }
203
204 /*
205 * Figure out if disk is large enough for
206 * expanded swap area and 'd', 'e', and 'f'
207 * partitions. Otherwise, use smaller defaults
208 * based on RK07.
209 */
210 for (def = 0; def < NDEFAULTS; def++) {
211 curcyl = 0;
212 for (part = PART('a'); part < NPARTITIONS; part++)
213 curcyl += howmany(defpart[def][part], spc);
214 if (curcyl < dp->d_ncylinders - threshhold)
215 break;
216 }
217 if (def >= NDEFAULTS) {
218 fprintf(stderr, "%s: disk too small, calculate by hand\n",
219 *argv);
220 exit(1);
221 }
222
223 /*
224 * Calculate number of cylinders allocated to each disk
225 * partition. We may waste a bit of space here, but it's
226 * in the interest of (very backward) compatibility
227 * (for mixed disk systems).
228 */
229 for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
230 numcyls[part] = 0;
231 if (defpart[def][part] != 0) {
232 numcyls[part] = howmany(defpart[def][part], spc);
233 curcyl += numcyls[part];
234 }
235 }
236 numcyls[PART('f')] = dp->d_ncylinders - curcyl;
237 numcyls[PART('g')] =
238 numcyls[PART('d')] + numcyls[PART('e')] + numcyls[PART('f')];
239 numcyls[PART('c')] = dp->d_ncylinders;
240 defpart[def][PART('f')] = numcyls[PART('f')] * spc - badsecttable;
241 defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
242 defpart[def][PART('c')] = numcyls[PART('c')] * spc;
243 #ifndef for_now
244 if (totsize || !pflag)
245 #else
246 if (totsize)
247 #endif
248 defpart[def][PART('c')] -= badsecttable;
249
250 /*
251 * Calculate starting cylinder number for each partition.
252 * Note the 'h' partition is physically located before the
253 * 'g' or 'd' partition. This is reflected in the layout
254 * arrays defined above.
255 */
256 for (layout = 0; layout < NLAYOUTS; layout++) {
257 curcyl = 0;
258 for (lp = layouts[layout]; *lp != 0; lp++) {
259 startcyl[PART(*lp)] = curcyl;
260 curcyl += numcyls[PART(*lp)];
261 }
262 }
263
264 if (pflag) {
265 printf("}, %s_sizes[%d] = {\n", dp->d_typename, NPARTITIONS);
266 for (part = PART('a'); part < NPARTITIONS; part++) {
267 if (numcyls[part] == 0) {
268 printf("\t0,\t0,\n");
269 continue;
270 }
271 if (dp->d_type != DTYPE_MSCP) {
272 printf("\t%d,\t%d,\t\t/* %c=cyl %d thru %d */\n",
273 defpart[def][part], startcyl[part],
274 'A' + part, startcyl[part],
275 startcyl[part] + numcyls[part] - 1);
276 continue;
277 }
278 printf("\t%d,\t%d,\t\t/* %c=sectors %d thru %d */\n",
279 defpart[def][part], spc * startcyl[part],
280 'A' + part, spc * startcyl[part],
281 spc * startcyl[part] + defpart[def][part] - 1);
282 }
283 exit(0);
284 }
285 if (dflag) {
286 int nparts;
287
288 /*
289 * In case the disk is in the ``in-between'' range
290 * where the 'g' partition is smaller than the 'h'
291 * partition, reverse the frag sizes so the /usr partition
292 * is always set up with a frag size larger than the
293 * user's partition.
294 */
295 if (defpart[def][PART('g')] < defpart[def][PART('h')]) {
296 int temp;
297
298 temp = defparam[PART('h')].p_fsize;
299 defparam[PART('h')].p_fsize =
300 defparam[PART('g')].p_fsize;
301 defparam[PART('g')].p_fsize = temp;
302 }
303 printf("%s:\\\n", dp->d_typename);
304 printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname,
305 dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
306 if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
307 printf("sc#%d:", dp->d_secpercyl);
308 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT)
309 printf("sf:");
310 printf("\\\n\t:dt=%s:", dktypenames[dp->d_type]);
311 for (part = NDDATA - 1; part >= 0; part--)
312 if (dp->d_drivedata[part])
313 break;
314 for (j = 0; j <= part; j++)
315 printf("d%d#%d:", j, dp->d_drivedata[j]);
316 printf("\\\n");
317 for (nparts = 0, part = PART('a'); part < NPARTITIONS; part++)
318 if (defpart[def][part] != 0)
319 nparts++;
320 for (part = PART('a'); part < NPARTITIONS; part++) {
321 if (defpart[def][part] == 0)
322 continue;
323 printf("\t:p%c#%d:", 'a' + part, defpart[def][part]);
324 printf("o%c#%d:b%c#%d:f%c#%d:",
325 'a' + part, spc * startcyl[part],
326 'a' + part,
327 defparam[part].p_frag * defparam[part].p_fsize,
328 'a' + part, defparam[part].p_fsize);
329 if (defparam[part].p_fstype == FS_SWAP)
330 printf("t%c=swap:", 'a' + part);
331 nparts--;
332 printf("%s\n", nparts > 0 ? "\\" : "");
333 }
334 #ifdef for_now
335 defpart[def][PART('c')] -= badsecttable;
336 part = PART('c');
337 printf("#\t:p%c#%d:", 'a' + part, defpart[def][part]);
338 printf("o%c#%d:b%c#%d:f%c#%d:\n",
339 'a' + part, spc * startcyl[part],
340 'a' + part,
341 defparam[part].p_frag * defparam[part].p_fsize,
342 'a' + part, defparam[part].p_fsize);
343 #endif
344 exit(0);
345 }
346 printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
347 dp->d_typename, dp->d_nsectors, dp->d_ntracks,
348 dp->d_ncylinders);
349 printf("\n Partition\t Size\t Offset\t Range\n");
350 for (part = PART('a'); part < NPARTITIONS; part++) {
351 printf("\t%c\t", 'a' + part);
352 if (numcyls[part] == 0) {
353 printf(" unused\n");
354 continue;
355 }
356 printf("%7d\t%7d\t%4d - %d%s\n",
357 defpart[def][part], startcyl[part] * spc,
358 startcyl[part], startcyl[part] + numcyls[part] - 1,
359 defpart[def][part] % spc ? "*" : "");
360 }
361 exit(0);
362 }
363
364 struct disklabel disk;
365
366 struct field {
367 char *f_name;
368 char *f_defaults;
369 u_int32_t *f_location;
370 } fields[] = {
371 { "sector size", "512", &disk.d_secsize },
372 { "#sectors/track", 0, &disk.d_nsectors },
373 { "#tracks/cylinder", 0, &disk.d_ntracks },
374 { "#cylinders", 0, &disk.d_ncylinders },
375 { 0, 0, 0 },
376 };
377
378 struct disklabel *
379 promptfordisk(name)
380 const char *name;
381 {
382 struct disklabel *dp = &disk;
383 struct field *fp;
384 int i;
385 char buf[BUFSIZ], **tp, *cp;
386
387 strncpy(dp->d_typename, name, sizeof(dp->d_typename));
388 fprintf(stderr,
389 "%s: unknown disk type, want to supply parameters (y/n)? ",
390 name);
391 if ((fgets(buf, BUFSIZ, stdin) == NULL) || buf[0] != 'y')
392 return ((struct disklabel *)0);
393 for (;;) {
394 fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]);
395 if (fgets(buf, BUFSIZ, stdin) == NULL)
396 return ((struct disklabel *)0);
397 if ((cp = strchr(buf, '\n')) != NULL)
398 *cp = '\0';
399 if (buf[0] == '\0') {
400 dp->d_type = 1;
401 break;
402 }
403 if ((i = gettype(buf, dktypenames)) >= 0) {
404 dp->d_type = i;
405 break;
406 }
407 fprintf(stderr, "%s: unrecognized controller type\n", buf);
408 fprintf(stderr, "use one of:\n");
409 for (tp = dktypenames; *tp; tp++)
410 if (strchr(*tp, ' ') == 0)
411 fprintf(stderr, "\t%s\n", *tp);
412 }
413 gettype:
414 dp->d_flags = 0;
415 fprintf(stderr, "type (winchester|removable|simulated)? ");
416 if (fgets(buf, BUFSIZ, stdin) == NULL)
417 return ((struct disklabel *)0);
418 if ((cp = strchr(buf, '\n')) != NULL)
419 *cp = '\0';
420 if (buf[0] == '\0')
421 goto gettype;
422 switch (buf[0]) {
423 case 'r':
424 dp->d_flags = D_REMOVABLE;
425 break;
426 case 's':
427 dp->d_flags = D_RAMDISK;
428 break;
429 case 'w':
430 break;
431 default:
432 fprintf(stderr, "%s: bad disk type\n", buf);
433 /* FALLTHROUGH */
434 case '\0':
435 goto gettype;
436 }
437 fprintf(stderr, "(type <cr> to get default value, if only one)\n");
438 if (dp->d_type == DTYPE_SMD) {
439 fprintf(stderr,
440 "Do '%s' disks support bad144 bad block forwarding (yes)? ",
441 dp->d_typename);
442 if (fgets(buf, BUFSIZ, stdin) == NULL)
443 return ((struct disklabel *)0);
444 if (buf[0] != 'n')
445 dp->d_flags |= D_BADSECT;
446 }
447 for (fp = fields; fp->f_name != NULL; fp++) {
448 again:
449 fprintf(stderr, "%s ", fp->f_name);
450 if (fp->f_defaults != NULL)
451 fprintf(stderr, "(%s)", fp->f_defaults);
452 fprintf(stderr, "? ");
453 if (fgets(buf, BUFSIZ, stdin) == NULL)
454 return ((struct disklabel *)0);
455 if ((cp = strchr(buf, '\n')) != NULL)
456 *cp = '\0';
457 cp = buf;
458 if (*cp == '\0') {
459 if (fp->f_defaults == NULL) {
460 fprintf(stderr, "no default value\n");
461 goto again;
462 }
463 cp = fp->f_defaults;
464 }
465 *fp->f_location = atol(cp);
466 if (*fp->f_location == 0) {
467 fprintf(stderr, "%s: bad value\n", cp);
468 goto again;
469 }
470 }
471 fprintf(stderr, "sectors/cylinder (%d)? ",
472 dp->d_nsectors * dp->d_ntracks);
473 if (fgets(buf, BUFSIZ, stdin) == NULL)
474 return ((struct disklabel *)0);
475 if ((cp = strchr(buf, '\n')) != NULL)
476 *cp = '\0';
477 if (buf[0] == 0)
478 dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks;
479 else
480 dp->d_secpercyl = atol(buf);
481 fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n");
482 for (i = 0; i < NDDATA; i++) {
483 fprintf(stderr, "d%d? ", i);
484 if (fgets(buf, BUFSIZ, stdin) == NULL)
485 return ((struct disklabel *)0);
486 if ((cp = strchr(buf, '\n')) != NULL)
487 *cp = '\0';
488 if (buf[0] == 0)
489 break;
490 dp->d_drivedata[i] = atol(buf);
491 }
492 return (dp);
493 }
494
495 int
496 gettype(t, names)
497 const char *t;
498 char **names;
499 {
500 char **nm;
501
502 for (nm = names; *nm; nm++)
503 if (strcasecmp(t, *nm) == 0)
504 return (nm - names);
505 if (isdigit(*t))
506 return (atoi(t));
507 return (-1);
508 }
509
510 void
511 usage(void)
512 {
513 (void)fprintf(stderr, "Usage: diskpart [-dp] [-s size] disk-type\n");
514 exit(1);
515 }
516