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"; */
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
59 #include <sys/param.h>
65 #include <sys/disklabel.h>
67 #include <sys/sun_disklabel.h>
80 #include "pathnames.h"
83 * Disklabel: read and write disklabels.
84 * The label is usually placed on one of the first sectors of the disk.
85 * Many machines also place a bootstrap in the same area,
86 * in which case the label is embedded in the bootstrap.
87 * The bootstrap source must leave space at the proper offset
88 * for the label on such machines.
92 #define BBSIZE 8192 /* size of boot area, with label */
95 /* FIX! These are too low, but are traditional */
96 #define DEFAULT_NEWFS_BLOCK 8192U
97 #define DEFAULT_NEWFS_FRAG 1024U
98 #define DEFAULT_NEWFS_CPG 16U
100 #define BIG_NEWFS_BLOCK 16384U
101 #define BIG_NEWFS_FRAG 2048U
102 #define BIG_NEWFS_CPG 64U
104 #if defined(__i386__) || defined(__ia64__)
106 #elif defined(__alpha__) || defined(__sparc64__) || defined(__powerpc__)
109 #error I do not know about this architecture.
112 void makelabel(const char *, const char *, struct disklabel *);
113 int writelabel(int, const char *, struct disklabel *);
114 void l_perror(const char *);
115 struct disklabel *readlabel(int);
116 struct disklabel *makebootarea(char *, struct disklabel *, int);
117 void display(FILE *, const struct disklabel *);
118 int edit(struct disklabel *, int);
122 int getasciilabel(FILE *, struct disklabel *);
123 int getasciipartspec(char *, struct disklabel *, int, int);
124 int checklabel(struct disklabel *);
125 void setbootflag(struct disklabel *);
126 void Warning(const char *, ...) __printflike(1, 2);
128 struct disklabel *getvirginlabel(void);
130 #define DEFEDITOR _PATH_VI
131 #define streq(a,b) (strcmp(a,b) == 0)
135 char tmpfil[] = PATH_TMPFILE;
137 char namebuf[BBSIZE], *np = namebuf;
138 struct disklabel lab;
139 char bootarea[BBSIZE];
141 char unknown[] = "unknown";
143 #define MAX_PART ('z')
144 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
145 char part_size_type[MAX_NUM_PARTS];
146 char part_offset_type[MAX_NUM_PARTS];
147 int part_set[MAX_NUM_PARTS];
150 int installboot; /* non-zero if we should install a boot program */
151 char *bootbuf; /* pointer to buffer with remainder of boot prog */
152 int bootsize; /* size of remaining boot program */
153 char *xxboot; /* primary boot */
154 char *bootxx; /* secondary boot */
155 char boot0[MAXPATHLEN];
156 char boot1[MAXPATHLEN];
160 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
164 int disable_write; /* set to disable writing to disk label */
166 #define OPTIONS "BNRWb:enrs:w"
169 main(int argc, char *argv[])
171 struct disklabel *lp;
173 int ch, f = 0, flag, error = 0;
176 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
246 if (dkname[0] != '/') {
247 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
249 np += strlen(specname) + 1;
252 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
253 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
254 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
255 np = namebuf + strlen(specname) + 1;
256 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
259 err(4, "%s", specname);
275 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
276 err(4, "ioctl DIOCWLABEL");
284 error = checklabel(lp);
289 if (installboot && argc == 3) {
290 makelabel(argv[2], 0, &lab);
294 * We only called makelabel() for its side effect
295 * of setting the bootstrap file names. Discard
296 * all changes to `lab' so that all values in the
297 * final label come from the ASCII label.
299 bzero((char *)&lab, sizeof(lab));
304 if (!(t = fopen(argv[1], "r")))
305 err(4, "%s", argv[1]);
306 if (!getasciilabel(t, &lab))
308 lp = makebootarea(bootarea, &lab, f);
310 error = writelabel(f, bootarea, lp);
320 makelabel(argv[1], name, &lab);
321 lp = makebootarea(bootarea, &lab, f);
323 if (checklabel(lp) == 0)
324 error = writelabel(f, bootarea, lp);
329 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
330 err(4, "ioctl DIOCWLABEL");
336 struct disklabel tlab;
341 makelabel(argv[1], 0, &lab);
342 lp = makebootarea(bootarea, &lab, f);
344 if (checklabel(lp) == 0)
345 error = writelabel(f, bootarea, lp);
354 * Construct a prototype disklabel from /etc/disktab. As a side
355 * effect, set the names of the primary and secondary boot files
359 makelabel(const char *type, const char *name, struct disklabel *lp)
361 struct disklabel *dp;
363 if (strcmp(type, "auto") == 0)
364 dp = getvirginlabel();
366 dp = getdiskbyname(type);
368 errx(1, "%s: unknown disk type", type);
370 bzero(lp->d_packname, sizeof(lp->d_packname));
372 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
376 writelabel(int f, const char *boot, struct disklabel *lp)
384 struct sun_disklabel *sl;
385 u_short cksum, *sp1, *sp2;
386 struct partition *npp;
387 struct sun_dkpart *spp;
392 Warning("write to disk label supressed - label was as follows:");
397 lp->d_magic = DISKMAGIC;
398 lp->d_magic2 = DISKMAGIC;
400 lp->d_checksum = dkcksum(lp);
403 * First set the kernel disk label,
404 * then write a label to the raw disk.
405 * If the SDINFO ioctl fails because it is unimplemented,
406 * keep going; otherwise, the kernel consistency checks
407 * may prevent us from changing the current (in-core)
410 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
411 errno != ENODEV && errno != ENOTTY) {
412 l_perror("ioctl DIOCSDINFO");
415 (void)lseek(f, (off_t)0, SEEK_SET);
419 * Generate the bootblock checksum for the SRM console.
421 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
427 * Generate a Sun disklabel around the BSD label for
428 * PROM compatability.
430 sl = (struct sun_disklabel *)boot;
431 memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
432 sl->sl_rpm = lp->d_rpm;
433 sl->sl_pcylinders = lp->d_ncylinders +
434 lp->d_acylinders; /* XXX */
435 sl->sl_sparespercyl = lp->d_sparespercyl;
436 sl->sl_interleave = lp->d_interleave;
437 sl->sl_ncylinders = lp->d_ncylinders;
438 sl->sl_acylinders = lp->d_acylinders;
439 sl->sl_ntracks = lp->d_ntracks;
440 sl->sl_nsectors = lp->d_nsectors;
441 sl->sl_magic = SUN_DKMAGIC;
442 secpercyl = sl->sl_nsectors * sl->sl_ntracks;
443 for (i = 0; i < 8; i++) {
444 spp = &sl->sl_part[i];
445 npp = &lp->d_partitions[i];
447 * SunOS partitions must start on a cylinder
448 * boundary. Note this restriction is forced
449 * upon FreeBSD/sparc64 labels too, since we
450 * want to keep both labels synchronised.
452 spp->sdkp_cyloffset = npp->p_offset / secpercyl;
453 spp->sdkp_nsectors = npp->p_size;
456 /* Compute the XOR checksum. */
458 sp2 = (u_short *)(sl + 1);
459 sl->sl_cksum = cksum = 0;
462 sl->sl_cksum = cksum;
465 * write enable label sector before write (if necessary),
466 * disable after writing.
469 if (ioctl(f, DIOCWLABEL, &flag) < 0)
470 warn("ioctl DIOCWLABEL");
471 if (write(f, boot, lp->d_bbsize) != (int)lp->d_bbsize) {
477 * Output the remainder of the disklabel
479 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
485 (void) ioctl(f, DIOCWLABEL, &flag);
486 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
487 l_perror("ioctl DIOCWDINFO");
495 l_perror(const char *s)
500 warnx("%s: no disk label on disk;", s);
501 fprintf(stderr, "add \"-r\" to install initial label\n");
505 warnx("%s: label magic number or checksum is wrong!", s);
506 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
510 warnx("%s: open partition would move or shrink", s);
514 warnx("%s: '%c' partition must start at beginning of disk",
525 * Fetch disklabel for disk.
526 * Use ioctl to get label unless -r flag is given.
531 struct disklabel *lp;
534 if (read(f, bootarea, BBSIZE) < BBSIZE)
535 err(4, "%s", specname);
536 for (lp = (struct disklabel *)bootarea;
537 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
538 lp = (struct disklabel *)((char *)lp + 16))
539 if (lp->d_magic == DISKMAGIC &&
540 lp->d_magic2 == DISKMAGIC)
542 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
543 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
546 "bad pack magic number (label is damaged, or pack is unlabeled)");
549 if (ioctl(f, DIOCGDINFO, lp) < 0)
550 err(4, "ioctl DIOCGDINFO");
556 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
557 * Returns a pointer to the disklabel portion of the bootarea.
560 makebootarea(char *boot, struct disklabel *dp, int f)
562 struct disklabel *lp;
579 if (dp->d_secsize == 0) {
580 dp->d_secsize = DEV_BSIZE;
581 dp->d_bbsize = BBSIZE;
583 lp = (struct disklabel *)
584 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
585 bzero((char *)lp, sizeof *lp);
588 * If we are not installing a boot program but we are installing a
589 * label on disk then we must read the current bootarea so we don't
590 * clobber the existing boot.
594 if (read(f, boot, BBSIZE) < BBSIZE)
595 err(4, "%s", specname);
596 bzero((char *)lp, sizeof *lp);
601 * We are installing a boot program. Determine the name(s) and
602 * read them into the appropriate places in the boot area.
604 if (!xxboot || !bootxx) {
606 if ((p = rindex(dkname, '/')) == NULL)
610 while (*p && !isdigit(*p))
615 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
620 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
628 * 1. One-piece bootstrap (hp300/hp800)
629 * 1. One-piece bootstrap (alpha/sparc64)
630 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
631 * is remembered and written later following the bootarea.
632 * 2. Two-piece bootstraps (i386/ia64)
633 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
634 * bytes of bootarea, remaining d_bbsize-d_secsize filled
637 b = open(xxboot, O_RDONLY);
639 err(4, "%s", xxboot);
644 * The i386 has the so-called fdisk table embedded into the
645 * primary bootstrap. We take care to not clobber it, but
646 * only if it does already contain some data. (Otherwise,
647 * the xxboot provides a template.)
649 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
650 err(4, "%s", xxboot);
651 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
652 #endif /* __i386__ */
653 if (read(b, boot, (int)dp->d_secsize) < 0)
654 err(4, "%s", xxboot);
657 for (i = DOSPARTOFF, found = 0;
658 !found && i < (int)(DOSPARTOFF + NDOSPART*sizeof(struct dos_partition));
660 found = tmpbuf[i] != 0;
662 memcpy((void *)&boot[DOSPARTOFF],
663 (void *)&tmpbuf[DOSPARTOFF],
664 NDOSPART * sizeof(struct dos_partition));
666 #endif /* __i386__ */
667 b = open(bootxx, O_RDONLY);
669 err(4, "%s", bootxx);
670 if (fstat(b, &sb) != 0)
671 err(4, "%s", bootxx);
672 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
673 errx(4, "%s too large", bootxx);
674 if (read(b, &boot[dp->d_secsize],
675 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
676 err(4, "%s", bootxx);
677 #else /* !(NUMBOOT > 1) */
680 * On the alpha, the primary bootstrap starts at the
681 * second sector of the boot area. The first sector
682 * contains the label and must be edited to contain the
683 * size and location of the primary bootstrap.
685 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
687 err(4, "%s", xxboot);
688 bootinfo = (u_long *)(boot + 480);
689 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
690 bootinfo[1] = 1; /* start at sector 1 */
691 bootinfo[2] = 0; /* flags (must be zero) */
692 #else /* !__alpha__ */
693 if (read(b, boot, (int)dp->d_bbsize) < 0)
694 err(4, "%s", xxboot);
695 #endif /* __alpha__ */
696 if (fstat(b, &sb) != 0)
697 err(4, "%s", xxboot);
698 bootsize = (int)sb.st_size - dp->d_bbsize;
700 /* XXX assume d_secsize is a power of two */
701 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
702 bootbuf = (char *)malloc((size_t)bootsize);
704 err(4, "%s", xxboot);
705 if (read(b, bootbuf, bootsize) < 0) {
707 err(4, "%s", xxboot);
710 #endif /* NUMBOOT > 1 */
712 #endif /* NUMBOOT > 0 */
714 * Make sure no part of the bootstrap is written in the area
715 * reserved for the label.
717 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
719 errx(2, "bootstrap doesn't leave room for disk label");
724 display(FILE *f, const struct disklabel *lp)
727 const struct partition *pp;
729 fprintf(f, "# %s:\n", specname);
730 if ((unsigned) lp->d_type < DKMAXTYPES)
731 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
733 fprintf(f, "type: %u\n", lp->d_type);
734 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
736 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
738 fprintf(f, "flags:");
739 if (lp->d_flags & D_REMOVABLE)
740 fprintf(f, " removeable");
741 if (lp->d_flags & D_ECC)
743 if (lp->d_flags & D_BADSECT)
744 fprintf(f, " badsect");
746 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
747 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
748 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
749 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
750 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
751 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
752 fprintf(f, "rpm: %u\n", lp->d_rpm);
753 fprintf(f, "interleave: %u\n", lp->d_interleave);
754 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
755 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
756 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
757 (u_long)lp->d_headswitch);
758 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
759 (u_long)lp->d_trkseek);
760 fprintf(f, "drivedata: ");
761 for (i = NDDATA - 1; i >= 0; i--)
762 if (lp->d_drivedata[i])
766 for (j = 0; j <= i; j++)
767 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
768 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
770 "# size offset fstype [fsize bsize bps/cpg]\n");
771 pp = lp->d_partitions;
772 for (i = 0; i < lp->d_npartitions; i++, pp++) {
774 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
775 (u_long)pp->p_size, (u_long)pp->p_offset);
776 if ((unsigned) pp->p_fstype < FSMAXTYPES)
777 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
779 fprintf(f, "%8d", pp->p_fstype);
780 switch (pp->p_fstype) {
782 case FS_UNUSED: /* XXX */
783 fprintf(f, " %5lu %5lu %5.5s ",
785 (u_long)(pp->p_fsize * pp->p_frag), "");
789 fprintf(f, " %5lu %5lu %5u ",
791 (u_long)(pp->p_fsize * pp->p_frag),
796 fprintf(f, " %5lu %5lu %5d",
798 (u_long)(pp->p_fsize * pp->p_frag),
803 fprintf(f, "%20.20s", "");
806 fprintf(f, "\t# (Cyl. %4lu",
807 (u_long)(pp->p_offset / lp->d_secpercyl));
808 if (pp->p_offset % lp->d_secpercyl)
813 (u_long)((pp->p_offset + pp->p_size +
814 lp->d_secpercyl - 1) /
815 lp->d_secpercyl - 1));
816 if (pp->p_size % lp->d_secpercyl)
825 edit(struct disklabel *lp, int f)
828 struct disklabel label;
831 if ((fd = mkstemp(tmpfil)) == -1 ||
832 (fp = fdopen(fd, "w")) == NULL) {
833 warnx("can't create %s", tmpfil);
841 fp = fopen(tmpfil, "r");
843 warnx("can't reopen %s for reading", tmpfil);
846 bzero((char *)&label, sizeof(label));
847 if (getasciilabel(fp, &label)) {
849 if (writelabel(f, bootarea, lp) == 0) {
851 (void) unlink(tmpfil);
856 printf("re-edit the label? [y]: "); fflush(stdout);
858 if (c != EOF && c != (int)'\n')
859 while (getchar() != (int)'\n')
864 (void) unlink(tmpfil);
875 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
876 while ((pid = fork()) < 0) {
877 if (errno == EPROCLIM) {
878 warnx("you have too many processes");
881 if (errno != EAGAIN) {
891 if ((ed = getenv("EDITOR")) == (char *)0)
893 execlp(ed, ed, tmpfil, (char *)0);
896 while ((xpid = wait(&locstat)) >= 0)
907 while (*cp != '\0' && isspace(*cp))
909 if (*cp == '\0' || *cp == '#')
919 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
921 if ((c = *cp) != '\0') {
930 * Read an ascii label in from fd f,
931 * in the same format as that put out by display(),
935 getasciilabel(FILE *f, struct disklabel *lp)
940 char *tp, line[BUFSIZ];
941 int v, lineno = 0, errors = 0;
944 lp->d_bbsize = BBSIZE; /* XXX */
945 lp->d_sbsize = 0; /* XXX */
946 while (fgets(line, sizeof(line) - 1, f)) {
948 if ((cp = index(line,'\n')) != 0)
955 fprintf(stderr, "line %d: syntax error\n", lineno);
959 *tp++ = '\0', tp = skip(tp);
960 if (streq(cp, "type")) {
964 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
965 if (*cpp && streq(*cpp, tp)) {
966 lp->d_type = cpp - dktypenames;
969 if (cpp < &dktypenames[DKMAXTYPES])
972 if ((unsigned)v >= DKMAXTYPES)
973 fprintf(stderr, "line %d:%s %d\n", lineno,
974 "Warning, unknown disk type", v);
978 if (streq(cp, "flags")) {
979 for (v = 0; (cp = tp) && *cp != '\0';) {
981 if (streq(cp, "removeable"))
983 else if (streq(cp, "ecc"))
985 else if (streq(cp, "badsect"))
989 "line %d: %s: bad flag\n",
997 if (streq(cp, "drivedata")) {
998 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
999 lp->d_drivedata[i++] = atoi(cp);
1004 if (sscanf(cp, "%d partitions", &v) == 1) {
1005 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
1007 "line %d: bad # of partitions\n", lineno);
1008 lp->d_npartitions = MAXPARTITIONS;
1011 lp->d_npartitions = v;
1016 if (streq(cp, "disk")) {
1017 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1020 if (streq(cp, "label")) {
1021 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1024 if (streq(cp, "bytes/sector")) {
1026 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1028 "line %d: %s: bad sector size\n",
1035 if (streq(cp, "sectors/track")) {
1038 fprintf(stderr, "line %d: %s: bad %s\n",
1045 if (streq(cp, "sectors/cylinder")) {
1048 fprintf(stderr, "line %d: %s: bad %s\n",
1052 lp->d_secpercyl = v;
1055 if (streq(cp, "tracks/cylinder")) {
1058 fprintf(stderr, "line %d: %s: bad %s\n",
1065 if (streq(cp, "cylinders")) {
1068 fprintf(stderr, "line %d: %s: bad %s\n",
1072 lp->d_ncylinders = v;
1075 if (streq(cp, "sectors/unit")) {
1078 fprintf(stderr, "line %d: %s: bad %s\n",
1082 lp->d_secperunit = v;
1085 if (streq(cp, "rpm")) {
1088 fprintf(stderr, "line %d: %s: bad %s\n",
1095 if (streq(cp, "interleave")) {
1098 fprintf(stderr, "line %d: %s: bad %s\n",
1102 lp->d_interleave = v;
1105 if (streq(cp, "trackskew")) {
1108 fprintf(stderr, "line %d: %s: bad %s\n",
1112 lp->d_trackskew = v;
1115 if (streq(cp, "cylinderskew")) {
1118 fprintf(stderr, "line %d: %s: bad %s\n",
1125 if (streq(cp, "headswitch")) {
1128 fprintf(stderr, "line %d: %s: bad %s\n",
1132 lp->d_headswitch = v;
1135 if (streq(cp, "track-to-track seek")) {
1138 fprintf(stderr, "line %d: %s: bad %s\n",
1145 /* the ':' was removed above */
1146 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1148 "line %d: %s: Unknown disklabel field\n", lineno,
1154 /* Process a partition specification line. */
1156 if (part >= lp->d_npartitions) {
1158 "line %d: partition name out of range a-%c: %s\n",
1159 lineno, 'a' + lp->d_npartitions - 1, cp);
1165 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1170 errors += checklabel(lp);
1171 return (errors == 0);
1174 #define NXTNUM(n) do { \
1176 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1179 cp = tp, tp = word(cp); \
1184 /* retain 1 character following number */
1185 #define NXTWORD(w,n) do { \
1187 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1191 cp = tp, tp = word(cp); \
1192 (n) = strtol(cp,&tmp,10); \
1193 if (tmp) (w) = *tmp; \
1198 * Read a partition line into partition `part' in the specified disklabel.
1199 * Return 0 on success, 1 on failure.
1202 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1204 struct partition *pp;
1209 pp = &lp->d_partitions[part];
1213 NXTWORD(part_size_type[part],v);
1214 if (v < 0 || (v == 0 && part_size_type[part] != '*')) {
1215 fprintf(stderr, "line %d: %s: bad partition size\n", lineno,
1222 NXTWORD(part_offset_type[part],v);
1223 if (v < 0 || (v == 0 && part_offset_type[part] != '*' &&
1224 part_offset_type[part] != '\0')) {
1225 fprintf(stderr, "line %d: %s: bad partition offset\n", lineno,
1230 cp = tp, tp = word(cp);
1231 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1232 if (*cpp && streq(*cpp, cp))
1235 pp->p_fstype = cpp - fstypenames;
1241 if ((unsigned)v >= FSMAXTYPES) {
1243 "line %d: Warning, unknown file system type %s\n",
1250 switch (pp->p_fstype) {
1253 * allow us to accept defaults for
1257 NXTNUM(pp->p_fsize);
1258 if (pp->p_fsize == 0)
1261 pp->p_frag = v / pp->p_fsize;
1263 /* else default to 0's */
1266 /* These happen to be the same */
1270 NXTNUM(pp->p_fsize);
1271 if (pp->p_fsize == 0)
1274 pp->p_frag = v / pp->p_fsize;
1278 * FIX! poor attempt at adaptive
1281 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1283 * FIX! These are too low, but are traditional
1285 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1286 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1288 pp->p_cpg = DEFAULT_NEWFS_CPG;
1290 pp->p_fsize = BIG_NEWFS_FRAG;
1291 pp->p_frag = BIG_NEWFS_BLOCK /
1293 pp->p_cpg = BIG_NEWFS_CPG;
1303 * Check disklabel for errors and fill in
1304 * derived fields according to supplied values.
1307 checklabel(struct disklabel *lp)
1309 struct partition *pp;
1312 unsigned long total_size, total_percent, current_offset;
1313 int seen_default_offset;
1316 struct partition *pp2;
1318 if (lp->d_secsize == 0) {
1319 fprintf(stderr, "sector size 0\n");
1322 if (lp->d_nsectors == 0) {
1323 fprintf(stderr, "sectors/track 0\n");
1326 if (lp->d_ntracks == 0) {
1327 fprintf(stderr, "tracks/cylinder 0\n");
1330 if (lp->d_ncylinders == 0) {
1331 fprintf(stderr, "cylinders/unit 0\n");
1335 Warning("revolutions/minute 0");
1336 if (lp->d_secpercyl == 0)
1337 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1338 if (lp->d_secperunit == 0)
1339 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1340 if (lp->d_bbsize == 0) {
1341 fprintf(stderr, "boot block size 0\n");
1343 } else if (lp->d_bbsize % lp->d_secsize)
1344 Warning("boot block size %% sector-size != 0");
1345 if (lp->d_npartitions > MAXPARTITIONS)
1346 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1347 (u_long)lp->d_npartitions, MAXPARTITIONS);
1349 /* first allocate space to the partitions, then offsets */
1350 total_size = 0; /* in sectors */
1351 total_percent = 0; /* in percent */
1353 /* find all fixed partitions */
1354 for (i = 0; i < lp->d_npartitions; i++) {
1355 pp = &lp->d_partitions[i];
1357 if (part_size_type[i] == '*') {
1358 if (i == RAW_PART) {
1359 pp->p_size = lp->d_secperunit;
1362 Warning("Too many '*' partitions (%c and %c)",
1363 hog_part + 'a',i + 'a');
1371 switch (part_size_type[i]) {
1373 total_percent += size;
1381 size *= 1024ULL * 1024ULL;
1385 size *= 1024ULL * 1024ULL * 1024ULL;
1390 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1393 /* don't count %'s yet */
1394 if (part_size_type[i] != '%') {
1396 * for all not in sectors, convert to
1399 if (part_size_type[i] != '\0') {
1400 if (size % lp->d_secsize != 0)
1401 Warning("partition %c not an integer number of sectors",
1403 size /= lp->d_secsize;
1406 /* else already in sectors */
1413 /* handle % partitions - note %'s don't need to add up to 100! */
1414 if (total_percent != 0) {
1415 long free_space = lp->d_secperunit - total_size;
1416 if (total_percent > 100) {
1417 fprintf(stderr,"total percentage %lu is greater than 100\n",
1422 if (free_space > 0) {
1423 for (i = 0; i < lp->d_npartitions; i++) {
1424 pp = &lp->d_partitions[i];
1425 if (part_set[i] && part_size_type[i] == '%') {
1426 /* careful of overflows! and integer roundoff */
1427 pp->p_size = ((double)pp->p_size/100) * free_space;
1428 total_size += pp->p_size;
1430 /* FIX we can lose a sector or so due to roundoff per
1431 partition. A more complex algorithm could avoid that */
1436 "%ld sectors available to give to '*' and '%%' partitions\n",
1439 /* fix? set all % partitions to size 0? */
1442 /* give anything remaining to the hog partition */
1443 if (hog_part != -1) {
1444 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1445 total_size = lp->d_secperunit;
1448 /* Now set the offsets for each partition */
1449 current_offset = 0; /* in sectors */
1450 seen_default_offset = 0;
1451 for (i = 0; i < lp->d_npartitions; i++) {
1453 pp = &lp->d_partitions[i];
1455 if (part_offset_type[i] == '*') {
1456 if (i == RAW_PART) {
1459 pp->p_offset = current_offset;
1460 seen_default_offset = 1;
1463 /* allow them to be out of order for old-style tables */
1464 if (pp->p_offset < current_offset &&
1465 seen_default_offset && i != RAW_PART) {
1467 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1468 (long)pp->p_offset,i+'a',current_offset);
1470 "Labels with any *'s for offset must be in ascending order by sector\n");
1472 } else if (pp->p_offset != current_offset &&
1473 i != RAW_PART && seen_default_offset) {
1475 * this may give unneeded warnings if
1476 * partitions are out-of-order
1479 "Offset %ld for partition %c doesn't match expected value %ld",
1480 (long)pp->p_offset, i + 'a', current_offset);
1484 current_offset = pp->p_offset + pp->p_size;
1488 for (i = 0; i < lp->d_npartitions; i++) {
1490 pp = &lp->d_partitions[i];
1491 if (pp->p_size == 0 && pp->p_offset != 0)
1492 Warning("partition %c: size 0, but offset %lu",
1493 part, (u_long)pp->p_offset);
1495 /* See comment in writelabel(). */
1496 if (pp->p_offset % lp->d_secpercyl != 0) {
1497 fprintf(stderr, "partition %c: does not start on a "
1498 "cylinder boundary!\n", part);
1503 if (pp->p_size % lp->d_secpercyl)
1504 Warning("partition %c: size %% cylinder-size != 0",
1506 if (pp->p_offset % lp->d_secpercyl)
1507 Warning("partition %c: offset %% cylinder-size != 0",
1510 if (pp->p_offset > lp->d_secperunit) {
1512 "partition %c: offset past end of unit\n", part);
1515 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1517 "partition %c: partition extends past end of unit\n",
1523 if (pp->p_fstype != FS_UNUSED)
1524 Warning("partition %c is not marked as unused!",part);
1525 if (pp->p_offset != 0)
1526 Warning("partition %c doesn't start at 0!",part);
1527 if (pp->p_size != lp->d_secperunit)
1528 Warning("partition %c doesn't cover the whole unit!",part);
1530 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1531 (pp->p_size != lp->d_secperunit)) {
1532 Warning("An incorrect partition %c may cause problems for "
1533 "standard system utilities",part);
1537 /* check for overlaps */
1538 /* this will check for all possible overlaps once and only once */
1539 for (j = 0; j < i; j++) {
1540 if (j != RAW_PART && i != RAW_PART &&
1541 part_set[i] && part_set[j]) {
1542 pp2 = &lp->d_partitions[j];
1543 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1544 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1545 pp2->p_offset >= pp->p_offset)) {
1546 fprintf(stderr,"partitions %c and %c overlap!\n",
1553 for (; i < MAXPARTITIONS; i++) {
1555 pp = &lp->d_partitions[i];
1556 if (pp->p_size || pp->p_offset)
1557 Warning("unused partition %c: size %d offset %lu",
1558 'a' + i, pp->p_size, (u_long)pp->p_offset);
1564 * When operating on a "virgin" disk, try getting an initial label
1565 * from the associated device driver. This might work for all device
1566 * drivers that are able to fetch some initial device parameters
1567 * without even having access to a (BSD) disklabel, like SCSI disks,
1568 * most IDE drives, or vn devices.
1570 * The device name must be given in its "canonical" form.
1573 getvirginlabel(void)
1575 static struct disklabel loclab;
1576 char lnamebuf[BBSIZE];
1579 if (dkname[0] == '/') {
1580 warnx("\"auto\" requires the usage of a canonical disk name");
1583 (void)snprintf(lnamebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1584 if ((f = open(lnamebuf, O_RDONLY)) == -1) {
1585 warn("cannot open %s", lnamebuf);
1590 * Try to use the new get-virgin-label ioctl. If it fails,
1591 * fallback to the old get-disdk-info ioctl.
1593 if (ioctl(f, DIOCGDVIRGIN, &loclab) == 0)
1595 if (ioctl(f, DIOCGDINFO, &loclab) == 0)
1598 (void)snprintf(lnamebuf, BBSIZE, "%s%s%c", _PATH_DEV, dkname,
1600 if ((f = open(lnamebuf, O_RDONLY)) == -1) {
1601 warn("cannot open %s", lnamebuf);
1604 if (ioctl(f, DIOCGDINFO, &loclab) == 0)
1607 warn("No virgin disklabel found %s", lnamebuf);
1615 * If we are installing a boot program that doesn't fit in d_bbsize
1616 * we need to mark those partitions that the boot overflows into.
1617 * This allows newfs to prevent creation of a file system where it might
1618 * clobber bootstrap code.
1621 setbootflag(struct disklabel *lp)
1623 struct partition *pp;
1630 boffset = bootsize / lp->d_secsize;
1631 for (i = 0; i < lp->d_npartitions; i++) {
1633 pp = &lp->d_partitions[i];
1634 if (pp->p_size == 0)
1636 if (boffset <= pp->p_offset) {
1637 if (pp->p_fstype == FS_BOOT)
1638 pp->p_fstype = FS_UNUSED;
1639 } else if (pp->p_fstype != FS_BOOT) {
1640 if (pp->p_fstype != FS_UNUSED) {
1642 "boot overlaps used partition %c\n",
1646 pp->p_fstype = FS_BOOT;
1647 Warning("boot overlaps partition %c, %s",
1648 part, "marked as FS_BOOT");
1653 errx(4, "cannot install boot program");
1658 Warning(const char *fmt, ...)
1662 fprintf(stderr, "Warning, ");
1664 vfprintf(stderr, fmt, ap);
1665 fprintf(stderr, "\n");
1673 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",
1674 "usage: disklabel [-r] disk",
1675 "\t\t(to read label)",
1676 " disklabel -w [-r] [-n] disk type [ packid ]",
1677 "\t\t(to write label with existing boot program)",
1678 " disklabel -e [-r] [-n] disk",
1679 "\t\t(to edit label)",
1680 " disklabel -R [-r] [-n] disk protofile",
1681 "\t\t(to restore label with existing boot program)",
1683 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1684 "\t\t(to install boot program with existing label)",
1685 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1686 "\t\t(to write label and boot program)",
1687 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1688 "\t\t(to restore label and boot program)",
1690 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1691 "\t\t(to install boot program with existing on-disk label)",
1692 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1693 "\t\t(to write label and install boot program)",
1694 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1695 "\t\t(to restore label and install boot program)",
1697 " disklabel [-NW] disk",
1698 "\t\t(to write disable/enable label)");
1700 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1701 "usage: disklabel [-r] disk", "(to read label)",
1702 " disklabel -w [-r] [-n] disk type [ packid ]",
1703 "\t\t(to write label)",
1704 " disklabel -e [-r] [-n] disk",
1705 "\t\t(to edit label)",
1706 " disklabel -R [-r] [-n] disk protofile",
1707 "\t\t(to restore label)",
1708 " disklabel [-NW] disk",
1709 "\t\t(to write disable/enable label)");