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>
57 #include <sys/disklabel.h>
58 #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 */
83 /* FIX! These are too low, but are traditional */
84 #define DEFAULT_NEWFS_BLOCK 8192U
85 #define DEFAULT_NEWFS_FRAG 1024U
86 #define DEFAULT_NEWFS_CPG 16U
88 #define BIG_NEWFS_BLOCK 16384U
89 #define BIG_NEWFS_FRAG 4096U
90 #define BIG_NEWFS_CPG 64U
95 #if defined(__alpha__) || defined(hp300) || defined(hp800)
102 void makelabel __P((char *, char *, struct disklabel *));
103 int writelabel __P((int, char *, struct disklabel *));
104 void l_perror __P((char *));
105 struct disklabel * readlabel __P((int));
106 struct disklabel * makebootarea __P((char *, struct disklabel *, int));
107 void display __P((FILE *, struct disklabel *));
108 int edit __P((struct disklabel *, int));
109 int editit __P((void));
110 char * skip __P((char *));
111 char * word __P((char *));
112 int getasciilabel __P((FILE *, struct disklabel *));
113 int checklabel __P((struct disklabel *));
114 void setbootflag __P((struct disklabel *));
115 void Warning (char *, ...) __printflike(1, 2);
116 void usage __P((void));
117 struct disklabel * getvirginlabel __P((void));
119 #define DEFEDITOR _PATH_VI
120 #define streq(a,b) (strcmp(a,b) == 0)
124 char tmpfil[] = PATH_TMPFILE;
126 char namebuf[BBSIZE], *np = namebuf;
127 struct disklabel lab;
128 char bootarea[BBSIZE];
130 /* partition 'c' is the full disk and is special */
131 #define FULL_DISK_PART 2
132 #define MAX_PART ('z')
133 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
134 char part_size_type[MAX_NUM_PARTS];
135 char part_offset_type[MAX_NUM_PARTS];
136 int part_set[MAX_NUM_PARTS];
139 int installboot; /* non-zero if we should install a boot program */
140 char *bootbuf; /* pointer to buffer with remainder of boot prog */
141 int bootsize; /* size of remaining boot program */
142 char *xxboot; /* primary boot */
143 char *bootxx; /* secondary boot */
144 char boot0[MAXPATHLEN];
145 char boot1[MAXPATHLEN];
149 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
153 int disable_write; /* set to disable writing to disk label */
157 #define OPTIONS "BNRWb:denrs:w"
159 #define OPTIONS "BNRWb:enrs:w"
167 register struct disklabel *lp;
169 int ch, f = 0, flag, error = 0;
172 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
247 if (dkname[0] != '/') {
248 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
250 np += strlen(specname) + 1;
253 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
254 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
255 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
256 np = namebuf + strlen(specname) + 1;
257 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
260 err(4, "%s", specname);
276 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
277 err(4, "ioctl DIOCWLABEL");
285 error = checklabel(lp);
290 if (installboot && argc == 3) {
291 makelabel(argv[2], 0, &lab);
295 * We only called makelabel() for its side effect
296 * of setting the bootstrap file names. Discard
297 * all changes to `lab' so that all values in the
298 * final label come from the ASCII label.
300 bzero((char *)&lab, sizeof(lab));
305 if (!(t = fopen(argv[1], "r")))
306 err(4, "%s", argv[1]);
307 if (!getasciilabel(t, &lab))
309 lp = makebootarea(bootarea, &lab, f);
311 error = writelabel(f, bootarea, lp);
321 makelabel(argv[1], name, &lab);
322 lp = makebootarea(bootarea, &lab, f);
324 if (checklabel(lp) == 0)
325 error = writelabel(f, bootarea, lp);
330 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
331 err(4, "ioctl DIOCWLABEL");
337 struct disklabel tlab;
342 makelabel(argv[1], 0, &lab);
343 lp = makebootarea(bootarea, &lab, f);
345 if (checklabel(lp) == 0)
346 error = writelabel(f, bootarea, lp);
355 * Construct a prototype disklabel from /etc/disktab. As a side
356 * effect, set the names of the primary and secondary boot files
360 makelabel(type, name, lp)
362 register struct disklabel *lp;
364 register struct disklabel *dp;
366 if (strcmp(type, "auto") == 0)
367 dp = getvirginlabel();
369 dp = getdiskbyname(type);
371 errx(1, "%s: unknown disk type", type);
375 * Set bootstrap name(s).
376 * 1. If set from command line, use those,
377 * 2. otherwise, check if disktab specifies them (b0 or b1),
378 * 3. otherwise, makebootarea() will choose ones based on the name
379 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
381 if (!xxboot && lp->d_boot0) {
382 if (*lp->d_boot0 != '/')
383 (void)sprintf(boot0, "%s/%s",
384 _PATH_BOOTDIR, lp->d_boot0);
386 (void)strcpy(boot0, lp->d_boot0);
390 if (!bootxx && lp->d_boot1) {
391 if (*lp->d_boot1 != '/')
392 (void)sprintf(boot1, "%s/%s",
393 _PATH_BOOTDIR, lp->d_boot1);
395 (void)strcpy(boot1, lp->d_boot1);
400 /* d_packname is union d_boot[01], so zero */
401 bzero(lp->d_packname, sizeof(lp->d_packname));
403 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
407 writelabel(f, boot, lp)
410 register struct disklabel *lp;
422 Warning("write to disk label supressed - label was as follows:");
427 lp->d_magic = DISKMAGIC;
428 lp->d_magic2 = DISKMAGIC;
430 lp->d_checksum = dkcksum(lp);
433 * First set the kernel disk label,
434 * then write a label to the raw disk.
435 * If the SDINFO ioctl fails because it is unimplemented,
436 * keep going; otherwise, the kernel consistency checks
437 * may prevent us from changing the current (in-core)
440 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
441 errno != ENODEV && errno != ENOTTY) {
442 l_perror("ioctl DIOCSDINFO");
445 (void)lseek(f, (off_t)0, SEEK_SET);
449 * Generate the bootblock checksum for the SRM console.
451 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
457 * write enable label sector before write (if necessary),
458 * disable after writing.
461 if (ioctl(f, DIOCWLABEL, &flag) < 0)
462 warn("ioctl DIOCWLABEL");
463 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
469 * Output the remainder of the disklabel
471 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
477 (void) ioctl(f, DIOCWLABEL, &flag);
478 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
479 l_perror("ioctl DIOCWDINFO");
483 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
486 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
487 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
488 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
490 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
491 warn("alternate label %d write", i/2);
506 warnx("%s: no disk label on disk;", s);
507 fprintf(stderr, "add \"-r\" to install initial label\n");
511 warnx("%s: label magic number or checksum is wrong!", s);
512 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
516 warnx("%s: open partition would move or shrink", s);
520 warnx("%s: '%c' partition must start at beginning of disk",
531 * Fetch disklabel for disk.
532 * Use ioctl to get label unless -r flag is given.
538 register struct disklabel *lp;
541 if (read(f, bootarea, BBSIZE) < BBSIZE)
542 err(4, "%s", specname);
543 for (lp = (struct disklabel *)bootarea;
544 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
545 lp = (struct disklabel *)((char *)lp + 16))
546 if (lp->d_magic == DISKMAGIC &&
547 lp->d_magic2 == DISKMAGIC)
549 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
550 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
553 "bad pack magic number (label is damaged, or pack is unlabeled)");
556 if (ioctl(f, DIOCGDINFO, lp) < 0)
557 err(4, "ioctl DIOCGDINFO");
563 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
564 * Returns a pointer to the disklabel portion of the bootarea.
567 makebootarea(boot, dp, f)
569 register struct disklabel *dp;
572 struct disklabel *lp;
589 if (dp->d_secsize == 0) {
590 dp->d_secsize = DEV_BSIZE;
591 dp->d_bbsize = BBSIZE;
593 lp = (struct disklabel *)
594 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
595 bzero((char *)lp, sizeof *lp);
598 * If we are not installing a boot program but we are installing a
599 * label on disk then we must read the current bootarea so we don't
600 * clobber the existing boot.
604 if (read(f, boot, BBSIZE) < BBSIZE)
605 err(4, "%s", specname);
606 bzero((char *)lp, sizeof *lp);
611 * We are installing a boot program. Determine the name(s) and
612 * read them into the appropriate places in the boot area.
614 if (!xxboot || !bootxx) {
616 if ((p = rindex(dkname, '/')) == NULL)
620 while (*p && !isdigit(*p))
625 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
630 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
637 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
638 xxboot, bootxx ? bootxx : "NONE");
643 * 1. One-piece bootstrap (hp300/hp800)
644 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
645 * is remembered and written later following the bootarea.
646 * 2. Two-piece bootstraps (vax/i386?/mips?)
647 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
648 * bytes of bootarea, remaining d_bbsize-d_secsize filled
651 b = open(xxboot, O_RDONLY);
653 err(4, "%s", xxboot);
658 * The i386 has the so-called fdisk table embedded into the
659 * primary bootstrap. We take care to not clobber it, but
660 * only if it does already contain some data. (Otherwise,
661 * the xxboot provides a template.)
663 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
664 err(4, "%s", xxboot);
665 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
667 if (read(b, boot, (int)dp->d_secsize) < 0)
668 err(4, "%s", xxboot);
671 for (i = DOSPARTOFF, found = 0;
672 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
674 found = tmpbuf[i] != 0;
676 memcpy((void *)&boot[DOSPARTOFF],
677 (void *)&tmpbuf[DOSPARTOFF],
678 NDOSPART * sizeof(struct dos_partition));
681 b = open(bootxx, O_RDONLY);
683 err(4, "%s", bootxx);
684 if (fstat(b, &sb) != 0)
685 err(4, "%s", bootxx);
686 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
687 errx(4, "%s too large", bootxx);
688 if (read(b, &boot[dp->d_secsize],
689 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
690 err(4, "%s", bootxx);
691 #else /* !(NUMBOOT > 1) */
694 * On the alpha, the primary bootstrap starts at the
695 * second sector of the boot area. The first sector
696 * contains the label and must be edited to contain the
697 * size and location of the primary bootstrap.
699 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
701 err(4, "%s", xxboot);
702 bootinfo = (u_long *)(boot + 480);
703 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
704 bootinfo[1] = 1; /* start at sector 1 */
705 bootinfo[2] = 0; /* flags (must be zero) */
706 #else /* !__alpha__ */
707 if (read(b, boot, (int)dp->d_bbsize) < 0)
708 err(4, "%s", xxboot);
709 #endif /* __alpha__ */
710 if (fstat(b, &sb) != 0)
711 err(4, "%s", xxboot);
712 bootsize = (int)sb.st_size - dp->d_bbsize;
714 /* XXX assume d_secsize is a power of two */
715 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
716 bootbuf = (char *)malloc((size_t)bootsize);
718 err(4, "%s", xxboot);
719 if (read(b, bootbuf, bootsize) < 0) {
721 err(4, "%s", xxboot);
724 #endif /* NUMBOOT > 1 */
726 #endif /* NUMBOOT > 0 */
728 * Make sure no part of the bootstrap is written in the area
729 * reserved for the label.
731 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
733 errx(2, "bootstrap doesn't leave room for disk label");
740 register struct disklabel *lp;
743 register struct partition *pp;
745 fprintf(f, "# %s:\n", specname);
746 if ((unsigned) lp->d_type < DKMAXTYPES)
747 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
749 fprintf(f, "type: %u\n", lp->d_type);
750 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
752 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
754 fprintf(f, "flags:");
755 if (lp->d_flags & D_REMOVABLE)
756 fprintf(f, " removeable");
757 if (lp->d_flags & D_ECC)
759 if (lp->d_flags & D_BADSECT)
760 fprintf(f, " badsect");
762 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
763 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
764 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
765 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
766 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
767 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
768 fprintf(f, "rpm: %u\n", lp->d_rpm);
769 fprintf(f, "interleave: %u\n", lp->d_interleave);
770 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
771 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
772 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
773 (u_long)lp->d_headswitch);
774 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
775 (u_long)lp->d_trkseek);
776 fprintf(f, "drivedata: ");
777 for (i = NDDATA - 1; i >= 0; i--)
778 if (lp->d_drivedata[i])
782 for (j = 0; j <= i; j++)
783 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
784 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
786 "# size offset fstype [fsize bsize bps/cpg]\n");
787 pp = lp->d_partitions;
788 for (i = 0; i < lp->d_npartitions; i++, pp++) {
790 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
791 (u_long)pp->p_size, (u_long)pp->p_offset);
792 if ((unsigned) pp->p_fstype < FSMAXTYPES)
793 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
795 fprintf(f, "%8d", pp->p_fstype);
796 switch (pp->p_fstype) {
798 case FS_UNUSED: /* XXX */
799 fprintf(f, " %5lu %5lu %5.5s ",
801 (u_long)(pp->p_fsize * pp->p_frag), "");
805 fprintf(f, " %5lu %5lu %5u ",
807 (u_long)(pp->p_fsize * pp->p_frag),
812 fprintf(f, " %5lu %5lu %5d",
814 (u_long)(pp->p_fsize * pp->p_frag),
819 fprintf(f, "%20.20s", "");
822 fprintf(f, "\t# (Cyl. %4lu",
823 (u_long)(pp->p_offset / lp->d_secpercyl));
824 if (pp->p_offset % lp->d_secpercyl)
829 (u_long)((pp->p_offset + pp->p_size +
830 lp->d_secpercyl - 1) /
831 lp->d_secpercyl - 1));
832 if (pp->p_size % lp->d_secpercyl)
842 struct disklabel *lp;
846 struct disklabel label;
849 if ((fd = mkstemp(tmpfil)) == -1 ||
850 (fp = fdopen(fd, "w")) == NULL) {
851 warnx("can't create %s", tmpfil);
859 fp = fopen(tmpfil, "r");
861 warnx("can't reopen %s for reading", tmpfil);
864 bzero((char *)&label, sizeof(label));
865 if (getasciilabel(fp, &label)) {
867 if (writelabel(f, bootarea, lp) == 0) {
869 (void) unlink(tmpfil);
874 printf("re-edit the label? [y]: "); fflush(stdout);
876 if (c != EOF && c != (int)'\n')
877 while (getchar() != (int)'\n')
882 (void) unlink(tmpfil);
889 register int pid, xpid;
892 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
893 while ((pid = fork()) < 0) {
894 if (errno == EPROCLIM) {
895 warnx("you have too many processes");
898 if (errno != EAGAIN) {
910 if ((ed = getenv("EDITOR")) == (char *)0)
912 execlp(ed, ed, tmpfil, (char *)0);
915 while ((xpid = wait(&stat)) >= 0)
927 while (*cp != '\0' && isspace(*cp))
929 if (*cp == '\0' || *cp == '#')
930 return ((char *)NULL);
940 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
942 if ((c = *cp) != '\0') {
947 return ((char *)NULL);
951 * Read an ascii label in from fd f,
952 * in the same format as that put out by display(),
958 register struct disklabel *lp;
960 register char **cpp, *cp;
961 register struct partition *pp;
963 char *tp, *s, line[BUFSIZ];
964 int v, lineno = 0, errors = 0;
966 lp->d_bbsize = BBSIZE; /* XXX */
967 lp->d_sbsize = SBSIZE; /* XXX */
968 while (fgets(line, sizeof(line) - 1, f)) {
970 if ((cp = index(line,'\n')) != 0)
977 fprintf(stderr, "line %d: syntax error\n", lineno);
981 *tp++ = '\0', tp = skip(tp);
982 if (streq(cp, "type")) {
986 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
987 if ((s = *cpp) && streq(s, tp)) {
988 lp->d_type = cpp - dktypenames;
992 if ((unsigned)v >= DKMAXTYPES)
993 fprintf(stderr, "line %d:%s %d\n", lineno,
994 "Warning, unknown disk type", v);
998 if (streq(cp, "flags")) {
999 for (v = 0; (cp = tp) && *cp != '\0';) {
1001 if (streq(cp, "removeable"))
1003 else if (streq(cp, "ecc"))
1005 else if (streq(cp, "badsect"))
1009 "line %d: %s: bad flag\n",
1017 if (streq(cp, "drivedata")) {
1020 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
1021 lp->d_drivedata[i++] = atoi(cp);
1026 if (sscanf(cp, "%d partitions", &v) == 1) {
1027 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
1029 "line %d: bad # of partitions\n", lineno);
1030 lp->d_npartitions = MAXPARTITIONS;
1033 lp->d_npartitions = v;
1038 if (streq(cp, "disk")) {
1039 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1042 if (streq(cp, "label")) {
1043 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1046 if (streq(cp, "bytes/sector")) {
1048 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1050 "line %d: %s: bad sector size\n",
1057 if (streq(cp, "sectors/track")) {
1060 fprintf(stderr, "line %d: %s: bad %s\n",
1067 if (streq(cp, "sectors/cylinder")) {
1070 fprintf(stderr, "line %d: %s: bad %s\n",
1074 lp->d_secpercyl = v;
1077 if (streq(cp, "tracks/cylinder")) {
1080 fprintf(stderr, "line %d: %s: bad %s\n",
1087 if (streq(cp, "cylinders")) {
1090 fprintf(stderr, "line %d: %s: bad %s\n",
1094 lp->d_ncylinders = v;
1097 if (streq(cp, "sectors/unit")) {
1100 fprintf(stderr, "line %d: %s: bad %s\n",
1104 lp->d_secperunit = v;
1107 if (streq(cp, "rpm")) {
1110 fprintf(stderr, "line %d: %s: bad %s\n",
1117 if (streq(cp, "interleave")) {
1120 fprintf(stderr, "line %d: %s: bad %s\n",
1124 lp->d_interleave = v;
1127 if (streq(cp, "trackskew")) {
1130 fprintf(stderr, "line %d: %s: bad %s\n",
1134 lp->d_trackskew = v;
1137 if (streq(cp, "cylinderskew")) {
1140 fprintf(stderr, "line %d: %s: bad %s\n",
1147 if (streq(cp, "headswitch")) {
1150 fprintf(stderr, "line %d: %s: bad %s\n",
1154 lp->d_headswitch = v;
1157 if (streq(cp, "track-to-track seek")) {
1160 fprintf(stderr, "line %d: %s: bad %s\n",
1167 /* the ':' was removed above */
1168 if ('a' <= *cp && *cp <= MAX_PART && cp[1] == '\0') {
1170 if (part >= lp->d_npartitions) {
1172 "line %d: partition name out of range a-%c: %s\n",
1173 lineno, 'a' + lp->d_npartitions - 1, cp);
1177 pp = &lp->d_partitions[part];
1179 #define NXTNUM(n) { \
1181 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1185 cp = tp, tp = word(cp); \
1191 /* retain 1 character following number */
1192 #define NXTWORD(w,n) { \
1194 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1199 cp = tp, tp = word(cp); \
1202 (n) = strtol(cp,&tmp,10); \
1203 if (tmp) (w) = *tmp; \
1207 NXTWORD(part_size_type[part],v);
1208 if (v < 0 || (v == 0 && part_size_type[part] != '*')) {
1210 "line %d: %s: bad partition size\n",
1218 NXTWORD(part_offset_type[part],v);
1219 if (v < 0 || (v == 0 &&
1220 part_offset_type[part] != '*' &&
1221 part_offset_type[part] != '\0')) {
1223 "line %d: %s: bad partition offset\n",
1229 cp = tp, tp = word(cp);
1231 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1232 if ((s = *cpp) && streq(s, cp)) {
1233 pp->p_fstype = cpp -
1241 if ((unsigned)v >= FSMAXTYPES) {
1243 "line %d: Warning, unknown "
1244 "filesystem type %s\n",
1251 * Note: NXTNUM will break us out of the
1254 switch (pp->p_fstype) {
1257 * allow us to accept defaults for
1261 NXTNUM(pp->p_fsize);
1262 if (pp->p_fsize == 0)
1265 pp->p_frag = v / pp->p_fsize;
1267 /* else default to 0's */
1270 /* These happen to be the same */
1274 NXTNUM(pp->p_fsize);
1275 if (pp->p_fsize == 0)
1278 pp->p_frag = v / pp->p_fsize;
1282 * FIX! poor attempt at
1286 if (pp->p_size < 1*1024*1024*1024/lp->d_secsize) {
1287 /* FIX! These are too low, but are traditional */
1288 pp->p_fsize = DEFAULT_NEWFS_BLOCK;
1289 pp->p_frag = (unsigned char) DEFAULT_NEWFS_FRAG;
1290 pp->p_cpg = DEFAULT_NEWFS_CPG;
1292 pp->p_fsize = BIG_NEWFS_BLOCK;
1293 pp->p_frag = (unsigned char) BIG_NEWFS_FRAG;
1294 pp->p_cpg = BIG_NEWFS_CPG;
1303 * note: we may not have
1304 * gotten all the entries for
1305 * the fs though if we didn't,
1306 * errors will be set.
1312 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1317 errors += checklabel(lp);
1318 return (errors == 0);
1322 * Check disklabel for errors and fill in
1323 * derived fields according to supplied values.
1327 register struct disklabel *lp;
1329 register struct partition *pp;
1332 unsigned long total_size, total_percent, current_offset;
1333 int seen_default_offset;
1336 struct partition *pp2;
1338 if (lp->d_secsize == 0) {
1339 fprintf(stderr, "sector size 0\n");
1342 if (lp->d_nsectors == 0) {
1343 fprintf(stderr, "sectors/track 0\n");
1346 if (lp->d_ntracks == 0) {
1347 fprintf(stderr, "tracks/cylinder 0\n");
1350 if (lp->d_ncylinders == 0) {
1351 fprintf(stderr, "cylinders/unit 0\n");
1355 Warning("revolutions/minute 0");
1356 if (lp->d_secpercyl == 0)
1357 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1358 if (lp->d_secperunit == 0)
1359 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1360 if (lp->d_bbsize == 0) {
1361 fprintf(stderr, "boot block size 0\n");
1363 } else if (lp->d_bbsize % lp->d_secsize)
1364 Warning("boot block size %% sector-size != 0");
1365 if (lp->d_sbsize == 0) {
1366 fprintf(stderr, "super block size 0\n");
1368 } else if (lp->d_sbsize % lp->d_secsize)
1369 Warning("super block size %% sector-size != 0");
1370 if (lp->d_npartitions > MAXPARTITIONS)
1371 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1372 (u_long)lp->d_npartitions, MAXPARTITIONS);
1374 /* first allocate space to the partitions, then offsets */
1375 total_size = 0; /* in sectors */
1376 total_percent = 0; /* in percent */
1378 /* find all fixed partitions */
1379 for (i = 0; i < lp->d_npartitions; i++) {
1380 pp = &lp->d_partitions[i];
1382 if (part_size_type[i] == '*') {
1383 /* partition 2 ('c') is special */
1384 if (i == FULL_DISK_PART) {
1385 pp->p_size = lp->d_secperunit;
1388 Warning("Too many '*' partitions (%c and %c)",
1389 hog_part + 'a',i + 'a');
1397 switch (part_size_type[i]) {
1399 total_percent += size;
1407 size *= 1024ULL * 1024ULL;
1411 size *= 1024ULL * 1024ULL * 1024ULL;
1416 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1419 /* don't count %'s yet */
1420 if (part_size_type[i] != '%') {
1422 * for all not in sectors, convert to
1425 if (part_size_type[i] != '\0') {
1426 if (size % lp->d_secsize != 0)
1427 Warning("partition %c not an integer number of sectors",
1429 size /= lp->d_secsize;
1432 /* else already in sectors */
1433 /* partition 2 ('c') is special */
1434 if (i != FULL_DISK_PART)
1440 /* handle % partitions - note %'s don't need to add up to 100! */
1441 if (total_percent != 0) {
1442 long free_space = lp->d_secperunit - total_size;
1443 if (total_percent > 100) {
1444 fprintf(stderr,"total percentage %d is greater than 100\n",
1449 if (free_space > 0) {
1450 for (i = 0; i < lp->d_npartitions; i++) {
1451 pp = &lp->d_partitions[i];
1452 if (part_set[i] && part_size_type[i] == '%') {
1453 /* careful of overflows! and integer roundoff */
1454 pp->p_size = ((double)pp->p_size/100) * free_space;
1455 total_size += pp->p_size;
1457 /* FIX we can lose a sector or so due to roundoff per
1458 partition. A more complex algorithm could avoid that */
1463 "%ld sectors available to give to '*' and '%' partitions\n",
1466 /* fix? set all % partitions to size 0? */
1469 /* give anything remaining to the hog partition */
1470 if (hog_part != -1) {
1471 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1472 total_size = lp->d_secperunit;
1475 /* Now set the offsets for each partition */
1476 current_offset = 0; /* in sectors */
1477 seen_default_offset = 0;
1478 for (i = 0; i < lp->d_npartitions; i++) {
1480 pp = &lp->d_partitions[i];
1482 if (part_offset_type[i] == '*') {
1483 /* partition 2 ('c') is special */
1484 if (i == FULL_DISK_PART) {
1487 pp->p_offset = current_offset;
1488 seen_default_offset = 1;
1491 /* allow them to be out of order for old-style tables */
1492 /* partition 2 ('c') is special */
1493 if (pp->p_offset < current_offset &&
1494 seen_default_offset && i != FULL_DISK_PART) {
1496 "Offset %ld for partition %c overlaps previous partition which ends at %ld\n",
1497 pp->p_offset,i+'a',current_offset);
1499 "Labels with any *'s for offset must be in ascending order by sector\n");
1501 } else if (pp->p_offset != current_offset &&
1502 i != FULL_DISK_PART && seen_default_offset) {
1504 * this may give unneeded warnings if
1505 * partitions are out-of-order
1508 "Offset %ld for partition %c doesn't match expected value %ld",
1509 pp->p_offset, i + 'a', current_offset);
1512 /* partition 2 ('c') is special */
1513 if (i != FULL_DISK_PART)
1514 current_offset = pp->p_offset + pp->p_size;
1518 for (i = 0; i < lp->d_npartitions; i++) {
1520 pp = &lp->d_partitions[i];
1521 if (pp->p_size == 0 && pp->p_offset != 0)
1522 Warning("partition %c: size 0, but offset %lu",
1523 part, (u_long)pp->p_offset);
1525 if (pp->p_size % lp->d_secpercyl)
1526 Warning("partition %c: size %% cylinder-size != 0",
1528 if (pp->p_offset % lp->d_secpercyl)
1529 Warning("partition %c: offset %% cylinder-size != 0",
1532 if (pp->p_offset > lp->d_secperunit) {
1534 "partition %c: offset past end of unit\n", part);
1537 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1539 "partition %c: partition extends past end of unit\n",
1543 if (i == FULL_DISK_PART)
1545 if (pp->p_fstype != FS_UNUSED)
1546 Warning("partition %c is not marked as unused!",part);
1547 if (pp->p_offset != 0)
1548 Warning("partition %c doesn't start at 0!",part);
1549 if (pp->p_size != lp->d_secperunit)
1550 Warning("partition %c doesn't cover the whole unit!",part);
1552 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1553 (pp->p_size != lp->d_secperunit)) {
1554 Warning("An incorrect partition %c may cause problems for "
1555 "standard system utilities",part);
1559 /* check for overlaps */
1560 /* this will check for all possible overlaps once and only once */
1561 for (j = 0; j < i; j++) {
1562 /* partition 2 ('c') is special */
1563 if (j != FULL_DISK_PART && i != FULL_DISK_PART &&
1564 part_set[i] && part_set[j]) {
1565 pp2 = &lp->d_partitions[j];
1566 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1567 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1568 pp2->p_offset >= pp->p_offset)) {
1569 fprintf(stderr,"partitions %c and %c overlap!\n",
1576 for (; i < MAXPARTITIONS; i++) {
1578 pp = &lp->d_partitions[i];
1579 if (pp->p_size || pp->p_offset)
1580 Warning("unused partition %c: size %d offset %lu",
1581 'a' + i, pp->p_size, (u_long)pp->p_offset);
1587 * When operating on a "virgin" disk, try getting an initial label
1588 * from the associated device driver. This might work for all device
1589 * drivers that are able to fetch some initial device parameters
1590 * without even having access to a (BSD) disklabel, like SCSI disks,
1591 * most IDE drives, or vn devices.
1593 * The device name must be given in its "canonical" form.
1596 getvirginlabel(void)
1598 static struct disklabel lab;
1599 char namebuf[BBSIZE];
1602 if (dkname[0] == '/') {
1603 warnx("\"auto\" requires the usage of a canonical disk name");
1606 (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1607 if ((f = open(namebuf, O_RDONLY)) == -1) {
1608 warn("cannot open %s", namebuf);
1613 * Try to use the new get-virgin-label ioctl. If it fails,
1614 * fallback to the old get-disdk-info ioctl.
1616 if (ioctl(f, DIOCGDVIRGIN, &lab) == 0)
1618 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1621 (void)snprintf(namebuf, BBSIZE, "%s%s%c", _PATH_DEV, dkname,
1623 if ((f = open(namebuf, O_RDONLY)) == -1) {
1624 warn("cannot open %s", namebuf);
1627 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1630 warn("No virgin disklabel found %s", namebuf);
1640 * If we are installing a boot program that doesn't fit in d_bbsize
1641 * we need to mark those partitions that the boot overflows into.
1642 * This allows newfs to prevent creation of a filesystem where it might
1643 * clobber bootstrap code.
1647 register struct disklabel *lp;
1649 register struct partition *pp;
1656 boffset = bootsize / lp->d_secsize;
1657 for (i = 0; i < lp->d_npartitions; i++) {
1659 pp = &lp->d_partitions[i];
1660 if (pp->p_size == 0)
1662 if (boffset <= pp->p_offset) {
1663 if (pp->p_fstype == FS_BOOT)
1664 pp->p_fstype = FS_UNUSED;
1665 } else if (pp->p_fstype != FS_BOOT) {
1666 if (pp->p_fstype != FS_UNUSED) {
1668 "boot overlaps used partition %c\n",
1672 pp->p_fstype = FS_BOOT;
1673 Warning("boot overlaps partition %c, %s",
1674 part, "marked as FS_BOOT");
1679 errx(4, "cannot install boot program");
1684 Warning(char *fmt, ...)
1688 fprintf(stderr, "Warning, ");
1690 vfprintf(stderr, fmt, ap);
1691 fprintf(stderr, "\n");
1699 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",
1700 "usage: disklabel [-r] disk",
1701 "\t\t(to read label)",
1702 " disklabel -w [-r] [-n] disk type [ packid ]",
1703 "\t\t(to write label with existing boot program)",
1704 " disklabel -e [-r] [-n] disk",
1705 "\t\t(to edit label)",
1706 " disklabel -R [-r] [-n] disk protofile",
1707 "\t\t(to restore label with existing boot program)",
1709 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1710 "\t\t(to install boot program with existing label)",
1711 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1712 "\t\t(to write label and boot program)",
1713 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1714 "\t\t(to restore label and boot program)",
1716 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1717 "\t\t(to install boot program with existing on-disk label)",
1718 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1719 "\t\t(to write label and install boot program)",
1720 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1721 "\t\t(to restore label and install boot program)",
1723 " disklabel [-NW] disk",
1724 "\t\t(to write disable/enable label)");
1726 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1727 "usage: disklabel [-r] disk", "(to read label)",
1728 " disklabel -w [-r] [-n] disk type [ packid ]",
1729 "\t\t(to write label)",
1730 " disklabel -e [-r] [-n] disk",
1731 "\t\t(to edit label)",
1732 " disklabel -R [-r] [-n] disk protofile",
1733 "\t\t(to restore label)",
1734 " disklabel [-NW] disk",
1735 "\t\t(to write disable/enable label)");