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>
41 #include <geom/concat/g_concat.h>
43 #include "core/geom.h"
44 #include "misc/subr.h"
47 uint32_t lib_version = G_LIB_VERSION;
48 uint32_t version = G_CONCAT_VERSION;
50 static void concat_main(struct gctl_req *req, unsigned flags);
51 static void concat_clear(struct gctl_req *req);
52 static void concat_dump(struct gctl_req *req);
53 static void concat_label(struct gctl_req *req);
55 struct g_command class_commands[] = {
56 { "clear", G_FLAG_VERBOSE, concat_main, G_NULL_OPTS,
59 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
62 { "destroy", G_FLAG_VERBOSE, NULL,
64 { 'f', "force", NULL, G_TYPE_BOOL },
69 { "dump", 0, concat_main, G_NULL_OPTS,
72 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, concat_main,
74 { 'h', "hardcode", NULL, G_TYPE_BOOL },
79 { "stop", G_FLAG_VERBOSE, NULL,
81 { 'f', "force", NULL, G_TYPE_BOOL },
89 static int verbose = 0;
92 concat_main(struct gctl_req *req, unsigned flags)
96 if ((flags & G_FLAG_VERBOSE) != 0)
99 name = gctl_get_ascii(req, "verb");
101 gctl_error(req, "No '%s' argument.", "verb");
104 if (strcmp(name, "label") == 0)
106 else if (strcmp(name, "clear") == 0)
108 else if (strcmp(name, "dump") == 0)
111 gctl_error(req, "Unknown command: %s.", name);
115 concat_label(struct gctl_req *req)
117 struct g_concat_metadata md;
120 int error, i, hardcode, nargs;
122 bzero(sector, sizeof(sector));
123 nargs = gctl_get_int(req, "nargs");
125 gctl_error(req, "Too few arguments.");
128 hardcode = gctl_get_int(req, "hardcode");
131 * Clear last sector first to spoil all components if device exists.
133 for (i = 1; i < nargs; i++) {
134 name = gctl_get_ascii(req, "arg%d", i);
135 error = g_metadata_clear(name, NULL);
137 gctl_error(req, "Can't store metadata on %s: %s.", name,
143 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
144 md.md_version = G_CONCAT_VERSION;
145 name = gctl_get_ascii(req, "arg0");
146 strlcpy(md.md_name, name, sizeof(md.md_name));
147 md.md_id = arc4random();
148 md.md_all = nargs - 1;
151 * Ok, store metadata.
153 for (i = 1; i < nargs; i++) {
154 name = gctl_get_ascii(req, "arg%d", i);
157 bzero(md.md_provider, sizeof(md.md_provider));
159 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
160 name += sizeof(_PATH_DEV) - 1;
161 strlcpy(md.md_provider, name, sizeof(md.md_provider));
163 md.md_provsize = g_get_mediasize(name);
164 if (md.md_provsize == 0) {
165 fprintf(stderr, "Can't get mediasize of %s: %s.\n",
166 name, strerror(errno));
167 gctl_error(req, "Not fully done.");
170 concat_metadata_encode(&md, sector);
171 error = g_metadata_store(name, sector, sizeof(sector));
173 fprintf(stderr, "Can't store metadata on %s: %s.\n",
174 name, strerror(error));
175 gctl_error(req, "Not fully done.");
179 printf("Metadata value stored on %s.\n", name);
184 concat_clear(struct gctl_req *req)
189 nargs = gctl_get_int(req, "nargs");
191 gctl_error(req, "Too few arguments.");
195 for (i = 0; i < nargs; i++) {
196 name = gctl_get_ascii(req, "arg%d", i);
197 error = g_metadata_clear(name, G_CONCAT_MAGIC);
199 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
200 name, strerror(error));
201 gctl_error(req, "Not fully done.");
205 printf("Metadata cleared on %s.\n", name);
210 concat_metadata_dump(const struct g_concat_metadata *md)
213 printf(" Magic string: %s\n", md->md_magic);
214 printf(" Metadata version: %u\n", (u_int)md->md_version);
215 printf(" Device name: %s\n", md->md_name);
216 printf(" Device ID: %u\n", (u_int)md->md_id);
217 printf(" Disk number: %u\n", (u_int)md->md_no);
218 printf("Total number of disks: %u\n", (u_int)md->md_all);
219 printf(" Hardcoded provider: %s\n", md->md_provider);
223 concat_dump(struct gctl_req *req)
225 struct g_concat_metadata md, tmpmd;
229 nargs = gctl_get_int(req, "nargs");
231 gctl_error(req, "Too few arguments.");
235 for (i = 0; i < nargs; i++) {
236 name = gctl_get_ascii(req, "arg%d", i);
237 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
240 fprintf(stderr, "Can't read metadata from %s: %s.\n",
241 name, strerror(error));
242 gctl_error(req, "Not fully done.");
245 concat_metadata_decode((u_char *)&tmpmd, &md);
246 printf("Metadata on %s:\n", name);
247 concat_metadata_dump(&md);