2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Symmetric Computer Systems.
8 * Redistribution and use in source and binary forms, with or without
9 * 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 the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1987, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)disklabel.c 1.2 (Symmetric) 11/28/85
38 * @(#)disklabel.c 8.2 (Berkeley) 1/7/94
39 * $FreeBSD: src/sbin/disklabel/disklabel.c,v 1.28.2.15 2003/01/24 16:18:16 des Exp $
40 * $DragonFly: src/sbin/disklabel/disklabel.c,v 1.27 2008/04/23 21:59:22 thomas Exp $
43 #include <sys/param.h>
48 #include <sys/disklabel32.h>
49 #include <sys/disklabel64.h>
50 #include <sys/diskslice.h>
51 #include <sys/diskmbr.h>
52 #include <sys/dtype.h>
53 #include <sys/sysctl.h>
56 #include <vfs/ufs/dinode.h>
57 #include <vfs/ufs/fs.h>
69 #include "pathnames.h"
72 * Disklabel: read and write disklabels.
73 * The label is usually placed on one of the first sectors of the disk.
74 * Many machines also place a bootstrap in the same area,
75 * in which case the label is embedded in the bootstrap.
76 * The bootstrap source must leave space at the proper offset
77 * for the label on such machines.
81 #define BBSIZE 8192 /* size of boot area, with label */
84 /* FIX! These are too low, but are traditional */
85 #define DEFAULT_NEWFS_BLOCK 8192U
86 #define DEFAULT_NEWFS_FRAG 1024U
87 #define DEFAULT_NEWFS_CPG 16U
89 #define BIG_NEWFS_BLOCK 16384U
90 #define BIG_NEWFS_FRAG 2048U
91 #define BIG_NEWFS_CPG 64U
95 void makelabel(const char *, const char *, struct disklabel32
*);
96 int writelabel(int, const char *, struct disklabel32
*);
97 void l_perror(const char *);
98 struct disklabel32
*readlabel(int);
99 struct disklabel32
*makebootarea(char *, struct disklabel32
*, int);
100 void display(FILE *, const struct disklabel32
*);
101 int edit(struct disklabel32
*, int);
105 int getasciilabel(FILE *, struct disklabel32
*);
106 int getasciipartspec(char *, struct disklabel32
*, int, int);
107 int checklabel(struct disklabel32
*);
108 void setbootflag(struct disklabel32
*);
109 void Warning(const char *, ...) __printflike(1, 2);
111 int checkoldboot(int, const char *);
112 const char *fixlabel(int, struct disklabel32
*, int);
113 struct disklabel32
*getvirginlabel(void);
114 struct disklabel32
*getdisklabelfromdisktab(const char *name
);
116 #define DEFEDITOR _PATH_VI
117 #define streq(a,b) (strcmp(a,b) == 0)
121 char tmpfil
[] = PATH_TMPFILE
;
123 char namebuf
[BBSIZE
], *np
= namebuf
;
124 struct disklabel32 lab
;
125 char bootarea
[BBSIZE
];
127 #define MAX_PART ('z')
128 #define MAX_NUM_PARTS (1 + MAX_PART - 'a')
129 char part_size_type
[MAX_NUM_PARTS
];
130 char part_offset_type
[MAX_NUM_PARTS
];
131 int part_set
[MAX_NUM_PARTS
];
134 int installboot
; /* non-zero if we should install a boot program */
135 char *bootbuf
; /* pointer to buffer with remainder of boot prog */
136 int bootsize
; /* size of remaining boot program */
137 char *xxboot
; /* primary boot */
138 char *bootxx
; /* secondary boot */
139 char boot0
[MAXPATHLEN
];
140 char boot1
[MAXPATHLEN
];
144 UNSPEC
, EDIT
, NOWRITE
, READ
, RESTORE
, WRITE
, WRITEABLE
, WRITEBOOT
148 int disable_write
; /* set to disable writing to disk label */
150 u_int32_t slice_start_lba
;
154 #define OPTIONS "BNRWb:def:nrs:w"
156 #define OPTIONS "BNRWb:ef:nrs:w"
160 main(int argc
, char *argv
[])
162 struct disklabel32
*lp
;
164 int ch
, f
= 0, flag
, error
= 0;
167 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != -1)
179 slice_start_lba
= strtoul(optarg
, NULL
, 0);
247 if (dkname
[0] != '/') {
248 sprintf(np
, "%s%s", _PATH_DEV
, dkname
);
250 np
+= strlen(specname
) + 1;
254 f
= open(specname
, op
== READ
? O_RDONLY
: O_RDWR
);
255 if (f
< 0 && errno
== ENOENT
&& dkname
[0] != '/') {
256 sprintf(specname
, "%s%s", _PATH_DEV
, dkname
);
257 np
= namebuf
+ strlen(specname
) + 1;
258 f
= open(specname
, op
== READ
? O_RDONLY
: O_RDWR
);
261 err(4, "%s", specname
);
277 if (ioctl(f
, DIOCWLABEL
, (char *)&flag
) < 0)
278 err(4, "ioctl DIOCWLABEL");
286 error
= checklabel(lp
);
287 if (checkoldboot(f
, NULL
))
288 warnx("Warning, old bootblocks detected, install new bootblocks & reinstall the disklabel");
293 if (installboot
&& argc
== 3) {
294 makelabel(argv
[2], 0, &lab
);
298 * We only called makelabel() for its side effect
299 * of setting the bootstrap file names. Discard
300 * all changes to `lab' so that all values in the
301 * final label come from the ASCII label.
303 bzero((char *)&lab
, sizeof(lab
));
308 if (!(t
= fopen(argv
[1], "r")))
309 err(4, "%s", argv
[1]);
310 if (!getasciilabel(t
, &lab
))
312 lp
= makebootarea(bootarea
, &lab
, f
);
314 error
= writelabel(f
, bootarea
, lp
);
324 makelabel(argv
[1], name
, &lab
);
325 lp
= makebootarea(bootarea
, &lab
, f
);
327 if (checklabel(lp
) == 0)
328 error
= writelabel(f
, bootarea
, lp
);
333 if (ioctl(f
, DIOCWLABEL
, (char *)&flag
) < 0)
334 err(4, "ioctl DIOCWLABEL");
340 struct disklabel32 tlab
;
345 makelabel(argv
[1], 0, &lab
);
346 lp
= makebootarea(bootarea
, &lab
, f
);
348 if (checklabel(lp
) == 0)
349 error
= writelabel(f
, bootarea
, lp
);
358 * Construct a prototype disklabel from /etc/disktab. As a side
359 * effect, set the names of the primary and secondary boot files
363 makelabel(const char *type
, const char *name
, struct disklabel32
*lp
)
365 struct disklabel32
*dp
;
367 if (strcmp(type
, "auto") == 0)
368 dp
= getvirginlabel();
370 dp
= getdisklabelfromdisktab(type
);
372 errx(1, "%s: unknown disk type", type
);
376 * NOTE: boot control files may no longer be specified in disktab.
379 strncpy(lp
->d_packname
, name
, sizeof(lp
->d_packname
));
383 writelabel(int f
, const char *boot
, struct disklabel32
*lp
)
390 Warning("write to disk label suppressed - label was as follows:");
394 /* make sure we are not overwriting our boot code */
395 if (checkoldboot(f
, boot
))
396 errx(4, "Will not overwrite old bootblocks w/ label, install new boot blocks first!");
398 lp
->d_magic
= DISKMAGIC32
;
399 lp
->d_magic2
= DISKMAGIC32
;
401 lp
->d_checksum
= dkcksum32(lp
);
404 * First set the kernel disk label,
405 * then write a label to the raw disk.
406 * If the SDINFO ioctl fails because it is unimplemented,
407 * keep going; otherwise, the kernel consistency checks
408 * may prevent us from changing the current (in-core)
411 if (ioctl(f
, DIOCSDINFO32
, lp
) < 0 &&
412 errno
!= ENODEV
&& errno
!= ENOTTY
) {
413 l_perror("ioctl DIOCSDINFO32");
416 lseek(f
, (off_t
)0, SEEK_SET
);
419 * write enable label sector before write
420 * (if necessary), disable after writing.
423 if (ioctl(f
, DIOCWLABEL
, &flag
) < 0)
424 warn("ioctl DIOCWLABEL");
425 msg
= fixlabel(f
, lp
, 1);
430 r
= write(f
, boot
, lp
->d_bbsize
);
432 if (r
!= ((ssize_t
)lp
->d_bbsize
)) {
438 * Output the remainder of the disklabel
442 r
= write(f
, bootbuf
, bootsize
);
451 ioctl(f
, DIOCWLABEL
, &flag
);
452 } else if (ioctl(f
, DIOCWDINFO32
, lp
) < 0) {
453 l_perror("ioctl DIOCWDINFO32");
461 l_perror(const char *s
)
466 warnx("%s: no disk label on disk;", s
);
467 fprintf(stderr
, "add \"-r\" to install initial label\n");
471 warnx("%s: label magic number or checksum is wrong!", s
);
472 fprintf(stderr
, "(disklabel or kernel is out of date?)\n");
476 warnx("%s: open partition would move or shrink", s
);
480 warnx("%s: '%c' partition must start at beginning of disk",
485 warnx("%s: the disk already has a label of a different type,\n"
486 "probably a 64 bit disklabel. It must be cleaned out "
497 * Fetch disklabel for disk.
498 * Use ioctl to get label unless -r flag is given.
504 struct disklabel32
*lp
;
508 r
= read(f
, bootarea
, BBSIZE
);
510 err(4, "%s", specname
);
511 for (lp
= (struct disklabel32
*)bootarea
;
512 lp
<= (struct disklabel32
*)(bootarea
+ BBSIZE
- sizeof(*lp
));
513 lp
= (struct disklabel32
*)((char *)lp
+ 16)) {
514 if (lp
->d_magic
== DISKMAGIC32
&&
515 lp
->d_magic2
== DISKMAGIC32
)
518 if (lp
> (struct disklabel32
*)(bootarea
+BBSIZE
-sizeof(*lp
)) ||
519 lp
->d_magic
!= DISKMAGIC32
|| lp
->d_magic2
!= DISKMAGIC32
||
520 dkcksum32(lp
) != 0) {
521 errx(1, "bad pack magic number (label is damaged, "
522 "or pack is unlabeled)");
524 if ((msg
= fixlabel(f
, lp
, 0)) != NULL
)
528 if (ioctl(f
, DIOCGDINFO32
, lp
) < 0) {
529 l_perror("ioctl DIOCGDINFO32");
537 * Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
538 * Returns a pointer to the disklabel portion of the bootarea.
541 makebootarea(char *boot
, struct disklabel32
*dp
, int f
)
543 struct disklabel32
*lp
;
552 unsigned int i
, found
;
556 if (dp
->d_secsize
== 0) {
557 dp
->d_secsize
= DEV_BSIZE
;
558 dp
->d_bbsize
= BBSIZE
;
560 lp
= (struct disklabel32
*)
561 (boot
+ (LABELSECTOR32
* dp
->d_secsize
) + LABELOFFSET32
);
562 bzero((char *)lp
, sizeof *lp
);
565 * If we are not installing a boot program but we are installing a
566 * label on disk then we must read the current bootarea so we don't
567 * clobber the existing boot.
571 if (read(f
, boot
, BBSIZE
) < BBSIZE
)
572 err(4, "%s", specname
);
573 bzero((char *)lp
, sizeof *lp
);
578 * We are installing a boot program. Determine the name(s) and
579 * read them into the appropriate places in the boot area.
581 if (!xxboot
|| !bootxx
) {
583 if ((p
= strrchr(dkname
, '/')) == NULL
)
587 while (*p
&& !isdigit(*p
))
592 sprintf(boot0
, "%s/boot1", _PATH_BOOTDIR
);
597 sprintf(boot1
, "%s/boot2", _PATH_BOOTDIR
);
604 fprintf(stderr
, "bootstraps: xxboot = %s, bootxx = %s\n",
605 xxboot
, bootxx
? bootxx
: "NONE");
610 * 1. One-piece bootstrap (hp300/hp800)
611 * up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
612 * is remembered and written later following the bootarea.
613 * 2. Two-piece bootstraps (vax/i386?/mips?)
614 * up to d_secsize bytes of ``xxboot'' go in first d_secsize
615 * bytes of bootarea, remaining d_bbsize-d_secsize filled
618 b
= open(xxboot
, O_RDONLY
);
620 err(4, "%s", xxboot
);
625 * The i386 has the so-called fdisk table embedded into the
626 * primary bootstrap. We take care to not clobber it, but
627 * only if it does already contain some data. (Otherwise,
628 * the xxboot provides a template.)
630 if ((tmpbuf
= (char *)malloc((int)dp
->d_secsize
)) == 0)
631 err(4, "%s", xxboot
);
632 memcpy((void *)tmpbuf
, (void *)boot
, (int)dp
->d_secsize
);
634 if (read(b
, boot
, (int)dp
->d_secsize
) < 0)
635 err(4, "%s", xxboot
);
638 for (i
= DOSPARTOFF
, found
= 0;
639 !found
&& i
< DOSPARTOFF
+ NDOSPART
*sizeof(struct dos_partition
);
641 found
= tmpbuf
[i
] != 0;
643 memcpy((void *)&boot
[DOSPARTOFF
],
644 (void *)&tmpbuf
[DOSPARTOFF
],
645 NDOSPART
* sizeof(struct dos_partition
));
648 b
= open(bootxx
, O_RDONLY
);
650 err(4, "%s", bootxx
);
651 if (fstat(b
, &sb
) != 0)
652 err(4, "%s", bootxx
);
653 if (dp
->d_secsize
+ sb
.st_size
> dp
->d_bbsize
)
654 errx(4, "%s too large", bootxx
);
655 if (read(b
, &boot
[dp
->d_secsize
],
656 (int)(dp
->d_bbsize
-dp
->d_secsize
)) < 0)
657 err(4, "%s", bootxx
);
658 #else /* !(NUMBOOT > 1) */
659 if (read(b
, boot
, (int)dp
->d_bbsize
) < 0)
660 err(4, "%s", xxboot
);
661 if (fstat(b
, &sb
) != 0)
662 err(4, "%s", xxboot
);
663 bootsize
= (int)sb
.st_size
- dp
->d_bbsize
;
665 /* XXX assume d_secsize is a power of two */
666 bootsize
= (bootsize
+ dp
->d_secsize
-1) & ~(dp
->d_secsize
-1);
667 bootbuf
= (char *)malloc((size_t)bootsize
);
669 err(4, "%s", xxboot
);
670 if (read(b
, bootbuf
, bootsize
) < 0) {
672 err(4, "%s", xxboot
);
675 #endif /* NUMBOOT > 1 */
677 #endif /* NUMBOOT > 0 */
679 * Make sure no part of the bootstrap is written in the area
680 * reserved for the label.
682 for (p
= (char *)lp
; p
< (char *)lp
+ sizeof(struct disklabel32
); p
++)
684 errx(2, "bootstrap doesn't leave room for disk label");
689 display(FILE *f
, const struct disklabel32
*lp
)
692 const struct partition32
*pp
;
694 fprintf(f
, "# %s:\n", specname
);
695 if (lp
->d_type
< DKMAXTYPES
)
696 fprintf(f
, "type: %s\n", dktypenames
[lp
->d_type
]);
698 fprintf(f
, "type: %u\n", lp
->d_type
);
699 fprintf(f
, "disk: %.*s\n", (int)sizeof(lp
->d_typename
),
701 fprintf(f
, "label: %.*s\n", (int)sizeof(lp
->d_packname
),
703 fprintf(f
, "flags:");
705 fprintf(f
, "bytes/sector: %lu\n", (u_long
)lp
->d_secsize
);
706 fprintf(f
, "sectors/track: %lu\n", (u_long
)lp
->d_nsectors
);
707 fprintf(f
, "tracks/cylinder: %lu\n", (u_long
)lp
->d_ntracks
);
708 fprintf(f
, "sectors/cylinder: %lu\n", (u_long
)lp
->d_secpercyl
);
709 fprintf(f
, "cylinders: %lu\n", (u_long
)lp
->d_ncylinders
);
710 fprintf(f
, "sectors/unit: %lu\n", (u_long
)lp
->d_secperunit
);
711 fprintf(f
, "rpm: %u\n", lp
->d_rpm
);
712 fprintf(f
, "interleave: %u\n", lp
->d_interleave
);
713 fprintf(f
, "trackskew: %u\n", lp
->d_trackskew
);
714 fprintf(f
, "cylinderskew: %u\n", lp
->d_cylskew
);
715 fprintf(f
, "headswitch: %lu\t\t# milliseconds\n",
716 (u_long
)lp
->d_headswitch
);
717 fprintf(f
, "track-to-track seek: %ld\t# milliseconds\n",
718 (u_long
)lp
->d_trkseek
);
719 fprintf(f
, "drivedata: ");
720 for (i
= NDDATA32
- 1; i
>= 0; i
--) {
721 if (lp
->d_drivedata
[i
])
726 for (j
= 0; j
<= i
; j
++)
727 fprintf(f
, "%lu ", (u_long
)lp
->d_drivedata
[j
]);
728 fprintf(f
, "\n\n%u partitions:\n", lp
->d_npartitions
);
730 "# size offset fstype\n");
731 pp
= lp
->d_partitions
;
732 for (i
= 0; i
< lp
->d_npartitions
; i
++, pp
++) {
734 u_long onemeg
= 1024 * 1024 / lp
->d_secsize
;
735 fprintf(f
, " %c: ", 'a' + i
);
737 fprintf(f
, "%10lu ", (u_long
)pp
->p_size
);
738 fprintf(f
, "%10lu ", (u_long
)pp
->p_offset
);
739 if (pp
->p_fstype
< FSMAXTYPES
)
740 fprintf(f
, "%8.8s", fstypenames
[pp
->p_fstype
]);
742 fprintf(f
, "%8d", pp
->p_fstype
);
744 fprintf(f
, "\t# %11.3fMB", (double)pp
->p_size
/ onemeg
);
752 edit(struct disklabel32
*lp
, int f
)
755 struct disklabel32 label
;
758 if ((fd
= mkstemp(tmpfil
)) == -1 ||
759 (fp
= fdopen(fd
, "w")) == NULL
) {
760 warnx("can't create %s", tmpfil
);
768 fp
= fopen(tmpfil
, "r");
770 warnx("can't reopen %s for reading", tmpfil
);
773 bzero((char *)&label
, sizeof(label
));
774 if (getasciilabel(fp
, &label
)) {
776 if (writelabel(f
, bootarea
, lp
) == 0) {
783 printf("re-edit the label? [y]: "); fflush(stdout
);
785 if (c
!= EOF
&& c
!= (int)'\n')
786 while (getchar() != (int)'\n')
802 omask
= sigblock(sigmask(SIGINT
)|sigmask(SIGQUIT
)|sigmask(SIGHUP
));
803 while ((pid
= fork()) < 0) {
804 if (errno
== EPROCLIM
) {
805 warnx("you have too many processes");
808 if (errno
!= EAGAIN
) {
818 if ((ed
= getenv("EDITOR")) == (char *)0)
820 execlp(ed
, ed
, tmpfil
, (char *)0);
823 while ((xpid
= wait(&status
)) >= 0)
834 while (*cp
!= '\0' && isspace(*cp
))
836 if (*cp
== '\0' || *cp
== '#')
846 while (*cp
!= '\0' && !isspace(*cp
) && *cp
!= '#')
848 if ((c
= *cp
) != '\0') {
857 * Read an ascii label in from fd f,
858 * in the same format as that put out by display(),
862 getasciilabel(FILE *f
, struct disklabel32
*lp
)
867 char *tp
, line
[BUFSIZ
];
869 int lineno
= 0, errors
= 0;
872 char unknown
[] = "unknown";
874 bzero(&part_set
, sizeof(part_set
));
875 bzero(&part_size_type
, sizeof(part_size_type
));
876 bzero(&part_offset_type
, sizeof(part_offset_type
));
877 lp
->d_bbsize
= BBSIZE
; /* XXX */
878 lp
->d_sbsize
= SBSIZE
; /* XXX */
879 while (fgets(line
, sizeof(line
) - 1, f
)) {
881 if ((cp
= strchr(line
,'\n')) != 0)
886 tp
= strchr(cp
, ':');
888 fprintf(stderr
, "line %d: syntax error\n", lineno
);
892 *tp
++ = '\0', tp
= skip(tp
);
893 if (streq(cp
, "type")) {
897 for (; cpp
< &dktypenames
[DKMAXTYPES
]; cpp
++) {
898 if (*cpp
&& streq(*cpp
, tp
)) {
899 lp
->d_type
= cpp
- dktypenames
;
903 if (cpp
< &dktypenames
[DKMAXTYPES
])
905 v
= strtoul(tp
, NULL
, 10);
906 if (v
>= DKMAXTYPES
) {
907 fprintf(stderr
, "line %d:%s %lu\n", lineno
,
908 "Warning, unknown disk type", v
);
913 if (streq(cp
, "flags")) {
914 for (v
= 0; (cp
= tp
) && *cp
!= '\0';) {
916 if (streq(cp
, "removeable"))
917 v
|= 0; /* obsolete */
918 else if (streq(cp
, "ecc"))
919 v
|= 0; /* obsolete */
920 else if (streq(cp
, "badsect"))
921 v
|= 0; /* obsolete */
924 "line %d: %s: bad flag\n",
932 if (streq(cp
, "drivedata")) {
933 for (i
= 0; (cp
= tp
) && *cp
!= '\0' && i
< NDDATA32
;) {
934 lp
->d_drivedata
[i
++] = strtoul(cp
, NULL
, 10);
939 if (sscanf(cp
, "%lu partitions", &v
) == 1) {
940 if (v
== 0 || v
> MAXPARTITIONS32
) {
942 "line %d: bad # of partitions\n", lineno
);
943 lp
->d_npartitions
= MAXPARTITIONS32
;
946 lp
->d_npartitions
= v
;
951 if (streq(cp
, "disk")) {
952 strncpy(lp
->d_typename
, tp
, sizeof (lp
->d_typename
));
955 if (streq(cp
, "label")) {
956 strncpy(lp
->d_packname
, tp
, sizeof (lp
->d_packname
));
959 if (streq(cp
, "bytes/sector")) {
960 v
= strtoul(tp
, NULL
, 10);
961 if (v
== 0 || (v
% DEV_BSIZE
) != 0) {
963 "line %d: %s: bad sector size\n",
970 if (streq(cp
, "sectors/track")) {
971 v
= strtoul(tp
, NULL
, 10);
972 #if (ULONG_MAX != 0xffffffffUL)
973 if (v
== 0 || v
> 0xffffffff) {
977 fprintf(stderr
, "line %d: %s: bad %s\n",
984 if (streq(cp
, "sectors/cylinder")) {
985 v
= strtoul(tp
, NULL
, 10);
987 fprintf(stderr
, "line %d: %s: bad %s\n",
994 if (streq(cp
, "tracks/cylinder")) {
995 v
= strtoul(tp
, NULL
, 10);
997 fprintf(stderr
, "line %d: %s: bad %s\n",
1004 if (streq(cp
, "cylinders")) {
1005 v
= strtoul(tp
, NULL
, 10);
1007 fprintf(stderr
, "line %d: %s: bad %s\n",
1011 lp
->d_ncylinders
= v
;
1014 if (streq(cp
, "sectors/unit")) {
1015 v
= strtoul(tp
, NULL
, 10);
1017 fprintf(stderr
, "line %d: %s: bad %s\n",
1021 lp
->d_secperunit
= v
;
1024 if (streq(cp
, "rpm")) {
1025 v
= strtoul(tp
, NULL
, 10);
1026 if (v
== 0 || v
> USHRT_MAX
) {
1027 fprintf(stderr
, "line %d: %s: bad %s\n",
1034 if (streq(cp
, "interleave")) {
1035 v
= strtoul(tp
, NULL
, 10);
1036 if (v
== 0 || v
> USHRT_MAX
) {
1037 fprintf(stderr
, "line %d: %s: bad %s\n",
1041 lp
->d_interleave
= v
;
1044 if (streq(cp
, "trackskew")) {
1045 v
= strtoul(tp
, NULL
, 10);
1046 if (v
> USHRT_MAX
) {
1047 fprintf(stderr
, "line %d: %s: bad %s\n",
1051 lp
->d_trackskew
= v
;
1054 if (streq(cp
, "cylinderskew")) {
1055 v
= strtoul(tp
, NULL
, 10);
1056 if (v
> USHRT_MAX
) {
1057 fprintf(stderr
, "line %d: %s: bad %s\n",
1064 if (streq(cp
, "headswitch")) {
1065 v
= strtoul(tp
, NULL
, 10);
1066 lp
->d_headswitch
= v
;
1069 if (streq(cp
, "track-to-track seek")) {
1070 v
= strtoul(tp
, NULL
, 10);
1074 /* the ':' was removed above */
1075 if (*cp
< 'a' || *cp
> MAX_PART
|| cp
[1] != '\0') {
1077 "line %d: %s: Unknown disklabel field\n", lineno
,
1083 /* Process a partition specification line. */
1085 if (part
>= lp
->d_npartitions
) {
1087 "line %d: partition name out of range a-%c: %s\n",
1088 lineno
, 'a' + lp
->d_npartitions
- 1, cp
);
1094 if (getasciipartspec(tp
, lp
, part
, lineno
) != 0) {
1099 errors
+= checklabel(lp
);
1100 return (errors
== 0);
1103 #define NXTNUM(n) do { \
1105 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1108 cp = tp, tp = word(cp); \
1109 (n) = strtoul(cp, NULL, 10); \
1113 /* retain 1 character following number */
1114 #define NXTWORD(w,n) do { \
1116 fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
1120 cp = tp, tp = word(cp); \
1121 (n) = strtoul(cp, &tmp, 10); \
1122 if (tmp) (w) = *tmp; \
1127 * Read a partition line into partition `part' in the specified disklabel.
1128 * Return 0 on success, 1 on failure.
1131 getasciipartspec(char *tp
, struct disklabel32
*lp
, int part
, int lineno
)
1133 struct partition32
*pp
;
1138 pp
= &lp
->d_partitions
[part
];
1145 NXTWORD(part_size_type
[part
],v
);
1146 if (v
== 0 && part_size_type
[part
] != '*') {
1148 "line %d: %s: bad partition size\n", lineno
, cp
);
1157 NXTWORD(part_offset_type
[part
],v
);
1158 if (v
== 0 && part_offset_type
[part
] != '*' &&
1159 part_offset_type
[part
] != '\0') {
1161 "line %d: %s: bad partition offset\n", lineno
, cp
);
1171 for (cpp
= fstypenames
; cpp
< &fstypenames
[FSMAXTYPES
]; cpp
++)
1172 if (*cpp
&& streq(*cpp
, cp
))
1175 pp
->p_fstype
= cpp
- fstypenames
;
1178 v
= strtoul(cp
, NULL
, 10);
1181 if (v
>= FSMAXTYPES
) {
1183 "line %d: Warning, unknown filesystem type %s\n",
1196 fprintf(stderr
, "line %d: Warning, fragment, block, "
1197 "and bps/cpg fields are no\n"
1198 "longer supported and must be specified "
1199 "via newfs options instead.\n",
1206 * Check disklabel for errors and fill in
1207 * derived fields according to supplied values.
1210 checklabel(struct disklabel32
*lp
)
1212 struct partition32
*pp
;
1215 u_long total_size
, total_percent
, current_offset
;
1216 int seen_default_offset
;
1219 struct partition32
*pp2
;
1221 if (lp
->d_secsize
== 0) {
1222 fprintf(stderr
, "sector size 0\n");
1225 if (lp
->d_nsectors
== 0) {
1226 fprintf(stderr
, "sectors/track 0\n");
1229 if (lp
->d_ntracks
== 0) {
1230 fprintf(stderr
, "tracks/cylinder 0\n");
1233 if (lp
->d_ncylinders
== 0) {
1234 fprintf(stderr
, "cylinders/unit 0\n");
1238 Warning("revolutions/minute 0");
1239 if (lp
->d_secpercyl
== 0)
1240 lp
->d_secpercyl
= lp
->d_nsectors
* lp
->d_ntracks
;
1241 if (lp
->d_secperunit
== 0)
1242 lp
->d_secperunit
= lp
->d_secpercyl
* lp
->d_ncylinders
;
1243 if (lp
->d_bbsize
== 0) {
1244 fprintf(stderr
, "boot block size 0\n");
1246 } else if (lp
->d_bbsize
% lp
->d_secsize
)
1247 Warning("boot block size %% sector-size != 0");
1248 if (lp
->d_sbsize
== 0) {
1249 fprintf(stderr
, "super block size 0\n");
1251 } else if (lp
->d_sbsize
% lp
->d_secsize
)
1252 Warning("super block size %% sector-size != 0");
1253 if (lp
->d_npartitions
> MAXPARTITIONS32
)
1254 Warning("number of partitions (%lu) > MAXPARTITIONS (%d)",
1255 (u_long
)lp
->d_npartitions
, MAXPARTITIONS32
);
1257 /* first allocate space to the partitions, then offsets */
1258 total_size
= 0; /* in sectors */
1259 total_percent
= 0; /* in percent */
1261 /* find all fixed partitions */
1262 for (i
= 0; i
< lp
->d_npartitions
; i
++) {
1263 pp
= &lp
->d_partitions
[i
];
1266 if (part_size_type
[i
] == '*') {
1267 if (i
== RAW_PART
) {
1268 pp
->p_size
= lp
->d_secperunit
;
1270 if (part_offset_type
[i
] != '*') {
1271 if (total_size
< pp
->p_offset
)
1272 total_size
= pp
->p_offset
;
1275 Warning("Too many '*' partitions (%c and %c)",
1276 hog_part
+ 'a',i
+ 'a');
1284 switch (part_size_type
[i
]) {
1286 total_percent
+= size
;
1294 size
*= 1024ULL * 1024ULL;
1298 size
*= 1024ULL * 1024ULL * 1024ULL;
1303 Warning("unknown size specifier '%c' (K/M/G are valid)",part_size_type
[i
]);
1306 /* don't count %'s yet */
1307 if (part_size_type
[i
] != '%') {
1309 * for all not in sectors, convert to
1312 if (part_size_type
[i
] != '\0') {
1313 if (size
% lp
->d_secsize
!= 0)
1314 Warning("partition %c not an integer number of sectors",
1316 size
/= lp
->d_secsize
;
1319 /* else already in sectors */
1326 /* handle % partitions - note %'s don't need to add up to 100! */
1327 if (total_percent
!= 0) {
1328 long free_space
= lp
->d_secperunit
- total_size
;
1329 if (total_percent
> 100) {
1330 fprintf(stderr
,"total percentage %lu is greater than 100\n",
1335 if (free_space
> 0) {
1336 for (i
= 0; i
< lp
->d_npartitions
; i
++) {
1337 pp
= &lp
->d_partitions
[i
];
1338 if (part_set
[i
] && part_size_type
[i
] == '%') {
1339 /* careful of overflows! and integer roundoff */
1340 pp
->p_size
= ((double)pp
->p_size
/100) * free_space
;
1341 total_size
+= pp
->p_size
;
1343 /* FIX we can lose a sector or so due to roundoff per
1344 partition. A more complex algorithm could avoid that */
1349 "%ld sectors available to give to '*' and '%%' partitions\n",
1352 /* fix? set all % partitions to size 0? */
1355 /* give anything remaining to the hog partition */
1356 if (hog_part
!= -1) {
1357 lp
->d_partitions
[hog_part
].p_size
= lp
->d_secperunit
- total_size
;
1358 total_size
= lp
->d_secperunit
;
1361 /* Now set the offsets for each partition */
1362 current_offset
= 0; /* in sectors */
1363 seen_default_offset
= 0;
1364 for (i
= 0; i
< lp
->d_npartitions
; i
++) {
1366 pp
= &lp
->d_partitions
[i
];
1368 if (part_offset_type
[i
] == '*') {
1369 if (i
== RAW_PART
) {
1372 pp
->p_offset
= current_offset
;
1373 seen_default_offset
= 1;
1376 /* allow them to be out of order for old-style tables */
1377 if (pp
->p_offset
< current_offset
&&
1378 seen_default_offset
&& i
!= RAW_PART
&&
1379 pp
->p_fstype
!= FS_VINUM
) {
1381 "Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
1382 (long)pp
->p_offset
,i
+'a',current_offset
);
1384 "Labels with any *'s for offset must be in ascending order by sector\n");
1386 } else if (pp
->p_offset
!= current_offset
&&
1387 i
!= RAW_PART
&& seen_default_offset
) {
1389 * this may give unneeded warnings if
1390 * partitions are out-of-order
1393 "Offset %ld for partition %c doesn't match expected value %ld",
1394 (long)pp
->p_offset
, i
+ 'a', current_offset
);
1398 current_offset
= pp
->p_offset
+ pp
->p_size
;
1402 for (i
= 0; i
< lp
->d_npartitions
; i
++) {
1404 pp
= &lp
->d_partitions
[i
];
1405 if (pp
->p_size
== 0 && pp
->p_offset
!= 0)
1406 Warning("partition %c: size 0, but offset %lu",
1407 part
, (u_long
)pp
->p_offset
);
1409 if (pp
->p_size
% lp
->d_secpercyl
)
1410 Warning("partition %c: size %% cylinder-size != 0",
1412 if (pp
->p_offset
% lp
->d_secpercyl
)
1413 Warning("partition %c: offset %% cylinder-size != 0",
1416 if (pp
->p_offset
> lp
->d_secperunit
) {
1418 "partition %c: offset past end of unit\n", part
);
1421 if (pp
->p_offset
+ pp
->p_size
> lp
->d_secperunit
) {
1423 "partition %c: partition extends past end of unit\n",
1429 if (pp
->p_fstype
!= FS_UNUSED
)
1430 Warning("partition %c is not marked as unused!",part
);
1431 if (pp
->p_offset
!= 0)
1432 Warning("partition %c doesn't start at 0!",part
);
1433 if (pp
->p_size
!= lp
->d_secperunit
)
1434 Warning("partition %c doesn't cover the whole unit!",part
);
1436 if ((pp
->p_fstype
!= FS_UNUSED
) || (pp
->p_offset
!= 0) ||
1437 (pp
->p_size
!= lp
->d_secperunit
)) {
1438 Warning("An incorrect partition %c may cause problems for "
1439 "standard system utilities",part
);
1443 /* check for overlaps */
1444 /* this will check for all possible overlaps once and only once */
1445 for (j
= 0; j
< i
; j
++) {
1446 pp2
= &lp
->d_partitions
[j
];
1447 if (j
!= RAW_PART
&& i
!= RAW_PART
&&
1448 pp
->p_fstype
!= FS_VINUM
&&
1449 pp2
->p_fstype
!= FS_VINUM
&&
1450 part_set
[i
] && part_set
[j
]) {
1451 if (pp2
->p_offset
< pp
->p_offset
+ pp
->p_size
&&
1452 (pp2
->p_offset
+ pp2
->p_size
> pp
->p_offset
||
1453 pp2
->p_offset
>= pp
->p_offset
)) {
1454 fprintf(stderr
,"partitions %c and %c overlap!\n",
1461 for (; i
< 8 || i
< lp
->d_npartitions
; i
++) {
1463 pp
= &lp
->d_partitions
[i
];
1464 if (pp
->p_size
|| pp
->p_offset
)
1465 Warning("unused partition %c: size %d offset %lu",
1466 'a' + i
, pp
->p_size
, (u_long
)pp
->p_offset
);
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 disklabel32 dlab
;
1482 struct disklabel32
*
1483 getvirginlabel(void)
1485 struct partinfo info
;
1486 struct disklabel32
*dl
= &dlab
;
1487 char nambuf
[BBSIZE
];
1490 if (dkname
[0] == '/') {
1491 warnx("\"auto\" requires the usage of a canonical disk name");
1494 snprintf(nambuf
, BBSIZE
, "%s%s", _PATH_DEV
, dkname
);
1495 if ((f
= open(nambuf
, O_RDONLY
)) == -1) {
1496 warn("cannot open %s", nambuf
);
1501 * Check to see if the media is too big for a 32 bit disklabel.
1503 if (ioctl(f
, DIOCGPART
, &info
) == 0) {
1504 if (info
.media_size
>= 0x100000000ULL
* 512) {
1505 warnx("The media is too large for a 32 bit disklabel");
1511 * Generate a virgin disklabel via ioctl
1513 if (ioctl(f
, DIOCGDVIRGIN32
, dl
) < 0) {
1514 l_perror("ioctl DIOCGDVIRGIN32");
1522 struct disklabel32
*
1523 getdisklabelfromdisktab(const char *name
)
1526 struct disklabel32
*dl
= &dlab
;
1529 if ((dt
= getdisktabbyname(name
)) == NULL
)
1531 dl
->d_magic
= DISKMAGIC32
;
1532 dl
->d_type
= dt
->d_typeid
;
1534 dl
->d_secsize
= dt
->d_media_blksize
;
1535 dl
->d_nsectors
= dt
->d_secpertrack
;
1536 dl
->d_ntracks
= dt
->d_nheads
;
1537 dl
->d_ncylinders
= dt
->d_ncylinders
;
1538 dl
->d_secpercyl
= dt
->d_secpercyl
;
1539 dl
->d_secperunit
= dt
->d_media_blocks
;
1540 dl
->d_rpm
= dt
->d_rpm
;
1541 dl
->d_interleave
= dt
->d_interleave
;
1542 dl
->d_trackskew
= dt
->d_trackskew
;
1543 dl
->d_cylskew
= dt
->d_cylskew
;
1544 dl
->d_headswitch
= dt
->d_headswitch
;
1545 dl
->d_trkseek
= dt
->d_trkseek
;
1546 dl
->d_magic2
= DISKMAGIC32
;
1547 dl
->d_npartitions
= dt
->d_npartitions
;
1548 dl
->d_bbsize
= dt
->d_bbsize
;
1549 dl
->d_sbsize
= dt
->d_sbsize
;
1550 for (i
= 0; i
< dt
->d_npartitions
; ++i
) {
1551 struct partition32
*dlp
= &dl
->d_partitions
[i
];
1552 struct dt_partition
*dtp
= &dt
->d_partitions
[i
];
1554 dlp
->p_size
= dtp
->p_size
;
1555 dlp
->p_offset
= dtp
->p_offset
;
1556 dlp
->p_fsize
= dtp
->p_fsize
;
1557 dlp
->p_fstype
= dtp
->p_fstype
;
1558 dlp
->p_frag
= dtp
->p_fsize
;
1564 * If we are installing a boot program that doesn't fit in d_bbsize
1565 * we need to mark those partitions that the boot overflows into.
1566 * This allows newfs to prevent creation of a filesystem where it might
1567 * clobber bootstrap code.
1570 setbootflag(struct disklabel32
*lp
)
1572 struct partition32
*pp
;
1579 boffset
= bootsize
/ lp
->d_secsize
;
1580 for (i
= 0; i
< lp
->d_npartitions
; i
++) {
1582 pp
= &lp
->d_partitions
[i
];
1583 if (pp
->p_size
== 0)
1585 if (boffset
<= pp
->p_offset
) {
1586 if (pp
->p_fstype
== FS_BOOT
)
1587 pp
->p_fstype
= FS_UNUSED
;
1588 } else if (pp
->p_fstype
!= FS_BOOT
) {
1589 if (pp
->p_fstype
!= FS_UNUSED
) {
1591 "boot overlaps used partition %c\n",
1595 pp
->p_fstype
= FS_BOOT
;
1596 Warning("boot overlaps partition %c, %s",
1597 part
, "marked as FS_BOOT");
1602 errx(4, "cannot install boot program");
1607 Warning(const char *fmt
, ...)
1611 fprintf(stderr
, "Warning, ");
1613 vfprintf(stderr
, fmt
, ap
);
1614 fprintf(stderr
, "\n");
1619 * Check to see if the bootblocks are in the wrong place. FBsd5 bootblocks
1620 * and earlier DFly bb's are packed against the old disklabel and a new
1621 * disklabel would blow them up. This is a hack that should be removed
1622 * in 2006 sometime (if ever).
1626 checkoldboot(int f
, const char *bootbuffer
)
1630 if (bootbuffer
&& strncmp(bootbuffer
+ 0x402, "BTX", 3) == 0)
1632 lseek(f
, (off_t
)0, SEEK_SET
);
1633 if (read(f
, buf
, sizeof(buf
)) != sizeof(buf
))
1635 if (strncmp(buf
+ 0x402, "BTX", 3) == 0) /* new location */
1637 if (strncmp(buf
+ 0x316, "BTX", 3) == 0) /* old location */
1643 * Traditional 32 bit disklabels actually use absolute sector numbers on
1644 * disk, NOT slice relative sector numbres. The OS hides this from us
1645 * when we use DIOC ioctls to access the label, but newer versions of
1646 * Dragonfly no longer adjusts the disklabel when snooping reads or writes
1647 * so we have to figure it out ourselves.
1650 fixlabel(int f
, struct disklabel32
*lp
, int writeadj
)
1652 const char *msg
= NULL
;
1653 struct partinfo info
;
1654 struct partition32
*pp
;
1660 int rev_len
= sizeof(rev
);
1662 if (sysctlbyname("kern.osrevision", &rev
, &rev_len
, NULL
, 0) < 0) {
1663 errx(1, "Cannot use raw mode on non-DragonFly systems\n");
1666 warnx("Warning running new disklabel on old DragonFly systems,\n"
1667 "assuming the disk layer will fixup the label.\n");
1672 pp
= &lp
->d_partitions
[RAW_PART
];
1675 info
.media_offset
= slice_start_lba
* lp
->d_secsize
;
1676 info
.media_blocks
= pp
->p_size
;
1677 info
.media_blksize
= lp
->d_secsize
;
1678 } else if (ioctl(f
, DIOCGPART
, &info
) < 0) {
1679 msg
= "Unable to extract the slice starting LBA, "
1680 "you must use the -f <slice_start_lba> option\n"
1681 "to specify it manually, or perhaps try without "
1682 "using -r and let the kernel deal with it\n";
1686 if (lp
->d_magic
!= DISKMAGIC32
|| lp
->d_magic2
!= DISKMAGIC32
)
1687 return ("fixlabel: invalid magic");
1688 if (dkcksum32(lp
) != 0)
1689 return ("fixlabel: invalid checksum");
1692 * What a mess. For ages old backwards compatibility the disklabel
1693 * on-disk stores absolute offsets instead of slice-relative offsets.
1694 * So fix it up when reading, writing, or snooping.
1696 * The in-core label is always slice-relative.
1703 offset
= info
.media_offset
/ info
.media_blksize
;
1708 start
= info
.media_offset
/ info
.media_blksize
;
1709 offset
= -info
.media_offset
/ info
.media_blksize
;
1711 if (pp
->p_offset
!= start
)
1712 return ("fixlabel: raw partition offset != slice offset");
1713 if (pp
->p_size
!= info
.media_blocks
) {
1714 if (pp
->p_size
> info
.media_blocks
)
1715 return ("fixlabel: raw partition size > slice size");
1717 end
= start
+ info
.media_blocks
;
1719 return ("fixlabel: slice wraps");
1720 if (lp
->d_secpercyl
<= 0)
1721 return ("fixlabel: d_secpercyl <= 0");
1723 for (part
= 0; part
< lp
->d_npartitions
; part
++, pp
++) {
1724 if (pp
->p_offset
!= 0 || pp
->p_size
!= 0) {
1725 if (pp
->p_offset
< start
1726 || pp
->p_offset
+ pp
->p_size
> end
1727 || pp
->p_offset
+ pp
->p_size
< pp
->p_offset
) {
1728 /* XXX else silently discard junk. */
1729 bzero(pp
, sizeof *pp
);
1731 pp
->p_offset
+= offset
;
1736 lp
->d_checksum
= dkcksum32(lp
);
1744 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",
1745 "usage: disklabel [-r] disk",
1746 "\t\t(to read label)",
1747 " disklabel -w [-r] [-n] disk type [ packid ]",
1748 "\t\t(to write label with existing boot program)",
1749 " disklabel -e [-r] [-n] disk",
1750 "\t\t(to edit label)",
1751 " disklabel -R [-r] [-n] disk protofile",
1752 "\t\t(to restore label with existing boot program)",
1754 " disklabel -B [-n] [ -b boot1 [ -s boot2 ] ] disk [ type ]",
1755 "\t\t(to install boot program with existing label)",
1756 " disklabel -w -B [-n] [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
1757 "\t\t(to write label and boot program)",
1758 " disklabel -R -B [-n] [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
1759 "\t\t(to restore label and boot program)",
1761 " disklabel -B [-n] [ -b bootprog ] disk [ type ]",
1762 "\t\t(to install boot program with existing on-disk label)",
1763 " disklabel -w -B [-n] [ -b bootprog ] disk type [ packid ]",
1764 "\t\t(to write label and install boot program)",
1765 " disklabel -R -B [-n] [ -b bootprog ] disk protofile [ type ]",
1766 "\t\t(to restore label and install boot program)",
1768 " disklabel [-NW] disk",
1769 "\t\t(to write disable/enable label)");
1771 fprintf(stderr
, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1772 "usage: disklabel [-r] disk", "(to read label)",
1773 " disklabel -w [-r] [-n] disk type [ packid ]",
1774 "\t\t(to write label)",
1775 " disklabel -e [-r] [-n] disk",
1776 "\t\t(to edit label)",
1777 " disklabel -R [-r] [-n] disk protofile",
1778 "\t\t(to restore label)",
1779 " disklabel [-NW] disk",
1780 "\t\t(to write disable/enable label)");