Add support for setting GPT partition labels. The partitions to be

labeled are selected in the same way as with the remove command.
Update the manpage to have the selection options described for the
label command and referenced to it from the remove command.
The label can be specified on the command line with the -l option
or read from a file with the -f option. In both cases, the label
is assumed to be encoded in UTF-8.

PR: ia64/83124
MFC after: 1 week
This commit is contained in:
Marcel Moolenaar 2005-09-01 02:49:20 +00:00
parent 376e47e284
commit 5201042924
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=149688
5 changed files with 343 additions and 33 deletions

View File

@ -1,8 +1,8 @@
# $FreeBSD$
PROG= gpt
SRCS= add.c create.c destroy.c gpt.c map.c migrate.c recover.c remove.c \
show.c
SRCS= add.c create.c destroy.c gpt.c label.c map.c migrate.c recover.c \
remove.c show.c
WARNS?= 4
MAN= gpt.8

View File

@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd November 12, 2004
.Dd August 31, 2005
.Os
.Dt GPT 8
.Sh NAME
@ -165,6 +165,71 @@ The
option instructs
.Nm
to destroy the table in a way that it can be recovered.
.\" ==== label ====
.It Xo
.Nm
.Ic label
.Op Fl a
.Aq Fl f Ar file | Fl l Ar label
.Ar device ...
.Xc
.It Xo
.Nm
.Ic label
.Op Fl b Ar number
.Op Fl i Ar index
.Op Fl s Ar count
.Op Fl t Ar type
.Aq Fl f Ar file | Fl l Ar label
.Ar device ...
.Xc
The
.Ic label
command allows the user to label any partitions that match the selection.
At least one of the following selection options must be specified.
.Pp
The
.Fl a
option specifies that all partitions should be labeled.
It is mutually exclusive with all other selection options.
.Pp
The
.Fl b Ar number
option selects the partition that starts at the given block number.
.Pp
The
.Fl i Ar index
option selects the partition with the given partition number.
.Pp
The
.Fl s Ar count
option selects all partitions that have the given size.
This can cause multiple partitions to be removed.
.Pp
The
.Fl t Ar type
option selects all partitions that have the given type.
The type is given as an UUID or by the aliases that the
.Ic add
command accepts.
This can cause multiple partitions to be removed.
.Pp
The
.Fl f Ar file
or
.Fl l Ar label
options specify the new label to be assigned to the selected partitions.
The
.Fl f Ar file
option is used to read the label from the specified file.
Only the first line is read from the file and the trailing newline
character is stripped.
If the file name is the dash or minus sign (-), the label is read from
the standard input.
The
.Fl l Ar label
option is used to specify the label in the command line.
The label is assumed to be encoded in UTF-8.
.\" ==== migrate ====
.It Nm Ic migrate Oo Fl fs Oc Ar device ...
The
@ -188,7 +253,7 @@ option prevents migrating
disk labels into GPT partitions by creating
the GPT equivalent of a slice.
.\" ==== remove ====
.It Nm Ic remove Oo Fl a Oc
.It Nm Ic remove Oo Fl a Oc Ar device ...
.It Xo
.Nm
.Ic remove
@ -200,34 +265,14 @@ the GPT equivalent of a slice.
.Xc
The
.Ic remove
command allows the user to remove any partitions that match the selection.
At least one option must be specified.
.Pp
The
.Fl a
option specifies that all partitions should be removed.
It is mutually exclusive with all other options.
.Pp
The
.Fl b Ar number
option selects the partitions that starts at the given number.
.Pp
The
.Fl i Ar index
option selects the partition with the given partition number.
.Pp
The
.Fl s Ar count
option selects all partitions that have the given size.
This can cause multiple partitions to be removed.
.Pp
The
.Fl t Ar type
option selects all partitions that have the given type.
The type is given as an UUID or by the aliases that the
.Ic add
command accepts.
This can cause multiple partitions to be removed.
command allows the user to remove 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.
Partitions are removed by clearing the partition type.
No other information is changed.
.\" ==== show ====
.It Nm Ic show Oo Fl lu Oc Ar device ...
The

View File

@ -556,6 +556,7 @@ static struct {
{ cmd_create, "create" },
{ cmd_destroy, "destroy" },
{ NULL, "help" },
{ cmd_label, "label" },
{ cmd_migrate, "migrate" },
{ cmd_recover, "recover" },
{ cmd_remove, "remove" },
@ -570,7 +571,7 @@ usage(void)
{
fprintf(stderr,
"usage: %s [-rv] [-p nparts] command [options] device\n",
"usage: %s [-rv] [-p nparts] command [options] device ...\n",
getprogname());
exit(1);
}

View File

@ -77,6 +77,7 @@ void utf8_to_utf16(const uint8_t *, uint16_t *, size_t);
int cmd_add(int, char *[]);
int cmd_create(int, char *[]);
int cmd_destroy(int, char *[]);
int cmd_label(int, char *[]);
int cmd_migrate(int, char *[]);
int cmd_recover(int, char *[]);
int cmd_remove(int, char *[]);

263
sbin/gpt/label.c Normal file
View File

@ -0,0 +1,263 @@
/*-
* Copyright (c) 2005 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#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"
static int all;
static uuid_t type;
static off_t block, size;
static unsigned int entry;
static uint8_t *name;
static void
usage_label(void)
{
const char *common = "<-l label | -f file> device ...";
fprintf(stderr,
"usage: %s -a %s\n"
" %s [-b lba] [-i index] [-s lba] [-t uuid] %s\n",
getprogname(), common, getprogname(), common);
exit(1);
}
static void
label(int fd)
{
uuid_t uuid;
map_t *gpt, *tpg;
map_t *tbl, *lbt;
map_t *m;
struct gpt_hdr *hdr;
struct gpt_ent *ent;
unsigned int i;
gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
if (gpt == NULL) {
warnx("%s: error: no primary GPT header; run create or recover",
device_name);
return;
}
tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
if (tpg == NULL) {
warnx("%s: error: no secondary GPT header; run recover",
device_name);
return;
}
tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
if (tbl == NULL || lbt == NULL) {
warnx("%s: error: run recover -- trust me", device_name);
return;
}
/* Relabel all matching entries in the map. */
for (m = map_first(); m != NULL; m = m->map_next) {
if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
continue;
if (entry > 0 && entry != m->map_index)
continue;
if (block > 0 && block != m->map_start)
continue;
if (size > 0 && size != m->map_size)
continue;
i = m->map_index - 1;
hdr = gpt->map_data;
ent = (void*)((char*)tbl->map_data + i *
le32toh(hdr->hdr_entsz));
le_uuid_dec(&ent->ent_type, &uuid);
if (!uuid_is_nil(&type, NULL) &&
!uuid_equal(&type, &uuid, NULL))
continue;
/* Label the primary entry. */
utf8_to_utf16(name, ent->ent_name, 36);
hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
hdr->hdr_crc_self = 0;
hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
gpt_write(fd, gpt);
gpt_write(fd, tbl);
hdr = tpg->map_data;
ent = (void*)((char*)lbt->map_data + i *
le32toh(hdr->hdr_entsz));
/* Label the secundary entry. */
utf8_to_utf16(name, ent->ent_name, 36);
hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
hdr->hdr_crc_self = 0;
hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
gpt_write(fd, lbt);
gpt_write(fd, tpg);
printf("%sp%u labeled\n", device_name, m->map_index);
}
}
static void
name_from_file(const char *fn)
{
FILE *f;
char *p;
size_t maxlen = 1024;
size_t len;
if (strcmp(fn, "-") != 0) {
f = fopen(fn, "r");
if (f == NULL)
err(1, "unable to open file %s", fn);
} else
f = stdin;
name = malloc(maxlen);
len = fread(name, 1, maxlen - 1, f);
if (ferror(f))
err(1, "unable to read label from file %s", fn);
if (f != stdin)
fclose(f);
name[len] = '\0';
/* Only keep the first line, excluding the newline character. */
p = strchr(name, '\n');
if (p != NULL)
*p = '\0';
}
int
cmd_label(int argc, char *argv[])
{
char *p;
int ch, fd;
uint32_t status;
/* Get the label options */
while ((ch = getopt(argc, argv, "ab:f:i:l:s:t:")) != -1) {
switch(ch) {
case 'a':
if (all > 0)
usage_label();
all = 1;
break;
case 'b':
if (block > 0)
usage_label();
block = strtol(optarg, &p, 10);
if (*p != 0 || block < 1)
usage_label();
break;
case 'f':
if (name != NULL)
usage_label();
name_from_file(optarg);
break;
case 'i':
if (entry > 0)
usage_label();
entry = strtol(optarg, &p, 10);
if (*p != 0 || entry < 1)
usage_label();
break;
case 'l':
if (name != NULL)
usage_label();
name = strdup(optarg);
break;
case 's':
if (size > 0)
usage_label();
size = strtol(optarg, &p, 10);
if (*p != 0 || size < 1)
usage_label();
break;
case 't':
if (!uuid_is_nil(&type, NULL))
usage_label();
uuid_from_string(optarg, &type, &status);
if (status != uuid_s_ok) {
if (strcmp(optarg, "efi") == 0) {
uuid_t efi = GPT_ENT_TYPE_EFI;
type = efi;
} else if (strcmp(optarg, "swap") == 0) {
uuid_t sw = GPT_ENT_TYPE_FREEBSD_SWAP;
type = sw;
} else if (strcmp(optarg, "ufs") == 0) {
uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
type = ufs;
} else if (strcmp(optarg, "linux") == 0 ||
strcmp(optarg, "windows") == 0) {
uuid_t m = GPT_ENT_TYPE_MS_BASIC_DATA;
type = m;
} else
usage_label();
}
break;
default:
usage_label();
}
}
if (!all ^
(block > 0 || entry > 0 || size > 0 || !uuid_is_nil(&type, NULL)))
usage_label();
if (name == NULL || argc == optind)
usage_label();
while (optind < argc) {
fd = gpt_open(argv[optind++]);
if (fd == -1) {
warn("unable to open device '%s'", device_name);
continue;
}
label(fd);
gpt_close(fd);
}
return (0);
}