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.4 2003/11/10 06:14:44 dillon 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 struct disklabel *getvirginlabel(void);
112 #define DEFEDITOR _PATH_VI
113 #define streq(a,b) (strcmp(a,b) == 0)
117 char tmpfil[] = PATH_TMPFILE;
119 char namebuf[BBSIZE], *np = namebuf;
120 struct disklabel lab;
121 char bootarea[BBSIZE];
123 #define MAX_PART ('z')
124 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
125 char part_size_type[MAX_NUM_PARTS];
126 char part_offset_type[MAX_NUM_PARTS];
127 int part_set[MAX_NUM_PARTS];
130 int installboot; /* non-zero if we should install a boot program */
131 char *bootbuf; /* pointer to buffer with remainder of boot prog */
132 int bootsize; /* size of remaining boot program */
133 char *xxboot; /* primary boot */
134 char *bootxx; /* secondary boot */
135 char boot0[MAXPATHLEN];
136 char boot1[MAXPATHLEN];
140 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
144 int disable_write; /* set to disable writing to disk label */
148 #define OPTIONS "BNRWb:denrs:w"
150 #define OPTIONS "BNRWb:enrs:w"
154 main(int argc, char *argv[])
156 struct disklabel *lp;
158 int ch, f = 0, flag, error = 0;
161 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
236 if (dkname[0] != '/') {
237 (void)sprintf(np, "%s%s%c", _PATH_DEV, dkname, 'a' + RAW_PART);
239 np += strlen(specname) + 1;
242 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
243 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
244 (void)sprintf(specname, "%s%s", _PATH_DEV, dkname);
245 np = namebuf + strlen(specname) + 1;
246 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
249 err(4, "%s", specname);
265 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
266 err(4, "ioctl DIOCWLABEL");
274 error = checklabel(lp);
279 if (installboot && argc == 3) {
280 makelabel(argv[2], 0, &lab);
284 * We only called makelabel() for its side effect
285 * of setting the bootstrap file names. Discard
286 * all changes to `lab' so that all values in the
287 * final label come from the ASCII label.
289 bzero((char *)&lab, sizeof(lab));
294 if (!(t = fopen(argv[1], "r")))
295 err(4, "%s", argv[1]);
296 if (!getasciilabel(t, &lab))
298 lp = makebootarea(bootarea, &lab, f);
300 error = writelabel(f, bootarea, lp);
310 makelabel(argv[1], name, &lab);
311 lp = makebootarea(bootarea, &lab, f);
313 if (checklabel(lp) == 0)
314 error = writelabel(f, bootarea, lp);
319 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
320 err(4, "ioctl DIOCWLABEL");
326 struct disklabel tlab;
331 makelabel(argv[1], 0, &lab);
332 lp = makebootarea(bootarea, &lab, f);
334 if (checklabel(lp) == 0)
335 error = writelabel(f, bootarea, lp);
344 * Construct a prototype disklabel from /etc/disktab. As a side
345 * effect, set the names of the primary and secondary boot files
349 makelabel(const char *type, const char *name, struct disklabel *lp)
351 struct disklabel *dp;
353 if (strcmp(type, "auto") == 0)
354 dp = getvirginlabel();
356 dp = getdiskbyname(type);
358 errx(1, "%s: unknown disk type", type);
362 * Set bootstrap name(s).
363 * 1. If set from command line, use those,
364 * 2. otherwise, check if disktab specifies them (b0 or b1),
365 * 3. otherwise, makebootarea() will choose ones based on the name
366 * of the disk special file. E.g. /dev/ra0 -> raboot, bootra
368 if (!xxboot && lp->d_boot0) {
369 if (*lp->d_boot0 != '/')
370 (void)sprintf(boot0, "%s/%s",
371 _PATH_BOOTDIR, lp->d_boot0);
373 (void)strcpy(boot0, lp->d_boot0);
377 if (!bootxx && lp->d_boot1) {
378 if (*lp->d_boot1 != '/')
379 (void)sprintf(boot1, "%s/%s",
380 _PATH_BOOTDIR, lp->d_boot1);
382 (void)strcpy(boot1, lp->d_boot1);
387 /* d_packname is union d_boot[01], so zero */
388 bzero(lp->d_packname, sizeof(lp->d_packname));
390 (void)strncpy(lp->d_packname, name, sizeof(lp->d_packname));
394 writelabel(int f, const char *boot, struct disklabel *lp)
406 Warning("write to disk label supressed - label was as follows:");
411 lp->d_magic = DISKMAGIC;
412 lp->d_magic2 = DISKMAGIC;
414 lp->d_checksum = dkcksum(lp);
417 * First set the kernel disk label,
418 * then write a label to the raw disk.
419 * If the SDINFO ioctl fails because it is unimplemented,
420 * keep going; otherwise, the kernel consistency checks
421 * may prevent us from changing the current (in-core)
424 if (ioctl(f, DIOCSDINFO, lp) < 0 &&
425 errno != ENODEV && errno != ENOTTY) {
426 l_perror("ioctl DIOCSDINFO");
429 (void)lseek(f, (off_t)0, SEEK_SET);
433 * Generate the bootblock checksum for the SRM console.
435 for (p = (u_long *)boot, i = 0, sum = 0; i < 63; i++)
441 * write enable label sector before write (if necessary),
442 * disable after writing.
445 if (ioctl(f, DIOCWLABEL, &flag) < 0)
446 warn("ioctl DIOCWLABEL");
447 if (write(f, boot, lp->d_bbsize) != lp->d_bbsize) {
453 * Output the remainder of the disklabel
455 if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
461 (void) ioctl(f, DIOCWLABEL, &flag);
462 } else if (ioctl(f, DIOCWDINFO, lp) < 0) {
463 l_perror("ioctl DIOCWDINFO");
467 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
470 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
471 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
472 (void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
474 if (write(f, boot, lp->d_secsize) < lp->d_secsize)
475 warn("alternate label %d write", i/2);
484 l_perror(const char *s)
489 warnx("%s: no disk label on disk;", s);
490 fprintf(stderr, "add \"-r\" to install initial label\n");
494 warnx("%s: label magic number or checksum is wrong!", s);
495 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
499 warnx("%s: open partition would move or shrink", s);
503 warnx("%s: '%c' partition must start at beginning of disk",
514 * Fetch disklabel for disk.
515 * Use ioctl to get label unless -r flag is given.
520 struct disklabel *lp;
523 if (read(f, bootarea, BBSIZE) < BBSIZE)
524 err(4, "%s", specname);
525 for (lp = (struct disklabel *)bootarea;
526 lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp));
527 lp = (struct disklabel *)((char *)lp + 16))
528 if (lp->d_magic == DISKMAGIC &&
529 lp->d_magic2 == DISKMAGIC)
531 if (lp > (struct disklabel *)(bootarea+BBSIZE-sizeof(*lp)) ||
532 lp->d_magic != DISKMAGIC || lp->d_magic2 != DISKMAGIC ||
535 "bad pack magic number (label is damaged, or pack is unlabeled)");
538 if (ioctl(f, DIOCGDINFO, lp) < 0)
539 err(4, "ioctl DIOCGDINFO");
545 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
546 * Returns a pointer to the disklabel portion of the bootarea.
549 makebootarea(char *boot, struct disklabel *dp, int f)
551 struct disklabel *lp;
568 if (dp->d_secsize == 0) {
569 dp->d_secsize = DEV_BSIZE;
570 dp->d_bbsize = BBSIZE;
572 lp = (struct disklabel *)
573 (boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
574 bzero((char *)lp, sizeof *lp);
577 * If we are not installing a boot program but we are installing a
578 * label on disk then we must read the current bootarea so we don't
579 * clobber the existing boot.
583 if (read(f, boot, BBSIZE) < BBSIZE)
584 err(4, "%s", specname);
585 bzero((char *)lp, sizeof *lp);
590 * We are installing a boot program. Determine the name(s) and
591 * read them into the appropriate places in the boot area.
593 if (!xxboot || !bootxx) {
595 if ((p = rindex(dkname, '/')) == NULL)
599 while (*p && !isdigit(*p))
604 (void)sprintf(boot0, "%s/boot1", _PATH_BOOTDIR);
609 (void)sprintf(boot1, "%s/boot2", _PATH_BOOTDIR);
616 fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
617 xxboot, bootxx ? bootxx : "NONE");
622 * 1. One-piece bootstrap (hp300/hp800)
623 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
624 * is remembered and written later following the bootarea.
625 * 2. Two-piece bootstraps (vax/i386?/mips?)
626 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
627 * bytes of bootarea, remaining d_bbsize-d_secsize filled
630 b = open(xxboot, O_RDONLY);
632 err(4, "%s", xxboot);
637 * The i386 has the so-called fdisk table embedded into the
638 * primary bootstrap. We take care to not clobber it, but
639 * only if it does already contain some data. (Otherwise,
640 * the xxboot provides a template.)
642 if ((tmpbuf = (char *)malloc((int)dp->d_secsize)) == 0)
643 err(4, "%s", xxboot);
644 memcpy((void *)tmpbuf, (void *)boot, (int)dp->d_secsize);
646 if (read(b, boot, (int)dp->d_secsize) < 0)
647 err(4, "%s", xxboot);
650 for (i = DOSPARTOFF, found = 0;
651 !found && i < DOSPARTOFF + NDOSPART*sizeof(struct dos_partition);
653 found = tmpbuf[i] != 0;
655 memcpy((void *)&boot[DOSPARTOFF],
656 (void *)&tmpbuf[DOSPARTOFF],
657 NDOSPART * sizeof(struct dos_partition));
660 b = open(bootxx, O_RDONLY);
662 err(4, "%s", bootxx);
663 if (fstat(b, &sb) != 0)
664 err(4, "%s", bootxx);
665 if (dp->d_secsize + sb.st_size > dp->d_bbsize)
666 errx(4, "%s too large", bootxx);
667 if (read(b, &boot[dp->d_secsize],
668 (int)(dp->d_bbsize-dp->d_secsize)) < 0)
669 err(4, "%s", bootxx);
670 #else /* !(NUMBOOT > 1) */
673 * On the alpha, the primary bootstrap starts at the
674 * second sector of the boot area. The first sector
675 * contains the label and must be edited to contain the
676 * size and location of the primary bootstrap.
678 n = read(b, boot + dp->d_secsize, (int)dp->d_bbsize);
680 err(4, "%s", xxboot);
681 bootinfo = (u_long *)(boot + 480);
682 bootinfo[0] = (n + dp->d_secsize - 1) / dp->d_secsize;
683 bootinfo[1] = 1; /* start at sector 1 */
684 bootinfo[2] = 0; /* flags (must be zero) */
685 #else /* !__alpha__ */
686 if (read(b, boot, (int)dp->d_bbsize) < 0)
687 err(4, "%s", xxboot);
688 #endif /* __alpha__ */
689 if (fstat(b, &sb) != 0)
690 err(4, "%s", xxboot);
691 bootsize = (int)sb.st_size - dp->d_bbsize;
693 /* XXX assume d_secsize is a power of two */
694 bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
695 bootbuf = (char *)malloc((size_t)bootsize);
697 err(4, "%s", xxboot);
698 if (read(b, bootbuf, bootsize) < 0) {
700 err(4, "%s", xxboot);
703 #endif /* NUMBOOT > 1 */
705 #endif /* NUMBOOT > 0 */
707 * Make sure no part of the bootstrap is written in the area
708 * reserved for the label.
710 for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
712 errx(2, "bootstrap doesn't leave room for disk label");
717 display(FILE *f, const struct disklabel *lp)
720 const struct partition *pp;
722 fprintf(f, "# %s:\n", specname);
723 if (lp->d_type < DKMAXTYPES)
724 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
726 fprintf(f, "type: %u\n", lp->d_type);
727 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
729 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
731 fprintf(f, "flags:");
732 if (lp->d_flags & D_REMOVABLE)
733 fprintf(f, " removeable");
734 if (lp->d_flags & D_ECC)
736 if (lp->d_flags & D_BADSECT)
737 fprintf(f, " badsect");
739 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
740 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
741 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
742 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
743 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
744 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
745 fprintf(f, "rpm: %u\n", lp->d_rpm);
746 fprintf(f, "interleave: %u\n", lp->d_interleave);
747 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
748 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
749 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
750 (u_long)lp->d_headswitch);
751 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
752 (u_long)lp->d_trkseek);
753 fprintf(f, "drivedata: ");
754 for (i = NDDATA - 1; i >= 0; i--)
755 if (lp->d_drivedata[i])
759 for (j = 0; j <= i; j++)
760 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
761 fprintf(f, "\n\n%u partitions:\n", lp->d_npartitions);
763 "# size offset fstype [fsize bsize bps/cpg]\n");
764 pp = lp->d_partitions;
765 for (i = 0; i < lp->d_npartitions; i++, pp++) {
767 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
768 (u_long)pp->p_size, (u_long)pp->p_offset);
769 if (pp->p_fstype < FSMAXTYPES)
770 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
772 fprintf(f, "%8d", pp->p_fstype);
773 switch (pp->p_fstype) {
775 case FS_UNUSED: /* XXX */
776 fprintf(f, " %5lu %5lu %5.5s ",
778 (u_long)(pp->p_fsize * pp->p_frag), "");
782 fprintf(f, " %5lu %5lu %5u ",
784 (u_long)(pp->p_fsize * pp->p_frag),
789 fprintf(f, " %5lu %5lu %5d",
791 (u_long)(pp->p_fsize * pp->p_frag),
796 fprintf(f, "%20.20s", "");
799 fprintf(f, "\t# (Cyl. %4lu",
800 (u_long)(pp->p_offset / lp->d_secpercyl));
801 if (pp->p_offset % lp->d_secpercyl)
806 (u_long)((pp->p_offset + pp->p_size +
807 lp->d_secpercyl - 1) /
808 lp->d_secpercyl - 1));
809 if (pp->p_size % lp->d_secpercyl)
818 edit(struct disklabel *lp, int f)
821 struct disklabel label;
824 if ((fd = mkstemp(tmpfil)) == -1 ||
825 (fp = fdopen(fd, "w")) == NULL) {
826 warnx("can't create %s", tmpfil);
834 fp = fopen(tmpfil, "r");
836 warnx("can't reopen %s for reading", tmpfil);
839 bzero((char *)&label, sizeof(label));
840 if (getasciilabel(fp, &label)) {
842 if (writelabel(f, bootarea, lp) == 0) {
844 (void) unlink(tmpfil);
849 printf("re-edit the label? [y]: "); fflush(stdout);
851 if (c != EOF && c != (int)'\n')
852 while (getchar() != (int)'\n')
857 (void) unlink(tmpfil);
868 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
869 while ((pid = fork()) < 0) {
870 if (errno == EPROCLIM) {
871 warnx("you have too many processes");
874 if (errno != EAGAIN) {
884 if ((ed = getenv("EDITOR")) == (char *)0)
886 execlp(ed, ed, tmpfil, (char *)0);
889 while ((xpid = wait(&stat)) >= 0)
900 while (*cp != '\0' && isspace(*cp))
902 if (*cp == '\0' || *cp == '#')
912 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
914 if ((c = *cp) != '\0') {
923 * Read an ascii label in from fd f,
924 * in the same format as that put out by display(),
928 getasciilabel(FILE *f, struct disklabel *lp)
933 char *tp, line[BUFSIZ];
935 int lineno = 0, errors = 0;
938 bzero(&part_set, sizeof(part_set));
939 bzero(&part_size_type, sizeof(part_size_type));
940 bzero(&part_offset_type, sizeof(part_offset_type));
941 lp->d_bbsize = BBSIZE; /* XXX */
942 lp->d_sbsize = SBSIZE; /* XXX */
943 while (fgets(line, sizeof(line) - 1, f)) {
945 if ((cp = index(line,'\n')) != 0)
952 fprintf(stderr, "line %d: syntax error\n", lineno);
956 *tp++ = '\0', tp = skip(tp);
957 if (streq(cp, "type")) {
961 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
962 if (*cpp && streq(*cpp, tp)) {
963 lp->d_type = cpp - dktypenames;
966 if (cpp < &dktypenames[DKMAXTYPES])
968 v = strtoul(tp, NULL, 10);
970 fprintf(stderr, "line %d:%s %lu\n", lineno,
971 "Warning, unknown disk type", v);
975 if (streq(cp, "flags")) {
976 for (v = 0; (cp = tp) && *cp != '\0';) {
978 if (streq(cp, "removeable"))
980 else if (streq(cp, "ecc"))
982 else if (streq(cp, "badsect"))
986 "line %d: %s: bad flag\n",
994 if (streq(cp, "drivedata")) {
995 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
996 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
1001 if (sscanf(cp, "%lu partitions", &v) == 1) {
1002 if (v == 0 || v > MAXPARTITIONS) {
1004 "line %d: bad # of partitions\n", lineno);
1005 lp->d_npartitions = MAXPARTITIONS;
1008 lp->d_npartitions = v;
1013 if (streq(cp, "disk")) {
1014 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
1017 if (streq(cp, "label")) {
1018 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
1021 if (streq(cp, "bytes/sector")) {
1022 v = strtoul(tp, NULL, 10);
1023 if (v == 0 || (v % DEV_BSIZE) != 0) {
1025 "line %d: %s: bad sector size\n",
1032 if (streq(cp, "sectors/track")) {
1033 v = strtoul(tp, NULL, 10);
1034 #if (ULONG_MAX != 0xffffffffUL)
1035 if (v == 0 || v > 0xffffffff) {
1039 fprintf(stderr, "line %d: %s: bad %s\n",
1046 if (streq(cp, "sectors/cylinder")) {
1047 v = strtoul(tp, NULL, 10);
1049 fprintf(stderr, "line %d: %s: bad %s\n",
1053 lp->d_secpercyl = v;
1056 if (streq(cp, "tracks/cylinder")) {
1057 v = strtoul(tp, NULL, 10);
1059 fprintf(stderr, "line %d: %s: bad %s\n",
1066 if (streq(cp, "cylinders")) {
1067 v = strtoul(tp, NULL, 10);
1069 fprintf(stderr, "line %d: %s: bad %s\n",
1073 lp->d_ncylinders = v;
1076 if (streq(cp, "sectors/unit")) {
1077 v = strtoul(tp, NULL, 10);
1079 fprintf(stderr, "line %d: %s: bad %s\n",
1083 lp->d_secperunit = v;
1086 if (streq(cp, "rpm")) {
1087 v = strtoul(tp, NULL, 10);
1088 if (v == 0 || v > USHRT_MAX) {
1089 fprintf(stderr, "line %d: %s: bad %s\n",
1096 if (streq(cp, "interleave")) {
1097 v = strtoul(tp, NULL, 10);
1098 if (v == 0 || v > USHRT_MAX) {
1099 fprintf(stderr, "line %d: %s: bad %s\n",
1103 lp->d_interleave = v;
1106 if (streq(cp, "trackskew")) {
1107 v = strtoul(tp, NULL, 10);
1108 if (v > USHRT_MAX) {
1109 fprintf(stderr, "line %d: %s: bad %s\n",
1113 lp->d_trackskew = v;
1116 if (streq(cp, "cylinderskew")) {
1117 v = strtoul(tp, NULL, 10);
1118 if (v > USHRT_MAX) {
1119 fprintf(stderr, "line %d: %s: bad %s\n",
1126 if (streq(cp, "headswitch")) {
1127 v = strtoul(tp, NULL, 10);
1128 lp->d_headswitch = v;
1131 if (streq(cp, "track-to-track seek")) {
1132 v = strtoul(tp, NULL, 10);
1136 /* the ':' was removed above */
1137 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
1139 "line %d: %s: Unknown disklabel field\n", lineno,
1145 /* Process a partition specification line. */
1147 if (part >= lp->d_npartitions) {
1149 "line %d: partition name out of range a-%c: %s\n",
1150 lineno, 'a' + lp->d_npartitions - 1, cp);
1156 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1161 errors += checklabel(lp);
1162 return (errors == 0);
1165 #define NXTNUM(n) do { \
1167 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1170 cp = tp, tp = word(cp); \
1171 (n) = strtoul(cp, NULL, 10); \
1175 /* retain 1 character following number */
1176 #define NXTWORD(w,n) do { \
1178 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1182 cp = tp, tp = word(cp); \
1183 (n) = strtoul(cp, &tmp, 10); \
1184 if (tmp) (w) = *tmp; \
1189 * Read a partition line into partition `part' in the specified disklabel.
1190 * Return 0 on success, 1 on failure.
1193 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1195 struct partition *pp;
1200 pp = &lp->d_partitions[part];
1204 NXTWORD(part_size_type[part],v);
1205 if (v == 0 && part_size_type[part] != '*') {
1207 "line %d: %s: bad partition size\n", lineno, cp);
1213 NXTWORD(part_offset_type[part],v);
1214 if (v == 0 && part_offset_type[part] != '*' &&
1215 part_offset_type[part] != '\0') {
1217 "line %d: %s: bad partition offset\n", lineno, cp);
1221 cp = tp, tp = word(cp);
1222 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1223 if (*cpp && streq(*cpp, cp))
1226 pp->p_fstype = cpp - fstypenames;
1229 v = strtoul(cp, NULL, 10);
1232 if (v >= FSMAXTYPES) {
1234 "line %d: Warning, unknown filesystem type %s\n",
1241 switch (pp->p_fstype) {
1244 * allow us to accept defaults for
1248 NXTNUM(pp->p_fsize);
1249 if (pp->p_fsize == 0)
1252 pp->p_frag = v / pp->p_fsize;
1254 /* else default to 0's */
1257 /* These happen to be the same */
1261 NXTNUM(pp->p_fsize);
1262 if (pp->p_fsize == 0)
1265 pp->p_frag = v / pp->p_fsize;
1269 * FIX! poor attempt at adaptive
1272 if (pp->p_size < 1024*1024*1024 / lp->d_secsize) {
1274 * FIX! These are too low, but are traditional
1276 pp->p_fsize = DEFAULT_NEWFS_FRAG;
1277 pp->p_frag = DEFAULT_NEWFS_BLOCK /
1279 pp->p_cpg = DEFAULT_NEWFS_CPG;
1281 pp->p_fsize = BIG_NEWFS_FRAG;
1282 pp->p_frag = BIG_NEWFS_BLOCK /
1284 pp->p_cpg = BIG_NEWFS_CPG;
1294 * Check disklabel for errors and fill in
1295 * derived fields according to supplied values.
1298 checklabel(struct disklabel *lp)
1300 struct partition *pp;
1303 u_long total_size, total_percent, current_offset;
1304 int seen_default_offset;
1307 struct partition *pp2;
1309 if (lp->d_secsize == 0) {
1310 fprintf(stderr, "sector size 0\n");
1313 if (lp->d_nsectors == 0) {
1314 fprintf(stderr, "sectors/track 0\n");
1317 if (lp->d_ntracks == 0) {
1318 fprintf(stderr, "tracks/cylinder 0\n");
1321 if (lp->d_ncylinders == 0) {
1322 fprintf(stderr, "cylinders/unit 0\n");
1326 Warning("revolutions/minute 0");
1327 if (lp->d_secpercyl == 0)
1328 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1329 if (lp->d_secperunit == 0)
1330 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1331 if (lp->d_bbsize == 0) {
1332 fprintf(stderr, "boot block size 0\n");
1334 } else if (lp->d_bbsize % lp->d_secsize)
1335 Warning("boot block size %% sector-size != 0");
1336 if (lp->d_sbsize == 0) {
1337 fprintf(stderr, "super block size 0\n");
1339 } else if (lp->d_sbsize % lp->d_secsize)
1340 Warning("super block size %% sector-size != 0");
1341 if (lp->d_npartitions > MAXPARTITIONS)
1342 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1343 (u_long)lp->d_npartitions, MAXPARTITIONS);
1345 /* first allocate space to the partitions, then offsets */
1346 total_size = 0; /* in sectors */
1347 total_percent = 0; /* in percent */
1349 /* find all fixed partitions */
1350 for (i = 0; i < lp->d_npartitions; i++) {
1351 pp = &lp->d_partitions[i];
1353 if (part_size_type[i] == '*') {
1354 if (i == RAW_PART) {
1355 pp->p_size = lp->d_secperunit;
1358 Warning("Too many '*' partitions (%c and %c)",
1359 hog_part + 'a',i + 'a');
1367 switch (part_size_type[i]) {
1369 total_percent += size;
1377 size *= 1024ULL * 1024ULL;
1381 size *= 1024ULL * 1024ULL * 1024ULL;
1386 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type[i]);
1389 /* don't count %'s yet */
1390 if (part_size_type[i] != '%') {
1392 * for all not in sectors, convert to
1395 if (part_size_type[i] != '\0') {
1396 if (size % lp->d_secsize != 0)
1397 Warning("partition %c not an integer number of sectors",
1399 size /= lp->d_secsize;
1402 /* else already in sectors */
1409 /* handle % partitions - note %'s don't need to add up to 100! */
1410 if (total_percent != 0) {
1411 long free_space = lp->d_secperunit - total_size;
1412 if (total_percent > 100) {
1413 fprintf(stderr,"total percentage %lu is greater than 100\n",
1418 if (free_space > 0) {
1419 for (i = 0; i < lp->d_npartitions; i++) {
1420 pp = &lp->d_partitions[i];
1421 if (part_set[i] && part_size_type[i] == '%') {
1422 /* careful of overflows! and integer roundoff */
1423 pp->p_size = ((double)pp->p_size/100) * free_space;
1424 total_size += pp->p_size;
1426 /* FIX we can lose a sector or so due to roundoff per
1427 partition. A more complex algorithm could avoid that */
1432 "%ld sectors available to give to '*' and '%%' partitions\n",
1435 /* fix? set all % partitions to size 0? */
1438 /* give anything remaining to the hog partition */
1439 if (hog_part != -1) {
1440 lp->d_partitions[hog_part].p_size = lp->d_secperunit - total_size;
1441 total_size = lp->d_secperunit;
1444 /* Now set the offsets for each partition */
1445 current_offset = 0; /* in sectors */
1446 seen_default_offset = 0;
1447 for (i = 0; i < lp->d_npartitions; i++) {
1449 pp = &lp->d_partitions[i];
1451 if (part_offset_type[i] == '*') {
1452 if (i == RAW_PART) {
1455 pp->p_offset = current_offset;
1456 seen_default_offset = 1;
1459 /* allow them to be out of order for old-style tables */
1460 if (pp->p_offset < current_offset &&
1461 seen_default_offset && i != RAW_PART &&
1462 pp->p_fstype != FS_VINUM) {
1464 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1465 (long)pp->p_offset,i+'a',current_offset);
1467 "Labels with any *'s for offset must be in ascending order by sector\n");
1469 } else if (pp->p_offset != current_offset &&
1470 i != RAW_PART && seen_default_offset) {
1472 * this may give unneeded warnings if
1473 * partitions are out-of-order
1476 "Offset %ld for partition %c doesn't match expected value %ld",
1477 (long)pp->p_offset, i + 'a', current_offset);
1481 current_offset = pp->p_offset + pp->p_size;
1485 for (i = 0; i < lp->d_npartitions; i++) {
1487 pp = &lp->d_partitions[i];
1488 if (pp->p_size == 0 && pp->p_offset != 0)
1489 Warning("partition %c: size 0, but offset %lu",
1490 part, (u_long)pp->p_offset);
1492 if (pp->p_size % lp->d_secpercyl)
1493 Warning("partition %c: size %% cylinder-size != 0",
1495 if (pp->p_offset % lp->d_secpercyl)
1496 Warning("partition %c: offset %% cylinder-size != 0",
1499 if (pp->p_offset > lp->d_secperunit) {
1501 "partition %c: offset past end of unit\n", part);
1504 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1506 "partition %c: partition extends past end of unit\n",
1512 if (pp->p_fstype != FS_UNUSED)
1513 Warning("partition %c is not marked as unused!",part);
1514 if (pp->p_offset != 0)
1515 Warning("partition %c doesn't start at 0!",part);
1516 if (pp->p_size != lp->d_secperunit)
1517 Warning("partition %c doesn't cover the whole unit!",part);
1519 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1520 (pp->p_size != lp->d_secperunit)) {
1521 Warning("An incorrect partition %c may cause problems for "
1522 "standard system utilities",part);
1526 /* check for overlaps */
1527 /* this will check for all possible overlaps once and only once */
1528 for (j = 0; j < i; j++) {
1529 pp2 = &lp->d_partitions[j];
1530 if (j != RAW_PART && i != RAW_PART &&
1531 pp->p_fstype != FS_VINUM &&
1532 pp2->p_fstype != FS_VINUM &&
1533 part_set[i] && part_set[j]) {
1534 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1535 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1536 pp2->p_offset >= pp->p_offset)) {
1537 fprintf(stderr,"partitions %c and %c overlap!\n",
1544 for (; i < MAXPARTITIONS; i++) {
1546 pp = &lp->d_partitions[i];
1547 if (pp->p_size || pp->p_offset)
1548 Warning("unused partition %c: size %d offset %lu",
1549 'a' + i, pp->p_size, (u_long)pp->p_offset);
1555 * When operating on a "virgin" disk, try getting an initial label
1556 * from the associated device driver. This might work for all device
1557 * drivers that are able to fetch some initial device parameters
1558 * without even having access to a (BSD) disklabel, like SCSI disks,
1559 * most IDE drives, or vn devices.
1561 * The device name must be given in its "canonical" form.
1564 getvirginlabel(void)
1566 static struct disklabel lab;
1567 char namebuf[BBSIZE];
1570 if (dkname[0] == '/') {
1571 warnx("\"auto\" requires the usage of a canonical disk name");
1574 (void)snprintf(namebuf, BBSIZE, "%s%s", _PATH_DEV, dkname);
1575 if ((f = open(namebuf, O_RDONLY)) == -1) {
1576 warn("cannot open %s", namebuf);
1581 * Try to use the new get-virgin-label ioctl. If it fails,
1582 * fallback to the old get-disdk-info ioctl.
1584 if (ioctl(f, DIOCGDVIRGIN, &lab) < 0) {
1585 if (ioctl(f, DIOCGDINFO, &lab) < 0) {
1586 warn("ioctl DIOCGDINFO");
1598 * If we are installing a boot program that doesn't fit in d_bbsize
1599 * we need to mark those partitions that the boot overflows into.
1600 * This allows newfs to prevent creation of a filesystem where it might
1601 * clobber bootstrap code.
1604 setbootflag(struct disklabel *lp)
1606 struct partition *pp;
1613 boffset = bootsize / lp->d_secsize;
1614 for (i = 0; i < lp->d_npartitions; i++) {
1616 pp = &lp->d_partitions[i];
1617 if (pp->p_size == 0)
1619 if (boffset <= pp->p_offset) {
1620 if (pp->p_fstype == FS_BOOT)
1621 pp->p_fstype = FS_UNUSED;
1622 } else if (pp->p_fstype != FS_BOOT) {
1623 if (pp->p_fstype != FS_UNUSED) {
1625 "boot overlaps used partition %c\n",
1629 pp->p_fstype = FS_BOOT;
1630 Warning("boot overlaps partition %c, %s",
1631 part, "marked as FS_BOOT");
1636 errx(4, "cannot install boot program");
1641 Warning(const char *fmt, ...)
1645 fprintf(stderr, "Warning, ");
1647 vfprintf(stderr, fmt, ap);
1648 fprintf(stderr, "\n");
1656 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",
1657 "usage: disklabel [-r] disk",
1658 "\t\t(to read label)",
1659 " disklabel -w [-r] [-n] disk type [ packid ]",
1660 "\t\t(to write label with existing boot program)",
1661 " disklabel -e [-r] [-n] disk",
1662 "\t\t(to edit label)",
1663 " disklabel -R [-r] [-n] disk protofile",
1664 "\t\t(to restore label with existing boot program)",
1666 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1667 "\t\t(to install boot program with existing label)",
1668 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1669 "\t\t(to write label and boot program)",
1670 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1671 "\t\t(to restore label and boot program)",
1673 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1674 "\t\t(to install boot program with existing on-disk label)",
1675 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1676 "\t\t(to write label and install boot program)",
1677 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1678 "\t\t(to restore label and install boot program)",
1680 " disklabel [-NW] disk",
1681 "\t\t(to write disable/enable label)");
1683 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1684 "usage: disklabel [-r] disk", "(to read label)",
1685 " disklabel -w [-r] [-n] disk type [ packid ]",
1686 "\t\t(to write label)",
1687 " disklabel -e [-r] [-n] disk",
1688 "\t\t(to edit label)",
1689 " disklabel -R [-r] [-n] disk protofile",
1690 "\t\t(to restore label)",
1691 " disklabel [-NW] disk",
1692 "\t\t(to write disable/enable label)");