2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Symmetric Computer Systems.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)disklabel.c 1.2 (Symmetric) 11/28/85
38 * @(#)disklabel.c 8.2 (Berkeley) 1/7/94
39 * $FreeBSD: src/sbin/disklabel/disklabel.c,v 1.28.2.15 2003/01/24 16:18:16 des Exp $
40 * $DragonFly: src/sbin/disklabel/disklabel.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
43 #include <sys/param.h>
48 #include <sys/disklabel.h>
49 #include <ufs/ffs/fs.h>
59 #include "pathnames.h"
62 * Disklabel: read and write disklabels.
63 * The label is usually placed on one of the first sectors of the disk.
64 * Many machines also place a bootstrap in the same area,
65 * in which case the label is embedded in the bootstrap.
66 * The bootstrap source must leave space at the proper offset
67 * for the label on such machines.
71 #define BBSIZE 8192 /* size of boot area, with label */
74 /* FIX! These are too low, but are traditional */
75 #define DEFAULT_NEWFS_BLOCK 8192U
76 #define DEFAULT_NEWFS_FRAG 1024U
77 #define DEFAULT_NEWFS_CPG 16U
79 #define BIG_NEWFS_BLOCK 16384U
80 #define BIG_NEWFS_FRAG 2048U
81 #define BIG_NEWFS_CPG 64U
86 #if defined(__alpha__) || defined(hp300) || defined(hp800)
93 void makelabel(const char *, const char *, struct disklabel *);
94 int writelabel(int, const char *, struct disklabel *);
95 void l_perror(const char *);
96 struct disklabel *readlabel(int);
97 struct disklabel *makebootarea(char *, struct disklabel *, int);
98 void display(FILE *, const struct disklabel *);
99 int edit(struct disklabel *, int);
103 int getasciilabel(FILE *, struct disklabel *);
104 int getasciipartspec(char *, struct disklabel *, int, int);
105 int checklabel(struct disklabel *);
106 void setbootflag(struct disklabel *);
107 void Warning(const char *, ...) __printflike(1, 2);
109 struct disklabel *getvirginlabel(void);
111 #define DEFEDITOR _PATH_VI
112 #define streq(a,b) (strcmp(a,b) == 0)
116 char tmpfil[] = PATH_TMPFILE;
118 char namebuf[BBSIZE], *np = namebuf;
119 struct disklabel lab;
120 char bootarea[BBSIZE];
122 #define MAX_PART ('z')
123 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
124 char part_size_type[MAX_NUM_PARTS];
125 char part_offset_type[MAX_NUM_PARTS];
126 int part_set[MAX_NUM_PARTS];
129 int installboot; /* non-zero if we should install a boot program */
130 char *bootbuf; /* pointer to buffer with remainder of boot prog */
131 int bootsize; /* size of remaining boot program */
132 char *xxboot; /* primary boot */
133 char *bootxx; /* secondary boot */
134 char boot0[MAXPATHLEN];
135 char boot1[MAXPATHLEN];
139 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
143 int disable_write; /* set to disable writing to disk label */
147 #define OPTIONS "BNRWb:denrs:w"
149 #define OPTIONS "BNRWb:enrs:w"
153 main(int argc, char *argv[])
155 struct disklabel *lp;
157 int ch, f = 0, flag, error = 0;
160 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
235 if (dkname[0] != '/') {
236 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
238 np += strlen(specname) + 1;
241 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
242 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
243 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
244 np = namebuf + strlen(specname) + 1;
245 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
248 err(4, "%s", specname);
264 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
265 err(4, "ioctl DIOCWLABEL");
273 error = checklabel(lp);
278 if (installboot && argc == 3) {
279 makelabel(argv[2], 0, &lab);
283 * We only called makelabel() for its side effect
284 * of setting the bootstrap file names. Discard
285 * all changes to `lab' so that all values in the
286 * final label come from the ASCII label.
288 bzero((char *)&lab, sizeof(lab));
293 if (!(t = fopen(argv[1], "r")))
294 err(4, "%s", argv[1]);
295 if (!getasciilabel(t, &lab))
297 lp = makebootarea(bootarea, &lab, f);
299 error = writelabel(f, bootarea, lp);
309 makelabel(argv[1], name, &lab);
310 lp = makebootarea(bootarea, &lab, f);
312 if (checklabel(lp) == 0)
313 error = writelabel(f, bootarea, lp);
318 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
319 err(4, "ioctl DIOCWLABEL");
325 struct disklabel tlab;
330 makelabel(argv[1], 0, &lab);
331 lp = makebootarea(bootarea, &lab, f);
333 if (checklabel(lp) == 0)
334 error = writelabel(f, bootarea, lp);
343 * Construct a prototype disklabel from /etc/disktab. As a side
344 * effect, set the names of the primary and secondary boot files
348 makelabel(const char *type, const char *name, struct disklabel *lp)
350 struct disklabel *dp;
352 if (strcmp(type, "auto") == 0)
353 dp = getvirginlabel();
355 dp = getdiskbyname(type);
357 errx(1, "%s: unknown disk type", type);
361 * Set bootstrap name(s).
362 * 1. If set from command line, use those,
363 * 2. otherwise, check if disktab specifies them (b0 or b1),
364 * 3. otherwise, makebootarea() will choose ones based on the name
365 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
367 if (!xxboot && lp->d_boot0) {
368 if (*lp->d_boot0 != '/')
369 (void)sprintf(boot0, "%s/%s",
370 _PATH_BOOTDIR, lp->d_boot0);
372 (void)strcpy(boot0, lp->d_boot0);
376 if (!bootxx && lp->d_boot1) {
377 if (*lp->d_boot1 != '/')
378 (void)sprintf(boot1, "%s/%s",
379 _PATH_BOOTDIR, lp->d_boot1);
381 (void)strcpy(boot1, lp->d_boot1);
386 /* d_packname is union d_boot[01], so zero */
387 bzero(lp->d_packname, sizeof(lp->d_packname));
389 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
393 writelabel(int f, const char *boot, struct disklabel *lp)
405 Warning("write to disk label supressed - label was as follows:");
410 lp->d_magic = DISKMAGIC;
411 lp->d_magic2 = DISKMAGIC;
413 lp->d_checksum = dkcksum(lp);
416 * First set the kernel disk label,
417 * then write a label to the raw disk.
418 * If the SDINFO ioctl fails because it is unimplemented,
419 * keep going; otherwise, the kernel consistency checks
420 * may prevent us from changing the current (in-core)
423 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
424 errno != ENODEV && errno != ENOTTY) {
425 l_perror("ioctl DIOCSDINFO");
428 (void)lseek(f, (off_t)0, SEEK_SET);
432 * Generate the bootblock checksum for the SRM console.
434 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
440 * write enable label sector before write (if necessary),
441 * disable after writing.
444 if (ioctl(f, DIOCWLABEL, &flag) < 0)
445 warn("ioctl DIOCWLABEL");
446 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
452 * Output the remainder of the disklabel
454 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
460 (void) ioctl(f, DIOCWLABEL, &flag);
461 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
462 l_perror("ioctl DIOCWDINFO");
466 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
469 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
470 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
471 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
473 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
474 warn("alternate label %d write", i/2);
483 l_perror(const char *s)
488 warnx("%s: no disk label on disk;", s);
489 fprintf(stderr, "add \"-r\" to install initial label\n");
493 warnx("%s: label magic number or checksum is wrong!", s);
494 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
498 warnx("%s: open partition would move or shrink", s);
502 warnx("%s: '%c' partition must start at beginning of disk",
513 * Fetch disklabel for disk.
514 * Use ioctl to get label unless -r flag is given.
519 struct disklabel *lp;
522 if (read(f, bootarea, BBSIZE) < BBSIZE)
523 err(4, "%s", specname);
524 for (lp = (struct disklabel *)bootarea;
525 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
526 lp = (struct disklabel *)((char *)lp + 16))
527 if (lp->d_magic == DISKMAGIC &&
528 lp->d_magic2 == DISKMAGIC)
530 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
531 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
534 "bad pack magic number (label is damaged, or pack is unlabeled)");
537 if (ioctl(f, DIOCGDINFO, lp) < 0)
538 err(4, "ioctl DIOCGDINFO");
544 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
545 * Returns a pointer to the disklabel portion of the bootarea.
548 makebootarea(char *boot, struct disklabel *dp, int f)
550 struct disklabel *lp;
567 if (dp->d_secsize == 0) {
568 dp->d_secsize = DEV_BSIZE;
569 dp->d_bbsize = BBSIZE;
571 lp = (struct disklabel *)
572 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
573 bzero((char *)lp, sizeof *lp);
576 * If we are not installing a boot program but we are installing a
577 * label on disk then we must read the current bootarea so we don't
578 * clobber the existing boot.
582 if (read(f, boot, BBSIZE) < BBSIZE)
583 err(4, "%s", specname);
584 bzero((char *)lp, sizeof *lp);
589 * We are installing a boot program. Determine the name(s) and
590 * read them into the appropriate places in the boot area.
592 if (!xxboot || !bootxx) {
594 if ((p = rindex(dkname, '/')) == NULL)
598 while (*p && !isdigit(*p))
603 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
608 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
615 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
616 xxboot, bootxx ? bootxx : "NONE");
621 * 1. One-piece bootstrap (hp300/hp800)
622 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
623 * is remembered and written later following the bootarea.
624 * 2. Two-piece bootstraps (vax/i386?/mips?)
625 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
626 * bytes of bootarea, remaining d_bbsize-d_secsize filled
629 b = open(xxboot, O_RDONLY);
631 err(4, "%s", xxboot);
636 * The i386 has the so-called fdisk table embedded into the
637 * primary bootstrap. We take care to not clobber it, but
638 * only if it does already contain some data. (Otherwise,
639 * the xxboot provides a template.)
641 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
642 err(4, "%s", xxboot);
643 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
645 if (read(b, boot, (int)dp->d_secsize) < 0)
646 err(4, "%s", xxboot);
649 for (i = DOSPARTOFF, found = 0;
650 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
652 found = tmpbuf[i] != 0;
654 memcpy((void *)&boot[DOSPARTOFF],
655 (void *)&tmpbuf[DOSPARTOFF],
656 NDOSPART * sizeof(struct dos_partition));
659 b = open(bootxx, O_RDONLY);
661 err(4, "%s", bootxx);
662 if (fstat(b, &sb) != 0)
663 err(4, "%s", bootxx);
664 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
665 errx(4, "%s too large", bootxx);
666 if (read(b, &boot[dp->d_secsize],
667 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
668 err(4, "%s", bootxx);
669 #else /* !(NUMBOOT > 1) */
672 * On the alpha, the primary bootstrap starts at the
673 * second sector of the boot area. The first sector
674 * contains the label and must be edited to contain the
675 * size and location of the primary bootstrap.
677 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
679 err(4, "%s", xxboot);
680 bootinfo = (u_long *)(boot + 480);
681 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
682 bootinfo[1] = 1; /* start at sector 1 */
683 bootinfo[2] = 0; /* flags (must be zero) */
684 #else /* !__alpha__ */
685 if (read(b, boot, (int)dp->d_bbsize) < 0)
686 err(4, "%s", xxboot);
687 #endif /* __alpha__ */
688 if (fstat(b, &sb) != 0)
689 err(4, "%s", xxboot);
690 bootsize = (int)sb.st_size - dp->d_bbsize;
692 /* XXX assume d_secsize is a power of two */
693 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
694 bootbuf = (char *)malloc((size_t)bootsize);
696 err(4, "%s", xxboot);
697 if (read(b, bootbuf, bootsize) < 0) {
699 err(4, "%s", xxboot);
702 #endif /* NUMBOOT > 1 */
704 #endif /* NUMBOOT > 0 */
706 * Make sure no part of the bootstrap is written in the area
707 * reserved for the label.
709 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
711 errx(2, "bootstrap doesn't leave room for disk label");
716 display(FILE *f, const struct disklabel *lp)
719 const struct partition *pp;
721 fprintf(f, "# %s:\n", specname);
722 if (lp->d_type < DKMAXTYPES)
723 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
725 fprintf(f, "type: %u\n", lp->d_type);
726 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
728 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
730 fprintf(f, "flags:");
731 if (lp->d_flags & D_REMOVABLE)
732 fprintf(f, " removeable");
733 if (lp->d_flags & D_ECC)
735 if (lp->d_flags & D_BADSECT)
736 fprintf(f, " badsect");
738 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
739 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
740 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
741 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
742 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
743 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
744 fprintf(f, "rpm: %u\n", lp->d_rpm);
745 fprintf(f, "interleave: %u\n", lp->d_interleave);
746 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
747 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
748 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
749 (u_long)lp->d_headswitch);
750 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
751 (u_long)lp->d_trkseek);
752 fprintf(f, "drivedata: ");
753 for (i = NDDATA - 1; i >= 0; i--)
754 if (lp->d_drivedata[i])
758 for (j = 0; j <= i; j++)
759 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
760 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
762 "# size offset fstype [fsize bsize bps/cpg]\n");
763 pp = lp->d_partitions;
764 for (i = 0; i < lp->d_npartitions; i++, pp++) {
766 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
767 (u_long)pp->p_size, (u_long)pp->p_offset);
768 if (pp->p_fstype < FSMAXTYPES)
769 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
771 fprintf(f, "%8d", pp->p_fstype);
772 switch (pp->p_fstype) {
774 case FS_UNUSED: /* XXX */
775 fprintf(f, " %5lu %5lu %5.5s ",
777 (u_long)(pp->p_fsize * pp->p_frag), "");
781 fprintf(f, " %5lu %5lu %5u ",
783 (u_long)(pp->p_fsize * pp->p_frag),
788 fprintf(f, " %5lu %5lu %5d",
790 (u_long)(pp->p_fsize * pp->p_frag),
795 fprintf(f, "%20.20s", "");
798 fprintf(f, "\t# (Cyl. %4lu",
799 (u_long)(pp->p_offset / lp->d_secpercyl));
800 if (pp->p_offset % lp->d_secpercyl)
805 (u_long)((pp->p_offset + pp->p_size +
806 lp->d_secpercyl - 1) /
807 lp->d_secpercyl - 1));
808 if (pp->p_size % lp->d_secpercyl)
817 edit(struct disklabel *lp, int f)
820 struct disklabel label;
823 if ((fd = mkstemp(tmpfil)) == -1 ||
824 (fp = fdopen(fd, "w")) == NULL) {
825 warnx("can't create %s", tmpfil);
833 fp = fopen(tmpfil, "r");
835 warnx("can't reopen %s for reading", tmpfil);
838 bzero((char *)&label, sizeof(label));
839 if (getasciilabel(fp, &label)) {
841 if (writelabel(f, bootarea, lp) == 0) {
843 (void) unlink(tmpfil);
848 printf("re-edit the label? [y]: "); fflush(stdout);
850 if (c != EOF && c != (int)'\n')
851 while (getchar() != (int)'\n')
856 (void) unlink(tmpfil);
867 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
868 while ((pid = fork()) < 0) {
869 if (errno == EPROCLIM) {
870 warnx("you have too many processes");
873 if (errno != EAGAIN) {
883 if ((ed = getenv("EDITOR")) == (char *)0)
885 execlp(ed, ed, tmpfil, (char *)0);
888 while ((xpid = wait(&stat)) >= 0)
899 while (*cp != '\0' && isspace(*cp))
901 if (*cp == '\0' || *cp == '#')
911 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
913 if ((c = *cp) != '\0') {
922 * Read an ascii label in from fd f,
923 * in the same format as that put out by display(),
927 getasciilabel(FILE *f, struct disklabel *lp)
932 char *tp, line[BUFSIZ];
934 int lineno = 0, errors = 0;
937 bzero(&part_set, sizeof(part_set));
938 bzero(&part_size_type, sizeof(part_size_type));
939 bzero(&part_offset_type, sizeof(part_offset_type));
940 lp->d_bbsize = BBSIZE; /* XXX */
941 lp->d_sbsize = SBSIZE; /* XXX */
942 while (fgets(line, sizeof(line) - 1, f)) {
944 if ((cp = index(line,'\n')) != 0)
951 fprintf(stderr, "line %d: syntax error\n", lineno);
955 *tp++ = '\0', tp = skip(tp);
956 if (streq(cp, "type")) {
960 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
961 if (*cpp && streq(*cpp, tp)) {
962 lp->d_type = cpp - dktypenames;
965 if (cpp < &dktypenames[DKMAXTYPES])
967 v = strtoul(tp, NULL, 10);
969 fprintf(stderr, "line %d:%s %lu\n", lineno,
970 "Warning, unknown disk type", v);
974 if (streq(cp, "flags")) {
975 for (v = 0; (cp = tp) && *cp != '\0';) {
977 if (streq(cp, "removeable"))
979 else if (streq(cp, "ecc"))
981 else if (streq(cp, "badsect"))
985 "line %d: %s: bad flag\n",
993 if (streq(cp, "drivedata")) {
994 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
995 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
1000 if (sscanf(cp, "%lu partitions", &v) == 1) {
1001 if (v == 0 || v > MAXPARTITIONS) {
1003 "line %d: bad # of partitions\n", lineno);
1004 lp->d_npartitions = MAXPARTITIONS;
1007 lp->d_npartitions = v;
1012 if (streq(cp, "disk")) {
1013 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1016 if (streq(cp, "label")) {
1017 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1020 if (streq(cp, "bytes/sector")) {
1021 v = strtoul(tp, NULL, 10);
1022 if (v == 0 || (v % DEV_BSIZE) != 0) {
1024 "line %d: %s: bad sector size\n",
1031 if (streq(cp, "sectors/track")) {
1032 v = strtoul(tp, NULL, 10);
1033 #if (ULONG_MAX != 0xffffffffUL)
1034 if (v == 0 || v > 0xffffffff) {
1038 fprintf(stderr, "line %d: %s: bad %s\n",
1045 if (streq(cp, "sectors/cylinder")) {
1046 v = strtoul(tp, NULL, 10);
1048 fprintf(stderr, "line %d: %s: bad %s\n",
1052 lp->d_secpercyl = v;
1055 if (streq(cp, "tracks/cylinder")) {
1056 v = strtoul(tp, NULL, 10);
1058 fprintf(stderr, "line %d: %s: bad %s\n",
1065 if (streq(cp, "cylinders")) {
1066 v = strtoul(tp, NULL, 10);
1068 fprintf(stderr, "line %d: %s: bad %s\n",
1072 lp->d_ncylinders = v;
1075 if (streq(cp, "sectors/unit")) {
1076 v = strtoul(tp, NULL, 10);
1078 fprintf(stderr, "line %d: %s: bad %s\n",
1082 lp->d_secperunit = v;
1085 if (streq(cp, "rpm")) {
1086 v = strtoul(tp, NULL, 10);
1087 if (v == 0 || v > USHRT_MAX) {
1088 fprintf(stderr, "line %d: %s: bad %s\n",
1095 if (streq(cp, "interleave")) {
1096 v = strtoul(tp, NULL, 10);
1097 if (v == 0 || v > USHRT_MAX) {
1098 fprintf(stderr, "line %d: %s: bad %s\n",
1102 lp->d_interleave = v;
1105 if (streq(cp, "trackskew")) {
1106 v = strtoul(tp, NULL, 10);
1107 if (v > USHRT_MAX) {
1108 fprintf(stderr, "line %d: %s: bad %s\n",
1112 lp->d_trackskew = v;
1115 if (streq(cp, "cylinderskew")) {
1116 v = strtoul(tp, NULL, 10);
1117 if (v > USHRT_MAX) {
1118 fprintf(stderr, "line %d: %s: bad %s\n",
1125 if (streq(cp, "headswitch")) {
1126 v = strtoul(tp, NULL, 10);
1127 lp->d_headswitch = v;
1130 if (streq(cp, "track-to-track seek")) {
1131 v = strtoul(tp, NULL, 10);
1135 /* the ':' was removed above */
1136 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1138 "line %d: %s: Unknown disklabel field\n", lineno,
1144 /* Process a partition specification line. */
1146 if (part >= lp->d_npartitions) {
1148 "line %d: partition name out of range a-%c: %s\n",
1149 lineno, 'a' + lp->d_npartitions - 1, cp);
1155 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1160 errors += checklabel(lp);
1161 return (errors == 0);
1164 #define NXTNUM(n) do { \
1166 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1169 cp = tp, tp = word(cp); \
1170 (n) = strtoul(cp, NULL, 10); \
1174 /* retain 1 character following number */
1175 #define NXTWORD(w,n) do { \
1177 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1181 cp = tp, tp = word(cp); \
1182 (n) = strtoul(cp, &tmp, 10); \
1183 if (tmp) (w) = *tmp; \
1188 * Read a partition line into partition `part' in the specified disklabel.
1189 * Return 0 on success, 1 on failure.
1192 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1194 struct partition *pp;
1199 pp = &lp->d_partitions[part];
1203 NXTWORD(part_size_type[part],v);
1204 if (v == 0 && part_size_type[part] != '*') {
1206 "line %d: %s: bad partition size\n", lineno, cp);
1212 NXTWORD(part_offset_type[part],v);
1213 if (v == 0 && part_offset_type[part] != '*' &&
1214 part_offset_type[part] != '\0') {
1216 "line %d: %s: bad partition offset\n", lineno, cp);
1220 cp = tp, tp = word(cp);
1221 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1222 if (*cpp && streq(*cpp, cp))
1225 pp->p_fstype = cpp - fstypenames;
1228 v = strtoul(cp, NULL, 10);
1231 if (v >= FSMAXTYPES) {
1233 "line %d: Warning, unknown filesystem type %s\n",
1240 switch (pp->p_fstype) {
1243 * allow us to accept defaults for
1247 NXTNUM(pp->p_fsize);
1248 if (pp->p_fsize == 0)
1251 pp->p_frag = v / pp->p_fsize;
1253 /* else default to 0's */
1256 /* These happen to be the same */
1260 NXTNUM(pp->p_fsize);
1261 if (pp->p_fsize == 0)
1264 pp->p_frag = v / pp->p_fsize;
1268 * FIX! poor attempt at adaptive
1271 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1273 * FIX! These are too low, but are traditional
1275 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1276 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1278 pp->p_cpg = DEFAULT_NEWFS_CPG;
1280 pp->p_fsize = BIG_NEWFS_FRAG;
1281 pp->p_frag = BIG_NEWFS_BLOCK /
1283 pp->p_cpg = BIG_NEWFS_CPG;
1293 * Check disklabel for errors and fill in
1294 * derived fields according to supplied values.
1297 checklabel(struct disklabel *lp)
1299 struct partition *pp;
1302 u_long total_size, total_percent, current_offset;
1303 int seen_default_offset;
1306 struct partition *pp2;
1308 if (lp->d_secsize == 0) {
1309 fprintf(stderr, "sector size 0\n");
1312 if (lp->d_nsectors == 0) {
1313 fprintf(stderr, "sectors/track 0\n");
1316 if (lp->d_ntracks == 0) {
1317 fprintf(stderr, "tracks/cylinder 0\n");
1320 if (lp->d_ncylinders == 0) {
1321 fprintf(stderr, "cylinders/unit 0\n");
1325 Warning("revolutions/minute 0");
1326 if (lp->d_secpercyl == 0)
1327 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1328 if (lp->d_secperunit == 0)
1329 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1330 if (lp->d_bbsize == 0) {
1331 fprintf(stderr, "boot block size 0\n");
1333 } else if (lp->d_bbsize % lp->d_secsize)
1334 Warning("boot block size %% sector-size != 0");
1335 if (lp->d_sbsize == 0) {
1336 fprintf(stderr, "super block size 0\n");
1338 } else if (lp->d_sbsize % lp->d_secsize)
1339 Warning("super block size %% sector-size != 0");
1340 if (lp->d_npartitions > MAXPARTITIONS)
1341 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1342 (u_long)lp->d_npartitions, MAXPARTITIONS);
1344 /* first allocate space to the partitions, then offsets */
1345 total_size = 0; /* in sectors */
1346 total_percent = 0; /* in percent */
1348 /* find all fixed partitions */
1349 for (i = 0; i < lp->d_npartitions; i++) {
1350 pp = &lp->d_partitions[i];
1352 if (part_size_type[i] == '*') {
1353 if (i == RAW_PART) {
1354 pp->p_size = lp->d_secperunit;
1357 Warning("Too many '*' partitions (%c and %c)",
1358 hog_part + 'a',i + 'a');
1366 switch (part_size_type[i]) {
1368 total_percent += size;
1376 size *= 1024ULL * 1024ULL;
1380 size *= 1024ULL * 1024ULL * 1024ULL;
1385 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1388 /* don't count %'s yet */
1389 if (part_size_type[i] != '%') {
1391 * for all not in sectors, convert to
1394 if (part_size_type[i] != '\0') {
1395 if (size % lp->d_secsize != 0)
1396 Warning("partition %c not an integer number of sectors",
1398 size /= lp->d_secsize;
1401 /* else already in sectors */
1408 /* handle % partitions - note %'s don't need to add up to 100! */
1409 if (total_percent != 0) {
1410 long free_space = lp->d_secperunit - total_size;
1411 if (total_percent > 100) {
1412 fprintf(stderr,"total percentage %lu is greater than 100\n",
1417 if (free_space > 0) {
1418 for (i = 0; i < lp->d_npartitions; i++) {
1419 pp = &lp->d_partitions[i];
1420 if (part_set[i] && part_size_type[i] == '%') {
1421 /* careful of overflows! and integer roundoff */
1422 pp->p_size = ((double)pp->p_size/100) * free_space;
1423 total_size += pp->p_size;
1425 /* FIX we can lose a sector or so due to roundoff per
1426 partition. A more complex algorithm could avoid that */
1431 "%ld sectors available to give to '*' and '%%' partitions\n",
1434 /* fix? set all % partitions to size 0? */
1437 /* give anything remaining to the hog partition */
1438 if (hog_part != -1) {
1439 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1440 total_size = lp->d_secperunit;
1443 /* Now set the offsets for each partition */
1444 current_offset = 0; /* in sectors */
1445 seen_default_offset = 0;
1446 for (i = 0; i < lp->d_npartitions; i++) {
1448 pp = &lp->d_partitions[i];
1450 if (part_offset_type[i] == '*') {
1451 if (i == RAW_PART) {
1454 pp->p_offset = current_offset;
1455 seen_default_offset = 1;
1458 /* allow them to be out of order for old-style tables */
1459 if (pp->p_offset < current_offset &&
1460 seen_default_offset && i != RAW_PART &&
1461 pp->p_fstype != FS_VINUM) {
1463 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1464 (long)pp->p_offset,i+'a',current_offset);
1466 "Labels with any *'s for offset must be in ascending order by sector\n");
1468 } else if (pp->p_offset != current_offset &&
1469 i != RAW_PART && seen_default_offset) {
1471 * this may give unneeded warnings if
1472 * partitions are out-of-order
1475 "Offset %ld for partition %c doesn't match expected value %ld",
1476 (long)pp->p_offset, i + 'a', current_offset);
1480 current_offset = pp->p_offset + pp->p_size;
1484 for (i = 0; i < lp->d_npartitions; i++) {
1486 pp = &lp->d_partitions[i];
1487 if (pp->p_size == 0 && pp->p_offset != 0)
1488 Warning("partition %c: size 0, but offset %lu",
1489 part, (u_long)pp->p_offset);
1491 if (pp->p_size % lp->d_secpercyl)
1492 Warning("partition %c: size %% cylinder-size != 0",
1494 if (pp->p_offset % lp->d_secpercyl)
1495 Warning("partition %c: offset %% cylinder-size != 0",
1498 if (pp->p_offset > lp->d_secperunit) {
1500 "partition %c: offset past end of unit\n", part);
1503 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1505 "partition %c: partition extends past end of unit\n",
1511 if (pp->p_fstype != FS_UNUSED)
1512 Warning("partition %c is not marked as unused!",part);
1513 if (pp->p_offset != 0)
1514 Warning("partition %c doesn't start at 0!",part);
1515 if (pp->p_size != lp->d_secperunit)
1516 Warning("partition %c doesn't cover the whole unit!",part);
1518 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1519 (pp->p_size != lp->d_secperunit)) {
1520 Warning("An incorrect partition %c may cause problems for "
1521 "standard system utilities",part);
1525 /* check for overlaps */
1526 /* this will check for all possible overlaps once and only once */
1527 for (j = 0; j < i; j++) {
1528 pp2 = &lp->d_partitions[j];
1529 if (j != RAW_PART && i != RAW_PART &&
1530 pp->p_fstype != FS_VINUM &&
1531 pp2->p_fstype != FS_VINUM &&
1532 part_set[i] && part_set[j]) {
1533 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1534 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1535 pp2->p_offset >= pp->p_offset)) {
1536 fprintf(stderr,"partitions %c and %c overlap!\n",
1543 for (; i < MAXPARTITIONS; i++) {
1545 pp = &lp->d_partitions[i];
1546 if (pp->p_size || pp->p_offset)
1547 Warning("unused partition %c: size %d offset %lu",
1548 'a' + i, pp->p_size, (u_long)pp->p_offset);
1554 * When operating on a "virgin" disk, try getting an initial label
1555 * from the associated device driver. This might work for all device
1556 * drivers that are able to fetch some initial device parameters
1557 * without even having access to a (BSD) disklabel, like SCSI disks,
1558 * most IDE drives, or vn devices.
1560 * The device name must be given in its "canonical" form.
1563 getvirginlabel(void)
1565 static struct disklabel lab;
1566 char namebuf[BBSIZE];
1569 if (dkname[0] == '/') {
1570 warnx("\"auto\" requires the usage of a canonical disk name");
1573 (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1574 if ((f = open(namebuf, O_RDONLY)) == -1) {
1575 warn("cannot open %s", namebuf);
1580 * Try to use the new get-virgin-label ioctl. If it fails,
1581 * fallback to the old get-disdk-info ioctl.
1583 if (ioctl(f, DIOCGDVIRGIN, &lab) < 0) {
1584 if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1585 warn("ioctl DIOCGDINFO");
1597 * If we are installing a boot program that doesn't fit in d_bbsize
1598 * we need to mark those partitions that the boot overflows into.
1599 * This allows newfs to prevent creation of a filesystem where it might
1600 * clobber bootstrap code.
1603 setbootflag(struct disklabel *lp)
1605 struct partition *pp;
1612 boffset = bootsize / lp->d_secsize;
1613 for (i = 0; i < lp->d_npartitions; i++) {
1615 pp = &lp->d_partitions[i];
1616 if (pp->p_size == 0)
1618 if (boffset <= pp->p_offset) {
1619 if (pp->p_fstype == FS_BOOT)
1620 pp->p_fstype = FS_UNUSED;
1621 } else if (pp->p_fstype != FS_BOOT) {
1622 if (pp->p_fstype != FS_UNUSED) {
1624 "boot overlaps used partition %c\n",
1628 pp->p_fstype = FS_BOOT;
1629 Warning("boot overlaps partition %c, %s",
1630 part, "marked as FS_BOOT");
1635 errx(4, "cannot install boot program");
1640 Warning(const char *fmt, ...)
1644 fprintf(stderr, "Warning, ");
1646 vfprintf(stderr, fmt, ap);
1647 fprintf(stderr, "\n");
1655 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",
1656 "usage: disklabel [-r] disk",
1657 "\t\t(to read label)",
1658 " disklabel -w [-r] [-n] disk type [ packid ]",
1659 "\t\t(to write label with existing boot program)",
1660 " disklabel -e [-r] [-n] disk",
1661 "\t\t(to edit label)",
1662 " disklabel -R [-r] [-n] disk protofile",
1663 "\t\t(to restore label with existing boot program)",
1665 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1666 "\t\t(to install boot program with existing label)",
1667 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1668 "\t\t(to write label and boot program)",
1669 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1670 "\t\t(to restore label and boot program)",
1672 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1673 "\t\t(to install boot program with existing on-disk label)",
1674 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1675 "\t\t(to write label and install boot program)",
1676 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1677 "\t\t(to restore label and install boot program)",
1679 " disklabel [-NW] disk",
1680 "\t\t(to write disable/enable label)");
1682 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1683 "usage: disklabel [-r] disk", "(to read label)",
1684 " disklabel -w [-r] [-n] disk type [ packid ]",
1685 "\t\t(to write label)",
1686 " disklabel -e [-r] [-n] disk",
1687 "\t\t(to edit label)",
1688 " disklabel -R [-r] [-n] disk protofile",
1689 "\t\t(to restore label)",
1690 " disklabel [-NW] disk",
1691 "\t\t(to write disable/enable label)");