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
100 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 uint32_t lba_offset;
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[])
156 while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1)
171 if (!strcmp(optarg, "i386") ||
172 !strcmp(optarg, "amd64") ||
173 !strcmp(optarg, "ia64") ||
174 !strcmp(optarg, "pc98")) {
179 } else if (!strcmp(optarg, "alpha")) {
185 errx(1, "Unsupported architecture");
203 * We accept and ignode -r for compatibility with
204 * historically disklabel usage.
221 if (labelsoffset < 0 || labeloffset < 0)
222 errx(1, "a -m <architecture> option must be specified");
224 /* Figure out the names of the thing we're working on */
228 specname = g_device_path(argv[0]);
229 if (specname == NULL) {
230 warn("unable to get correct path for %s", argv[0]);
233 fd = open(specname, O_RDONLY);
235 warn("error opening %s", specname);
238 pname = g_providername(fd);
240 warn("error getting providername for %s", specname);
247 if (installboot && op == UNSPEC)
249 else if (op == UNSPEC)
269 display(stdout, NULL);
270 error = checklabel(NULL);
276 if (!(t = fopen(argv[1], "r")))
277 err(4, "fopen %s", argv[1]);
279 if (!getasciilabel(t, &lab))
281 error = writelabel();
292 makelabel(name, &lab);
294 if (checklabel(NULL) == 0)
295 error = writelabel();
303 makelabel(argv[1], &lab);
304 if (checklabel(NULL) == 0)
305 error = writelabel();
312 fixlabel(struct disklabel *lp)
314 struct partition *dp;
317 for (i = 0; i < lp->d_npartitions; i++) {
320 if (lp->d_partitions[i].p_size)
324 dp = &lp->d_partitions[0];
325 dp->p_offset = BBSIZE / secsize;
326 dp->p_size = lp->d_secperunit - dp->p_offset;
330 * Construct a prototype disklabel from /etc/disktab.
333 makelabel(const char *type, struct disklabel *lp)
335 struct disklabel *dp;
337 if (strcmp(type, "auto") == 0)
338 dp = getvirginlabel();
340 dp = getdiskbyname(type);
342 errx(1, "%s: unknown disk type", type);
344 bzero(lp->d_packname, sizeof(lp->d_packname));
355 xxboot = "/boot/boot";
356 fd = open(xxboot, O_RDONLY);
358 err(1, "cannot open %s", xxboot);
360 if (alphacksum && st.st_size <= BBSIZE - 512) {
361 i = read(fd, bootarea + 512, 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 i = read(fd, bootarea, st.st_size);
377 err(1, "read error %s", xxboot);
380 errx(1, "boot code %s is wrong size", xxboot);
388 struct gctl_req *grq;
390 struct disklabel *lp = &lab;
393 warnx("write to disk label supressed - label was as follows:");
394 display(stdout, NULL);
398 lp->d_magic = DISKMAGIC;
399 lp->d_magic2 = DISKMAGIC;
401 lp->d_checksum = dkcksum(lp);
404 for (i = 0; i < lab.d_npartitions; i++)
405 if (lab.d_partitions[i].p_size)
406 lab.d_partitions[i].p_offset += lba_offset;
407 bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * secsize,
410 /* Generate the bootblock checksum for the SRM console. */
411 for (p = (uint64_t *)bootarea, i = 0, sum = 0; i < 63; i++)
416 fd = open(specname, O_RDWR);
419 warn("cannot open file %s for writing label", specname);
423 grq = gctl_get_handle();
424 gctl_ro_param(grq, "verb", -1, "write label");
425 gctl_ro_param(grq, "class", -1, "BSD");
426 gctl_ro_param(grq, "geom", -1, pname);
427 gctl_ro_param(grq, "label", 148+16*8,
428 bootarea + labeloffset + labelsoffset * secsize);
429 errstr = gctl_issue(grq);
430 if (errstr != NULL) {
437 grq = gctl_get_handle();
438 gctl_ro_param(grq, "verb", -1, "write bootcode");
439 gctl_ro_param(grq, "class", -1, "BSD");
440 gctl_ro_param(grq, "geom", -1, pname);
441 gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
442 errstr = gctl_issue(grq);
443 if (errstr != NULL) {
451 if (write(fd, bootarea, bbsize) != bbsize) {
452 warn("write %s", specname);
462 get_file_parms(int f)
467 if (fstat(f, &sb) != 0)
468 err(4, "fstat failed");
469 i = sb.st_mode & S_IFMT;
470 if (i != S_IFREG && i != S_IFLNK)
471 errx(4, "%s is not a valid file or link", specname);
473 mediasize = sb.st_size;
477 * Fetch disklabel for disk.
486 f = open(specname, O_RDONLY);
492 mediasize = g_mediasize(f);
493 secsize = g_sectorsize(f);
494 if (secsize < 0 || mediasize < 0)
495 err(4, "cannot get disk geometry");
497 if (mediasize > (off_t)0xffffffff * secsize)
499 "disks with more than 2^32-1 sectors are not supported");
500 (void)lseek(f, (off_t)0, SEEK_SET);
501 if (read(f, bootarea, BBSIZE) != BBSIZE)
502 err(4, "%s read", specname);
504 error = bsd_disklabel_le_dec(
505 bootarea + (labeloffset + labelsoffset * secsize),
506 &lab, MAXPARTITIONS);
508 errx(1, "%s: no valid label found", specname);
514 * Compensate for absolute block addressing by finding the
515 * smallest partition offset and if the offset of the 'c'
516 * partition is equal to that, subtract it from all offsets.
519 for (i = 0; i < lab.d_npartitions; i++) {
520 if (lab.d_partitions[i].p_size)
521 lba = MIN(lba, lab.d_partitions[i].p_offset);
523 if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
524 for (i = 0; i < lab.d_npartitions; i++) {
525 if (lab.d_partitions[i].p_size)
526 lab.d_partitions[i].p_offset -= lba;
529 * Save the offset so that we can write the label
530 * back with absolute block addresses.
539 display(FILE *f, const struct disklabel *lp)
542 const struct partition *pp;
547 fprintf(f, "# %s:\n", specname);
549 if (lp->d_type < DKMAXTYPES)
550 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
552 fprintf(f, "type: %u\n", lp->d_type);
553 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
555 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
557 fprintf(f, "flags:");
558 if (lp->d_flags & D_REMOVABLE)
559 fprintf(f, " removeable");
560 if (lp->d_flags & D_ECC)
562 if (lp->d_flags & D_BADSECT)
563 fprintf(f, " badsect");
565 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
566 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
567 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
568 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
569 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
570 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
571 fprintf(f, "rpm: %u\n", lp->d_rpm);
572 fprintf(f, "interleave: %u\n", lp->d_interleave);
573 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
574 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
575 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
576 (u_long)lp->d_headswitch);
577 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
578 (u_long)lp->d_trkseek);
579 fprintf(f, "drivedata: ");
580 for (i = NDDATA - 1; i >= 0; i--)
581 if (lp->d_drivedata[i])
585 for (j = 0; j <= i; j++)
586 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
589 fprintf(f, "%u partitions:\n", lp->d_npartitions);
591 "# size offset fstype [fsize bsize bps/cpg]\n");
592 pp = lp->d_partitions;
593 for (i = 0; i < lp->d_npartitions; i++, pp++) {
595 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
596 (u_long)pp->p_size, (u_long)pp->p_offset);
597 if (pp->p_fstype < FSMAXTYPES)
598 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
600 fprintf(f, "%8d", pp->p_fstype);
601 switch (pp->p_fstype) {
603 case FS_UNUSED: /* XXX */
604 fprintf(f, " %5lu %5lu %5.5s ",
606 (u_long)(pp->p_fsize * pp->p_frag), "");
610 fprintf(f, " %5lu %5lu %5u ",
612 (u_long)(pp->p_fsize * pp->p_frag),
617 fprintf(f, " %5lu %5lu %5d",
619 (u_long)(pp->p_fsize * pp->p_frag),
624 fprintf(f, "%20.20s", "");
628 fprintf(f, " # \"raw\" part, don't edit");
640 struct disklabel label;
643 if ((fd = mkstemp(tmpfil)) == -1 ||
644 (fp = fdopen(fd, "w")) == NULL) {
645 warnx("can't create %s", tmpfil);
653 fp = fopen(tmpfil, "r");
655 warnx("can't reopen %s for reading", tmpfil);
658 bzero((char *)&label, sizeof(label));
659 c = getasciilabel(fp, &label);
663 if (writelabel() == 0) {
664 (void) unlink(tmpfil);
668 printf("re-edit the label? [y]: ");
671 if (c != EOF && c != (int)'\n')
672 while (getchar() != (int)'\n')
677 (void) unlink(tmpfil);
688 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
689 while ((pid = fork()) < 0) {
690 if (errno == EPROCLIM) {
691 warnx("you have too many processes");
694 if (errno != EAGAIN) {
704 if ((ed = getenv("EDITOR")) == (char *)0)
706 execlp(ed, ed, tmpfil, (char *)0);
709 while ((xpid = wait(&locstat)) >= 0)
720 while (*cp != '\0' && isspace(*cp))
722 if (*cp == '\0' || *cp == '#')
732 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
734 if ((c = *cp) != '\0') {
743 * Read an ascii label in from fd f,
744 * in the same format as that put out by display(),
748 getasciilabel(FILE *f, struct disklabel *lp)
753 char *tp, line[BUFSIZ];
755 int lineno = 0, errors = 0;
758 makelabel("auto", lp);
759 bzero(&part_set, sizeof(part_set));
760 bzero(&part_size_type, sizeof(part_size_type));
761 bzero(&part_offset_type, sizeof(part_offset_type));
762 lp->d_bbsize = BBSIZE; /* XXX */
763 lp->d_sbsize = 0; /* XXX */
764 while (fgets(line, sizeof(line) - 1, f)) {
766 if ((cp = index(line,'\n')) != 0)
773 fprintf(stderr, "line %d: syntax error\n", lineno);
777 *tp++ = '\0', tp = skip(tp);
778 if (!strcmp(cp, "type")) {
782 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
783 if (*cpp && !strcmp(*cpp, tp)) {
784 lp->d_type = cpp - dktypenames;
787 if (cpp < &dktypenames[DKMAXTYPES])
789 v = strtoul(tp, NULL, 10);
791 fprintf(stderr, "line %d:%s %lu\n", lineno,
792 "Warning, unknown disk type", v);
796 if (!strcmp(cp, "flags")) {
797 for (v = 0; (cp = tp) && *cp != '\0';) {
799 if (!strcmp(cp, "removeable"))
801 else if (!strcmp(cp, "ecc"))
803 else if (!strcmp(cp, "badsect"))
807 "line %d: %s: bad flag\n",
815 if (!strcmp(cp, "drivedata")) {
816 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
817 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
822 if (sscanf(cp, "%lu partitions", &v) == 1) {
823 if (v == 0 || v > MAXPARTITIONS) {
825 "line %d: bad # of partitions\n", lineno);
826 lp->d_npartitions = MAXPARTITIONS;
829 lp->d_npartitions = v;
834 if (!strcmp(cp, "disk")) {
835 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
838 if (!strcmp(cp, "label")) {
839 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
842 if (!strcmp(cp, "bytes/sector")) {
843 v = strtoul(tp, NULL, 10);
844 if (v == 0 || (v % DEV_BSIZE) != 0) {
846 "line %d: %s: bad sector size\n",
853 if (!strcmp(cp, "sectors/track")) {
854 v = strtoul(tp, NULL, 10);
855 #if (ULONG_MAX != 0xffffffffUL)
856 if (v == 0 || v > 0xffffffff)
861 fprintf(stderr, "line %d: %s: bad %s\n",
868 if (!strcmp(cp, "sectors/cylinder")) {
869 v = strtoul(tp, NULL, 10);
871 fprintf(stderr, "line %d: %s: bad %s\n",
878 if (!strcmp(cp, "tracks/cylinder")) {
879 v = strtoul(tp, NULL, 10);
881 fprintf(stderr, "line %d: %s: bad %s\n",
888 if (!strcmp(cp, "cylinders")) {
889 v = strtoul(tp, NULL, 10);
891 fprintf(stderr, "line %d: %s: bad %s\n",
895 lp->d_ncylinders = v;
898 if (!strcmp(cp, "sectors/unit")) {
899 v = strtoul(tp, NULL, 10);
901 fprintf(stderr, "line %d: %s: bad %s\n",
905 lp->d_secperunit = v;
908 if (!strcmp(cp, "rpm")) {
909 v = strtoul(tp, NULL, 10);
910 if (v == 0 || v > USHRT_MAX) {
911 fprintf(stderr, "line %d: %s: bad %s\n",
918 if (!strcmp(cp, "interleave")) {
919 v = strtoul(tp, NULL, 10);
920 if (v == 0 || v > USHRT_MAX) {
921 fprintf(stderr, "line %d: %s: bad %s\n",
925 lp->d_interleave = v;
928 if (!strcmp(cp, "trackskew")) {
929 v = strtoul(tp, NULL, 10);
931 fprintf(stderr, "line %d: %s: bad %s\n",
938 if (!strcmp(cp, "cylinderskew")) {
939 v = strtoul(tp, NULL, 10);
941 fprintf(stderr, "line %d: %s: bad %s\n",
948 if (!strcmp(cp, "headswitch")) {
949 v = strtoul(tp, NULL, 10);
950 lp->d_headswitch = v;
953 if (!strcmp(cp, "track-to-track seek")) {
954 v = strtoul(tp, NULL, 10);
958 /* the ':' was removed above */
959 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
961 "line %d: %s: Unknown disklabel field\n", lineno,
967 /* Process a partition specification line. */
969 if (part >= lp->d_npartitions) {
971 "line %d: partition name out of range a-%c: %s\n",
972 lineno, 'a' + lp->d_npartitions - 1, cp);
978 if (getasciipartspec(tp, lp, part, lineno) != 0) {
983 errors += checklabel(lp);
984 return (errors == 0);
987 #define NXTNUM(n) do { \
989 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
992 cp = tp, tp = word(cp); \
993 (n) = strtoul(cp, NULL, 10); \
997 /* retain 1 character following number */
998 #define NXTWORD(w,n) do { \
1000 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1004 cp = tp, tp = word(cp); \
1005 (n) = strtoul(cp, &tmp, 10); \
1006 if (tmp) (w) = *tmp; \
1011 * Read a partition line into partition `part' in the specified disklabel.
1012 * Return 0 on success, 1 on failure.
1015 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1017 struct partition *pp;
1022 pp = &lp->d_partitions[part];
1026 NXTWORD(part_size_type[part],v);
1027 if (v == 0 && part_size_type[part] != '*') {
1029 "line %d: %s: bad partition size\n", lineno, cp);
1035 NXTWORD(part_offset_type[part],v);
1036 if (v == 0 && part_offset_type[part] != '*' &&
1037 part_offset_type[part] != '\0') {
1039 "line %d: %s: bad partition offset\n", lineno, cp);
1044 fprintf(stderr, "line %d: missing file system type\n", lineno);
1047 cp = tp, tp = word(cp);
1048 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1049 if (*cpp && !strcmp(*cpp, cp))
1052 pp->p_fstype = cpp - fstypenames;
1055 v = strtoul(cp, NULL, 10);
1058 if (v >= FSMAXTYPES) {
1060 "line %d: Warning, unknown file system type %s\n",
1067 switch (pp->p_fstype) {
1071 /* accept defaults for fsize/frag/cpg */
1073 NXTNUM(pp->p_fsize);
1074 if (pp->p_fsize == 0)
1077 pp->p_frag = v / pp->p_fsize;
1081 /* else default to 0's */
1090 * Check disklabel for errors and fill in
1091 * derived fields according to supplied values.
1094 checklabel(struct disklabel *lp)
1096 struct partition *pp;
1099 u_long base_offset, needed, total_size, total_percent, current_offset;
1101 int seen_default_offset;
1104 struct partition *pp2;
1111 if (lp->d_secsize == 0) {
1112 fprintf(stderr, "sector size 0\n");
1115 if (lp->d_nsectors == 0) {
1116 fprintf(stderr, "sectors/track 0\n");
1119 if (lp->d_ntracks == 0) {
1120 fprintf(stderr, "tracks/cylinder 0\n");
1123 if (lp->d_ncylinders == 0) {
1124 fprintf(stderr, "cylinders/unit 0\n");
1128 warnx("revolutions/minute 0");
1129 if (lp->d_secpercyl == 0)
1130 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1131 if (lp->d_secperunit == 0)
1132 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1133 if (lp->d_bbsize == 0) {
1134 fprintf(stderr, "boot block size 0\n");
1136 } else if (lp->d_bbsize % lp->d_secsize)
1137 warnx("boot block size %% sector-size != 0");
1138 if (lp->d_npartitions > MAXPARTITIONS)
1139 warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
1140 (u_long)lp->d_npartitions, MAXPARTITIONS);
1142 struct disklabel *vl;
1144 vl = getvirginlabel();
1145 lp->d_secsize = vl->d_secsize;
1146 lp->d_nsectors = vl->d_nsectors;
1147 lp->d_ntracks = vl->d_ntracks;
1148 lp->d_ncylinders = vl->d_ncylinders;
1149 lp->d_rpm = vl->d_rpm;
1150 lp->d_interleave = vl->d_interleave;
1151 lp->d_secpercyl = vl->d_secpercyl;
1152 lp->d_secperunit = vl->d_secperunit;
1153 lp->d_bbsize = vl->d_bbsize;
1154 lp->d_npartitions = vl->d_npartitions;
1158 /* first allocate space to the partitions, then offsets */
1159 total_size = 0; /* in sectors */
1160 total_percent = 0; /* in percent */
1162 /* find all fixed partitions */
1163 for (i = 0; i < lp->d_npartitions; i++) {
1164 pp = &lp->d_partitions[i];
1166 if (part_size_type[i] == '*') {
1167 if (i == RAW_PART) {
1168 pp->p_size = lp->d_secperunit;
1171 warnx("Too many '*' partitions (%c and %c)",
1172 hog_part + 'a',i + 'a');
1180 switch (part_size_type[i]) {
1182 total_percent += size;
1203 warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
1204 part_size_type[i], i + 'a');
1207 /* don't count %'s yet */
1208 if (part_size_type[i] != '%') {
1210 * for all not in sectors, convert to
1213 if (part_size_type[i] != '\0') {
1214 if (size % lp->d_secsize != 0)
1215 warnx("partition %c not an integer number of sectors",
1217 size /= lp->d_secsize;
1220 /* else already in sectors */
1228 /* Find out the total free space, excluding the boot block area. */
1229 base_offset = BBSIZE / secsize;
1231 for (i = 0; i < lp->d_npartitions; i++) {
1232 pp = &lp->d_partitions[i];
1233 if (!part_set[i] || i == RAW_PART ||
1234 part_size_type[i] == '%' || part_size_type[i] == '*')
1236 if (pp->p_offset > base_offset)
1237 free_space += pp->p_offset - base_offset;
1238 if (pp->p_offset + pp->p_size > base_offset)
1239 base_offset = pp->p_offset + pp->p_size;
1241 if (base_offset < lp->d_secperunit)
1242 free_space += lp->d_secperunit - base_offset;
1244 /* handle % partitions - note %'s don't need to add up to 100! */
1245 if (total_percent != 0) {
1246 if (total_percent > 100) {
1247 fprintf(stderr,"total percentage %lu is greater than 100\n",
1252 if (free_space > 0) {
1253 for (i = 0; i < lp->d_npartitions; i++) {
1254 pp = &lp->d_partitions[i];
1255 if (part_set[i] && part_size_type[i] == '%') {
1256 /* careful of overflows! and integer roundoff */
1257 pp->p_size = ((double)pp->p_size/100) * free_space;
1258 total_size += pp->p_size;
1260 /* FIX we can lose a sector or so due to roundoff per
1261 partition. A more complex algorithm could avoid that */
1266 "%ld sectors available to give to '*' and '%%' partitions\n",
1269 /* fix? set all % partitions to size 0? */
1272 /* give anything remaining to the hog partition */
1273 if (hog_part != -1) {
1275 * Find the range of offsets usable by '*' partitions around
1276 * the hog partition and how much space they need.
1279 base_offset = BBSIZE / secsize;
1280 for (i = hog_part - 1; i >= 0; i--) {
1281 pp = &lp->d_partitions[i];
1282 if (!part_set[i] || i == RAW_PART)
1284 if (part_offset_type[i] == '*') {
1285 needed += pp->p_size;
1288 base_offset = pp->p_offset + pp->p_size;
1291 current_offset = lp->d_secperunit;
1292 for (i = lp->d_npartitions - 1; i > hog_part; i--) {
1293 pp = &lp->d_partitions[i];
1294 if (!part_set[i] || i == RAW_PART)
1296 if (part_offset_type[i] == '*') {
1297 needed += pp->p_size;
1300 current_offset = pp->p_offset;
1303 if (current_offset - base_offset <= needed) {
1304 fprintf(stderr, "Cannot find space for partition %c\n",
1307 "Need more than %lu sectors between %lu and %lu\n",
1308 needed, base_offset, current_offset);
1310 lp->d_partitions[hog_part].p_size = 0;
1312 lp->d_partitions[hog_part].p_size = current_offset -
1313 base_offset - needed;
1314 total_size += lp->d_partitions[hog_part].p_size;
1318 /* Now set the offsets for each partition */
1319 current_offset = BBSIZE / secsize; /* in sectors */
1320 seen_default_offset = 0;
1321 for (i = 0; i < lp->d_npartitions; i++) {
1323 pp = &lp->d_partitions[i];
1325 if (part_offset_type[i] == '*') {
1326 if (i == RAW_PART) {
1329 pp->p_offset = current_offset;
1330 seen_default_offset = 1;
1333 /* allow them to be out of order for old-style tables */
1334 if (pp->p_offset < current_offset &&
1335 seen_default_offset && i != RAW_PART &&
1336 pp->p_fstype != FS_VINUM) {
1338 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1339 (long)pp->p_offset,i+'a',current_offset);
1341 "Labels with any *'s for offset must be in ascending order by sector\n");
1343 } else if (pp->p_offset != current_offset &&
1344 i != RAW_PART && seen_default_offset) {
1346 * this may give unneeded warnings if
1347 * partitions are out-of-order
1350 "Offset %ld for partition %c doesn't match expected value %ld",
1351 (long)pp->p_offset, i + 'a', current_offset);
1355 current_offset = pp->p_offset + pp->p_size;
1359 for (i = 0; i < lp->d_npartitions; i++) {
1361 pp = &lp->d_partitions[i];
1362 if (pp->p_size == 0 && pp->p_offset != 0)
1363 warnx("partition %c: size 0, but offset %lu",
1364 part, (u_long)pp->p_offset);
1366 if (pp->p_size % lp->d_secpercyl)
1367 warnx("partition %c: size %% cylinder-size != 0",
1369 if (pp->p_offset % lp->d_secpercyl)
1370 warnx("partition %c: offset %% cylinder-size != 0",
1373 if (pp->p_offset > lp->d_secperunit) {
1375 "partition %c: offset past end of unit\n", part);
1378 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1380 "partition %c: partition extends past end of unit\n",
1384 if (i == RAW_PART) {
1385 if (pp->p_fstype != FS_UNUSED)
1386 warnx("partition %c is not marked as unused!",part);
1387 if (pp->p_offset != 0)
1388 warnx("partition %c doesn't start at 0!",part);
1389 if (pp->p_size != lp->d_secperunit)
1390 warnx("partition %c doesn't cover the whole unit!",part);
1392 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1393 (pp->p_size != lp->d_secperunit)) {
1394 warnx("An incorrect partition %c may cause problems for "
1395 "standard system utilities",part);
1399 /* check for overlaps */
1400 /* this will check for all possible overlaps once and only once */
1401 for (j = 0; j < i; j++) {
1402 pp2 = &lp->d_partitions[j];
1403 if (j != RAW_PART && i != RAW_PART &&
1404 pp->p_fstype != FS_VINUM &&
1405 pp2->p_fstype != FS_VINUM &&
1406 part_set[i] && part_set[j]) {
1407 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1408 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1409 pp2->p_offset >= pp->p_offset)) {
1410 fprintf(stderr,"partitions %c and %c overlap!\n",
1417 for (; i < lp->d_npartitions; i++) {
1419 pp = &lp->d_partitions[i];
1420 if (pp->p_size || pp->p_offset)
1421 warnx("unused partition %c: size %d offset %lu",
1422 'a' + i, pp->p_size, (u_long)pp->p_offset);
1428 * When operating on a "virgin" disk, try getting an initial label
1429 * from the associated device driver. This might work for all device
1430 * drivers that are able to fetch some initial device parameters
1431 * without even having access to a (BSD) disklabel, like SCSI disks,
1432 * most IDE drives, or vn devices.
1434 * The device name must be given in its "canonical" form.
1436 static struct disklabel *
1437 getvirginlabel(void)
1439 static struct disklabel loclab;
1440 struct partition *dp;
1444 if ((f = open(specname, O_RDONLY)) == -1) {
1445 warn("cannot open %s", specname);
1452 mediasize = g_mediasize(f);
1453 secsize = g_sectorsize(f);
1454 if (secsize < 0 || mediasize < 0) {
1459 memset(&loclab, 0, sizeof loclab);
1460 loclab.d_magic = DISKMAGIC;
1461 loclab.d_magic2 = DISKMAGIC;
1462 loclab.d_secsize = secsize;
1463 loclab.d_secperunit = mediasize / secsize;
1466 * Nobody in these enligthened days uses the CHS geometry for
1467 * anything, but nontheless try to get it right. If we fail
1468 * to get any good ideas from the device, construct something
1469 * which is IBM-PC friendly.
1471 if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1472 loclab.d_nsectors = u;
1474 loclab.d_nsectors = 63;
1475 if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1476 loclab.d_ntracks = u;
1477 else if (loclab.d_secperunit <= 63*1*1024)
1478 loclab.d_ntracks = 1;
1479 else if (loclab.d_secperunit <= 63*16*1024)
1480 loclab.d_ntracks = 16;
1482 loclab.d_ntracks = 255;
1483 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1484 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1485 loclab.d_npartitions = DEFPARTITIONS;
1487 /* Various (unneeded) compat stuff */
1488 loclab.d_rpm = 3600;
1489 loclab.d_bbsize = BBSIZE;
1490 loclab.d_interleave = 1;
1491 strncpy(loclab.d_typename, "amnesiac",
1492 sizeof(loclab.d_typename));
1494 dp = &loclab.d_partitions[RAW_PART];
1495 dp->p_size = loclab.d_secperunit;
1496 loclab.d_checksum = dkcksum(&loclab);
1506 "%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",
1507 "usage: bsdlabel disk",
1508 "\t\t(to read label)",
1509 " bsdlabel -w [-n] [-m machine] disk [type]",
1510 "\t\t(to write label with existing boot program)",
1511 " bsdlabel -e [-n] [-m machine] disk",
1512 "\t\t(to edit label)",
1513 " bsdlabel -R [-n] [-m machine] disk protofile",
1514 "\t\t(to restore label with existing boot program)",
1515 " bsdlabel -B [-b boot] [-m machine] disk",
1516 "\t\t(to install boot program with existing on-disk label)",
1517 " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1518 "\t\t(to write label and install boot program)",
1519 " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
1520 "\t\t(to restore label and install boot program)"