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 char bootarea[BBSIZE];
123 int installboot; /* non-zero if we should install a boot program */
124 char *bootbuf; /* pointer to buffer with remainder of boot prog */
125 int bootsize; /* size of remaining boot program */
126 char *xxboot; /* primary boot */
127 char *bootxx; /* secondary boot */
128 char boot0[MAXPATHLEN];
129 char boot1[MAXPATHLEN];
133 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
140 #define OPTIONS "BNRWb:ders:w"
142 #define OPTIONS "BNRWb:ers:w"
150 register struct disklabel *lp;
152 int ch, f = 0, flag, error = 0;
155 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
227 if (dkname[0] != '/') {
228 (void)sprintf(np, "%sr%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
230 np += strlen(specname) + 1;
233 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
234 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
235 (void)sprintf(specname, "%sr%s", _PATH_DEV, dkname);
236 np = namebuf + strlen(specname) + 1;
237 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
240 err(4, "%s", specname);
256 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
257 err(4, "ioctl DIOCWLABEL");
265 error = checklabel(lp);
270 if (installboot && argc == 3) {
271 makelabel(argv[2], 0, &lab);
275 * We only called makelabel() for its side effect
276 * of setting the bootstrap file names. Discard
277 * all changes to `lab' so that all values in the
278 * final label come from the ASCII label.
280 bzero((char *)&lab, sizeof(lab));
285 if (!(t = fopen(argv[1], "r")))
286 err(4, "%s", argv[1]);
287 if (!getasciilabel(t, &lab))
289 lp = makebootarea(bootarea, &lab, f);
291 error = writelabel(f, bootarea, lp);
301 makelabel(argv[1], name, &lab);
302 lp = makebootarea(bootarea, &lab, f);
304 if (checklabel(lp) == 0)
305 error = writelabel(f, bootarea, lp);
310 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
311 err(4, "ioctl DIOCWLABEL");
317 struct disklabel tlab;
322 makelabel(argv[1], 0, &lab);
323 lp = makebootarea(bootarea, &lab, f);
325 if (checklabel(lp) == 0)
326 error = writelabel(f, bootarea, lp);
335 * Construct a prototype disklabel from /etc/disktab. As a side
336 * effect, set the names of the primary and secondary boot files
340 makelabel(type, name, lp)
342 register struct disklabel *lp;
344 register struct disklabel *dp;
346 if (strcmp(type, "auto") == 0)
347 dp = getvirginlabel();
349 dp = getdiskbyname(type);
351 errx(1, "%s: unknown disk type", type);
355 * Set bootstrap name(s).
356 * 1. If set from command line, use those,
357 * 2. otherwise, check if disktab specifies them (b0 or b1),
358 * 3. otherwise, makebootarea() will choose ones based on the name
359 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
361 if (!xxboot && lp->d_boot0) {
362 if (*lp->d_boot0 != '/')
363 (void)sprintf(boot0, "%s/%s",
364 _PATH_BOOTDIR, lp->d_boot0);
366 (void)strcpy(boot0, lp->d_boot0);
370 if (!bootxx && lp->d_boot1) {
371 if (*lp->d_boot1 != '/')
372 (void)sprintf(boot1, "%s/%s",
373 _PATH_BOOTDIR, lp->d_boot1);
375 (void)strcpy(boot1, lp->d_boot1);
380 /* d_packname is union d_boot[01], so zero */
381 bzero(lp->d_packname, sizeof(lp->d_packname));
383 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
387 writelabel(f, boot, lp)
390 register struct disklabel *lp;
402 lp->d_magic = DISKMAGIC;
403 lp->d_magic2 = DISKMAGIC;
405 lp->d_checksum = dkcksum(lp);
408 * First set the kernel disk label,
409 * then write a label to the raw disk.
410 * If the SDINFO ioctl fails because it is unimplemented,
411 * keep going; otherwise, the kernel consistency checks
412 * may prevent us from changing the current (in-core)
415 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
416 errno != ENODEV && errno != ENOTTY) {
417 l_perror("ioctl DIOCSDINFO");
420 (void)lseek(f, (off_t)0, SEEK_SET);
424 * Generate the bootblock checksum for the SRM console.
426 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
432 * write enable label sector before write (if necessary),
433 * disable after writing.
436 if (ioctl(f, DIOCWLABEL, &flag) < 0)
437 warn("ioctl DIOCWLABEL");
438 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
444 * Output the remainder of the disklabel
446 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
452 (void) ioctl(f, DIOCWLABEL, &flag);
453 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
454 l_perror("ioctl DIOCWDINFO");
458 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
461 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
462 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
463 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
465 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
466 warn("alternate label %d write", i/2);
480 warnx("%s: no disk label on disk;", s);
482 "use \"disklabel -r\" to install initial label\n");
486 warnx("%s: label magic number or checksum is wrong!", s);
487 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
491 warnx("%s: open partition would move or shrink", s);
495 warnx("%s: '%c' partition must start at beginning of disk",
506 * Fetch disklabel for disk.
507 * Use ioctl to get label unless -r flag is given.
513 register struct disklabel *lp;
516 if (read(f, bootarea, BBSIZE) < BBSIZE)
517 err(4, "%s", specname);
518 for (lp = (struct disklabel *)bootarea;
519 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
520 lp = (struct disklabel *)((char *)lp + 16))
521 if (lp->d_magic == DISKMAGIC &&
522 lp->d_magic2 == DISKMAGIC)
524 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
525 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
528 "bad pack magic number (label is damaged, or pack is unlabeled)");
531 if (ioctl(f, DIOCGDINFO, lp) < 0)
532 err(4, "ioctl DIOCGDINFO");
538 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
539 * Returns a pointer to the disklabel portion of the bootarea.
542 makebootarea(boot, dp, f)
544 register struct disklabel *dp;
547 struct disklabel *lp;
564 if (dp->d_secsize == 0) {
565 dp->d_secsize = DEV_BSIZE;
566 dp->d_bbsize = BBSIZE;
568 lp = (struct disklabel *)
569 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
570 bzero((char *)lp, sizeof *lp);
573 * If we are not installing a boot program but we are installing a
574 * label on disk then we must read the current bootarea so we don't
575 * clobber the existing boot.
579 if (read(f, boot, BBSIZE) < BBSIZE)
580 err(4, "%s", specname);
581 bzero((char *)lp, sizeof *lp);
586 * We are installing a boot program. Determine the name(s) and
587 * read them into the appropriate places in the boot area.
589 if (!xxboot || !bootxx) {
591 if ((p = rindex(dkname, '/')) == NULL)
595 while (*p && !isdigit(*p))
600 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
605 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
612 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
613 xxboot, bootxx ? bootxx : "NONE");
618 * 1. One-piece bootstrap (hp300/hp800)
619 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
620 * is remembered and written later following the bootarea.
621 * 2. Two-piece bootstraps (vax/i386?/mips?)
622 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
623 * bytes of bootarea, remaining d_bbsize-d_secsize filled
626 b = open(xxboot, O_RDONLY);
628 err(4, "%s", xxboot);
633 * The i386 has the so-called fdisk table embedded into the
634 * primary bootstrap. We take care to not clobber it, but
635 * only if it does already contain some data. (Otherwise,
636 * the xxboot provides a template.)
638 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
639 err(4, "%s", xxboot);
640 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
642 if (read(b, boot, (int)dp->d_secsize) < 0)
643 err(4, "%s", xxboot);
646 for (i = DOSPARTOFF, found = 0;
647 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
649 found = tmpbuf[i] != 0;
651 memcpy((void *)&boot[DOSPARTOFF],
652 (void *)&tmpbuf[DOSPARTOFF],
653 NDOSPART * sizeof(struct dos_partition));
656 b = open(bootxx, O_RDONLY);
658 err(4, "%s", bootxx);
659 if (fstat(b, &sb) != 0)
660 err(4, "%s", bootxx);
661 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
662 errx(4, "%s too large", bootxx);
663 if (read(b, &boot[dp->d_secsize],
664 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
665 err(4, "%s", bootxx);
666 #else /* !(NUMBOOT > 1) */
669 * On the alpha, the primary bootstrap starts at the
670 * second sector of the boot area. The first sector
671 * contains the label and must be edited to contain the
672 * size and location of the primary bootstrap.
674 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
676 err(4, "%s", xxboot);
677 bootinfo = (u_long *)(boot + 480);
678 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
679 bootinfo[1] = 1; /* start at sector 1 */
680 bootinfo[2] = 0; /* flags (must be zero) */
681 #else /* !__alpha__ */
682 if (read(b, boot, (int)dp->d_bbsize) < 0)
683 err(4, "%s", xxboot);
684 #endif /* __alpha__ */
685 if (fstat(b, &sb) != 0)
686 err(4, "%s", xxboot);
687 bootsize = (int)sb.st_size - dp->d_bbsize;
689 /* XXX assume d_secsize is a power of two */
690 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
691 bootbuf = (char *)malloc((size_t)bootsize);
693 err(4, "%s", xxboot);
694 if (read(b, bootbuf, bootsize) < 0) {
696 err(4, "%s", xxboot);
699 #endif /* NUMBOOT > 1 */
701 #endif /* NUMBOOT > 0 */
703 * Make sure no part of the bootstrap is written in the area
704 * reserved for the label.
706 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
708 errx(2, "bootstrap doesn't leave room for disk label");
715 register struct disklabel *lp;
718 register struct partition *pp;
720 fprintf(f, "# %s:\n", specname);
721 if ((unsigned) lp->d_type < DKMAXTYPES)
722 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
724 fprintf(f, "type: %u\n", lp->d_type);
725 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
727 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
729 fprintf(f, "flags:");
730 if (lp->d_flags & D_REMOVABLE)
731 fprintf(f, " removeable");
732 if (lp->d_flags & D_ECC)
734 if (lp->d_flags & D_BADSECT)
735 fprintf(f, " badsect");
737 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
738 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
739 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
740 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
741 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
742 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
743 fprintf(f, "rpm: %u\n", lp->d_rpm);
744 fprintf(f, "interleave: %u\n", lp->d_interleave);
745 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
746 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
747 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
748 (u_long)lp->d_headswitch);
749 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
750 (u_long)lp->d_trkseek);
751 fprintf(f, "drivedata: ");
752 for (i = NDDATA - 1; i >= 0; i--)
753 if (lp->d_drivedata[i])
757 for (j = 0; j <= i; j++)
758 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
759 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
761 "# size offset fstype [fsize bsize bps/cpg]\n");
762 pp = lp->d_partitions;
763 for (i = 0; i < lp->d_npartitions; i++, pp++) {
765 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
766 (u_long)pp->p_size, (u_long)pp->p_offset);
767 if ((unsigned) pp->p_fstype < FSMAXTYPES)
768 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
770 fprintf(f, "%8d", pp->p_fstype);
771 switch (pp->p_fstype) {
773 case FS_UNUSED: /* XXX */
774 fprintf(f, " %5lu %5lu %5.5s ",
776 (u_long)(pp->p_fsize * pp->p_frag), "");
780 fprintf(f, " %5lu %5lu %5u ",
782 (u_long)(pp->p_fsize * pp->p_frag),
787 fprintf(f, " %5lu %5lu %5d",
789 (u_long)(pp->p_fsize * pp->p_frag),
794 fprintf(f, "%20.20s", "");
797 fprintf(f, "\t# (Cyl. %4lu",
798 (u_long)(pp->p_offset / lp->d_secpercyl));
799 if (pp->p_offset % lp->d_secpercyl)
804 (u_long)((pp->p_offset + pp->p_size +
805 lp->d_secpercyl - 1) /
806 lp->d_secpercyl - 1));
807 if (pp->p_size % lp->d_secpercyl)
817 struct disklabel *lp;
821 struct disklabel label;
824 if ((fd = mkstemp(tmpfil)) == -1 ||
825 (fp = fdopen(fd, "w")) == NULL) {
826 warnx("can't create %s", tmpfil);
834 fp = fopen(tmpfil, "r");
836 warnx("can't reopen %s for reading", tmpfil);
839 bzero((char *)&label, sizeof(label));
840 if (getasciilabel(fp, &label)) {
842 if (writelabel(f, bootarea, lp) == 0) {
844 (void) unlink(tmpfil);
849 printf("re-edit the label? [y]: "); fflush(stdout);
851 if (c != EOF && c != (int)'\n')
852 while (getchar() != (int)'\n')
857 (void) unlink(tmpfil);
864 register int pid, xpid;
867 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
868 while ((pid = fork()) < 0) {
871 if (errno == EPROCLIM) {
872 warnx("you have too many processes");
875 if (errno != EAGAIN) {
887 if ((ed = getenv("EDITOR")) == (char *)0)
889 execlp(ed, ed, tmpfil, 0);
892 while ((xpid = wait(&stat)) >= 0)
904 while (*cp != '\0' && isspace(*cp))
906 if (*cp == '\0' || *cp == '#')
907 return ((char *)NULL);
917 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
919 if ((c = *cp) != '\0') {
924 return ((char *)NULL);
928 * Read an ascii label in from fd f,
929 * in the same format as that put out by display(),
935 register struct disklabel *lp;
937 register char **cpp, *cp;
938 register struct partition *pp;
939 char *tp, *s, line[BUFSIZ];
940 int v, lineno = 0, errors = 0;
942 lp->d_bbsize = BBSIZE; /* XXX */
943 lp->d_sbsize = SBSIZE; /* XXX */
944 while (fgets(line, sizeof(line) - 1, f)) {
946 if ((cp = index(line,'\n')) != 0)
953 fprintf(stderr, "line %d: syntax error\n", lineno);
957 *tp++ = '\0', tp = skip(tp);
958 if (streq(cp, "type")) {
962 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
963 if ((s = *cpp) && streq(s, tp)) {
964 lp->d_type = cpp - dktypenames;
968 if ((unsigned)v >= DKMAXTYPES)
969 fprintf(stderr, "line %d:%s %d\n", lineno,
970 "Warning, unknown disk type", v);
974 if (streq(cp, "flags")) {
975 for (v = 0; (cp = tp) && *cp != '\0';) {
977 if (streq(cp, "removeable"))
979 else if (streq(cp, "ecc"))
981 else if (streq(cp, "badsect"))
985 "line %d: %s: bad flag\n",
993 if (streq(cp, "drivedata")) {
996 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
997 lp->d_drivedata[i++] = atoi(cp);
1002 if (sscanf(cp, "%d partitions", &v) == 1) {
1003 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
1005 "line %d: bad # of partitions\n", lineno);
1006 lp->d_npartitions = MAXPARTITIONS;
1009 lp->d_npartitions = v;
1014 if (streq(cp, "disk")) {
1015 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1018 if (streq(cp, "label")) {
1019 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1022 if (streq(cp, "bytes/sector")) {
1024 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1026 "line %d: %s: bad sector size\n",
1033 if (streq(cp, "sectors/track")) {
1036 fprintf(stderr, "line %d: %s: bad %s\n",
1043 if (streq(cp, "sectors/cylinder")) {
1046 fprintf(stderr, "line %d: %s: bad %s\n",
1050 lp->d_secpercyl = v;
1053 if (streq(cp, "tracks/cylinder")) {
1056 fprintf(stderr, "line %d: %s: bad %s\n",
1063 if (streq(cp, "cylinders")) {
1066 fprintf(stderr, "line %d: %s: bad %s\n",
1070 lp->d_ncylinders = v;
1073 if (streq(cp, "sectors/unit")) {
1076 fprintf(stderr, "line %d: %s: bad %s\n",
1080 lp->d_secperunit = v;
1083 if (streq(cp, "rpm")) {
1086 fprintf(stderr, "line %d: %s: bad %s\n",
1093 if (streq(cp, "interleave")) {
1096 fprintf(stderr, "line %d: %s: bad %s\n",
1100 lp->d_interleave = v;
1103 if (streq(cp, "trackskew")) {
1106 fprintf(stderr, "line %d: %s: bad %s\n",
1110 lp->d_trackskew = v;
1113 if (streq(cp, "cylinderskew")) {
1116 fprintf(stderr, "line %d: %s: bad %s\n",
1123 if (streq(cp, "headswitch")) {
1126 fprintf(stderr, "line %d: %s: bad %s\n",
1130 lp->d_headswitch = v;
1133 if (streq(cp, "track-to-track seek")) {
1136 fprintf(stderr, "line %d: %s: bad %s\n",
1143 if ('a' <= *cp && *cp <= 'z' && cp[1] == '\0') {
1144 unsigned part = *cp - 'a';
1146 if (part > lp->d_npartitions) {
1148 "line %d: bad partition name\n", lineno);
1152 pp = &lp->d_partitions[part];
1153 #define NXTNUM(n) { \
1155 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1159 cp = tp, tp = word(cp); \
1169 "line %d: %s: bad partition size\n",
1177 "line %d: %s: bad partition offset\n",
1182 cp = tp, tp = word(cp);
1184 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1185 if ((s = *cpp) && streq(s, cp)) {
1186 pp->p_fstype = cpp - fstypenames;
1193 if ((unsigned)v >= FSMAXTYPES) {
1194 fprintf(stderr, "line %d: %s %s\n", lineno,
1195 "Warning, unknown filesystem type", cp);
1201 switch (pp->p_fstype) {
1203 case FS_UNUSED: /* XXX */
1204 NXTNUM(pp->p_fsize);
1205 if (pp->p_fsize == 0)
1208 pp->p_frag = v / pp->p_fsize;
1212 NXTNUM(pp->p_fsize);
1213 if (pp->p_fsize == 0)
1216 pp->p_frag = v / pp->p_fsize;
1221 NXTNUM(pp->p_fsize);
1222 if (pp->p_fsize == 0)
1225 pp->p_frag = v / pp->p_fsize;
1234 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1240 errors += checklabel(lp);
1241 return (errors == 0);
1245 * Check disklabel for errors and fill in
1246 * derived fields according to supplied values.
1250 register struct disklabel *lp;
1252 register struct partition *pp;
1256 if (lp->d_secsize == 0) {
1257 fprintf(stderr, "sector size 0\n");
1260 if (lp->d_nsectors == 0) {
1261 fprintf(stderr, "sectors/track 0\n");
1264 if (lp->d_ntracks == 0) {
1265 fprintf(stderr, "tracks/cylinder 0\n");
1268 if (lp->d_ncylinders == 0) {
1269 fprintf(stderr, "cylinders/unit 0\n");
1273 Warning("revolutions/minute 0");
1274 if (lp->d_secpercyl == 0)
1275 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1276 if (lp->d_secperunit == 0)
1277 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1278 if (lp->d_bbsize == 0) {
1279 fprintf(stderr, "boot block size 0\n");
1281 } else if (lp->d_bbsize % lp->d_secsize)
1282 Warning("boot block size %% sector-size != 0");
1283 if (lp->d_sbsize == 0) {
1284 fprintf(stderr, "super block size 0\n");
1286 } else if (lp->d_sbsize % lp->d_secsize)
1287 Warning("super block size %% sector-size != 0");
1288 if (lp->d_npartitions > MAXPARTITIONS)
1289 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1290 (u_long)lp->d_npartitions, MAXPARTITIONS);
1291 for (i = 0; i < lp->d_npartitions; i++) {
1293 pp = &lp->d_partitions[i];
1294 if (pp->p_size == 0 && pp->p_offset != 0)
1295 Warning("partition %c: size 0, but offset %lu",
1296 part, (u_long)pp->p_offset);
1298 if (pp->p_size % lp->d_secpercyl)
1299 Warning("partition %c: size %% cylinder-size != 0",
1301 if (pp->p_offset % lp->d_secpercyl)
1302 Warning("partition %c: offset %% cylinder-size != 0",
1305 if (pp->p_offset > lp->d_secperunit) {
1307 "partition %c: offset past end of unit\n", part);
1310 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1312 "partition %c: partition extends past end of unit\n",
1317 for (; i < MAXPARTITIONS; i++) {
1319 pp = &lp->d_partitions[i];
1320 if (pp->p_size || pp->p_offset)
1321 Warning("unused partition %c: size %d offset %lu",
1322 'a' + i, pp->p_size, (u_long)pp->p_offset);
1328 * When operating on a "virgin" disk, try getting an initial label
1329 * from the associated device driver. This might work for all device
1330 * drivers that are able to fetch some initial device parameters
1331 * without even having access to a (BSD) disklabel, like SCSI disks,
1332 * most IDE drives, or vn devices.
1334 * The device name must be given in its "canonical" form.
1337 getvirginlabel(void)
1339 static struct disklabel lab;
1340 char namebuf[BBSIZE];
1343 if (dkname[0] == '/') {
1344 warnx("\"auto\" requires the usage of a canonical disk name");
1347 (void)snprintf(namebuf, BBSIZE, "%sr%s", _PATH_DEV, dkname);
1348 if ((f = open(namebuf, O_RDONLY)) == -1) {
1349 warn("cannot open %s", namebuf);
1352 if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1353 warn("ioctl DIOCGDINFO");
1364 * If we are installing a boot program that doesn't fit in d_bbsize
1365 * we need to mark those partitions that the boot overflows into.
1366 * This allows newfs to prevent creation of a filesystem where it might
1367 * clobber bootstrap code.
1371 register struct disklabel *lp;
1373 register struct partition *pp;
1380 boffset = bootsize / lp->d_secsize;
1381 for (i = 0; i < lp->d_npartitions; i++) {
1383 pp = &lp->d_partitions[i];
1384 if (pp->p_size == 0)
1386 if (boffset <= pp->p_offset) {
1387 if (pp->p_fstype == FS_BOOT)
1388 pp->p_fstype = FS_UNUSED;
1389 } else if (pp->p_fstype != FS_BOOT) {
1390 if (pp->p_fstype != FS_UNUSED) {
1392 "boot overlaps used partition %c\n",
1396 pp->p_fstype = FS_BOOT;
1397 Warning("boot overlaps partition %c, %s",
1398 part, "marked as FS_BOOT");
1403 errx(4, "cannot install boot program");
1408 Warning(char *fmt, ...)
1412 fprintf(stderr, "Warning, ");
1414 vfprintf(stderr, fmt, ap);
1415 fprintf(stderr, "\n");
1423 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",
1424 "usage: disklabel [-r] disk",
1425 "\t\t(to read label)",
1426 " disklabel -w [-r] disk type [ packid ]",
1427 "\t\t(to write label with existing boot program)",
1428 " disklabel -e [-r] disk",
1429 "\t\t(to edit label)",
1430 " disklabel -R [-r] disk protofile",
1431 "\t\t(to restore label with existing boot program)",
1433 " disklabel -B [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1434 "\t\t(to install boot program with existing label)",
1435 " disklabel -w -B [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1436 "\t\t(to write label and boot program)",
1437 " disklabel -R -B [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1438 "\t\t(to restore label and boot program)",
1440 " disklabel -B [ -b bootprog ] disk [ type ]",
1441 "\t\t(to install boot program with existing on-disk label)",
1442 " disklabel -w -B [ -b bootprog ] disk type [ packid ]",
1443 "\t\t(to write label and install boot program)",
1444 " disklabel -R -B [ -b bootprog ] disk protofile [ type ]",
1445 "\t\t(to restore label and install boot program)",
1447 " disklabel [-NW] disk",
1448 "\t\t(to write disable/enable label)");
1450 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1451 "usage: disklabel [-r] disk", "(to read label)",
1452 " disklabel -w [-r] disk type [ packid ]",
1453 "\t\t(to write label)",
1454 " disklabel -e [-r] disk",
1455 "\t\t(to edit label)",
1456 " disklabel -R [-r] disk protofile",
1457 "\t\t(to restore label)",
1458 " disklabel [-NW] disk",
1459 "\t\t(to write disable/enable label)");