2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Symmetric Computer Systems.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static const char copyright[] =
39 "@(#) Copyright (c) 1987, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94";
46 /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
48 static const char rcsid[] =
52 #include <sys/param.h>
53 #include <sys/errno.h>
58 #include <sys/disklabel.h>
59 #include <ufs/ffs/fs.h>
68 #include "pathnames.h"
71 * Disklabel: read and write disklabels.
72 * The label is usually placed on one of the first sectors of the disk.
73 * Many machines also place a bootstrap in the same area,
74 * in which case the label is embedded in the bootstrap.
75 * The bootstrap source must leave space at the proper offset
76 * for the label on such machines.
80 #define BBSIZE 8192 /* size of boot area, with label */
86 #if defined(__alpha__) || defined(hp300) || defined(hp800)
93 void makelabel __P((char *, char *, struct disklabel *));
94 int writelabel __P((int, char *, struct disklabel *));
95 void l_perror __P((char *));
96 struct disklabel * readlabel __P((int));
97 struct disklabel * makebootarea __P((char *, struct disklabel *, int));
98 void display __P((FILE *, struct disklabel *));
99 int edit __P((struct disklabel *, int));
100 int editit __P((void));
101 char * skip __P((char *));
102 char * word __P((char *));
103 int getasciilabel __P((FILE *, struct disklabel *));
104 int checklabel __P((struct disklabel *));
105 void setbootflag __P((struct disklabel *));
106 void Warning (char *, ...);
107 void usage __P((void));
108 extern u_short dkcksum __P((struct disklabel *));
109 struct disklabel * getvirginlabel __P((void));
111 #define DEFEDITOR _PATH_VI
112 #define streq(a,b) (strcmp(a,b) == 0)
116 char tmpfil[] = _PATH_TMP;
118 char namebuf[BBSIZE], *np = namebuf;
119 struct disklabel lab;
120 struct disklabel *readlabel(), *makebootarea();
121 char bootarea[BBSIZE];
124 int installboot; /* non-zero if we should install a boot program */
125 char *bootbuf; /* pointer to buffer with remainder of boot prog */
126 int bootsize; /* size of remaining boot program */
127 char *xxboot; /* primary boot */
128 char *bootxx; /* secondary boot */
129 char boot0[MAXPATHLEN];
130 char boot1[MAXPATHLEN];
134 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
141 #define OPTIONS "BNRWb:ders:w"
143 #define OPTIONS "BNRWb:ers:w"
151 register struct disklabel *lp;
153 int ch, f = 0, flag, error = 0;
156 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
228 if (dkname[0] != '/') {
229 (void)sprintf(np, "%sr%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
231 np += strlen(specname) + 1;
234 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
235 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
236 (void)sprintf(specname, "%sr%s", _PATH_DEV, dkname);
237 np = namebuf + strlen(specname) + 1;
238 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
241 err(4, "%s", specname);
257 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
258 err(4, "ioctl DIOCWLABEL");
266 error = checklabel(lp);
271 if (installboot && argc == 3) {
272 makelabel(argv[2], 0, &lab);
276 * We only called makelabel() for its side effect
277 * of setting the bootstrap file names. Discard
278 * all changes to `lab' so that all values in the
279 * final label come from the ASCII label.
281 bzero((char *)&lab, sizeof(lab));
286 if (!(t = fopen(argv[1], "r")))
287 err(4, "%s", argv[1]);
288 if (!getasciilabel(t, &lab))
290 lp = makebootarea(bootarea, &lab, f);
292 error = writelabel(f, bootarea, lp);
302 makelabel(argv[1], name, &lab);
303 lp = makebootarea(bootarea, &lab, f);
305 if (checklabel(lp) == 0)
306 error = writelabel(f, bootarea, lp);
311 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
312 err(4, "ioctl DIOCWLABEL");
318 struct disklabel tlab;
323 makelabel(argv[1], 0, &lab);
324 lp = makebootarea(bootarea, &lab, f);
326 if (checklabel(lp) == 0)
327 error = writelabel(f, bootarea, lp);
336 * Construct a prototype disklabel from /etc/disktab. As a side
337 * effect, set the names of the primary and secondary boot files
341 makelabel(type, name, lp)
343 register struct disklabel *lp;
345 register struct disklabel *dp;
347 if (strcmp(type, "auto") == 0)
348 dp = getvirginlabel();
350 dp = getdiskbyname(type);
352 errx(1, "%s: unknown disk type", type);
356 * Set bootstrap name(s).
357 * 1. If set from command line, use those,
358 * 2. otherwise, check if disktab specifies them (b0 or b1),
359 * 3. otherwise, makebootarea() will choose ones based on the name
360 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
362 if (!xxboot && lp->d_boot0) {
363 if (*lp->d_boot0 != '/')
364 (void)sprintf(boot0, "%s/%s",
365 _PATH_BOOTDIR, lp->d_boot0);
367 (void)strcpy(boot0, lp->d_boot0);
371 if (!bootxx && lp->d_boot1) {
372 if (*lp->d_boot1 != '/')
373 (void)sprintf(boot1, "%s/%s",
374 _PATH_BOOTDIR, lp->d_boot1);
376 (void)strcpy(boot1, lp->d_boot1);
381 /* d_packname is union d_boot[01], so zero */
382 bzero(lp->d_packname, sizeof(lp->d_packname));
384 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
388 writelabel(f, boot, lp)
391 register struct disklabel *lp;
403 lp->d_magic = DISKMAGIC;
404 lp->d_magic2 = DISKMAGIC;
406 lp->d_checksum = dkcksum(lp);
409 * First set the kernel disk label,
410 * then write a label to the raw disk.
411 * If the SDINFO ioctl fails because it is unimplemented,
412 * keep going; otherwise, the kernel consistency checks
413 * may prevent us from changing the current (in-core)
416 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
417 errno != ENODEV && errno != ENOTTY) {
418 l_perror("ioctl DIOCSDINFO");
421 (void)lseek(f, (off_t)0, SEEK_SET);
425 * Generate the bootblock checksum for the SRM console.
427 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
433 * write enable label sector before write (if necessary),
434 * disable after writing.
437 if (ioctl(f, DIOCWLABEL, &flag) < 0)
438 warn("ioctl DIOCWLABEL");
439 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
445 * Output the remainder of the disklabel
447 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
453 (void) ioctl(f, DIOCWLABEL, &flag);
454 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
455 l_perror("ioctl DIOCWDINFO");
459 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
462 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
463 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
464 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
466 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
467 warn("alternate label %d write", i/2);
481 warnx("%s: no disk label on disk;", s);
483 "use \"disklabel -r\" to install initial label\n");
487 warnx("%s: label magic number or checksum is wrong!", s);
488 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
492 warnx("%s: open partition would move or shrink", s);
496 warnx("%s: '%c' partition must start at beginning of disk",
507 * Fetch disklabel for disk.
508 * Use ioctl to get label unless -r flag is given.
514 register struct disklabel *lp;
517 if (read(f, bootarea, BBSIZE) < BBSIZE)
518 err(4, "%s", specname);
519 for (lp = (struct disklabel *)bootarea;
520 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
521 lp = (struct disklabel *)((char *)lp + 16))
522 if (lp->d_magic == DISKMAGIC &&
523 lp->d_magic2 == DISKMAGIC)
525 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
526 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
529 "bad pack magic number (label is damaged, or pack is unlabeled)");
532 if (ioctl(f, DIOCGDINFO, lp) < 0)
533 err(4, "ioctl DIOCGDINFO");
539 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
540 * Returns a pointer to the disklabel portion of the bootarea.
543 makebootarea(boot, dp, f)
545 register struct disklabel *dp;
548 struct disklabel *lp;
565 if (dp->d_secsize == 0) {
566 dp->d_secsize = DEV_BSIZE;
567 dp->d_bbsize = BBSIZE;
569 lp = (struct disklabel *)
570 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
571 bzero((char *)lp, sizeof *lp);
574 * If we are not installing a boot program but we are installing a
575 * label on disk then we must read the current bootarea so we don't
576 * clobber the existing boot.
580 if (read(f, boot, BBSIZE) < BBSIZE)
581 err(4, "%s", specname);
582 bzero((char *)lp, sizeof *lp);
587 * We are installing a boot program. Determine the name(s) and
588 * read them into the appropriate places in the boot area.
590 if (!xxboot || !bootxx) {
592 if ((p = rindex(dkname, '/')) == NULL)
596 while (*p && !isdigit(*p))
601 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
606 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
613 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
614 xxboot, bootxx ? bootxx : "NONE");
619 * 1. One-piece bootstrap (hp300/hp800)
620 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
621 * is remembered and written later following the bootarea.
622 * 2. Two-piece bootstraps (vax/i386?/mips?)
623 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
624 * bytes of bootarea, remaining d_bbsize-d_secsize filled
627 b = open(xxboot, O_RDONLY);
629 err(4, "%s", xxboot);
634 * The i386 has the so-called fdisk table embedded into the
635 * primary bootstrap. We take care to not clobber it, but
636 * only if it does already contain some data. (Otherwise,
637 * the xxboot provides a template.)
639 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
640 err(4, "%s", xxboot);
641 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
643 if (read(b, boot, (int)dp->d_secsize) < 0)
644 err(4, "%s", xxboot);
647 for (i = DOSPARTOFF, found = 0;
648 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
650 found = tmpbuf[i] != 0;
652 memcpy((void *)&boot[DOSPARTOFF],
653 (void *)&tmpbuf[DOSPARTOFF],
654 NDOSPART * sizeof(struct dos_partition));
657 b = open(bootxx, O_RDONLY);
659 err(4, "%s", bootxx);
660 if (fstat(b, &sb) != 0)
661 err(4, "%s", bootxx);
662 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
663 errx(4, "%s too large", bootxx);
664 if (read(b, &boot[dp->d_secsize],
665 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
666 err(4, "%s", bootxx);
667 #else /* !(NUMBOOT > 1) */
670 * On the alpha, the primary bootstrap starts at the
671 * second sector of the boot area. The first sector
672 * contains the label and must be edited to contain the
673 * size and location of the primary bootstrap.
675 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
677 err(4, "%s", xxboot);
678 bootinfo = (u_long *)(boot + 480);
679 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
680 bootinfo[1] = 1; /* start at sector 1 */
681 bootinfo[2] = 0; /* flags (must be zero) */
682 #else /* !__alpha__ */
683 if (read(b, boot, (int)dp->d_bbsize) < 0)
684 err(4, "%s", xxboot);
685 #endif /* __alpha__ */
686 if (fstat(b, &sb) != 0)
687 err(4, "%s", xxboot);
688 bootsize = (int)sb.st_size - dp->d_bbsize;
690 /* XXX assume d_secsize is a power of two */
691 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
692 bootbuf = (char *)malloc((size_t)bootsize);
694 err(4, "%s", xxboot);
695 if (read(b, bootbuf, bootsize) < 0) {
697 err(4, "%s", xxboot);
700 #endif /* NUMBOOT > 1 */
702 #endif /* NUMBOOT > 0 */
704 * Make sure no part of the bootstrap is written in the area
705 * reserved for the label.
707 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
709 errx(2, "bootstrap doesn't leave room for disk label");
716 register struct disklabel *lp;
719 register struct partition *pp;
721 fprintf(f, "# %s:\n", specname);
722 if ((unsigned) lp->d_type < DKMAXTYPES)
723 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
725 fprintf(f, "type: %u\n", lp->d_type);
726 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
728 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
730 fprintf(f, "flags:");
731 if (lp->d_flags & D_REMOVABLE)
732 fprintf(f, " removeable");
733 if (lp->d_flags & D_ECC)
735 if (lp->d_flags & D_BADSECT)
736 fprintf(f, " badsect");
738 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
739 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
740 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
741 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
742 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
743 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
744 fprintf(f, "rpm: %u\n", lp->d_rpm);
745 fprintf(f, "interleave: %u\n", lp->d_interleave);
746 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
747 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
748 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
749 (u_long)lp->d_headswitch);
750 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
751 (u_long)lp->d_trkseek);
752 fprintf(f, "drivedata: ");
753 for (i = NDDATA - 1; i >= 0; i--)
754 if (lp->d_drivedata[i])
758 for (j = 0; j <= i; j++)
759 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
760 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
762 "# size offset fstype [fsize bsize bps/cpg]\n");
763 pp = lp->d_partitions;
764 for (i = 0; i < lp->d_npartitions; i++, pp++) {
766 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
767 (u_long)pp->p_size, (u_long)pp->p_offset);
768 if ((unsigned) pp->p_fstype < FSMAXTYPES)
769 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
771 fprintf(f, "%8d", pp->p_fstype);
772 switch (pp->p_fstype) {
774 case FS_UNUSED: /* XXX */
775 fprintf(f, " %5lu %5lu %5.5s ",
777 (u_long)(pp->p_fsize * pp->p_frag), "");
781 fprintf(f, " %5lu %5lu %5u ",
783 (u_long)(pp->p_fsize * pp->p_frag),
788 fprintf(f, " %5lu %5lu %5d",
790 (u_long)(pp->p_fsize * pp->p_frag),
795 fprintf(f, "%20.20s", "");
798 fprintf(f, "\t# (Cyl. %4lu",
799 (u_long)(pp->p_offset / lp->d_secpercyl));
800 if (pp->p_offset % lp->d_secpercyl)
805 (u_long)((pp->p_offset + pp->p_size +
806 lp->d_secpercyl - 1) /
807 lp->d_secpercyl - 1));
808 if (pp->p_size % lp->d_secpercyl)
818 struct disklabel *lp;
822 struct disklabel label;
825 if ((fd = mkstemp(tmpfil)) == -1 ||
826 (fp = fdopen(fd, "w")) == NULL) {
827 warnx("can't create %s", tmpfil);
835 fp = fopen(tmpfil, "r");
837 warnx("can't reopen %s for reading", tmpfil);
840 bzero((char *)&label, sizeof(label));
841 if (getasciilabel(fp, &label)) {
843 if (writelabel(f, bootarea, lp) == 0) {
845 (void) unlink(tmpfil);
850 printf("re-edit the label? [y]: "); fflush(stdout);
852 if (c != EOF && c != (int)'\n')
853 while (getchar() != (int)'\n')
858 (void) unlink(tmpfil);
865 register int pid, xpid;
867 extern char *getenv();
869 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
870 while ((pid = fork()) < 0) {
873 if (errno == EPROCLIM) {
874 warnx("you have too many processes");
877 if (errno != EAGAIN) {
889 if ((ed = getenv("EDITOR")) == (char *)0)
891 execlp(ed, ed, tmpfil, 0);
894 while ((xpid = wait(&stat)) >= 0)
906 while (*cp != '\0' && isspace(*cp))
908 if (*cp == '\0' || *cp == '#')
909 return ((char *)NULL);
919 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
921 if ((c = *cp) != '\0') {
926 return ((char *)NULL);
930 * Read an ascii label in from fd f,
931 * in the same format as that put out by display(),
937 register struct disklabel *lp;
939 register char **cpp, *cp;
940 register struct partition *pp;
941 char *tp, *s, line[BUFSIZ];
942 int v, lineno = 0, errors = 0;
944 lp->d_bbsize = BBSIZE; /* XXX */
945 lp->d_sbsize = SBSIZE; /* XXX */
946 while (fgets(line, sizeof(line) - 1, f)) {
948 if ((cp = index(line,'\n')) != 0)
955 fprintf(stderr, "line %d: syntax error\n", lineno);
959 *tp++ = '\0', tp = skip(tp);
960 if (streq(cp, "type")) {
964 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
965 if ((s = *cpp) && streq(s, tp)) {
966 lp->d_type = cpp - dktypenames;
970 if ((unsigned)v >= DKMAXTYPES)
971 fprintf(stderr, "line %d:%s %d\n", lineno,
972 "Warning, unknown disk type", v);
976 if (streq(cp, "flags")) {
977 for (v = 0; (cp = tp) && *cp != '\0';) {
979 if (streq(cp, "removeable"))
981 else if (streq(cp, "ecc"))
983 else if (streq(cp, "badsect"))
987 "line %d: %s: bad flag\n",
995 if (streq(cp, "drivedata")) {
998 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
999 lp->d_drivedata[i++] = atoi(cp);
1004 if (sscanf(cp, "%d partitions", &v) == 1) {
1005 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
1007 "line %d: bad # of partitions\n", lineno);
1008 lp->d_npartitions = MAXPARTITIONS;
1011 lp->d_npartitions = v;
1016 if (streq(cp, "disk")) {
1017 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1020 if (streq(cp, "label")) {
1021 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1024 if (streq(cp, "bytes/sector")) {
1026 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1028 "line %d: %s: bad sector size\n",
1035 if (streq(cp, "sectors/track")) {
1038 fprintf(stderr, "line %d: %s: bad %s\n",
1045 if (streq(cp, "sectors/cylinder")) {
1048 fprintf(stderr, "line %d: %s: bad %s\n",
1052 lp->d_secpercyl = v;
1055 if (streq(cp, "tracks/cylinder")) {
1058 fprintf(stderr, "line %d: %s: bad %s\n",
1065 if (streq(cp, "cylinders")) {
1068 fprintf(stderr, "line %d: %s: bad %s\n",
1072 lp->d_ncylinders = v;
1075 if (streq(cp, "sectors/unit")) {
1078 fprintf(stderr, "line %d: %s: bad %s\n",
1082 lp->d_secperunit = v;
1085 if (streq(cp, "rpm")) {
1088 fprintf(stderr, "line %d: %s: bad %s\n",
1095 if (streq(cp, "interleave")) {
1098 fprintf(stderr, "line %d: %s: bad %s\n",
1102 lp->d_interleave = v;
1105 if (streq(cp, "trackskew")) {
1108 fprintf(stderr, "line %d: %s: bad %s\n",
1112 lp->d_trackskew = v;
1115 if (streq(cp, "cylinderskew")) {
1118 fprintf(stderr, "line %d: %s: bad %s\n",
1125 if (streq(cp, "headswitch")) {
1128 fprintf(stderr, "line %d: %s: bad %s\n",
1132 lp->d_headswitch = v;
1135 if (streq(cp, "track-to-track seek")) {
1138 fprintf(stderr, "line %d: %s: bad %s\n",
1145 if ('a' <= *cp && *cp <= 'z' && cp[1] == '\0') {
1146 unsigned part = *cp - 'a';
1148 if (part > lp->d_npartitions) {
1150 "line %d: bad partition name\n", lineno);
1154 pp = &lp->d_partitions[part];
1155 #define NXTNUM(n) { \
1157 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1161 cp = tp, tp = word(cp); \
1171 "line %d: %s: bad partition size\n",
1179 "line %d: %s: bad partition offset\n",
1184 cp = tp, tp = word(cp);
1186 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1187 if ((s = *cpp) && streq(s, cp)) {
1188 pp->p_fstype = cpp - fstypenames;
1195 if ((unsigned)v >= FSMAXTYPES) {
1196 fprintf(stderr, "line %d: %s %s\n", lineno,
1197 "Warning, unknown filesystem type", cp);
1203 switch (pp->p_fstype) {
1205 case FS_UNUSED: /* XXX */
1206 NXTNUM(pp->p_fsize);
1207 if (pp->p_fsize == 0)
1210 pp->p_frag = v / pp->p_fsize;
1214 NXTNUM(pp->p_fsize);
1215 if (pp->p_fsize == 0)
1218 pp->p_frag = v / pp->p_fsize;
1223 NXTNUM(pp->p_fsize);
1224 if (pp->p_fsize == 0)
1227 pp->p_frag = v / pp->p_fsize;
1236 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1242 errors += checklabel(lp);
1243 return (errors == 0);
1247 * Check disklabel for errors and fill in
1248 * derived fields according to supplied values.
1252 register struct disklabel *lp;
1254 register struct partition *pp;
1258 if (lp->d_secsize == 0) {
1259 fprintf(stderr, "sector size 0\n");
1262 if (lp->d_nsectors == 0) {
1263 fprintf(stderr, "sectors/track 0\n");
1266 if (lp->d_ntracks == 0) {
1267 fprintf(stderr, "tracks/cylinder 0\n");
1270 if (lp->d_ncylinders == 0) {
1271 fprintf(stderr, "cylinders/unit 0\n");
1275 Warning("revolutions/minute 0");
1276 if (lp->d_secpercyl == 0)
1277 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1278 if (lp->d_secperunit == 0)
1279 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1280 if (lp->d_bbsize == 0) {
1281 fprintf(stderr, "boot block size 0\n");
1283 } else if (lp->d_bbsize % lp->d_secsize)
1284 Warning("boot block size %% sector-size != 0");
1285 if (lp->d_sbsize == 0) {
1286 fprintf(stderr, "super block size 0\n");
1288 } else if (lp->d_sbsize % lp->d_secsize)
1289 Warning("super block size %% sector-size != 0");
1290 if (lp->d_npartitions > MAXPARTITIONS)
1291 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1292 (u_long)lp->d_npartitions, MAXPARTITIONS);
1293 for (i = 0; i < lp->d_npartitions; i++) {
1295 pp = &lp->d_partitions[i];
1296 if (pp->p_size == 0 && pp->p_offset != 0)
1297 Warning("partition %c: size 0, but offset %lu",
1298 part, (u_long)pp->p_offset);
1300 if (pp->p_size % lp->d_secpercyl)
1301 Warning("partition %c: size %% cylinder-size != 0",
1303 if (pp->p_offset % lp->d_secpercyl)
1304 Warning("partition %c: offset %% cylinder-size != 0",
1307 if (pp->p_offset > lp->d_secperunit) {
1309 "partition %c: offset past end of unit\n", part);
1312 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1314 "partition %c: partition extends past end of unit\n",
1319 for (; i < MAXPARTITIONS; i++) {
1321 pp = &lp->d_partitions[i];
1322 if (pp->p_size || pp->p_offset)
1323 Warning("unused partition %c: size %d offset %lu",
1324 'a' + i, pp->p_size, (u_long)pp->p_offset);
1330 * When operating on a "virgin" disk, try getting an initial label
1331 * from the associated device driver. This might work for all device
1332 * drivers that are able to fetch some initial device parameters
1333 * without even having access to a (BSD) disklabel, like SCSI disks,
1334 * most IDE drives, or vn devices.
1336 * The device name must be given in its "canonical" form.
1339 getvirginlabel(void)
1341 static struct disklabel lab;
1342 char namebuf[BBSIZE];
1345 if (dkname[0] == '/') {
1346 warnx("\"auto\" requires the usage of a canonical disk name");
1349 (void)snprintf(namebuf, BBSIZE, "%sr%s", _PATH_DEV, dkname);
1350 if ((f = open(namebuf, O_RDONLY)) == -1) {
1351 warn("cannot open %s", namebuf);
1354 if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1355 warn("ioctl DIOCGDINFO");
1366 * If we are installing a boot program that doesn't fit in d_bbsize
1367 * we need to mark those partitions that the boot overflows into.
1368 * This allows newfs to prevent creation of a filesystem where it might
1369 * clobber bootstrap code.
1373 register struct disklabel *lp;
1375 register struct partition *pp;
1382 boffset = bootsize / lp->d_secsize;
1383 for (i = 0; i < lp->d_npartitions; i++) {
1385 pp = &lp->d_partitions[i];
1386 if (pp->p_size == 0)
1388 if (boffset <= pp->p_offset) {
1389 if (pp->p_fstype == FS_BOOT)
1390 pp->p_fstype = FS_UNUSED;
1391 } else if (pp->p_fstype != FS_BOOT) {
1392 if (pp->p_fstype != FS_UNUSED) {
1394 "boot overlaps used partition %c\n",
1398 pp->p_fstype = FS_BOOT;
1399 Warning("boot overlaps partition %c, %s",
1400 part, "marked as FS_BOOT");
1405 errx(4, "cannot install boot program");
1410 Warning(char *fmt, ...)
1414 fprintf(stderr, "Warning, ");
1416 vfprintf(stderr, fmt, ap);
1417 fprintf(stderr, "\n");
1425 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1426 "usage: disklabel [-r] disk",
1427 "\t\t(to read label)",
1428 " disklabel -w [-r] disk type [ packid ]",
1429 "\t\t(to write label with existing boot program)",
1430 " disklabel -e [-r] disk",
1431 "\t\t(to edit label)",
1432 " disklabel -R [-r] disk protofile",
1433 "\t\t(to restore label with existing boot program)",
1435 " disklabel -B [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1436 "\t\t(to install boot program with existing label)",
1437 " disklabel -w -B [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1438 "\t\t(to write label and boot program)",
1439 " disklabel -R -B [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1440 "\t\t(to restore label and boot program)",
1442 " disklabel -B [ -b bootprog ] disk [ type ]",
1443 "\t\t(to install boot program with existing on-disk label)",
1444 " disklabel -w -B [ -b bootprog ] disk type [ packid ]",
1445 "\t\t(to write label and install boot program)",
1446 " disklabel -R -B [ -b bootprog ] disk protofile [ type ]",
1447 "\t\t(to restore label and install boot program)",
1449 " disklabel [-NW] disk",
1450 "\t\t(to write disable/enable label)");
1452 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1453 "usage: disklabel [-r] disk", "(to read label)",
1454 " disklabel -w [-r] disk type [ packid ]",
1455 "\t\t(to write label)",
1456 " disklabel -e [-r] disk",
1457 "\t\t(to edit label)",
1458 " disklabel -R [-r] disk protofile",
1459 "\t\t(to restore label)",
1460 " disklabel [-NW] disk",
1461 "\t\t(to write disable/enable label)");