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 { "append", G_FLAG_VERBOSE, NULL,
58 { 'h', "hardcode", NULL, G_TYPE_BOOL },
63 { "clear", G_FLAG_VERBOSE, concat_main, G_NULL_OPTS,
66 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
69 { "destroy", G_FLAG_VERBOSE, NULL,
71 { 'f', "force", NULL, G_TYPE_BOOL },
76 { "dump", 0, concat_main, G_NULL_OPTS,
79 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, concat_main,
81 { 'h', "hardcode", NULL, G_TYPE_BOOL },
86 { "stop", G_FLAG_VERBOSE, NULL,
88 { 'f', "force", NULL, G_TYPE_BOOL },
96 static int verbose = 0;
99 concat_main(struct gctl_req *req, unsigned flags)
103 if ((flags & G_FLAG_VERBOSE) != 0)
106 name = gctl_get_ascii(req, "verb");
108 gctl_error(req, "No '%s' argument.", "verb");
111 if (strcmp(name, "label") == 0)
113 else if (strcmp(name, "clear") == 0)
115 else if (strcmp(name, "dump") == 0)
118 gctl_error(req, "Unknown command: %s.", name);
122 concat_label(struct gctl_req *req)
124 struct g_concat_metadata md;
127 int error, i, hardcode, nargs;
129 bzero(sector, sizeof(sector));
130 nargs = gctl_get_int(req, "nargs");
132 gctl_error(req, "Too few arguments.");
135 hardcode = gctl_get_int(req, "hardcode");
138 * Clear last sector first to spoil all components if device exists.
140 for (i = 1; i < nargs; i++) {
141 name = gctl_get_ascii(req, "arg%d", i);
142 error = g_metadata_clear(name, NULL);
144 gctl_error(req, "Can't store metadata on %s: %s.", name,
150 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
151 md.md_version = G_CONCAT_VERSION;
152 name = gctl_get_ascii(req, "arg0");
153 strlcpy(md.md_name, name, sizeof(md.md_name));
154 md.md_id = arc4random();
155 md.md_all = nargs - 1;
158 * Ok, store metadata.
160 for (i = 1; i < nargs; i++) {
161 name = gctl_get_ascii(req, "arg%d", i);
164 bzero(md.md_provider, sizeof(md.md_provider));
166 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
167 name += sizeof(_PATH_DEV) - 1;
168 strlcpy(md.md_provider, name, sizeof(md.md_provider));
170 md.md_provsize = g_get_mediasize(name);
171 if (md.md_provsize == 0) {
172 fprintf(stderr, "Can't get mediasize of %s: %s.\n",
173 name, strerror(errno));
174 gctl_error(req, "Not fully done.");
177 concat_metadata_encode(&md, sector);
178 error = g_metadata_store(name, sector, sizeof(sector));
180 fprintf(stderr, "Can't store metadata on %s: %s.\n",
181 name, strerror(error));
182 gctl_error(req, "Not fully done.");
186 printf("Metadata value stored on %s.\n", name);
191 concat_clear(struct gctl_req *req)
196 nargs = gctl_get_int(req, "nargs");
198 gctl_error(req, "Too few arguments.");
202 for (i = 0; i < nargs; i++) {
203 name = gctl_get_ascii(req, "arg%d", i);
204 error = g_metadata_clear(name, G_CONCAT_MAGIC);
206 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
207 name, strerror(error));
208 gctl_error(req, "Not fully done.");
212 printf("Metadata cleared on %s.\n", name);
217 concat_metadata_dump(const struct g_concat_metadata *md)
220 printf(" Magic string: %s\n", md->md_magic);
221 printf(" Metadata version: %u\n", (u_int)md->md_version);
222 printf(" Device name: %s\n", md->md_name);
223 printf(" Device ID: %u\n", (u_int)md->md_id);
224 printf(" Disk number: %u\n", (u_int)md->md_no);
225 printf("Total number of disks: %u\n", (u_int)md->md_all);
226 printf(" Hardcoded provider: %s\n", md->md_provider);
230 concat_dump(struct gctl_req *req)
232 struct g_concat_metadata md, tmpmd;
236 nargs = gctl_get_int(req, "nargs");
238 gctl_error(req, "Too few arguments.");
242 for (i = 0; i < nargs; i++) {
243 name = gctl_get_ascii(req, "arg%d", i);
244 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
247 fprintf(stderr, "Can't read metadata from %s: %s.\n",
248 name, strerror(error));
249 gctl_error(req, "Not fully done.");
252 concat_metadata_decode((u_char *)&tmpmd, &md);
253 printf("Metadata on %s:\n", name);
254 concat_metadata_dump(&md);