2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)diskpart.c 8.3 (Berkeley) 11/30/94";
44 static const char rcsid[] =
45 "$FreeBSD: src/usr.sbin/diskpart/diskpart.c,v 1.11.2.2 2002/12/04 16:24:08 roam Exp $";
49 * Program to calculate standard disk partition sizes.
51 #include <sys/param.h>
53 #include <sys/disklabel.h>
60 #define for_now /* show all of `c' partition for disklabel */
62 #define PART(x) (x - 'a')
65 * Default partition sizes, where they exist.
68 int defpart[NDEFAULTS][NPARTITIONS] = {
69 { 15884, 66880, 0, 15884, 307200, 0, 0, 291346 }, /* ~ 356+ Mbytes */
70 { 15884, 33440, 0, 15884, 55936, 0, 0, 291346 }, /* ~ 206-355 Mbytes */
71 { 15884, 33440, 0, 15884, 55936, 0, 0, 0 }, /* ~ 61-205 Mbytes */
72 { 15884, 10032, 0, 15884, 0, 0, 0, 0 }, /* ~ 20-60 Mbytes */
76 * Each array defines a layout for a disk;
77 * that is, the collection of partitions totally
78 * covers the physical space on a disk.
81 char layouts[NLAYOUTS][NPARTITIONS] = {
82 { 'a', 'b', 'h', 'g' },
83 { 'a', 'b', 'h', 'd', 'e', 'f' },
88 * Default disk block and disk block fragment
89 * sizes for each file system. Those file systems
90 * with zero block and frag sizes are special cases
91 * (e.g. swap areas or for access to the entire device).
93 struct partition defparam[NPARTITIONS] = {
94 { 0, 0, 1024, FS_UNUSED, 8, 0 }, /* a */
95 { 0, 0, 1024, FS_SWAP, 8, 0 }, /* b */
96 { 0, 0, 1024, FS_UNUSED, 8, 0 }, /* c */
97 { 0, 0, 512, FS_UNUSED, 8, 0 }, /* d */
98 { 0, 0, 1024, FS_UNUSED, 8, 0 }, /* e */
99 { 0, 0, 1024, FS_UNUSED, 8, 0 }, /* f */
100 { 0, 0, 1024, FS_UNUSED, 8, 0 }, /* g */
101 { 0, 0, 1024, FS_UNUSED, 8, 0 } /* h */
105 * Each disk has some space reserved for a bad sector
106 * forwarding table. DEC standard 144 uses the first
107 * 5 even numbered sectors in the last track of the
108 * last cylinder for replicated storage of the bad sector
109 * table; another 126 sectors past this is needed as a
110 * pool of replacement sectors.
112 int badsecttable = 126; /* # sectors */
114 int pflag; /* print device driver partition tables */
115 int dflag; /* print disktab entry */
117 struct disklabel *promptfordisk();
118 static void usage __P((void));
125 struct disklabel *dp;
126 register int curcyl, spc, def, part, layout, j;
127 int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS];
132 while ((ch = getopt(argc, argv, "dps:")) != EOF)
143 totsize = atoi(optarg);
153 dp = getdiskbyname(*argv);
156 dp = promptfordisk(*argv);
158 errx(2, "%s: unknown disk type", *argv);
160 if (dp->d_flags & D_REMOVABLE)
161 tyname = "removable";
162 else if (dp->d_flags & D_RAMDISK)
163 tyname = "simulated";
165 tyname = "winchester";
167 spc = dp->d_secpercyl;
169 * Bad sector table contains one track for the replicated
170 * copies of the table and enough full tracks preceding
171 * the last track to hold the pool of free blocks to which
172 * bad sectors are mapped.
173 * If disk size was specified explicitly, use specified size.
175 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT &&
177 badsecttable = dp->d_nsectors +
178 roundup(badsecttable, dp->d_nsectors);
179 threshhold = howmany(spc, badsecttable);
185 * If disk size was specified, recompute number of cylinders
186 * that may be used, and set badsecttable to any remaining
187 * fraction of the last cylinder.
190 dp->d_ncylinders = howmany(totsize, spc);
191 badsecttable = spc * dp->d_ncylinders - totsize;
195 * Figure out if disk is large enough for
196 * expanded swap area and 'd', 'e', and 'f'
197 * partitions. Otherwise, use smaller defaults
200 for (def = 0; def < NDEFAULTS; def++) {
202 for (part = PART('a'); part < NPARTITIONS; part++)
203 curcyl += howmany(defpart[def][part], spc);
204 if (curcyl < dp->d_ncylinders - threshhold)
207 if (def >= NDEFAULTS)
208 errx(3, "%s: disk too small, calculate by hand", *argv);
211 * Calculate number of cylinders allocated to each disk
212 * partition. We may waste a bit of space here, but it's
213 * in the interest of (very backward) compatibility
214 * (for mixed disk systems).
216 for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) {
218 if (defpart[def][part] != 0) {
219 numcyls[part] = howmany(defpart[def][part], spc);
220 curcyl += numcyls[part];
223 numcyls[PART('f')] = dp->d_ncylinders - curcyl;
225 numcyls[PART('d')] + numcyls[PART('e')] + numcyls[PART('f')];
226 numcyls[PART('c')] = dp->d_ncylinders;
227 defpart[def][PART('f')] = numcyls[PART('f')] * spc - badsecttable;
228 defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable;
229 defpart[def][PART('c')] = numcyls[PART('c')] * spc;
231 if (totsize || !pflag)
235 defpart[def][PART('c')] -= badsecttable;
238 * Calculate starting cylinder number for each partition.
239 * Note the 'h' partition is physically located before the
240 * 'g' or 'd' partition. This is reflected in the layout
241 * arrays defined above.
243 for (layout = 0; layout < NLAYOUTS; layout++) {
245 for (lp = layouts[layout]; *lp != 0; lp++) {
246 startcyl[PART(*lp)] = curcyl;
247 curcyl += numcyls[PART(*lp)];
252 printf("}, %s_sizes[%d] = {\n", dp->d_typename, NPARTITIONS);
253 for (part = PART('a'); part < NPARTITIONS; part++) {
254 if (numcyls[part] == 0) {
255 printf("\t0,\t0,\n");
258 if (dp->d_type != DTYPE_MSCP) {
259 printf("\t%d,\t%d,\t\t/* %c=cyl %d thru %d */\n",
260 defpart[def][part], startcyl[part],
261 'A' + part, startcyl[part],
262 startcyl[part] + numcyls[part] - 1);
265 printf("\t%d,\t%d,\t\t/* %c=sectors %d thru %d */\n",
266 defpart[def][part], spc * startcyl[part],
267 'A' + part, spc * startcyl[part],
268 spc * startcyl[part] + defpart[def][part] - 1);
276 * In case the disk is in the ``in-between'' range
277 * where the 'g' partition is smaller than the 'h'
278 * partition, reverse the frag sizes so the /usr partition
279 * is always set up with a frag size larger than the
282 if (defpart[def][PART('g')] < defpart[def][PART('h')]) {
285 temp = defparam[PART('h')].p_fsize;
286 defparam[PART('h')].p_fsize =
287 defparam[PART('g')].p_fsize;
288 defparam[PART('g')].p_fsize = temp;
290 printf("%s:\\\n", dp->d_typename);
291 printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname,
292 dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders);
293 if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks)
294 printf("sc#%d:", dp->d_secpercyl);
295 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT)
297 printf("\\\n\t:dt=%s:", dktypenames[dp->d_type]);
298 for (part = NDDATA - 1; part >= 0; part--)
299 if (dp->d_drivedata[part])
301 for (j = 0; j <= part; j++)
302 printf("d%d#%d:", j, dp->d_drivedata[j]);
304 for (nparts = 0, part = PART('a'); part < NPARTITIONS; part++)
305 if (defpart[def][part] != 0)
307 for (part = PART('a'); part < NPARTITIONS; part++) {
308 if (defpart[def][part] == 0)
310 printf("\t:p%c#%d:", 'a' + part, defpart[def][part]);
311 printf("o%c#%d:b%c#%d:f%c#%d:",
312 'a' + part, spc * startcyl[part],
314 defparam[part].p_frag * defparam[part].p_fsize,
315 'a' + part, defparam[part].p_fsize);
316 if (defparam[part].p_fstype == FS_SWAP)
317 printf("t%c=swap:", 'a' + part);
319 printf("%s\n", nparts > 0 ? "\\" : "");
322 defpart[def][PART('c')] -= badsecttable;
324 printf("#\t:p%c#%d:", 'a' + part, defpart[def][part]);
325 printf("o%c#%d:b%c#%d:f%c#%d:\n",
326 'a' + part, spc * startcyl[part],
328 defparam[part].p_frag * defparam[part].p_fsize,
329 'a' + part, defparam[part].p_fsize);
333 printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n",
334 dp->d_typename, dp->d_nsectors, dp->d_ntracks,
336 printf("\n Partition\t Size\t Offset\t Range\n");
337 for (part = PART('a'); part < NPARTITIONS; part++) {
338 printf("\t%c\t", 'a' + part);
339 if (numcyls[part] == 0) {
343 printf("%7d\t%7d\t%4d - %d%s\n",
344 defpart[def][part], startcyl[part] * spc,
345 startcyl[part], startcyl[part] + numcyls[part] - 1,
346 defpart[def][part] % spc ? "*" : "");
353 fprintf(stderr, "usage: diskpart [-p] [-d] [-s size] disk-type\n");
357 struct disklabel disk;
362 u_int32_t *f_location;
364 { "sector size", "512", &disk.d_secsize },
365 { "#sectors/track", 0, &disk.d_nsectors },
366 { "#tracks/cylinder", 0, &disk.d_ntracks },
367 { "#cylinders", 0, &disk.d_ncylinders },
377 if (fgets(buf, BUFSIZ, stdin) == NULL)
380 if (len != 0 && buf[len - 1] == '\n')
389 struct disklabel *dp = &disk;
392 char buf[BUFSIZ], **tp, *cp;
394 strncpy(dp->d_typename, name, sizeof(dp->d_typename));
396 "%s: unknown disk type, want to supply parameters (y/n)? ",
400 return ((struct disklabel *)0);
402 fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]);
408 if ((i = gettype(buf, dktypenames)) >= 0) {
412 fprintf(stderr, "%s: unrecognized controller type\n", buf);
413 fprintf(stderr, "use one of:\n");
414 for (tp = dktypenames; *tp; tp++)
415 if (index(*tp, ' ') == 0)
416 fprintf(stderr, "\t%s\n", *tp);
420 fprintf(stderr, "type (winchester|removable|simulated)? ");
422 if (strcmp(buf, "removable") == 0)
423 dp->d_flags = D_REMOVABLE;
424 else if (strcmp(buf, "simulated") == 0)
425 dp->d_flags = D_RAMDISK;
426 else if (strcmp(buf, "winchester")) {
427 fprintf(stderr, "%s: bad disk type\n", buf);
430 strncpy(dp->d_typename, buf, sizeof(dp->d_typename));
431 fprintf(stderr, "(type <cr> to get default value, if only one)\n");
432 if (dp->d_type == DTYPE_SMD)
433 fprintf(stderr, "Do %ss support bad144 bad block forwarding (yes)? ",
437 dp->d_flags |= D_BADSECT;
438 for (fp = fields; fp->f_name != NULL; fp++) {
440 fprintf(stderr, "%s ", fp->f_name);
441 if (fp->f_defaults != NULL)
442 fprintf(stderr, "(%s)", fp->f_defaults);
443 fprintf(stderr, "? ");
446 if (fp->f_defaults == NULL) {
447 fprintf(stderr, "no default value\n");
452 *fp->f_location = atol(cp);
453 if (*fp->f_location == 0) {
454 fprintf(stderr, "%s: bad value\n", cp);
458 fprintf(stderr, "sectors/cylinder (%d)? ",
459 dp->d_nsectors * dp->d_ntracks);
462 dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks;
464 dp->d_secpercyl = atol(buf);
465 fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n");
466 for (i = 0; i < NDDATA; i++) {
467 fprintf(stderr, "d%d? ", i);
471 dp->d_drivedata[i] = atol(buf);
482 for (nm = names; *nm; nm++)
483 if (ustrcmp(t, *nm) == 0)
491 register char *s1, *s2;
493 #define lower(c) (islower(c) ? (c) : tolower(c))
495 for (; *s1; s1++, s2++) {
498 if (isalpha(*s1) && isalpha(*s2) &&
499 lower(*s1) == lower(*s2))