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>
39 #include <geom/concat/g_concat.h>
41 #include "core/geom.h"
42 #include "misc/subr.h"
45 uint32_t lib_version = G_LIB_VERSION;
46 uint32_t version = G_CONCAT_VERSION;
48 static void concat_main(struct gctl_req *req, unsigned flags);
49 static void concat_clear(struct gctl_req *req);
50 static void concat_dump(struct gctl_req *req);
51 static void concat_label(struct gctl_req *req);
53 struct g_command class_commands[] = {
54 { "clear", G_FLAG_VERBOSE, concat_main, G_NULL_OPTS, NULL,
57 { "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
58 NULL, "[-v] name prov ..."
60 { "destroy", G_FLAG_VERBOSE, NULL,
62 { 'f', "force", NULL, G_TYPE_BOOL },
65 NULL, "[-fv] name ..."
67 { "dump", 0, concat_main, G_NULL_OPTS, NULL,
70 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, concat_main,
72 { 'h', "hardcode", NULL, G_TYPE_BOOL },
75 NULL, "[-hv] name prov ..."
77 { "stop", G_FLAG_VERBOSE, NULL,
79 { 'f', "force", NULL, G_TYPE_BOOL },
82 NULL, "[-fv] name ..."
87 static int verbose = 0;
90 concat_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 concat_label(struct gctl_req *req)
115 struct g_concat_metadata md;
118 int error, i, hardcode, nargs;
120 nargs = gctl_get_int(req, "nargs");
122 gctl_error(req, "Too few arguments.");
125 hardcode = gctl_get_int(req, "hardcode");
128 * Clear last sector first to spoil all components if device exists.
130 for (i = 1; i < nargs; i++) {
131 name = gctl_get_ascii(req, "arg%d", i);
132 error = g_metadata_clear(name, NULL);
134 gctl_error(req, "Can't store metadata on %s: %s.", name,
140 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
141 md.md_version = G_CONCAT_VERSION;
142 name = gctl_get_ascii(req, "arg0");
143 strlcpy(md.md_name, name, sizeof(md.md_name));
144 md.md_id = arc4random();
145 md.md_all = nargs - 1;
148 * Ok, store metadata.
150 for (i = 1; i < nargs; i++) {
151 name = gctl_get_ascii(req, "arg%d", i);
154 bzero(md.md_provider, sizeof(md.md_provider));
156 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
157 name += strlen(_PATH_DEV);
158 strlcpy(md.md_provider, name, sizeof(md.md_provider));
160 md.md_provsize = g_get_mediasize(name);
161 if (md.md_provsize == 0) {
162 fprintf(stderr, "Can't get mediasize of %s: %s.\n",
163 name, strerror(errno));
164 gctl_error(req, "Not fully done.");
167 concat_metadata_encode(&md, sector);
168 error = g_metadata_store(name, sector, sizeof(sector));
170 fprintf(stderr, "Can't store metadata on %s: %s.\n",
171 name, strerror(error));
172 gctl_error(req, "Not fully done.");
176 printf("Metadata value stored on %s.\n", name);
181 concat_clear(struct gctl_req *req)
186 nargs = gctl_get_int(req, "nargs");
188 gctl_error(req, "Too few arguments.");
192 for (i = 0; i < nargs; i++) {
193 name = gctl_get_ascii(req, "arg%d", i);
194 error = g_metadata_clear(name, G_CONCAT_MAGIC);
196 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
197 name, strerror(error));
198 gctl_error(req, "Not fully done.");
202 printf("Metadata cleared on %s.\n", name);
207 concat_metadata_dump(const struct g_concat_metadata *md)
210 printf(" Magic string: %s\n", md->md_magic);
211 printf(" Metadata version: %u\n", (u_int)md->md_version);
212 printf(" Device name: %s\n", md->md_name);
213 printf(" Device ID: %u\n", (u_int)md->md_id);
214 printf(" Disk number: %u\n", (u_int)md->md_no);
215 printf("Total number of disks: %u\n", (u_int)md->md_all);
216 printf(" Hardcoded provider: %s\n", md->md_provider);
220 concat_dump(struct gctl_req *req)
222 struct g_concat_metadata md, tmpmd;
226 nargs = gctl_get_int(req, "nargs");
228 gctl_error(req, "Too few arguments.");
232 for (i = 0; i < nargs; i++) {
233 name = gctl_get_ascii(req, "arg%d", i);
234 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
237 fprintf(stderr, "Can't read metadata from %s: %s.\n",
238 name, strerror(error));
239 gctl_error(req, "Not fully done.");
242 concat_metadata_decode((u_char *)&tmpmd, &md);
243 printf("Metadata on %s:\n", name);
244 concat_metadata_dump(&md);