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.18 2007/05/20 23:21:34 dillon Exp $
43 #include <sys/param.h>
48 #include <sys/disklabel.h>
49 #include <sys/diskslice.h>
50 #include <sys/diskmbr.h>
53 #include <vfs/ufs/dinode.h>
54 #include <vfs/ufs/fs.h>
66 #include "pathnames.h"
69 * Disklabel: read and write disklabels.
70 * The label is usually placed on one of the first sectors of the disk.
71 * Many machines also place a bootstrap in the same area,
72 * in which case the label is embedded in the bootstrap.
73 * The bootstrap source must leave space at the proper offset
74 * for the label on such machines.
78 #define BBSIZE 8192 /* size of boot area, with label */
81 /* FIX! These are too low, but are traditional */
82 #define DEFAULT_NEWFS_BLOCK 8192U
83 #define DEFAULT_NEWFS_FRAG 1024U
84 #define DEFAULT_NEWFS_CPG 16U
86 #define BIG_NEWFS_BLOCK 16384U
87 #define BIG_NEWFS_FRAG 2048U
88 #define BIG_NEWFS_CPG 64U
92 void makelabel(const char *, const char *, struct disklabel *);
93 int writelabel(int, const char *, struct disklabel *);
94 void l_perror(const char *);
95 struct disklabel *readlabel(int);
96 struct disklabel *makebootarea(char *, struct disklabel *, int);
97 void display(FILE *, const struct disklabel *);
98 int edit(struct disklabel *, int);
102 int getasciilabel(FILE *, struct disklabel *);
103 int getasciipartspec(char *, struct disklabel *, int, int);
104 int checklabel(struct disklabel *);
105 void setbootflag(struct disklabel *);
106 void Warning(const char *, ...) __printflike(1, 2);
108 int checkoldboot(int, const char *);
109 struct disklabel *getvirginlabel(void);
110 struct disklabel *getdisklabelfromdisktab(const char *name);
112 #define DEFEDITOR _PATH_VI
113 #define streq(a,b) (strcmp(a,b) == 0)
117 char tmpfil[] = PATH_TMPFILE;
119 char namebuf[BBSIZE], *np = namebuf;
120 struct disklabel lab;
121 char bootarea[BBSIZE];
123 #define MAX_PART ('z')
124 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
125 char part_size_type[MAX_NUM_PARTS];
126 char part_offset_type[MAX_NUM_PARTS];
127 int part_set[MAX_NUM_PARTS];
130 int installboot; /* non-zero if we should install a boot program */
131 char *bootbuf; /* pointer to buffer with remainder of boot prog */
132 int bootsize; /* size of remaining boot program */
133 char *xxboot; /* primary boot */
134 char *bootxx; /* secondary boot */
135 char boot0[MAXPATHLEN];
136 char boot1[MAXPATHLEN];
140 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
144 int disable_write; /* set to disable writing to disk label */
148 #define OPTIONS "BNRWb:denrs:w"
150 #define OPTIONS "BNRWb:enrs:w"
154 main(int argc, char *argv[])
156 struct disklabel *lp;
158 int ch, f = 0, flag, error = 0;
161 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
236 if (dkname[0] != '/') {
237 sprintf(np, "%s%s", _PATH_DEV, dkname);
239 np += strlen(specname) + 1;
243 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
244 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
245 sprintf(specname, "%s%s", _PATH_DEV, dkname);
246 np = namebuf + strlen(specname) + 1;
247 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
250 err(4, "%s", specname);
266 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
267 err(4, "ioctl DIOCWLABEL");
275 error = checklabel(lp);
276 if (checkoldboot(f, NULL))
277 warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
282 if (installboot && argc == 3) {
283 makelabel(argv[2], 0, &lab);
287 * We only called makelabel() for its side effect
288 * of setting the bootstrap file names. Discard
289 * all changes to `lab' so that all values in the
290 * final label come from the ASCII label.
292 bzero((char *)&lab, sizeof(lab));
297 if (!(t = fopen(argv[1], "r")))
298 err(4, "%s", argv[1]);
299 if (!getasciilabel(t, &lab))
301 lp = makebootarea(bootarea, &lab, f);
303 error = writelabel(f, bootarea, lp);
313 makelabel(argv[1], name, &lab);
314 lp = makebootarea(bootarea, &lab, f);
316 if (checklabel(lp) == 0)
317 error = writelabel(f, bootarea, lp);
322 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
323 err(4, "ioctl DIOCWLABEL");
329 struct disklabel tlab;
334 makelabel(argv[1], 0, &lab);
335 lp = makebootarea(bootarea, &lab, f);
337 if (checklabel(lp) == 0)
338 error = writelabel(f, bootarea, lp);
347 * Construct a prototype disklabel from /etc/disktab. As a side
348 * effect, set the names of the primary and secondary boot files
352 makelabel(const char *type, const char *name, struct disklabel *lp)
354 struct disklabel *dp;
356 if (strcmp(type, "auto") == 0)
357 dp = getvirginlabel();
359 dp = getdisklabelfromdisktab(type);
361 errx(1, "%s: unknown disk type", type);
365 * NOTE: boot control files may no longer be specified in disktab.
368 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
372 writelabel(int f, const char *boot, struct disklabel *lp)
377 Warning("write to disk label supressed - label was as follows:");
381 /* make sure we are not overwriting our boot code */
382 if (checkoldboot(f, boot))
383 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
385 lp->d_magic = DISKMAGIC;
386 lp->d_magic2 = DISKMAGIC;
388 lp->d_checksum = dkcksum(lp);
391 * First set the kernel disk label,
392 * then write a label to the raw disk.
393 * If the SDINFO ioctl fails because it is unimplemented,
394 * keep going; otherwise, the kernel consistency checks
395 * may prevent us from changing the current (in-core)
398 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
399 errno != ENODEV && errno != ENOTTY) {
400 l_perror("ioctl DIOCSDINFO");
403 lseek(f, (off_t)0, SEEK_SET);
406 * write enable label sector before write (if necessary),
407 * disable after writing.
410 if (ioctl(f, DIOCWLABEL, &flag) < 0)
411 warn("ioctl DIOCWLABEL");
412 if (write(f, boot, lp->d_bbsize) != (ssize_t)lp->d_bbsize) {
418 * Output the remainder of the disklabel
420 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
426 ioctl(f, DIOCWLABEL, &flag);
427 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
428 l_perror("ioctl DIOCWDINFO");
436 l_perror(const char *s)
441 warnx("%s: no disk label on disk;", s);
442 fprintf(stderr, "add \"-r\" to install initial label\n");
446 warnx("%s: label magic number or checksum is wrong!", s);
447 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
451 warnx("%s: open partition would move or shrink", s);
455 warnx("%s: '%c' partition must start at beginning of disk",
466 * Fetch disklabel for disk.
467 * Use ioctl to get label unless -r flag is given.
472 struct disklabel *lp;
475 if (read(f, bootarea, BBSIZE) < BBSIZE)
476 err(4, "%s", specname);
477 for (lp = (struct disklabel *)bootarea;
478 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
479 lp = (struct disklabel *)((char *)lp + 16))
480 if (lp->d_magic == DISKMAGIC &&
481 lp->d_magic2 == DISKMAGIC)
483 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
484 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
487 "bad pack magic number (label is damaged, or pack is unlabeled)");
490 if (ioctl(f, DIOCGDINFO, lp) < 0)
491 err(4, "ioctl DIOCGDINFO");
497 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
498 * Returns a pointer to the disklabel portion of the bootarea.
501 makebootarea(char *boot, struct disklabel *dp, int f)
503 struct disklabel *lp;
512 unsigned int i, found;
516 if (dp->d_secsize == 0) {
517 dp->d_secsize = DEV_BSIZE;
518 dp->d_bbsize = BBSIZE;
520 lp = (struct disklabel *)
521 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
522 bzero((char *)lp, sizeof *lp);
525 * If we are not installing a boot program but we are installing a
526 * label on disk then we must read the current bootarea so we don't
527 * clobber the existing boot.
531 if (read(f, boot, BBSIZE) < BBSIZE)
532 err(4, "%s", specname);
533 bzero((char *)lp, sizeof *lp);
538 * We are installing a boot program. Determine the name(s) and
539 * read them into the appropriate places in the boot area.
541 if (!xxboot || !bootxx) {
543 if ((p = strrchr(dkname, '/')) == NULL)
547 while (*p && !isdigit(*p))
552 sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
557 sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
564 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
565 xxboot, bootxx ? bootxx : "NONE");
570 * 1. One-piece bootstrap (hp300/hp800)
571 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
572 * is remembered and written later following the bootarea.
573 * 2. Two-piece bootstraps (vax/i386?/mips?)
574 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
575 * bytes of bootarea, remaining d_bbsize-d_secsize filled
578 b = open(xxboot, O_RDONLY);
580 err(4, "%s", xxboot);
585 * The i386 has the so-called fdisk table embedded into the
586 * primary bootstrap. We take care to not clobber it, but
587 * only if it does already contain some data. (Otherwise,
588 * the xxboot provides a template.)
590 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
591 err(4, "%s", xxboot);
592 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
594 if (read(b, boot, (int)dp->d_secsize) < 0)
595 err(4, "%s", xxboot);
598 for (i = DOSPARTOFF, found = 0;
599 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
601 found = tmpbuf[i] != 0;
603 memcpy((void *)&boot[DOSPARTOFF],
604 (void *)&tmpbuf[DOSPARTOFF],
605 NDOSPART * sizeof(struct dos_partition));
608 b = open(bootxx, O_RDONLY);
610 err(4, "%s", bootxx);
611 if (fstat(b, &sb) != 0)
612 err(4, "%s", bootxx);
613 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
614 errx(4, "%s too large", bootxx);
615 if (read(b, &boot[dp->d_secsize],
616 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
617 err(4, "%s", bootxx);
618 #else /* !(NUMBOOT > 1) */
619 if (read(b, boot, (int)dp->d_bbsize) < 0)
620 err(4, "%s", xxboot);
621 if (fstat(b, &sb) != 0)
622 err(4, "%s", xxboot);
623 bootsize = (int)sb.st_size - dp->d_bbsize;
625 /* XXX assume d_secsize is a power of two */
626 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
627 bootbuf = (char *)malloc((size_t)bootsize);
629 err(4, "%s", xxboot);
630 if (read(b, bootbuf, bootsize) < 0) {
632 err(4, "%s", xxboot);
635 #endif /* NUMBOOT > 1 */
637 #endif /* NUMBOOT > 0 */
639 * Make sure no part of the bootstrap is written in the area
640 * reserved for the label.
642 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
644 errx(2, "bootstrap doesn't leave room for disk label");
649 display(FILE *f, const struct disklabel *lp)
652 const struct partition *pp;
654 fprintf(f, "# %s:\n", specname);
655 if (lp->d_type < DKMAXTYPES)
656 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
658 fprintf(f, "type: %u\n", lp->d_type);
659 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
661 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
663 fprintf(f, "flags:");
665 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
666 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
667 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
668 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
669 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
670 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
671 fprintf(f, "rpm: %u\n", lp->d_rpm);
672 fprintf(f, "interleave: %u\n", lp->d_interleave);
673 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
674 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
675 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
676 (u_long)lp->d_headswitch);
677 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
678 (u_long)lp->d_trkseek);
679 fprintf(f, "drivedata: ");
680 for (i = NDDATA - 1; i >= 0; i--) {
681 if (lp->d_drivedata[i])
686 for (j = 0; j <= i; j++)
687 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
688 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
690 "# size offset fstype \n");
691 pp = lp->d_partitions;
692 for (i = 0; i < lp->d_npartitions; i++, pp++) {
694 u_long onemeg = 1024 * 1024 / lp->d_secsize;
695 fprintf(f, " %c: ", 'a' + i);
697 fprintf(f, "%10lu ", (u_long)pp->p_size);
698 fprintf(f, "%10lu ", (u_long)pp->p_offset);
699 if (pp->p_fstype < FSMAXTYPES)
700 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
702 fprintf(f, "%8d", pp->p_fstype);
704 fprintf(f, "\t# %11.3fM", (double)pp->p_size / onemeg);
712 edit(struct disklabel *lp, int f)
715 struct disklabel label;
718 if ((fd = mkstemp(tmpfil)) == -1 ||
719 (fp = fdopen(fd, "w")) == NULL) {
720 warnx("can't create %s", tmpfil);
728 fp = fopen(tmpfil, "r");
730 warnx("can't reopen %s for reading", tmpfil);
733 bzero((char *)&label, sizeof(label));
734 if (getasciilabel(fp, &label)) {
736 if (writelabel(f, bootarea, lp) == 0) {
743 printf("re-edit the label? [y]: "); fflush(stdout);
745 if (c != EOF && c != (int)'\n')
746 while (getchar() != (int)'\n')
762 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
763 while ((pid = fork()) < 0) {
764 if (errno == EPROCLIM) {
765 warnx("you have too many processes");
768 if (errno != EAGAIN) {
778 if ((ed = getenv("EDITOR")) == (char *)0)
780 execlp(ed, ed, tmpfil, (char *)0);
783 while ((xpid = wait(&status)) >= 0)
794 while (*cp != '\0' && isspace(*cp))
796 if (*cp == '\0' || *cp == '#')
806 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
808 if ((c = *cp) != '\0') {
817 * Read an ascii label in from fd f,
818 * in the same format as that put out by display(),
822 getasciilabel(FILE *f, struct disklabel *lp)
827 char *tp, line[BUFSIZ];
829 int lineno = 0, errors = 0;
832 char unknown[] = "unknown";
834 bzero(&part_set, sizeof(part_set));
835 bzero(&part_size_type, sizeof(part_size_type));
836 bzero(&part_offset_type, sizeof(part_offset_type));
837 lp->d_bbsize = BBSIZE; /* XXX */
838 lp->d_sbsize = SBSIZE; /* XXX */
839 while (fgets(line, sizeof(line) - 1, f)) {
841 if ((cp = strchr(line,'\n')) != 0)
846 tp = strchr(cp, ':');
848 fprintf(stderr, "line %d: syntax error\n", lineno);
852 *tp++ = '\0', tp = skip(tp);
853 if (streq(cp, "type")) {
857 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) {
858 if (*cpp && streq(*cpp, tp)) {
859 lp->d_type = cpp - dktypenames;
863 if (cpp < &dktypenames[DKMAXTYPES])
865 v = strtoul(tp, NULL, 10);
866 if (v >= DKMAXTYPES) {
867 fprintf(stderr, "line %d:%s %lu\n", lineno,
868 "Warning, unknown disk type", v);
873 if (streq(cp, "flags")) {
874 for (v = 0; (cp = tp) && *cp != '\0';) {
876 if (streq(cp, "removeable"))
877 v |= 0; /* obsolete */
878 else if (streq(cp, "ecc"))
879 v |= 0; /* obsolete */
880 else if (streq(cp, "badsect"))
881 v |= 0; /* obsolete */
884 "line %d: %s: bad flag\n",
892 if (streq(cp, "drivedata")) {
893 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
894 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
899 if (sscanf(cp, "%lu partitions", &v) == 1) {
900 if (v == 0 || v > MAXPARTITIONS) {
902 "line %d: bad # of partitions\n", lineno);
903 lp->d_npartitions = MAXPARTITIONS;
906 lp->d_npartitions = v;
911 if (streq(cp, "disk")) {
912 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
915 if (streq(cp, "label")) {
916 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
919 if (streq(cp, "bytes/sector")) {
920 v = strtoul(tp, NULL, 10);
921 if (v == 0 || (v % DEV_BSIZE) != 0) {
923 "line %d: %s: bad sector size\n",
930 if (streq(cp, "sectors/track")) {
931 v = strtoul(tp, NULL, 10);
932 #if (ULONG_MAX != 0xffffffffUL)
933 if (v == 0 || v > 0xffffffff) {
937 fprintf(stderr, "line %d: %s: bad %s\n",
944 if (streq(cp, "sectors/cylinder")) {
945 v = strtoul(tp, NULL, 10);
947 fprintf(stderr, "line %d: %s: bad %s\n",
954 if (streq(cp, "tracks/cylinder")) {
955 v = strtoul(tp, NULL, 10);
957 fprintf(stderr, "line %d: %s: bad %s\n",
964 if (streq(cp, "cylinders")) {
965 v = strtoul(tp, NULL, 10);
967 fprintf(stderr, "line %d: %s: bad %s\n",
971 lp->d_ncylinders = v;
974 if (streq(cp, "sectors/unit")) {
975 v = strtoul(tp, NULL, 10);
977 fprintf(stderr, "line %d: %s: bad %s\n",
981 lp->d_secperunit = v;
984 if (streq(cp, "rpm")) {
985 v = strtoul(tp, NULL, 10);
986 if (v == 0 || v > USHRT_MAX) {
987 fprintf(stderr, "line %d: %s: bad %s\n",
994 if (streq(cp, "interleave")) {
995 v = strtoul(tp, NULL, 10);
996 if (v == 0 || v > USHRT_MAX) {
997 fprintf(stderr, "line %d: %s: bad %s\n",
1001 lp->d_interleave = v;
1004 if (streq(cp, "trackskew")) {
1005 v = strtoul(tp, NULL, 10);
1006 if (v > USHRT_MAX) {
1007 fprintf(stderr, "line %d: %s: bad %s\n",
1011 lp->d_trackskew = v;
1014 if (streq(cp, "cylinderskew")) {
1015 v = strtoul(tp, NULL, 10);
1016 if (v > USHRT_MAX) {
1017 fprintf(stderr, "line %d: %s: bad %s\n",
1024 if (streq(cp, "headswitch")) {
1025 v = strtoul(tp, NULL, 10);
1026 lp->d_headswitch = v;
1029 if (streq(cp, "track-to-track seek")) {
1030 v = strtoul(tp, NULL, 10);
1034 /* the ':' was removed above */
1035 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1037 "line %d: %s: Unknown disklabel field\n", lineno,
1043 /* Process a partition specification line. */
1045 if (part >= lp->d_npartitions) {
1047 "line %d: partition name out of range a-%c: %s\n",
1048 lineno, 'a' + lp->d_npartitions - 1, cp);
1054 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1059 errors += checklabel(lp);
1060 return (errors == 0);
1063 #define NXTNUM(n) do { \
1065 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1068 cp = tp, tp = word(cp); \
1069 (n) = strtoul(cp, NULL, 10); \
1073 /* retain 1 character following number */
1074 #define NXTWORD(w,n) do { \
1076 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1080 cp = tp, tp = word(cp); \
1081 (n) = strtoul(cp, &tmp, 10); \
1082 if (tmp) (w) = *tmp; \
1087 * Read a partition line into partition `part' in the specified disklabel.
1088 * Return 0 on success, 1 on failure.
1091 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1093 struct partition *pp;
1098 pp = &lp->d_partitions[part];
1105 NXTWORD(part_size_type[part],v);
1106 if (v == 0 && part_size_type[part] != '*') {
1108 "line %d: %s: bad partition size\n", lineno, cp);
1117 NXTWORD(part_offset_type[part],v);
1118 if (v == 0 && part_offset_type[part] != '*' &&
1119 part_offset_type[part] != '\0') {
1121 "line %d: %s: bad partition offset\n", lineno, cp);
1131 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1132 if (*cpp && streq(*cpp, cp))
1135 pp->p_fstype = cpp - fstypenames;
1138 v = strtoul(cp, NULL, 10);
1141 if (v >= FSMAXTYPES) {
1143 "line %d: Warning, unknown filesystem type %s\n",
1156 fprintf(stderr, "line %d: Warning, fragment, block, "
1157 "and bps/cpg fields are no\n"
1158 "longer supported and must be specified "
1159 "via newfs options instead.\n",
1166 * Check disklabel for errors and fill in
1167 * derived fields according to supplied values.
1170 checklabel(struct disklabel *lp)
1172 struct partition *pp;
1175 u_long total_size, total_percent, current_offset;
1176 int seen_default_offset;
1179 struct partition *pp2;
1181 if (lp->d_secsize == 0) {
1182 fprintf(stderr, "sector size 0\n");
1185 if (lp->d_nsectors == 0) {
1186 fprintf(stderr, "sectors/track 0\n");
1189 if (lp->d_ntracks == 0) {
1190 fprintf(stderr, "tracks/cylinder 0\n");
1193 if (lp->d_ncylinders == 0) {
1194 fprintf(stderr, "cylinders/unit 0\n");
1198 Warning("revolutions/minute 0");
1199 if (lp->d_secpercyl == 0)
1200 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1201 if (lp->d_secperunit == 0)
1202 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1203 if (lp->d_bbsize == 0) {
1204 fprintf(stderr, "boot block size 0\n");
1206 } else if (lp->d_bbsize % lp->d_secsize)
1207 Warning("boot block size %% sector-size != 0");
1208 if (lp->d_sbsize == 0) {
1209 fprintf(stderr, "super block size 0\n");
1211 } else if (lp->d_sbsize % lp->d_secsize)
1212 Warning("super block size %% sector-size != 0");
1213 if (lp->d_npartitions > MAXPARTITIONS)
1214 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1215 (u_long)lp->d_npartitions, MAXPARTITIONS);
1217 /* first allocate space to the partitions, then offsets */
1218 total_size = 0; /* in sectors */
1219 total_percent = 0; /* in percent */
1221 /* find all fixed partitions */
1222 for (i = 0; i < lp->d_npartitions; i++) {
1223 pp = &lp->d_partitions[i];
1225 if (part_size_type[i] == '*') {
1226 if (i == RAW_PART) {
1227 pp->p_size = lp->d_secperunit;
1230 Warning("Too many '*' partitions (%c and %c)",
1231 hog_part + 'a',i + 'a');
1239 switch (part_size_type[i]) {
1241 total_percent += size;
1249 size *= 1024ULL * 1024ULL;
1253 size *= 1024ULL * 1024ULL * 1024ULL;
1258 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1261 /* don't count %'s yet */
1262 if (part_size_type[i] != '%') {
1264 * for all not in sectors, convert to
1267 if (part_size_type[i] != '\0') {
1268 if (size % lp->d_secsize != 0)
1269 Warning("partition %c not an integer number of sectors",
1271 size /= lp->d_secsize;
1274 /* else already in sectors */
1281 /* handle % partitions - note %'s don't need to add up to 100! */
1282 if (total_percent != 0) {
1283 long free_space = lp->d_secperunit - total_size;
1284 if (total_percent > 100) {
1285 fprintf(stderr,"total percentage %lu is greater than 100\n",
1290 if (free_space > 0) {
1291 for (i = 0; i < lp->d_npartitions; i++) {
1292 pp = &lp->d_partitions[i];
1293 if (part_set[i] && part_size_type[i] == '%') {
1294 /* careful of overflows! and integer roundoff */
1295 pp->p_size = ((double)pp->p_size/100) * free_space;
1296 total_size += pp->p_size;
1298 /* FIX we can lose a sector or so due to roundoff per
1299 partition. A more complex algorithm could avoid that */
1304 "%ld sectors available to give to '*' and '%%' partitions\n",
1307 /* fix? set all % partitions to size 0? */
1310 /* give anything remaining to the hog partition */
1311 if (hog_part != -1) {
1312 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1313 total_size = lp->d_secperunit;
1316 /* Now set the offsets for each partition */
1317 current_offset = 0; /* in sectors */
1318 seen_default_offset = 0;
1319 for (i = 0; i < lp->d_npartitions; i++) {
1321 pp = &lp->d_partitions[i];
1323 if (part_offset_type[i] == '*') {
1324 if (i == RAW_PART) {
1327 pp->p_offset = current_offset;
1328 seen_default_offset = 1;
1331 /* allow them to be out of order for old-style tables */
1332 if (pp->p_offset < current_offset &&
1333 seen_default_offset && i != RAW_PART &&
1334 pp->p_fstype != FS_VINUM) {
1336 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1337 (long)pp->p_offset,i+'a',current_offset);
1339 "Labels with any *'s for offset must be in ascending order by sector\n");
1341 } else if (pp->p_offset != current_offset &&
1342 i != RAW_PART && seen_default_offset) {
1344 * this may give unneeded warnings if
1345 * partitions are out-of-order
1348 "Offset %ld for partition %c doesn't match expected value %ld",
1349 (long)pp->p_offset, i + 'a', current_offset);
1353 current_offset = pp->p_offset + pp->p_size;
1357 for (i = 0; i < lp->d_npartitions; i++) {
1359 pp = &lp->d_partitions[i];
1360 if (pp->p_size == 0 && pp->p_offset != 0)
1361 Warning("partition %c: size 0, but offset %lu",
1362 part, (u_long)pp->p_offset);
1364 if (pp->p_size % lp->d_secpercyl)
1365 Warning("partition %c: size %% cylinder-size != 0",
1367 if (pp->p_offset % lp->d_secpercyl)
1368 Warning("partition %c: offset %% cylinder-size != 0",
1371 if (pp->p_offset > lp->d_secperunit) {
1373 "partition %c: offset past end of unit\n", part);
1376 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1378 "partition %c: partition extends past end of unit\n",
1384 if (pp->p_fstype != FS_UNUSED)
1385 Warning("partition %c is not marked as unused!",part);
1386 if (pp->p_offset != 0)
1387 Warning("partition %c doesn't start at 0!",part);
1388 if (pp->p_size != lp->d_secperunit)
1389 Warning("partition %c doesn't cover the whole unit!",part);
1391 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1392 (pp->p_size != lp->d_secperunit)) {
1393 Warning("An incorrect partition %c may cause problems for "
1394 "standard system utilities",part);
1398 /* check for overlaps */
1399 /* this will check for all possible overlaps once and only once */
1400 for (j = 0; j < i; j++) {
1401 pp2 = &lp->d_partitions[j];
1402 if (j != RAW_PART && i != RAW_PART &&
1403 pp->p_fstype != FS_VINUM &&
1404 pp2->p_fstype != FS_VINUM &&
1405 part_set[i] && part_set[j]) {
1406 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1407 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1408 pp2->p_offset >= pp->p_offset)) {
1409 fprintf(stderr,"partitions %c and %c overlap!\n",
1416 for (; i < 8 || i < lp->d_npartitions; i++) {
1418 pp = &lp->d_partitions[i];
1419 if (pp->p_size || pp->p_offset)
1420 Warning("unused partition %c: size %d offset %lu",
1421 'a' + i, pp->p_size, (u_long)pp->p_offset);
1427 * When operating on a "virgin" disk, try getting an initial label
1428 * from the associated device driver. This might work for all device
1429 * drivers that are able to fetch some initial device parameters
1430 * without even having access to a (BSD) disklabel, like SCSI disks,
1431 * most IDE drives, or vn devices.
1433 * The device name must be given in its "canonical" form.
1435 static struct disklabel dlab;
1438 getvirginlabel(void)
1440 struct disklabel *dl = &dlab;
1441 char nambuf[BBSIZE];
1444 if (dkname[0] == '/') {
1445 warnx("\"auto\" requires the usage of a canonical disk name");
1448 snprintf(nambuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1449 if ((f = open(nambuf, O_RDONLY)) == -1) {
1450 warn("cannot open %s", nambuf);
1455 * Try to use the new get-virgin-label ioctl. If it fails,
1456 * fallback to the old get-disdk-info ioctl.
1458 if (ioctl(f, DIOCGDVIRGIN, dl) < 0) {
1459 if (ioctl(f, DIOCGDINFO, dl) < 0) {
1460 warn("ioctl DIOCGDINFO");
1470 getdisklabelfromdisktab(const char *name)
1473 struct disklabel *dl = &dlab;
1476 if ((dt = getdisktabbyname(name)) == NULL)
1478 dl->d_magic = DISKMAGIC;
1479 dl->d_type = dt->d_typeid;
1481 dl->d_secsize = dt->d_media_blksize;
1482 dl->d_nsectors = dt->d_secpertrack;
1483 dl->d_ntracks = dt->d_nheads;
1484 dl->d_ncylinders = dt->d_ncylinders;
1485 dl->d_secpercyl = dt->d_secpercyl;
1486 dl->d_secperunit = dt->d_media_blocks;
1487 dl->d_rpm = dt->d_rpm;
1488 dl->d_interleave = dt->d_interleave;
1489 dl->d_trackskew = dt->d_trackskew;
1490 dl->d_cylskew = dt->d_cylskew;
1491 dl->d_headswitch = dt->d_headswitch;
1492 dl->d_trkseek = dt->d_trkseek;
1493 dl->d_magic2 = DISKMAGIC;
1494 dl->d_npartitions = dt->d_npartitions;
1495 dl->d_bbsize = dt->d_bbsize;
1496 dl->d_sbsize = dt->d_sbsize;
1497 for (i = 0; i < dt->d_npartitions; ++i) {
1498 struct partition *dlp = &dl->d_partitions[i];
1499 struct dt_partition *dtp = &dt->d_partitions[i];
1501 dlp->p_size = dtp->p_size;
1502 dlp->p_offset = dtp->p_offset;
1503 dlp->p_fsize = dtp->p_fsize;
1504 dlp->p_fstype = dtp->p_fstype;
1505 dlp->p_frag = dtp->p_fsize;
1511 * If we are installing a boot program that doesn't fit in d_bbsize
1512 * we need to mark those partitions that the boot overflows into.
1513 * This allows newfs to prevent creation of a filesystem where it might
1514 * clobber bootstrap code.
1517 setbootflag(struct disklabel *lp)
1519 struct partition *pp;
1526 boffset = bootsize / lp->d_secsize;
1527 for (i = 0; i < lp->d_npartitions; i++) {
1529 pp = &lp->d_partitions[i];
1530 if (pp->p_size == 0)
1532 if (boffset <= pp->p_offset) {
1533 if (pp->p_fstype == FS_BOOT)
1534 pp->p_fstype = FS_UNUSED;
1535 } else if (pp->p_fstype != FS_BOOT) {
1536 if (pp->p_fstype != FS_UNUSED) {
1538 "boot overlaps used partition %c\n",
1542 pp->p_fstype = FS_BOOT;
1543 Warning("boot overlaps partition %c, %s",
1544 part, "marked as FS_BOOT");
1549 errx(4, "cannot install boot program");
1554 Warning(const char *fmt, ...)
1558 fprintf(stderr, "Warning, ");
1560 vfprintf(stderr, fmt, ap);
1561 fprintf(stderr, "\n");
1566 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1567 * and earlier DFly bb's are packed against the old disklabel and a new
1568 * disklabel would blow them up. This is a hack that should be removed
1569 * in 2006 sometime (if ever).
1573 checkoldboot(int f, const char *bootbuffer)
1577 if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0)
1579 lseek(f, (off_t)0, SEEK_SET);
1580 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1582 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1584 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1593 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",
1594 "usage: disklabel [-r] disk",
1595 "\t\t(to read label)",
1596 " disklabel -w [-r] [-n] disk type [ packid ]",
1597 "\t\t(to write label with existing boot program)",
1598 " disklabel -e [-r] [-n] disk",
1599 "\t\t(to edit label)",
1600 " disklabel -R [-r] [-n] disk protofile",
1601 "\t\t(to restore label with existing boot program)",
1603 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1604 "\t\t(to install boot program with existing label)",
1605 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1606 "\t\t(to write label and boot program)",
1607 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1608 "\t\t(to restore label and boot program)",
1610 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1611 "\t\t(to install boot program with existing on-disk label)",
1612 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1613 "\t\t(to write label and install boot program)",
1614 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1615 "\t\t(to restore label and install boot program)",
1617 " disklabel [-NW] disk",
1618 "\t\t(to write disable/enable label)");
1620 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1621 "usage: disklabel [-r] disk", "(to read label)",
1622 " disklabel -w [-r] [-n] disk type [ packid ]",
1623 "\t\t(to write label)",
1624 " disklabel -e [-r] [-n] disk",
1625 "\t\t(to edit label)",
1626 " disklabel -R [-r] [-n] disk protofile",
1627 "\t\t(to restore label)",
1628 " disklabel [-NW] disk",
1629 "\t\t(to write disable/enable label)");