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.1 2007/06/19 02:53:55 dillon 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.1 2007/06/19 02:53:55 dillon 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 checklabel(struct disklabel64 *);
142 void Warning(const char *, ...) __printflike(1, 2);
144 struct disklabel64 *getvirginlabel(void);
146 #define DEFEDITOR _PATH_VI
147 #define streq(a,b) (strcmp(a,b) == 0)
151 char tmpfil[] = PATH_TMPFILE;
153 struct disklabel64 lab;
155 #define MAX_PART ('z')
156 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
157 char part_size_type[MAX_NUM_PARTS];
158 char part_offset_type[MAX_NUM_PARTS];
159 int part_set[MAX_NUM_PARTS];
161 int installboot; /* non-zero if we should install a boot program */
171 UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
176 int disable_write; /* set to disable writing to disk label */
177 u_int32_t slice_start_lba;
181 #define OPTIONS "BNRWb:denrs:Vw"
183 #define OPTIONS "BNRWb:enrs:Vw"
187 main(int argc, char *argv[])
189 struct disklabel64 *lp;
191 int ch, f = 0, flag, error = 0;
194 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
265 if (dkname[0] != '/') {
266 asprintf(&specname, "%s%s", _PATH_DEV, dkname);
270 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
271 if (f < 0 && errno == ENOENT && dkname[0] != '/') {
272 asprintf(&specname, "%s%s", _PATH_DEV, dkname);
273 f = open(specname, op == READ ? O_RDONLY : O_RDWR);
276 err(4, "%s", specname);
292 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
293 err(4, "ioctl DIOCWLABEL");
301 error = checklabel(lp);
305 if (installboot && argc == 3) {
306 makelabel(argv[2], 0, &lab);
310 * We only called makelabel() for its side effect
311 * of setting the bootstrap file names. Discard
312 * all changes to `lab' so that all values in the
313 * final label come from the ASCII label.
315 bzero((char *)&lab, sizeof(lab));
319 if (!(t = fopen(argv[1], "r")))
320 err(4, "%s", argv[1]);
321 if (!getasciilabel(t, &lab))
323 lp = makebootarea(f);
324 bcopy(&lab.d_magic, &lp->d_magic,
325 sizeof(lab) - offsetof(struct disklabel64, d_magic));
326 error = writelabel(f, lp);
336 makelabel(argv[1], name, &lab);
337 lp = makebootarea(f);
338 bcopy(&lab.d_magic, &lp->d_magic,
339 sizeof(lab) - offsetof(struct disklabel64, d_magic));
340 if (checklabel(lp) == 0)
341 error = writelabel(f, lp);
346 if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
347 err(4, "ioctl DIOCWLABEL");
352 struct disklabel64 tlab;
357 makelabel(argv[1], 0, &lab);
358 lp = makebootarea(f);
359 bcopy(&tlab.d_magic, &lp->d_magic,
360 sizeof(tlab) - offsetof(struct disklabel64, d_magic));
361 if (checklabel(lp) == 0)
362 error = writelabel(f, lp);
370 * Construct a prototype disklabel from /etc/disktab. As a side
371 * effect, set the names of the primary and secondary boot files
375 makelabel(const char *type, const char *name, struct disklabel64 *lp)
377 struct disklabel64 *dp;
379 if (strcmp(type, "auto") == 0)
380 dp = getvirginlabel();
384 errx(1, "%s: unknown disk type", type);
388 * NOTE: boot control files may no longer be specified in disktab.
391 strncpy(lp->d_packname, name, sizeof(lp->d_packname));
395 writelabel(int f, struct disklabel64 *lp)
397 struct disklabel64 *blp;
403 lpsize = offsetof(struct disklabel64, d_partitions[lp->d_npartitions]);
404 lpcrcsize = lpsize - offsetof(struct disklabel64, d_magic);
407 Warning("write to disk label supressed - label was as follows:");
411 lp->d_magic = DISKMAGIC64;
413 lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
416 * Make sure the boot area is not too large
419 int lpbsize = (int)(lp->d_pbase - lp->d_bbase);
420 if (lp->d_pbase == 0) {
421 errx(1, "no space was set aside in "
422 "the disklabel for boot2!");
424 if (boot2size > lpbsize) {
425 errx(1, "label did not reserve enough "
426 "space for boot! %d/%d",
432 * First set the kernel disk label,
433 * then write a label to the raw disk.
434 * If the SDINFO ioctl fails because it is
435 * unimplemented, keep going; otherwise, the kernel
436 * consistency checks may prevent us from changing
437 * the current (in-core) label.
439 if (ioctl(f, DIOCSDINFO64, lp) < 0 &&
440 errno != ENODEV && errno != ENOTTY) {
441 l_perror("ioctl DIOCSDINFO");
444 lseek(f, (off_t)0, SEEK_SET);
447 * The disklabel embeds areas which we may not
448 * have wanted to change. Merge those areas in
451 blp = makebootarea(f);
453 bcopy(&lp->d_magic, &blp->d_magic,
455 offsetof(struct disklabel64, d_magic));
459 * write enable label sector before write
460 * (if necessary), disable after writing.
463 if (ioctl(f, DIOCWLABEL, &flag) < 0)
464 warn("ioctl DIOCWLABEL");
466 r = write(f, boot1buf, boot1lsize);
467 if (r != (ssize_t)boot1lsize) {
472 * Output the remainder of the disklabel
475 lseek(f, lp->d_bbase, 0);
476 r = write(f, boot2buf, boot2size);
477 if (r != boot2size) {
483 ioctl(f, DIOCWLABEL, &flag);
484 } else if (ioctl(f, DIOCWDINFO64, lp) < 0) {
485 l_perror("ioctl DIOCWDINFO64");
493 l_perror(const char *s)
498 warnx("%s: no disk label on disk;", s);
499 fprintf(stderr, "add \"-r\" to install initial label\n");
503 warnx("%s: label magic number or checksum is wrong!", s);
504 fprintf(stderr, "(disklabel or kernel is out of date?)\n");
508 warnx("%s: open partition would move or shrink", s);
512 warnx("%s: the disk already has a label of a different type,\n"
513 "probably a 32 bit disklabel. It must be cleaned out "
524 * Fetch disklabel for disk.
525 * Use ioctl to get label unless -r flag is given.
530 struct disklabel64 *lp;
536 * Allocate space for the label. The boot1 code, if any,
537 * is embedded in the label. The label overlaps the boot1
540 lp = makebootarea(f);
541 lpcrcsize = offsetof(struct disklabel64,
542 d_partitions[lp->d_npartitions]) -
543 offsetof(struct disklabel64, d_magic);
546 if (lp->d_magic != DISKMAGIC64)
547 errx(1, "bad pack magic number");
548 if (lp->d_npartitions > MAXPARTITIONS64 ||
549 savecrc != crc32(&lp->d_magic, lpcrcsize)
551 errx(1, "corrupted disklabel64");
556 * Just use a static structure to hold the label. Note
557 * that DIOCSDINFO64 does not overwrite the boot1 area
558 * even though it is part of the disklabel64 structure.
562 if (ioctl(f, DIOCGDVIRGIN64, lp) < 0) {
563 l_perror("ioctl DIOCGDVIRGIN64");
567 if (ioctl(f, DIOCGDINFO64, lp) < 0) {
568 l_perror("ioctl DIOCGDINFO64");
577 * Construct a boot area for boot1 and boot2 and return the location of
578 * the label within the area. The caller will overwrite the label so
579 * we don't actually have to read it.
584 struct disklabel64 *lp;
585 struct partinfo info;
591 if (ioctl(f, DIOCGPART, &info) == 0)
592 secsize = info.media_blksize;
596 if (boot1buf == NULL) {
599 rsize = (sizeof(struct disklabel64) + secsize - 1) &
601 boot1size = offsetof(struct disklabel64, d_magic);
603 boot1buf = malloc(rsize);
604 bzero(boot1buf, rsize);
605 r = read(f, boot1buf, rsize);
607 err(4, "%s", specname);
609 lp = (void *)boot1buf;
611 if (installboot == 0)
614 if (boot2buf == NULL) {
616 boot2buf = malloc(boot2size);
617 bzero(boot2buf, boot2size);
621 * If installing the boot code, read it into the appropriate portions
624 if (boot1path == NULL)
625 asprintf(&boot1path, "%s/boot1_64", _PATH_BOOTDIR);
626 if (boot2path == NULL)
627 asprintf(&boot2path, "%s/boot2_64", _PATH_BOOTDIR);
629 if ((fd = open(boot1path, O_RDONLY)) < 0)
630 err(4, "%s", boot1path);
631 if (fstat(fd, &st) < 0)
632 err(4, "%s", boot1path);
633 if (st.st_size > boot1size)
634 err(4, "%s must be exactly %d bytes!", boot1path, boot1size);
635 if (read(fd, boot1buf, boot1size) != boot1size)
636 err(4, "%s must be exactly %d bytes!", boot1path, boot1size);
639 if ((fd = open(boot2path, O_RDONLY)) < 0)
640 err(4, "%s", boot2path);
641 if (fstat(fd, &st) < 0)
642 err(4, "%s", boot2path);
643 if (st.st_size > boot2size)
644 err(4, "%s must be <= %d bytes!", boot2path, boot2size);
645 if ((r = read(fd, boot2buf, boot2size)) < 1)
646 err(4, "%s is empty!", boot2path);
647 boot2size = (r + secsize - 1) & ~(secsize - 1);
651 * XXX dangerously dedicated support goes here XXX
657 display(FILE *f, const struct disklabel64 *lp)
659 const struct partition64 *pp;
666 * Use a human readable block size if possible. This is for
667 * display and editing purposes only.
669 if (lp->d_align > 1024)
672 blksize = lp->d_align;
674 fprintf(f, "# %s:\n", specname);
676 fprintf(f, "# Informational fields calculated from the above\n");
677 fprintf(f, "# All byte equivalent offsets must be aligned\n");
679 fprintf(f, "# boot space: %10llu bytes\n", lp->d_pbase - lp->d_bbase);
680 fprintf(f, "# data space: %10llu blocks\t# %6.2f MB (%llu bytes)\n",
681 (lp->d_pstop - lp->d_pbase) / blksize,
682 (double)(lp->d_pstop - lp->d_pbase) / 1024.0 / 1024.0,
683 lp->d_pstop - lp->d_pbase);
686 uuid_to_string(&lp->d_obj_uuid, &str, NULL);
687 fprintf(f, "diskid: %s\n", str ? str : "<unknown>");
690 fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
692 fprintf(f, "boot2 data base: 0x%012llx\n", lp->d_bbase);
693 fprintf(f, "partitions data base: 0x%012llx\n", lp->d_pbase);
694 fprintf(f, "partitions data stop: 0x%012llx\n", lp->d_pstop);
695 fprintf(f, "backup label: 0x%012llx\n", lp->d_abase);
696 fprintf(f, "total size: 0x%012llx\t# %6.2f MB\n",
698 (double)lp->d_total_size / 1024.0 / 1024.0);
699 fprintf(f, "alignment: %u\n", lp->d_align);
700 fprintf(f, "display block size: %u\t# for partition display only\n",
704 fprintf(f, "%u partitions:\n", lp->d_npartitions);
705 fprintf(f, "# size offset fstype fsuuid\n");
706 pp = lp->d_partitions;
708 for (part = 0; part < lp->d_npartitions; part++, pp++) {
709 const u_long onemeg = 1024 * 1024;
710 if (pp->p_bsize == 0)
713 fprintf(f, " %c: ", 'a' + part);
715 if (pp->p_bsize % lp->d_align)
716 fprintf(f, "%10s ", "ILLEGAL");
718 fprintf(f, "%10llu ", pp->p_bsize / blksize);
719 if (pp->p_boffset % lp->d_align)
720 fprintf(f, "%10s ", "ILLEGAL");
722 fprintf(f, "%10llu ", (pp->p_boffset - lp->d_pbase) / blksize);
723 if (pp->p_fstype < FSMAXTYPES)
724 fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
726 fprintf(f, "%8d", pp->p_fstype);
727 fprintf(f, "\t# %11.3fM", (double)pp->p_bsize / onemeg);
731 fprintf(f, "# EXAMPLE\n");
732 fprintf(f, "#a: 4g 0 4.2BSD\n");
733 fprintf(f, "#b: * * 4.2BSD\n");
740 edit(struct disklabel64 *lp, int f)
743 struct disklabel64 label;
746 if ((fd = mkstemp(tmpfil)) == -1 ||
747 (fp = fdopen(fd, "w")) == NULL) {
748 warnx("can't create %s", tmpfil);
756 fp = fopen(tmpfil, "r");
758 warnx("can't reopen %s for reading", tmpfil);
761 bzero((char *)&label, sizeof(label));
762 if (getasciilabel(fp, &label)) {
764 if (writelabel(f, lp) == 0) {
771 printf("re-edit the label? [y]: "); fflush(stdout);
773 if (c != EOF && c != (int)'\n')
774 while (getchar() != (int)'\n')
790 omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
791 while ((pid = fork()) < 0) {
792 if (errno == EPROCLIM) {
793 warnx("you have too many processes");
796 if (errno != EAGAIN) {
806 if ((ed = getenv("EDITOR")) == (char *)0)
808 execlp(ed, ed, tmpfil, (char *)0);
811 while ((xpid = wait(&status)) >= 0)
822 while (*cp != '\0' && isspace(*cp))
824 if (*cp == '\0' || *cp == '#')
834 while (*cp != '\0' && !isspace(*cp) && *cp != '#')
836 if ((c = *cp) != '\0') {
845 * Read an ascii label in from fd f,
846 * in the same format as that put out by display(),
850 getasciilabel(FILE *f, struct disklabel64 *lp)
854 char *tp, line[BUFSIZ];
856 uint32_t blksize = 0;
858 int lineno = 0, errors = 0;
861 bzero(&part_set, sizeof(part_set));
862 bzero(&part_size_type, sizeof(part_size_type));
863 bzero(&part_offset_type, sizeof(part_offset_type));
864 while (fgets(line, sizeof(line) - 1, f)) {
866 if ((cp = strchr(line,'\n')) != 0)
871 tp = strchr(cp, ':');
873 fprintf(stderr, "line %d: syntax error\n", lineno);
877 *tp++ = '\0', tp = skip(tp);
878 if (sscanf(cp, "%lu partitions", &v) == 1) {
879 if (v == 0 || v > MAXPARTITIONS64) {
881 "line %d: bad # of partitions\n", lineno);
882 lp->d_npartitions = MAXPARTITIONS64;
885 lp->d_npartitions = v;
891 if (streq(cp, "diskid")) {
893 uuid_from_string(tp, &lp->d_obj_uuid, &status);
894 if (status != uuid_s_ok) {
896 "line %d: %s: illegal UUID\n",
902 if (streq(cp, "label")) {
903 strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
907 if (streq(cp, "alignment")) {
908 v = strtoul(tp, NULL, 0);
909 if (v <= 0 || (v & DEV_BMASK) != 0 || v > 1024*1024) {
911 "line %d: %s: bad alignment\n",
919 if (streq(cp, "total size")) {
920 vv = strtoull(tp, NULL, 0);
921 if (vv == 0 || vv == (uint64_t)-1) {
922 fprintf(stderr, "line %d: %s: bad %s\n",
926 lp->d_total_size = vv;
930 if (streq(cp, "boot2 data base")) {
931 vv = strtoull(tp, NULL, 0);
932 if (vv == 0 || vv == (uint64_t)-1) {
933 fprintf(stderr, "line %d: %s: bad %s\n",
941 if (streq(cp, "partitions data base")) {
942 vv = strtoull(tp, NULL, 0);
943 if (vv == 0 || vv == (uint64_t)-1) {
944 fprintf(stderr, "line %d: %s: bad %s\n",
952 if (streq(cp, "partitions data stop")) {
953 vv = strtoull(tp, NULL, 0);
954 if (vv == 0 || vv == (uint64_t)-1) {
955 fprintf(stderr, "line %d: %s: bad %s\n",
963 if (streq(cp, "backup label")) {
964 vv = strtoull(tp, NULL, 0);
965 if (vv == 0 || vv == (uint64_t)-1) {
966 fprintf(stderr, "line %d: %s: bad %s\n",
974 if (streq(cp, "display block size")) {
975 v = strtoul(tp, NULL, 0);
976 if (v <= 0 || (v & DEV_BMASK) != 0 || v > 1024*1024) {
978 "line %d: %s: bad alignment\n",
987 /* the ':' was removed above */
988 if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
990 "line %d: %s: Unknown disklabel field\n", lineno,
996 /* Process a partition specification line. */
998 if (part >= lp->d_npartitions) {
1000 "line %d: partition name out of range a-%c: %s\n",
1001 lineno, 'a' + lp->d_npartitions - 1, cp);
1008 fprintf(stderr, "block size to use for partition "
1009 "display was not specified!\n");
1014 if (getasciipartspec(tp, lp, part, lineno, blksize) != 0) {
1019 errors += checklabel(lp);
1020 return (errors == 0);
1025 parse_field_val(char **tp, char **cp, u_int64_t *vv, int lineno)
1029 if (*tp == NULL || **tp == 0) {
1030 fprintf(stderr, "line %d: too few numeric fields\n", lineno);
1035 *vv = strtoull(*cp, &tmp, 0);
1036 if (*vv == ULLONG_MAX) {
1037 fprintf(stderr, "line %d: illegal number\n", lineno);
1047 * Read a partition line into partition `part' in the specified disklabel.
1048 * Return 0 on success, 1 on failure.
1051 getasciipartspec(char *tp, struct disklabel64 *lp, int part, int lineno, uint32_t blksize)
1053 struct partition64 *pp;
1061 pp = &lp->d_partitions[part];
1067 r = parse_field_val(&tp, &cp, &vv, lineno);
1093 r = 0; /* eat the suffix */
1096 Warning("unknown size specifier '%c' (*/%%/K/M/G are valid)",
1101 part_size_type[part] = r;
1102 if (vv == 0 && r != '*') {
1104 "line %d: %s: bad partition size (0)\n", lineno, cp);
1107 pp->p_bsize = vv * mpx;
1112 r = parse_field_val(&tp, &cp, &vv, lineno);
1115 part_offset_type[part] = r;
1121 pp->p_boffset = vv * blksize + lp->d_pbase;
1125 "line %d: %s: bad suffix on partition offset (%c)\n",
1135 for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
1136 if (*cpp && streq(*cpp, cp))
1139 pp->p_fstype = cpp - fstypenames;
1142 v = strtoul(cp, NULL, 0);
1145 if (v >= FSMAXTYPES) {
1147 "line %d: Warning, unknown filesystem type %s\n",
1156 fprintf(stderr, "line %d: Warning, extra data on line\n",
1163 * Check disklabel for errors and fill in
1164 * derived fields according to supplied values.
1167 checklabel(struct disklabel64 *lp)
1169 struct partition64 *pp;
1172 u_int64_t total_size;
1173 u_int64_t current_offset;
1174 u_long total_percent;
1175 int seen_default_offset;
1178 struct partition64 *pp2;
1181 if (lp->d_align < 512 ||
1182 (lp->d_align ^ (lp->d_align - 1)) != lp->d_align * 2 - 1) {
1183 Warning("Illegal alignment specified: %u\n", lp->d_align);
1186 if (lp->d_npartitions > MAXPARTITIONS64) {
1187 Warning("number of partitions (%u) > MAXPARTITIONS (%d)",
1188 lp->d_npartitions, MAXPARTITIONS64);
1191 off = offsetof(struct disklabel64, d_partitions[lp->d_npartitions]);
1192 off = (off + lp->d_align - 1) & ~(int64_t)(lp->d_align - 1);
1194 if (lp->d_bbase < off || lp->d_bbase % lp->d_align) {
1195 Warning("illegal boot2 data base ");
1198 if (lp->d_pbase < lp->d_bbase || lp->d_pbase % lp->d_align) {
1199 Warning("illegal partition data base");
1202 if (lp->d_pstop < lp->d_pbase || lp->d_pstop % lp->d_align) {
1203 Warning("illegal partition data stop");
1206 if (lp->d_pstop > lp->d_total_size) {
1207 printf("%012llx\n%012llx\n", lp->d_pstop, lp->d_total_size);
1208 Warning("disklabel control info is beyond the total size");
1212 (lp->d_abase < lp->d_pstop || lp->d_pstop % lp->d_align ||
1213 lp->d_abase > lp->d_total_size - off)) {
1214 Warning("illegal backup label location");
1218 /* first allocate space to the partitions, then offsets */
1219 total_size = 0; /* in bytes */
1220 total_percent = 0; /* in percent */
1222 /* find all fixed partitions */
1223 for (i = 0; i < (int)lp->d_npartitions; i++) {
1224 pp = &lp->d_partitions[i];
1226 if (part_size_type[i] == '*') {
1228 Warning("Too many '*' partitions (%c and %c)",
1229 hog_part + 'a',i + 'a');
1236 if (part_size_type[i] == '%') {
1238 * don't count %'s yet
1240 total_percent += size;
1243 * Value has already been converted
1246 if (size % lp->d_align != 0) {
1247 Warning("partition %c's size is not properly aligned",
1255 /* handle % partitions - note %'s don't need to add up to 100! */
1256 if (total_percent != 0) {
1260 free_space = (int64_t)(lp->d_pstop - lp->d_pbase - total_size);
1261 space_left = free_space;
1262 if (total_percent > 100) {
1263 fprintf(stderr,"total percentage %lu is greater than 100\n",
1268 if (free_space > 0) {
1269 for (i = 0; i < (int)lp->d_npartitions; i++) {
1270 pp = &lp->d_partitions[i];
1271 if (part_set[i] && part_size_type[i] == '%') {
1272 /* careful of overflows! and integer roundoff */
1273 pp->p_bsize = ((double)pp->p_bsize/100) * free_space;
1274 pp->p_bsize = (pp->p_bsize + lp->d_align - 1) & ~(u_int64_t)(lp->d_align - 1);
1275 if ((int64_t)pp->p_bsize > space_left)
1276 pp->p_bsize = (u_int64_t)space_left;
1277 total_size += pp->p_bsize;
1278 space_left -= pp->p_bsize;
1283 "%lld bytes available to give to '*' and '%%' partitions\n",
1286 /* fix? set all % partitions to size 0? */
1289 /* give anything remaining to the hog partition */
1290 if (hog_part != -1) {
1291 lp->d_partitions[hog_part].p_bsize = lp->d_pstop - lp->d_pbase - total_size;
1292 total_size = lp->d_pstop - lp->d_pbase;
1295 /* Now set the offsets for each partition */
1296 current_offset = lp->d_pbase;
1297 seen_default_offset = 0;
1298 for (i = 0; i < (int)lp->d_npartitions; i++) {
1300 pp = &lp->d_partitions[i];
1301 if (pp->p_bsize == 0)
1304 if (part_offset_type[i] == '*') {
1305 pp->p_boffset = current_offset;
1306 seen_default_offset = 1;
1308 /* allow them to be out of order for old-style tables */
1309 if (pp->p_boffset < current_offset &&
1310 seen_default_offset &&
1311 pp->p_fstype != FS_VINUM) {
1313 "Offset 0x%012llx for partition %c overlaps previous partition which ends at 0x%012llx\n",
1314 pp->p_boffset, i + 'a',
1317 "Labels with any *'s for offset must be in ascending order by sector\n");
1319 } else if (pp->p_boffset != current_offset &&
1320 seen_default_offset) {
1322 * this may give unneeded warnings if
1323 * partitions are out-of-order
1326 "Offset 0x%012llx for partition %c doesn't match expected value 0x%012llx",
1327 pp->p_boffset, i + 'a',
1331 current_offset = pp->p_boffset + pp->p_bsize;
1335 for (i = 0; i < (int)lp->d_npartitions; i++) {
1337 pp = &lp->d_partitions[i];
1338 if (pp->p_bsize == 0 && pp->p_boffset != 0)
1339 Warning("partition %c: size 0, but offset 0x%012llx",
1340 part, pp->p_boffset);
1341 if (pp->p_bsize == 0) {
1346 if (pp->p_boffset < lp->d_pbase) {
1348 "partition %c: offset out of bounds (%lld)\n",
1349 part, pp->p_boffset - lp->d_pbase);
1352 if (pp->p_boffset > lp->d_pstop) {
1354 "partition %c: offset out of bounds (%lld)\n",
1355 part, pp->p_boffset - lp->d_pbase);
1358 if (pp->p_boffset + pp->p_bsize > lp->d_pstop) {
1360 "partition %c: size out of bounds (%lld)\n",
1361 part, pp->p_boffset - lp->d_pbase);
1365 /* check for overlaps */
1366 /* this will check for all possible overlaps once and only once */
1367 for (j = 0; j < i; j++) {
1368 pp2 = &lp->d_partitions[j];
1369 if (pp->p_fstype != FS_VINUM &&
1370 pp2->p_fstype != FS_VINUM &&
1371 part_set[i] && part_set[j]) {
1372 if (pp2->p_boffset < pp->p_boffset + pp->p_bsize &&
1373 (pp2->p_boffset + pp2->p_bsize > pp->p_boffset ||
1374 pp2->p_boffset >= pp->p_boffset)) {
1375 fprintf(stderr,"partitions %c and %c overlap!\n",
1382 for (; i < (int)lp->d_npartitions; i++) {
1384 pp = &lp->d_partitions[i];
1385 if (pp->p_bsize || pp->p_boffset)
1386 Warning("unused partition %c: size 0x%012llx offset 0x%012llx",
1387 'a' + i, pp->p_bsize, pp->p_boffset);
1393 * When operating on a "virgin" disk, try getting an initial label
1394 * from the associated device driver. This might work for all device
1395 * drivers that are able to fetch some initial device parameters
1396 * without even having access to a (BSD) disklabel, like SCSI disks,
1397 * most IDE drives, or vn devices.
1399 * The device name must be given in its "canonical" form.
1401 static struct disklabel64 dlab;
1403 struct disklabel64 *
1404 getvirginlabel(void)
1406 struct disklabel64 *dl = &dlab;
1410 if (dkname[0] == '/') {
1411 warnx("\"auto\" requires the usage of a canonical disk name");
1414 asprintf(&path, "%s%s", _PATH_DEV, dkname);
1415 if ((f = open(path, O_RDONLY)) == -1) {
1416 warn("cannot open %s", path);
1421 * Try to use the new get-virgin-label ioctl. If it fails,
1422 * fallback to the old get-disk-info ioctl.
1424 if (ioctl(f, DIOCGDVIRGIN64, dl) < 0) {
1425 l_perror("ioctl DIOCGDVIRGIN64");
1435 Warning(const char *fmt, ...)
1439 fprintf(stderr, "Warning, ");
1441 vfprintf(stderr, fmt, ap);
1442 fprintf(stderr, "\n");
1449 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",
1450 "usage: disklabel [-r] disk",
1451 "\t\t(to read label)",
1452 " disklabel -w [-r] [-n] disk type [ packid ]",
1453 "\t\t(to write label with existing boot program)",
1454 " disklabel -e [-r] [-n] disk",
1455 "\t\t(to edit label)",
1456 " disklabel -R [-r] [-n] disk protofile",
1457 "\t\t(to restore label with existing boot program)",
1458 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1459 "\t\t(to install boot program with existing label)",
1460 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1461 "\t\t(to write label and boot program)",
1462 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1463 "\t\t(to restore label and boot program)",
1464 " disklabel [-NW] disk",
1465 "\t\t(to write disable/enable label)");