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.16 2007/05/19 00:51:59 dillon Exp $
43 #include <sys/param.h>
48 #include <sys/disklabel.h>
49 #include <sys/diskmbr.h>
52 #include <vfs/ufs/dinode.h>
53 #include <vfs/ufs/fs.h>
65 #include "pathnames.h"
68 * Disklabel: read and write disklabels.
69 * The label is usually placed on one of the first sectors of the disk.
70 * Many machines also place a bootstrap in the same area,
71 * in which case the label is embedded in the bootstrap.
72 * The bootstrap source must leave space at the proper offset
73 * for the label on such machines.
77 #define BBSIZE 8192 /* size of boot area, with label */
80 /* FIX! These are too low, but are traditional */
81 #define DEFAULT_NEWFS_BLOCK 8192U
82 #define DEFAULT_NEWFS_FRAG 1024U
83 #define DEFAULT_NEWFS_CPG 16U
85 #define BIG_NEWFS_BLOCK 16384U
86 #define BIG_NEWFS_FRAG 2048U
87 #define BIG_NEWFS_CPG 64U
91 void makelabel(const char *, const char *, struct disklabel *);
92 int writelabel(int, const char *, struct disklabel *);
93 void l_perror(const char *);
94 struct disklabel *readlabel(int);
95 struct disklabel *makebootarea(char *, struct disklabel *, int);
96 void display(FILE *, const struct disklabel *);
97 int edit(struct disklabel *, int);
101 int getasciilabel(FILE *, struct disklabel *);
102 int getasciipartspec(char *, struct disklabel *, int, int);
103 int checklabel(struct disklabel *);
104 void setbootflag(struct disklabel *);
105 void Warning(const char *, ...) __printflike(1, 2);
107 int checkoldboot(int, const char *);
108 struct disklabel *getvirginlabel(void);
109 struct disklabel *getdisklabelfromdisktab(const char *name);
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 sprintf(np, "%s%s", _PATH_DEV, dkname);
238 np += strlen(specname) + 1;
242 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
243 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
244 sprintf(specname, "%s%s", _PATH_DEV, dkname);
245 np = namebuf + strlen(specname) + 1;
246 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
249 err(4, "%s", specname);
265 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
266 err(4, "ioctl DIOCWLABEL");
274 error = checklabel(lp);
275 if (checkoldboot(f, NULL))
276 warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
281 if (installboot && argc == 3) {
282 makelabel(argv[2], 0, &lab);
286 * We only called makelabel() for its side effect
287 * of setting the bootstrap file names. Discard
288 * all changes to `lab' so that all values in the
289 * final label come from the ASCII label.
291 bzero((char *)&lab, sizeof(lab));
296 if (!(t = fopen(argv[1], "r")))
297 err(4, "%s", argv[1]);
298 if (!getasciilabel(t, &lab))
300 lp = makebootarea(bootarea, &lab, f);
302 error = writelabel(f, bootarea, lp);
312 makelabel(argv[1], name, &lab);
313 lp = makebootarea(bootarea, &lab, f);
315 if (checklabel(lp) == 0)
316 error = writelabel(f, bootarea, lp);
321 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
322 err(4, "ioctl DIOCWLABEL");
328 struct disklabel tlab;
333 makelabel(argv[1], 0, &lab);
334 lp = makebootarea(bootarea, &lab, f);
336 if (checklabel(lp) == 0)
337 error = writelabel(f, bootarea, lp);
346 * Construct a prototype disklabel from /etc/disktab. As a side
347 * effect, set the names of the primary and secondary boot files
351 makelabel(const char *type, const char *name, struct disklabel *lp)
353 struct disklabel *dp;
355 if (strcmp(type, "auto") == 0)
356 dp = getvirginlabel();
358 dp = getdisklabelfromdisktab(type);
360 errx(1, "%s: unknown disk type", type);
364 * NOTE: boot control files may no longer be specified in disktab.
367 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
371 writelabel(int f, const char *boot, struct disklabel *lp)
376 Warning("write to disk label supressed - label was as follows:");
380 /* make sure we are not overwriting our boot code */
381 if (checkoldboot(f, boot))
382 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
384 lp->d_magic = DISKMAGIC;
385 lp->d_magic2 = DISKMAGIC;
387 lp->d_checksum = dkcksum(lp);
390 * First set the kernel disk label,
391 * then write a label to the raw disk.
392 * If the SDINFO ioctl fails because it is unimplemented,
393 * keep going; otherwise, the kernel consistency checks
394 * may prevent us from changing the current (in-core)
397 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
398 errno != ENODEV && errno != ENOTTY) {
399 l_perror("ioctl DIOCSDINFO");
402 lseek(f, (off_t)0, SEEK_SET);
405 * write enable label sector before write (if necessary),
406 * disable after writing.
409 if (ioctl(f, DIOCWLABEL, &flag) < 0)
410 warn("ioctl DIOCWLABEL");
411 if (write(f, boot, lp->d_bbsize) != (ssize_t)lp->d_bbsize) {
417 * Output the remainder of the disklabel
419 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
425 ioctl(f, DIOCWLABEL, &flag);
426 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
427 l_perror("ioctl DIOCWDINFO");
435 l_perror(const char *s)
440 warnx("%s: no disk label on disk;", s);
441 fprintf(stderr, "add \"-r\" to install initial label\n");
445 warnx("%s: label magic number or checksum is wrong!", s);
446 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
450 warnx("%s: open partition would move or shrink", s);
454 warnx("%s: '%c' partition must start at beginning of disk",
465 * Fetch disklabel for disk.
466 * Use ioctl to get label unless -r flag is given.
471 struct disklabel *lp;
474 if (read(f, bootarea, BBSIZE) < BBSIZE)
475 err(4, "%s", specname);
476 for (lp = (struct disklabel *)bootarea;
477 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
478 lp = (struct disklabel *)((char *)lp + 16))
479 if (lp->d_magic == DISKMAGIC &&
480 lp->d_magic2 == DISKMAGIC)
482 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
483 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
486 "bad pack magic number (label is damaged, or pack is unlabeled)");
489 if (ioctl(f, DIOCGDINFO, lp) < 0)
490 err(4, "ioctl DIOCGDINFO");
496 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
497 * Returns a pointer to the disklabel portion of the bootarea.
500 makebootarea(char *boot, struct disklabel *dp, int f)
502 struct disklabel *lp;
511 unsigned int i, found;
515 if (dp->d_secsize == 0) {
516 dp->d_secsize = DEV_BSIZE;
517 dp->d_bbsize = BBSIZE;
519 lp = (struct disklabel *)
520 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
521 bzero((char *)lp, sizeof *lp);
524 * If we are not installing a boot program but we are installing a
525 * label on disk then we must read the current bootarea so we don't
526 * clobber the existing boot.
530 if (read(f, boot, BBSIZE) < BBSIZE)
531 err(4, "%s", specname);
532 bzero((char *)lp, sizeof *lp);
537 * We are installing a boot program. Determine the name(s) and
538 * read them into the appropriate places in the boot area.
540 if (!xxboot || !bootxx) {
542 if ((p = strrchr(dkname, '/')) == NULL)
546 while (*p && !isdigit(*p))
551 sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
556 sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
563 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
564 xxboot, bootxx ? bootxx : "NONE");
569 * 1. One-piece bootstrap (hp300/hp800)
570 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
571 * is remembered and written later following the bootarea.
572 * 2. Two-piece bootstraps (vax/i386?/mips?)
573 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
574 * bytes of bootarea, remaining d_bbsize-d_secsize filled
577 b = open(xxboot, O_RDONLY);
579 err(4, "%s", xxboot);
584 * The i386 has the so-called fdisk table embedded into the
585 * primary bootstrap. We take care to not clobber it, but
586 * only if it does already contain some data. (Otherwise,
587 * the xxboot provides a template.)
589 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
590 err(4, "%s", xxboot);
591 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
593 if (read(b, boot, (int)dp->d_secsize) < 0)
594 err(4, "%s", xxboot);
597 for (i = DOSPARTOFF, found = 0;
598 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
600 found = tmpbuf[i] != 0;
602 memcpy((void *)&boot[DOSPARTOFF],
603 (void *)&tmpbuf[DOSPARTOFF],
604 NDOSPART * sizeof(struct dos_partition));
607 b = open(bootxx, O_RDONLY);
609 err(4, "%s", bootxx);
610 if (fstat(b, &sb) != 0)
611 err(4, "%s", bootxx);
612 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
613 errx(4, "%s too large", bootxx);
614 if (read(b, &boot[dp->d_secsize],
615 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
616 err(4, "%s", bootxx);
617 #else /* !(NUMBOOT > 1) */
618 if (read(b, boot, (int)dp->d_bbsize) < 0)
619 err(4, "%s", xxboot);
620 if (fstat(b, &sb) != 0)
621 err(4, "%s", xxboot);
622 bootsize = (int)sb.st_size - dp->d_bbsize;
624 /* XXX assume d_secsize is a power of two */
625 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
626 bootbuf = (char *)malloc((size_t)bootsize);
628 err(4, "%s", xxboot);
629 if (read(b, bootbuf, bootsize) < 0) {
631 err(4, "%s", xxboot);
634 #endif /* NUMBOOT > 1 */
636 #endif /* NUMBOOT > 0 */
638 * Make sure no part of the bootstrap is written in the area
639 * reserved for the label.
641 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
643 errx(2, "bootstrap doesn't leave room for disk label");
648 display(FILE *f, const struct disklabel *lp)
651 const struct partition *pp;
653 fprintf(f, "# %s:\n", specname);
654 if (lp->d_type < DKMAXTYPES)
655 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
657 fprintf(f, "type: %u\n", lp->d_type);
658 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
660 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
662 fprintf(f, "flags:");
664 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
665 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
666 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
667 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
668 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
669 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
670 fprintf(f, "rpm: %u\n", lp->d_rpm);
671 fprintf(f, "interleave: %u\n", lp->d_interleave);
672 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
673 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
674 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
675 (u_long)lp->d_headswitch);
676 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
677 (u_long)lp->d_trkseek);
678 fprintf(f, "drivedata: ");
679 for (i = NDDATA - 1; i >= 0; i--)
680 if (lp->d_drivedata[i])
684 for (j = 0; j <= i; j++)
685 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
686 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
688 "# size offset fstype [fsize bsize bps/cpg]\n");
689 pp = lp->d_partitions;
690 for (i = 0; i < lp->d_npartitions; i++, pp++) {
692 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
693 (u_long)pp->p_size, (u_long)pp->p_offset);
694 if (pp->p_fstype < FSMAXTYPES)
695 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
697 fprintf(f, "%8d", pp->p_fstype);
698 switch (pp->p_fstype) {
700 case FS_UNUSED: /* XXX */
701 fprintf(f, " %5lu %5lu %5.5s ",
703 (u_long)(pp->p_fsize * pp->p_frag), "");
707 fprintf(f, " %5lu %5lu %5u ",
709 (u_long)(pp->p_fsize * pp->p_frag),
714 fprintf(f, " %5lu %5lu %5d",
716 (u_long)(pp->p_fsize * pp->p_frag),
721 fprintf(f, "%20.20s", "");
724 fprintf(f, "\t# (Cyl. %4lu",
725 (u_long)(pp->p_offset / lp->d_secpercyl));
726 if (pp->p_offset % lp->d_secpercyl)
731 (u_long)((pp->p_offset + pp->p_size +
732 lp->d_secpercyl - 1) /
733 lp->d_secpercyl - 1));
734 if (pp->p_size % lp->d_secpercyl)
743 edit(struct disklabel *lp, int f)
746 struct disklabel label;
749 if ((fd = mkstemp(tmpfil)) == -1 ||
750 (fp = fdopen(fd, "w")) == NULL) {
751 warnx("can't create %s", tmpfil);
759 fp = fopen(tmpfil, "r");
761 warnx("can't reopen %s for reading", tmpfil);
764 bzero((char *)&label, sizeof(label));
765 if (getasciilabel(fp, &label)) {
767 if (writelabel(f, bootarea, lp) == 0) {
774 printf("re-edit the label? [y]: "); fflush(stdout);
776 if (c != EOF && c != (int)'\n')
777 while (getchar() != (int)'\n')
793 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
794 while ((pid = fork()) < 0) {
795 if (errno == EPROCLIM) {
796 warnx("you have too many processes");
799 if (errno != EAGAIN) {
809 if ((ed = getenv("EDITOR")) == (char *)0)
811 execlp(ed, ed, tmpfil, (char *)0);
814 while ((xpid = wait(&status)) >= 0)
825 while (*cp != '\0' && isspace(*cp))
827 if (*cp == '\0' || *cp == '#')
837 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
839 if ((c = *cp) != '\0') {
848 * Read an ascii label in from fd f,
849 * in the same format as that put out by display(),
853 getasciilabel(FILE *f, struct disklabel *lp)
858 char *tp, line[BUFSIZ];
860 int lineno = 0, errors = 0;
863 char unknown[] = "unknown";
865 bzero(&part_set, sizeof(part_set));
866 bzero(&part_size_type, sizeof(part_size_type));
867 bzero(&part_offset_type, sizeof(part_offset_type));
868 lp->d_bbsize = BBSIZE; /* XXX */
869 lp->d_sbsize = SBSIZE; /* XXX */
870 while (fgets(line, sizeof(line) - 1, f)) {
872 if ((cp = strchr(line,'\n')) != 0)
877 tp = strchr(cp, ':');
879 fprintf(stderr, "line %d: syntax error\n", lineno);
883 *tp++ = '\0', tp = skip(tp);
884 if (streq(cp, "type")) {
888 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
889 if (*cpp && streq(*cpp, tp)) {
890 lp->d_type = cpp - dktypenames;
893 if (cpp < &dktypenames[DKMAXTYPES])
895 v = strtoul(tp, NULL, 10);
897 fprintf(stderr, "line %d:%s %lu\n", lineno,
898 "Warning, unknown disk type", v);
902 if (streq(cp, "flags")) {
903 for (v = 0; (cp = tp) && *cp != '\0';) {
905 if (streq(cp, "removeable"))
906 v |= 0; /* obsolete */
907 else if (streq(cp, "ecc"))
908 v |= 0; /* obsolete */
909 else if (streq(cp, "badsect"))
910 v |= 0; /* obsolete */
913 "line %d: %s: bad flag\n",
921 if (streq(cp, "drivedata")) {
922 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
923 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
928 if (sscanf(cp, "%lu partitions", &v) == 1) {
929 if (v == 0 || v > MAXPARTITIONS) {
931 "line %d: bad # of partitions\n", lineno);
932 lp->d_npartitions = MAXPARTITIONS;
935 lp->d_npartitions = v;
940 if (streq(cp, "disk")) {
941 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
944 if (streq(cp, "label")) {
945 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
948 if (streq(cp, "bytes/sector")) {
949 v = strtoul(tp, NULL, 10);
950 if (v == 0 || (v % DEV_BSIZE) != 0) {
952 "line %d: %s: bad sector size\n",
959 if (streq(cp, "sectors/track")) {
960 v = strtoul(tp, NULL, 10);
961 #if (ULONG_MAX != 0xffffffffUL)
962 if (v == 0 || v > 0xffffffff) {
966 fprintf(stderr, "line %d: %s: bad %s\n",
973 if (streq(cp, "sectors/cylinder")) {
974 v = strtoul(tp, NULL, 10);
976 fprintf(stderr, "line %d: %s: bad %s\n",
983 if (streq(cp, "tracks/cylinder")) {
984 v = strtoul(tp, NULL, 10);
986 fprintf(stderr, "line %d: %s: bad %s\n",
993 if (streq(cp, "cylinders")) {
994 v = strtoul(tp, NULL, 10);
996 fprintf(stderr, "line %d: %s: bad %s\n",
1000 lp->d_ncylinders = v;
1003 if (streq(cp, "sectors/unit")) {
1004 v = strtoul(tp, NULL, 10);
1006 fprintf(stderr, "line %d: %s: bad %s\n",
1010 lp->d_secperunit = v;
1013 if (streq(cp, "rpm")) {
1014 v = strtoul(tp, NULL, 10);
1015 if (v == 0 || v > USHRT_MAX) {
1016 fprintf(stderr, "line %d: %s: bad %s\n",
1023 if (streq(cp, "interleave")) {
1024 v = strtoul(tp, NULL, 10);
1025 if (v == 0 || v > USHRT_MAX) {
1026 fprintf(stderr, "line %d: %s: bad %s\n",
1030 lp->d_interleave = v;
1033 if (streq(cp, "trackskew")) {
1034 v = strtoul(tp, NULL, 10);
1035 if (v > USHRT_MAX) {
1036 fprintf(stderr, "line %d: %s: bad %s\n",
1040 lp->d_trackskew = v;
1043 if (streq(cp, "cylinderskew")) {
1044 v = strtoul(tp, NULL, 10);
1045 if (v > USHRT_MAX) {
1046 fprintf(stderr, "line %d: %s: bad %s\n",
1053 if (streq(cp, "headswitch")) {
1054 v = strtoul(tp, NULL, 10);
1055 lp->d_headswitch = v;
1058 if (streq(cp, "track-to-track seek")) {
1059 v = strtoul(tp, NULL, 10);
1063 /* the ':' was removed above */
1064 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1066 "line %d: %s: Unknown disklabel field\n", lineno,
1072 /* Process a partition specification line. */
1074 if (part >= lp->d_npartitions) {
1076 "line %d: partition name out of range a-%c: %s\n",
1077 lineno, 'a' + lp->d_npartitions - 1, cp);
1083 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1088 errors += checklabel(lp);
1089 return (errors == 0);
1092 #define NXTNUM(n) do { \
1094 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1097 cp = tp, tp = word(cp); \
1098 (n) = strtoul(cp, NULL, 10); \
1102 /* retain 1 character following number */
1103 #define NXTWORD(w,n) do { \
1105 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1109 cp = tp, tp = word(cp); \
1110 (n) = strtoul(cp, &tmp, 10); \
1111 if (tmp) (w) = *tmp; \
1116 * Read a partition line into partition `part' in the specified disklabel.
1117 * Return 0 on success, 1 on failure.
1120 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1122 struct partition *pp;
1127 pp = &lp->d_partitions[part];
1131 NXTWORD(part_size_type[part],v);
1132 if (v == 0 && part_size_type[part] != '*') {
1134 "line %d: %s: bad partition size\n", lineno, cp);
1140 NXTWORD(part_offset_type[part],v);
1141 if (v == 0 && part_offset_type[part] != '*' &&
1142 part_offset_type[part] != '\0') {
1144 "line %d: %s: bad partition offset\n", lineno, cp);
1148 cp = tp, tp = word(cp);
1149 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1150 if (*cpp && streq(*cpp, cp))
1153 pp->p_fstype = cpp - fstypenames;
1156 v = strtoul(cp, NULL, 10);
1159 if (v >= FSMAXTYPES) {
1161 "line %d: Warning, unknown filesystem type %s\n",
1168 switch (pp->p_fstype) {
1171 * allow us to accept defaults for
1175 NXTNUM(pp->p_fsize);
1176 if (pp->p_fsize == 0)
1179 pp->p_frag = v / pp->p_fsize;
1181 /* else default to 0's */
1184 /* These happen to be the same */
1188 NXTNUM(pp->p_fsize);
1189 if (pp->p_fsize == 0)
1192 pp->p_frag = v / pp->p_fsize;
1196 * FIX! poor attempt at adaptive
1199 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1201 * FIX! These are too low, but are traditional
1203 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1204 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1206 pp->p_cpg = DEFAULT_NEWFS_CPG;
1208 pp->p_fsize = BIG_NEWFS_FRAG;
1209 pp->p_frag = BIG_NEWFS_BLOCK /
1211 pp->p_cpg = BIG_NEWFS_CPG;
1221 * Check disklabel for errors and fill in
1222 * derived fields according to supplied values.
1225 checklabel(struct disklabel *lp)
1227 struct partition *pp;
1230 u_long total_size, total_percent, current_offset;
1231 int seen_default_offset;
1234 struct partition *pp2;
1236 if (lp->d_secsize == 0) {
1237 fprintf(stderr, "sector size 0\n");
1240 if (lp->d_nsectors == 0) {
1241 fprintf(stderr, "sectors/track 0\n");
1244 if (lp->d_ntracks == 0) {
1245 fprintf(stderr, "tracks/cylinder 0\n");
1248 if (lp->d_ncylinders == 0) {
1249 fprintf(stderr, "cylinders/unit 0\n");
1253 Warning("revolutions/minute 0");
1254 if (lp->d_secpercyl == 0)
1255 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1256 if (lp->d_secperunit == 0)
1257 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1258 if (lp->d_bbsize == 0) {
1259 fprintf(stderr, "boot block size 0\n");
1261 } else if (lp->d_bbsize % lp->d_secsize)
1262 Warning("boot block size %% sector-size != 0");
1263 if (lp->d_sbsize == 0) {
1264 fprintf(stderr, "super block size 0\n");
1266 } else if (lp->d_sbsize % lp->d_secsize)
1267 Warning("super block size %% sector-size != 0");
1268 if (lp->d_npartitions > MAXPARTITIONS)
1269 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1270 (u_long)lp->d_npartitions, MAXPARTITIONS);
1272 /* first allocate space to the partitions, then offsets */
1273 total_size = 0; /* in sectors */
1274 total_percent = 0; /* in percent */
1276 /* find all fixed partitions */
1277 for (i = 0; i < lp->d_npartitions; i++) {
1278 pp = &lp->d_partitions[i];
1280 if (part_size_type[i] == '*') {
1281 if (i == RAW_PART) {
1282 pp->p_size = lp->d_secperunit;
1285 Warning("Too many '*' partitions (%c and %c)",
1286 hog_part + 'a',i + 'a');
1294 switch (part_size_type[i]) {
1296 total_percent += size;
1304 size *= 1024ULL * 1024ULL;
1308 size *= 1024ULL * 1024ULL * 1024ULL;
1313 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1316 /* don't count %'s yet */
1317 if (part_size_type[i] != '%') {
1319 * for all not in sectors, convert to
1322 if (part_size_type[i] != '\0') {
1323 if (size % lp->d_secsize != 0)
1324 Warning("partition %c not an integer number of sectors",
1326 size /= lp->d_secsize;
1329 /* else already in sectors */
1336 /* handle % partitions - note %'s don't need to add up to 100! */
1337 if (total_percent != 0) {
1338 long free_space = lp->d_secperunit - total_size;
1339 if (total_percent > 100) {
1340 fprintf(stderr,"total percentage %lu is greater than 100\n",
1345 if (free_space > 0) {
1346 for (i = 0; i < lp->d_npartitions; i++) {
1347 pp = &lp->d_partitions[i];
1348 if (part_set[i] && part_size_type[i] == '%') {
1349 /* careful of overflows! and integer roundoff */
1350 pp->p_size = ((double)pp->p_size/100) * free_space;
1351 total_size += pp->p_size;
1353 /* FIX we can lose a sector or so due to roundoff per
1354 partition. A more complex algorithm could avoid that */
1359 "%ld sectors available to give to '*' and '%%' partitions\n",
1362 /* fix? set all % partitions to size 0? */
1365 /* give anything remaining to the hog partition */
1366 if (hog_part != -1) {
1367 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1368 total_size = lp->d_secperunit;
1371 /* Now set the offsets for each partition */
1372 current_offset = 0; /* in sectors */
1373 seen_default_offset = 0;
1374 for (i = 0; i < lp->d_npartitions; i++) {
1376 pp = &lp->d_partitions[i];
1378 if (part_offset_type[i] == '*') {
1379 if (i == RAW_PART) {
1382 pp->p_offset = current_offset;
1383 seen_default_offset = 1;
1386 /* allow them to be out of order for old-style tables */
1387 if (pp->p_offset < current_offset &&
1388 seen_default_offset && i != RAW_PART &&
1389 pp->p_fstype != FS_VINUM) {
1391 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1392 (long)pp->p_offset,i+'a',current_offset);
1394 "Labels with any *'s for offset must be in ascending order by sector\n");
1396 } else if (pp->p_offset != current_offset &&
1397 i != RAW_PART && seen_default_offset) {
1399 * this may give unneeded warnings if
1400 * partitions are out-of-order
1403 "Offset %ld for partition %c doesn't match expected value %ld",
1404 (long)pp->p_offset, i + 'a', current_offset);
1408 current_offset = pp->p_offset + pp->p_size;
1412 for (i = 0; i < lp->d_npartitions; i++) {
1414 pp = &lp->d_partitions[i];
1415 if (pp->p_size == 0 && pp->p_offset != 0)
1416 Warning("partition %c: size 0, but offset %lu",
1417 part, (u_long)pp->p_offset);
1419 if (pp->p_size % lp->d_secpercyl)
1420 Warning("partition %c: size %% cylinder-size != 0",
1422 if (pp->p_offset % lp->d_secpercyl)
1423 Warning("partition %c: offset %% cylinder-size != 0",
1426 if (pp->p_offset > lp->d_secperunit) {
1428 "partition %c: offset past end of unit\n", part);
1431 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1433 "partition %c: partition extends past end of unit\n",
1439 if (pp->p_fstype != FS_UNUSED)
1440 Warning("partition %c is not marked as unused!",part);
1441 if (pp->p_offset != 0)
1442 Warning("partition %c doesn't start at 0!",part);
1443 if (pp->p_size != lp->d_secperunit)
1444 Warning("partition %c doesn't cover the whole unit!",part);
1446 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1447 (pp->p_size != lp->d_secperunit)) {
1448 Warning("An incorrect partition %c may cause problems for "
1449 "standard system utilities",part);
1453 /* check for overlaps */
1454 /* this will check for all possible overlaps once and only once */
1455 for (j = 0; j < i; j++) {
1456 pp2 = &lp->d_partitions[j];
1457 if (j != RAW_PART && i != RAW_PART &&
1458 pp->p_fstype != FS_VINUM &&
1459 pp2->p_fstype != FS_VINUM &&
1460 part_set[i] && part_set[j]) {
1461 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1462 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1463 pp2->p_offset >= pp->p_offset)) {
1464 fprintf(stderr,"partitions %c and %c overlap!\n",
1471 for (; i < 8 || i < lp->d_npartitions; i++) {
1473 pp = &lp->d_partitions[i];
1474 if (pp->p_size || pp->p_offset)
1475 Warning("unused partition %c: size %d offset %lu",
1476 'a' + i, pp->p_size, (u_long)pp->p_offset);
1482 * When operating on a "virgin" disk, try getting an initial label
1483 * from the associated device driver. This might work for all device
1484 * drivers that are able to fetch some initial device parameters
1485 * without even having access to a (BSD) disklabel, like SCSI disks,
1486 * most IDE drives, or vn devices.
1488 * The device name must be given in its "canonical" form.
1490 static struct disklabel dlab;
1493 getvirginlabel(void)
1495 struct disklabel *dl = &dlab;
1496 char nambuf[BBSIZE];
1499 if (dkname[0] == '/') {
1500 warnx("\"auto\" requires the usage of a canonical disk name");
1503 snprintf(nambuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1504 if ((f = open(nambuf, O_RDONLY)) == -1) {
1505 warn("cannot open %s", nambuf);
1510 * Try to use the new get-virgin-label ioctl. If it fails,
1511 * fallback to the old get-disdk-info ioctl.
1513 if (ioctl(f, DIOCGDVIRGIN, dl) < 0) {
1514 if (ioctl(f, DIOCGDINFO, dl) < 0) {
1515 warn("ioctl DIOCGDINFO");
1525 getdisklabelfromdisktab(const char *name)
1528 struct disklabel *dl = &dlab;
1531 if ((dt = getdisktabbyname(name)) == NULL)
1533 dl->d_magic = DISKMAGIC;
1534 dl->d_type = dt->d_typeid;
1536 dl->d_secsize = dt->d_media_blksize;
1537 dl->d_nsectors = dt->d_secpertrack;
1538 dl->d_ntracks = dt->d_nheads;
1539 dl->d_ncylinders = dt->d_ncylinders;
1540 dl->d_secpercyl = dt->d_secpercyl;
1541 dl->d_secperunit = dt->d_media_blocks;
1542 dl->d_rpm = dt->d_rpm;
1543 dl->d_interleave = dt->d_interleave;
1544 dl->d_trackskew = dt->d_trackskew;
1545 dl->d_cylskew = dt->d_cylskew;
1546 dl->d_headswitch = dt->d_headswitch;
1547 dl->d_trkseek = dt->d_trkseek;
1548 dl->d_magic2 = DISKMAGIC;
1549 dl->d_npartitions = dt->d_npartitions;
1550 dl->d_bbsize = dt->d_bbsize;
1551 dl->d_sbsize = dt->d_sbsize;
1552 for (i = 0; i < dt->d_npartitions; ++i) {
1553 struct partition *dlp = &dl->d_partitions[i];
1554 struct dt_partition *dtp = &dt->d_partitions[i];
1556 dlp->p_size = dtp->p_size;
1557 dlp->p_offset = dtp->p_offset;
1558 dlp->p_fsize = dtp->p_fsize;
1559 dlp->p_fstype = dtp->p_fstype;
1560 dlp->p_frag = dtp->p_fsize;
1566 * If we are installing a boot program that doesn't fit in d_bbsize
1567 * we need to mark those partitions that the boot overflows into.
1568 * This allows newfs to prevent creation of a filesystem where it might
1569 * clobber bootstrap code.
1572 setbootflag(struct disklabel *lp)
1574 struct partition *pp;
1581 boffset = bootsize / lp->d_secsize;
1582 for (i = 0; i < lp->d_npartitions; i++) {
1584 pp = &lp->d_partitions[i];
1585 if (pp->p_size == 0)
1587 if (boffset <= pp->p_offset) {
1588 if (pp->p_fstype == FS_BOOT)
1589 pp->p_fstype = FS_UNUSED;
1590 } else if (pp->p_fstype != FS_BOOT) {
1591 if (pp->p_fstype != FS_UNUSED) {
1593 "boot overlaps used partition %c\n",
1597 pp->p_fstype = FS_BOOT;
1598 Warning("boot overlaps partition %c, %s",
1599 part, "marked as FS_BOOT");
1604 errx(4, "cannot install boot program");
1609 Warning(const char *fmt, ...)
1613 fprintf(stderr, "Warning, ");
1615 vfprintf(stderr, fmt, ap);
1616 fprintf(stderr, "\n");
1621 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1622 * and earlier DFly bb's are packed against the old disklabel and a new
1623 * disklabel would blow them up. This is a hack that should be removed
1624 * in 2006 sometime (if ever).
1628 checkoldboot(int f, const char *bootbuffer)
1632 if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0)
1634 lseek(f, (off_t)0, SEEK_SET);
1635 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1637 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1639 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1648 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",
1649 "usage: disklabel [-r] disk",
1650 "\t\t(to read label)",
1651 " disklabel -w [-r] [-n] disk type [ packid ]",
1652 "\t\t(to write label with existing boot program)",
1653 " disklabel -e [-r] [-n] disk",
1654 "\t\t(to edit label)",
1655 " disklabel -R [-r] [-n] disk protofile",
1656 "\t\t(to restore label with existing boot program)",
1658 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1659 "\t\t(to install boot program with existing label)",
1660 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1661 "\t\t(to write label and boot program)",
1662 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1663 "\t\t(to restore label and boot program)",
1665 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1666 "\t\t(to install boot program with existing on-disk label)",
1667 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1668 "\t\t(to write label and install boot program)",
1669 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1670 "\t\t(to restore label and install boot program)",
1672 " disklabel [-NW] disk",
1673 "\t\t(to write disable/enable label)");
1675 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1676 "usage: disklabel [-r] disk", "(to read label)",
1677 " disklabel -w [-r] [-n] disk type [ packid ]",
1678 "\t\t(to write label)",
1679 " disklabel -e [-r] [-n] disk",
1680 "\t\t(to edit label)",
1681 " disklabel -R [-r] [-n] disk protofile",
1682 "\t\t(to restore label)",
1683 " disklabel [-NW] disk",
1684 "\t\t(to write disable/enable label)");