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>
76 #include "pathnames.h"
79 * Disklabel: read and write disklabels.
80 * The label is usually placed on one of the first sectors of the disk.
81 * Many machines also place a bootstrap in the same area,
82 * in which case the label is embedded in the bootstrap.
83 * The bootstrap source must leave space at the proper offset
84 * for the label on such machines.
88 #define BBSIZE 8192 /* size of boot area, with label */
91 /* FIX! These are too low, but are traditional */
92 #define DEFAULT_NEWFS_BLOCK 8192U
93 #define DEFAULT_NEWFS_FRAG 1024U
94 #define DEFAULT_NEWFS_CPG 16U
96 #define BIG_NEWFS_BLOCK 16384U
97 #define BIG_NEWFS_FRAG 2048U
98 #define BIG_NEWFS_CPG 64U
100 #if defined(__i386__) || defined(__ia64__)
102 #elif defined(__alpha__) || defined(__sparc64__) || defined(__powerpc__)
105 #error I do not know about this architecture.
108 void makelabel(const char *, const char *, struct disklabel *);
109 int writelabel(int, const char *, struct disklabel *);
110 void l_perror(const char *);
111 struct disklabel *readlabel(int);
112 struct disklabel *makebootarea(char *, struct disklabel *, int);
113 void display(FILE *, const struct disklabel *);
114 int edit(struct disklabel *, int);
118 int getasciilabel(FILE *, struct disklabel *);
119 int getasciipartspec(char *, struct disklabel *, int, int);
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 #define MAX_PART ('z')
138 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
139 char part_size_type[MAX_NUM_PARTS];
140 char part_offset_type[MAX_NUM_PARTS];
141 int part_set[MAX_NUM_PARTS];
144 int installboot; /* non-zero if we should install a boot program */
145 char *bootbuf; /* pointer to buffer with remainder of boot prog */
146 int bootsize; /* size of remaining boot program */
147 char *xxboot; /* primary boot */
148 char *bootxx; /* secondary boot */
149 char boot0[MAXPATHLEN];
150 char boot1[MAXPATHLEN];
154 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
158 int disable_write; /* set to disable writing to disk label */
160 #define OPTIONS "BNRWb:enrs:w"
163 main(int argc, char *argv[])
165 struct disklabel *lp;
167 int ch, f = 0, flag, error = 0;
170 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
240 if (dkname[0] != '/') {
241 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
243 np += strlen(specname) + 1;
246 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
247 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
248 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
249 np = namebuf + strlen(specname) + 1;
250 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
253 err(4, "%s", specname);
269 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
270 err(4, "ioctl DIOCWLABEL");
278 error = checklabel(lp);
283 if (installboot && argc == 3) {
284 makelabel(argv[2], 0, &lab);
288 * We only called makelabel() for its side effect
289 * of setting the bootstrap file names. Discard
290 * all changes to `lab' so that all values in the
291 * final label come from the ASCII label.
293 bzero((char *)&lab, sizeof(lab));
298 if (!(t = fopen(argv[1], "r")))
299 err(4, "%s", argv[1]);
300 if (!getasciilabel(t, &lab))
302 lp = makebootarea(bootarea, &lab, f);
304 error = writelabel(f, bootarea, lp);
314 makelabel(argv[1], name, &lab);
315 lp = makebootarea(bootarea, &lab, f);
317 if (checklabel(lp) == 0)
318 error = writelabel(f, bootarea, lp);
323 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
324 err(4, "ioctl DIOCWLABEL");
330 struct disklabel tlab;
335 makelabel(argv[1], 0, &lab);
336 lp = makebootarea(bootarea, &lab, f);
338 if (checklabel(lp) == 0)
339 error = writelabel(f, bootarea, lp);
348 * Construct a prototype disklabel from /etc/disktab. As a side
349 * effect, set the names of the primary and secondary boot files
353 makelabel(const char *type, const char *name, struct disklabel *lp)
355 struct disklabel *dp;
357 if (strcmp(type, "auto") == 0)
358 dp = getvirginlabel();
360 dp = getdiskbyname(type);
362 errx(1, "%s: unknown disk type", type);
364 bzero(lp->d_packname, sizeof(lp->d_packname));
366 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
370 writelabel(int f, const char *boot, struct disklabel *lp)
378 struct sun_disklabel *sl;
379 u_short cksum, *sp1, *sp2;
380 struct partition *npp;
381 struct sun_dkpart *spp;
386 Warning("write to disk label supressed - label was as follows:");
391 lp->d_magic = DISKMAGIC;
392 lp->d_magic2 = DISKMAGIC;
394 lp->d_checksum = dkcksum(lp);
397 * First set the kernel disk label,
398 * then write a label to the raw disk.
399 * If the SDINFO ioctl fails because it is unimplemented,
400 * keep going; otherwise, the kernel consistency checks
401 * may prevent us from changing the current (in-core)
404 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
405 errno != ENODEV && errno != ENOTTY) {
406 l_perror("ioctl DIOCSDINFO");
409 (void)lseek(f, (off_t)0, SEEK_SET);
413 * Generate the bootblock checksum for the SRM console.
415 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
421 * Generate a Sun disklabel around the BSD label for
422 * PROM compatability.
424 sl = (struct sun_disklabel *)boot;
425 memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
426 sl->sl_rpm = lp->d_rpm;
427 sl->sl_pcylinders = lp->d_ncylinders +
428 lp->d_acylinders; /* XXX */
429 sl->sl_sparespercyl = lp->d_sparespercyl;
430 sl->sl_interleave = lp->d_interleave;
431 sl->sl_ncylinders = lp->d_ncylinders;
432 sl->sl_acylinders = lp->d_acylinders;
433 sl->sl_ntracks = lp->d_ntracks;
434 sl->sl_nsectors = lp->d_nsectors;
435 sl->sl_magic = SUN_DKMAGIC;
436 secpercyl = sl->sl_nsectors * sl->sl_ntracks;
437 for (i = 0; i < 8; i++) {
438 spp = &sl->sl_part[i];
439 npp = &lp->d_partitions[i];
441 * SunOS partitions must start on a cylinder
442 * boundary. Note this restriction is forced
443 * upon FreeBSD/sparc64 labels too, since we
444 * want to keep both labels synchronised.
446 spp->sdkp_cyloffset = npp->p_offset / secpercyl;
447 spp->sdkp_nsectors = npp->p_size;
450 /* Compute the XOR checksum. */
452 sp2 = (u_short *)(sl + 1);
453 sl->sl_cksum = cksum = 0;
456 sl->sl_cksum = cksum;
459 * write enable label sector before write (if necessary),
460 * disable after writing.
463 if (ioctl(f, DIOCWLABEL, &flag) < 0)
464 warn("ioctl DIOCWLABEL");
465 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
471 * Output the remainder of the disklabel
473 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
479 (void) ioctl(f, DIOCWLABEL, &flag);
480 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
481 l_perror("ioctl DIOCWDINFO");
489 l_perror(const char *s)
494 warnx("%s: no disk label on disk;", s);
495 fprintf(stderr, "add \"-r\" to install initial label\n");
499 warnx("%s: label magic number or checksum is wrong!", s);
500 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
504 warnx("%s: open partition would move or shrink", s);
508 warnx("%s: '%c' partition must start at beginning of disk",
519 * Fetch disklabel for disk.
520 * Use ioctl to get label unless -r flag is given.
525 struct disklabel *lp;
528 if (read(f, bootarea, BBSIZE) < BBSIZE)
529 err(4, "%s", specname);
530 for (lp = (struct disklabel *)bootarea;
531 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
532 lp = (struct disklabel *)((char *)lp + 16))
533 if (lp->d_magic == DISKMAGIC &&
534 lp->d_magic2 == DISKMAGIC)
536 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
537 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
540 "bad pack magic number (label is damaged, or pack is unlabeled)");
543 if (ioctl(f, DIOCGDINFO, lp) < 0)
544 err(4, "ioctl DIOCGDINFO");
550 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
551 * Returns a pointer to the disklabel portion of the bootarea.
554 makebootarea(char *boot, struct disklabel *dp, int f)
556 struct disklabel *lp;
573 if (dp->d_secsize == 0) {
574 dp->d_secsize = DEV_BSIZE;
575 dp->d_bbsize = BBSIZE;
577 lp = (struct disklabel *)
578 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
579 bzero((char *)lp, sizeof *lp);
582 * If we are not installing a boot program but we are installing a
583 * label on disk then we must read the current bootarea so we don't
584 * clobber the existing boot.
588 if (read(f, boot, BBSIZE) < BBSIZE)
589 err(4, "%s", specname);
590 bzero((char *)lp, sizeof *lp);
595 * We are installing a boot program. Determine the name(s) and
596 * read them into the appropriate places in the boot area.
598 if (!xxboot || !bootxx) {
600 if ((p = rindex(dkname, '/')) == NULL)
604 while (*p && !isdigit(*p))
609 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
614 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
622 * 1. One-piece bootstrap (hp300/hp800)
623 * 1. One-piece bootstrap (alpha/sparc64)
624 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
625 * is remembered and written later following the bootarea.
626 * 2. Two-piece bootstraps (i386/ia64)
627 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
628 * bytes of bootarea, remaining d_bbsize-d_secsize filled
631 b = open(xxboot, O_RDONLY);
633 err(4, "%s", xxboot);
638 * The i386 has the so-called fdisk table embedded into the
639 * primary bootstrap. We take care to not clobber it, but
640 * only if it does already contain some data. (Otherwise,
641 * the xxboot provides a template.)
643 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
644 err(4, "%s", xxboot);
645 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
646 #endif /* __i386__ */
647 if (read(b, boot, (int)dp->d_secsize) < 0)
648 err(4, "%s", xxboot);
651 for (i = DOSPARTOFF, found = 0;
652 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
654 found = tmpbuf[i] != 0;
656 memcpy((void *)&boot[DOSPARTOFF],
657 (void *)&tmpbuf[DOSPARTOFF],
658 NDOSPART * sizeof(struct dos_partition));
660 #endif /* __i386__ */
661 b = open(bootxx, O_RDONLY);
663 err(4, "%s", bootxx);
664 if (fstat(b, &sb) != 0)
665 err(4, "%s", bootxx);
666 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
667 errx(4, "%s too large", bootxx);
668 if (read(b, &boot[dp->d_secsize],
669 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
670 err(4, "%s", bootxx);
671 #else /* !(NUMBOOT > 1) */
674 * On the alpha, the primary bootstrap starts at the
675 * second sector of the boot area. The first sector
676 * contains the label and must be edited to contain the
677 * size and location of the primary bootstrap.
679 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
681 err(4, "%s", xxboot);
682 bootinfo = (u_long *)(boot + 480);
683 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
684 bootinfo[1] = 1; /* start at sector 1 */
685 bootinfo[2] = 0; /* flags (must be zero) */
686 #else /* !__alpha__ */
687 if (read(b, boot, (int)dp->d_bbsize) < 0)
688 err(4, "%s", xxboot);
689 #endif /* __alpha__ */
690 if (fstat(b, &sb) != 0)
691 err(4, "%s", xxboot);
692 bootsize = (int)sb.st_size - dp->d_bbsize;
694 /* XXX assume d_secsize is a power of two */
695 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
696 bootbuf = (char *)malloc((size_t)bootsize);
698 err(4, "%s", xxboot);
699 if (read(b, bootbuf, bootsize) < 0) {
701 err(4, "%s", xxboot);
704 #endif /* NUMBOOT > 1 */
706 #endif /* NUMBOOT > 0 */
708 * Make sure no part of the bootstrap is written in the area
709 * reserved for the label.
711 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
713 errx(2, "bootstrap doesn't leave room for disk label");
718 display(FILE *f, const struct disklabel *lp)
721 const struct partition *pp;
723 fprintf(f, "# %s:\n", specname);
724 if ((unsigned) lp->d_type < DKMAXTYPES)
725 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
727 fprintf(f, "type: %u\n", lp->d_type);
728 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
730 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
732 fprintf(f, "flags:");
733 if (lp->d_flags & D_REMOVABLE)
734 fprintf(f, " removeable");
735 if (lp->d_flags & D_ECC)
737 if (lp->d_flags & D_BADSECT)
738 fprintf(f, " badsect");
740 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
741 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
742 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
743 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
744 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
745 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
746 fprintf(f, "rpm: %u\n", lp->d_rpm);
747 fprintf(f, "interleave: %u\n", lp->d_interleave);
748 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
749 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
750 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
751 (u_long)lp->d_headswitch);
752 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
753 (u_long)lp->d_trkseek);
754 fprintf(f, "drivedata: ");
755 for (i = NDDATA - 1; i >= 0; i--)
756 if (lp->d_drivedata[i])
760 for (j = 0; j <= i; j++)
761 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
762 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
764 "# size offset fstype [fsize bsize bps/cpg]\n");
765 pp = lp->d_partitions;
766 for (i = 0; i < lp->d_npartitions; i++, pp++) {
768 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
769 (u_long)pp->p_size, (u_long)pp->p_offset);
770 if ((unsigned) pp->p_fstype < FSMAXTYPES)
771 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
773 fprintf(f, "%8d", pp->p_fstype);
774 switch (pp->p_fstype) {
776 case FS_UNUSED: /* XXX */
777 fprintf(f, " %5lu %5lu %5.5s ",
779 (u_long)(pp->p_fsize * pp->p_frag), "");
783 fprintf(f, " %5lu %5lu %5u ",
785 (u_long)(pp->p_fsize * pp->p_frag),
790 fprintf(f, " %5lu %5lu %5d",
792 (u_long)(pp->p_fsize * pp->p_frag),
797 fprintf(f, "%20.20s", "");
800 fprintf(f, "\t# (Cyl. %4lu",
801 (u_long)(pp->p_offset / lp->d_secpercyl));
802 if (pp->p_offset % lp->d_secpercyl)
807 (u_long)((pp->p_offset + pp->p_size +
808 lp->d_secpercyl - 1) /
809 lp->d_secpercyl - 1));
810 if (pp->p_size % lp->d_secpercyl)
819 edit(struct disklabel *lp, int f)
822 struct disklabel label;
825 if ((fd = mkstemp(tmpfil)) == -1 ||
826 (fp = fdopen(fd, "w")) == NULL) {
827 warnx("can't create %s", tmpfil);
835 fp = fopen(tmpfil, "r");
837 warnx("can't reopen %s for reading", tmpfil);
840 bzero((char *)&label, sizeof(label));
841 if (getasciilabel(fp, &label)) {
843 if (writelabel(f, bootarea, lp) == 0) {
845 (void) unlink(tmpfil);
850 printf("re-edit the label? [y]: "); fflush(stdout);
852 if (c != EOF && c != (int)'\n')
853 while (getchar() != (int)'\n')
858 (void) unlink(tmpfil);
869 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
870 while ((pid = fork()) < 0) {
871 if (errno == EPROCLIM) {
872 warnx("you have too many processes");
875 if (errno != EAGAIN) {
885 if ((ed = getenv("EDITOR")) == (char *)0)
887 execlp(ed, ed, tmpfil, (char *)0);
890 while ((xpid = wait(&stat)) >= 0)
901 while (*cp != '\0' && isspace(*cp))
903 if (*cp == '\0' || *cp == '#')
913 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
915 if ((c = *cp) != '\0') {
924 * Read an ascii label in from fd f,
925 * in the same format as that put out by display(),
929 getasciilabel(FILE *f, struct disklabel *lp)
934 char *tp, line[BUFSIZ];
935 int v, lineno = 0, errors = 0;
938 lp->d_bbsize = BBSIZE; /* XXX */
939 lp->d_sbsize = 0; /* XXX */
940 while (fgets(line, sizeof(line) - 1, f)) {
942 if ((cp = index(line,'\n')) != 0)
949 fprintf(stderr, "line %d: syntax error\n", lineno);
953 *tp++ = '\0', tp = skip(tp);
954 if (streq(cp, "type")) {
958 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
959 if (*cpp && streq(*cpp, tp)) {
960 lp->d_type = cpp - dktypenames;
963 if (cpp < &dktypenames[DKMAXTYPES])
966 if ((unsigned)v >= DKMAXTYPES)
967 fprintf(stderr, "line %d:%s %d\n", lineno,
968 "Warning, unknown disk type", v);
972 if (streq(cp, "flags")) {
973 for (v = 0; (cp = tp) && *cp != '\0';) {
975 if (streq(cp, "removeable"))
977 else if (streq(cp, "ecc"))
979 else if (streq(cp, "badsect"))
983 "line %d: %s: bad flag\n",
991 if (streq(cp, "drivedata")) {
992 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
993 lp->d_drivedata[i++] = atoi(cp);
998 if (sscanf(cp, "%d partitions", &v) == 1) {
999 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
1001 "line %d: bad # of partitions\n", lineno);
1002 lp->d_npartitions = MAXPARTITIONS;
1005 lp->d_npartitions = v;
1010 if (streq(cp, "disk")) {
1011 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1014 if (streq(cp, "label")) {
1015 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1018 if (streq(cp, "bytes/sector")) {
1020 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1022 "line %d: %s: bad sector size\n",
1029 if (streq(cp, "sectors/track")) {
1032 fprintf(stderr, "line %d: %s: bad %s\n",
1039 if (streq(cp, "sectors/cylinder")) {
1042 fprintf(stderr, "line %d: %s: bad %s\n",
1046 lp->d_secpercyl = v;
1049 if (streq(cp, "tracks/cylinder")) {
1052 fprintf(stderr, "line %d: %s: bad %s\n",
1059 if (streq(cp, "cylinders")) {
1062 fprintf(stderr, "line %d: %s: bad %s\n",
1066 lp->d_ncylinders = v;
1069 if (streq(cp, "sectors/unit")) {
1072 fprintf(stderr, "line %d: %s: bad %s\n",
1076 lp->d_secperunit = v;
1079 if (streq(cp, "rpm")) {
1082 fprintf(stderr, "line %d: %s: bad %s\n",
1089 if (streq(cp, "interleave")) {
1092 fprintf(stderr, "line %d: %s: bad %s\n",
1096 lp->d_interleave = v;
1099 if (streq(cp, "trackskew")) {
1102 fprintf(stderr, "line %d: %s: bad %s\n",
1106 lp->d_trackskew = v;
1109 if (streq(cp, "cylinderskew")) {
1112 fprintf(stderr, "line %d: %s: bad %s\n",
1119 if (streq(cp, "headswitch")) {
1122 fprintf(stderr, "line %d: %s: bad %s\n",
1126 lp->d_headswitch = v;
1129 if (streq(cp, "track-to-track seek")) {
1132 fprintf(stderr, "line %d: %s: bad %s\n",
1139 /* the ':' was removed above */
1140 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1142 "line %d: %s: Unknown disklabel field\n", lineno,
1148 /* Process a partition specification line. */
1150 if (part >= lp->d_npartitions) {
1152 "line %d: partition name out of range a-%c: %s\n",
1153 lineno, 'a' + lp->d_npartitions - 1, cp);
1159 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1164 errors += checklabel(lp);
1165 return (errors == 0);
1168 #define NXTNUM(n) do { \
1170 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1173 cp = tp, tp = word(cp); \
1178 /* retain 1 character following number */
1179 #define NXTWORD(w,n) do { \
1181 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1185 cp = tp, tp = word(cp); \
1186 (n) = strtol(cp,&tmp,10); \
1187 if (tmp) (w) = *tmp; \
1192 * Read a partition line into partition `part' in the specified disklabel.
1193 * Return 0 on success, 1 on failure.
1196 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1198 struct partition *pp;
1203 pp = &lp->d_partitions[part];
1207 NXTWORD(part_size_type[part],v);
1208 if (v < 0 || (v == 0 && part_size_type[part] != '*')) {
1209 fprintf(stderr, "line %d: %s: bad partition size\n", lineno,
1216 NXTWORD(part_offset_type[part],v);
1217 if (v < 0 || (v == 0 && part_offset_type[part] != '*' &&
1218 part_offset_type[part] != '\0')) {
1219 fprintf(stderr, "line %d: %s: bad partition offset\n", lineno,
1224 cp = tp, tp = word(cp);
1225 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1226 if (*cpp && streq(*cpp, cp))
1229 pp->p_fstype = cpp - fstypenames;
1235 if ((unsigned)v >= FSMAXTYPES) {
1237 "line %d: Warning, unknown filesystem type %s\n",
1244 switch (pp->p_fstype) {
1247 * allow us to accept defaults for
1251 NXTNUM(pp->p_fsize);
1252 if (pp->p_fsize == 0)
1255 pp->p_frag = v / pp->p_fsize;
1257 /* else default to 0's */
1260 /* These happen to be the same */
1264 NXTNUM(pp->p_fsize);
1265 if (pp->p_fsize == 0)
1268 pp->p_frag = v / pp->p_fsize;
1272 * FIX! poor attempt at adaptive
1275 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1277 * FIX! These are too low, but are traditional
1279 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1280 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1282 pp->p_cpg = DEFAULT_NEWFS_CPG;
1284 pp->p_fsize = BIG_NEWFS_FRAG;
1285 pp->p_frag = BIG_NEWFS_BLOCK /
1287 pp->p_cpg = BIG_NEWFS_CPG;
1297 * Check disklabel for errors and fill in
1298 * derived fields according to supplied values.
1301 checklabel(struct disklabel *lp)
1303 struct partition *pp;
1306 unsigned long total_size, total_percent, current_offset;
1307 int seen_default_offset;
1310 struct partition *pp2;
1312 if (lp->d_secsize == 0) {
1313 fprintf(stderr, "sector size 0\n");
1316 if (lp->d_nsectors == 0) {
1317 fprintf(stderr, "sectors/track 0\n");
1320 if (lp->d_ntracks == 0) {
1321 fprintf(stderr, "tracks/cylinder 0\n");
1324 if (lp->d_ncylinders == 0) {
1325 fprintf(stderr, "cylinders/unit 0\n");
1329 Warning("revolutions/minute 0");
1330 if (lp->d_secpercyl == 0)
1331 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1332 if (lp->d_secperunit == 0)
1333 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1334 if (lp->d_bbsize == 0) {
1335 fprintf(stderr, "boot block size 0\n");
1337 } else if (lp->d_bbsize % lp->d_secsize)
1338 Warning("boot block size %% sector-size != 0");
1339 if (lp->d_npartitions > MAXPARTITIONS)
1340 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1341 (u_long)lp->d_npartitions, MAXPARTITIONS);
1343 /* first allocate space to the partitions, then offsets */
1344 total_size = 0; /* in sectors */
1345 total_percent = 0; /* in percent */
1347 /* find all fixed partitions */
1348 for (i = 0; i < lp->d_npartitions; i++) {
1349 pp = &lp->d_partitions[i];
1351 if (part_size_type[i] == '*') {
1352 if (i == RAW_PART) {
1353 pp->p_size = lp->d_secperunit;
1356 Warning("Too many '*' partitions (%c and %c)",
1357 hog_part + 'a',i + 'a');
1365 switch (part_size_type[i]) {
1367 total_percent += size;
1375 size *= 1024ULL * 1024ULL;
1379 size *= 1024ULL * 1024ULL * 1024ULL;
1384 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1387 /* don't count %'s yet */
1388 if (part_size_type[i] != '%') {
1390 * for all not in sectors, convert to
1393 if (part_size_type[i] != '\0') {
1394 if (size % lp->d_secsize != 0)
1395 Warning("partition %c not an integer number of sectors",
1397 size /= lp->d_secsize;
1400 /* else already in sectors */
1407 /* handle % partitions - note %'s don't need to add up to 100! */
1408 if (total_percent != 0) {
1409 long free_space = lp->d_secperunit - total_size;
1410 if (total_percent > 100) {
1411 fprintf(stderr,"total percentage %lu is greater than 100\n",
1416 if (free_space > 0) {
1417 for (i = 0; i < lp->d_npartitions; i++) {
1418 pp = &lp->d_partitions[i];
1419 if (part_set[i] && part_size_type[i] == '%') {
1420 /* careful of overflows! and integer roundoff */
1421 pp->p_size = ((double)pp->p_size/100) * free_space;
1422 total_size += pp->p_size;
1424 /* FIX we can lose a sector or so due to roundoff per
1425 partition. A more complex algorithm could avoid that */
1430 "%ld sectors available to give to '*' and '%%' partitions\n",
1433 /* fix? set all % partitions to size 0? */
1436 /* give anything remaining to the hog partition */
1437 if (hog_part != -1) {
1438 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1439 total_size = lp->d_secperunit;
1442 /* Now set the offsets for each partition */
1443 current_offset = 0; /* in sectors */
1444 seen_default_offset = 0;
1445 for (i = 0; i < lp->d_npartitions; i++) {
1447 pp = &lp->d_partitions[i];
1449 if (part_offset_type[i] == '*') {
1450 if (i == RAW_PART) {
1453 pp->p_offset = current_offset;
1454 seen_default_offset = 1;
1457 /* allow them to be out of order for old-style tables */
1458 if (pp->p_offset < current_offset &&
1459 seen_default_offset && i != RAW_PART) {
1461 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1462 (long)pp->p_offset,i+'a',current_offset);
1464 "Labels with any *'s for offset must be in ascending order by sector\n");
1466 } else if (pp->p_offset != current_offset &&
1467 i != RAW_PART && seen_default_offset) {
1469 * this may give unneeded warnings if
1470 * partitions are out-of-order
1473 "Offset %ld for partition %c doesn't match expected value %ld",
1474 (long)pp->p_offset, i + 'a', current_offset);
1478 current_offset = pp->p_offset + pp->p_size;
1482 for (i = 0; i < lp->d_npartitions; i++) {
1484 pp = &lp->d_partitions[i];
1485 if (pp->p_size == 0 && pp->p_offset != 0)
1486 Warning("partition %c: size 0, but offset %lu",
1487 part, (u_long)pp->p_offset);
1489 /* See comment in writelabel(). */
1490 if (pp->p_offset % lp->d_secpercyl != 0) {
1491 fprintf(stderr, "partition %c: does not start on a "
1492 "cylinder boundary!\n", part);
1497 if (pp->p_size % lp->d_secpercyl)
1498 Warning("partition %c: size %% cylinder-size != 0",
1500 if (pp->p_offset % lp->d_secpercyl)
1501 Warning("partition %c: offset %% cylinder-size != 0",
1504 if (pp->p_offset > lp->d_secperunit) {
1506 "partition %c: offset past end of unit\n", part);
1509 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1511 "partition %c: partition extends past end of unit\n",
1517 if (pp->p_fstype != FS_UNUSED)
1518 Warning("partition %c is not marked as unused!",part);
1519 if (pp->p_offset != 0)
1520 Warning("partition %c doesn't start at 0!",part);
1521 if (pp->p_size != lp->d_secperunit)
1522 Warning("partition %c doesn't cover the whole unit!",part);
1524 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1525 (pp->p_size != lp->d_secperunit)) {
1526 Warning("An incorrect partition %c may cause problems for "
1527 "standard system utilities",part);
1531 /* check for overlaps */
1532 /* this will check for all possible overlaps once and only once */
1533 for (j = 0; j < i; j++) {
1534 if (j != RAW_PART && i != RAW_PART &&
1535 part_set[i] && part_set[j]) {
1536 pp2 = &lp->d_partitions[j];
1537 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1538 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1539 pp2->p_offset >= pp->p_offset)) {
1540 fprintf(stderr,"partitions %c and %c overlap!\n",
1547 for (; i < MAXPARTITIONS; i++) {
1549 pp = &lp->d_partitions[i];
1550 if (pp->p_size || pp->p_offset)
1551 Warning("unused partition %c: size %d offset %lu",
1552 'a' + i, pp->p_size, (u_long)pp->p_offset);
1558 * When operating on a "virgin" disk, try getting an initial label
1559 * from the associated device driver. This might work for all device
1560 * drivers that are able to fetch some initial device parameters
1561 * without even having access to a (BSD) disklabel, like SCSI disks,
1562 * most IDE drives, or vn devices.
1564 * The device name must be given in its "canonical" form.
1567 getvirginlabel(void)
1569 static struct disklabel lab;
1570 char namebuf[BBSIZE];
1573 if (dkname[0] == '/') {
1574 warnx("\"auto\" requires the usage of a canonical disk name");
1577 (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1578 if ((f = open(namebuf, O_RDONLY)) == -1) {
1579 warn("cannot open %s", namebuf);
1584 * Try to use the new get-virgin-label ioctl. If it fails,
1585 * fallback to the old get-disdk-info ioctl.
1587 if (ioctl(f, DIOCGDVIRGIN, &lab) == 0)
1589 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1592 (void)snprintf(namebuf, BBSIZE, "%s%s%c", _PATH_DEV, dkname,
1594 if ((f = open(namebuf, O_RDONLY)) == -1) {
1595 warn("cannot open %s", namebuf);
1598 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1601 warn("No virgin disklabel found %s", namebuf);
1609 * If we are installing a boot program that doesn't fit in d_bbsize
1610 * we need to mark those partitions that the boot overflows into.
1611 * This allows newfs to prevent creation of a filesystem where it might
1612 * clobber bootstrap code.
1615 setbootflag(struct disklabel *lp)
1617 struct partition *pp;
1624 boffset = bootsize / lp->d_secsize;
1625 for (i = 0; i < lp->d_npartitions; i++) {
1627 pp = &lp->d_partitions[i];
1628 if (pp->p_size == 0)
1630 if (boffset <= pp->p_offset) {
1631 if (pp->p_fstype == FS_BOOT)
1632 pp->p_fstype = FS_UNUSED;
1633 } else if (pp->p_fstype != FS_BOOT) {
1634 if (pp->p_fstype != FS_UNUSED) {
1636 "boot overlaps used partition %c\n",
1640 pp->p_fstype = FS_BOOT;
1641 Warning("boot overlaps partition %c, %s",
1642 part, "marked as FS_BOOT");
1647 errx(4, "cannot install boot program");
1652 Warning(const char *fmt, ...)
1656 fprintf(stderr, "Warning, ");
1658 vfprintf(stderr, fmt, ap);
1659 fprintf(stderr, "\n");
1667 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",
1668 "usage: disklabel [-r] disk",
1669 "\t\t(to read label)",
1670 " disklabel -w [-r] [-n] disk type [ packid ]",
1671 "\t\t(to write label with existing boot program)",
1672 " disklabel -e [-r] [-n] disk",
1673 "\t\t(to edit label)",
1674 " disklabel -R [-r] [-n] disk protofile",
1675 "\t\t(to restore label with existing boot program)",
1677 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1678 "\t\t(to install boot program with existing label)",
1679 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1680 "\t\t(to write label and boot program)",
1681 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1682 "\t\t(to restore label and boot program)",
1684 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1685 "\t\t(to install boot program with existing on-disk label)",
1686 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1687 "\t\t(to write label and install boot program)",
1688 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1689 "\t\t(to restore label and install boot program)",
1691 " disklabel [-NW] disk",
1692 "\t\t(to write disable/enable label)");
1694 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1695 "usage: disklabel [-r] disk", "(to read label)",
1696 " disklabel -w [-r] [-n] disk type [ packid ]",
1697 "\t\t(to write label)",
1698 " disklabel -e [-r] [-n] disk",
1699 "\t\t(to edit label)",
1700 " disklabel -R [-r] [-n] disk protofile",
1701 "\t\t(to restore label)",
1702 " disklabel [-NW] disk",
1703 "\t\t(to write disable/enable label)");