2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
40 #include <geom/label/g_label.h>
42 #include "core/geom.h"
43 #include "misc/subr.h"
45 #ifdef STATIC_GEOM_CLASSES
46 #define PUBSYM(x) glabel_##x
51 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
52 uint32_t PUBSYM(version) = G_LABEL_VERSION;
54 static void label_main(struct gctl_req *req, unsigned flags);
55 static void label_clear(struct gctl_req *req);
56 static void label_dump(struct gctl_req *req);
57 static void label_label(struct gctl_req *req);
58 static void label_refresh(struct gctl_req *req);
60 struct g_command PUBSYM(class_commands)[] = {
61 { "clear", G_FLAG_VERBOSE, label_main, G_NULL_OPTS,
64 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
67 { "destroy", G_FLAG_VERBOSE, NULL,
69 { 'f', "force", NULL, G_TYPE_BOOL },
74 { "dump", 0, label_main, G_NULL_OPTS,
77 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, label_main, G_NULL_OPTS,
80 { "refresh", 0, label_main, G_NULL_OPTS,
83 { "stop", G_FLAG_VERBOSE, NULL,
85 { 'f', "force", NULL, G_TYPE_BOOL },
93 static int verbose = 0;
96 label_main(struct gctl_req *req, unsigned flags)
100 if ((flags & G_FLAG_VERBOSE) != 0)
103 name = gctl_get_ascii(req, "verb");
105 gctl_error(req, "No '%s' argument.", "verb");
108 if (strcmp(name, "label") == 0)
110 else if (strcmp(name, "clear") == 0)
112 else if (strcmp(name, "dump") == 0)
114 else if (strcmp(name, "refresh") == 0)
117 gctl_error(req, "Unknown command: %s.", name);
121 label_label(struct gctl_req *req)
123 struct g_label_metadata md;
124 const char *name, *label;
128 bzero(sector, sizeof(sector));
129 nargs = gctl_get_int(req, "nargs");
131 gctl_error(req, "Invalid number of arguments.");
136 * Clear last sector first to spoil all components if device exists.
138 name = gctl_get_ascii(req, "arg1");
139 error = g_metadata_clear(name, NULL);
141 gctl_error(req, "Can't store metadata on %s: %s.", name,
146 strlcpy(md.md_magic, G_LABEL_MAGIC, sizeof(md.md_magic));
147 md.md_version = G_LABEL_VERSION;
148 label = gctl_get_ascii(req, "arg0");
149 bzero(md.md_label, sizeof(md.md_label));
150 strlcpy(md.md_label, label, sizeof(md.md_label));
151 md.md_provsize = g_get_mediasize(name);
152 if (md.md_provsize == 0) {
153 gctl_error(req, "Can't get mediasize of %s: %s.", name,
159 * Ok, store metadata.
161 label_metadata_encode(&md, sector);
162 error = g_metadata_store(name, sector, sizeof(sector));
164 fprintf(stderr, "Can't store metadata on %s: %s.\n", name,
166 gctl_error(req, "Not done.");
169 printf("Metadata value stored on %s.\n", name);
173 label_clear(struct gctl_req *req)
178 nargs = gctl_get_int(req, "nargs");
180 gctl_error(req, "Too few arguments.");
184 for (i = 0; i < nargs; i++) {
185 name = gctl_get_ascii(req, "arg%d", i);
186 error = g_metadata_clear(name, G_LABEL_MAGIC);
188 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
189 name, strerror(error));
190 gctl_error(req, "Not fully done.");
194 printf("Metadata cleared on %s.\n", name);
199 label_metadata_dump(const struct g_label_metadata *md)
202 printf(" Magic string: %s\n", md->md_magic);
203 printf("Metadata version: %u\n", (u_int)md->md_version);
204 printf(" Label: %s\n", md->md_label);
208 label_dump(struct gctl_req *req)
210 struct g_label_metadata md, tmpmd;
214 nargs = gctl_get_int(req, "nargs");
216 gctl_error(req, "Too few arguments.");
220 for (i = 0; i < nargs; i++) {
221 name = gctl_get_ascii(req, "arg%d", i);
222 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
225 fprintf(stderr, "Can't read metadata from %s: %s.\n",
226 name, strerror(error));
227 gctl_error(req, "Not fully done.");
230 label_metadata_decode((u_char *)&tmpmd, &md);
231 printf("Metadata on %s:\n", name);
232 label_metadata_dump(&md);
238 label_refresh(struct gctl_req *req)
243 nargs = gctl_get_int(req, "nargs");
245 gctl_error(req, "Too few arguments.");
249 for (i = 0; i < nargs; i++) {
250 name = gctl_get_ascii(req, "arg%d", i);
251 fd = g_open(name, 1);
253 printf("Can't refresh metadata from %s: %s.\n",
254 name, strerror(errno));
256 printf("Metadata from %s refreshed.\n", name);