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.6 2004/08/30 19:27:21 eirikn 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
87 #if defined(__alpha__) || defined(hp300) || defined(hp800)
94 void makelabel(const char *, const char *, struct disklabel *);
95 int writelabel(int, const char *, struct disklabel *);
96 void l_perror(const char *);
97 struct disklabel *readlabel(int);
98 struct disklabel *makebootarea(char *, struct disklabel *, int);
99 void display(FILE *, const struct disklabel *);
100 int edit(struct disklabel *, int);
104 int getasciilabel(FILE *, struct disklabel *);
105 int getasciipartspec(char *, struct disklabel *, int, int);
106 int checklabel(struct disklabel *);
107 void setbootflag(struct disklabel *);
108 void Warning(const char *, ...) __printflike(1, 2);
110 int checkoldboot(int f, const char *bootbuf);
111 struct disklabel *getvirginlabel(void);
113 #define DEFEDITOR _PATH_VI
114 #define streq(a,b) (strcmp(a,b) == 0)
118 char tmpfil[] = PATH_TMPFILE;
120 char namebuf[BBSIZE], *np = namebuf;
121 struct disklabel lab;
122 char bootarea[BBSIZE];
124 #define MAX_PART ('z')
125 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
126 char part_size_type[MAX_NUM_PARTS];
127 char part_offset_type[MAX_NUM_PARTS];
128 int part_set[MAX_NUM_PARTS];
131 int installboot; /* non-zero if we should install a boot program */
132 char *bootbuf; /* pointer to buffer with remainder of boot prog */
133 int bootsize; /* size of remaining boot program */
134 char *xxboot; /* primary boot */
135 char *bootxx; /* secondary boot */
136 char boot0[MAXPATHLEN];
137 char boot1[MAXPATHLEN];
141 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
145 int disable_write; /* set to disable writing to disk label */
149 #define OPTIONS "BNRWb:denrs:w"
151 #define OPTIONS "BNRWb:enrs:w"
155 main(int argc, char *argv[])
157 struct disklabel *lp;
159 int ch, f = 0, flag, error = 0;
162 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
237 if (dkname[0] != '/') {
238 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
240 np += strlen(specname) + 1;
243 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
244 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
245 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
246 np = namebuf + strlen(specname) + 1;
247 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
250 err(4, "%s", specname);
266 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
267 err(4, "ioctl DIOCWLABEL");
275 error = checklabel(lp);
276 if (checkoldboot(f, NULL))
277 warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
282 if (installboot && argc == 3) {
283 makelabel(argv[2], 0, &lab);
287 * We only called makelabel() for its side effect
288 * of setting the bootstrap file names. Discard
289 * all changes to `lab' so that all values in the
290 * final label come from the ASCII label.
292 bzero((char *)&lab, sizeof(lab));
297 if (!(t = fopen(argv[1], "r")))
298 err(4, "%s", argv[1]);
299 if (!getasciilabel(t, &lab))
301 lp = makebootarea(bootarea, &lab, f);
303 error = writelabel(f, bootarea, lp);
313 makelabel(argv[1], name, &lab);
314 lp = makebootarea(bootarea, &lab, f);
316 if (checklabel(lp) == 0)
317 error = writelabel(f, bootarea, lp);
322 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
323 err(4, "ioctl DIOCWLABEL");
329 struct disklabel tlab;
334 makelabel(argv[1], 0, &lab);
335 lp = makebootarea(bootarea, &lab, f);
337 if (checklabel(lp) == 0)
338 error = writelabel(f, bootarea, lp);
347 * Construct a prototype disklabel from /etc/disktab. As a side
348 * effect, set the names of the primary and secondary boot files
352 makelabel(const char *type, const char *name, struct disklabel *lp)
354 struct disklabel *dp;
356 if (strcmp(type, "auto") == 0)
357 dp = getvirginlabel();
359 dp = getdiskbyname(type);
361 errx(1, "%s: unknown disk type", type);
365 * Set bootstrap name(s).
366 * 1. If set from command line, use those,
367 * 2. otherwise, check if disktab specifies them (b0 or b1),
368 * 3. otherwise, makebootarea() will choose ones based on the name
369 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
371 if (!xxboot && lp->d_boot0) {
372 if (*lp->d_boot0 != '/')
373 (void)sprintf(boot0, "%s/%s",
374 _PATH_BOOTDIR, lp->d_boot0);
376 (void)strcpy(boot0, lp->d_boot0);
380 if (!bootxx && lp->d_boot1) {
381 if (*lp->d_boot1 != '/')
382 (void)sprintf(boot1, "%s/%s",
383 _PATH_BOOTDIR, lp->d_boot1);
385 (void)strcpy(boot1, lp->d_boot1);
390 /* d_packname is union d_boot[01], so zero */
391 bzero(lp->d_packname, sizeof(lp->d_packname));
393 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
397 writelabel(int f, const char *boot, struct disklabel *lp)
409 Warning("write to disk label supressed - label was as follows:");
413 /* make sure we are not overwriting our boot code */
414 if (checkoldboot(f, boot))
415 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
417 lp->d_magic = DISKMAGIC;
418 lp->d_magic2 = DISKMAGIC;
420 lp->d_checksum = dkcksum(lp);
423 * First set the kernel disk label,
424 * then write a label to the raw disk.
425 * If the SDINFO ioctl fails because it is unimplemented,
426 * keep going; otherwise, the kernel consistency checks
427 * may prevent us from changing the current (in-core)
430 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
431 errno != ENODEV && errno != ENOTTY) {
432 l_perror("ioctl DIOCSDINFO");
435 (void)lseek(f, (off_t)0, SEEK_SET);
439 * Generate the bootblock checksum for the SRM console.
441 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
447 * write enable label sector before write (if necessary),
448 * disable after writing.
451 if (ioctl(f, DIOCWLABEL, &flag) < 0)
452 warn("ioctl DIOCWLABEL");
453 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
459 * Output the remainder of the disklabel
461 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
467 (void) ioctl(f, DIOCWLABEL, &flag);
468 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
469 l_perror("ioctl DIOCWDINFO");
473 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
476 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
477 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
478 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
480 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
481 warn("alternate label %d write", i/2);
490 l_perror(const char *s)
495 warnx("%s: no disk label on disk;", s);
496 fprintf(stderr, "add \"-r\" to install initial label\n");
500 warnx("%s: label magic number or checksum is wrong!", s);
501 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
505 warnx("%s: open partition would move or shrink", s);
509 warnx("%s: '%c' partition must start at beginning of disk",
520 * Fetch disklabel for disk.
521 * Use ioctl to get label unless -r flag is given.
526 struct disklabel *lp;
529 if (read(f, bootarea, BBSIZE) < BBSIZE)
530 err(4, "%s", specname);
531 for (lp = (struct disklabel *)bootarea;
532 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
533 lp = (struct disklabel *)((char *)lp + 16))
534 if (lp->d_magic == DISKMAGIC &&
535 lp->d_magic2 == DISKMAGIC)
537 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
538 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
541 "bad pack magic number (label is damaged, or pack is unlabeled)");
544 if (ioctl(f, DIOCGDINFO, lp) < 0)
545 err(4, "ioctl DIOCGDINFO");
551 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
552 * Returns a pointer to the disklabel portion of the bootarea.
555 makebootarea(char *boot, struct disklabel *dp, int f)
557 struct disklabel *lp;
574 if (dp->d_secsize == 0) {
575 dp->d_secsize = DEV_BSIZE;
576 dp->d_bbsize = BBSIZE;
578 lp = (struct disklabel *)
579 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
580 bzero((char *)lp, sizeof *lp);
583 * If we are not installing a boot program but we are installing a
584 * label on disk then we must read the current bootarea so we don't
585 * clobber the existing boot.
589 if (read(f, boot, BBSIZE) < BBSIZE)
590 err(4, "%s", specname);
591 bzero((char *)lp, sizeof *lp);
596 * We are installing a boot program. Determine the name(s) and
597 * read them into the appropriate places in the boot area.
599 if (!xxboot || !bootxx) {
601 if ((p = strrchr(dkname, '/')) == NULL)
605 while (*p && !isdigit(*p))
610 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
615 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
622 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
623 xxboot, bootxx ? bootxx : "NONE");
628 * 1. One-piece bootstrap (hp300/hp800)
629 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
630 * is remembered and written later following the bootarea.
631 * 2. Two-piece bootstraps (vax/i386?/mips?)
632 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
633 * bytes of bootarea, remaining d_bbsize-d_secsize filled
636 b = open(xxboot, O_RDONLY);
638 err(4, "%s", xxboot);
643 * The i386 has the so-called fdisk table embedded into the
644 * primary bootstrap. We take care to not clobber it, but
645 * only if it does already contain some data. (Otherwise,
646 * the xxboot provides a template.)
648 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
649 err(4, "%s", xxboot);
650 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
652 if (read(b, boot, (int)dp->d_secsize) < 0)
653 err(4, "%s", xxboot);
656 for (i = DOSPARTOFF, found = 0;
657 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
659 found = tmpbuf[i] != 0;
661 memcpy((void *)&boot[DOSPARTOFF],
662 (void *)&tmpbuf[DOSPARTOFF],
663 NDOSPART * sizeof(struct dos_partition));
666 b = open(bootxx, O_RDONLY);
668 err(4, "%s", bootxx);
669 if (fstat(b, &sb) != 0)
670 err(4, "%s", bootxx);
671 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
672 errx(4, "%s too large", bootxx);
673 if (read(b, &boot[dp->d_secsize],
674 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
675 err(4, "%s", bootxx);
676 #else /* !(NUMBOOT > 1) */
679 * On the alpha, the primary bootstrap starts at the
680 * second sector of the boot area. The first sector
681 * contains the label and must be edited to contain the
682 * size and location of the primary bootstrap.
684 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
686 err(4, "%s", xxboot);
687 bootinfo = (u_long *)(boot + 480);
688 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
689 bootinfo[1] = 1; /* start at sector 1 */
690 bootinfo[2] = 0; /* flags (must be zero) */
691 #else /* !__alpha__ */
692 if (read(b, boot, (int)dp->d_bbsize) < 0)
693 err(4, "%s", xxboot);
694 #endif /* __alpha__ */
695 if (fstat(b, &sb) != 0)
696 err(4, "%s", xxboot);
697 bootsize = (int)sb.st_size - dp->d_bbsize;
699 /* XXX assume d_secsize is a power of two */
700 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
701 bootbuf = (char *)malloc((size_t)bootsize);
703 err(4, "%s", xxboot);
704 if (read(b, bootbuf, bootsize) < 0) {
706 err(4, "%s", xxboot);
709 #endif /* NUMBOOT > 1 */
711 #endif /* NUMBOOT > 0 */
713 * Make sure no part of the bootstrap is written in the area
714 * reserved for the label.
716 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
718 errx(2, "bootstrap doesn't leave room for disk label");
723 display(FILE *f, const struct disklabel *lp)
726 const struct partition *pp;
728 fprintf(f, "# %s:\n", specname);
729 if (lp->d_type < DKMAXTYPES)
730 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
732 fprintf(f, "type: %u\n", lp->d_type);
733 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
735 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
737 fprintf(f, "flags:");
738 if (lp->d_flags & D_REMOVABLE)
739 fprintf(f, " removeable");
740 if (lp->d_flags & D_ECC)
742 if (lp->d_flags & D_BADSECT)
743 fprintf(f, " badsect");
745 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
746 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
747 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
748 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
749 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
750 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
751 fprintf(f, "rpm: %u\n", lp->d_rpm);
752 fprintf(f, "interleave: %u\n", lp->d_interleave);
753 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
754 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
755 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
756 (u_long)lp->d_headswitch);
757 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
758 (u_long)lp->d_trkseek);
759 fprintf(f, "drivedata: ");
760 for (i = NDDATA - 1; i >= 0; i--)
761 if (lp->d_drivedata[i])
765 for (j = 0; j <= i; j++)
766 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
767 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
769 "# size offset fstype [fsize bsize bps/cpg]\n");
770 pp = lp->d_partitions;
771 for (i = 0; i < lp->d_npartitions; i++, pp++) {
773 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
774 (u_long)pp->p_size, (u_long)pp->p_offset);
775 if (pp->p_fstype < FSMAXTYPES)
776 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
778 fprintf(f, "%8d", pp->p_fstype);
779 switch (pp->p_fstype) {
781 case FS_UNUSED: /* XXX */
782 fprintf(f, " %5lu %5lu %5.5s ",
784 (u_long)(pp->p_fsize * pp->p_frag), "");
788 fprintf(f, " %5lu %5lu %5u ",
790 (u_long)(pp->p_fsize * pp->p_frag),
795 fprintf(f, " %5lu %5lu %5d",
797 (u_long)(pp->p_fsize * pp->p_frag),
802 fprintf(f, "%20.20s", "");
805 fprintf(f, "\t# (Cyl. %4lu",
806 (u_long)(pp->p_offset / lp->d_secpercyl));
807 if (pp->p_offset % lp->d_secpercyl)
812 (u_long)((pp->p_offset + pp->p_size +
813 lp->d_secpercyl - 1) /
814 lp->d_secpercyl - 1));
815 if (pp->p_size % lp->d_secpercyl)
824 edit(struct disklabel *lp, int f)
827 struct disklabel label;
830 if ((fd = mkstemp(tmpfil)) == -1 ||
831 (fp = fdopen(fd, "w")) == NULL) {
832 warnx("can't create %s", tmpfil);
840 fp = fopen(tmpfil, "r");
842 warnx("can't reopen %s for reading", tmpfil);
845 bzero((char *)&label, sizeof(label));
846 if (getasciilabel(fp, &label)) {
848 if (writelabel(f, bootarea, lp) == 0) {
850 (void) unlink(tmpfil);
855 printf("re-edit the label? [y]: "); fflush(stdout);
857 if (c != EOF && c != (int)'\n')
858 while (getchar() != (int)'\n')
863 (void) unlink(tmpfil);
874 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
875 while ((pid = fork()) < 0) {
876 if (errno == EPROCLIM) {
877 warnx("you have too many processes");
880 if (errno != EAGAIN) {
890 if ((ed = getenv("EDITOR")) == (char *)0)
892 execlp(ed, ed, tmpfil, (char *)0);
895 while ((xpid = wait(&stat)) >= 0)
906 while (*cp != '\0' && isspace(*cp))
908 if (*cp == '\0' || *cp == '#')
918 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
920 if ((c = *cp) != '\0') {
929 * Read an ascii label in from fd f,
930 * in the same format as that put out by display(),
934 getasciilabel(FILE *f, struct disklabel *lp)
939 char *tp, line[BUFSIZ];
941 int lineno = 0, errors = 0;
944 bzero(&part_set, sizeof(part_set));
945 bzero(&part_size_type, sizeof(part_size_type));
946 bzero(&part_offset_type, sizeof(part_offset_type));
947 lp->d_bbsize = BBSIZE; /* XXX */
948 lp->d_sbsize = SBSIZE; /* XXX */
949 while (fgets(line, sizeof(line) - 1, f)) {
951 if ((cp = strchr(line,'\n')) != 0)
956 tp = strchr(cp, ':');
958 fprintf(stderr, "line %d: syntax error\n", lineno);
962 *tp++ = '\0', tp = skip(tp);
963 if (streq(cp, "type")) {
967 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
968 if (*cpp && streq(*cpp, tp)) {
969 lp->d_type = cpp - dktypenames;
972 if (cpp < &dktypenames[DKMAXTYPES])
974 v = strtoul(tp, NULL, 10);
976 fprintf(stderr, "line %d:%s %lu\n", lineno,
977 "Warning, unknown disk type", v);
981 if (streq(cp, "flags")) {
982 for (v = 0; (cp = tp) && *cp != '\0';) {
984 if (streq(cp, "removeable"))
986 else if (streq(cp, "ecc"))
988 else if (streq(cp, "badsect"))
992 "line %d: %s: bad flag\n",
1000 if (streq(cp, "drivedata")) {
1001 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
1002 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
1007 if (sscanf(cp, "%lu partitions", &v) == 1) {
1008 if (v == 0 || v > MAXPARTITIONS) {
1010 "line %d: bad # of partitions\n", lineno);
1011 lp->d_npartitions = MAXPARTITIONS;
1014 lp->d_npartitions = v;
1019 if (streq(cp, "disk")) {
1020 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1023 if (streq(cp, "label")) {
1024 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1027 if (streq(cp, "bytes/sector")) {
1028 v = strtoul(tp, NULL, 10);
1029 if (v == 0 || (v % DEV_BSIZE) != 0) {
1031 "line %d: %s: bad sector size\n",
1038 if (streq(cp, "sectors/track")) {
1039 v = strtoul(tp, NULL, 10);
1040 #if (ULONG_MAX != 0xffffffffUL)
1041 if (v == 0 || v > 0xffffffff) {
1045 fprintf(stderr, "line %d: %s: bad %s\n",
1052 if (streq(cp, "sectors/cylinder")) {
1053 v = strtoul(tp, NULL, 10);
1055 fprintf(stderr, "line %d: %s: bad %s\n",
1059 lp->d_secpercyl = v;
1062 if (streq(cp, "tracks/cylinder")) {
1063 v = strtoul(tp, NULL, 10);
1065 fprintf(stderr, "line %d: %s: bad %s\n",
1072 if (streq(cp, "cylinders")) {
1073 v = strtoul(tp, NULL, 10);
1075 fprintf(stderr, "line %d: %s: bad %s\n",
1079 lp->d_ncylinders = v;
1082 if (streq(cp, "sectors/unit")) {
1083 v = strtoul(tp, NULL, 10);
1085 fprintf(stderr, "line %d: %s: bad %s\n",
1089 lp->d_secperunit = v;
1092 if (streq(cp, "rpm")) {
1093 v = strtoul(tp, NULL, 10);
1094 if (v == 0 || v > USHRT_MAX) {
1095 fprintf(stderr, "line %d: %s: bad %s\n",
1102 if (streq(cp, "interleave")) {
1103 v = strtoul(tp, NULL, 10);
1104 if (v == 0 || v > USHRT_MAX) {
1105 fprintf(stderr, "line %d: %s: bad %s\n",
1109 lp->d_interleave = v;
1112 if (streq(cp, "trackskew")) {
1113 v = strtoul(tp, NULL, 10);
1114 if (v > USHRT_MAX) {
1115 fprintf(stderr, "line %d: %s: bad %s\n",
1119 lp->d_trackskew = v;
1122 if (streq(cp, "cylinderskew")) {
1123 v = strtoul(tp, NULL, 10);
1124 if (v > USHRT_MAX) {
1125 fprintf(stderr, "line %d: %s: bad %s\n",
1132 if (streq(cp, "headswitch")) {
1133 v = strtoul(tp, NULL, 10);
1134 lp->d_headswitch = v;
1137 if (streq(cp, "track-to-track seek")) {
1138 v = strtoul(tp, NULL, 10);
1142 /* the ':' was removed above */
1143 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1145 "line %d: %s: Unknown disklabel field\n", lineno,
1151 /* Process a partition specification line. */
1153 if (part >= lp->d_npartitions) {
1155 "line %d: partition name out of range a-%c: %s\n",
1156 lineno, 'a' + lp->d_npartitions - 1, cp);
1162 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1167 errors += checklabel(lp);
1168 return (errors == 0);
1171 #define NXTNUM(n) do { \
1173 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1176 cp = tp, tp = word(cp); \
1177 (n) = strtoul(cp, NULL, 10); \
1181 /* retain 1 character following number */
1182 #define NXTWORD(w,n) do { \
1184 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1188 cp = tp, tp = word(cp); \
1189 (n) = strtoul(cp, &tmp, 10); \
1190 if (tmp) (w) = *tmp; \
1195 * Read a partition line into partition `part' in the specified disklabel.
1196 * Return 0 on success, 1 on failure.
1199 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1201 struct partition *pp;
1206 pp = &lp->d_partitions[part];
1210 NXTWORD(part_size_type[part],v);
1211 if (v == 0 && part_size_type[part] != '*') {
1213 "line %d: %s: bad partition size\n", lineno, cp);
1219 NXTWORD(part_offset_type[part],v);
1220 if (v == 0 && part_offset_type[part] != '*' &&
1221 part_offset_type[part] != '\0') {
1223 "line %d: %s: bad partition offset\n", lineno, cp);
1227 cp = tp, tp = word(cp);
1228 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1229 if (*cpp && streq(*cpp, cp))
1232 pp->p_fstype = cpp - fstypenames;
1235 v = strtoul(cp, NULL, 10);
1238 if (v >= FSMAXTYPES) {
1240 "line %d: Warning, unknown filesystem type %s\n",
1247 switch (pp->p_fstype) {
1250 * allow us to accept defaults for
1254 NXTNUM(pp->p_fsize);
1255 if (pp->p_fsize == 0)
1258 pp->p_frag = v / pp->p_fsize;
1260 /* else default to 0's */
1263 /* These happen to be the same */
1267 NXTNUM(pp->p_fsize);
1268 if (pp->p_fsize == 0)
1271 pp->p_frag = v / pp->p_fsize;
1275 * FIX! poor attempt at adaptive
1278 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1280 * FIX! These are too low, but are traditional
1282 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1283 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1285 pp->p_cpg = DEFAULT_NEWFS_CPG;
1287 pp->p_fsize = BIG_NEWFS_FRAG;
1288 pp->p_frag = BIG_NEWFS_BLOCK /
1290 pp->p_cpg = BIG_NEWFS_CPG;
1300 * Check disklabel for errors and fill in
1301 * derived fields according to supplied values.
1304 checklabel(struct disklabel *lp)
1306 struct partition *pp;
1309 u_long total_size, total_percent, current_offset;
1310 int seen_default_offset;
1313 struct partition *pp2;
1315 if (lp->d_secsize == 0) {
1316 fprintf(stderr, "sector size 0\n");
1319 if (lp->d_nsectors == 0) {
1320 fprintf(stderr, "sectors/track 0\n");
1323 if (lp->d_ntracks == 0) {
1324 fprintf(stderr, "tracks/cylinder 0\n");
1327 if (lp->d_ncylinders == 0) {
1328 fprintf(stderr, "cylinders/unit 0\n");
1332 Warning("revolutions/minute 0");
1333 if (lp->d_secpercyl == 0)
1334 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1335 if (lp->d_secperunit == 0)
1336 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1337 if (lp->d_bbsize == 0) {
1338 fprintf(stderr, "boot block size 0\n");
1340 } else if (lp->d_bbsize % lp->d_secsize)
1341 Warning("boot block size %% sector-size != 0");
1342 if (lp->d_sbsize == 0) {
1343 fprintf(stderr, "super block size 0\n");
1345 } else if (lp->d_sbsize % lp->d_secsize)
1346 Warning("super block size %% sector-size != 0");
1347 if (lp->d_npartitions > MAXPARTITIONS)
1348 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1349 (u_long)lp->d_npartitions, MAXPARTITIONS);
1351 /* first allocate space to the partitions, then offsets */
1352 total_size = 0; /* in sectors */
1353 total_percent = 0; /* in percent */
1355 /* find all fixed partitions */
1356 for (i = 0; i < lp->d_npartitions; i++) {
1357 pp = &lp->d_partitions[i];
1359 if (part_size_type[i] == '*') {
1360 if (i == RAW_PART) {
1361 pp->p_size = lp->d_secperunit;
1364 Warning("Too many '*' partitions (%c and %c)",
1365 hog_part + 'a',i + 'a');
1373 switch (part_size_type[i]) {
1375 total_percent += size;
1383 size *= 1024ULL * 1024ULL;
1387 size *= 1024ULL * 1024ULL * 1024ULL;
1392 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1395 /* don't count %'s yet */
1396 if (part_size_type[i] != '%') {
1398 * for all not in sectors, convert to
1401 if (part_size_type[i] != '\0') {
1402 if (size % lp->d_secsize != 0)
1403 Warning("partition %c not an integer number of sectors",
1405 size /= lp->d_secsize;
1408 /* else already in sectors */
1415 /* handle % partitions - note %'s don't need to add up to 100! */
1416 if (total_percent != 0) {
1417 long free_space = lp->d_secperunit - total_size;
1418 if (total_percent > 100) {
1419 fprintf(stderr,"total percentage %lu is greater than 100\n",
1424 if (free_space > 0) {
1425 for (i = 0; i < lp->d_npartitions; i++) {
1426 pp = &lp->d_partitions[i];
1427 if (part_set[i] && part_size_type[i] == '%') {
1428 /* careful of overflows! and integer roundoff */
1429 pp->p_size = ((double)pp->p_size/100) * free_space;
1430 total_size += pp->p_size;
1432 /* FIX we can lose a sector or so due to roundoff per
1433 partition. A more complex algorithm could avoid that */
1438 "%ld sectors available to give to '*' and '%%' partitions\n",
1441 /* fix? set all % partitions to size 0? */
1444 /* give anything remaining to the hog partition */
1445 if (hog_part != -1) {
1446 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1447 total_size = lp->d_secperunit;
1450 /* Now set the offsets for each partition */
1451 current_offset = 0; /* in sectors */
1452 seen_default_offset = 0;
1453 for (i = 0; i < lp->d_npartitions; i++) {
1455 pp = &lp->d_partitions[i];
1457 if (part_offset_type[i] == '*') {
1458 if (i == RAW_PART) {
1461 pp->p_offset = current_offset;
1462 seen_default_offset = 1;
1465 /* allow them to be out of order for old-style tables */
1466 if (pp->p_offset < current_offset &&
1467 seen_default_offset && i != RAW_PART &&
1468 pp->p_fstype != FS_VINUM) {
1470 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1471 (long)pp->p_offset,i+'a',current_offset);
1473 "Labels with any *'s for offset must be in ascending order by sector\n");
1475 } else if (pp->p_offset != current_offset &&
1476 i != RAW_PART && seen_default_offset) {
1478 * this may give unneeded warnings if
1479 * partitions are out-of-order
1482 "Offset %ld for partition %c doesn't match expected value %ld",
1483 (long)pp->p_offset, i + 'a', current_offset);
1487 current_offset = pp->p_offset + pp->p_size;
1491 for (i = 0; i < lp->d_npartitions; i++) {
1493 pp = &lp->d_partitions[i];
1494 if (pp->p_size == 0 && pp->p_offset != 0)
1495 Warning("partition %c: size 0, but offset %lu",
1496 part, (u_long)pp->p_offset);
1498 if (pp->p_size % lp->d_secpercyl)
1499 Warning("partition %c: size %% cylinder-size != 0",
1501 if (pp->p_offset % lp->d_secpercyl)
1502 Warning("partition %c: offset %% cylinder-size != 0",
1505 if (pp->p_offset > lp->d_secperunit) {
1507 "partition %c: offset past end of unit\n", part);
1510 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1512 "partition %c: partition extends past end of unit\n",
1518 if (pp->p_fstype != FS_UNUSED)
1519 Warning("partition %c is not marked as unused!",part);
1520 if (pp->p_offset != 0)
1521 Warning("partition %c doesn't start at 0!",part);
1522 if (pp->p_size != lp->d_secperunit)
1523 Warning("partition %c doesn't cover the whole unit!",part);
1525 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1526 (pp->p_size != lp->d_secperunit)) {
1527 Warning("An incorrect partition %c may cause problems for "
1528 "standard system utilities",part);
1532 /* check for overlaps */
1533 /* this will check for all possible overlaps once and only once */
1534 for (j = 0; j < i; j++) {
1535 pp2 = &lp->d_partitions[j];
1536 if (j != RAW_PART && i != RAW_PART &&
1537 pp->p_fstype != FS_VINUM &&
1538 pp2->p_fstype != FS_VINUM &&
1539 part_set[i] && part_set[j]) {
1540 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1541 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1542 pp2->p_offset >= pp->p_offset)) {
1543 fprintf(stderr,"partitions %c and %c overlap!\n",
1550 for (; i < 8 || i < lp->d_npartitions; i++) {
1552 pp = &lp->d_partitions[i];
1553 if (pp->p_size || pp->p_offset)
1554 Warning("unused partition %c: size %d offset %lu",
1555 'a' + i, pp->p_size, (u_long)pp->p_offset);
1561 * When operating on a "virgin" disk, try getting an initial label
1562 * from the associated device driver. This might work for all device
1563 * drivers that are able to fetch some initial device parameters
1564 * without even having access to a (BSD) disklabel, like SCSI disks,
1565 * most IDE drives, or vn devices.
1567 * The device name must be given in its "canonical" form.
1570 getvirginlabel(void)
1572 static struct disklabel lab;
1573 char namebuf[BBSIZE];
1576 if (dkname[0] == '/') {
1577 warnx("\"auto\" requires the usage of a canonical disk name");
1580 (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1581 if ((f = open(namebuf, O_RDONLY)) == -1) {
1582 warn("cannot open %s", namebuf);
1587 * Try to use the new get-virgin-label ioctl. If it fails,
1588 * fallback to the old get-disdk-info ioctl.
1590 if (ioctl(f, DIOCGDVIRGIN, &lab) < 0) {
1591 if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1592 warn("ioctl DIOCGDINFO");
1604 * If we are installing a boot program that doesn't fit in d_bbsize
1605 * we need to mark those partitions that the boot overflows into.
1606 * This allows newfs to prevent creation of a filesystem where it might
1607 * clobber bootstrap code.
1610 setbootflag(struct disklabel *lp)
1612 struct partition *pp;
1619 boffset = bootsize / lp->d_secsize;
1620 for (i = 0; i < lp->d_npartitions; i++) {
1622 pp = &lp->d_partitions[i];
1623 if (pp->p_size == 0)
1625 if (boffset <= pp->p_offset) {
1626 if (pp->p_fstype == FS_BOOT)
1627 pp->p_fstype = FS_UNUSED;
1628 } else if (pp->p_fstype != FS_BOOT) {
1629 if (pp->p_fstype != FS_UNUSED) {
1631 "boot overlaps used partition %c\n",
1635 pp->p_fstype = FS_BOOT;
1636 Warning("boot overlaps partition %c, %s",
1637 part, "marked as FS_BOOT");
1642 errx(4, "cannot install boot program");
1647 Warning(const char *fmt, ...)
1651 fprintf(stderr, "Warning, ");
1653 vfprintf(stderr, fmt, ap);
1654 fprintf(stderr, "\n");
1659 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1660 * and earlier DFly bb's are packed against the old disklabel and a new
1661 * disklabel would blow them up. This is a hack that should be removed
1662 * in 2006 sometime (if ever).
1666 checkoldboot(int f, const char *bootbuf)
1670 if (bootbuf && strncmp(bootbuf + 0x402, "BTX", 3) == 0)
1672 lseek(f, (off_t)0, SEEK_SET);
1673 if (read(f, buf, sizeof(buf)) != sizeof(buf))
1675 if (strncmp(buf + 0x402, "BTX", 3) == 0) /* new location */
1677 if (strncmp(buf + 0x316, "BTX", 3) == 0) /* old location */
1686 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",
1687 "usage: disklabel [-r] disk",
1688 "\t\t(to read label)",
1689 " disklabel -w [-r] [-n] disk type [ packid ]",
1690 "\t\t(to write label with existing boot program)",
1691 " disklabel -e [-r] [-n] disk",
1692 "\t\t(to edit label)",
1693 " disklabel -R [-r] [-n] disk protofile",
1694 "\t\t(to restore label with existing boot program)",
1696 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1697 "\t\t(to install boot program with existing label)",
1698 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1699 "\t\t(to write label and boot program)",
1700 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1701 "\t\t(to restore label and boot program)",
1703 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1704 "\t\t(to install boot program with existing on-disk label)",
1705 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1706 "\t\t(to write label and install boot program)",
1707 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1708 "\t\t(to restore label and install boot program)",
1710 " disklabel [-NW] disk",
1711 "\t\t(to write disable/enable label)");
1713 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1714 "usage: disklabel [-r] disk", "(to read label)",
1715 " disklabel -w [-r] [-n] disk type [ packid ]",
1716 "\t\t(to write label)",
1717 " disklabel -e [-r] [-n] disk",
1718 "\t\t(to edit label)",
1719 " disklabel -R [-r] [-n] disk protofile",
1720 "\t\t(to restore label)",
1721 " disklabel [-NW] disk",
1722 "\t\t(to write disable/enable label)");