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 geom_bsd_available(void);
84 static int writelabel(void);
85 static int readlabel(int flag);
86 static void display(FILE *, const struct disklabel *);
87 static int edit(void);
88 static int editit(void);
89 static void fixlabel(struct disklabel *);
90 static char *skip(char *);
91 static char *word(char *);
92 static int getasciilabel(FILE *, struct disklabel *);
93 static int getasciipartspec(char *, struct disklabel *, int, int);
94 static int checklabel(struct disklabel *);
95 static void usage(void);
96 static struct disklabel *getvirginlabel(void);
98 #define DEFEDITOR _PATH_VI
99 #define DEFPARTITIONS 8
101 static char *specname;
103 static char tmpfil[] = PATH_TMPFILE;
105 static struct disklabel lab;
106 static u_char bootarea[BBSIZE];
107 static off_t mediasize;
108 static u_int secsize;
109 static char blank[] = "";
110 static char unknown[] = "unknown";
112 #define MAX_PART ('z')
113 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
114 static char part_size_type[MAX_NUM_PARTS];
115 static char part_offset_type[MAX_NUM_PARTS];
116 static int part_set[MAX_NUM_PARTS];
118 static int installboot; /* non-zero if we should install a boot program */
119 static int allfields; /* present all fields in edit */
120 static char const *xxboot; /* primary boot */
122 static uint32_t lba_offset;
124 #define LABELSECTOR -1
127 #define LABELOFFSET -1
129 static int labelsoffset = LABELSECTOR;
130 static int labeloffset = LABELOFFSET;
131 static int bbsize = BBSIZE;
132 static int alphacksum =
133 #if defined(__alpha__)
140 UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
144 static int disable_write; /* set to disable writing to disk label */
145 static int is_file; /* work on a file (abs. pathname), "-f" opt. */
148 main(int argc, char *argv[])
157 while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1)
172 if (!strcmp(optarg, "i386") ||
173 !strcmp(optarg, "amd64") ||
174 !strcmp(optarg, "ia64") ||
175 !strcmp(optarg, "pc98")) {
180 } else if (!strcmp(optarg, "alpha")) {
186 errx(1, "Unsupported architecture");
204 * We accept and ignode -r for compatibility with
205 * historically disklabel usage.
222 if (labelsoffset < 0 || labeloffset < 0)
223 errx(1, "a -m <architecture> option must be specified");
225 /* Figure out the names of the thing we're working on */
229 specname = g_device_path(argv[0]);
230 if (specname == NULL) {
231 warn("unable to get correct path for %s", argv[0]);
234 fd = open(specname, O_RDONLY);
236 warn("error opening %s", specname);
239 pname = g_providername(fd);
241 warn("error getting providername for %s", specname);
248 if (installboot && op == UNSPEC)
250 else if (op == UNSPEC)
270 display(stdout, NULL);
271 error = checklabel(NULL);
277 if (!(t = fopen(argv[1], "r")))
278 err(4, "fopen %s", argv[1]);
280 if (!getasciilabel(t, &lab))
282 error = writelabel();
293 makelabel(name, &lab);
295 if (checklabel(NULL) == 0)
296 error = writelabel();
304 makelabel(argv[1], &lab);
305 if (checklabel(NULL) == 0)
306 error = writelabel();
313 fixlabel(struct disklabel *lp)
315 struct partition *dp;
318 for (i = 0; i < lp->d_npartitions; i++) {
321 if (lp->d_partitions[i].p_size)
325 dp = &lp->d_partitions[0];
326 dp->p_offset = BBSIZE / secsize;
327 dp->p_size = lp->d_secperunit - dp->p_offset;
331 * Construct a prototype disklabel from /etc/disktab.
334 makelabel(const char *type, struct disklabel *lp)
336 struct disklabel *dp;
338 if (strcmp(type, "auto") == 0)
339 dp = getvirginlabel();
341 dp = getdiskbyname(type);
343 errx(1, "%s: unknown disk type", type);
345 bzero(lp->d_packname, sizeof(lp->d_packname));
356 xxboot = "/boot/boot";
357 fd = open(xxboot, O_RDONLY);
359 err(1, "cannot open %s", xxboot);
361 if (alphacksum && st.st_size <= BBSIZE - 512) {
362 if (read(fd, bootarea + 512, st.st_size) != st.st_size)
363 err(1, "read error %s", xxboot);
366 * Set the location and length so SRM can find the
369 p = (uint64_t *)bootarea;
370 p[60] = (st.st_size + secsize - 1) / secsize;
374 } else if ((!alphacksum) && st.st_size <= BBSIZE) {
375 if (read(fd, bootarea, st.st_size) != st.st_size)
376 err(1, "read error %s", xxboot);
379 errx(1, "boot code %s is wrong size", xxboot);
383 geom_bsd_available(void)
385 struct gclass *class;
389 error = geom_gettree(&mesh);
391 errc(1, error, "Cannot get GEOM tree");
393 LIST_FOREACH(class, &mesh.lg_class, lg_class) {
394 if (strcmp(class->lg_name, "BSD") == 0) {
395 geom_deletetree(&mesh);
400 geom_deletetree(&mesh);
410 struct gctl_req *grq;
412 struct disklabel *lp = &lab;
415 warnx("write to disk label supressed - label was as follows:");
416 display(stdout, NULL);
420 lp->d_magic = DISKMAGIC;
421 lp->d_magic2 = DISKMAGIC;
423 lp->d_checksum = dkcksum(lp);
426 for (i = 0; i < lab.d_npartitions; i++)
427 if (lab.d_partitions[i].p_size)
428 lab.d_partitions[i].p_offset += lba_offset;
429 bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * secsize,
432 /* Generate the bootblock checksum for the SRM console. */
433 for (p = (uint64_t *)bootarea, i = 0, sum = 0; i < 63; i++)
438 fd = open(specname, O_RDWR);
441 warn("cannot open file %s for writing label", specname);
446 /* Give up if GEOM_BSD is not available. */
447 if (geom_bsd_available() == 0) {
448 warnc(serrno, "%s", specname);
452 grq = gctl_get_handle();
453 gctl_ro_param(grq, "verb", -1, "write label");
454 gctl_ro_param(grq, "class", -1, "BSD");
455 gctl_ro_param(grq, "geom", -1, pname);
456 gctl_ro_param(grq, "label", 148+16*8,
457 bootarea + labeloffset + labelsoffset * secsize);
458 errstr = gctl_issue(grq);
459 if (errstr != NULL) {
466 grq = gctl_get_handle();
467 gctl_ro_param(grq, "verb", -1, "write bootcode");
468 gctl_ro_param(grq, "class", -1, "BSD");
469 gctl_ro_param(grq, "geom", -1, pname);
470 gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
471 errstr = gctl_issue(grq);
472 if (errstr != NULL) {
480 if (write(fd, bootarea, bbsize) != bbsize) {
481 warn("write %s", specname);
491 get_file_parms(int f)
496 if (fstat(f, &sb) != 0)
497 err(4, "fstat failed");
498 i = sb.st_mode & S_IFMT;
499 if (i != S_IFREG && i != S_IFLNK)
500 errx(4, "%s is not a valid file or link", specname);
502 mediasize = sb.st_size;
506 * Fetch disklabel for disk.
516 f = open(specname, O_RDONLY);
522 mediasize = g_mediasize(f);
523 secsize = g_sectorsize(f);
524 if (secsize < 0 || mediasize < 0)
525 err(4, "cannot get disk geometry");
527 if (mediasize > (off_t)0xffffffff * secsize)
529 "disks with more than 2^32-1 sectors are not supported");
530 (void)lseek(f, (off_t)0, SEEK_SET);
531 nbytes = read(f, bootarea, BBSIZE);
533 err(4, "%s read", specname);
534 if (nbytes != BBSIZE)
535 errx(4, "couldn't read %d bytes from %s", BBSIZE, specname);
537 error = bsd_disklabel_le_dec(
538 bootarea + (labeloffset + labelsoffset * secsize),
539 &lab, MAXPARTITIONS);
541 errx(1, "%s: no valid label found", specname);
547 * Compensate for absolute block addressing by finding the
548 * smallest partition offset and if the offset of the 'c'
549 * partition is equal to that, subtract it from all offsets.
552 for (i = 0; i < lab.d_npartitions; i++) {
553 if (lab.d_partitions[i].p_size)
554 lba = MIN(lba, lab.d_partitions[i].p_offset);
556 if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
557 for (i = 0; i < lab.d_npartitions; i++) {
558 if (lab.d_partitions[i].p_size)
559 lab.d_partitions[i].p_offset -= lba;
562 * Save the offset so that we can write the label
563 * back with absolute block addresses.
572 display(FILE *f, const struct disklabel *lp)
575 const struct partition *pp;
580 fprintf(f, "# %s:\n", specname);
582 if (lp->d_type < DKMAXTYPES)
583 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
585 fprintf(f, "type: %u\n", lp->d_type);
586 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
588 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
590 fprintf(f, "flags:");
591 if (lp->d_flags & D_REMOVABLE)
592 fprintf(f, " removeable");
593 if (lp->d_flags & D_ECC)
595 if (lp->d_flags & D_BADSECT)
596 fprintf(f, " badsect");
598 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
599 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
600 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
601 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
602 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
603 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
604 fprintf(f, "rpm: %u\n", lp->d_rpm);
605 fprintf(f, "interleave: %u\n", lp->d_interleave);
606 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
607 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
608 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
609 (u_long)lp->d_headswitch);
610 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
611 (u_long)lp->d_trkseek);
612 fprintf(f, "drivedata: ");
613 for (i = NDDATA - 1; i >= 0; i--)
614 if (lp->d_drivedata[i])
618 for (j = 0; j <= i; j++)
619 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
622 fprintf(f, "%u partitions:\n", lp->d_npartitions);
624 "# size offset fstype [fsize bsize bps/cpg]\n");
625 pp = lp->d_partitions;
626 for (i = 0; i < lp->d_npartitions; i++, pp++) {
628 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
629 (u_long)pp->p_size, (u_long)pp->p_offset);
630 if (pp->p_fstype < FSMAXTYPES)
631 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
633 fprintf(f, "%8d", pp->p_fstype);
634 switch (pp->p_fstype) {
636 case FS_UNUSED: /* XXX */
637 fprintf(f, " %5lu %5lu %5.5s ",
639 (u_long)(pp->p_fsize * pp->p_frag), "");
643 fprintf(f, " %5lu %5lu %5u ",
645 (u_long)(pp->p_fsize * pp->p_frag),
650 fprintf(f, " %5lu %5lu %5d",
652 (u_long)(pp->p_fsize * pp->p_frag),
657 fprintf(f, "%20.20s", "");
661 fprintf(f, " # \"raw\" part, don't edit");
673 struct disklabel label;
676 if ((fd = mkstemp(tmpfil)) == -1 ||
677 (fp = fdopen(fd, "w")) == NULL) {
678 warnx("can't create %s", tmpfil);
686 fp = fopen(tmpfil, "r");
688 warnx("can't reopen %s for reading", tmpfil);
691 bzero((char *)&label, sizeof(label));
692 c = getasciilabel(fp, &label);
696 if (writelabel() == 0) {
697 (void) unlink(tmpfil);
701 printf("re-edit the label? [y]: ");
704 if (c != EOF && c != (int)'\n')
705 while (getchar() != (int)'\n')
710 (void) unlink(tmpfil);
721 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
722 while ((pid = fork()) < 0) {
723 if (errno == EPROCLIM) {
724 warnx("you have too many processes");
727 if (errno != EAGAIN) {
737 if ((ed = getenv("EDITOR")) == (char *)0)
739 execlp(ed, ed, tmpfil, (char *)0);
742 while ((xpid = wait(&locstat)) >= 0)
753 while (*cp != '\0' && isspace(*cp))
755 if (*cp == '\0' || *cp == '#')
765 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
767 if ((c = *cp) != '\0') {
776 * Read an ascii label in from fd f,
777 * in the same format as that put out by display(),
781 getasciilabel(FILE *f, struct disklabel *lp)
786 char *tp, line[BUFSIZ];
788 int lineno = 0, errors = 0;
791 makelabel("auto", lp);
792 bzero(&part_set, sizeof(part_set));
793 bzero(&part_size_type, sizeof(part_size_type));
794 bzero(&part_offset_type, sizeof(part_offset_type));
795 lp->d_bbsize = BBSIZE; /* XXX */
796 lp->d_sbsize = 0; /* XXX */
797 while (fgets(line, sizeof(line) - 1, f)) {
799 if ((cp = index(line,'\n')) != 0)
806 fprintf(stderr, "line %d: syntax error\n", lineno);
810 *tp++ = '\0', tp = skip(tp);
811 if (!strcmp(cp, "type")) {
815 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
816 if (*cpp && !strcmp(*cpp, tp)) {
817 lp->d_type = cpp - dktypenames;
820 if (cpp < &dktypenames[DKMAXTYPES])
823 v = strtoul(tp, &endp, 10);
824 if (errno != 0 || *endp != '\0')
827 fprintf(stderr, "line %d:%s %lu\n", lineno,
828 "Warning, unknown disk type", v);
833 if (!strcmp(cp, "flags")) {
834 for (v = 0; (cp = tp) && *cp != '\0';) {
836 if (!strcmp(cp, "removeable"))
838 else if (!strcmp(cp, "ecc"))
840 else if (!strcmp(cp, "badsect"))
844 "line %d: %s: bad flag\n",
852 if (!strcmp(cp, "drivedata")) {
853 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
854 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
859 if (sscanf(cp, "%lu partitions", &v) == 1) {
860 if (v == 0 || v > MAXPARTITIONS) {
862 "line %d: bad # of partitions\n", lineno);
863 lp->d_npartitions = MAXPARTITIONS;
866 lp->d_npartitions = v;
871 if (!strcmp(cp, "disk")) {
872 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
875 if (!strcmp(cp, "label")) {
876 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
879 if (!strcmp(cp, "bytes/sector")) {
880 v = strtoul(tp, NULL, 10);
881 if (v == 0 || (v % DEV_BSIZE) != 0) {
883 "line %d: %s: bad sector size\n",
890 if (!strcmp(cp, "sectors/track")) {
891 v = strtoul(tp, NULL, 10);
892 #if (ULONG_MAX != 0xffffffffUL)
893 if (v == 0 || v > 0xffffffff)
898 fprintf(stderr, "line %d: %s: bad %s\n",
905 if (!strcmp(cp, "sectors/cylinder")) {
906 v = strtoul(tp, NULL, 10);
908 fprintf(stderr, "line %d: %s: bad %s\n",
915 if (!strcmp(cp, "tracks/cylinder")) {
916 v = strtoul(tp, NULL, 10);
918 fprintf(stderr, "line %d: %s: bad %s\n",
925 if (!strcmp(cp, "cylinders")) {
926 v = strtoul(tp, NULL, 10);
928 fprintf(stderr, "line %d: %s: bad %s\n",
932 lp->d_ncylinders = v;
935 if (!strcmp(cp, "sectors/unit")) {
936 v = strtoul(tp, NULL, 10);
938 fprintf(stderr, "line %d: %s: bad %s\n",
942 lp->d_secperunit = v;
945 if (!strcmp(cp, "rpm")) {
946 v = strtoul(tp, NULL, 10);
947 if (v == 0 || v > USHRT_MAX) {
948 fprintf(stderr, "line %d: %s: bad %s\n",
955 if (!strcmp(cp, "interleave")) {
956 v = strtoul(tp, NULL, 10);
957 if (v == 0 || v > USHRT_MAX) {
958 fprintf(stderr, "line %d: %s: bad %s\n",
962 lp->d_interleave = v;
965 if (!strcmp(cp, "trackskew")) {
966 v = strtoul(tp, NULL, 10);
968 fprintf(stderr, "line %d: %s: bad %s\n",
975 if (!strcmp(cp, "cylinderskew")) {
976 v = strtoul(tp, NULL, 10);
978 fprintf(stderr, "line %d: %s: bad %s\n",
985 if (!strcmp(cp, "headswitch")) {
986 v = strtoul(tp, NULL, 10);
987 lp->d_headswitch = v;
990 if (!strcmp(cp, "track-to-track seek")) {
991 v = strtoul(tp, NULL, 10);
995 /* the ':' was removed above */
996 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
998 "line %d: %s: Unknown disklabel field\n", lineno,
1004 /* Process a partition specification line. */
1006 if (part >= lp->d_npartitions) {
1008 "line %d: partition name out of range a-%c: %s\n",
1009 lineno, 'a' + lp->d_npartitions - 1, cp);
1015 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1020 errors += checklabel(lp);
1021 return (errors == 0);
1024 #define NXTNUM(n) do { \
1026 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1029 cp = tp, tp = word(cp); \
1030 (n) = strtoul(cp, NULL, 10); \
1034 /* retain 1 character following number */
1035 #define NXTWORD(w,n) do { \
1037 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1041 cp = tp, tp = word(cp); \
1042 (n) = strtoul(cp, &tmp, 10); \
1043 if (tmp) (w) = *tmp; \
1048 * Read a partition line into partition `part' in the specified disklabel.
1049 * Return 0 on success, 1 on failure.
1052 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1054 struct partition *pp;
1059 pp = &lp->d_partitions[part];
1063 NXTWORD(part_size_type[part],v);
1064 if (v == 0 && part_size_type[part] != '*') {
1066 "line %d: %s: bad partition size\n", lineno, cp);
1072 NXTWORD(part_offset_type[part],v);
1073 if (v == 0 && part_offset_type[part] != '*' &&
1074 part_offset_type[part] != '\0') {
1076 "line %d: %s: bad partition offset\n", lineno, cp);
1081 fprintf(stderr, "line %d: missing file system type\n", lineno);
1084 cp = tp, tp = word(cp);
1085 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1086 if (*cpp && !strcmp(*cpp, cp))
1089 pp->p_fstype = cpp - fstypenames;
1093 v = strtoul(cp, &endp, 10);
1094 if (errno != 0 || *endp != '\0')
1098 if (v >= FSMAXTYPES) {
1100 "line %d: Warning, unknown file system type %s\n",
1107 switch (pp->p_fstype) {
1111 /* accept defaults for fsize/frag/cpg */
1113 NXTNUM(pp->p_fsize);
1114 if (pp->p_fsize == 0)
1117 pp->p_frag = v / pp->p_fsize;
1121 /* else default to 0's */
1130 * Check disklabel for errors and fill in
1131 * derived fields according to supplied values.
1134 checklabel(struct disklabel *lp)
1136 struct partition *pp;
1139 u_long base_offset, needed, total_size, total_percent, current_offset;
1141 int seen_default_offset;
1144 struct partition *pp2;
1151 if (lp->d_secsize == 0) {
1152 fprintf(stderr, "sector size 0\n");
1155 if (lp->d_nsectors == 0) {
1156 fprintf(stderr, "sectors/track 0\n");
1159 if (lp->d_ntracks == 0) {
1160 fprintf(stderr, "tracks/cylinder 0\n");
1163 if (lp->d_ncylinders == 0) {
1164 fprintf(stderr, "cylinders/unit 0\n");
1168 warnx("revolutions/minute 0");
1169 if (lp->d_secpercyl == 0)
1170 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1171 if (lp->d_secperunit == 0)
1172 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1173 if (lp->d_bbsize == 0) {
1174 fprintf(stderr, "boot block size 0\n");
1176 } else if (lp->d_bbsize % lp->d_secsize)
1177 warnx("boot block size %% sector-size != 0");
1178 if (lp->d_npartitions > MAXPARTITIONS)
1179 warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
1180 (u_long)lp->d_npartitions, MAXPARTITIONS);
1182 struct disklabel *vl;
1184 vl = getvirginlabel();
1185 lp->d_secsize = vl->d_secsize;
1186 lp->d_nsectors = vl->d_nsectors;
1187 lp->d_ntracks = vl->d_ntracks;
1188 lp->d_ncylinders = vl->d_ncylinders;
1189 lp->d_rpm = vl->d_rpm;
1190 lp->d_interleave = vl->d_interleave;
1191 lp->d_secpercyl = vl->d_secpercyl;
1192 lp->d_secperunit = vl->d_secperunit;
1193 lp->d_bbsize = vl->d_bbsize;
1194 lp->d_npartitions = vl->d_npartitions;
1198 /* first allocate space to the partitions, then offsets */
1199 total_size = 0; /* in sectors */
1200 total_percent = 0; /* in percent */
1202 /* find all fixed partitions */
1203 for (i = 0; i < lp->d_npartitions; i++) {
1204 pp = &lp->d_partitions[i];
1206 if (part_size_type[i] == '*') {
1207 if (i == RAW_PART) {
1208 pp->p_size = lp->d_secperunit;
1211 warnx("Too many '*' partitions (%c and %c)",
1212 hog_part + 'a',i + 'a');
1220 switch (part_size_type[i]) {
1222 total_percent += size;
1243 warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
1244 part_size_type[i], i + 'a');
1247 /* don't count %'s yet */
1248 if (part_size_type[i] != '%') {
1250 * for all not in sectors, convert to
1253 if (part_size_type[i] != '\0') {
1254 if (size % lp->d_secsize != 0)
1255 warnx("partition %c not an integer number of sectors",
1257 size /= lp->d_secsize;
1260 /* else already in sectors */
1268 /* Find out the total free space, excluding the boot block area. */
1269 base_offset = BBSIZE / secsize;
1271 for (i = 0; i < lp->d_npartitions; i++) {
1272 pp = &lp->d_partitions[i];
1273 if (!part_set[i] || i == RAW_PART ||
1274 part_size_type[i] == '%' || part_size_type[i] == '*')
1276 if (pp->p_offset > base_offset)
1277 free_space += pp->p_offset - base_offset;
1278 if (pp->p_offset + pp->p_size > base_offset)
1279 base_offset = pp->p_offset + pp->p_size;
1281 if (base_offset < lp->d_secperunit)
1282 free_space += lp->d_secperunit - base_offset;
1284 /* handle % partitions - note %'s don't need to add up to 100! */
1285 if (total_percent != 0) {
1286 if (total_percent > 100) {
1287 fprintf(stderr,"total percentage %lu is greater than 100\n",
1292 if (free_space > 0) {
1293 for (i = 0; i < lp->d_npartitions; i++) {
1294 pp = &lp->d_partitions[i];
1295 if (part_set[i] && part_size_type[i] == '%') {
1296 /* careful of overflows! and integer roundoff */
1297 pp->p_size = ((double)pp->p_size/100) * free_space;
1298 total_size += pp->p_size;
1300 /* FIX we can lose a sector or so due to roundoff per
1301 partition. A more complex algorithm could avoid that */
1306 "%ld sectors available to give to '*' and '%%' partitions\n",
1309 /* fix? set all % partitions to size 0? */
1312 /* give anything remaining to the hog partition */
1313 if (hog_part != -1) {
1315 * Find the range of offsets usable by '*' partitions around
1316 * the hog partition and how much space they need.
1319 base_offset = BBSIZE / secsize;
1320 for (i = hog_part - 1; i >= 0; i--) {
1321 pp = &lp->d_partitions[i];
1322 if (!part_set[i] || i == RAW_PART)
1324 if (part_offset_type[i] == '*') {
1325 needed += pp->p_size;
1328 base_offset = pp->p_offset + pp->p_size;
1331 current_offset = lp->d_secperunit;
1332 for (i = lp->d_npartitions - 1; i > hog_part; i--) {
1333 pp = &lp->d_partitions[i];
1334 if (!part_set[i] || i == RAW_PART)
1336 if (part_offset_type[i] == '*') {
1337 needed += pp->p_size;
1340 current_offset = pp->p_offset;
1343 if (current_offset - base_offset <= needed) {
1344 fprintf(stderr, "Cannot find space for partition %c\n",
1347 "Need more than %lu sectors between %lu and %lu\n",
1348 needed, base_offset, current_offset);
1350 lp->d_partitions[hog_part].p_size = 0;
1352 lp->d_partitions[hog_part].p_size = current_offset -
1353 base_offset - needed;
1354 total_size += lp->d_partitions[hog_part].p_size;
1358 /* Now set the offsets for each partition */
1359 current_offset = BBSIZE / secsize; /* in sectors */
1360 seen_default_offset = 0;
1361 for (i = 0; i < lp->d_npartitions; i++) {
1363 pp = &lp->d_partitions[i];
1365 if (part_offset_type[i] == '*') {
1366 if (i == RAW_PART) {
1369 pp->p_offset = current_offset;
1370 seen_default_offset = 1;
1373 /* allow them to be out of order for old-style tables */
1374 if (pp->p_offset < current_offset &&
1375 seen_default_offset && i != RAW_PART &&
1376 pp->p_fstype != FS_VINUM) {
1378 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1379 (long)pp->p_offset,i+'a',current_offset);
1381 "Labels with any *'s for offset must be in ascending order by sector\n");
1383 } else if (pp->p_offset != current_offset &&
1384 i != RAW_PART && seen_default_offset) {
1386 * this may give unneeded warnings if
1387 * partitions are out-of-order
1390 "Offset %ld for partition %c doesn't match expected value %ld",
1391 (long)pp->p_offset, i + 'a', current_offset);
1395 current_offset = pp->p_offset + pp->p_size;
1399 for (i = 0; i < lp->d_npartitions; i++) {
1401 pp = &lp->d_partitions[i];
1402 if (pp->p_size == 0 && pp->p_offset != 0)
1403 warnx("partition %c: size 0, but offset %lu",
1404 part, (u_long)pp->p_offset);
1406 if (pp->p_size % lp->d_secpercyl)
1407 warnx("partition %c: size %% cylinder-size != 0",
1409 if (pp->p_offset % lp->d_secpercyl)
1410 warnx("partition %c: offset %% cylinder-size != 0",
1413 if (pp->p_offset > lp->d_secperunit) {
1415 "partition %c: offset past end of unit\n", part);
1418 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1420 "partition %c: partition extends past end of unit\n",
1424 if (i == RAW_PART) {
1425 if (pp->p_fstype != FS_UNUSED)
1426 warnx("partition %c is not marked as unused!",part);
1427 if (pp->p_offset != 0)
1428 warnx("partition %c doesn't start at 0!",part);
1429 if (pp->p_size != lp->d_secperunit)
1430 warnx("partition %c doesn't cover the whole unit!",part);
1432 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1433 (pp->p_size != lp->d_secperunit)) {
1434 warnx("An incorrect partition %c may cause problems for "
1435 "standard system utilities",part);
1439 /* check for overlaps */
1440 /* this will check for all possible overlaps once and only once */
1441 for (j = 0; j < i; j++) {
1442 pp2 = &lp->d_partitions[j];
1443 if (j != RAW_PART && i != RAW_PART &&
1444 pp->p_fstype != FS_VINUM &&
1445 pp2->p_fstype != FS_VINUM &&
1446 part_set[i] && part_set[j]) {
1447 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1448 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1449 pp2->p_offset >= pp->p_offset)) {
1450 fprintf(stderr,"partitions %c and %c overlap!\n",
1457 for (; i < lp->d_npartitions; i++) {
1459 pp = &lp->d_partitions[i];
1460 if (pp->p_size || pp->p_offset)
1461 warnx("unused partition %c: size %d offset %lu",
1462 'a' + i, pp->p_size, (u_long)pp->p_offset);
1468 * When operating on a "virgin" disk, try getting an initial label
1469 * from the associated device driver. This might work for all device
1470 * drivers that are able to fetch some initial device parameters
1471 * without even having access to a (BSD) disklabel, like SCSI disks,
1472 * most IDE drives, or vn devices.
1474 * The device name must be given in its "canonical" form.
1476 static struct disklabel *
1477 getvirginlabel(void)
1479 static struct disklabel loclab;
1480 struct partition *dp;
1484 if ((f = open(specname, O_RDONLY)) == -1) {
1485 warn("cannot open %s", specname);
1492 mediasize = g_mediasize(f);
1493 secsize = g_sectorsize(f);
1494 if (secsize < 0 || mediasize < 0) {
1499 memset(&loclab, 0, sizeof loclab);
1500 loclab.d_magic = DISKMAGIC;
1501 loclab.d_magic2 = DISKMAGIC;
1502 loclab.d_secsize = secsize;
1503 loclab.d_secperunit = mediasize / secsize;
1506 * Nobody in these enligthened days uses the CHS geometry for
1507 * anything, but nontheless try to get it right. If we fail
1508 * to get any good ideas from the device, construct something
1509 * which is IBM-PC friendly.
1511 if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1512 loclab.d_nsectors = u;
1514 loclab.d_nsectors = 63;
1515 if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1516 loclab.d_ntracks = u;
1517 else if (loclab.d_secperunit <= 63*1*1024)
1518 loclab.d_ntracks = 1;
1519 else if (loclab.d_secperunit <= 63*16*1024)
1520 loclab.d_ntracks = 16;
1522 loclab.d_ntracks = 255;
1523 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1524 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1525 loclab.d_npartitions = DEFPARTITIONS;
1527 /* Various (unneeded) compat stuff */
1528 loclab.d_rpm = 3600;
1529 loclab.d_bbsize = BBSIZE;
1530 loclab.d_interleave = 1;
1531 strncpy(loclab.d_typename, "amnesiac",
1532 sizeof(loclab.d_typename));
1534 dp = &loclab.d_partitions[RAW_PART];
1535 dp->p_size = loclab.d_secperunit;
1536 loclab.d_checksum = dkcksum(&loclab);
1546 "%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",
1547 "usage: bsdlabel disk",
1548 "\t\t(to read label)",
1549 " bsdlabel -w [-n] [-m machine] disk [type]",
1550 "\t\t(to write label with existing boot program)",
1551 " bsdlabel -e [-n] [-m machine] disk",
1552 "\t\t(to edit label)",
1553 " bsdlabel -R [-n] [-m machine] disk protofile",
1554 "\t\t(to restore label with existing boot program)",
1555 " bsdlabel -B [-b boot] [-m machine] disk",
1556 "\t\t(to install boot program with existing on-disk label)",
1557 " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1558 "\t\t(to write label and install boot program)",
1559 " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
1560 "\t\t(to restore label and install boot program)"