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 4096U
98 #define BIG_NEWFS_CPG 64U
100 #if defined(__i386__) || defined(__ia64__)
102 #elif defined(__alpha__) || defined(__sparc64__)
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 checklabel(struct disklabel *);
120 void setbootflag(struct disklabel *);
121 void Warning(const char *, ...) __printflike(1, 2);
123 struct disklabel *getvirginlabel(void);
125 #define DEFEDITOR _PATH_VI
126 #define streq(a,b) (strcmp(a,b) == 0)
130 char tmpfil[] = PATH_TMPFILE;
132 char namebuf[BBSIZE], *np = namebuf;
133 struct disklabel lab;
134 char bootarea[BBSIZE];
136 #define MAX_PART ('z')
137 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
138 char part_size_type[MAX_NUM_PARTS];
139 char part_offset_type[MAX_NUM_PARTS];
140 int part_set[MAX_NUM_PARTS];
143 int installboot; /* non-zero if we should install a boot program */
144 char *bootbuf; /* pointer to buffer with remainder of boot prog */
145 int bootsize; /* size of remaining boot program */
146 char *xxboot; /* primary boot */
147 char *bootxx; /* secondary boot */
148 char boot0[MAXPATHLEN];
149 char boot1[MAXPATHLEN];
153 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
157 int disable_write; /* set to disable writing to disk label */
159 #define OPTIONS "BNRWb:enrs:w"
162 main(int argc, char *argv[])
164 struct disklabel *lp;
166 int ch, f = 0, flag, error = 0;
169 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
239 if (dkname[0] != '/') {
240 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
242 np += strlen(specname) + 1;
245 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
246 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
247 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
248 np = namebuf + strlen(specname) + 1;
249 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
252 err(4, "%s", specname);
268 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
269 err(4, "ioctl DIOCWLABEL");
277 error = checklabel(lp);
282 if (installboot && argc == 3) {
283 makelabel(argv[2], 0, &lab);
287 * We only called makelabel() for its side effect
288 * of setting the bootstrap file names. Discard
289 * all changes to `lab' so that all values in the
290 * final label come from the ASCII label.
292 bzero((char *)&lab, sizeof(lab));
297 if (!(t = fopen(argv[1], "r")))
298 err(4, "%s", argv[1]);
299 if (!getasciilabel(t, &lab))
301 lp = makebootarea(bootarea, &lab, f);
303 error = writelabel(f, bootarea, lp);
313 makelabel(argv[1], name, &lab);
314 lp = makebootarea(bootarea, &lab, f);
316 if (checklabel(lp) == 0)
317 error = writelabel(f, bootarea, lp);
322 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
323 err(4, "ioctl DIOCWLABEL");
329 struct disklabel tlab;
334 makelabel(argv[1], 0, &lab);
335 lp = makebootarea(bootarea, &lab, f);
337 if (checklabel(lp) == 0)
338 error = writelabel(f, bootarea, lp);
347 * Construct a prototype disklabel from /etc/disktab. As a side
348 * effect, set the names of the primary and secondary boot files
352 makelabel(const char *type, const char *name, struct disklabel *lp)
354 struct disklabel *dp;
356 if (strcmp(type, "auto") == 0)
357 dp = getvirginlabel();
359 dp = getdiskbyname(type);
361 errx(1, "%s: unknown disk type", type);
363 bzero(lp->d_packname, sizeof(lp->d_packname));
365 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
369 writelabel(int f, const char *boot, struct disklabel *lp)
377 struct sun_disklabel *sl;
378 u_short cksum, *sp1, *sp2;
379 struct partition *npp;
380 struct sun_dkpart *spp;
385 Warning("write to disk label supressed - label was as follows:");
390 lp->d_magic = DISKMAGIC;
391 lp->d_magic2 = DISKMAGIC;
393 lp->d_checksum = dkcksum(lp);
396 * First set the kernel disk label,
397 * then write a label to the raw disk.
398 * If the SDINFO ioctl fails because it is unimplemented,
399 * keep going; otherwise, the kernel consistency checks
400 * may prevent us from changing the current (in-core)
403 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
404 errno != ENODEV && errno != ENOTTY) {
405 l_perror("ioctl DIOCSDINFO");
408 (void)lseek(f, (off_t)0, SEEK_SET);
412 * Generate the bootblock checksum for the SRM console.
414 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
420 * Generate a Sun disklabel around the BSD label for
421 * PROM compatability.
423 sl = (struct sun_disklabel *)boot;
424 memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
425 sl->sl_rpm = lp->d_rpm;
426 sl->sl_pcylinders = lp->d_ncylinders +
427 lp->d_acylinders; /* XXX */
428 sl->sl_sparespercyl = lp->d_sparespercyl;
429 sl->sl_interleave = lp->d_interleave;
430 sl->sl_ncylinders = lp->d_ncylinders;
431 sl->sl_acylinders = lp->d_acylinders;
432 sl->sl_ntracks = lp->d_ntracks;
433 sl->sl_nsectors = lp->d_nsectors;
434 sl->sl_magic = SUN_DKMAGIC;
435 secpercyl = sl->sl_nsectors * sl->sl_ntracks;
436 for (i = 0; i < 8; i++) {
437 spp = &sl->sl_part[i];
438 npp = &lp->d_partitions[i];
440 * SunOS partitions must start on a cylinder
441 * boundary. Note this restriction is forced
442 * upon FreeBSD/sparc64 labels too, since we
443 * want to keep both labels synchronised.
445 spp->sdkp_cyloffset = npp->p_offset / secpercyl;
446 spp->sdkp_nsectors = npp->p_size;
449 /* Compute the XOR checksum. */
451 sp2 = (u_short *)(sl + 1);
452 sl->sl_cksum = cksum = 0;
455 sl->sl_cksum = cksum;
458 * write enable label sector before write (if necessary),
459 * disable after writing.
462 if (ioctl(f, DIOCWLABEL, &flag) < 0)
463 warn("ioctl DIOCWLABEL");
464 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
470 * Output the remainder of the disklabel
472 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
478 (void) ioctl(f, DIOCWLABEL, &flag);
479 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
480 l_perror("ioctl DIOCWDINFO");
488 l_perror(const char *s)
493 warnx("%s: no disk label on disk;", s);
494 fprintf(stderr, "add \"-r\" to install initial label\n");
498 warnx("%s: label magic number or checksum is wrong!", s);
499 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
503 warnx("%s: open partition would move or shrink", s);
507 warnx("%s: '%c' partition must start at beginning of disk",
518 * Fetch disklabel for disk.
519 * Use ioctl to get label unless -r flag is given.
524 struct disklabel *lp;
527 if (read(f, bootarea, BBSIZE) < BBSIZE)
528 err(4, "%s", specname);
529 for (lp = (struct disklabel *)bootarea;
530 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
531 lp = (struct disklabel *)((char *)lp + 16))
532 if (lp->d_magic == DISKMAGIC &&
533 lp->d_magic2 == DISKMAGIC)
535 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
536 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
539 "bad pack magic number (label is damaged, or pack is unlabeled)");
542 if (ioctl(f, DIOCGDINFO, lp) < 0)
543 err(4, "ioctl DIOCGDINFO");
549 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
550 * Returns a pointer to the disklabel portion of the bootarea.
553 makebootarea(char *boot, struct disklabel *dp, int f)
555 struct disklabel *lp;
572 if (dp->d_secsize == 0) {
573 dp->d_secsize = DEV_BSIZE;
574 dp->d_bbsize = BBSIZE;
576 lp = (struct disklabel *)
577 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
578 bzero((char *)lp, sizeof *lp);
581 * If we are not installing a boot program but we are installing a
582 * label on disk then we must read the current bootarea so we don't
583 * clobber the existing boot.
587 if (read(f, boot, BBSIZE) < BBSIZE)
588 err(4, "%s", specname);
589 bzero((char *)lp, sizeof *lp);
594 * We are installing a boot program. Determine the name(s) and
595 * read them into the appropriate places in the boot area.
597 if (!xxboot || !bootxx) {
599 if ((p = rindex(dkname, '/')) == NULL)
603 while (*p && !isdigit(*p))
608 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
613 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
621 * 1. One-piece bootstrap (hp300/hp800)
622 * 1. One-piece bootstrap (alpha/sparc64)
623 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
624 * is remembered and written later following the bootarea.
625 * 2. Two-piece bootstraps (i386/ia64)
626 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
627 * bytes of bootarea, remaining d_bbsize-d_secsize filled
630 b = open(xxboot, O_RDONLY);
632 err(4, "%s", xxboot);
637 * The i386 has the so-called fdisk table embedded into the
638 * primary bootstrap. We take care to not clobber it, but
639 * only if it does already contain some data. (Otherwise,
640 * the xxboot provides a template.)
642 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
643 err(4, "%s", xxboot);
644 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
646 if (read(b, boot, (int)dp->d_secsize) < 0)
647 err(4, "%s", xxboot);
650 for (i = DOSPARTOFF, found = 0;
651 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
653 found = tmpbuf[i] != 0;
655 memcpy((void *)&boot[DOSPARTOFF],
656 (void *)&tmpbuf[DOSPARTOFF],
657 NDOSPART * sizeof(struct dos_partition));
660 b = open(bootxx, O_RDONLY);
662 err(4, "%s", bootxx);
663 if (fstat(b, &sb) != 0)
664 err(4, "%s", bootxx);
665 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
666 errx(4, "%s too large", bootxx);
667 if (read(b, &boot[dp->d_secsize],
668 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
669 err(4, "%s", bootxx);
670 #else /* !(NUMBOOT > 1) */
673 * On the alpha, the primary bootstrap starts at the
674 * second sector of the boot area. The first sector
675 * contains the label and must be edited to contain the
676 * size and location of the primary bootstrap.
678 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
680 err(4, "%s", xxboot);
681 bootinfo = (u_long *)(boot + 480);
682 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
683 bootinfo[1] = 1; /* start at sector 1 */
684 bootinfo[2] = 0; /* flags (must be zero) */
685 #else /* !__alpha__ */
686 if (read(b, boot, (int)dp->d_bbsize) < 0)
687 err(4, "%s", xxboot);
688 #endif /* __alpha__ */
689 if (fstat(b, &sb) != 0)
690 err(4, "%s", xxboot);
691 bootsize = (int)sb.st_size - dp->d_bbsize;
693 /* XXX assume d_secsize is a power of two */
694 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
695 bootbuf = (char *)malloc((size_t)bootsize);
697 err(4, "%s", xxboot);
698 if (read(b, bootbuf, bootsize) < 0) {
700 err(4, "%s", xxboot);
703 #endif /* NUMBOOT > 1 */
705 #endif /* NUMBOOT > 0 */
707 * Make sure no part of the bootstrap is written in the area
708 * reserved for the label.
710 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
712 errx(2, "bootstrap doesn't leave room for disk label");
717 display(FILE *f, const struct disklabel *lp)
720 const struct partition *pp;
722 fprintf(f, "# %s:\n", specname);
723 if ((unsigned) lp->d_type < DKMAXTYPES)
724 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
726 fprintf(f, "type: %u\n", lp->d_type);
727 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
729 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
731 fprintf(f, "flags:");
732 if (lp->d_flags & D_REMOVABLE)
733 fprintf(f, " removeable");
734 if (lp->d_flags & D_ECC)
736 if (lp->d_flags & D_BADSECT)
737 fprintf(f, " badsect");
739 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
740 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
741 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
742 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
743 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
744 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
745 fprintf(f, "rpm: %u\n", lp->d_rpm);
746 fprintf(f, "interleave: %u\n", lp->d_interleave);
747 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
748 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
749 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
750 (u_long)lp->d_headswitch);
751 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
752 (u_long)lp->d_trkseek);
753 fprintf(f, "drivedata: ");
754 for (i = NDDATA - 1; i >= 0; i--)
755 if (lp->d_drivedata[i])
759 for (j = 0; j <= i; j++)
760 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
761 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
763 "# size offset fstype [fsize bsize bps/cpg]\n");
764 pp = lp->d_partitions;
765 for (i = 0; i < lp->d_npartitions; i++, pp++) {
767 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
768 (u_long)pp->p_size, (u_long)pp->p_offset);
769 if ((unsigned) pp->p_fstype < FSMAXTYPES)
770 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
772 fprintf(f, "%8d", pp->p_fstype);
773 switch (pp->p_fstype) {
775 case FS_UNUSED: /* XXX */
776 fprintf(f, " %5lu %5lu %5.5s ",
778 (u_long)(pp->p_fsize * pp->p_frag), "");
782 fprintf(f, " %5lu %5lu %5u ",
784 (u_long)(pp->p_fsize * pp->p_frag),
789 fprintf(f, " %5lu %5lu %5d",
791 (u_long)(pp->p_fsize * pp->p_frag),
796 fprintf(f, "%20.20s", "");
799 fprintf(f, "\t# (Cyl. %4lu",
800 (u_long)(pp->p_offset / lp->d_secpercyl));
801 if (pp->p_offset % lp->d_secpercyl)
806 (u_long)((pp->p_offset + pp->p_size +
807 lp->d_secpercyl - 1) /
808 lp->d_secpercyl - 1));
809 if (pp->p_size % lp->d_secpercyl)
818 edit(struct disklabel *lp, int f)
821 struct disklabel label;
824 if ((fd = mkstemp(tmpfil)) == -1 ||
825 (fp = fdopen(fd, "w")) == NULL) {
826 warnx("can't create %s", tmpfil);
834 fp = fopen(tmpfil, "r");
836 warnx("can't reopen %s for reading", tmpfil);
839 bzero((char *)&label, sizeof(label));
840 if (getasciilabel(fp, &label)) {
842 if (writelabel(f, bootarea, lp) == 0) {
844 (void) unlink(tmpfil);
849 printf("re-edit the label? [y]: "); fflush(stdout);
851 if (c != EOF && c != (int)'\n')
852 while (getchar() != (int)'\n')
857 (void) unlink(tmpfil);
868 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
869 while ((pid = fork()) < 0) {
870 if (errno == EPROCLIM) {
871 warnx("you have too many processes");
874 if (errno != EAGAIN) {
884 if ((ed = getenv("EDITOR")) == (char *)0)
886 execlp(ed, ed, tmpfil, (char *)0);
889 while ((xpid = wait(&stat)) >= 0)
900 while (*cp != '\0' && isspace(*cp))
902 if (*cp == '\0' || *cp == '#')
912 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
914 if ((c = *cp) != '\0') {
923 * Read an ascii label in from fd f,
924 * in the same format as that put out by display(),
928 getasciilabel(FILE *f, struct disklabel *lp)
932 struct partition *pp;
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;
964 if ((unsigned)v >= DKMAXTYPES)
965 fprintf(stderr, "line %d:%s %d\n", lineno,
966 "Warning, unknown disk type", v);
970 if (streq(cp, "flags")) {
971 for (v = 0; (cp = tp) && *cp != '\0';) {
973 if (streq(cp, "removeable"))
975 else if (streq(cp, "ecc"))
977 else if (streq(cp, "badsect"))
981 "line %d: %s: bad flag\n",
989 if (streq(cp, "drivedata")) {
990 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
991 lp->d_drivedata[i++] = atoi(cp);
996 if (sscanf(cp, "%d partitions", &v) == 1) {
997 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
999 "line %d: bad # of partitions\n", lineno);
1000 lp->d_npartitions = MAXPARTITIONS;
1003 lp->d_npartitions = v;
1008 if (streq(cp, "disk")) {
1009 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1012 if (streq(cp, "label")) {
1013 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1016 if (streq(cp, "bytes/sector")) {
1018 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1020 "line %d: %s: bad sector size\n",
1027 if (streq(cp, "sectors/track")) {
1030 fprintf(stderr, "line %d: %s: bad %s\n",
1037 if (streq(cp, "sectors/cylinder")) {
1040 fprintf(stderr, "line %d: %s: bad %s\n",
1044 lp->d_secpercyl = v;
1047 if (streq(cp, "tracks/cylinder")) {
1050 fprintf(stderr, "line %d: %s: bad %s\n",
1057 if (streq(cp, "cylinders")) {
1060 fprintf(stderr, "line %d: %s: bad %s\n",
1064 lp->d_ncylinders = v;
1067 if (streq(cp, "sectors/unit")) {
1070 fprintf(stderr, "line %d: %s: bad %s\n",
1074 lp->d_secperunit = v;
1077 if (streq(cp, "rpm")) {
1080 fprintf(stderr, "line %d: %s: bad %s\n",
1087 if (streq(cp, "interleave")) {
1090 fprintf(stderr, "line %d: %s: bad %s\n",
1094 lp->d_interleave = v;
1097 if (streq(cp, "trackskew")) {
1100 fprintf(stderr, "line %d: %s: bad %s\n",
1104 lp->d_trackskew = v;
1107 if (streq(cp, "cylinderskew")) {
1110 fprintf(stderr, "line %d: %s: bad %s\n",
1117 if (streq(cp, "headswitch")) {
1120 fprintf(stderr, "line %d: %s: bad %s\n",
1124 lp->d_headswitch = v;
1127 if (streq(cp, "track-to-track seek")) {
1130 fprintf(stderr, "line %d: %s: bad %s\n",
1137 /* the ':' was removed above */
1138 if ('a' <= *cp && *cp <= MAX_PART && cp[1] == '\0') {
1140 if (part >= lp->d_npartitions) {
1142 "line %d: partition name out of range a-%c: %s\n",
1143 lineno, 'a' + lp->d_npartitions - 1, cp);
1147 pp = &lp->d_partitions[part];
1149 #define NXTNUM(n) { \
1151 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1155 cp = tp, tp = word(cp); \
1161 /* retain 1 character following number */
1162 #define NXTWORD(w,n) { \
1164 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1169 cp = tp, tp = word(cp); \
1172 (n) = strtol(cp,&tmp,10); \
1173 if (tmp) (w) = *tmp; \
1177 NXTWORD(part_size_type[part],v);
1178 if (v < 0 || (v == 0 && part_size_type[part] != '*')) {
1180 "line %d: %s: bad partition size\n",
1188 NXTWORD(part_offset_type[part],v);
1189 if (v < 0 || (v == 0 &&
1190 part_offset_type[part] != '*' &&
1191 part_offset_type[part] != '\0')) {
1193 "line %d: %s: bad partition offset\n",
1199 cp = tp, tp = word(cp);
1201 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1202 if (*cpp && streq(*cpp, cp)) {
1203 pp->p_fstype = cpp -
1211 if ((unsigned)v >= FSMAXTYPES) {
1213 "line %d: Warning, unknown "
1214 "filesystem type %s\n",
1221 * Note: NXTNUM will break us out of the
1224 switch (pp->p_fstype) {
1227 * allow us to accept defaults for
1231 NXTNUM(pp->p_fsize);
1232 if (pp->p_fsize == 0)
1235 pp->p_frag = v / pp->p_fsize;
1237 /* else default to 0's */
1240 /* These happen to be the same */
1244 NXTNUM(pp->p_fsize);
1245 if (pp->p_fsize == 0)
1248 pp->p_frag = v / pp->p_fsize;
1252 * FIX! poor attempt at
1256 if (pp->p_size < 1*1024*1024*1024/lp->d_secsize) {
1257 /* FIX! These are too low, but are traditional */
1258 pp->p_fsize = DEFAULT_NEWFS_BLOCK;
1259 pp->p_frag = (unsigned char) DEFAULT_NEWFS_FRAG;
1260 pp->p_cpg = DEFAULT_NEWFS_CPG;
1262 pp->p_fsize = BIG_NEWFS_BLOCK;
1263 pp->p_frag = (unsigned char) BIG_NEWFS_FRAG;
1264 pp->p_cpg = BIG_NEWFS_CPG;
1273 * note: we may not have
1274 * gotten all the entries for
1275 * the fs though if we didn't,
1276 * errors will be set.
1282 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1287 errors += checklabel(lp);
1288 return (errors == 0);
1292 * Check disklabel for errors and fill in
1293 * derived fields according to supplied values.
1296 checklabel(struct disklabel *lp)
1298 struct partition *pp;
1301 unsigned long total_size, total_percent, current_offset;
1302 int seen_default_offset;
1305 struct partition *pp2;
1307 if (lp->d_secsize == 0) {
1308 fprintf(stderr, "sector size 0\n");
1311 if (lp->d_nsectors == 0) {
1312 fprintf(stderr, "sectors/track 0\n");
1315 if (lp->d_ntracks == 0) {
1316 fprintf(stderr, "tracks/cylinder 0\n");
1319 if (lp->d_ncylinders == 0) {
1320 fprintf(stderr, "cylinders/unit 0\n");
1324 Warning("revolutions/minute 0");
1325 if (lp->d_secpercyl == 0)
1326 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1327 if (lp->d_secperunit == 0)
1328 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1329 if (lp->d_bbsize == 0) {
1330 fprintf(stderr, "boot block size 0\n");
1332 } else if (lp->d_bbsize % lp->d_secsize)
1333 Warning("boot block size %% sector-size != 0");
1334 if (lp->d_npartitions > MAXPARTITIONS)
1335 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1336 (u_long)lp->d_npartitions, MAXPARTITIONS);
1338 /* first allocate space to the partitions, then offsets */
1339 total_size = 0; /* in sectors */
1340 total_percent = 0; /* in percent */
1342 /* find all fixed partitions */
1343 for (i = 0; i < lp->d_npartitions; i++) {
1344 pp = &lp->d_partitions[i];
1346 if (part_size_type[i] == '*') {
1347 if (i == RAW_PART) {
1348 pp->p_size = lp->d_secperunit;
1351 Warning("Too many '*' partitions (%c and %c)",
1352 hog_part + 'a',i + 'a');
1360 switch (part_size_type[i]) {
1362 total_percent += size;
1370 size *= 1024ULL * 1024ULL;
1374 size *= 1024ULL * 1024ULL * 1024ULL;
1379 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1382 /* don't count %'s yet */
1383 if (part_size_type[i] != '%') {
1385 * for all not in sectors, convert to
1388 if (part_size_type[i] != '\0') {
1389 if (size % lp->d_secsize != 0)
1390 Warning("partition %c not an integer number of sectors",
1392 size /= lp->d_secsize;
1395 /* else already in sectors */
1402 /* handle % partitions - note %'s don't need to add up to 100! */
1403 if (total_percent != 0) {
1404 long free_space = lp->d_secperunit - total_size;
1405 if (total_percent > 100) {
1406 fprintf(stderr,"total percentage %lu is greater than 100\n",
1411 if (free_space > 0) {
1412 for (i = 0; i < lp->d_npartitions; i++) {
1413 pp = &lp->d_partitions[i];
1414 if (part_set[i] && part_size_type[i] == '%') {
1415 /* careful of overflows! and integer roundoff */
1416 pp->p_size = ((double)pp->p_size/100) * free_space;
1417 total_size += pp->p_size;
1419 /* FIX we can lose a sector or so due to roundoff per
1420 partition. A more complex algorithm could avoid that */
1425 "%ld sectors available to give to '*' and '%%' partitions\n",
1428 /* fix? set all % partitions to size 0? */
1431 /* give anything remaining to the hog partition */
1432 if (hog_part != -1) {
1433 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1434 total_size = lp->d_secperunit;
1437 /* Now set the offsets for each partition */
1438 current_offset = 0; /* in sectors */
1439 seen_default_offset = 0;
1440 for (i = 0; i < lp->d_npartitions; i++) {
1442 pp = &lp->d_partitions[i];
1444 if (part_offset_type[i] == '*') {
1445 if (i == RAW_PART) {
1448 pp->p_offset = current_offset;
1449 seen_default_offset = 1;
1452 /* allow them to be out of order for old-style tables */
1453 if (pp->p_offset < current_offset &&
1454 seen_default_offset && i != RAW_PART) {
1456 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1457 (long)pp->p_offset,i+'a',current_offset);
1459 "Labels with any *'s for offset must be in ascending order by sector\n");
1461 } else if (pp->p_offset != current_offset &&
1462 i != RAW_PART && seen_default_offset) {
1464 * this may give unneeded warnings if
1465 * partitions are out-of-order
1468 "Offset %ld for partition %c doesn't match expected value %ld",
1469 (long)pp->p_offset, i + 'a', current_offset);
1473 current_offset = pp->p_offset + pp->p_size;
1477 for (i = 0; i < lp->d_npartitions; i++) {
1479 pp = &lp->d_partitions[i];
1480 if (pp->p_size == 0 && pp->p_offset != 0)
1481 Warning("partition %c: size 0, but offset %lu",
1482 part, (u_long)pp->p_offset);
1484 /* See comment in writelabel(). */
1485 if (pp->p_offset % lp->d_secpercyl != 0) {
1486 fprintf(stderr, "partition %c: does not start on a "
1487 "cylinder boundary!\n", part);
1492 if (pp->p_size % lp->d_secpercyl)
1493 Warning("partition %c: size %% cylinder-size != 0",
1495 if (pp->p_offset % lp->d_secpercyl)
1496 Warning("partition %c: offset %% cylinder-size != 0",
1499 if (pp->p_offset > lp->d_secperunit) {
1501 "partition %c: offset past end of unit\n", part);
1504 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1506 "partition %c: partition extends past end of unit\n",
1512 if (pp->p_fstype != FS_UNUSED)
1513 Warning("partition %c is not marked as unused!",part);
1514 if (pp->p_offset != 0)
1515 Warning("partition %c doesn't start at 0!",part);
1516 if (pp->p_size != lp->d_secperunit)
1517 Warning("partition %c doesn't cover the whole unit!",part);
1519 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1520 (pp->p_size != lp->d_secperunit)) {
1521 Warning("An incorrect partition %c may cause problems for "
1522 "standard system utilities",part);
1526 /* check for overlaps */
1527 /* this will check for all possible overlaps once and only once */
1528 for (j = 0; j < i; j++) {
1529 if (j != RAW_PART && i != RAW_PART &&
1530 part_set[i] && part_set[j]) {
1531 pp2 = &lp->d_partitions[j];
1532 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1533 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1534 pp2->p_offset >= pp->p_offset)) {
1535 fprintf(stderr,"partitions %c and %c overlap!\n",
1542 for (; i < MAXPARTITIONS; i++) {
1544 pp = &lp->d_partitions[i];
1545 if (pp->p_size || pp->p_offset)
1546 Warning("unused partition %c: size %d offset %lu",
1547 'a' + i, pp->p_size, (u_long)pp->p_offset);
1553 * When operating on a "virgin" disk, try getting an initial label
1554 * from the associated device driver. This might work for all device
1555 * drivers that are able to fetch some initial device parameters
1556 * without even having access to a (BSD) disklabel, like SCSI disks,
1557 * most IDE drives, or vn devices.
1559 * The device name must be given in its "canonical" form.
1562 getvirginlabel(void)
1564 static struct disklabel lab;
1565 char namebuf[BBSIZE];
1568 if (dkname[0] == '/') {
1569 warnx("\"auto\" requires the usage of a canonical disk name");
1572 (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1573 if ((f = open(namebuf, O_RDONLY)) == -1) {
1574 warn("cannot open %s", namebuf);
1579 * Try to use the new get-virgin-label ioctl. If it fails,
1580 * fallback to the old get-disdk-info ioctl.
1582 if (ioctl(f, DIOCGDVIRGIN, &lab) == 0)
1584 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1587 (void)snprintf(namebuf, BBSIZE, "%s%s%c", _PATH_DEV, dkname,
1589 if ((f = open(namebuf, O_RDONLY)) == -1) {
1590 warn("cannot open %s", namebuf);
1593 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1596 warn("No virgin disklabel found %s", namebuf);
1604 * If we are installing a boot program that doesn't fit in d_bbsize
1605 * we need to mark those partitions that the boot overflows into.
1606 * This allows newfs to prevent creation of a filesystem where it might
1607 * clobber bootstrap code.
1610 setbootflag(struct disklabel *lp)
1612 struct partition *pp;
1619 boffset = bootsize / lp->d_secsize;
1620 for (i = 0; i < lp->d_npartitions; i++) {
1622 pp = &lp->d_partitions[i];
1623 if (pp->p_size == 0)
1625 if (boffset <= pp->p_offset) {
1626 if (pp->p_fstype == FS_BOOT)
1627 pp->p_fstype = FS_UNUSED;
1628 } else if (pp->p_fstype != FS_BOOT) {
1629 if (pp->p_fstype != FS_UNUSED) {
1631 "boot overlaps used partition %c\n",
1635 pp->p_fstype = FS_BOOT;
1636 Warning("boot overlaps partition %c, %s",
1637 part, "marked as FS_BOOT");
1642 errx(4, "cannot install boot program");
1647 Warning(const char *fmt, ...)
1651 fprintf(stderr, "Warning, ");
1653 vfprintf(stderr, fmt, ap);
1654 fprintf(stderr, "\n");
1662 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",
1663 "usage: disklabel [-r] disk",
1664 "\t\t(to read label)",
1665 " disklabel -w [-r] [-n] disk type [ packid ]",
1666 "\t\t(to write label with existing boot program)",
1667 " disklabel -e [-r] [-n] disk",
1668 "\t\t(to edit label)",
1669 " disklabel -R [-r] [-n] disk protofile",
1670 "\t\t(to restore label with existing boot program)",
1672 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1673 "\t\t(to install boot program with existing label)",
1674 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1675 "\t\t(to write label and boot program)",
1676 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1677 "\t\t(to restore label and boot program)",
1679 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1680 "\t\t(to install boot program with existing on-disk label)",
1681 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1682 "\t\t(to write label and install boot program)",
1683 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1684 "\t\t(to restore label and install boot program)",
1686 " disklabel [-NW] disk",
1687 "\t\t(to write disable/enable label)");
1689 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1690 "usage: disklabel [-r] disk", "(to read label)",
1691 " disklabel -w [-r] [-n] disk type [ packid ]",
1692 "\t\t(to write label)",
1693 " disklabel -e [-r] [-n] disk",
1694 "\t\t(to edit label)",
1695 " disklabel -R [-r] [-n] disk protofile",
1696 "\t\t(to restore label)",
1697 " disklabel [-NW] disk",
1698 "\t\t(to write disable/enable label)");