2 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
38 #include <geom/label/g_label.h>
40 #include "core/geom.h"
41 #include "misc/subr.h"
43 #ifdef STATIC_GEOM_CLASSES
44 #define PUBSYM(x) glabel_##x
49 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
50 uint32_t PUBSYM(version) = G_LABEL_VERSION;
52 static void label_main(struct gctl_req *req, unsigned flags);
53 static void label_clear(struct gctl_req *req);
54 static void label_dump(struct gctl_req *req);
55 static void label_label(struct gctl_req *req);
57 struct g_command PUBSYM(class_commands)[] = {
58 { "clear", G_FLAG_VERBOSE, label_main, G_NULL_OPTS,
61 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
64 { "destroy", G_FLAG_VERBOSE, NULL,
66 { 'f', "force", NULL, G_TYPE_BOOL },
71 { "dump", 0, label_main, G_NULL_OPTS,
74 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, label_main, G_NULL_OPTS,
77 { "stop", G_FLAG_VERBOSE, NULL,
79 { 'f', "force", NULL, G_TYPE_BOOL },
87 static int verbose = 0;
90 label_main(struct gctl_req *req, unsigned flags)
94 if ((flags & G_FLAG_VERBOSE) != 0)
97 name = gctl_get_ascii(req, "verb");
99 gctl_error(req, "No '%s' argument.", "verb");
102 if (strcmp(name, "label") == 0)
104 else if (strcmp(name, "clear") == 0)
106 else if (strcmp(name, "dump") == 0)
109 gctl_error(req, "Unknown command: %s.", name);
113 label_label(struct gctl_req *req)
115 struct g_label_metadata md;
116 const char *name, *label;
120 nargs = gctl_get_int(req, "nargs");
122 gctl_error(req, "Invalid number of arguments.");
127 * Clear last sector first to spoil all components if device exists.
129 name = gctl_get_ascii(req, "arg1");
130 error = g_metadata_clear(name, NULL);
132 gctl_error(req, "Can't store metadata on %s: %s.", name,
137 strlcpy(md.md_magic, G_LABEL_MAGIC, sizeof(md.md_magic));
138 md.md_version = G_LABEL_VERSION;
139 label = gctl_get_ascii(req, "arg0");
140 strlcpy(md.md_label, label, sizeof(md.md_label));
141 md.md_provsize = g_get_mediasize(name);
142 if (md.md_provsize == 0) {
143 gctl_error(req, "Can't get mediasize of %s: %s.", name,
149 * Ok, store metadata.
151 label_metadata_encode(&md, sector);
152 error = g_metadata_store(name, sector, sizeof(sector));
154 fprintf(stderr, "Can't store metadata on %s: %s.\n", name,
156 gctl_error(req, "Not done.");
159 printf("Metadata value stored on %s.\n", name);
163 label_clear(struct gctl_req *req)
168 nargs = gctl_get_int(req, "nargs");
170 gctl_error(req, "Too few arguments.");
174 for (i = 0; i < nargs; i++) {
175 name = gctl_get_ascii(req, "arg%d", i);
176 error = g_metadata_clear(name, G_LABEL_MAGIC);
178 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
179 name, strerror(error));
180 gctl_error(req, "Not fully done.");
184 printf("Metadata cleared on %s.\n", name);
189 label_metadata_dump(const struct g_label_metadata *md)
192 printf(" Magic string: %s\n", md->md_magic);
193 printf("Metadata version: %u\n", (u_int)md->md_version);
194 printf(" Label: %s\n", md->md_label);
198 label_dump(struct gctl_req *req)
200 struct g_label_metadata md, tmpmd;
204 nargs = gctl_get_int(req, "nargs");
206 gctl_error(req, "Too few arguments.");
210 for (i = 0; i < nargs; i++) {
211 name = gctl_get_ascii(req, "arg%d", i);
212 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
215 fprintf(stderr, "Can't read metadata from %s: %s.\n",
216 name, strerror(error));
217 gctl_error(req, "Not fully done.");
220 label_metadata_decode((u_char *)&tmpmd, &md);
221 printf("Metadata on %s:\n", name);
222 label_metadata_dump(&md);