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
92 #if defined(__i386__) || defined(__ia64__)
94 #elif defined(__alpha__) || defined(__sparc64__)
97 #error I do not know about this architecture.
100 void makelabel __P((char *, char *, struct disklabel *));
101 int writelabel __P((int, char *, struct disklabel *));
102 void l_perror __P((char *));
103 struct disklabel * readlabel __P((int));
104 struct disklabel * makebootarea __P((char *, struct disklabel *, int));
105 void display __P((FILE *, struct disklabel *));
106 int edit __P((struct disklabel *, int));
107 int editit __P((void));
108 char * skip __P((char *));
109 char * word __P((char *));
110 int getasciilabel __P((FILE *, struct disklabel *));
111 int checklabel __P((struct disklabel *));
112 void setbootflag __P((struct disklabel *));
113 void Warning (char *, ...) __printflike(1, 2);
114 void usage __P((void));
115 struct disklabel * getvirginlabel __P((void));
117 #define DEFEDITOR _PATH_VI
118 #define streq(a,b) (strcmp(a,b) == 0)
122 char tmpfil[] = PATH_TMPFILE;
124 char namebuf[BBSIZE], *np = namebuf;
125 struct disklabel lab;
126 char bootarea[BBSIZE];
128 /* partition 'c' is the full disk and is special */
129 #define FULL_DISK_PART 2
130 #define MAX_PART ('z')
131 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
132 char part_size_type[MAX_NUM_PARTS];
133 char part_offset_type[MAX_NUM_PARTS];
134 int part_set[MAX_NUM_PARTS];
137 int installboot; /* non-zero if we should install a boot program */
138 char *bootbuf; /* pointer to buffer with remainder of boot prog */
139 int bootsize; /* size of remaining boot program */
140 char *xxboot; /* primary boot */
141 char *bootxx; /* secondary boot */
142 char boot0[MAXPATHLEN];
143 char boot1[MAXPATHLEN];
147 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
151 int disable_write; /* set to disable writing to disk label */
155 #define OPTIONS "BNRWb:denrs:w"
157 #define OPTIONS "BNRWb:enrs:w"
165 register struct disklabel *lp;
167 int ch, f = 0, flag, error = 0;
170 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
245 if (dkname[0] != '/') {
246 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
248 np += strlen(specname) + 1;
251 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
252 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
253 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
254 np = namebuf + strlen(specname) + 1;
255 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
258 err(4, "%s", specname);
274 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
275 err(4, "ioctl DIOCWLABEL");
283 error = checklabel(lp);
288 if (installboot && argc == 3) {
289 makelabel(argv[2], 0, &lab);
293 * We only called makelabel() for its side effect
294 * of setting the bootstrap file names. Discard
295 * all changes to `lab' so that all values in the
296 * final label come from the ASCII label.
298 bzero((char *)&lab, sizeof(lab));
303 if (!(t = fopen(argv[1], "r")))
304 err(4, "%s", argv[1]);
305 if (!getasciilabel(t, &lab))
307 lp = makebootarea(bootarea, &lab, f);
309 error = writelabel(f, bootarea, lp);
319 makelabel(argv[1], name, &lab);
320 lp = makebootarea(bootarea, &lab, f);
322 if (checklabel(lp) == 0)
323 error = writelabel(f, bootarea, lp);
328 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
329 err(4, "ioctl DIOCWLABEL");
335 struct disklabel tlab;
340 makelabel(argv[1], 0, &lab);
341 lp = makebootarea(bootarea, &lab, f);
343 if (checklabel(lp) == 0)
344 error = writelabel(f, bootarea, lp);
353 * Construct a prototype disklabel from /etc/disktab. As a side
354 * effect, set the names of the primary and secondary boot files
358 makelabel(type, name, lp)
360 register struct disklabel *lp;
362 register struct disklabel *dp;
364 if (strcmp(type, "auto") == 0)
365 dp = getvirginlabel();
367 dp = getdiskbyname(type);
369 errx(1, "%s: unknown disk type", type);
373 * Set bootstrap name(s).
374 * 1. If set from command line, use those,
375 * 2. otherwise, check if disktab specifies them (b0 or b1),
376 * 3. otherwise, makebootarea() will choose ones based on the name
377 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
379 if (!xxboot && lp->d_boot0) {
380 if (*lp->d_boot0 != '/')
381 (void)sprintf(boot0, "%s/%s",
382 _PATH_BOOTDIR, lp->d_boot0);
384 (void)strcpy(boot0, lp->d_boot0);
388 if (!bootxx && lp->d_boot1) {
389 if (*lp->d_boot1 != '/')
390 (void)sprintf(boot1, "%s/%s",
391 _PATH_BOOTDIR, lp->d_boot1);
393 (void)strcpy(boot1, lp->d_boot1);
398 /* d_packname is union d_boot[01], so zero */
399 bzero(lp->d_packname, sizeof(lp->d_packname));
401 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
405 writelabel(f, boot, lp)
408 register struct disklabel *lp;
420 Warning("write to disk label supressed - label was as follows:");
425 lp->d_magic = DISKMAGIC;
426 lp->d_magic2 = DISKMAGIC;
428 lp->d_checksum = dkcksum(lp);
431 * First set the kernel disk label,
432 * then write a label to the raw disk.
433 * If the SDINFO ioctl fails because it is unimplemented,
434 * keep going; otherwise, the kernel consistency checks
435 * may prevent us from changing the current (in-core)
438 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
439 errno != ENODEV && errno != ENOTTY) {
440 l_perror("ioctl DIOCSDINFO");
443 (void)lseek(f, (off_t)0, SEEK_SET);
447 * Generate the bootblock checksum for the SRM console.
449 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
455 * write enable label sector before write (if necessary),
456 * disable after writing.
459 if (ioctl(f, DIOCWLABEL, &flag) < 0)
460 warn("ioctl DIOCWLABEL");
461 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
467 * Output the remainder of the disklabel
469 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
475 (void) ioctl(f, DIOCWLABEL, &flag);
476 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
477 l_perror("ioctl DIOCWDINFO");
481 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
484 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
485 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
486 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
488 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
489 warn("alternate label %d write", i/2);
504 warnx("%s: no disk label on disk;", s);
505 fprintf(stderr, "add \"-r\" to install initial label\n");
509 warnx("%s: label magic number or checksum is wrong!", s);
510 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
514 warnx("%s: open partition would move or shrink", s);
518 warnx("%s: '%c' partition must start at beginning of disk",
529 * Fetch disklabel for disk.
530 * Use ioctl to get label unless -r flag is given.
536 register struct disklabel *lp;
539 if (read(f, bootarea, BBSIZE) < BBSIZE)
540 err(4, "%s", specname);
541 for (lp = (struct disklabel *)bootarea;
542 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
543 lp = (struct disklabel *)((char *)lp + 16))
544 if (lp->d_magic == DISKMAGIC &&
545 lp->d_magic2 == DISKMAGIC)
547 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
548 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
551 "bad pack magic number (label is damaged, or pack is unlabeled)");
554 if (ioctl(f, DIOCGDINFO, lp) < 0)
555 err(4, "ioctl DIOCGDINFO");
561 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
562 * Returns a pointer to the disklabel portion of the bootarea.
565 makebootarea(boot, dp, f)
567 register struct disklabel *dp;
570 struct disklabel *lp;
587 if (dp->d_secsize == 0) {
588 dp->d_secsize = DEV_BSIZE;
589 dp->d_bbsize = BBSIZE;
591 lp = (struct disklabel *)
592 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
593 bzero((char *)lp, sizeof *lp);
596 * If we are not installing a boot program but we are installing a
597 * label on disk then we must read the current bootarea so we don't
598 * clobber the existing boot.
602 if (read(f, boot, BBSIZE) < BBSIZE)
603 err(4, "%s", specname);
604 bzero((char *)lp, sizeof *lp);
609 * We are installing a boot program. Determine the name(s) and
610 * read them into the appropriate places in the boot area.
612 if (!xxboot || !bootxx) {
614 if ((p = rindex(dkname, '/')) == NULL)
618 while (*p && !isdigit(*p))
623 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
628 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
635 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
636 xxboot, bootxx ? bootxx : "NONE");
641 * 1. One-piece bootstrap (alpha/sparc64)
642 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
643 * is remembered and written later following the bootarea.
644 * 2. Two-piece bootstraps (vax/i386?/mips?)
645 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
646 * bytes of bootarea, remaining d_bbsize-d_secsize filled
649 b = open(xxboot, O_RDONLY);
651 err(4, "%s", xxboot);
656 * The i386 has the so-called fdisk table embedded into the
657 * primary bootstrap. We take care to not clobber it, but
658 * only if it does already contain some data. (Otherwise,
659 * the xxboot provides a template.)
661 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
662 err(4, "%s", xxboot);
663 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
665 if (read(b, boot, (int)dp->d_secsize) < 0)
666 err(4, "%s", xxboot);
669 for (i = DOSPARTOFF, found = 0;
670 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
672 found = tmpbuf[i] != 0;
674 memcpy((void *)&boot[DOSPARTOFF],
675 (void *)&tmpbuf[DOSPARTOFF],
676 NDOSPART * sizeof(struct dos_partition));
679 b = open(bootxx, O_RDONLY);
681 err(4, "%s", bootxx);
682 if (fstat(b, &sb) != 0)
683 err(4, "%s", bootxx);
684 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
685 errx(4, "%s too large", bootxx);
686 if (read(b, &boot[dp->d_secsize],
687 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
688 err(4, "%s", bootxx);
689 #else /* !(NUMBOOT > 1) */
692 * On the alpha, the primary bootstrap starts at the
693 * second sector of the boot area. The first sector
694 * contains the label and must be edited to contain the
695 * size and location of the primary bootstrap.
697 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
699 err(4, "%s", xxboot);
700 bootinfo = (u_long *)(boot + 480);
701 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
702 bootinfo[1] = 1; /* start at sector 1 */
703 bootinfo[2] = 0; /* flags (must be zero) */
704 #else /* !__alpha__ */
705 if (read(b, boot, (int)dp->d_bbsize) < 0)
706 err(4, "%s", xxboot);
707 #endif /* __alpha__ */
708 if (fstat(b, &sb) != 0)
709 err(4, "%s", xxboot);
710 bootsize = (int)sb.st_size - dp->d_bbsize;
712 /* XXX assume d_secsize is a power of two */
713 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
714 bootbuf = (char *)malloc((size_t)bootsize);
716 err(4, "%s", xxboot);
717 if (read(b, bootbuf, bootsize) < 0) {
719 err(4, "%s", xxboot);
722 #endif /* NUMBOOT > 1 */
724 #endif /* NUMBOOT > 0 */
726 * Make sure no part of the bootstrap is written in the area
727 * reserved for the label.
729 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
731 errx(2, "bootstrap doesn't leave room for disk label");
738 register struct disklabel *lp;
741 register struct partition *pp;
743 fprintf(f, "# %s:\n", specname);
744 if ((unsigned) lp->d_type < DKMAXTYPES)
745 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
747 fprintf(f, "type: %u\n", lp->d_type);
748 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
750 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
752 fprintf(f, "flags:");
753 if (lp->d_flags & D_REMOVABLE)
754 fprintf(f, " removeable");
755 if (lp->d_flags & D_ECC)
757 if (lp->d_flags & D_BADSECT)
758 fprintf(f, " badsect");
760 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
761 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
762 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
763 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
764 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
765 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
766 fprintf(f, "rpm: %u\n", lp->d_rpm);
767 fprintf(f, "interleave: %u\n", lp->d_interleave);
768 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
769 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
770 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
771 (u_long)lp->d_headswitch);
772 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
773 (u_long)lp->d_trkseek);
774 fprintf(f, "drivedata: ");
775 for (i = NDDATA - 1; i >= 0; i--)
776 if (lp->d_drivedata[i])
780 for (j = 0; j <= i; j++)
781 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
782 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
784 "# size offset fstype [fsize bsize bps/cpg]\n");
785 pp = lp->d_partitions;
786 for (i = 0; i < lp->d_npartitions; i++, pp++) {
788 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
789 (u_long)pp->p_size, (u_long)pp->p_offset);
790 if ((unsigned) pp->p_fstype < FSMAXTYPES)
791 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
793 fprintf(f, "%8d", pp->p_fstype);
794 switch (pp->p_fstype) {
796 case FS_UNUSED: /* XXX */
797 fprintf(f, " %5lu %5lu %5.5s ",
799 (u_long)(pp->p_fsize * pp->p_frag), "");
803 fprintf(f, " %5lu %5lu %5u ",
805 (u_long)(pp->p_fsize * pp->p_frag),
810 fprintf(f, " %5lu %5lu %5d",
812 (u_long)(pp->p_fsize * pp->p_frag),
817 fprintf(f, "%20.20s", "");
820 fprintf(f, "\t# (Cyl. %4lu",
821 (u_long)(pp->p_offset / lp->d_secpercyl));
822 if (pp->p_offset % lp->d_secpercyl)
827 (u_long)((pp->p_offset + pp->p_size +
828 lp->d_secpercyl - 1) /
829 lp->d_secpercyl - 1));
830 if (pp->p_size % lp->d_secpercyl)
840 struct disklabel *lp;
844 struct disklabel label;
847 if ((fd = mkstemp(tmpfil)) == -1 ||
848 (fp = fdopen(fd, "w")) == NULL) {
849 warnx("can't create %s", tmpfil);
857 fp = fopen(tmpfil, "r");
859 warnx("can't reopen %s for reading", tmpfil);
862 bzero((char *)&label, sizeof(label));
863 if (getasciilabel(fp, &label)) {
865 if (writelabel(f, bootarea, lp) == 0) {
867 (void) unlink(tmpfil);
872 printf("re-edit the label? [y]: "); fflush(stdout);
874 if (c != EOF && c != (int)'\n')
875 while (getchar() != (int)'\n')
880 (void) unlink(tmpfil);
887 register int pid, xpid;
890 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
891 while ((pid = fork()) < 0) {
892 if (errno == EPROCLIM) {
893 warnx("you have too many processes");
896 if (errno != EAGAIN) {
908 if ((ed = getenv("EDITOR")) == (char *)0)
910 execlp(ed, ed, tmpfil, (char *)0);
913 while ((xpid = wait(&stat)) >= 0)
925 while (*cp != '\0' && isspace(*cp))
927 if (*cp == '\0' || *cp == '#')
928 return ((char *)NULL);
938 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
940 if ((c = *cp) != '\0') {
945 return ((char *)NULL);
949 * Read an ascii label in from fd f,
950 * in the same format as that put out by display(),
956 register struct disklabel *lp;
958 register char **cpp, *cp;
959 register struct partition *pp;
961 char *tp, *s, line[BUFSIZ];
962 int v, lineno = 0, errors = 0;
964 lp->d_bbsize = BBSIZE; /* XXX */
965 lp->d_sbsize = SBSIZE; /* XXX */
966 while (fgets(line, sizeof(line) - 1, f)) {
968 if ((cp = index(line,'\n')) != 0)
975 fprintf(stderr, "line %d: syntax error\n", lineno);
979 *tp++ = '\0', tp = skip(tp);
980 if (streq(cp, "type")) {
984 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
985 if ((s = *cpp) && streq(s, tp)) {
986 lp->d_type = cpp - dktypenames;
990 if ((unsigned)v >= DKMAXTYPES)
991 fprintf(stderr, "line %d:%s %d\n", lineno,
992 "Warning, unknown disk type", v);
996 if (streq(cp, "flags")) {
997 for (v = 0; (cp = tp) && *cp != '\0';) {
999 if (streq(cp, "removeable"))
1001 else if (streq(cp, "ecc"))
1003 else if (streq(cp, "badsect"))
1007 "line %d: %s: bad flag\n",
1015 if (streq(cp, "drivedata")) {
1018 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
1019 lp->d_drivedata[i++] = atoi(cp);
1024 if (sscanf(cp, "%d partitions", &v) == 1) {
1025 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
1027 "line %d: bad # of partitions\n", lineno);
1028 lp->d_npartitions = MAXPARTITIONS;
1031 lp->d_npartitions = v;
1036 if (streq(cp, "disk")) {
1037 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1040 if (streq(cp, "label")) {
1041 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1044 if (streq(cp, "bytes/sector")) {
1046 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1048 "line %d: %s: bad sector size\n",
1055 if (streq(cp, "sectors/track")) {
1058 fprintf(stderr, "line %d: %s: bad %s\n",
1065 if (streq(cp, "sectors/cylinder")) {
1068 fprintf(stderr, "line %d: %s: bad %s\n",
1072 lp->d_secpercyl = v;
1075 if (streq(cp, "tracks/cylinder")) {
1078 fprintf(stderr, "line %d: %s: bad %s\n",
1085 if (streq(cp, "cylinders")) {
1088 fprintf(stderr, "line %d: %s: bad %s\n",
1092 lp->d_ncylinders = v;
1095 if (streq(cp, "sectors/unit")) {
1098 fprintf(stderr, "line %d: %s: bad %s\n",
1102 lp->d_secperunit = v;
1105 if (streq(cp, "rpm")) {
1108 fprintf(stderr, "line %d: %s: bad %s\n",
1115 if (streq(cp, "interleave")) {
1118 fprintf(stderr, "line %d: %s: bad %s\n",
1122 lp->d_interleave = v;
1125 if (streq(cp, "trackskew")) {
1128 fprintf(stderr, "line %d: %s: bad %s\n",
1132 lp->d_trackskew = v;
1135 if (streq(cp, "cylinderskew")) {
1138 fprintf(stderr, "line %d: %s: bad %s\n",
1145 if (streq(cp, "headswitch")) {
1148 fprintf(stderr, "line %d: %s: bad %s\n",
1152 lp->d_headswitch = v;
1155 if (streq(cp, "track-to-track seek")) {
1158 fprintf(stderr, "line %d: %s: bad %s\n",
1165 /* the ':' was removed above */
1166 if ('a' <= *cp && *cp <= MAX_PART && cp[1] == '\0') {
1168 if (part >= lp->d_npartitions) {
1170 "line %d: partition name out of range a-%c: %s\n",
1171 lineno, 'a' + lp->d_npartitions - 1, cp);
1175 pp = &lp->d_partitions[part];
1177 #define NXTNUM(n) { \
1179 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1183 cp = tp, tp = word(cp); \
1189 /* retain 1 character following number */
1190 #define NXTWORD(w,n) { \
1192 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1197 cp = tp, tp = word(cp); \
1200 (n) = strtol(cp,&tmp,10); \
1201 if (tmp) (w) = *tmp; \
1205 NXTWORD(part_size_type[part],v);
1206 if (v < 0 || (v == 0 && part_size_type[part] != '*')) {
1208 "line %d: %s: bad partition size\n",
1216 NXTWORD(part_offset_type[part],v);
1217 if (v < 0 || (v == 0 &&
1218 part_offset_type[part] != '*' &&
1219 part_offset_type[part] != '\0')) {
1221 "line %d: %s: bad partition offset\n",
1227 cp = tp, tp = word(cp);
1229 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1230 if ((s = *cpp) && streq(s, cp)) {
1231 pp->p_fstype = cpp -
1239 if ((unsigned)v >= FSMAXTYPES) {
1241 "line %d: Warning, unknown "
1242 "filesystem type %s\n",
1249 * Note: NXTNUM will break us out of the
1252 switch (pp->p_fstype) {
1255 * allow us to accept defaults for
1259 NXTNUM(pp->p_fsize);
1260 if (pp->p_fsize == 0)
1263 pp->p_frag = v / pp->p_fsize;
1265 /* else default to 0's */
1268 /* These happen to be the same */
1272 NXTNUM(pp->p_fsize);
1273 if (pp->p_fsize == 0)
1276 pp->p_frag = v / pp->p_fsize;
1280 * FIX! poor attempt at
1284 if (pp->p_size < 1*1024*1024*1024/lp->d_secsize) {
1285 /* FIX! These are too low, but are traditional */
1286 pp->p_fsize = DEFAULT_NEWFS_BLOCK;
1287 pp->p_frag = (unsigned char) DEFAULT_NEWFS_FRAG;
1288 pp->p_cpg = DEFAULT_NEWFS_CPG;
1290 pp->p_fsize = BIG_NEWFS_BLOCK;
1291 pp->p_frag = (unsigned char) BIG_NEWFS_FRAG;
1292 pp->p_cpg = BIG_NEWFS_CPG;
1301 * note: we may not have
1302 * gotten all the entries for
1303 * the fs though if we didn't,
1304 * errors will be set.
1310 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1315 errors += checklabel(lp);
1316 return (errors == 0);
1320 * Check disklabel for errors and fill in
1321 * derived fields according to supplied values.
1325 register struct disklabel *lp;
1327 register struct partition *pp;
1330 unsigned long total_size, total_percent, current_offset;
1331 int seen_default_offset;
1334 struct partition *pp2;
1336 if (lp->d_secsize == 0) {
1337 fprintf(stderr, "sector size 0\n");
1340 if (lp->d_nsectors == 0) {
1341 fprintf(stderr, "sectors/track 0\n");
1344 if (lp->d_ntracks == 0) {
1345 fprintf(stderr, "tracks/cylinder 0\n");
1348 if (lp->d_ncylinders == 0) {
1349 fprintf(stderr, "cylinders/unit 0\n");
1353 Warning("revolutions/minute 0");
1354 if (lp->d_secpercyl == 0)
1355 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1356 if (lp->d_secperunit == 0)
1357 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1358 if (lp->d_bbsize == 0) {
1359 fprintf(stderr, "boot block size 0\n");
1361 } else if (lp->d_bbsize % lp->d_secsize)
1362 Warning("boot block size %% sector-size != 0");
1363 if (lp->d_sbsize == 0) {
1364 fprintf(stderr, "super block size 0\n");
1366 } else if (lp->d_sbsize % lp->d_secsize)
1367 Warning("super block size %% sector-size != 0");
1368 if (lp->d_npartitions > MAXPARTITIONS)
1369 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1370 (u_long)lp->d_npartitions, MAXPARTITIONS);
1372 /* first allocate space to the partitions, then offsets */
1373 total_size = 0; /* in sectors */
1374 total_percent = 0; /* in percent */
1376 /* find all fixed partitions */
1377 for (i = 0; i < lp->d_npartitions; i++) {
1378 pp = &lp->d_partitions[i];
1380 if (part_size_type[i] == '*') {
1381 /* partition 2 ('c') is special */
1382 if (i == FULL_DISK_PART) {
1383 pp->p_size = lp->d_secperunit;
1386 Warning("Too many '*' partitions (%c and %c)",
1387 hog_part + 'a',i + 'a');
1395 switch (part_size_type[i]) {
1397 total_percent += size;
1405 size *= 1024ULL * 1024ULL;
1409 size *= 1024ULL * 1024ULL * 1024ULL;
1414 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1417 /* don't count %'s yet */
1418 if (part_size_type[i] != '%') {
1420 * for all not in sectors, convert to
1423 if (part_size_type[i] != '\0') {
1424 if (size % lp->d_secsize != 0)
1425 Warning("partition %c not an integer number of sectors",
1427 size /= lp->d_secsize;
1430 /* else already in sectors */
1431 /* partition 2 ('c') is special */
1432 if (i != FULL_DISK_PART)
1438 /* handle % partitions - note %'s don't need to add up to 100! */
1439 if (total_percent != 0) {
1440 long free_space = lp->d_secperunit - total_size;
1441 if (total_percent > 100) {
1442 fprintf(stderr,"total percentage %d is greater than 100\n",
1447 if (free_space > 0) {
1448 for (i = 0; i < lp->d_npartitions; i++) {
1449 pp = &lp->d_partitions[i];
1450 if (part_set[i] && part_size_type[i] == '%') {
1451 /* careful of overflows! and integer roundoff */
1452 pp->p_size = ((double)pp->p_size/100) * free_space;
1453 total_size += pp->p_size;
1455 /* FIX we can lose a sector or so due to roundoff per
1456 partition. A more complex algorithm could avoid that */
1461 "%ld sectors available to give to '*' and '%' partitions\n",
1464 /* fix? set all % partitions to size 0? */
1467 /* give anything remaining to the hog partition */
1468 if (hog_part != -1) {
1469 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1470 total_size = lp->d_secperunit;
1473 /* Now set the offsets for each partition */
1474 current_offset = 0; /* in sectors */
1475 seen_default_offset = 0;
1476 for (i = 0; i < lp->d_npartitions; i++) {
1478 pp = &lp->d_partitions[i];
1480 if (part_offset_type[i] == '*') {
1481 /* partition 2 ('c') is special */
1482 if (i == FULL_DISK_PART) {
1485 pp->p_offset = current_offset;
1486 seen_default_offset = 1;
1489 /* allow them to be out of order for old-style tables */
1490 /* partition 2 ('c') is special */
1491 if (pp->p_offset < current_offset &&
1492 seen_default_offset && i != FULL_DISK_PART) {
1494 "Offset %ld for partition %c overlaps previous partition which ends at %ld\n",
1495 pp->p_offset,i+'a',current_offset);
1497 "Labels with any *'s for offset must be in ascending order by sector\n");
1499 } else if (pp->p_offset != current_offset &&
1500 i != FULL_DISK_PART && seen_default_offset) {
1502 * this may give unneeded warnings if
1503 * partitions are out-of-order
1506 "Offset %ld for partition %c doesn't match expected value %ld",
1507 pp->p_offset, i + 'a', current_offset);
1510 /* partition 2 ('c') is special */
1511 if (i != FULL_DISK_PART)
1512 current_offset = pp->p_offset + pp->p_size;
1516 for (i = 0; i < lp->d_npartitions; i++) {
1518 pp = &lp->d_partitions[i];
1519 if (pp->p_size == 0 && pp->p_offset != 0)
1520 Warning("partition %c: size 0, but offset %lu",
1521 part, (u_long)pp->p_offset);
1523 if (pp->p_size % lp->d_secpercyl)
1524 Warning("partition %c: size %% cylinder-size != 0",
1526 if (pp->p_offset % lp->d_secpercyl)
1527 Warning("partition %c: offset %% cylinder-size != 0",
1530 if (pp->p_offset > lp->d_secperunit) {
1532 "partition %c: offset past end of unit\n", part);
1535 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1537 "partition %c: partition extends past end of unit\n",
1541 if (i == FULL_DISK_PART)
1543 if (pp->p_fstype != FS_UNUSED)
1544 Warning("partition %c is not marked as unused!",part);
1545 if (pp->p_offset != 0)
1546 Warning("partition %c doesn't start at 0!",part);
1547 if (pp->p_size != lp->d_secperunit)
1548 Warning("partition %c doesn't cover the whole unit!",part);
1550 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1551 (pp->p_size != lp->d_secperunit)) {
1552 Warning("An incorrect partition %c may cause problems for "
1553 "standard system utilities",part);
1557 /* check for overlaps */
1558 /* this will check for all possible overlaps once and only once */
1559 for (j = 0; j < i; j++) {
1560 /* partition 2 ('c') is special */
1561 if (j != FULL_DISK_PART && i != FULL_DISK_PART &&
1562 part_set[i] && part_set[j]) {
1563 pp2 = &lp->d_partitions[j];
1564 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1565 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1566 pp2->p_offset >= pp->p_offset)) {
1567 fprintf(stderr,"partitions %c and %c overlap!\n",
1574 for (; i < MAXPARTITIONS; i++) {
1576 pp = &lp->d_partitions[i];
1577 if (pp->p_size || pp->p_offset)
1578 Warning("unused partition %c: size %d offset %lu",
1579 'a' + i, pp->p_size, (u_long)pp->p_offset);
1585 * When operating on a "virgin" disk, try getting an initial label
1586 * from the associated device driver. This might work for all device
1587 * drivers that are able to fetch some initial device parameters
1588 * without even having access to a (BSD) disklabel, like SCSI disks,
1589 * most IDE drives, or vn devices.
1591 * The device name must be given in its "canonical" form.
1594 getvirginlabel(void)
1596 static struct disklabel lab;
1597 char namebuf[BBSIZE];
1600 if (dkname[0] == '/') {
1601 warnx("\"auto\" requires the usage of a canonical disk name");
1604 (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1605 if ((f = open(namebuf, O_RDONLY)) == -1) {
1606 warn("cannot open %s", namebuf);
1611 * Try to use the new get-virgin-label ioctl. If it fails,
1612 * fallback to the old get-disdk-info ioctl.
1614 if (ioctl(f, DIOCGDVIRGIN, &lab) == 0)
1616 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1619 (void)snprintf(namebuf, BBSIZE, "%s%s%c", _PATH_DEV, dkname,
1621 if ((f = open(namebuf, O_RDONLY)) == -1) {
1622 warn("cannot open %s", namebuf);
1625 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1628 warn("No virgin disklabel found %s", namebuf);
1638 * If we are installing a boot program that doesn't fit in d_bbsize
1639 * we need to mark those partitions that the boot overflows into.
1640 * This allows newfs to prevent creation of a filesystem where it might
1641 * clobber bootstrap code.
1645 register struct disklabel *lp;
1647 register struct partition *pp;
1654 boffset = bootsize / lp->d_secsize;
1655 for (i = 0; i < lp->d_npartitions; i++) {
1657 pp = &lp->d_partitions[i];
1658 if (pp->p_size == 0)
1660 if (boffset <= pp->p_offset) {
1661 if (pp->p_fstype == FS_BOOT)
1662 pp->p_fstype = FS_UNUSED;
1663 } else if (pp->p_fstype != FS_BOOT) {
1664 if (pp->p_fstype != FS_UNUSED) {
1666 "boot overlaps used partition %c\n",
1670 pp->p_fstype = FS_BOOT;
1671 Warning("boot overlaps partition %c, %s",
1672 part, "marked as FS_BOOT");
1677 errx(4, "cannot install boot program");
1682 Warning(char *fmt, ...)
1686 fprintf(stderr, "Warning, ");
1688 vfprintf(stderr, fmt, ap);
1689 fprintf(stderr, "\n");
1697 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",
1698 "usage: disklabel [-r] disk",
1699 "\t\t(to read label)",
1700 " disklabel -w [-r] [-n] disk type [ packid ]",
1701 "\t\t(to write label with existing boot program)",
1702 " disklabel -e [-r] [-n] disk",
1703 "\t\t(to edit label)",
1704 " disklabel -R [-r] [-n] disk protofile",
1705 "\t\t(to restore label with existing boot program)",
1707 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1708 "\t\t(to install boot program with existing label)",
1709 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1710 "\t\t(to write label and boot program)",
1711 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1712 "\t\t(to restore label and boot program)",
1714 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1715 "\t\t(to install boot program with existing on-disk label)",
1716 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1717 "\t\t(to write label and install boot program)",
1718 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1719 "\t\t(to restore label and install boot program)",
1721 " disklabel [-NW] disk",
1722 "\t\t(to write disable/enable label)");
1724 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1725 "usage: disklabel [-r] disk", "(to read label)",
1726 " disklabel -w [-r] [-n] disk type [ packid ]",
1727 "\t\t(to write label)",
1728 " disklabel -e [-r] [-n] disk",
1729 "\t\t(to edit label)",
1730 " disklabel -R [-r] [-n] disk protofile",
1731 "\t\t(to restore label)",
1732 " disklabel [-NW] disk",
1733 "\t\t(to write disable/enable label)");