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>
42 #include <geom/shsec/g_shsec.h>
44 #include "core/geom.h"
45 #include "misc/subr.h"
48 uint32_t lib_version = G_LIB_VERSION;
49 uint32_t version = G_SHSEC_VERSION;
51 static void shsec_main(struct gctl_req *req, unsigned flags);
52 static void shsec_clear(struct gctl_req *req);
53 static void shsec_dump(struct gctl_req *req);
54 static void shsec_label(struct gctl_req *req);
56 struct g_command class_commands[] = {
57 { "clear", G_FLAG_VERBOSE, shsec_main, G_NULL_OPTS,
60 { "dump", 0, shsec_main, G_NULL_OPTS,
63 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, shsec_main,
65 { 'h', "hardcode", NULL, G_TYPE_BOOL },
68 "[-hv] name prov prov ..."
70 { "stop", G_FLAG_VERBOSE, NULL,
72 { 'f', "force", NULL, G_TYPE_BOOL },
80 static int verbose = 0;
83 shsec_main(struct gctl_req *req, unsigned flags)
87 if ((flags & G_FLAG_VERBOSE) != 0)
90 name = gctl_get_ascii(req, "verb");
92 gctl_error(req, "No '%s' argument.", "verb");
95 if (strcmp(name, "label") == 0)
97 else if (strcmp(name, "clear") == 0)
99 else if (strcmp(name, "dump") == 0)
102 gctl_error(req, "Unknown command: %s.", name);
106 shsec_label(struct gctl_req *req)
108 struct g_shsec_metadata md;
109 off_t compsize, msize;
111 unsigned ssize, secsize;
113 int error, i, nargs, hardcode;
115 bzero(sector, sizeof(sector));
116 nargs = gctl_get_int(req, "nargs");
118 gctl_error(req, "Too few arguments.");
121 hardcode = gctl_get_int(req, "hardcode");
124 * Clear last sector first to spoil all components if device exists.
128 for (i = 1; i < nargs; i++) {
129 name = gctl_get_ascii(req, "arg%d", i);
130 msize = g_get_mediasize(name);
131 ssize = g_get_sectorsize(name);
132 if (msize == 0 || ssize == 0) {
133 gctl_error(req, "Can't get informations about %s: %s.",
134 name, strerror(errno));
138 if (compsize == 0 || (compsize > 0 && msize < compsize))
143 secsize = g_lcm(secsize, ssize);
145 error = g_metadata_clear(name, NULL);
147 gctl_error(req, "Can't store metadata on %s: %s.", name,
153 strlcpy(md.md_magic, G_SHSEC_MAGIC, sizeof(md.md_magic));
154 md.md_version = G_SHSEC_VERSION;
155 name = gctl_get_ascii(req, "arg0");
156 strlcpy(md.md_name, name, sizeof(md.md_name));
157 md.md_id = arc4random();
158 md.md_all = nargs - 1;
161 * Ok, store metadata.
163 for (i = 1; i < nargs; i++) {
164 name = gctl_get_ascii(req, "arg%d", i);
165 msize = g_get_mediasize(name);
166 ssize = g_get_sectorsize(name);
167 if (compsize < msize - ssize) {
169 "warning: %s: only %jd bytes from %jd bytes used.\n",
170 name, (intmax_t)compsize, (intmax_t)(msize - ssize));
174 md.md_provsize = msize;
176 bzero(md.md_provider, sizeof(md.md_provider));
178 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
179 name += sizeof(_PATH_DEV) - 1;
180 strlcpy(md.md_provider, name, sizeof(md.md_provider));
182 shsec_metadata_encode(&md, sector);
183 error = g_metadata_store(name, sector, sizeof(sector));
185 fprintf(stderr, "Can't store metadata on %s: %s.\n",
186 name, strerror(error));
187 gctl_error(req, "Not fully done.");
191 printf("Metadata value stored on %s.\n", name);
196 shsec_clear(struct gctl_req *req)
201 nargs = gctl_get_int(req, "nargs");
203 gctl_error(req, "Too few arguments.");
207 for (i = 0; i < nargs; i++) {
208 name = gctl_get_ascii(req, "arg%d", i);
209 error = g_metadata_clear(name, G_SHSEC_MAGIC);
211 fprintf(stderr, "Can't clear metadata on %s: %s.\n",
212 name, strerror(error));
213 gctl_error(req, "Not fully done.");
217 printf("Metadata cleared on %s.\n", name);
222 shsec_metadata_dump(const struct g_shsec_metadata *md)
225 printf(" Magic string: %s\n", md->md_magic);
226 printf(" Metadata version: %u\n", (u_int)md->md_version);
227 printf(" Device name: %s\n", md->md_name);
228 printf(" Device ID: %u\n", (u_int)md->md_id);
229 printf(" Disk number: %u\n", (u_int)md->md_no);
230 printf("Total number of disks: %u\n", (u_int)md->md_all);
231 printf(" Hardcoded provider: %s\n", md->md_provider);
235 shsec_dump(struct gctl_req *req)
237 struct g_shsec_metadata md, tmpmd;
241 nargs = gctl_get_int(req, "nargs");
243 gctl_error(req, "Too few arguments.");
247 for (i = 0; i < nargs; i++) {
248 name = gctl_get_ascii(req, "arg%d", i);
249 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
252 fprintf(stderr, "Can't read metadata from %s: %s.\n",
253 name, strerror(error));
254 gctl_error(req, "Not fully done.");
257 shsec_metadata_decode((u_char *)&tmpmd, &md);
258 printf("Metadata on %s:\n", name);
259 shsec_metadata_dump(&md);