disklabel(8) from 4.4 merged with our old disklabel(8) by John Brezak

(brezak@apollo.hp.com).  hp300's can now disklabel by themselves.
This commit is contained in:
hpeyerl 1994-06-24 13:48:17 +00:00
parent 52cb3eb56c
commit 965c81a824
4 changed files with 552 additions and 242 deletions

View File

@ -1,5 +1,5 @@
# from: @(#)Makefile 5.6 (Berkeley) 4/1/91
# $Id: Makefile,v 1.8 1994/02/12 17:49:32 chopps Exp $
# $Id: Makefile,v 1.9 1994/06/24 13:48:17 hpeyerl Exp $
PROG= disklabel
SRCS= disklabel.c dkcksum.c
@ -7,14 +7,27 @@ MAN8= disklabel.0
BINGRP=operator
BINMODE=2550
CLEANFILES=disklabel.5.0
.if (${MACHINE} == "amiga")
CFLAGS+=-D${MACHINE}
.endif
.if !defined(NOMAN)
all: disklabel.5.0
.endif
.if ${MACHINE} == "amiga"
CFLAGS+= -D${MACHINE}
.endif
.if ${MACHINE} == "i386"
CFLAGS+= -DNUMBOOT=2
.endif
.if ${MACHINE} == "hp300"
CFLAGS+= -DNUMBOOT=1
.endif
.if ${MACHINE} == "i386"
CFLAGS+= -DRAWPARTITION=\'d\'
.endif
beforeinstall:
@if [ ! -d ${DESTDIR}${MANDIR}5 ]; then \
/bin/rm -f ${DESTDIR}${MANDIR}5 ; \

View File

@ -1,5 +1,5 @@
.\" Copyright (c) 1987, 1991 Regents of the University of California.
.\" All rights reserved.
.\" Copyright (c) 1987, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Symmetric Computer Systems.
@ -32,10 +32,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" from: @(#)disklabel.5.5 6.6 (Berkeley) 5/10/91
.\" $Id: disklabel.5.5,v 1.4 1993/08/01 07:39:33 mycroft Exp $
.\" @(#)disklabel.5.5 8.1 (Berkeley) 6/5/93
.\"
.Dd May 10, 1991
.Dd June 5, 1993
.Dt DISKLABEL 5
.Os
.Sh NAME
@ -118,8 +117,14 @@ is
* to leave room for a bootstrap, etc.
*/
#define LABELSECTOR 0 /* sector containing label */
#define LABELOFFSET 64 /* offset of label in sector */
#ifndef LABELSECTOR
#define LABELSECTOR 0 /* sector containing label */
#endif
#ifndef LABELOFFSET
#define LABELOFFSET 64 /* offset of label in sector */
#endif
#define DISKMAGIC ((u_long) 0x82564557) /* The disk magic number */
#ifndef MAXPARTITIONS
#define MAXPARTITIONS 8
@ -218,6 +223,12 @@ struct disklabel {
u_long p_fsize; /* filesystem basic fragment size */
u_char p_fstype; /* filesystem type, see below */
u_char p_frag; /* filesystem fragments per block */
union {
u_short cpg; /* UFS: FS cylinders per group */
u_short sgs; /* LFS: FS segment shift */
} __partition_u1;
#define p_cpg __partition_u1.cpg
#define p_sgs __partition_u1.sgs
u_short p_cpg; /* filesystem cylinders per group */
} d_partitions[MAXPARTITIONS]; /* actually may be more */
};
@ -229,6 +240,8 @@ struct disklabel {
#define DTYPE_SCSI 4 /* SCSI */
#define DTYPE_ESDI 5 /* ESDI interface */
#define DTYPE_ST506 6 /* ST506 etc. */
#define DTYPE_HPIB 7 /* CS/80 on HP-IB */
#define DTYPE_HPFL 8 /* HP Fiber-link */
#define DTYPE_FLOPPY 10 /* floppy */
#ifdef DKTYPENAMES
@ -239,9 +252,9 @@ static char *dktypenames[] = {
"old DEC",
"SCSI",
"ESDI",
"type 6",
"type 7",
"type 8",
"ST506",
"HP-IB",
"HP-FL",
"type 9",
"floppy",
0
@ -262,6 +275,12 @@ static char *dktypenames[] = {
#define FS_V71K 5 /* V7 with 1K blocks (4.1, 2.9) */
#define FS_V8 6 /* Eighth Edition, 4K blocks */
#define FS_BSDFFS 7 /* 4.2BSD fast file system */
#define FS_MSDOS 8 /* MSDOS file system */
#define FS_BSDLFS 9 /* 4.4BSD log-structured file system */
#define FS_OTHER 10 /* in use, but unknown/unsupported */
#define FS_HPFS 11 /* OS/2 high-performance file system */
#define FS_ISO9660 12 /* ISO 9660, normally CD-ROM */
#define FS_BOOT 13 /* partition contains bootstrap */
#ifdef DKTYPENAMES
static char *fstypenames[] = {
@ -273,6 +292,12 @@ static char *fstypenames[] = {
"4.1BSD",
"Eighth Edition",
"4.2BSD",
"MSDOS",
"4.4LFS",
"unknown",
"HPFS",
"ISO9660",
"boot",
0
};
#define FSMAXTYPES (sizeof(fstypenames) / sizeof(fstypenames[0]) - 1)
@ -303,6 +328,11 @@ static char *fstypenames[] = {
#define d_precompcyl d_drivedata[0]
#define d_gap3 d_drivedata[1] /* used only when formatting */
/*
* Drive data for SCSI.
*/
#define d_blind d_drivedata[0]
#ifndef LOCORE
/*
* Structure used to perform a format
@ -352,7 +382,3 @@ struct partinfo {
.Xr disktab 5 ,
.Xr disklabel 8
.Sh HISTORY
The
.Nm
file format is
.Ud .

View File

@ -1,5 +1,5 @@
.\" Copyright (c) 1987, 1988 Regents of the University of California.
.\" All rights reserved.
.\" Copyright (c) 1987, 1988, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Symmetric Computer Systems.
@ -32,14 +32,13 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" from: @(#)disklabel.8 6.8 (Berkeley) 6/24/90
.\" $Id: disklabel.8,v 1.6 1994/01/14 02:05:23 jtc Exp $
.\" @(#)disklabel.8 8.1 (Berkeley) 6/5/93
.\"
.Dd June 24, 1990
.Dd "June 5, 1993"
.Dt DISKLABEL 8
.Os
.Os BSD 4.2
.Sh NAME
.Nm disklabel
.Nm disklabel
.Nd read and write disk pack label
.Sh SYNOPSIS
.Nm disklabel
@ -48,48 +47,75 @@
.Nm disklabel
.Fl w
.Op Fl r
.Ar disk
.Ar disktype
.Op Ar packid Op Ar priboot secboot
.Ar disk Ar disktype
.Oo Ar packid Oc
.Nm disklabel
.Fl e
.Op Fl r
.Ar disk
.Nm disklabel
.Fl R
.Op r
.Ar disk
.Ar protofile
.Op Ar priboot secboot
.Op Fl r
.Ar disk Ar protofile
.Nm disklabel
.Op Fl NW
.Op disk
.Ar disk
.sp
.Nm disklabel
.Fl B
.Oo
.Fl b Ar boot1
.Op Fl s Ar boot2
.Oc
.Ar disk
.Oo Ar disktype Oc
.Nm disklabel
.Fl w
.Fl B
.Oo
.Fl b Ar boot1
.Op Fl s Ar boot2
.Oc
.Ar disk Ar disktype
.Oo Ar packid Oc
.Nm disklabel
.Fl R
.Fl B
.Oo
.Fl b Ar boot1
.Op Fl s Ar boot2
.Oc
.Ar disk Ar protofile
.Oo Ar disktype Oc
.Sh DESCRIPTION
.Nm Disklabel
can be used to install, examine or modify the label on a disk drive or pack.
When writing the label, it can be used
to change the drive identification,
the disk partitions on the drive,
or to replace a damaged label or bootstrap.
The disk label is located on one of the first sectors
of each disk (usually block 0).
On machines that require a block-0 bootstrap (VAX 11/750),
the label is inserted into the bootstrap program.
This information is used by the system disk driver and by the bootstrap
program to determine how to program the drive.
There are several forms of the command that display, install or modify
or to replace a damaged label.
On some systems,
.Nm disklabel
can be used to install bootstrap code as well.
There are several forms of the command that read (display), install or edit
the label on a disk.
Each form has an additional option,
.Fl r ,
which causes the label to be read from or written to the disk directly,
rather than going through the system's in-core copy of the label.
When writing, the in-core copy is also updated if possible.
This option may allow a label to be installed on a disk
without kernel support for a label, such as when labels are first installed
on a system; it must be used when first installing a label on a disk.
The specific effect of
.Fl r
is described under each command.
The read and install forms also support the
.Fl B
option to install bootstrap code.
These varients are described later.
.Pp
The first form of the command is used to examine the label on the named
disk drive (e.g. hp0 or /dev/rhp0c).
The first form of the command (read) is used to examine the label on the named
disk drive (e.g. sd0 or /dev/rsd0c).
It will display all of the parameters associated with the drive
and its partition layout.
Unless the
@ -108,7 +134,7 @@ The second form of the command, with the
flag, is used to write a standard label on the designated drive.
The required arguments to
.Nm disklabel
are the drive to be labeled (e.g. hp0), and
are the drive to be labelled (e.g. sd0), and
the drive type as described in the
.Xr disktab 5
file.
@ -116,40 +142,25 @@ The drive parameters and partitions are taken from that file.
If different disks of the same physical type are to have different
partitions, it will be necessary to have separate disktab entries
describing each, or to edit the label after installation as described below.
The first optional argument is a pack identification string,
The optional argument is a pack identification string,
up to 16 characters long.
The pack id must be quoted if it contains blanks.
If the
.Fl r
flag is given, the disk sectors containing the label and bootstrap
will be written directly,
otherwise the existing label will be updated in place without modifying
the bootstrap.
will be written directly.
A side-effect of this is that any existing bootstrap code will be overwritten
and the disk rendered unbootable.
If
.Fl r
is not specified,
the existing label will be updated via the in-core copy and any bootstrap
code will be unaffected.
If the disk does not already have a label, the
.Fl r
flag must be used.
In either case, the kernel's in-core label is replaced.
.Pp
Alternate versions of the bootstrap files may be specified
after the pack identifier.
If an alternate bootstrap is not specified, the standard bootstrap will be used.
The bootstrap programs are located in
.Pa /usr/mdec .
The names of the bootstrap programs may be specified in
.Xr disktab 5 ;
if not specified,
the default names are of the form
.Em basename No boot
for the primary (block 0) bootstrap, and
.No boot Em basename
for the secondary (block 1-15) bootstrap;
for example,
.Pa /usr/mdec/hpboot
and
.Pa /usr/mdec/boothp
if the disk device is
.Em hp0 .
.Pp
An existing disk label may be edited by using the
.Fl e
flag.
@ -158,11 +169,16 @@ or directly from the disk if the
.Fl r
flag is also given.
The label is formatted and then supplied to an editor for changes.
If no editor is specified in an EDITOR environment variable,
If no editor is specified in an
.Ev EDITOR
environment variable,
.Xr vi 1
is used.
When the editor terminates, the formatted label is reread
and used to rewrite the disk label.
Existing bootstrap code is unchanged regardless of whether
.Fl r
was specified.
.Pp
With the
.Fl R
@ -173,49 +189,155 @@ in a prior operation and saved in an ascii file.
The prototype file used to create the label should be in the same format
as that produced when reading or editing a label.
Comments are delimited by
.Em #
.Ar \&#
and newline.
If the
As with
.Fl w ,
any existing bootstrap code will be clobbered if
.Fl r
option is also given,
a block-0 bootstrap is installed on machines that use one;
either the disktype or the names of the bootstrap files must be specified
on such machines.
is specified and will be unaffected otherwise.
.Pp
Finally, the
The
.Fl NW
flags for
flags for
.Nm disklabel
explicitly disallow and allow, respectively, writing of the pack label area
on the selected disk.
explicitly disallow and
allow, respectively, writing of the pack label area on the selected disk.
.Pp
The final three forms of
.Nm disklabel
are used to install boostrap code on machines where the bootstrap is part
of the label.
The bootstrap code is comprised of one or two boot programs depending on
the machine.
The
.Fl B
option is used to denote that bootstrap code is to be installed.
The
.Fl r
flag is implied by
.Fl B
and never needs to be specified.
The name of the boot program(s) to be installed can be selected in a
variety of ways.
First, the names can be specified explicitly via the
.Fl b
and
.Fl s
flags.
On machines with only a single level of boot program,
.Fl b
is the name of that program.
For machines with a two-level bootstrap,
.Fl b
indicates the primary boot program and
.Fl s
the secondary boot program.
If the names are not explicitly given, standard boot programs will be used.
The boot programs are located in
.Pa /usr/mdec .
The names of the programs are taken from the ``b0'' and ``b1'' parameters
of the
.Xr disktab 5
entry for the disk if
.Ar disktype
was given and its disktab entry exists and includes those parameters.
Otherwise, boot program names are derived from the name of the disk.
These names are of the form
.Pa basename Ns boot
for the primary (or only) bootstrap, and
.Pf boot Pa basename
for the secondary bootstrap;
for example,
.Pa /usr/mdec/sdboot
and
.Pa /usr/mdec/bootsd
if the disk device is
.Em sd0 .
.Pp
The first of the three boot-installation forms is used to install
bootstrap code without changing the existing label.
It is essentially a read command with respect to the disk label
itself and all options are related to the specification of the boot
program as described previously.
The final two forms are analogous to the basic write and restore versions
except that they will install bootstrap code in addition to a new label.
.Sh FILES
.Bl -item -compact
.It
.Pa /etc/disktab
.It
.Pa /usr/mdec/ Ns Sy xx Ns Pa boot
.It
.Pa /usr/mdec/boot Ns Sy xx
.Bl -tag -width Pa -compact
.It Pa /etc/disktab
.It Pa /usr/mdec/ Ns Em xx Ns boot
.It Pa /usr/mdec/boot Ns Em xx
.El
.Sh EXAMPLES
.Dl disklabel sd0
.Pp
Display the in-core label for sd0 as obtained via
.Pa /dev/rsd0c .
.Pp
.Dl disklabel -w -r /dev/rsd0c sd2212 foo
.Pp
Create a label for sd0 based on information for ``sd2212'' found in
.Pa /etc/disktab .
Any existing bootstrap code will be clobbered.
.Pp
.Dl disklabel -e -r sd0
.Pp
Read the on-disk label for sd0, edit it and reinstall in-core as well
as on-disk.
Existing bootstrap code is unaffected.
.Pp
.Dl disklabel -R sd0 mylabel
.Pp
Restore the on-disk and in-core label for sd0 from information in
.Pa mylabel .
Existing bootstrap code is unaffected.
.Pp
.Dl disklabel -B sd0
.Pp
Install a new bootstrap on sd0.
The boot code comes from
.Pa /usr/mdec/sdboot
and possibly
.Pa /usr/mdec/bootsd .
On-disk and in-core labels are unchanged.
.Pp
.Dl disklabel -w -B /dev/rsd0c -b newboot sd2212
.Pp
Install a new label and bootstrap.
The label is derived from disktab information for ``sd2212'' and
installed both in-core and on-disk.
The bootstrap code comes from the file
.Pa /usr/mdec/newboot .
.Sh SEE ALSO
.Xr disktab 5 ,
.Xr disklabel 5
.Xr disklabel 5
.Sh DIAGNOSTICS
The kernel device drivers will not allow the size of a disk partition
to be decreased or the offset of a partition to be changed while it is open.
Some device drivers create a label containing only a single large partition
if a disk is unlabeled; thus, the label must be written to the
.Dq a
if a disk is unlabeled; thus, the label must be written to the ``a''
partition of the disk while it is open.
This sometimes requires the desired label to be set in two steps,
the first one creating at least one other partition,
and the second setting the label on the new partition
while shrinking the
.Dq a
partition.
while shrinking the ``a'' partition.
.Pp
On some machines the bootstrap code may not fit entirely in the area
allocated for it by some filesystems.
As a result, it may not be possible to have filesystems on some partitions
of a ``bootable'' disk.
When installing bootstrap code,
.Nm disklabel
checks for these cases.
If the installed boot code would overlap a partition of type FS_UNUSED
it is marked as type FS_BOOT.
The
.Xr newfs 8
utility will disallow creation of filesystems on FS_BOOT partitions.
Conversely, if a partition has a type other than FS_UNUSED or FS_BOOT,
.Nm disklabel
will not install bootstrap code that overlaps it.
.Sh BUGS
When a disk name is given without a full pathname,
the constructed device name uses the
.Dq c
partition on the vax and the
.Dq a
partition on the tahoe.
the constructed device name uses the ``a'' partition on the tahoe,
the ``c'' partition on all others.

View File

@ -1,6 +1,6 @@
/*
* Copyright (c) 1987 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Symmetric Computer Systems.
@ -35,15 +35,15 @@
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1987 The Regents of the University of California.\n\
All rights reserved.\n";
static char copyright[] =
"@(#) Copyright (c) 1987, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
/*static char sccsid[] = "from: @(#)disklabel.c 1.2 (Symmetric) 11/28/85";*/
/*static char sccsid[] = "from: @(#)disklabel.c 5.20 (Berkeley) 2/9/91";*/
static char rcsid[] = "$Id: disklabel.c,v 1.12 1994/06/13 14:29:08 cgd Exp $";
/* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
/* from static char sccsid[] = "@(#)disklabel.c 8.1 (Berkeley) 6/5/93"; */
static char rcsid[] = "$Id: disklabel.c,v 1.13 1994/06/24 13:48:22 hpeyerl Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -51,67 +51,40 @@ static char rcsid[] = "$Id: disklabel.c,v 1.12 1994/06/13 14:29:08 cgd Exp $";
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <ufs/ffs/fs.h>
#include <string.h>
#include <sys/stat.h>
#define DKTYPENAMES
#include <sys/disklabel.h>
#include <ufs/ffs/fs.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include "pathnames.h"
/*
* Disklabel: read and write disklabels.
* The label is usually placed on one of the first sectors of the disk.
* Many machines (VAX 11/750) also place a bootstrap in the same area,
* Many machines also place a bootstrap in the same area,
* in which case the label is embedded in the bootstrap.
* The bootstrap source must leave space at the proper offset
* for the label on such machines.
*
* On NetBSD/i386, the disklabel may either be at the start of the disk, or, at
* the start of an MS/DOS partition. In this way, it can be used either
* in concert with other operating systems sharing a disk, or with the
* disk dedicated to NetBSD. In shared mode, the DOS disk geometry must be
* identical to that which disklabel uses, and the disklabel must solely
* describe the space within the partition selected. Otherwise, the disk
* must be dedicated to NetBSD. -wfj
*/
#if defined(vax)
#ifndef RAWPARTITION
#define RAWPARTITION 'c'
#endif
#if defined(i386)
/* with NetBSD/i386, 'c' maps the portion of the disk given over to NetBSD,
and 'd' maps the entire drive, ignoring any partition tables (XXX) */
#define RAWPARTITION 'd'
#endif
#if defined(vax)==0 && defined(i386)==0
#define RAWPARTITION 'a'
#endif
#ifndef BBSIZE
#define BBSIZE 8192 /* size of boot area, with label */
#endif
#if defined(vax) || defined(i386)
#define BOOT /* also have bootstrap in "boot area" */
#define BOOTDIR _PATH_BOOTDIR /* source of boot binaries */
#else
#ifdef lint
#define BOOT
#endif
#ifndef NUMBOOT
#define NUMBOOT 0
#endif
#define DEFEDITOR _PATH_VI
#define streq(a,b) (strcmp(a,b) == 0)
#ifdef BOOT
char *xxboot;
char *bootxx;
#endif
char *dkname;
char *specname;
char tmpfil[] = _PATH_TMP;
@ -121,15 +94,28 @@ char namebuf[BBSIZE], *np = namebuf;
struct disklabel lab;
struct disklabel *readlabel(), *makebootarea();
char bootarea[BBSIZE];
#if NUMBOOT > 0
int installboot; /* non-zero if we should install a boot program */
char *bootbuf; /* pointer to buffer with remainder of boot prog */
int bootsize; /* size of remaining boot program */
char *xxboot; /* primary boot */
char *bootxx; /* secondary boot */
char boot0[MAXPATHLEN];
char boot1[MAXPATHLEN];
#endif
enum { UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE } op = UNSPEC;
enum {
UNSPEC, EDIT, NOWRITE, READ, RESTORE, WRITE, WRITEABLE, WRITEBOOT
} op = UNSPEC;
int rflag;
#ifdef DEBUG
int debug;
#define OPTIONS "BNRWb:ders:w"
#else
#define OPTIONS "BNRWb:ers:w"
#endif
#ifdef i386
@ -141,14 +127,28 @@ main(argc, argv)
int argc;
char *argv[];
{
extern char *optarg;
extern int optind;
register struct disklabel *lp;
FILE *t;
int ch, f, error = 0;
char *name = 0, *type;
int ch, f, flag, error = 0;
char *name = 0;
while ((ch = getopt(argc, argv, "NRWerw")) != EOF)
while ((ch = getopt(argc, argv, OPTIONS)) != EOF)
switch (ch) {
#if NUMBOOT > 0
case 'B':
++installboot;
break;
case 'b':
xxboot = optarg;
break;
#if NUMBOOT > 1
case 's':
bootxx = optarg;
break;
#endif
#endif
case 'N':
if (op != UNSPEC)
usage();
@ -188,8 +188,20 @@ main(argc, argv)
}
argc -= optind;
argv += optind;
#if NUMBOOT > 0
if (installboot) {
rflag++;
if (op == UNSPEC)
op = WRITEBOOT;
} else {
if (op == UNSPEC)
op = READ;
xxboot = bootxx = 0;
}
#else
if (op == UNSPEC)
op = READ;
#endif
if (argc < 1)
usage();
@ -227,82 +239,81 @@ main(argc, argv)
}
#endif
switch(op) {
case EDIT:
if (argc != 1)
usage();
lp = readlabel(f);
error = edit(lp, f);
break;
case NOWRITE: {
int flag = 0;
case NOWRITE:
flag = 0;
if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
Perror("ioctl DIOCWLABEL");
break;
}
case READ:
if (argc != 1)
usage();
lp = readlabel(f);
display(stdout, lp);
error = checklabel(lp);
break;
case RESTORE:
#ifdef BOOT
if (rflag) {
if (argc == 4) { /* [ priboot secboot ] */
xxboot = argv[2];
bootxx = argv[3];
lab.d_secsize = DEV_BSIZE; /* XXX */
lab.d_bbsize = BBSIZE; /* XXX */
}
else if (argc == 3) /* [ disktype ] */
makelabel(argv[2], (char *)NULL, &lab);
else {
fprintf(stderr,
"Must specify either disktype or bootfiles with -r flag of RESTORE option\n");
exit(1);
}
#if NUMBOOT > 0
if (installboot && argc == 3) {
makelabel(argv[2], 0, &lab);
argc--;
}
else
#endif
if (argc != 2)
usage();
lp = makebootarea(bootarea, &lab);
if (!(t = fopen(argv[1],"r")))
lp = makebootarea(bootarea, &lab, f);
if (!(t = fopen(argv[1], "r")))
Perror(argv[1]);
if (getasciilabel(t, lp))
error = writelabel(f, bootarea, lp);
break;
case WRITE:
type = argv[1];
#ifdef BOOT
if (argc > 5 || argc < 2)
usage();
if (argc > 3) {
bootxx = argv[--argc];
xxboot = argv[--argc];
if (argc == 3) {
name = argv[2];
argc--;
}
#else
if (argc > 3 || argc < 2)
if (argc != 2)
usage();
#endif
if (argc > 2)
name = argv[--argc];
makelabel(type, name, &lab);
lp = makebootarea(bootarea, &lab);
makelabel(argv[1], name, &lab);
lp = makebootarea(bootarea, &lab, f);
*lp = lab;
if (checklabel(lp) == 0)
error = writelabel(f, bootarea, lp);
break;
case WRITEABLE: {
int flag = 1;
case WRITEABLE:
flag = 1;
if (ioctl(f, DIOCWLABEL, (char *)&flag) < 0)
Perror("ioctl DIOCWLABEL");
break;
#if NUMBOOT > 0
case WRITEBOOT:
{
struct disklabel tlab;
lp = readlabel(f);
tlab = *lp;
if (argc == 2)
makelabel(argv[1], 0, &lab);
lp = makebootarea(bootarea, &lab, f);
*lp = tlab;
if (checklabel(lp) == 0)
error = writelabel(f, bootarea, lp);
break;
}
#endif
}
exit(error);
}
@ -325,30 +336,33 @@ makelabel(type, name, lp)
exit(1);
}
*lp = *dp;
#ifdef BOOT
#if NUMBOOT > 0
/*
* Check if disktab specifies the bootstraps (b0 or b1).
* Set bootstrap name(s).
* 1. If set from command line, use those,
* 2. otherwise, check if disktab specifies them (b0 or b1),
* 3. otherwise, makebootarea() will choose ones based on the name
* of the disk special file. E.g. /dev/ra0 -> raboot, bootra
*/
if (!xxboot && lp->d_boot0) {
if (*lp->d_boot0 != '/')
(void)sprintf(boot0, "%s/%s", BOOTDIR, lp->d_boot0);
(void)sprintf(boot0, "%s/%s",
_PATH_BOOTDIR, lp->d_boot0);
else
(void)strcpy(boot0, lp->d_boot0);
xxboot = boot0;
}
#if NUMBOOT > 1
if (!bootxx && lp->d_boot1) {
if (*lp->d_boot1 != '/')
(void)sprintf(boot1, "%s/%s", BOOTDIR, lp->d_boot1);
(void)sprintf(boot1, "%s/%s",
_PATH_BOOTDIR, lp->d_boot1);
else
(void)strcpy(boot1, lp->d_boot1);
bootxx = boot1;
}
/*
* If bootstraps not specified anywhere, makebootarea()
* will choose ones based on the name of the disk special
* file. E.g. /dev/ra0 -> raboot, bootra
*/
#endif /*BOOT*/
#endif
#endif
/* d_packname is union d_boot[01], so zero */
bzero(lp->d_packname, sizeof(lp->d_packname));
if (name)
@ -366,12 +380,14 @@ writelabel(f, boot, lp)
off_t lbl_off; struct partition *pp = &lp->d_partitions[2];
#endif
#if NUMBOOT > 0
setbootflag(lp);
#endif
lp->d_magic = DISKMAGIC;
lp->d_magic2 = DISKMAGIC;
lp->d_checksum = 0;
lp->d_checksum = dkcksum(lp);
if (rflag) {
#ifdef i386
/*
* If NetBSD/i386 DOS partition is missing, or if
@ -413,9 +429,9 @@ writelabel(f, boot, lp)
if (ioctl(f, DIOCSDINFO, lp) < 0 &&
errno != ENODEV && errno != ENOTTY) {
l_perror("ioctl DIOCSDINFO");
/*return (1);*/
return (1);
}
(void)lseek(f, (off_t)0, SEEK_SET);
/*
* write enable label sector before write (if necessary),
* disable after writing.
@ -427,19 +443,29 @@ writelabel(f, boot, lp)
perror("write");
return (1);
}
#if NUMBOOT > 0
/*
* Output the remainder of the disklabel
*/
if (bootbuf && write(f, bootbuf, bootsize) != bootsize) {
perror("write");
return(1);
}
#endif
flag = 0;
(void) ioctl(f, DIOCWLABEL, &flag);
} else if (ioctl(f, DIOCWDINFO, lp) < 0) {
l_perror("ioctl DIOCWDINFO");
return (1);
}
if (lp->d_type != DTYPE_SCSI && lp->d_flags & D_BADSECT) {
#ifdef vax
if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
daddr_t alt;
alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
(void)lseek(f, (off_t)((alt + i) * lp->d_secsize), L_SET);
(void)lseek(f, (off_t)((alt + i) * lp->d_secsize),
SEEK_SET);
if (write(f, boot, lp->d_secsize) < lp->d_secsize) {
int oerrno = errno;
fprintf(stderr, "alternate label %d ", i/2);
@ -448,7 +474,7 @@ writelabel(f, boot, lp)
}
}
}
#endif
return (0);
}
@ -479,7 +505,6 @@ l_perror(s)
case EXDEV:
fprintf(stderr,
"Labeled partition or 'a' partition must start at beginning of disk\n");
fprintf(stderr, "or DOS partition\n");
break;
default:
@ -558,7 +583,6 @@ readlabel(f)
sectoffset = 0;
(void)lseek(f, sectoffset, L_SET);
#endif
if (read(f, bootarea, BBSIZE) < BBSIZE)
Perror(specname);
for (lp = (struct disklabel *)bootarea;
@ -572,12 +596,10 @@ readlabel(f)
dkcksum(lp) != 0) {
fprintf(stderr,
"Bad pack magic number (label is damaged, or pack is unlabeled)\n");
/* lp = (struct disklabel *)(bootarea + LABELOFFSET);
exit (1); */
goto tryioctl;
/* lp = (struct disklabel *)(bootarea + LABELOFFSET); */
exit (1);
}
} else {
tryioctl:
lp = &lab;
if (ioctl(f, DIOCGDINFO, lp) < 0)
Perror("ioctl DIOCGDINFO");
@ -585,25 +607,51 @@ tryioctl:
return (lp);
}
/*
* Construct a bootarea (d_bbsize bytes) in the specified buffer ``boot''
* Returns a pointer to the disklabel portion of the bootarea.
*/
struct disklabel *
makebootarea(boot, dp)
makebootarea(boot, dp, f)
char *boot;
register struct disklabel *dp;
int f;
{
struct disklabel *lp;
register char *p;
int b;
#ifdef BOOT
char *dkbasename;
#endif /*BOOT*/
#if NUMBOOT > 0
char *dkbasename;
struct stat sb;
#endif
lp = (struct disklabel *)(boot + (LABELSECTOR * dp->d_secsize) +
LABELOFFSET);
#ifdef BOOT
if (!rflag)
/* XXX */
if (dp->d_secsize == 0) {
dp->d_secsize = DEV_BSIZE;
dp->d_bbsize = BBSIZE;
}
lp = (struct disklabel *)
(boot + (LABELSECTOR * dp->d_secsize) + LABELOFFSET);
bzero((char *)lp, sizeof *lp);
#if NUMBOOT > 0
/*
* If we are not installing a boot program but we are installing a
* label on disk then we must read the current bootarea so we don't
* clobber the existing boot.
*/
if (!installboot) {
if (rflag) {
if (read(f, boot, BBSIZE) < BBSIZE)
Perror(specname);
bzero((char *)lp, sizeof *lp);
}
return (lp);
if (xxboot == NULL || bootxx == NULL) {
}
/*
* We are installing a boot program. Determine the name(s) and
* read them into the appropriate places in the boot area.
*/
if (!xxboot || !bootxx) {
dkbasename = np;
if ((p = rindex(dkname, '/')) == NULL)
p = dkname;
@ -613,43 +661,80 @@ makebootarea(boot, dp)
*np++ = *p++;
*np++ = '\0';
if (xxboot == NULL) {
(void)sprintf(np, "%s/%sboot", BOOTDIR, dkbasename);
if (!xxboot) {
(void)sprintf(np, "%s/%sboot",
_PATH_BOOTDIR, dkbasename);
if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
dkbasename++;
xxboot = np;
(void)sprintf(xxboot, "%s/%sboot", BOOTDIR, dkbasename);
(void)sprintf(xxboot, "%s/%sboot",
_PATH_BOOTDIR, dkbasename);
np += strlen(xxboot) + 1;
}
if (bootxx == NULL) {
(void)sprintf(np, "%s/boot%s", BOOTDIR, dkbasename);
#if NUMBOOT > 1
if (!bootxx) {
(void)sprintf(np, "%s/boot%s",
_PATH_BOOTDIR, dkbasename);
if (access(np, F_OK) < 0 && dkbasename[0] == 'r')
dkbasename++;
bootxx = np;
(void)sprintf(bootxx, "%s/boot%s", BOOTDIR, dkbasename);
(void)sprintf(bootxx, "%s/boot%s",
_PATH_BOOTDIR, dkbasename);
np += strlen(bootxx) + 1;
}
#endif
}
#ifdef DEBUG
if (debug)
fprintf(stderr, "bootstraps: xxboot = %s, bootxx = %s\n",
xxboot, bootxx);
xxboot, bootxx ? bootxx : "NONE");
#endif
/*
* Strange rules:
* 1. One-piece bootstrap (hp300/hp800)
* up to d_bbsize bytes of ``xxboot'' go in bootarea, the rest
* is remembered and written later following the bootarea.
* 2. Two-piece bootstraps (vax/i386?/mips?)
* up to d_secsize bytes of ``xxboot'' go in first d_secsize
* bytes of bootarea, remaining d_bbsize-d_secsize filled
* from ``bootxx''.
*/
b = open(xxboot, O_RDONLY);
if (b < 0)
Perror(xxboot);
#if NUMBOOT > 1
if (read(b, boot, (int)dp->d_secsize) < 0)
Perror(xxboot);
close(b);
(void)close(b);
b = open(bootxx, O_RDONLY);
if (b < 0)
Perror(bootxx);
if (read(b, &boot[dp->d_secsize], (int)(dp->d_bbsize-dp->d_secsize)) < 0)
Perror(bootxx);
#else
if (read(b, boot, (int)dp->d_bbsize) < 0)
Perror(xxboot);
(void)fstat(b, &sb);
bootsize = (int)sb.st_size - dp->d_bbsize;
if (bootsize > 0) {
/* XXX assume d_secsize is a power of two */
bootsize = (bootsize + dp->d_secsize-1) & ~(dp->d_secsize-1);
bootbuf = (char *)malloc((size_t)bootsize);
if (bootbuf == 0)
Perror(xxboot);
if (read(b, bootbuf, bootsize) < 0) {
free(bootbuf);
Perror(xxboot);
}
}
#endif
(void)close(b);
#endif /*BOOT*/
#endif
/*
* Make sure no part of the bootstrap is written in the area
* reserved for the label.
*/
for (p = (char *)lp; p < (char *)lp + sizeof(struct disklabel); p++)
if (*p) {
fprintf(stderr,
@ -762,7 +847,7 @@ edit(lp, f)
fprintf(stderr, "%s: Can't create\n", tmpfil);
return (1);
}
(void)fchmod(fd, 0600);
(void)fchmod(fileno(fd), 0600);
display(fd, lp);
fclose(fd);
for (;;) {
@ -1077,13 +1162,10 @@ getasciilabel(f, lp)
}
pp = &lp->d_partitions[part];
#define NXTNUM(n) { \
if (tp != NULL) { \
cp = tp, tp = word(cp); \
if (tp == NULL) \
tp = cp; \
(n) = atoi(cp); \
} else \
(n) = 0; \
cp = tp, tp = word(cp); \
if (tp == NULL) \
tp = cp; \
(n) = atoi(cp); \
}
NXTNUM(v);
@ -1246,6 +1328,52 @@ checklabel(lp)
return (errors);
}
#if NUMBOOT > 0
/*
* If we are installing a boot program that doesn't fit in d_bbsize
* we need to mark those partitions that the boot overflows into.
* This allows newfs to prevent creation of a filesystem where it might
* clobber bootstrap code.
*/
setbootflag(lp)
register struct disklabel *lp;
{
register struct partition *pp;
int i, errors = 0;
char part;
u_long boffset;
if (bootbuf == 0)
return;
boffset = bootsize / lp->d_secsize;
for (i = 0; i < lp->d_npartitions; i++) {
part = 'a' + i;
pp = &lp->d_partitions[i];
if (pp->p_size == 0)
continue;
if (boffset <= pp->p_offset) {
if (pp->p_fstype == FS_BOOT)
pp->p_fstype = FS_UNUSED;
} else if (pp->p_fstype != FS_BOOT) {
if (pp->p_fstype != FS_UNUSED) {
fprintf(stderr,
"boot overlaps used partition %c\n",
part);
errors++;
} else {
pp->p_fstype = FS_BOOT;
Warning("boot overlaps partition %c, %s",
part, "marked as FS_BOOT");
}
}
}
if (errors) {
fprintf(stderr, "Cannot install boot program\n");
exit(4);
}
}
#endif
/*VARARGS1*/
Warning(fmt, a1, a2, a3, a4, a5)
char *fmt;
@ -1265,13 +1393,34 @@ Perror(str)
usage()
{
#ifdef BOOT
fprintf(stderr, "%-62s%s\n%-62s%s\n%-62s%s\n%-62s%s\n%-62s%s\n",
"usage: disklabel [-r] disk", "(to read label)",
"or disklabel -w [-r] disk type [ packid ] [ xxboot bootxx ]", "(to write label)",
"or disklabel -e [-r] disk", "(to edit label)",
"or disklabel -R [-r] disk protofile [ type | xxboot bootxx ]", "(to restore label)",
"or disklabel [-NW] disk", "(to write disable/enable label)");
#if NUMBOOT > 0
fprintf(stderr,
"%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n%s\n\t%s\n",
"usage: disklabel [-r] disk",
"(to read label)",
"or disklabel -w [-r] disk type [ packid ]",
"(to write label with existing boot program)",
"or disklabel -e [-r] disk",
"(to edit label)",
"or disklabel -R [-r] disk protofile",
"(to restore label with existing boot program)",
#if NUMBOOT > 1
"or disklabel -B [ -b boot1 [ -s boot2 ] ] disk [ type ]",
"(to install boot program with existing label)",
"or disklabel -w -B [ -b boot1 [ -s boot2 ] ] disk type [ packid ]",
"(to write label and boot program)",
"or disklabel -R -B [ -b boot1 [ -s boot2 ] ] disk protofile [ type ]",
"(to restore label and boot program)",
#else
"or disklabel -B [ -b bootprog ] disk [ type ]",
"(to install boot program with existing on-disk label)",
"or disklabel -w -B [ -b bootprog ] disk type [ packid ]",
"(to write label and install boot program)",
"or disklabel -R -B [ -b bootprog ] disk protofile [ type ]",
"(to restore label and install boot program)",
#endif
"or disklabel [-NW] disk",
"(to write disable/enable label)");
#else
fprintf(stderr, "%-43s%s\n%-43s%s\n%-43s%s\n%-43s%s\n%-43s%s\n",
"usage: disklabel [-r] disk", "(to read label)",