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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 #ifndef lint 32 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"); 34 #endif /* not lint */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "from: @(#)diskpart.c 8.3 (Berkeley) 11/30/94"; 39 #else 40 __RCSID("$NetBSD: diskpart.c,v 1.15 2004/10/29 21:21:42 dsl Exp $"); 41 #endif 42 #endif /* not lint */ 43 44 /* 45 * Program to calculate standard disk partition sizes. 46 */ 47 #include <sys/param.h> 48 #define DKTYPENAMES 49 #include <sys/disklabel.h> 50 51 #include <ctype.h> 52 #include <disktab.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 int gettype __P((const char *, const char *const *)); 117 int main __P((int, char **)); 118 struct disklabel *promptfordisk __P((const char *)); 119 void usage __P((void)); 120 121 int 122 main(argc, argv) 123 int argc; 124 char *argv[]; 125 { 126 127 struct disklabel *dp; 128 int curcyl, spc, def, part, layout, j, ch; 129 int threshhold, numcyls[NPARTITIONS], startcyl[NPARTITIONS]; 130 off_t totsize = 0; 131 char *lp, *tyname; 132 133 while ((ch = getopt(argc, argv, "pds:")) != -1) { 134 switch (ch) { 135 case 'd': 136 dflag++; 137 break; 138 139 case 'p': 140 pflag++; 141 break; 142 143 case 's': 144 totsize = strtoul(optarg, &lp, 10); 145 if (*lp != '\0') 146 usage(); 147 break; 148 149 case '?': 150 default: 151 usage(); 152 /* NOTREACHED */ 153 } 154 } 155 argc -= optind; 156 argv += optind; 157 158 if (argc != 1) { 159 usage(); 160 /* NOTREACHED */ 161 } 162 163 dp = getdiskbyname(*argv); 164 if (dp == NULL) { 165 if (isatty(0)) 166 dp = promptfordisk(*argv); 167 if (dp == NULL) { 168 fprintf(stderr, "%s: unknown disk type\n", *argv); 169 exit(1); 170 } 171 } 172 if (dp->d_flags & D_REMOVABLE) 173 tyname = "removable"; 174 else if (dp->d_flags & D_RAMDISK) 175 tyname = "simulated"; 176 else 177 tyname = "winchester"; 178 spc = dp->d_secpercyl; 179 /* 180 * Bad sector table contains one track for the replicated 181 * copies of the table and enough full tracks preceding 182 * the last track to hold the pool of free blocks to which 183 * bad sectors are mapped. 184 * If disk size was specified explicitly, use specified size. 185 */ 186 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT && 187 totsize == 0) { 188 badsecttable = dp->d_nsectors + 189 roundup(badsecttable, dp->d_nsectors); 190 threshhold = howmany(spc, badsecttable); 191 } else { 192 badsecttable = 0; 193 threshhold = 0; 194 } 195 /* 196 * If disk size was specified, recompute number of cylinders 197 * that may be used, and set badsecttable to any remaining 198 * fraction of the last cylinder. 199 */ 200 if (totsize != 0) { 201 dp->d_ncylinders = howmany(totsize, spc); 202 badsecttable = spc * dp->d_ncylinders - totsize; 203 } 204 205 /* 206 * Figure out if disk is large enough for 207 * expanded swap area and 'd', 'e', and 'f' 208 * partitions. Otherwise, use smaller defaults 209 * based on RK07. 210 */ 211 for (def = 0; def < NDEFAULTS; def++) { 212 curcyl = 0; 213 for (part = PART('a'); part < NPARTITIONS; part++) 214 curcyl += howmany(defpart[def][part], spc); 215 if (curcyl < dp->d_ncylinders - threshhold) 216 break; 217 } 218 if (def >= NDEFAULTS) { 219 fprintf(stderr, "%s: disk too small, calculate by hand\n", 220 *argv); 221 exit(1); 222 } 223 224 /* 225 * Calculate number of cylinders allocated to each disk 226 * partition. We may waste a bit of space here, but it's 227 * in the interest of (very backward) compatibility 228 * (for mixed disk systems). 229 */ 230 for (curcyl = 0, part = PART('a'); part < NPARTITIONS; part++) { 231 numcyls[part] = 0; 232 if (defpart[def][part] != 0) { 233 numcyls[part] = howmany(defpart[def][part], spc); 234 curcyl += numcyls[part]; 235 } 236 } 237 numcyls[PART('f')] = dp->d_ncylinders - curcyl; 238 numcyls[PART('g')] = 239 numcyls[PART('d')] + numcyls[PART('e')] + numcyls[PART('f')]; 240 numcyls[PART('c')] = dp->d_ncylinders; 241 defpart[def][PART('f')] = numcyls[PART('f')] * spc - badsecttable; 242 defpart[def][PART('g')] = numcyls[PART('g')] * spc - badsecttable; 243 defpart[def][PART('c')] = numcyls[PART('c')] * spc; 244 #ifndef for_now 245 if (totsize || !pflag) 246 #else 247 if (totsize) 248 #endif 249 defpart[def][PART('c')] -= badsecttable; 250 251 /* 252 * Calculate starting cylinder number for each partition. 253 * Note the 'h' partition is physically located before the 254 * 'g' or 'd' partition. This is reflected in the layout 255 * arrays defined above. 256 */ 257 for (layout = 0; layout < NLAYOUTS; layout++) { 258 curcyl = 0; 259 for (lp = layouts[layout]; *lp != 0; lp++) { 260 startcyl[PART(*lp)] = curcyl; 261 curcyl += numcyls[PART(*lp)]; 262 } 263 } 264 265 if (pflag) { 266 printf("}, %s_sizes[%d] = {\n", dp->d_typename, NPARTITIONS); 267 for (part = PART('a'); part < NPARTITIONS; part++) { 268 if (numcyls[part] == 0) { 269 printf("\t0,\t0,\n"); 270 continue; 271 } 272 if (dp->d_type != DTYPE_MSCP) { 273 printf("\t%d,\t%d,\t\t/* %c=cyl %d thru %d */\n", 274 defpart[def][part], startcyl[part], 275 'A' + part, startcyl[part], 276 startcyl[part] + numcyls[part] - 1); 277 continue; 278 } 279 printf("\t%d,\t%d,\t\t/* %c=sectors %d thru %d */\n", 280 defpart[def][part], spc * startcyl[part], 281 'A' + part, spc * startcyl[part], 282 spc * startcyl[part] + defpart[def][part] - 1); 283 } 284 exit(0); 285 } 286 if (dflag) { 287 int nparts; 288 289 /* 290 * In case the disk is in the ``in-between'' range 291 * where the 'g' partition is smaller than the 'h' 292 * partition, reverse the frag sizes so the /usr partition 293 * is always set up with a frag size larger than the 294 * user's partition. 295 */ 296 if (defpart[def][PART('g')] < defpart[def][PART('h')]) { 297 int temp; 298 299 temp = defparam[PART('h')].p_fsize; 300 defparam[PART('h')].p_fsize = 301 defparam[PART('g')].p_fsize; 302 defparam[PART('g')].p_fsize = temp; 303 } 304 printf("%s:\\\n", dp->d_typename); 305 printf("\t:ty=%s:ns#%d:nt#%d:nc#%d:", tyname, 306 dp->d_nsectors, dp->d_ntracks, dp->d_ncylinders); 307 if (dp->d_secpercyl != dp->d_nsectors * dp->d_ntracks) 308 printf("sc#%d:", dp->d_secpercyl); 309 if (dp->d_type == DTYPE_SMD && dp->d_flags & D_BADSECT) 310 printf("sf:"); 311 printf("\\\n\t:dt=%s:", dktypenames[dp->d_type]); 312 for (part = NDDATA - 1; part >= 0; part--) 313 if (dp->d_drivedata[part]) 314 break; 315 for (j = 0; j <= part; j++) 316 printf("d%d#%d:", j, dp->d_drivedata[j]); 317 printf("\\\n"); 318 for (nparts = 0, part = PART('a'); part < NPARTITIONS; part++) 319 if (defpart[def][part] != 0) 320 nparts++; 321 for (part = PART('a'); part < NPARTITIONS; part++) { 322 if (defpart[def][part] == 0) 323 continue; 324 printf("\t:p%c#%d:", 'a' + part, defpart[def][part]); 325 printf("o%c#%d:b%c#%d:f%c#%d:", 326 'a' + part, spc * startcyl[part], 327 'a' + part, 328 defparam[part].p_frag * defparam[part].p_fsize, 329 'a' + part, defparam[part].p_fsize); 330 if (defparam[part].p_fstype == FS_SWAP) 331 printf("t%c=swap:", 'a' + part); 332 nparts--; 333 printf("%s\n", nparts > 0 ? "\\" : ""); 334 } 335 #ifdef for_now 336 defpart[def][PART('c')] -= badsecttable; 337 part = PART('c'); 338 printf("#\t:p%c#%d:", 'a' + part, defpart[def][part]); 339 printf("o%c#%d:b%c#%d:f%c#%d:\n", 340 'a' + part, spc * startcyl[part], 341 'a' + part, 342 defparam[part].p_frag * defparam[part].p_fsize, 343 'a' + part, defparam[part].p_fsize); 344 #endif 345 exit(0); 346 } 347 printf("%s: #sectors/track=%d, #tracks/cylinder=%d #cylinders=%d\n", 348 dp->d_typename, dp->d_nsectors, dp->d_ntracks, 349 dp->d_ncylinders); 350 printf("\n Partition\t Size\t Offset\t Range\n"); 351 for (part = PART('a'); part < NPARTITIONS; part++) { 352 printf("\t%c\t", 'a' + part); 353 if (numcyls[part] == 0) { 354 printf(" unused\n"); 355 continue; 356 } 357 printf("%7d\t%7d\t%4d - %d%s\n", 358 defpart[def][part], startcyl[part] * spc, 359 startcyl[part], startcyl[part] + numcyls[part] - 1, 360 defpart[def][part] % spc ? "*" : ""); 361 } 362 exit(0); 363 } 364 365 struct disklabel disk; 366 367 struct field { 368 char *f_name; 369 char *f_defaults; 370 u_int32_t *f_location; 371 } fields[] = { 372 { "sector size", "512", &disk.d_secsize }, 373 { "#sectors/track", 0, &disk.d_nsectors }, 374 { "#tracks/cylinder", 0, &disk.d_ntracks }, 375 { "#cylinders", 0, &disk.d_ncylinders }, 376 { 0, 0, 0 }, 377 }; 378 379 struct disklabel * 380 promptfordisk(name) 381 const char *name; 382 { 383 struct disklabel *dp = &disk; 384 struct field *fp; 385 int i; 386 const char *const *tp; 387 char buf[BUFSIZ], *cp; 388 389 strncpy(dp->d_typename, name, sizeof(dp->d_typename)); 390 fprintf(stderr, 391 "%s: unknown disk type, want to supply parameters (y/n)? ", 392 name); 393 if ((fgets(buf, BUFSIZ, stdin) == NULL) || buf[0] != 'y') 394 return ((struct disklabel *)0); 395 for (;;) { 396 fprintf(stderr, "Disk/controller type (%s)? ", dktypenames[1]); 397 if (fgets(buf, BUFSIZ, stdin) == NULL) 398 return ((struct disklabel *)0); 399 if ((cp = strchr(buf, '\n')) != NULL) 400 *cp = '\0'; 401 if (buf[0] == '\0') { 402 dp->d_type = 1; 403 break; 404 } 405 if ((i = gettype(buf, dktypenames)) >= 0) { 406 dp->d_type = i; 407 break; 408 } 409 fprintf(stderr, "%s: unrecognized controller type\n", buf); 410 fprintf(stderr, "use one of:\n"); 411 for (tp = dktypenames; *tp; tp++) 412 if (strchr(*tp, ' ') == 0) 413 fprintf(stderr, "\t%s\n", *tp); 414 } 415 gettype: 416 dp->d_flags = 0; 417 fprintf(stderr, "type (winchester|removable|simulated)? "); 418 if (fgets(buf, BUFSIZ, stdin) == NULL) 419 return ((struct disklabel *)0); 420 if ((cp = strchr(buf, '\n')) != NULL) 421 *cp = '\0'; 422 if (buf[0] == '\0') 423 goto gettype; 424 switch (buf[0]) { 425 case 'r': 426 dp->d_flags = D_REMOVABLE; 427 break; 428 case 's': 429 dp->d_flags = D_RAMDISK; 430 break; 431 case 'w': 432 break; 433 default: 434 fprintf(stderr, "%s: bad disk type\n", buf); 435 /* FALLTHROUGH */ 436 case '\0': 437 goto gettype; 438 } 439 fprintf(stderr, "(type <cr> to get default value, if only one)\n"); 440 if (dp->d_type == DTYPE_SMD) { 441 fprintf(stderr, 442 "Do '%s' disks support bad144 bad block forwarding (yes)? ", 443 dp->d_typename); 444 if (fgets(buf, BUFSIZ, stdin) == NULL) 445 return ((struct disklabel *)0); 446 if (buf[0] != 'n') 447 dp->d_flags |= D_BADSECT; 448 } 449 for (fp = fields; fp->f_name != NULL; fp++) { 450 again: 451 fprintf(stderr, "%s ", fp->f_name); 452 if (fp->f_defaults != NULL) 453 fprintf(stderr, "(%s)", fp->f_defaults); 454 fprintf(stderr, "? "); 455 if (fgets(buf, BUFSIZ, stdin) == NULL) 456 return ((struct disklabel *)0); 457 if ((cp = strchr(buf, '\n')) != NULL) 458 *cp = '\0'; 459 cp = buf; 460 if (*cp == '\0') { 461 if (fp->f_defaults == NULL) { 462 fprintf(stderr, "no default value\n"); 463 goto again; 464 } 465 cp = fp->f_defaults; 466 } 467 *fp->f_location = atol(cp); 468 if (*fp->f_location == 0) { 469 fprintf(stderr, "%s: bad value\n", cp); 470 goto again; 471 } 472 } 473 fprintf(stderr, "sectors/cylinder (%d)? ", 474 dp->d_nsectors * dp->d_ntracks); 475 if (fgets(buf, BUFSIZ, stdin) == NULL) 476 return ((struct disklabel *)0); 477 if ((cp = strchr(buf, '\n')) != NULL) 478 *cp = '\0'; 479 if (buf[0] == 0) 480 dp->d_secpercyl = dp->d_nsectors * dp->d_ntracks; 481 else 482 dp->d_secpercyl = atol(buf); 483 fprintf(stderr, "Drive-type-specific parameters, <cr> to terminate:\n"); 484 for (i = 0; i < NDDATA; i++) { 485 fprintf(stderr, "d%d? ", i); 486 if (fgets(buf, BUFSIZ, stdin) == NULL) 487 return ((struct disklabel *)0); 488 if ((cp = strchr(buf, '\n')) != NULL) 489 *cp = '\0'; 490 if (buf[0] == 0) 491 break; 492 dp->d_drivedata[i] = atol(buf); 493 } 494 return (dp); 495 } 496 497 int 498 gettype(t, names) 499 const char *t; 500 const char *const *names; 501 { 502 const char *const *nm; 503 504 for (nm = names; *nm; nm++) 505 if (strcasecmp(t, *nm) == 0) 506 return (nm - names); 507 if (isdigit((unsigned char)*t)) 508 return (atoi(t)); 509 return (-1); 510 } 511 512 void 513 usage(void) 514 { 515 (void)fprintf(stderr, "usage: diskpart [-dp] [-s size] disk-type\n"); 516 exit(1); 517 } 518