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.15 2007/05/18 17:05:12 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%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 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);
274 if (checkoldboot(f, NULL))
275 warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
280 if (installboot && argc == 3) {
281 makelabel(argv[2], 0, &lab);
285 * We only called makelabel() for its side effect
286 * of setting the bootstrap file names. Discard
287 * all changes to `lab' so that all values in the
288 * final label come from the ASCII label.
290 bzero((char *)&lab, sizeof(lab));
295 if (!(t = fopen(argv[1], "r")))
296 err(4, "%s", argv[1]);
297 if (!getasciilabel(t, &lab))
299 lp = makebootarea(bootarea, &lab, f);
301 error = writelabel(f, bootarea, lp);
311 makelabel(argv[1], name, &lab);
312 lp = makebootarea(bootarea, &lab, f);
314 if (checklabel(lp) == 0)
315 error = writelabel(f, bootarea, lp);
320 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
321 err(4, "ioctl DIOCWLABEL");
327 struct disklabel tlab;
332 makelabel(argv[1], 0, &lab);
333 lp = makebootarea(bootarea, &lab, f);
335 if (checklabel(lp) == 0)
336 error = writelabel(f, bootarea, lp);
345 * Construct a prototype disklabel from /etc/disktab. As a side
346 * effect, set the names of the primary and secondary boot files
350 makelabel(const char *type, const char *name, struct disklabel *lp)
352 struct disklabel *dp;
354 if (strcmp(type, "auto") == 0)
355 dp = getvirginlabel();
357 dp = getdisklabelfromdisktab(type);
359 errx(1, "%s: unknown disk type", type);
363 * NOTE: boot control files may no longer be specified in disktab.
366 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
370 writelabel(int f, const char *boot, struct disklabel *lp)
375 Warning("write to disk label supressed - label was as follows:");
379 /* make sure we are not overwriting our boot code */
380 if (checkoldboot(f, boot))
381 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
383 lp->d_magic = DISKMAGIC;
384 lp->d_magic2 = DISKMAGIC;
386 lp->d_checksum = dkcksum(lp);
389 * First set the kernel disk label,
390 * then write a label to the raw disk.
391 * If the SDINFO ioctl fails because it is unimplemented,
392 * keep going; otherwise, the kernel consistency checks
393 * may prevent us from changing the current (in-core)
396 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
397 errno != ENODEV && errno != ENOTTY) {
398 l_perror("ioctl DIOCSDINFO");
401 lseek(f, (off_t)0, SEEK_SET);
404 * write enable label sector before write (if necessary),
405 * disable after writing.
408 if (ioctl(f, DIOCWLABEL, &flag) < 0)
409 warn("ioctl DIOCWLABEL");
410 if (write(f, boot, lp->d_bbsize) != (ssize_t)lp->d_bbsize) {
416 * Output the remainder of the disklabel
418 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
424 ioctl(f, DIOCWLABEL, &flag);
425 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
426 l_perror("ioctl DIOCWDINFO");
434 l_perror(const char *s)
439 warnx("%s: no disk label on disk;", s);
440 fprintf(stderr, "add \"-r\" to install initial label\n");
444 warnx("%s: label magic number or checksum is wrong!", s);
445 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
449 warnx("%s: open partition would move or shrink", s);
453 warnx("%s: '%c' partition must start at beginning of disk",
464 * Fetch disklabel for disk.
465 * Use ioctl to get label unless -r flag is given.
470 struct disklabel *lp;
473 if (read(f, bootarea, BBSIZE) < BBSIZE)
474 err(4, "%s", specname);
475 for (lp = (struct disklabel *)bootarea;
476 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
477 lp = (struct disklabel *)((char *)lp + 16))
478 if (lp->d_magic == DISKMAGIC &&
479 lp->d_magic2 == DISKMAGIC)
481 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
482 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
485 "bad pack magic number (label is damaged, or pack is unlabeled)");
488 if (ioctl(f, DIOCGDINFO, lp) < 0)
489 err(4, "ioctl DIOCGDINFO");
495 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
496 * Returns a pointer to the disklabel portion of the bootarea.
499 makebootarea(char *boot, struct disklabel *dp, int f)
501 struct disklabel *lp;
510 unsigned int i, found;
514 if (dp->d_secsize == 0) {
515 dp->d_secsize = DEV_BSIZE;
516 dp->d_bbsize = BBSIZE;
518 lp = (struct disklabel *)
519 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
520 bzero((char *)lp, sizeof *lp);
523 * If we are not installing a boot program but we are installing a
524 * label on disk then we must read the current bootarea so we don't
525 * clobber the existing boot.
529 if (read(f, boot, BBSIZE) < BBSIZE)
530 err(4, "%s", specname);
531 bzero((char *)lp, sizeof *lp);
536 * We are installing a boot program. Determine the name(s) and
537 * read them into the appropriate places in the boot area.
539 if (!xxboot || !bootxx) {
541 if ((p = strrchr(dkname, '/')) == NULL)
545 while (*p && !isdigit(*p))
550 sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
555 sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
562 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
563 xxboot, bootxx ? bootxx : "NONE");
568 * 1. One-piece bootstrap (hp300/hp800)
569 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
570 * is remembered and written later following the bootarea.
571 * 2. Two-piece bootstraps (vax/i386?/mips?)
572 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
573 * bytes of bootarea, remaining d_bbsize-d_secsize filled
576 b = open(xxboot, O_RDONLY);
578 err(4, "%s", xxboot);
583 * The i386 has the so-called fdisk table embedded into the
584 * primary bootstrap. We take care to not clobber it, but
585 * only if it does already contain some data. (Otherwise,
586 * the xxboot provides a template.)
588 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
589 err(4, "%s", xxboot);
590 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
592 if (read(b, boot, (int)dp->d_secsize) < 0)
593 err(4, "%s", xxboot);
596 for (i = DOSPARTOFF, found = 0;
597 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
599 found = tmpbuf[i] != 0;
601 memcpy((void *)&boot[DOSPARTOFF],
602 (void *)&tmpbuf[DOSPARTOFF],
603 NDOSPART * sizeof(struct dos_partition));
606 b = open(bootxx, O_RDONLY);
608 err(4, "%s", bootxx);
609 if (fstat(b, &sb) != 0)
610 err(4, "%s", bootxx);
611 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
612 errx(4, "%s too large", bootxx);
613 if (read(b, &boot[dp->d_secsize],
614 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
615 err(4, "%s", bootxx);
616 #else /* !(NUMBOOT > 1) */
617 if (read(b, boot, (int)dp->d_bbsize) < 0)
618 err(4, "%s", xxboot);
619 if (fstat(b, &sb) != 0)
620 err(4, "%s", xxboot);
621 bootsize = (int)sb.st_size - dp->d_bbsize;
623 /* XXX assume d_secsize is a power of two */
624 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
625 bootbuf = (char *)malloc((size_t)bootsize);
627 err(4, "%s", xxboot);
628 if (read(b, bootbuf, bootsize) < 0) {
630 err(4, "%s", xxboot);
633 #endif /* NUMBOOT > 1 */
635 #endif /* NUMBOOT > 0 */
637 * Make sure no part of the bootstrap is written in the area
638 * reserved for the label.
640 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
642 errx(2, "bootstrap doesn't leave room for disk label");
647 display(FILE *f, const struct disklabel *lp)
650 const struct partition *pp;
652 fprintf(f, "# %s:\n", specname);
653 if (lp->d_type < DKMAXTYPES)
654 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
656 fprintf(f, "type: %u\n", lp->d_type);
657 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
659 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
661 fprintf(f, "flags:");
663 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
664 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
665 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
666 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
667 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
668 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
669 fprintf(f, "rpm: %u\n", lp->d_rpm);
670 fprintf(f, "interleave: %u\n", lp->d_interleave);
671 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
672 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
673 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
674 (u_long)lp->d_headswitch);
675 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
676 (u_long)lp->d_trkseek);
677 fprintf(f, "drivedata: ");
678 for (i = NDDATA - 1; i >= 0; i--)
679 if (lp->d_drivedata[i])
683 for (j = 0; j <= i; j++)
684 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
685 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
687 "# size offset fstype [fsize bsize bps/cpg]\n");
688 pp = lp->d_partitions;
689 for (i = 0; i < lp->d_npartitions; i++, pp++) {
691 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
692 (u_long)pp->p_size, (u_long)pp->p_offset);
693 if (pp->p_fstype < FSMAXTYPES)
694 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
696 fprintf(f, "%8d", pp->p_fstype);
697 switch (pp->p_fstype) {
699 case FS_UNUSED: /* XXX */
700 fprintf(f, " %5lu %5lu %5.5s ",
702 (u_long)(pp->p_fsize * pp->p_frag), "");
706 fprintf(f, " %5lu %5lu %5u ",
708 (u_long)(pp->p_fsize * pp->p_frag),
713 fprintf(f, " %5lu %5lu %5d",
715 (u_long)(pp->p_fsize * pp->p_frag),
720 fprintf(f, "%20.20s", "");
723 fprintf(f, "\t# (Cyl. %4lu",
724 (u_long)(pp->p_offset / lp->d_secpercyl));
725 if (pp->p_offset % lp->d_secpercyl)
730 (u_long)((pp->p_offset + pp->p_size +
731 lp->d_secpercyl - 1) /
732 lp->d_secpercyl - 1));
733 if (pp->p_size % lp->d_secpercyl)
742 edit(struct disklabel *lp, int f)
745 struct disklabel label;
748 if ((fd = mkstemp(tmpfil)) == -1 ||
749 (fp = fdopen(fd, "w")) == NULL) {
750 warnx("can't create %s", tmpfil);
758 fp = fopen(tmpfil, "r");
760 warnx("can't reopen %s for reading", tmpfil);
763 bzero((char *)&label, sizeof(label));
764 if (getasciilabel(fp, &label)) {
766 if (writelabel(f, bootarea, lp) == 0) {
773 printf("re-edit the label? [y]: "); fflush(stdout);
775 if (c != EOF && c != (int)'\n')
776 while (getchar() != (int)'\n')
792 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
793 while ((pid = fork()) < 0) {
794 if (errno == EPROCLIM) {
795 warnx("you have too many processes");
798 if (errno != EAGAIN) {
808 if ((ed = getenv("EDITOR")) == (char *)0)
810 execlp(ed, ed, tmpfil, (char *)0);
813 while ((xpid = wait(&status)) >= 0)
824 while (*cp != '\0' && isspace(*cp))
826 if (*cp == '\0' || *cp == '#')
836 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
838 if ((c = *cp) != '\0') {
847 * Read an ascii label in from fd f,
848 * in the same format as that put out by display(),
852 getasciilabel(FILE *f, struct disklabel *lp)
857 char *tp, line[BUFSIZ];
859 int lineno = 0, errors = 0;
862 char unknown[] = "unknown";
864 bzero(&part_set, sizeof(part_set));
865 bzero(&part_size_type, sizeof(part_size_type));
866 bzero(&part_offset_type, sizeof(part_offset_type));
867 lp->d_bbsize = BBSIZE; /* XXX */
868 lp->d_sbsize = SBSIZE; /* XXX */
869 while (fgets(line, sizeof(line) - 1, f)) {
871 if ((cp = strchr(line,'\n')) != 0)
876 tp = strchr(cp, ':');
878 fprintf(stderr, "line %d: syntax error\n", lineno);
882 *tp++ = '\0', tp = skip(tp);
883 if (streq(cp, "type")) {
887 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
888 if (*cpp && streq(*cpp, tp)) {
889 lp->d_type = cpp - dktypenames;
892 if (cpp < &dktypenames[DKMAXTYPES])
894 v = strtoul(tp, NULL, 10);
896 fprintf(stderr, "line %d:%s %lu\n", lineno,
897 "Warning, unknown disk type", v);
901 if (streq(cp, "flags")) {
902 for (v = 0; (cp = tp) && *cp != '\0';) {
904 if (streq(cp, "removeable"))
905 v |= 0; /* obsolete */
906 else if (streq(cp, "ecc"))
907 v |= 0; /* obsolete */
908 else if (streq(cp, "badsect"))
909 v |= 0; /* obsolete */
912 "line %d: %s: bad flag\n",
920 if (streq(cp, "drivedata")) {
921 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
922 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
927 if (sscanf(cp, "%lu partitions", &v) == 1) {
928 if (v == 0 || v > MAXPARTITIONS) {
930 "line %d: bad # of partitions\n", lineno);
931 lp->d_npartitions = MAXPARTITIONS;
934 lp->d_npartitions = v;
939 if (streq(cp, "disk")) {
940 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
943 if (streq(cp, "label")) {
944 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
947 if (streq(cp, "bytes/sector")) {
948 v = strtoul(tp, NULL, 10);
949 if (v == 0 || (v % DEV_BSIZE) != 0) {
951 "line %d: %s: bad sector size\n",
958 if (streq(cp, "sectors/track")) {
959 v = strtoul(tp, NULL, 10);
960 #if (ULONG_MAX != 0xffffffffUL)
961 if (v == 0 || v > 0xffffffff) {
965 fprintf(stderr, "line %d: %s: bad %s\n",
972 if (streq(cp, "sectors/cylinder")) {
973 v = strtoul(tp, NULL, 10);
975 fprintf(stderr, "line %d: %s: bad %s\n",
982 if (streq(cp, "tracks/cylinder")) {
983 v = strtoul(tp, NULL, 10);
985 fprintf(stderr, "line %d: %s: bad %s\n",
992 if (streq(cp, "cylinders")) {
993 v = strtoul(tp, NULL, 10);
995 fprintf(stderr, "line %d: %s: bad %s\n",
999 lp->d_ncylinders = v;
1002 if (streq(cp, "sectors/unit")) {
1003 v = strtoul(tp, NULL, 10);
1005 fprintf(stderr, "line %d: %s: bad %s\n",
1009 lp->d_secperunit = v;
1012 if (streq(cp, "rpm")) {
1013 v = strtoul(tp, NULL, 10);
1014 if (v == 0 || v > USHRT_MAX) {
1015 fprintf(stderr, "line %d: %s: bad %s\n",
1022 if (streq(cp, "interleave")) {
1023 v = strtoul(tp, NULL, 10);
1024 if (v == 0 || v > USHRT_MAX) {
1025 fprintf(stderr, "line %d: %s: bad %s\n",
1029 lp->d_interleave = v;
1032 if (streq(cp, "trackskew")) {
1033 v = strtoul(tp, NULL, 10);
1034 if (v > USHRT_MAX) {
1035 fprintf(stderr, "line %d: %s: bad %s\n",
1039 lp->d_trackskew = v;
1042 if (streq(cp, "cylinderskew")) {
1043 v = strtoul(tp, NULL, 10);
1044 if (v > USHRT_MAX) {
1045 fprintf(stderr, "line %d: %s: bad %s\n",
1052 if (streq(cp, "headswitch")) {
1053 v = strtoul(tp, NULL, 10);
1054 lp->d_headswitch = v;
1057 if (streq(cp, "track-to-track seek")) {
1058 v = strtoul(tp, NULL, 10);
1062 /* the ':' was removed above */
1063 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1065 "line %d: %s: Unknown disklabel field\n", lineno,
1071 /* Process a partition specification line. */
1073 if (part >= lp->d_npartitions) {
1075 "line %d: partition name out of range a-%c: %s\n",
1076 lineno, 'a' + lp->d_npartitions - 1, cp);
1082 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1087 errors += checklabel(lp);
1088 return (errors == 0);
1091 #define NXTNUM(n) do { \
1093 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1096 cp = tp, tp = word(cp); \
1097 (n) = strtoul(cp, NULL, 10); \
1101 /* retain 1 character following number */
1102 #define NXTWORD(w,n) do { \
1104 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1108 cp = tp, tp = word(cp); \
1109 (n) = strtoul(cp, &tmp, 10); \
1110 if (tmp) (w) = *tmp; \
1115 * Read a partition line into partition `part' in the specified disklabel.
1116 * Return 0 on success, 1 on failure.
1119 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1121 struct partition *pp;
1126 pp = &lp->d_partitions[part];
1130 NXTWORD(part_size_type[part],v);
1131 if (v == 0 && part_size_type[part] != '*') {
1133 "line %d: %s: bad partition size\n", lineno, cp);
1139 NXTWORD(part_offset_type[part],v);
1140 if (v == 0 && part_offset_type[part] != '*' &&
1141 part_offset_type[part] != '\0') {
1143 "line %d: %s: bad partition offset\n", lineno, cp);
1147 cp = tp, tp = word(cp);
1148 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1149 if (*cpp && streq(*cpp, cp))
1152 pp->p_fstype = cpp - fstypenames;
1155 v = strtoul(cp, NULL, 10);
1158 if (v >= FSMAXTYPES) {
1160 "line %d: Warning, unknown filesystem type %s\n",
1167 switch (pp->p_fstype) {
1170 * allow us to accept defaults for
1174 NXTNUM(pp->p_fsize);
1175 if (pp->p_fsize == 0)
1178 pp->p_frag = v / pp->p_fsize;
1180 /* else default to 0's */
1183 /* These happen to be the same */
1187 NXTNUM(pp->p_fsize);
1188 if (pp->p_fsize == 0)
1191 pp->p_frag = v / pp->p_fsize;
1195 * FIX! poor attempt at adaptive
1198 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1200 * FIX! These are too low, but are traditional
1202 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1203 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1205 pp->p_cpg = DEFAULT_NEWFS_CPG;
1207 pp->p_fsize = BIG_NEWFS_FRAG;
1208 pp->p_frag = BIG_NEWFS_BLOCK /
1210 pp->p_cpg = BIG_NEWFS_CPG;
1220 * Check disklabel for errors and fill in
1221 * derived fields according to supplied values.
1224 checklabel(struct disklabel *lp)
1226 struct partition *pp;
1229 u_long total_size, total_percent, current_offset;
1230 int seen_default_offset;
1233 struct partition *pp2;
1235 if (lp->d_secsize == 0) {
1236 fprintf(stderr, "sector size 0\n");
1239 if (lp->d_nsectors == 0) {
1240 fprintf(stderr, "sectors/track 0\n");
1243 if (lp->d_ntracks == 0) {
1244 fprintf(stderr, "tracks/cylinder 0\n");
1247 if (lp->d_ncylinders == 0) {
1248 fprintf(stderr, "cylinders/unit 0\n");
1252 Warning("revolutions/minute 0");
1253 if (lp->d_secpercyl == 0)
1254 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1255 if (lp->d_secperunit == 0)
1256 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1257 if (lp->d_bbsize == 0) {
1258 fprintf(stderr, "boot block size 0\n");
1260 } else if (lp->d_bbsize % lp->d_secsize)
1261 Warning("boot block size %% sector-size != 0");
1262 if (lp->d_sbsize == 0) {
1263 fprintf(stderr, "super block size 0\n");
1265 } else if (lp->d_sbsize % lp->d_secsize)
1266 Warning("super block size %% sector-size != 0");
1267 if (lp->d_npartitions > MAXPARTITIONS)
1268 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1269 (u_long)lp->d_npartitions, MAXPARTITIONS);
1271 /* first allocate space to the partitions, then offsets */
1272 total_size = 0; /* in sectors */
1273 total_percent = 0; /* in percent */
1275 /* find all fixed partitions */
1276 for (i = 0; i < lp->d_npartitions; i++) {
1277 pp = &lp->d_partitions[i];
1279 if (part_size_type[i] == '*') {
1280 if (i == RAW_PART) {
1281 pp->p_size = lp->d_secperunit;
1284 Warning("Too many '*' partitions (%c and %c)",
1285 hog_part + 'a',i + 'a');
1293 switch (part_size_type[i]) {
1295 total_percent += size;
1303 size *= 1024ULL * 1024ULL;
1307 size *= 1024ULL * 1024ULL * 1024ULL;
1312 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1315 /* don't count %'s yet */
1316 if (part_size_type[i] != '%') {
1318 * for all not in sectors, convert to
1321 if (part_size_type[i] != '\0') {
1322 if (size % lp->d_secsize != 0)
1323 Warning("partition %c not an integer number of sectors",
1325 size /= lp->d_secsize;
1328 /* else already in sectors */
1335 /* handle % partitions - note %'s don't need to add up to 100! */
1336 if (total_percent != 0) {
1337 long free_space = lp->d_secperunit - total_size;
1338 if (total_percent > 100) {
1339 fprintf(stderr,"total percentage %lu is greater than 100\n",
1344 if (free_space > 0) {
1345 for (i = 0; i < lp->d_npartitions; i++) {
1346 pp = &lp->d_partitions[i];
1347 if (part_set[i] && part_size_type[i] == '%') {
1348 /* careful of overflows! and integer roundoff */
1349 pp->p_size = ((double)pp->p_size/100) * free_space;
1350 total_size += pp->p_size;
1352 /* FIX we can lose a sector or so due to roundoff per
1353 partition. A more complex algorithm could avoid that */
1358 "%ld sectors available to give to '*' and '%%' partitions\n",
1361 /* fix? set all % partitions to size 0? */
1364 /* give anything remaining to the hog partition */
1365 if (hog_part != -1) {
1366 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1367 total_size = lp->d_secperunit;
1370 /* Now set the offsets for each partition */
1371 current_offset = 0; /* in sectors */
1372 seen_default_offset = 0;
1373 for (i = 0; i < lp->d_npartitions; i++) {
1375 pp = &lp->d_partitions[i];
1377 if (part_offset_type[i] == '*') {
1378 if (i == RAW_PART) {
1381 pp->p_offset = current_offset;
1382 seen_default_offset = 1;
1385 /* allow them to be out of order for old-style tables */
1386 if (pp->p_offset < current_offset &&
1387 seen_default_offset && i != RAW_PART &&
1388 pp->p_fstype != FS_VINUM) {
1390 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1391 (long)pp->p_offset,i+'a',current_offset);
1393 "Labels with any *'s for offset must be in ascending order by sector\n");
1395 } else if (pp->p_offset != current_offset &&
1396 i != RAW_PART && seen_default_offset) {
1398 * this may give unneeded warnings if
1399 * partitions are out-of-order
1402 "Offset %ld for partition %c doesn't match expected value %ld",
1403 (long)pp->p_offset, i + 'a', current_offset);
1407 current_offset = pp->p_offset + pp->p_size;
1411 for (i = 0; i < lp->d_npartitions; i++) {
1413 pp = &lp->d_partitions[i];
1414 if (pp->p_size == 0 && pp->p_offset != 0)
1415 Warning("partition %c: size 0, but offset %lu",
1416 part, (u_long)pp->p_offset);
1418 if (pp->p_size % lp->d_secpercyl)
1419 Warning("partition %c: size %% cylinder-size != 0",
1421 if (pp->p_offset % lp->d_secpercyl)
1422 Warning("partition %c: offset %% cylinder-size != 0",
1425 if (pp->p_offset > lp->d_secperunit) {
1427 "partition %c: offset past end of unit\n", part);
1430 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1432 "partition %c: partition extends past end of unit\n",
1438 if (pp->p_fstype != FS_UNUSED)
1439 Warning("partition %c is not marked as unused!",part);
1440 if (pp->p_offset != 0)
1441 Warning("partition %c doesn't start at 0!",part);
1442 if (pp->p_size != lp->d_secperunit)
1443 Warning("partition %c doesn't cover the whole unit!",part);
1445 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1446 (pp->p_size != lp->d_secperunit)) {
1447 Warning("An incorrect partition %c may cause problems for "
1448 "standard system utilities",part);
1452 /* check for overlaps */
1453 /* this will check for all possible overlaps once and only once */
1454 for (j = 0; j < i; j++) {
1455 pp2 = &lp->d_partitions[j];
1456 if (j != RAW_PART && i != RAW_PART &&
1457 pp->p_fstype != FS_VINUM &&
1458 pp2->p_fstype != FS_VINUM &&
1459 part_set[i] && part_set[j]) {
1460 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1461 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1462 pp2->p_offset >= pp->p_offset)) {
1463 fprintf(stderr,"partitions %c and %c overlap!\n",
1470 for (; i < 8 || i < lp->d_npartitions; i++) {
1472 pp = &lp->d_partitions[i];
1473 if (pp->p_size || pp->p_offset)
1474 Warning("unused partition %c: size %d offset %lu",
1475 'a' + i, pp->p_size, (u_long)pp->p_offset);
1481 * When operating on a "virgin" disk, try getting an initial label
1482 * from the associated device driver. This might work for all device
1483 * drivers that are able to fetch some initial device parameters
1484 * without even having access to a (BSD) disklabel, like SCSI disks,
1485 * most IDE drives, or vn devices.
1487 * The device name must be given in its "canonical" form.
1489 static struct disklabel dlab;
1492 getvirginlabel(void)
1494 struct disklabel *dl = &dlab;
1495 char nambuf[BBSIZE];
1498 if (dkname[0] == '/') {
1499 warnx("\"auto\" requires the usage of a canonical disk name");
1502 snprintf(nambuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1503 if ((f = open(nambuf, O_RDONLY)) == -1) {
1504 warn("cannot open %s", nambuf);
1509 * Try to use the new get-virgin-label ioctl. If it fails,
1510 * fallback to the old get-disdk-info ioctl.
1512 if (ioctl(f, DIOCGDVIRGIN, dl) < 0) {
1513 if (ioctl(f, DIOCGDINFO, dl) < 0) {
1514 warn("ioctl DIOCGDINFO");
1524 getdisklabelfromdisktab(const char *name)
1527 struct disklabel *dl = &dlab;
1530 if ((dt = getdisktabbyname(name)) == NULL)
1532 dl->d_magic = DISKMAGIC;
1533 dl->d_type = dt->d_typeid;
1535 dl->d_secsize = dt->d_media_blksize;
1536 dl->d_nsectors = dt->d_secpertrack;
1537 dl->d_ntracks = dt->d_nheads;
1538 dl->d_ncylinders = dt->d_ncylinders;
1539 dl->d_secpercyl = dt->d_secpercyl;
1540 dl->d_secperunit = dt->d_media_blocks;
1541 dl->d_rpm = dt->d_rpm;
1542 dl->d_interleave = dt->d_interleave;
1543 dl->d_trackskew = dt->d_trackskew;
1544 dl->d_cylskew = dt->d_cylskew;
1545 dl->d_headswitch = dt->d_headswitch;
1546 dl->d_trkseek = dt->d_trkseek;
1547 dl->d_magic2 = DISKMAGIC;
1548 dl->d_npartitions = dt->d_npartitions;
1549 dl->d_bbsize = dt->d_bbsize;
1550 dl->d_sbsize = dt->d_sbsize;
1551 for (i = 0; i < dt->d_npartitions; ++i) {
1552 struct partition *dlp = &dl->d_partitions[i];
1553 struct dt_partition *dtp = &dt->d_partitions[i];
1555 dlp->p_size = dtp->p_size;
1556 dlp->p_offset = dtp->p_offset;
1557 dlp->p_fsize = dtp->p_fsize;
1558 dlp->p_fstype = dtp->p_fstype;
1559 dlp->p_frag = dtp->p_fsize;
1565 * If we are installing a boot program that doesn't fit in d_bbsize
1566 * we need to mark those partitions that the boot overflows into.
1567 * This allows newfs to prevent creation of a filesystem where it might
1568 * clobber bootstrap code.
1571 setbootflag(struct disklabel *lp)
1573 struct partition *pp;
1580 boffset = bootsize / lp->d_secsize;
1581 for (i = 0; i < lp->d_npartitions; i++) {
1583 pp = &lp->d_partitions[i];
1584 if (pp->p_size == 0)
1586 if (boffset <= pp->p_offset) {
1587 if (pp->p_fstype == FS_BOOT)
1588 pp->p_fstype = FS_UNUSED;
1589 } else if (pp->p_fstype != FS_BOOT) {
1590 if (pp->p_fstype != FS_UNUSED) {
1592 "boot overlaps used partition %c\n",
1596 pp->p_fstype = FS_BOOT;
1597 Warning("boot overlaps partition %c, %s",
1598 part, "marked as FS_BOOT");
1603 errx(4, "cannot install boot program");
1608 Warning(const char *fmt, ...)
1612 fprintf(stderr, "Warning, ");
1614 vfprintf(stderr, fmt, ap);
1615 fprintf(stderr, "\n");
1620 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1621 * and earlier DFly bb's are packed against the old disklabel and a new
1622 * disklabel would blow them up. This is a hack that should be removed
1623 * in 2006 sometime (if ever).
1627 checkoldboot(int f, const char *bootbuffer)
1631 if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0)
1633 lseek(f, (off_t)0, SEEK_SET);
1634 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1636 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1638 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1647 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",
1648 "usage: disklabel [-r] disk",
1649 "\t\t(to read label)",
1650 " disklabel -w [-r] [-n] disk type [ packid ]",
1651 "\t\t(to write label with existing boot program)",
1652 " disklabel -e [-r] [-n] disk",
1653 "\t\t(to edit label)",
1654 " disklabel -R [-r] [-n] disk protofile",
1655 "\t\t(to restore label with existing boot program)",
1657 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1658 "\t\t(to install boot program with existing label)",
1659 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1660 "\t\t(to write label and boot program)",
1661 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1662 "\t\t(to restore label and boot program)",
1664 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1665 "\t\t(to install boot program with existing on-disk label)",
1666 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1667 "\t\t(to write label and install boot program)",
1668 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1669 "\t\t(to restore label and install boot program)",
1671 " disklabel [-NW] disk",
1672 "\t\t(to write disable/enable label)");
1674 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1675 "usage: disklabel [-r] disk", "(to read label)",
1676 " disklabel -w [-r] [-n] disk type [ packid ]",
1677 "\t\t(to write label)",
1678 " disklabel -e [-r] [-n] disk",
1679 "\t\t(to edit label)",
1680 " disklabel -R [-r] [-n] disk protofile",
1681 "\t\t(to restore label)",
1682 " disklabel [-NW] disk",
1683 "\t\t(to write disable/enable label)");