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 #include <sys/disklabel.h>
79 #include "pathnames.h"
81 static void makelabel(const char *, struct disklabel *);
82 static int writelabel(void);
83 static int readlabel(int flag);
84 static void display(FILE *, const struct disklabel *);
85 static int edit(void);
86 static int editit(void);
87 static void fixlabel(struct disklabel *);
88 static char *skip(char *);
89 static char *word(char *);
90 static int getasciilabel(FILE *, struct disklabel *);
91 static int getasciipartspec(char *, struct disklabel *, int, int);
92 static int checklabel(struct disklabel *);
93 static void usage(void);
94 static struct disklabel *getvirginlabel(void);
96 #define DEFEDITOR _PATH_VI
99 static char *specname;
100 static char tmpfil[] = PATH_TMPFILE;
102 static struct disklabel lab;
103 static u_char bootarea[BBSIZE];
104 static off_t mediasize;
105 static u_int secsize;
106 static char blank[] = "";
107 static char unknown[] = "unknown";
109 #define MAX_PART ('z')
110 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
111 static char part_size_type[MAX_NUM_PARTS];
112 static char part_offset_type[MAX_NUM_PARTS];
113 static int part_set[MAX_NUM_PARTS];
115 static int installboot; /* non-zero if we should install a boot program */
116 static int allfields; /* present all fields in edit */
117 static char const *xxboot; /* primary boot */
119 static off_t mbroffset;
121 #define LABELSECTOR -1
124 #define LABELOFFSET -1
126 static int labelsoffset = LABELSECTOR;
127 static int labeloffset = LABELOFFSET;
128 static int bbsize = BBSIZE;
129 static int alphacksum =
130 #if defined(__alpha__)
137 UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
141 static int disable_write; /* set to disable writing to disk label */
142 static int is_file; /* work on a file (abs. pathname), "-f" opt. */
145 main(int argc, char *argv[])
149 char const *name = 0;
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 } else if (!strcmp(optarg, "alpha")) {
180 errx(1, "Unsupported architecture");
198 * We accept and ignode -r for compatibility with
199 * historically disklabel usage.
216 if (labelsoffset < 0 || labeloffset < 0)
217 errx(1, "a -m <architecture> option must be specified");
219 /* Figure out the names of the thing we're working on */
221 dkname = specname = argv[0];
222 } else if (argv[0][0] != '/') {
224 asprintf(&specname, "%s%s", _PATH_DEV, argv[0]);
225 } else if (strncmp(argv[0], _PATH_DEV, strlen(_PATH_DEV)) == 0) {
226 dkname = argv[0] + strlen(_PATH_DEV);
229 dkname = strrchr(argv[0], '/');
234 if (installboot && op == UNSPEC)
236 else if (op == UNSPEC)
256 display(stdout, NULL);
257 error = checklabel(NULL);
263 if (!(t = fopen(argv[1], "r")))
264 err(4, "fopen %s", argv[1]);
266 if (!getasciilabel(t, &lab))
268 error = writelabel();
279 makelabel(name, &lab);
281 if (checklabel(NULL) == 0)
282 error = writelabel();
290 makelabel(argv[1], &lab);
291 if (checklabel(NULL) == 0)
292 error = writelabel();
299 fixlabel(struct disklabel *lp)
301 struct partition *dp;
304 for (i = 0; i < MAXPARTITIONS; i++) {
307 if (lp->d_partitions[i].p_size)
311 dp = &lp->d_partitions[0];
312 dp->p_offset = BBSIZE / secsize;
313 dp->p_size = lp->d_secperunit - dp->p_offset;
317 * Construct a prototype disklabel from /etc/disktab.
320 makelabel(const char *type, struct disklabel *lp)
322 struct disklabel *dp;
324 if (strcmp(type, "auto") == 0)
325 dp = getvirginlabel();
327 dp = getdiskbyname(type);
329 errx(1, "%s: unknown disk type", type);
331 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 (alphacksum && st.st_size <= BBSIZE - 512) {
348 i = read(fd, bootarea + 512, st.st_size);
350 err(1, "read error %s", xxboot);
353 * Set the location and length so SRM can find the
356 p = (uint64_t *)bootarea;
357 p[60] = (st.st_size + secsize - 1) / secsize;
361 } else if ((!alphacksum) && st.st_size <= BBSIZE) {
362 i = read(fd, bootarea, st.st_size);
364 err(1, "read error %s", xxboot);
367 errx(1, "boot code %s is wrong size", xxboot);
375 struct gctl_req *grq;
377 struct disklabel *lp = &lab;
380 warnx("write to disk label supressed - label was as follows:");
381 display(stdout, NULL);
385 lp->d_magic = DISKMAGIC;
386 lp->d_magic2 = DISKMAGIC;
388 lp->d_checksum = dkcksum(lp);
391 for (i = 0; i < lab.d_npartitions; i++)
392 if (lab.d_partitions[i].p_size)
393 lab.d_partitions[i].p_offset += mbroffset;
394 bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * secsize,
397 /* Generate the bootblock checksum for the SRM console. */
398 for (p = (uint64_t *)bootarea, i = 0, sum = 0; i < 63; i++)
403 fd = open(specname, O_RDWR);
406 warn("cannot open file %s for writing label", specname);
409 grq = gctl_get_handle();
410 gctl_ro_param(grq, "verb", -1, "write label");
411 gctl_ro_param(grq, "class", -1, "BSD");
412 gctl_ro_param(grq, "geom", -1, dkname);
413 gctl_ro_param(grq, "label", 148+16*8,
414 bootarea + labeloffset + labelsoffset * secsize);
415 errstr = gctl_issue(grq);
416 if (errstr != NULL) {
423 grq = gctl_get_handle();
424 gctl_ro_param(grq, "verb", -1, "write bootcode");
425 gctl_ro_param(grq, "class", -1, "BSD");
426 gctl_ro_param(grq, "geom", -1, dkname);
427 gctl_ro_param(grq, "bootcode", BBSIZE, bootarea);
428 errstr = gctl_issue(grq);
429 if (errstr != NULL) {
437 if (write(fd, bootarea, bbsize) != bbsize) {
438 warn("write %s", specname);
448 get_file_parms(int f)
453 if (fstat(f, &sb) != 0)
454 err(4, "fstat failed");
455 i = sb.st_mode & S_IFMT;
456 if (i != S_IFREG && i != S_IFLNK)
457 errx(4, "%s is not a valid file or link", specname);
459 mediasize = sb.st_size;
463 * Fetch disklabel for disk.
464 * Use ioctl to get label unless -r flag is given.
471 struct gctl_req *grq;
474 f = open(specname, O_RDONLY);
479 else if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
480 (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
481 err(4, "cannot get disk geometry");
483 if (mediasize > (off_t)0xffffffff * secsize)
485 "disks with more than 2^32-1 sectors are not supported");
486 (void)lseek(f, (off_t)0, SEEK_SET);
487 if (read(f, bootarea, BBSIZE) != BBSIZE)
488 err(4, "%s read", specname);
490 error = bsd_disklabel_le_dec(
491 bootarea + (labeloffset + labelsoffset * secsize),
492 &lab, MAXPARTITIONS);
494 errx(1, "%s: no valid label found", specname);
496 grq = gctl_get_handle();
497 gctl_ro_param(grq, "verb", -1, "read mbroffset");
498 gctl_ro_param(grq, "class", -1, "BSD");
499 gctl_ro_param(grq, "geom", -1, dkname);
500 gctl_rw_param(grq, "mbroffset", sizeof(mbroffset), &mbroffset);
501 errstr = gctl_issue(grq);
502 if (errstr != NULL) {
507 mbroffset /= lab.d_secsize;
508 if (lab.d_partitions[RAW_PART].p_offset == mbroffset)
509 for (i = 0; i < lab.d_npartitions; i++)
510 if (lab.d_partitions[i].p_size)
511 lab.d_partitions[i].p_offset -= mbroffset;
517 display(FILE *f, const struct disklabel *lp)
520 const struct partition *pp;
525 fprintf(f, "# %s:\n", specname);
527 if (lp->d_type < DKMAXTYPES)
528 fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
530 fprintf(f, "type: %u\n", lp->d_type);
531 fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
533 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
535 fprintf(f, "flags:");
536 if (lp->d_flags & D_REMOVABLE)
537 fprintf(f, " removeable");
538 if (lp->d_flags & D_ECC)
540 if (lp->d_flags & D_BADSECT)
541 fprintf(f, " badsect");
543 fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
544 fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
545 fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
546 fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
547 fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
548 fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
549 fprintf(f, "rpm: %u\n", lp->d_rpm);
550 fprintf(f, "interleave: %u\n", lp->d_interleave);
551 fprintf(f, "trackskew: %u\n", lp->d_trackskew);
552 fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
553 fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
554 (u_long)lp->d_headswitch);
555 fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
556 (u_long)lp->d_trkseek);
557 fprintf(f, "drivedata: ");
558 for (i = NDDATA - 1; i >= 0; i--)
559 if (lp->d_drivedata[i])
563 for (j = 0; j <= i; j++)
564 fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
567 fprintf(f, "%u partitions:\n", lp->d_npartitions);
569 "# size offset fstype [fsize bsize bps/cpg]\n");
570 pp = lp->d_partitions;
571 for (i = 0; i < lp->d_npartitions; i++, pp++) {
573 fprintf(f, " %c: %8lu %8lu ", 'a' + i,
574 (u_long)pp->p_size, (u_long)pp->p_offset);
575 if (pp->p_fstype < FSMAXTYPES)
576 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
578 fprintf(f, "%8d", pp->p_fstype);
579 switch (pp->p_fstype) {
581 case FS_UNUSED: /* XXX */
582 fprintf(f, " %5lu %5lu %5.5s ",
584 (u_long)(pp->p_fsize * pp->p_frag), "");
588 fprintf(f, " %5lu %5lu %5u ",
590 (u_long)(pp->p_fsize * pp->p_frag),
595 fprintf(f, " %5lu %5lu %5d",
597 (u_long)(pp->p_fsize * pp->p_frag),
602 fprintf(f, "%20.20s", "");
606 fprintf(f, " # \"raw\" part, don't edit");
618 struct disklabel label;
621 if ((fd = mkstemp(tmpfil)) == -1 ||
622 (fp = fdopen(fd, "w")) == NULL) {
623 warnx("can't create %s", tmpfil);
631 fp = fopen(tmpfil, "r");
633 warnx("can't reopen %s for reading", tmpfil);
636 bzero((char *)&label, sizeof(label));
637 c = getasciilabel(fp, &label);
641 if (writelabel() == 0) {
642 (void) unlink(tmpfil);
646 printf("re-edit the label? [y]: ");
649 if (c != EOF && c != (int)'\n')
650 while (getchar() != (int)'\n')
655 (void) unlink(tmpfil);
666 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
667 while ((pid = fork()) < 0) {
668 if (errno == EPROCLIM) {
669 warnx("you have too many processes");
672 if (errno != EAGAIN) {
682 if ((ed = getenv("EDITOR")) == (char *)0)
684 execlp(ed, ed, tmpfil, (char *)0);
687 while ((xpid = wait(&locstat)) >= 0)
698 while (*cp != '\0' && isspace(*cp))
700 if (*cp == '\0' || *cp == '#')
710 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
712 if ((c = *cp) != '\0') {
721 * Read an ascii label in from fd f,
722 * in the same format as that put out by display(),
726 getasciilabel(FILE *f, struct disklabel *lp)
731 char *tp, line[BUFSIZ];
733 int lineno = 0, errors = 0;
736 makelabel("auto", lp);
737 bzero(&part_set, sizeof(part_set));
738 bzero(&part_size_type, sizeof(part_size_type));
739 bzero(&part_offset_type, sizeof(part_offset_type));
740 lp->d_bbsize = BBSIZE; /* XXX */
741 lp->d_sbsize = 0; /* XXX */
742 while (fgets(line, sizeof(line) - 1, f)) {
744 if ((cp = index(line,'\n')) != 0)
751 fprintf(stderr, "line %d: syntax error\n", lineno);
755 *tp++ = '\0', tp = skip(tp);
756 if (!strcmp(cp, "type")) {
760 for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
761 if (*cpp && !strcmp(*cpp, tp)) {
762 lp->d_type = cpp - dktypenames;
765 if (cpp < &dktypenames[DKMAXTYPES])
767 v = strtoul(tp, NULL, 10);
769 fprintf(stderr, "line %d:%s %lu\n", lineno,
770 "Warning, unknown disk type", v);
774 if (!strcmp(cp, "flags")) {
775 for (v = 0; (cp = tp) && *cp != '\0';) {
777 if (!strcmp(cp, "removeable"))
779 else if (!strcmp(cp, "ecc"))
781 else if (!strcmp(cp, "badsect"))
785 "line %d: %s: bad flag\n",
793 if (!strcmp(cp, "drivedata")) {
794 for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
795 lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
800 if (sscanf(cp, "%lu partitions", &v) == 1) {
801 if (v == 0 || v > MAXPARTITIONS) {
803 "line %d: bad # of partitions\n", lineno);
804 lp->d_npartitions = MAXPARTITIONS;
807 lp->d_npartitions = v;
812 if (!strcmp(cp, "disk")) {
813 strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
816 if (!strcmp(cp, "label")) {
817 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
820 if (!strcmp(cp, "bytes/sector")) {
821 v = strtoul(tp, NULL, 10);
822 if (v == 0 || (v % DEV_BSIZE) != 0) {
824 "line %d: %s: bad sector size\n",
831 if (!strcmp(cp, "sectors/track")) {
832 v = strtoul(tp, NULL, 10);
833 #if (ULONG_MAX != 0xffffffffUL)
834 if (v == 0 || v > 0xffffffff)
839 fprintf(stderr, "line %d: %s: bad %s\n",
846 if (!strcmp(cp, "sectors/cylinder")) {
847 v = strtoul(tp, NULL, 10);
849 fprintf(stderr, "line %d: %s: bad %s\n",
856 if (!strcmp(cp, "tracks/cylinder")) {
857 v = strtoul(tp, NULL, 10);
859 fprintf(stderr, "line %d: %s: bad %s\n",
866 if (!strcmp(cp, "cylinders")) {
867 v = strtoul(tp, NULL, 10);
869 fprintf(stderr, "line %d: %s: bad %s\n",
873 lp->d_ncylinders = v;
876 if (!strcmp(cp, "sectors/unit")) {
877 v = strtoul(tp, NULL, 10);
879 fprintf(stderr, "line %d: %s: bad %s\n",
883 lp->d_secperunit = v;
886 if (!strcmp(cp, "rpm")) {
887 v = strtoul(tp, NULL, 10);
888 if (v == 0 || v > USHRT_MAX) {
889 fprintf(stderr, "line %d: %s: bad %s\n",
896 if (!strcmp(cp, "interleave")) {
897 v = strtoul(tp, NULL, 10);
898 if (v == 0 || v > USHRT_MAX) {
899 fprintf(stderr, "line %d: %s: bad %s\n",
903 lp->d_interleave = v;
906 if (!strcmp(cp, "trackskew")) {
907 v = strtoul(tp, NULL, 10);
909 fprintf(stderr, "line %d: %s: bad %s\n",
916 if (!strcmp(cp, "cylinderskew")) {
917 v = strtoul(tp, NULL, 10);
919 fprintf(stderr, "line %d: %s: bad %s\n",
926 if (!strcmp(cp, "headswitch")) {
927 v = strtoul(tp, NULL, 10);
928 lp->d_headswitch = v;
931 if (!strcmp(cp, "track-to-track seek")) {
932 v = strtoul(tp, NULL, 10);
936 /* the ':' was removed above */
937 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
939 "line %d: %s: Unknown disklabel field\n", lineno,
945 /* Process a partition specification line. */
947 if (part >= lp->d_npartitions) {
949 "line %d: partition name out of range a-%c: %s\n",
950 lineno, 'a' + lp->d_npartitions - 1, cp);
956 if (getasciipartspec(tp, lp, part, lineno) != 0) {
961 errors += checklabel(lp);
962 return (errors == 0);
965 #define NXTNUM(n) do { \
967 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
970 cp = tp, tp = word(cp); \
971 (n) = strtoul(cp, NULL, 10); \
975 /* retain 1 character following number */
976 #define NXTWORD(w,n) do { \
978 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
982 cp = tp, tp = word(cp); \
983 (n) = strtoul(cp, &tmp, 10); \
984 if (tmp) (w) = *tmp; \
989 * Read a partition line into partition `part' in the specified disklabel.
990 * Return 0 on success, 1 on failure.
993 getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
995 struct partition *pp;
1000 pp = &lp->d_partitions[part];
1004 NXTWORD(part_size_type[part],v);
1005 if (v == 0 && part_size_type[part] != '*') {
1007 "line %d: %s: bad partition size\n", lineno, cp);
1013 NXTWORD(part_offset_type[part],v);
1014 if (v == 0 && part_offset_type[part] != '*' &&
1015 part_offset_type[part] != '\0') {
1017 "line %d: %s: bad partition offset\n", lineno, cp);
1022 fprintf(stderr, "line %d: missing file system type\n", lineno);
1025 cp = tp, tp = word(cp);
1026 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1027 if (*cpp && !strcmp(*cpp, cp))
1030 pp->p_fstype = cpp - fstypenames;
1033 v = strtoul(cp, NULL, 10);
1036 if (v >= FSMAXTYPES) {
1038 "line %d: Warning, unknown file system type %s\n",
1045 switch (pp->p_fstype) {
1049 /* accept defaults for fsize/frag/cpg */
1051 NXTNUM(pp->p_fsize);
1052 if (pp->p_fsize == 0)
1055 pp->p_frag = v / pp->p_fsize;
1059 /* else default to 0's */
1068 * Check disklabel for errors and fill in
1069 * derived fields according to supplied values.
1072 checklabel(struct disklabel *lp)
1074 struct partition *pp;
1077 u_long base_offset, needed, total_size, total_percent, current_offset;
1079 int seen_default_offset;
1082 struct partition *pp2;
1089 if (lp->d_secsize == 0) {
1090 fprintf(stderr, "sector size 0\n");
1093 if (lp->d_nsectors == 0) {
1094 fprintf(stderr, "sectors/track 0\n");
1097 if (lp->d_ntracks == 0) {
1098 fprintf(stderr, "tracks/cylinder 0\n");
1101 if (lp->d_ncylinders == 0) {
1102 fprintf(stderr, "cylinders/unit 0\n");
1106 warnx("revolutions/minute 0");
1107 if (lp->d_secpercyl == 0)
1108 lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
1109 if (lp->d_secperunit == 0)
1110 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
1111 if (lp->d_bbsize == 0) {
1112 fprintf(stderr, "boot block size 0\n");
1114 } else if (lp->d_bbsize % lp->d_secsize)
1115 warnx("boot block size %% sector-size != 0");
1116 if (lp->d_npartitions > MAXPARTITIONS)
1117 warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
1118 (u_long)lp->d_npartitions, MAXPARTITIONS);
1120 struct disklabel *vl;
1122 vl = getvirginlabel();
1123 lp->d_secsize = vl->d_secsize;
1124 lp->d_nsectors = vl->d_nsectors;
1125 lp->d_ntracks = vl->d_ntracks;
1126 lp->d_ncylinders = vl->d_ncylinders;
1127 lp->d_rpm = vl->d_rpm;
1128 lp->d_interleave = vl->d_interleave;
1129 lp->d_secpercyl = vl->d_secpercyl;
1130 lp->d_secperunit = vl->d_secperunit;
1131 lp->d_bbsize = vl->d_bbsize;
1132 lp->d_npartitions = vl->d_npartitions;
1136 /* first allocate space to the partitions, then offsets */
1137 total_size = 0; /* in sectors */
1138 total_percent = 0; /* in percent */
1140 /* find all fixed partitions */
1141 for (i = 0; i < lp->d_npartitions; i++) {
1142 pp = &lp->d_partitions[i];
1144 if (part_size_type[i] == '*') {
1145 if (i == RAW_PART) {
1146 pp->p_size = lp->d_secperunit;
1149 warnx("Too many '*' partitions (%c and %c)",
1150 hog_part + 'a',i + 'a');
1158 switch (part_size_type[i]) {
1160 total_percent += size;
1181 warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
1182 part_size_type[i], i + 'a');
1185 /* don't count %'s yet */
1186 if (part_size_type[i] != '%') {
1188 * for all not in sectors, convert to
1191 if (part_size_type[i] != '\0') {
1192 if (size % lp->d_secsize != 0)
1193 warnx("partition %c not an integer number of sectors",
1195 size /= lp->d_secsize;
1198 /* else already in sectors */
1206 /* Find out the total free space, excluding the boot block area. */
1207 base_offset = BBSIZE / secsize;
1209 for (i = 0; i < lp->d_npartitions; i++) {
1210 pp = &lp->d_partitions[i];
1211 if (!part_set[i] || i == RAW_PART ||
1212 part_size_type[i] == '%' || part_size_type[i] == '*')
1214 if (pp->p_offset > base_offset)
1215 free_space += pp->p_offset - base_offset;
1216 if (pp->p_offset + pp->p_size > base_offset)
1217 base_offset = pp->p_offset + pp->p_size;
1219 if (base_offset < lp->d_secperunit)
1220 free_space += lp->d_secperunit - base_offset;
1222 /* handle % partitions - note %'s don't need to add up to 100! */
1223 if (total_percent != 0) {
1224 if (total_percent > 100) {
1225 fprintf(stderr,"total percentage %lu is greater than 100\n",
1230 if (free_space > 0) {
1231 for (i = 0; i < lp->d_npartitions; i++) {
1232 pp = &lp->d_partitions[i];
1233 if (part_set[i] && part_size_type[i] == '%') {
1234 /* careful of overflows! and integer roundoff */
1235 pp->p_size = ((double)pp->p_size/100) * free_space;
1236 total_size += pp->p_size;
1238 /* FIX we can lose a sector or so due to roundoff per
1239 partition. A more complex algorithm could avoid that */
1244 "%ld sectors available to give to '*' and '%%' partitions\n",
1247 /* fix? set all % partitions to size 0? */
1250 /* give anything remaining to the hog partition */
1251 if (hog_part != -1) {
1253 * Find the range of offsets usable by '*' partitions around
1254 * the hog partition and how much space they need.
1257 base_offset = BBSIZE / secsize;
1258 for (i = hog_part - 1; i >= 0; i--) {
1259 pp = &lp->d_partitions[i];
1260 if (!part_set[i] || i == RAW_PART)
1262 if (part_offset_type[i] == '*') {
1263 needed += pp->p_size;
1266 base_offset = pp->p_offset + pp->p_size;
1269 current_offset = lp->d_secperunit;
1270 for (i = lp->d_npartitions - 1; i > hog_part; i--) {
1271 pp = &lp->d_partitions[i];
1272 if (!part_set[i] || i == RAW_PART)
1274 if (part_offset_type[i] == '*') {
1275 needed += pp->p_size;
1278 current_offset = pp->p_offset;
1281 if (current_offset - base_offset <= needed) {
1282 fprintf(stderr, "Cannot find space for partition %c\n",
1285 "Need more than %lu sectors between %lu and %lu\n",
1286 needed, base_offset, current_offset);
1288 lp->d_partitions[hog_part].p_size = 0;
1290 lp->d_partitions[hog_part].p_size = current_offset -
1291 base_offset - needed;
1292 total_size += lp->d_partitions[hog_part].p_size;
1296 /* Now set the offsets for each partition */
1297 current_offset = BBSIZE / secsize; /* in sectors */
1298 seen_default_offset = 0;
1299 for (i = 0; i < lp->d_npartitions; i++) {
1301 pp = &lp->d_partitions[i];
1303 if (part_offset_type[i] == '*') {
1304 if (i == RAW_PART) {
1307 pp->p_offset = current_offset;
1308 seen_default_offset = 1;
1311 /* allow them to be out of order for old-style tables */
1312 if (pp->p_offset < current_offset &&
1313 seen_default_offset && i != RAW_PART &&
1314 pp->p_fstype != FS_VINUM) {
1316 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1317 (long)pp->p_offset,i+'a',current_offset);
1319 "Labels with any *'s for offset must be in ascending order by sector\n");
1321 } else if (pp->p_offset != current_offset &&
1322 i != RAW_PART && seen_default_offset) {
1324 * this may give unneeded warnings if
1325 * partitions are out-of-order
1328 "Offset %ld for partition %c doesn't match expected value %ld",
1329 (long)pp->p_offset, i + 'a', current_offset);
1333 current_offset = pp->p_offset + pp->p_size;
1337 for (i = 0; i < lp->d_npartitions; i++) {
1339 pp = &lp->d_partitions[i];
1340 if (pp->p_size == 0 && pp->p_offset != 0)
1341 warnx("partition %c: size 0, but offset %lu",
1342 part, (u_long)pp->p_offset);
1344 if (pp->p_size % lp->d_secpercyl)
1345 warnx("partition %c: size %% cylinder-size != 0",
1347 if (pp->p_offset % lp->d_secpercyl)
1348 warnx("partition %c: offset %% cylinder-size != 0",
1351 if (pp->p_offset > lp->d_secperunit) {
1353 "partition %c: offset past end of unit\n", part);
1356 if (pp->p_offset + pp->p_size > lp->d_secperunit) {
1358 "partition %c: partition extends past end of unit\n",
1362 if (i == RAW_PART) {
1363 if (pp->p_fstype != FS_UNUSED)
1364 warnx("partition %c is not marked as unused!",part);
1365 if (pp->p_offset != 0)
1366 warnx("partition %c doesn't start at 0!",part);
1367 if (pp->p_size != lp->d_secperunit)
1368 warnx("partition %c doesn't cover the whole unit!",part);
1370 if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
1371 (pp->p_size != lp->d_secperunit)) {
1372 warnx("An incorrect partition %c may cause problems for "
1373 "standard system utilities",part);
1377 /* check for overlaps */
1378 /* this will check for all possible overlaps once and only once */
1379 for (j = 0; j < i; j++) {
1380 pp2 = &lp->d_partitions[j];
1381 if (j != RAW_PART && i != RAW_PART &&
1382 pp->p_fstype != FS_VINUM &&
1383 pp2->p_fstype != FS_VINUM &&
1384 part_set[i] && part_set[j]) {
1385 if (pp2->p_offset < pp->p_offset + pp->p_size &&
1386 (pp2->p_offset + pp2->p_size > pp->p_offset ||
1387 pp2->p_offset >= pp->p_offset)) {
1388 fprintf(stderr,"partitions %c and %c overlap!\n",
1395 for (; i < MAXPARTITIONS; i++) {
1397 pp = &lp->d_partitions[i];
1398 if (pp->p_size || pp->p_offset)
1399 warnx("unused partition %c: size %d offset %lu",
1400 'a' + i, pp->p_size, (u_long)pp->p_offset);
1406 * When operating on a "virgin" disk, try getting an initial label
1407 * from the associated device driver. This might work for all device
1408 * drivers that are able to fetch some initial device parameters
1409 * without even having access to a (BSD) disklabel, like SCSI disks,
1410 * most IDE drives, or vn devices.
1412 * The device name must be given in its "canonical" form.
1414 static struct disklabel *
1415 getvirginlabel(void)
1417 static struct disklabel loclab;
1418 struct partition *dp;
1422 if ((f = open(specname, O_RDONLY)) == -1) {
1423 warn("cannot open %s", specname);
1429 else if ((ioctl(f, DIOCGMEDIASIZE, &mediasize) != 0) ||
1430 (ioctl(f, DIOCGSECTORSIZE, &secsize) != 0)) {
1434 memset(&loclab, 0, sizeof loclab);
1435 loclab.d_magic = DISKMAGIC;
1436 loclab.d_magic2 = DISKMAGIC;
1437 loclab.d_secsize = secsize;
1438 loclab.d_secperunit = mediasize / secsize;
1441 * Nobody in these enligthened days uses the CHS geometry for
1442 * anything, but nontheless try to get it right. If we fail
1443 * to get any good ideas from the device, construct something
1444 * which is IBM-PC friendly.
1446 if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
1447 loclab.d_nsectors = u;
1449 loclab.d_nsectors = 63;
1450 if (ioctl(f, DIOCGFWHEADS, &u) == 0)
1451 loclab.d_ntracks = u;
1452 else if (loclab.d_secperunit <= 63*1*1024)
1453 loclab.d_ntracks = 1;
1454 else if (loclab.d_secperunit <= 63*16*1024)
1455 loclab.d_ntracks = 16;
1457 loclab.d_ntracks = 255;
1458 loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
1459 loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
1460 loclab.d_npartitions = MAXPARTITIONS;
1462 /* Various (unneeded) compat stuff */
1463 loclab.d_rpm = 3600;
1464 loclab.d_bbsize = BBSIZE;
1465 loclab.d_interleave = 1;
1466 strncpy(loclab.d_typename, "amnesiac",
1467 sizeof(loclab.d_typename));
1469 dp = &loclab.d_partitions[RAW_PART];
1470 dp->p_size = loclab.d_secperunit;
1471 loclab.d_checksum = dkcksum(&loclab);
1481 "%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",
1482 "usage: bsdlabel disk",
1483 "\t\t(to read label)",
1484 " bsdlabel -w [-n] [-m machine] disk [type]",
1485 "\t\t(to write label with existing boot program)",
1486 " bsdlabel -e [-n] [-m machine] disk",
1487 "\t\t(to edit label)",
1488 " bsdlabel -R [-n] [-m machine] disk protofile",
1489 "\t\t(to restore label with existing boot program)",
1490 " bsdlabel -B [-b boot] [-m machine] disk",
1491 "\t\t(to install boot program with existing on-disk label)",
1492 " bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
1493 "\t\t(to write label and install boot program)",
1494 " bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
1495 "\t\t(to restore label and install boot program)"