geom_label.c revision 131476
1/*-
2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sbin/geom/class/label/geom_label.c 131476 2004-07-02 19:40:36Z pjd $");
29
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
44uint32_t lib_version = G_LIB_VERSION;
45uint32_t version = G_LABEL_VERSION;
46
47static void label_main(struct gctl_req *req, unsigned flags);
48static void label_label(struct gctl_req *req);
49static void label_clear(struct gctl_req *req);
50
51struct g_command class_commands[] = {
52	{ "create", G_FLAG_VERBOSE | G_FLAG_LOADKLD, NULL, G_NULL_OPTS },
53	{ "destroy", G_FLAG_VERBOSE, NULL,
54	    {
55		{ 'f', "force", NULL, G_TYPE_NONE },
56		G_OPT_SENTINEL
57	    }
58	},
59	{ "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, label_main, G_NULL_OPTS },
60	{ "clear", G_FLAG_VERBOSE, label_main, G_NULL_OPTS },
61	G_CMD_SENTINEL
62};
63
64static int verbose = 0;
65
66
67void usage(const char *name);
68void
69usage(const char *name)
70{
71
72	fprintf(stderr, "usage: %s create [-v] <name> <dev>\n", name);
73	fprintf(stderr, "       %s destroy [-fv] <name> [name2 [...]]\n", name);
74	fprintf(stderr, "       %s label [-v] <name> <dev>\n", name);
75	fprintf(stderr, "       %s clear [-v] <dev1> [dev2 [...]]\n", name);
76}
77
78static void
79label_main(struct gctl_req *req, unsigned flags)
80{
81	const char *name;
82
83	if ((flags & G_FLAG_VERBOSE) != 0)
84		verbose = 1;
85
86	name = gctl_get_asciiparam(req, "verb");
87	if (name == NULL) {
88		gctl_error(req, "No '%s' argument.", "verb");
89		return;
90	}
91	if (strcmp(name, "label") == 0)
92		label_label(req);
93	else if (strcmp(name, "clear") == 0)
94		label_clear(req);
95	else
96		gctl_error(req, "Unknown command: %s.", name);
97}
98
99static void
100label_label(struct gctl_req *req)
101{
102	struct g_label_metadata md;
103	const char *name, *label;
104	u_char sector[512];
105	int *nargs, error;
106
107	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
108	if (nargs == NULL) {
109		gctl_error(req, "No '%s' argument.", "nargs");
110		return;
111	}
112	if (*nargs != 2) {
113		gctl_error(req, "Invalid number of arguments.");
114		return;
115	}
116
117	/*
118	 * Clear last sector first to spoil all components if device exists.
119	 */
120	name = gctl_get_asciiparam(req, "arg1");
121	if (name == NULL) {
122		gctl_error(req, "No 'arg%u' argument.", 1);
123		return;
124	}
125	error = g_metadata_clear(name, NULL);
126	if (error != 0) {
127		gctl_error(req, "Can't store metadata on %s: %s.", name,
128		    strerror(error));
129		return;
130	}
131
132	strlcpy(md.md_magic, G_LABEL_MAGIC, sizeof(md.md_magic));
133	md.md_version = G_LABEL_VERSION;
134	label = gctl_get_asciiparam(req, "arg0");
135	if (label == NULL) {
136		gctl_error(req, "No 'arg%u' argument.", 0);
137		return;
138	}
139	strlcpy(md.md_label, label, sizeof(md.md_label));
140
141	/*
142	 * Ok, store metadata.
143	 */
144	label_metadata_encode(&md, sector);
145	error = g_metadata_store(name, sector, sizeof(sector));
146	if (error != 0) {
147		fprintf(stderr, "Can't store metadata on %s: %s.\n", name,
148		    strerror(error));
149		gctl_error(req, "Not done.");
150	}
151	if (verbose)
152		printf("Metadata value stored on %s.\n", name);
153}
154
155static void
156label_clear(struct gctl_req *req)
157{
158	const char *name;
159	char param[16];
160	unsigned i;
161	int *nargs, error;
162
163	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
164	if (nargs == NULL) {
165		gctl_error(req, "No '%s' argument.", "nargs");
166		return;
167	}
168	if (*nargs < 1) {
169		gctl_error(req, "Too few arguments.");
170		return;
171	}
172
173	for (i = 0; i < (unsigned)*nargs; i++) {
174		snprintf(param, sizeof(param), "arg%u", i);
175		name = gctl_get_asciiparam(req, param);
176
177		error = g_metadata_clear(name, G_LABEL_MAGIC);
178		if (error != 0) {
179			fprintf(stderr, "Can't clear metadata on %s: %s.\n",
180			    name, strerror(error));
181			gctl_error(req, "Not fully done.");
182			continue;
183		}
184		if (verbose)
185			printf("Metadata cleared on %s.\n", name);
186	}
187}
188