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.10 2005/04/22 01:47:03 swildner Exp $
43 #include <sys/param.h>
48 #include <sys/disklabel.h>
49 #include <sys/diskmbr.h>
50 #include <vfs/ufs/fs.h>
60 #include "pathnames.h"
63 * Disklabel: read and write disklabels.
64 * The label is usually placed on one of the first sectors of the disk.
65 * Many machines also place a bootstrap in the same area,
66 * in which case the label is embedded in the bootstrap.
67 * The bootstrap source must leave space at the proper offset
68 * for the label on such machines.
72 #define BBSIZE 8192 /* size of boot area, with label */
75 /* FIX! These are too low, but are traditional */
76 #define DEFAULT_NEWFS_BLOCK 8192U
77 #define DEFAULT_NEWFS_FRAG 1024U
78 #define DEFAULT_NEWFS_CPG 16U
80 #define BIG_NEWFS_BLOCK 16384U
81 #define BIG_NEWFS_FRAG 2048U
82 #define BIG_NEWFS_CPG 64U
86 void makelabel(const char *, const char *, struct disklabel *);
87 int writelabel(int, const char *, struct disklabel *);
88 void l_perror(const char *);
89 struct disklabel *readlabel(int);
90 struct disklabel *makebootarea(char *, struct disklabel *, int);
91 void display(FILE *, const struct disklabel *);
92 int edit(struct disklabel *, int);
96 int getasciilabel(FILE *, struct disklabel *);
97 int getasciipartspec(char *, struct disklabel *, int, int);
98 int checklabel(struct disklabel *);
99 void setbootflag(struct disklabel *);
100 void Warning(const char *, ...) __printflike(1, 2);
102 int checkoldboot(int f, const char *bootbuf);
103 struct disklabel *getvirginlabel(void);
105 #define DEFEDITOR _PATH_VI
106 #define streq(a,b) (strcmp(a,b) == 0)
110 char tmpfil[] = PATH_TMPFILE;
112 char namebuf[BBSIZE], *np = namebuf;
113 struct disklabel lab;
114 char bootarea[BBSIZE];
116 #define MAX_PART ('z')
117 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
118 char part_size_type[MAX_NUM_PARTS];
119 char part_offset_type[MAX_NUM_PARTS];
120 int part_set[MAX_NUM_PARTS];
123 int installboot; /* non-zero if we should install a boot program */
124 char *bootbuf; /* pointer to buffer with remainder of boot prog */
125 int bootsize; /* size of remaining boot program */
126 char *xxboot; /* primary boot */
127 char *bootxx; /* secondary boot */
128 char boot0[MAXPATHLEN];
129 char boot1[MAXPATHLEN];
133 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
137 int disable_write; /* set to disable writing to disk label */
141 #define OPTIONS "BNRWb:denrs:w"
143 #define OPTIONS "BNRWb:enrs:w"
147 main(int argc, char *argv[])
149 struct disklabel *lp;
151 int ch, f = 0, flag, error = 0;
154 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
229 if (dkname[0] != '/') {
230 sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
232 np += strlen(specname) + 1;
235 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
236 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
237 sprintf(specname, "%s%s", _PATH_DEV, dkname);
238 np = namebuf + strlen(specname) + 1;
239 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
242 err(4, "%s", specname);
258 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
259 err(4, "ioctl DIOCWLABEL");
267 error = checklabel(lp);
268 if (checkoldboot(f, NULL))
269 warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
274 if (installboot && argc == 3) {
275 makelabel(argv[2], 0, &lab);
279 * We only called makelabel() for its side effect
280 * of setting the bootstrap file names. Discard
281 * all changes to `lab' so that all values in the
282 * final label come from the ASCII label.
284 bzero((char *)&lab, sizeof(lab));
289 if (!(t = fopen(argv[1], "r")))
290 err(4, "%s", argv[1]);
291 if (!getasciilabel(t, &lab))
293 lp = makebootarea(bootarea, &lab, f);
295 error = writelabel(f, bootarea, lp);
305 makelabel(argv[1], name, &lab);
306 lp = makebootarea(bootarea, &lab, f);
308 if (checklabel(lp) == 0)
309 error = writelabel(f, bootarea, lp);
314 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
315 err(4, "ioctl DIOCWLABEL");
321 struct disklabel tlab;
326 makelabel(argv[1], 0, &lab);
327 lp = makebootarea(bootarea, &lab, f);
329 if (checklabel(lp) == 0)
330 error = writelabel(f, bootarea, lp);
339 * Construct a prototype disklabel from /etc/disktab. As a side
340 * effect, set the names of the primary and secondary boot files
344 makelabel(const char *type, const char *name, struct disklabel *lp)
346 struct disklabel *dp;
348 if (strcmp(type, "auto") == 0)
349 dp = getvirginlabel();
351 dp = getdiskbyname(type);
353 errx(1, "%s: unknown disk type", type);
357 * Set bootstrap name(s).
358 * 1. If set from command line, use those,
359 * 2. otherwise, check if disktab specifies them (b0 or b1),
360 * 3. otherwise, makebootarea() will choose ones based on the name
361 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
363 if (!xxboot && lp->d_boot0) {
364 if (*lp->d_boot0 != '/')
365 sprintf(boot0, "%s/%s", _PATH_BOOTDIR, lp->d_boot0);
367 strcpy(boot0, lp->d_boot0);
371 if (!bootxx && lp->d_boot1) {
372 if (*lp->d_boot1 != '/')
373 sprintf(boot1, "%s/%s", _PATH_BOOTDIR, lp->d_boot1);
375 strcpy(boot1, lp->d_boot1);
380 /* d_packname is union d_boot[01], so zero */
381 bzero(lp->d_packname, sizeof(lp->d_packname));
383 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
387 writelabel(int f, const char *boot, struct disklabel *lp)
392 Warning("write to disk label supressed - label was as follows:");
396 /* make sure we are not overwriting our boot code */
397 if (checkoldboot(f, boot))
398 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
400 lp->d_magic = DISKMAGIC;
401 lp->d_magic2 = DISKMAGIC;
403 lp->d_checksum = dkcksum(lp);
406 * First set the kernel disk label,
407 * then write a label to the raw disk.
408 * If the SDINFO ioctl fails because it is unimplemented,
409 * keep going; otherwise, the kernel consistency checks
410 * may prevent us from changing the current (in-core)
413 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
414 errno != ENODEV && errno != ENOTTY) {
415 l_perror("ioctl DIOCSDINFO");
418 lseek(f, (off_t)0, SEEK_SET);
421 * write enable label sector before write (if necessary),
422 * disable after writing.
425 if (ioctl(f, DIOCWLABEL, &flag) < 0)
426 warn("ioctl DIOCWLABEL");
427 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
433 * Output the remainder of the disklabel
435 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
441 ioctl(f, DIOCWLABEL, &flag);
442 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
443 l_perror("ioctl DIOCWDINFO");
451 l_perror(const char *s)
456 warnx("%s: no disk label on disk;", s);
457 fprintf(stderr, "add \"-r\" to install initial label\n");
461 warnx("%s: label magic number or checksum is wrong!", s);
462 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
466 warnx("%s: open partition would move or shrink", s);
470 warnx("%s: '%c' partition must start at beginning of disk",
481 * Fetch disklabel for disk.
482 * Use ioctl to get label unless -r flag is given.
487 struct disklabel *lp;
490 if (read(f, bootarea, BBSIZE) < BBSIZE)
491 err(4, "%s", specname);
492 for (lp = (struct disklabel *)bootarea;
493 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
494 lp = (struct disklabel *)((char *)lp + 16))
495 if (lp->d_magic == DISKMAGIC &&
496 lp->d_magic2 == DISKMAGIC)
498 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
499 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
502 "bad pack magic number (label is damaged, or pack is unlabeled)");
505 if (ioctl(f, DIOCGDINFO, lp) < 0)
506 err(4, "ioctl DIOCGDINFO");
512 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
513 * Returns a pointer to the disklabel portion of the bootarea.
516 makebootarea(char *boot, struct disklabel *dp, int f)
518 struct disklabel *lp;
531 if (dp->d_secsize == 0) {
532 dp->d_secsize = DEV_BSIZE;
533 dp->d_bbsize = BBSIZE;
535 lp = (struct disklabel *)
536 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
537 bzero((char *)lp, sizeof *lp);
540 * If we are not installing a boot program but we are installing a
541 * label on disk then we must read the current bootarea so we don't
542 * clobber the existing boot.
546 if (read(f, boot, BBSIZE) < BBSIZE)
547 err(4, "%s", specname);
548 bzero((char *)lp, sizeof *lp);
553 * We are installing a boot program. Determine the name(s) and
554 * read them into the appropriate places in the boot area.
556 if (!xxboot || !bootxx) {
558 if ((p = strrchr(dkname, '/')) == NULL)
562 while (*p && !isdigit(*p))
567 sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
572 sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
579 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
580 xxboot, bootxx ? bootxx : "NONE");
585 * 1. One-piece bootstrap (hp300/hp800)
586 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
587 * is remembered and written later following the bootarea.
588 * 2. Two-piece bootstraps (vax/i386?/mips?)
589 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
590 * bytes of bootarea, remaining d_bbsize-d_secsize filled
593 b = open(xxboot, O_RDONLY);
595 err(4, "%s", xxboot);
600 * The i386 has the so-called fdisk table embedded into the
601 * primary bootstrap. We take care to not clobber it, but
602 * only if it does already contain some data. (Otherwise,
603 * the xxboot provides a template.)
605 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
606 err(4, "%s", xxboot);
607 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
609 if (read(b, boot, (int)dp->d_secsize) < 0)
610 err(4, "%s", xxboot);
613 for (i = DOSPARTOFF, found = 0;
614 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
616 found = tmpbuf[i] != 0;
618 memcpy((void *)&boot[DOSPARTOFF],
619 (void *)&tmpbuf[DOSPARTOFF],
620 NDOSPART * sizeof(struct dos_partition));
623 b = open(bootxx, O_RDONLY);
625 err(4, "%s", bootxx);
626 if (fstat(b, &sb) != 0)
627 err(4, "%s", bootxx);
628 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
629 errx(4, "%s too large", bootxx);
630 if (read(b, &boot[dp->d_secsize],
631 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
632 err(4, "%s", bootxx);
633 #else /* !(NUMBOOT > 1) */
634 if (read(b, boot, (int)dp->d_bbsize) < 0)
635 err(4, "%s", xxboot);
636 if (fstat(b, &sb) != 0)
637 err(4, "%s", xxboot);
638 bootsize = (int)sb.st_size - dp->d_bbsize;
640 /* XXX assume d_secsize is a power of two */
641 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
642 bootbuf = (char *)malloc((size_t)bootsize);
644 err(4, "%s", xxboot);
645 if (read(b, bootbuf, bootsize) < 0) {
647 err(4, "%s", xxboot);
650 #endif /* NUMBOOT > 1 */
652 #endif /* NUMBOOT > 0 */
654 * Make sure no part of the bootstrap is written in the area
655 * reserved for the label.
657 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
659 errx(2, "bootstrap doesn't leave room for disk label");
664 display(FILE *f, const struct disklabel *lp)
667 const struct partition *pp;
669 fprintf(f, "# %s:\n", specname);
670 if (lp->d_type < DKMAXTYPES)
671 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
673 fprintf(f, "type: %u\n", lp->d_type);
674 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
676 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
678 fprintf(f, "flags:");
679 if (lp->d_flags & D_REMOVABLE)
680 fprintf(f, " removeable");
681 if (lp->d_flags & D_ECC)
683 if (lp->d_flags & D_BADSECT)
684 fprintf(f, " badsect");
686 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
687 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
688 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
689 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
690 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
691 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
692 fprintf(f, "rpm: %u\n", lp->d_rpm);
693 fprintf(f, "interleave: %u\n", lp->d_interleave);
694 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
695 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
696 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
697 (u_long)lp->d_headswitch);
698 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
699 (u_long)lp->d_trkseek);
700 fprintf(f, "drivedata: ");
701 for (i = NDDATA - 1; i >= 0; i--)
702 if (lp->d_drivedata[i])
706 for (j = 0; j <= i; j++)
707 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
708 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
710 "# size offset fstype [fsize bsize bps/cpg]\n");
711 pp = lp->d_partitions;
712 for (i = 0; i < lp->d_npartitions; i++, pp++) {
714 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
715 (u_long)pp->p_size, (u_long)pp->p_offset);
716 if (pp->p_fstype < FSMAXTYPES)
717 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
719 fprintf(f, "%8d", pp->p_fstype);
720 switch (pp->p_fstype) {
722 case FS_UNUSED: /* XXX */
723 fprintf(f, " %5lu %5lu %5.5s ",
725 (u_long)(pp->p_fsize * pp->p_frag), "");
729 fprintf(f, " %5lu %5lu %5u ",
731 (u_long)(pp->p_fsize * pp->p_frag),
736 fprintf(f, " %5lu %5lu %5d",
738 (u_long)(pp->p_fsize * pp->p_frag),
743 fprintf(f, "%20.20s", "");
746 fprintf(f, "\t# (Cyl. %4lu",
747 (u_long)(pp->p_offset / lp->d_secpercyl));
748 if (pp->p_offset % lp->d_secpercyl)
753 (u_long)((pp->p_offset + pp->p_size +
754 lp->d_secpercyl - 1) /
755 lp->d_secpercyl - 1));
756 if (pp->p_size % lp->d_secpercyl)
765 edit(struct disklabel *lp, int f)
768 struct disklabel label;
771 if ((fd = mkstemp(tmpfil)) == -1 ||
772 (fp = fdopen(fd, "w")) == NULL) {
773 warnx("can't create %s", tmpfil);
781 fp = fopen(tmpfil, "r");
783 warnx("can't reopen %s for reading", tmpfil);
786 bzero((char *)&label, sizeof(label));
787 if (getasciilabel(fp, &label)) {
789 if (writelabel(f, bootarea, lp) == 0) {
796 printf("re-edit the label? [y]: "); fflush(stdout);
798 if (c != EOF && c != (int)'\n')
799 while (getchar() != (int)'\n')
815 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
816 while ((pid = fork()) < 0) {
817 if (errno == EPROCLIM) {
818 warnx("you have too many processes");
821 if (errno != EAGAIN) {
831 if ((ed = getenv("EDITOR")) == (char *)0)
833 execlp(ed, ed, tmpfil, (char *)0);
836 while ((xpid = wait(&stat)) >= 0)
847 while (*cp != '\0' && isspace(*cp))
849 if (*cp == '\0' || *cp == '#')
859 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
861 if ((c = *cp) != '\0') {
870 * Read an ascii label in from fd f,
871 * in the same format as that put out by display(),
875 getasciilabel(FILE *f, struct disklabel *lp)
880 char *tp, line[BUFSIZ];
882 int lineno = 0, errors = 0;
885 bzero(&part_set, sizeof(part_set));
886 bzero(&part_size_type, sizeof(part_size_type));
887 bzero(&part_offset_type, sizeof(part_offset_type));
888 lp->d_bbsize = BBSIZE; /* XXX */
889 lp->d_sbsize = SBSIZE; /* XXX */
890 while (fgets(line, sizeof(line) - 1, f)) {
892 if ((cp = strchr(line,'\n')) != 0)
897 tp = strchr(cp, ':');
899 fprintf(stderr, "line %d: syntax error\n", lineno);
903 *tp++ = '\0', tp = skip(tp);
904 if (streq(cp, "type")) {
908 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
909 if (*cpp && streq(*cpp, tp)) {
910 lp->d_type = cpp - dktypenames;
913 if (cpp < &dktypenames[DKMAXTYPES])
915 v = strtoul(tp, NULL, 10);
917 fprintf(stderr, "line %d:%s %lu\n", lineno,
918 "Warning, unknown disk type", v);
922 if (streq(cp, "flags")) {
923 for (v = 0; (cp = tp) && *cp != '\0';) {
925 if (streq(cp, "removeable"))
927 else if (streq(cp, "ecc"))
929 else if (streq(cp, "badsect"))
933 "line %d: %s: bad flag\n",
941 if (streq(cp, "drivedata")) {
942 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
943 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
948 if (sscanf(cp, "%lu partitions", &v) == 1) {
949 if (v == 0 || v > MAXPARTITIONS) {
951 "line %d: bad # of partitions\n", lineno);
952 lp->d_npartitions = MAXPARTITIONS;
955 lp->d_npartitions = v;
960 if (streq(cp, "disk")) {
961 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
964 if (streq(cp, "label")) {
965 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
968 if (streq(cp, "bytes/sector")) {
969 v = strtoul(tp, NULL, 10);
970 if (v == 0 || (v % DEV_BSIZE) != 0) {
972 "line %d: %s: bad sector size\n",
979 if (streq(cp, "sectors/track")) {
980 v = strtoul(tp, NULL, 10);
981 #if (ULONG_MAX != 0xffffffffUL)
982 if (v == 0 || v > 0xffffffff) {
986 fprintf(stderr, "line %d: %s: bad %s\n",
993 if (streq(cp, "sectors/cylinder")) {
994 v = strtoul(tp, NULL, 10);
996 fprintf(stderr, "line %d: %s: bad %s\n",
1000 lp->d_secpercyl = v;
1003 if (streq(cp, "tracks/cylinder")) {
1004 v = strtoul(tp, NULL, 10);
1006 fprintf(stderr, "line %d: %s: bad %s\n",
1013 if (streq(cp, "cylinders")) {
1014 v = strtoul(tp, NULL, 10);
1016 fprintf(stderr, "line %d: %s: bad %s\n",
1020 lp->d_ncylinders = v;
1023 if (streq(cp, "sectors/unit")) {
1024 v = strtoul(tp, NULL, 10);
1026 fprintf(stderr, "line %d: %s: bad %s\n",
1030 lp->d_secperunit = v;
1033 if (streq(cp, "rpm")) {
1034 v = strtoul(tp, NULL, 10);
1035 if (v == 0 || v > USHRT_MAX) {
1036 fprintf(stderr, "line %d: %s: bad %s\n",
1043 if (streq(cp, "interleave")) {
1044 v = strtoul(tp, NULL, 10);
1045 if (v == 0 || v > USHRT_MAX) {
1046 fprintf(stderr, "line %d: %s: bad %s\n",
1050 lp->d_interleave = v;
1053 if (streq(cp, "trackskew")) {
1054 v = strtoul(tp, NULL, 10);
1055 if (v > USHRT_MAX) {
1056 fprintf(stderr, "line %d: %s: bad %s\n",
1060 lp->d_trackskew = v;
1063 if (streq(cp, "cylinderskew")) {
1064 v = strtoul(tp, NULL, 10);
1065 if (v > USHRT_MAX) {
1066 fprintf(stderr, "line %d: %s: bad %s\n",
1073 if (streq(cp, "headswitch")) {
1074 v = strtoul(tp, NULL, 10);
1075 lp->d_headswitch = v;
1078 if (streq(cp, "track-to-track seek")) {
1079 v = strtoul(tp, NULL, 10);
1083 /* the ':' was removed above */
1084 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1086 "line %d: %s: Unknown disklabel field\n", lineno,
1092 /* Process a partition specification line. */
1094 if (part >= lp->d_npartitions) {
1096 "line %d: partition name out of range a-%c: %s\n",
1097 lineno, 'a' + lp->d_npartitions - 1, cp);
1103 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1108 errors += checklabel(lp);
1109 return (errors == 0);
1112 #define NXTNUM(n) do { \
1114 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1117 cp = tp, tp = word(cp); \
1118 (n) = strtoul(cp, NULL, 10); \
1122 /* retain 1 character following number */
1123 #define NXTWORD(w,n) do { \
1125 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1129 cp = tp, tp = word(cp); \
1130 (n) = strtoul(cp, &tmp, 10); \
1131 if (tmp) (w) = *tmp; \
1136 * Read a partition line into partition `part' in the specified disklabel.
1137 * Return 0 on success, 1 on failure.
1140 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1142 struct partition *pp;
1147 pp = &lp->d_partitions[part];
1151 NXTWORD(part_size_type[part],v);
1152 if (v == 0 && part_size_type[part] != '*') {
1154 "line %d: %s: bad partition size\n", lineno, cp);
1160 NXTWORD(part_offset_type[part],v);
1161 if (v == 0 && part_offset_type[part] != '*' &&
1162 part_offset_type[part] != '\0') {
1164 "line %d: %s: bad partition offset\n", lineno, cp);
1168 cp = tp, tp = word(cp);
1169 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1170 if (*cpp && streq(*cpp, cp))
1173 pp->p_fstype = cpp - fstypenames;
1176 v = strtoul(cp, NULL, 10);
1179 if (v >= FSMAXTYPES) {
1181 "line %d: Warning, unknown filesystem type %s\n",
1188 switch (pp->p_fstype) {
1191 * allow us to accept defaults for
1195 NXTNUM(pp->p_fsize);
1196 if (pp->p_fsize == 0)
1199 pp->p_frag = v / pp->p_fsize;
1201 /* else default to 0's */
1204 /* These happen to be the same */
1208 NXTNUM(pp->p_fsize);
1209 if (pp->p_fsize == 0)
1212 pp->p_frag = v / pp->p_fsize;
1216 * FIX! poor attempt at adaptive
1219 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1221 * FIX! These are too low, but are traditional
1223 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1224 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1226 pp->p_cpg = DEFAULT_NEWFS_CPG;
1228 pp->p_fsize = BIG_NEWFS_FRAG;
1229 pp->p_frag = BIG_NEWFS_BLOCK /
1231 pp->p_cpg = BIG_NEWFS_CPG;
1241 * Check disklabel for errors and fill in
1242 * derived fields according to supplied values.
1245 checklabel(struct disklabel *lp)
1247 struct partition *pp;
1250 u_long total_size, total_percent, current_offset;
1251 int seen_default_offset;
1254 struct partition *pp2;
1256 if (lp->d_secsize == 0) {
1257 fprintf(stderr, "sector size 0\n");
1260 if (lp->d_nsectors == 0) {
1261 fprintf(stderr, "sectors/track 0\n");
1264 if (lp->d_ntracks == 0) {
1265 fprintf(stderr, "tracks/cylinder 0\n");
1268 if (lp->d_ncylinders == 0) {
1269 fprintf(stderr, "cylinders/unit 0\n");
1273 Warning("revolutions/minute 0");
1274 if (lp->d_secpercyl == 0)
1275 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1276 if (lp->d_secperunit == 0)
1277 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1278 if (lp->d_bbsize == 0) {
1279 fprintf(stderr, "boot block size 0\n");
1281 } else if (lp->d_bbsize % lp->d_secsize)
1282 Warning("boot block size %% sector-size != 0");
1283 if (lp->d_sbsize == 0) {
1284 fprintf(stderr, "super block size 0\n");
1286 } else if (lp->d_sbsize % lp->d_secsize)
1287 Warning("super block size %% sector-size != 0");
1288 if (lp->d_npartitions > MAXPARTITIONS)
1289 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1290 (u_long)lp->d_npartitions, MAXPARTITIONS);
1292 /* first allocate space to the partitions, then offsets */
1293 total_size = 0; /* in sectors */
1294 total_percent = 0; /* in percent */
1296 /* find all fixed partitions */
1297 for (i = 0; i < lp->d_npartitions; i++) {
1298 pp = &lp->d_partitions[i];
1300 if (part_size_type[i] == '*') {
1301 if (i == RAW_PART) {
1302 pp->p_size = lp->d_secperunit;
1305 Warning("Too many '*' partitions (%c and %c)",
1306 hog_part + 'a',i + 'a');
1314 switch (part_size_type[i]) {
1316 total_percent += size;
1324 size *= 1024ULL * 1024ULL;
1328 size *= 1024ULL * 1024ULL * 1024ULL;
1333 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1336 /* don't count %'s yet */
1337 if (part_size_type[i] != '%') {
1339 * for all not in sectors, convert to
1342 if (part_size_type[i] != '\0') {
1343 if (size % lp->d_secsize != 0)
1344 Warning("partition %c not an integer number of sectors",
1346 size /= lp->d_secsize;
1349 /* else already in sectors */
1356 /* handle % partitions - note %'s don't need to add up to 100! */
1357 if (total_percent != 0) {
1358 long free_space = lp->d_secperunit - total_size;
1359 if (total_percent > 100) {
1360 fprintf(stderr,"total percentage %lu is greater than 100\n",
1365 if (free_space > 0) {
1366 for (i = 0; i < lp->d_npartitions; i++) {
1367 pp = &lp->d_partitions[i];
1368 if (part_set[i] && part_size_type[i] == '%') {
1369 /* careful of overflows! and integer roundoff */
1370 pp->p_size = ((double)pp->p_size/100) * free_space;
1371 total_size += pp->p_size;
1373 /* FIX we can lose a sector or so due to roundoff per
1374 partition. A more complex algorithm could avoid that */
1379 "%ld sectors available to give to '*' and '%%' partitions\n",
1382 /* fix? set all % partitions to size 0? */
1385 /* give anything remaining to the hog partition */
1386 if (hog_part != -1) {
1387 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1388 total_size = lp->d_secperunit;
1391 /* Now set the offsets for each partition */
1392 current_offset = 0; /* in sectors */
1393 seen_default_offset = 0;
1394 for (i = 0; i < lp->d_npartitions; i++) {
1396 pp = &lp->d_partitions[i];
1398 if (part_offset_type[i] == '*') {
1399 if (i == RAW_PART) {
1402 pp->p_offset = current_offset;
1403 seen_default_offset = 1;
1406 /* allow them to be out of order for old-style tables */
1407 if (pp->p_offset < current_offset &&
1408 seen_default_offset && i != RAW_PART &&
1409 pp->p_fstype != FS_VINUM) {
1411 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1412 (long)pp->p_offset,i+'a',current_offset);
1414 "Labels with any *'s for offset must be in ascending order by sector\n");
1416 } else if (pp->p_offset != current_offset &&
1417 i != RAW_PART && seen_default_offset) {
1419 * this may give unneeded warnings if
1420 * partitions are out-of-order
1423 "Offset %ld for partition %c doesn't match expected value %ld",
1424 (long)pp->p_offset, i + 'a', current_offset);
1428 current_offset = pp->p_offset + pp->p_size;
1432 for (i = 0; i < lp->d_npartitions; i++) {
1434 pp = &lp->d_partitions[i];
1435 if (pp->p_size == 0 && pp->p_offset != 0)
1436 Warning("partition %c: size 0, but offset %lu",
1437 part, (u_long)pp->p_offset);
1439 if (pp->p_size % lp->d_secpercyl)
1440 Warning("partition %c: size %% cylinder-size != 0",
1442 if (pp->p_offset % lp->d_secpercyl)
1443 Warning("partition %c: offset %% cylinder-size != 0",
1446 if (pp->p_offset > lp->d_secperunit) {
1448 "partition %c: offset past end of unit\n", part);
1451 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1453 "partition %c: partition extends past end of unit\n",
1459 if (pp->p_fstype != FS_UNUSED)
1460 Warning("partition %c is not marked as unused!",part);
1461 if (pp->p_offset != 0)
1462 Warning("partition %c doesn't start at 0!",part);
1463 if (pp->p_size != lp->d_secperunit)
1464 Warning("partition %c doesn't cover the whole unit!",part);
1466 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1467 (pp->p_size != lp->d_secperunit)) {
1468 Warning("An incorrect partition %c may cause problems for "
1469 "standard system utilities",part);
1473 /* check for overlaps */
1474 /* this will check for all possible overlaps once and only once */
1475 for (j = 0; j < i; j++) {
1476 pp2 = &lp->d_partitions[j];
1477 if (j != RAW_PART && i != RAW_PART &&
1478 pp->p_fstype != FS_VINUM &&
1479 pp2->p_fstype != FS_VINUM &&
1480 part_set[i] && part_set[j]) {
1481 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1482 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1483 pp2->p_offset >= pp->p_offset)) {
1484 fprintf(stderr,"partitions %c and %c overlap!\n",
1491 for (; i < 8 || i < lp->d_npartitions; i++) {
1493 pp = &lp->d_partitions[i];
1494 if (pp->p_size || pp->p_offset)
1495 Warning("unused partition %c: size %d offset %lu",
1496 'a' + i, pp->p_size, (u_long)pp->p_offset);
1502 * When operating on a "virgin" disk, try getting an initial label
1503 * from the associated device driver. This might work for all device
1504 * drivers that are able to fetch some initial device parameters
1505 * without even having access to a (BSD) disklabel, like SCSI disks,
1506 * most IDE drives, or vn devices.
1508 * The device name must be given in its "canonical" form.
1511 getvirginlabel(void)
1513 static struct disklabel lab;
1514 char namebuf[BBSIZE];
1517 if (dkname[0] == '/') {
1518 warnx("\"auto\" requires the usage of a canonical disk name");
1521 snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1522 if ((f = open(namebuf, O_RDONLY)) == -1) {
1523 warn("cannot open %s", namebuf);
1528 * Try to use the new get-virgin-label ioctl. If it fails,
1529 * fallback to the old get-disdk-info ioctl.
1531 if (ioctl(f, DIOCGDVIRGIN, &lab) < 0) {
1532 if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1533 warn("ioctl DIOCGDINFO");
1545 * If we are installing a boot program that doesn't fit in d_bbsize
1546 * we need to mark those partitions that the boot overflows into.
1547 * This allows newfs to prevent creation of a filesystem where it might
1548 * clobber bootstrap code.
1551 setbootflag(struct disklabel *lp)
1553 struct partition *pp;
1560 boffset = bootsize / lp->d_secsize;
1561 for (i = 0; i < lp->d_npartitions; i++) {
1563 pp = &lp->d_partitions[i];
1564 if (pp->p_size == 0)
1566 if (boffset <= pp->p_offset) {
1567 if (pp->p_fstype == FS_BOOT)
1568 pp->p_fstype = FS_UNUSED;
1569 } else if (pp->p_fstype != FS_BOOT) {
1570 if (pp->p_fstype != FS_UNUSED) {
1572 "boot overlaps used partition %c\n",
1576 pp->p_fstype = FS_BOOT;
1577 Warning("boot overlaps partition %c, %s",
1578 part, "marked as FS_BOOT");
1583 errx(4, "cannot install boot program");
1588 Warning(const char *fmt, ...)
1592 fprintf(stderr, "Warning, ");
1594 vfprintf(stderr, fmt, ap);
1595 fprintf(stderr, "\n");
1600 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1601 * and earlier DFly bb's are packed against the old disklabel and a new
1602 * disklabel would blow them up. This is a hack that should be removed
1603 * in 2006 sometime (if ever).
1607 checkoldboot(int f, const char *bootbuf)
1611 if (bootbuf && strncmp(bootbuf + 0x402, "BTX", 3) == 0)
1613 lseek(f, (off_t)0, SEEK_SET);
1614 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1616 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1618 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1627 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",
1628 "usage: disklabel [-r] disk",
1629 "\t\t(to read label)",
1630 " disklabel -w [-r] [-n] disk type [ packid ]",
1631 "\t\t(to write label with existing boot program)",
1632 " disklabel -e [-r] [-n] disk",
1633 "\t\t(to edit label)",
1634 " disklabel -R [-r] [-n] disk protofile",
1635 "\t\t(to restore label with existing boot program)",
1637 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1638 "\t\t(to install boot program with existing label)",
1639 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1640 "\t\t(to write label and boot program)",
1641 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1642 "\t\t(to restore label and boot program)",
1644 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1645 "\t\t(to install boot program with existing on-disk label)",
1646 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1647 "\t\t(to write label and install boot program)",
1648 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1649 "\t\t(to restore label and install boot program)",
1651 " disklabel [-NW] disk",
1652 "\t\t(to write disable/enable label)");
1654 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1655 "usage: disklabel [-r] disk", "(to read label)",
1656 " disklabel -w [-r] [-n] disk type [ packid ]",
1657 "\t\t(to write label)",
1658 " disklabel -e [-r] [-n] disk",
1659 "\t\t(to edit label)",
1660 " disklabel -R [-r] [-n] disk protofile",
1661 "\t\t(to restore label)",
1662 " disklabel [-NW] disk",
1663 "\t\t(to write disable/enable label)");