2 * Copyright (c) 1994, 1995 Gordon W. Ross
3 * Copyright (c) 1994 Theo de Raadt
5 * Copyright (c) 1987, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Symmetric Computer Systems.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * This product includes software developed by Theo de Raadt.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $
45 static const char copyright[] =
46 "@(#) Copyright (c) 1987, 1993\n\
47 The Regents of the University of California. All rights reserved.\n";
51 static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94";
52 /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
58 #include <sys/param.h>
66 #define MAXPARTITIONS 26
67 #include <sys/disklabel.h>
80 #include "pathnames.h"
82 static void makelabel(const char *, struct disklabel *);
83 static int writelabel(void);
84 static int readlabel(int flag);
85 static void display(FILE *, const struct disklabel *);
86 static int edit(void);
87 static int editit(void);
88 static void fixlabel(struct disklabel *);
89 static char *skip(char *);
90 static char *word(char *);
91 static int getasciilabel(FILE *, struct disklabel *);
92 static int getasciipartspec(char *, struct disklabel *, int, int);
93 static int checklabel(struct disklabel *);
94 static void usage(void);
95 static struct disklabel *getvirginlabel(void);
97 #define DEFEDITOR _PATH_VI
98 #define DEFPARTITIONS 8
101 static char *specname;
102 static char tmpfil[] = PATH_TMPFILE;
104 static struct disklabel lab;
105 static u_char bootarea[BBSIZE];
106 static off_t mediasize;
107 static u_int secsize;
108 static char blank[] = "";
109 static char unknown[] = "unknown";
111 #define MAX_PART ('z')
112 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
113 static char part_size_type[MAX_NUM_PARTS];
114 static char part_offset_type[MAX_NUM_PARTS];
115 static int part_set[MAX_NUM_PARTS];
117 static int installboot; /* non-zero if we should install a boot program */
118 static int allfields; /* present all fields in edit */
119 static char const *xxboot; /* primary boot */
121 static off_t mbroffset;
123 #define LABELSECTOR -1
126 #define LABELOFFSET -1
128 static int labelsoffset = LABELSECTOR;
129 static int labeloffset = LABELOFFSET;
130 static int bbsize = BBSIZE;
131 static int alphacksum =
132 #if defined(__alpha__)
139 UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
143 static int disable_write; /* set to disable writing to disk label */
144 static int is_file; /* work on a file (abs. pathname), "-f" opt. */
147 main(int argc, char *argv[])
151 char const *name = 0;
153 while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1)
168 if (!strcmp(optarg, "i386") ||
169 !strcmp(optarg, "amd64") ||
170 !strcmp(optarg, "ia64") ||
171 !strcmp(optarg, "pc98")) {
176 } else if (!strcmp(optarg, "alpha")) {
182 errx(1, "Unsupported architecture");
200 * We accept and ignode -r for compatibility with
201 * historically disklabel usage.
218 if (labelsoffset < 0 || labeloffset < 0)
219 errx(1, "a -m <architecture> option must be specified");
221 /* Figure out the names of the thing we're working on */
223 dkname = specname = argv[0];
224 } else if (argv[0][0] != '/') {
226 asprintf(&specname, "%s%s", _PATH_DEV, argv[0]);
227 } else if (strncmp(argv[0], _PATH_DEV, strlen(_PATH_DEV)) == 0) {
228 dkname = argv[0] + strlen(_PATH_DEV);
231 dkname = strrchr(argv[0], '/');
236 if (installboot && op == UNSPEC)
238 else if (op == UNSPEC)
258 display(stdout, NULL);
259 error = checklabel(NULL);
265 if (!(t = fopen(argv[1], "r")))
266 err(4, "fopen %s", argv[1]);
268 if (!getasciilabel(t, &lab))
270 error = writelabel();
281 makelabel(name, &lab);
283 if (checklabel(NULL) == 0)
284 error = writelabel();
292 makelabel(argv[1], &lab);
293 if (checklabel(NULL) == 0)
294 error = writelabel();
301 fixlabel(struct disklabel *lp)
303 struct partition *dp;
306 for (i = 0; i < lp->d_npartitions; i++) {
309 if (lp->d_partitions[i].p_size)
313 dp = &lp->d_partitions[0];
314 dp->p_offset = BBSIZE / secsize;
315 dp->p_size = lp->d_secperunit - dp->p_offset;
319 * Construct a prototype disklabel from /etc/disktab.
322 makelabel(const char *type, struct disklabel *lp)
324 struct disklabel *dp;
326 if (strcmp(type, "auto") == 0)
327 dp = getvirginlabel();
329 dp = getdiskbyname(type);
331 errx(1, "%s: unknown disk type", type);
333 bzero(lp->d_packname, sizeof(lp->d_packname));
344 xxboot = "/boot/boot";
345 fd = open(xxboot, O_RDONLY);
347 err(1, "cannot open %s", xxboot);
349 if (alphacksum && st.st_size <= BBSIZE - 512) {
350 i = read(fd, bootarea + 512, st.st_size);
352 err(1, "read error %s", xxboot);
355 * Set the location and length so SRM can find the
358 p = (uint64_t *)bootarea;
359 p[60] = (st.st_size + secsize - 1) / secsize;
363 } else if ((!alphacksum) && st.st_size <= BBSIZE) {
364 i = read(fd, bootarea, st.st_size);
366 err(1, "read error %s", xxboot);
369 errx(1, "boot code %s is wrong size", xxboot);
377 struct gctl_req *grq;
379 struct disklabel *lp = &lab;
382 warnx("write to disk label supressed - label was as follows:");
383 display(stdout, NULL);
387 lp->d_magic = DISKMAGIC;
388 lp->d_magic2 = DISKMAGIC;
390 lp->d_checksum = dkcksum(lp);
393 for (i = 0; i < lab.d_npartitions; i++)
394 if (lab.d_partitions[i].p_size)
395 lab.d_partitions[i].p_offset += mbroffset;
396 bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * secsize,
399 /* Generate the bootblock checksum for the SRM console. */
400 for (p = (uint64_t *)bootarea, i = 0, sum = 0; i < 63; i++)
405 fd = open(specname, O_RDWR);
408 warn("cannot open file %s for writing label", specname);
411 grq = gctl_get_handle();
412 gctl_ro_param(grq, "verb", -1, "write label");
413 gctl_ro_param(grq, "class", -1, "BSD");
414 gctl_ro_param(grq, "geom", -1, dkname);
415 gctl_ro_param(grq, "label", 148+16*8,
416 bootarea + labeloffset + labelsoffset * secsize);
417 errstr = gctl_issue(grq);
418 if (errstr != NULL) {
425 grq = gctl_get_handle();
426 gctl_ro_param(grq, "verb", -1, "write bootcode");
427 gctl_ro_param(grq, "class", -1, "BSD");
428 gctl_ro_param(grq, "geom", -1, dkname);
429 gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
430 errstr = gctl_issue(grq);
431 if (errstr != NULL) {
439 if (write(fd, bootarea, bbsize) != bbsize) {
440 warn("write %s", specname);
450 get_file_parms(int f)
455 if (fstat(f, &sb) != 0)
456 err(4, "fstat failed");
457 i = sb.st_mode & S_IFMT;
458 if (i != S_IFREG && i != S_IFLNK)
459 errx(4, "%s is not a valid file or link", specname);
461 mediasize = sb.st_size;
465 * Fetch disklabel for disk.
466 * Use ioctl to get label unless -r flag is given.
473 struct gctl_req *grq;
476 f = open(specname, O_RDONLY);
481 else if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
482 (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
483 err(4, "cannot get disk geometry");
485 if (mediasize > (off_t)0xffffffff * secsize)
487 "disks with more than 2^32-1 sectors are not supported");
488 (void)lseek(f, (off_t)0, SEEK_SET);
489 if (read(f, bootarea, BBSIZE) != BBSIZE)
490 err(4, "%s read", specname);
492 error = bsd_disklabel_le_dec(
493 bootarea + (labeloffset + labelsoffset * secsize),
494 &lab, MAXPARTITIONS);
496 errx(1, "%s: no valid label found", specname);
498 grq = gctl_get_handle();
499 gctl_ro_param(grq, "verb", -1, "read mbroffset");
500 gctl_ro_param(grq, "class", -1, "BSD");
501 gctl_ro_param(grq, "geom", -1, dkname);
502 gctl_rw_param(grq, "mbroffset", sizeof(mbroffset), &mbroffset);
503 errstr = gctl_issue(grq);
504 if (errstr != NULL) {
509 mbroffset /= lab.d_secsize;
510 if (lab.d_partitions[RAW_PART].p_offset == mbroffset)
511 for (i = 0; i < lab.d_npartitions; i++)
512 if (lab.d_partitions[i].p_size)
513 lab.d_partitions[i].p_offset -= mbroffset;
519 display(FILE *f, const struct disklabel *lp)
522 const struct partition *pp;
527 fprintf(f, "# %s:\n", specname);
529 if (lp->d_type < DKMAXTYPES)
530 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
532 fprintf(f, "type: %u\n", lp->d_type);
533 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
535 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
537 fprintf(f, "flags:");
538 if (lp->d_flags & D_REMOVABLE)
539 fprintf(f, " removeable");
540 if (lp->d_flags & D_ECC)
542 if (lp->d_flags & D_BADSECT)
543 fprintf(f, " badsect");
545 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
546 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
547 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
548 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
549 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
550 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
551 fprintf(f, "rpm: %u\n", lp->d_rpm);
552 fprintf(f, "interleave: %u\n", lp->d_interleave);
553 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
554 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
555 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
556 (u_long)lp->d_headswitch);
557 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
558 (u_long)lp->d_trkseek);
559 fprintf(f, "drivedata: ");
560 for (i = NDDATA - 1; i >= 0; i--)
561 if (lp->d_drivedata[i])
565 for (j = 0; j <= i; j++)
566 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
569 fprintf(f, "%u partitions:\n", lp->d_npartitions);
571 "# size offset fstype [fsize bsize bps/cpg]\n");
572 pp = lp->d_partitions;
573 for (i = 0; i < lp->d_npartitions; i++, pp++) {
575 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
576 (u_long)pp->p_size, (u_long)pp->p_offset);
577 if (pp->p_fstype < FSMAXTYPES)
578 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
580 fprintf(f, "%8d", pp->p_fstype);
581 switch (pp->p_fstype) {
583 case FS_UNUSED: /* XXX */
584 fprintf(f, " %5lu %5lu %5.5s ",
586 (u_long)(pp->p_fsize * pp->p_frag), "");
590 fprintf(f, " %5lu %5lu %5u ",
592 (u_long)(pp->p_fsize * pp->p_frag),
597 fprintf(f, " %5lu %5lu %5d",
599 (u_long)(pp->p_fsize * pp->p_frag),
604 fprintf(f, "%20.20s", "");
608 fprintf(f, " # \"raw\" part, don't edit");
620 struct disklabel label;
623 if ((fd = mkstemp(tmpfil)) == -1 ||
624 (fp = fdopen(fd, "w")) == NULL) {
625 warnx("can't create %s", tmpfil);
633 fp = fopen(tmpfil, "r");
635 warnx("can't reopen %s for reading", tmpfil);
638 bzero((char *)&label, sizeof(label));
639 c = getasciilabel(fp, &label);
643 if (writelabel() == 0) {
644 (void) unlink(tmpfil);
648 printf("re-edit the label? [y]: ");
651 if (c != EOF && c != (int)'\n')
652 while (getchar() != (int)'\n')
657 (void) unlink(tmpfil);
668 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
669 while ((pid = fork()) < 0) {
670 if (errno == EPROCLIM) {
671 warnx("you have too many processes");
674 if (errno != EAGAIN) {
684 if ((ed = getenv("EDITOR")) == (char *)0)
686 execlp(ed, ed, tmpfil, (char *)0);
689 while ((xpid = wait(&locstat)) >= 0)
700 while (*cp != '\0' && isspace(*cp))
702 if (*cp == '\0' || *cp == '#')
712 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
714 if ((c = *cp) != '\0') {
723 * Read an ascii label in from fd f,
724 * in the same format as that put out by display(),
728 getasciilabel(FILE *f, struct disklabel *lp)
733 char *tp, line[BUFSIZ];
735 int lineno = 0, errors = 0;
738 makelabel("auto", lp);
739 bzero(&part_set, sizeof(part_set));
740 bzero(&part_size_type, sizeof(part_size_type));
741 bzero(&part_offset_type, sizeof(part_offset_type));
742 lp->d_bbsize = BBSIZE; /* XXX */
743 lp->d_sbsize = 0; /* XXX */
744 while (fgets(line, sizeof(line) - 1, f)) {
746 if ((cp = index(line,'\n')) != 0)
753 fprintf(stderr, "line %d: syntax error\n", lineno);
757 *tp++ = '\0', tp = skip(tp);
758 if (!strcmp(cp, "type")) {
762 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
763 if (*cpp && !strcmp(*cpp, tp)) {
764 lp->d_type = cpp - dktypenames;
767 if (cpp < &dktypenames[DKMAXTYPES])
769 v = strtoul(tp, NULL, 10);
771 fprintf(stderr, "line %d:%s %lu\n", lineno,
772 "Warning, unknown disk type", v);
776 if (!strcmp(cp, "flags")) {
777 for (v = 0; (cp = tp) && *cp != '\0';) {
779 if (!strcmp(cp, "removeable"))
781 else if (!strcmp(cp, "ecc"))
783 else if (!strcmp(cp, "badsect"))
787 "line %d: %s: bad flag\n",
795 if (!strcmp(cp, "drivedata")) {
796 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
797 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
802 if (sscanf(cp, "%lu partitions", &v) == 1) {
803 if (v == 0 || v > MAXPARTITIONS) {
805 "line %d: bad # of partitions\n", lineno);
806 lp->d_npartitions = MAXPARTITIONS;
809 lp->d_npartitions = v;
814 if (!strcmp(cp, "disk")) {
815 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
818 if (!strcmp(cp, "label")) {
819 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
822 if (!strcmp(cp, "bytes/sector")) {
823 v = strtoul(tp, NULL, 10);
824 if (v == 0 || (v % DEV_BSIZE) != 0) {
826 "line %d: %s: bad sector size\n",
833 if (!strcmp(cp, "sectors/track")) {
834 v = strtoul(tp, NULL, 10);
835 #if (ULONG_MAX != 0xffffffffUL)
836 if (v == 0 || v > 0xffffffff)
841 fprintf(stderr, "line %d: %s: bad %s\n",
848 if (!strcmp(cp, "sectors/cylinder")) {
849 v = strtoul(tp, NULL, 10);
851 fprintf(stderr, "line %d: %s: bad %s\n",
858 if (!strcmp(cp, "tracks/cylinder")) {
859 v = strtoul(tp, NULL, 10);
861 fprintf(stderr, "line %d: %s: bad %s\n",
868 if (!strcmp(cp, "cylinders")) {
869 v = strtoul(tp, NULL, 10);
871 fprintf(stderr, "line %d: %s: bad %s\n",
875 lp->d_ncylinders = v;
878 if (!strcmp(cp, "sectors/unit")) {
879 v = strtoul(tp, NULL, 10);
881 fprintf(stderr, "line %d: %s: bad %s\n",
885 lp->d_secperunit = v;
888 if (!strcmp(cp, "rpm")) {
889 v = strtoul(tp, NULL, 10);
890 if (v == 0 || v > USHRT_MAX) {
891 fprintf(stderr, "line %d: %s: bad %s\n",
898 if (!strcmp(cp, "interleave")) {
899 v = strtoul(tp, NULL, 10);
900 if (v == 0 || v > USHRT_MAX) {
901 fprintf(stderr, "line %d: %s: bad %s\n",
905 lp->d_interleave = v;
908 if (!strcmp(cp, "trackskew")) {
909 v = strtoul(tp, NULL, 10);
911 fprintf(stderr, "line %d: %s: bad %s\n",
918 if (!strcmp(cp, "cylinderskew")) {
919 v = strtoul(tp, NULL, 10);
921 fprintf(stderr, "line %d: %s: bad %s\n",
928 if (!strcmp(cp, "headswitch")) {
929 v = strtoul(tp, NULL, 10);
930 lp->d_headswitch = v;
933 if (!strcmp(cp, "track-to-track seek")) {
934 v = strtoul(tp, NULL, 10);
938 /* the ':' was removed above */
939 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
941 "line %d: %s: Unknown disklabel field\n", lineno,
947 /* Process a partition specification line. */
949 if (part >= lp->d_npartitions) {
951 "line %d: partition name out of range a-%c: %s\n",
952 lineno, 'a' + lp->d_npartitions - 1, cp);
958 if (getasciipartspec(tp, lp, part, lineno) != 0) {
963 errors += checklabel(lp);
964 return (errors == 0);
967 #define NXTNUM(n) do { \
969 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
972 cp = tp, tp = word(cp); \
973 (n) = strtoul(cp, NULL, 10); \
977 /* retain 1 character following number */
978 #define NXTWORD(w,n) do { \
980 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
984 cp = tp, tp = word(cp); \
985 (n) = strtoul(cp, &tmp, 10); \
986 if (tmp) (w) = *tmp; \
991 * Read a partition line into partition `part' in the specified disklabel.
992 * Return 0 on success, 1 on failure.
995 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
997 struct partition *pp;
1002 pp = &lp->d_partitions[part];
1006 NXTWORD(part_size_type[part],v);
1007 if (v == 0 && part_size_type[part] != '*') {
1009 "line %d: %s: bad partition size\n", lineno, cp);
1015 NXTWORD(part_offset_type[part],v);
1016 if (v == 0 && part_offset_type[part] != '*' &&
1017 part_offset_type[part] != '\0') {
1019 "line %d: %s: bad partition offset\n", lineno, cp);
1024 fprintf(stderr, "line %d: missing file system type\n", lineno);
1027 cp = tp, tp = word(cp);
1028 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1029 if (*cpp && !strcmp(*cpp, cp))
1032 pp->p_fstype = cpp - fstypenames;
1035 v = strtoul(cp, NULL, 10);
1038 if (v >= FSMAXTYPES) {
1040 "line %d: Warning, unknown file system type %s\n",
1047 switch (pp->p_fstype) {
1051 /* accept defaults for fsize/frag/cpg */
1053 NXTNUM(pp->p_fsize);
1054 if (pp->p_fsize == 0)
1057 pp->p_frag = v / pp->p_fsize;
1061 /* else default to 0's */
1070 * Check disklabel for errors and fill in
1071 * derived fields according to supplied values.
1074 checklabel(struct disklabel *lp)
1076 struct partition *pp;
1079 u_long base_offset, needed, total_size, total_percent, current_offset;
1081 int seen_default_offset;
1084 struct partition *pp2;
1091 if (lp->d_secsize == 0) {
1092 fprintf(stderr, "sector size 0\n");
1095 if (lp->d_nsectors == 0) {
1096 fprintf(stderr, "sectors/track 0\n");
1099 if (lp->d_ntracks == 0) {
1100 fprintf(stderr, "tracks/cylinder 0\n");
1103 if (lp->d_ncylinders == 0) {
1104 fprintf(stderr, "cylinders/unit 0\n");
1108 warnx("revolutions/minute 0");
1109 if (lp->d_secpercyl == 0)
1110 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1111 if (lp->d_secperunit == 0)
1112 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1113 if (lp->d_bbsize == 0) {
1114 fprintf(stderr, "boot block size 0\n");
1116 } else if (lp->d_bbsize % lp->d_secsize)
1117 warnx("boot block size %% sector-size != 0");
1118 if (lp->d_npartitions > MAXPARTITIONS)
1119 warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
1120 (u_long)lp->d_npartitions, MAXPARTITIONS);
1122 struct disklabel *vl;
1124 vl = getvirginlabel();
1125 lp->d_secsize = vl->d_secsize;
1126 lp->d_nsectors = vl->d_nsectors;
1127 lp->d_ntracks = vl->d_ntracks;
1128 lp->d_ncylinders = vl->d_ncylinders;
1129 lp->d_rpm = vl->d_rpm;
1130 lp->d_interleave = vl->d_interleave;
1131 lp->d_secpercyl = vl->d_secpercyl;
1132 lp->d_secperunit = vl->d_secperunit;
1133 lp->d_bbsize = vl->d_bbsize;
1134 lp->d_npartitions = vl->d_npartitions;
1138 /* first allocate space to the partitions, then offsets */
1139 total_size = 0; /* in sectors */
1140 total_percent = 0; /* in percent */
1142 /* find all fixed partitions */
1143 for (i = 0; i < lp->d_npartitions; i++) {
1144 pp = &lp->d_partitions[i];
1146 if (part_size_type[i] == '*') {
1147 if (i == RAW_PART) {
1148 pp->p_size = lp->d_secperunit;
1151 warnx("Too many '*' partitions (%c and %c)",
1152 hog_part + 'a',i + 'a');
1160 switch (part_size_type[i]) {
1162 total_percent += size;
1183 warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
1184 part_size_type[i], i + 'a');
1187 /* don't count %'s yet */
1188 if (part_size_type[i] != '%') {
1190 * for all not in sectors, convert to
1193 if (part_size_type[i] != '\0') {
1194 if (size % lp->d_secsize != 0)
1195 warnx("partition %c not an integer number of sectors",
1197 size /= lp->d_secsize;
1200 /* else already in sectors */
1208 /* Find out the total free space, excluding the boot block area. */
1209 base_offset = BBSIZE / secsize;
1211 for (i = 0; i < lp->d_npartitions; i++) {
1212 pp = &lp->d_partitions[i];
1213 if (!part_set[i] || i == RAW_PART ||
1214 part_size_type[i] == '%' || part_size_type[i] == '*')
1216 if (pp->p_offset > base_offset)
1217 free_space += pp->p_offset - base_offset;
1218 if (pp->p_offset + pp->p_size > base_offset)
1219 base_offset = pp->p_offset + pp->p_size;
1221 if (base_offset < lp->d_secperunit)
1222 free_space += lp->d_secperunit - base_offset;
1224 /* handle % partitions - note %'s don't need to add up to 100! */
1225 if (total_percent != 0) {
1226 if (total_percent > 100) {
1227 fprintf(stderr,"total percentage %lu is greater than 100\n",
1232 if (free_space > 0) {
1233 for (i = 0; i < lp->d_npartitions; i++) {
1234 pp = &lp->d_partitions[i];
1235 if (part_set[i] && part_size_type[i] == '%') {
1236 /* careful of overflows! and integer roundoff */
1237 pp->p_size = ((double)pp->p_size/100) * free_space;
1238 total_size += pp->p_size;
1240 /* FIX we can lose a sector or so due to roundoff per
1241 partition. A more complex algorithm could avoid that */
1246 "%ld sectors available to give to '*' and '%%' partitions\n",
1249 /* fix? set all % partitions to size 0? */
1252 /* give anything remaining to the hog partition */
1253 if (hog_part != -1) {
1255 * Find the range of offsets usable by '*' partitions around
1256 * the hog partition and how much space they need.
1259 base_offset = BBSIZE / secsize;
1260 for (i = hog_part - 1; i >= 0; i--) {
1261 pp = &lp->d_partitions[i];
1262 if (!part_set[i] || i == RAW_PART)
1264 if (part_offset_type[i] == '*') {
1265 needed += pp->p_size;
1268 base_offset = pp->p_offset + pp->p_size;
1271 current_offset = lp->d_secperunit;
1272 for (i = lp->d_npartitions - 1; i > hog_part; i--) {
1273 pp = &lp->d_partitions[i];
1274 if (!part_set[i] || i == RAW_PART)
1276 if (part_offset_type[i] == '*') {
1277 needed += pp->p_size;
1280 current_offset = pp->p_offset;
1283 if (current_offset - base_offset <= needed) {
1284 fprintf(stderr, "Cannot find space for partition %c\n",
1287 "Need more than %lu sectors between %lu and %lu\n",
1288 needed, base_offset, current_offset);
1290 lp->d_partitions[hog_part].p_size = 0;
1292 lp->d_partitions[hog_part].p_size = current_offset -
1293 base_offset - needed;
1294 total_size += lp->d_partitions[hog_part].p_size;
1298 /* Now set the offsets for each partition */
1299 current_offset = BBSIZE / secsize; /* in sectors */
1300 seen_default_offset = 0;
1301 for (i = 0; i < lp->d_npartitions; i++) {
1303 pp = &lp->d_partitions[i];
1305 if (part_offset_type[i] == '*') {
1306 if (i == RAW_PART) {
1309 pp->p_offset = current_offset;
1310 seen_default_offset = 1;
1313 /* allow them to be out of order for old-style tables */
1314 if (pp->p_offset < current_offset &&
1315 seen_default_offset && i != RAW_PART &&
1316 pp->p_fstype != FS_VINUM) {
1318 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1319 (long)pp->p_offset,i+'a',current_offset);
1321 "Labels with any *'s for offset must be in ascending order by sector\n");
1323 } else if (pp->p_offset != current_offset &&
1324 i != RAW_PART && seen_default_offset) {
1326 * this may give unneeded warnings if
1327 * partitions are out-of-order
1330 "Offset %ld for partition %c doesn't match expected value %ld",
1331 (long)pp->p_offset, i + 'a', current_offset);
1335 current_offset = pp->p_offset + pp->p_size;
1339 for (i = 0; i < lp->d_npartitions; i++) {
1341 pp = &lp->d_partitions[i];
1342 if (pp->p_size == 0 && pp->p_offset != 0)
1343 warnx("partition %c: size 0, but offset %lu",
1344 part, (u_long)pp->p_offset);
1346 if (pp->p_size % lp->d_secpercyl)
1347 warnx("partition %c: size %% cylinder-size != 0",
1349 if (pp->p_offset % lp->d_secpercyl)
1350 warnx("partition %c: offset %% cylinder-size != 0",
1353 if (pp->p_offset > lp->d_secperunit) {
1355 "partition %c: offset past end of unit\n", part);
1358 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1360 "partition %c: partition extends past end of unit\n",
1364 if (i == RAW_PART) {
1365 if (pp->p_fstype != FS_UNUSED)
1366 warnx("partition %c is not marked as unused!",part);
1367 if (pp->p_offset != 0)
1368 warnx("partition %c doesn't start at 0!",part);
1369 if (pp->p_size != lp->d_secperunit)
1370 warnx("partition %c doesn't cover the whole unit!",part);
1372 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1373 (pp->p_size != lp->d_secperunit)) {
1374 warnx("An incorrect partition %c may cause problems for "
1375 "standard system utilities",part);
1379 /* check for overlaps */
1380 /* this will check for all possible overlaps once and only once */
1381 for (j = 0; j < i; j++) {
1382 pp2 = &lp->d_partitions[j];
1383 if (j != RAW_PART && i != RAW_PART &&
1384 pp->p_fstype != FS_VINUM &&
1385 pp2->p_fstype != FS_VINUM &&
1386 part_set[i] && part_set[j]) {
1387 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1388 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1389 pp2->p_offset >= pp->p_offset)) {
1390 fprintf(stderr,"partitions %c and %c overlap!\n",
1397 for (; i < lp->d_npartitions; i++) {
1399 pp = &lp->d_partitions[i];
1400 if (pp->p_size || pp->p_offset)
1401 warnx("unused partition %c: size %d offset %lu",
1402 'a' + i, pp->p_size, (u_long)pp->p_offset);
1408 * When operating on a "virgin" disk, try getting an initial label
1409 * from the associated device driver. This might work for all device
1410 * drivers that are able to fetch some initial device parameters
1411 * without even having access to a (BSD) disklabel, like SCSI disks,
1412 * most IDE drives, or vn devices.
1414 * The device name must be given in its "canonical" form.
1416 static struct disklabel *
1417 getvirginlabel(void)
1419 static struct disklabel loclab;
1420 struct partition *dp;
1424 if ((f = open(specname, O_RDONLY)) == -1) {
1425 warn("cannot open %s", specname);
1431 else if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
1432 (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
1436 memset(&loclab, 0, sizeof loclab);
1437 loclab.d_magic = DISKMAGIC;
1438 loclab.d_magic2 = DISKMAGIC;
1439 loclab.d_secsize = secsize;
1440 loclab.d_secperunit = mediasize / secsize;
1443 * Nobody in these enligthened days uses the CHS geometry for
1444 * anything, but nontheless try to get it right. If we fail
1445 * to get any good ideas from the device, construct something
1446 * which is IBM-PC friendly.
1448 if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1449 loclab.d_nsectors = u;
1451 loclab.d_nsectors = 63;
1452 if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1453 loclab.d_ntracks = u;
1454 else if (loclab.d_secperunit <= 63*1*1024)
1455 loclab.d_ntracks = 1;
1456 else if (loclab.d_secperunit <= 63*16*1024)
1457 loclab.d_ntracks = 16;
1459 loclab.d_ntracks = 255;
1460 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1461 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1462 loclab.d_npartitions = DEFPARTITIONS;
1464 /* Various (unneeded) compat stuff */
1465 loclab.d_rpm = 3600;
1466 loclab.d_bbsize = BBSIZE;
1467 loclab.d_interleave = 1;
1468 strncpy(loclab.d_typename, "amnesiac",
1469 sizeof(loclab.d_typename));
1471 dp = &loclab.d_partitions[RAW_PART];
1472 dp->p_size = loclab.d_secperunit;
1473 loclab.d_checksum = dkcksum(&loclab);
1483 "%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",
1484 "usage: bsdlabel disk",
1485 "\t\t(to read label)",
1486 " bsdlabel -w [-n] [-m machine] disk [type]",
1487 "\t\t(to write label with existing boot program)",
1488 " bsdlabel -e [-n] [-m machine] disk",
1489 "\t\t(to edit label)",
1490 " bsdlabel -R [-n] [-m machine] disk protofile",
1491 "\t\t(to restore label with existing boot program)",
1492 " bsdlabel -B [-b boot] [-m machine] disk",
1493 "\t\t(to install boot program with existing on-disk label)",
1494 " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1495 "\t\t(to write label and install boot program)",
1496 " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
1497 "\t\t(to restore label and install boot program)"