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 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, "ia64") ||
169 !strcmp(optarg, "pc98")) {
174 errx(1, "Unsupported architecture");
192 * We accept and ignode -r for compatibility with
193 * historically disklabel usage.
210 if (labelsoffset < 0 || labeloffset < 0)
211 errx(1, "a -m <architecture> option must be specified");
213 /* Figure out the names of the thing we're working on */
217 specname = g_device_path(argv[0]);
218 if (specname == NULL) {
219 warn("unable to get correct path for %s", argv[0]);
222 fd = open(specname, O_RDONLY);
224 warn("error opening %s", specname);
227 pname = g_providername(fd);
229 warn("error getting providername for %s", specname);
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));
343 xxboot = "/boot/boot";
344 fd = open(xxboot, O_RDONLY);
346 err(1, "cannot open %s", xxboot);
348 if (st.st_size <= BBSIZE) {
349 if (read(fd, bootarea, st.st_size) != st.st_size)
350 err(1, "read error %s", xxboot);
354 errx(1, "boot code %s is wrong size", xxboot);
358 geom_bsd_available(void)
360 struct gclass *class;
364 error = geom_gettree(&mesh);
366 errc(1, error, "Cannot get GEOM tree");
368 LIST_FOREACH(class, &mesh.lg_class, lg_class) {
369 if (strcmp(class->lg_name, "BSD") == 0) {
370 geom_deletetree(&mesh);
375 geom_deletetree(&mesh);
384 struct gctl_req *grq;
386 struct disklabel *lp = &lab;
389 warnx("write to disk label supressed - label was as follows:");
390 display(stdout, NULL);
394 lp->d_magic = DISKMAGIC;
395 lp->d_magic2 = DISKMAGIC;
397 lp->d_checksum = dkcksum(lp);
400 for (i = 0; i < lab.d_npartitions; i++)
401 if (lab.d_partitions[i].p_size)
402 lab.d_partitions[i].p_offset += lba_offset;
403 bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * secsize,
406 fd = open(specname, O_RDWR);
409 warn("cannot open file %s for writing label", specname);
414 /* Give up if GEOM_BSD is not available. */
415 if (geom_bsd_available() == 0) {
416 warnc(serrno, "%s", specname);
420 grq = gctl_get_handle();
421 gctl_ro_param(grq, "verb", -1, "write label");
422 gctl_ro_param(grq, "class", -1, "BSD");
423 gctl_ro_param(grq, "geom", -1, pname);
424 gctl_ro_param(grq, "label", 148+16*8,
425 bootarea + labeloffset + labelsoffset * secsize);
426 errstr = gctl_issue(grq);
427 if (errstr != NULL) {
434 grq = gctl_get_handle();
435 gctl_ro_param(grq, "verb", -1, "write bootcode");
436 gctl_ro_param(grq, "class", -1, "BSD");
437 gctl_ro_param(grq, "geom", -1, pname);
438 gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
439 errstr = gctl_issue(grq);
440 if (errstr != NULL) {
448 if (write(fd, bootarea, bbsize) != bbsize) {
449 warn("write %s", specname);
459 get_file_parms(int f)
464 if (fstat(f, &sb) != 0)
465 err(4, "fstat failed");
466 i = sb.st_mode & S_IFMT;
467 if (i != S_IFREG && i != S_IFLNK)
468 errx(4, "%s is not a valid file or link", specname);
470 mediasize = sb.st_size;
474 * Fetch disklabel for disk.
484 f = open(specname, O_RDONLY);
486 err(1, "%s", specname);
490 mediasize = g_mediasize(f);
491 secsize = g_sectorsize(f);
492 if (secsize < 0 || mediasize < 0)
493 err(4, "cannot get disk geometry");
495 if (mediasize > (off_t)0xffffffff * secsize)
497 "disks with more than 2^32-1 sectors are not supported");
498 (void)lseek(f, (off_t)0, SEEK_SET);
499 nbytes = read(f, bootarea, BBSIZE);
501 err(4, "%s read", specname);
502 if (nbytes != BBSIZE)
503 errx(4, "couldn't read %d bytes from %s", BBSIZE, specname);
505 error = bsd_disklabel_le_dec(
506 bootarea + (labeloffset + labelsoffset * secsize),
507 &lab, MAXPARTITIONS);
509 errx(1, "%s: no valid label found", specname);
515 * Compensate for absolute block addressing by finding the
516 * smallest partition offset and if the offset of the 'c'
517 * partition is equal to that, subtract it from all offsets.
520 for (i = 0; i < lab.d_npartitions; i++) {
521 if (lab.d_partitions[i].p_size)
522 lba = MIN(lba, lab.d_partitions[i].p_offset);
524 if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
525 for (i = 0; i < lab.d_npartitions; i++) {
526 if (lab.d_partitions[i].p_size)
527 lab.d_partitions[i].p_offset -= lba;
530 * Save the offset so that we can write the label
531 * back with absolute block addresses.
540 display(FILE *f, const struct disklabel *lp)
543 const struct partition *pp;
548 fprintf(f, "# %s:\n", specname);
550 if (lp->d_type < DKMAXTYPES)
551 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
553 fprintf(f, "type: %u\n", lp->d_type);
554 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
556 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
558 fprintf(f, "flags:");
559 if (lp->d_flags & D_REMOVABLE)
560 fprintf(f, " removeable");
561 if (lp->d_flags & D_ECC)
563 if (lp->d_flags & D_BADSECT)
564 fprintf(f, " badsect");
566 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
567 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
568 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
569 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
570 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
571 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
572 fprintf(f, "rpm: %u\n", lp->d_rpm);
573 fprintf(f, "interleave: %u\n", lp->d_interleave);
574 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
575 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
576 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
577 (u_long)lp->d_headswitch);
578 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
579 (u_long)lp->d_trkseek);
580 fprintf(f, "drivedata: ");
581 for (i = NDDATA - 1; i >= 0; i--)
582 if (lp->d_drivedata[i])
586 for (j = 0; j <= i; j++)
587 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
590 fprintf(f, "%u partitions:\n", lp->d_npartitions);
592 "# size offset fstype [fsize bsize bps/cpg]\n");
593 pp = lp->d_partitions;
594 for (i = 0; i < lp->d_npartitions; i++, pp++) {
596 fprintf(f, " %c: %10lu %10lu ", 'a' + i,
597 (u_long)pp->p_size, (u_long)pp->p_offset);
598 if (pp->p_fstype < FSMAXTYPES)
599 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
601 fprintf(f, "%8d", pp->p_fstype);
602 switch (pp->p_fstype) {
604 case FS_UNUSED: /* XXX */
605 fprintf(f, " %5lu %5lu %2s",
607 (u_long)(pp->p_fsize * pp->p_frag), "");
611 fprintf(f, " %5lu %5lu %5u",
613 (u_long)(pp->p_fsize * pp->p_frag),
618 fprintf(f, " %5lu %5lu %5d",
620 (u_long)(pp->p_fsize * pp->p_frag),
625 fprintf(f, "%20.20s", "");
629 fprintf(f, " # \"raw\" part, don't edit");
641 struct disklabel label;
644 if ((fd = mkstemp(tmpfil)) == -1 ||
645 (fp = fdopen(fd, "w")) == NULL) {
646 warnx("can't create %s", tmpfil);
654 fp = fopen(tmpfil, "r");
656 warnx("can't reopen %s for reading", tmpfil);
659 bzero((char *)&label, sizeof(label));
660 c = getasciilabel(fp, &label);
664 if (writelabel() == 0) {
665 (void) unlink(tmpfil);
669 printf("re-edit the label? [y]: ");
672 if (c != EOF && c != (int)'\n')
673 while (getchar() != (int)'\n')
678 (void) unlink(tmpfil);
691 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
692 while ((pid = fork()) < 0) {
693 if (errno == EPROCLIM) {
694 warnx("you have too many processes");
697 if (errno != EAGAIN) {
706 if (setresgid(gid, gid, gid) == -1)
709 if (setresuid(uid, uid, uid) == -1)
711 if ((ed = getenv("EDITOR")) == (char *)0)
713 execlp(ed, ed, tmpfil, (char *)0);
716 while ((xpid = wait(&locstat)) >= 0)
727 while (*cp != '\0' && isspace(*cp))
729 if (*cp == '\0' || *cp == '#')
739 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
741 if ((c = *cp) != '\0') {
750 * Read an ascii label in from fd f,
751 * in the same format as that put out by display(),
755 getasciilabel(FILE *f, struct disklabel *lp)
760 char *tp, line[BUFSIZ];
762 int lineno = 0, errors = 0;
765 makelabel("auto", lp);
766 bzero(&part_set, sizeof(part_set));
767 bzero(&part_size_type, sizeof(part_size_type));
768 bzero(&part_offset_type, sizeof(part_offset_type));
769 lp->d_bbsize = BBSIZE; /* XXX */
770 lp->d_sbsize = 0; /* XXX */
771 while (fgets(line, sizeof(line) - 1, f)) {
773 if ((cp = index(line,'\n')) != 0)
780 fprintf(stderr, "line %d: syntax error\n", lineno);
784 *tp++ = '\0', tp = skip(tp);
785 if (!strcmp(cp, "type")) {
789 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
790 if (*cpp && !strcmp(*cpp, tp)) {
791 lp->d_type = cpp - dktypenames;
794 if (cpp < &dktypenames[DKMAXTYPES])
797 v = strtoul(tp, &endp, 10);
798 if (errno != 0 || *endp != '\0')
801 fprintf(stderr, "line %d:%s %lu\n", lineno,
802 "Warning, unknown disk type", v);
807 if (!strcmp(cp, "flags")) {
808 for (v = 0; (cp = tp) && *cp != '\0';) {
810 if (!strcmp(cp, "removeable"))
812 else if (!strcmp(cp, "ecc"))
814 else if (!strcmp(cp, "badsect"))
818 "line %d: %s: bad flag\n",
826 if (!strcmp(cp, "drivedata")) {
827 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
828 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
833 if (sscanf(cp, "%lu partitions", &v) == 1) {
834 if (v == 0 || v > MAXPARTITIONS) {
836 "line %d: bad # of partitions\n", lineno);
837 lp->d_npartitions = MAXPARTITIONS;
840 lp->d_npartitions = v;
845 if (!strcmp(cp, "disk")) {
846 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
849 if (!strcmp(cp, "label")) {
850 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
853 if (!strcmp(cp, "bytes/sector")) {
854 v = strtoul(tp, NULL, 10);
855 if (v == 0 || (v % DEV_BSIZE) != 0) {
857 "line %d: %s: bad sector size\n",
864 if (!strcmp(cp, "sectors/track")) {
865 v = strtoul(tp, NULL, 10);
866 #if (ULONG_MAX != 0xffffffffUL)
867 if (v == 0 || v > 0xffffffff)
872 fprintf(stderr, "line %d: %s: bad %s\n",
879 if (!strcmp(cp, "sectors/cylinder")) {
880 v = strtoul(tp, NULL, 10);
882 fprintf(stderr, "line %d: %s: bad %s\n",
889 if (!strcmp(cp, "tracks/cylinder")) {
890 v = strtoul(tp, NULL, 10);
892 fprintf(stderr, "line %d: %s: bad %s\n",
899 if (!strcmp(cp, "cylinders")) {
900 v = strtoul(tp, NULL, 10);
902 fprintf(stderr, "line %d: %s: bad %s\n",
906 lp->d_ncylinders = v;
909 if (!strcmp(cp, "sectors/unit")) {
910 v = strtoul(tp, NULL, 10);
912 fprintf(stderr, "line %d: %s: bad %s\n",
916 lp->d_secperunit = v;
919 if (!strcmp(cp, "rpm")) {
920 v = strtoul(tp, NULL, 10);
921 if (v == 0 || v > USHRT_MAX) {
922 fprintf(stderr, "line %d: %s: bad %s\n",
929 if (!strcmp(cp, "interleave")) {
930 v = strtoul(tp, NULL, 10);
931 if (v == 0 || v > USHRT_MAX) {
932 fprintf(stderr, "line %d: %s: bad %s\n",
936 lp->d_interleave = v;
939 if (!strcmp(cp, "trackskew")) {
940 v = strtoul(tp, NULL, 10);
942 fprintf(stderr, "line %d: %s: bad %s\n",
949 if (!strcmp(cp, "cylinderskew")) {
950 v = strtoul(tp, NULL, 10);
952 fprintf(stderr, "line %d: %s: bad %s\n",
959 if (!strcmp(cp, "headswitch")) {
960 v = strtoul(tp, NULL, 10);
961 lp->d_headswitch = v;
964 if (!strcmp(cp, "track-to-track seek")) {
965 v = strtoul(tp, NULL, 10);
969 /* the ':' was removed above */
970 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
972 "line %d: %s: Unknown disklabel field\n", lineno,
978 /* Process a partition specification line. */
980 if (part >= lp->d_npartitions) {
982 "line %d: partition name out of range a-%c: %s\n",
983 lineno, 'a' + lp->d_npartitions - 1, cp);
989 if (getasciipartspec(tp, lp, part, lineno) != 0) {
994 errors += checklabel(lp);
995 return (errors == 0);
998 #define NXTNUM(n) do { \
1000 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1003 cp = tp, tp = word(cp); \
1004 (n) = strtoul(cp, NULL, 10); \
1008 /* retain 1 character following number */
1009 #define NXTWORD(w,n) do { \
1011 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1015 cp = tp, tp = word(cp); \
1016 (n) = strtoul(cp, &tmp, 10); \
1017 if (tmp) (w) = *tmp; \
1022 * Read a partition line into partition `part' in the specified disklabel.
1023 * Return 0 on success, 1 on failure.
1026 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
1028 struct partition *pp;
1033 pp = &lp->d_partitions[part];
1037 NXTWORD(part_size_type[part],v);
1038 if (v == 0 && part_size_type[part] != '*') {
1040 "line %d: %s: bad partition size\n", lineno, cp);
1046 NXTWORD(part_offset_type[part],v);
1047 if (v == 0 && part_offset_type[part] != '*' &&
1048 part_offset_type[part] != '\0') {
1050 "line %d: %s: bad partition offset\n", lineno, cp);
1055 fprintf(stderr, "line %d: missing file system type\n", lineno);
1058 cp = tp, tp = word(cp);
1059 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1060 if (*cpp && !strcmp(*cpp, cp))
1063 pp->p_fstype = cpp - fstypenames;
1067 v = strtoul(cp, &endp, 10);
1068 if (errno != 0 || *endp != '\0')
1072 if (v >= FSMAXTYPES) {
1074 "line %d: Warning, unknown file system type %s\n",
1081 switch (pp->p_fstype) {
1085 /* accept defaults for fsize/frag/cpg */
1087 NXTNUM(pp->p_fsize);
1088 if (pp->p_fsize == 0)
1091 pp->p_frag = v / pp->p_fsize;
1095 /* else default to 0's */
1104 * Check disklabel for errors and fill in
1105 * derived fields according to supplied values.
1108 checklabel(struct disklabel *lp)
1110 struct partition *pp;
1113 u_long base_offset, needed, total_size, total_percent, current_offset;
1115 int seen_default_offset;
1118 struct partition *pp2;
1125 if (lp->d_secsize == 0) {
1126 fprintf(stderr, "sector size 0\n");
1129 if (lp->d_nsectors == 0) {
1130 fprintf(stderr, "sectors/track 0\n");
1133 if (lp->d_ntracks == 0) {
1134 fprintf(stderr, "tracks/cylinder 0\n");
1137 if (lp->d_ncylinders == 0) {
1138 fprintf(stderr, "cylinders/unit 0\n");
1142 warnx("revolutions/minute 0");
1143 if (lp->d_secpercyl == 0)
1144 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1145 if (lp->d_secperunit == 0)
1146 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1147 if (lp->d_bbsize == 0) {
1148 fprintf(stderr, "boot block size 0\n");
1150 } else if (lp->d_bbsize % lp->d_secsize)
1151 warnx("boot block size %% sector-size != 0");
1152 if (lp->d_npartitions > MAXPARTITIONS)
1153 warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
1154 (u_long)lp->d_npartitions, MAXPARTITIONS);
1156 struct disklabel *vl;
1158 vl = getvirginlabel();
1159 lp->d_secsize = vl->d_secsize;
1160 lp->d_nsectors = vl->d_nsectors;
1161 lp->d_ntracks = vl->d_ntracks;
1162 lp->d_ncylinders = vl->d_ncylinders;
1163 lp->d_rpm = vl->d_rpm;
1164 lp->d_interleave = vl->d_interleave;
1165 lp->d_secpercyl = vl->d_secpercyl;
1166 lp->d_secperunit = vl->d_secperunit;
1167 lp->d_bbsize = vl->d_bbsize;
1168 lp->d_npartitions = vl->d_npartitions;
1172 /* first allocate space to the partitions, then offsets */
1173 total_size = 0; /* in sectors */
1174 total_percent = 0; /* in percent */
1176 /* find all fixed partitions */
1177 for (i = 0; i < lp->d_npartitions; i++) {
1178 pp = &lp->d_partitions[i];
1180 if (part_size_type[i] == '*') {
1181 if (i == RAW_PART) {
1182 pp->p_size = lp->d_secperunit;
1185 warnx("Too many '*' partitions (%c and %c)",
1186 hog_part + 'a',i + 'a');
1194 switch (part_size_type[i]) {
1196 total_percent += size;
1217 warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
1218 part_size_type[i], i + 'a');
1221 /* don't count %'s yet */
1222 if (part_size_type[i] != '%') {
1224 * for all not in sectors, convert to
1227 if (part_size_type[i] != '\0') {
1228 if (size % lp->d_secsize != 0)
1229 warnx("partition %c not an integer number of sectors",
1231 size /= lp->d_secsize;
1234 /* else already in sectors */
1242 /* Find out the total free space, excluding the boot block area. */
1243 base_offset = BBSIZE / secsize;
1245 for (i = 0; i < lp->d_npartitions; i++) {
1246 pp = &lp->d_partitions[i];
1247 if (!part_set[i] || i == RAW_PART ||
1248 part_size_type[i] == '%' || part_size_type[i] == '*')
1250 if (pp->p_offset > base_offset)
1251 free_space += pp->p_offset - base_offset;
1252 if (pp->p_offset + pp->p_size > base_offset)
1253 base_offset = pp->p_offset + pp->p_size;
1255 if (base_offset < lp->d_secperunit)
1256 free_space += lp->d_secperunit - base_offset;
1258 /* handle % partitions - note %'s don't need to add up to 100! */
1259 if (total_percent != 0) {
1260 if (total_percent > 100) {
1261 fprintf(stderr,"total percentage %lu is greater than 100\n",
1266 if (free_space > 0) {
1267 for (i = 0; i < lp->d_npartitions; i++) {
1268 pp = &lp->d_partitions[i];
1269 if (part_set[i] && part_size_type[i] == '%') {
1270 /* careful of overflows! and integer roundoff */
1271 pp->p_size = ((double)pp->p_size/100) * free_space;
1272 total_size += pp->p_size;
1274 /* FIX we can lose a sector or so due to roundoff per
1275 partition. A more complex algorithm could avoid that */
1280 "%ld sectors available to give to '*' and '%%' partitions\n",
1283 /* fix? set all % partitions to size 0? */
1286 /* give anything remaining to the hog partition */
1287 if (hog_part != -1) {
1289 * Find the range of offsets usable by '*' partitions around
1290 * the hog partition and how much space they need.
1293 base_offset = BBSIZE / secsize;
1294 for (i = hog_part - 1; i >= 0; i--) {
1295 pp = &lp->d_partitions[i];
1296 if (!part_set[i] || i == RAW_PART)
1298 if (part_offset_type[i] == '*') {
1299 needed += pp->p_size;
1302 base_offset = pp->p_offset + pp->p_size;
1305 current_offset = lp->d_secperunit;
1306 for (i = lp->d_npartitions - 1; i > hog_part; i--) {
1307 pp = &lp->d_partitions[i];
1308 if (!part_set[i] || i == RAW_PART)
1310 if (part_offset_type[i] == '*') {
1311 needed += pp->p_size;
1314 current_offset = pp->p_offset;
1317 if (current_offset - base_offset <= needed) {
1318 fprintf(stderr, "Cannot find space for partition %c\n",
1321 "Need more than %lu sectors between %lu and %lu\n",
1322 needed, base_offset, current_offset);
1324 lp->d_partitions[hog_part].p_size = 0;
1326 lp->d_partitions[hog_part].p_size = current_offset -
1327 base_offset - needed;
1328 total_size += lp->d_partitions[hog_part].p_size;
1332 /* Now set the offsets for each partition */
1333 current_offset = BBSIZE / secsize; /* in sectors */
1334 seen_default_offset = 0;
1335 for (i = 0; i < lp->d_npartitions; i++) {
1337 pp = &lp->d_partitions[i];
1339 if (part_offset_type[i] == '*') {
1340 if (i == RAW_PART) {
1343 pp->p_offset = current_offset;
1344 seen_default_offset = 1;
1347 /* allow them to be out of order for old-style tables */
1348 if (pp->p_offset < current_offset &&
1349 seen_default_offset && i != RAW_PART &&
1350 pp->p_fstype != FS_VINUM) {
1352 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1353 (long)pp->p_offset,i+'a',current_offset);
1355 "Labels with any *'s for offset must be in ascending order by sector\n");
1357 } else if (pp->p_offset != current_offset &&
1358 i != RAW_PART && seen_default_offset) {
1360 * this may give unneeded warnings if
1361 * partitions are out-of-order
1364 "Offset %ld for partition %c doesn't match expected value %ld",
1365 (long)pp->p_offset, i + 'a', current_offset);
1369 current_offset = pp->p_offset + pp->p_size;
1373 for (i = 0; i < lp->d_npartitions; i++) {
1375 pp = &lp->d_partitions[i];
1376 if (pp->p_size == 0 && pp->p_offset != 0)
1377 warnx("partition %c: size 0, but offset %lu",
1378 part, (u_long)pp->p_offset);
1380 if (pp->p_size % lp->d_secpercyl)
1381 warnx("partition %c: size %% cylinder-size != 0",
1383 if (pp->p_offset % lp->d_secpercyl)
1384 warnx("partition %c: offset %% cylinder-size != 0",
1387 if (pp->p_offset > lp->d_secperunit) {
1389 "partition %c: offset past end of unit\n", part);
1392 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1394 "partition %c: partition extends past end of unit\n",
1398 if (i == RAW_PART) {
1399 if (pp->p_fstype != FS_UNUSED)
1400 warnx("partition %c is not marked as unused!",part);
1401 if (pp->p_offset != 0)
1402 warnx("partition %c doesn't start at 0!",part);
1403 if (pp->p_size != lp->d_secperunit)
1404 warnx("partition %c doesn't cover the whole unit!",part);
1406 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1407 (pp->p_size != lp->d_secperunit)) {
1408 warnx("An incorrect partition %c may cause problems for "
1409 "standard system utilities",part);
1413 /* check for overlaps */
1414 /* this will check for all possible overlaps once and only once */
1415 for (j = 0; j < i; j++) {
1416 pp2 = &lp->d_partitions[j];
1417 if (j != RAW_PART && i != RAW_PART &&
1418 pp->p_fstype != FS_VINUM &&
1419 pp2->p_fstype != FS_VINUM &&
1420 part_set[i] && part_set[j]) {
1421 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1422 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1423 pp2->p_offset >= pp->p_offset)) {
1424 fprintf(stderr,"partitions %c and %c overlap!\n",
1431 for (; i < lp->d_npartitions; i++) {
1433 pp = &lp->d_partitions[i];
1434 if (pp->p_size || pp->p_offset)
1435 warnx("unused partition %c: size %d offset %lu",
1436 'a' + i, pp->p_size, (u_long)pp->p_offset);
1442 * When operating on a "virgin" disk, try getting an initial label
1443 * from the associated device driver. This might work for all device
1444 * drivers that are able to fetch some initial device parameters
1445 * without even having access to a (BSD) disklabel, like SCSI disks,
1446 * most IDE drives, or vn devices.
1448 * The device name must be given in its "canonical" form.
1450 static struct disklabel *
1451 getvirginlabel(void)
1453 static struct disklabel loclab;
1454 struct partition *dp;
1458 if ((f = open(specname, O_RDONLY)) == -1) {
1459 warn("cannot open %s", specname);
1466 mediasize = g_mediasize(f);
1467 secsize = g_sectorsize(f);
1468 if (secsize < 0 || mediasize < 0) {
1473 memset(&loclab, 0, sizeof loclab);
1474 loclab.d_magic = DISKMAGIC;
1475 loclab.d_magic2 = DISKMAGIC;
1476 loclab.d_secsize = secsize;
1477 loclab.d_secperunit = mediasize / secsize;
1480 * Nobody in these enligthened days uses the CHS geometry for
1481 * anything, but nontheless try to get it right. If we fail
1482 * to get any good ideas from the device, construct something
1483 * which is IBM-PC friendly.
1485 if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1486 loclab.d_nsectors = u;
1488 loclab.d_nsectors = 63;
1489 if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1490 loclab.d_ntracks = u;
1491 else if (loclab.d_secperunit <= 63*1*1024)
1492 loclab.d_ntracks = 1;
1493 else if (loclab.d_secperunit <= 63*16*1024)
1494 loclab.d_ntracks = 16;
1496 loclab.d_ntracks = 255;
1497 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1498 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1499 loclab.d_npartitions = DEFPARTITIONS;
1501 /* Various (unneeded) compat stuff */
1502 loclab.d_rpm = 3600;
1503 loclab.d_bbsize = BBSIZE;
1504 loclab.d_interleave = 1;
1505 strncpy(loclab.d_typename, "amnesiac",
1506 sizeof(loclab.d_typename));
1508 dp = &loclab.d_partitions[RAW_PART];
1509 dp->p_size = loclab.d_secperunit;
1510 loclab.d_checksum = dkcksum(&loclab);
1520 "%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",
1521 "usage: bsdlabel disk",
1522 "\t\t(to read label)",
1523 " bsdlabel -w [-n] [-m machine] disk [type]",
1524 "\t\t(to write label with existing boot program)",
1525 " bsdlabel -e [-n] [-m machine] disk",
1526 "\t\t(to edit label)",
1527 " bsdlabel -R [-n] [-m machine] disk protofile",
1528 "\t\t(to restore label with existing boot program)",
1529 " bsdlabel -B [-b boot] [-m machine] disk",
1530 "\t\t(to install boot program with existing on-disk label)",
1531 " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1532 "\t\t(to write label and install boot program)",
1533 " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
1534 "\t\t(to restore label and install boot program)"