diff options
| author | jnemeth <jnemeth@NetBSD.org> | 2019-06-25 04:53:40 +0000 |
|---|---|---|
| committer | jnemeth <jnemeth@NetBSD.org> | 2019-06-25 04:53:40 +0000 |
| commit | df51d8817e41f9c76396e33e6bf4d370d1389c90 (patch) | |
| tree | 4d15fb1f2311f048bc57afb783904f66d4463947 /sbin | |
| parent | aebd558d0edb9f86d4c8f65dde3df260e3053128 (diff) | |
Add an "uuid" command to generate a new UUID for a portion. The
primary intention is for use for after cloning disks to prevent
collisions.
Diffstat (limited to 'sbin')
| -rw-r--r-- | sbin/gpt/Makefile | 4 | ||||
| -rw-r--r-- | sbin/gpt/gpt.8 | 23 | ||||
| -rw-r--r-- | sbin/gpt/main.c | 8 | ||||
| -rw-r--r-- | sbin/gpt/uuid.c | 114 |
4 files changed, 142 insertions, 7 deletions
diff --git a/sbin/gpt/Makefile b/sbin/gpt/Makefile index a975a803a33..53b453ef42b 100644 --- a/sbin/gpt/Makefile +++ b/sbin/gpt/Makefile @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.18 2015/12/03 02:02:43 christos Exp $ +# $NetBSD: Makefile,v 1.19 2019/06/25 04:53:40 jnemeth Exp $ # $FreeBSD: src/sbin/gpt/Makefile,v 1.7 2005/09/01 02:49:20 marcel Exp $ .include <bsd.own.mk> @@ -7,7 +7,7 @@ WARNS=6 PROG= gpt SRCS= add.c biosboot.c create.c destroy.c gpt.c header.c label.c map.c \ main.c migrate.c recover.c remove.c resize.c resizedisk.c \ - set.c show.c type.c unset.c gpt_uuid.c + set.c show.c type.c unset.c gpt_uuid.c uuid.c MAN= gpt.8 #LINKS= ${BINDIR}/gpt ${BINDIR}/gptlabel diff --git a/sbin/gpt/gpt.8 b/sbin/gpt/gpt.8 index b3a264e161c..cad2811bbd0 100644 --- a/sbin/gpt/gpt.8 +++ b/sbin/gpt/gpt.8 @@ -1,4 +1,4 @@ -.\" $NetBSD: gpt.8,v 1.68 2019/04/04 13:55:40 martin Exp $ +.\" $NetBSD: gpt.8,v 1.69 2019/06/25 04:53:40 jnemeth Exp $ .\" .\" Copyright (c) 2002 Marcel Moolenaar .\" All rights reserved. @@ -26,7 +26,7 @@ .\" .\" $FreeBSD: src/sbin/gpt/gpt.8,v 1.17 2006/06/22 22:22:32 marcel Exp $ .\" -.Dd April 4, 2019 +.Dd June 22, 2019 .Dt GPT 8 .Os .Sh NAME @@ -693,6 +693,25 @@ code. They may be used by .Nx code in the future. +.\" ==== uuid ==== +.It Nm Ic uuid Oo Fl a Oc +.It Nm Ic uuid Oo Fl b Ar blocknr Oc Oo Fl i Ar index Oc \ +Oo Fl L Ar label Oc Oo Fl s Ar sectors Oc Oo Fl t Ar type Oc +The +.Ic uuid +command allows the user to change the UUID of any and all partitions +that match the selection. +It uses the same selection options as the +.Ic label +command. +See above for a description of these options. +If +.Fl a +is used, then the header UUID is changed as well. +.Pp +The primary purpose of this command is for use after cloning a disk to +prevent collisions when both disks are used in the same system. +.\" ==== end of commands ==== .El .Sh EXIT STATUS The diff --git a/sbin/gpt/main.c b/sbin/gpt/main.c index 196d9391749..40a4c5b8ae5 100644 --- a/sbin/gpt/main.c +++ b/sbin/gpt/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.12 2018/08/27 15:50:39 sevan Exp $ */ +/* $NetBSD: main.c,v 1.13 2019/06/25 04:53:40 jnemeth Exp $ */ /*- * Copyright (c) 2002 Marcel Moolenaar @@ -34,7 +34,7 @@ #include <sys/cdefs.h> #ifdef __RCSID -__RCSID("$NetBSD: main.c,v 1.12 2018/08/27 15:50:39 sevan Exp $"); +__RCSID("$NetBSD: main.c,v 1.13 2019/06/25 04:53:40 jnemeth Exp $"); #endif #include <stdio.h> @@ -73,7 +73,8 @@ extern const struct gpt_cmd c_set, c_show, c_type, - c_unset; + c_unset, + c_uuid; static const struct gpt_cmd *cmdsw[] = { &c_add, @@ -97,6 +98,7 @@ static const struct gpt_cmd *cmdsw[] = { &c_show, &c_type, &c_unset, + &c_uuid, &c_null, }; diff --git a/sbin/gpt/uuid.c b/sbin/gpt/uuid.c new file mode 100644 index 00000000000..428639835e5 --- /dev/null +++ b/sbin/gpt/uuid.c @@ -0,0 +1,114 @@ +/*- + * Copyright (c) 2004 Marcel Moolenaar + * All rights reserved. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include <sys/cdefs.h> +#ifdef __FBSDID +__FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $"); +#endif +#ifdef __RCSID +__RCSID("$NetBSD: uuid.c,v 1.1 2019/06/25 04:53:40 jnemeth Exp $"); +#endif + +#include <sys/types.h> + +#include <err.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "map.h" +#include "gpt.h" +#include "gpt_private.h" + +static int cmd_uuid(gpt_t, int, char *[]); + +static const char *uuidhelp[] = { + "-a", + "[-b blocknr] [-i index] [-L label] [-s sectors] [-t type]", +}; + +struct gpt_cmd c_uuid = { + "uuid", + cmd_uuid, + uuidhelp, __arraycount(uuidhelp), + GPT_SYNC, +}; + +#define usage() gpt_usage(NULL, &c_uuid) + +static void +change_ent(struct gpt_ent *ent, void *v, int backup) +{ + static gpt_uuid_t uuidstore; + + if (!backup) + gpt_uuid_generate(NULL, uuidstore); + memmove(ent->ent_guid, uuidstore, sizeof(ent->ent_guid)); +} + +static void +change_hdr(struct gpt_hdr *hdr, void *v, int backup) +{ + static gpt_uuid_t uuidstore; + + if (!backup) + gpt_uuid_generate(NULL, uuidstore); + memmove(hdr->hdr_guid, uuidstore, sizeof(hdr->hdr_guid)); +} + +static int +cmd_uuid(gpt_t gpt, int argc, char *argv[]) +{ + int ch, rc; + struct gpt_find find; + + memset(&find, 0, sizeof(find)); + find.msg = "UUID changed"; + + /* Get the uuid options */ + while ((ch = getopt(argc, argv, GPT_FIND)) != -1) { + if (gpt == NULL || gpt_add_find(gpt, &find, ch) == -1) + return usage(); + } + + if (gpt == NULL || argc != optind) + return usage(); + + rc = gpt_change_ent(gpt, &find, change_ent, NULL); + if (rc != 0) + return rc; + + if (find.all) + return gpt_change_hdr(gpt, &find, change_hdr, NULL); + + return 0; +} |
