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 $
42 #include <sys/param.h>
47 #include <sys/disklabel32.h>
48 #include <sys/disklabel64.h>
49 #include <sys/diskslice.h>
50 #include <sys/diskmbr.h>
51 #include <sys/dtype.h>
52 #include <sys/sysctl.h>
56 #include <vfs/ufs/dinode.h>
57 #include <vfs/ufs/fs.h>
68 #include "pathnames.h"
71 * Disklabel32: read and write 32 bit disklabels.
72 * The label is usually placed on one of the first sectors of the disk.
73 * Many machines also place a bootstrap in the same area,
74 * in which case the label is embedded in the bootstrap.
75 * The bootstrap source must leave space at the proper offset
76 * for the label on such machines.
80 #define BBSIZE 8192 /* size of boot area, with label */
83 /* FIX! These are too low, but are traditional */
84 #define DEFAULT_NEWFS_BLOCK 8192U
85 #define DEFAULT_NEWFS_FRAG 1024U
86 #define DEFAULT_NEWFS_CPG 16U
88 #define BIG_NEWFS_BLOCK 16384U
89 #define BIG_NEWFS_FRAG 2048U
90 #define BIG_NEWFS_CPG 64U
94 void makelabel(const char *, const char *, struct disklabel32 *);
95 int writelabel(int, const char *, struct disklabel32 *);
96 void l_perror(const char *);
97 struct disklabel32 *readlabel(int);
98 struct disklabel32 *makebootarea(char *, struct disklabel32 *, int);
99 void display(FILE *, const struct disklabel32 *);
100 int edit(struct disklabel32 *, int);
104 int getasciilabel(FILE *, struct disklabel32 *);
105 int getasciipartspec(char *, struct disklabel32 *, int, int);
106 int checklabel(struct disklabel32 *);
107 void setbootflag(struct disklabel32 *);
108 void Warning(const char *, ...) __printflike(1, 2);
110 int checkoldboot(int, const char *);
111 const char *fixlabel(int, struct disklabel32 *, int);
112 struct disklabel32 *getvirginlabel(void);
113 struct disklabel32 *getdisklabelfromdisktab(const char *name);
115 #define DEFEDITOR _PATH_VI
116 #define streq(a,b) (strcmp(a,b) == 0)
120 char tmpfil[] = PATH_TMPFILE;
122 char namebuf[BBSIZE];
123 struct disklabel32 lab;
124 char bootarea[BBSIZE];
126 #define MAX_PART ('z')
127 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
128 char part_size_type[MAX_NUM_PARTS];
129 char part_offset_type[MAX_NUM_PARTS];
130 int part_set[MAX_NUM_PARTS];
133 int installboot; /* non-zero if we should install a boot program */
134 char *bootbuf; /* pointer to buffer with remainder of boot prog */
135 int bootsize; /* size of remaining boot program */
136 char *xxboot; /* primary boot */
137 char *bootxx; /* secondary boot */
138 char boot0[MAXPATHLEN];
139 char boot1[MAXPATHLEN];
143 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
147 int disable_write; /* set to disable writing to disk label */
149 u_int32_t slice_start_lba;
153 #define OPTIONS "BNRWb:def:nrs:w"
155 #define OPTIONS "BNRWb:ef:nrs:w"
159 main(int argc, char *argv[])
161 struct disklabel32 *lp;
163 int ch, f = 0, flag, error = 0;
166 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
178 slice_start_lba = strtoul(optarg, NULL, 0);
236 xxboot = bootxx = NULL;
245 dkname = getdevpath(argv[0], 0);
247 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
249 err(4, "%s", specname);
265 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
266 err(4, "ioctl DIOCWLABEL");
274 error = checklabel(lp);
275 if (checkoldboot(f, NULL))
276 warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
281 if (installboot && argc == 3) {
282 makelabel(argv[2], 0, &lab);
286 * We only called makelabel() for its side effect
287 * of setting the bootstrap file names. Discard
288 * all changes to `lab' so that all values in the
289 * final label come from the ASCII label.
291 bzero((char *)&lab, sizeof(lab));
296 if (!(t = fopen(argv[1], "r")))
297 err(4, "%s", argv[1]);
298 if (!getasciilabel(t, &lab))
300 lp = makebootarea(bootarea, &lab, f);
302 error = writelabel(f, bootarea, lp);
312 makelabel(argv[1], name, &lab);
313 lp = makebootarea(bootarea, &lab, f);
315 if (checklabel(lp) == 0)
316 error = writelabel(f, bootarea, lp);
321 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
322 err(4, "ioctl DIOCWLABEL");
328 struct disklabel32 tlab;
333 makelabel(argv[1], 0, &lab);
334 lp = makebootarea(bootarea, &lab, f);
336 if (checklabel(lp) == 0)
337 error = writelabel(f, bootarea, lp);
346 * Construct a prototype disklabel from /etc/disktab. As a side
347 * effect, set the names of the primary and secondary boot files
351 makelabel(const char *type, const char *name, struct disklabel32 *lp)
353 struct disklabel32 *dp;
355 if (strcmp(type, "auto") == 0)
356 dp = getvirginlabel();
358 dp = getdisklabelfromdisktab(type);
360 errx(1, "%s: unknown disk type", type);
364 * NOTE: boot control files may no longer be specified in disktab.
367 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
371 writelabel(int f, const char *boot, struct disklabel32 *lp)
378 Warning("write to disk label suppressed - label was as follows:");
382 /* make sure we are not overwriting our boot code */
383 if (checkoldboot(f, boot))
384 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
386 lp->d_magic = DISKMAGIC32;
387 lp->d_magic2 = DISKMAGIC32;
389 lp->d_checksum = dkcksum32(lp);
392 * First set the kernel disk label,
393 * then write a label to the raw disk.
394 * If the SDINFO ioctl fails because it is unimplemented,
395 * keep going; otherwise, the kernel consistency checks
396 * may prevent us from changing the current (in-core)
399 if (ioctl(f, DIOCSDINFO32, lp) < 0 &&
400 errno != ENODEV && errno != ENOTTY) {
401 l_perror("ioctl DIOCSDINFO32");
404 lseek(f, (off_t)0, SEEK_SET);
407 * write enable label sector before write
408 * (if necessary), disable after writing.
411 if (ioctl(f, DIOCWLABEL, &flag) < 0)
412 warn("ioctl DIOCWLABEL");
413 msg = fixlabel(f, lp, 1);
418 r = write(f, boot, lp->d_bbsize);
420 if (r != ((ssize_t)lp->d_bbsize)) {
426 * Output the remainder of the disklabel
430 r = write(f, bootbuf, bootsize);
439 ioctl(f, DIOCWLABEL, &flag);
440 } else if (ioctl(f, DIOCWDINFO32, lp) < 0) {
441 l_perror("ioctl DIOCWDINFO32");
449 l_perror(const char *s)
454 warnx("%s: no disk label on disk;", s);
455 fprintf(stderr, "add \"-r\" to install initial label\n");
459 warnx("%s: label magic number or checksum is wrong!", s);
460 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
464 warnx("%s: open partition would move or shrink", s);
468 warnx("%s: '%c' partition must start at beginning of disk",
473 warnx("%s: the disk already has a label of a different type,\n"
474 "probably a 64 bit disklabel. It must be cleaned out "
485 * Fetch disklabel for disk.
486 * Use ioctl to get label unless -r flag is given.
492 struct disklabel32 *lp;
496 r = read(f, bootarea, BBSIZE);
498 err(4, "%s", specname);
499 for (lp = (struct disklabel32 *)bootarea;
500 lp <= (struct disklabel32 *)(bootarea + BBSIZE - sizeof(*lp));
501 lp = (struct disklabel32 *)((char *)lp + 16)) {
502 if (lp->d_magic == DISKMAGIC32 &&
503 lp->d_magic2 == DISKMAGIC32)
506 if (lp > (struct disklabel32 *)(bootarea+BBSIZE-sizeof(*lp)) ||
507 lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32 ||
508 dkcksum32(lp) != 0) {
509 errx(1, "bad pack magic number (label is damaged, "
510 "or pack is unlabeled)");
512 if ((msg = fixlabel(f, lp, 0)) != NULL)
516 if (ioctl(f, DIOCGDINFO32, lp) < 0) {
517 l_perror("ioctl DIOCGDINFO32");
525 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
526 * Returns a pointer to the disklabel portion of the bootarea.
529 makebootarea(char *boot, struct disklabel32 *dp, int f)
531 struct disklabel32 *lp;
539 unsigned int i, found;
543 if (dp->d_secsize == 0) {
544 dp->d_secsize = DEV_BSIZE;
545 dp->d_bbsize = BBSIZE;
547 lp = (struct disklabel32 *)
548 (boot + (LABELSECTOR32 * dp->d_secsize) + LABELOFFSET32);
549 bzero((char *)lp, sizeof *lp);
552 * If we are not installing a boot program but we are installing a
553 * label on disk then we must read the current bootarea so we don't
554 * clobber the existing boot.
558 if (read(f, boot, BBSIZE) < BBSIZE)
559 err(4, "%s", specname);
560 bzero((char *)lp, sizeof *lp);
565 * We are installing a boot program. Determine the name(s) and
566 * read them into the appropriate places in the boot area.
568 if (!xxboot || !bootxx) {
570 sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
575 sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
582 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
583 xxboot, bootxx ? bootxx : "NONE");
588 * 1. One-piece bootstrap (hp300/hp800)
589 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
590 * is remembered and written later following the bootarea.
591 * 2. Two-piece bootstraps (vax/i386?/mips?)
592 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
593 * bytes of bootarea, remaining d_bbsize-d_secsize filled
596 b = open(xxboot, O_RDONLY);
598 err(4, "%s", xxboot);
603 * The i386 has the so-called fdisk table embedded into the
604 * primary bootstrap. We take care to not clobber it, but
605 * only if it does already contain some data. (Otherwise,
606 * the xxboot provides a template.)
608 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == NULL)
609 err(4, "%s", xxboot);
610 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
612 if (read(b, boot, (int)dp->d_secsize) < 0)
613 err(4, "%s", xxboot);
616 for (i = DOSPARTOFF, found = 0;
617 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
619 found = tmpbuf[i] != 0;
621 memcpy((void *)&boot[DOSPARTOFF],
622 (void *)&tmpbuf[DOSPARTOFF],
623 NDOSPART * sizeof(struct dos_partition));
626 b = open(bootxx, O_RDONLY);
628 err(4, "%s", bootxx);
629 if (fstat(b, &sb) != 0)
630 err(4, "%s", bootxx);
631 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
632 errx(4, "%s too large", bootxx);
633 if (read(b, &boot[dp->d_secsize],
634 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
635 err(4, "%s", bootxx);
636 #else /* !(NUMBOOT > 1) */
637 if (read(b, boot, (int)dp->d_bbsize) < 0)
638 err(4, "%s", xxboot);
639 if (fstat(b, &sb) != 0)
640 err(4, "%s", xxboot);
641 bootsize = (int)sb.st_size - dp->d_bbsize;
643 /* XXX assume d_secsize is a power of two */
644 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
645 bootbuf = (char *)malloc((size_t)bootsize);
647 err(4, "%s", xxboot);
648 if (read(b, bootbuf, bootsize) < 0) {
650 err(4, "%s", xxboot);
653 #endif /* NUMBOOT > 1 */
655 #endif /* NUMBOOT > 0 */
657 * Make sure no part of the bootstrap is written in the area
658 * reserved for the label.
660 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel32); p++)
662 errx(2, "bootstrap doesn't leave room for disk label");
667 display(FILE *f, const struct disklabel32 *lp)
670 const struct partition32 *pp;
672 fprintf(f, "# %s:\n", specname);
673 if (lp->d_type < DKMAXTYPES)
674 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
676 fprintf(f, "type: %u\n", lp->d_type);
677 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
679 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
681 fprintf(f, "flags:");
683 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
684 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
685 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
686 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
687 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
688 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
689 fprintf(f, "rpm: %u\n", lp->d_rpm);
690 fprintf(f, "interleave: %u\n", lp->d_interleave);
691 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
692 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
693 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
694 (u_long)lp->d_headswitch);
695 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
696 (u_long)lp->d_trkseek);
697 fprintf(f, "drivedata: ");
698 for (i = NDDATA32 - 1; i >= 0; i--) {
699 if (lp->d_drivedata[i])
704 for (j = 0; j <= i; j++)
705 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
706 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
708 "# size offset fstype\n");
709 pp = lp->d_partitions;
710 for (i = 0; i < lp->d_npartitions; i++, pp++) {
712 u_long onemeg = 1024 * 1024 / lp->d_secsize;
713 fprintf(f, " %c: ", 'a' + i);
715 fprintf(f, "%10lu ", (u_long)pp->p_size);
716 fprintf(f, "%10lu ", (u_long)pp->p_offset);
717 if (pp->p_fstype < FSMAXTYPES)
718 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
720 fprintf(f, "%8d", pp->p_fstype);
722 fprintf(f, "\t# %11.3fMB", (double)pp->p_size / onemeg);
730 edit(struct disklabel32 *lp, int f)
733 struct disklabel32 label;
736 if ((fd = mkstemp(tmpfil)) == -1 ||
737 (fp = fdopen(fd, "w")) == NULL) {
738 warnx("can't create %s", tmpfil);
746 fp = fopen(tmpfil, "r");
748 warnx("can't reopen %s for reading", tmpfil);
751 bzero((char *)&label, sizeof(label));
752 if (getasciilabel(fp, &label)) {
754 if (writelabel(f, bootarea, lp) == 0) {
761 printf("re-edit the label? [y]: "); fflush(stdout);
763 if (c != EOF && c != (int)'\n')
764 while (getchar() != (int)'\n')
780 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
781 while ((pid = fork()) < 0) {
782 if (errno == EPROCLIM) {
783 warnx("you have too many processes");
786 if (errno != EAGAIN) {
796 if ((ed = getenv("EDITOR")) == NULL)
798 execlp(ed, ed, tmpfil, NULL);
801 while ((xpid = wait(&status)) >= 0)
812 while (*cp != '\0' && isspace(*cp))
814 if (*cp == '\0' || *cp == '#')
824 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
826 if ((c = *cp) != '\0') {
835 * Read an ascii label in from fd f,
836 * in the same format as that put out by display(),
840 getasciilabel(FILE *f, struct disklabel32 *lp)
845 char *tp, line[BUFSIZ];
847 int lineno = 0, errors = 0;
850 char unknown[] = "unknown";
852 bzero(&part_set, sizeof(part_set));
853 bzero(&part_size_type, sizeof(part_size_type));
854 bzero(&part_offset_type, sizeof(part_offset_type));
855 lp->d_bbsize = BBSIZE; /* XXX */
856 lp->d_sbsize = SBSIZE; /* XXX */
857 while (fgets(line, sizeof(line) - 1, f)) {
859 if ((cp = strchr(line,'\n')) != NULL)
864 tp = strchr(cp, ':');
866 fprintf(stderr, "line %d: syntax error\n", lineno);
870 *tp++ = '\0', tp = skip(tp);
871 if (streq(cp, "type")) {
875 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++) {
876 if (*cpp && strcasecmp(*cpp, tp) == 0) {
877 lp->d_type = cpp - dktypenames;
881 if (cpp < &dktypenames[DKMAXTYPES])
883 v = strtoul(tp, NULL, 10);
884 if (v >= DKMAXTYPES) {
885 fprintf(stderr, "line %d:%s %lu\n", lineno,
886 "Warning, unknown disk type", v);
891 if (streq(cp, "flags")) {
892 for (v = 0; (cp = tp) && *cp != '\0';) {
894 if (streq(cp, "removeable"))
895 v |= 0; /* obsolete */
896 else if (streq(cp, "ecc"))
897 v |= 0; /* obsolete */
898 else if (streq(cp, "badsect"))
899 v |= 0; /* obsolete */
902 "line %d: %s: bad flag\n",
910 if (streq(cp, "drivedata")) {
911 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA32;) {
912 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
917 if (sscanf(cp, "%lu partitions", &v) == 1) {
918 if (v == 0 || v > MAXPARTITIONS32) {
920 "line %d: bad # of partitions\n", lineno);
921 lp->d_npartitions = MAXPARTITIONS32;
924 lp->d_npartitions = v;
929 if (streq(cp, "disk")) {
930 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
933 if (streq(cp, "label")) {
934 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
937 if (streq(cp, "bytes/sector")) {
938 v = strtoul(tp, NULL, 10);
939 if (v == 0 || (v % DEV_BSIZE) != 0) {
941 "line %d: %s: bad sector size\n",
948 if (streq(cp, "sectors/track")) {
949 v = strtoul(tp, NULL, 10);
950 #if (ULONG_MAX != 0xffffffffUL)
951 if (v == 0 || v > 0xffffffff) {
955 fprintf(stderr, "line %d: %s: bad %s\n",
962 if (streq(cp, "sectors/cylinder")) {
963 v = strtoul(tp, NULL, 10);
965 fprintf(stderr, "line %d: %s: bad %s\n",
972 if (streq(cp, "tracks/cylinder")) {
973 v = strtoul(tp, NULL, 10);
975 fprintf(stderr, "line %d: %s: bad %s\n",
982 if (streq(cp, "cylinders")) {
983 v = strtoul(tp, NULL, 10);
985 fprintf(stderr, "line %d: %s: bad %s\n",
989 lp->d_ncylinders = v;
992 if (streq(cp, "sectors/unit")) {
993 v = strtoul(tp, NULL, 10);
995 fprintf(stderr, "line %d: %s: bad %s\n",
999 lp->d_secperunit = v;
1002 if (streq(cp, "rpm")) {
1003 v = strtoul(tp, NULL, 10);
1004 if (v == 0 || v > USHRT_MAX) {
1005 fprintf(stderr, "line %d: %s: bad %s\n",
1012 if (streq(cp, "interleave")) {
1013 v = strtoul(tp, NULL, 10);
1014 if (v == 0 || v > USHRT_MAX) {
1015 fprintf(stderr, "line %d: %s: bad %s\n",
1019 lp->d_interleave = v;
1022 if (streq(cp, "trackskew")) {
1023 v = strtoul(tp, NULL, 10);
1024 if (v > USHRT_MAX) {
1025 fprintf(stderr, "line %d: %s: bad %s\n",
1029 lp->d_trackskew = v;
1032 if (streq(cp, "cylinderskew")) {
1033 v = strtoul(tp, NULL, 10);
1034 if (v > USHRT_MAX) {
1035 fprintf(stderr, "line %d: %s: bad %s\n",
1042 if (streq(cp, "headswitch")) {
1043 v = strtoul(tp, NULL, 10);
1044 lp->d_headswitch = v;
1047 if (streq(cp, "track-to-track seek")) {
1048 v = strtoul(tp, NULL, 10);
1052 /* the ':' was removed above */
1053 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1055 "line %d: %s: Unknown disklabel field\n", lineno,
1061 /* Process a partition specification line. */
1063 if (part >= lp->d_npartitions) {
1065 "line %d: partition name out of range a-%c: %s\n",
1066 lineno, 'a' + lp->d_npartitions - 1, cp);
1072 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1077 errors += checklabel(lp);
1078 return (errors == 0);
1081 #define NXTNUM(n) do { \
1083 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1086 cp = tp, tp = word(cp); \
1087 (n) = strtoul(cp, NULL, 10); \
1091 /* retain 1 character following number */
1092 #define NXTWORD(w,n) do { \
1094 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1098 cp = tp, tp = word(cp); \
1099 (n) = strtoul(cp, &tmp, 10); \
1100 if (tmp) (w) = *tmp; \
1105 * Read a partition line into partition `part' in the specified disklabel.
1106 * Return 0 on success, 1 on failure.
1109 getasciipartspec(char *tp, struct disklabel32 *lp, int part, int lineno)
1111 struct partition32 *pp;
1116 pp = &lp->d_partitions[part];
1123 NXTWORD(part_size_type[part],v);
1124 if (v == 0 && part_size_type[part] != '*') {
1126 "line %d: %s: bad partition size\n", lineno, cp);
1135 NXTWORD(part_offset_type[part],v);
1136 if (v == 0 && part_offset_type[part] != '*' &&
1137 part_offset_type[part] != '\0') {
1139 "line %d: %s: bad partition offset\n", lineno, cp);
1149 "line %d: no filesystem type was specified\n", lineno);
1154 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++) {
1155 if (*cpp && strcasecmp(*cpp, cp) == 0)
1159 pp->p_fstype = cpp - fstypenames;
1162 v = strtoul(cp, NULL, 10);
1165 if (v >= FSMAXTYPES) {
1167 "line %d: Warning, unknown filesystem type %s\n",
1180 fprintf(stderr, "line %d: Warning, fragment, block, "
1181 "and bps/cpg fields are no\n"
1182 "longer supported and must be specified "
1183 "via newfs options instead.\n",
1190 * Check disklabel for errors and fill in
1191 * derived fields according to supplied values.
1194 checklabel(struct disklabel32 *lp)
1196 struct partition32 *pp;
1199 u_long base_offset, needed, total_size, total_percent, current_offset;
1201 int seen_default_offset;
1204 struct partition32 *pp2;
1206 if (lp->d_secsize == 0) {
1207 fprintf(stderr, "sector size 0\n");
1210 if (lp->d_nsectors == 0) {
1211 fprintf(stderr, "sectors/track 0\n");
1214 if (lp->d_ntracks == 0) {
1215 fprintf(stderr, "tracks/cylinder 0\n");
1218 if (lp->d_ncylinders == 0) {
1219 fprintf(stderr, "cylinders/unit 0\n");
1223 Warning("revolutions/minute 0");
1224 if (lp->d_secpercyl == 0)
1225 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1226 if (lp->d_secperunit == 0)
1227 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1228 if (lp->d_bbsize == 0) {
1229 fprintf(stderr, "boot block size 0\n");
1231 } else if (lp->d_bbsize % lp->d_secsize)
1232 Warning("boot block size %% sector-size != 0");
1233 if (lp->d_sbsize == 0) {
1234 fprintf(stderr, "super block size 0\n");
1236 } else if (lp->d_sbsize % lp->d_secsize)
1237 Warning("super block size %% sector-size != 0");
1238 if (lp->d_npartitions > MAXPARTITIONS32)
1239 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1240 (u_long)lp->d_npartitions, MAXPARTITIONS32);
1242 /* first allocate space to the partitions, then offsets */
1243 total_size = 0; /* in sectors */
1244 total_percent = 0; /* in percent */
1246 /* find all fixed partitions */
1247 for (i = 0; i < lp->d_npartitions; i++) {
1248 pp = &lp->d_partitions[i];
1251 if (part_size_type[i] == '*') {
1252 if (i == RAW_PART) {
1253 pp->p_size = lp->d_secperunit;
1255 if (part_offset_type[i] != '*') {
1256 if (total_size < pp->p_offset)
1257 total_size = pp->p_offset;
1260 Warning("Too many '*' partitions (%c and %c)",
1261 hog_part + 'a',i + 'a');
1269 switch (part_size_type[i]) {
1271 total_percent += size;
1279 size *= 1024ULL * 1024ULL;
1283 size *= 1024ULL * 1024ULL * 1024ULL;
1288 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1291 /* don't count %'s yet */
1292 if (part_size_type[i] != '%') {
1294 * for all not in sectors, convert to
1297 if (part_size_type[i] != '\0') {
1298 if (size % lp->d_secsize != 0)
1299 Warning("partition %c not an integer number of sectors",
1301 size /= lp->d_secsize;
1304 /* else already in sectors */
1312 /* Find out the total free space, excluding the boot block area. */
1313 base_offset = BBSIZE / lp->d_secsize;
1315 for (i = 0; i < lp->d_npartitions; i++) {
1316 pp = &lp->d_partitions[i];
1317 if (!part_set[i] || i == RAW_PART ||
1318 part_size_type[i] == '%' || part_size_type[i] == '*')
1320 if (pp->p_offset > base_offset)
1321 free_space += pp->p_offset - base_offset;
1322 if (pp->p_offset + pp->p_size > base_offset)
1323 base_offset = pp->p_offset + pp->p_size;
1325 if (base_offset < lp->d_secperunit)
1326 free_space += lp->d_secperunit - base_offset;
1328 /* handle % partitions - note %'s don't need to add up to 100! */
1329 if (total_percent != 0) {
1330 if (total_percent > 100) {
1331 fprintf(stderr,"total percentage %lu is greater than 100\n",
1336 if (free_space > 0) {
1337 for (i = 0; i < lp->d_npartitions; i++) {
1338 pp = &lp->d_partitions[i];
1339 if (part_set[i] && part_size_type[i] == '%') {
1340 /* careful of overflows! and integer roundoff */
1341 pp->p_size = ((double)pp->p_size/100) * free_space;
1342 total_size += pp->p_size;
1344 /* FIX we can lose a sector or so due to roundoff per
1345 partition. A more complex algorithm could avoid that */
1350 "%ld sectors available to give to '*' and '%%' partitions\n",
1353 /* fix? set all % partitions to size 0? */
1356 /* give anything remaining to the hog partition */
1357 if (hog_part != -1) {
1359 * Find the range of offsets usable by '*' partitions around
1360 * the hog partition and how much space they need.
1363 base_offset = BBSIZE / lp->d_secsize;
1364 for (i = hog_part - 1; i >= 0; i--) {
1365 pp = &lp->d_partitions[i];
1366 if (!part_set[i] || i == RAW_PART)
1368 if (part_offset_type[i] == '*') {
1369 needed += pp->p_size;
1372 base_offset = pp->p_offset + pp->p_size;
1375 current_offset = lp->d_secperunit;
1376 for (i = lp->d_npartitions - 1; i > hog_part; i--) {
1377 pp = &lp->d_partitions[i];
1378 if (!part_set[i] || i == RAW_PART)
1380 if (part_offset_type[i] == '*') {
1381 needed += pp->p_size;
1384 current_offset = pp->p_offset;
1387 if (current_offset - base_offset <= needed) {
1388 fprintf(stderr, "Cannot find space for partition %c\n",
1391 "Need more than %lu sectors between %lu and %lu\n",
1392 needed, base_offset, current_offset);
1394 lp->d_partitions[hog_part].p_size = 0;
1396 lp->d_partitions[hog_part].p_size = current_offset -
1397 base_offset - needed;
1398 total_size += lp->d_partitions[hog_part].p_size;
1402 /* Now set the offsets for each partition */
1403 current_offset = BBSIZE / lp->d_secsize; /* in sectors */
1404 seen_default_offset = 0;
1405 for (i = 0; i < lp->d_npartitions; i++) {
1407 pp = &lp->d_partitions[i];
1409 if (part_offset_type[i] == '*') {
1410 if (i == RAW_PART) {
1413 pp->p_offset = current_offset;
1414 seen_default_offset = 1;
1417 /* allow them to be out of order for old-style tables */
1418 if (pp->p_offset < current_offset &&
1419 seen_default_offset && i != RAW_PART &&
1420 pp->p_fstype != FS_VINUM) {
1422 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1423 (long)pp->p_offset,i+'a',current_offset);
1425 "Labels with any *'s for offset must be in ascending order by sector\n");
1427 } else if (pp->p_offset != current_offset &&
1428 i != RAW_PART && seen_default_offset) {
1430 * this may give unneeded warnings if
1431 * partitions are out-of-order
1434 "Offset %ld for partition %c doesn't match expected value %ld",
1435 (long)pp->p_offset, i + 'a', current_offset);
1439 current_offset = pp->p_offset + pp->p_size;
1443 for (i = 0; i < lp->d_npartitions; i++) {
1445 pp = &lp->d_partitions[i];
1446 if (pp->p_size == 0 && pp->p_offset != 0)
1447 Warning("partition %c: size 0, but offset %lu",
1448 part, (u_long)pp->p_offset);
1450 if (pp->p_size % lp->d_secpercyl)
1451 Warning("partition %c: size %% cylinder-size != 0",
1453 if (pp->p_offset % lp->d_secpercyl)
1454 Warning("partition %c: offset %% cylinder-size != 0",
1457 if (pp->p_offset > lp->d_secperunit) {
1459 "partition %c: offset past end of unit\n", part);
1462 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1464 "partition %c: partition extends past end of unit\n",
1470 if (pp->p_fstype != FS_UNUSED)
1471 Warning("partition %c is not marked as unused!",part);
1472 if (pp->p_offset != 0)
1473 Warning("partition %c doesn't start at 0!",part);
1474 if (pp->p_size != lp->d_secperunit)
1475 Warning("partition %c doesn't cover the whole unit!",part);
1477 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1478 (pp->p_size != lp->d_secperunit)) {
1479 Warning("An incorrect partition %c may cause problems for "
1480 "standard system utilities",part);
1484 /* check for overlaps */
1485 /* this will check for all possible overlaps once and only once */
1486 for (j = 0; j < i; j++) {
1487 pp2 = &lp->d_partitions[j];
1488 if (j != RAW_PART && i != RAW_PART &&
1489 pp->p_fstype != FS_VINUM &&
1490 pp2->p_fstype != FS_VINUM &&
1491 part_set[i] && part_set[j]) {
1492 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1493 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1494 pp2->p_offset >= pp->p_offset)) {
1495 fprintf(stderr,"partitions %c and %c overlap!\n",
1502 for (; i < 8 || i < lp->d_npartitions; i++) {
1504 pp = &lp->d_partitions[i];
1505 if (pp->p_size || pp->p_offset)
1506 Warning("unused partition %c: size %d offset %lu",
1507 'a' + i, pp->p_size, (u_long)pp->p_offset);
1513 * When operating on a "virgin" disk, try getting an initial label
1514 * from the associated device driver. This might work for all device
1515 * drivers that are able to fetch some initial device parameters
1516 * without even having access to a (BSD) disklabel, like SCSI disks,
1517 * most IDE drives, or vn devices.
1519 static struct disklabel32 dlab;
1521 struct disklabel32 *
1522 getvirginlabel(void)
1524 struct partinfo info;
1525 struct disklabel32 *dl = &dlab;
1528 if ((f = open(dkname, O_RDONLY)) == -1) {
1529 warn("cannot open %s", dkname);
1534 * Check to see if the media is too big for a 32 bit disklabel.
1536 if (ioctl(f, DIOCGPART, &info) == 0) {
1537 if (info.media_size >= 0x100000000ULL * 512) {
1538 warnx("The media is too large for a 32 bit disklabel,"
1539 " please use disklabel64.");
1545 * Generate a virgin disklabel via ioctl
1547 if (ioctl(f, DIOCGDVIRGIN32, dl) < 0) {
1548 l_perror("ioctl DIOCGDVIRGIN32");
1556 struct disklabel32 *
1557 getdisklabelfromdisktab(const char *name)
1560 struct disklabel32 *dl = &dlab;
1563 if ((dt = getdisktabbyname(name)) == NULL)
1565 dl->d_magic = DISKMAGIC32;
1566 dl->d_type = dt->d_typeid;
1568 dl->d_secsize = dt->d_media_blksize;
1569 dl->d_nsectors = dt->d_secpertrack;
1570 dl->d_ntracks = dt->d_nheads;
1571 dl->d_ncylinders = dt->d_ncylinders;
1572 dl->d_secpercyl = dt->d_secpercyl;
1573 dl->d_secperunit = dt->d_media_blocks;
1574 dl->d_rpm = dt->d_rpm;
1575 dl->d_interleave = dt->d_interleave;
1576 dl->d_trackskew = dt->d_trackskew;
1577 dl->d_cylskew = dt->d_cylskew;
1578 dl->d_headswitch = dt->d_headswitch;
1579 dl->d_trkseek = dt->d_trkseek;
1580 dl->d_magic2 = DISKMAGIC32;
1581 dl->d_npartitions = dt->d_npartitions;
1582 dl->d_bbsize = dt->d_bbsize;
1583 dl->d_sbsize = dt->d_sbsize;
1584 for (i = 0; i < dt->d_npartitions; ++i) {
1585 struct partition32 *dlp = &dl->d_partitions[i];
1586 struct dt_partition *dtp = &dt->d_partitions[i];
1588 dlp->p_size = dtp->p_size;
1589 dlp->p_offset = dtp->p_offset;
1590 dlp->p_fsize = dtp->p_fsize;
1591 dlp->p_fstype = dtp->p_fstype;
1592 dlp->p_frag = dtp->p_fsize;
1598 * If we are installing a boot program that doesn't fit in d_bbsize
1599 * we need to mark those partitions that the boot overflows into.
1600 * This allows newfs to prevent creation of a filesystem where it might
1601 * clobber bootstrap code.
1604 setbootflag(struct disklabel32 *lp)
1606 struct partition32 *pp;
1611 if (bootbuf == NULL)
1613 boffset = bootsize / lp->d_secsize;
1614 for (i = 0; i < lp->d_npartitions; i++) {
1616 pp = &lp->d_partitions[i];
1617 if (pp->p_size == 0)
1619 if (boffset <= pp->p_offset) {
1620 if (pp->p_fstype == FS_BOOT)
1621 pp->p_fstype = FS_UNUSED;
1622 } else if (pp->p_fstype != FS_BOOT) {
1623 if (pp->p_fstype != FS_UNUSED) {
1625 "boot overlaps used partition %c\n",
1629 pp->p_fstype = FS_BOOT;
1630 Warning("boot overlaps partition %c, %s",
1631 part, "marked as FS_BOOT");
1636 errx(4, "cannot install boot program");
1641 Warning(const char *fmt, ...)
1645 fprintf(stderr, "Warning, ");
1647 vfprintf(stderr, fmt, ap);
1648 fprintf(stderr, "\n");
1653 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1654 * and earlier DFly bb's are packed against the old disklabel and a new
1655 * disklabel would blow them up. This is a hack that should be removed
1656 * in 2006 sometime (if ever).
1660 checkoldboot(int f, const char *bootbuffer)
1664 if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0)
1666 lseek(f, (off_t)0, SEEK_SET);
1667 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1669 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1671 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1677 * Traditional 32 bit disklabels actually use absolute sector numbers on
1678 * disk, NOT slice relative sector numbres. The OS hides this from us
1679 * when we use DIOC ioctls to access the label, but newer versions of
1680 * Dragonfly no longer adjusts the disklabel when snooping reads or writes
1681 * so we have to figure it out ourselves.
1684 fixlabel(int f, struct disklabel32 *lp, int writeadj)
1686 const char *msg = NULL;
1687 struct partinfo info;
1688 struct partition32 *pp;
1694 size_t rev_len = sizeof(rev);
1696 if (sysctlbyname("kern.osrevision", &rev, &rev_len, NULL, 0) < 0) {
1697 errx(1, "Cannot use raw mode on non-DragonFly systems\n");
1700 warnx("Warning running new disklabel on old DragonFly systems,\n"
1701 "assuming the disk layer will fixup the label.\n");
1706 pp = &lp->d_partitions[RAW_PART];
1709 info.media_offset = slice_start_lba * lp->d_secsize;
1710 info.media_blocks = pp->p_size;
1711 info.media_blksize = lp->d_secsize;
1712 } else if (ioctl(f, DIOCGPART, &info) < 0) {
1713 msg = "Unable to extract the slice starting LBA, "
1714 "you must use the -f <slice_start_lba> option\n"
1715 "to specify it manually, or perhaps try without "
1716 "using -r and let the kernel deal with it\n";
1720 if (lp->d_magic != DISKMAGIC32 || lp->d_magic2 != DISKMAGIC32)
1721 return ("fixlabel: invalid magic");
1722 if (dkcksum32(lp) != 0)
1723 return ("fixlabel: invalid checksum");
1726 * What a mess. For ages old backwards compatibility the disklabel
1727 * on-disk stores absolute offsets instead of slice-relative offsets.
1728 * So fix it up when reading, writing, or snooping.
1730 * The in-core label is always slice-relative.
1737 offset = info.media_offset / info.media_blksize;
1742 start = info.media_offset / info.media_blksize;
1743 offset = -info.media_offset / info.media_blksize;
1745 if (pp->p_offset != start)
1746 return ("fixlabel: raw partition offset != slice offset");
1747 if (pp->p_size != info.media_blocks) {
1748 if (pp->p_size > info.media_blocks)
1749 return ("fixlabel: raw partition size > slice size");
1751 end = start + info.media_blocks;
1753 return ("fixlabel: slice wraps");
1754 if (lp->d_secpercyl <= 0)
1755 return ("fixlabel: d_secpercyl <= 0");
1757 for (part = 0; part < lp->d_npartitions; part++, pp++) {
1758 if (pp->p_offset != 0 || pp->p_size != 0) {
1759 if (pp->p_offset < start
1760 || pp->p_offset + pp->p_size > end
1761 || pp->p_offset + pp->p_size < pp->p_offset) {
1762 /* XXX else silently discard junk. */
1763 bzero(pp, sizeof *pp);
1765 pp->p_offset += offset;
1770 lp->d_checksum = dkcksum32(lp);
1778 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",
1779 "usage: disklabel32 [-r] disk",
1780 "\t\t(to read label)",
1781 " disklabel32 -w [-r] [-n] disk type [packid]",
1782 "\t\t(to write label with existing boot program)",
1783 " disklabel32 -e [-r] [-n] disk",
1784 "\t\t(to edit label)",
1785 " disklabel32 -R [-r] [-n] disk protofile",
1786 "\t\t(to restore label with existing boot program)",
1788 " disklabel32 -B [-n] [-b boot1 -s boot2] disk [type]",
1789 "\t\t(to install boot program with existing label)",
1790 " disklabel32 -w -B [-n] [-b boot1 -s boot2] disk type [packid]",
1791 "\t\t(to write label and boot program)",
1792 " disklabel32 -R -B [-n] [-b boot1 -s boot2] disk protofile [type]",
1793 "\t\t(to restore label and boot program)",
1795 " disklabel32 -B [-n] [-b bootprog] disk [type]",
1796 "\t\t(to install boot program with existing on-disk label)",
1797 " disklabel32 -w -B [-n] [-b bootprog] disk type [packid]",
1798 "\t\t(to write label and install boot program)",
1799 " disklabel32 -R -B [-n] [-b bootprog] disk protofile [type]",
1800 "\t\t(to restore label and install boot program)",
1802 " disklabel32 [-NW] disk",
1803 "\t\t(to write disable/enable label)");
1805 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1806 "usage: disklabel32 [-r] disk", "(to read label)",
1807 " disklabel32 -w [-r] [-n] disk type [packid]",
1808 "\t\t(to write label)",
1809 " disklabel32 -e [-r] [-n] disk",
1810 "\t\t(to edit label)",
1811 " disklabel32 -R [-r] [-n] disk protofile",
1812 "\t\t(to restore label)",
1813 " disklabel32 [-NW] disk",
1814 "\t\t(to write disable/enable label)");
1816 fprintf(stderr, "%s\n%s\n",
1817 " disklabel32 [-f slice_start_lba] [options]",
1818 "\t\t(to force using manual offset)");