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
35 * Copyright (c) 1987, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
39 * Symmetric Computer Systems.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * This product includes software developed by the University of
52 * California, Berkeley and its contributors.
53 * 4. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * @(#)disklabel.c 1.2 (Symmetric) 11/28/85
70 * @(#)disklabel.c 8.2 (Berkeley) 1/7/94
71 * $FreeBSD: src/sbin/disklabel/disklabel.c,v 1.28.2.15 2003/01/24 16:18:16 des Exp $
74 #include <sys/param.h>
79 #include <sys/disklabel64.h>
80 #include <sys/diskslice.h>
81 #include <sys/diskmbr.h>
82 #include <sys/dtype.h>
83 #include <sys/sysctl.h>
87 #include <vfs/ufs/dinode.h>
88 #include <vfs/ufs/fs.h>
101 #include "pathnames.h"
103 extern uint32_t crc32(const void *buf, size_t size);
106 * Disklabel64: read and write 64 bit disklabels.
107 * The label is usually placed on one of the first sectors of the disk.
108 * Many machines also place a bootstrap in the same area,
109 * in which case the label is embedded in the bootstrap.
110 * The bootstrap source must leave space at the proper offset
111 * for the label on such machines.
114 #define LABELSIZE ((sizeof(struct disklabel64) + 4095) & ~4095)
115 #define BOOTSIZE 32768
117 /* FIX! These are too low, but are traditional */
118 #define DEFAULT_NEWFS_BLOCK 8192U
119 #define DEFAULT_NEWFS_FRAG 1024U
120 #define DEFAULT_NEWFS_CPG 16U
122 #define BIG_NEWFS_BLOCK 16384U
123 #define BIG_NEWFS_FRAG 2048U
124 #define BIG_NEWFS_CPG 64U
126 void makelabel(const char *, const char *, struct disklabel64 *);
127 int writelabel(int, struct disklabel64 *);
128 void l_perror(const char *);
129 struct disklabel64 *readlabel(int);
130 struct disklabel64 *makebootarea(int);
131 void display(FILE *, const struct disklabel64 *);
132 int edit(struct disklabel64 *, int);
136 int getasciilabel(FILE *, struct disklabel64 *);
137 int getasciipartspec(char *, struct disklabel64 *, int, int, uint32_t);
138 int getasciipartuuid(char *, struct disklabel64 *, int, int, uint32_t);
139 int checklabel(struct disklabel64 *);
140 void Warning(const char *, ...) __printflike(1, 2);
142 struct disklabel64 *getvirginlabel(void);
144 #define DEFEDITOR _PATH_VI
145 #define streq(a,b) (strcmp(a,b) == 0)
149 char tmpfil[] = PATH_TMPFILE;
151 struct disklabel64 lab;
153 #define MAX_PART ('z')
154 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
155 char part_size_type[MAX_NUM_PARTS];
156 char part_offset_type[MAX_NUM_PARTS];
157 int part_set[MAX_NUM_PARTS];
159 int installboot; /* non-zero if we should install a boot program */
169 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
174 int disable_write; /* set to disable writing to disk label */
175 u_int32_t slice_start_lba;
179 #define OPTIONS "BNRWb:denrs:Vw"
181 #define OPTIONS "BNRWb:enrs:Vw"
185 main(int argc, char *argv[])
187 struct disklabel64 *lp;
189 int ch, f = 0, flag, error = 0;
192 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
262 dkname = getdevpath(argv[0], 0);
264 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
266 err(4, "%s", specname);
282 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
283 err(4, "ioctl DIOCWLABEL");
291 error = checklabel(lp);
295 if (installboot && argc == 3) {
296 makelabel(argv[2], 0, &lab);
300 * We only called makelabel() for its side effect
301 * of setting the bootstrap file names. Discard
302 * all changes to `lab' so that all values in the
303 * final label come from the ASCII label.
305 bzero((char *)&lab, sizeof(lab));
309 if (!(t = fopen(argv[1], "r")))
310 err(4, "%s", argv[1]);
311 if (!getasciilabel(t, &lab))
313 lp = makebootarea(f);
314 bcopy(&lab.d_magic, &lp->d_magic,
315 sizeof(lab) - offsetof(struct disklabel64, d_magic));
316 error = writelabel(f, lp);
326 makelabel(argv[1], name, &lab);
327 lp = makebootarea(f);
328 bcopy(&lab.d_magic, &lp->d_magic,
329 sizeof(lab) - offsetof(struct disklabel64, d_magic));
330 if (checklabel(lp) == 0)
331 error = writelabel(f, lp);
336 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
337 err(4, "ioctl DIOCWLABEL");
342 struct disklabel64 tlab;
347 makelabel(argv[1], 0, &lab);
348 lp = makebootarea(f);
349 bcopy(&tlab.d_magic, &lp->d_magic,
350 sizeof(tlab) - offsetof(struct disklabel64, d_magic));
351 if (checklabel(lp) == 0)
352 error = writelabel(f, lp);
360 * Construct a prototype disklabel from /etc/disktab. As a side
361 * effect, set the names of the primary and secondary boot files
365 makelabel(const char *type, const char *name, struct disklabel64 *lp)
367 struct disklabel64 *dp;
369 if (strcmp(type, "auto") == 0)
370 dp = getvirginlabel();
374 errx(1, "%s: unknown disk type", type);
378 * NOTE: boot control files may no longer be specified in disktab.
381 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
385 writelabel(int f, struct disklabel64 *lp)
387 struct disklabel64 *blp;
393 lpsize = offsetof(struct disklabel64, d_partitions[lp->d_npartitions]);
394 lpcrcsize = lpsize - offsetof(struct disklabel64, d_magic);
397 Warning("write to disk label suppressed - label was as follows:");
401 lp->d_magic = DISKMAGIC64;
403 lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
406 * Make sure the boot area is not too large
409 int lpbsize = (int)(lp->d_pbase - lp->d_bbase);
410 if (lp->d_pbase == 0) {
411 errx(1, "no space was set aside in "
412 "the disklabel for boot2!");
414 if (boot2size > lpbsize) {
415 errx(1, "label did not reserve enough "
416 "space for boot! %d/%d",
422 * First set the kernel disk label,
423 * then write a label to the raw disk.
424 * If the SDINFO ioctl fails because it is
425 * unimplemented, keep going; otherwise, the kernel
426 * consistency checks may prevent us from changing
427 * the current (in-core) label.
429 if (ioctl(f, DIOCSDINFO64, lp) < 0 &&
430 errno != ENODEV && errno != ENOTTY) {
431 l_perror("ioctl DIOCSDINFO");
434 lseek(f, (off_t)0, SEEK_SET);
437 * The disklabel embeds areas which we may not
438 * have wanted to change. Merge those areas in
441 blp = makebootarea(f);
443 bcopy(&lp->d_magic, &blp->d_magic,
445 offsetof(struct disklabel64, d_magic));
449 * write enable label sector before write
450 * (if necessary), disable after writing.
453 if (ioctl(f, DIOCWLABEL, &flag) < 0)
454 warn("ioctl DIOCWLABEL");
456 r = write(f, boot1buf, boot1lsize);
457 if (r != (ssize_t)boot1lsize) {
462 * Output the remainder of the disklabel
465 lseek(f, lp->d_bbase, 0);
466 r = write(f, boot2buf, boot2size);
467 if (r != boot2size) {
473 ioctl(f, DIOCWLABEL, &flag);
474 } else if (ioctl(f, DIOCWDINFO64, lp) < 0) {
475 l_perror("ioctl DIOCWDINFO64");
483 l_perror(const char *s)
488 warnx("%s: no disk label on disk;", s);
489 fprintf(stderr, "add \"-r\" to install initial label\n");
493 warnx("%s: label magic number or checksum is wrong!", s);
494 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
498 warnx("%s: open partition would move or shrink", s);
502 warnx("%s: the disk already has a label of a different type,\n"
503 "probably a 32 bit disklabel. It must be cleaned out "
514 * Fetch disklabel for disk.
515 * Use ioctl to get label unless -r flag is given.
520 struct disklabel64 *lp;
526 * Allocate space for the label. The boot1 code, if any,
527 * is embedded in the label. The label overlaps the boot1
530 lp = makebootarea(f);
531 lpcrcsize = offsetof(struct disklabel64,
532 d_partitions[lp->d_npartitions]) -
533 offsetof(struct disklabel64, d_magic);
536 if (lp->d_magic != DISKMAGIC64)
537 errx(1, "bad pack magic number");
538 if (lp->d_npartitions > MAXPARTITIONS64 ||
539 savecrc != crc32(&lp->d_magic, lpcrcsize)
541 errx(1, "corrupted disklabel64");
546 * Just use a static structure to hold the label. Note
547 * that DIOCSDINFO64 does not overwrite the boot1 area
548 * even though it is part of the disklabel64 structure.
552 if (ioctl(f, DIOCGDVIRGIN64, lp) < 0) {
553 l_perror("ioctl DIOCGDVIRGIN64");
557 if (ioctl(f, DIOCGDINFO64, lp) < 0) {
558 l_perror("ioctl DIOCGDINFO64");
567 * Construct a boot area for boot1 and boot2 and return the location of
568 * the label within the area. The caller will overwrite the label so
569 * we don't actually have to read it.
574 struct disklabel64 *lp;
575 struct partinfo info;
581 if (ioctl(f, DIOCGPART, &info) == 0)
582 secsize = info.media_blksize;
586 if (boot1buf == NULL) {
589 rsize = (sizeof(struct disklabel64) + secsize - 1) &
591 boot1size = offsetof(struct disklabel64, d_magic);
593 boot1buf = malloc(rsize);
594 bzero(boot1buf, rsize);
595 r = read(f, boot1buf, rsize);
597 err(4, "%s", specname);
599 lp = (void *)boot1buf;
601 if (installboot == 0)
604 if (boot2buf == NULL) {
606 boot2buf = malloc(boot2size);
607 bzero(boot2buf, boot2size);
611 * If installing the boot code, read it into the appropriate portions
614 if (boot1path == NULL)
615 asprintf(&boot1path, "%s/boot1_64", _PATH_BOOTDIR);
616 if (boot2path == NULL)
617 asprintf(&boot2path, "%s/boot2_64", _PATH_BOOTDIR);
619 if ((fd = open(boot1path, O_RDONLY)) < 0)
620 err(4, "%s", boot1path);
621 if (fstat(fd, &st) < 0)
622 err(4, "%s", boot1path);
623 if (st.st_size > boot1size)
624 err(4, "%s must be exactly %d bytes!", boot1path, boot1size);
625 if (read(fd, boot1buf, boot1size) != boot1size)
626 err(4, "%s must be exactly %d bytes!", boot1path, boot1size);
629 if ((fd = open(boot2path, O_RDONLY)) < 0)
630 err(4, "%s", boot2path);
631 if (fstat(fd, &st) < 0)
632 err(4, "%s", boot2path);
633 if (st.st_size > boot2size)
634 err(4, "%s must be <= %d bytes!", boot2path, boot2size);
635 if ((r = read(fd, boot2buf, boot2size)) < 1)
636 err(4, "%s is empty!", boot2path);
637 boot2size = (r + secsize - 1) & ~(secsize - 1);
641 * XXX dangerously dedicated support goes here XXX
647 display(FILE *f, const struct disklabel64 *lp)
649 const struct partition64 *pp;
656 * Use a human readable block size if possible. This is for
657 * display and editing purposes only.
659 if (lp->d_align > 1024)
662 blksize = lp->d_align;
664 fprintf(f, "# %s:\n", specname);
666 fprintf(f, "# Informational fields calculated from the above\n");
667 fprintf(f, "# All byte equivalent offsets must be aligned\n");
669 fprintf(f, "# boot space: %10ju bytes\n",
670 (intmax_t)(lp->d_pbase - lp->d_bbase));
671 fprintf(f, "# data space: %10ju blocks\t# %6.2f MB (%ju bytes)\n",
672 (intmax_t)(lp->d_pstop - lp->d_pbase) / blksize,
673 (double)(lp->d_pstop - lp->d_pbase) / 1024.0 / 1024.0,
674 (intmax_t)(lp->d_pstop - lp->d_pbase));
676 fprintf(f, "# NOTE: If the partition data base looks odd it may be\n");
677 fprintf(f, "# physically aligned instead of slice-aligned\n");
680 uuid_to_string(&lp->d_stor_uuid, &str, NULL);
681 fprintf(f, "diskid: %s\n", str ? str : "<unknown>");
684 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
686 fprintf(f, "boot2 data base: 0x%012jx\n", (intmax_t)lp->d_bbase);
687 fprintf(f, "partitions data base: 0x%012jx\n", (intmax_t)lp->d_pbase);
688 fprintf(f, "partitions data stop: 0x%012jx\n", (intmax_t)lp->d_pstop);
689 fprintf(f, "backup label: 0x%012jx\n", (intmax_t)lp->d_abase);
690 fprintf(f, "total size: 0x%012jx\t# %6.2f MB\n",
691 (intmax_t)lp->d_total_size,
692 (double)lp->d_total_size / 1024.0 / 1024.0);
693 fprintf(f, "alignment: %u\n", lp->d_align);
694 fprintf(f, "display block size: %u\t# for partition display only\n",
698 fprintf(f, "%u partitions:\n", lp->d_npartitions);
699 fprintf(f, "# size offset fstype fsuuid\n");
701 for (part = 0; part < lp->d_npartitions; part++) {
702 pp = &lp->d_partitions[part];
703 const u_long onemeg = 1024 * 1024;
705 if (pp->p_bsize == 0)
708 fprintf(f, " %c: ", 'a' + part);
710 if (pp->p_bsize % lp->d_align)
711 fprintf(f, "%10s ", "ILLEGAL");
713 fprintf(f, "%10ju ", (intmax_t)pp->p_bsize / blksize);
715 if ((pp->p_boffset - lp->d_pbase) % lp->d_align)
716 fprintf(f, "%10s ", "ILLEGAL");
719 (intmax_t)(pp->p_boffset - lp->d_pbase) / blksize);
721 if (pp->p_fstype < FSMAXTYPES)
722 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
724 fprintf(f, "%8d", pp->p_fstype);
725 fprintf(f, "\t# %11.3fMB", (double)pp->p_bsize / onemeg);
728 for (part = 0; part < lp->d_npartitions; part++) {
729 pp = &lp->d_partitions[part];
731 if (pp->p_bsize == 0)
734 if (uuid_is_nil(&lp->d_stor_uuid, NULL) == 0) {
735 fprintf(f, " %c-stor_uuid: ", 'a' + part);
737 uuid_to_string(&pp->p_stor_uuid, &str, NULL);
739 fprintf(f, "%s", str);
746 fprintf(f, "# EXAMPLE\n");
747 fprintf(f, "#a: 4g 0 4.2BSD\n");
748 fprintf(f, "#a: * * 4.2BSD\n");
755 edit(struct disklabel64 *lp, int f)
758 struct disklabel64 label;
761 if ((fd = mkstemp(tmpfil)) == -1 ||
762 (fp = fdopen(fd, "w")) == NULL) {
763 warnx("can't create %s", tmpfil);
771 fp = fopen(tmpfil, "r");
773 warnx("can't reopen %s for reading", tmpfil);
776 bzero((char *)&label, sizeof(label));
777 if (getasciilabel(fp, &label)) {
779 if (writelabel(f, lp) == 0) {
786 printf("re-edit the label? [y]: "); fflush(stdout);
788 if (c != EOF && c != (int)'\n')
789 while (getchar() != (int)'\n')
805 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
806 while ((pid = fork()) < 0) {
807 if (errno == EPROCLIM) {
808 warnx("you have too many processes");
811 if (errno != EAGAIN) {
821 if ((ed = getenv("EDITOR")) == NULL)
823 execlp(ed, ed, tmpfil, NULL);
826 while ((xpid = wait(&status)) >= 0)
837 while (*cp != '\0' && isspace(*cp))
839 if (*cp == '\0' || *cp == '#')
849 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
851 if ((c = *cp) != '\0') {
860 * Read an ascii label in from fd f,
861 * in the same format as that put out by display(),
865 getasciilabel(FILE *f, struct disklabel64 *lp)
869 char *tp, line[BUFSIZ];
871 uint32_t blksize = 0;
873 int lineno = 0, errors = 0;
876 bzero(&part_set, sizeof(part_set));
877 bzero(&part_size_type, sizeof(part_size_type));
878 bzero(&part_offset_type, sizeof(part_offset_type));
879 while (fgets(line, sizeof(line) - 1, f)) {
881 if ((cp = strchr(line,'\n')) != NULL)
886 tp = strchr(cp, ':');
888 fprintf(stderr, "line %d: syntax error\n", lineno);
892 *tp++ = '\0', tp = skip(tp);
893 if (sscanf(cp, "%lu partitions", &v) == 1) {
894 if (v == 0 || v > MAXPARTITIONS64) {
896 "line %d: bad # of partitions\n", lineno);
897 lp->d_npartitions = MAXPARTITIONS64;
900 lp->d_npartitions = v;
906 if (streq(cp, "diskid")) {
908 uuid_from_string(tp, &lp->d_stor_uuid, &status);
909 if (status != uuid_s_ok) {
911 "line %d: %s: illegal UUID\n",
917 if (streq(cp, "label")) {
918 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
922 if (streq(cp, "alignment")) {
923 v = strtoul(tp, NULL, 0);
924 if (v <= 0 || (v & DEV_BMASK) != 0 || v > 1024*1024) {
926 "line %d: %s: bad alignment\n",
934 if (streq(cp, "total size")) {
935 vv = strtoull(tp, NULL, 0);
936 if (vv == 0 || vv == (uint64_t)-1) {
937 fprintf(stderr, "line %d: %s: bad %s\n",
941 lp->d_total_size = vv;
945 if (streq(cp, "boot2 data base")) {
946 vv = strtoull(tp, NULL, 0);
947 if (vv == 0 || vv == (uint64_t)-1) {
948 fprintf(stderr, "line %d: %s: bad %s\n",
956 if (streq(cp, "partitions data base")) {
957 vv = strtoull(tp, NULL, 0);
958 if (vv == 0 || vv == (uint64_t)-1) {
959 fprintf(stderr, "line %d: %s: bad %s\n",
967 if (streq(cp, "partitions data stop")) {
968 vv = strtoull(tp, NULL, 0);
969 if (vv == 0 || vv == (uint64_t)-1) {
970 fprintf(stderr, "line %d: %s: bad %s\n",
978 if (streq(cp, "backup label")) {
979 vv = strtoull(tp, NULL, 0);
980 if (vv == 0 || vv == (uint64_t)-1) {
981 fprintf(stderr, "line %d: %s: bad %s\n",
989 if (streq(cp, "display block size")) {
990 v = strtoul(tp, NULL, 0);
991 if (v <= 0 || (v & DEV_BMASK) != 0 || v > 1024*1024) {
993 "line %d: %s: bad alignment\n",
1002 /* the ':' was removed above */
1005 * Handle main partition data, e.g. a:, b:, etc.
1007 if (*cp < 'a' || *cp > MAX_PART) {
1009 "line %d: %s: Unknown disklabel field\n", lineno,
1015 /* Process a partition specification line. */
1017 if (part >= lp->d_npartitions) {
1019 "line %d: partition name out of range a-%c: %s\n",
1020 lineno, 'a' + lp->d_npartitions - 1, cp);
1026 fprintf(stderr, "block size to use for partition "
1027 "display was not specified!\n");
1032 if (strcmp(cp + 1, "-stor_uuid") == 0) {
1033 if (getasciipartuuid(tp, lp, part, lineno, blksize)) {
1038 } else if (cp[1] == 0) {
1040 if (getasciipartspec(tp, lp, part, lineno, blksize)) {
1046 fprintf(stderr, "line %d: %s: Unknown disklabel field\n",
1051 errors += checklabel(lp);
1052 return (errors == 0);
1057 parse_field_val(char **tp, char **cp, u_int64_t *vv, int lineno)
1061 if (*tp == NULL || **tp == 0) {
1062 fprintf(stderr, "line %d: too few numeric fields\n", lineno);
1067 *vv = strtoull(*cp, &tmp, 0);
1068 if (*vv == ULLONG_MAX) {
1069 fprintf(stderr, "line %d: illegal number\n", lineno);
1079 * Read a partition line into partition `part' in the specified disklabel.
1080 * Return 0 on success, 1 on failure.
1083 getasciipartspec(char *tp, struct disklabel64 *lp, int part,
1084 int lineno, uint32_t blksize)
1086 struct partition64 *pp;
1094 pp = &lp->d_partitions[part];
1100 r = parse_field_val(&tp, &cp, &vv, lineno);
1130 r = 0; /* eat the suffix */
1133 Warning("unknown size specifier '%c' (*/%%/K/M/G/T 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 && strcasecmp(*cpp, cp) == 0)
1177 pp->p_fstype = cpp - fstypenames;
1180 v = strtoul(cp, NULL, 0);
1183 if (v >= FSMAXTYPES) {
1185 "line %d: Warning, unknown filesystem type %s\n",
1194 fprintf(stderr, "line %d: Warning, extra data on line\n",
1201 getasciipartuuid(char *tp, struct disklabel64 *lp, int part,
1202 int lineno, uint32_t blksize __unused)
1204 struct partition64 *pp;
1208 pp = &lp->d_partitions[part];
1212 uuid_from_string(cp, &pp->p_stor_uuid, &status);
1213 if (status != uuid_s_ok) {
1214 fprintf(stderr, "line %d: Illegal storage uuid specification\n",
1222 * Check disklabel for errors and fill in
1223 * derived fields according to supplied values.
1226 checklabel(struct disklabel64 *lp)
1228 struct partition64 *pp;
1231 u_int64_t total_size;
1232 u_int64_t current_offset;
1233 u_long total_percent;
1234 int seen_default_offset;
1237 struct partition64 *pp2;
1240 if (lp->d_align < 512 ||
1241 (lp->d_align ^ (lp->d_align - 1)) != lp->d_align * 2 - 1) {
1242 Warning("Illegal alignment specified: %u\n", lp->d_align);
1245 if (lp->d_npartitions > MAXPARTITIONS64) {
1246 Warning("number of partitions (%u) > MAXPARTITIONS (%d)",
1247 lp->d_npartitions, MAXPARTITIONS64);
1250 off = offsetof(struct disklabel64, d_partitions[lp->d_npartitions]);
1251 off = (off + lp->d_align - 1) & ~(int64_t)(lp->d_align - 1);
1253 if (lp->d_bbase < off || lp->d_bbase % lp->d_align) {
1254 Warning("illegal boot2 data base ");
1259 * pbase can be unaligned slice-relative but will be
1260 * aligned physically.
1262 if (lp->d_pbase < lp->d_bbase) {
1263 Warning("illegal partition data base");
1266 if (lp->d_pstop < lp->d_pbase) {
1267 Warning("illegal partition data stop");
1270 if (lp->d_pstop > lp->d_total_size) {
1271 printf("%012jx\n%012jx\n",
1272 (intmax_t)lp->d_pstop, (intmax_t)lp->d_total_size);
1273 Warning("disklabel control info is beyond the total size");
1277 (lp->d_abase < lp->d_pstop ||
1278 lp->d_abase > lp->d_total_size - off)) {
1279 Warning("illegal backup label location");
1283 /* first allocate space to the partitions, then offsets */
1284 total_size = 0; /* in bytes */
1285 total_percent = 0; /* in percent */
1287 /* find all fixed partitions */
1288 for (i = 0; i < (int)lp->d_npartitions; i++) {
1289 pp = &lp->d_partitions[i];
1291 if (part_size_type[i] == '*') {
1292 if (part_offset_type[i] != '*') {
1293 if (total_size < pp->p_boffset)
1294 total_size = pp->p_boffset;
1296 if (hog_part != -1) {
1297 Warning("Too many '*' partitions (%c and %c)",
1298 hog_part + 'a',i + 'a');
1306 if (part_size_type[i] == '%') {
1308 * don't count %'s yet
1310 total_percent += size;
1313 * Value has already been converted
1316 if (size % lp->d_align != 0) {
1317 Warning("partition %c's size is not properly aligned",
1325 /* handle % partitions - note %'s don't need to add up to 100! */
1326 if (total_percent != 0) {
1330 free_space = (int64_t)(lp->d_pstop - lp->d_pbase - total_size);
1331 free_space &= ~(u_int64_t)(lp->d_align - 1);
1333 space_left = free_space;
1334 if (total_percent > 100) {
1335 fprintf(stderr,"total percentage %lu is greater than 100\n",
1340 if (free_space > 0) {
1341 for (i = 0; i < (int)lp->d_npartitions; i++) {
1342 pp = &lp->d_partitions[i];
1343 if (part_set[i] && part_size_type[i] == '%') {
1344 /* careful of overflows! and integer roundoff */
1345 pp->p_bsize = ((double)pp->p_bsize/100) * free_space;
1346 pp->p_bsize = (pp->p_bsize + lp->d_align - 1) & ~(u_int64_t)(lp->d_align - 1);
1347 if ((int64_t)pp->p_bsize > space_left)
1348 pp->p_bsize = (u_int64_t)space_left;
1349 total_size += pp->p_bsize;
1350 space_left -= pp->p_bsize;
1354 fprintf(stderr, "%jd bytes available to give to "
1355 "'*' and '%%' partitions\n",
1356 (intmax_t)free_space);
1358 /* fix? set all % partitions to size 0? */
1361 /* give anything remaining to the hog partition */
1362 if (hog_part != -1) {
1363 off = lp->d_pstop - lp->d_pbase - total_size;
1364 off &= ~(u_int64_t)(lp->d_align - 1);
1365 lp->d_partitions[hog_part].p_bsize = off;
1366 total_size = lp->d_pstop - lp->d_pbase;
1369 /* Now set the offsets for each partition */
1370 current_offset = lp->d_pbase;
1371 seen_default_offset = 0;
1372 for (i = 0; i < (int)lp->d_npartitions; i++) {
1374 pp = &lp->d_partitions[i];
1375 if (pp->p_bsize == 0)
1378 if (part_offset_type[i] == '*') {
1379 pp->p_boffset = current_offset;
1380 seen_default_offset = 1;
1382 /* allow them to be out of order for old-style tables */
1383 if (pp->p_boffset < current_offset &&
1384 seen_default_offset &&
1385 pp->p_fstype != FS_VINUM) {
1387 "Offset 0x%012jx for partition %c overlaps previous partition which ends at 0x%012jx\n",
1388 (intmax_t)pp->p_boffset,
1390 (intmax_t)current_offset);
1392 "Labels with any *'s for offset must be in ascending order by sector\n");
1394 } else if (pp->p_boffset != current_offset &&
1395 seen_default_offset) {
1397 * this may give unneeded warnings if
1398 * partitions are out-of-order
1401 "Offset 0x%012jx for partition %c doesn't match expected value 0x%012jx",
1402 pp->p_boffset, i + 'a',
1403 (intmax_t)current_offset);
1406 current_offset = pp->p_boffset + pp->p_bsize;
1410 for (i = 0; i < (int)lp->d_npartitions; i++) {
1412 pp = &lp->d_partitions[i];
1413 if (pp->p_bsize == 0 && pp->p_boffset != 0)
1414 Warning("partition %c: size 0, but offset 0x%012jx",
1415 part, (intmax_t)pp->p_boffset);
1416 if (pp->p_bsize == 0) {
1420 if (uuid_is_nil(&pp->p_stor_uuid, NULL))
1421 uuid_create(&pp->p_stor_uuid, NULL);
1423 if (pp->p_boffset < lp->d_pbase) {
1425 "partition %c: offset out of bounds (%jd)\n",
1426 part, (intmax_t)(pp->p_boffset - lp->d_pbase));
1429 if (pp->p_boffset > lp->d_pstop) {
1431 "partition %c: offset out of bounds (%jd)\n",
1432 part, (intmax_t)(pp->p_boffset - lp->d_pbase));
1435 if (pp->p_boffset + pp->p_bsize > lp->d_pstop) {
1437 "partition %c: size out of bounds (%jd)\n",
1438 part, (intmax_t)(pp->p_boffset - lp->d_pbase));
1442 /* check for overlaps */
1443 /* this will check for all possible overlaps once and only once */
1444 for (j = 0; j < i; j++) {
1445 pp2 = &lp->d_partitions[j];
1446 if (pp->p_fstype != FS_VINUM &&
1447 pp2->p_fstype != FS_VINUM &&
1448 part_set[i] && part_set[j]) {
1449 if (pp2->p_boffset < pp->p_boffset + pp->p_bsize &&
1450 (pp2->p_boffset + pp2->p_bsize > pp->p_boffset ||
1451 pp2->p_boffset >= pp->p_boffset)) {
1452 fprintf(stderr,"partitions %c and %c overlap!\n",
1459 for (; i < (int)lp->d_npartitions; i++) {
1461 pp = &lp->d_partitions[i];
1462 if (pp->p_bsize || pp->p_boffset)
1463 Warning("unused partition %c: size 0x%012jx "
1465 'a' + i, (intmax_t)pp->p_bsize,
1466 (intmax_t)pp->p_boffset);
1472 * When operating on a "virgin" disk, try getting an initial label
1473 * from the associated device driver. This might work for all device
1474 * drivers that are able to fetch some initial device parameters
1475 * without even having access to a (BSD) disklabel, like SCSI disks,
1476 * most IDE drives, or vn devices.
1478 * The device name must be given in its "canonical" form.
1480 static struct disklabel64 dlab;
1482 struct disklabel64 *
1483 getvirginlabel(void)
1485 struct disklabel64 *dl = &dlab;
1488 if ((f = open(dkname, O_RDONLY)) == -1) {
1489 warn("cannot open %s", dkname);
1494 * Try to use the new get-virgin-label ioctl. If it fails,
1495 * fallback to the old get-disk-info ioctl.
1497 if (ioctl(f, DIOCGDVIRGIN64, dl) < 0) {
1498 l_perror("ioctl DIOCGDVIRGIN64");
1508 Warning(const char *fmt, ...)
1512 fprintf(stderr, "Warning, ");
1514 vfprintf(stderr, fmt, ap);
1515 fprintf(stderr, "\n");
1522 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",
1523 "usage: disklabel64 [-r] disk",
1524 "\t\t(to read label)",
1525 " disklabel64 -w [-r] [-n] disk type [packid]",
1526 "\t\t(to write label with existing boot program)",
1527 " disklabel64 -e [-r] [-n] disk",
1528 "\t\t(to edit label)",
1529 " disklabel64 -R [-r] [-n] disk protofile",
1530 "\t\t(to restore label with existing boot program)",
1531 " disklabel64 -B [-n] [-b boot1 -s boot2] disk [type]",
1532 "\t\t(to install boot program with existing label)",
1533 " disklabel64 -w -B [-n] [-b boot1 -s boot2] disk type [packid]",
1534 "\t\t(to write label and boot program)",
1535 " disklabel64 -R -B [-n] [-b boot1 -s boot2] disk protofile [type]",
1536 "\t\t(to restore label and boot program)",
1537 " disklabel64 [-NW] disk",
1538 "\t\t(to write disable/enable label)");