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.12 2006/10/17 02:18:51 pavalos Exp $
43 #include <sys/param.h>
48 #include <sys/disklabel.h>
49 #include <sys/diskmbr.h>
51 #include <vfs/ufs/dinode.h>
52 #include <vfs/ufs/fs.h>
63 #include "pathnames.h"
66 * Disklabel: read and write disklabels.
67 * The label is usually placed on one of the first sectors of the disk.
68 * Many machines also place a bootstrap in the same area,
69 * in which case the label is embedded in the bootstrap.
70 * The bootstrap source must leave space at the proper offset
71 * for the label on such machines.
75 #define BBSIZE 8192 /* size of boot area, with label */
78 /* FIX! These are too low, but are traditional */
79 #define DEFAULT_NEWFS_BLOCK 8192U
80 #define DEFAULT_NEWFS_FRAG 1024U
81 #define DEFAULT_NEWFS_CPG 16U
83 #define BIG_NEWFS_BLOCK 16384U
84 #define BIG_NEWFS_FRAG 2048U
85 #define BIG_NEWFS_CPG 64U
89 void makelabel(const char *, const char *, struct disklabel *);
90 int writelabel(int, const char *, struct disklabel *);
91 void l_perror(const char *);
92 struct disklabel *readlabel(int);
93 struct disklabel *makebootarea(char *, struct disklabel *, int);
94 void display(FILE *, const struct disklabel *);
95 int edit(struct disklabel *, int);
99 int getasciilabel(FILE *, struct disklabel *);
100 int getasciipartspec(char *, struct disklabel *, int, int);
101 int checklabel(struct disklabel *);
102 void setbootflag(struct disklabel *);
103 void Warning(const char *, ...) __printflike(1, 2);
105 int checkoldboot(int, const char *);
106 struct disklabel *getvirginlabel(void);
108 #define DEFEDITOR _PATH_VI
109 #define streq(a,b) (strcmp(a,b) == 0)
113 char tmpfil[] = PATH_TMPFILE;
115 char namebuf[BBSIZE], *np = namebuf;
116 struct disklabel lab;
117 char bootarea[BBSIZE];
119 #define MAX_PART ('z')
120 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
121 char part_size_type[MAX_NUM_PARTS];
122 char part_offset_type[MAX_NUM_PARTS];
123 int part_set[MAX_NUM_PARTS];
126 int installboot; /* non-zero if we should install a boot program */
127 char *bootbuf; /* pointer to buffer with remainder of boot prog */
128 int bootsize; /* size of remaining boot program */
129 char *xxboot; /* primary boot */
130 char *bootxx; /* secondary boot */
131 char boot0[MAXPATHLEN];
132 char boot1[MAXPATHLEN];
136 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
140 int disable_write; /* set to disable writing to disk label */
144 #define OPTIONS "BNRWb:denrs:w"
146 #define OPTIONS "BNRWb:enrs:w"
150 main(int argc, char *argv[])
152 struct disklabel *lp;
154 int ch, f = 0, flag, error = 0;
157 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
232 if (dkname[0] != '/') {
233 sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
235 np += strlen(specname) + 1;
238 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
239 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
240 sprintf(specname, "%s%s", _PATH_DEV, dkname);
241 np = namebuf + strlen(specname) + 1;
242 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
245 err(4, "%s", specname);
261 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
262 err(4, "ioctl DIOCWLABEL");
270 error = checklabel(lp);
271 if (checkoldboot(f, NULL))
272 warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
277 if (installboot && argc == 3) {
278 makelabel(argv[2], 0, &lab);
282 * We only called makelabel() for its side effect
283 * of setting the bootstrap file names. Discard
284 * all changes to `lab' so that all values in the
285 * final label come from the ASCII label.
287 bzero((char *)&lab, sizeof(lab));
292 if (!(t = fopen(argv[1], "r")))
293 err(4, "%s", argv[1]);
294 if (!getasciilabel(t, &lab))
296 lp = makebootarea(bootarea, &lab, f);
298 error = writelabel(f, bootarea, lp);
308 makelabel(argv[1], name, &lab);
309 lp = makebootarea(bootarea, &lab, f);
311 if (checklabel(lp) == 0)
312 error = writelabel(f, bootarea, lp);
317 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
318 err(4, "ioctl DIOCWLABEL");
324 struct disklabel tlab;
329 makelabel(argv[1], 0, &lab);
330 lp = makebootarea(bootarea, &lab, f);
332 if (checklabel(lp) == 0)
333 error = writelabel(f, bootarea, lp);
342 * Construct a prototype disklabel from /etc/disktab. As a side
343 * effect, set the names of the primary and secondary boot files
347 makelabel(const char *type, const char *name, struct disklabel *lp)
349 struct disklabel *dp;
351 if (strcmp(type, "auto") == 0)
352 dp = getvirginlabel();
354 dp = getdiskbyname(type);
356 errx(1, "%s: unknown disk type", type);
360 * Set bootstrap name(s).
361 * 1. If set from command line, use those,
362 * 2. otherwise, check if disktab specifies them (b0 or b1),
363 * 3. otherwise, makebootarea() will choose ones based on the name
364 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
366 if (!xxboot && lp->d_boot0) {
367 if (*lp->d_boot0 != '/')
368 sprintf(boot0, "%s/%s", _PATH_BOOTDIR, lp->d_boot0);
370 strcpy(boot0, lp->d_boot0);
374 if (!bootxx && lp->d_boot1) {
375 if (*lp->d_boot1 != '/')
376 sprintf(boot1, "%s/%s", _PATH_BOOTDIR, lp->d_boot1);
378 strcpy(boot1, lp->d_boot1);
383 /* d_packname is union d_boot[01], so zero */
384 bzero(lp->d_packname, sizeof(lp->d_packname));
386 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
390 writelabel(int f, const char *boot, struct disklabel *lp)
395 Warning("write to disk label supressed - label was as follows:");
399 /* make sure we are not overwriting our boot code */
400 if (checkoldboot(f, boot))
401 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
403 lp->d_magic = DISKMAGIC;
404 lp->d_magic2 = DISKMAGIC;
406 lp->d_checksum = dkcksum(lp);
409 * First set the kernel disk label,
410 * then write a label to the raw disk.
411 * If the SDINFO ioctl fails because it is unimplemented,
412 * keep going; otherwise, the kernel consistency checks
413 * may prevent us from changing the current (in-core)
416 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
417 errno != ENODEV && errno != ENOTTY) {
418 l_perror("ioctl DIOCSDINFO");
421 lseek(f, (off_t)0, SEEK_SET);
424 * write enable label sector before write (if necessary),
425 * disable after writing.
428 if (ioctl(f, DIOCWLABEL, &flag) < 0)
429 warn("ioctl DIOCWLABEL");
430 if (write(f, boot, lp->d_bbsize) != (ssize_t)lp->d_bbsize) {
436 * Output the remainder of the disklabel
438 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
444 ioctl(f, DIOCWLABEL, &flag);
445 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
446 l_perror("ioctl DIOCWDINFO");
454 l_perror(const char *s)
459 warnx("%s: no disk label on disk;", s);
460 fprintf(stderr, "add \"-r\" to install initial label\n");
464 warnx("%s: label magic number or checksum is wrong!", s);
465 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
469 warnx("%s: open partition would move or shrink", s);
473 warnx("%s: '%c' partition must start at beginning of disk",
484 * Fetch disklabel for disk.
485 * Use ioctl to get label unless -r flag is given.
490 struct disklabel *lp;
493 if (read(f, bootarea, BBSIZE) < BBSIZE)
494 err(4, "%s", specname);
495 for (lp = (struct disklabel *)bootarea;
496 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
497 lp = (struct disklabel *)((char *)lp + 16))
498 if (lp->d_magic == DISKMAGIC &&
499 lp->d_magic2 == DISKMAGIC)
501 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
502 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
505 "bad pack magic number (label is damaged, or pack is unlabeled)");
508 if (ioctl(f, DIOCGDINFO, lp) < 0)
509 err(4, "ioctl DIOCGDINFO");
515 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
516 * Returns a pointer to the disklabel portion of the bootarea.
519 makebootarea(char *boot, struct disklabel *dp, int f)
521 struct disklabel *lp;
530 unsigned int i, found;
534 if (dp->d_secsize == 0) {
535 dp->d_secsize = DEV_BSIZE;
536 dp->d_bbsize = BBSIZE;
538 lp = (struct disklabel *)
539 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
540 bzero((char *)lp, sizeof *lp);
543 * If we are not installing a boot program but we are installing a
544 * label on disk then we must read the current bootarea so we don't
545 * clobber the existing boot.
549 if (read(f, boot, BBSIZE) < BBSIZE)
550 err(4, "%s", specname);
551 bzero((char *)lp, sizeof *lp);
556 * We are installing a boot program. Determine the name(s) and
557 * read them into the appropriate places in the boot area.
559 if (!xxboot || !bootxx) {
561 if ((p = strrchr(dkname, '/')) == NULL)
565 while (*p && !isdigit(*p))
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)) == 0)
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 disklabel); p++)
662 errx(2, "bootstrap doesn't leave room for disk label");
667 display(FILE *f, const struct disklabel *lp)
670 const struct partition *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:");
682 if (lp->d_flags & D_REMOVABLE)
683 fprintf(f, " removeable");
684 if (lp->d_flags & D_ECC)
686 if (lp->d_flags & D_BADSECT)
687 fprintf(f, " badsect");
689 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
690 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
691 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
692 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
693 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
694 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
695 fprintf(f, "rpm: %u\n", lp->d_rpm);
696 fprintf(f, "interleave: %u\n", lp->d_interleave);
697 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
698 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
699 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
700 (u_long)lp->d_headswitch);
701 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
702 (u_long)lp->d_trkseek);
703 fprintf(f, "drivedata: ");
704 for (i = NDDATA - 1; i >= 0; i--)
705 if (lp->d_drivedata[i])
709 for (j = 0; j <= i; j++)
710 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
711 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
713 "# size offset fstype [fsize bsize bps/cpg]\n");
714 pp = lp->d_partitions;
715 for (i = 0; i < lp->d_npartitions; i++, pp++) {
717 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
718 (u_long)pp->p_size, (u_long)pp->p_offset);
719 if (pp->p_fstype < FSMAXTYPES)
720 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
722 fprintf(f, "%8d", pp->p_fstype);
723 switch (pp->p_fstype) {
725 case FS_UNUSED: /* XXX */
726 fprintf(f, " %5lu %5lu %5.5s ",
728 (u_long)(pp->p_fsize * pp->p_frag), "");
732 fprintf(f, " %5lu %5lu %5u ",
734 (u_long)(pp->p_fsize * pp->p_frag),
739 fprintf(f, " %5lu %5lu %5d",
741 (u_long)(pp->p_fsize * pp->p_frag),
746 fprintf(f, "%20.20s", "");
749 fprintf(f, "\t# (Cyl. %4lu",
750 (u_long)(pp->p_offset / lp->d_secpercyl));
751 if (pp->p_offset % lp->d_secpercyl)
756 (u_long)((pp->p_offset + pp->p_size +
757 lp->d_secpercyl - 1) /
758 lp->d_secpercyl - 1));
759 if (pp->p_size % lp->d_secpercyl)
768 edit(struct disklabel *lp, int f)
771 struct disklabel label;
774 if ((fd = mkstemp(tmpfil)) == -1 ||
775 (fp = fdopen(fd, "w")) == NULL) {
776 warnx("can't create %s", tmpfil);
784 fp = fopen(tmpfil, "r");
786 warnx("can't reopen %s for reading", tmpfil);
789 bzero((char *)&label, sizeof(label));
790 if (getasciilabel(fp, &label)) {
792 if (writelabel(f, bootarea, lp) == 0) {
799 printf("re-edit the label? [y]: "); fflush(stdout);
801 if (c != EOF && c != (int)'\n')
802 while (getchar() != (int)'\n')
818 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
819 while ((pid = fork()) < 0) {
820 if (errno == EPROCLIM) {
821 warnx("you have too many processes");
824 if (errno != EAGAIN) {
834 if ((ed = getenv("EDITOR")) == (char *)0)
836 execlp(ed, ed, tmpfil, (char *)0);
839 while ((xpid = wait(&status)) >= 0)
850 while (*cp != '\0' && isspace(*cp))
852 if (*cp == '\0' || *cp == '#')
862 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
864 if ((c = *cp) != '\0') {
873 * Read an ascii label in from fd f,
874 * in the same format as that put out by display(),
878 getasciilabel(FILE *f, struct disklabel *lp)
883 char *tp, line[BUFSIZ];
885 int lineno = 0, errors = 0;
888 char unknown[] = "unknown";
890 bzero(&part_set, sizeof(part_set));
891 bzero(&part_size_type, sizeof(part_size_type));
892 bzero(&part_offset_type, sizeof(part_offset_type));
893 lp->d_bbsize = BBSIZE; /* XXX */
894 lp->d_sbsize = SBSIZE; /* XXX */
895 while (fgets(line, sizeof(line) - 1, f)) {
897 if ((cp = strchr(line,'\n')) != 0)
902 tp = strchr(cp, ':');
904 fprintf(stderr, "line %d: syntax error\n", lineno);
908 *tp++ = '\0', tp = skip(tp);
909 if (streq(cp, "type")) {
913 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
914 if (*cpp && streq(*cpp, tp)) {
915 lp->d_type = cpp - dktypenames;
918 if (cpp < &dktypenames[DKMAXTYPES])
920 v = strtoul(tp, NULL, 10);
922 fprintf(stderr, "line %d:%s %lu\n", lineno,
923 "Warning, unknown disk type", v);
927 if (streq(cp, "flags")) {
928 for (v = 0; (cp = tp) && *cp != '\0';) {
930 if (streq(cp, "removeable"))
932 else if (streq(cp, "ecc"))
934 else if (streq(cp, "badsect"))
938 "line %d: %s: bad flag\n",
946 if (streq(cp, "drivedata")) {
947 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
948 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
953 if (sscanf(cp, "%lu partitions", &v) == 1) {
954 if (v == 0 || v > MAXPARTITIONS) {
956 "line %d: bad # of partitions\n", lineno);
957 lp->d_npartitions = MAXPARTITIONS;
960 lp->d_npartitions = v;
965 if (streq(cp, "disk")) {
966 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
969 if (streq(cp, "label")) {
970 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
973 if (streq(cp, "bytes/sector")) {
974 v = strtoul(tp, NULL, 10);
975 if (v == 0 || (v % DEV_BSIZE) != 0) {
977 "line %d: %s: bad sector size\n",
984 if (streq(cp, "sectors/track")) {
985 v = strtoul(tp, NULL, 10);
986 #if (ULONG_MAX != 0xffffffffUL)
987 if (v == 0 || v > 0xffffffff) {
991 fprintf(stderr, "line %d: %s: bad %s\n",
998 if (streq(cp, "sectors/cylinder")) {
999 v = strtoul(tp, NULL, 10);
1001 fprintf(stderr, "line %d: %s: bad %s\n",
1005 lp->d_secpercyl = v;
1008 if (streq(cp, "tracks/cylinder")) {
1009 v = strtoul(tp, NULL, 10);
1011 fprintf(stderr, "line %d: %s: bad %s\n",
1018 if (streq(cp, "cylinders")) {
1019 v = strtoul(tp, NULL, 10);
1021 fprintf(stderr, "line %d: %s: bad %s\n",
1025 lp->d_ncylinders = v;
1028 if (streq(cp, "sectors/unit")) {
1029 v = strtoul(tp, NULL, 10);
1031 fprintf(stderr, "line %d: %s: bad %s\n",
1035 lp->d_secperunit = v;
1038 if (streq(cp, "rpm")) {
1039 v = strtoul(tp, NULL, 10);
1040 if (v == 0 || v > USHRT_MAX) {
1041 fprintf(stderr, "line %d: %s: bad %s\n",
1048 if (streq(cp, "interleave")) {
1049 v = strtoul(tp, NULL, 10);
1050 if (v == 0 || v > USHRT_MAX) {
1051 fprintf(stderr, "line %d: %s: bad %s\n",
1055 lp->d_interleave = v;
1058 if (streq(cp, "trackskew")) {
1059 v = strtoul(tp, NULL, 10);
1060 if (v > USHRT_MAX) {
1061 fprintf(stderr, "line %d: %s: bad %s\n",
1065 lp->d_trackskew = v;
1068 if (streq(cp, "cylinderskew")) {
1069 v = strtoul(tp, NULL, 10);
1070 if (v > USHRT_MAX) {
1071 fprintf(stderr, "line %d: %s: bad %s\n",
1078 if (streq(cp, "headswitch")) {
1079 v = strtoul(tp, NULL, 10);
1080 lp->d_headswitch = v;
1083 if (streq(cp, "track-to-track seek")) {
1084 v = strtoul(tp, NULL, 10);
1088 /* the ':' was removed above */
1089 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1091 "line %d: %s: Unknown disklabel field\n", lineno,
1097 /* Process a partition specification line. */
1099 if (part >= lp->d_npartitions) {
1101 "line %d: partition name out of range a-%c: %s\n",
1102 lineno, 'a' + lp->d_npartitions - 1, cp);
1108 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1113 errors += checklabel(lp);
1114 return (errors == 0);
1117 #define NXTNUM(n) do { \
1119 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1122 cp = tp, tp = word(cp); \
1123 (n) = strtoul(cp, NULL, 10); \
1127 /* retain 1 character following number */
1128 #define NXTWORD(w,n) do { \
1130 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1134 cp = tp, tp = word(cp); \
1135 (n) = strtoul(cp, &tmp, 10); \
1136 if (tmp) (w) = *tmp; \
1141 * Read a partition line into partition `part' in the specified disklabel.
1142 * Return 0 on success, 1 on failure.
1145 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1147 struct partition *pp;
1152 pp = &lp->d_partitions[part];
1156 NXTWORD(part_size_type[part],v);
1157 if (v == 0 && part_size_type[part] != '*') {
1159 "line %d: %s: bad partition size\n", lineno, cp);
1165 NXTWORD(part_offset_type[part],v);
1166 if (v == 0 && part_offset_type[part] != '*' &&
1167 part_offset_type[part] != '\0') {
1169 "line %d: %s: bad partition offset\n", lineno, cp);
1173 cp = tp, tp = word(cp);
1174 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1175 if (*cpp && streq(*cpp, cp))
1178 pp->p_fstype = cpp - fstypenames;
1181 v = strtoul(cp, NULL, 10);
1184 if (v >= FSMAXTYPES) {
1186 "line %d: Warning, unknown filesystem type %s\n",
1193 switch (pp->p_fstype) {
1196 * allow us to accept defaults for
1200 NXTNUM(pp->p_fsize);
1201 if (pp->p_fsize == 0)
1204 pp->p_frag = v / pp->p_fsize;
1206 /* else default to 0's */
1209 /* These happen to be the same */
1213 NXTNUM(pp->p_fsize);
1214 if (pp->p_fsize == 0)
1217 pp->p_frag = v / pp->p_fsize;
1221 * FIX! poor attempt at adaptive
1224 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1226 * FIX! These are too low, but are traditional
1228 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1229 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1231 pp->p_cpg = DEFAULT_NEWFS_CPG;
1233 pp->p_fsize = BIG_NEWFS_FRAG;
1234 pp->p_frag = BIG_NEWFS_BLOCK /
1236 pp->p_cpg = BIG_NEWFS_CPG;
1246 * Check disklabel for errors and fill in
1247 * derived fields according to supplied values.
1250 checklabel(struct disklabel *lp)
1252 struct partition *pp;
1255 u_long total_size, total_percent, current_offset;
1256 int seen_default_offset;
1259 struct partition *pp2;
1261 if (lp->d_secsize == 0) {
1262 fprintf(stderr, "sector size 0\n");
1265 if (lp->d_nsectors == 0) {
1266 fprintf(stderr, "sectors/track 0\n");
1269 if (lp->d_ntracks == 0) {
1270 fprintf(stderr, "tracks/cylinder 0\n");
1273 if (lp->d_ncylinders == 0) {
1274 fprintf(stderr, "cylinders/unit 0\n");
1278 Warning("revolutions/minute 0");
1279 if (lp->d_secpercyl == 0)
1280 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1281 if (lp->d_secperunit == 0)
1282 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1283 if (lp->d_bbsize == 0) {
1284 fprintf(stderr, "boot block size 0\n");
1286 } else if (lp->d_bbsize % lp->d_secsize)
1287 Warning("boot block size %% sector-size != 0");
1288 if (lp->d_sbsize == 0) {
1289 fprintf(stderr, "super block size 0\n");
1291 } else if (lp->d_sbsize % lp->d_secsize)
1292 Warning("super block size %% sector-size != 0");
1293 if (lp->d_npartitions > MAXPARTITIONS)
1294 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1295 (u_long)lp->d_npartitions, MAXPARTITIONS);
1297 /* first allocate space to the partitions, then offsets */
1298 total_size = 0; /* in sectors */
1299 total_percent = 0; /* in percent */
1301 /* find all fixed partitions */
1302 for (i = 0; i < lp->d_npartitions; i++) {
1303 pp = &lp->d_partitions[i];
1305 if (part_size_type[i] == '*') {
1306 if (i == RAW_PART) {
1307 pp->p_size = lp->d_secperunit;
1310 Warning("Too many '*' partitions (%c and %c)",
1311 hog_part + 'a',i + 'a');
1319 switch (part_size_type[i]) {
1321 total_percent += size;
1329 size *= 1024ULL * 1024ULL;
1333 size *= 1024ULL * 1024ULL * 1024ULL;
1338 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1341 /* don't count %'s yet */
1342 if (part_size_type[i] != '%') {
1344 * for all not in sectors, convert to
1347 if (part_size_type[i] != '\0') {
1348 if (size % lp->d_secsize != 0)
1349 Warning("partition %c not an integer number of sectors",
1351 size /= lp->d_secsize;
1354 /* else already in sectors */
1361 /* handle % partitions - note %'s don't need to add up to 100! */
1362 if (total_percent != 0) {
1363 long free_space = lp->d_secperunit - total_size;
1364 if (total_percent > 100) {
1365 fprintf(stderr,"total percentage %lu is greater than 100\n",
1370 if (free_space > 0) {
1371 for (i = 0; i < lp->d_npartitions; i++) {
1372 pp = &lp->d_partitions[i];
1373 if (part_set[i] && part_size_type[i] == '%') {
1374 /* careful of overflows! and integer roundoff */
1375 pp->p_size = ((double)pp->p_size/100) * free_space;
1376 total_size += pp->p_size;
1378 /* FIX we can lose a sector or so due to roundoff per
1379 partition. A more complex algorithm could avoid that */
1384 "%ld sectors available to give to '*' and '%%' partitions\n",
1387 /* fix? set all % partitions to size 0? */
1390 /* give anything remaining to the hog partition */
1391 if (hog_part != -1) {
1392 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1393 total_size = lp->d_secperunit;
1396 /* Now set the offsets for each partition */
1397 current_offset = 0; /* in sectors */
1398 seen_default_offset = 0;
1399 for (i = 0; i < lp->d_npartitions; i++) {
1401 pp = &lp->d_partitions[i];
1403 if (part_offset_type[i] == '*') {
1404 if (i == RAW_PART) {
1407 pp->p_offset = current_offset;
1408 seen_default_offset = 1;
1411 /* allow them to be out of order for old-style tables */
1412 if (pp->p_offset < current_offset &&
1413 seen_default_offset && i != RAW_PART &&
1414 pp->p_fstype != FS_VINUM) {
1416 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1417 (long)pp->p_offset,i+'a',current_offset);
1419 "Labels with any *'s for offset must be in ascending order by sector\n");
1421 } else if (pp->p_offset != current_offset &&
1422 i != RAW_PART && seen_default_offset) {
1424 * this may give unneeded warnings if
1425 * partitions are out-of-order
1428 "Offset %ld for partition %c doesn't match expected value %ld",
1429 (long)pp->p_offset, i + 'a', current_offset);
1433 current_offset = pp->p_offset + pp->p_size;
1437 for (i = 0; i < lp->d_npartitions; i++) {
1439 pp = &lp->d_partitions[i];
1440 if (pp->p_size == 0 && pp->p_offset != 0)
1441 Warning("partition %c: size 0, but offset %lu",
1442 part, (u_long)pp->p_offset);
1444 if (pp->p_size % lp->d_secpercyl)
1445 Warning("partition %c: size %% cylinder-size != 0",
1447 if (pp->p_offset % lp->d_secpercyl)
1448 Warning("partition %c: offset %% cylinder-size != 0",
1451 if (pp->p_offset > lp->d_secperunit) {
1453 "partition %c: offset past end of unit\n", part);
1456 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1458 "partition %c: partition extends past end of unit\n",
1464 if (pp->p_fstype != FS_UNUSED)
1465 Warning("partition %c is not marked as unused!",part);
1466 if (pp->p_offset != 0)
1467 Warning("partition %c doesn't start at 0!",part);
1468 if (pp->p_size != lp->d_secperunit)
1469 Warning("partition %c doesn't cover the whole unit!",part);
1471 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1472 (pp->p_size != lp->d_secperunit)) {
1473 Warning("An incorrect partition %c may cause problems for "
1474 "standard system utilities",part);
1478 /* check for overlaps */
1479 /* this will check for all possible overlaps once and only once */
1480 for (j = 0; j < i; j++) {
1481 pp2 = &lp->d_partitions[j];
1482 if (j != RAW_PART && i != RAW_PART &&
1483 pp->p_fstype != FS_VINUM &&
1484 pp2->p_fstype != FS_VINUM &&
1485 part_set[i] && part_set[j]) {
1486 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1487 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1488 pp2->p_offset >= pp->p_offset)) {
1489 fprintf(stderr,"partitions %c and %c overlap!\n",
1496 for (; i < 8 || i < lp->d_npartitions; i++) {
1498 pp = &lp->d_partitions[i];
1499 if (pp->p_size || pp->p_offset)
1500 Warning("unused partition %c: size %d offset %lu",
1501 'a' + i, pp->p_size, (u_long)pp->p_offset);
1507 * When operating on a "virgin" disk, try getting an initial label
1508 * from the associated device driver. This might work for all device
1509 * drivers that are able to fetch some initial device parameters
1510 * without even having access to a (BSD) disklabel, like SCSI disks,
1511 * most IDE drives, or vn devices.
1513 * The device name must be given in its "canonical" form.
1516 getvirginlabel(void)
1518 static struct disklabel dlab;
1519 char nambuf[BBSIZE];
1522 if (dkname[0] == '/') {
1523 warnx("\"auto\" requires the usage of a canonical disk name");
1526 snprintf(nambuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1527 if ((f = open(nambuf, O_RDONLY)) == -1) {
1528 warn("cannot open %s", nambuf);
1533 * Try to use the new get-virgin-label ioctl. If it fails,
1534 * fallback to the old get-disdk-info ioctl.
1536 if (ioctl(f, DIOCGDVIRGIN, &dlab) < 0) {
1537 if (ioctl(f, DIOCGDINFO, &dlab) < 0) {
1538 warn("ioctl DIOCGDINFO");
1544 dlab.d_boot0 = NULL;
1545 dlab.d_boot1 = NULL;
1550 * If we are installing a boot program that doesn't fit in d_bbsize
1551 * we need to mark those partitions that the boot overflows into.
1552 * This allows newfs to prevent creation of a filesystem where it might
1553 * clobber bootstrap code.
1556 setbootflag(struct disklabel *lp)
1558 struct partition *pp;
1565 boffset = bootsize / lp->d_secsize;
1566 for (i = 0; i < lp->d_npartitions; i++) {
1568 pp = &lp->d_partitions[i];
1569 if (pp->p_size == 0)
1571 if (boffset <= pp->p_offset) {
1572 if (pp->p_fstype == FS_BOOT)
1573 pp->p_fstype = FS_UNUSED;
1574 } else if (pp->p_fstype != FS_BOOT) {
1575 if (pp->p_fstype != FS_UNUSED) {
1577 "boot overlaps used partition %c\n",
1581 pp->p_fstype = FS_BOOT;
1582 Warning("boot overlaps partition %c, %s",
1583 part, "marked as FS_BOOT");
1588 errx(4, "cannot install boot program");
1593 Warning(const char *fmt, ...)
1597 fprintf(stderr, "Warning, ");
1599 vfprintf(stderr, fmt, ap);
1600 fprintf(stderr, "\n");
1605 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1606 * and earlier DFly bb's are packed against the old disklabel and a new
1607 * disklabel would blow them up. This is a hack that should be removed
1608 * in 2006 sometime (if ever).
1612 checkoldboot(int f, const char *bootbuffer)
1616 if (bootbuffer && strncmp(bootbuffer + 0x402, "BTX", 3) == 0)
1618 lseek(f, (off_t)0, SEEK_SET);
1619 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1621 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1623 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1632 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",
1633 "usage: disklabel [-r] disk",
1634 "\t\t(to read label)",
1635 " disklabel -w [-r] [-n] disk type [ packid ]",
1636 "\t\t(to write label with existing boot program)",
1637 " disklabel -e [-r] [-n] disk",
1638 "\t\t(to edit label)",
1639 " disklabel -R [-r] [-n] disk protofile",
1640 "\t\t(to restore label with existing boot program)",
1642 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1643 "\t\t(to install boot program with existing label)",
1644 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1645 "\t\t(to write label and boot program)",
1646 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1647 "\t\t(to restore label and boot program)",
1649 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1650 "\t\t(to install boot program with existing on-disk label)",
1651 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1652 "\t\t(to write label and install boot program)",
1653 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1654 "\t\t(to restore label and install boot program)",
1656 " disklabel [-NW] disk",
1657 "\t\t(to write disable/enable label)");
1659 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1660 "usage: disklabel [-r] disk", "(to read label)",
1661 " disklabel -w [-r] [-n] disk type [ packid ]",
1662 "\t\t(to write label)",
1663 " disklabel -e [-r] [-n] disk",
1664 "\t\t(to edit label)",
1665 " disklabel -R [-r] [-n] disk protofile",
1666 "\t\t(to restore label)",
1667 " disklabel [-NW] disk",
1668 "\t\t(to write disable/enable label)");