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[] =
49 "$Id: disklabel.c,v 1.23 1998/10/23 18:57:39 bde Exp $";
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, 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);
254 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
255 err(4, "ioctl DIOCWLABEL");
263 error = checklabel(lp);
268 if (installboot && argc == 3) {
269 makelabel(argv[2], 0, &lab);
273 * We only called makelabel() for its side effect
274 * of setting the bootstrap file names. Discard
275 * all changes to `lab' so that all values in the
276 * final label come from the ASCII label.
278 bzero((char *)&lab, sizeof(lab));
283 if (!(t = fopen(argv[1], "r")))
284 err(4, "%s", argv[1]);
285 if (!getasciilabel(t, &lab))
287 lp = makebootarea(bootarea, &lab, f);
289 error = writelabel(f, bootarea, lp);
299 makelabel(argv[1], name, &lab);
300 lp = makebootarea(bootarea, &lab, f);
302 if (checklabel(lp) == 0)
303 error = writelabel(f, bootarea, lp);
308 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
309 err(4, "ioctl DIOCWLABEL");
315 struct disklabel tlab;
320 makelabel(argv[1], 0, &lab);
321 lp = makebootarea(bootarea, &lab, f);
323 if (checklabel(lp) == 0)
324 error = writelabel(f, bootarea, lp);
333 * Construct a prototype disklabel from /etc/disktab. As a side
334 * effect, set the names of the primary and secondary boot files
338 makelabel(type, name, lp)
340 register struct disklabel *lp;
342 register struct disklabel *dp;
344 if (strcmp(type, "auto") == 0)
345 dp = getvirginlabel();
347 dp = getdiskbyname(type);
349 errx(1, "%s: unknown disk type", type);
353 * Set bootstrap name(s).
354 * 1. If set from command line, use those,
355 * 2. otherwise, check if disktab specifies them (b0 or b1),
356 * 3. otherwise, makebootarea() will choose ones based on the name
357 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
359 if (!xxboot && lp->d_boot0) {
360 if (*lp->d_boot0 != '/')
361 (void)sprintf(boot0, "%s/%s",
362 _PATH_BOOTDIR, lp->d_boot0);
364 (void)strcpy(boot0, lp->d_boot0);
368 if (!bootxx && lp->d_boot1) {
369 if (*lp->d_boot1 != '/')
370 (void)sprintf(boot1, "%s/%s",
371 _PATH_BOOTDIR, lp->d_boot1);
373 (void)strcpy(boot1, lp->d_boot1);
378 /* d_packname is union d_boot[01], so zero */
379 bzero(lp->d_packname, sizeof(lp->d_packname));
381 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
385 writelabel(f, boot, lp)
388 register struct disklabel *lp;
400 lp->d_magic = DISKMAGIC;
401 lp->d_magic2 = DISKMAGIC;
403 lp->d_checksum = dkcksum(lp);
406 * First set the kernel disk label,
407 * then write a label to the raw disk.
408 * If the SDINFO ioctl fails because it is unimplemented,
409 * keep going; otherwise, the kernel consistency checks
410 * may prevent us from changing the current (in-core)
413 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
414 errno != ENODEV && errno != ENOTTY) {
415 l_perror("ioctl DIOCSDINFO");
418 (void)lseek(f, (off_t)0, SEEK_SET);
422 * Generate the bootblock checksum for the SRM console.
424 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
430 * write enable label sector before write (if necessary),
431 * disable after writing.
434 if (ioctl(f, DIOCWLABEL, &flag) < 0)
435 warn("ioctl DIOCWLABEL");
436 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
442 * Output the remainder of the disklabel
444 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
450 (void) ioctl(f, DIOCWLABEL, &flag);
451 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
452 l_perror("ioctl DIOCWDINFO");
456 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
459 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
460 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
461 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
463 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
464 warn("alternate label %d write", i/2);
478 warnx("%s: no disk label on disk;", s);
480 "use \"disklabel -r\" to install initial label\n");
484 warnx("%s: label magic number or checksum is wrong!", s);
485 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
489 warnx("%s: open partition would move or shrink", s);
493 warnx("%s: '%c' partition must start at beginning of disk",
504 * Fetch disklabel for disk.
505 * Use ioctl to get label unless -r flag is given.
511 register struct disklabel *lp;
514 if (read(f, bootarea, BBSIZE) < BBSIZE)
515 err(4, "%s", specname);
516 for (lp = (struct disklabel *)bootarea;
517 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
518 lp = (struct disklabel *)((char *)lp + 16))
519 if (lp->d_magic == DISKMAGIC &&
520 lp->d_magic2 == DISKMAGIC)
522 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
523 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
526 "bad pack magic number (label is damaged, or pack is unlabeled)");
529 if (ioctl(f, DIOCGDINFO, lp) < 0)
530 err(4, "ioctl DIOCGDINFO");
536 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
537 * Returns a pointer to the disklabel portion of the bootarea.
540 makebootarea(boot, dp, f)
542 register struct disklabel *dp;
545 struct disklabel *lp;
562 if (dp->d_secsize == 0) {
563 dp->d_secsize = DEV_BSIZE;
564 dp->d_bbsize = BBSIZE;
566 lp = (struct disklabel *)
567 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
568 bzero((char *)lp, sizeof *lp);
571 * If we are not installing a boot program but we are installing a
572 * label on disk then we must read the current bootarea so we don't
573 * clobber the existing boot.
577 if (read(f, boot, BBSIZE) < BBSIZE)
578 err(4, "%s", specname);
579 bzero((char *)lp, sizeof *lp);
584 * We are installing a boot program. Determine the name(s) and
585 * read them into the appropriate places in the boot area.
587 if (!xxboot || !bootxx) {
589 if ((p = rindex(dkname, '/')) == NULL)
593 while (*p && !isdigit(*p))
598 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
603 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
610 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
611 xxboot, bootxx ? bootxx : "NONE");
616 * 1. One-piece bootstrap (hp300/hp800)
617 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
618 * is remembered and written later following the bootarea.
619 * 2. Two-piece bootstraps (vax/i386?/mips?)
620 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
621 * bytes of bootarea, remaining d_bbsize-d_secsize filled
624 b = open(xxboot, O_RDONLY);
626 err(4, "%s", xxboot);
631 * The i386 has the so-called fdisk table embedded into the
632 * primary bootstrap. We take care to not clobber it, but
633 * only if it does already contain some data. (Otherwise,
634 * the xxboot provides a template.)
636 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
637 err(4, "%s", xxboot);
638 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
640 if (read(b, boot, (int)dp->d_secsize) < 0)
641 err(4, "%s", xxboot);
644 for (i = DOSPARTOFF, found = 0;
645 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
647 found = tmpbuf[i] != 0;
649 memcpy((void *)&boot[DOSPARTOFF],
650 (void *)&tmpbuf[DOSPARTOFF],
651 NDOSPART * sizeof(struct dos_partition));
654 b = open(bootxx, O_RDONLY);
656 err(4, "%s", bootxx);
657 if (fstat(b, &sb) != 0)
658 err(4, "%s", bootxx);
659 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
660 errx(4, "%s too large", bootxx);
661 if (read(b, &boot[dp->d_secsize],
662 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
663 err(4, "%s", bootxx);
664 #else /* !(NUMBOOT > 1) */
667 * On the alpha, the primary bootstrap starts at the
668 * second sector of the boot area. The first sector
669 * contains the label and must be edited to contain the
670 * size and location of the primary bootstrap.
672 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
674 err(4, "%s", xxboot);
675 bootinfo = (u_long *)(boot + 480);
676 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
677 bootinfo[1] = 1; /* start at sector 1 */
678 bootinfo[2] = 0; /* flags (must be zero) */
679 #else /* !__alpha__ */
680 if (read(b, boot, (int)dp->d_bbsize) < 0)
681 err(4, "%s", xxboot);
682 #endif /* __alpha__ */
683 if (fstat(b, &sb) != 0)
684 err(4, "%s", xxboot);
685 bootsize = (int)sb.st_size - dp->d_bbsize;
687 /* XXX assume d_secsize is a power of two */
688 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
689 bootbuf = (char *)malloc((size_t)bootsize);
691 err(4, "%s", xxboot);
692 if (read(b, bootbuf, bootsize) < 0) {
694 err(4, "%s", xxboot);
697 #endif /* NUMBOOT > 1 */
699 #endif /* NUMBOOT > 0 */
701 * Make sure no part of the bootstrap is written in the area
702 * reserved for the label.
704 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
706 errx(2, "bootstrap doesn't leave room for disk label");
713 register struct disklabel *lp;
716 register struct partition *pp;
718 fprintf(f, "# %s:\n", specname);
719 if ((unsigned) lp->d_type < DKMAXTYPES)
720 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
722 fprintf(f, "type: %u\n", lp->d_type);
723 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
725 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
727 fprintf(f, "flags:");
728 if (lp->d_flags & D_REMOVABLE)
729 fprintf(f, " removeable");
730 if (lp->d_flags & D_ECC)
732 if (lp->d_flags & D_BADSECT)
733 fprintf(f, " badsect");
735 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
736 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
737 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
738 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
739 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
740 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
741 fprintf(f, "rpm: %u\n", lp->d_rpm);
742 fprintf(f, "interleave: %u\n", lp->d_interleave);
743 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
744 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
745 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
746 (u_long)lp->d_headswitch);
747 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
748 (u_long)lp->d_trkseek);
749 fprintf(f, "drivedata: ");
750 for (i = NDDATA - 1; i >= 0; i--)
751 if (lp->d_drivedata[i])
755 for (j = 0; j <= i; j++)
756 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
757 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
759 "# size offset fstype [fsize bsize bps/cpg]\n");
760 pp = lp->d_partitions;
761 for (i = 0; i < lp->d_npartitions; i++, pp++) {
763 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
764 (u_long)pp->p_size, (u_long)pp->p_offset);
765 if ((unsigned) pp->p_fstype < FSMAXTYPES)
766 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
768 fprintf(f, "%8d", pp->p_fstype);
769 switch (pp->p_fstype) {
771 case FS_UNUSED: /* XXX */
772 fprintf(f, " %5lu %5lu %5.5s ",
774 (u_long)(pp->p_fsize * pp->p_frag), "");
778 fprintf(f, " %5lu %5lu %5u ",
780 (u_long)(pp->p_fsize * pp->p_frag),
785 fprintf(f, " %5lu %5lu %5d",
787 (u_long)(pp->p_fsize * pp->p_frag),
792 fprintf(f, "%20.20s", "");
795 fprintf(f, "\t# (Cyl. %4lu",
796 (u_long)(pp->p_offset / lp->d_secpercyl));
797 if (pp->p_offset % lp->d_secpercyl)
802 (u_long)((pp->p_offset + pp->p_size +
803 lp->d_secpercyl - 1) /
804 lp->d_secpercyl - 1));
805 if (pp->p_size % lp->d_secpercyl)
815 struct disklabel *lp;
819 struct disklabel label;
822 if ((fd = mkstemp(tmpfil)) == -1 ||
823 (fp = fdopen(fd, "w")) == NULL) {
824 warnx("can't create %s", tmpfil);
832 fp = fopen(tmpfil, "r");
834 warnx("can't reopen %s for reading", tmpfil);
837 bzero((char *)&label, sizeof(label));
838 if (getasciilabel(fp, &label)) {
840 if (writelabel(f, bootarea, lp) == 0) {
842 (void) unlink(tmpfil);
847 printf("re-edit the label? [y]: "); fflush(stdout);
849 if (c != EOF && c != (int)'\n')
850 while (getchar() != (int)'\n')
855 (void) unlink(tmpfil);
862 register int pid, xpid;
864 extern char *getenv();
866 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
867 while ((pid = fork()) < 0) {
870 if (errno == EPROCLIM) {
871 warnx("you have too many processes");
874 if (errno != EAGAIN) {
886 if ((ed = getenv("EDITOR")) == (char *)0)
888 execlp(ed, ed, tmpfil, 0);
891 while ((xpid = wait(&stat)) >= 0)
903 while (*cp != '\0' && isspace(*cp))
905 if (*cp == '\0' || *cp == '#')
906 return ((char *)NULL);
916 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
918 if ((c = *cp) != '\0') {
923 return ((char *)NULL);
927 * Read an ascii label in from fd f,
928 * in the same format as that put out by display(),
934 register struct disklabel *lp;
936 register char **cpp, *cp;
937 register struct partition *pp;
938 char *tp, *s, line[BUFSIZ];
939 int v, lineno = 0, errors = 0;
941 lp->d_bbsize = BBSIZE; /* XXX */
942 lp->d_sbsize = SBSIZE; /* XXX */
943 while (fgets(line, sizeof(line) - 1, f)) {
945 if ((cp = index(line,'\n')) != 0)
952 fprintf(stderr, "line %d: syntax error\n", lineno);
956 *tp++ = '\0', tp = skip(tp);
957 if (streq(cp, "type")) {
961 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
962 if ((s = *cpp) && streq(s, tp)) {
963 lp->d_type = cpp - dktypenames;
967 if ((unsigned)v >= DKMAXTYPES)
968 fprintf(stderr, "line %d:%s %d\n", lineno,
969 "Warning, unknown disk type", v);
973 if (streq(cp, "flags")) {
974 for (v = 0; (cp = tp) && *cp != '\0';) {
976 if (streq(cp, "removeable"))
978 else if (streq(cp, "ecc"))
980 else if (streq(cp, "badsect"))
984 "line %d: %s: bad flag\n",
992 if (streq(cp, "drivedata")) {
995 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
996 lp->d_drivedata[i++] = atoi(cp);
1001 if (sscanf(cp, "%d partitions", &v) == 1) {
1002 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
1004 "line %d: bad # of partitions\n", lineno);
1005 lp->d_npartitions = MAXPARTITIONS;
1008 lp->d_npartitions = v;
1013 if (streq(cp, "disk")) {
1014 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1017 if (streq(cp, "label")) {
1018 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1021 if (streq(cp, "bytes/sector")) {
1023 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1025 "line %d: %s: bad sector size\n",
1032 if (streq(cp, "sectors/track")) {
1035 fprintf(stderr, "line %d: %s: bad %s\n",
1042 if (streq(cp, "sectors/cylinder")) {
1045 fprintf(stderr, "line %d: %s: bad %s\n",
1049 lp->d_secpercyl = v;
1052 if (streq(cp, "tracks/cylinder")) {
1055 fprintf(stderr, "line %d: %s: bad %s\n",
1062 if (streq(cp, "cylinders")) {
1065 fprintf(stderr, "line %d: %s: bad %s\n",
1069 lp->d_ncylinders = v;
1072 if (streq(cp, "sectors/unit")) {
1075 fprintf(stderr, "line %d: %s: bad %s\n",
1079 lp->d_secperunit = v;
1082 if (streq(cp, "rpm")) {
1085 fprintf(stderr, "line %d: %s: bad %s\n",
1092 if (streq(cp, "interleave")) {
1095 fprintf(stderr, "line %d: %s: bad %s\n",
1099 lp->d_interleave = v;
1102 if (streq(cp, "trackskew")) {
1105 fprintf(stderr, "line %d: %s: bad %s\n",
1109 lp->d_trackskew = v;
1112 if (streq(cp, "cylinderskew")) {
1115 fprintf(stderr, "line %d: %s: bad %s\n",
1122 if (streq(cp, "headswitch")) {
1125 fprintf(stderr, "line %d: %s: bad %s\n",
1129 lp->d_headswitch = v;
1132 if (streq(cp, "track-to-track seek")) {
1135 fprintf(stderr, "line %d: %s: bad %s\n",
1142 if ('a' <= *cp && *cp <= 'z' && cp[1] == '\0') {
1143 unsigned part = *cp - 'a';
1145 if (part > lp->d_npartitions) {
1147 "line %d: bad partition name\n", lineno);
1151 pp = &lp->d_partitions[part];
1152 #define NXTNUM(n) { \
1154 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1158 cp = tp, tp = word(cp); \
1168 "line %d: %s: bad partition size\n",
1176 "line %d: %s: bad partition offset\n",
1181 cp = tp, tp = word(cp);
1183 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1184 if ((s = *cpp) && streq(s, cp)) {
1185 pp->p_fstype = cpp - fstypenames;
1192 if ((unsigned)v >= FSMAXTYPES) {
1193 fprintf(stderr, "line %d: %s %s\n", lineno,
1194 "Warning, unknown filesystem type", cp);
1200 switch (pp->p_fstype) {
1202 case FS_UNUSED: /* XXX */
1203 NXTNUM(pp->p_fsize);
1204 if (pp->p_fsize == 0)
1207 pp->p_frag = v / pp->p_fsize;
1211 NXTNUM(pp->p_fsize);
1212 if (pp->p_fsize == 0)
1215 pp->p_frag = v / pp->p_fsize;
1220 NXTNUM(pp->p_fsize);
1221 if (pp->p_fsize == 0)
1224 pp->p_frag = v / pp->p_fsize;
1233 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1239 errors += checklabel(lp);
1240 return (errors == 0);
1244 * Check disklabel for errors and fill in
1245 * derived fields according to supplied values.
1249 register struct disklabel *lp;
1251 register struct partition *pp;
1255 if (lp->d_secsize == 0) {
1256 fprintf(stderr, "sector size 0\n");
1259 if (lp->d_nsectors == 0) {
1260 fprintf(stderr, "sectors/track 0\n");
1263 if (lp->d_ntracks == 0) {
1264 fprintf(stderr, "tracks/cylinder 0\n");
1267 if (lp->d_ncylinders == 0) {
1268 fprintf(stderr, "cylinders/unit 0\n");
1272 Warning("revolutions/minute 0");
1273 if (lp->d_secpercyl == 0)
1274 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1275 if (lp->d_secperunit == 0)
1276 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1277 if (lp->d_bbsize == 0) {
1278 fprintf(stderr, "boot block size 0\n");
1280 } else if (lp->d_bbsize % lp->d_secsize)
1281 Warning("boot block size %% sector-size != 0");
1282 if (lp->d_sbsize == 0) {
1283 fprintf(stderr, "super block size 0\n");
1285 } else if (lp->d_sbsize % lp->d_secsize)
1286 Warning("super block size %% sector-size != 0");
1287 if (lp->d_npartitions > MAXPARTITIONS)
1288 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1289 (u_long)lp->d_npartitions, MAXPARTITIONS);
1290 for (i = 0; i < lp->d_npartitions; i++) {
1292 pp = &lp->d_partitions[i];
1293 if (pp->p_size == 0 && pp->p_offset != 0)
1294 Warning("partition %c: size 0, but offset %lu",
1295 part, (u_long)pp->p_offset);
1297 if (pp->p_size % lp->d_secpercyl)
1298 Warning("partition %c: size %% cylinder-size != 0",
1300 if (pp->p_offset % lp->d_secpercyl)
1301 Warning("partition %c: offset %% cylinder-size != 0",
1304 if (pp->p_offset > lp->d_secperunit) {
1306 "partition %c: offset past end of unit\n", part);
1309 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1311 "partition %c: partition extends past end of unit\n",
1316 for (; i < MAXPARTITIONS; i++) {
1318 pp = &lp->d_partitions[i];
1319 if (pp->p_size || pp->p_offset)
1320 Warning("unused partition %c: size %d offset %lu",
1321 'a' + i, pp->p_size, (u_long)pp->p_offset);
1327 * When operating on a "virgin" disk, try getting an initial label
1328 * from the associated device driver. This might work for all device
1329 * drivers that are able to fetch some initial device parameters
1330 * without even having access to a (BSD) disklabel, like SCSI disks,
1331 * most IDE drives, or vn devices.
1333 * The device name must be given in its "canonical" form.
1336 getvirginlabel(void)
1338 static struct disklabel lab;
1339 char namebuf[BBSIZE];
1342 if (dkname[0] == '/') {
1343 warnx("\"auto\" requires the usage of a canonical disk name");
1346 (void)snprintf(namebuf, BBSIZE, "%sr%s", _PATH_DEV, dkname);
1347 if ((f = open(namebuf, O_RDONLY)) == -1) {
1348 warn("cannot open %s", namebuf);
1351 if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1352 warn("ioctl DIOCGDINFO");
1363 * If we are installing a boot program that doesn't fit in d_bbsize
1364 * we need to mark those partitions that the boot overflows into.
1365 * This allows newfs to prevent creation of a filesystem where it might
1366 * clobber bootstrap code.
1370 register struct disklabel *lp;
1372 register struct partition *pp;
1379 boffset = bootsize / lp->d_secsize;
1380 for (i = 0; i < lp->d_npartitions; i++) {
1382 pp = &lp->d_partitions[i];
1383 if (pp->p_size == 0)
1385 if (boffset <= pp->p_offset) {
1386 if (pp->p_fstype == FS_BOOT)
1387 pp->p_fstype = FS_UNUSED;
1388 } else if (pp->p_fstype != FS_BOOT) {
1389 if (pp->p_fstype != FS_UNUSED) {
1391 "boot overlaps used partition %c\n",
1395 pp->p_fstype = FS_BOOT;
1396 Warning("boot overlaps partition %c, %s",
1397 part, "marked as FS_BOOT");
1402 errx(4, "cannot install boot program");
1407 Warning(char *fmt, ...)
1411 fprintf(stderr, "Warning, ");
1413 vfprintf(stderr, fmt, ap);
1414 fprintf(stderr, "\n");
1422 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",
1423 "usage: disklabel [-r] disk",
1424 "\t\t(to read label)",
1425 " disklabel -w [-r] disk type [ packid ]",
1426 "\t\t(to write label with existing boot program)",
1427 " disklabel -e [-r] disk",
1428 "\t\t(to edit label)",
1429 " disklabel -R [-r] disk protofile",
1430 "\t\t(to restore label with existing boot program)",
1432 " disklabel -B [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1433 "\t\t(to install boot program with existing label)",
1434 " disklabel -w -B [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1435 "\t\t(to write label and boot program)",
1436 " disklabel -R -B [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1437 "\t\t(to restore label and boot program)",
1439 " disklabel -B [ -b bootprog ] disk [ type ]",
1440 "\t\t(to install boot program with existing on-disk label)",
1441 " disklabel -w -B [ -b bootprog ] disk type [ packid ]",
1442 "\t\t(to write label and install boot program)",
1443 " disklabel -R -B [ -b bootprog ] disk protofile [ type ]",
1444 "\t\t(to restore label and install boot program)",
1446 " disklabel [-NW] disk",
1447 "\t\t(to write disable/enable label)");
1449 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1450 "usage: disklabel [-r] disk", "(to read label)",
1451 " disklabel -w [-r] disk type [ packid ]",
1452 "\t\t(to write label)",
1453 " disklabel -e [-r] disk",
1454 "\t\t(to edit label)",
1455 " disklabel -R [-r] disk protofile",
1456 "\t\t(to restore label)",
1457 " disklabel [-NW] disk",
1458 "\t\t(to write disable/enable label)");