2017-11-18 14:26:50 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-4-Clause
|
|
|
|
*
|
2002-03-11 04:41:42 +00:00
|
|
|
* Copyright (c) 1994, 1995 Gordon W. Ross
|
|
|
|
* Copyright (c) 1994 Theo de Raadt
|
|
|
|
* All rights reserved.
|
1994-05-26 06:35:07 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
2002-03-11 04:41:42 +00:00
|
|
|
* This product includes software developed by Theo de Raadt.
|
1994-05-26 06:35:07 +00:00
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2002-03-11 04:41:42 +00:00
|
|
|
*
|
|
|
|
* from: $NetBSD: disksubr.c,v 1.13 2000/12/17 22:39:18 pk $
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
|
|
|
|
2003-05-31 15:42:56 +00:00
|
|
|
#if 0
|
1994-05-26 06:35:07 +00:00
|
|
|
#ifndef lint
|
1998-06-04 06:49:13 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 06:35:07 +00:00
|
|
|
"@(#) Copyright (c) 1987, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static char sccsid[] = "@(#)disklabel.c 8.2 (Berkeley) 1/7/94";
|
|
|
|
/* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */
|
|
|
|
#endif /* not lint */
|
2003-05-31 15:42:56 +00:00
|
|
|
#endif
|
2002-07-03 16:43:11 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <sys/param.h>
|
2003-05-02 20:14:48 +00:00
|
|
|
#include <stdint.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
1996-01-21 23:55:47 +00:00
|
|
|
#include <sys/wait.h>
|
2002-09-20 09:18:31 +00:00
|
|
|
#include <sys/disk.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#define DKTYPENAMES
|
2002-08-16 16:08:36 +00:00
|
|
|
#define FSTYPENAMES
|
2011-11-06 17:30:32 +00:00
|
|
|
#define MAXPARTITIONS 20
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <sys/disklabel.h>
|
2003-03-16 00:11:49 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2003-05-03 08:04:24 +00:00
|
|
|
#include <libgeom.h>
|
1996-01-21 23:55:47 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <ctype.h>
|
1997-06-10 11:08:53 +00:00
|
|
|
#include <err.h>
|
2000-04-14 06:15:01 +00:00
|
|
|
#include <errno.h>
|
2002-07-03 16:43:11 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static void makelabel(const char *, struct disklabel *);
|
2011-11-06 20:32:55 +00:00
|
|
|
static int geom_class_available(const char *);
|
2003-05-03 09:02:27 +00:00
|
|
|
static int writelabel(void);
|
2004-08-08 23:11:43 +00:00
|
|
|
static int readlabel(int flag);
|
2003-05-03 09:02:27 +00:00
|
|
|
static void display(FILE *, const struct disklabel *);
|
2004-08-08 23:11:43 +00:00
|
|
|
static int edit(void);
|
2003-05-03 09:02:27 +00:00
|
|
|
static int editit(void);
|
2003-10-18 19:32:35 +00:00
|
|
|
static void fixlabel(struct disklabel *);
|
2003-05-03 09:02:27 +00:00
|
|
|
static char *skip(char *);
|
|
|
|
static char *word(char *);
|
|
|
|
static int getasciilabel(FILE *, struct disklabel *);
|
|
|
|
static int getasciipartspec(char *, struct disklabel *, int, int);
|
|
|
|
static int checklabel(struct disklabel *);
|
|
|
|
static void usage(void);
|
|
|
|
static struct disklabel *getvirginlabel(void);
|
1996-01-21 23:55:47 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
#define DEFEDITOR _PATH_VI
|
2007-12-09 22:58:49 +00:00
|
|
|
#define DEFPARTITIONS 8
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static char *specname;
|
2008-09-18 14:04:02 +00:00
|
|
|
static char *pname;
|
2003-05-03 09:02:27 +00:00
|
|
|
static char tmpfil[] = PATH_TMPFILE;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static struct disklabel lab;
|
|
|
|
static u_char bootarea[BBSIZE];
|
2003-05-03 09:58:20 +00:00
|
|
|
static off_t mediasize;
|
2011-03-10 08:24:33 +00:00
|
|
|
static ssize_t secsize;
|
2003-05-03 09:02:27 +00:00
|
|
|
static char blank[] = "";
|
|
|
|
static char unknown[] = "unknown";
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2001-02-25 16:47:44 +00:00
|
|
|
#define MAX_PART ('z')
|
|
|
|
#define MAX_NUM_PARTS (1 + MAX_PART - 'a')
|
2003-05-03 09:02:27 +00:00
|
|
|
static char part_size_type[MAX_NUM_PARTS];
|
|
|
|
static char part_offset_type[MAX_NUM_PARTS];
|
|
|
|
static int part_set[MAX_NUM_PARTS];
|
2001-02-25 16:47:44 +00:00
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static int installboot; /* non-zero if we should install a boot program */
|
2003-05-03 09:58:20 +00:00
|
|
|
static int allfields; /* present all fields in edit */
|
2003-05-03 09:02:27 +00:00
|
|
|
static char const *xxboot; /* primary boot */
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2009-08-19 16:29:20 +00:00
|
|
|
static uint32_t lba_offset;
|
2003-06-07 22:02:01 +00:00
|
|
|
#ifndef LABELSECTOR
|
|
|
|
#define LABELSECTOR -1
|
|
|
|
#endif
|
|
|
|
#ifndef LABELOFFSET
|
|
|
|
#define LABELOFFSET -1
|
|
|
|
#endif
|
2003-06-02 14:19:31 +00:00
|
|
|
static int labelsoffset = LABELSECTOR;
|
|
|
|
static int labeloffset = LABELOFFSET;
|
2003-05-02 20:02:11 +00:00
|
|
|
static int bbsize = BBSIZE;
|
2003-05-02 19:58:08 +00:00
|
|
|
|
2011-12-11 19:28:04 +00:00
|
|
|
static enum {
|
2003-01-26 11:11:19 +00:00
|
|
|
UNSPEC, EDIT, READ, RESTORE, WRITE, WRITEBOOT
|
1994-05-26 06:35:07 +00:00
|
|
|
} op = UNSPEC;
|
|
|
|
|
2003-03-16 00:11:49 +00:00
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static int disable_write; /* set to disable writing to disk label */
|
2004-03-30 23:15:03 +00:00
|
|
|
static int is_file; /* work on a file (abs. pathname), "-f" opt. */
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1996-01-21 23:55:47 +00:00
|
|
|
int
|
2002-03-18 06:17:50 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
FILE *t;
|
2008-09-18 14:04:02 +00:00
|
|
|
int ch, error, fd;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
name = NULL;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2006-10-20 13:10:27 +00:00
|
|
|
while ((ch = getopt(argc, argv, "ABb:efm:nRrw")) != -1)
|
1994-05-26 06:35:07 +00:00
|
|
|
switch (ch) {
|
2003-05-03 09:58:20 +00:00
|
|
|
case 'A':
|
|
|
|
allfields = 1;
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case 'B':
|
|
|
|
++installboot;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
xxboot = optarg;
|
|
|
|
break;
|
2004-03-30 23:15:03 +00:00
|
|
|
case 'f':
|
|
|
|
is_file=1;
|
|
|
|
break;
|
2003-03-16 00:11:49 +00:00
|
|
|
case 'm':
|
2003-06-07 09:09:39 +00:00
|
|
|
if (!strcmp(optarg, "i386") ||
|
2017-01-28 02:22:15 +00:00
|
|
|
!strcmp(optarg, "amd64")) {
|
2003-06-02 14:19:31 +00:00
|
|
|
labelsoffset = 1;
|
|
|
|
labeloffset = 0;
|
2003-05-13 19:42:52 +00:00
|
|
|
bbsize = 8192;
|
2003-05-03 09:02:27 +00:00
|
|
|
} else {
|
|
|
|
errx(1, "Unsupported architecture");
|
2003-05-02 19:58:08 +00:00
|
|
|
}
|
2003-03-16 00:11:49 +00:00
|
|
|
break;
|
2001-02-25 16:47:44 +00:00
|
|
|
case 'n':
|
|
|
|
disable_write = 1;
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case 'R':
|
|
|
|
if (op != UNSPEC)
|
|
|
|
usage();
|
|
|
|
op = RESTORE;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (op != UNSPEC)
|
|
|
|
usage();
|
|
|
|
op = EDIT;
|
|
|
|
break;
|
|
|
|
case 'r':
|
2003-05-03 09:02:27 +00:00
|
|
|
/*
|
2012-01-07 16:09:33 +00:00
|
|
|
* We accept and ignore -r for compatibility with
|
|
|
|
* historical disklabel usage.
|
2003-05-03 09:02:27 +00:00
|
|
|
*/
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
if (op != UNSPEC)
|
|
|
|
usage();
|
|
|
|
op = WRITE;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2003-05-03 09:02:27 +00:00
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
2003-06-07 22:02:01 +00:00
|
|
|
if (labelsoffset < 0 || labeloffset < 0)
|
|
|
|
errx(1, "a -m <architecture> option must be specified");
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2003-05-03 08:04:24 +00:00
|
|
|
/* Figure out the names of the thing we're working on */
|
2004-03-30 23:15:03 +00:00
|
|
|
if (is_file) {
|
2005-01-07 12:19:57 +00:00
|
|
|
specname = argv[0];
|
2003-05-03 08:04:24 +00:00
|
|
|
} else {
|
2008-09-18 14:04:02 +00:00
|
|
|
specname = g_device_path(argv[0]);
|
|
|
|
if (specname == NULL) {
|
|
|
|
warn("unable to get correct path for %s", argv[0]);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
fd = open(specname, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
warn("error opening %s", specname);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
pname = g_providername(fd);
|
|
|
|
if (pname == NULL) {
|
2008-09-30 07:18:49 +00:00
|
|
|
warn("error getting providername for %s", specname);
|
2008-09-30 11:46:14 +00:00
|
|
|
close(fd);
|
2008-09-18 14:04:02 +00:00
|
|
|
return(1);
|
|
|
|
}
|
2008-09-30 11:46:14 +00:00
|
|
|
close(fd);
|
2003-01-26 21:55:43 +00:00
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
|
|
|
|
if (installboot && op == UNSPEC)
|
|
|
|
op = WRITEBOOT;
|
|
|
|
else if (op == UNSPEC)
|
|
|
|
op = READ;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
switch(op) {
|
|
|
|
|
1999-07-21 03:04:36 +00:00
|
|
|
case UNSPEC:
|
|
|
|
break;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
case EDIT:
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
2003-05-03 09:02:27 +00:00
|
|
|
readlabel(1);
|
2003-10-18 19:32:35 +00:00
|
|
|
fixlabel(&lab);
|
2003-05-03 09:02:27 +00:00
|
|
|
error = edit();
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case READ:
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
2003-05-03 09:02:27 +00:00
|
|
|
readlabel(1);
|
|
|
|
display(stdout, NULL);
|
|
|
|
error = checklabel(NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RESTORE:
|
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
if (!(t = fopen(argv[1], "r")))
|
2003-05-03 08:04:24 +00:00
|
|
|
err(4, "fopen %s", argv[1]);
|
2003-05-03 09:02:27 +00:00
|
|
|
readlabel(0);
|
1998-07-25 16:19:10 +00:00
|
|
|
if (!getasciilabel(t, &lab))
|
|
|
|
exit(1);
|
2003-05-03 09:02:27 +00:00
|
|
|
error = writelabel();
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WRITE:
|
2003-05-03 09:02:27 +00:00
|
|
|
if (argc == 2)
|
|
|
|
name = argv[1];
|
|
|
|
else if (argc == 1)
|
|
|
|
name = "auto";
|
|
|
|
else
|
1994-05-26 06:35:07 +00:00
|
|
|
usage();
|
2003-05-03 09:02:27 +00:00
|
|
|
readlabel(0);
|
|
|
|
makelabel(name, &lab);
|
2003-10-18 19:32:35 +00:00
|
|
|
fixlabel(&lab);
|
2003-05-03 09:02:27 +00:00
|
|
|
if (checklabel(NULL) == 0)
|
|
|
|
error = writelabel();
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WRITEBOOT:
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
readlabel(1);
|
2003-10-18 19:32:35 +00:00
|
|
|
fixlabel(&lab);
|
1994-05-26 06:35:07 +00:00
|
|
|
if (argc == 2)
|
2003-05-03 09:02:27 +00:00
|
|
|
makelabel(argv[1], &lab);
|
|
|
|
if (checklabel(NULL) == 0)
|
|
|
|
error = writelabel();
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(error);
|
|
|
|
}
|
|
|
|
|
2003-10-18 19:32:35 +00:00
|
|
|
static void
|
|
|
|
fixlabel(struct disklabel *lp)
|
|
|
|
{
|
|
|
|
struct partition *dp;
|
|
|
|
int i;
|
|
|
|
|
2007-12-09 22:58:49 +00:00
|
|
|
for (i = 0; i < lp->d_npartitions; i++) {
|
2003-10-18 19:32:35 +00:00
|
|
|
if (i == RAW_PART)
|
|
|
|
continue;
|
|
|
|
if (lp->d_partitions[i].p_size)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dp = &lp->d_partitions[0];
|
|
|
|
dp->p_offset = BBSIZE / secsize;
|
|
|
|
dp->p_size = lp->d_secperunit - dp->p_offset;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
2003-02-23 01:48:42 +00:00
|
|
|
* Construct a prototype disklabel from /etc/disktab.
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
2003-05-03 09:02:27 +00:00
|
|
|
static void
|
|
|
|
makelabel(const char *type, struct disklabel *lp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-18 06:17:50 +00:00
|
|
|
struct disklabel *dp;
|
1996-01-22 01:01:15 +00:00
|
|
|
|
|
|
|
if (strcmp(type, "auto") == 0)
|
|
|
|
dp = getvirginlabel();
|
|
|
|
else
|
1996-01-21 23:55:47 +00:00
|
|
|
dp = getdiskbyname(type);
|
1998-06-04 06:49:13 +00:00
|
|
|
if (dp == NULL)
|
|
|
|
errx(1, "%s: unknown disk type", type);
|
1994-05-26 06:35:07 +00:00
|
|
|
*lp = *dp;
|
|
|
|
bzero(lp->d_packname, sizeof(lp->d_packname));
|
|
|
|
}
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static void
|
|
|
|
readboot(void)
|
|
|
|
{
|
2010-06-30 18:34:45 +00:00
|
|
|
int fd;
|
2003-05-03 09:02:27 +00:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (xxboot == NULL)
|
|
|
|
xxboot = "/boot/boot";
|
|
|
|
fd = open(xxboot, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
err(1, "cannot open %s", xxboot);
|
|
|
|
fstat(fd, &st);
|
2011-03-10 08:24:21 +00:00
|
|
|
if (st.st_size <= BBSIZE) {
|
2010-06-30 18:34:45 +00:00
|
|
|
if (read(fd, bootarea, st.st_size) != st.st_size)
|
2003-05-03 09:02:27 +00:00
|
|
|
err(1, "read error %s", xxboot);
|
2010-12-01 08:07:32 +00:00
|
|
|
close(fd);
|
2003-05-03 09:02:27 +00:00
|
|
|
return;
|
2004-08-08 23:11:43 +00:00
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
errx(1, "boot code %s is wrong size", xxboot);
|
|
|
|
}
|
|
|
|
|
2010-08-27 11:08:11 +00:00
|
|
|
static int
|
2011-11-06 20:32:55 +00:00
|
|
|
geom_class_available(const char *name)
|
2010-08-27 11:08:11 +00:00
|
|
|
{
|
|
|
|
struct gclass *class;
|
|
|
|
struct gmesh mesh;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = geom_gettree(&mesh);
|
|
|
|
if (error != 0)
|
|
|
|
errc(1, error, "Cannot get GEOM tree");
|
|
|
|
|
|
|
|
LIST_FOREACH(class, &mesh.lg_class, lg_class) {
|
2011-11-06 20:32:55 +00:00
|
|
|
if (strcmp(class->lg_name, name) == 0) {
|
2010-08-27 11:08:11 +00:00
|
|
|
geom_deletetree(&mesh);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
geom_deletetree(&mesh);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static int
|
|
|
|
writelabel(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2010-08-27 11:08:11 +00:00
|
|
|
int i, fd, serrno;
|
2003-05-03 09:02:27 +00:00
|
|
|
struct disklabel *lp = &lab;
|
1998-08-17 07:43:54 +00:00
|
|
|
|
2001-02-25 16:47:44 +00:00
|
|
|
if (disable_write) {
|
2012-01-07 16:09:33 +00:00
|
|
|
warnx("write to disk label suppressed - label was as follows:");
|
2003-05-03 09:02:27 +00:00
|
|
|
display(stdout, NULL);
|
2001-02-25 16:47:44 +00:00
|
|
|
return (0);
|
2003-01-26 11:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lp->d_magic = DISKMAGIC;
|
|
|
|
lp->d_magic2 = DISKMAGIC;
|
|
|
|
lp->d_checksum = 0;
|
|
|
|
lp->d_checksum = dkcksum(lp);
|
2003-05-03 09:02:27 +00:00
|
|
|
if (installboot)
|
|
|
|
readboot();
|
2003-05-04 19:27:22 +00:00
|
|
|
for (i = 0; i < lab.d_npartitions; i++)
|
|
|
|
if (lab.d_partitions[i].p_size)
|
2009-08-19 16:29:20 +00:00
|
|
|
lab.d_partitions[i].p_offset += lba_offset;
|
2011-12-31 00:09:33 +00:00
|
|
|
bsd_disklabel_le_enc(bootarea + labeloffset + labelsoffset * lab.d_secsize,
|
2003-06-02 14:19:31 +00:00
|
|
|
lp);
|
2003-05-03 08:04:24 +00:00
|
|
|
|
|
|
|
fd = open(specname, O_RDWR);
|
|
|
|
if (fd < 0) {
|
2004-03-30 23:15:03 +00:00
|
|
|
if (is_file) {
|
|
|
|
warn("cannot open file %s for writing label", specname);
|
|
|
|
return(1);
|
2010-08-27 11:08:11 +00:00
|
|
|
} else
|
|
|
|
serrno = errno;
|
|
|
|
|
2011-11-06 20:32:55 +00:00
|
|
|
if (geom_class_available("PART") != 0) {
|
2011-11-07 07:51:10 +00:00
|
|
|
/*
|
|
|
|
* Since we weren't able open provider for
|
2011-11-06 20:32:55 +00:00
|
|
|
* writing, then recommend user to use gpart(8).
|
|
|
|
*/
|
|
|
|
warnc(serrno,
|
|
|
|
"cannot open provider %s for writing label",
|
|
|
|
specname);
|
|
|
|
warnx("Try to use gpart(8).");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2019-08-13 20:06:55 +00:00
|
|
|
warnc(serrno, "%s", specname);
|
|
|
|
return (1);
|
2003-05-03 08:04:24 +00:00
|
|
|
} else {
|
2003-05-03 09:02:27 +00:00
|
|
|
if (write(fd, bootarea, bbsize) != bbsize) {
|
2003-05-03 08:04:24 +00:00
|
|
|
warn("write %s", specname);
|
|
|
|
close (fd);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
close (fd);
|
2003-01-26 11:41:37 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2004-03-30 23:15:03 +00:00
|
|
|
static void
|
|
|
|
get_file_parms(int f)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (fstat(f, &sb) != 0)
|
|
|
|
err(4, "fstat failed");
|
|
|
|
i = sb.st_mode & S_IFMT;
|
|
|
|
if (i != S_IFREG && i != S_IFLNK)
|
|
|
|
errx(4, "%s is not a valid file or link", specname);
|
|
|
|
secsize = DEV_BSIZE;
|
|
|
|
mediasize = sb.st_size;
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
|
|
|
* Fetch disklabel for disk.
|
|
|
|
*/
|
2003-05-03 09:02:27 +00:00
|
|
|
static int
|
|
|
|
readlabel(int flag)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2010-06-30 18:34:45 +00:00
|
|
|
ssize_t nbytes;
|
2009-08-19 16:29:20 +00:00
|
|
|
uint32_t lba;
|
2003-05-04 19:27:22 +00:00
|
|
|
int f, i;
|
2003-05-03 08:04:24 +00:00
|
|
|
int error;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2003-05-03 08:04:24 +00:00
|
|
|
f = open(specname, O_RDONLY);
|
|
|
|
if (f < 0)
|
2010-11-22 20:10:48 +00:00
|
|
|
err(1, "%s", specname);
|
2004-03-30 23:15:03 +00:00
|
|
|
if (is_file)
|
|
|
|
get_file_parms(f);
|
2008-09-18 14:04:02 +00:00
|
|
|
else {
|
|
|
|
mediasize = g_mediasize(f);
|
|
|
|
secsize = g_sectorsize(f);
|
|
|
|
if (secsize < 0 || mediasize < 0)
|
|
|
|
err(4, "cannot get disk geometry");
|
2003-06-02 14:19:31 +00:00
|
|
|
}
|
2004-09-09 07:46:53 +00:00
|
|
|
if (mediasize > (off_t)0xffffffff * secsize)
|
|
|
|
errx(1,
|
|
|
|
"disks with more than 2^32-1 sectors are not supported");
|
2003-05-02 19:58:08 +00:00
|
|
|
(void)lseek(f, (off_t)0, SEEK_SET);
|
2010-06-30 18:34:45 +00:00
|
|
|
nbytes = read(f, bootarea, BBSIZE);
|
|
|
|
if (nbytes == -1)
|
2003-05-03 08:04:24 +00:00
|
|
|
err(4, "%s read", specname);
|
2010-06-30 18:34:45 +00:00
|
|
|
if (nbytes != BBSIZE)
|
|
|
|
errx(4, "couldn't read %d bytes from %s", BBSIZE, specname);
|
2003-05-03 08:04:24 +00:00
|
|
|
close (f);
|
2003-06-02 14:19:31 +00:00
|
|
|
error = bsd_disklabel_le_dec(
|
|
|
|
bootarea + (labeloffset + labelsoffset * secsize),
|
|
|
|
&lab, MAXPARTITIONS);
|
2003-05-03 09:02:27 +00:00
|
|
|
if (flag && error)
|
|
|
|
errx(1, "%s: no valid label found", specname);
|
2003-05-04 19:27:22 +00:00
|
|
|
|
2008-09-18 14:04:02 +00:00
|
|
|
if (is_file)
|
|
|
|
return(0);
|
2009-08-19 16:29:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compensate for absolute block addressing by finding the
|
|
|
|
* smallest partition offset and if the offset of the 'c'
|
|
|
|
* partition is equal to that, subtract it from all offsets.
|
|
|
|
*/
|
|
|
|
lba = ~0;
|
|
|
|
for (i = 0; i < lab.d_npartitions; i++) {
|
|
|
|
if (lab.d_partitions[i].p_size)
|
|
|
|
lba = MIN(lba, lab.d_partitions[i].p_offset);
|
2003-05-04 19:27:22 +00:00
|
|
|
}
|
2009-08-19 16:29:20 +00:00
|
|
|
if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) {
|
|
|
|
for (i = 0; i < lab.d_npartitions; i++) {
|
2003-05-04 19:27:22 +00:00
|
|
|
if (lab.d_partitions[i].p_size)
|
2009-08-19 16:29:20 +00:00
|
|
|
lab.d_partitions[i].p_offset -= lba;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Save the offset so that we can write the label
|
|
|
|
* back with absolute block addresses.
|
|
|
|
*/
|
|
|
|
lba_offset = lba;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
return (error);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static void
|
2002-03-18 06:17:50 +00:00
|
|
|
display(FILE *f, const struct disklabel *lp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-18 06:17:50 +00:00
|
|
|
int i, j;
|
|
|
|
const struct partition *pp;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
if (lp == NULL)
|
|
|
|
lp = &lab;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(f, "# %s:\n", specname);
|
2003-05-03 09:58:20 +00:00
|
|
|
if (allfields) {
|
|
|
|
if (lp->d_type < DKMAXTYPES)
|
|
|
|
fprintf(f, "type: %s\n", dktypenames[lp->d_type]);
|
|
|
|
else
|
|
|
|
fprintf(f, "type: %u\n", lp->d_type);
|
|
|
|
fprintf(f, "disk: %.*s\n", (int)sizeof(lp->d_typename),
|
|
|
|
lp->d_typename);
|
|
|
|
fprintf(f, "label: %.*s\n", (int)sizeof(lp->d_packname),
|
|
|
|
lp->d_packname);
|
|
|
|
fprintf(f, "flags:");
|
|
|
|
if (lp->d_flags & D_REMOVABLE)
|
|
|
|
fprintf(f, " removeable");
|
|
|
|
if (lp->d_flags & D_ECC)
|
|
|
|
fprintf(f, " ecc");
|
|
|
|
if (lp->d_flags & D_BADSECT)
|
|
|
|
fprintf(f, " badsect");
|
|
|
|
fprintf(f, "\n");
|
|
|
|
fprintf(f, "bytes/sector: %lu\n", (u_long)lp->d_secsize);
|
|
|
|
fprintf(f, "sectors/track: %lu\n", (u_long)lp->d_nsectors);
|
|
|
|
fprintf(f, "tracks/cylinder: %lu\n", (u_long)lp->d_ntracks);
|
|
|
|
fprintf(f, "sectors/cylinder: %lu\n", (u_long)lp->d_secpercyl);
|
|
|
|
fprintf(f, "cylinders: %lu\n", (u_long)lp->d_ncylinders);
|
|
|
|
fprintf(f, "sectors/unit: %lu\n", (u_long)lp->d_secperunit);
|
|
|
|
fprintf(f, "rpm: %u\n", lp->d_rpm);
|
|
|
|
fprintf(f, "interleave: %u\n", lp->d_interleave);
|
|
|
|
fprintf(f, "trackskew: %u\n", lp->d_trackskew);
|
|
|
|
fprintf(f, "cylinderskew: %u\n", lp->d_cylskew);
|
|
|
|
fprintf(f, "headswitch: %lu\t\t# milliseconds\n",
|
|
|
|
(u_long)lp->d_headswitch);
|
|
|
|
fprintf(f, "track-to-track seek: %ld\t# milliseconds\n",
|
|
|
|
(u_long)lp->d_trkseek);
|
|
|
|
fprintf(f, "drivedata: ");
|
|
|
|
for (i = NDDATA - 1; i >= 0; i--)
|
|
|
|
if (lp->d_drivedata[i])
|
|
|
|
break;
|
|
|
|
if (i < 0)
|
|
|
|
i = 0;
|
|
|
|
for (j = 0; j <= i; j++)
|
|
|
|
fprintf(f, "%lu ", (u_long)lp->d_drivedata[j]);
|
|
|
|
fprintf(f, "\n\n");
|
|
|
|
}
|
|
|
|
fprintf(f, "%u partitions:\n", lp->d_npartitions);
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(f,
|
2011-03-10 08:24:33 +00:00
|
|
|
"# size offset fstype [fsize bsize bps/cpg]\n");
|
1994-05-26 06:35:07 +00:00
|
|
|
pp = lp->d_partitions;
|
|
|
|
for (i = 0; i < lp->d_npartitions; i++, pp++) {
|
|
|
|
if (pp->p_size) {
|
2011-03-10 08:24:33 +00:00
|
|
|
fprintf(f, " %c: %10lu %10lu ", 'a' + i,
|
1998-06-28 18:59:04 +00:00
|
|
|
(u_long)pp->p_size, (u_long)pp->p_offset);
|
2002-11-18 04:58:11 +00:00
|
|
|
if (pp->p_fstype < FSMAXTYPES)
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(f, "%8.8s", fstypenames[pp->p_fstype]);
|
|
|
|
else
|
|
|
|
fprintf(f, "%8d", pp->p_fstype);
|
|
|
|
switch (pp->p_fstype) {
|
|
|
|
|
|
|
|
case FS_UNUSED: /* XXX */
|
2011-03-10 08:24:33 +00:00
|
|
|
fprintf(f, " %5lu %5lu %2s",
|
1998-06-28 18:59:04 +00:00
|
|
|
(u_long)pp->p_fsize,
|
|
|
|
(u_long)(pp->p_fsize * pp->p_frag), "");
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FS_BSDFFS:
|
2011-03-10 08:24:33 +00:00
|
|
|
fprintf(f, " %5lu %5lu %5u",
|
1998-06-28 18:59:04 +00:00
|
|
|
(u_long)pp->p_fsize,
|
|
|
|
(u_long)(pp->p_fsize * pp->p_frag),
|
1994-05-26 06:35:07 +00:00
|
|
|
pp->p_cpg);
|
|
|
|
break;
|
|
|
|
|
1995-01-04 23:49:51 +00:00
|
|
|
case FS_BSDLFS:
|
1998-06-28 18:59:04 +00:00
|
|
|
fprintf(f, " %5lu %5lu %5d",
|
|
|
|
(u_long)pp->p_fsize,
|
|
|
|
(u_long)(pp->p_fsize * pp->p_frag),
|
1995-01-04 23:49:51 +00:00
|
|
|
pp->p_cpg);
|
|
|
|
break;
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
default:
|
|
|
|
fprintf(f, "%20.20s", "");
|
|
|
|
break;
|
|
|
|
}
|
2003-05-03 09:58:20 +00:00
|
|
|
if (i == RAW_PART) {
|
|
|
|
fprintf(f, " # \"raw\" part, don't edit");
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
fprintf(f, "\n");
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fflush(f);
|
|
|
|
}
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static int
|
|
|
|
edit(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-18 06:17:50 +00:00
|
|
|
int c, fd;
|
1994-05-26 06:35:07 +00:00
|
|
|
struct disklabel label;
|
1997-03-24 05:44:28 +00:00
|
|
|
FILE *fp;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1997-03-24 05:44:28 +00:00
|
|
|
if ((fd = mkstemp(tmpfil)) == -1 ||
|
|
|
|
(fp = fdopen(fd, "w")) == NULL) {
|
1998-06-04 06:49:13 +00:00
|
|
|
warnx("can't create %s", tmpfil);
|
1994-05-26 06:35:07 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
display(fp, NULL);
|
1997-03-24 05:44:28 +00:00
|
|
|
fclose(fp);
|
1994-05-26 06:35:07 +00:00
|
|
|
for (;;) {
|
|
|
|
if (!editit())
|
|
|
|
break;
|
1997-03-24 05:44:28 +00:00
|
|
|
fp = fopen(tmpfil, "r");
|
|
|
|
if (fp == NULL) {
|
1998-06-04 06:49:13 +00:00
|
|
|
warnx("can't reopen %s for reading", tmpfil);
|
1994-05-26 06:35:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
bzero((char *)&label, sizeof(label));
|
2003-05-03 09:02:27 +00:00
|
|
|
c = getasciilabel(fp, &label);
|
|
|
|
fclose(fp);
|
|
|
|
if (c) {
|
|
|
|
lab = label;
|
|
|
|
if (writelabel() == 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
(void) unlink(tmpfil);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
printf("re-edit the label? [y]: ");
|
|
|
|
fflush(stdout);
|
1994-05-26 06:35:07 +00:00
|
|
|
c = getchar();
|
|
|
|
if (c != EOF && c != (int)'\n')
|
|
|
|
while (getchar() != (int)'\n')
|
|
|
|
;
|
|
|
|
if (c == (int)'n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(void) unlink(tmpfil);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static int
|
2002-03-18 06:17:50 +00:00
|
|
|
editit(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-18 06:17:50 +00:00
|
|
|
int pid, xpid;
|
2002-07-03 16:43:11 +00:00
|
|
|
int locstat, omask;
|
|
|
|
const char *ed;
|
2010-05-16 08:03:24 +00:00
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
|
|
|
|
while ((pid = fork()) < 0) {
|
|
|
|
if (errno == EPROCLIM) {
|
1998-06-04 06:49:13 +00:00
|
|
|
warnx("you have too many processes");
|
1994-05-26 06:35:07 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
if (errno != EAGAIN) {
|
1998-06-04 06:49:13 +00:00
|
|
|
warn("fork");
|
1994-05-26 06:35:07 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
if (pid == 0) {
|
|
|
|
sigsetmask(omask);
|
2010-05-16 08:03:24 +00:00
|
|
|
gid = getgid();
|
|
|
|
if (setresgid(gid, gid, gid) == -1)
|
|
|
|
err(1, "setresgid");
|
|
|
|
uid = getuid();
|
|
|
|
if (setresuid(uid, uid, uid) == -1)
|
|
|
|
err(1, "setresuid");
|
1994-05-26 06:35:07 +00:00
|
|
|
if ((ed = getenv("EDITOR")) == (char *)0)
|
|
|
|
ed = DEFEDITOR;
|
2001-07-09 09:24:06 +00:00
|
|
|
execlp(ed, ed, tmpfil, (char *)0);
|
1998-06-04 06:49:13 +00:00
|
|
|
err(1, "%s", ed);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2002-07-03 16:43:11 +00:00
|
|
|
while ((xpid = wait(&locstat)) >= 0)
|
1994-05-26 06:35:07 +00:00
|
|
|
if (xpid == pid)
|
|
|
|
break;
|
|
|
|
sigsetmask(omask);
|
2002-07-03 16:43:11 +00:00
|
|
|
return(!locstat);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static char *
|
2002-03-18 06:17:50 +00:00
|
|
|
skip(char *cp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
while (*cp != '\0' && isspace(*cp))
|
|
|
|
cp++;
|
|
|
|
if (*cp == '\0' || *cp == '#')
|
2002-03-18 06:17:50 +00:00
|
|
|
return (NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
return (cp);
|
|
|
|
}
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static char *
|
2002-03-18 06:17:50 +00:00
|
|
|
word(char *cp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-18 06:17:50 +00:00
|
|
|
char c;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
while (*cp != '\0' && !isspace(*cp) && *cp != '#')
|
|
|
|
cp++;
|
|
|
|
if ((c = *cp) != '\0') {
|
|
|
|
*cp++ = '\0';
|
|
|
|
if (c != '#')
|
|
|
|
return (skip(cp));
|
|
|
|
}
|
2002-03-18 06:17:50 +00:00
|
|
|
return (NULL);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read an ascii label in from fd f,
|
|
|
|
* in the same format as that put out by display(),
|
|
|
|
* and fill in lp.
|
|
|
|
*/
|
2003-05-03 09:02:27 +00:00
|
|
|
static int
|
2002-03-18 06:17:50 +00:00
|
|
|
getasciilabel(FILE *f, struct disklabel *lp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2010-08-15 17:49:41 +00:00
|
|
|
char *cp, *endp;
|
2002-04-07 10:57:00 +00:00
|
|
|
const char **cpp;
|
2002-11-18 04:58:11 +00:00
|
|
|
u_int part;
|
2002-04-07 10:57:00 +00:00
|
|
|
char *tp, line[BUFSIZ];
|
2002-11-18 04:58:11 +00:00
|
|
|
u_long v;
|
|
|
|
int lineno = 0, errors = 0;
|
2002-03-18 06:17:50 +00:00
|
|
|
int i;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2003-05-09 20:26:17 +00:00
|
|
|
makelabel("auto", lp);
|
2003-01-16 12:57:53 +00:00
|
|
|
bzero(&part_set, sizeof(part_set));
|
|
|
|
bzero(&part_size_type, sizeof(part_size_type));
|
|
|
|
bzero(&part_offset_type, sizeof(part_offset_type));
|
1994-05-26 06:35:07 +00:00
|
|
|
lp->d_bbsize = BBSIZE; /* XXX */
|
2002-05-12 20:49:41 +00:00
|
|
|
lp->d_sbsize = 0; /* XXX */
|
1994-05-26 06:35:07 +00:00
|
|
|
while (fgets(line, sizeof(line) - 1, f)) {
|
|
|
|
lineno++;
|
2016-04-18 07:40:36 +00:00
|
|
|
if ((cp = strchr(line,'\n')) != NULL)
|
1994-05-26 06:35:07 +00:00
|
|
|
*cp = '\0';
|
|
|
|
cp = skip(line);
|
|
|
|
if (cp == NULL)
|
|
|
|
continue;
|
2012-01-03 18:51:58 +00:00
|
|
|
tp = strchr(cp, ':');
|
1994-05-26 06:35:07 +00:00
|
|
|
if (tp == NULL) {
|
|
|
|
fprintf(stderr, "line %d: syntax error\n", lineno);
|
|
|
|
errors++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*tp++ = '\0', tp = skip(tp);
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "type")) {
|
1994-05-26 06:35:07 +00:00
|
|
|
if (tp == NULL)
|
2002-07-03 16:43:11 +00:00
|
|
|
tp = unknown;
|
1994-05-26 06:35:07 +00:00
|
|
|
cpp = dktypenames;
|
|
|
|
for (; cpp < &dktypenames[DKMAXTYPES]; cpp++)
|
2003-05-03 09:02:27 +00:00
|
|
|
if (*cpp && !strcmp(*cpp, tp)) {
|
1994-05-26 06:35:07 +00:00
|
|
|
lp->d_type = cpp - dktypenames;
|
2002-06-05 08:11:28 +00:00
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2002-06-05 08:11:28 +00:00
|
|
|
if (cpp < &dktypenames[DKMAXTYPES])
|
|
|
|
continue;
|
2010-08-15 17:49:41 +00:00
|
|
|
errno = 0;
|
|
|
|
v = strtoul(tp, &endp, 10);
|
|
|
|
if (errno != 0 || *endp != '\0')
|
|
|
|
v = DKMAXTYPES;
|
2002-11-18 04:58:11 +00:00
|
|
|
if (v >= DKMAXTYPES)
|
|
|
|
fprintf(stderr, "line %d:%s %lu\n", lineno,
|
1994-05-26 06:35:07 +00:00
|
|
|
"Warning, unknown disk type", v);
|
2010-08-15 17:49:41 +00:00
|
|
|
else
|
|
|
|
lp->d_type = v;
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "flags")) {
|
1994-05-26 06:35:07 +00:00
|
|
|
for (v = 0; (cp = tp) && *cp != '\0';) {
|
|
|
|
tp = word(cp);
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "removeable"))
|
1994-05-26 06:35:07 +00:00
|
|
|
v |= D_REMOVABLE;
|
2003-05-03 09:02:27 +00:00
|
|
|
else if (!strcmp(cp, "ecc"))
|
1994-05-26 06:35:07 +00:00
|
|
|
v |= D_ECC;
|
2003-05-03 09:02:27 +00:00
|
|
|
else if (!strcmp(cp, "badsect"))
|
1994-05-26 06:35:07 +00:00
|
|
|
v |= D_BADSECT;
|
|
|
|
else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"line %d: %s: bad flag\n",
|
|
|
|
lineno, cp);
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lp->d_flags = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "drivedata")) {
|
1994-05-26 06:35:07 +00:00
|
|
|
for (i = 0; (cp = tp) && *cp != '\0' && i < NDDATA;) {
|
2002-11-18 04:58:11 +00:00
|
|
|
lp->d_drivedata[i++] = strtoul(cp, NULL, 10);
|
1994-05-26 06:35:07 +00:00
|
|
|
tp = word(cp);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2002-11-18 04:58:11 +00:00
|
|
|
if (sscanf(cp, "%lu partitions", &v) == 1) {
|
2011-11-06 19:03:07 +00:00
|
|
|
if (v > MAXPARTITIONS) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"line %d: bad # of partitions\n", lineno);
|
|
|
|
lp->d_npartitions = MAXPARTITIONS;
|
|
|
|
errors++;
|
2011-11-06 18:59:42 +00:00
|
|
|
} else if (v < DEFPARTITIONS) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"line %d: bad # of partitions\n", lineno);
|
|
|
|
lp->d_npartitions = DEFPARTITIONS;
|
|
|
|
errors++;
|
1994-05-26 06:35:07 +00:00
|
|
|
} else
|
|
|
|
lp->d_npartitions = v;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (tp == NULL)
|
2002-07-03 16:43:11 +00:00
|
|
|
tp = blank;
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "disk")) {
|
1994-05-26 06:35:07 +00:00
|
|
|
strncpy(lp->d_typename, tp, sizeof (lp->d_typename));
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "label")) {
|
1994-05-26 06:35:07 +00:00
|
|
|
strncpy(lp->d_packname, tp, sizeof (lp->d_packname));
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "bytes/sector")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
if (v == 0 || (v % DEV_BSIZE) != 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"line %d: %s: bad sector size\n",
|
|
|
|
lineno, tp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_secsize = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "sectors/track")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
#if (ULONG_MAX != 0xffffffffUL)
|
2004-08-08 23:11:43 +00:00
|
|
|
if (v == 0 || v > 0xffffffff)
|
2002-11-18 04:58:11 +00:00
|
|
|
#else
|
2004-08-08 23:11:43 +00:00
|
|
|
if (v == 0)
|
2002-11-18 04:58:11 +00:00
|
|
|
#endif
|
2004-08-08 23:11:43 +00:00
|
|
|
{
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr, "line %d: %s: bad %s\n",
|
|
|
|
lineno, tp, cp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_nsectors = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "sectors/cylinder")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
if (v == 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr, "line %d: %s: bad %s\n",
|
|
|
|
lineno, tp, cp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_secpercyl = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "tracks/cylinder")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
if (v == 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr, "line %d: %s: bad %s\n",
|
|
|
|
lineno, tp, cp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_ntracks = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "cylinders")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
if (v == 0) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr, "line %d: %s: bad %s\n",
|
|
|
|
lineno, tp, cp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_ncylinders = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "sectors/unit")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
if (v == 0) {
|
1995-02-22 23:21:29 +00:00
|
|
|
fprintf(stderr, "line %d: %s: bad %s\n",
|
|
|
|
lineno, tp, cp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_secperunit = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "rpm")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
if (v == 0 || v > USHRT_MAX) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr, "line %d: %s: bad %s\n",
|
|
|
|
lineno, tp, cp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_rpm = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "interleave")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
if (v == 0 || v > USHRT_MAX) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr, "line %d: %s: bad %s\n",
|
|
|
|
lineno, tp, cp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_interleave = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "trackskew")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
if (v > USHRT_MAX) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr, "line %d: %s: bad %s\n",
|
|
|
|
lineno, tp, cp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_trackskew = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "cylinderskew")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
if (v > USHRT_MAX) {
|
1994-05-26 06:35:07 +00:00
|
|
|
fprintf(stderr, "line %d: %s: bad %s\n",
|
|
|
|
lineno, tp, cp);
|
|
|
|
errors++;
|
|
|
|
} else
|
|
|
|
lp->d_cylskew = v;
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "headswitch")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
lp->d_headswitch = v;
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2003-05-03 09:02:27 +00:00
|
|
|
if (!strcmp(cp, "track-to-track seek")) {
|
2002-11-18 04:58:11 +00:00
|
|
|
v = strtoul(tp, NULL, 10);
|
|
|
|
lp->d_trkseek = v;
|
1994-05-26 06:35:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-02-25 16:47:44 +00:00
|
|
|
/* the ':' was removed above */
|
2002-05-30 01:19:25 +00:00
|
|
|
if (*cp < 'a' || *cp > MAX_PART || cp[1] != '\0') {
|
|
|
|
fprintf(stderr,
|
|
|
|
"line %d: %s: Unknown disklabel field\n", lineno,
|
|
|
|
cp);
|
|
|
|
errors++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process a partition specification line. */
|
|
|
|
part = *cp - 'a';
|
|
|
|
if (part >= lp->d_npartitions) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"line %d: partition name out of range a-%c: %s\n",
|
|
|
|
lineno, 'a' + lp->d_npartitions - 1, cp);
|
|
|
|
errors++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
part_set[part] = 1;
|
|
|
|
|
|
|
|
if (getasciipartspec(tp, lp, part, lineno) != 0) {
|
|
|
|
errors++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errors += checklabel(lp);
|
|
|
|
return (errors == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NXTNUM(n) do { \
|
1994-09-26 04:45:11 +00:00
|
|
|
if (tp == NULL) { \
|
|
|
|
fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
|
2002-05-30 01:19:25 +00:00
|
|
|
return (1); \
|
1994-09-26 04:45:11 +00:00
|
|
|
} else { \
|
|
|
|
cp = tp, tp = word(cp); \
|
2002-11-18 04:58:11 +00:00
|
|
|
(n) = strtoul(cp, NULL, 10); \
|
1994-09-26 04:45:11 +00:00
|
|
|
} \
|
2002-05-30 01:19:25 +00:00
|
|
|
} while (0)
|
|
|
|
|
2001-02-25 16:47:44 +00:00
|
|
|
/* retain 1 character following number */
|
2002-05-30 01:19:25 +00:00
|
|
|
#define NXTWORD(w,n) do { \
|
2001-02-25 16:47:44 +00:00
|
|
|
if (tp == NULL) { \
|
|
|
|
fprintf(stderr, "line %d: too few numeric fields\n", lineno); \
|
2002-05-30 01:19:25 +00:00
|
|
|
return (1); \
|
2001-02-25 16:47:44 +00:00
|
|
|
} else { \
|
2004-08-08 23:11:43 +00:00
|
|
|
char *tmp; \
|
2001-02-25 16:47:44 +00:00
|
|
|
cp = tp, tp = word(cp); \
|
2004-08-08 23:11:43 +00:00
|
|
|
(n) = strtoul(cp, &tmp, 10); \
|
2001-02-25 16:47:44 +00:00
|
|
|
if (tmp) (w) = *tmp; \
|
|
|
|
} \
|
2002-05-30 01:19:25 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a partition line into partition `part' in the specified disklabel.
|
|
|
|
* Return 0 on success, 1 on failure.
|
|
|
|
*/
|
2003-05-03 09:02:27 +00:00
|
|
|
static int
|
2002-05-30 01:19:25 +00:00
|
|
|
getasciipartspec(char *tp, struct disklabel *lp, int part, int lineno)
|
|
|
|
{
|
|
|
|
struct partition *pp;
|
2010-08-15 17:49:41 +00:00
|
|
|
char *cp, *endp;
|
2002-05-30 01:19:25 +00:00
|
|
|
const char **cpp;
|
2002-11-18 04:58:11 +00:00
|
|
|
u_long v;
|
2002-05-30 01:19:25 +00:00
|
|
|
|
|
|
|
pp = &lp->d_partitions[part];
|
|
|
|
cp = NULL;
|
|
|
|
|
|
|
|
v = 0;
|
|
|
|
NXTWORD(part_size_type[part],v);
|
2002-11-18 04:58:11 +00:00
|
|
|
if (v == 0 && part_size_type[part] != '*') {
|
|
|
|
fprintf(stderr,
|
|
|
|
"line %d: %s: bad partition size\n", lineno, cp);
|
2002-05-30 01:19:25 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
pp->p_size = v;
|
|
|
|
|
|
|
|
v = 0;
|
|
|
|
NXTWORD(part_offset_type[part],v);
|
2002-11-18 04:58:11 +00:00
|
|
|
if (v == 0 && part_offset_type[part] != '*' &&
|
|
|
|
part_offset_type[part] != '\0') {
|
|
|
|
fprintf(stderr,
|
|
|
|
"line %d: %s: bad partition offset\n", lineno, cp);
|
2002-05-30 01:19:25 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
pp->p_offset = v;
|
2003-04-01 14:44:53 +00:00
|
|
|
if (tp == NULL) {
|
|
|
|
fprintf(stderr, "line %d: missing file system type\n", lineno);
|
|
|
|
return (1);
|
|
|
|
}
|
2002-05-30 01:19:25 +00:00
|
|
|
cp = tp, tp = word(cp);
|
|
|
|
for (cpp = fstypenames; cpp < &fstypenames[FSMAXTYPES]; cpp++)
|
2003-05-03 09:02:27 +00:00
|
|
|
if (*cpp && !strcmp(*cpp, cp))
|
2002-05-30 01:19:25 +00:00
|
|
|
break;
|
|
|
|
if (*cpp != NULL) {
|
|
|
|
pp->p_fstype = cpp - fstypenames;
|
|
|
|
} else {
|
2010-08-15 17:49:41 +00:00
|
|
|
if (isdigit(*cp)) {
|
|
|
|
errno = 0;
|
|
|
|
v = strtoul(cp, &endp, 10);
|
|
|
|
if (errno != 0 || *endp != '\0')
|
|
|
|
v = FSMAXTYPES;
|
|
|
|
} else
|
2002-05-30 01:19:25 +00:00
|
|
|
v = FSMAXTYPES;
|
2002-11-18 04:58:11 +00:00
|
|
|
if (v >= FSMAXTYPES) {
|
2002-05-30 01:19:25 +00:00
|
|
|
fprintf(stderr,
|
2002-08-21 18:11:48 +00:00
|
|
|
"line %d: Warning, unknown file system type %s\n",
|
2002-05-30 01:19:25 +00:00
|
|
|
lineno, cp);
|
|
|
|
v = FS_UNUSED;
|
|
|
|
}
|
|
|
|
pp->p_fstype = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (pp->p_fstype) {
|
|
|
|
case FS_UNUSED:
|
|
|
|
case FS_BSDFFS:
|
|
|
|
case FS_BSDLFS:
|
2003-10-05 19:40:02 +00:00
|
|
|
/* accept defaults for fsize/frag/cpg */
|
2002-05-30 01:19:25 +00:00
|
|
|
if (tp) {
|
|
|
|
NXTNUM(pp->p_fsize);
|
|
|
|
if (pp->p_fsize == 0)
|
|
|
|
break;
|
|
|
|
NXTNUM(v);
|
|
|
|
pp->p_frag = v / pp->p_fsize;
|
2003-10-05 19:40:02 +00:00
|
|
|
if (tp != NULL)
|
|
|
|
NXTNUM(pp->p_cpg);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2003-10-05 19:40:02 +00:00
|
|
|
/* else default to 0's */
|
|
|
|
break;
|
2002-05-30 01:19:25 +00:00
|
|
|
default:
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2002-05-30 01:19:25 +00:00
|
|
|
return (0);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check disklabel for errors and fill in
|
|
|
|
* derived fields according to supplied values.
|
|
|
|
*/
|
2003-05-03 09:02:27 +00:00
|
|
|
static int
|
2002-03-18 06:17:50 +00:00
|
|
|
checklabel(struct disklabel *lp)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2002-03-18 06:17:50 +00:00
|
|
|
struct partition *pp;
|
1994-05-26 06:35:07 +00:00
|
|
|
int i, errors = 0;
|
|
|
|
char part;
|
2005-08-14 22:46:50 +00:00
|
|
|
u_long base_offset, needed, total_size, total_percent, current_offset;
|
|
|
|
long free_space;
|
2001-02-25 16:47:44 +00:00
|
|
|
int seen_default_offset;
|
|
|
|
int hog_part;
|
|
|
|
int j;
|
|
|
|
struct partition *pp2;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
if (lp == NULL)
|
|
|
|
lp = &lab;
|
|
|
|
|
2003-05-03 09:58:20 +00:00
|
|
|
if (allfields) {
|
|
|
|
|
|
|
|
if (lp->d_secsize == 0) {
|
|
|
|
fprintf(stderr, "sector size 0\n");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (lp->d_nsectors == 0) {
|
|
|
|
fprintf(stderr, "sectors/track 0\n");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (lp->d_ntracks == 0) {
|
|
|
|
fprintf(stderr, "tracks/cylinder 0\n");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (lp->d_ncylinders == 0) {
|
|
|
|
fprintf(stderr, "cylinders/unit 0\n");
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
if (lp->d_rpm == 0)
|
|
|
|
warnx("revolutions/minute 0");
|
|
|
|
if (lp->d_secpercyl == 0)
|
|
|
|
lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
|
|
|
|
if (lp->d_secperunit == 0)
|
|
|
|
lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
|
|
|
|
if (lp->d_bbsize == 0) {
|
|
|
|
fprintf(stderr, "boot block size 0\n");
|
|
|
|
errors++;
|
|
|
|
} else if (lp->d_bbsize % lp->d_secsize)
|
|
|
|
warnx("boot block size %% sector-size != 0");
|
2011-11-06 18:59:42 +00:00
|
|
|
if (lp->d_npartitions > MAXPARTITIONS) {
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("number of partitions (%lu) > MAXPARTITIONS (%d)",
|
|
|
|
(u_long)lp->d_npartitions, MAXPARTITIONS);
|
2011-11-06 18:59:42 +00:00
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
if (lp->d_npartitions < DEFPARTITIONS) {
|
|
|
|
warnx("number of partitions (%lu) < DEFPARTITIONS (%d)",
|
|
|
|
(u_long)lp->d_npartitions, DEFPARTITIONS);
|
|
|
|
errors++;
|
|
|
|
}
|
2003-05-03 09:58:20 +00:00
|
|
|
} else {
|
|
|
|
struct disklabel *vl;
|
|
|
|
|
|
|
|
vl = getvirginlabel();
|
2011-11-06 18:59:42 +00:00
|
|
|
if (lp->d_secsize == 0)
|
|
|
|
lp->d_secsize = vl->d_secsize;
|
|
|
|
if (lp->d_nsectors == 0)
|
|
|
|
lp->d_nsectors = vl->d_nsectors;
|
|
|
|
if (lp->d_ntracks == 0)
|
|
|
|
lp->d_ntracks = vl->d_ntracks;
|
|
|
|
if (lp->d_ncylinders == 0)
|
|
|
|
lp->d_ncylinders = vl->d_ncylinders;
|
|
|
|
if (lp->d_rpm == 0)
|
|
|
|
lp->d_rpm = vl->d_rpm;
|
|
|
|
if (lp->d_interleave == 0)
|
|
|
|
lp->d_interleave = vl->d_interleave;
|
|
|
|
if (lp->d_secpercyl == 0)
|
|
|
|
lp->d_secpercyl = vl->d_secpercyl;
|
2012-04-11 06:35:13 +00:00
|
|
|
if (lp->d_secperunit == 0 ||
|
|
|
|
lp->d_secperunit > vl->d_secperunit)
|
2011-11-06 18:59:42 +00:00
|
|
|
lp->d_secperunit = vl->d_secperunit;
|
|
|
|
if (lp->d_bbsize == 0)
|
|
|
|
lp->d_bbsize = vl->d_bbsize;
|
2011-11-06 19:03:07 +00:00
|
|
|
if (lp->d_npartitions < DEFPARTITIONS ||
|
2011-11-06 18:59:42 +00:00
|
|
|
lp->d_npartitions > MAXPARTITIONS)
|
|
|
|
lp->d_npartitions = vl->d_npartitions;
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2003-05-03 09:58:20 +00:00
|
|
|
|
2001-02-25 16:47:44 +00:00
|
|
|
|
|
|
|
/* first allocate space to the partitions, then offsets */
|
|
|
|
total_size = 0; /* in sectors */
|
|
|
|
total_percent = 0; /* in percent */
|
|
|
|
hog_part = -1;
|
|
|
|
/* find all fixed partitions */
|
|
|
|
for (i = 0; i < lp->d_npartitions; i++) {
|
|
|
|
pp = &lp->d_partitions[i];
|
|
|
|
if (part_set[i]) {
|
|
|
|
if (part_size_type[i] == '*') {
|
2002-04-07 10:57:00 +00:00
|
|
|
if (i == RAW_PART) {
|
2001-02-25 16:47:44 +00:00
|
|
|
pp->p_size = lp->d_secperunit;
|
|
|
|
} else {
|
|
|
|
if (hog_part != -1)
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("Too many '*' partitions (%c and %c)",
|
2001-02-25 16:47:44 +00:00
|
|
|
hog_part + 'a',i + 'a');
|
|
|
|
else
|
|
|
|
hog_part = i;
|
|
|
|
}
|
|
|
|
} else {
|
2001-03-05 03:39:57 +00:00
|
|
|
off_t size;
|
2001-02-25 16:47:44 +00:00
|
|
|
|
|
|
|
size = pp->p_size;
|
|
|
|
switch (part_size_type[i]) {
|
|
|
|
case '%':
|
|
|
|
total_percent += size;
|
|
|
|
break;
|
2004-08-08 23:14:44 +00:00
|
|
|
case 't':
|
|
|
|
case 'T':
|
2001-03-05 03:39:57 +00:00
|
|
|
size *= 1024ULL;
|
2004-08-08 23:14:44 +00:00
|
|
|
/* FALLTHROUGH */
|
2001-02-25 16:47:44 +00:00
|
|
|
case 'g':
|
|
|
|
case 'G':
|
2004-08-08 23:14:44 +00:00
|
|
|
size *= 1024ULL;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
size *= 1024ULL;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
size *= 1024ULL;
|
2001-02-25 16:47:44 +00:00
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
default:
|
2004-08-09 14:43:50 +00:00
|
|
|
warnx("unknown multiplier suffix '%c' for partition %c (should be K, M, G or T)",
|
2004-08-08 23:14:44 +00:00
|
|
|
part_size_type[i], i + 'a');
|
2001-02-25 16:47:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* don't count %'s yet */
|
|
|
|
if (part_size_type[i] != '%') {
|
|
|
|
/*
|
|
|
|
* for all not in sectors, convert to
|
|
|
|
* sectors
|
|
|
|
*/
|
|
|
|
if (part_size_type[i] != '\0') {
|
|
|
|
if (size % lp->d_secsize != 0)
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("partition %c not an integer number of sectors",
|
2001-02-25 16:47:44 +00:00
|
|
|
i + 'a');
|
|
|
|
size /= lp->d_secsize;
|
|
|
|
pp->p_size = size;
|
|
|
|
}
|
|
|
|
/* else already in sectors */
|
2002-04-07 10:57:00 +00:00
|
|
|
if (i != RAW_PART)
|
2001-02-25 16:47:44 +00:00
|
|
|
total_size += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-08-14 22:46:50 +00:00
|
|
|
|
|
|
|
/* Find out the total free space, excluding the boot block area. */
|
|
|
|
base_offset = BBSIZE / secsize;
|
|
|
|
free_space = 0;
|
|
|
|
for (i = 0; i < lp->d_npartitions; i++) {
|
|
|
|
pp = &lp->d_partitions[i];
|
|
|
|
if (!part_set[i] || i == RAW_PART ||
|
|
|
|
part_size_type[i] == '%' || part_size_type[i] == '*')
|
|
|
|
continue;
|
|
|
|
if (pp->p_offset > base_offset)
|
|
|
|
free_space += pp->p_offset - base_offset;
|
|
|
|
if (pp->p_offset + pp->p_size > base_offset)
|
|
|
|
base_offset = pp->p_offset + pp->p_size;
|
|
|
|
}
|
|
|
|
if (base_offset < lp->d_secperunit)
|
|
|
|
free_space += lp->d_secperunit - base_offset;
|
|
|
|
|
2001-02-25 16:47:44 +00:00
|
|
|
/* handle % partitions - note %'s don't need to add up to 100! */
|
|
|
|
if (total_percent != 0) {
|
|
|
|
if (total_percent > 100) {
|
2002-04-07 10:57:00 +00:00
|
|
|
fprintf(stderr,"total percentage %lu is greater than 100\n",
|
2001-02-25 16:47:44 +00:00
|
|
|
total_percent);
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (free_space > 0) {
|
|
|
|
for (i = 0; i < lp->d_npartitions; i++) {
|
|
|
|
pp = &lp->d_partitions[i];
|
|
|
|
if (part_set[i] && part_size_type[i] == '%') {
|
|
|
|
/* careful of overflows! and integer roundoff */
|
|
|
|
pp->p_size = ((double)pp->p_size/100) * free_space;
|
|
|
|
total_size += pp->p_size;
|
|
|
|
|
|
|
|
/* FIX we can lose a sector or so due to roundoff per
|
|
|
|
partition. A more complex algorithm could avoid that */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
2002-04-07 10:57:00 +00:00
|
|
|
"%ld sectors available to give to '*' and '%%' partitions\n",
|
2001-02-25 16:47:44 +00:00
|
|
|
free_space);
|
|
|
|
errors++;
|
|
|
|
/* fix? set all % partitions to size 0? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* give anything remaining to the hog partition */
|
|
|
|
if (hog_part != -1) {
|
2005-08-14 22:46:50 +00:00
|
|
|
/*
|
|
|
|
* Find the range of offsets usable by '*' partitions around
|
|
|
|
* the hog partition and how much space they need.
|
|
|
|
*/
|
|
|
|
needed = 0;
|
|
|
|
base_offset = BBSIZE / secsize;
|
|
|
|
for (i = hog_part - 1; i >= 0; i--) {
|
|
|
|
pp = &lp->d_partitions[i];
|
|
|
|
if (!part_set[i] || i == RAW_PART)
|
|
|
|
continue;
|
|
|
|
if (part_offset_type[i] == '*') {
|
|
|
|
needed += pp->p_size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
base_offset = pp->p_offset + pp->p_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
current_offset = lp->d_secperunit;
|
|
|
|
for (i = lp->d_npartitions - 1; i > hog_part; i--) {
|
|
|
|
pp = &lp->d_partitions[i];
|
|
|
|
if (!part_set[i] || i == RAW_PART)
|
|
|
|
continue;
|
|
|
|
if (part_offset_type[i] == '*') {
|
|
|
|
needed += pp->p_size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
current_offset = pp->p_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_offset - base_offset <= needed) {
|
|
|
|
fprintf(stderr, "Cannot find space for partition %c\n",
|
|
|
|
hog_part + 'a');
|
|
|
|
fprintf(stderr,
|
|
|
|
"Need more than %lu sectors between %lu and %lu\n",
|
|
|
|
needed, base_offset, current_offset);
|
|
|
|
errors++;
|
|
|
|
lp->d_partitions[hog_part].p_size = 0;
|
|
|
|
} else {
|
|
|
|
lp->d_partitions[hog_part].p_size = current_offset -
|
|
|
|
base_offset - needed;
|
|
|
|
total_size += lp->d_partitions[hog_part].p_size;
|
|
|
|
}
|
2001-02-25 16:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now set the offsets for each partition */
|
2005-08-14 22:46:50 +00:00
|
|
|
current_offset = BBSIZE / secsize; /* in sectors */
|
2001-02-25 16:47:44 +00:00
|
|
|
seen_default_offset = 0;
|
|
|
|
for (i = 0; i < lp->d_npartitions; i++) {
|
|
|
|
part = 'a' + i;
|
|
|
|
pp = &lp->d_partitions[i];
|
|
|
|
if (part_set[i]) {
|
|
|
|
if (part_offset_type[i] == '*') {
|
2002-04-07 10:57:00 +00:00
|
|
|
if (i == RAW_PART) {
|
2001-02-25 16:47:44 +00:00
|
|
|
pp->p_offset = 0;
|
|
|
|
} else {
|
|
|
|
pp->p_offset = current_offset;
|
|
|
|
seen_default_offset = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* allow them to be out of order for old-style tables */
|
2004-08-08 23:11:43 +00:00
|
|
|
if (pp->p_offset < current_offset &&
|
2002-12-02 23:49:59 +00:00
|
|
|
seen_default_offset && i != RAW_PART &&
|
|
|
|
pp->p_fstype != FS_VINUM) {
|
2001-02-25 16:47:44 +00:00
|
|
|
fprintf(stderr,
|
2002-04-07 10:57:00 +00:00
|
|
|
"Offset %ld for partition %c overlaps previous partition which ends at %lu\n",
|
|
|
|
(long)pp->p_offset,i+'a',current_offset);
|
2001-02-25 16:47:44 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Labels with any *'s for offset must be in ascending order by sector\n");
|
|
|
|
errors++;
|
|
|
|
} else if (pp->p_offset != current_offset &&
|
2002-04-07 10:57:00 +00:00
|
|
|
i != RAW_PART && seen_default_offset) {
|
2004-08-08 23:11:43 +00:00
|
|
|
/*
|
|
|
|
* this may give unneeded warnings if
|
2001-02-25 16:47:44 +00:00
|
|
|
* partitions are out-of-order
|
|
|
|
*/
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx(
|
2001-02-25 16:47:44 +00:00
|
|
|
"Offset %ld for partition %c doesn't match expected value %ld",
|
2002-04-07 10:57:00 +00:00
|
|
|
(long)pp->p_offset, i + 'a', current_offset);
|
2001-02-25 16:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
2002-04-07 10:57:00 +00:00
|
|
|
if (i != RAW_PART)
|
2004-08-08 23:11:43 +00:00
|
|
|
current_offset = pp->p_offset + pp->p_size;
|
2001-02-25 16:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
for (i = 0; i < lp->d_npartitions; i++) {
|
|
|
|
part = 'a' + i;
|
|
|
|
pp = &lp->d_partitions[i];
|
|
|
|
if (pp->p_size == 0 && pp->p_offset != 0)
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("partition %c: size 0, but offset %lu",
|
1998-06-28 18:59:04 +00:00
|
|
|
part, (u_long)pp->p_offset);
|
1994-05-26 06:35:07 +00:00
|
|
|
#ifdef notdef
|
|
|
|
if (pp->p_size % lp->d_secpercyl)
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("partition %c: size %% cylinder-size != 0",
|
1994-05-26 06:35:07 +00:00
|
|
|
part);
|
|
|
|
if (pp->p_offset % lp->d_secpercyl)
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("partition %c: offset %% cylinder-size != 0",
|
1994-05-26 06:35:07 +00:00
|
|
|
part);
|
|
|
|
#endif
|
|
|
|
if (pp->p_offset > lp->d_secperunit) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"partition %c: offset past end of unit\n", part);
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
if (pp->p_offset + pp->p_size > lp->d_secperunit) {
|
|
|
|
fprintf(stderr,
|
1996-01-21 23:55:47 +00:00
|
|
|
"partition %c: partition extends past end of unit\n",
|
1994-05-26 06:35:07 +00:00
|
|
|
part);
|
|
|
|
errors++;
|
|
|
|
}
|
2003-05-03 09:58:20 +00:00
|
|
|
if (i == RAW_PART) {
|
2001-02-25 16:47:44 +00:00
|
|
|
if (pp->p_fstype != FS_UNUSED)
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("partition %c is not marked as unused!",part);
|
2001-02-25 16:47:44 +00:00
|
|
|
if (pp->p_offset != 0)
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("partition %c doesn't start at 0!",part);
|
2001-02-25 16:47:44 +00:00
|
|
|
if (pp->p_size != lp->d_secperunit)
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("partition %c doesn't cover the whole unit!",part);
|
2001-02-25 16:47:44 +00:00
|
|
|
|
|
|
|
if ((pp->p_fstype != FS_UNUSED) || (pp->p_offset != 0) ||
|
|
|
|
(pp->p_size != lp->d_secperunit)) {
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("An incorrect partition %c may cause problems for "
|
2001-02-25 16:47:44 +00:00
|
|
|
"standard system utilities",part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for overlaps */
|
|
|
|
/* this will check for all possible overlaps once and only once */
|
|
|
|
for (j = 0; j < i; j++) {
|
2002-12-02 23:49:59 +00:00
|
|
|
pp2 = &lp->d_partitions[j];
|
2004-08-08 23:11:43 +00:00
|
|
|
if (j != RAW_PART && i != RAW_PART &&
|
2002-12-02 23:49:59 +00:00
|
|
|
pp->p_fstype != FS_VINUM &&
|
|
|
|
pp2->p_fstype != FS_VINUM &&
|
2001-02-25 16:47:44 +00:00
|
|
|
part_set[i] && part_set[j]) {
|
|
|
|
if (pp2->p_offset < pp->p_offset + pp->p_size &&
|
|
|
|
(pp2->p_offset + pp2->p_size > pp->p_offset ||
|
|
|
|
pp2->p_offset >= pp->p_offset)) {
|
|
|
|
fprintf(stderr,"partitions %c and %c overlap!\n",
|
|
|
|
j + 'a', i + 'a');
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2007-12-09 22:58:49 +00:00
|
|
|
for (; i < lp->d_npartitions; i++) {
|
1994-05-26 06:35:07 +00:00
|
|
|
part = 'a' + i;
|
|
|
|
pp = &lp->d_partitions[i];
|
|
|
|
if (pp->p_size || pp->p_offset)
|
2003-05-03 09:58:20 +00:00
|
|
|
warnx("unused partition %c: size %d offset %lu",
|
1998-06-28 18:59:04 +00:00
|
|
|
'a' + i, pp->p_size, (u_long)pp->p_offset);
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
|
|
|
return (errors);
|
|
|
|
}
|
|
|
|
|
1996-01-22 01:01:15 +00:00
|
|
|
/*
|
|
|
|
* When operating on a "virgin" disk, try getting an initial label
|
|
|
|
* from the associated device driver. This might work for all device
|
|
|
|
* drivers that are able to fetch some initial device parameters
|
|
|
|
* without even having access to a (BSD) disklabel, like SCSI disks,
|
|
|
|
* most IDE drives, or vn devices.
|
|
|
|
*
|
|
|
|
* The device name must be given in its "canonical" form.
|
|
|
|
*/
|
2003-05-03 09:02:27 +00:00
|
|
|
static struct disklabel *
|
1996-01-22 01:01:15 +00:00
|
|
|
getvirginlabel(void)
|
|
|
|
{
|
2002-07-03 16:43:11 +00:00
|
|
|
static struct disklabel loclab;
|
2002-09-20 09:18:31 +00:00
|
|
|
struct partition *dp;
|
1996-01-22 01:01:15 +00:00
|
|
|
int f;
|
2003-05-03 09:58:20 +00:00
|
|
|
u_int u;
|
1996-01-22 01:01:15 +00:00
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
if ((f = open(specname, O_RDONLY)) == -1) {
|
|
|
|
warn("cannot open %s", specname);
|
1996-06-17 14:43:54 +00:00
|
|
|
return (NULL);
|
1996-01-22 01:01:15 +00:00
|
|
|
}
|
2000-10-31 07:07:39 +00:00
|
|
|
|
2004-03-30 23:15:03 +00:00
|
|
|
if (is_file)
|
|
|
|
get_file_parms(f);
|
2008-09-18 14:04:02 +00:00
|
|
|
else {
|
|
|
|
mediasize = g_mediasize(f);
|
|
|
|
secsize = g_sectorsize(f);
|
|
|
|
if (secsize < 0 || mediasize < 0) {
|
|
|
|
close (f);
|
|
|
|
return (NULL);
|
|
|
|
}
|
1996-01-22 01:01:15 +00:00
|
|
|
}
|
2002-09-20 09:18:31 +00:00
|
|
|
memset(&loclab, 0, sizeof loclab);
|
|
|
|
loclab.d_magic = DISKMAGIC;
|
|
|
|
loclab.d_magic2 = DISKMAGIC;
|
|
|
|
loclab.d_secsize = secsize;
|
|
|
|
loclab.d_secperunit = mediasize / secsize;
|
|
|
|
|
|
|
|
/*
|
2012-01-07 16:09:33 +00:00
|
|
|
* Nobody in these enlightened days uses the CHS geometry for
|
|
|
|
* anything, but nonetheless try to get it right. If we fail
|
2002-09-20 09:18:31 +00:00
|
|
|
* to get any good ideas from the device, construct something
|
|
|
|
* which is IBM-PC friendly.
|
|
|
|
*/
|
|
|
|
if (ioctl(f, DIOCGFWSECTORS, &u) == 0)
|
|
|
|
loclab.d_nsectors = u;
|
|
|
|
else
|
|
|
|
loclab.d_nsectors = 63;
|
|
|
|
if (ioctl(f, DIOCGFWHEADS, &u) == 0)
|
|
|
|
loclab.d_ntracks = u;
|
|
|
|
else if (loclab.d_secperunit <= 63*1*1024)
|
|
|
|
loclab.d_ntracks = 1;
|
|
|
|
else if (loclab.d_secperunit <= 63*16*1024)
|
|
|
|
loclab.d_ntracks = 16;
|
|
|
|
else
|
|
|
|
loclab.d_ntracks = 255;
|
|
|
|
loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors;
|
|
|
|
loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl;
|
2007-12-09 22:58:49 +00:00
|
|
|
loclab.d_npartitions = DEFPARTITIONS;
|
2002-09-20 09:18:31 +00:00
|
|
|
|
|
|
|
/* Various (unneeded) compat stuff */
|
|
|
|
loclab.d_rpm = 3600;
|
|
|
|
loclab.d_bbsize = BBSIZE;
|
2003-05-03 09:58:20 +00:00
|
|
|
loclab.d_interleave = 1;
|
2002-09-20 09:18:31 +00:00
|
|
|
strncpy(loclab.d_typename, "amnesiac",
|
|
|
|
sizeof(loclab.d_typename));
|
|
|
|
|
|
|
|
dp = &loclab.d_partitions[RAW_PART];
|
|
|
|
dp->p_size = loclab.d_secperunit;
|
|
|
|
loclab.d_checksum = dkcksum(&loclab);
|
|
|
|
close (f);
|
2002-07-03 16:43:11 +00:00
|
|
|
return (&loclab);
|
1996-01-22 01:01:15 +00:00
|
|
|
}
|
|
|
|
|
2003-05-03 09:02:27 +00:00
|
|
|
static void
|
2002-03-18 06:17:50 +00:00
|
|
|
usage(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2003-03-16 00:11:49 +00:00
|
|
|
|
|
|
|
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",
|
2003-05-03 08:04:24 +00:00
|
|
|
"usage: bsdlabel disk",
|
2003-03-16 00:11:49 +00:00
|
|
|
"\t\t(to read label)",
|
2003-05-03 09:02:27 +00:00
|
|
|
" bsdlabel -w [-n] [-m machine] disk [type]",
|
2003-03-16 00:11:49 +00:00
|
|
|
"\t\t(to write label with existing boot program)",
|
2003-05-03 08:04:24 +00:00
|
|
|
" bsdlabel -e [-n] [-m machine] disk",
|
2003-03-16 00:11:49 +00:00
|
|
|
"\t\t(to edit label)",
|
2003-05-03 08:04:24 +00:00
|
|
|
" bsdlabel -R [-n] [-m machine] disk protofile",
|
2003-03-16 00:11:49 +00:00
|
|
|
"\t\t(to restore label with existing boot program)",
|
2003-04-18 19:44:12 +00:00
|
|
|
" bsdlabel -B [-b boot] [-m machine] disk",
|
2003-03-16 00:11:49 +00:00
|
|
|
"\t\t(to install boot program with existing on-disk label)",
|
2003-05-03 09:02:27 +00:00
|
|
|
" bsdlabel -w -B [-n] [-b boot] [-m machine] disk [type]",
|
2003-03-16 00:11:49 +00:00
|
|
|
"\t\t(to write label and install boot program)",
|
2003-04-18 19:44:12 +00:00
|
|
|
" bsdlabel -R -B [-n] [-b boot] [-m machine] disk protofile",
|
2003-01-26 11:41:37 +00:00
|
|
|
"\t\t(to restore label and install boot program)"
|
|
|
|
);
|
1994-05-26 06:35:07 +00:00
|
|
|
exit(1);
|
|
|
|
}
|