2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sbin/disklabel64/disklabel64.c,v 1.5 2008/04/20 13:44:25 swildner Exp $
37 * Copyright (c) 1987, 1993
38 * The Regents of the University of California. All rights reserved.
40 * This code is derived from software contributed to Berkeley by
41 * Symmetric Computer Systems.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * @(#)disklabel.c 1.2 (Symmetric) 11/28/85
72 * @(#)disklabel.c 8.2 (Berkeley) 1/7/94
73 * $FreeBSD: src/sbin/disklabel/disklabel.c,v 1.28.2.15 2003/01/24 16:18:16 des Exp $
74 * $DragonFly: src/sbin/disklabel64/disklabel64.c,v 1.5 2008/04/20 13:44:25 swildner Exp $
77 #include <sys/param.h>
82 #include <sys/disklabel64.h>
83 #include <sys/diskslice.h>
84 #include <sys/diskmbr.h>
85 #include <sys/dtype.h>
86 #include <sys/sysctl.h>
89 #include <vfs/ufs/dinode.h>
90 #include <vfs/ufs/fs.h>
104 #include "pathnames.h"
106 extern uint32_t crc32(const void *buf, size_t size);
109 * Disklabel: read and write disklabels.
110 * The label is usually placed on one of the first sectors of the disk.
111 * Many machines also place a bootstrap in the same area,
112 * in which case the label is embedded in the bootstrap.
113 * The bootstrap source must leave space at the proper offset
114 * for the label on such machines.
117 #define LABELSIZE ((sizeof(struct disklabel64) + 4095) & ~4095)
118 #define BOOTSIZE 32768
120 /* FIX! These are too low, but are traditional */
121 #define DEFAULT_NEWFS_BLOCK 8192U
122 #define DEFAULT_NEWFS_FRAG 1024U
123 #define DEFAULT_NEWFS_CPG 16U
125 #define BIG_NEWFS_BLOCK 16384U
126 #define BIG_NEWFS_FRAG 2048U
127 #define BIG_NEWFS_CPG 64U
129 void makelabel(const char *, const char *, struct disklabel64 *);
130 int writelabel(int, struct disklabel64 *);
131 void l_perror(const char *);
132 struct disklabel64 *readlabel(int);
133 struct disklabel64 *makebootarea(int);
134 void display(FILE *, const struct disklabel64 *);
135 int edit(struct disklabel64 *, int);
139 int getasciilabel(FILE *, struct disklabel64 *);
140 int getasciipartspec(char *, struct disklabel64 *, int, int, uint32_t);
141 int getasciipartuuid(char *, struct disklabel64 *, int, int, uint32_t);
142 int checklabel(struct disklabel64 *);
143 void Warning(const char *, ...) __printflike(1, 2);
145 struct disklabel64 *getvirginlabel(void);
147 #define DEFEDITOR _PATH_VI
148 #define streq(a,b) (strcmp(a,b) == 0)
152 char tmpfil[] = PATH_TMPFILE;
154 struct disklabel64 lab;
156 #define MAX_PART ('z')
157 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
158 char part_size_type[MAX_NUM_PARTS];
159 char part_offset_type[MAX_NUM_PARTS];
160 int part_set[MAX_NUM_PARTS];
162 int installboot; /* non-zero if we should install a boot program */
172 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
177 int disable_write; /* set to disable writing to disk label */
178 u_int32_t slice_start_lba;
182 #define OPTIONS "BNRWb:denrs:Vw"
184 #define OPTIONS "BNRWb:enrs:Vw"
188 main(int argc, char *argv[])
190 struct disklabel64 *lp;
192 int ch, f = 0, flag, error = 0;
195 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
266 if (dkname[0] != '/') {
267 asprintf(&specname, "%s%s", _PATH_DEV, dkname);
271 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
272 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
273 asprintf(&specname, "%s%s", _PATH_DEV, dkname);
274 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
277 err(4, "%s", specname);
293 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
294 err(4, "ioctl DIOCWLABEL");
302 error = checklabel(lp);
306 if (installboot && argc == 3) {
307 makelabel(argv[2], 0, &lab);
311 * We only called makelabel() for its side effect
312 * of setting the bootstrap file names. Discard
313 * all changes to `lab' so that all values in the
314 * final label come from the ASCII label.
316 bzero((char *)&lab, sizeof(lab));
320 if (!(t = fopen(argv[1], "r")))
321 err(4, "%s", argv[1]);
322 if (!getasciilabel(t, &lab))
324 lp = makebootarea(f);
325 bcopy(&lab.d_magic, &lp->d_magic,
326 sizeof(lab) - offsetof(struct disklabel64, d_magic));
327 error = writelabel(f, lp);
337 makelabel(argv[1], name, &lab);
338 lp = makebootarea(f);
339 bcopy(&lab.d_magic, &lp->d_magic,
340 sizeof(lab) - offsetof(struct disklabel64, d_magic));
341 if (checklabel(lp) == 0)
342 error = writelabel(f, lp);
347 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
348 err(4, "ioctl DIOCWLABEL");
353 struct disklabel64 tlab;
358 makelabel(argv[1], 0, &lab);
359 lp = makebootarea(f);
360 bcopy(&tlab.d_magic, &lp->d_magic,
361 sizeof(tlab) - offsetof(struct disklabel64, d_magic));
362 if (checklabel(lp) == 0)
363 error = writelabel(f, lp);
371 * Construct a prototype disklabel from /etc/disktab. As a side
372 * effect, set the names of the primary and secondary boot files
376 makelabel(const char *type, const char *name, struct disklabel64 *lp)
378 struct disklabel64 *dp;
380 if (strcmp(type, "auto") == 0)
381 dp = getvirginlabel();
385 errx(1, "%s: unknown disk type", type);
389 * NOTE: boot control files may no longer be specified in disktab.
392 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
396 writelabel(int f, struct disklabel64 *lp)
398 struct disklabel64 *blp;
404 lpsize = offsetof(struct disklabel64, d_partitions[lp->d_npartitions]);
405 lpcrcsize = lpsize - offsetof(struct disklabel64, d_magic);
408 Warning("write to disk label suppressed - label was as follows:");
412 lp->d_magic = DISKMAGIC64;
414 lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
417 * Make sure the boot area is not too large
420 int lpbsize = (int)(lp->d_pbase - lp->d_bbase);
421 if (lp->d_pbase == 0) {
422 errx(1, "no space was set aside in "
423 "the disklabel for boot2!");
425 if (boot2size > lpbsize) {
426 errx(1, "label did not reserve enough "
427 "space for boot! %d/%d",
433 * First set the kernel disk label,
434 * then write a label to the raw disk.
435 * If the SDINFO ioctl fails because it is
436 * unimplemented, keep going; otherwise, the kernel
437 * consistency checks may prevent us from changing
438 * the current (in-core) label.
440 if (ioctl(f, DIOCSDINFO64, lp) < 0 &&
441 errno != ENODEV && errno != ENOTTY) {
442 l_perror("ioctl DIOCSDINFO");
445 lseek(f, (off_t)0, SEEK_SET);
448 * The disklabel embeds areas which we may not
449 * have wanted to change. Merge those areas in
452 blp = makebootarea(f);
454 bcopy(&lp->d_magic, &blp->d_magic,
456 offsetof(struct disklabel64, d_magic));
460 * write enable label sector before write
461 * (if necessary), disable after writing.
464 if (ioctl(f, DIOCWLABEL, &flag) < 0)
465 warn("ioctl DIOCWLABEL");
467 r = write(f, boot1buf, boot1lsize);
468 if (r != (ssize_t)boot1lsize) {
473 * Output the remainder of the disklabel
476 lseek(f, lp->d_bbase, 0);
477 r = write(f, boot2buf, boot2size);
478 if (r != boot2size) {
484 ioctl(f, DIOCWLABEL, &flag);
485 } else if (ioctl(f, DIOCWDINFO64, lp) < 0) {
486 l_perror("ioctl DIOCWDINFO64");
494 l_perror(const char *s)
499 warnx("%s: no disk label on disk;", s);
500 fprintf(stderr, "add \"-r\" to install initial label\n");
504 warnx("%s: label magic number or checksum is wrong!", s);
505 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
509 warnx("%s: open partition would move or shrink", s);
513 warnx("%s: the disk already has a label of a different type,\n"
514 "probably a 32 bit disklabel. It must be cleaned out "
525 * Fetch disklabel for disk.
526 * Use ioctl to get label unless -r flag is given.
531 struct disklabel64 *lp;
537 * Allocate space for the label. The boot1 code, if any,
538 * is embedded in the label. The label overlaps the boot1
541 lp = makebootarea(f);
542 lpcrcsize = offsetof(struct disklabel64,
543 d_partitions[lp->d_npartitions]) -
544 offsetof(struct disklabel64, d_magic);
547 if (lp->d_magic != DISKMAGIC64)
548 errx(1, "bad pack magic number");
549 if (lp->d_npartitions > MAXPARTITIONS64 ||
550 savecrc != crc32(&lp->d_magic, lpcrcsize)
552 errx(1, "corrupted disklabel64");
557 * Just use a static structure to hold the label. Note
558 * that DIOCSDINFO64 does not overwrite the boot1 area
559 * even though it is part of the disklabel64 structure.
563 if (ioctl(f, DIOCGDVIRGIN64, lp) < 0) {
564 l_perror("ioctl DIOCGDVIRGIN64");
568 if (ioctl(f, DIOCGDINFO64, lp) < 0) {
569 l_perror("ioctl DIOCGDINFO64");
578 * Construct a boot area for boot1 and boot2 and return the location of
579 * the label within the area. The caller will overwrite the label so
580 * we don't actually have to read it.
585 struct disklabel64 *lp;
586 struct partinfo info;
592 if (ioctl(f, DIOCGPART, &info) == 0)
593 secsize = info.media_blksize;
597 if (boot1buf == NULL) {
600 rsize = (sizeof(struct disklabel64) + secsize - 1) &
602 boot1size = offsetof(struct disklabel64, d_magic);
604 boot1buf = malloc(rsize);
605 bzero(boot1buf, rsize);
606 r = read(f, boot1buf, rsize);
608 err(4, "%s", specname);
610 lp = (void *)boot1buf;
612 if (installboot == 0)
615 if (boot2buf == NULL) {
617 boot2buf = malloc(boot2size);
618 bzero(boot2buf, boot2size);
622 * If installing the boot code, read it into the appropriate portions
625 if (boot1path == NULL)
626 asprintf(&boot1path, "%s/boot1_64", _PATH_BOOTDIR);
627 if (boot2path == NULL)
628 asprintf(&boot2path, "%s/boot2_64", _PATH_BOOTDIR);
630 if ((fd = open(boot1path, O_RDONLY)) < 0)
631 err(4, "%s", boot1path);
632 if (fstat(fd, &st) < 0)
633 err(4, "%s", boot1path);
634 if (st.st_size > boot1size)
635 err(4, "%s must be exactly %d bytes!", boot1path, boot1size);
636 if (read(fd, boot1buf, boot1size) != boot1size)
637 err(4, "%s must be exactly %d bytes!", boot1path, boot1size);
640 if ((fd = open(boot2path, O_RDONLY)) < 0)
641 err(4, "%s", boot2path);
642 if (fstat(fd, &st) < 0)
643 err(4, "%s", boot2path);
644 if (st.st_size > boot2size)
645 err(4, "%s must be <= %d bytes!", boot2path, boot2size);
646 if ((r = read(fd, boot2buf, boot2size)) < 1)
647 err(4, "%s is empty!", boot2path);
648 boot2size = (r + secsize - 1) & ~(secsize - 1);
652 * XXX dangerously dedicated support goes here XXX
658 display(FILE *f, const struct disklabel64 *lp)
660 const struct partition64 *pp;
667 * Use a human readable block size if possible. This is for
668 * display and editing purposes only.
670 if (lp->d_align > 1024)
673 blksize = lp->d_align;
675 fprintf(f, "# %s:\n", specname);
677 fprintf(f, "# Informational fields calculated from the above\n");
678 fprintf(f, "# All byte equivalent offsets must be aligned\n");
680 fprintf(f, "# boot space: %10llu bytes\n", lp->d_pbase - lp->d_bbase);
681 fprintf(f, "# data space: %10llu blocks\t# %6.2f MB (%llu bytes)\n",
682 (lp->d_pstop - lp->d_pbase) / blksize,
683 (double)(lp->d_pstop - lp->d_pbase) / 1024.0 / 1024.0,
684 lp->d_pstop - lp->d_pbase);
687 uuid_to_string(&lp->d_stor_uuid, &str, NULL);
688 fprintf(f, "diskid: %s\n", str ? str : "<unknown>");
691 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
693 fprintf(f, "boot2 data base: 0x%012llx\n", lp->d_bbase);
694 fprintf(f, "partitions data base: 0x%012llx\n", lp->d_pbase);
695 fprintf(f, "partitions data stop: 0x%012llx\n", lp->d_pstop);
696 fprintf(f, "backup label: 0x%012llx\n", lp->d_abase);
697 fprintf(f, "total size: 0x%012llx\t# %6.2f MB\n",
699 (double)lp->d_total_size / 1024.0 / 1024.0);
700 fprintf(f, "alignment: %u\n", lp->d_align);
701 fprintf(f, "display block size: %u\t# for partition display only\n",
705 fprintf(f, "%u partitions:\n", lp->d_npartitions);
706 fprintf(f, "# size offset fstype fsuuid\n");
708 for (part = 0; part < lp->d_npartitions; part++) {
709 pp = &lp->d_partitions[part];
710 const u_long onemeg = 1024 * 1024;
712 if (pp->p_bsize == 0)
715 fprintf(f, " %c: ", 'a' + part);
717 if (pp->p_bsize % lp->d_align)
718 fprintf(f, "%10s ", "ILLEGAL");
720 fprintf(f, "%10llu ", pp->p_bsize / blksize);
721 if (pp->p_boffset % lp->d_align)
722 fprintf(f, "%10s ", "ILLEGAL");
724 fprintf(f, "%10llu ", (pp->p_boffset - lp->d_pbase) / blksize);
725 if (pp->p_fstype < FSMAXTYPES)
726 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
728 fprintf(f, "%8d", pp->p_fstype);
729 fprintf(f, "\t# %11.3fM", (double)pp->p_bsize / onemeg);
732 for (part = 0; part < lp->d_npartitions; part++) {
733 pp = &lp->d_partitions[part];
735 if (pp->p_bsize == 0)
738 if (uuid_is_nil(&lp->d_stor_uuid, NULL) == 0) {
739 fprintf(f, " %c-stor_uuid: ", 'a' + part);
741 uuid_to_string(&pp->p_stor_uuid, &str, NULL);
743 fprintf(f, "%s", str);
750 fprintf(f, "# EXAMPLE\n");
751 fprintf(f, "#a: 4g 0 4.2BSD\n");
752 fprintf(f, "#a: * * 4.2BSD\n");
759 edit(struct disklabel64 *lp, int f)
762 struct disklabel64 label;
765 if ((fd = mkstemp(tmpfil)) == -1 ||
766 (fp = fdopen(fd, "w")) == NULL) {
767 warnx("can't create %s", tmpfil);
775 fp = fopen(tmpfil, "r");
777 warnx("can't reopen %s for reading", tmpfil);
780 bzero((char *)&label, sizeof(label));
781 if (getasciilabel(fp, &label)) {
783 if (writelabel(f, lp) == 0) {
790 printf("re-edit the label? [y]: "); fflush(stdout);
792 if (c != EOF && c != (int)'\n')
793 while (getchar() != (int)'\n')
809 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
810 while ((pid = fork()) < 0) {
811 if (errno == EPROCLIM) {
812 warnx("you have too many processes");
815 if (errno != EAGAIN) {
825 if ((ed = getenv("EDITOR")) == (char *)0)
827 execlp(ed, ed, tmpfil, (char *)0);
830 while ((xpid = wait(&status)) >= 0)
841 while (*cp != '\0' && isspace(*cp))
843 if (*cp == '\0' || *cp == '#')
853 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
855 if ((c = *cp) != '\0') {
864 * Read an ascii label in from fd f,
865 * in the same format as that put out by display(),
869 getasciilabel(FILE *f, struct disklabel64 *lp)
873 char *tp, line[BUFSIZ];
875 uint32_t blksize = 0;
877 int lineno = 0, errors = 0;
880 bzero(&part_set, sizeof(part_set));
881 bzero(&part_size_type, sizeof(part_size_type));
882 bzero(&part_offset_type, sizeof(part_offset_type));
883 while (fgets(line, sizeof(line) - 1, f)) {
885 if ((cp = strchr(line,'\n')) != 0)
890 tp = strchr(cp, ':');
892 fprintf(stderr, "line %d: syntax error\n", lineno);
896 *tp++ = '\0', tp = skip(tp);
897 if (sscanf(cp, "%lu partitions", &v) == 1) {
898 if (v == 0 || v > MAXPARTITIONS64) {
900 "line %d: bad # of partitions\n", lineno);
901 lp->d_npartitions = MAXPARTITIONS64;
904 lp->d_npartitions = v;
910 if (streq(cp, "diskid")) {
912 uuid_from_string(tp, &lp->d_stor_uuid, &status);
913 if (status != uuid_s_ok) {
915 "line %d: %s: illegal UUID\n",
921 if (streq(cp, "label")) {
922 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
926 if (streq(cp, "alignment")) {
927 v = strtoul(tp, NULL, 0);
928 if (v <= 0 || (v & DEV_BMASK) != 0 || v > 1024*1024) {
930 "line %d: %s: bad alignment\n",
938 if (streq(cp, "total size")) {
939 vv = strtoull(tp, NULL, 0);
940 if (vv == 0 || vv == (uint64_t)-1) {
941 fprintf(stderr, "line %d: %s: bad %s\n",
945 lp->d_total_size = vv;
949 if (streq(cp, "boot2 data base")) {
950 vv = strtoull(tp, NULL, 0);
951 if (vv == 0 || vv == (uint64_t)-1) {
952 fprintf(stderr, "line %d: %s: bad %s\n",
960 if (streq(cp, "partitions data base")) {
961 vv = strtoull(tp, NULL, 0);
962 if (vv == 0 || vv == (uint64_t)-1) {
963 fprintf(stderr, "line %d: %s: bad %s\n",
971 if (streq(cp, "partitions data stop")) {
972 vv = strtoull(tp, NULL, 0);
973 if (vv == 0 || vv == (uint64_t)-1) {
974 fprintf(stderr, "line %d: %s: bad %s\n",
982 if (streq(cp, "backup label")) {
983 vv = strtoull(tp, NULL, 0);
984 if (vv == 0 || vv == (uint64_t)-1) {
985 fprintf(stderr, "line %d: %s: bad %s\n",
993 if (streq(cp, "display block size")) {
994 v = strtoul(tp, NULL, 0);
995 if (v <= 0 || (v & DEV_BMASK) != 0 || v > 1024*1024) {
997 "line %d: %s: bad alignment\n",
1006 /* the ':' was removed above */
1009 * Handle main partition data, e.g. a:, b:, etc.
1011 if (*cp < 'a' || *cp > MAX_PART) {
1013 "line %d: %s: Unknown disklabel field\n", lineno,
1019 /* Process a partition specification line. */
1021 if (part >= lp->d_npartitions) {
1023 "line %d: partition name out of range a-%c: %s\n",
1024 lineno, 'a' + lp->d_npartitions - 1, cp);
1030 fprintf(stderr, "block size to use for partition "
1031 "display was not specified!\n");
1036 if (strcmp(cp + 1, "-stor_uuid") == 0) {
1037 if (getasciipartuuid(tp, lp, part, lineno, blksize)) {
1042 } else if (cp[1] == 0) {
1044 if (getasciipartspec(tp, lp, part, lineno, blksize)) {
1050 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1055 errors += checklabel(lp);
1056 return (errors == 0);
1061 parse_field_val(char **tp, char **cp, u_int64_t *vv, int lineno)
1065 if (*tp == NULL || **tp == 0) {
1066 fprintf(stderr, "line %d: too few numeric fields\n", lineno);
1071 *vv = strtoull(*cp, &tmp, 0);
1072 if (*vv == ULLONG_MAX) {
1073 fprintf(stderr, "line %d: illegal number\n", lineno);
1083 * Read a partition line into partition `part' in the specified disklabel.
1084 * Return 0 on success, 1 on failure.
1087 getasciipartspec(char *tp, struct disklabel64 *lp, int part,
1088 int lineno, uint32_t blksize)
1090 struct partition64 *pp;
1098 pp = &lp->d_partitions[part];
1104 r = parse_field_val(&tp, &cp, &vv, lineno);
1130 r = 0; /* eat the suffix */
1133 Warning("unknown size specifier '%c' (*/%%/K/M/G are valid)",
1138 part_size_type[part] = r;
1139 if (vv == 0 && r != '*') {
1141 "line %d: %s: bad partition size (0)\n", lineno, cp);
1144 pp->p_bsize = vv * mpx;
1149 r = parse_field_val(&tp, &cp, &vv, lineno);
1152 part_offset_type[part] = r;
1158 pp->p_boffset = vv * blksize + lp->d_pbase;
1162 "line %d: %s: bad suffix on partition offset (%c)\n",
1172 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1173 if (*cpp && streq(*cpp, cp))
1176 pp->p_fstype = cpp - fstypenames;
1179 v = strtoul(cp, NULL, 0);
1182 if (v >= FSMAXTYPES) {
1184 "line %d: Warning, unknown filesystem type %s\n",
1193 fprintf(stderr, "line %d: Warning, extra data on line\n",
1200 getasciipartuuid(char *tp, struct disklabel64 *lp, int part,
1201 int lineno, uint32_t blksize __unused)
1203 struct partition64 *pp;
1207 pp = &lp->d_partitions[part];
1211 uuid_from_string(cp, &pp->p_stor_uuid, &status);
1212 if (status != uuid_s_ok) {
1213 fprintf(stderr, "line %d: Illegal storage uuid specification\n",
1221 * Check disklabel for errors and fill in
1222 * derived fields according to supplied values.
1225 checklabel(struct disklabel64 *lp)
1227 struct partition64 *pp;
1230 u_int64_t total_size;
1231 u_int64_t current_offset;
1232 u_long total_percent;
1233 int seen_default_offset;
1236 struct partition64 *pp2;
1239 if (lp->d_align < 512 ||
1240 (lp->d_align ^ (lp->d_align - 1)) != lp->d_align * 2 - 1) {
1241 Warning("Illegal alignment specified: %u\n", lp->d_align);
1244 if (lp->d_npartitions > MAXPARTITIONS64) {
1245 Warning("number of partitions (%u) > MAXPARTITIONS (%d)",
1246 lp->d_npartitions, MAXPARTITIONS64);
1249 off = offsetof(struct disklabel64, d_partitions[lp->d_npartitions]);
1250 off = (off + lp->d_align - 1) & ~(int64_t)(lp->d_align - 1);
1252 if (lp->d_bbase < off || lp->d_bbase % lp->d_align) {
1253 Warning("illegal boot2 data base ");
1256 if (lp->d_pbase < lp->d_bbase || lp->d_pbase % lp->d_align) {
1257 Warning("illegal partition data base");
1260 if (lp->d_pstop < lp->d_pbase || lp->d_pstop % lp->d_align) {
1261 Warning("illegal partition data stop");
1264 if (lp->d_pstop > lp->d_total_size) {
1265 printf("%012llx\n%012llx\n", lp->d_pstop, lp->d_total_size);
1266 Warning("disklabel control info is beyond the total size");
1270 (lp->d_abase < lp->d_pstop || lp->d_pstop % lp->d_align ||
1271 lp->d_abase > lp->d_total_size - off)) {
1272 Warning("illegal backup label location");
1276 /* first allocate space to the partitions, then offsets */
1277 total_size = 0; /* in bytes */
1278 total_percent = 0; /* in percent */
1280 /* find all fixed partitions */
1281 for (i = 0; i < (int)lp->d_npartitions; i++) {
1282 pp = &lp->d_partitions[i];
1284 if (part_size_type[i] == '*') {
1285 if (part_offset_type[i] != '*') {
1286 if (total_size < pp->p_boffset)
1287 total_size = pp->p_boffset;
1289 if (hog_part != -1) {
1290 Warning("Too many '*' partitions (%c and %c)",
1291 hog_part + 'a',i + 'a');
1299 if (part_size_type[i] == '%') {
1301 * don't count %'s yet
1303 total_percent += size;
1306 * Value has already been converted
1309 if (size % lp->d_align != 0) {
1310 Warning("partition %c's size is not properly aligned",
1318 /* handle % partitions - note %'s don't need to add up to 100! */
1319 if (total_percent != 0) {
1323 free_space = (int64_t)(lp->d_pstop - lp->d_pbase - total_size);
1324 space_left = free_space;
1325 if (total_percent > 100) {
1326 fprintf(stderr,"total percentage %lu is greater than 100\n",
1331 if (free_space > 0) {
1332 for (i = 0; i < (int)lp->d_npartitions; i++) {
1333 pp = &lp->d_partitions[i];
1334 if (part_set[i] && part_size_type[i] == '%') {
1335 /* careful of overflows! and integer roundoff */
1336 pp->p_bsize = ((double)pp->p_bsize/100) * free_space;
1337 pp->p_bsize = (pp->p_bsize + lp->d_align - 1) & ~(u_int64_t)(lp->d_align - 1);
1338 if ((int64_t)pp->p_bsize > space_left)
1339 pp->p_bsize = (u_int64_t)space_left;
1340 total_size += pp->p_bsize;
1341 space_left -= pp->p_bsize;
1346 "%lld bytes available to give to '*' and '%%' partitions\n",
1349 /* fix? set all % partitions to size 0? */
1352 /* give anything remaining to the hog partition */
1353 if (hog_part != -1) {
1354 lp->d_partitions[hog_part].p_bsize = lp->d_pstop - lp->d_pbase - total_size;
1355 total_size = lp->d_pstop - lp->d_pbase;
1358 /* Now set the offsets for each partition */
1359 current_offset = lp->d_pbase;
1360 seen_default_offset = 0;
1361 for (i = 0; i < (int)lp->d_npartitions; i++) {
1363 pp = &lp->d_partitions[i];
1364 if (pp->p_bsize == 0)
1367 if (part_offset_type[i] == '*') {
1368 pp->p_boffset = current_offset;
1369 seen_default_offset = 1;
1371 /* allow them to be out of order for old-style tables */
1372 if (pp->p_boffset < current_offset &&
1373 seen_default_offset &&
1374 pp->p_fstype != FS_VINUM) {
1376 "Offset 0x%012llx for partition %c overlaps previous partition which ends at 0x%012llx\n",
1377 pp->p_boffset, i + 'a',
1380 "Labels with any *'s for offset must be in ascending order by sector\n");
1382 } else if (pp->p_boffset != current_offset &&
1383 seen_default_offset) {
1385 * this may give unneeded warnings if
1386 * partitions are out-of-order
1389 "Offset 0x%012llx for partition %c doesn't match expected value 0x%012llx",
1390 pp->p_boffset, i + 'a',
1394 current_offset = pp->p_boffset + pp->p_bsize;
1398 for (i = 0; i < (int)lp->d_npartitions; i++) {
1400 pp = &lp->d_partitions[i];
1401 if (pp->p_bsize == 0 && pp->p_boffset != 0)
1402 Warning("partition %c: size 0, but offset 0x%012llx",
1403 part, pp->p_boffset);
1404 if (pp->p_bsize == 0) {
1408 if (uuid_is_nil(&pp->p_stor_uuid, NULL))
1409 uuid_create(&pp->p_stor_uuid, NULL);
1411 if (pp->p_boffset < lp->d_pbase) {
1413 "partition %c: offset out of bounds (%lld)\n",
1414 part, pp->p_boffset - lp->d_pbase);
1417 if (pp->p_boffset > lp->d_pstop) {
1419 "partition %c: offset out of bounds (%lld)\n",
1420 part, pp->p_boffset - lp->d_pbase);
1423 if (pp->p_boffset + pp->p_bsize > lp->d_pstop) {
1425 "partition %c: size out of bounds (%lld)\n",
1426 part, pp->p_boffset - lp->d_pbase);
1430 /* check for overlaps */
1431 /* this will check for all possible overlaps once and only once */
1432 for (j = 0; j < i; j++) {
1433 pp2 = &lp->d_partitions[j];
1434 if (pp->p_fstype != FS_VINUM &&
1435 pp2->p_fstype != FS_VINUM &&
1436 part_set[i] && part_set[j]) {
1437 if (pp2->p_boffset < pp->p_boffset + pp->p_bsize &&
1438 (pp2->p_boffset + pp2->p_bsize > pp->p_boffset ||
1439 pp2->p_boffset >= pp->p_boffset)) {
1440 fprintf(stderr,"partitions %c and %c overlap!\n",
1447 for (; i < (int)lp->d_npartitions; i++) {
1449 pp = &lp->d_partitions[i];
1450 if (pp->p_bsize || pp->p_boffset)
1451 Warning("unused partition %c: size 0x%012llx offset 0x%012llx",
1452 'a' + i, pp->p_bsize, pp->p_boffset);
1458 * When operating on a "virgin" disk, try getting an initial label
1459 * from the associated device driver. This might work for all device
1460 * drivers that are able to fetch some initial device parameters
1461 * without even having access to a (BSD) disklabel, like SCSI disks,
1462 * most IDE drives, or vn devices.
1464 * The device name must be given in its "canonical" form.
1466 static struct disklabel64 dlab;
1468 struct disklabel64 *
1469 getvirginlabel(void)
1471 struct disklabel64 *dl = &dlab;
1475 if (dkname[0] == '/') {
1476 warnx("\"auto\" requires the usage of a canonical disk name");
1479 asprintf(&path, "%s%s", _PATH_DEV, dkname);
1480 if ((f = open(path, O_RDONLY)) == -1) {
1481 warn("cannot open %s", path);
1486 * Try to use the new get-virgin-label ioctl. If it fails,
1487 * fallback to the old get-disk-info ioctl.
1489 if (ioctl(f, DIOCGDVIRGIN64, dl) < 0) {
1490 l_perror("ioctl DIOCGDVIRGIN64");
1500 Warning(const char *fmt, ...)
1504 fprintf(stderr, "Warning, ");
1506 vfprintf(stderr, fmt, ap);
1507 fprintf(stderr, "\n");
1514 fprintf(stderr, "%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%s\n%s\n",
1515 "usage: disklabel64 [-r] disk",
1516 "\t\t(to read label)",
1517 " disklabel64 -w [-r] [-n] disk type [ packid ]",
1518 "\t\t(to write label with existing boot program)",
1519 " disklabel64 -e [-r] [-n] disk",
1520 "\t\t(to edit label)",
1521 " disklabel64 -R [-r] [-n] disk protofile",
1522 "\t\t(to restore label with existing boot program)",
1523 " disklabel64 -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1524 "\t\t(to install boot program with existing label)",
1525 " disklabel64 -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1526 "\t\t(to write label and boot program)",
1527 " disklabel64 -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1528 "\t\t(to restore label and boot program)",
1529 " disklabel64 [-NW] disk",
1530 "\t\t(to write disable/enable label)");