2 * Copyright (c) 1994, 1995 Gordon W. Ross
3 * Copyright (c) 1994 Theo de Raadt
5 * Copyright (c) 1987, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Symmetric Computer Systems.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * This product includes software developed by Theo de Raadt.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $
44 static const char copyright[] =
45 "@(#) Copyright (c) 1987, 1993\n\
46 The Regents of the University of California. All rights reserved.\n";
51 static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94";
52 /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
54 static const char rcsid[] =
58 #include <sys/param.h>
63 #include <sys/disklabel.h>
65 #include <sys/sun_disklabel.h>
67 #include <ufs/ffs/fs.h>
77 #include "pathnames.h"
80 * Disklabel: read and write disklabels.
81 * The label is usually placed on one of the first sectors of the disk.
82 * Many machines also place a bootstrap in the same area,
83 * in which case the label is embedded in the bootstrap.
84 * The bootstrap source must leave space at the proper offset
85 * for the label on such machines.
89 #define BBSIZE 8192 /* size of boot area, with label */
92 /* FIX! These are too low, but are traditional */
93 #define DEFAULT_NEWFS_BLOCK 8192U
94 #define DEFAULT_NEWFS_FRAG 1024U
95 #define DEFAULT_NEWFS_CPG 16U
97 #define BIG_NEWFS_BLOCK 16384U
98 #define BIG_NEWFS_FRAG 4096U
99 #define BIG_NEWFS_CPG 64U
101 #if defined(__i386__) || defined(__ia64__)
103 #elif defined(__alpha__) || defined(__sparc64__)
106 #error I do not know about this architecture.
109 void makelabel(const char *, const char *, struct disklabel *);
110 int writelabel(int, const char *, struct disklabel *);
111 void l_perror(const char *);
112 struct disklabel *readlabel(int);
113 struct disklabel *makebootarea(char *, struct disklabel *, int);
114 void display(FILE *, const struct disklabel *);
115 int edit(struct disklabel *, int);
119 int getasciilabel(FILE *, struct disklabel *);
120 int checklabel(struct disklabel *);
121 void setbootflag(struct disklabel *);
122 void Warning(const char *, ...) __printflike(1, 2);
124 struct disklabel *getvirginlabel(void);
126 #define DEFEDITOR _PATH_VI
127 #define streq(a,b) (strcmp(a,b) == 0)
131 char tmpfil[] = PATH_TMPFILE;
133 char namebuf[BBSIZE], *np = namebuf;
134 struct disklabel lab;
135 char bootarea[BBSIZE];
137 /* partition 'c' is the full disk and is special */
138 #define FULL_DISK_PART 2
139 #define MAX_PART ('z')
140 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
141 char part_size_type[MAX_NUM_PARTS];
142 char part_offset_type[MAX_NUM_PARTS];
143 int part_set[MAX_NUM_PARTS];
146 int installboot; /* non-zero if we should install a boot program */
147 char *bootbuf; /* pointer to buffer with remainder of boot prog */
148 int bootsize; /* size of remaining boot program */
149 char *xxboot; /* primary boot */
150 char *bootxx; /* secondary boot */
151 char boot0[MAXPATHLEN];
152 char boot1[MAXPATHLEN];
156 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
160 int disable_write; /* set to disable writing to disk label */
164 #define OPTIONS "BNRWb:denrs:w"
166 #define OPTIONS "BNRWb:enrs:w"
170 main(int argc, char *argv[])
172 struct disklabel *lp;
174 int ch, f = 0, flag, error = 0;
177 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
252 if (dkname[0] != '/') {
253 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
255 np += strlen(specname) + 1;
258 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
259 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
260 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
261 np = namebuf + strlen(specname) + 1;
262 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
265 err(4, "%s", specname);
281 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
282 err(4, "ioctl DIOCWLABEL");
290 error = checklabel(lp);
295 if (installboot && argc == 3) {
296 makelabel(argv[2], 0, &lab);
300 * We only called makelabel() for its side effect
301 * of setting the bootstrap file names. Discard
302 * all changes to `lab' so that all values in the
303 * final label come from the ASCII label.
305 bzero((char *)&lab, sizeof(lab));
310 if (!(t = fopen(argv[1], "r")))
311 err(4, "%s", argv[1]);
312 if (!getasciilabel(t, &lab))
314 lp = makebootarea(bootarea, &lab, f);
316 error = writelabel(f, bootarea, lp);
326 makelabel(argv[1], name, &lab);
327 lp = makebootarea(bootarea, &lab, f);
329 if (checklabel(lp) == 0)
330 error = writelabel(f, bootarea, lp);
335 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
336 err(4, "ioctl DIOCWLABEL");
342 struct disklabel tlab;
347 makelabel(argv[1], 0, &lab);
348 lp = makebootarea(bootarea, &lab, f);
350 if (checklabel(lp) == 0)
351 error = writelabel(f, bootarea, lp);
360 * Construct a prototype disklabel from /etc/disktab. As a side
361 * effect, set the names of the primary and secondary boot files
365 makelabel(const char *type, const char *name, struct disklabel *lp)
367 struct disklabel *dp;
369 if (strcmp(type, "auto") == 0)
370 dp = getvirginlabel();
372 dp = getdiskbyname(type);
374 errx(1, "%s: unknown disk type", type);
376 bzero(lp->d_packname, sizeof(lp->d_packname));
378 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
382 writelabel(int f, const char *boot, struct disklabel *lp)
390 struct sun_disklabel *sl;
391 u_short cksum, *sp1, *sp2;
392 struct partition *npp;
393 struct sun_dkpart *spp;
398 Warning("write to disk label supressed - label was as follows:");
403 lp->d_magic = DISKMAGIC;
404 lp->d_magic2 = DISKMAGIC;
406 lp->d_checksum = dkcksum(lp);
409 * First set the kernel disk label,
410 * then write a label to the raw disk.
411 * If the SDINFO ioctl fails because it is unimplemented,
412 * keep going; otherwise, the kernel consistency checks
413 * may prevent us from changing the current (in-core)
416 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
417 errno != ENODEV && errno != ENOTTY) {
418 l_perror("ioctl DIOCSDINFO");
421 (void)lseek(f, (off_t)0, SEEK_SET);
425 * Generate the bootblock checksum for the SRM console.
427 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
433 * Generate a Sun disklabel around the BSD label for
434 * PROM compatability.
436 sl = (struct sun_disklabel *)boot;
437 memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
438 sl->sl_rpm = lp->d_rpm;
439 sl->sl_pcylinders = lp->d_ncylinders +
440 lp->d_acylinders; /* XXX */
441 sl->sl_sparespercyl = lp->d_sparespercyl;
442 sl->sl_interleave = lp->d_interleave;
443 sl->sl_ncylinders = lp->d_ncylinders;
444 sl->sl_acylinders = lp->d_acylinders;
445 sl->sl_ntracks = lp->d_ntracks;
446 sl->sl_nsectors = lp->d_nsectors;
447 sl->sl_magic = SUN_DKMAGIC;
448 secpercyl = sl->sl_nsectors * sl->sl_ntracks;
449 for (i = 0; i < 8; i++) {
450 spp = &sl->sl_part[i];
451 npp = &lp->d_partitions[i];
453 * SunOS partitions must start on a cylinder
454 * boundary. Note this restriction is forced
455 * upon FreeBSD/sparc64 labels too, since we
456 * want to keep both labels synchronised.
458 spp->sdkp_cyloffset = npp->p_offset / secpercyl;
459 spp->sdkp_nsectors = npp->p_size;
462 /* Compute the XOR checksum. */
464 sp2 = (u_short *)(sl + 1);
465 sl->sl_cksum = cksum = 0;
468 sl->sl_cksum = cksum;
471 * write enable label sector before write (if necessary),
472 * disable after writing.
475 if (ioctl(f, DIOCWLABEL, &flag) < 0)
476 warn("ioctl DIOCWLABEL");
477 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
483 * Output the remainder of the disklabel
485 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
491 (void) ioctl(f, DIOCWLABEL, &flag);
492 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
493 l_perror("ioctl DIOCWDINFO");
501 l_perror(const char *s)
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.
537 struct disklabel *lp;
540 if (read(f, bootarea, BBSIZE) < BBSIZE)
541 err(4, "%s", specname);
542 for (lp = (struct disklabel *)bootarea;
543 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
544 lp = (struct disklabel *)((char *)lp + 16))
545 if (lp->d_magic == DISKMAGIC &&
546 lp->d_magic2 == DISKMAGIC)
548 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
549 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
552 "bad pack magic number (label is damaged, or pack is unlabeled)");
555 if (ioctl(f, DIOCGDINFO, lp) < 0)
556 err(4, "ioctl DIOCGDINFO");
562 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
563 * Returns a pointer to the disklabel portion of the bootarea.
566 makebootarea(char *boot, struct disklabel *dp, int f)
568 struct disklabel *lp;
585 if (dp->d_secsize == 0) {
586 dp->d_secsize = DEV_BSIZE;
587 dp->d_bbsize = BBSIZE;
589 lp = (struct disklabel *)
590 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
591 bzero((char *)lp, sizeof *lp);
594 * If we are not installing a boot program but we are installing a
595 * label on disk then we must read the current bootarea so we don't
596 * clobber the existing boot.
600 if (read(f, boot, BBSIZE) < BBSIZE)
601 err(4, "%s", specname);
602 bzero((char *)lp, sizeof *lp);
607 * We are installing a boot program. Determine the name(s) and
608 * read them into the appropriate places in the boot area.
610 if (!xxboot || !bootxx) {
612 if ((p = rindex(dkname, '/')) == NULL)
616 while (*p && !isdigit(*p))
621 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
626 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
633 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
634 xxboot, bootxx ? bootxx : "NONE");
639 * 1. One-piece bootstrap (hp300/hp800)
640 * 1. One-piece bootstrap (alpha/sparc64)
641 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
642 * is remembered and written later following the bootarea.
643 * 2. Two-piece bootstraps (i386/ia64)
644 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
645 * bytes of bootarea, remaining d_bbsize-d_secsize filled
648 b = open(xxboot, O_RDONLY);
650 err(4, "%s", xxboot);
655 * The i386 has the so-called fdisk table embedded into the
656 * primary bootstrap. We take care to not clobber it, but
657 * only if it does already contain some data. (Otherwise,
658 * the xxboot provides a template.)
660 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
661 err(4, "%s", xxboot);
662 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
664 if (read(b, boot, (int)dp->d_secsize) < 0)
665 err(4, "%s", xxboot);
668 for (i = DOSPARTOFF, found = 0;
669 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
671 found = tmpbuf[i] != 0;
673 memcpy((void *)&boot[DOSPARTOFF],
674 (void *)&tmpbuf[DOSPARTOFF],
675 NDOSPART * sizeof(struct dos_partition));
678 b = open(bootxx, O_RDONLY);
680 err(4, "%s", bootxx);
681 if (fstat(b, &sb) != 0)
682 err(4, "%s", bootxx);
683 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
684 errx(4, "%s too large", bootxx);
685 if (read(b, &boot[dp->d_secsize],
686 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
687 err(4, "%s", bootxx);
688 #else /* !(NUMBOOT > 1) */
691 * On the alpha, the primary bootstrap starts at the
692 * second sector of the boot area. The first sector
693 * contains the label and must be edited to contain the
694 * size and location of the primary bootstrap.
696 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
698 err(4, "%s", xxboot);
699 bootinfo = (u_long *)(boot + 480);
700 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
701 bootinfo[1] = 1; /* start at sector 1 */
702 bootinfo[2] = 0; /* flags (must be zero) */
703 #else /* !__alpha__ */
704 if (read(b, boot, (int)dp->d_bbsize) < 0)
705 err(4, "%s", xxboot);
706 #endif /* __alpha__ */
707 if (fstat(b, &sb) != 0)
708 err(4, "%s", xxboot);
709 bootsize = (int)sb.st_size - dp->d_bbsize;
711 /* XXX assume d_secsize is a power of two */
712 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
713 bootbuf = (char *)malloc((size_t)bootsize);
715 err(4, "%s", xxboot);
716 if (read(b, bootbuf, bootsize) < 0) {
718 err(4, "%s", xxboot);
721 #endif /* NUMBOOT > 1 */
723 #endif /* NUMBOOT > 0 */
725 * Make sure no part of the bootstrap is written in the area
726 * reserved for the label.
728 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
730 errx(2, "bootstrap doesn't leave room for disk label");
735 display(FILE *f, const struct disklabel *lp)
738 const struct partition *pp;
740 fprintf(f, "# %s:\n", specname);
741 if ((unsigned) lp->d_type < DKMAXTYPES)
742 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
744 fprintf(f, "type: %u\n", lp->d_type);
745 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
747 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
749 fprintf(f, "flags:");
750 if (lp->d_flags & D_REMOVABLE)
751 fprintf(f, " removeable");
752 if (lp->d_flags & D_ECC)
754 if (lp->d_flags & D_BADSECT)
755 fprintf(f, " badsect");
757 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
758 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
759 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
760 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
761 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
762 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
763 fprintf(f, "rpm: %u\n", lp->d_rpm);
764 fprintf(f, "interleave: %u\n", lp->d_interleave);
765 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
766 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
767 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
768 (u_long)lp->d_headswitch);
769 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
770 (u_long)lp->d_trkseek);
771 fprintf(f, "drivedata: ");
772 for (i = NDDATA - 1; i >= 0; i--)
773 if (lp->d_drivedata[i])
777 for (j = 0; j <= i; j++)
778 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
779 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
781 "# size offset fstype [fsize bsize bps/cpg]\n");
782 pp = lp->d_partitions;
783 for (i = 0; i < lp->d_npartitions; i++, pp++) {
785 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
786 (u_long)pp->p_size, (u_long)pp->p_offset);
787 if ((unsigned) pp->p_fstype < FSMAXTYPES)
788 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
790 fprintf(f, "%8d", pp->p_fstype);
791 switch (pp->p_fstype) {
793 case FS_UNUSED: /* XXX */
794 fprintf(f, " %5lu %5lu %5.5s ",
796 (u_long)(pp->p_fsize * pp->p_frag), "");
800 fprintf(f, " %5lu %5lu %5u ",
802 (u_long)(pp->p_fsize * pp->p_frag),
807 fprintf(f, " %5lu %5lu %5d",
809 (u_long)(pp->p_fsize * pp->p_frag),
814 fprintf(f, "%20.20s", "");
817 fprintf(f, "\t# (Cyl. %4lu",
818 (u_long)(pp->p_offset / lp->d_secpercyl));
819 if (pp->p_offset % lp->d_secpercyl)
824 (u_long)((pp->p_offset + pp->p_size +
825 lp->d_secpercyl - 1) /
826 lp->d_secpercyl - 1));
827 if (pp->p_size % lp->d_secpercyl)
836 edit(struct disklabel *lp, int f)
839 struct disklabel label;
842 if ((fd = mkstemp(tmpfil)) == -1 ||
843 (fp = fdopen(fd, "w")) == NULL) {
844 warnx("can't create %s", tmpfil);
852 fp = fopen(tmpfil, "r");
854 warnx("can't reopen %s for reading", tmpfil);
857 bzero((char *)&label, sizeof(label));
858 if (getasciilabel(fp, &label)) {
860 if (writelabel(f, bootarea, lp) == 0) {
862 (void) unlink(tmpfil);
867 printf("re-edit the label? [y]: "); fflush(stdout);
869 if (c != EOF && c != (int)'\n')
870 while (getchar() != (int)'\n')
875 (void) unlink(tmpfil);
886 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
887 while ((pid = fork()) < 0) {
888 if (errno == EPROCLIM) {
889 warnx("you have too many processes");
892 if (errno != EAGAIN) {
902 if ((ed = getenv("EDITOR")) == (char *)0)
904 execlp(ed, ed, tmpfil, (char *)0);
907 while ((xpid = wait(&stat)) >= 0)
918 while (*cp != '\0' && isspace(*cp))
920 if (*cp == '\0' || *cp == '#')
930 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
932 if ((c = *cp) != '\0') {
941 * Read an ascii label in from fd f,
942 * in the same format as that put out by display(),
946 getasciilabel(FILE *f, struct disklabel *lp)
949 struct partition *pp;
951 char *tp, *s, line[BUFSIZ];
952 int v, lineno = 0, errors = 0;
955 lp->d_bbsize = BBSIZE; /* XXX */
956 lp->d_sbsize = SBSIZE; /* XXX */
957 while (fgets(line, sizeof(line) - 1, f)) {
959 if ((cp = index(line,'\n')) != 0)
966 fprintf(stderr, "line %d: syntax error\n", lineno);
970 *tp++ = '\0', tp = skip(tp);
971 if (streq(cp, "type")) {
975 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
976 if ((s = *cpp) && streq(s, tp)) {
977 lp->d_type = cpp - dktypenames;
981 if ((unsigned)v >= DKMAXTYPES)
982 fprintf(stderr, "line %d:%s %d\n", lineno,
983 "Warning, unknown disk type", v);
987 if (streq(cp, "flags")) {
988 for (v = 0; (cp = tp) && *cp != '\0';) {
990 if (streq(cp, "removeable"))
992 else if (streq(cp, "ecc"))
994 else if (streq(cp, "badsect"))
998 "line %d: %s: bad flag\n",
1006 if (streq(cp, "drivedata")) {
1007 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
1008 lp->d_drivedata[i++] = atoi(cp);
1013 if (sscanf(cp, "%d partitions", &v) == 1) {
1014 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
1016 "line %d: bad # of partitions\n", lineno);
1017 lp->d_npartitions = MAXPARTITIONS;
1020 lp->d_npartitions = v;
1025 if (streq(cp, "disk")) {
1026 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1029 if (streq(cp, "label")) {
1030 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1033 if (streq(cp, "bytes/sector")) {
1035 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1037 "line %d: %s: bad sector size\n",
1044 if (streq(cp, "sectors/track")) {
1047 fprintf(stderr, "line %d: %s: bad %s\n",
1054 if (streq(cp, "sectors/cylinder")) {
1057 fprintf(stderr, "line %d: %s: bad %s\n",
1061 lp->d_secpercyl = v;
1064 if (streq(cp, "tracks/cylinder")) {
1067 fprintf(stderr, "line %d: %s: bad %s\n",
1074 if (streq(cp, "cylinders")) {
1077 fprintf(stderr, "line %d: %s: bad %s\n",
1081 lp->d_ncylinders = v;
1084 if (streq(cp, "sectors/unit")) {
1087 fprintf(stderr, "line %d: %s: bad %s\n",
1091 lp->d_secperunit = v;
1094 if (streq(cp, "rpm")) {
1097 fprintf(stderr, "line %d: %s: bad %s\n",
1104 if (streq(cp, "interleave")) {
1107 fprintf(stderr, "line %d: %s: bad %s\n",
1111 lp->d_interleave = v;
1114 if (streq(cp, "trackskew")) {
1117 fprintf(stderr, "line %d: %s: bad %s\n",
1121 lp->d_trackskew = v;
1124 if (streq(cp, "cylinderskew")) {
1127 fprintf(stderr, "line %d: %s: bad %s\n",
1134 if (streq(cp, "headswitch")) {
1137 fprintf(stderr, "line %d: %s: bad %s\n",
1141 lp->d_headswitch = v;
1144 if (streq(cp, "track-to-track seek")) {
1147 fprintf(stderr, "line %d: %s: bad %s\n",
1154 /* the ':' was removed above */
1155 if ('a' <= *cp && *cp <= MAX_PART && cp[1] == '\0') {
1157 if (part >= lp->d_npartitions) {
1159 "line %d: partition name out of range a-%c: %s\n",
1160 lineno, 'a' + lp->d_npartitions - 1, cp);
1164 pp = &lp->d_partitions[part];
1166 #define NXTNUM(n) { \
1168 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1172 cp = tp, tp = word(cp); \
1178 /* retain 1 character following number */
1179 #define NXTWORD(w,n) { \
1181 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1186 cp = tp, tp = word(cp); \
1189 (n) = strtol(cp,&tmp,10); \
1190 if (tmp) (w) = *tmp; \
1194 NXTWORD(part_size_type[part],v);
1195 if (v < 0 || (v == 0 && part_size_type[part] != '*')) {
1197 "line %d: %s: bad partition size\n",
1205 NXTWORD(part_offset_type[part],v);
1206 if (v < 0 || (v == 0 &&
1207 part_offset_type[part] != '*' &&
1208 part_offset_type[part] != '\0')) {
1210 "line %d: %s: bad partition offset\n",
1216 cp = tp, tp = word(cp);
1218 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1219 if ((s = *cpp) && streq(s, cp)) {
1220 pp->p_fstype = cpp -
1228 if ((unsigned)v >= FSMAXTYPES) {
1230 "line %d: Warning, unknown "
1231 "filesystem type %s\n",
1238 * Note: NXTNUM will break us out of the
1241 switch (pp->p_fstype) {
1244 * allow us to accept defaults for
1248 NXTNUM(pp->p_fsize);
1249 if (pp->p_fsize == 0)
1252 pp->p_frag = v / pp->p_fsize;
1254 /* else default to 0's */
1257 /* These happen to be the same */
1261 NXTNUM(pp->p_fsize);
1262 if (pp->p_fsize == 0)
1265 pp->p_frag = v / pp->p_fsize;
1269 * FIX! poor attempt at
1273 if (pp->p_size < 1*1024*1024*1024/lp->d_secsize) {
1274 /* FIX! These are too low, but are traditional */
1275 pp->p_fsize = DEFAULT_NEWFS_BLOCK;
1276 pp->p_frag = (unsigned char) DEFAULT_NEWFS_FRAG;
1277 pp->p_cpg = DEFAULT_NEWFS_CPG;
1279 pp->p_fsize = BIG_NEWFS_BLOCK;
1280 pp->p_frag = (unsigned char) BIG_NEWFS_FRAG;
1281 pp->p_cpg = BIG_NEWFS_CPG;
1290 * note: we may not have
1291 * gotten all the entries for
1292 * the fs though if we didn't,
1293 * errors will be set.
1299 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1304 errors += checklabel(lp);
1305 return (errors == 0);
1309 * Check disklabel for errors and fill in
1310 * derived fields according to supplied values.
1313 checklabel(struct disklabel *lp)
1315 struct partition *pp;
1318 unsigned long total_size, total_percent, current_offset;
1319 int seen_default_offset;
1322 struct partition *pp2;
1324 if (lp->d_secsize == 0) {
1325 fprintf(stderr, "sector size 0\n");
1328 if (lp->d_nsectors == 0) {
1329 fprintf(stderr, "sectors/track 0\n");
1332 if (lp->d_ntracks == 0) {
1333 fprintf(stderr, "tracks/cylinder 0\n");
1336 if (lp->d_ncylinders == 0) {
1337 fprintf(stderr, "cylinders/unit 0\n");
1341 Warning("revolutions/minute 0");
1342 if (lp->d_secpercyl == 0)
1343 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1344 if (lp->d_secperunit == 0)
1345 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1346 if (lp->d_bbsize == 0) {
1347 fprintf(stderr, "boot block size 0\n");
1349 } else if (lp->d_bbsize % lp->d_secsize)
1350 Warning("boot block size %% sector-size != 0");
1351 if (lp->d_sbsize == 0) {
1352 fprintf(stderr, "super block size 0\n");
1354 } else if (lp->d_sbsize % lp->d_secsize)
1355 Warning("super block size %% sector-size != 0");
1356 if (lp->d_npartitions > MAXPARTITIONS)
1357 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1358 (u_long)lp->d_npartitions, MAXPARTITIONS);
1360 /* first allocate space to the partitions, then offsets */
1361 total_size = 0; /* in sectors */
1362 total_percent = 0; /* in percent */
1364 /* find all fixed partitions */
1365 for (i = 0; i < lp->d_npartitions; i++) {
1366 pp = &lp->d_partitions[i];
1368 if (part_size_type[i] == '*') {
1369 /* partition 2 ('c') is special */
1370 if (i == FULL_DISK_PART) {
1371 pp->p_size = lp->d_secperunit;
1374 Warning("Too many '*' partitions (%c and %c)",
1375 hog_part + 'a',i + 'a');
1383 switch (part_size_type[i]) {
1385 total_percent += size;
1393 size *= 1024ULL * 1024ULL;
1397 size *= 1024ULL * 1024ULL * 1024ULL;
1402 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1405 /* don't count %'s yet */
1406 if (part_size_type[i] != '%') {
1408 * for all not in sectors, convert to
1411 if (part_size_type[i] != '\0') {
1412 if (size % lp->d_secsize != 0)
1413 Warning("partition %c not an integer number of sectors",
1415 size /= lp->d_secsize;
1418 /* else already in sectors */
1419 /* partition 2 ('c') is special */
1420 if (i != FULL_DISK_PART)
1426 /* handle % partitions - note %'s don't need to add up to 100! */
1427 if (total_percent != 0) {
1428 long free_space = lp->d_secperunit - total_size;
1429 if (total_percent > 100) {
1430 fprintf(stderr,"total percentage %d is greater than 100\n",
1435 if (free_space > 0) {
1436 for (i = 0; i < lp->d_npartitions; i++) {
1437 pp = &lp->d_partitions[i];
1438 if (part_set[i] && part_size_type[i] == '%') {
1439 /* careful of overflows! and integer roundoff */
1440 pp->p_size = ((double)pp->p_size/100) * free_space;
1441 total_size += pp->p_size;
1443 /* FIX we can lose a sector or so due to roundoff per
1444 partition. A more complex algorithm could avoid that */
1449 "%ld sectors available to give to '*' and '%' partitions\n",
1452 /* fix? set all % partitions to size 0? */
1455 /* give anything remaining to the hog partition */
1456 if (hog_part != -1) {
1457 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1458 total_size = lp->d_secperunit;
1461 /* Now set the offsets for each partition */
1462 current_offset = 0; /* in sectors */
1463 seen_default_offset = 0;
1464 for (i = 0; i < lp->d_npartitions; i++) {
1466 pp = &lp->d_partitions[i];
1468 if (part_offset_type[i] == '*') {
1469 /* partition 2 ('c') is special */
1470 if (i == FULL_DISK_PART) {
1473 pp->p_offset = current_offset;
1474 seen_default_offset = 1;
1477 /* allow them to be out of order for old-style tables */
1478 /* partition 2 ('c') is special */
1479 if (pp->p_offset < current_offset &&
1480 seen_default_offset && i != FULL_DISK_PART) {
1482 "Offset %ld for partition %c overlaps previous partition which ends at %ld\n",
1483 pp->p_offset,i+'a',current_offset);
1485 "Labels with any *'s for offset must be in ascending order by sector\n");
1487 } else if (pp->p_offset != current_offset &&
1488 i != FULL_DISK_PART && seen_default_offset) {
1490 * this may give unneeded warnings if
1491 * partitions are out-of-order
1494 "Offset %ld for partition %c doesn't match expected value %ld",
1495 pp->p_offset, i + 'a', current_offset);
1498 /* partition 2 ('c') is special */
1499 if (i != FULL_DISK_PART)
1500 current_offset = pp->p_offset + pp->p_size;
1504 for (i = 0; i < lp->d_npartitions; i++) {
1506 pp = &lp->d_partitions[i];
1507 if (pp->p_size == 0 && pp->p_offset != 0)
1508 Warning("partition %c: size 0, but offset %lu",
1509 part, (u_long)pp->p_offset);
1511 /* See comment in writelabel(). */
1512 if (pp->p_offset % lp->d_secpercyl != 0) {
1513 fprintf(stderr, "partition %c: does not start on a "
1514 "cylinder boundary!\n", part);
1519 if (pp->p_size % lp->d_secpercyl)
1520 Warning("partition %c: size %% cylinder-size != 0",
1522 if (pp->p_offset % lp->d_secpercyl)
1523 Warning("partition %c: offset %% cylinder-size != 0",
1526 if (pp->p_offset > lp->d_secperunit) {
1528 "partition %c: offset past end of unit\n", part);
1531 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1533 "partition %c: partition extends past end of unit\n",
1537 if (i == FULL_DISK_PART)
1539 if (pp->p_fstype != FS_UNUSED)
1540 Warning("partition %c is not marked as unused!",part);
1541 if (pp->p_offset != 0)
1542 Warning("partition %c doesn't start at 0!",part);
1543 if (pp->p_size != lp->d_secperunit)
1544 Warning("partition %c doesn't cover the whole unit!",part);
1546 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1547 (pp->p_size != lp->d_secperunit)) {
1548 Warning("An incorrect partition %c may cause problems for "
1549 "standard system utilities",part);
1553 /* check for overlaps */
1554 /* this will check for all possible overlaps once and only once */
1555 for (j = 0; j < i; j++) {
1556 /* partition 2 ('c') is special */
1557 if (j != FULL_DISK_PART && i != FULL_DISK_PART &&
1558 part_set[i] && part_set[j]) {
1559 pp2 = &lp->d_partitions[j];
1560 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1561 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1562 pp2->p_offset >= pp->p_offset)) {
1563 fprintf(stderr,"partitions %c and %c overlap!\n",
1570 for (; i < MAXPARTITIONS; i++) {
1572 pp = &lp->d_partitions[i];
1573 if (pp->p_size || pp->p_offset)
1574 Warning("unused partition %c: size %d offset %lu",
1575 'a' + i, pp->p_size, (u_long)pp->p_offset);
1581 * When operating on a "virgin" disk, try getting an initial label
1582 * from the associated device driver. This might work for all device
1583 * drivers that are able to fetch some initial device parameters
1584 * without even having access to a (BSD) disklabel, like SCSI disks,
1585 * most IDE drives, or vn devices.
1587 * The device name must be given in its "canonical" form.
1590 getvirginlabel(void)
1592 static struct disklabel lab;
1593 char namebuf[BBSIZE];
1596 if (dkname[0] == '/') {
1597 warnx("\"auto\" requires the usage of a canonical disk name");
1600 (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1601 if ((f = open(namebuf, O_RDONLY)) == -1) {
1602 warn("cannot open %s", namebuf);
1607 * Try to use the new get-virgin-label ioctl. If it fails,
1608 * fallback to the old get-disdk-info ioctl.
1610 if (ioctl(f, DIOCGDVIRGIN, &lab) == 0)
1612 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1615 (void)snprintf(namebuf, BBSIZE, "%s%s%c", _PATH_DEV, dkname,
1617 if ((f = open(namebuf, O_RDONLY)) == -1) {
1618 warn("cannot open %s", namebuf);
1621 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1624 warn("No virgin disklabel found %s", namebuf);
1632 * If we are installing a boot program that doesn't fit in d_bbsize
1633 * we need to mark those partitions that the boot overflows into.
1634 * This allows newfs to prevent creation of a filesystem where it might
1635 * clobber bootstrap code.
1638 setbootflag(struct disklabel *lp)
1640 struct partition *pp;
1647 boffset = bootsize / lp->d_secsize;
1648 for (i = 0; i < lp->d_npartitions; i++) {
1650 pp = &lp->d_partitions[i];
1651 if (pp->p_size == 0)
1653 if (boffset <= pp->p_offset) {
1654 if (pp->p_fstype == FS_BOOT)
1655 pp->p_fstype = FS_UNUSED;
1656 } else if (pp->p_fstype != FS_BOOT) {
1657 if (pp->p_fstype != FS_UNUSED) {
1659 "boot overlaps used partition %c\n",
1663 pp->p_fstype = FS_BOOT;
1664 Warning("boot overlaps partition %c, %s",
1665 part, "marked as FS_BOOT");
1670 errx(4, "cannot install boot program");
1675 Warning(const char *fmt, ...)
1679 fprintf(stderr, "Warning, ");
1681 vfprintf(stderr, fmt, ap);
1682 fprintf(stderr, "\n");
1690 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",
1691 "usage: disklabel [-r] disk",
1692 "\t\t(to read label)",
1693 " disklabel -w [-r] [-n] disk type [ packid ]",
1694 "\t\t(to write label with existing boot program)",
1695 " disklabel -e [-r] [-n] disk",
1696 "\t\t(to edit label)",
1697 " disklabel -R [-r] [-n] disk protofile",
1698 "\t\t(to restore label with existing boot program)",
1700 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1701 "\t\t(to install boot program with existing label)",
1702 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1703 "\t\t(to write label and boot program)",
1704 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1705 "\t\t(to restore label and boot program)",
1707 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1708 "\t\t(to install boot program with existing on-disk label)",
1709 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1710 "\t\t(to write label and install boot program)",
1711 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1712 "\t\t(to restore label and install boot program)",
1714 " disklabel [-NW] disk",
1715 "\t\t(to write disable/enable label)");
1717 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1718 "usage: disklabel [-r] disk", "(to read label)",
1719 " disklabel -w [-r] [-n] disk type [ packid ]",
1720 "\t\t(to write label)",
1721 " disklabel -e [-r] [-n] disk",
1722 "\t\t(to edit label)",
1723 " disklabel -R [-r] [-n] disk protofile",
1724 "\t\t(to restore label)",
1725 " disklabel [-NW] disk",
1726 "\t\t(to write disable/enable label)");