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. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)disklabel.c 1.2 (Symmetric) 11/28/85
66 * @(#)disklabel.c 8.2 (Berkeley) 1/7/94
67 * $FreeBSD: src/sbin/disklabel/disklabel.c,v 1.28.2.15 2003/01/24 16:18:16 des Exp $
70 #include <sys/param.h>
75 #include <sys/disklabel64.h>
76 #include <sys/diskslice.h>
77 #include <sys/diskmbr.h>
78 #include <sys/dtype.h>
79 #include <sys/sysctl.h>
83 #include <vfs/ufs/dinode.h>
84 #include <vfs/ufs/fs.h>
97 #include "pathnames.h"
99 extern uint32_t crc32(const void *buf
, size_t size
);
102 * Disklabel64: read and write 64 bit disklabels.
103 * The label is usually placed on one of the first sectors of the disk.
104 * Many machines also place a bootstrap in the same area,
105 * in which case the label is embedded in the bootstrap.
106 * The bootstrap source must leave space at the proper offset
107 * for the label on such machines.
110 #define LABELSIZE ((sizeof(struct disklabel64) + 4095) & ~4095)
111 #define BOOTSIZE 32768
113 /* FIX! These are too low, but are traditional */
114 #define DEFAULT_NEWFS_BLOCK 8192U
115 #define DEFAULT_NEWFS_FRAG 1024U
116 #define DEFAULT_NEWFS_CPG 16U
118 #define BIG_NEWFS_BLOCK 16384U
119 #define BIG_NEWFS_FRAG 2048U
120 #define BIG_NEWFS_CPG 64U
122 void makelabel(const char *, const char *, struct disklabel64
*);
123 int writelabel(int, struct disklabel64
*);
124 void l_perror(const char *);
125 struct disklabel64
*readlabel(int);
126 struct disklabel64
*makebootarea(int);
127 void display(FILE *, const struct disklabel64
*);
128 int edit(struct disklabel64
*, int);
132 int getasciilabel(FILE *, struct disklabel64
*);
133 int getasciipartspec(char *, struct disklabel64
*, int, int, uint32_t);
134 int getasciipartuuid(char *, struct disklabel64
*, int, int, uint32_t);
135 int checklabel(struct disklabel64
*);
136 void Warning(const char *, ...) __printflike(1, 2);
138 struct disklabel64
*getvirginlabel(void);
140 #define DEFEDITOR _PATH_VI
141 #define streq(a,b) (strcmp(a,b) == 0)
145 char tmpfil
[] = PATH_TMPFILE
;
147 struct disklabel64 lab
;
149 #define MAX_PART ('z')
150 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
151 char part_size_type
[MAX_NUM_PARTS
];
152 char part_offset_type
[MAX_NUM_PARTS
];
153 int part_set
[MAX_NUM_PARTS
];
155 int installboot
; /* non-zero if we should install a boot program */
165 UNSPEC
, EDIT
, NOWRITE
, READ
, RESTORE
, WRITE
, WRITEABLE
, WRITEBOOT
170 int disable_write
; /* set to disable writing to disk label */
171 u_int32_t slice_start_lba
;
175 #define OPTIONS "BNRWb:denrs:Vw"
177 #define OPTIONS "BNRWb:enrs:Vw"
181 main(int argc
, char *argv
[])
183 struct disklabel64
*lp
;
185 int ch
, f
= 0, flag
, error
= 0;
188 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != -1)
258 dkname
= getdevpath(argv
[0], 0);
260 f
= open(specname
, op
== READ
? O_RDONLY
: O_RDWR
);
262 err(4, "%s", specname
);
278 if (ioctl(f
, DIOCWLABEL
, (char *)&flag
) < 0)
279 err(4, "ioctl DIOCWLABEL");
287 error
= checklabel(lp
);
291 if (installboot
&& argc
== 3) {
292 makelabel(argv
[2], 0, &lab
);
296 * We only called makelabel() for its side effect
297 * of setting the bootstrap file names. Discard
298 * all changes to `lab' so that all values in the
299 * final label come from the ASCII label.
301 bzero((char *)&lab
, sizeof(lab
));
305 if (!(t
= fopen(argv
[1], "r")))
306 err(4, "%s", argv
[1]);
307 if (!getasciilabel(t
, &lab
))
309 lp
= makebootarea(f
);
310 bcopy(&lab
.d_magic
, &lp
->d_magic
,
311 sizeof(lab
) - offsetof(struct disklabel64
, d_magic
));
312 error
= writelabel(f
, lp
);
322 makelabel(argv
[1], name
, &lab
);
323 lp
= makebootarea(f
);
324 bcopy(&lab
.d_magic
, &lp
->d_magic
,
325 sizeof(lab
) - offsetof(struct disklabel64
, d_magic
));
326 if (checklabel(lp
) == 0)
327 error
= writelabel(f
, lp
);
332 if (ioctl(f
, DIOCWLABEL
, (char *)&flag
) < 0)
333 err(4, "ioctl DIOCWLABEL");
338 struct disklabel64 tlab
;
343 makelabel(argv
[1], 0, &lab
);
344 lp
= makebootarea(f
);
345 bcopy(&tlab
.d_magic
, &lp
->d_magic
,
346 sizeof(tlab
) - offsetof(struct disklabel64
, d_magic
));
347 if (checklabel(lp
) == 0)
348 error
= writelabel(f
, lp
);
356 * Construct a prototype disklabel from /etc/disktab. As a side
357 * effect, set the names of the primary and secondary boot files
361 makelabel(const char *type
, const char *name
, struct disklabel64
*lp
)
363 struct disklabel64
*dp
;
365 if (strcmp(type
, "auto") == 0)
366 dp
= getvirginlabel();
370 errx(1, "%s: unknown disk type", type
);
374 * NOTE: boot control files may no longer be specified in disktab.
377 strncpy(lp
->d_packname
, name
, sizeof(lp
->d_packname
));
381 writelabel(int f
, struct disklabel64
*lp
)
383 struct disklabel64
*blp
;
389 lpsize
= offsetof(struct disklabel64
, d_partitions
[lp
->d_npartitions
]);
390 lpcrcsize
= lpsize
- offsetof(struct disklabel64
, d_magic
);
393 Warning("write to disk label suppressed - label was as follows:");
397 lp
->d_magic
= DISKMAGIC64
;
399 lp
->d_crc
= crc32(&lp
->d_magic
, lpcrcsize
);
402 * Make sure the boot area is not too large
405 int lpbsize
= (int)(lp
->d_pbase
- lp
->d_bbase
);
406 if (lp
->d_pbase
== 0) {
407 errx(1, "no space was set aside in "
408 "the disklabel for boot2!");
410 if (boot2size
> lpbsize
) {
411 errx(1, "label did not reserve enough "
412 "space for boot! %d/%d",
418 * First set the kernel disk label,
419 * then write a label to the raw disk.
420 * If the SDINFO ioctl fails because it is
421 * unimplemented, keep going; otherwise, the kernel
422 * consistency checks may prevent us from changing
423 * the current (in-core) label.
425 if (ioctl(f
, DIOCSDINFO64
, lp
) < 0 &&
426 errno
!= ENODEV
&& errno
!= ENOTTY
) {
427 l_perror("ioctl DIOCSDINFO");
430 lseek(f
, (off_t
)0, SEEK_SET
);
433 * The disklabel embeds areas which we may not
434 * have wanted to change. Merge those areas in
437 blp
= makebootarea(f
);
439 bcopy(&lp
->d_magic
, &blp
->d_magic
,
441 offsetof(struct disklabel64
, d_magic
));
445 * write enable label sector before write
446 * (if necessary), disable after writing.
449 if (ioctl(f
, DIOCWLABEL
, &flag
) < 0)
450 warn("ioctl DIOCWLABEL");
452 r
= write(f
, boot1buf
, boot1lsize
);
453 if (r
!= (ssize_t
)boot1lsize
) {
458 * Output the remainder of the disklabel
461 lseek(f
, lp
->d_bbase
, 0);
462 r
= write(f
, boot2buf
, boot2size
);
463 if (r
!= boot2size
) {
469 ioctl(f
, DIOCWLABEL
, &flag
);
470 } else if (ioctl(f
, DIOCWDINFO64
, lp
) < 0) {
471 l_perror("ioctl DIOCWDINFO64");
479 l_perror(const char *s
)
484 warnx("%s: no disk label on disk;", s
);
485 fprintf(stderr
, "add \"-r\" to install initial label\n");
489 warnx("%s: label magic number or checksum is wrong!", s
);
490 fprintf(stderr
, "(disklabel or kernel is out of date?)\n");
494 warnx("%s: open partition would move or shrink", s
);
498 warnx("%s: the disk already has a label of a different type,\n"
499 "probably a 32 bit disklabel. It must be cleaned out "
510 * Fetch disklabel for disk.
511 * Use ioctl to get label unless -r flag is given.
516 struct disklabel64
*lp
;
522 * Allocate space for the label. The boot1 code, if any,
523 * is embedded in the label. The label overlaps the boot1
526 lp
= makebootarea(f
);
527 lpcrcsize
= offsetof(struct disklabel64
,
528 d_partitions
[lp
->d_npartitions
]) -
529 offsetof(struct disklabel64
, d_magic
);
532 if (lp
->d_magic
!= DISKMAGIC64
)
533 errx(1, "bad pack magic number");
534 if (lp
->d_npartitions
> MAXPARTITIONS64
||
535 savecrc
!= crc32(&lp
->d_magic
, lpcrcsize
)
537 errx(1, "corrupted disklabel64");
542 * Just use a static structure to hold the label. Note
543 * that DIOCSDINFO64 does not overwrite the boot1 area
544 * even though it is part of the disklabel64 structure.
548 if (ioctl(f
, DIOCGDVIRGIN64
, lp
) < 0) {
549 l_perror("ioctl DIOCGDVIRGIN64");
553 if (ioctl(f
, DIOCGDINFO64
, lp
) < 0) {
554 l_perror("ioctl DIOCGDINFO64");
563 * Construct a boot area for boot1 and boot2 and return the location of
564 * the label within the area. The caller will overwrite the label so
565 * we don't actually have to read it.
570 struct disklabel64
*lp
;
571 struct partinfo info
;
577 if (ioctl(f
, DIOCGPART
, &info
) == 0)
578 secsize
= info
.media_blksize
;
582 if (boot1buf
== NULL
) {
585 rsize
= roundup2(sizeof(struct disklabel64
), secsize
);
586 boot1size
= offsetof(struct disklabel64
, d_magic
);
588 boot1buf
= malloc(rsize
);
589 bzero(boot1buf
, rsize
);
590 r
= read(f
, boot1buf
, rsize
);
592 err(4, "%s", specname
);
594 lp
= (void *)boot1buf
;
596 if (installboot
== 0)
599 if (boot2buf
== NULL
) {
601 boot2buf
= malloc(boot2size
);
602 bzero(boot2buf
, boot2size
);
606 * If installing the boot code, read it into the appropriate portions
609 if (boot1path
== NULL
)
610 asprintf(&boot1path
, "%s/boot1_64", _PATH_BOOTDIR
);
611 if (boot2path
== NULL
)
612 asprintf(&boot2path
, "%s/boot2_64", _PATH_BOOTDIR
);
614 if ((fd
= open(boot1path
, O_RDONLY
)) < 0)
615 err(4, "%s", boot1path
);
616 if (fstat(fd
, &st
) < 0)
617 err(4, "%s", boot1path
);
618 if (st
.st_size
> boot1size
)
619 err(4, "%s must be exactly %d bytes!", boot1path
, boot1size
);
620 if (read(fd
, boot1buf
, boot1size
) != boot1size
)
621 err(4, "%s must be exactly %d bytes!", boot1path
, boot1size
);
624 if ((fd
= open(boot2path
, O_RDONLY
)) < 0)
625 err(4, "%s", boot2path
);
626 if (fstat(fd
, &st
) < 0)
627 err(4, "%s", boot2path
);
628 if (st
.st_size
> boot2size
)
629 err(4, "%s must be <= %d bytes!", boot2path
, boot2size
);
630 if ((r
= read(fd
, boot2buf
, boot2size
)) < 1)
631 err(4, "%s is empty!", boot2path
);
632 boot2size
= roundup2(r
, secsize
);
636 * XXX dangerously dedicated support goes here XXX
642 display(FILE *f
, const struct disklabel64
*lp
)
644 const struct partition64
*pp
;
651 * Use a human readable block size if possible. This is for
652 * display and editing purposes only.
654 if (lp
->d_align
> 1024)
657 blksize
= lp
->d_align
;
659 fprintf(f
, "# %s:\n", specname
);
661 fprintf(f
, "# Informational fields calculated from the above\n");
662 fprintf(f
, "# All byte equivalent offsets must be aligned\n");
664 fprintf(f
, "# boot space: %10ju bytes\n",
665 (intmax_t)(lp
->d_pbase
- lp
->d_bbase
));
666 fprintf(f
, "# data space: %10ju blocks\t# %6.2f MB (%ju bytes)\n",
667 (intmax_t)(lp
->d_pstop
- lp
->d_pbase
) / blksize
,
668 (double)(lp
->d_pstop
- lp
->d_pbase
) / 1024.0 / 1024.0,
669 (intmax_t)(lp
->d_pstop
- lp
->d_pbase
));
671 fprintf(f
, "# NOTE: If the partition data base looks odd it may be\n");
672 fprintf(f
, "# physically aligned instead of slice-aligned\n");
675 uuid_to_string(&lp
->d_stor_uuid
, &str
, NULL
);
676 fprintf(f
, "diskid: %s\n", str
? str
: "<unknown>");
679 fprintf(f
, "label: %.*s\n", (int)sizeof(lp
->d_packname
),
681 fprintf(f
, "boot2 data base: 0x%012jx\n", (intmax_t)lp
->d_bbase
);
682 fprintf(f
, "partitions data base: 0x%012jx\n", (intmax_t)lp
->d_pbase
);
683 fprintf(f
, "partitions data stop: 0x%012jx\n", (intmax_t)lp
->d_pstop
);
684 fprintf(f
, "backup label: 0x%012jx\n", (intmax_t)lp
->d_abase
);
685 fprintf(f
, "total size: 0x%012jx\t# %6.2f MB\n",
686 (intmax_t)lp
->d_total_size
,
687 (double)lp
->d_total_size
/ 1024.0 / 1024.0);
688 fprintf(f
, "alignment: %u\n", lp
->d_align
);
689 fprintf(f
, "display block size: %u\t# for partition display only\n",
693 fprintf(f
, "%u partitions:\n", lp
->d_npartitions
);
694 fprintf(f
, "# size offset fstype fsuuid\n");
696 for (part
= 0; part
< lp
->d_npartitions
; part
++) {
697 pp
= &lp
->d_partitions
[part
];
698 const u_long onemeg
= 1024 * 1024;
700 if (pp
->p_bsize
== 0)
703 fprintf(f
, " %c: ", 'a' + part
);
705 if (pp
->p_bsize
% lp
->d_align
)
706 fprintf(f
, "%10s ", "ILLEGAL");
708 fprintf(f
, "%10ju ", (intmax_t)pp
->p_bsize
/ blksize
);
710 if ((pp
->p_boffset
- lp
->d_pbase
) % lp
->d_align
)
711 fprintf(f
, "%10s ", "ILLEGAL");
714 (intmax_t)(pp
->p_boffset
- lp
->d_pbase
) / blksize
);
716 if (pp
->p_fstype
< FSMAXTYPES
)
717 fprintf(f
, "%8.8s", fstypenames
[pp
->p_fstype
]);
719 fprintf(f
, "%8d", pp
->p_fstype
);
720 fprintf(f
, "\t# %11.3fMB", (double)pp
->p_bsize
/ onemeg
);
723 for (part
= 0; part
< lp
->d_npartitions
; part
++) {
724 pp
= &lp
->d_partitions
[part
];
726 if (pp
->p_bsize
== 0)
729 if (uuid_is_nil(&lp
->d_stor_uuid
, NULL
) == 0) {
730 fprintf(f
, " %c-stor_uuid: ", 'a' + part
);
732 uuid_to_string(&pp
->p_stor_uuid
, &str
, NULL
);
734 fprintf(f
, "%s", str
);
741 fprintf(f
, "# EXAMPLE\n");
742 fprintf(f
, "#a: 4g 0 4.2BSD\n");
743 fprintf(f
, "#a: * * 4.2BSD\n");
750 edit(struct disklabel64
*lp
, int f
)
753 struct disklabel64 label
;
756 if ((fd
= mkstemp(tmpfil
)) == -1 ||
757 (fp
= fdopen(fd
, "w")) == NULL
) {
758 warnx("can't create %s", tmpfil
);
766 fp
= fopen(tmpfil
, "r");
768 warnx("can't reopen %s for reading", tmpfil
);
771 bzero((char *)&label
, sizeof(label
));
772 if (getasciilabel(fp
, &label
)) {
774 if (writelabel(f
, lp
) == 0) {
781 printf("re-edit the label? [y]: "); fflush(stdout
);
783 if (c
!= EOF
&& c
!= (int)'\n')
784 while (getchar() != (int)'\n')
800 omask
= sigblock(sigmask(SIGINT
)|sigmask(SIGQUIT
)|sigmask(SIGHUP
));
801 while ((pid
= fork()) < 0) {
802 if (errno
== EPROCLIM
) {
803 warnx("you have too many processes");
806 if (errno
!= EAGAIN
) {
816 if ((ed
= getenv("EDITOR")) == NULL
)
818 execlp(ed
, ed
, tmpfil
, NULL
);
821 while ((xpid
= wait(&status
)) >= 0)
832 while (*cp
!= '\0' && isspace(*cp
))
834 if (*cp
== '\0' || *cp
== '#')
844 while (*cp
!= '\0' && !isspace(*cp
) && *cp
!= '#')
846 if ((c
= *cp
) != '\0') {
855 * Read an ascii label in from fd f,
856 * in the same format as that put out by display(),
860 getasciilabel(FILE *f
, struct disklabel64
*lp
)
864 char *tp
, line
[BUFSIZ
];
866 uint32_t blksize
= 0;
868 int lineno
= 0, errors
= 0;
871 bzero(&part_set
, sizeof(part_set
));
872 bzero(&part_size_type
, sizeof(part_size_type
));
873 bzero(&part_offset_type
, sizeof(part_offset_type
));
874 while (fgets(line
, sizeof(line
) - 1, f
)) {
876 if ((cp
= strchr(line
,'\n')) != NULL
)
881 tp
= strchr(cp
, ':');
883 fprintf(stderr
, "line %d: syntax error\n", lineno
);
887 *tp
++ = '\0', tp
= skip(tp
);
888 if (sscanf(cp
, "%lu partitions", &v
) == 1) {
889 if (v
== 0 || v
> MAXPARTITIONS64
) {
891 "line %d: bad # of partitions\n", lineno
);
892 lp
->d_npartitions
= MAXPARTITIONS64
;
895 lp
->d_npartitions
= v
;
901 if (streq(cp
, "diskid")) {
903 uuid_from_string(tp
, &lp
->d_stor_uuid
, &status
);
904 if (status
!= uuid_s_ok
) {
906 "line %d: %s: illegal UUID\n",
912 if (streq(cp
, "label")) {
913 strncpy(lp
->d_packname
, tp
, sizeof (lp
->d_packname
));
917 if (streq(cp
, "alignment")) {
918 v
= strtoul(tp
, NULL
, 0);
919 if (v
<= 0 || (v
& DEV_BMASK
) != 0 || v
> 1024*1024) {
921 "line %d: %s: bad alignment\n",
929 if (streq(cp
, "total size")) {
930 vv
= strtoull(tp
, NULL
, 0);
931 if (vv
== 0 || vv
== (uint64_t)-1) {
932 fprintf(stderr
, "line %d: %s: bad %s\n",
936 lp
->d_total_size
= vv
;
940 if (streq(cp
, "boot2 data base")) {
941 vv
= strtoull(tp
, NULL
, 0);
942 if (vv
== 0 || vv
== (uint64_t)-1) {
943 fprintf(stderr
, "line %d: %s: bad %s\n",
951 if (streq(cp
, "partitions data base")) {
952 vv
= strtoull(tp
, NULL
, 0);
953 if (vv
== 0 || vv
== (uint64_t)-1) {
954 fprintf(stderr
, "line %d: %s: bad %s\n",
962 if (streq(cp
, "partitions data stop")) {
963 vv
= strtoull(tp
, NULL
, 0);
964 if (vv
== 0 || vv
== (uint64_t)-1) {
965 fprintf(stderr
, "line %d: %s: bad %s\n",
973 if (streq(cp
, "backup label")) {
974 vv
= strtoull(tp
, NULL
, 0);
975 if (vv
== 0 || vv
== (uint64_t)-1) {
976 fprintf(stderr
, "line %d: %s: bad %s\n",
984 if (streq(cp
, "display block size")) {
985 v
= strtoul(tp
, NULL
, 0);
986 if (v
<= 0 || (v
& DEV_BMASK
) != 0 || v
> 1024*1024) {
987 fprintf(stderr
, "line %d: %s: bad %s\n",
996 /* the ':' was removed above */
999 * Handle main partition data, e.g. a:, b:, etc.
1001 if (*cp
< 'a' || *cp
> MAX_PART
) {
1003 "line %d: %s: Unknown disklabel field\n", lineno
,
1009 /* Process a partition specification line. */
1011 if (part
>= lp
->d_npartitions
) {
1013 "line %d: partition name out of range a-%c: %s\n",
1014 lineno
, 'a' + lp
->d_npartitions
- 1, cp
);
1020 fprintf(stderr
, "block size to use for partition "
1021 "display was not specified!\n");
1026 if (strcmp(cp
+ 1, "-stor_uuid") == 0) {
1027 if (getasciipartuuid(tp
, lp
, part
, lineno
, blksize
)) {
1032 } else if (cp
[1] == 0) {
1034 if (getasciipartspec(tp
, lp
, part
, lineno
, blksize
)) {
1040 fprintf(stderr
, "line %d: %s: Unknown disklabel field\n",
1045 errors
+= checklabel(lp
);
1046 return (errors
== 0);
1051 parse_field_val(char **tp
, char **cp
, u_int64_t
*vv
, int lineno
)
1055 if (*tp
== NULL
|| **tp
== 0) {
1056 fprintf(stderr
, "line %d: too few numeric fields\n", lineno
);
1061 *vv
= strtoull(*cp
, &tmp
, 0);
1062 if (*vv
== ULLONG_MAX
) {
1063 fprintf(stderr
, "line %d: illegal number\n", lineno
);
1073 * Read a partition line into partition `part' in the specified disklabel.
1074 * Return 0 on success, 1 on failure.
1077 getasciipartspec(char *tp
, struct disklabel64
*lp
, int part
,
1078 int lineno
, uint32_t blksize
)
1080 struct partition64
*pp
;
1088 pp
= &lp
->d_partitions
[part
];
1094 r
= parse_field_val(&tp
, &cp
, &vv
, lineno
);
1124 r
= 0; /* eat the suffix */
1127 Warning("unknown size specifier '%c' (*/%%/K/M/G/T are valid)",
1132 part_size_type
[part
] = r
;
1133 if (vv
== 0 && r
!= '*') {
1135 "line %d: %s: bad partition size (0)\n", lineno
, cp
);
1138 pp
->p_bsize
= vv
* mpx
;
1143 r
= parse_field_val(&tp
, &cp
, &vv
, lineno
);
1146 part_offset_type
[part
] = r
;
1152 pp
->p_boffset
= vv
* blksize
+ lp
->d_pbase
;
1156 "line %d: %s: bad suffix on partition offset (%c)\n",
1166 "line %d: no filesystem type was specified\n", lineno
);
1171 for (cpp
= fstypenames
; cpp
< &fstypenames
[FSMAXTYPES
]; cpp
++) {
1172 if (*cpp
&& strcasecmp(*cpp
, cp
) == 0)
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 ");
1258 * pbase can be unaligned slice-relative but will be
1259 * aligned physically.
1261 if (lp
->d_pbase
< lp
->d_bbase
) {
1262 Warning("illegal partition data base");
1265 if (lp
->d_pstop
< lp
->d_pbase
) {
1266 Warning("illegal partition data stop");
1269 if (lp
->d_pstop
> lp
->d_total_size
) {
1270 printf("%012jx\n%012jx\n",
1271 (intmax_t)lp
->d_pstop
, (intmax_t)lp
->d_total_size
);
1272 Warning("disklabel control info is beyond the total size");
1276 (lp
->d_abase
< lp
->d_pstop
||
1277 lp
->d_abase
> lp
->d_total_size
- off
)) {
1278 Warning("illegal backup label location");
1282 /* first allocate space to the partitions, then offsets */
1283 total_size
= 0; /* in bytes */
1284 total_percent
= 0; /* in percent */
1286 /* find all fixed partitions */
1287 for (i
= 0; i
< (int)lp
->d_npartitions
; i
++) {
1288 pp
= &lp
->d_partitions
[i
];
1290 if (part_size_type
[i
] == '*') {
1291 if (part_offset_type
[i
] != '*') {
1292 if (total_size
< pp
->p_boffset
)
1293 total_size
= pp
->p_boffset
;
1295 if (hog_part
!= -1) {
1296 Warning("Too many '*' partitions (%c and %c)",
1297 hog_part
+ 'a',i
+ 'a');
1305 if (part_size_type
[i
] == '%') {
1307 * don't count %'s yet
1309 total_percent
+= size
;
1312 * Value has already been converted
1315 if (size
% lp
->d_align
!= 0) {
1316 Warning("partition %c's size is not properly aligned",
1324 /* handle % partitions - note %'s don't need to add up to 100! */
1325 if (total_percent
!= 0) {
1329 free_space
= (int64_t)(lp
->d_pstop
- lp
->d_pbase
- total_size
);
1330 free_space
&= ~(u_int64_t
)(lp
->d_align
- 1);
1332 space_left
= free_space
;
1333 if (total_percent
> 100) {
1334 fprintf(stderr
,"total percentage %lu is greater than 100\n",
1339 if (free_space
> 0) {
1340 for (i
= 0; i
< (int)lp
->d_npartitions
; i
++) {
1341 pp
= &lp
->d_partitions
[i
];
1342 if (part_set
[i
] && part_size_type
[i
] == '%') {
1343 /* careful of overflows! and integer roundoff */
1344 pp
->p_bsize
= ((double)pp
->p_bsize
/100) * free_space
;
1345 pp
->p_bsize
= (pp
->p_bsize
+ lp
->d_align
- 1) & ~(u_int64_t
)(lp
->d_align
- 1);
1346 if ((int64_t)pp
->p_bsize
> space_left
)
1347 pp
->p_bsize
= (u_int64_t
)space_left
;
1348 total_size
+= pp
->p_bsize
;
1349 space_left
-= pp
->p_bsize
;
1353 fprintf(stderr
, "%jd bytes available to give to "
1354 "'*' and '%%' partitions\n",
1355 (intmax_t)free_space
);
1357 /* fix? set all % partitions to size 0? */
1360 /* give anything remaining to the hog partition */
1361 if (hog_part
!= -1) {
1362 off
= lp
->d_pstop
- lp
->d_pbase
- total_size
;
1363 off
&= ~(u_int64_t
)(lp
->d_align
- 1);
1364 lp
->d_partitions
[hog_part
].p_bsize
= off
;
1365 total_size
= lp
->d_pstop
- lp
->d_pbase
;
1368 /* Now set the offsets for each partition */
1369 current_offset
= lp
->d_pbase
;
1370 seen_default_offset
= 0;
1371 for (i
= 0; i
< (int)lp
->d_npartitions
; i
++) {
1373 pp
= &lp
->d_partitions
[i
];
1374 if (pp
->p_bsize
== 0)
1377 if (part_offset_type
[i
] == '*') {
1378 pp
->p_boffset
= current_offset
;
1379 seen_default_offset
= 1;
1381 /* allow them to be out of order for old-style tables */
1382 if (pp
->p_boffset
< current_offset
&&
1383 seen_default_offset
&&
1384 pp
->p_fstype
!= FS_VINUM
) {
1386 "Offset 0x%012jx for partition %c overlaps previous partition which ends at 0x%012jx\n",
1387 (intmax_t)pp
->p_boffset
,
1389 (intmax_t)current_offset
);
1391 "Labels with any *'s for offset must be in ascending order by sector\n");
1393 } else if (pp
->p_boffset
!= current_offset
&&
1394 seen_default_offset
) {
1396 * this may give unneeded warnings if
1397 * partitions are out-of-order
1400 "Offset 0x%012jx for partition %c doesn't match expected value 0x%012jx",
1401 pp
->p_boffset
, i
+ 'a',
1402 (intmax_t)current_offset
);
1405 current_offset
= pp
->p_boffset
+ pp
->p_bsize
;
1409 for (i
= 0; i
< (int)lp
->d_npartitions
; i
++) {
1411 pp
= &lp
->d_partitions
[i
];
1412 if (pp
->p_bsize
== 0 && pp
->p_boffset
!= 0)
1413 Warning("partition %c: size 0, but offset 0x%012jx",
1414 part
, (intmax_t)pp
->p_boffset
);
1415 if (pp
->p_bsize
== 0) {
1419 if (uuid_is_nil(&pp
->p_stor_uuid
, NULL
))
1420 uuid_create(&pp
->p_stor_uuid
, NULL
);
1422 if (pp
->p_boffset
< lp
->d_pbase
) {
1424 "partition %c: offset out of bounds (%jd)\n",
1425 part
, (intmax_t)(pp
->p_boffset
- lp
->d_pbase
));
1428 if (pp
->p_boffset
> lp
->d_pstop
) {
1430 "partition %c: offset out of bounds (%jd)\n",
1431 part
, (intmax_t)(pp
->p_boffset
- lp
->d_pbase
));
1434 if (pp
->p_boffset
+ pp
->p_bsize
> lp
->d_pstop
) {
1436 "partition %c: size out of bounds (%jd)\n",
1437 part
, (intmax_t)(pp
->p_boffset
- lp
->d_pbase
));
1441 /* check for overlaps */
1442 /* this will check for all possible overlaps once and only once */
1443 for (j
= 0; j
< i
; j
++) {
1444 pp2
= &lp
->d_partitions
[j
];
1445 if (pp
->p_fstype
!= FS_VINUM
&&
1446 pp2
->p_fstype
!= FS_VINUM
&&
1447 part_set
[i
] && part_set
[j
]) {
1448 if (pp2
->p_boffset
< pp
->p_boffset
+ pp
->p_bsize
&&
1449 (pp2
->p_boffset
+ pp2
->p_bsize
> pp
->p_boffset
||
1450 pp2
->p_boffset
>= pp
->p_boffset
)) {
1451 fprintf(stderr
,"partitions %c and %c overlap!\n",
1458 for (; i
< (int)lp
->d_npartitions
; i
++) {
1460 pp
= &lp
->d_partitions
[i
];
1461 if (pp
->p_bsize
|| pp
->p_boffset
)
1462 Warning("unused partition %c: size 0x%012jx "
1464 'a' + i
, (intmax_t)pp
->p_bsize
,
1465 (intmax_t)pp
->p_boffset
);
1471 * When operating on a "virgin" disk, try getting an initial label
1472 * from the associated device driver. This might work for all device
1473 * drivers that are able to fetch some initial device parameters
1474 * without even having access to a (BSD) disklabel, like SCSI disks,
1475 * most IDE drives, or vn devices.
1477 * The device name must be given in its "canonical" form.
1479 static struct disklabel64 dlab
;
1481 struct disklabel64
*
1482 getvirginlabel(void)
1484 struct disklabel64
*dl
= &dlab
;
1487 if ((f
= open(dkname
, O_RDONLY
)) == -1) {
1488 warn("cannot open %s", dkname
);
1493 * Try to use the new get-virgin-label ioctl. If it fails,
1494 * fallback to the old get-disk-info ioctl.
1496 if (ioctl(f
, DIOCGDVIRGIN64
, dl
) < 0) {
1497 l_perror("ioctl DIOCGDVIRGIN64");
1507 Warning(const char *fmt
, ...)
1511 fprintf(stderr
, "Warning, ");
1513 vfprintf(stderr
, fmt
, ap
);
1514 fprintf(stderr
, "\n");
1521 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",
1522 "usage: disklabel64 [-r] disk",
1523 "\t\t(to read label)",
1524 " disklabel64 -w [-r] [-n] disk type [packid]",
1525 "\t\t(to write label with existing boot program)",
1526 " disklabel64 -e [-r] [-n] disk",
1527 "\t\t(to edit label)",
1528 " disklabel64 -R [-r] [-n] disk protofile",
1529 "\t\t(to restore label with existing boot program)",
1530 " disklabel64 -B [-n] [-b boot1 -s boot2] disk [type]",
1531 "\t\t(to install boot program with existing label)",
1532 " disklabel64 -w -B [-n] [-b boot1 -s boot2] disk type [packid]",
1533 "\t\t(to write label and boot program)",
1534 " disklabel64 -R -B [-n] [-b boot1 -s boot2] disk protofile [type]",
1535 "\t\t(to restore label and boot program)",
1536 " disklabel64 [-NW] disk",
1537 "\t\t(to write disable/enable label)");