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>
40 #include <geom/shsec/g_shsec.h>
42 #include "core/geom.h"
43 #include "misc/subr.h"
46 uint32_t lib_version = G_LIB_VERSION;
47 uint32_t version = G_SHSEC_VERSION;
49 static void shsec_main(struct gctl_req *req, unsigned flags);
50 static void shsec_clear(struct gctl_req *req);
51 static void shsec_dump(struct gctl_req *req);
52 static void shsec_label(struct gctl_req *req);
54 struct g_command class_commands[] = {
55 { "clear", G_FLAG_VERBOSE, shsec_main, G_NULL_OPTS,
58 { "dump", 0, shsec_main, G_NULL_OPTS,
61 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, shsec_main,
63 { 'h', "hardcode", NULL, G_TYPE_BOOL },
66 "[-hv] name prov prov ..."
68 { "stop", G_FLAG_VERBOSE, NULL,
70 { 'f', "force", NULL, G_TYPE_BOOL },
78 static int verbose = 0;
81 shsec_main(struct gctl_req *req, unsigned flags)
85 if ((flags & G_FLAG_VERBOSE) != 0)
88 name = gctl_get_ascii(req, "verb");
90 gctl_error(req, "No '%s' argument.", "verb");
93 if (strcmp(name, "label") == 0)
95 else if (strcmp(name, "clear") == 0)
97 else if (strcmp(name, "dump") == 0)
100 gctl_error(req, "Unknown command: %s.", name);
104 shsec_label(struct gctl_req *req)
106 struct g_shsec_metadata md;
107 off_t compsize, msize;
109 unsigned ssize, secsize;
111 int error, i, nargs, hardcode;
113 nargs = gctl_get_int(req, "nargs");
115 gctl_error(req, "Too few arguments.");
118 hardcode = gctl_get_int(req, "hardcode");
121 * Clear last sector first to spoil all components if device exists.
125 for (i = 1; i < nargs; i++) {
126 name = gctl_get_ascii(req, "arg%d", i);
127 msize = g_get_mediasize(name);
128 ssize = g_get_sectorsize(name);
129 if (msize == 0 || ssize == 0) {
130 gctl_error(req, "Can't get informations about %s: %s.",
131 name, strerror(errno));
135 if (compsize == 0 || (compsize > 0 && msize < compsize))
140 secsize = g_lcm(secsize, ssize);
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_SHSEC_MAGIC, sizeof(md.md_magic));
151 md.md_version = G_SHSEC_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);
162 msize = g_get_mediasize(name);
163 ssize = g_get_sectorsize(name);
164 if (compsize < msize - ssize) {
166 "warning: %s: only %jd bytes from %jd bytes used.\n",
167 name, (intmax_t)compsize, (intmax_t)(msize - ssize));
171 md.md_provsize = msize;
173 bzero(md.md_provider, sizeof(md.md_provider));
175 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
176 name += sizeof(_PATH_DEV) - 1;
177 strlcpy(md.md_provider, name, sizeof(md.md_provider));
179 shsec_metadata_encode(&md, sector);
180 error = g_metadata_store(name, sector, sizeof(sector));
182 fprintf(stderr, "Can't store metadata on %s: %s.\n",
183 name, strerror(error));
184 gctl_error(req, "Not fully done.");
188 printf("Metadata value stored on %s.\n", name);
193 shsec_clear(struct gctl_req *req)
198 nargs = gctl_get_int(req, "nargs");
200 gctl_error(req, "Too few arguments.");
204 for (i = 0; i < nargs; i++) {
205 name = gctl_get_ascii(req, "arg%d", i);
206 error = g_metadata_clear(name, G_SHSEC_MAGIC);
208 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
209 name, strerror(error));
210 gctl_error(req, "Not fully done.");
214 printf("Metadata cleared on %s.\n", name);
219 shsec_metadata_dump(const struct g_shsec_metadata *md)
222 printf(" Magic string: %s\n", md->md_magic);
223 printf(" Metadata version: %u\n", (u_int)md->md_version);
224 printf(" Device name: %s\n", md->md_name);
225 printf(" Device ID: %u\n", (u_int)md->md_id);
226 printf(" Disk number: %u\n", (u_int)md->md_no);
227 printf("Total number of disks: %u\n", (u_int)md->md_all);
228 printf(" Hardcoded provider: %s\n", md->md_provider);
232 shsec_dump(struct gctl_req *req)
234 struct g_shsec_metadata md, tmpmd;
238 nargs = gctl_get_int(req, "nargs");
240 gctl_error(req, "Too few arguments.");
244 for (i = 0; i < nargs; i++) {
245 name = gctl_get_ascii(req, "arg%d", i);
246 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
249 fprintf(stderr, "Can't read metadata from %s: %s.\n",
250 name, strerror(error));
251 gctl_error(req, "Not fully done.");
254 shsec_metadata_decode((u_char *)&tmpmd, &md);
255 printf("Metadata on %s:\n", name);
256 shsec_metadata_dump(&md);