xref: /freebsd/lib/geom/label/geom_label.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <assert.h>
37 #include <libgeom.h>
38 #include <geom/label/g_label.h>
39 
40 #include "core/geom.h"
41 #include "misc/subr.h"
42 
43 #ifdef STATIC_GEOM_CLASSES
44 #define PUBSYM(x)	glabel_##x
45 #else
46 #define PUBSYM(x)	x
47 #endif
48 
49 uint32_t PUBSYM(lib_version) = G_LIB_VERSION;
50 uint32_t PUBSYM(version) = G_LABEL_VERSION;
51 
52 static void label_main(struct gctl_req *req, unsigned flags);
53 static void label_clear(struct gctl_req *req);
54 static void label_dump(struct gctl_req *req);
55 static void label_label(struct gctl_req *req);
56 static void label_refresh(struct gctl_req *req);
57 
58 struct g_command PUBSYM(class_commands)[] = {
59 	{ "clear", G_FLAG_VERBOSE, label_main, G_NULL_OPTS,
60 	    "[-v] dev ..."
61 	},
62 	{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS,
63 	    "[-v] name dev"
64 	},
65 	{ "destroy", G_FLAG_VERBOSE, NULL,
66 	    {
67 		{ 'f', "force", NULL, G_TYPE_BOOL },
68 		G_OPT_SENTINEL
69 	    },
70 	    "[-fv] name ..."
71 	},
72 	{ "dump", 0, label_main, G_NULL_OPTS,
73 	    "dev ..."
74 	},
75 	{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, label_main, G_NULL_OPTS,
76 	    "[-v] name dev"
77 	},
78 	{ "refresh", 0, label_main, G_NULL_OPTS,
79 	    "dev ..."
80 	},
81 	{ "stop", G_FLAG_VERBOSE, NULL,
82 	    {
83 		{ 'f', "force", NULL, G_TYPE_BOOL },
84 		G_OPT_SENTINEL
85 	    },
86 	    "[-fv] name ..."
87 	},
88 	G_CMD_SENTINEL
89 };
90 
91 static int verbose = 0;
92 
93 static void
94 label_main(struct gctl_req *req, unsigned flags)
95 {
96 	const char *name;
97 
98 	if ((flags & G_FLAG_VERBOSE) != 0)
99 		verbose = 1;
100 
101 	name = gctl_get_ascii(req, "verb");
102 	if (name == NULL) {
103 		gctl_error(req, "No '%s' argument.", "verb");
104 		return;
105 	}
106 	if (strcmp(name, "label") == 0)
107 		label_label(req);
108 	else if (strcmp(name, "clear") == 0)
109 		label_clear(req);
110 	else if (strcmp(name, "dump") == 0)
111 		label_dump(req);
112 	else if (strcmp(name, "refresh") == 0)
113 		label_refresh(req);
114 	else
115 		gctl_error(req, "Unknown command: %s.", name);
116 }
117 
118 static void
119 label_label(struct gctl_req *req)
120 {
121 	struct g_label_metadata md;
122 	const char *name, *label;
123 	u_char sector[512];
124 	int error, nargs;
125 
126 	bzero(sector, sizeof(sector));
127 	nargs = gctl_get_int(req, "nargs");
128 	if (nargs != 2) {
129 		gctl_error(req, "Invalid number of arguments.");
130 		return;
131 	}
132 
133 	/*
134 	 * Clear last sector first to spoil all components if device exists.
135 	 */
136 	name = gctl_get_ascii(req, "arg1");
137 	error = g_metadata_clear(name, NULL);
138 	if (error != 0) {
139 		gctl_error(req, "Can't store metadata on %s: %s.", name,
140 		    strerror(error));
141 		return;
142 	}
143 
144 	strlcpy(md.md_magic, G_LABEL_MAGIC, sizeof(md.md_magic));
145 	md.md_version = G_LABEL_VERSION;
146 	label = gctl_get_ascii(req, "arg0");
147 	bzero(md.md_label, sizeof(md.md_label));
148 	strlcpy(md.md_label, label, sizeof(md.md_label));
149 	md.md_provsize = g_get_mediasize(name);
150 	if (md.md_provsize == 0) {
151 		gctl_error(req, "Can't get mediasize of %s: %s.", name,
152 		    strerror(errno));
153 		return;
154 	}
155 
156 	/*
157 	 * Ok, store metadata.
158 	 */
159 	label_metadata_encode(&md, sector);
160 	error = g_metadata_store(name, sector, sizeof(sector));
161 	if (error != 0) {
162 		fprintf(stderr, "Can't store metadata on %s: %s.\n", name,
163 		    strerror(error));
164 		gctl_error(req, "Not done.");
165 	}
166 	if (verbose)
167 		printf("Metadata value stored on %s.\n", name);
168 }
169 
170 static void
171 label_clear(struct gctl_req *req)
172 {
173 	const char *name;
174 	int error, i, nargs;
175 
176 	nargs = gctl_get_int(req, "nargs");
177 	if (nargs < 1) {
178 		gctl_error(req, "Too few arguments.");
179 		return;
180 	}
181 
182 	for (i = 0; i < nargs; i++) {
183 		name = gctl_get_ascii(req, "arg%d", i);
184 		error = g_metadata_clear(name, G_LABEL_MAGIC);
185 		if (error != 0) {
186 			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
187 			    name, strerror(error));
188 			gctl_error(req, "Not fully done.");
189 			continue;
190 		}
191 		if (verbose)
192 			printf("Metadata cleared on %s.\n", name);
193 	}
194 }
195 
196 static void
197 label_metadata_dump(const struct g_label_metadata *md)
198 {
199 
200 	printf("    Magic string: %s\n", md->md_magic);
201 	printf("Metadata version: %u\n", (u_int)md->md_version);
202 	printf("           Label: %s\n", md->md_label);
203 }
204 
205 static void
206 label_dump(struct gctl_req *req)
207 {
208 	struct g_label_metadata md, tmpmd;
209 	const char *name;
210 	int error, i, nargs;
211 
212 	nargs = gctl_get_int(req, "nargs");
213 	if (nargs < 1) {
214 		gctl_error(req, "Too few arguments.");
215 		return;
216 	}
217 
218 	for (i = 0; i < nargs; i++) {
219 		name = gctl_get_ascii(req, "arg%d", i);
220 		error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd),
221 		    G_LABEL_MAGIC);
222 		if (error != 0) {
223 			fprintf(stderr, "Can't read metadata from %s: %s.\n",
224 			    name, strerror(error));
225 			gctl_error(req, "Not fully done.");
226 			continue;
227 		}
228 		label_metadata_decode((u_char *)&tmpmd, &md);
229 		printf("Metadata on %s:\n", name);
230 		label_metadata_dump(&md);
231 		printf("\n");
232 	}
233 }
234 
235 static void
236 label_refresh(struct gctl_req *req)
237 {
238 	const char *name;
239 	int i, nargs, fd;
240 
241 	nargs = gctl_get_int(req, "nargs");
242 	if (nargs < 1) {
243 		gctl_error(req, "Too few arguments.");
244 		return;
245 	}
246 
247 	for (i = 0; i < nargs; i++) {
248 		name = gctl_get_ascii(req, "arg%d", i);
249 		fd = g_open(name, 1);
250 		if (fd == -1) {
251 			printf("Can't refresh metadata from %s: %s.\n",
252 			    name, strerror(errno));
253 		} else {
254 			printf("Metadata from %s refreshed.\n", name);
255 			(void)g_close(fd);
256 		}
257 	}
258 }
259