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.19 2007/06/17 03:51:13 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)
380 Warning("write to disk label supressed - label was as follows:");
384 /* make sure we are not overwriting our boot code */
385 if (checkoldboot(f, boot))
386 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
388 lp->d_magic = DISKMAGIC;
389 lp->d_magic2 = DISKMAGIC;
391 lp->d_checksum = dkcksum(lp);
394 * First set the kernel disk label,
395 * then write a label to the raw disk.
396 * If the SDINFO ioctl fails because it is unimplemented,
397 * keep going; otherwise, the kernel consistency checks
398 * may prevent us from changing the current (in-core)
401 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
402 errno != ENODEV && errno != ENOTTY) {
403 l_perror("ioctl DIOCSDINFO");
406 lseek(f, (off_t)0, SEEK_SET);
409 * write enable label sector before write
410 * (if necessary), disable after writing.
413 if (ioctl(f, DIOCWLABEL, &flag) < 0)
414 warn("ioctl DIOCWLABEL");
415 r = write(f, boot, lp->d_bbsize);
416 if (r != ((ssize_t)lp->d_bbsize)) {
422 * Output the remainder of the disklabel
425 ioctl(f, DIOCSETSNOOP, &yes);
426 r = write(f, bootbuf, bootsize);
427 ioctl(f, DIOCSETSNOOP, &no);
435 ioctl(f, DIOCWLABEL, &flag);
436 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
437 l_perror("ioctl DIOCWDINFO");
445 l_perror(const char *s)
450 warnx("%s: no disk label on disk;", s);
451 fprintf(stderr, "add \"-r\" to install initial label\n");
455 warnx("%s: label magic number or checksum is wrong!", s);
456 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
460 warnx("%s: open partition would move or shrink", s);
464 warnx("%s: '%c' partition must start at beginning of disk",
475 * Fetch disklabel for disk.
476 * Use ioctl to get label unless -r flag is given.
481 struct disklabel *lp;
487 if (ioctl(f, DIOCSETSNOOP, &yes) < 0)
488 printf("BAD SNOOP\n");
489 r = read(f, bootarea, BBSIZE);
490 ioctl(f, DIOCSETSNOOP, &no);
492 err(4, "%s", specname);
493 for (lp = (struct disklabel *)bootarea;
494 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
495 lp = (struct disklabel *)((char *)lp + 16))
496 if (lp->d_magic == DISKMAGIC &&
497 lp->d_magic2 == DISKMAGIC)
499 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
500 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
503 "bad pack magic number (label is damaged, or pack is unlabeled)");
506 if (ioctl(f, DIOCGDINFO, lp) < 0)
507 err(4, "ioctl DIOCGDINFO");
513 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
514 * Returns a pointer to the disklabel portion of the bootarea.
517 makebootarea(char *boot, struct disklabel *dp, int f)
519 struct disklabel *lp;
528 unsigned int i, found;
532 if (dp->d_secsize == 0) {
533 dp->d_secsize = DEV_BSIZE;
534 dp->d_bbsize = BBSIZE;
536 lp = (struct disklabel *)
537 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
538 bzero((char *)lp, sizeof *lp);
541 * If we are not installing a boot program but we are installing a
542 * label on disk then we must read the current bootarea so we don't
543 * clobber the existing boot.
547 if (read(f, boot, BBSIZE) < BBSIZE)
548 err(4, "%s", specname);
549 bzero((char *)lp, sizeof *lp);
554 * We are installing a boot program. Determine the name(s) and
555 * read them into the appropriate places in the boot area.
557 if (!xxboot || !bootxx) {
559 if ((p = strrchr(dkname, '/')) == NULL)
563 while (*p && !isdigit(*p))
568 sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
573 sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
580 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
581 xxboot, bootxx ? bootxx : "NONE");
586 * 1. One-piece bootstrap (hp300/hp800)
587 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
588 * is remembered and written later following the bootarea.
589 * 2. Two-piece bootstraps (vax/i386?/mips?)
590 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
591 * bytes of bootarea, remaining d_bbsize-d_secsize filled
594 b = open(xxboot, O_RDONLY);
596 err(4, "%s", xxboot);
601 * The i386 has the so-called fdisk table embedded into the
602 * primary bootstrap. We take care to not clobber it, but
603 * only if it does already contain some data. (Otherwise,
604 * the xxboot provides a template.)
606 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
607 err(4, "%s", xxboot);
608 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
610 if (read(b, boot, (int)dp->d_secsize) < 0)
611 err(4, "%s", xxboot);
614 for (i = DOSPARTOFF, found = 0;
615 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
617 found = tmpbuf[i] != 0;
619 memcpy((void *)&boot[DOSPARTOFF],
620 (void *)&tmpbuf[DOSPARTOFF],
621 NDOSPART * sizeof(struct dos_partition));
624 b = open(bootxx, O_RDONLY);
626 err(4, "%s", bootxx);
627 if (fstat(b, &sb) != 0)
628 err(4, "%s", bootxx);
629 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
630 errx(4, "%s too large", bootxx);
631 if (read(b, &boot[dp->d_secsize],
632 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
633 err(4, "%s", bootxx);
634 #else /* !(NUMBOOT > 1) */
635 if (read(b, boot, (int)dp->d_bbsize) < 0)
636 err(4, "%s", xxboot);
637 if (fstat(b, &sb) != 0)
638 err(4, "%s", xxboot);
639 bootsize = (int)sb.st_size - dp->d_bbsize;
641 /* XXX assume d_secsize is a power of two */
642 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
643 bootbuf = (char *)malloc((size_t)bootsize);
645 err(4, "%s", xxboot);
646 if (read(b, bootbuf, bootsize) < 0) {
648 err(4, "%s", xxboot);
651 #endif /* NUMBOOT > 1 */
653 #endif /* NUMBOOT > 0 */
655 * Make sure no part of the bootstrap is written in the area
656 * reserved for the label.
658 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
660 errx(2, "bootstrap doesn't leave room for disk label");
665 display(FILE *f, const struct disklabel *lp)
668 const struct partition *pp;
670 fprintf(f, "# %s:\n", specname);
671 if (lp->d_type < DKMAXTYPES)
672 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
674 fprintf(f, "type: %u\n", lp->d_type);
675 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
677 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
679 fprintf(f, "flags:");
681 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
682 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
683 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
684 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
685 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
686 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
687 fprintf(f, "rpm: %u\n", lp->d_rpm);
688 fprintf(f, "interleave: %u\n", lp->d_interleave);
689 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
690 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
691 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
692 (u_long)lp->d_headswitch);
693 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
694 (u_long)lp->d_trkseek);
695 fprintf(f, "drivedata: ");
696 for (i = NDDATA - 1; i >= 0; i--) {
697 if (lp->d_drivedata[i])
702 for (j = 0; j <= i; j++)
703 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
704 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
706 "# size offset fstype \n");
707 pp = lp->d_partitions;
708 for (i = 0; i < lp->d_npartitions; i++, pp++) {
710 u_long onemeg = 1024 * 1024 / lp->d_secsize;
711 fprintf(f, " %c: ", 'a' + i);
713 fprintf(f, "%10lu ", (u_long)pp->p_size);
714 fprintf(f, "%10lu ", (u_long)pp->p_offset);
715 if (pp->p_fstype < FSMAXTYPES)
716 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
718 fprintf(f, "%8d", pp->p_fstype);
720 fprintf(f, "\t# %11.3fM", (double)pp->p_size / onemeg);
728 edit(struct disklabel *lp, int f)
731 struct disklabel label;
734 if ((fd = mkstemp(tmpfil)) == -1 ||
735 (fp = fdopen(fd, "w")) == NULL) {
736 warnx("can't create %s", tmpfil);
744 fp = fopen(tmpfil, "r");
746 warnx("can't reopen %s for reading", tmpfil);
749 bzero((char *)&label, sizeof(label));
750 if (getasciilabel(fp, &label)) {
752 if (writelabel(f, bootarea, lp) == 0) {
759 printf("re-edit the label? [y]: "); fflush(stdout);
761 if (c != EOF && c != (int)'\n')
762 while (getchar() != (int)'\n')
778 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
779 while ((pid = fork()) < 0) {
780 if (errno == EPROCLIM) {
781 warnx("you have too many processes");
784 if (errno != EAGAIN) {
794 if ((ed = getenv("EDITOR")) == (char *)0)
796 execlp(ed, ed, tmpfil, (char *)0);
799 while ((xpid = wait(&status)) >= 0)
810 while (*cp != '\0' && isspace(*cp))
812 if (*cp == '\0' || *cp == '#')
822 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
824 if ((c = *cp) != '\0') {
833 * Read an ascii label in from fd f,
834 * in the same format as that put out by display(),
838 getasciilabel(FILE *f, struct disklabel *lp)
843 char *tp, line[BUFSIZ];
845 int lineno = 0, errors = 0;
848 char unknown[] = "unknown";
850 bzero(&part_set, sizeof(part_set));
851 bzero(&part_size_type, sizeof(part_size_type));
852 bzero(&part_offset_type, sizeof(part_offset_type));
853 lp->d_bbsize = BBSIZE; /* XXX */
854 lp->d_sbsize = SBSIZE; /* XXX */
855 while (fgets(line, sizeof(line) - 1, f)) {
857 if ((cp = strchr(line,'\n')) != 0)
862 tp = strchr(cp, ':');
864 fprintf(stderr, "line %d: syntax error\n", lineno);
868 *tp++ = '\0', tp = skip(tp);
869 if (streq(cp, "type")) {
873 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) {
874 if (*cpp && streq(*cpp, tp)) {
875 lp->d_type = cpp - dktypenames;
879 if (cpp < &dktypenames[DKMAXTYPES])
881 v = strtoul(tp, NULL, 10);
882 if (v >= DKMAXTYPES) {
883 fprintf(stderr, "line %d:%s %lu\n", lineno,
884 "Warning, unknown disk type", v);
889 if (streq(cp, "flags")) {
890 for (v = 0; (cp = tp) && *cp != '\0';) {
892 if (streq(cp, "removeable"))
893 v |= 0; /* obsolete */
894 else if (streq(cp, "ecc"))
895 v |= 0; /* obsolete */
896 else if (streq(cp, "badsect"))
897 v |= 0; /* obsolete */
900 "line %d: %s: bad flag\n",
908 if (streq(cp, "drivedata")) {
909 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
910 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
915 if (sscanf(cp, "%lu partitions", &v) == 1) {
916 if (v == 0 || v > MAXPARTITIONS) {
918 "line %d: bad # of partitions\n", lineno);
919 lp->d_npartitions = MAXPARTITIONS;
922 lp->d_npartitions = v;
927 if (streq(cp, "disk")) {
928 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
931 if (streq(cp, "label")) {
932 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
935 if (streq(cp, "bytes/sector")) {
936 v = strtoul(tp, NULL, 10);
937 if (v == 0 || (v % DEV_BSIZE) != 0) {
939 "line %d: %s: bad sector size\n",
946 if (streq(cp, "sectors/track")) {
947 v = strtoul(tp, NULL, 10);
948 #if (ULONG_MAX != 0xffffffffUL)
949 if (v == 0 || v > 0xffffffff) {
953 fprintf(stderr, "line %d: %s: bad %s\n",
960 if (streq(cp, "sectors/cylinder")) {
961 v = strtoul(tp, NULL, 10);
963 fprintf(stderr, "line %d: %s: bad %s\n",
970 if (streq(cp, "tracks/cylinder")) {
971 v = strtoul(tp, NULL, 10);
973 fprintf(stderr, "line %d: %s: bad %s\n",
980 if (streq(cp, "cylinders")) {
981 v = strtoul(tp, NULL, 10);
983 fprintf(stderr, "line %d: %s: bad %s\n",
987 lp->d_ncylinders = v;
990 if (streq(cp, "sectors/unit")) {
991 v = strtoul(tp, NULL, 10);
993 fprintf(stderr, "line %d: %s: bad %s\n",
997 lp->d_secperunit = v;
1000 if (streq(cp, "rpm")) {
1001 v = strtoul(tp, NULL, 10);
1002 if (v == 0 || v > USHRT_MAX) {
1003 fprintf(stderr, "line %d: %s: bad %s\n",
1010 if (streq(cp, "interleave")) {
1011 v = strtoul(tp, NULL, 10);
1012 if (v == 0 || v > USHRT_MAX) {
1013 fprintf(stderr, "line %d: %s: bad %s\n",
1017 lp->d_interleave = v;
1020 if (streq(cp, "trackskew")) {
1021 v = strtoul(tp, NULL, 10);
1022 if (v > USHRT_MAX) {
1023 fprintf(stderr, "line %d: %s: bad %s\n",
1027 lp->d_trackskew = v;
1030 if (streq(cp, "cylinderskew")) {
1031 v = strtoul(tp, NULL, 10);
1032 if (v > USHRT_MAX) {
1033 fprintf(stderr, "line %d: %s: bad %s\n",
1040 if (streq(cp, "headswitch")) {
1041 v = strtoul(tp, NULL, 10);
1042 lp->d_headswitch = v;
1045 if (streq(cp, "track-to-track seek")) {
1046 v = strtoul(tp, NULL, 10);
1050 /* the ':' was removed above */
1051 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1053 "line %d: %s: Unknown disklabel field\n", lineno,
1059 /* Process a partition specification line. */
1061 if (part >= lp->d_npartitions) {
1063 "line %d: partition name out of range a-%c: %s\n",
1064 lineno, 'a' + lp->d_npartitions - 1, cp);
1070 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1075 errors += checklabel(lp);
1076 return (errors == 0);
1079 #define NXTNUM(n) do { \
1081 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1084 cp = tp, tp = word(cp); \
1085 (n) = strtoul(cp, NULL, 10); \
1089 /* retain 1 character following number */
1090 #define NXTWORD(w,n) do { \
1092 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1096 cp = tp, tp = word(cp); \
1097 (n) = strtoul(cp, &tmp, 10); \
1098 if (tmp) (w) = *tmp; \
1103 * Read a partition line into partition `part' in the specified disklabel.
1104 * Return 0 on success, 1 on failure.
1107 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1109 struct partition *pp;
1114 pp = &lp->d_partitions[part];
1121 NXTWORD(part_size_type[part],v);
1122 if (v == 0 && part_size_type[part] != '*') {
1124 "line %d: %s: bad partition size\n", lineno, cp);
1133 NXTWORD(part_offset_type[part],v);
1134 if (v == 0 && part_offset_type[part] != '*' &&
1135 part_offset_type[part] != '\0') {
1137 "line %d: %s: bad partition offset\n", lineno, cp);
1147 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1148 if (*cpp && streq(*cpp, cp))
1151 pp->p_fstype = cpp - fstypenames;
1154 v = strtoul(cp, NULL, 10);
1157 if (v >= FSMAXTYPES) {
1159 "line %d: Warning, unknown filesystem type %s\n",
1172 fprintf(stderr, "line %d: Warning, fragment, block, "
1173 "and bps/cpg fields are no\n"
1174 "longer supported and must be specified "
1175 "via newfs options instead.\n",
1182 * Check disklabel for errors and fill in
1183 * derived fields according to supplied values.
1186 checklabel(struct disklabel *lp)
1188 struct partition *pp;
1191 u_long total_size, total_percent, current_offset;
1192 int seen_default_offset;
1195 struct partition *pp2;
1197 if (lp->d_secsize == 0) {
1198 fprintf(stderr, "sector size 0\n");
1201 if (lp->d_nsectors == 0) {
1202 fprintf(stderr, "sectors/track 0\n");
1205 if (lp->d_ntracks == 0) {
1206 fprintf(stderr, "tracks/cylinder 0\n");
1209 if (lp->d_ncylinders == 0) {
1210 fprintf(stderr, "cylinders/unit 0\n");
1214 Warning("revolutions/minute 0");
1215 if (lp->d_secpercyl == 0)
1216 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1217 if (lp->d_secperunit == 0)
1218 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1219 if (lp->d_bbsize == 0) {
1220 fprintf(stderr, "boot block size 0\n");
1222 } else if (lp->d_bbsize % lp->d_secsize)
1223 Warning("boot block size %% sector-size != 0");
1224 if (lp->d_sbsize == 0) {
1225 fprintf(stderr, "super block size 0\n");
1227 } else if (lp->d_sbsize % lp->d_secsize)
1228 Warning("super block size %% sector-size != 0");
1229 if (lp->d_npartitions > MAXPARTITIONS)
1230 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1231 (u_long)lp->d_npartitions, MAXPARTITIONS);
1233 /* first allocate space to the partitions, then offsets */
1234 total_size = 0; /* in sectors */
1235 total_percent = 0; /* in percent */
1237 /* find all fixed partitions */
1238 for (i = 0; i < lp->d_npartitions; i++) {
1239 pp = &lp->d_partitions[i];
1241 if (part_size_type[i] == '*') {
1242 if (i == RAW_PART) {
1243 pp->p_size = lp->d_secperunit;
1246 Warning("Too many '*' partitions (%c and %c)",
1247 hog_part + 'a',i + 'a');
1255 switch (part_size_type[i]) {
1257 total_percent += size;
1265 size *= 1024ULL * 1024ULL;
1269 size *= 1024ULL * 1024ULL * 1024ULL;
1274 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1277 /* don't count %'s yet */
1278 if (part_size_type[i] != '%') {
1280 * for all not in sectors, convert to
1283 if (part_size_type[i] != '\0') {
1284 if (size % lp->d_secsize != 0)
1285 Warning("partition %c not an integer number of sectors",
1287 size /= lp->d_secsize;
1290 /* else already in sectors */
1297 /* handle % partitions - note %'s don't need to add up to 100! */
1298 if (total_percent != 0) {
1299 long free_space = lp->d_secperunit - total_size;
1300 if (total_percent > 100) {
1301 fprintf(stderr,"total percentage %lu is greater than 100\n",
1306 if (free_space > 0) {
1307 for (i = 0; i < lp->d_npartitions; i++) {
1308 pp = &lp->d_partitions[i];
1309 if (part_set[i] && part_size_type[i] == '%') {
1310 /* careful of overflows! and integer roundoff */
1311 pp->p_size = ((double)pp->p_size/100) * free_space;
1312 total_size += pp->p_size;
1314 /* FIX we can lose a sector or so due to roundoff per
1315 partition. A more complex algorithm could avoid that */
1320 "%ld sectors available to give to '*' and '%%' partitions\n",
1323 /* fix? set all % partitions to size 0? */
1326 /* give anything remaining to the hog partition */
1327 if (hog_part != -1) {
1328 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1329 total_size = lp->d_secperunit;
1332 /* Now set the offsets for each partition */
1333 current_offset = 0; /* in sectors */
1334 seen_default_offset = 0;
1335 for (i = 0; i < lp->d_npartitions; i++) {
1337 pp = &lp->d_partitions[i];
1339 if (part_offset_type[i] == '*') {
1340 if (i == RAW_PART) {
1343 pp->p_offset = current_offset;
1344 seen_default_offset = 1;
1347 /* allow them to be out of order for old-style tables */
1348 if (pp->p_offset < current_offset &&
1349 seen_default_offset && i != RAW_PART &&
1350 pp->p_fstype != FS_VINUM) {
1352 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1353 (long)pp->p_offset,i+'a',current_offset);
1355 "Labels with any *'s for offset must be in ascending order by sector\n");
1357 } else if (pp->p_offset != current_offset &&
1358 i != RAW_PART && seen_default_offset) {
1360 * this may give unneeded warnings if
1361 * partitions are out-of-order
1364 "Offset %ld for partition %c doesn't match expected value %ld",
1365 (long)pp->p_offset, i + 'a', current_offset);
1369 current_offset = pp->p_offset + pp->p_size;
1373 for (i = 0; i < lp->d_npartitions; i++) {
1375 pp = &lp->d_partitions[i];
1376 if (pp->p_size == 0 && pp->p_offset != 0)
1377 Warning("partition %c: size 0, but offset %lu",
1378 part, (u_long)pp->p_offset);
1380 if (pp->p_size % lp->d_secpercyl)
1381 Warning("partition %c: size %% cylinder-size != 0",
1383 if (pp->p_offset % lp->d_secpercyl)
1384 Warning("partition %c: offset %% cylinder-size != 0",
1387 if (pp->p_offset > lp->d_secperunit) {
1389 "partition %c: offset past end of unit\n", part);
1392 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1394 "partition %c: partition extends past end of unit\n",
1400 if (pp->p_fstype != FS_UNUSED)
1401 Warning("partition %c is not marked as unused!",part);
1402 if (pp->p_offset != 0)
1403 Warning("partition %c doesn't start at 0!",part);
1404 if (pp->p_size != lp->d_secperunit)
1405 Warning("partition %c doesn't cover the whole unit!",part);
1407 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1408 (pp->p_size != lp->d_secperunit)) {
1409 Warning("An incorrect partition %c may cause problems for "
1410 "standard system utilities",part);
1414 /* check for overlaps */
1415 /* this will check for all possible overlaps once and only once */
1416 for (j = 0; j < i; j++) {
1417 pp2 = &lp->d_partitions[j];
1418 if (j != RAW_PART && i != RAW_PART &&
1419 pp->p_fstype != FS_VINUM &&
1420 pp2->p_fstype != FS_VINUM &&
1421 part_set[i] && part_set[j]) {
1422 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1423 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1424 pp2->p_offset >= pp->p_offset)) {
1425 fprintf(stderr,"partitions %c and %c overlap!\n",
1432 for (; i < 8 || i < lp->d_npartitions; i++) {
1434 pp = &lp->d_partitions[i];
1435 if (pp->p_size || pp->p_offset)
1436 Warning("unused partition %c: size %d offset %lu",
1437 'a' + i, pp->p_size, (u_long)pp->p_offset);
1443 * When operating on a "virgin" disk, try getting an initial label
1444 * from the associated device driver. This might work for all device
1445 * drivers that are able to fetch some initial device parameters
1446 * without even having access to a (BSD) disklabel, like SCSI disks,
1447 * most IDE drives, or vn devices.
1449 * The device name must be given in its "canonical" form.
1451 static struct disklabel dlab;
1454 getvirginlabel(void)
1456 struct disklabel *dl = &dlab;
1457 char nambuf[BBSIZE];
1460 if (dkname[0] == '/') {
1461 warnx("\"auto\" requires the usage of a canonical disk name");
1464 snprintf(nambuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1465 if ((f = open(nambuf, O_RDONLY)) == -1) {
1466 warn("cannot open %s", nambuf);
1471 * Try to use the new get-virgin-label ioctl. If it fails,
1472 * fallback to the old get-disdk-info ioctl.
1474 if (ioctl(f, DIOCGDVIRGIN, dl) < 0) {
1475 if (ioctl(f, DIOCGDINFO, dl) < 0) {
1476 warn("ioctl DIOCGDINFO");
1486 getdisklabelfromdisktab(const char *name)
1489 struct disklabel *dl = &dlab;
1492 if ((dt = getdisktabbyname(name)) == NULL)
1494 dl->d_magic = DISKMAGIC;
1495 dl->d_type = dt->d_typeid;
1497 dl->d_secsize = dt->d_media_blksize;
1498 dl->d_nsectors = dt->d_secpertrack;
1499 dl->d_ntracks = dt->d_nheads;
1500 dl->d_ncylinders = dt->d_ncylinders;
1501 dl->d_secpercyl = dt->d_secpercyl;
1502 dl->d_secperunit = dt->d_media_blocks;
1503 dl->d_rpm = dt->d_rpm;
1504 dl->d_interleave = dt->d_interleave;
1505 dl->d_trackskew = dt->d_trackskew;
1506 dl->d_cylskew = dt->d_cylskew;
1507 dl->d_headswitch = dt->d_headswitch;
1508 dl->d_trkseek = dt->d_trkseek;
1509 dl->d_magic2 = DISKMAGIC;
1510 dl->d_npartitions = dt->d_npartitions;
1511 dl->d_bbsize = dt->d_bbsize;
1512 dl->d_sbsize = dt->d_sbsize;
1513 for (i = 0; i < dt->d_npartitions; ++i) {
1514 struct partition *dlp = &dl->d_partitions[i];
1515 struct dt_partition *dtp = &dt->d_partitions[i];
1517 dlp->p_size = dtp->p_size;
1518 dlp->p_offset = dtp->p_offset;
1519 dlp->p_fsize = dtp->p_fsize;
1520 dlp->p_fstype = dtp->p_fstype;
1521 dlp->p_frag = dtp->p_fsize;
1527 * If we are installing a boot program that doesn't fit in d_bbsize
1528 * we need to mark those partitions that the boot overflows into.
1529 * This allows newfs to prevent creation of a filesystem where it might
1530 * clobber bootstrap code.
1533 setbootflag(struct disklabel *lp)
1535 struct partition *pp;
1542 boffset = bootsize / lp->d_secsize;
1543 for (i = 0; i < lp->d_npartitions; i++) {
1545 pp = &lp->d_partitions[i];
1546 if (pp->p_size == 0)
1548 if (boffset <= pp->p_offset) {
1549 if (pp->p_fstype == FS_BOOT)
1550 pp->p_fstype = FS_UNUSED;
1551 } else if (pp->p_fstype != FS_BOOT) {
1552 if (pp->p_fstype != FS_UNUSED) {
1554 "boot overlaps used partition %c\n",
1558 pp->p_fstype = FS_BOOT;
1559 Warning("boot overlaps partition %c, %s",
1560 part, "marked as FS_BOOT");
1565 errx(4, "cannot install boot program");
1570 Warning(const char *fmt, ...)
1574 fprintf(stderr, "Warning, ");
1576 vfprintf(stderr, fmt, ap);
1577 fprintf(stderr, "\n");
1582 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1583 * and earlier DFly bb's are packed against the old disklabel and a new
1584 * disklabel would blow them up. This is a hack that should be removed
1585 * in 2006 sometime (if ever).
1589 checkoldboot(int f, const char *bootbuffer)
1593 if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0)
1595 lseek(f, (off_t)0, SEEK_SET);
1596 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1598 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1600 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1609 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",
1610 "usage: disklabel [-r] disk",
1611 "\t\t(to read label)",
1612 " disklabel -w [-r] [-n] disk type [ packid ]",
1613 "\t\t(to write label with existing boot program)",
1614 " disklabel -e [-r] [-n] disk",
1615 "\t\t(to edit label)",
1616 " disklabel -R [-r] [-n] disk protofile",
1617 "\t\t(to restore label with existing boot program)",
1619 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1620 "\t\t(to install boot program with existing label)",
1621 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1622 "\t\t(to write label and boot program)",
1623 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1624 "\t\t(to restore label and boot program)",
1626 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1627 "\t\t(to install boot program with existing on-disk label)",
1628 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1629 "\t\t(to write label and install boot program)",
1630 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1631 "\t\t(to restore label and install boot program)",
1633 " disklabel [-NW] disk",
1634 "\t\t(to write disable/enable label)");
1636 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1637 "usage: disklabel [-r] disk", "(to read label)",
1638 " disklabel -w [-r] [-n] disk type [ packid ]",
1639 "\t\t(to write label)",
1640 " disklabel -e [-r] [-n] disk",
1641 "\t\t(to edit label)",
1642 " disklabel -R [-r] [-n] disk protofile",
1643 "\t\t(to restore label)",
1644 " disklabel [-NW] disk",
1645 "\t\t(to write disable/enable label)");