2 * Copyright (c) 1994, 1995 Gordon W. Ross
3 * Copyright (c) 1994 Theo de Raadt
5 * Copyright (c) 1987, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Symmetric Computer Systems.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * This product includes software developed by Theo de Raadt.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $
44 static const char copyright[] =
45 "@(#) Copyright (c) 1987, 1993\n\
46 The Regents of the University of California. All rights reserved.\n";
51 static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94";
52 /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
54 static const char rcsid[] =
58 #include <sys/param.h>
63 #include <sys/disklabel.h>
65 #include <sys/sun_disklabel.h>
67 #include <ufs/ffs/fs.h>
77 #include "pathnames.h"
80 * Disklabel: read and write disklabels.
81 * The label is usually placed on one of the first sectors of the disk.
82 * Many machines also place a bootstrap in the same area,
83 * in which case the label is embedded in the bootstrap.
84 * The bootstrap source must leave space at the proper offset
85 * for the label on such machines.
89 #define BBSIZE 8192 /* size of boot area, with label */
92 /* FIX! These are too low, but are traditional */
93 #define DEFAULT_NEWFS_BLOCK 8192U
94 #define DEFAULT_NEWFS_FRAG 1024U
95 #define DEFAULT_NEWFS_CPG 16U
97 #define BIG_NEWFS_BLOCK 16384U
98 #define BIG_NEWFS_FRAG 4096U
99 #define BIG_NEWFS_CPG 64U
101 #if defined(__i386__) || defined(__ia64__)
103 #elif defined(__alpha__) || defined(__sparc64__)
106 #error I do not know about this architecture.
109 void makelabel __P((char *, char *, struct disklabel *));
110 int writelabel __P((int, char *, struct disklabel *));
111 void l_perror __P((char *));
112 struct disklabel * readlabel __P((int));
113 struct disklabel * makebootarea __P((char *, struct disklabel *, int));
114 void display __P((FILE *, struct disklabel *));
115 int edit __P((struct disklabel *, int));
116 int editit __P((void));
117 char * skip __P((char *));
118 char * word __P((char *));
119 int getasciilabel __P((FILE *, struct disklabel *));
120 int checklabel __P((struct disklabel *));
121 void setbootflag __P((struct disklabel *));
122 void Warning (char *, ...) __printflike(1, 2);
123 void usage __P((void));
124 struct disklabel * getvirginlabel __P((void));
126 #define DEFEDITOR _PATH_VI
127 #define streq(a,b) (strcmp(a,b) == 0)
131 char tmpfil[] = PATH_TMPFILE;
133 char namebuf[BBSIZE], *np = namebuf;
134 struct disklabel lab;
135 char bootarea[BBSIZE];
137 /* partition 'c' is the full disk and is special */
138 #define FULL_DISK_PART 2
139 #define MAX_PART ('z')
140 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
141 char part_size_type[MAX_NUM_PARTS];
142 char part_offset_type[MAX_NUM_PARTS];
143 int part_set[MAX_NUM_PARTS];
146 int installboot; /* non-zero if we should install a boot program */
147 char *bootbuf; /* pointer to buffer with remainder of boot prog */
148 int bootsize; /* size of remaining boot program */
149 char *xxboot; /* primary boot */
150 char *bootxx; /* secondary boot */
151 char boot0[MAXPATHLEN];
152 char boot1[MAXPATHLEN];
156 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
160 int disable_write; /* set to disable writing to disk label */
164 #define OPTIONS "BNRWb:denrs:w"
166 #define OPTIONS "BNRWb:enrs:w"
174 register struct disklabel *lp;
176 int ch, f = 0, flag, error = 0;
179 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
254 if (dkname[0] != '/') {
255 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
257 np += strlen(specname) + 1;
260 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
261 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
262 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
263 np = namebuf + strlen(specname) + 1;
264 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
267 err(4, "%s", specname);
283 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
284 err(4, "ioctl DIOCWLABEL");
292 error = checklabel(lp);
297 if (installboot && argc == 3) {
298 makelabel(argv[2], 0, &lab);
302 * We only called makelabel() for its side effect
303 * of setting the bootstrap file names. Discard
304 * all changes to `lab' so that all values in the
305 * final label come from the ASCII label.
307 bzero((char *)&lab, sizeof(lab));
312 if (!(t = fopen(argv[1], "r")))
313 err(4, "%s", argv[1]);
314 if (!getasciilabel(t, &lab))
316 lp = makebootarea(bootarea, &lab, f);
318 error = writelabel(f, bootarea, lp);
328 makelabel(argv[1], name, &lab);
329 lp = makebootarea(bootarea, &lab, f);
331 if (checklabel(lp) == 0)
332 error = writelabel(f, bootarea, lp);
337 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
338 err(4, "ioctl DIOCWLABEL");
344 struct disklabel tlab;
349 makelabel(argv[1], 0, &lab);
350 lp = makebootarea(bootarea, &lab, f);
352 if (checklabel(lp) == 0)
353 error = writelabel(f, bootarea, lp);
362 * Construct a prototype disklabel from /etc/disktab. As a side
363 * effect, set the names of the primary and secondary boot files
367 makelabel(type, name, lp)
369 register struct disklabel *lp;
371 register struct disklabel *dp;
373 if (strcmp(type, "auto") == 0)
374 dp = getvirginlabel();
376 dp = getdiskbyname(type);
378 errx(1, "%s: unknown disk type", type);
382 * Set bootstrap name(s).
383 * 1. If set from command line, use those,
384 * 2. otherwise, check if disktab specifies them (b0 or b1),
385 * 3. otherwise, makebootarea() will choose ones based on the name
386 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
388 if (!xxboot && lp->d_boot0) {
389 if (*lp->d_boot0 != '/')
390 (void)sprintf(boot0, "%s/%s",
391 _PATH_BOOTDIR, lp->d_boot0);
393 (void)strcpy(boot0, lp->d_boot0);
397 if (!bootxx && lp->d_boot1) {
398 if (*lp->d_boot1 != '/')
399 (void)sprintf(boot1, "%s/%s",
400 _PATH_BOOTDIR, lp->d_boot1);
402 (void)strcpy(boot1, lp->d_boot1);
407 /* d_packname is union d_boot[01], so zero */
408 bzero(lp->d_packname, sizeof(lp->d_packname));
410 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
414 writelabel(f, boot, lp)
417 register struct disklabel *lp;
425 struct sun_disklabel *sl;
426 u_short cksum, *sp1, *sp2;
427 struct partition *npp;
428 struct sun_dkpart *spp;
433 Warning("write to disk label supressed - label was as follows:");
438 lp->d_magic = DISKMAGIC;
439 lp->d_magic2 = DISKMAGIC;
441 lp->d_checksum = dkcksum(lp);
444 * First set the kernel disk label,
445 * then write a label to the raw disk.
446 * If the SDINFO ioctl fails because it is unimplemented,
447 * keep going; otherwise, the kernel consistency checks
448 * may prevent us from changing the current (in-core)
451 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
452 errno != ENODEV && errno != ENOTTY) {
453 l_perror("ioctl DIOCSDINFO");
456 (void)lseek(f, (off_t)0, SEEK_SET);
460 * Generate the bootblock checksum for the SRM console.
462 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
468 * Generate a Sun disklabel around the BSD label for
469 * PROM compatability.
471 sl = (struct sun_disklabel *)boot;
472 memcpy(sl->sl_text, lp->d_packname, sizeof(lp->d_packname));
473 sl->sl_rpm = lp->d_rpm;
474 sl->sl_pcylinders = lp->d_ncylinders +
475 lp->d_acylinders; /* XXX */
476 sl->sl_sparespercyl = lp->d_sparespercyl;
477 sl->sl_interleave = lp->d_interleave;
478 sl->sl_ncylinders = lp->d_ncylinders;
479 sl->sl_acylinders = lp->d_acylinders;
480 sl->sl_ntracks = lp->d_ntracks;
481 sl->sl_nsectors = lp->d_nsectors;
482 sl->sl_magic = SUN_DKMAGIC;
483 secpercyl = sl->sl_nsectors * sl->sl_ntracks;
484 for (i = 0; i < 8; i++) {
485 spp = &sl->sl_part[i];
486 npp = &lp->d_partitions[i];
488 * SunOS partitions must start on a cylinder
489 * boundary. Note this restriction is forced
490 * upon FreeBSD/sparc64 labels too, since we
491 * want to keep both labels synchronised.
493 spp->sdkp_cyloffset = npp->p_offset / secpercyl;
494 spp->sdkp_nsectors = npp->p_size;
497 /* Compute the XOR checksum. */
499 sp2 = (u_short *)(sl + 1);
500 sl->sl_cksum = cksum = 0;
503 sl->sl_cksum = cksum;
506 * write enable label sector before write (if necessary),
507 * disable after writing.
510 if (ioctl(f, DIOCWLABEL, &flag) < 0)
511 warn("ioctl DIOCWLABEL");
512 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
518 * Output the remainder of the disklabel
520 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
526 (void) ioctl(f, DIOCWLABEL, &flag);
527 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
528 l_perror("ioctl DIOCWDINFO");
542 warnx("%s: no disk label on disk;", s);
543 fprintf(stderr, "add \"-r\" to install initial label\n");
547 warnx("%s: label magic number or checksum is wrong!", s);
548 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
552 warnx("%s: open partition would move or shrink", s);
556 warnx("%s: '%c' partition must start at beginning of disk",
567 * Fetch disklabel for disk.
568 * Use ioctl to get label unless -r flag is given.
574 register struct disklabel *lp;
577 if (read(f, bootarea, BBSIZE) < BBSIZE)
578 err(4, "%s", specname);
579 for (lp = (struct disklabel *)bootarea;
580 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
581 lp = (struct disklabel *)((char *)lp + 16))
582 if (lp->d_magic == DISKMAGIC &&
583 lp->d_magic2 == DISKMAGIC)
585 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
586 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
589 "bad pack magic number (label is damaged, or pack is unlabeled)");
592 if (ioctl(f, DIOCGDINFO, lp) < 0)
593 err(4, "ioctl DIOCGDINFO");
599 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
600 * Returns a pointer to the disklabel portion of the bootarea.
603 makebootarea(boot, dp, f)
605 register struct disklabel *dp;
608 struct disklabel *lp;
625 if (dp->d_secsize == 0) {
626 dp->d_secsize = DEV_BSIZE;
627 dp->d_bbsize = BBSIZE;
629 lp = (struct disklabel *)
630 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
631 bzero((char *)lp, sizeof *lp);
634 * If we are not installing a boot program but we are installing a
635 * label on disk then we must read the current bootarea so we don't
636 * clobber the existing boot.
640 if (read(f, boot, BBSIZE) < BBSIZE)
641 err(4, "%s", specname);
642 bzero((char *)lp, sizeof *lp);
647 * We are installing a boot program. Determine the name(s) and
648 * read them into the appropriate places in the boot area.
650 if (!xxboot || !bootxx) {
652 if ((p = rindex(dkname, '/')) == NULL)
656 while (*p && !isdigit(*p))
661 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
666 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
673 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
674 xxboot, bootxx ? bootxx : "NONE");
679 * 1. One-piece bootstrap (hp300/hp800)
680 * 1. One-piece bootstrap (alpha/sparc64)
681 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
682 * is remembered and written later following the bootarea.
683 * 2. Two-piece bootstraps (i386/ia64)
684 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
685 * bytes of bootarea, remaining d_bbsize-d_secsize filled
688 b = open(xxboot, O_RDONLY);
690 err(4, "%s", xxboot);
695 * The i386 has the so-called fdisk table embedded into the
696 * primary bootstrap. We take care to not clobber it, but
697 * only if it does already contain some data. (Otherwise,
698 * the xxboot provides a template.)
700 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
701 err(4, "%s", xxboot);
702 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
704 if (read(b, boot, (int)dp->d_secsize) < 0)
705 err(4, "%s", xxboot);
708 for (i = DOSPARTOFF, found = 0;
709 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
711 found = tmpbuf[i] != 0;
713 memcpy((void *)&boot[DOSPARTOFF],
714 (void *)&tmpbuf[DOSPARTOFF],
715 NDOSPART * sizeof(struct dos_partition));
718 b = open(bootxx, O_RDONLY);
720 err(4, "%s", bootxx);
721 if (fstat(b, &sb) != 0)
722 err(4, "%s", bootxx);
723 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
724 errx(4, "%s too large", bootxx);
725 if (read(b, &boot[dp->d_secsize],
726 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
727 err(4, "%s", bootxx);
728 #else /* !(NUMBOOT > 1) */
731 * On the alpha, the primary bootstrap starts at the
732 * second sector of the boot area. The first sector
733 * contains the label and must be edited to contain the
734 * size and location of the primary bootstrap.
736 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
738 err(4, "%s", xxboot);
739 bootinfo = (u_long *)(boot + 480);
740 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
741 bootinfo[1] = 1; /* start at sector 1 */
742 bootinfo[2] = 0; /* flags (must be zero) */
743 #else /* !__alpha__ */
744 if (read(b, boot, (int)dp->d_bbsize) < 0)
745 err(4, "%s", xxboot);
746 #endif /* __alpha__ */
747 if (fstat(b, &sb) != 0)
748 err(4, "%s", xxboot);
749 bootsize = (int)sb.st_size - dp->d_bbsize;
751 /* XXX assume d_secsize is a power of two */
752 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
753 bootbuf = (char *)malloc((size_t)bootsize);
755 err(4, "%s", xxboot);
756 if (read(b, bootbuf, bootsize) < 0) {
758 err(4, "%s", xxboot);
761 #endif /* NUMBOOT > 1 */
763 #endif /* NUMBOOT > 0 */
765 * Make sure no part of the bootstrap is written in the area
766 * reserved for the label.
768 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
770 errx(2, "bootstrap doesn't leave room for disk label");
777 register struct disklabel *lp;
780 register struct partition *pp;
782 fprintf(f, "# %s:\n", specname);
783 if ((unsigned) lp->d_type < DKMAXTYPES)
784 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
786 fprintf(f, "type: %u\n", lp->d_type);
787 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
789 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
791 fprintf(f, "flags:");
792 if (lp->d_flags & D_REMOVABLE)
793 fprintf(f, " removeable");
794 if (lp->d_flags & D_ECC)
796 if (lp->d_flags & D_BADSECT)
797 fprintf(f, " badsect");
799 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
800 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
801 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
802 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
803 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
804 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
805 fprintf(f, "rpm: %u\n", lp->d_rpm);
806 fprintf(f, "interleave: %u\n", lp->d_interleave);
807 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
808 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
809 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
810 (u_long)lp->d_headswitch);
811 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
812 (u_long)lp->d_trkseek);
813 fprintf(f, "drivedata: ");
814 for (i = NDDATA - 1; i >= 0; i--)
815 if (lp->d_drivedata[i])
819 for (j = 0; j <= i; j++)
820 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
821 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
823 "# size offset fstype [fsize bsize bps/cpg]\n");
824 pp = lp->d_partitions;
825 for (i = 0; i < lp->d_npartitions; i++, pp++) {
827 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
828 (u_long)pp->p_size, (u_long)pp->p_offset);
829 if ((unsigned) pp->p_fstype < FSMAXTYPES)
830 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
832 fprintf(f, "%8d", pp->p_fstype);
833 switch (pp->p_fstype) {
835 case FS_UNUSED: /* XXX */
836 fprintf(f, " %5lu %5lu %5.5s ",
838 (u_long)(pp->p_fsize * pp->p_frag), "");
842 fprintf(f, " %5lu %5lu %5u ",
844 (u_long)(pp->p_fsize * pp->p_frag),
849 fprintf(f, " %5lu %5lu %5d",
851 (u_long)(pp->p_fsize * pp->p_frag),
856 fprintf(f, "%20.20s", "");
859 fprintf(f, "\t# (Cyl. %4lu",
860 (u_long)(pp->p_offset / lp->d_secpercyl));
861 if (pp->p_offset % lp->d_secpercyl)
866 (u_long)((pp->p_offset + pp->p_size +
867 lp->d_secpercyl - 1) /
868 lp->d_secpercyl - 1));
869 if (pp->p_size % lp->d_secpercyl)
879 struct disklabel *lp;
883 struct disklabel label;
886 if ((fd = mkstemp(tmpfil)) == -1 ||
887 (fp = fdopen(fd, "w")) == NULL) {
888 warnx("can't create %s", tmpfil);
896 fp = fopen(tmpfil, "r");
898 warnx("can't reopen %s for reading", tmpfil);
901 bzero((char *)&label, sizeof(label));
902 if (getasciilabel(fp, &label)) {
904 if (writelabel(f, bootarea, lp) == 0) {
906 (void) unlink(tmpfil);
911 printf("re-edit the label? [y]: "); fflush(stdout);
913 if (c != EOF && c != (int)'\n')
914 while (getchar() != (int)'\n')
919 (void) unlink(tmpfil);
926 register int pid, xpid;
929 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
930 while ((pid = fork()) < 0) {
931 if (errno == EPROCLIM) {
932 warnx("you have too many processes");
935 if (errno != EAGAIN) {
947 if ((ed = getenv("EDITOR")) == (char *)0)
949 execlp(ed, ed, tmpfil, (char *)0);
952 while ((xpid = wait(&stat)) >= 0)
964 while (*cp != '\0' && isspace(*cp))
966 if (*cp == '\0' || *cp == '#')
967 return ((char *)NULL);
977 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
979 if ((c = *cp) != '\0') {
984 return ((char *)NULL);
988 * Read an ascii label in from fd f,
989 * in the same format as that put out by display(),
995 register struct disklabel *lp;
997 register char **cpp, *cp;
998 register struct partition *pp;
1000 char *tp, *s, line[BUFSIZ];
1001 int v, lineno = 0, errors = 0;
1003 lp->d_bbsize = BBSIZE; /* XXX */
1004 lp->d_sbsize = SBSIZE; /* XXX */
1005 while (fgets(line, sizeof(line) - 1, f)) {
1007 if ((cp = index(line,'\n')) != 0)
1012 tp = index(cp, ':');
1014 fprintf(stderr, "line %d: syntax error\n", lineno);
1018 *tp++ = '\0', tp = skip(tp);
1019 if (streq(cp, "type")) {
1023 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
1024 if ((s = *cpp) && streq(s, tp)) {
1025 lp->d_type = cpp - dktypenames;
1029 if ((unsigned)v >= DKMAXTYPES)
1030 fprintf(stderr, "line %d:%s %d\n", lineno,
1031 "Warning, unknown disk type", v);
1035 if (streq(cp, "flags")) {
1036 for (v = 0; (cp = tp) && *cp != '\0';) {
1038 if (streq(cp, "removeable"))
1040 else if (streq(cp, "ecc"))
1042 else if (streq(cp, "badsect"))
1046 "line %d: %s: bad flag\n",
1054 if (streq(cp, "drivedata")) {
1057 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
1058 lp->d_drivedata[i++] = atoi(cp);
1063 if (sscanf(cp, "%d partitions", &v) == 1) {
1064 if (v == 0 || (unsigned)v > MAXPARTITIONS) {
1066 "line %d: bad # of partitions\n", lineno);
1067 lp->d_npartitions = MAXPARTITIONS;
1070 lp->d_npartitions = v;
1075 if (streq(cp, "disk")) {
1076 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1079 if (streq(cp, "label")) {
1080 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1083 if (streq(cp, "bytes/sector")) {
1085 if (v <= 0 || (v % DEV_BSIZE) != 0) {
1087 "line %d: %s: bad sector size\n",
1094 if (streq(cp, "sectors/track")) {
1097 fprintf(stderr, "line %d: %s: bad %s\n",
1104 if (streq(cp, "sectors/cylinder")) {
1107 fprintf(stderr, "line %d: %s: bad %s\n",
1111 lp->d_secpercyl = v;
1114 if (streq(cp, "tracks/cylinder")) {
1117 fprintf(stderr, "line %d: %s: bad %s\n",
1124 if (streq(cp, "cylinders")) {
1127 fprintf(stderr, "line %d: %s: bad %s\n",
1131 lp->d_ncylinders = v;
1134 if (streq(cp, "sectors/unit")) {
1137 fprintf(stderr, "line %d: %s: bad %s\n",
1141 lp->d_secperunit = v;
1144 if (streq(cp, "rpm")) {
1147 fprintf(stderr, "line %d: %s: bad %s\n",
1154 if (streq(cp, "interleave")) {
1157 fprintf(stderr, "line %d: %s: bad %s\n",
1161 lp->d_interleave = v;
1164 if (streq(cp, "trackskew")) {
1167 fprintf(stderr, "line %d: %s: bad %s\n",
1171 lp->d_trackskew = v;
1174 if (streq(cp, "cylinderskew")) {
1177 fprintf(stderr, "line %d: %s: bad %s\n",
1184 if (streq(cp, "headswitch")) {
1187 fprintf(stderr, "line %d: %s: bad %s\n",
1191 lp->d_headswitch = v;
1194 if (streq(cp, "track-to-track seek")) {
1197 fprintf(stderr, "line %d: %s: bad %s\n",
1204 /* the ':' was removed above */
1205 if ('a' <= *cp && *cp <= MAX_PART && cp[1] == '\0') {
1207 if (part >= lp->d_npartitions) {
1209 "line %d: partition name out of range a-%c: %s\n",
1210 lineno, 'a' + lp->d_npartitions - 1, cp);
1214 pp = &lp->d_partitions[part];
1216 #define NXTNUM(n) { \
1218 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1222 cp = tp, tp = word(cp); \
1228 /* retain 1 character following number */
1229 #define NXTWORD(w,n) { \
1231 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1236 cp = tp, tp = word(cp); \
1239 (n) = strtol(cp,&tmp,10); \
1240 if (tmp) (w) = *tmp; \
1244 NXTWORD(part_size_type[part],v);
1245 if (v < 0 || (v == 0 && part_size_type[part] != '*')) {
1247 "line %d: %s: bad partition size\n",
1255 NXTWORD(part_offset_type[part],v);
1256 if (v < 0 || (v == 0 &&
1257 part_offset_type[part] != '*' &&
1258 part_offset_type[part] != '\0')) {
1260 "line %d: %s: bad partition offset\n",
1266 cp = tp, tp = word(cp);
1268 for (; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1269 if ((s = *cpp) && streq(s, cp)) {
1270 pp->p_fstype = cpp -
1278 if ((unsigned)v >= FSMAXTYPES) {
1280 "line %d: Warning, unknown "
1281 "filesystem type %s\n",
1288 * Note: NXTNUM will break us out of the
1291 switch (pp->p_fstype) {
1294 * allow us to accept defaults for
1298 NXTNUM(pp->p_fsize);
1299 if (pp->p_fsize == 0)
1302 pp->p_frag = v / pp->p_fsize;
1304 /* else default to 0's */
1307 /* These happen to be the same */
1311 NXTNUM(pp->p_fsize);
1312 if (pp->p_fsize == 0)
1315 pp->p_frag = v / pp->p_fsize;
1319 * FIX! poor attempt at
1323 if (pp->p_size < 1*1024*1024*1024/lp->d_secsize) {
1324 /* FIX! These are too low, but are traditional */
1325 pp->p_fsize = DEFAULT_NEWFS_BLOCK;
1326 pp->p_frag = (unsigned char) DEFAULT_NEWFS_FRAG;
1327 pp->p_cpg = DEFAULT_NEWFS_CPG;
1329 pp->p_fsize = BIG_NEWFS_BLOCK;
1330 pp->p_frag = (unsigned char) BIG_NEWFS_FRAG;
1331 pp->p_cpg = BIG_NEWFS_CPG;
1340 * note: we may not have
1341 * gotten all the entries for
1342 * the fs though if we didn't,
1343 * errors will be set.
1349 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1354 errors += checklabel(lp);
1355 return (errors == 0);
1359 * Check disklabel for errors and fill in
1360 * derived fields according to supplied values.
1364 register struct disklabel *lp;
1366 register struct partition *pp;
1369 unsigned long total_size, total_percent, current_offset;
1370 int seen_default_offset;
1373 struct partition *pp2;
1375 if (lp->d_secsize == 0) {
1376 fprintf(stderr, "sector size 0\n");
1379 if (lp->d_nsectors == 0) {
1380 fprintf(stderr, "sectors/track 0\n");
1383 if (lp->d_ntracks == 0) {
1384 fprintf(stderr, "tracks/cylinder 0\n");
1387 if (lp->d_ncylinders == 0) {
1388 fprintf(stderr, "cylinders/unit 0\n");
1392 Warning("revolutions/minute 0");
1393 if (lp->d_secpercyl == 0)
1394 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1395 if (lp->d_secperunit == 0)
1396 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1397 if (lp->d_bbsize == 0) {
1398 fprintf(stderr, "boot block size 0\n");
1400 } else if (lp->d_bbsize % lp->d_secsize)
1401 Warning("boot block size %% sector-size != 0");
1402 if (lp->d_sbsize == 0) {
1403 fprintf(stderr, "super block size 0\n");
1405 } else if (lp->d_sbsize % lp->d_secsize)
1406 Warning("super block size %% sector-size != 0");
1407 if (lp->d_npartitions > MAXPARTITIONS)
1408 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1409 (u_long)lp->d_npartitions, MAXPARTITIONS);
1411 /* first allocate space to the partitions, then offsets */
1412 total_size = 0; /* in sectors */
1413 total_percent = 0; /* in percent */
1415 /* find all fixed partitions */
1416 for (i = 0; i < lp->d_npartitions; i++) {
1417 pp = &lp->d_partitions[i];
1419 if (part_size_type[i] == '*') {
1420 /* partition 2 ('c') is special */
1421 if (i == FULL_DISK_PART) {
1422 pp->p_size = lp->d_secperunit;
1425 Warning("Too many '*' partitions (%c and %c)",
1426 hog_part + 'a',i + 'a');
1434 switch (part_size_type[i]) {
1436 total_percent += size;
1444 size *= 1024ULL * 1024ULL;
1448 size *= 1024ULL * 1024ULL * 1024ULL;
1453 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1456 /* don't count %'s yet */
1457 if (part_size_type[i] != '%') {
1459 * for all not in sectors, convert to
1462 if (part_size_type[i] != '\0') {
1463 if (size % lp->d_secsize != 0)
1464 Warning("partition %c not an integer number of sectors",
1466 size /= lp->d_secsize;
1469 /* else already in sectors */
1470 /* partition 2 ('c') is special */
1471 if (i != FULL_DISK_PART)
1477 /* handle % partitions - note %'s don't need to add up to 100! */
1478 if (total_percent != 0) {
1479 long free_space = lp->d_secperunit - total_size;
1480 if (total_percent > 100) {
1481 fprintf(stderr,"total percentage %d is greater than 100\n",
1486 if (free_space > 0) {
1487 for (i = 0; i < lp->d_npartitions; i++) {
1488 pp = &lp->d_partitions[i];
1489 if (part_set[i] && part_size_type[i] == '%') {
1490 /* careful of overflows! and integer roundoff */
1491 pp->p_size = ((double)pp->p_size/100) * free_space;
1492 total_size += pp->p_size;
1494 /* FIX we can lose a sector or so due to roundoff per
1495 partition. A more complex algorithm could avoid that */
1500 "%ld sectors available to give to '*' and '%' partitions\n",
1503 /* fix? set all % partitions to size 0? */
1506 /* give anything remaining to the hog partition */
1507 if (hog_part != -1) {
1508 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1509 total_size = lp->d_secperunit;
1512 /* Now set the offsets for each partition */
1513 current_offset = 0; /* in sectors */
1514 seen_default_offset = 0;
1515 for (i = 0; i < lp->d_npartitions; i++) {
1517 pp = &lp->d_partitions[i];
1519 if (part_offset_type[i] == '*') {
1520 /* partition 2 ('c') is special */
1521 if (i == FULL_DISK_PART) {
1524 pp->p_offset = current_offset;
1525 seen_default_offset = 1;
1528 /* allow them to be out of order for old-style tables */
1529 /* partition 2 ('c') is special */
1530 if (pp->p_offset < current_offset &&
1531 seen_default_offset && i != FULL_DISK_PART) {
1533 "Offset %ld for partition %c overlaps previous partition which ends at %ld\n",
1534 pp->p_offset,i+'a',current_offset);
1536 "Labels with any *'s for offset must be in ascending order by sector\n");
1538 } else if (pp->p_offset != current_offset &&
1539 i != FULL_DISK_PART && seen_default_offset) {
1541 * this may give unneeded warnings if
1542 * partitions are out-of-order
1545 "Offset %ld for partition %c doesn't match expected value %ld",
1546 pp->p_offset, i + 'a', current_offset);
1549 /* partition 2 ('c') is special */
1550 if (i != FULL_DISK_PART)
1551 current_offset = pp->p_offset + pp->p_size;
1555 for (i = 0; i < lp->d_npartitions; i++) {
1557 pp = &lp->d_partitions[i];
1558 if (pp->p_size == 0 && pp->p_offset != 0)
1559 Warning("partition %c: size 0, but offset %lu",
1560 part, (u_long)pp->p_offset);
1562 /* See comment in writelabel(). */
1563 if (pp->p_offset % lp->d_secpercyl != 0) {
1564 fprintf(stderr, "partition %c: does not start on a "
1565 "cylinder boundary!\n", part);
1570 if (pp->p_size % lp->d_secpercyl)
1571 Warning("partition %c: size %% cylinder-size != 0",
1573 if (pp->p_offset % lp->d_secpercyl)
1574 Warning("partition %c: offset %% cylinder-size != 0",
1577 if (pp->p_offset > lp->d_secperunit) {
1579 "partition %c: offset past end of unit\n", part);
1582 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1584 "partition %c: partition extends past end of unit\n",
1588 if (i == FULL_DISK_PART)
1590 if (pp->p_fstype != FS_UNUSED)
1591 Warning("partition %c is not marked as unused!",part);
1592 if (pp->p_offset != 0)
1593 Warning("partition %c doesn't start at 0!",part);
1594 if (pp->p_size != lp->d_secperunit)
1595 Warning("partition %c doesn't cover the whole unit!",part);
1597 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1598 (pp->p_size != lp->d_secperunit)) {
1599 Warning("An incorrect partition %c may cause problems for "
1600 "standard system utilities",part);
1604 /* check for overlaps */
1605 /* this will check for all possible overlaps once and only once */
1606 for (j = 0; j < i; j++) {
1607 /* partition 2 ('c') is special */
1608 if (j != FULL_DISK_PART && i != FULL_DISK_PART &&
1609 part_set[i] && part_set[j]) {
1610 pp2 = &lp->d_partitions[j];
1611 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1612 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1613 pp2->p_offset >= pp->p_offset)) {
1614 fprintf(stderr,"partitions %c and %c overlap!\n",
1621 for (; i < MAXPARTITIONS; i++) {
1623 pp = &lp->d_partitions[i];
1624 if (pp->p_size || pp->p_offset)
1625 Warning("unused partition %c: size %d offset %lu",
1626 'a' + i, pp->p_size, (u_long)pp->p_offset);
1632 * When operating on a "virgin" disk, try getting an initial label
1633 * from the associated device driver. This might work for all device
1634 * drivers that are able to fetch some initial device parameters
1635 * without even having access to a (BSD) disklabel, like SCSI disks,
1636 * most IDE drives, or vn devices.
1638 * The device name must be given in its "canonical" form.
1641 getvirginlabel(void)
1643 static struct disklabel lab;
1644 char namebuf[BBSIZE];
1647 if (dkname[0] == '/') {
1648 warnx("\"auto\" requires the usage of a canonical disk name");
1651 (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1652 if ((f = open(namebuf, O_RDONLY)) == -1) {
1653 warn("cannot open %s", namebuf);
1658 * Try to use the new get-virgin-label ioctl. If it fails,
1659 * fallback to the old get-disdk-info ioctl.
1661 if (ioctl(f, DIOCGDVIRGIN, &lab) == 0)
1663 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1666 (void)snprintf(namebuf, BBSIZE, "%s%s%c", _PATH_DEV, dkname,
1668 if ((f = open(namebuf, O_RDONLY)) == -1) {
1669 warn("cannot open %s", namebuf);
1672 if (ioctl(f, DIOCGDINFO, &lab) == 0)
1675 warn("No virgin disklabel found %s", namebuf);
1685 * If we are installing a boot program that doesn't fit in d_bbsize
1686 * we need to mark those partitions that the boot overflows into.
1687 * This allows newfs to prevent creation of a filesystem where it might
1688 * clobber bootstrap code.
1692 register struct disklabel *lp;
1694 register struct partition *pp;
1701 boffset = bootsize / lp->d_secsize;
1702 for (i = 0; i < lp->d_npartitions; i++) {
1704 pp = &lp->d_partitions[i];
1705 if (pp->p_size == 0)
1707 if (boffset <= pp->p_offset) {
1708 if (pp->p_fstype == FS_BOOT)
1709 pp->p_fstype = FS_UNUSED;
1710 } else if (pp->p_fstype != FS_BOOT) {
1711 if (pp->p_fstype != FS_UNUSED) {
1713 "boot overlaps used partition %c\n",
1717 pp->p_fstype = FS_BOOT;
1718 Warning("boot overlaps partition %c, %s",
1719 part, "marked as FS_BOOT");
1724 errx(4, "cannot install boot program");
1729 Warning(char *fmt, ...)
1733 fprintf(stderr, "Warning, ");
1735 vfprintf(stderr, fmt, ap);
1736 fprintf(stderr, "\n");
1744 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",
1745 "usage: disklabel [-r] disk",
1746 "\t\t(to read label)",
1747 " disklabel -w [-r] [-n] disk type [ packid ]",
1748 "\t\t(to write label with existing boot program)",
1749 " disklabel -e [-r] [-n] disk",
1750 "\t\t(to edit label)",
1751 " disklabel -R [-r] [-n] disk protofile",
1752 "\t\t(to restore label with existing boot program)",
1754 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1755 "\t\t(to install boot program with existing label)",
1756 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1757 "\t\t(to write label and boot program)",
1758 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1759 "\t\t(to restore label and boot program)",
1761 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1762 "\t\t(to install boot program with existing on-disk label)",
1763 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1764 "\t\t(to write label and install boot program)",
1765 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1766 "\t\t(to restore label and install boot program)",
1768 " disklabel [-NW] disk",
1769 "\t\t(to write disable/enable label)");
1771 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1772 "usage: disklabel [-r] disk", "(to read label)",
1773 " disklabel -w [-r] [-n] disk type [ packid ]",
1774 "\t\t(to write label)",
1775 " disklabel -e [-r] [-n] disk",
1776 "\t\t(to edit label)",
1777 " disklabel -R [-r] [-n] disk protofile",
1778 "\t\t(to restore label)",
1779 " disklabel [-NW] disk",
1780 "\t\t(to write disable/enable label)");