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 20
67 #include <sys/disklabel.h>
80 #include "pathnames.h"
82 static void makelabel(const char *, struct disklabel *);
83 static int geom_class_available(const char *);
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 ssize_t 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;
134 UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
138 static int disable_write; /* set to disable writing to disk label */
139 static int is_file; /* work on a file (abs. pathname), "-f" opt. */
142 main(int argc, char *argv[])
151 while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1)
166 if (!strcmp(optarg, "i386") ||
167 !strcmp(optarg, "amd64") ||
168 !strcmp(optarg, "pc98")) {
173 errx(1, "Unsupported architecture");
191 * We accept and ignore -r for compatibility with
192 * historical disklabel usage.
209 if (labelsoffset < 0 || labeloffset < 0)
210 errx(1, "a -m <architecture> option must be specified");
212 /* Figure out the names of the thing we're working on */
216 specname = g_device_path(argv[0]);
217 if (specname == NULL) {
218 warn("unable to get correct path for %s", argv[0]);
221 fd = open(specname, O_RDONLY);
223 warn("error opening %s", specname);
226 pname = g_providername(fd);
228 warn("error getting providername for %s", specname);
235 if (installboot && op == UNSPEC)
237 else if (op == UNSPEC)
257 display(stdout, NULL);
258 error = checklabel(NULL);
264 if (!(t = fopen(argv[1], "r")))
265 err(4, "fopen %s", argv[1]);
267 if (!getasciilabel(t, &lab))
269 error = writelabel();
280 makelabel(name, &lab);
282 if (checklabel(NULL) == 0)
283 error = writelabel();
291 makelabel(argv[1], &lab);
292 if (checklabel(NULL) == 0)
293 error = writelabel();
300 fixlabel(struct disklabel *lp)
302 struct partition *dp;
305 for (i = 0; i < lp->d_npartitions; i++) {
308 if (lp->d_partitions[i].p_size)
312 dp = &lp->d_partitions[0];
313 dp->p_offset = BBSIZE / secsize;
314 dp->p_size = lp->d_secperunit - dp->p_offset;
318 * Construct a prototype disklabel from /etc/disktab.
321 makelabel(const char *type, struct disklabel *lp)
323 struct disklabel *dp;
325 if (strcmp(type, "auto") == 0)
326 dp = getvirginlabel();
328 dp = getdiskbyname(type);
330 errx(1, "%s: unknown disk type", type);
332 bzero(lp->d_packname, sizeof(lp->d_packname));
342 xxboot = "/boot/boot";
343 fd = open(xxboot, O_RDONLY);
345 err(1, "cannot open %s", xxboot);
347 if (st.st_size <= BBSIZE) {
348 if (read(fd, bootarea, st.st_size) != st.st_size)
349 err(1, "read error %s", xxboot);
353 errx(1, "boot code %s is wrong size", xxboot);
357 geom_class_available(const char *name)
359 struct gclass *class;
363 error = geom_gettree(&mesh);
365 errc(1, error, "Cannot get GEOM tree");
367 LIST_FOREACH(class, &mesh.lg_class, lg_class) {
368 if (strcmp(class->lg_name, name) == 0) {
369 geom_deletetree(&mesh);
374 geom_deletetree(&mesh);
383 struct gctl_req *grq;
385 struct disklabel *lp = &lab;
388 warnx("write to disk label suppressed - label was as follows:");
389 display(stdout, NULL);
393 lp->d_magic = DISKMAGIC;
394 lp->d_magic2 = DISKMAGIC;
396 lp->d_checksum = dkcksum(lp);
399 for (i = 0; i < lab.d_npartitions; i++)
400 if (lab.d_partitions[i].p_size)
401 lab.d_partitions[i].p_offset += lba_offset;
402 bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * lab.d_secsize,
405 fd = open(specname, O_RDWR);
408 warn("cannot open file %s for writing label", specname);
413 if (geom_class_available("PART") != 0) {
415 * Since we weren't able open provider for
416 * writing, then recommend user to use gpart(8).
419 "cannot open provider %s for writing label",
421 warnx("Try to use gpart(8).");
425 /* Give up if GEOM_BSD is not available. */
426 if (geom_class_available("BSD") == 0) {
427 warnc(serrno, "%s", specname);
431 grq = gctl_get_handle();
432 gctl_ro_param(grq, "verb", -1, "write label");
433 gctl_ro_param(grq, "class", -1, "BSD");
434 gctl_ro_param(grq, "geom", -1, pname);
435 gctl_ro_param(grq, "label", 148+16*8,
436 bootarea + labeloffset + labelsoffset * lab.d_secsize);
437 errstr = gctl_issue(grq);
438 if (errstr != NULL) {
445 grq = gctl_get_handle();
446 gctl_ro_param(grq, "verb", -1, "write bootcode");
447 gctl_ro_param(grq, "class", -1, "BSD");
448 gctl_ro_param(grq, "geom", -1, pname);
449 gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
450 errstr = gctl_issue(grq);
451 if (errstr != NULL) {
459 if (write(fd, bootarea, bbsize) != bbsize) {
460 warn("write %s", specname);
470 get_file_parms(int f)
475 if (fstat(f, &sb) != 0)
476 err(4, "fstat failed");
477 i = sb.st_mode & S_IFMT;
478 if (i != S_IFREG && i != S_IFLNK)
479 errx(4, "%s is not a valid file or link", specname);
481 mediasize = sb.st_size;
485 * Fetch disklabel for disk.
495 f = open(specname, O_RDONLY);
497 err(1, "%s", specname);
501 mediasize = g_mediasize(f);
502 secsize = g_sectorsize(f);
503 if (secsize < 0 || mediasize < 0)
504 err(4, "cannot get disk geometry");
506 if (mediasize > (off_t)0xffffffff * secsize)
508 "disks with more than 2^32-1 sectors are not supported");
509 (void)lseek(f, (off_t)0, SEEK_SET);
510 nbytes = read(f, bootarea, BBSIZE);
512 err(4, "%s read", specname);
513 if (nbytes != BBSIZE)
514 errx(4, "couldn't read %d bytes from %s", BBSIZE, specname);
516 error = bsd_disklabel_le_dec(
517 bootarea + (labeloffset + labelsoffset * secsize),
518 &lab, MAXPARTITIONS);
520 errx(1, "%s: no valid label found", specname);
526 * Compensate for absolute block addressing by finding the
527 * smallest partition offset and if the offset of the 'c'
528 * partition is equal to that, subtract it from all offsets.
531 for (i = 0; i < lab.d_npartitions; i++) {
532 if (lab.d_partitions[i].p_size)
533 lba = MIN(lba, lab.d_partitions[i].p_offset);
535 if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
536 for (i = 0; i < lab.d_npartitions; i++) {
537 if (lab.d_partitions[i].p_size)
538 lab.d_partitions[i].p_offset -= lba;
541 * Save the offset so that we can write the label
542 * back with absolute block addresses.
551 display(FILE *f, const struct disklabel *lp)
554 const struct partition *pp;
559 fprintf(f, "# %s:\n", specname);
561 if (lp->d_type < DKMAXTYPES)
562 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
564 fprintf(f, "type: %u\n", lp->d_type);
565 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
567 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
569 fprintf(f, "flags:");
570 if (lp->d_flags & D_REMOVABLE)
571 fprintf(f, " removeable");
572 if (lp->d_flags & D_ECC)
574 if (lp->d_flags & D_BADSECT)
575 fprintf(f, " badsect");
577 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
578 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
579 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
580 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
581 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
582 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
583 fprintf(f, "rpm: %u\n", lp->d_rpm);
584 fprintf(f, "interleave: %u\n", lp->d_interleave);
585 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
586 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
587 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
588 (u_long)lp->d_headswitch);
589 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
590 (u_long)lp->d_trkseek);
591 fprintf(f, "drivedata: ");
592 for (i = NDDATA - 1; i >= 0; i--)
593 if (lp->d_drivedata[i])
597 for (j = 0; j <= i; j++)
598 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
601 fprintf(f, "%u partitions:\n", lp->d_npartitions);
603 "# size offset fstype [fsize bsize bps/cpg]\n");
604 pp = lp->d_partitions;
605 for (i = 0; i < lp->d_npartitions; i++, pp++) {
607 fprintf(f, " %c: %10lu %10lu ", 'a' + i,
608 (u_long)pp->p_size, (u_long)pp->p_offset);
609 if (pp->p_fstype < FSMAXTYPES)
610 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
612 fprintf(f, "%8d", pp->p_fstype);
613 switch (pp->p_fstype) {
615 case FS_UNUSED: /* XXX */
616 fprintf(f, " %5lu %5lu %2s",
618 (u_long)(pp->p_fsize * pp->p_frag), "");
622 fprintf(f, " %5lu %5lu %5u",
624 (u_long)(pp->p_fsize * pp->p_frag),
629 fprintf(f, " %5lu %5lu %5d",
631 (u_long)(pp->p_fsize * pp->p_frag),
636 fprintf(f, "%20.20s", "");
640 fprintf(f, " # \"raw\" part, don't edit");
652 struct disklabel label;
655 if ((fd = mkstemp(tmpfil)) == -1 ||
656 (fp = fdopen(fd, "w")) == NULL) {
657 warnx("can't create %s", tmpfil);
665 fp = fopen(tmpfil, "r");
667 warnx("can't reopen %s for reading", tmpfil);
670 bzero((char *)&label, sizeof(label));
671 c = getasciilabel(fp, &label);
675 if (writelabel() == 0) {
676 (void) unlink(tmpfil);
680 printf("re-edit the label? [y]: ");
683 if (c != EOF && c != (int)'\n')
684 while (getchar() != (int)'\n')
689 (void) unlink(tmpfil);
702 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
703 while ((pid = fork()) < 0) {
704 if (errno == EPROCLIM) {
705 warnx("you have too many processes");
708 if (errno != EAGAIN) {
717 if (setresgid(gid, gid, gid) == -1)
720 if (setresuid(uid, uid, uid) == -1)
722 if ((ed = getenv("EDITOR")) == (char *)0)
724 execlp(ed, ed, tmpfil, (char *)0);
727 while ((xpid = wait(&locstat)) >= 0)
738 while (*cp != '\0' && isspace(*cp))
740 if (*cp == '\0' || *cp == '#')
750 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
752 if ((c = *cp) != '\0') {
761 * Read an ascii label in from fd f,
762 * in the same format as that put out by display(),
766 getasciilabel(FILE *f, struct disklabel *lp)
771 char *tp, line[BUFSIZ];
773 int lineno = 0, errors = 0;
776 makelabel("auto", lp);
777 bzero(&part_set, sizeof(part_set));
778 bzero(&part_size_type, sizeof(part_size_type));
779 bzero(&part_offset_type, sizeof(part_offset_type));
780 lp->d_bbsize = BBSIZE; /* XXX */
781 lp->d_sbsize = 0; /* XXX */
782 while (fgets(line, sizeof(line) - 1, f)) {
784 if ((cp = strchr(line,'\n')) != NULL)
789 tp = strchr(cp, ':');
791 fprintf(stderr, "line %d: syntax error\n", lineno);
795 *tp++ = '\0', tp = skip(tp);
796 if (!strcmp(cp, "type")) {
800 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
801 if (*cpp && !strcmp(*cpp, tp)) {
802 lp->d_type = cpp - dktypenames;
805 if (cpp < &dktypenames[DKMAXTYPES])
808 v = strtoul(tp, &endp, 10);
809 if (errno != 0 || *endp != '\0')
812 fprintf(stderr, "line %d:%s %lu\n", lineno,
813 "Warning, unknown disk type", v);
818 if (!strcmp(cp, "flags")) {
819 for (v = 0; (cp = tp) && *cp != '\0';) {
821 if (!strcmp(cp, "removeable"))
823 else if (!strcmp(cp, "ecc"))
825 else if (!strcmp(cp, "badsect"))
829 "line %d: %s: bad flag\n",
837 if (!strcmp(cp, "drivedata")) {
838 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
839 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
844 if (sscanf(cp, "%lu partitions", &v) == 1) {
845 if (v > MAXPARTITIONS) {
847 "line %d: bad # of partitions\n", lineno);
848 lp->d_npartitions = MAXPARTITIONS;
850 } else if (v < DEFPARTITIONS) {
852 "line %d: bad # of partitions\n", lineno);
853 lp->d_npartitions = DEFPARTITIONS;
856 lp->d_npartitions = v;
861 if (!strcmp(cp, "disk")) {
862 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
865 if (!strcmp(cp, "label")) {
866 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
869 if (!strcmp(cp, "bytes/sector")) {
870 v = strtoul(tp, NULL, 10);
871 if (v == 0 || (v % DEV_BSIZE) != 0) {
873 "line %d: %s: bad sector size\n",
880 if (!strcmp(cp, "sectors/track")) {
881 v = strtoul(tp, NULL, 10);
882 #if (ULONG_MAX != 0xffffffffUL)
883 if (v == 0 || v > 0xffffffff)
888 fprintf(stderr, "line %d: %s: bad %s\n",
895 if (!strcmp(cp, "sectors/cylinder")) {
896 v = strtoul(tp, NULL, 10);
898 fprintf(stderr, "line %d: %s: bad %s\n",
905 if (!strcmp(cp, "tracks/cylinder")) {
906 v = strtoul(tp, NULL, 10);
908 fprintf(stderr, "line %d: %s: bad %s\n",
915 if (!strcmp(cp, "cylinders")) {
916 v = strtoul(tp, NULL, 10);
918 fprintf(stderr, "line %d: %s: bad %s\n",
922 lp->d_ncylinders = v;
925 if (!strcmp(cp, "sectors/unit")) {
926 v = strtoul(tp, NULL, 10);
928 fprintf(stderr, "line %d: %s: bad %s\n",
932 lp->d_secperunit = v;
935 if (!strcmp(cp, "rpm")) {
936 v = strtoul(tp, NULL, 10);
937 if (v == 0 || v > USHRT_MAX) {
938 fprintf(stderr, "line %d: %s: bad %s\n",
945 if (!strcmp(cp, "interleave")) {
946 v = strtoul(tp, NULL, 10);
947 if (v == 0 || v > USHRT_MAX) {
948 fprintf(stderr, "line %d: %s: bad %s\n",
952 lp->d_interleave = v;
955 if (!strcmp(cp, "trackskew")) {
956 v = strtoul(tp, NULL, 10);
958 fprintf(stderr, "line %d: %s: bad %s\n",
965 if (!strcmp(cp, "cylinderskew")) {
966 v = strtoul(tp, NULL, 10);
968 fprintf(stderr, "line %d: %s: bad %s\n",
975 if (!strcmp(cp, "headswitch")) {
976 v = strtoul(tp, NULL, 10);
977 lp->d_headswitch = v;
980 if (!strcmp(cp, "track-to-track seek")) {
981 v = strtoul(tp, NULL, 10);
985 /* the ':' was removed above */
986 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
988 "line %d: %s: Unknown disklabel field\n", lineno,
994 /* Process a partition specification line. */
996 if (part >= lp->d_npartitions) {
998 "line %d: partition name out of range a-%c: %s\n",
999 lineno, 'a' + lp->d_npartitions - 1, cp);
1005 if (getasciipartspec(tp, lp, part, lineno) != 0) {
1010 errors += checklabel(lp);
1011 return (errors == 0);
1014 #define NXTNUM(n) do { \
1016 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1019 cp = tp, tp = word(cp); \
1020 (n) = strtoul(cp, NULL, 10); \
1024 /* retain 1 character following number */
1025 #define NXTWORD(w,n) do { \
1027 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1031 cp = tp, tp = word(cp); \
1032 (n) = strtoul(cp, &tmp, 10); \
1033 if (tmp) (w) = *tmp; \
1038 * Read a partition line into partition `part' in the specified disklabel.
1039 * Return 0 on success, 1 on failure.
1042 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1044 struct partition *pp;
1049 pp = &lp->d_partitions[part];
1053 NXTWORD(part_size_type[part],v);
1054 if (v == 0 && part_size_type[part] != '*') {
1056 "line %d: %s: bad partition size\n", lineno, cp);
1062 NXTWORD(part_offset_type[part],v);
1063 if (v == 0 && part_offset_type[part] != '*' &&
1064 part_offset_type[part] != '\0') {
1066 "line %d: %s: bad partition offset\n", lineno, cp);
1071 fprintf(stderr, "line %d: missing file system type\n", lineno);
1074 cp = tp, tp = word(cp);
1075 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1076 if (*cpp && !strcmp(*cpp, cp))
1079 pp->p_fstype = cpp - fstypenames;
1083 v = strtoul(cp, &endp, 10);
1084 if (errno != 0 || *endp != '\0')
1088 if (v >= FSMAXTYPES) {
1090 "line %d: Warning, unknown file system type %s\n",
1097 switch (pp->p_fstype) {
1101 /* accept defaults for fsize/frag/cpg */
1103 NXTNUM(pp->p_fsize);
1104 if (pp->p_fsize == 0)
1107 pp->p_frag = v / pp->p_fsize;
1111 /* else default to 0's */
1120 * Check disklabel for errors and fill in
1121 * derived fields according to supplied values.
1124 checklabel(struct disklabel *lp)
1126 struct partition *pp;
1129 u_long base_offset, needed, total_size, total_percent, current_offset;
1131 int seen_default_offset;
1134 struct partition *pp2;
1141 if (lp->d_secsize == 0) {
1142 fprintf(stderr, "sector size 0\n");
1145 if (lp->d_nsectors == 0) {
1146 fprintf(stderr, "sectors/track 0\n");
1149 if (lp->d_ntracks == 0) {
1150 fprintf(stderr, "tracks/cylinder 0\n");
1153 if (lp->d_ncylinders == 0) {
1154 fprintf(stderr, "cylinders/unit 0\n");
1158 warnx("revolutions/minute 0");
1159 if (lp->d_secpercyl == 0)
1160 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1161 if (lp->d_secperunit == 0)
1162 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1163 if (lp->d_bbsize == 0) {
1164 fprintf(stderr, "boot block size 0\n");
1166 } else if (lp->d_bbsize % lp->d_secsize)
1167 warnx("boot block size %% sector-size != 0");
1168 if (lp->d_npartitions > MAXPARTITIONS) {
1169 warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
1170 (u_long)lp->d_npartitions, MAXPARTITIONS);
1173 if (lp->d_npartitions < DEFPARTITIONS) {
1174 warnx("number of partitions (%lu) < DEFPARTITIONS (%d)",
1175 (u_long)lp->d_npartitions, DEFPARTITIONS);
1179 struct disklabel *vl;
1181 vl = getvirginlabel();
1182 if (lp->d_secsize == 0)
1183 lp->d_secsize = vl->d_secsize;
1184 if (lp->d_nsectors == 0)
1185 lp->d_nsectors = vl->d_nsectors;
1186 if (lp->d_ntracks == 0)
1187 lp->d_ntracks = vl->d_ntracks;
1188 if (lp->d_ncylinders == 0)
1189 lp->d_ncylinders = vl->d_ncylinders;
1191 lp->d_rpm = vl->d_rpm;
1192 if (lp->d_interleave == 0)
1193 lp->d_interleave = vl->d_interleave;
1194 if (lp->d_secpercyl == 0)
1195 lp->d_secpercyl = vl->d_secpercyl;
1196 if (lp->d_secperunit == 0 ||
1197 lp->d_secperunit > vl->d_secperunit)
1198 lp->d_secperunit = vl->d_secperunit;
1199 if (lp->d_bbsize == 0)
1200 lp->d_bbsize = vl->d_bbsize;
1201 if (lp->d_npartitions < DEFPARTITIONS ||
1202 lp->d_npartitions > MAXPARTITIONS)
1203 lp->d_npartitions = vl->d_npartitions;
1207 /* first allocate space to the partitions, then offsets */
1208 total_size = 0; /* in sectors */
1209 total_percent = 0; /* in percent */
1211 /* find all fixed partitions */
1212 for (i = 0; i < lp->d_npartitions; i++) {
1213 pp = &lp->d_partitions[i];
1215 if (part_size_type[i] == '*') {
1216 if (i == RAW_PART) {
1217 pp->p_size = lp->d_secperunit;
1220 warnx("Too many '*' partitions (%c and %c)",
1221 hog_part + 'a',i + 'a');
1229 switch (part_size_type[i]) {
1231 total_percent += size;
1252 warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
1253 part_size_type[i], i + 'a');
1256 /* don't count %'s yet */
1257 if (part_size_type[i] != '%') {
1259 * for all not in sectors, convert to
1262 if (part_size_type[i] != '\0') {
1263 if (size % lp->d_secsize != 0)
1264 warnx("partition %c not an integer number of sectors",
1266 size /= lp->d_secsize;
1269 /* else already in sectors */
1277 /* Find out the total free space, excluding the boot block area. */
1278 base_offset = BBSIZE / secsize;
1280 for (i = 0; i < lp->d_npartitions; i++) {
1281 pp = &lp->d_partitions[i];
1282 if (!part_set[i] || i == RAW_PART ||
1283 part_size_type[i] == '%' || part_size_type[i] == '*')
1285 if (pp->p_offset > base_offset)
1286 free_space += pp->p_offset - base_offset;
1287 if (pp->p_offset + pp->p_size > base_offset)
1288 base_offset = pp->p_offset + pp->p_size;
1290 if (base_offset < lp->d_secperunit)
1291 free_space += lp->d_secperunit - base_offset;
1293 /* handle % partitions - note %'s don't need to add up to 100! */
1294 if (total_percent != 0) {
1295 if (total_percent > 100) {
1296 fprintf(stderr,"total percentage %lu is greater than 100\n",
1301 if (free_space > 0) {
1302 for (i = 0; i < lp->d_npartitions; i++) {
1303 pp = &lp->d_partitions[i];
1304 if (part_set[i] && part_size_type[i] == '%') {
1305 /* careful of overflows! and integer roundoff */
1306 pp->p_size = ((double)pp->p_size/100) * free_space;
1307 total_size += pp->p_size;
1309 /* FIX we can lose a sector or so due to roundoff per
1310 partition. A more complex algorithm could avoid that */
1315 "%ld sectors available to give to '*' and '%%' partitions\n",
1318 /* fix? set all % partitions to size 0? */
1321 /* give anything remaining to the hog partition */
1322 if (hog_part != -1) {
1324 * Find the range of offsets usable by '*' partitions around
1325 * the hog partition and how much space they need.
1328 base_offset = BBSIZE / secsize;
1329 for (i = hog_part - 1; i >= 0; i--) {
1330 pp = &lp->d_partitions[i];
1331 if (!part_set[i] || i == RAW_PART)
1333 if (part_offset_type[i] == '*') {
1334 needed += pp->p_size;
1337 base_offset = pp->p_offset + pp->p_size;
1340 current_offset = lp->d_secperunit;
1341 for (i = lp->d_npartitions - 1; i > hog_part; i--) {
1342 pp = &lp->d_partitions[i];
1343 if (!part_set[i] || i == RAW_PART)
1345 if (part_offset_type[i] == '*') {
1346 needed += pp->p_size;
1349 current_offset = pp->p_offset;
1352 if (current_offset - base_offset <= needed) {
1353 fprintf(stderr, "Cannot find space for partition %c\n",
1356 "Need more than %lu sectors between %lu and %lu\n",
1357 needed, base_offset, current_offset);
1359 lp->d_partitions[hog_part].p_size = 0;
1361 lp->d_partitions[hog_part].p_size = current_offset -
1362 base_offset - needed;
1363 total_size += lp->d_partitions[hog_part].p_size;
1367 /* Now set the offsets for each partition */
1368 current_offset = BBSIZE / secsize; /* in sectors */
1369 seen_default_offset = 0;
1370 for (i = 0; i < lp->d_npartitions; i++) {
1372 pp = &lp->d_partitions[i];
1374 if (part_offset_type[i] == '*') {
1375 if (i == RAW_PART) {
1378 pp->p_offset = current_offset;
1379 seen_default_offset = 1;
1382 /* allow them to be out of order for old-style tables */
1383 if (pp->p_offset < current_offset &&
1384 seen_default_offset && i != RAW_PART &&
1385 pp->p_fstype != FS_VINUM) {
1387 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1388 (long)pp->p_offset,i+'a',current_offset);
1390 "Labels with any *'s for offset must be in ascending order by sector\n");
1392 } else if (pp->p_offset != current_offset &&
1393 i != RAW_PART && seen_default_offset) {
1395 * this may give unneeded warnings if
1396 * partitions are out-of-order
1399 "Offset %ld for partition %c doesn't match expected value %ld",
1400 (long)pp->p_offset, i + 'a', current_offset);
1404 current_offset = pp->p_offset + pp->p_size;
1408 for (i = 0; i < lp->d_npartitions; i++) {
1410 pp = &lp->d_partitions[i];
1411 if (pp->p_size == 0 && pp->p_offset != 0)
1412 warnx("partition %c: size 0, but offset %lu",
1413 part, (u_long)pp->p_offset);
1415 if (pp->p_size % lp->d_secpercyl)
1416 warnx("partition %c: size %% cylinder-size != 0",
1418 if (pp->p_offset % lp->d_secpercyl)
1419 warnx("partition %c: offset %% cylinder-size != 0",
1422 if (pp->p_offset > lp->d_secperunit) {
1424 "partition %c: offset past end of unit\n", part);
1427 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1429 "partition %c: partition extends past end of unit\n",
1433 if (i == RAW_PART) {
1434 if (pp->p_fstype != FS_UNUSED)
1435 warnx("partition %c is not marked as unused!",part);
1436 if (pp->p_offset != 0)
1437 warnx("partition %c doesn't start at 0!",part);
1438 if (pp->p_size != lp->d_secperunit)
1439 warnx("partition %c doesn't cover the whole unit!",part);
1441 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1442 (pp->p_size != lp->d_secperunit)) {
1443 warnx("An incorrect partition %c may cause problems for "
1444 "standard system utilities",part);
1448 /* check for overlaps */
1449 /* this will check for all possible overlaps once and only once */
1450 for (j = 0; j < i; j++) {
1451 pp2 = &lp->d_partitions[j];
1452 if (j != RAW_PART && i != RAW_PART &&
1453 pp->p_fstype != FS_VINUM &&
1454 pp2->p_fstype != FS_VINUM &&
1455 part_set[i] && part_set[j]) {
1456 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1457 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1458 pp2->p_offset >= pp->p_offset)) {
1459 fprintf(stderr,"partitions %c and %c overlap!\n",
1466 for (; i < lp->d_npartitions; i++) {
1468 pp = &lp->d_partitions[i];
1469 if (pp->p_size || pp->p_offset)
1470 warnx("unused partition %c: size %d offset %lu",
1471 'a' + i, pp->p_size, (u_long)pp->p_offset);
1477 * When operating on a "virgin" disk, try getting an initial label
1478 * from the associated device driver. This might work for all device
1479 * drivers that are able to fetch some initial device parameters
1480 * without even having access to a (BSD) disklabel, like SCSI disks,
1481 * most IDE drives, or vn devices.
1483 * The device name must be given in its "canonical" form.
1485 static struct disklabel *
1486 getvirginlabel(void)
1488 static struct disklabel loclab;
1489 struct partition *dp;
1493 if ((f = open(specname, O_RDONLY)) == -1) {
1494 warn("cannot open %s", specname);
1501 mediasize = g_mediasize(f);
1502 secsize = g_sectorsize(f);
1503 if (secsize < 0 || mediasize < 0) {
1508 memset(&loclab, 0, sizeof loclab);
1509 loclab.d_magic = DISKMAGIC;
1510 loclab.d_magic2 = DISKMAGIC;
1511 loclab.d_secsize = secsize;
1512 loclab.d_secperunit = mediasize / secsize;
1515 * Nobody in these enlightened days uses the CHS geometry for
1516 * anything, but nonetheless try to get it right. If we fail
1517 * to get any good ideas from the device, construct something
1518 * which is IBM-PC friendly.
1520 if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1521 loclab.d_nsectors = u;
1523 loclab.d_nsectors = 63;
1524 if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1525 loclab.d_ntracks = u;
1526 else if (loclab.d_secperunit <= 63*1*1024)
1527 loclab.d_ntracks = 1;
1528 else if (loclab.d_secperunit <= 63*16*1024)
1529 loclab.d_ntracks = 16;
1531 loclab.d_ntracks = 255;
1532 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1533 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1534 loclab.d_npartitions = DEFPARTITIONS;
1536 /* Various (unneeded) compat stuff */
1537 loclab.d_rpm = 3600;
1538 loclab.d_bbsize = BBSIZE;
1539 loclab.d_interleave = 1;
1540 strncpy(loclab.d_typename, "amnesiac",
1541 sizeof(loclab.d_typename));
1543 dp = &loclab.d_partitions[RAW_PART];
1544 dp->p_size = loclab.d_secperunit;
1545 loclab.d_checksum = dkcksum(&loclab);
1555 "%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",
1556 "usage: bsdlabel disk",
1557 "\t\t(to read label)",
1558 " bsdlabel -w [-n] [-m machine] disk [type]",
1559 "\t\t(to write label with existing boot program)",
1560 " bsdlabel -e [-n] [-m machine] disk",
1561 "\t\t(to edit label)",
1562 " bsdlabel -R [-n] [-m machine] disk protofile",
1563 "\t\t(to restore label with existing boot program)",
1564 " bsdlabel -B [-b boot] [-m machine] disk",
1565 "\t\t(to install boot program with existing on-disk label)",
1566 " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1567 "\t\t(to write label and install boot program)",
1568 " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
1569 "\t\t(to restore label and install boot program)"