diff --git a/sbin/disklabel/disklabel.8 b/sbin/disklabel/disklabel.8 index 4bea8392a559..561542a8e498 100644 --- a/sbin/disklabel/disklabel.8 +++ b/sbin/disklabel/disklabel.8 @@ -1,4 +1,4 @@ -.\" $NetBSD: disklabel.8,v 1.29 2000/03/09 12:50:36 hubertf Exp $ +.\" $NetBSD: disklabel.8,v 1.30 2000/05/31 14:13:48 fvdl Exp $ .\" .\" Copyright (c) 1987, 1988, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -59,6 +59,11 @@ .Op Fl C .Ar disk .Nm "" +.Fl I +.Op Fl r +.Op Fl C +.Ar disk +.Nm "" .Fl i .Op Fl r .Ar disk @@ -319,6 +324,11 @@ 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 -e -I sd0 +.Pp +As previous, but don't fail if there was no label on the disk yet; +provide some default values instead. +.Pp .Dl disklabel -R sd0 mylabel .Pp Restore the on-disk and in-core label for sd0 from information in diff --git a/sbin/disklabel/disklabel.c b/sbin/disklabel/disklabel.c index ced9e443d9b0..259b66a14c67 100644 --- a/sbin/disklabel/disklabel.c +++ b/sbin/disklabel/disklabel.c @@ -1,4 +1,4 @@ -/* $NetBSD: disklabel.c,v 1.83 2000/05/27 19:01:13 jdolecek Exp $ */ +/* $NetBSD: disklabel.c,v 1.84 2000/05/31 14:13:48 fvdl Exp $ */ /* * Copyright (c) 1987, 1993 @@ -47,7 +47,7 @@ __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\ static char sccsid[] = "@(#)disklabel.c 8.4 (Berkeley) 5/4/95"; /* from static char sccsid[] = "@(#)disklabel.c 1.2 (Symmetric) 11/28/85"; */ #else -__RCSID("$NetBSD: disklabel.c,v 1.83 2000/05/27 19:01:13 jdolecek Exp $"); +__RCSID("$NetBSD: disklabel.c,v 1.84 2000/05/31 14:13:48 fvdl Exp $"); #endif #endif /* not lint */ @@ -129,12 +129,13 @@ static enum { static int rflag; static int tflag; static int Cflag; +static int Iflag; #ifdef DEBUG static int debug; -#define OPTIONS "BCNRWb:def:irs:tw" +#define OPTIONS "BCINRWb:def:irs:tw" #else -#define OPTIONS "BCNRWb:ef:irs:tw" +#define OPTIONS "BCINRWb:ef:irs:tw" #endif #ifdef USE_MBR @@ -195,6 +196,9 @@ main(argc, argv) case 'C': ++Cflag; break; + case 'I': + ++Iflag; + break; case 'N': if (op != UNSPEC) usage(); @@ -266,6 +270,9 @@ main(argc, argv) if (argc < 1) usage(); + if (Iflag && op != EDIT) + usage(); + dkname = argv[0]; f = opendisk(dkname, op == READ ? O_RDONLY : O_RDWR, np, MAXPATHLEN, 0); specname = np; @@ -470,7 +477,7 @@ writelabel(f, boot, lp) /* Let the kernel deal with SunOS disklabel compatibility */ if (0) #else - if (rflag) + if (rflag || Iflag) #endif { #ifdef USE_MBR @@ -586,7 +593,7 @@ l_perror(s) case ESRCH: warnx("%s: No disk label on disk;\n" - "use \"disklabel -r\" to install initial label", s); + "use \"disklabel -I\" to install initial label", s); break; case EINVAL: @@ -825,8 +832,8 @@ readlabel(f) { struct disklabel *lp; - if (rflag) { - char *msg; + if (rflag || Iflag) { + char *msg = NULL; off_t sectoffset = 0; #ifdef USE_MBR @@ -840,7 +847,8 @@ readlabel(f) if (lseek(f, sectoffset, SEEK_SET) < 0 || read(f, bootarea, BBSIZE) < BBSIZE) err(4, "%s", specname); - msg = "no disk label"; + if (!Iflag) + msg = "no disklabel"; for (lp = (struct disklabel *)bootarea; lp <= (struct disklabel *)(bootarea + BBSIZE - sizeof(*lp)); lp = (struct disklabel *)((char *)lp + sizeof(long))) { @@ -852,8 +860,15 @@ readlabel(f) msg = "disk label corrupted"; } } - /* lp = (struct disklabel *)(bootarea + LABELOFFSET); */ - errx(1, msg); + if (msg != NULL) + errx(1, msg); + /* + * There was no label on the disk. Get the fictious one + * as a basis for initialisation. + */ + lp = makebootarea(bootarea, &lab, f); + if (ioctl(f, DIOCGDINFO, lp) < 0) + errx(1, "no disklabel"); } else { lp = &lab; if (ioctl(f, DIOCGDINFO, lp) < 0) @@ -895,7 +910,7 @@ makebootarea(boot, dp, f) * We must read the current bootarea so we don't clobber the * existing boot block, if any. */ - if (rflag) { + if (rflag || Iflag) { off_t sectoffset = 0; if (lseek(f, sectoffset, SEEK_SET) < 0 || @@ -911,7 +926,7 @@ makebootarea(boot, dp, f) * clobber the existing boot. */ if (!installboot) { - if (rflag) { + if (rflag || Iflag) { off_t sectoffset = 0; #ifdef USE_MBR